blob: 568cebea05bc1720c78350b220cff00b98cba1a0 [file] [log] [blame]
dea31012005-04-17 16:05:31 -05001/*******************************************************************
2 * This file is part of the Emulex Linux Device Driver for *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04003 * Fibre Channel Host Bus Adapters. *
James Smartbdcd2b92012-03-01 22:33:52 -05004 * Copyright (C) 2004-2012 Emulex. All rights reserved. *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04005 * EMULEX and SLI are trademarks of Emulex. *
dea31012005-04-17 16:05:31 -05006 * www.emulex.com *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04007 * Portions Copyright (C) 2004-2005 Christoph Hellwig *
dea31012005-04-17 16:05:31 -05008 * *
9 * This program is free software; you can redistribute it and/or *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -040010 * modify it under the terms of version 2 of the GNU General *
11 * Public License as published by the Free Software Foundation. *
12 * This program is distributed in the hope that it will be useful. *
13 * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND *
14 * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, *
15 * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE *
16 * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
17 * TO BE LEGALLY INVALID. See the GNU General Public License for *
18 * more details, a copy of which can be found in the file COPYING *
19 * included with this package. *
dea31012005-04-17 16:05:31 -050020 *******************************************************************/
21
dea31012005-04-17 16:05:31 -050022#include <linux/blkdev.h>
23#include <linux/pci.h>
24#include <linux/interrupt.h>
25#include <linux/delay.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090026#include <linux/slab.h>
dea31012005-04-17 16:05:31 -050027
James.Smart@Emulex.Com91886522005-08-10 15:03:09 -040028#include <scsi/scsi.h>
dea31012005-04-17 16:05:31 -050029#include <scsi/scsi_cmnd.h>
30#include <scsi/scsi_device.h>
31#include <scsi/scsi_host.h>
James.Smart@Emulex.Comf888ba32005-08-10 15:03:01 -040032#include <scsi/scsi_transport_fc.h>
James Smartda0436e2009-05-22 14:51:39 -040033#include <scsi/fc/fc_fs.h>
James Smart0d878412009-10-02 15:16:56 -040034#include <linux/aer.h>
dea31012005-04-17 16:05:31 -050035
James Smartda0436e2009-05-22 14:51:39 -040036#include "lpfc_hw4.h"
dea31012005-04-17 16:05:31 -050037#include "lpfc_hw.h"
38#include "lpfc_sli.h"
James Smartda0436e2009-05-22 14:51:39 -040039#include "lpfc_sli4.h"
James Smartea2151b2008-09-07 11:52:10 -040040#include "lpfc_nl.h"
dea31012005-04-17 16:05:31 -050041#include "lpfc_disc.h"
42#include "lpfc_scsi.h"
43#include "lpfc.h"
44#include "lpfc_crtn.h"
45#include "lpfc_logmsg.h"
46#include "lpfc_compat.h"
James Smart858c9f62007-06-17 19:56:39 -050047#include "lpfc_debugfs.h"
James Smart04c68492009-05-22 14:52:52 -040048#include "lpfc_vport.h"
dea31012005-04-17 16:05:31 -050049
50/* There are only four IOCB completion types. */
51typedef enum _lpfc_iocb_type {
52 LPFC_UNKNOWN_IOCB,
53 LPFC_UNSOL_IOCB,
54 LPFC_SOL_IOCB,
55 LPFC_ABORT_IOCB
56} lpfc_iocb_type;
57
James Smart4f774512009-05-22 14:52:35 -040058
59/* Provide function prototypes local to this module. */
60static int lpfc_sli_issue_mbox_s4(struct lpfc_hba *, LPFC_MBOXQ_t *,
61 uint32_t);
62static int lpfc_sli4_read_rev(struct lpfc_hba *, LPFC_MBOXQ_t *,
James Smart45ed1192009-10-02 15:17:02 -040063 uint8_t *, uint32_t *);
64static struct lpfc_iocbq *lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *,
65 struct lpfc_iocbq *);
James Smart6669f9b2009-10-02 15:16:45 -040066static void lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *,
67 struct hbq_dmabuf *);
James Smart05580562011-05-24 11:40:48 -040068static int lpfc_sli4_fp_handle_wcqe(struct lpfc_hba *, struct lpfc_queue *,
69 struct lpfc_cqe *);
James Smart8a9d2e82012-05-09 21:16:12 -040070static int lpfc_sli4_post_els_sgl_list(struct lpfc_hba *, struct list_head *,
71 int);
James Smartba20c852012-08-03 12:36:52 -040072static void lpfc_sli4_hba_handle_eqe(struct lpfc_hba *, struct lpfc_eqe *,
73 uint32_t);
James Smart05580562011-05-24 11:40:48 -040074
James Smart4f774512009-05-22 14:52:35 -040075static IOCB_t *
76lpfc_get_iocb_from_iocbq(struct lpfc_iocbq *iocbq)
77{
78 return &iocbq->iocb;
79}
80
81/**
82 * lpfc_sli4_wq_put - Put a Work Queue Entry on an Work Queue
83 * @q: The Work Queue to operate on.
84 * @wqe: The work Queue Entry to put on the Work queue.
85 *
86 * This routine will copy the contents of @wqe to the next available entry on
87 * the @q. This function will then ring the Work Queue Doorbell to signal the
88 * HBA to start processing the Work Queue Entry. This function returns 0 if
89 * successful. If no entries are available on @q then this function will return
90 * -ENOMEM.
91 * The caller is expected to hold the hbalock when calling this routine.
92 **/
93static uint32_t
94lpfc_sli4_wq_put(struct lpfc_queue *q, union lpfc_wqe *wqe)
95{
James Smart2e90f4b2011-12-13 13:22:37 -050096 union lpfc_wqe *temp_wqe;
James Smart4f774512009-05-22 14:52:35 -040097 struct lpfc_register doorbell;
98 uint32_t host_index;
James Smart027140e2012-08-03 12:35:44 -040099 uint32_t idx;
James Smart4f774512009-05-22 14:52:35 -0400100
James Smart2e90f4b2011-12-13 13:22:37 -0500101 /* sanity check on queue memory */
102 if (unlikely(!q))
103 return -ENOMEM;
104 temp_wqe = q->qe[q->host_index].wqe;
105
James Smart4f774512009-05-22 14:52:35 -0400106 /* If the host has not yet processed the next entry then we are done */
James Smart027140e2012-08-03 12:35:44 -0400107 idx = ((q->host_index + 1) % q->entry_count);
108 if (idx == q->hba_index) {
James Smartb84daac2012-08-03 12:35:13 -0400109 q->WQ_overflow++;
James Smart4f774512009-05-22 14:52:35 -0400110 return -ENOMEM;
James Smartb84daac2012-08-03 12:35:13 -0400111 }
112 q->WQ_posted++;
James Smart4f774512009-05-22 14:52:35 -0400113 /* set consumption flag every once in a while */
James Smartff78d8f2011-12-13 13:21:35 -0500114 if (!((q->host_index + 1) % q->entry_repost))
James Smartf0d9bcc2010-10-22 11:07:09 -0400115 bf_set(wqe_wqec, &wqe->generic.wqe_com, 1);
James Smartfedd3b72011-02-16 12:39:24 -0500116 if (q->phba->sli3_options & LPFC_SLI4_PHWQ_ENABLED)
117 bf_set(wqe_wqid, &wqe->generic.wqe_com, q->queue_id);
James Smart4f774512009-05-22 14:52:35 -0400118 lpfc_sli_pcimem_bcopy(wqe, temp_wqe, q->entry_size);
119
120 /* Update the host index before invoking device */
121 host_index = q->host_index;
James Smart027140e2012-08-03 12:35:44 -0400122
123 q->host_index = idx;
James Smart4f774512009-05-22 14:52:35 -0400124
125 /* Ring Doorbell */
126 doorbell.word0 = 0;
James Smart962bc512013-01-03 15:44:00 -0500127 if (q->db_format == LPFC_DB_LIST_FORMAT) {
128 bf_set(lpfc_wq_db_list_fm_num_posted, &doorbell, 1);
129 bf_set(lpfc_wq_db_list_fm_index, &doorbell, host_index);
130 bf_set(lpfc_wq_db_list_fm_id, &doorbell, q->queue_id);
131 } else if (q->db_format == LPFC_DB_RING_FORMAT) {
132 bf_set(lpfc_wq_db_ring_fm_num_posted, &doorbell, 1);
133 bf_set(lpfc_wq_db_ring_fm_id, &doorbell, q->queue_id);
134 } else {
135 return -EINVAL;
136 }
137 writel(doorbell.word0, q->db_regaddr);
James Smart4f774512009-05-22 14:52:35 -0400138
139 return 0;
140}
141
142/**
143 * lpfc_sli4_wq_release - Updates internal hba index for WQ
144 * @q: The Work Queue to operate on.
145 * @index: The index to advance the hba index to.
146 *
147 * This routine will update the HBA index of a queue to reflect consumption of
148 * Work Queue Entries by the HBA. When the HBA indicates that it has consumed
149 * an entry the host calls this function to update the queue's internal
150 * pointers. This routine returns the number of entries that were consumed by
151 * the HBA.
152 **/
153static uint32_t
154lpfc_sli4_wq_release(struct lpfc_queue *q, uint32_t index)
155{
156 uint32_t released = 0;
157
James Smart2e90f4b2011-12-13 13:22:37 -0500158 /* sanity check on queue memory */
159 if (unlikely(!q))
160 return 0;
161
James Smart4f774512009-05-22 14:52:35 -0400162 if (q->hba_index == index)
163 return 0;
164 do {
165 q->hba_index = ((q->hba_index + 1) % q->entry_count);
166 released++;
167 } while (q->hba_index != index);
168 return released;
169}
170
171/**
172 * lpfc_sli4_mq_put - Put a Mailbox Queue Entry on an Mailbox Queue
173 * @q: The Mailbox Queue to operate on.
174 * @wqe: The Mailbox Queue Entry to put on the Work queue.
175 *
176 * This routine will copy the contents of @mqe to the next available entry on
177 * the @q. This function will then ring the Work Queue Doorbell to signal the
178 * HBA to start processing the Work Queue Entry. This function returns 0 if
179 * successful. If no entries are available on @q then this function will return
180 * -ENOMEM.
181 * The caller is expected to hold the hbalock when calling this routine.
182 **/
183static uint32_t
184lpfc_sli4_mq_put(struct lpfc_queue *q, struct lpfc_mqe *mqe)
185{
James Smart2e90f4b2011-12-13 13:22:37 -0500186 struct lpfc_mqe *temp_mqe;
James Smart4f774512009-05-22 14:52:35 -0400187 struct lpfc_register doorbell;
188 uint32_t host_index;
189
James Smart2e90f4b2011-12-13 13:22:37 -0500190 /* sanity check on queue memory */
191 if (unlikely(!q))
192 return -ENOMEM;
193 temp_mqe = q->qe[q->host_index].mqe;
194
James Smart4f774512009-05-22 14:52:35 -0400195 /* If the host has not yet processed the next entry then we are done */
196 if (((q->host_index + 1) % q->entry_count) == q->hba_index)
197 return -ENOMEM;
198 lpfc_sli_pcimem_bcopy(mqe, temp_mqe, q->entry_size);
199 /* Save off the mailbox pointer for completion */
200 q->phba->mbox = (MAILBOX_t *)temp_mqe;
201
202 /* Update the host index before invoking device */
203 host_index = q->host_index;
204 q->host_index = ((q->host_index + 1) % q->entry_count);
205
206 /* Ring Doorbell */
207 doorbell.word0 = 0;
208 bf_set(lpfc_mq_doorbell_num_posted, &doorbell, 1);
209 bf_set(lpfc_mq_doorbell_id, &doorbell, q->queue_id);
210 writel(doorbell.word0, q->phba->sli4_hba.MQDBregaddr);
James Smart4f774512009-05-22 14:52:35 -0400211 return 0;
212}
213
214/**
215 * lpfc_sli4_mq_release - Updates internal hba index for MQ
216 * @q: The Mailbox Queue to operate on.
217 *
218 * This routine will update the HBA index of a queue to reflect consumption of
219 * a Mailbox Queue Entry by the HBA. When the HBA indicates that it has consumed
220 * an entry the host calls this function to update the queue's internal
221 * pointers. This routine returns the number of entries that were consumed by
222 * the HBA.
223 **/
224static uint32_t
225lpfc_sli4_mq_release(struct lpfc_queue *q)
226{
James Smart2e90f4b2011-12-13 13:22:37 -0500227 /* sanity check on queue memory */
228 if (unlikely(!q))
229 return 0;
230
James Smart4f774512009-05-22 14:52:35 -0400231 /* Clear the mailbox pointer for completion */
232 q->phba->mbox = NULL;
233 q->hba_index = ((q->hba_index + 1) % q->entry_count);
234 return 1;
235}
236
237/**
238 * lpfc_sli4_eq_get - Gets the next valid EQE from a EQ
239 * @q: The Event Queue to get the first valid EQE from
240 *
241 * This routine will get the first valid Event Queue Entry from @q, update
242 * the queue's internal hba index, and return the EQE. If no valid EQEs are in
243 * the Queue (no more work to do), or the Queue is full of EQEs that have been
244 * processed, but not popped back to the HBA then this routine will return NULL.
245 **/
246static struct lpfc_eqe *
247lpfc_sli4_eq_get(struct lpfc_queue *q)
248{
James Smart2e90f4b2011-12-13 13:22:37 -0500249 struct lpfc_eqe *eqe;
James Smart027140e2012-08-03 12:35:44 -0400250 uint32_t idx;
James Smart2e90f4b2011-12-13 13:22:37 -0500251
252 /* sanity check on queue memory */
253 if (unlikely(!q))
254 return NULL;
255 eqe = q->qe[q->hba_index].eqe;
James Smart4f774512009-05-22 14:52:35 -0400256
257 /* If the next EQE is not valid then we are done */
James Smartcb5172e2010-03-15 11:25:07 -0400258 if (!bf_get_le32(lpfc_eqe_valid, eqe))
James Smart4f774512009-05-22 14:52:35 -0400259 return NULL;
260 /* If the host has not yet processed the next entry then we are done */
James Smart027140e2012-08-03 12:35:44 -0400261 idx = ((q->hba_index + 1) % q->entry_count);
262 if (idx == q->host_index)
James Smart4f774512009-05-22 14:52:35 -0400263 return NULL;
264
James Smart027140e2012-08-03 12:35:44 -0400265 q->hba_index = idx;
James Smart4f774512009-05-22 14:52:35 -0400266 return eqe;
267}
268
269/**
James Smartba20c852012-08-03 12:36:52 -0400270 * lpfc_sli4_eq_clr_intr - Turn off interrupts from this EQ
271 * @q: The Event Queue to disable interrupts
272 *
273 **/
274static inline void
275lpfc_sli4_eq_clr_intr(struct lpfc_queue *q)
276{
277 struct lpfc_register doorbell;
278
279 doorbell.word0 = 0;
280 bf_set(lpfc_eqcq_doorbell_eqci, &doorbell, 1);
281 bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_EVENT);
282 bf_set(lpfc_eqcq_doorbell_eqid_hi, &doorbell,
283 (q->queue_id >> LPFC_EQID_HI_FIELD_SHIFT));
284 bf_set(lpfc_eqcq_doorbell_eqid_lo, &doorbell, q->queue_id);
285 writel(doorbell.word0, q->phba->sli4_hba.EQCQDBregaddr);
286}
287
288/**
James Smart4f774512009-05-22 14:52:35 -0400289 * lpfc_sli4_eq_release - Indicates the host has finished processing an EQ
290 * @q: The Event Queue that the host has completed processing for.
291 * @arm: Indicates whether the host wants to arms this CQ.
292 *
293 * This routine will mark all Event Queue Entries on @q, from the last
294 * known completed entry to the last entry that was processed, as completed
295 * by clearing the valid bit for each completion queue entry. Then it will
296 * notify the HBA, by ringing the doorbell, that the EQEs have been processed.
297 * The internal host index in the @q will be updated by this routine to indicate
298 * that the host has finished processing the entries. The @arm parameter
299 * indicates that the queue should be rearmed when ringing the doorbell.
300 *
301 * This function will return the number of EQEs that were popped.
302 **/
303uint32_t
304lpfc_sli4_eq_release(struct lpfc_queue *q, bool arm)
305{
306 uint32_t released = 0;
307 struct lpfc_eqe *temp_eqe;
308 struct lpfc_register doorbell;
309
James Smart2e90f4b2011-12-13 13:22:37 -0500310 /* sanity check on queue memory */
311 if (unlikely(!q))
312 return 0;
313
James Smart4f774512009-05-22 14:52:35 -0400314 /* while there are valid entries */
315 while (q->hba_index != q->host_index) {
316 temp_eqe = q->qe[q->host_index].eqe;
James Smartcb5172e2010-03-15 11:25:07 -0400317 bf_set_le32(lpfc_eqe_valid, temp_eqe, 0);
James Smart4f774512009-05-22 14:52:35 -0400318 released++;
319 q->host_index = ((q->host_index + 1) % q->entry_count);
320 }
321 if (unlikely(released == 0 && !arm))
322 return 0;
323
324 /* ring doorbell for number popped */
325 doorbell.word0 = 0;
326 if (arm) {
327 bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);
328 bf_set(lpfc_eqcq_doorbell_eqci, &doorbell, 1);
329 }
330 bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, released);
331 bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_EVENT);
James Smart6b5151f2012-01-18 16:24:06 -0500332 bf_set(lpfc_eqcq_doorbell_eqid_hi, &doorbell,
333 (q->queue_id >> LPFC_EQID_HI_FIELD_SHIFT));
334 bf_set(lpfc_eqcq_doorbell_eqid_lo, &doorbell, q->queue_id);
James Smart4f774512009-05-22 14:52:35 -0400335 writel(doorbell.word0, q->phba->sli4_hba.EQCQDBregaddr);
James Smarta747c9c2009-11-18 15:41:10 -0500336 /* PCI read to flush PCI pipeline on re-arming for INTx mode */
337 if ((q->phba->intr_type == INTx) && (arm == LPFC_QUEUE_REARM))
338 readl(q->phba->sli4_hba.EQCQDBregaddr);
James Smart4f774512009-05-22 14:52:35 -0400339 return released;
340}
341
342/**
343 * lpfc_sli4_cq_get - Gets the next valid CQE from a CQ
344 * @q: The Completion Queue to get the first valid CQE from
345 *
346 * This routine will get the first valid Completion Queue Entry from @q, update
347 * the queue's internal hba index, and return the CQE. If no valid CQEs are in
348 * the Queue (no more work to do), or the Queue is full of CQEs that have been
349 * processed, but not popped back to the HBA then this routine will return NULL.
350 **/
351static struct lpfc_cqe *
352lpfc_sli4_cq_get(struct lpfc_queue *q)
353{
354 struct lpfc_cqe *cqe;
James Smart027140e2012-08-03 12:35:44 -0400355 uint32_t idx;
James Smart4f774512009-05-22 14:52:35 -0400356
James Smart2e90f4b2011-12-13 13:22:37 -0500357 /* sanity check on queue memory */
358 if (unlikely(!q))
359 return NULL;
360
James Smart4f774512009-05-22 14:52:35 -0400361 /* If the next CQE is not valid then we are done */
James Smartcb5172e2010-03-15 11:25:07 -0400362 if (!bf_get_le32(lpfc_cqe_valid, q->qe[q->hba_index].cqe))
James Smart4f774512009-05-22 14:52:35 -0400363 return NULL;
364 /* If the host has not yet processed the next entry then we are done */
James Smart027140e2012-08-03 12:35:44 -0400365 idx = ((q->hba_index + 1) % q->entry_count);
366 if (idx == q->host_index)
James Smart4f774512009-05-22 14:52:35 -0400367 return NULL;
368
369 cqe = q->qe[q->hba_index].cqe;
James Smart027140e2012-08-03 12:35:44 -0400370 q->hba_index = idx;
James Smart4f774512009-05-22 14:52:35 -0400371 return cqe;
372}
373
374/**
375 * lpfc_sli4_cq_release - Indicates the host has finished processing a CQ
376 * @q: The Completion Queue that the host has completed processing for.
377 * @arm: Indicates whether the host wants to arms this CQ.
378 *
379 * This routine will mark all Completion queue entries on @q, from the last
380 * known completed entry to the last entry that was processed, as completed
381 * by clearing the valid bit for each completion queue entry. Then it will
382 * notify the HBA, by ringing the doorbell, that the CQEs have been processed.
383 * The internal host index in the @q will be updated by this routine to indicate
384 * that the host has finished processing the entries. The @arm parameter
385 * indicates that the queue should be rearmed when ringing the doorbell.
386 *
387 * This function will return the number of CQEs that were released.
388 **/
389uint32_t
390lpfc_sli4_cq_release(struct lpfc_queue *q, bool arm)
391{
392 uint32_t released = 0;
393 struct lpfc_cqe *temp_qe;
394 struct lpfc_register doorbell;
395
James Smart2e90f4b2011-12-13 13:22:37 -0500396 /* sanity check on queue memory */
397 if (unlikely(!q))
398 return 0;
James Smart4f774512009-05-22 14:52:35 -0400399 /* while there are valid entries */
400 while (q->hba_index != q->host_index) {
401 temp_qe = q->qe[q->host_index].cqe;
James Smartcb5172e2010-03-15 11:25:07 -0400402 bf_set_le32(lpfc_cqe_valid, temp_qe, 0);
James Smart4f774512009-05-22 14:52:35 -0400403 released++;
404 q->host_index = ((q->host_index + 1) % q->entry_count);
405 }
406 if (unlikely(released == 0 && !arm))
407 return 0;
408
409 /* ring doorbell for number popped */
410 doorbell.word0 = 0;
411 if (arm)
412 bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);
413 bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, released);
414 bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_COMPLETION);
James Smart6b5151f2012-01-18 16:24:06 -0500415 bf_set(lpfc_eqcq_doorbell_cqid_hi, &doorbell,
416 (q->queue_id >> LPFC_CQID_HI_FIELD_SHIFT));
417 bf_set(lpfc_eqcq_doorbell_cqid_lo, &doorbell, q->queue_id);
James Smart4f774512009-05-22 14:52:35 -0400418 writel(doorbell.word0, q->phba->sli4_hba.EQCQDBregaddr);
419 return released;
420}
421
422/**
423 * lpfc_sli4_rq_put - Put a Receive Buffer Queue Entry on a Receive Queue
424 * @q: The Header Receive Queue to operate on.
425 * @wqe: The Receive Queue Entry to put on the Receive queue.
426 *
427 * This routine will copy the contents of @wqe to the next available entry on
428 * the @q. This function will then ring the Receive Queue Doorbell to signal the
429 * HBA to start processing the Receive Queue Entry. This function returns the
430 * index that the rqe was copied to if successful. If no entries are available
431 * on @q then this function will return -ENOMEM.
432 * The caller is expected to hold the hbalock when calling this routine.
433 **/
434static int
435lpfc_sli4_rq_put(struct lpfc_queue *hq, struct lpfc_queue *dq,
436 struct lpfc_rqe *hrqe, struct lpfc_rqe *drqe)
437{
James Smart2e90f4b2011-12-13 13:22:37 -0500438 struct lpfc_rqe *temp_hrqe;
439 struct lpfc_rqe *temp_drqe;
James Smart4f774512009-05-22 14:52:35 -0400440 struct lpfc_register doorbell;
441 int put_index = hq->host_index;
442
James Smart2e90f4b2011-12-13 13:22:37 -0500443 /* sanity check on queue memory */
444 if (unlikely(!hq) || unlikely(!dq))
445 return -ENOMEM;
446 temp_hrqe = hq->qe[hq->host_index].rqe;
447 temp_drqe = dq->qe[dq->host_index].rqe;
448
James Smart4f774512009-05-22 14:52:35 -0400449 if (hq->type != LPFC_HRQ || dq->type != LPFC_DRQ)
450 return -EINVAL;
451 if (hq->host_index != dq->host_index)
452 return -EINVAL;
453 /* If the host has not yet processed the next entry then we are done */
454 if (((hq->host_index + 1) % hq->entry_count) == hq->hba_index)
455 return -EBUSY;
456 lpfc_sli_pcimem_bcopy(hrqe, temp_hrqe, hq->entry_size);
457 lpfc_sli_pcimem_bcopy(drqe, temp_drqe, dq->entry_size);
458
459 /* Update the host index to point to the next slot */
460 hq->host_index = ((hq->host_index + 1) % hq->entry_count);
461 dq->host_index = ((dq->host_index + 1) % dq->entry_count);
462
463 /* Ring The Header Receive Queue Doorbell */
James Smart73d91e52011-10-10 21:32:10 -0400464 if (!(hq->host_index % hq->entry_repost)) {
James Smart4f774512009-05-22 14:52:35 -0400465 doorbell.word0 = 0;
James Smart962bc512013-01-03 15:44:00 -0500466 if (hq->db_format == LPFC_DB_RING_FORMAT) {
467 bf_set(lpfc_rq_db_ring_fm_num_posted, &doorbell,
468 hq->entry_repost);
469 bf_set(lpfc_rq_db_ring_fm_id, &doorbell, hq->queue_id);
470 } else if (hq->db_format == LPFC_DB_LIST_FORMAT) {
471 bf_set(lpfc_rq_db_list_fm_num_posted, &doorbell,
472 hq->entry_repost);
473 bf_set(lpfc_rq_db_list_fm_index, &doorbell,
474 hq->host_index);
475 bf_set(lpfc_rq_db_list_fm_id, &doorbell, hq->queue_id);
476 } else {
477 return -EINVAL;
478 }
479 writel(doorbell.word0, hq->db_regaddr);
James Smart4f774512009-05-22 14:52:35 -0400480 }
481 return put_index;
482}
483
484/**
485 * lpfc_sli4_rq_release - Updates internal hba index for RQ
486 * @q: The Header Receive Queue to operate on.
487 *
488 * This routine will update the HBA index of a queue to reflect consumption of
489 * one Receive Queue Entry by the HBA. When the HBA indicates that it has
490 * consumed an entry the host calls this function to update the queue's
491 * internal pointers. This routine returns the number of entries that were
492 * consumed by the HBA.
493 **/
494static uint32_t
495lpfc_sli4_rq_release(struct lpfc_queue *hq, struct lpfc_queue *dq)
496{
James Smart2e90f4b2011-12-13 13:22:37 -0500497 /* sanity check on queue memory */
498 if (unlikely(!hq) || unlikely(!dq))
499 return 0;
500
James Smart4f774512009-05-22 14:52:35 -0400501 if ((hq->type != LPFC_HRQ) || (dq->type != LPFC_DRQ))
502 return 0;
503 hq->hba_index = ((hq->hba_index + 1) % hq->entry_count);
504 dq->hba_index = ((dq->hba_index + 1) % dq->entry_count);
505 return 1;
506}
507
James Smarte59058c2008-08-24 21:49:00 -0400508/**
James Smart3621a712009-04-06 18:47:14 -0400509 * lpfc_cmd_iocb - Get next command iocb entry in the ring
James Smarte59058c2008-08-24 21:49:00 -0400510 * @phba: Pointer to HBA context object.
511 * @pring: Pointer to driver SLI ring object.
512 *
513 * This function returns pointer to next command iocb entry
514 * in the command ring. The caller must hold hbalock to prevent
515 * other threads consume the next command iocb.
516 * SLI-2/SLI-3 provide different sized iocbs.
517 **/
James Smarted957682007-06-17 19:56:37 -0500518static inline IOCB_t *
519lpfc_cmd_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
520{
James Smart7e56aa22012-08-03 12:35:34 -0400521 return (IOCB_t *) (((char *) pring->sli.sli3.cmdringaddr) +
522 pring->sli.sli3.cmdidx * phba->iocb_cmd_size);
James Smarted957682007-06-17 19:56:37 -0500523}
524
James Smarte59058c2008-08-24 21:49:00 -0400525/**
James Smart3621a712009-04-06 18:47:14 -0400526 * lpfc_resp_iocb - Get next response iocb entry in the ring
James Smarte59058c2008-08-24 21:49:00 -0400527 * @phba: Pointer to HBA context object.
528 * @pring: Pointer to driver SLI ring object.
529 *
530 * This function returns pointer to next response iocb entry
531 * in the response ring. The caller must hold hbalock to make sure
532 * that no other thread consume the next response iocb.
533 * SLI-2/SLI-3 provide different sized iocbs.
534 **/
James Smarted957682007-06-17 19:56:37 -0500535static inline IOCB_t *
536lpfc_resp_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
537{
James Smart7e56aa22012-08-03 12:35:34 -0400538 return (IOCB_t *) (((char *) pring->sli.sli3.rspringaddr) +
539 pring->sli.sli3.rspidx * phba->iocb_rsp_size);
James Smarted957682007-06-17 19:56:37 -0500540}
541
James Smarte59058c2008-08-24 21:49:00 -0400542/**
James Smart3621a712009-04-06 18:47:14 -0400543 * __lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
James Smarte59058c2008-08-24 21:49:00 -0400544 * @phba: Pointer to HBA context object.
545 *
546 * This function is called with hbalock held. This function
547 * allocates a new driver iocb object from the iocb pool. If the
548 * allocation is successful, it returns pointer to the newly
549 * allocated iocb object else it returns NULL.
550 **/
James Smart4f2e66c2012-05-09 21:17:07 -0400551struct lpfc_iocbq *
James Smart2e0fef82007-06-17 19:56:36 -0500552__lpfc_sli_get_iocbq(struct lpfc_hba *phba)
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400553{
554 struct list_head *lpfc_iocb_list = &phba->lpfc_iocb_list;
555 struct lpfc_iocbq * iocbq = NULL;
556
557 list_remove_head(lpfc_iocb_list, iocbq, struct lpfc_iocbq, list);
James Smart2a9bf3d2010-06-07 15:24:45 -0400558 if (iocbq)
559 phba->iocb_cnt++;
560 if (phba->iocb_cnt > phba->iocb_max)
561 phba->iocb_max = phba->iocb_cnt;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400562 return iocbq;
563}
564
James Smarte59058c2008-08-24 21:49:00 -0400565/**
James Smartda0436e2009-05-22 14:51:39 -0400566 * __lpfc_clear_active_sglq - Remove the active sglq for this XRI.
567 * @phba: Pointer to HBA context object.
568 * @xritag: XRI value.
569 *
570 * This function clears the sglq pointer from the array of acive
571 * sglq's. The xritag that is passed in is used to index into the
572 * array. Before the xritag can be used it needs to be adjusted
573 * by subtracting the xribase.
574 *
575 * Returns sglq ponter = success, NULL = Failure.
576 **/
577static struct lpfc_sglq *
578__lpfc_clear_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
579{
James Smartda0436e2009-05-22 14:51:39 -0400580 struct lpfc_sglq *sglq;
James Smart6d368e52011-05-24 11:44:12 -0400581
582 sglq = phba->sli4_hba.lpfc_sglq_active_list[xritag];
583 phba->sli4_hba.lpfc_sglq_active_list[xritag] = NULL;
James Smartda0436e2009-05-22 14:51:39 -0400584 return sglq;
585}
586
587/**
588 * __lpfc_get_active_sglq - Get the active sglq for this XRI.
589 * @phba: Pointer to HBA context object.
590 * @xritag: XRI value.
591 *
592 * This function returns the sglq pointer from the array of acive
593 * sglq's. The xritag that is passed in is used to index into the
594 * array. Before the xritag can be used it needs to be adjusted
595 * by subtracting the xribase.
596 *
597 * Returns sglq ponter = success, NULL = Failure.
598 **/
James Smart0f65ff62010-02-26 14:14:23 -0500599struct lpfc_sglq *
James Smartda0436e2009-05-22 14:51:39 -0400600__lpfc_get_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
601{
James Smartda0436e2009-05-22 14:51:39 -0400602 struct lpfc_sglq *sglq;
James Smart6d368e52011-05-24 11:44:12 -0400603
604 sglq = phba->sli4_hba.lpfc_sglq_active_list[xritag];
James Smartda0436e2009-05-22 14:51:39 -0400605 return sglq;
606}
607
608/**
James Smart1151e3e2011-02-16 12:39:35 -0500609 * lpfc_clr_rrq_active - Clears RRQ active bit in xri_bitmap.
James Smart19ca7602010-11-20 23:11:55 -0500610 * @phba: Pointer to HBA context object.
611 * @xritag: xri used in this exchange.
612 * @rrq: The RRQ to be cleared.
613 *
James Smart19ca7602010-11-20 23:11:55 -0500614 **/
James Smart1151e3e2011-02-16 12:39:35 -0500615void
616lpfc_clr_rrq_active(struct lpfc_hba *phba,
617 uint16_t xritag,
618 struct lpfc_node_rrq *rrq)
James Smart19ca7602010-11-20 23:11:55 -0500619{
James Smart1151e3e2011-02-16 12:39:35 -0500620 struct lpfc_nodelist *ndlp = NULL;
James Smart19ca7602010-11-20 23:11:55 -0500621
James Smart1151e3e2011-02-16 12:39:35 -0500622 if ((rrq->vport) && NLP_CHK_NODE_ACT(rrq->ndlp))
623 ndlp = lpfc_findnode_did(rrq->vport, rrq->nlp_DID);
James Smart19ca7602010-11-20 23:11:55 -0500624
625 /* The target DID could have been swapped (cable swap)
626 * we should use the ndlp from the findnode if it is
627 * available.
628 */
James Smart1151e3e2011-02-16 12:39:35 -0500629 if ((!ndlp) && rrq->ndlp)
James Smart19ca7602010-11-20 23:11:55 -0500630 ndlp = rrq->ndlp;
631
James Smart1151e3e2011-02-16 12:39:35 -0500632 if (!ndlp)
633 goto out;
634
James Smart6d368e52011-05-24 11:44:12 -0400635 if (test_and_clear_bit(xritag, ndlp->active_rrqs.xri_bitmap)) {
James Smart19ca7602010-11-20 23:11:55 -0500636 rrq->send_rrq = 0;
637 rrq->xritag = 0;
638 rrq->rrq_stop_time = 0;
639 }
James Smart1151e3e2011-02-16 12:39:35 -0500640out:
James Smart19ca7602010-11-20 23:11:55 -0500641 mempool_free(rrq, phba->rrq_pool);
642}
643
644/**
645 * lpfc_handle_rrq_active - Checks if RRQ has waithed RATOV.
646 * @phba: Pointer to HBA context object.
647 *
648 * This function is called with hbalock held. This function
649 * Checks if stop_time (ratov from setting rrq active) has
650 * been reached, if it has and the send_rrq flag is set then
651 * it will call lpfc_send_rrq. If the send_rrq flag is not set
652 * then it will just call the routine to clear the rrq and
653 * free the rrq resource.
654 * The timer is set to the next rrq that is going to expire before
655 * leaving the routine.
656 *
657 **/
658void
659lpfc_handle_rrq_active(struct lpfc_hba *phba)
660{
661 struct lpfc_node_rrq *rrq;
662 struct lpfc_node_rrq *nextrrq;
663 unsigned long next_time;
664 unsigned long iflags;
James Smart1151e3e2011-02-16 12:39:35 -0500665 LIST_HEAD(send_rrq);
James Smart19ca7602010-11-20 23:11:55 -0500666
667 spin_lock_irqsave(&phba->hbalock, iflags);
668 phba->hba_flag &= ~HBA_RRQ_ACTIVE;
669 next_time = jiffies + HZ * (phba->fc_ratov + 1);
670 list_for_each_entry_safe(rrq, nextrrq,
James Smart1151e3e2011-02-16 12:39:35 -0500671 &phba->active_rrq_list, list) {
672 if (time_after(jiffies, rrq->rrq_stop_time))
673 list_move(&rrq->list, &send_rrq);
674 else if (time_before(rrq->rrq_stop_time, next_time))
James Smart19ca7602010-11-20 23:11:55 -0500675 next_time = rrq->rrq_stop_time;
676 }
677 spin_unlock_irqrestore(&phba->hbalock, iflags);
678 if (!list_empty(&phba->active_rrq_list))
679 mod_timer(&phba->rrq_tmr, next_time);
James Smart1151e3e2011-02-16 12:39:35 -0500680 list_for_each_entry_safe(rrq, nextrrq, &send_rrq, list) {
681 list_del(&rrq->list);
682 if (!rrq->send_rrq)
683 /* this call will free the rrq */
684 lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
685 else if (lpfc_send_rrq(phba, rrq)) {
686 /* if we send the rrq then the completion handler
687 * will clear the bit in the xribitmap.
688 */
689 lpfc_clr_rrq_active(phba, rrq->xritag,
690 rrq);
691 }
692 }
James Smart19ca7602010-11-20 23:11:55 -0500693}
694
695/**
696 * lpfc_get_active_rrq - Get the active RRQ for this exchange.
697 * @vport: Pointer to vport context object.
698 * @xri: The xri used in the exchange.
699 * @did: The targets DID for this exchange.
700 *
701 * returns NULL = rrq not found in the phba->active_rrq_list.
702 * rrq = rrq for this xri and target.
703 **/
704struct lpfc_node_rrq *
705lpfc_get_active_rrq(struct lpfc_vport *vport, uint16_t xri, uint32_t did)
706{
707 struct lpfc_hba *phba = vport->phba;
708 struct lpfc_node_rrq *rrq;
709 struct lpfc_node_rrq *nextrrq;
710 unsigned long iflags;
711
712 if (phba->sli_rev != LPFC_SLI_REV4)
713 return NULL;
714 spin_lock_irqsave(&phba->hbalock, iflags);
715 list_for_each_entry_safe(rrq, nextrrq, &phba->active_rrq_list, list) {
716 if (rrq->vport == vport && rrq->xritag == xri &&
717 rrq->nlp_DID == did){
718 list_del(&rrq->list);
719 spin_unlock_irqrestore(&phba->hbalock, iflags);
720 return rrq;
721 }
722 }
723 spin_unlock_irqrestore(&phba->hbalock, iflags);
724 return NULL;
725}
726
727/**
728 * lpfc_cleanup_vports_rrqs - Remove and clear the active RRQ for this vport.
729 * @vport: Pointer to vport context object.
James Smart1151e3e2011-02-16 12:39:35 -0500730 * @ndlp: Pointer to the lpfc_node_list structure.
731 * If ndlp is NULL Remove all active RRQs for this vport from the
732 * phba->active_rrq_list and clear the rrq.
733 * If ndlp is not NULL then only remove rrqs for this vport & this ndlp.
James Smart19ca7602010-11-20 23:11:55 -0500734 **/
735void
James Smart1151e3e2011-02-16 12:39:35 -0500736lpfc_cleanup_vports_rrqs(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
James Smart19ca7602010-11-20 23:11:55 -0500737
738{
739 struct lpfc_hba *phba = vport->phba;
740 struct lpfc_node_rrq *rrq;
741 struct lpfc_node_rrq *nextrrq;
742 unsigned long iflags;
James Smart1151e3e2011-02-16 12:39:35 -0500743 LIST_HEAD(rrq_list);
James Smart19ca7602010-11-20 23:11:55 -0500744
745 if (phba->sli_rev != LPFC_SLI_REV4)
746 return;
James Smart1151e3e2011-02-16 12:39:35 -0500747 if (!ndlp) {
748 lpfc_sli4_vport_delete_els_xri_aborted(vport);
749 lpfc_sli4_vport_delete_fcp_xri_aborted(vport);
James Smart19ca7602010-11-20 23:11:55 -0500750 }
James Smart1151e3e2011-02-16 12:39:35 -0500751 spin_lock_irqsave(&phba->hbalock, iflags);
752 list_for_each_entry_safe(rrq, nextrrq, &phba->active_rrq_list, list)
753 if ((rrq->vport == vport) && (!ndlp || rrq->ndlp == ndlp))
754 list_move(&rrq->list, &rrq_list);
James Smart19ca7602010-11-20 23:11:55 -0500755 spin_unlock_irqrestore(&phba->hbalock, iflags);
James Smart1151e3e2011-02-16 12:39:35 -0500756
757 list_for_each_entry_safe(rrq, nextrrq, &rrq_list, list) {
758 list_del(&rrq->list);
759 lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
760 }
James Smart19ca7602010-11-20 23:11:55 -0500761}
762
763/**
764 * lpfc_cleanup_wt_rrqs - Remove all rrq's from the active list.
765 * @phba: Pointer to HBA context object.
766 *
767 * Remove all rrqs from the phba->active_rrq_list and free them by
768 * calling __lpfc_clr_active_rrq
769 *
770 **/
771void
772lpfc_cleanup_wt_rrqs(struct lpfc_hba *phba)
773{
774 struct lpfc_node_rrq *rrq;
775 struct lpfc_node_rrq *nextrrq;
776 unsigned long next_time;
777 unsigned long iflags;
James Smart1151e3e2011-02-16 12:39:35 -0500778 LIST_HEAD(rrq_list);
James Smart19ca7602010-11-20 23:11:55 -0500779
780 if (phba->sli_rev != LPFC_SLI_REV4)
781 return;
782 spin_lock_irqsave(&phba->hbalock, iflags);
783 phba->hba_flag &= ~HBA_RRQ_ACTIVE;
784 next_time = jiffies + HZ * (phba->fc_ratov * 2);
James Smart1151e3e2011-02-16 12:39:35 -0500785 list_splice_init(&phba->active_rrq_list, &rrq_list);
James Smart19ca7602010-11-20 23:11:55 -0500786 spin_unlock_irqrestore(&phba->hbalock, iflags);
James Smart1151e3e2011-02-16 12:39:35 -0500787
788 list_for_each_entry_safe(rrq, nextrrq, &rrq_list, list) {
789 list_del(&rrq->list);
790 lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
791 }
James Smart19ca7602010-11-20 23:11:55 -0500792 if (!list_empty(&phba->active_rrq_list))
793 mod_timer(&phba->rrq_tmr, next_time);
794}
795
796
797/**
James Smart1151e3e2011-02-16 12:39:35 -0500798 * lpfc_test_rrq_active - Test RRQ bit in xri_bitmap.
James Smart19ca7602010-11-20 23:11:55 -0500799 * @phba: Pointer to HBA context object.
800 * @ndlp: Targets nodelist pointer for this exchange.
801 * @xritag the xri in the bitmap to test.
802 *
803 * This function is called with hbalock held. This function
804 * returns 0 = rrq not active for this xri
805 * 1 = rrq is valid for this xri.
806 **/
James Smart1151e3e2011-02-16 12:39:35 -0500807int
808lpfc_test_rrq_active(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
James Smart19ca7602010-11-20 23:11:55 -0500809 uint16_t xritag)
810{
James Smart19ca7602010-11-20 23:11:55 -0500811 if (!ndlp)
812 return 0;
James Smart6d368e52011-05-24 11:44:12 -0400813 if (test_bit(xritag, ndlp->active_rrqs.xri_bitmap))
James Smart19ca7602010-11-20 23:11:55 -0500814 return 1;
815 else
816 return 0;
817}
818
819/**
820 * lpfc_set_rrq_active - set RRQ active bit in xri_bitmap.
821 * @phba: Pointer to HBA context object.
822 * @ndlp: nodelist pointer for this target.
823 * @xritag: xri used in this exchange.
824 * @rxid: Remote Exchange ID.
825 * @send_rrq: Flag used to determine if we should send rrq els cmd.
826 *
827 * This function takes the hbalock.
828 * The active bit is always set in the active rrq xri_bitmap even
829 * if there is no slot avaiable for the other rrq information.
830 *
831 * returns 0 rrq actived for this xri
832 * < 0 No memory or invalid ndlp.
833 **/
834int
835lpfc_set_rrq_active(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
James Smartb42c07c2012-01-18 16:25:55 -0500836 uint16_t xritag, uint16_t rxid, uint16_t send_rrq)
James Smart19ca7602010-11-20 23:11:55 -0500837{
James Smart19ca7602010-11-20 23:11:55 -0500838 unsigned long iflags;
James Smartb42c07c2012-01-18 16:25:55 -0500839 struct lpfc_node_rrq *rrq;
840 int empty;
841
842 if (!ndlp)
843 return -EINVAL;
844
845 if (!phba->cfg_enable_rrq)
846 return -EINVAL;
James Smart19ca7602010-11-20 23:11:55 -0500847
848 spin_lock_irqsave(&phba->hbalock, iflags);
James Smartb42c07c2012-01-18 16:25:55 -0500849 if (phba->pport->load_flag & FC_UNLOADING) {
850 phba->hba_flag &= ~HBA_RRQ_ACTIVE;
851 goto out;
852 }
853
854 /*
855 * set the active bit even if there is no mem available.
856 */
857 if (NLP_CHK_FREE_REQ(ndlp))
858 goto out;
859
860 if (ndlp->vport && (ndlp->vport->load_flag & FC_UNLOADING))
861 goto out;
862
863 if (test_and_set_bit(xritag, ndlp->active_rrqs.xri_bitmap))
864 goto out;
865
James Smart19ca7602010-11-20 23:11:55 -0500866 spin_unlock_irqrestore(&phba->hbalock, iflags);
James Smartb42c07c2012-01-18 16:25:55 -0500867 rrq = mempool_alloc(phba->rrq_pool, GFP_KERNEL);
868 if (!rrq) {
869 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
870 "3155 Unable to allocate RRQ xri:0x%x rxid:0x%x"
871 " DID:0x%x Send:%d\n",
872 xritag, rxid, ndlp->nlp_DID, send_rrq);
873 return -EINVAL;
874 }
James Smarte5771b42013-03-01 16:37:14 -0500875 if (phba->cfg_enable_rrq == 1)
876 rrq->send_rrq = send_rrq;
877 else
878 rrq->send_rrq = 0;
James Smartb42c07c2012-01-18 16:25:55 -0500879 rrq->xritag = xritag;
880 rrq->rrq_stop_time = jiffies + HZ * (phba->fc_ratov + 1);
881 rrq->ndlp = ndlp;
882 rrq->nlp_DID = ndlp->nlp_DID;
883 rrq->vport = ndlp->vport;
884 rrq->rxid = rxid;
James Smartb42c07c2012-01-18 16:25:55 -0500885 spin_lock_irqsave(&phba->hbalock, iflags);
886 empty = list_empty(&phba->active_rrq_list);
887 list_add_tail(&rrq->list, &phba->active_rrq_list);
888 phba->hba_flag |= HBA_RRQ_ACTIVE;
889 if (empty)
890 lpfc_worker_wake_up(phba);
891 spin_unlock_irqrestore(&phba->hbalock, iflags);
892 return 0;
893out:
894 spin_unlock_irqrestore(&phba->hbalock, iflags);
895 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
896 "2921 Can't set rrq active xri:0x%x rxid:0x%x"
897 " DID:0x%x Send:%d\n",
898 xritag, rxid, ndlp->nlp_DID, send_rrq);
899 return -EINVAL;
James Smart19ca7602010-11-20 23:11:55 -0500900}
901
902/**
James Smartda0436e2009-05-22 14:51:39 -0400903 * __lpfc_sli_get_sglq - Allocates an iocb object from sgl pool
904 * @phba: Pointer to HBA context object.
James Smart19ca7602010-11-20 23:11:55 -0500905 * @piocb: Pointer to the iocbq.
James Smartda0436e2009-05-22 14:51:39 -0400906 *
907 * This function is called with hbalock held. This function
James Smart6d368e52011-05-24 11:44:12 -0400908 * gets a new driver sglq object from the sglq list. If the
James Smartda0436e2009-05-22 14:51:39 -0400909 * list is not empty then it is successful, it returns pointer to the newly
910 * allocated sglq object else it returns NULL.
911 **/
912static struct lpfc_sglq *
James Smart19ca7602010-11-20 23:11:55 -0500913__lpfc_sli_get_sglq(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq)
James Smartda0436e2009-05-22 14:51:39 -0400914{
915 struct list_head *lpfc_sgl_list = &phba->sli4_hba.lpfc_sgl_list;
916 struct lpfc_sglq *sglq = NULL;
James Smart19ca7602010-11-20 23:11:55 -0500917 struct lpfc_sglq *start_sglq = NULL;
James Smart19ca7602010-11-20 23:11:55 -0500918 struct lpfc_scsi_buf *lpfc_cmd;
919 struct lpfc_nodelist *ndlp;
920 int found = 0;
921
922 if (piocbq->iocb_flag & LPFC_IO_FCP) {
923 lpfc_cmd = (struct lpfc_scsi_buf *) piocbq->context1;
924 ndlp = lpfc_cmd->rdata->pnode;
James Smartbe858b62010-12-15 17:57:20 -0500925 } else if ((piocbq->iocb.ulpCommand == CMD_GEN_REQUEST64_CR) &&
926 !(piocbq->iocb_flag & LPFC_IO_LIBDFC))
James Smart19ca7602010-11-20 23:11:55 -0500927 ndlp = piocbq->context_un.ndlp;
James Smart93d1379e2012-05-09 21:19:34 -0400928 else if ((piocbq->iocb.ulpCommand == CMD_ELS_REQUEST64_CR) &&
929 (piocbq->iocb_flag & LPFC_IO_LIBDFC))
930 ndlp = piocbq->context_un.ndlp;
James Smart19ca7602010-11-20 23:11:55 -0500931 else
932 ndlp = piocbq->context1;
933
James Smartda0436e2009-05-22 14:51:39 -0400934 list_remove_head(lpfc_sgl_list, sglq, struct lpfc_sglq, list);
James Smart19ca7602010-11-20 23:11:55 -0500935 start_sglq = sglq;
936 while (!found) {
937 if (!sglq)
938 return NULL;
James Smartee0f4fe2012-05-09 21:19:14 -0400939 if (lpfc_test_rrq_active(phba, ndlp, sglq->sli4_lxritag)) {
James Smart19ca7602010-11-20 23:11:55 -0500940 /* This xri has an rrq outstanding for this DID.
941 * put it back in the list and get another xri.
942 */
943 list_add_tail(&sglq->list, lpfc_sgl_list);
944 sglq = NULL;
945 list_remove_head(lpfc_sgl_list, sglq,
946 struct lpfc_sglq, list);
947 if (sglq == start_sglq) {
948 sglq = NULL;
949 break;
950 } else
951 continue;
952 }
953 sglq->ndlp = ndlp;
954 found = 1;
James Smart6d368e52011-05-24 11:44:12 -0400955 phba->sli4_hba.lpfc_sglq_active_list[sglq->sli4_lxritag] = sglq;
James Smart19ca7602010-11-20 23:11:55 -0500956 sglq->state = SGL_ALLOCATED;
957 }
James Smartda0436e2009-05-22 14:51:39 -0400958 return sglq;
959}
960
961/**
James Smart3621a712009-04-06 18:47:14 -0400962 * lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
James Smarte59058c2008-08-24 21:49:00 -0400963 * @phba: Pointer to HBA context object.
964 *
965 * This function is called with no lock held. This function
966 * allocates a new driver iocb object from the iocb pool. If the
967 * allocation is successful, it returns pointer to the newly
968 * allocated iocb object else it returns NULL.
969 **/
James Smart2e0fef82007-06-17 19:56:36 -0500970struct lpfc_iocbq *
971lpfc_sli_get_iocbq(struct lpfc_hba *phba)
James Bottomley604a3e32005-10-29 10:28:33 -0500972{
James Smart2e0fef82007-06-17 19:56:36 -0500973 struct lpfc_iocbq * iocbq = NULL;
974 unsigned long iflags;
975
976 spin_lock_irqsave(&phba->hbalock, iflags);
977 iocbq = __lpfc_sli_get_iocbq(phba);
978 spin_unlock_irqrestore(&phba->hbalock, iflags);
979 return iocbq;
980}
981
James Smarte59058c2008-08-24 21:49:00 -0400982/**
James Smart4f774512009-05-22 14:52:35 -0400983 * __lpfc_sli_release_iocbq_s4 - Release iocb to the iocb pool
984 * @phba: Pointer to HBA context object.
985 * @iocbq: Pointer to driver iocb object.
986 *
987 * This function is called with hbalock held to release driver
988 * iocb object to the iocb pool. The iotag in the iocb object
989 * does not change for each use of the iocb object. This function
990 * clears all other fields of the iocb object when it is freed.
991 * The sqlq structure that holds the xritag and phys and virtual
992 * mappings for the scatter gather list is retrieved from the
993 * active array of sglq. The get of the sglq pointer also clears
994 * the entry in the array. If the status of the IO indiactes that
995 * this IO was aborted then the sglq entry it put on the
996 * lpfc_abts_els_sgl_list until the CQ_ABORTED_XRI is received. If the
997 * IO has good status or fails for any other reason then the sglq
998 * entry is added to the free list (lpfc_sgl_list).
999 **/
1000static void
1001__lpfc_sli_release_iocbq_s4(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1002{
1003 struct lpfc_sglq *sglq;
1004 size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
James Smart2a9bf3d2010-06-07 15:24:45 -04001005 unsigned long iflag = 0;
1006 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
James Smart4f774512009-05-22 14:52:35 -04001007
1008 if (iocbq->sli4_xritag == NO_XRI)
1009 sglq = NULL;
1010 else
James Smart6d368e52011-05-24 11:44:12 -04001011 sglq = __lpfc_clear_active_sglq(phba, iocbq->sli4_lxritag);
1012
James Smart0e9bb8d2013-03-01 16:35:12 -05001013 /*
1014 ** This should have been removed from the txcmplq before calling
1015 ** iocbq_release. The normal completion
1016 ** path should have already done the list_del_init.
1017 */
1018 if (unlikely(!list_empty(&iocbq->list))) {
1019 if (iocbq->iocb_flag & LPFC_IO_ON_TXCMPLQ)
1020 iocbq->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
1021 list_del_init(&iocbq->list);
1022 }
1023
1024
James Smart4f774512009-05-22 14:52:35 -04001025 if (sglq) {
James Smart0f65ff62010-02-26 14:14:23 -05001026 if ((iocbq->iocb_flag & LPFC_EXCHANGE_BUSY) &&
1027 (sglq->state != SGL_XRI_ABORTED)) {
James Smart4f774512009-05-22 14:52:35 -04001028 spin_lock_irqsave(&phba->sli4_hba.abts_sgl_list_lock,
1029 iflag);
1030 list_add(&sglq->list,
1031 &phba->sli4_hba.lpfc_abts_els_sgl_list);
1032 spin_unlock_irqrestore(
1033 &phba->sli4_hba.abts_sgl_list_lock, iflag);
James Smart0f65ff62010-02-26 14:14:23 -05001034 } else {
1035 sglq->state = SGL_FREED;
James Smart19ca7602010-11-20 23:11:55 -05001036 sglq->ndlp = NULL;
James Smartfedd3b72011-02-16 12:39:24 -05001037 list_add_tail(&sglq->list,
1038 &phba->sli4_hba.lpfc_sgl_list);
James Smart2a9bf3d2010-06-07 15:24:45 -04001039
1040 /* Check if TXQ queue needs to be serviced */
James Smart0e9bb8d2013-03-01 16:35:12 -05001041 if (!list_empty(&pring->txq))
James Smart2a9bf3d2010-06-07 15:24:45 -04001042 lpfc_worker_wake_up(phba);
James Smart0f65ff62010-02-26 14:14:23 -05001043 }
James Smart4f774512009-05-22 14:52:35 -04001044 }
1045
1046
1047 /*
1048 * Clean all volatile data fields, preserve iotag and node struct.
1049 */
1050 memset((char *)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
James Smart6d368e52011-05-24 11:44:12 -04001051 iocbq->sli4_lxritag = NO_XRI;
James Smart4f774512009-05-22 14:52:35 -04001052 iocbq->sli4_xritag = NO_XRI;
1053 list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
1054}
1055
James Smart2a9bf3d2010-06-07 15:24:45 -04001056
James Smart4f774512009-05-22 14:52:35 -04001057/**
James Smart3772a992009-05-22 14:50:54 -04001058 * __lpfc_sli_release_iocbq_s3 - Release iocb to the iocb pool
1059 * @phba: Pointer to HBA context object.
1060 * @iocbq: Pointer to driver iocb object.
1061 *
1062 * This function is called with hbalock held to release driver
1063 * iocb object to the iocb pool. The iotag in the iocb object
1064 * does not change for each use of the iocb object. This function
1065 * clears all other fields of the iocb object when it is freed.
1066 **/
1067static void
1068__lpfc_sli_release_iocbq_s3(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1069{
1070 size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
1071
1072 /*
James Smart0e9bb8d2013-03-01 16:35:12 -05001073 ** This should have been removed from the txcmplq before calling
1074 ** iocbq_release. The normal completion
1075 ** path should have already done the list_del_init.
1076 */
1077 if (unlikely(!list_empty(&iocbq->list)))
1078 list_del_init(&iocbq->list);
1079
1080 /*
James Smart3772a992009-05-22 14:50:54 -04001081 * Clean all volatile data fields, preserve iotag and node struct.
1082 */
1083 memset((char*)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
1084 iocbq->sli4_xritag = NO_XRI;
1085 list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
1086}
1087
1088/**
James Smart3621a712009-04-06 18:47:14 -04001089 * __lpfc_sli_release_iocbq - Release iocb to the iocb pool
James Smarte59058c2008-08-24 21:49:00 -04001090 * @phba: Pointer to HBA context object.
1091 * @iocbq: Pointer to driver iocb object.
1092 *
1093 * This function is called with hbalock held to release driver
1094 * iocb object to the iocb pool. The iotag in the iocb object
1095 * does not change for each use of the iocb object. This function
1096 * clears all other fields of the iocb object when it is freed.
1097 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01001098static void
James Smart2e0fef82007-06-17 19:56:36 -05001099__lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1100{
James Smart3772a992009-05-22 14:50:54 -04001101 phba->__lpfc_sli_release_iocbq(phba, iocbq);
James Smart2a9bf3d2010-06-07 15:24:45 -04001102 phba->iocb_cnt--;
James Bottomley604a3e32005-10-29 10:28:33 -05001103}
1104
James Smarte59058c2008-08-24 21:49:00 -04001105/**
James Smart3621a712009-04-06 18:47:14 -04001106 * lpfc_sli_release_iocbq - Release iocb to the iocb pool
James Smarte59058c2008-08-24 21:49:00 -04001107 * @phba: Pointer to HBA context object.
1108 * @iocbq: Pointer to driver iocb object.
1109 *
1110 * This function is called with no lock held to release the iocb to
1111 * iocb pool.
1112 **/
James Smart2e0fef82007-06-17 19:56:36 -05001113void
1114lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1115{
1116 unsigned long iflags;
1117
1118 /*
1119 * Clean all volatile data fields, preserve iotag and node struct.
1120 */
1121 spin_lock_irqsave(&phba->hbalock, iflags);
1122 __lpfc_sli_release_iocbq(phba, iocbq);
1123 spin_unlock_irqrestore(&phba->hbalock, iflags);
1124}
1125
James Smarte59058c2008-08-24 21:49:00 -04001126/**
James Smarta257bf92009-04-06 18:48:10 -04001127 * lpfc_sli_cancel_iocbs - Cancel all iocbs from a list.
1128 * @phba: Pointer to HBA context object.
1129 * @iocblist: List of IOCBs.
1130 * @ulpstatus: ULP status in IOCB command field.
1131 * @ulpWord4: ULP word-4 in IOCB command field.
1132 *
1133 * This function is called with a list of IOCBs to cancel. It cancels the IOCB
1134 * on the list by invoking the complete callback function associated with the
1135 * IOCB with the provided @ulpstatus and @ulpword4 set to the IOCB commond
1136 * fields.
1137 **/
1138void
1139lpfc_sli_cancel_iocbs(struct lpfc_hba *phba, struct list_head *iocblist,
1140 uint32_t ulpstatus, uint32_t ulpWord4)
1141{
1142 struct lpfc_iocbq *piocb;
1143
1144 while (!list_empty(iocblist)) {
1145 list_remove_head(iocblist, piocb, struct lpfc_iocbq, list);
James Smarta257bf92009-04-06 18:48:10 -04001146 if (!piocb->iocb_cmpl)
1147 lpfc_sli_release_iocbq(phba, piocb);
1148 else {
1149 piocb->iocb.ulpStatus = ulpstatus;
1150 piocb->iocb.un.ulpWord[4] = ulpWord4;
1151 (piocb->iocb_cmpl) (phba, piocb, piocb);
1152 }
1153 }
1154 return;
1155}
1156
1157/**
James Smart3621a712009-04-06 18:47:14 -04001158 * lpfc_sli_iocb_cmd_type - Get the iocb type
1159 * @iocb_cmnd: iocb command code.
James Smarte59058c2008-08-24 21:49:00 -04001160 *
1161 * This function is called by ring event handler function to get the iocb type.
1162 * This function translates the iocb command to an iocb command type used to
1163 * decide the final disposition of each completed IOCB.
1164 * The function returns
1165 * LPFC_UNKNOWN_IOCB if it is an unsupported iocb
1166 * LPFC_SOL_IOCB if it is a solicited iocb completion
1167 * LPFC_ABORT_IOCB if it is an abort iocb
1168 * LPFC_UNSOL_IOCB if it is an unsolicited iocb
1169 *
1170 * The caller is not required to hold any lock.
1171 **/
dea31012005-04-17 16:05:31 -05001172static lpfc_iocb_type
1173lpfc_sli_iocb_cmd_type(uint8_t iocb_cmnd)
1174{
1175 lpfc_iocb_type type = LPFC_UNKNOWN_IOCB;
1176
1177 if (iocb_cmnd > CMD_MAX_IOCB_CMD)
1178 return 0;
1179
1180 switch (iocb_cmnd) {
1181 case CMD_XMIT_SEQUENCE_CR:
1182 case CMD_XMIT_SEQUENCE_CX:
1183 case CMD_XMIT_BCAST_CN:
1184 case CMD_XMIT_BCAST_CX:
1185 case CMD_ELS_REQUEST_CR:
1186 case CMD_ELS_REQUEST_CX:
1187 case CMD_CREATE_XRI_CR:
1188 case CMD_CREATE_XRI_CX:
1189 case CMD_GET_RPI_CN:
1190 case CMD_XMIT_ELS_RSP_CX:
1191 case CMD_GET_RPI_CR:
1192 case CMD_FCP_IWRITE_CR:
1193 case CMD_FCP_IWRITE_CX:
1194 case CMD_FCP_IREAD_CR:
1195 case CMD_FCP_IREAD_CX:
1196 case CMD_FCP_ICMND_CR:
1197 case CMD_FCP_ICMND_CX:
James Smartf5603512006-12-02 13:35:43 -05001198 case CMD_FCP_TSEND_CX:
1199 case CMD_FCP_TRSP_CX:
1200 case CMD_FCP_TRECEIVE_CX:
1201 case CMD_FCP_AUTO_TRSP_CX:
dea31012005-04-17 16:05:31 -05001202 case CMD_ADAPTER_MSG:
1203 case CMD_ADAPTER_DUMP:
1204 case CMD_XMIT_SEQUENCE64_CR:
1205 case CMD_XMIT_SEQUENCE64_CX:
1206 case CMD_XMIT_BCAST64_CN:
1207 case CMD_XMIT_BCAST64_CX:
1208 case CMD_ELS_REQUEST64_CR:
1209 case CMD_ELS_REQUEST64_CX:
1210 case CMD_FCP_IWRITE64_CR:
1211 case CMD_FCP_IWRITE64_CX:
1212 case CMD_FCP_IREAD64_CR:
1213 case CMD_FCP_IREAD64_CX:
1214 case CMD_FCP_ICMND64_CR:
1215 case CMD_FCP_ICMND64_CX:
James Smartf5603512006-12-02 13:35:43 -05001216 case CMD_FCP_TSEND64_CX:
1217 case CMD_FCP_TRSP64_CX:
1218 case CMD_FCP_TRECEIVE64_CX:
dea31012005-04-17 16:05:31 -05001219 case CMD_GEN_REQUEST64_CR:
1220 case CMD_GEN_REQUEST64_CX:
1221 case CMD_XMIT_ELS_RSP64_CX:
James Smartda0436e2009-05-22 14:51:39 -04001222 case DSSCMD_IWRITE64_CR:
1223 case DSSCMD_IWRITE64_CX:
1224 case DSSCMD_IREAD64_CR:
1225 case DSSCMD_IREAD64_CX:
dea31012005-04-17 16:05:31 -05001226 type = LPFC_SOL_IOCB;
1227 break;
1228 case CMD_ABORT_XRI_CN:
1229 case CMD_ABORT_XRI_CX:
1230 case CMD_CLOSE_XRI_CN:
1231 case CMD_CLOSE_XRI_CX:
1232 case CMD_XRI_ABORTED_CX:
1233 case CMD_ABORT_MXRI64_CN:
James Smart6669f9b2009-10-02 15:16:45 -04001234 case CMD_XMIT_BLS_RSP64_CX:
dea31012005-04-17 16:05:31 -05001235 type = LPFC_ABORT_IOCB;
1236 break;
1237 case CMD_RCV_SEQUENCE_CX:
1238 case CMD_RCV_ELS_REQ_CX:
1239 case CMD_RCV_SEQUENCE64_CX:
1240 case CMD_RCV_ELS_REQ64_CX:
James Smart57127f12007-10-27 13:37:05 -04001241 case CMD_ASYNC_STATUS:
James Smarted957682007-06-17 19:56:37 -05001242 case CMD_IOCB_RCV_SEQ64_CX:
1243 case CMD_IOCB_RCV_ELS64_CX:
1244 case CMD_IOCB_RCV_CONT64_CX:
James Smart3163f722008-02-08 18:50:25 -05001245 case CMD_IOCB_RET_XRI64_CX:
dea31012005-04-17 16:05:31 -05001246 type = LPFC_UNSOL_IOCB;
1247 break;
James Smart3163f722008-02-08 18:50:25 -05001248 case CMD_IOCB_XMIT_MSEQ64_CR:
1249 case CMD_IOCB_XMIT_MSEQ64_CX:
1250 case CMD_IOCB_RCV_SEQ_LIST64_CX:
1251 case CMD_IOCB_RCV_ELS_LIST64_CX:
1252 case CMD_IOCB_CLOSE_EXTENDED_CN:
1253 case CMD_IOCB_ABORT_EXTENDED_CN:
1254 case CMD_IOCB_RET_HBQE64_CN:
1255 case CMD_IOCB_FCP_IBIDIR64_CR:
1256 case CMD_IOCB_FCP_IBIDIR64_CX:
1257 case CMD_IOCB_FCP_ITASKMGT64_CX:
1258 case CMD_IOCB_LOGENTRY_CN:
1259 case CMD_IOCB_LOGENTRY_ASYNC_CN:
1260 printk("%s - Unhandled SLI-3 Command x%x\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07001261 __func__, iocb_cmnd);
James Smart3163f722008-02-08 18:50:25 -05001262 type = LPFC_UNKNOWN_IOCB;
1263 break;
dea31012005-04-17 16:05:31 -05001264 default:
1265 type = LPFC_UNKNOWN_IOCB;
1266 break;
1267 }
1268
1269 return type;
1270}
1271
James Smarte59058c2008-08-24 21:49:00 -04001272/**
James Smart3621a712009-04-06 18:47:14 -04001273 * lpfc_sli_ring_map - Issue config_ring mbox for all rings
James Smarte59058c2008-08-24 21:49:00 -04001274 * @phba: Pointer to HBA context object.
1275 *
1276 * This function is called from SLI initialization code
1277 * to configure every ring of the HBA's SLI interface. The
1278 * caller is not required to hold any lock. This function issues
1279 * a config_ring mailbox command for each ring.
1280 * This function returns zero if successful else returns a negative
1281 * error code.
1282 **/
dea31012005-04-17 16:05:31 -05001283static int
James Smarted957682007-06-17 19:56:37 -05001284lpfc_sli_ring_map(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05001285{
1286 struct lpfc_sli *psli = &phba->sli;
James Smarted957682007-06-17 19:56:37 -05001287 LPFC_MBOXQ_t *pmb;
1288 MAILBOX_t *pmbox;
1289 int i, rc, ret = 0;
dea31012005-04-17 16:05:31 -05001290
James Smarted957682007-06-17 19:56:37 -05001291 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1292 if (!pmb)
1293 return -ENOMEM;
James Smart04c68492009-05-22 14:52:52 -04001294 pmbox = &pmb->u.mb;
James Smarted957682007-06-17 19:56:37 -05001295 phba->link_state = LPFC_INIT_MBX_CMDS;
dea31012005-04-17 16:05:31 -05001296 for (i = 0; i < psli->num_rings; i++) {
dea31012005-04-17 16:05:31 -05001297 lpfc_config_ring(phba, i, pmb);
1298 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
1299 if (rc != MBX_SUCCESS) {
James Smart92d7f7b2007-06-17 19:56:38 -05001300 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04001301 "0446 Adapter failed to init (%d), "
dea31012005-04-17 16:05:31 -05001302 "mbxCmd x%x CFG_RING, mbxStatus x%x, "
1303 "ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04001304 rc, pmbox->mbxCommand,
1305 pmbox->mbxStatus, i);
James Smart2e0fef82007-06-17 19:56:36 -05001306 phba->link_state = LPFC_HBA_ERROR;
James Smarted957682007-06-17 19:56:37 -05001307 ret = -ENXIO;
1308 break;
dea31012005-04-17 16:05:31 -05001309 }
1310 }
James Smarted957682007-06-17 19:56:37 -05001311 mempool_free(pmb, phba->mbox_mem_pool);
1312 return ret;
dea31012005-04-17 16:05:31 -05001313}
1314
James Smarte59058c2008-08-24 21:49:00 -04001315/**
James Smart3621a712009-04-06 18:47:14 -04001316 * lpfc_sli_ringtxcmpl_put - Adds new iocb to the txcmplq
James Smarte59058c2008-08-24 21:49:00 -04001317 * @phba: Pointer to HBA context object.
1318 * @pring: Pointer to driver SLI ring object.
1319 * @piocb: Pointer to the driver iocb object.
1320 *
1321 * This function is called with hbalock held. The function adds the
1322 * new iocb to txcmplq of the given ring. This function always returns
1323 * 0. If this function is called for ELS ring, this function checks if
1324 * there is a vport associated with the ELS command. This function also
1325 * starts els_tmofunc timer if this is an ELS command.
1326 **/
dea31012005-04-17 16:05:31 -05001327static int
James Smart2e0fef82007-06-17 19:56:36 -05001328lpfc_sli_ringtxcmpl_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1329 struct lpfc_iocbq *piocb)
dea31012005-04-17 16:05:31 -05001330{
dea31012005-04-17 16:05:31 -05001331 list_add_tail(&piocb->list, &pring->txcmplq);
James Smart4f2e66c2012-05-09 21:17:07 -04001332 piocb->iocb_flag |= LPFC_IO_ON_TXCMPLQ;
James Smart2a9bf3d2010-06-07 15:24:45 -04001333
James Smart92d7f7b2007-06-17 19:56:38 -05001334 if ((unlikely(pring->ringno == LPFC_ELS_RING)) &&
1335 (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
1336 (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
1337 if (!piocb->vport)
1338 BUG();
1339 else
1340 mod_timer(&piocb->vport->els_tmofunc,
1341 jiffies + HZ * (phba->fc_ratov << 1));
1342 }
1343
dea31012005-04-17 16:05:31 -05001344
James Smart2e0fef82007-06-17 19:56:36 -05001345 return 0;
dea31012005-04-17 16:05:31 -05001346}
1347
James Smarte59058c2008-08-24 21:49:00 -04001348/**
James Smart3621a712009-04-06 18:47:14 -04001349 * lpfc_sli_ringtx_get - Get first element of the txq
James Smarte59058c2008-08-24 21:49:00 -04001350 * @phba: Pointer to HBA context object.
1351 * @pring: Pointer to driver SLI ring object.
1352 *
1353 * This function is called with hbalock held to get next
1354 * iocb in txq of the given ring. If there is any iocb in
1355 * the txq, the function returns first iocb in the list after
1356 * removing the iocb from the list, else it returns NULL.
1357 **/
James Smart2a9bf3d2010-06-07 15:24:45 -04001358struct lpfc_iocbq *
James Smart2e0fef82007-06-17 19:56:36 -05001359lpfc_sli_ringtx_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -05001360{
dea31012005-04-17 16:05:31 -05001361 struct lpfc_iocbq *cmd_iocb;
1362
James Smart858c9f62007-06-17 19:56:39 -05001363 list_remove_head((&pring->txq), cmd_iocb, struct lpfc_iocbq, list);
James Smart2e0fef82007-06-17 19:56:36 -05001364 return cmd_iocb;
dea31012005-04-17 16:05:31 -05001365}
1366
James Smarte59058c2008-08-24 21:49:00 -04001367/**
James Smart3621a712009-04-06 18:47:14 -04001368 * lpfc_sli_next_iocb_slot - Get next iocb slot in the ring
James Smarte59058c2008-08-24 21:49:00 -04001369 * @phba: Pointer to HBA context object.
1370 * @pring: Pointer to driver SLI ring object.
1371 *
1372 * This function is called with hbalock held and the caller must post the
1373 * iocb without releasing the lock. If the caller releases the lock,
1374 * iocb slot returned by the function is not guaranteed to be available.
1375 * The function returns pointer to the next available iocb slot if there
1376 * is available slot in the ring, else it returns NULL.
1377 * If the get index of the ring is ahead of the put index, the function
1378 * will post an error attention event to the worker thread to take the
1379 * HBA to offline state.
1380 **/
dea31012005-04-17 16:05:31 -05001381static IOCB_t *
1382lpfc_sli_next_iocb_slot (struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
1383{
James Smart34b02dc2008-08-24 21:49:55 -04001384 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
James Smart7e56aa22012-08-03 12:35:34 -04001385 uint32_t max_cmd_idx = pring->sli.sli3.numCiocb;
1386 if ((pring->sli.sli3.next_cmdidx == pring->sli.sli3.cmdidx) &&
1387 (++pring->sli.sli3.next_cmdidx >= max_cmd_idx))
1388 pring->sli.sli3.next_cmdidx = 0;
dea31012005-04-17 16:05:31 -05001389
James Smart7e56aa22012-08-03 12:35:34 -04001390 if (unlikely(pring->sli.sli3.local_getidx ==
1391 pring->sli.sli3.next_cmdidx)) {
dea31012005-04-17 16:05:31 -05001392
James Smart7e56aa22012-08-03 12:35:34 -04001393 pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
dea31012005-04-17 16:05:31 -05001394
James Smart7e56aa22012-08-03 12:35:34 -04001395 if (unlikely(pring->sli.sli3.local_getidx >= max_cmd_idx)) {
dea31012005-04-17 16:05:31 -05001396 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001397 "0315 Ring %d issue: portCmdGet %d "
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02001398 "is bigger than cmd ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04001399 pring->ringno,
James Smart7e56aa22012-08-03 12:35:34 -04001400 pring->sli.sli3.local_getidx,
1401 max_cmd_idx);
dea31012005-04-17 16:05:31 -05001402
James Smart2e0fef82007-06-17 19:56:36 -05001403 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05001404 /*
1405 * All error attention handlers are posted to
1406 * worker thread
1407 */
1408 phba->work_ha |= HA_ERATT;
1409 phba->work_hs = HS_FFER3;
James Smart92d7f7b2007-06-17 19:56:38 -05001410
James Smart5e9d9b82008-06-14 22:52:53 -04001411 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -05001412
1413 return NULL;
1414 }
1415
James Smart7e56aa22012-08-03 12:35:34 -04001416 if (pring->sli.sli3.local_getidx == pring->sli.sli3.next_cmdidx)
dea31012005-04-17 16:05:31 -05001417 return NULL;
1418 }
1419
James Smarted957682007-06-17 19:56:37 -05001420 return lpfc_cmd_iocb(phba, pring);
dea31012005-04-17 16:05:31 -05001421}
1422
James Smarte59058c2008-08-24 21:49:00 -04001423/**
James Smart3621a712009-04-06 18:47:14 -04001424 * lpfc_sli_next_iotag - Get an iotag for the iocb
James Smarte59058c2008-08-24 21:49:00 -04001425 * @phba: Pointer to HBA context object.
1426 * @iocbq: Pointer to driver iocb object.
1427 *
1428 * This function gets an iotag for the iocb. If there is no unused iotag and
1429 * the iocbq_lookup_len < 0xffff, this function allocates a bigger iotag_lookup
1430 * array and assigns a new iotag.
1431 * The function returns the allocated iotag if successful, else returns zero.
1432 * Zero is not a valid iotag.
1433 * The caller is not required to hold any lock.
1434 **/
James Bottomley604a3e32005-10-29 10:28:33 -05001435uint16_t
James Smart2e0fef82007-06-17 19:56:36 -05001436lpfc_sli_next_iotag(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
dea31012005-04-17 16:05:31 -05001437{
James Smart2e0fef82007-06-17 19:56:36 -05001438 struct lpfc_iocbq **new_arr;
1439 struct lpfc_iocbq **old_arr;
James Bottomley604a3e32005-10-29 10:28:33 -05001440 size_t new_len;
1441 struct lpfc_sli *psli = &phba->sli;
1442 uint16_t iotag;
dea31012005-04-17 16:05:31 -05001443
James Smart2e0fef82007-06-17 19:56:36 -05001444 spin_lock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001445 iotag = psli->last_iotag;
1446 if(++iotag < psli->iocbq_lookup_len) {
1447 psli->last_iotag = iotag;
1448 psli->iocbq_lookup[iotag] = iocbq;
James Smart2e0fef82007-06-17 19:56:36 -05001449 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001450 iocbq->iotag = iotag;
1451 return iotag;
James Smart2e0fef82007-06-17 19:56:36 -05001452 } else if (psli->iocbq_lookup_len < (0xffff
James Bottomley604a3e32005-10-29 10:28:33 -05001453 - LPFC_IOCBQ_LOOKUP_INCREMENT)) {
1454 new_len = psli->iocbq_lookup_len + LPFC_IOCBQ_LOOKUP_INCREMENT;
James Smart2e0fef82007-06-17 19:56:36 -05001455 spin_unlock_irq(&phba->hbalock);
1456 new_arr = kzalloc(new_len * sizeof (struct lpfc_iocbq *),
James Bottomley604a3e32005-10-29 10:28:33 -05001457 GFP_KERNEL);
1458 if (new_arr) {
James Smart2e0fef82007-06-17 19:56:36 -05001459 spin_lock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001460 old_arr = psli->iocbq_lookup;
1461 if (new_len <= psli->iocbq_lookup_len) {
1462 /* highly unprobable case */
1463 kfree(new_arr);
1464 iotag = psli->last_iotag;
1465 if(++iotag < psli->iocbq_lookup_len) {
1466 psli->last_iotag = iotag;
1467 psli->iocbq_lookup[iotag] = iocbq;
James Smart2e0fef82007-06-17 19:56:36 -05001468 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001469 iocbq->iotag = iotag;
1470 return iotag;
1471 }
James Smart2e0fef82007-06-17 19:56:36 -05001472 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001473 return 0;
1474 }
1475 if (psli->iocbq_lookup)
1476 memcpy(new_arr, old_arr,
1477 ((psli->last_iotag + 1) *
James Smart311464e2007-08-02 11:10:37 -04001478 sizeof (struct lpfc_iocbq *)));
James Bottomley604a3e32005-10-29 10:28:33 -05001479 psli->iocbq_lookup = new_arr;
1480 psli->iocbq_lookup_len = new_len;
1481 psli->last_iotag = iotag;
1482 psli->iocbq_lookup[iotag] = iocbq;
James Smart2e0fef82007-06-17 19:56:36 -05001483 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001484 iocbq->iotag = iotag;
1485 kfree(old_arr);
1486 return iotag;
1487 }
James Smart8f6d98d2006-08-01 07:34:00 -04001488 } else
James Smart2e0fef82007-06-17 19:56:36 -05001489 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001490
James Smartbc739052010-08-04 16:11:18 -04001491 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001492 "0318 Failed to allocate IOTAG.last IOTAG is %d\n",
1493 psli->last_iotag);
dea31012005-04-17 16:05:31 -05001494
James Bottomley604a3e32005-10-29 10:28:33 -05001495 return 0;
dea31012005-04-17 16:05:31 -05001496}
1497
James Smarte59058c2008-08-24 21:49:00 -04001498/**
James Smart3621a712009-04-06 18:47:14 -04001499 * lpfc_sli_submit_iocb - Submit an iocb to the firmware
James Smarte59058c2008-08-24 21:49:00 -04001500 * @phba: Pointer to HBA context object.
1501 * @pring: Pointer to driver SLI ring object.
1502 * @iocb: Pointer to iocb slot in the ring.
1503 * @nextiocb: Pointer to driver iocb object which need to be
1504 * posted to firmware.
1505 *
1506 * This function is called with hbalock held to post a new iocb to
1507 * the firmware. This function copies the new iocb to ring iocb slot and
1508 * updates the ring pointers. It adds the new iocb to txcmplq if there is
1509 * a completion call back for this iocb else the function will free the
1510 * iocb object.
1511 **/
dea31012005-04-17 16:05:31 -05001512static void
1513lpfc_sli_submit_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1514 IOCB_t *iocb, struct lpfc_iocbq *nextiocb)
1515{
1516 /*
James Bottomley604a3e32005-10-29 10:28:33 -05001517 * Set up an iotag
dea31012005-04-17 16:05:31 -05001518 */
James Bottomley604a3e32005-10-29 10:28:33 -05001519 nextiocb->iocb.ulpIoTag = (nextiocb->iocb_cmpl) ? nextiocb->iotag : 0;
dea31012005-04-17 16:05:31 -05001520
James Smarte2a0a9d2008-12-04 22:40:02 -05001521
James Smarta58cbd52007-08-02 11:09:43 -04001522 if (pring->ringno == LPFC_ELS_RING) {
1523 lpfc_debugfs_slow_ring_trc(phba,
1524 "IOCB cmd ring: wd4:x%08x wd6:x%08x wd7:x%08x",
1525 *(((uint32_t *) &nextiocb->iocb) + 4),
1526 *(((uint32_t *) &nextiocb->iocb) + 6),
1527 *(((uint32_t *) &nextiocb->iocb) + 7));
1528 }
1529
dea31012005-04-17 16:05:31 -05001530 /*
1531 * Issue iocb command to adapter
1532 */
James Smart92d7f7b2007-06-17 19:56:38 -05001533 lpfc_sli_pcimem_bcopy(&nextiocb->iocb, iocb, phba->iocb_cmd_size);
dea31012005-04-17 16:05:31 -05001534 wmb();
1535 pring->stats.iocb_cmd++;
1536
1537 /*
1538 * If there is no completion routine to call, we can release the
1539 * IOCB buffer back right now. For IOCBs, like QUE_RING_BUF,
1540 * that have no rsp ring completion, iocb_cmpl MUST be NULL.
1541 */
1542 if (nextiocb->iocb_cmpl)
1543 lpfc_sli_ringtxcmpl_put(phba, pring, nextiocb);
James Bottomley604a3e32005-10-29 10:28:33 -05001544 else
James Smart2e0fef82007-06-17 19:56:36 -05001545 __lpfc_sli_release_iocbq(phba, nextiocb);
dea31012005-04-17 16:05:31 -05001546
1547 /*
1548 * Let the HBA know what IOCB slot will be the next one the
1549 * driver will put a command into.
1550 */
James Smart7e56aa22012-08-03 12:35:34 -04001551 pring->sli.sli3.cmdidx = pring->sli.sli3.next_cmdidx;
1552 writel(pring->sli.sli3.cmdidx, &phba->host_gp[pring->ringno].cmdPutInx);
dea31012005-04-17 16:05:31 -05001553}
1554
James Smarte59058c2008-08-24 21:49:00 -04001555/**
James Smart3621a712009-04-06 18:47:14 -04001556 * lpfc_sli_update_full_ring - Update the chip attention register
James Smarte59058c2008-08-24 21:49:00 -04001557 * @phba: Pointer to HBA context object.
1558 * @pring: Pointer to driver SLI ring object.
1559 *
1560 * The caller is not required to hold any lock for calling this function.
1561 * This function updates the chip attention bits for the ring to inform firmware
1562 * that there are pending work to be done for this ring and requests an
1563 * interrupt when there is space available in the ring. This function is
1564 * called when the driver is unable to post more iocbs to the ring due
1565 * to unavailability of space in the ring.
1566 **/
dea31012005-04-17 16:05:31 -05001567static void
James Smart2e0fef82007-06-17 19:56:36 -05001568lpfc_sli_update_full_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -05001569{
1570 int ringno = pring->ringno;
1571
1572 pring->flag |= LPFC_CALL_RING_AVAILABLE;
1573
1574 wmb();
1575
1576 /*
1577 * Set ring 'ringno' to SET R0CE_REQ in Chip Att register.
1578 * The HBA will tell us when an IOCB entry is available.
1579 */
1580 writel((CA_R0ATT|CA_R0CE_REQ) << (ringno*4), phba->CAregaddr);
1581 readl(phba->CAregaddr); /* flush */
1582
1583 pring->stats.iocb_cmd_full++;
1584}
1585
James Smarte59058c2008-08-24 21:49:00 -04001586/**
James Smart3621a712009-04-06 18:47:14 -04001587 * lpfc_sli_update_ring - Update chip attention register
James Smarte59058c2008-08-24 21:49:00 -04001588 * @phba: Pointer to HBA context object.
1589 * @pring: Pointer to driver SLI ring object.
1590 *
1591 * This function updates the chip attention register bit for the
1592 * given ring to inform HBA that there is more work to be done
1593 * in this ring. The caller is not required to hold any lock.
1594 **/
dea31012005-04-17 16:05:31 -05001595static void
James Smart2e0fef82007-06-17 19:56:36 -05001596lpfc_sli_update_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -05001597{
1598 int ringno = pring->ringno;
1599
1600 /*
1601 * Tell the HBA that there is work to do in this ring.
1602 */
James Smart34b02dc2008-08-24 21:49:55 -04001603 if (!(phba->sli3_options & LPFC_SLI3_CRP_ENABLED)) {
1604 wmb();
1605 writel(CA_R0ATT << (ringno * 4), phba->CAregaddr);
1606 readl(phba->CAregaddr); /* flush */
1607 }
dea31012005-04-17 16:05:31 -05001608}
1609
James Smarte59058c2008-08-24 21:49:00 -04001610/**
James Smart3621a712009-04-06 18:47:14 -04001611 * lpfc_sli_resume_iocb - Process iocbs in the txq
James Smarte59058c2008-08-24 21:49:00 -04001612 * @phba: Pointer to HBA context object.
1613 * @pring: Pointer to driver SLI ring object.
1614 *
1615 * This function is called with hbalock held to post pending iocbs
1616 * in the txq to the firmware. This function is called when driver
1617 * detects space available in the ring.
1618 **/
dea31012005-04-17 16:05:31 -05001619static void
James Smart2e0fef82007-06-17 19:56:36 -05001620lpfc_sli_resume_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -05001621{
1622 IOCB_t *iocb;
1623 struct lpfc_iocbq *nextiocb;
1624
1625 /*
1626 * Check to see if:
1627 * (a) there is anything on the txq to send
1628 * (b) link is up
1629 * (c) link attention events can be processed (fcp ring only)
1630 * (d) IOCB processing is not blocked by the outstanding mbox command.
1631 */
James Smart0e9bb8d2013-03-01 16:35:12 -05001632
1633 if (lpfc_is_link_up(phba) &&
1634 (!list_empty(&pring->txq)) &&
dea31012005-04-17 16:05:31 -05001635 (pring->ringno != phba->sli.fcp_ring ||
James Smart0b727fe2007-10-27 13:37:25 -04001636 phba->sli.sli_flag & LPFC_PROCESS_LA)) {
dea31012005-04-17 16:05:31 -05001637
1638 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
1639 (nextiocb = lpfc_sli_ringtx_get(phba, pring)))
1640 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
1641
1642 if (iocb)
1643 lpfc_sli_update_ring(phba, pring);
1644 else
1645 lpfc_sli_update_full_ring(phba, pring);
1646 }
1647
1648 return;
1649}
1650
James Smarte59058c2008-08-24 21:49:00 -04001651/**
James Smart3621a712009-04-06 18:47:14 -04001652 * lpfc_sli_next_hbq_slot - Get next hbq entry for the HBQ
James Smarte59058c2008-08-24 21:49:00 -04001653 * @phba: Pointer to HBA context object.
1654 * @hbqno: HBQ number.
1655 *
1656 * This function is called with hbalock held to get the next
1657 * available slot for the given HBQ. If there is free slot
1658 * available for the HBQ it will return pointer to the next available
1659 * HBQ entry else it will return NULL.
1660 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01001661static struct lpfc_hbq_entry *
James Smarted957682007-06-17 19:56:37 -05001662lpfc_sli_next_hbq_slot(struct lpfc_hba *phba, uint32_t hbqno)
1663{
1664 struct hbq_s *hbqp = &phba->hbqs[hbqno];
1665
1666 if (hbqp->next_hbqPutIdx == hbqp->hbqPutIdx &&
1667 ++hbqp->next_hbqPutIdx >= hbqp->entry_count)
1668 hbqp->next_hbqPutIdx = 0;
1669
1670 if (unlikely(hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)) {
James Smart92d7f7b2007-06-17 19:56:38 -05001671 uint32_t raw_index = phba->hbq_get[hbqno];
James Smarted957682007-06-17 19:56:37 -05001672 uint32_t getidx = le32_to_cpu(raw_index);
1673
1674 hbqp->local_hbqGetIdx = getidx;
1675
1676 if (unlikely(hbqp->local_hbqGetIdx >= hbqp->entry_count)) {
1677 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -05001678 LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04001679 "1802 HBQ %d: local_hbqGetIdx "
James Smarted957682007-06-17 19:56:37 -05001680 "%u is > than hbqp->entry_count %u\n",
James Smarte8b62012007-08-02 11:10:09 -04001681 hbqno, hbqp->local_hbqGetIdx,
James Smarted957682007-06-17 19:56:37 -05001682 hbqp->entry_count);
1683
1684 phba->link_state = LPFC_HBA_ERROR;
1685 return NULL;
1686 }
1687
1688 if (hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)
1689 return NULL;
1690 }
1691
James Smart51ef4c22007-08-02 11:10:31 -04001692 return (struct lpfc_hbq_entry *) phba->hbqs[hbqno].hbq_virt +
1693 hbqp->hbqPutIdx;
James Smarted957682007-06-17 19:56:37 -05001694}
1695
James Smarte59058c2008-08-24 21:49:00 -04001696/**
James Smart3621a712009-04-06 18:47:14 -04001697 * lpfc_sli_hbqbuf_free_all - Free all the hbq buffers
James Smarte59058c2008-08-24 21:49:00 -04001698 * @phba: Pointer to HBA context object.
1699 *
1700 * This function is called with no lock held to free all the
1701 * hbq buffers while uninitializing the SLI interface. It also
1702 * frees the HBQ buffers returned by the firmware but not yet
1703 * processed by the upper layers.
1704 **/
James Smarted957682007-06-17 19:56:37 -05001705void
1706lpfc_sli_hbqbuf_free_all(struct lpfc_hba *phba)
1707{
James Smart92d7f7b2007-06-17 19:56:38 -05001708 struct lpfc_dmabuf *dmabuf, *next_dmabuf;
1709 struct hbq_dmabuf *hbq_buf;
James Smart3163f722008-02-08 18:50:25 -05001710 unsigned long flags;
James Smart51ef4c22007-08-02 11:10:31 -04001711 int i, hbq_count;
James Smart3163f722008-02-08 18:50:25 -05001712 uint32_t hbqno;
James Smarted957682007-06-17 19:56:37 -05001713
James Smart51ef4c22007-08-02 11:10:31 -04001714 hbq_count = lpfc_sli_hbq_count();
James Smarted957682007-06-17 19:56:37 -05001715 /* Return all memory used by all HBQs */
James Smart3163f722008-02-08 18:50:25 -05001716 spin_lock_irqsave(&phba->hbalock, flags);
James Smart51ef4c22007-08-02 11:10:31 -04001717 for (i = 0; i < hbq_count; ++i) {
1718 list_for_each_entry_safe(dmabuf, next_dmabuf,
1719 &phba->hbqs[i].hbq_buffer_list, list) {
1720 hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
1721 list_del(&hbq_buf->dbuf.list);
1722 (phba->hbqs[i].hbq_free_buffer)(phba, hbq_buf);
1723 }
James Smarta8adb832007-10-27 13:37:53 -04001724 phba->hbqs[i].buffer_count = 0;
James Smarted957682007-06-17 19:56:37 -05001725 }
James Smart3163f722008-02-08 18:50:25 -05001726 /* Return all HBQ buffer that are in-fly */
James Smart3772a992009-05-22 14:50:54 -04001727 list_for_each_entry_safe(dmabuf, next_dmabuf, &phba->rb_pend_list,
1728 list) {
James Smart3163f722008-02-08 18:50:25 -05001729 hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
1730 list_del(&hbq_buf->dbuf.list);
1731 if (hbq_buf->tag == -1) {
1732 (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
1733 (phba, hbq_buf);
1734 } else {
1735 hbqno = hbq_buf->tag >> 16;
1736 if (hbqno >= LPFC_MAX_HBQS)
1737 (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
1738 (phba, hbq_buf);
1739 else
1740 (phba->hbqs[hbqno].hbq_free_buffer)(phba,
1741 hbq_buf);
1742 }
1743 }
1744
1745 /* Mark the HBQs not in use */
1746 phba->hbq_in_use = 0;
1747 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smarted957682007-06-17 19:56:37 -05001748}
1749
James Smarte59058c2008-08-24 21:49:00 -04001750/**
James Smart3621a712009-04-06 18:47:14 -04001751 * lpfc_sli_hbq_to_firmware - Post the hbq buffer to firmware
James Smarte59058c2008-08-24 21:49:00 -04001752 * @phba: Pointer to HBA context object.
1753 * @hbqno: HBQ number.
1754 * @hbq_buf: Pointer to HBQ buffer.
1755 *
1756 * This function is called with the hbalock held to post a
1757 * hbq buffer to the firmware. If the function finds an empty
1758 * slot in the HBQ, it will post the buffer. The function will return
1759 * pointer to the hbq entry if it successfully post the buffer
1760 * else it will return NULL.
1761 **/
James Smart3772a992009-05-22 14:50:54 -04001762static int
James Smarted957682007-06-17 19:56:37 -05001763lpfc_sli_hbq_to_firmware(struct lpfc_hba *phba, uint32_t hbqno,
James Smart92d7f7b2007-06-17 19:56:38 -05001764 struct hbq_dmabuf *hbq_buf)
James Smarted957682007-06-17 19:56:37 -05001765{
James Smart3772a992009-05-22 14:50:54 -04001766 return phba->lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buf);
1767}
1768
1769/**
1770 * lpfc_sli_hbq_to_firmware_s3 - Post the hbq buffer to SLI3 firmware
1771 * @phba: Pointer to HBA context object.
1772 * @hbqno: HBQ number.
1773 * @hbq_buf: Pointer to HBQ buffer.
1774 *
1775 * This function is called with the hbalock held to post a hbq buffer to the
1776 * firmware. If the function finds an empty slot in the HBQ, it will post the
1777 * buffer and place it on the hbq_buffer_list. The function will return zero if
1778 * it successfully post the buffer else it will return an error.
1779 **/
1780static int
1781lpfc_sli_hbq_to_firmware_s3(struct lpfc_hba *phba, uint32_t hbqno,
1782 struct hbq_dmabuf *hbq_buf)
1783{
James Smarted957682007-06-17 19:56:37 -05001784 struct lpfc_hbq_entry *hbqe;
James Smart92d7f7b2007-06-17 19:56:38 -05001785 dma_addr_t physaddr = hbq_buf->dbuf.phys;
James Smarted957682007-06-17 19:56:37 -05001786
1787 /* Get next HBQ entry slot to use */
1788 hbqe = lpfc_sli_next_hbq_slot(phba, hbqno);
1789 if (hbqe) {
1790 struct hbq_s *hbqp = &phba->hbqs[hbqno];
1791
James Smart92d7f7b2007-06-17 19:56:38 -05001792 hbqe->bde.addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
1793 hbqe->bde.addrLow = le32_to_cpu(putPaddrLow(physaddr));
James Smart51ef4c22007-08-02 11:10:31 -04001794 hbqe->bde.tus.f.bdeSize = hbq_buf->size;
James Smarted957682007-06-17 19:56:37 -05001795 hbqe->bde.tus.f.bdeFlags = 0;
James Smart92d7f7b2007-06-17 19:56:38 -05001796 hbqe->bde.tus.w = le32_to_cpu(hbqe->bde.tus.w);
1797 hbqe->buffer_tag = le32_to_cpu(hbq_buf->tag);
1798 /* Sync SLIM */
James Smarted957682007-06-17 19:56:37 -05001799 hbqp->hbqPutIdx = hbqp->next_hbqPutIdx;
1800 writel(hbqp->hbqPutIdx, phba->hbq_put + hbqno);
James Smart92d7f7b2007-06-17 19:56:38 -05001801 /* flush */
James Smarted957682007-06-17 19:56:37 -05001802 readl(phba->hbq_put + hbqno);
James Smart51ef4c22007-08-02 11:10:31 -04001803 list_add_tail(&hbq_buf->dbuf.list, &hbqp->hbq_buffer_list);
James Smart3772a992009-05-22 14:50:54 -04001804 return 0;
1805 } else
1806 return -ENOMEM;
James Smarted957682007-06-17 19:56:37 -05001807}
1808
James Smart4f774512009-05-22 14:52:35 -04001809/**
1810 * lpfc_sli_hbq_to_firmware_s4 - Post the hbq buffer to SLI4 firmware
1811 * @phba: Pointer to HBA context object.
1812 * @hbqno: HBQ number.
1813 * @hbq_buf: Pointer to HBQ buffer.
1814 *
1815 * This function is called with the hbalock held to post an RQE to the SLI4
1816 * firmware. If able to post the RQE to the RQ it will queue the hbq entry to
1817 * the hbq_buffer_list and return zero, otherwise it will return an error.
1818 **/
1819static int
1820lpfc_sli_hbq_to_firmware_s4(struct lpfc_hba *phba, uint32_t hbqno,
1821 struct hbq_dmabuf *hbq_buf)
1822{
1823 int rc;
1824 struct lpfc_rqe hrqe;
1825 struct lpfc_rqe drqe;
1826
1827 hrqe.address_lo = putPaddrLow(hbq_buf->hbuf.phys);
1828 hrqe.address_hi = putPaddrHigh(hbq_buf->hbuf.phys);
1829 drqe.address_lo = putPaddrLow(hbq_buf->dbuf.phys);
1830 drqe.address_hi = putPaddrHigh(hbq_buf->dbuf.phys);
1831 rc = lpfc_sli4_rq_put(phba->sli4_hba.hdr_rq, phba->sli4_hba.dat_rq,
1832 &hrqe, &drqe);
1833 if (rc < 0)
1834 return rc;
1835 hbq_buf->tag = rc;
1836 list_add_tail(&hbq_buf->dbuf.list, &phba->hbqs[hbqno].hbq_buffer_list);
1837 return 0;
1838}
1839
James Smarte59058c2008-08-24 21:49:00 -04001840/* HBQ for ELS and CT traffic. */
James Smart92d7f7b2007-06-17 19:56:38 -05001841static struct lpfc_hbq_init lpfc_els_hbq = {
1842 .rn = 1,
James Smartdef9c7a2009-12-21 17:02:28 -05001843 .entry_count = 256,
James Smart92d7f7b2007-06-17 19:56:38 -05001844 .mask_count = 0,
1845 .profile = 0,
James Smart51ef4c22007-08-02 11:10:31 -04001846 .ring_mask = (1 << LPFC_ELS_RING),
James Smart92d7f7b2007-06-17 19:56:38 -05001847 .buffer_count = 0,
James Smarta257bf92009-04-06 18:48:10 -04001848 .init_count = 40,
1849 .add_count = 40,
James Smart92d7f7b2007-06-17 19:56:38 -05001850};
James Smarted957682007-06-17 19:56:37 -05001851
James Smarte59058c2008-08-24 21:49:00 -04001852/* HBQ for the extra ring if needed */
James Smart51ef4c22007-08-02 11:10:31 -04001853static struct lpfc_hbq_init lpfc_extra_hbq = {
1854 .rn = 1,
1855 .entry_count = 200,
1856 .mask_count = 0,
1857 .profile = 0,
1858 .ring_mask = (1 << LPFC_EXTRA_RING),
1859 .buffer_count = 0,
1860 .init_count = 0,
1861 .add_count = 5,
1862};
1863
James Smarte59058c2008-08-24 21:49:00 -04001864/* Array of HBQs */
James Smart78b2d852007-08-02 11:10:21 -04001865struct lpfc_hbq_init *lpfc_hbq_defs[] = {
James Smart92d7f7b2007-06-17 19:56:38 -05001866 &lpfc_els_hbq,
James Smart51ef4c22007-08-02 11:10:31 -04001867 &lpfc_extra_hbq,
James Smart92d7f7b2007-06-17 19:56:38 -05001868};
1869
James Smarte59058c2008-08-24 21:49:00 -04001870/**
James Smart3621a712009-04-06 18:47:14 -04001871 * lpfc_sli_hbqbuf_fill_hbqs - Post more hbq buffers to HBQ
James Smarte59058c2008-08-24 21:49:00 -04001872 * @phba: Pointer to HBA context object.
1873 * @hbqno: HBQ number.
1874 * @count: Number of HBQ buffers to be posted.
1875 *
James Smartd7c255b2008-08-24 21:50:00 -04001876 * This function is called with no lock held to post more hbq buffers to the
1877 * given HBQ. The function returns the number of HBQ buffers successfully
1878 * posted.
James Smarte59058c2008-08-24 21:49:00 -04001879 **/
James Smart311464e2007-08-02 11:10:37 -04001880static int
James Smart92d7f7b2007-06-17 19:56:38 -05001881lpfc_sli_hbqbuf_fill_hbqs(struct lpfc_hba *phba, uint32_t hbqno, uint32_t count)
1882{
James Smartd7c255b2008-08-24 21:50:00 -04001883 uint32_t i, posted = 0;
James Smart3163f722008-02-08 18:50:25 -05001884 unsigned long flags;
James Smart92d7f7b2007-06-17 19:56:38 -05001885 struct hbq_dmabuf *hbq_buffer;
James Smartd7c255b2008-08-24 21:50:00 -04001886 LIST_HEAD(hbq_buf_list);
Matthew Wilcoxeafe1df2008-02-21 05:44:33 -07001887 if (!phba->hbqs[hbqno].hbq_alloc_buffer)
James Smart51ef4c22007-08-02 11:10:31 -04001888 return 0;
James Smart51ef4c22007-08-02 11:10:31 -04001889
James Smartd7c255b2008-08-24 21:50:00 -04001890 if ((phba->hbqs[hbqno].buffer_count + count) >
1891 lpfc_hbq_defs[hbqno]->entry_count)
1892 count = lpfc_hbq_defs[hbqno]->entry_count -
1893 phba->hbqs[hbqno].buffer_count;
1894 if (!count)
1895 return 0;
1896 /* Allocate HBQ entries */
1897 for (i = 0; i < count; i++) {
1898 hbq_buffer = (phba->hbqs[hbqno].hbq_alloc_buffer)(phba);
1899 if (!hbq_buffer)
1900 break;
1901 list_add_tail(&hbq_buffer->dbuf.list, &hbq_buf_list);
1902 }
James Smart3163f722008-02-08 18:50:25 -05001903 /* Check whether HBQ is still in use */
1904 spin_lock_irqsave(&phba->hbalock, flags);
Matthew Wilcoxeafe1df2008-02-21 05:44:33 -07001905 if (!phba->hbq_in_use)
James Smartd7c255b2008-08-24 21:50:00 -04001906 goto err;
1907 while (!list_empty(&hbq_buf_list)) {
1908 list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
1909 dbuf.list);
1910 hbq_buffer->tag = (phba->hbqs[hbqno].buffer_count |
1911 (hbqno << 16));
James Smart3772a992009-05-22 14:50:54 -04001912 if (!lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer)) {
James Smarta8adb832007-10-27 13:37:53 -04001913 phba->hbqs[hbqno].buffer_count++;
James Smartd7c255b2008-08-24 21:50:00 -04001914 posted++;
1915 } else
James Smart51ef4c22007-08-02 11:10:31 -04001916 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
James Smart92d7f7b2007-06-17 19:56:38 -05001917 }
James Smart3163f722008-02-08 18:50:25 -05001918 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smartd7c255b2008-08-24 21:50:00 -04001919 return posted;
1920err:
1921 spin_unlock_irqrestore(&phba->hbalock, flags);
1922 while (!list_empty(&hbq_buf_list)) {
1923 list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
1924 dbuf.list);
1925 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
1926 }
James Smart92d7f7b2007-06-17 19:56:38 -05001927 return 0;
James Smarted957682007-06-17 19:56:37 -05001928}
1929
James Smarte59058c2008-08-24 21:49:00 -04001930/**
James Smart3621a712009-04-06 18:47:14 -04001931 * lpfc_sli_hbqbuf_add_hbqs - Post more HBQ buffers to firmware
James Smarte59058c2008-08-24 21:49:00 -04001932 * @phba: Pointer to HBA context object.
1933 * @qno: HBQ number.
1934 *
1935 * This function posts more buffers to the HBQ. This function
James Smartd7c255b2008-08-24 21:50:00 -04001936 * is called with no lock held. The function returns the number of HBQ entries
1937 * successfully allocated.
James Smarte59058c2008-08-24 21:49:00 -04001938 **/
James Smarted957682007-06-17 19:56:37 -05001939int
James Smart92d7f7b2007-06-17 19:56:38 -05001940lpfc_sli_hbqbuf_add_hbqs(struct lpfc_hba *phba, uint32_t qno)
James Smarted957682007-06-17 19:56:37 -05001941{
James Smartdef9c7a2009-12-21 17:02:28 -05001942 if (phba->sli_rev == LPFC_SLI_REV4)
1943 return 0;
1944 else
1945 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
1946 lpfc_hbq_defs[qno]->add_count);
James Smarted957682007-06-17 19:56:37 -05001947}
1948
James Smarte59058c2008-08-24 21:49:00 -04001949/**
James Smart3621a712009-04-06 18:47:14 -04001950 * lpfc_sli_hbqbuf_init_hbqs - Post initial buffers to the HBQ
James Smarte59058c2008-08-24 21:49:00 -04001951 * @phba: Pointer to HBA context object.
1952 * @qno: HBQ queue number.
1953 *
1954 * This function is called from SLI initialization code path with
1955 * no lock held to post initial HBQ buffers to firmware. The
James Smartd7c255b2008-08-24 21:50:00 -04001956 * function returns the number of HBQ entries successfully allocated.
James Smarte59058c2008-08-24 21:49:00 -04001957 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01001958static int
James Smart92d7f7b2007-06-17 19:56:38 -05001959lpfc_sli_hbqbuf_init_hbqs(struct lpfc_hba *phba, uint32_t qno)
James Smarted957682007-06-17 19:56:37 -05001960{
James Smartdef9c7a2009-12-21 17:02:28 -05001961 if (phba->sli_rev == LPFC_SLI_REV4)
1962 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
James Smart73d91e52011-10-10 21:32:10 -04001963 lpfc_hbq_defs[qno]->entry_count);
James Smartdef9c7a2009-12-21 17:02:28 -05001964 else
1965 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
1966 lpfc_hbq_defs[qno]->init_count);
James Smarted957682007-06-17 19:56:37 -05001967}
1968
James Smarte59058c2008-08-24 21:49:00 -04001969/**
James Smart3772a992009-05-22 14:50:54 -04001970 * lpfc_sli_hbqbuf_get - Remove the first hbq off of an hbq list
1971 * @phba: Pointer to HBA context object.
1972 * @hbqno: HBQ number.
1973 *
1974 * This function removes the first hbq buffer on an hbq list and returns a
1975 * pointer to that buffer. If it finds no buffers on the list it returns NULL.
1976 **/
1977static struct hbq_dmabuf *
1978lpfc_sli_hbqbuf_get(struct list_head *rb_list)
1979{
1980 struct lpfc_dmabuf *d_buf;
1981
1982 list_remove_head(rb_list, d_buf, struct lpfc_dmabuf, list);
1983 if (!d_buf)
1984 return NULL;
1985 return container_of(d_buf, struct hbq_dmabuf, dbuf);
1986}
1987
1988/**
James Smart3621a712009-04-06 18:47:14 -04001989 * lpfc_sli_hbqbuf_find - Find the hbq buffer associated with a tag
James Smarte59058c2008-08-24 21:49:00 -04001990 * @phba: Pointer to HBA context object.
1991 * @tag: Tag of the hbq buffer.
1992 *
1993 * This function is called with hbalock held. This function searches
1994 * for the hbq buffer associated with the given tag in the hbq buffer
1995 * list. If it finds the hbq buffer, it returns the hbq_buffer other wise
1996 * it returns NULL.
1997 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01001998static struct hbq_dmabuf *
James Smarted957682007-06-17 19:56:37 -05001999lpfc_sli_hbqbuf_find(struct lpfc_hba *phba, uint32_t tag)
2000{
James Smart92d7f7b2007-06-17 19:56:38 -05002001 struct lpfc_dmabuf *d_buf;
2002 struct hbq_dmabuf *hbq_buf;
James Smart51ef4c22007-08-02 11:10:31 -04002003 uint32_t hbqno;
James Smarted957682007-06-17 19:56:37 -05002004
James Smart51ef4c22007-08-02 11:10:31 -04002005 hbqno = tag >> 16;
Jesper Juhla0a74e452007-08-09 20:47:15 +02002006 if (hbqno >= LPFC_MAX_HBQS)
James Smart51ef4c22007-08-02 11:10:31 -04002007 return NULL;
2008
James Smart3772a992009-05-22 14:50:54 -04002009 spin_lock_irq(&phba->hbalock);
James Smart51ef4c22007-08-02 11:10:31 -04002010 list_for_each_entry(d_buf, &phba->hbqs[hbqno].hbq_buffer_list, list) {
James Smart92d7f7b2007-06-17 19:56:38 -05002011 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
James Smart51ef4c22007-08-02 11:10:31 -04002012 if (hbq_buf->tag == tag) {
James Smart3772a992009-05-22 14:50:54 -04002013 spin_unlock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -05002014 return hbq_buf;
James Smarted957682007-06-17 19:56:37 -05002015 }
2016 }
James Smart3772a992009-05-22 14:50:54 -04002017 spin_unlock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -05002018 lpfc_printf_log(phba, KERN_ERR, LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04002019 "1803 Bad hbq tag. Data: x%x x%x\n",
James Smarta8adb832007-10-27 13:37:53 -04002020 tag, phba->hbqs[tag >> 16].buffer_count);
James Smart92d7f7b2007-06-17 19:56:38 -05002021 return NULL;
James Smarted957682007-06-17 19:56:37 -05002022}
2023
James Smarte59058c2008-08-24 21:49:00 -04002024/**
James Smart3621a712009-04-06 18:47:14 -04002025 * lpfc_sli_free_hbq - Give back the hbq buffer to firmware
James Smarte59058c2008-08-24 21:49:00 -04002026 * @phba: Pointer to HBA context object.
2027 * @hbq_buffer: Pointer to HBQ buffer.
2028 *
2029 * This function is called with hbalock. This function gives back
2030 * the hbq buffer to firmware. If the HBQ does not have space to
2031 * post the buffer, it will free the buffer.
2032 **/
James Smarted957682007-06-17 19:56:37 -05002033void
James Smart51ef4c22007-08-02 11:10:31 -04002034lpfc_sli_free_hbq(struct lpfc_hba *phba, struct hbq_dmabuf *hbq_buffer)
James Smarted957682007-06-17 19:56:37 -05002035{
2036 uint32_t hbqno;
2037
James Smart51ef4c22007-08-02 11:10:31 -04002038 if (hbq_buffer) {
2039 hbqno = hbq_buffer->tag >> 16;
James Smart3772a992009-05-22 14:50:54 -04002040 if (lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer))
James Smart51ef4c22007-08-02 11:10:31 -04002041 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
James Smarted957682007-06-17 19:56:37 -05002042 }
2043}
2044
James Smarte59058c2008-08-24 21:49:00 -04002045/**
James Smart3621a712009-04-06 18:47:14 -04002046 * lpfc_sli_chk_mbx_command - Check if the mailbox is a legitimate mailbox
James Smarte59058c2008-08-24 21:49:00 -04002047 * @mbxCommand: mailbox command code.
2048 *
2049 * This function is called by the mailbox event handler function to verify
2050 * that the completed mailbox command is a legitimate mailbox command. If the
2051 * completed mailbox is not known to the function, it will return MBX_SHUTDOWN
2052 * and the mailbox event handler will take the HBA offline.
2053 **/
dea31012005-04-17 16:05:31 -05002054static int
2055lpfc_sli_chk_mbx_command(uint8_t mbxCommand)
2056{
2057 uint8_t ret;
2058
2059 switch (mbxCommand) {
2060 case MBX_LOAD_SM:
2061 case MBX_READ_NV:
2062 case MBX_WRITE_NV:
James Smarta8adb832007-10-27 13:37:53 -04002063 case MBX_WRITE_VPARMS:
dea31012005-04-17 16:05:31 -05002064 case MBX_RUN_BIU_DIAG:
2065 case MBX_INIT_LINK:
2066 case MBX_DOWN_LINK:
2067 case MBX_CONFIG_LINK:
2068 case MBX_CONFIG_RING:
2069 case MBX_RESET_RING:
2070 case MBX_READ_CONFIG:
2071 case MBX_READ_RCONFIG:
2072 case MBX_READ_SPARM:
2073 case MBX_READ_STATUS:
2074 case MBX_READ_RPI:
2075 case MBX_READ_XRI:
2076 case MBX_READ_REV:
2077 case MBX_READ_LNK_STAT:
2078 case MBX_REG_LOGIN:
2079 case MBX_UNREG_LOGIN:
dea31012005-04-17 16:05:31 -05002080 case MBX_CLEAR_LA:
2081 case MBX_DUMP_MEMORY:
2082 case MBX_DUMP_CONTEXT:
2083 case MBX_RUN_DIAGS:
2084 case MBX_RESTART:
2085 case MBX_UPDATE_CFG:
2086 case MBX_DOWN_LOAD:
2087 case MBX_DEL_LD_ENTRY:
2088 case MBX_RUN_PROGRAM:
2089 case MBX_SET_MASK:
James Smart09372822008-01-11 01:52:54 -05002090 case MBX_SET_VARIABLE:
dea31012005-04-17 16:05:31 -05002091 case MBX_UNREG_D_ID:
Jamie Wellnitz41415862006-02-28 19:25:27 -05002092 case MBX_KILL_BOARD:
dea31012005-04-17 16:05:31 -05002093 case MBX_CONFIG_FARP:
Jamie Wellnitz41415862006-02-28 19:25:27 -05002094 case MBX_BEACON:
dea31012005-04-17 16:05:31 -05002095 case MBX_LOAD_AREA:
2096 case MBX_RUN_BIU_DIAG64:
2097 case MBX_CONFIG_PORT:
2098 case MBX_READ_SPARM64:
2099 case MBX_READ_RPI64:
2100 case MBX_REG_LOGIN64:
James Smart76a95d72010-11-20 23:11:48 -05002101 case MBX_READ_TOPOLOGY:
James Smart09372822008-01-11 01:52:54 -05002102 case MBX_WRITE_WWN:
dea31012005-04-17 16:05:31 -05002103 case MBX_SET_DEBUG:
2104 case MBX_LOAD_EXP_ROM:
James Smart57127f12007-10-27 13:37:05 -04002105 case MBX_ASYNCEVT_ENABLE:
James Smart92d7f7b2007-06-17 19:56:38 -05002106 case MBX_REG_VPI:
2107 case MBX_UNREG_VPI:
James Smart858c9f62007-06-17 19:56:39 -05002108 case MBX_HEARTBEAT:
James Smart84774a42008-08-24 21:50:06 -04002109 case MBX_PORT_CAPABILITIES:
2110 case MBX_PORT_IOV_CONTROL:
James Smart04c68492009-05-22 14:52:52 -04002111 case MBX_SLI4_CONFIG:
2112 case MBX_SLI4_REQ_FTRS:
2113 case MBX_REG_FCFI:
2114 case MBX_UNREG_FCFI:
2115 case MBX_REG_VFI:
2116 case MBX_UNREG_VFI:
2117 case MBX_INIT_VPI:
2118 case MBX_INIT_VFI:
2119 case MBX_RESUME_RPI:
James Smartc7495932010-04-06 15:05:28 -04002120 case MBX_READ_EVENT_LOG_STATUS:
2121 case MBX_READ_EVENT_LOG:
James Smartdcf2a4e2010-09-29 11:18:53 -04002122 case MBX_SECURITY_MGMT:
2123 case MBX_AUTH_PORT:
James Smart940eb682012-08-03 12:37:08 -04002124 case MBX_ACCESS_VDATA:
dea31012005-04-17 16:05:31 -05002125 ret = mbxCommand;
2126 break;
2127 default:
2128 ret = MBX_SHUTDOWN;
2129 break;
2130 }
James Smart2e0fef82007-06-17 19:56:36 -05002131 return ret;
dea31012005-04-17 16:05:31 -05002132}
James Smarte59058c2008-08-24 21:49:00 -04002133
2134/**
James Smart3621a712009-04-06 18:47:14 -04002135 * lpfc_sli_wake_mbox_wait - lpfc_sli_issue_mbox_wait mbox completion handler
James Smarte59058c2008-08-24 21:49:00 -04002136 * @phba: Pointer to HBA context object.
2137 * @pmboxq: Pointer to mailbox command.
2138 *
2139 * This is completion handler function for mailbox commands issued from
2140 * lpfc_sli_issue_mbox_wait function. This function is called by the
2141 * mailbox event handler function with no lock held. This function
2142 * will wake up thread waiting on the wait queue pointed by context1
2143 * of the mailbox.
2144 **/
James Smart04c68492009-05-22 14:52:52 -04002145void
James Smart2e0fef82007-06-17 19:56:36 -05002146lpfc_sli_wake_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
dea31012005-04-17 16:05:31 -05002147{
2148 wait_queue_head_t *pdone_q;
James Smart858c9f62007-06-17 19:56:39 -05002149 unsigned long drvr_flag;
dea31012005-04-17 16:05:31 -05002150
2151 /*
2152 * If pdone_q is empty, the driver thread gave up waiting and
2153 * continued running.
2154 */
James Smart7054a602007-04-25 09:52:34 -04002155 pmboxq->mbox_flag |= LPFC_MBX_WAKE;
James Smart858c9f62007-06-17 19:56:39 -05002156 spin_lock_irqsave(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05002157 pdone_q = (wait_queue_head_t *) pmboxq->context1;
2158 if (pdone_q)
2159 wake_up_interruptible(pdone_q);
James Smart858c9f62007-06-17 19:56:39 -05002160 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05002161 return;
2162}
2163
James Smarte59058c2008-08-24 21:49:00 -04002164
2165/**
James Smart3621a712009-04-06 18:47:14 -04002166 * lpfc_sli_def_mbox_cmpl - Default mailbox completion handler
James Smarte59058c2008-08-24 21:49:00 -04002167 * @phba: Pointer to HBA context object.
2168 * @pmb: Pointer to mailbox object.
2169 *
2170 * This function is the default mailbox completion handler. It
2171 * frees the memory resources associated with the completed mailbox
2172 * command. If the completed command is a REG_LOGIN mailbox command,
2173 * this function will issue a UREG_LOGIN to re-claim the RPI.
2174 **/
dea31012005-04-17 16:05:31 -05002175void
James Smart2e0fef82007-06-17 19:56:36 -05002176lpfc_sli_def_mbox_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05002177{
James Smartd439d282010-09-29 11:18:45 -04002178 struct lpfc_vport *vport = pmb->vport;
dea31012005-04-17 16:05:31 -05002179 struct lpfc_dmabuf *mp;
James Smartd439d282010-09-29 11:18:45 -04002180 struct lpfc_nodelist *ndlp;
James Smart5af5eee2010-10-22 11:06:38 -04002181 struct Scsi_Host *shost;
James Smart04c68492009-05-22 14:52:52 -04002182 uint16_t rpi, vpi;
James Smart7054a602007-04-25 09:52:34 -04002183 int rc;
2184
dea31012005-04-17 16:05:31 -05002185 mp = (struct lpfc_dmabuf *) (pmb->context1);
James Smart7054a602007-04-25 09:52:34 -04002186
dea31012005-04-17 16:05:31 -05002187 if (mp) {
2188 lpfc_mbuf_free(phba, mp->virt, mp->phys);
2189 kfree(mp);
2190 }
James Smart7054a602007-04-25 09:52:34 -04002191
2192 /*
2193 * If a REG_LOGIN succeeded after node is destroyed or node
2194 * is in re-discovery driver need to cleanup the RPI.
2195 */
James Smart2e0fef82007-06-17 19:56:36 -05002196 if (!(phba->pport->load_flag & FC_UNLOADING) &&
James Smart04c68492009-05-22 14:52:52 -04002197 pmb->u.mb.mbxCommand == MBX_REG_LOGIN64 &&
2198 !pmb->u.mb.mbxStatus) {
2199 rpi = pmb->u.mb.un.varWords[0];
James Smart6d368e52011-05-24 11:44:12 -04002200 vpi = pmb->u.mb.un.varRegLogin.vpi;
James Smart04c68492009-05-22 14:52:52 -04002201 lpfc_unreg_login(phba, vpi, rpi, pmb);
James Smart92d7f7b2007-06-17 19:56:38 -05002202 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart7054a602007-04-25 09:52:34 -04002203 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
2204 if (rc != MBX_NOT_FINISHED)
2205 return;
2206 }
2207
James Smart695a8142010-01-26 23:08:03 -05002208 if ((pmb->u.mb.mbxCommand == MBX_REG_VPI) &&
2209 !(phba->pport->load_flag & FC_UNLOADING) &&
2210 !pmb->u.mb.mbxStatus) {
James Smart5af5eee2010-10-22 11:06:38 -04002211 shost = lpfc_shost_from_vport(vport);
2212 spin_lock_irq(shost->host_lock);
2213 vport->vpi_state |= LPFC_VPI_REGISTERED;
2214 vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
2215 spin_unlock_irq(shost->host_lock);
James Smart695a8142010-01-26 23:08:03 -05002216 }
2217
James Smartd439d282010-09-29 11:18:45 -04002218 if (pmb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
2219 ndlp = (struct lpfc_nodelist *)pmb->context2;
2220 lpfc_nlp_put(ndlp);
2221 pmb->context2 = NULL;
2222 }
2223
James Smartdcf2a4e2010-09-29 11:18:53 -04002224 /* Check security permission status on INIT_LINK mailbox command */
2225 if ((pmb->u.mb.mbxCommand == MBX_INIT_LINK) &&
2226 (pmb->u.mb.mbxStatus == MBXERR_SEC_NO_PERMISSION))
2227 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
2228 "2860 SLI authentication is required "
2229 "for INIT_LINK but has not done yet\n");
2230
James Smart04c68492009-05-22 14:52:52 -04002231 if (bf_get(lpfc_mqe_command, &pmb->u.mqe) == MBX_SLI4_CONFIG)
2232 lpfc_sli4_mbox_cmd_free(phba, pmb);
2233 else
2234 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05002235}
2236
James Smarte59058c2008-08-24 21:49:00 -04002237/**
James Smart3621a712009-04-06 18:47:14 -04002238 * lpfc_sli_handle_mb_event - Handle mailbox completions from firmware
James Smarte59058c2008-08-24 21:49:00 -04002239 * @phba: Pointer to HBA context object.
2240 *
2241 * This function is called with no lock held. This function processes all
2242 * the completed mailbox commands and gives it to upper layers. The interrupt
2243 * service routine processes mailbox completion interrupt and adds completed
2244 * mailbox commands to the mboxq_cmpl queue and signals the worker thread.
2245 * Worker thread call lpfc_sli_handle_mb_event, which will return the
2246 * completed mailbox commands in mboxq_cmpl queue to the upper layers. This
2247 * function returns the mailbox commands to the upper layer by calling the
2248 * completion handler function of each mailbox.
2249 **/
dea31012005-04-17 16:05:31 -05002250int
James Smart2e0fef82007-06-17 19:56:36 -05002251lpfc_sli_handle_mb_event(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05002252{
James Smart92d7f7b2007-06-17 19:56:38 -05002253 MAILBOX_t *pmbox;
dea31012005-04-17 16:05:31 -05002254 LPFC_MBOXQ_t *pmb;
James Smart92d7f7b2007-06-17 19:56:38 -05002255 int rc;
2256 LIST_HEAD(cmplq);
dea31012005-04-17 16:05:31 -05002257
2258 phba->sli.slistat.mbox_event++;
2259
James Smart92d7f7b2007-06-17 19:56:38 -05002260 /* Get all completed mailboxe buffers into the cmplq */
2261 spin_lock_irq(&phba->hbalock);
2262 list_splice_init(&phba->sli.mboxq_cmpl, &cmplq);
2263 spin_unlock_irq(&phba->hbalock);
2264
dea31012005-04-17 16:05:31 -05002265 /* Get a Mailbox buffer to setup mailbox commands for callback */
James Smart92d7f7b2007-06-17 19:56:38 -05002266 do {
2267 list_remove_head(&cmplq, pmb, LPFC_MBOXQ_t, list);
2268 if (pmb == NULL)
2269 break;
2270
James Smart04c68492009-05-22 14:52:52 -04002271 pmbox = &pmb->u.mb;
dea31012005-04-17 16:05:31 -05002272
James Smart858c9f62007-06-17 19:56:39 -05002273 if (pmbox->mbxCommand != MBX_HEARTBEAT) {
2274 if (pmb->vport) {
2275 lpfc_debugfs_disc_trc(pmb->vport,
2276 LPFC_DISC_TRC_MBOX_VPORT,
2277 "MBOX cmpl vport: cmd:x%x mb:x%x x%x",
2278 (uint32_t)pmbox->mbxCommand,
2279 pmbox->un.varWords[0],
2280 pmbox->un.varWords[1]);
2281 }
2282 else {
2283 lpfc_debugfs_disc_trc(phba->pport,
2284 LPFC_DISC_TRC_MBOX,
2285 "MBOX cmpl: cmd:x%x mb:x%x x%x",
2286 (uint32_t)pmbox->mbxCommand,
2287 pmbox->un.varWords[0],
2288 pmbox->un.varWords[1]);
2289 }
2290 }
2291
dea31012005-04-17 16:05:31 -05002292 /*
2293 * It is a fatal error if unknown mbox command completion.
2294 */
2295 if (lpfc_sli_chk_mbx_command(pmbox->mbxCommand) ==
2296 MBX_SHUTDOWN) {
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02002297 /* Unknown mailbox command compl */
James Smart92d7f7b2007-06-17 19:56:38 -05002298 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002299 "(%d):0323 Unknown Mailbox command "
James Smarta183a152011-10-10 21:32:43 -04002300 "x%x (x%x/x%x) Cmpl\n",
James Smart92d7f7b2007-06-17 19:56:38 -05002301 pmb->vport ? pmb->vport->vpi : 0,
James Smart04c68492009-05-22 14:52:52 -04002302 pmbox->mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04002303 lpfc_sli_config_mbox_subsys_get(phba,
2304 pmb),
2305 lpfc_sli_config_mbox_opcode_get(phba,
2306 pmb));
James Smart2e0fef82007-06-17 19:56:36 -05002307 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05002308 phba->work_hs = HS_FFER3;
2309 lpfc_handle_eratt(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05002310 continue;
dea31012005-04-17 16:05:31 -05002311 }
2312
dea31012005-04-17 16:05:31 -05002313 if (pmbox->mbxStatus) {
2314 phba->sli.slistat.mbox_stat_err++;
2315 if (pmbox->mbxStatus == MBXERR_NO_RESOURCES) {
2316 /* Mbox cmd cmpl error - RETRYing */
James Smart92d7f7b2007-06-17 19:56:38 -05002317 lpfc_printf_log(phba, KERN_INFO,
James Smarta183a152011-10-10 21:32:43 -04002318 LOG_MBOX | LOG_SLI,
2319 "(%d):0305 Mbox cmd cmpl "
2320 "error - RETRYing Data: x%x "
2321 "(x%x/x%x) x%x x%x x%x\n",
2322 pmb->vport ? pmb->vport->vpi : 0,
2323 pmbox->mbxCommand,
2324 lpfc_sli_config_mbox_subsys_get(phba,
2325 pmb),
2326 lpfc_sli_config_mbox_opcode_get(phba,
2327 pmb),
2328 pmbox->mbxStatus,
2329 pmbox->un.varWords[0],
2330 pmb->vport->port_state);
dea31012005-04-17 16:05:31 -05002331 pmbox->mbxStatus = 0;
2332 pmbox->mbxOwner = OWN_HOST;
dea31012005-04-17 16:05:31 -05002333 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
James Smart04c68492009-05-22 14:52:52 -04002334 if (rc != MBX_NOT_FINISHED)
James Smart92d7f7b2007-06-17 19:56:38 -05002335 continue;
dea31012005-04-17 16:05:31 -05002336 }
2337 }
2338
2339 /* Mailbox cmd <cmd> Cmpl <cmpl> */
James Smart92d7f7b2007-06-17 19:56:38 -05002340 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04002341 "(%d):0307 Mailbox cmd x%x (x%x/x%x) Cmpl x%p "
dea31012005-04-17 16:05:31 -05002342 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -05002343 pmb->vport ? pmb->vport->vpi : 0,
dea31012005-04-17 16:05:31 -05002344 pmbox->mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04002345 lpfc_sli_config_mbox_subsys_get(phba, pmb),
2346 lpfc_sli_config_mbox_opcode_get(phba, pmb),
dea31012005-04-17 16:05:31 -05002347 pmb->mbox_cmpl,
2348 *((uint32_t *) pmbox),
2349 pmbox->un.varWords[0],
2350 pmbox->un.varWords[1],
2351 pmbox->un.varWords[2],
2352 pmbox->un.varWords[3],
2353 pmbox->un.varWords[4],
2354 pmbox->un.varWords[5],
2355 pmbox->un.varWords[6],
2356 pmbox->un.varWords[7]);
2357
James Smart92d7f7b2007-06-17 19:56:38 -05002358 if (pmb->mbox_cmpl)
dea31012005-04-17 16:05:31 -05002359 pmb->mbox_cmpl(phba,pmb);
James Smart92d7f7b2007-06-17 19:56:38 -05002360 } while (1);
James Smart2e0fef82007-06-17 19:56:36 -05002361 return 0;
dea31012005-04-17 16:05:31 -05002362}
James Smart92d7f7b2007-06-17 19:56:38 -05002363
James Smarte59058c2008-08-24 21:49:00 -04002364/**
James Smart3621a712009-04-06 18:47:14 -04002365 * lpfc_sli_get_buff - Get the buffer associated with the buffer tag
James Smarte59058c2008-08-24 21:49:00 -04002366 * @phba: Pointer to HBA context object.
2367 * @pring: Pointer to driver SLI ring object.
2368 * @tag: buffer tag.
2369 *
2370 * This function is called with no lock held. When QUE_BUFTAG_BIT bit
2371 * is set in the tag the buffer is posted for a particular exchange,
2372 * the function will return the buffer without replacing the buffer.
2373 * If the buffer is for unsolicited ELS or CT traffic, this function
2374 * returns the buffer and also posts another buffer to the firmware.
2375 **/
James Smart76bb24e2007-10-27 13:38:00 -04002376static struct lpfc_dmabuf *
2377lpfc_sli_get_buff(struct lpfc_hba *phba,
James Smart9f1e1b52008-12-04 22:39:40 -05002378 struct lpfc_sli_ring *pring,
2379 uint32_t tag)
James Smart76bb24e2007-10-27 13:38:00 -04002380{
James Smart9f1e1b52008-12-04 22:39:40 -05002381 struct hbq_dmabuf *hbq_entry;
2382
James Smart76bb24e2007-10-27 13:38:00 -04002383 if (tag & QUE_BUFTAG_BIT)
2384 return lpfc_sli_ring_taggedbuf_get(phba, pring, tag);
James Smart9f1e1b52008-12-04 22:39:40 -05002385 hbq_entry = lpfc_sli_hbqbuf_find(phba, tag);
2386 if (!hbq_entry)
2387 return NULL;
2388 return &hbq_entry->dbuf;
James Smart76bb24e2007-10-27 13:38:00 -04002389}
James Smart57127f12007-10-27 13:37:05 -04002390
James Smart3772a992009-05-22 14:50:54 -04002391/**
2392 * lpfc_complete_unsol_iocb - Complete an unsolicited sequence
2393 * @phba: Pointer to HBA context object.
2394 * @pring: Pointer to driver SLI ring object.
2395 * @saveq: Pointer to the iocbq struct representing the sequence starting frame.
2396 * @fch_r_ctl: the r_ctl for the first frame of the sequence.
2397 * @fch_type: the type for the first frame of the sequence.
2398 *
2399 * This function is called with no lock held. This function uses the r_ctl and
2400 * type of the received sequence to find the correct callback function to call
2401 * to process the sequence.
2402 **/
2403static int
2404lpfc_complete_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2405 struct lpfc_iocbq *saveq, uint32_t fch_r_ctl,
2406 uint32_t fch_type)
2407{
2408 int i;
2409
2410 /* unSolicited Responses */
2411 if (pring->prt[0].profile) {
2412 if (pring->prt[0].lpfc_sli_rcv_unsol_event)
2413 (pring->prt[0].lpfc_sli_rcv_unsol_event) (phba, pring,
2414 saveq);
2415 return 1;
2416 }
2417 /* We must search, based on rctl / type
2418 for the right routine */
2419 for (i = 0; i < pring->num_mask; i++) {
2420 if ((pring->prt[i].rctl == fch_r_ctl) &&
2421 (pring->prt[i].type == fch_type)) {
2422 if (pring->prt[i].lpfc_sli_rcv_unsol_event)
2423 (pring->prt[i].lpfc_sli_rcv_unsol_event)
2424 (phba, pring, saveq);
2425 return 1;
2426 }
2427 }
2428 return 0;
2429}
James Smarte59058c2008-08-24 21:49:00 -04002430
2431/**
James Smart3621a712009-04-06 18:47:14 -04002432 * lpfc_sli_process_unsol_iocb - Unsolicited iocb handler
James Smarte59058c2008-08-24 21:49:00 -04002433 * @phba: Pointer to HBA context object.
2434 * @pring: Pointer to driver SLI ring object.
2435 * @saveq: Pointer to the unsolicited iocb.
2436 *
2437 * This function is called with no lock held by the ring event handler
2438 * when there is an unsolicited iocb posted to the response ring by the
2439 * firmware. This function gets the buffer associated with the iocbs
2440 * and calls the event handler for the ring. This function handles both
2441 * qring buffers and hbq buffers.
2442 * When the function returns 1 the caller can free the iocb object otherwise
2443 * upper layer functions will free the iocb objects.
2444 **/
dea31012005-04-17 16:05:31 -05002445static int
2446lpfc_sli_process_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2447 struct lpfc_iocbq *saveq)
2448{
2449 IOCB_t * irsp;
2450 WORD5 * w5p;
2451 uint32_t Rctl, Type;
James Smart3772a992009-05-22 14:50:54 -04002452 uint32_t match;
James Smart76bb24e2007-10-27 13:38:00 -04002453 struct lpfc_iocbq *iocbq;
James Smart3163f722008-02-08 18:50:25 -05002454 struct lpfc_dmabuf *dmzbuf;
dea31012005-04-17 16:05:31 -05002455
2456 match = 0;
2457 irsp = &(saveq->iocb);
James Smart57127f12007-10-27 13:37:05 -04002458
2459 if (irsp->ulpCommand == CMD_ASYNC_STATUS) {
2460 if (pring->lpfc_sli_rcv_async_status)
2461 pring->lpfc_sli_rcv_async_status(phba, pring, saveq);
2462 else
2463 lpfc_printf_log(phba,
2464 KERN_WARNING,
2465 LOG_SLI,
2466 "0316 Ring %d handler: unexpected "
2467 "ASYNC_STATUS iocb received evt_code "
2468 "0x%x\n",
2469 pring->ringno,
2470 irsp->un.asyncstat.evt_code);
2471 return 1;
2472 }
2473
James Smart3163f722008-02-08 18:50:25 -05002474 if ((irsp->ulpCommand == CMD_IOCB_RET_XRI64_CX) &&
2475 (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)) {
2476 if (irsp->ulpBdeCount > 0) {
2477 dmzbuf = lpfc_sli_get_buff(phba, pring,
2478 irsp->un.ulpWord[3]);
2479 lpfc_in_buf_free(phba, dmzbuf);
2480 }
2481
2482 if (irsp->ulpBdeCount > 1) {
2483 dmzbuf = lpfc_sli_get_buff(phba, pring,
2484 irsp->unsli3.sli3Words[3]);
2485 lpfc_in_buf_free(phba, dmzbuf);
2486 }
2487
2488 if (irsp->ulpBdeCount > 2) {
2489 dmzbuf = lpfc_sli_get_buff(phba, pring,
2490 irsp->unsli3.sli3Words[7]);
2491 lpfc_in_buf_free(phba, dmzbuf);
2492 }
2493
2494 return 1;
2495 }
2496
James Smart92d7f7b2007-06-17 19:56:38 -05002497 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
James Smart76bb24e2007-10-27 13:38:00 -04002498 if (irsp->ulpBdeCount != 0) {
2499 saveq->context2 = lpfc_sli_get_buff(phba, pring,
James Smart92d7f7b2007-06-17 19:56:38 -05002500 irsp->un.ulpWord[3]);
James Smart76bb24e2007-10-27 13:38:00 -04002501 if (!saveq->context2)
2502 lpfc_printf_log(phba,
2503 KERN_ERR,
2504 LOG_SLI,
2505 "0341 Ring %d Cannot find buffer for "
2506 "an unsolicited iocb. tag 0x%x\n",
2507 pring->ringno,
2508 irsp->un.ulpWord[3]);
James Smart76bb24e2007-10-27 13:38:00 -04002509 }
2510 if (irsp->ulpBdeCount == 2) {
2511 saveq->context3 = lpfc_sli_get_buff(phba, pring,
James Smart51ef4c22007-08-02 11:10:31 -04002512 irsp->unsli3.sli3Words[7]);
James Smart76bb24e2007-10-27 13:38:00 -04002513 if (!saveq->context3)
2514 lpfc_printf_log(phba,
2515 KERN_ERR,
2516 LOG_SLI,
2517 "0342 Ring %d Cannot find buffer for an"
2518 " unsolicited iocb. tag 0x%x\n",
2519 pring->ringno,
2520 irsp->unsli3.sli3Words[7]);
2521 }
2522 list_for_each_entry(iocbq, &saveq->list, list) {
James Smart76bb24e2007-10-27 13:38:00 -04002523 irsp = &(iocbq->iocb);
James Smart76bb24e2007-10-27 13:38:00 -04002524 if (irsp->ulpBdeCount != 0) {
2525 iocbq->context2 = lpfc_sli_get_buff(phba, pring,
2526 irsp->un.ulpWord[3]);
James Smart9c2face2008-01-11 01:53:18 -05002527 if (!iocbq->context2)
James Smart76bb24e2007-10-27 13:38:00 -04002528 lpfc_printf_log(phba,
2529 KERN_ERR,
2530 LOG_SLI,
2531 "0343 Ring %d Cannot find "
2532 "buffer for an unsolicited iocb"
2533 ". tag 0x%x\n", pring->ringno,
2534 irsp->un.ulpWord[3]);
2535 }
2536 if (irsp->ulpBdeCount == 2) {
2537 iocbq->context3 = lpfc_sli_get_buff(phba, pring,
2538 irsp->unsli3.sli3Words[7]);
James Smart9c2face2008-01-11 01:53:18 -05002539 if (!iocbq->context3)
James Smart76bb24e2007-10-27 13:38:00 -04002540 lpfc_printf_log(phba,
2541 KERN_ERR,
2542 LOG_SLI,
2543 "0344 Ring %d Cannot find "
2544 "buffer for an unsolicited "
2545 "iocb. tag 0x%x\n",
2546 pring->ringno,
2547 irsp->unsli3.sli3Words[7]);
2548 }
2549 }
James Smart92d7f7b2007-06-17 19:56:38 -05002550 }
James Smart9c2face2008-01-11 01:53:18 -05002551 if (irsp->ulpBdeCount != 0 &&
2552 (irsp->ulpCommand == CMD_IOCB_RCV_CONT64_CX ||
2553 irsp->ulpStatus == IOSTAT_INTERMED_RSP)) {
2554 int found = 0;
2555
2556 /* search continue save q for same XRI */
2557 list_for_each_entry(iocbq, &pring->iocb_continue_saveq, clist) {
James Smart7851fe22011-07-22 18:36:52 -04002558 if (iocbq->iocb.unsli3.rcvsli3.ox_id ==
2559 saveq->iocb.unsli3.rcvsli3.ox_id) {
James Smart9c2face2008-01-11 01:53:18 -05002560 list_add_tail(&saveq->list, &iocbq->list);
2561 found = 1;
2562 break;
2563 }
2564 }
2565 if (!found)
2566 list_add_tail(&saveq->clist,
2567 &pring->iocb_continue_saveq);
2568 if (saveq->iocb.ulpStatus != IOSTAT_INTERMED_RSP) {
2569 list_del_init(&iocbq->clist);
2570 saveq = iocbq;
2571 irsp = &(saveq->iocb);
2572 } else
2573 return 0;
2574 }
2575 if ((irsp->ulpCommand == CMD_RCV_ELS_REQ64_CX) ||
2576 (irsp->ulpCommand == CMD_RCV_ELS_REQ_CX) ||
2577 (irsp->ulpCommand == CMD_IOCB_RCV_ELS64_CX)) {
James Smart6a9c52c2009-10-02 15:16:51 -04002578 Rctl = FC_RCTL_ELS_REQ;
2579 Type = FC_TYPE_ELS;
James Smart9c2face2008-01-11 01:53:18 -05002580 } else {
2581 w5p = (WORD5 *)&(saveq->iocb.un.ulpWord[5]);
2582 Rctl = w5p->hcsw.Rctl;
2583 Type = w5p->hcsw.Type;
2584
2585 /* Firmware Workaround */
2586 if ((Rctl == 0) && (pring->ringno == LPFC_ELS_RING) &&
2587 (irsp->ulpCommand == CMD_RCV_SEQUENCE64_CX ||
2588 irsp->ulpCommand == CMD_IOCB_RCV_SEQ64_CX)) {
James Smart6a9c52c2009-10-02 15:16:51 -04002589 Rctl = FC_RCTL_ELS_REQ;
2590 Type = FC_TYPE_ELS;
James Smart9c2face2008-01-11 01:53:18 -05002591 w5p->hcsw.Rctl = Rctl;
2592 w5p->hcsw.Type = Type;
2593 }
2594 }
James Smart92d7f7b2007-06-17 19:56:38 -05002595
James Smart3772a992009-05-22 14:50:54 -04002596 if (!lpfc_complete_unsol_iocb(phba, pring, saveq, Rctl, Type))
James Smart92d7f7b2007-06-17 19:56:38 -05002597 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002598 "0313 Ring %d handler: unexpected Rctl x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05002599 "Type x%x received\n",
James Smarte8b62012007-08-02 11:10:09 -04002600 pring->ringno, Rctl, Type);
James Smart3772a992009-05-22 14:50:54 -04002601
James Smart92d7f7b2007-06-17 19:56:38 -05002602 return 1;
dea31012005-04-17 16:05:31 -05002603}
2604
James Smarte59058c2008-08-24 21:49:00 -04002605/**
James Smart3621a712009-04-06 18:47:14 -04002606 * lpfc_sli_iocbq_lookup - Find command iocb for the given response iocb
James Smarte59058c2008-08-24 21:49:00 -04002607 * @phba: Pointer to HBA context object.
2608 * @pring: Pointer to driver SLI ring object.
2609 * @prspiocb: Pointer to response iocb object.
2610 *
2611 * This function looks up the iocb_lookup table to get the command iocb
2612 * corresponding to the given response iocb using the iotag of the
2613 * response iocb. This function is called with the hbalock held.
2614 * This function returns the command iocb object if it finds the command
2615 * iocb else returns NULL.
2616 **/
dea31012005-04-17 16:05:31 -05002617static struct lpfc_iocbq *
James Smart2e0fef82007-06-17 19:56:36 -05002618lpfc_sli_iocbq_lookup(struct lpfc_hba *phba,
2619 struct lpfc_sli_ring *pring,
2620 struct lpfc_iocbq *prspiocb)
dea31012005-04-17 16:05:31 -05002621{
dea31012005-04-17 16:05:31 -05002622 struct lpfc_iocbq *cmd_iocb = NULL;
2623 uint16_t iotag;
2624
James Bottomley604a3e32005-10-29 10:28:33 -05002625 iotag = prspiocb->iocb.ulpIoTag;
dea31012005-04-17 16:05:31 -05002626
James Bottomley604a3e32005-10-29 10:28:33 -05002627 if (iotag != 0 && iotag <= phba->sli.last_iotag) {
2628 cmd_iocb = phba->sli.iocbq_lookup[iotag];
James Smart92d7f7b2007-06-17 19:56:38 -05002629 list_del_init(&cmd_iocb->list);
James Smart4f2e66c2012-05-09 21:17:07 -04002630 if (cmd_iocb->iocb_flag & LPFC_IO_ON_TXCMPLQ) {
James Smart4f2e66c2012-05-09 21:17:07 -04002631 cmd_iocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
James Smart2a9bf3d2010-06-07 15:24:45 -04002632 }
James Bottomley604a3e32005-10-29 10:28:33 -05002633 return cmd_iocb;
dea31012005-04-17 16:05:31 -05002634 }
2635
dea31012005-04-17 16:05:31 -05002636 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002637 "0317 iotag x%x is out off "
James Bottomley604a3e32005-10-29 10:28:33 -05002638 "range: max iotag x%x wd0 x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04002639 iotag, phba->sli.last_iotag,
James Bottomley604a3e32005-10-29 10:28:33 -05002640 *(((uint32_t *) &prspiocb->iocb) + 7));
dea31012005-04-17 16:05:31 -05002641 return NULL;
2642}
2643
James Smarte59058c2008-08-24 21:49:00 -04002644/**
James Smart3772a992009-05-22 14:50:54 -04002645 * lpfc_sli_iocbq_lookup_by_tag - Find command iocb for the iotag
2646 * @phba: Pointer to HBA context object.
2647 * @pring: Pointer to driver SLI ring object.
2648 * @iotag: IOCB tag.
2649 *
2650 * This function looks up the iocb_lookup table to get the command iocb
2651 * corresponding to the given iotag. This function is called with the
2652 * hbalock held.
2653 * This function returns the command iocb object if it finds the command
2654 * iocb else returns NULL.
2655 **/
2656static struct lpfc_iocbq *
2657lpfc_sli_iocbq_lookup_by_tag(struct lpfc_hba *phba,
2658 struct lpfc_sli_ring *pring, uint16_t iotag)
2659{
2660 struct lpfc_iocbq *cmd_iocb;
2661
2662 if (iotag != 0 && iotag <= phba->sli.last_iotag) {
2663 cmd_iocb = phba->sli.iocbq_lookup[iotag];
James Smart4f2e66c2012-05-09 21:17:07 -04002664 if (cmd_iocb->iocb_flag & LPFC_IO_ON_TXCMPLQ) {
2665 /* remove from txcmpl queue list */
2666 list_del_init(&cmd_iocb->list);
2667 cmd_iocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
James Smart4f2e66c2012-05-09 21:17:07 -04002668 return cmd_iocb;
James Smart2a9bf3d2010-06-07 15:24:45 -04002669 }
James Smart3772a992009-05-22 14:50:54 -04002670 }
James Smart3772a992009-05-22 14:50:54 -04002671 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2672 "0372 iotag x%x is out off range: max iotag (x%x)\n",
2673 iotag, phba->sli.last_iotag);
2674 return NULL;
2675}
2676
2677/**
James Smart3621a712009-04-06 18:47:14 -04002678 * lpfc_sli_process_sol_iocb - process solicited iocb completion
James Smarte59058c2008-08-24 21:49:00 -04002679 * @phba: Pointer to HBA context object.
2680 * @pring: Pointer to driver SLI ring object.
2681 * @saveq: Pointer to the response iocb to be processed.
2682 *
2683 * This function is called by the ring event handler for non-fcp
2684 * rings when there is a new response iocb in the response ring.
2685 * The caller is not required to hold any locks. This function
2686 * gets the command iocb associated with the response iocb and
2687 * calls the completion handler for the command iocb. If there
2688 * is no completion handler, the function will free the resources
2689 * associated with command iocb. If the response iocb is for
2690 * an already aborted command iocb, the status of the completion
2691 * is changed to IOSTAT_LOCAL_REJECT/IOERR_SLI_ABORTED.
2692 * This function always returns 1.
2693 **/
dea31012005-04-17 16:05:31 -05002694static int
James Smart2e0fef82007-06-17 19:56:36 -05002695lpfc_sli_process_sol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
dea31012005-04-17 16:05:31 -05002696 struct lpfc_iocbq *saveq)
2697{
James Smart2e0fef82007-06-17 19:56:36 -05002698 struct lpfc_iocbq *cmdiocbp;
dea31012005-04-17 16:05:31 -05002699 int rc = 1;
2700 unsigned long iflag;
2701
2702 /* Based on the iotag field, get the cmd IOCB from the txcmplq */
James Smart2e0fef82007-06-17 19:56:36 -05002703 spin_lock_irqsave(&phba->hbalock, iflag);
James Bottomley604a3e32005-10-29 10:28:33 -05002704 cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring, saveq);
James Smart2e0fef82007-06-17 19:56:36 -05002705 spin_unlock_irqrestore(&phba->hbalock, iflag);
2706
dea31012005-04-17 16:05:31 -05002707 if (cmdiocbp) {
2708 if (cmdiocbp->iocb_cmpl) {
2709 /*
James Smartea2151b2008-09-07 11:52:10 -04002710 * If an ELS command failed send an event to mgmt
2711 * application.
2712 */
2713 if (saveq->iocb.ulpStatus &&
2714 (pring->ringno == LPFC_ELS_RING) &&
2715 (cmdiocbp->iocb.ulpCommand ==
2716 CMD_ELS_REQUEST64_CR))
2717 lpfc_send_els_failure_event(phba,
2718 cmdiocbp, saveq);
2719
2720 /*
dea31012005-04-17 16:05:31 -05002721 * Post all ELS completions to the worker thread.
2722 * All other are passed to the completion callback.
2723 */
2724 if (pring->ringno == LPFC_ELS_RING) {
James Smart341af102010-01-26 23:07:37 -05002725 if ((phba->sli_rev < LPFC_SLI_REV4) &&
2726 (cmdiocbp->iocb_flag &
2727 LPFC_DRIVER_ABORTED)) {
2728 spin_lock_irqsave(&phba->hbalock,
2729 iflag);
James Smart07951072007-04-25 09:51:38 -04002730 cmdiocbp->iocb_flag &=
2731 ~LPFC_DRIVER_ABORTED;
James Smart341af102010-01-26 23:07:37 -05002732 spin_unlock_irqrestore(&phba->hbalock,
2733 iflag);
James Smart07951072007-04-25 09:51:38 -04002734 saveq->iocb.ulpStatus =
2735 IOSTAT_LOCAL_REJECT;
2736 saveq->iocb.un.ulpWord[4] =
2737 IOERR_SLI_ABORTED;
James Smart0ff10d42008-01-11 01:52:36 -05002738
2739 /* Firmware could still be in progress
2740 * of DMAing payload, so don't free data
2741 * buffer till after a hbeat.
2742 */
James Smart341af102010-01-26 23:07:37 -05002743 spin_lock_irqsave(&phba->hbalock,
2744 iflag);
James Smart0ff10d42008-01-11 01:52:36 -05002745 saveq->iocb_flag |= LPFC_DELAY_MEM_FREE;
James Smart341af102010-01-26 23:07:37 -05002746 spin_unlock_irqrestore(&phba->hbalock,
2747 iflag);
2748 }
James Smart0f65ff62010-02-26 14:14:23 -05002749 if (phba->sli_rev == LPFC_SLI_REV4) {
2750 if (saveq->iocb_flag &
2751 LPFC_EXCHANGE_BUSY) {
2752 /* Set cmdiocb flag for the
2753 * exchange busy so sgl (xri)
2754 * will not be released until
2755 * the abort xri is received
2756 * from hba.
2757 */
2758 spin_lock_irqsave(
2759 &phba->hbalock, iflag);
2760 cmdiocbp->iocb_flag |=
2761 LPFC_EXCHANGE_BUSY;
2762 spin_unlock_irqrestore(
2763 &phba->hbalock, iflag);
2764 }
2765 if (cmdiocbp->iocb_flag &
2766 LPFC_DRIVER_ABORTED) {
2767 /*
2768 * Clear LPFC_DRIVER_ABORTED
2769 * bit in case it was driver
2770 * initiated abort.
2771 */
2772 spin_lock_irqsave(
2773 &phba->hbalock, iflag);
2774 cmdiocbp->iocb_flag &=
2775 ~LPFC_DRIVER_ABORTED;
2776 spin_unlock_irqrestore(
2777 &phba->hbalock, iflag);
2778 cmdiocbp->iocb.ulpStatus =
2779 IOSTAT_LOCAL_REJECT;
2780 cmdiocbp->iocb.un.ulpWord[4] =
2781 IOERR_ABORT_REQUESTED;
2782 /*
2783 * For SLI4, irsiocb contains
2784 * NO_XRI in sli_xritag, it
2785 * shall not affect releasing
2786 * sgl (xri) process.
2787 */
2788 saveq->iocb.ulpStatus =
2789 IOSTAT_LOCAL_REJECT;
2790 saveq->iocb.un.ulpWord[4] =
2791 IOERR_SLI_ABORTED;
2792 spin_lock_irqsave(
2793 &phba->hbalock, iflag);
2794 saveq->iocb_flag |=
2795 LPFC_DELAY_MEM_FREE;
2796 spin_unlock_irqrestore(
2797 &phba->hbalock, iflag);
2798 }
James Smart07951072007-04-25 09:51:38 -04002799 }
dea31012005-04-17 16:05:31 -05002800 }
James Smart2e0fef82007-06-17 19:56:36 -05002801 (cmdiocbp->iocb_cmpl) (phba, cmdiocbp, saveq);
James Bottomley604a3e32005-10-29 10:28:33 -05002802 } else
2803 lpfc_sli_release_iocbq(phba, cmdiocbp);
dea31012005-04-17 16:05:31 -05002804 } else {
2805 /*
2806 * Unknown initiating command based on the response iotag.
2807 * This could be the case on the ELS ring because of
2808 * lpfc_els_abort().
2809 */
2810 if (pring->ringno != LPFC_ELS_RING) {
2811 /*
2812 * Ring <ringno> handler: unexpected completion IoTag
2813 * <IoTag>
2814 */
James Smarta257bf92009-04-06 18:48:10 -04002815 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002816 "0322 Ring %d handler: "
2817 "unexpected completion IoTag x%x "
2818 "Data: x%x x%x x%x x%x\n",
2819 pring->ringno,
2820 saveq->iocb.ulpIoTag,
2821 saveq->iocb.ulpStatus,
2822 saveq->iocb.un.ulpWord[4],
2823 saveq->iocb.ulpCommand,
2824 saveq->iocb.ulpContext);
dea31012005-04-17 16:05:31 -05002825 }
2826 }
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04002827
dea31012005-04-17 16:05:31 -05002828 return rc;
2829}
2830
James Smarte59058c2008-08-24 21:49:00 -04002831/**
James Smart3621a712009-04-06 18:47:14 -04002832 * lpfc_sli_rsp_pointers_error - Response ring pointer error handler
James Smarte59058c2008-08-24 21:49:00 -04002833 * @phba: Pointer to HBA context object.
2834 * @pring: Pointer to driver SLI ring object.
2835 *
2836 * This function is called from the iocb ring event handlers when
2837 * put pointer is ahead of the get pointer for a ring. This function signal
2838 * an error attention condition to the worker thread and the worker
2839 * thread will transition the HBA to offline state.
2840 **/
James Smart2e0fef82007-06-17 19:56:36 -05002841static void
2842lpfc_sli_rsp_pointers_error(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002843{
James Smart34b02dc2008-08-24 21:49:55 -04002844 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002845 /*
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02002846 * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002847 * rsp ring <portRspMax>
2848 */
2849 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002850 "0312 Ring %d handler: portRspPut %d "
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02002851 "is bigger than rsp ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04002852 pring->ringno, le32_to_cpu(pgp->rspPutInx),
James Smart7e56aa22012-08-03 12:35:34 -04002853 pring->sli.sli3.numRiocb);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002854
James Smart2e0fef82007-06-17 19:56:36 -05002855 phba->link_state = LPFC_HBA_ERROR;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002856
2857 /*
2858 * All error attention handlers are posted to
2859 * worker thread
2860 */
2861 phba->work_ha |= HA_ERATT;
2862 phba->work_hs = HS_FFER3;
James Smart92d7f7b2007-06-17 19:56:38 -05002863
James Smart5e9d9b82008-06-14 22:52:53 -04002864 lpfc_worker_wake_up(phba);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002865
2866 return;
2867}
2868
James Smarte59058c2008-08-24 21:49:00 -04002869/**
James Smart3621a712009-04-06 18:47:14 -04002870 * lpfc_poll_eratt - Error attention polling timer timeout handler
James Smart93996272008-08-24 21:50:30 -04002871 * @ptr: Pointer to address of HBA context object.
2872 *
2873 * This function is invoked by the Error Attention polling timer when the
2874 * timer times out. It will check the SLI Error Attention register for
2875 * possible attention events. If so, it will post an Error Attention event
2876 * and wake up worker thread to process it. Otherwise, it will set up the
2877 * Error Attention polling timer for the next poll.
2878 **/
2879void lpfc_poll_eratt(unsigned long ptr)
2880{
2881 struct lpfc_hba *phba;
James Smartaa6fbb72012-08-03 12:36:03 -04002882 uint32_t eratt = 0, rem;
2883 uint64_t sli_intr, cnt;
James Smart93996272008-08-24 21:50:30 -04002884
2885 phba = (struct lpfc_hba *)ptr;
2886
James Smartaa6fbb72012-08-03 12:36:03 -04002887 /* Here we will also keep track of interrupts per sec of the hba */
2888 sli_intr = phba->sli.slistat.sli_intr;
2889
2890 if (phba->sli.slistat.sli_prev_intr > sli_intr)
2891 cnt = (((uint64_t)(-1) - phba->sli.slistat.sli_prev_intr) +
2892 sli_intr);
2893 else
2894 cnt = (sli_intr - phba->sli.slistat.sli_prev_intr);
2895
2896 /* 64-bit integer division not supporte on 32-bit x86 - use do_div */
2897 rem = do_div(cnt, LPFC_ERATT_POLL_INTERVAL);
2898 phba->sli.slistat.sli_ips = cnt;
2899
2900 phba->sli.slistat.sli_prev_intr = sli_intr;
2901
James Smart93996272008-08-24 21:50:30 -04002902 /* Check chip HA register for error event */
2903 eratt = lpfc_sli_check_eratt(phba);
2904
2905 if (eratt)
2906 /* Tell the worker thread there is work to do */
2907 lpfc_worker_wake_up(phba);
2908 else
2909 /* Restart the timer for next eratt poll */
2910 mod_timer(&phba->eratt_poll, jiffies +
2911 HZ * LPFC_ERATT_POLL_INTERVAL);
2912 return;
2913}
2914
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002915
James Smarte59058c2008-08-24 21:49:00 -04002916/**
James Smart3621a712009-04-06 18:47:14 -04002917 * lpfc_sli_handle_fast_ring_event - Handle ring events on FCP ring
James Smarte59058c2008-08-24 21:49:00 -04002918 * @phba: Pointer to HBA context object.
2919 * @pring: Pointer to driver SLI ring object.
2920 * @mask: Host attention register mask for this ring.
2921 *
2922 * This function is called from the interrupt context when there is a ring
2923 * event for the fcp ring. The caller does not hold any lock.
2924 * The function processes each response iocb in the response ring until it
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002925 * finds an iocb with LE bit set and chains all the iocbs up to the iocb with
James Smarte59058c2008-08-24 21:49:00 -04002926 * LE bit set. The function will call the completion handler of the command iocb
2927 * if the response iocb indicates a completion for a command iocb or it is
2928 * an abort completion. The function will call lpfc_sli_process_unsol_iocb
2929 * function if this is an unsolicited iocb.
dea31012005-04-17 16:05:31 -05002930 * This routine presumes LPFC_FCP_RING handling and doesn't bother
James Smart45ed1192009-10-02 15:17:02 -04002931 * to check it explicitly.
2932 */
2933int
James Smart2e0fef82007-06-17 19:56:36 -05002934lpfc_sli_handle_fast_ring_event(struct lpfc_hba *phba,
2935 struct lpfc_sli_ring *pring, uint32_t mask)
dea31012005-04-17 16:05:31 -05002936{
James Smart34b02dc2008-08-24 21:49:55 -04002937 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
dea31012005-04-17 16:05:31 -05002938 IOCB_t *irsp = NULL;
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04002939 IOCB_t *entry = NULL;
dea31012005-04-17 16:05:31 -05002940 struct lpfc_iocbq *cmdiocbq = NULL;
2941 struct lpfc_iocbq rspiocbq;
dea31012005-04-17 16:05:31 -05002942 uint32_t status;
2943 uint32_t portRspPut, portRspMax;
2944 int rc = 1;
2945 lpfc_iocb_type type;
2946 unsigned long iflag;
2947 uint32_t rsp_cmpl = 0;
dea31012005-04-17 16:05:31 -05002948
James Smart2e0fef82007-06-17 19:56:36 -05002949 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05002950 pring->stats.iocb_event++;
2951
dea31012005-04-17 16:05:31 -05002952 /*
2953 * The next available response entry should never exceed the maximum
2954 * entries. If it does, treat it as an adapter hardware error.
2955 */
James Smart7e56aa22012-08-03 12:35:34 -04002956 portRspMax = pring->sli.sli3.numRiocb;
dea31012005-04-17 16:05:31 -05002957 portRspPut = le32_to_cpu(pgp->rspPutInx);
2958 if (unlikely(portRspPut >= portRspMax)) {
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002959 lpfc_sli_rsp_pointers_error(phba, pring);
James Smart2e0fef82007-06-17 19:56:36 -05002960 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05002961 return 1;
2962 }
James Smart45ed1192009-10-02 15:17:02 -04002963 if (phba->fcp_ring_in_use) {
2964 spin_unlock_irqrestore(&phba->hbalock, iflag);
2965 return 1;
2966 } else
2967 phba->fcp_ring_in_use = 1;
dea31012005-04-17 16:05:31 -05002968
2969 rmb();
James Smart7e56aa22012-08-03 12:35:34 -04002970 while (pring->sli.sli3.rspidx != portRspPut) {
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04002971 /*
2972 * Fetch an entry off the ring and copy it into a local data
2973 * structure. The copy involves a byte-swap since the
2974 * network byte order and pci byte orders are different.
2975 */
James Smarted957682007-06-17 19:56:37 -05002976 entry = lpfc_resp_iocb(phba, pring);
James Smart858c9f62007-06-17 19:56:39 -05002977 phba->last_completion_time = jiffies;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002978
James Smart7e56aa22012-08-03 12:35:34 -04002979 if (++pring->sli.sli3.rspidx >= portRspMax)
2980 pring->sli.sli3.rspidx = 0;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002981
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04002982 lpfc_sli_pcimem_bcopy((uint32_t *) entry,
2983 (uint32_t *) &rspiocbq.iocb,
James Smarted957682007-06-17 19:56:37 -05002984 phba->iocb_rsp_size);
James Smarta4bc3372006-12-02 13:34:16 -05002985 INIT_LIST_HEAD(&(rspiocbq.list));
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04002986 irsp = &rspiocbq.iocb;
2987
dea31012005-04-17 16:05:31 -05002988 type = lpfc_sli_iocb_cmd_type(irsp->ulpCommand & CMD_IOCB_MASK);
2989 pring->stats.iocb_rsp++;
2990 rsp_cmpl++;
2991
2992 if (unlikely(irsp->ulpStatus)) {
James Smart92d7f7b2007-06-17 19:56:38 -05002993 /*
2994 * If resource errors reported from HBA, reduce
2995 * queuedepths of the SCSI device.
2996 */
2997 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
James Smarte3d2b802012-08-14 14:25:43 -04002998 ((irsp->un.ulpWord[4] & IOERR_PARAM_MASK) ==
2999 IOERR_NO_RESOURCES)) {
James Smart92d7f7b2007-06-17 19:56:38 -05003000 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart3772a992009-05-22 14:50:54 -04003001 phba->lpfc_rampdown_queue_depth(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05003002 spin_lock_irqsave(&phba->hbalock, iflag);
3003 }
3004
dea31012005-04-17 16:05:31 -05003005 /* Rsp ring <ringno> error: IOCB */
3006 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003007 "0336 Rsp Ring %d error: IOCB Data: "
James Smart92d7f7b2007-06-17 19:56:38 -05003008 "x%x x%x x%x x%x x%x x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04003009 pring->ringno,
James Smart92d7f7b2007-06-17 19:56:38 -05003010 irsp->un.ulpWord[0],
3011 irsp->un.ulpWord[1],
3012 irsp->un.ulpWord[2],
3013 irsp->un.ulpWord[3],
3014 irsp->un.ulpWord[4],
3015 irsp->un.ulpWord[5],
James Smartd7c255b2008-08-24 21:50:00 -04003016 *(uint32_t *)&irsp->un1,
3017 *((uint32_t *)&irsp->un1 + 1));
dea31012005-04-17 16:05:31 -05003018 }
3019
3020 switch (type) {
3021 case LPFC_ABORT_IOCB:
3022 case LPFC_SOL_IOCB:
3023 /*
3024 * Idle exchange closed via ABTS from port. No iocb
3025 * resources need to be recovered.
3026 */
3027 if (unlikely(irsp->ulpCommand == CMD_XRI_ABORTED_CX)) {
James Smartdca94792006-08-01 07:34:08 -04003028 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003029 "0333 IOCB cmd 0x%x"
James Smartdca94792006-08-01 07:34:08 -04003030 " processed. Skipping"
James Smart92d7f7b2007-06-17 19:56:38 -05003031 " completion\n",
James Smartdca94792006-08-01 07:34:08 -04003032 irsp->ulpCommand);
dea31012005-04-17 16:05:31 -05003033 break;
3034 }
3035
James Bottomley604a3e32005-10-29 10:28:33 -05003036 cmdiocbq = lpfc_sli_iocbq_lookup(phba, pring,
3037 &rspiocbq);
James Smart0f65ff62010-02-26 14:14:23 -05003038 if (unlikely(!cmdiocbq))
3039 break;
3040 if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED)
3041 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
3042 if (cmdiocbq->iocb_cmpl) {
3043 spin_unlock_irqrestore(&phba->hbalock, iflag);
3044 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
3045 &rspiocbq);
3046 spin_lock_irqsave(&phba->hbalock, iflag);
3047 }
dea31012005-04-17 16:05:31 -05003048 break;
James Smarta4bc3372006-12-02 13:34:16 -05003049 case LPFC_UNSOL_IOCB:
James Smart2e0fef82007-06-17 19:56:36 -05003050 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta4bc3372006-12-02 13:34:16 -05003051 lpfc_sli_process_unsol_iocb(phba, pring, &rspiocbq);
James Smart2e0fef82007-06-17 19:56:36 -05003052 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta4bc3372006-12-02 13:34:16 -05003053 break;
dea31012005-04-17 16:05:31 -05003054 default:
3055 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
3056 char adaptermsg[LPFC_MAX_ADPTMSG];
3057 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
3058 memcpy(&adaptermsg[0], (uint8_t *) irsp,
3059 MAX_MSG_DATA);
Joe Perches898eb712007-10-18 03:06:30 -07003060 dev_warn(&((phba->pcidev)->dev),
3061 "lpfc%d: %s\n",
dea31012005-04-17 16:05:31 -05003062 phba->brd_no, adaptermsg);
3063 } else {
3064 /* Unknown IOCB command */
3065 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003066 "0334 Unknown IOCB command "
James Smart92d7f7b2007-06-17 19:56:38 -05003067 "Data: x%x, x%x x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04003068 type, irsp->ulpCommand,
James Smart92d7f7b2007-06-17 19:56:38 -05003069 irsp->ulpStatus,
3070 irsp->ulpIoTag,
3071 irsp->ulpContext);
dea31012005-04-17 16:05:31 -05003072 }
3073 break;
3074 }
3075
3076 /*
3077 * The response IOCB has been processed. Update the ring
3078 * pointer in SLIM. If the port response put pointer has not
3079 * been updated, sync the pgp->rspPutInx and fetch the new port
3080 * response put pointer.
3081 */
James Smart7e56aa22012-08-03 12:35:34 -04003082 writel(pring->sli.sli3.rspidx,
3083 &phba->host_gp[pring->ringno].rspGetInx);
dea31012005-04-17 16:05:31 -05003084
James Smart7e56aa22012-08-03 12:35:34 -04003085 if (pring->sli.sli3.rspidx == portRspPut)
dea31012005-04-17 16:05:31 -05003086 portRspPut = le32_to_cpu(pgp->rspPutInx);
3087 }
3088
3089 if ((rsp_cmpl > 0) && (mask & HA_R0RE_REQ)) {
3090 pring->stats.iocb_rsp_full++;
3091 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
3092 writel(status, phba->CAregaddr);
3093 readl(phba->CAregaddr);
3094 }
3095 if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
3096 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
3097 pring->stats.iocb_cmd_empty++;
3098
3099 /* Force update of the local copy of cmdGetInx */
James Smart7e56aa22012-08-03 12:35:34 -04003100 pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
dea31012005-04-17 16:05:31 -05003101 lpfc_sli_resume_iocb(phba, pring);
3102
3103 if ((pring->lpfc_sli_cmd_available))
3104 (pring->lpfc_sli_cmd_available) (phba, pring);
3105
3106 }
3107
James Smart45ed1192009-10-02 15:17:02 -04003108 phba->fcp_ring_in_use = 0;
James Smart2e0fef82007-06-17 19:56:36 -05003109 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003110 return rc;
3111}
3112
James Smarte59058c2008-08-24 21:49:00 -04003113/**
James Smart3772a992009-05-22 14:50:54 -04003114 * lpfc_sli_sp_handle_rspiocb - Handle slow-path response iocb
3115 * @phba: Pointer to HBA context object.
3116 * @pring: Pointer to driver SLI ring object.
3117 * @rspiocbp: Pointer to driver response IOCB object.
3118 *
3119 * This function is called from the worker thread when there is a slow-path
3120 * response IOCB to process. This function chains all the response iocbs until
3121 * seeing the iocb with the LE bit set. The function will call
3122 * lpfc_sli_process_sol_iocb function if the response iocb indicates a
3123 * completion of a command iocb. The function will call the
3124 * lpfc_sli_process_unsol_iocb function if this is an unsolicited iocb.
3125 * The function frees the resources or calls the completion handler if this
3126 * iocb is an abort completion. The function returns NULL when the response
3127 * iocb has the LE bit set and all the chained iocbs are processed, otherwise
3128 * this function shall chain the iocb on to the iocb_continueq and return the
3129 * response iocb passed in.
3130 **/
3131static struct lpfc_iocbq *
3132lpfc_sli_sp_handle_rspiocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
3133 struct lpfc_iocbq *rspiocbp)
3134{
3135 struct lpfc_iocbq *saveq;
3136 struct lpfc_iocbq *cmdiocbp;
3137 struct lpfc_iocbq *next_iocb;
3138 IOCB_t *irsp = NULL;
3139 uint32_t free_saveq;
3140 uint8_t iocb_cmd_type;
3141 lpfc_iocb_type type;
3142 unsigned long iflag;
3143 int rc;
3144
3145 spin_lock_irqsave(&phba->hbalock, iflag);
3146 /* First add the response iocb to the countinueq list */
3147 list_add_tail(&rspiocbp->list, &(pring->iocb_continueq));
3148 pring->iocb_continueq_cnt++;
3149
Justin P. Mattock70f23fd2011-05-10 10:16:21 +02003150 /* Now, determine whether the list is completed for processing */
James Smart3772a992009-05-22 14:50:54 -04003151 irsp = &rspiocbp->iocb;
3152 if (irsp->ulpLe) {
3153 /*
3154 * By default, the driver expects to free all resources
3155 * associated with this iocb completion.
3156 */
3157 free_saveq = 1;
3158 saveq = list_get_first(&pring->iocb_continueq,
3159 struct lpfc_iocbq, list);
3160 irsp = &(saveq->iocb);
3161 list_del_init(&pring->iocb_continueq);
3162 pring->iocb_continueq_cnt = 0;
3163
3164 pring->stats.iocb_rsp++;
3165
3166 /*
3167 * If resource errors reported from HBA, reduce
3168 * queuedepths of the SCSI device.
3169 */
3170 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
James Smarte3d2b802012-08-14 14:25:43 -04003171 ((irsp->un.ulpWord[4] & IOERR_PARAM_MASK) ==
3172 IOERR_NO_RESOURCES)) {
James Smart3772a992009-05-22 14:50:54 -04003173 spin_unlock_irqrestore(&phba->hbalock, iflag);
3174 phba->lpfc_rampdown_queue_depth(phba);
3175 spin_lock_irqsave(&phba->hbalock, iflag);
3176 }
3177
3178 if (irsp->ulpStatus) {
3179 /* Rsp ring <ringno> error: IOCB */
3180 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
3181 "0328 Rsp Ring %d error: "
3182 "IOCB Data: "
3183 "x%x x%x x%x x%x "
3184 "x%x x%x x%x x%x "
3185 "x%x x%x x%x x%x "
3186 "x%x x%x x%x x%x\n",
3187 pring->ringno,
3188 irsp->un.ulpWord[0],
3189 irsp->un.ulpWord[1],
3190 irsp->un.ulpWord[2],
3191 irsp->un.ulpWord[3],
3192 irsp->un.ulpWord[4],
3193 irsp->un.ulpWord[5],
3194 *(((uint32_t *) irsp) + 6),
3195 *(((uint32_t *) irsp) + 7),
3196 *(((uint32_t *) irsp) + 8),
3197 *(((uint32_t *) irsp) + 9),
3198 *(((uint32_t *) irsp) + 10),
3199 *(((uint32_t *) irsp) + 11),
3200 *(((uint32_t *) irsp) + 12),
3201 *(((uint32_t *) irsp) + 13),
3202 *(((uint32_t *) irsp) + 14),
3203 *(((uint32_t *) irsp) + 15));
3204 }
3205
3206 /*
3207 * Fetch the IOCB command type and call the correct completion
3208 * routine. Solicited and Unsolicited IOCBs on the ELS ring
3209 * get freed back to the lpfc_iocb_list by the discovery
3210 * kernel thread.
3211 */
3212 iocb_cmd_type = irsp->ulpCommand & CMD_IOCB_MASK;
3213 type = lpfc_sli_iocb_cmd_type(iocb_cmd_type);
3214 switch (type) {
3215 case LPFC_SOL_IOCB:
3216 spin_unlock_irqrestore(&phba->hbalock, iflag);
3217 rc = lpfc_sli_process_sol_iocb(phba, pring, saveq);
3218 spin_lock_irqsave(&phba->hbalock, iflag);
3219 break;
3220
3221 case LPFC_UNSOL_IOCB:
3222 spin_unlock_irqrestore(&phba->hbalock, iflag);
3223 rc = lpfc_sli_process_unsol_iocb(phba, pring, saveq);
3224 spin_lock_irqsave(&phba->hbalock, iflag);
3225 if (!rc)
3226 free_saveq = 0;
3227 break;
3228
3229 case LPFC_ABORT_IOCB:
3230 cmdiocbp = NULL;
3231 if (irsp->ulpCommand != CMD_XRI_ABORTED_CX)
3232 cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring,
3233 saveq);
3234 if (cmdiocbp) {
3235 /* Call the specified completion routine */
3236 if (cmdiocbp->iocb_cmpl) {
3237 spin_unlock_irqrestore(&phba->hbalock,
3238 iflag);
3239 (cmdiocbp->iocb_cmpl)(phba, cmdiocbp,
3240 saveq);
3241 spin_lock_irqsave(&phba->hbalock,
3242 iflag);
3243 } else
3244 __lpfc_sli_release_iocbq(phba,
3245 cmdiocbp);
3246 }
3247 break;
3248
3249 case LPFC_UNKNOWN_IOCB:
3250 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
3251 char adaptermsg[LPFC_MAX_ADPTMSG];
3252 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
3253 memcpy(&adaptermsg[0], (uint8_t *)irsp,
3254 MAX_MSG_DATA);
3255 dev_warn(&((phba->pcidev)->dev),
3256 "lpfc%d: %s\n",
3257 phba->brd_no, adaptermsg);
3258 } else {
3259 /* Unknown IOCB command */
3260 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3261 "0335 Unknown IOCB "
3262 "command Data: x%x "
3263 "x%x x%x x%x\n",
3264 irsp->ulpCommand,
3265 irsp->ulpStatus,
3266 irsp->ulpIoTag,
3267 irsp->ulpContext);
3268 }
3269 break;
3270 }
3271
3272 if (free_saveq) {
3273 list_for_each_entry_safe(rspiocbp, next_iocb,
3274 &saveq->list, list) {
3275 list_del(&rspiocbp->list);
3276 __lpfc_sli_release_iocbq(phba, rspiocbp);
3277 }
3278 __lpfc_sli_release_iocbq(phba, saveq);
3279 }
3280 rspiocbp = NULL;
3281 }
3282 spin_unlock_irqrestore(&phba->hbalock, iflag);
3283 return rspiocbp;
3284}
3285
3286/**
3287 * lpfc_sli_handle_slow_ring_event - Wrapper func for handling slow-path iocbs
James Smarte59058c2008-08-24 21:49:00 -04003288 * @phba: Pointer to HBA context object.
3289 * @pring: Pointer to driver SLI ring object.
3290 * @mask: Host attention register mask for this ring.
3291 *
James Smart3772a992009-05-22 14:50:54 -04003292 * This routine wraps the actual slow_ring event process routine from the
3293 * API jump table function pointer from the lpfc_hba struct.
James Smarte59058c2008-08-24 21:49:00 -04003294 **/
James Smart3772a992009-05-22 14:50:54 -04003295void
James Smart2e0fef82007-06-17 19:56:36 -05003296lpfc_sli_handle_slow_ring_event(struct lpfc_hba *phba,
3297 struct lpfc_sli_ring *pring, uint32_t mask)
dea31012005-04-17 16:05:31 -05003298{
James Smart3772a992009-05-22 14:50:54 -04003299 phba->lpfc_sli_handle_slow_ring_event(phba, pring, mask);
3300}
3301
3302/**
3303 * lpfc_sli_handle_slow_ring_event_s3 - Handle SLI3 ring event for non-FCP rings
3304 * @phba: Pointer to HBA context object.
3305 * @pring: Pointer to driver SLI ring object.
3306 * @mask: Host attention register mask for this ring.
3307 *
3308 * This function is called from the worker thread when there is a ring event
3309 * for non-fcp rings. The caller does not hold any lock. The function will
3310 * remove each response iocb in the response ring and calls the handle
3311 * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
3312 **/
3313static void
3314lpfc_sli_handle_slow_ring_event_s3(struct lpfc_hba *phba,
3315 struct lpfc_sli_ring *pring, uint32_t mask)
3316{
James Smart34b02dc2008-08-24 21:49:55 -04003317 struct lpfc_pgp *pgp;
dea31012005-04-17 16:05:31 -05003318 IOCB_t *entry;
3319 IOCB_t *irsp = NULL;
3320 struct lpfc_iocbq *rspiocbp = NULL;
dea31012005-04-17 16:05:31 -05003321 uint32_t portRspPut, portRspMax;
dea31012005-04-17 16:05:31 -05003322 unsigned long iflag;
James Smart3772a992009-05-22 14:50:54 -04003323 uint32_t status;
dea31012005-04-17 16:05:31 -05003324
James Smart34b02dc2008-08-24 21:49:55 -04003325 pgp = &phba->port_gp[pring->ringno];
James Smart2e0fef82007-06-17 19:56:36 -05003326 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003327 pring->stats.iocb_event++;
3328
dea31012005-04-17 16:05:31 -05003329 /*
3330 * The next available response entry should never exceed the maximum
3331 * entries. If it does, treat it as an adapter hardware error.
3332 */
James Smart7e56aa22012-08-03 12:35:34 -04003333 portRspMax = pring->sli.sli3.numRiocb;
dea31012005-04-17 16:05:31 -05003334 portRspPut = le32_to_cpu(pgp->rspPutInx);
3335 if (portRspPut >= portRspMax) {
3336 /*
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02003337 * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
dea31012005-04-17 16:05:31 -05003338 * rsp ring <portRspMax>
3339 */
James Smarted957682007-06-17 19:56:37 -05003340 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003341 "0303 Ring %d handler: portRspPut %d "
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02003342 "is bigger than rsp ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04003343 pring->ringno, portRspPut, portRspMax);
dea31012005-04-17 16:05:31 -05003344
James Smart2e0fef82007-06-17 19:56:36 -05003345 phba->link_state = LPFC_HBA_ERROR;
3346 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003347
3348 phba->work_hs = HS_FFER3;
3349 lpfc_handle_eratt(phba);
3350
James Smart3772a992009-05-22 14:50:54 -04003351 return;
dea31012005-04-17 16:05:31 -05003352 }
3353
3354 rmb();
James Smart7e56aa22012-08-03 12:35:34 -04003355 while (pring->sli.sli3.rspidx != portRspPut) {
dea31012005-04-17 16:05:31 -05003356 /*
3357 * Build a completion list and call the appropriate handler.
3358 * The process is to get the next available response iocb, get
3359 * a free iocb from the list, copy the response data into the
3360 * free iocb, insert to the continuation list, and update the
3361 * next response index to slim. This process makes response
3362 * iocb's in the ring available to DMA as fast as possible but
3363 * pays a penalty for a copy operation. Since the iocb is
3364 * only 32 bytes, this penalty is considered small relative to
3365 * the PCI reads for register values and a slim write. When
3366 * the ulpLe field is set, the entire Command has been
3367 * received.
3368 */
James Smarted957682007-06-17 19:56:37 -05003369 entry = lpfc_resp_iocb(phba, pring);
3370
James Smart858c9f62007-06-17 19:56:39 -05003371 phba->last_completion_time = jiffies;
James Smart2e0fef82007-06-17 19:56:36 -05003372 rspiocbp = __lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05003373 if (rspiocbp == NULL) {
3374 printk(KERN_ERR "%s: out of buffers! Failing "
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07003375 "completion.\n", __func__);
dea31012005-04-17 16:05:31 -05003376 break;
3377 }
3378
James Smarted957682007-06-17 19:56:37 -05003379 lpfc_sli_pcimem_bcopy(entry, &rspiocbp->iocb,
3380 phba->iocb_rsp_size);
dea31012005-04-17 16:05:31 -05003381 irsp = &rspiocbp->iocb;
3382
James Smart7e56aa22012-08-03 12:35:34 -04003383 if (++pring->sli.sli3.rspidx >= portRspMax)
3384 pring->sli.sli3.rspidx = 0;
dea31012005-04-17 16:05:31 -05003385
James Smarta58cbd52007-08-02 11:09:43 -04003386 if (pring->ringno == LPFC_ELS_RING) {
3387 lpfc_debugfs_slow_ring_trc(phba,
3388 "IOCB rsp ring: wd4:x%08x wd6:x%08x wd7:x%08x",
3389 *(((uint32_t *) irsp) + 4),
3390 *(((uint32_t *) irsp) + 6),
3391 *(((uint32_t *) irsp) + 7));
3392 }
3393
James Smart7e56aa22012-08-03 12:35:34 -04003394 writel(pring->sli.sli3.rspidx,
3395 &phba->host_gp[pring->ringno].rspGetInx);
dea31012005-04-17 16:05:31 -05003396
James Smart3772a992009-05-22 14:50:54 -04003397 spin_unlock_irqrestore(&phba->hbalock, iflag);
3398 /* Handle the response IOCB */
3399 rspiocbp = lpfc_sli_sp_handle_rspiocb(phba, pring, rspiocbp);
3400 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003401
3402 /*
3403 * If the port response put pointer has not been updated, sync
3404 * the pgp->rspPutInx in the MAILBOX_tand fetch the new port
3405 * response put pointer.
3406 */
James Smart7e56aa22012-08-03 12:35:34 -04003407 if (pring->sli.sli3.rspidx == portRspPut) {
dea31012005-04-17 16:05:31 -05003408 portRspPut = le32_to_cpu(pgp->rspPutInx);
3409 }
James Smart7e56aa22012-08-03 12:35:34 -04003410 } /* while (pring->sli.sli3.rspidx != portRspPut) */
dea31012005-04-17 16:05:31 -05003411
James Smart92d7f7b2007-06-17 19:56:38 -05003412 if ((rspiocbp != NULL) && (mask & HA_R0RE_REQ)) {
dea31012005-04-17 16:05:31 -05003413 /* At least one response entry has been freed */
3414 pring->stats.iocb_rsp_full++;
3415 /* SET RxRE_RSP in Chip Att register */
3416 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
3417 writel(status, phba->CAregaddr);
3418 readl(phba->CAregaddr); /* flush */
3419 }
3420 if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
3421 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
3422 pring->stats.iocb_cmd_empty++;
3423
3424 /* Force update of the local copy of cmdGetInx */
James Smart7e56aa22012-08-03 12:35:34 -04003425 pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
dea31012005-04-17 16:05:31 -05003426 lpfc_sli_resume_iocb(phba, pring);
3427
3428 if ((pring->lpfc_sli_cmd_available))
3429 (pring->lpfc_sli_cmd_available) (phba, pring);
3430
3431 }
3432
James Smart2e0fef82007-06-17 19:56:36 -05003433 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart3772a992009-05-22 14:50:54 -04003434 return;
dea31012005-04-17 16:05:31 -05003435}
3436
James Smarte59058c2008-08-24 21:49:00 -04003437/**
James Smart4f774512009-05-22 14:52:35 -04003438 * lpfc_sli_handle_slow_ring_event_s4 - Handle SLI4 slow-path els events
3439 * @phba: Pointer to HBA context object.
3440 * @pring: Pointer to driver SLI ring object.
3441 * @mask: Host attention register mask for this ring.
3442 *
3443 * This function is called from the worker thread when there is a pending
3444 * ELS response iocb on the driver internal slow-path response iocb worker
3445 * queue. The caller does not hold any lock. The function will remove each
3446 * response iocb from the response worker queue and calls the handle
3447 * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
3448 **/
3449static void
3450lpfc_sli_handle_slow_ring_event_s4(struct lpfc_hba *phba,
3451 struct lpfc_sli_ring *pring, uint32_t mask)
3452{
3453 struct lpfc_iocbq *irspiocbq;
James Smart4d9ab992009-10-02 15:16:39 -04003454 struct hbq_dmabuf *dmabuf;
3455 struct lpfc_cq_event *cq_event;
James Smart4f774512009-05-22 14:52:35 -04003456 unsigned long iflag;
3457
James Smart45ed1192009-10-02 15:17:02 -04003458 spin_lock_irqsave(&phba->hbalock, iflag);
3459 phba->hba_flag &= ~HBA_SP_QUEUE_EVT;
3460 spin_unlock_irqrestore(&phba->hbalock, iflag);
3461 while (!list_empty(&phba->sli4_hba.sp_queue_event)) {
James Smart4f774512009-05-22 14:52:35 -04003462 /* Get the response iocb from the head of work queue */
3463 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart45ed1192009-10-02 15:17:02 -04003464 list_remove_head(&phba->sli4_hba.sp_queue_event,
James Smart4d9ab992009-10-02 15:16:39 -04003465 cq_event, struct lpfc_cq_event, list);
James Smart4f774512009-05-22 14:52:35 -04003466 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart4d9ab992009-10-02 15:16:39 -04003467
3468 switch (bf_get(lpfc_wcqe_c_code, &cq_event->cqe.wcqe_cmpl)) {
3469 case CQE_CODE_COMPL_WQE:
3470 irspiocbq = container_of(cq_event, struct lpfc_iocbq,
3471 cq_event);
James Smart45ed1192009-10-02 15:17:02 -04003472 /* Translate ELS WCQE to response IOCBQ */
3473 irspiocbq = lpfc_sli4_els_wcqe_to_rspiocbq(phba,
3474 irspiocbq);
3475 if (irspiocbq)
3476 lpfc_sli_sp_handle_rspiocb(phba, pring,
3477 irspiocbq);
James Smart4d9ab992009-10-02 15:16:39 -04003478 break;
3479 case CQE_CODE_RECEIVE:
James Smart7851fe22011-07-22 18:36:52 -04003480 case CQE_CODE_RECEIVE_V1:
James Smart4d9ab992009-10-02 15:16:39 -04003481 dmabuf = container_of(cq_event, struct hbq_dmabuf,
3482 cq_event);
3483 lpfc_sli4_handle_received_buffer(phba, dmabuf);
3484 break;
3485 default:
3486 break;
3487 }
James Smart4f774512009-05-22 14:52:35 -04003488 }
3489}
3490
3491/**
James Smart3621a712009-04-06 18:47:14 -04003492 * lpfc_sli_abort_iocb_ring - Abort all iocbs in the ring
James Smarte59058c2008-08-24 21:49:00 -04003493 * @phba: Pointer to HBA context object.
3494 * @pring: Pointer to driver SLI ring object.
3495 *
3496 * This function aborts all iocbs in the given ring and frees all the iocb
3497 * objects in txq. This function issues an abort iocb for all the iocb commands
3498 * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
3499 * the return of this function. The caller is not required to hold any locks.
3500 **/
James Smart2e0fef82007-06-17 19:56:36 -05003501void
dea31012005-04-17 16:05:31 -05003502lpfc_sli_abort_iocb_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
3503{
James Smart2534ba72007-04-25 09:52:20 -04003504 LIST_HEAD(completions);
dea31012005-04-17 16:05:31 -05003505 struct lpfc_iocbq *iocb, *next_iocb;
dea31012005-04-17 16:05:31 -05003506
James Smart92d7f7b2007-06-17 19:56:38 -05003507 if (pring->ringno == LPFC_ELS_RING) {
3508 lpfc_fabric_abort_hba(phba);
3509 }
3510
dea31012005-04-17 16:05:31 -05003511 /* Error everything on txq and txcmplq
3512 * First do the txq.
3513 */
James Smart2e0fef82007-06-17 19:56:36 -05003514 spin_lock_irq(&phba->hbalock);
James Smart2534ba72007-04-25 09:52:20 -04003515 list_splice_init(&pring->txq, &completions);
dea31012005-04-17 16:05:31 -05003516
3517 /* Next issue ABTS for everything on the txcmplq */
James Smart2534ba72007-04-25 09:52:20 -04003518 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
3519 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
3520
James Smart2e0fef82007-06-17 19:56:36 -05003521 spin_unlock_irq(&phba->hbalock);
James Smart2534ba72007-04-25 09:52:20 -04003522
James Smarta257bf92009-04-06 18:48:10 -04003523 /* Cancel all the IOCBs from the completions list */
3524 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
3525 IOERR_SLI_ABORTED);
dea31012005-04-17 16:05:31 -05003526}
3527
James Smarte59058c2008-08-24 21:49:00 -04003528/**
James Smart3621a712009-04-06 18:47:14 -04003529 * lpfc_sli_flush_fcp_rings - flush all iocbs in the fcp ring
James Smarta8e497d2008-08-24 21:50:11 -04003530 * @phba: Pointer to HBA context object.
3531 *
3532 * This function flushes all iocbs in the fcp ring and frees all the iocb
3533 * objects in txq and txcmplq. This function will not issue abort iocbs
3534 * for all the iocb commands in txcmplq, they will just be returned with
3535 * IOERR_SLI_DOWN. This function is invoked with EEH when device's PCI
3536 * slot has been permanently disabled.
3537 **/
3538void
3539lpfc_sli_flush_fcp_rings(struct lpfc_hba *phba)
3540{
3541 LIST_HEAD(txq);
3542 LIST_HEAD(txcmplq);
James Smarta8e497d2008-08-24 21:50:11 -04003543 struct lpfc_sli *psli = &phba->sli;
3544 struct lpfc_sli_ring *pring;
3545
3546 /* Currently, only one fcp ring */
3547 pring = &psli->ring[psli->fcp_ring];
3548
3549 spin_lock_irq(&phba->hbalock);
3550 /* Retrieve everything on txq */
3551 list_splice_init(&pring->txq, &txq);
James Smarta8e497d2008-08-24 21:50:11 -04003552
3553 /* Retrieve everything on the txcmplq */
3554 list_splice_init(&pring->txcmplq, &txcmplq);
James Smart4f2e66c2012-05-09 21:17:07 -04003555
3556 /* Indicate the I/O queues are flushed */
3557 phba->hba_flag |= HBA_FCP_IOQ_FLUSH;
James Smarta8e497d2008-08-24 21:50:11 -04003558 spin_unlock_irq(&phba->hbalock);
3559
3560 /* Flush the txq */
James Smarta257bf92009-04-06 18:48:10 -04003561 lpfc_sli_cancel_iocbs(phba, &txq, IOSTAT_LOCAL_REJECT,
3562 IOERR_SLI_DOWN);
James Smarta8e497d2008-08-24 21:50:11 -04003563
3564 /* Flush the txcmpq */
James Smarta257bf92009-04-06 18:48:10 -04003565 lpfc_sli_cancel_iocbs(phba, &txcmplq, IOSTAT_LOCAL_REJECT,
3566 IOERR_SLI_DOWN);
James Smarta8e497d2008-08-24 21:50:11 -04003567}
3568
3569/**
James Smart3772a992009-05-22 14:50:54 -04003570 * lpfc_sli_brdready_s3 - Check for sli3 host ready status
James Smarte59058c2008-08-24 21:49:00 -04003571 * @phba: Pointer to HBA context object.
3572 * @mask: Bit mask to be checked.
3573 *
3574 * This function reads the host status register and compares
3575 * with the provided bit mask to check if HBA completed
3576 * the restart. This function will wait in a loop for the
3577 * HBA to complete restart. If the HBA does not restart within
3578 * 15 iterations, the function will reset the HBA again. The
3579 * function returns 1 when HBA fail to restart otherwise returns
3580 * zero.
3581 **/
James Smart3772a992009-05-22 14:50:54 -04003582static int
3583lpfc_sli_brdready_s3(struct lpfc_hba *phba, uint32_t mask)
dea31012005-04-17 16:05:31 -05003584{
Jamie Wellnitz41415862006-02-28 19:25:27 -05003585 uint32_t status;
3586 int i = 0;
3587 int retval = 0;
dea31012005-04-17 16:05:31 -05003588
Jamie Wellnitz41415862006-02-28 19:25:27 -05003589 /* Read the HBA Host Status Register */
James Smart9940b972011-03-11 16:06:12 -05003590 if (lpfc_readl(phba->HSregaddr, &status))
3591 return 1;
dea31012005-04-17 16:05:31 -05003592
Jamie Wellnitz41415862006-02-28 19:25:27 -05003593 /*
3594 * Check status register every 100ms for 5 retries, then every
3595 * 500ms for 5, then every 2.5 sec for 5, then reset board and
3596 * every 2.5 sec for 4.
3597 * Break our of the loop if errors occurred during init.
3598 */
3599 while (((status & mask) != mask) &&
3600 !(status & HS_FFERM) &&
3601 i++ < 20) {
dea31012005-04-17 16:05:31 -05003602
Jamie Wellnitz41415862006-02-28 19:25:27 -05003603 if (i <= 5)
3604 msleep(10);
3605 else if (i <= 10)
3606 msleep(500);
3607 else
3608 msleep(2500);
dea31012005-04-17 16:05:31 -05003609
Jamie Wellnitz41415862006-02-28 19:25:27 -05003610 if (i == 15) {
James Smart2e0fef82007-06-17 19:56:36 -05003611 /* Do post */
James Smart92d7f7b2007-06-17 19:56:38 -05003612 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003613 lpfc_sli_brdrestart(phba);
3614 }
3615 /* Read the HBA Host Status Register */
James Smart9940b972011-03-11 16:06:12 -05003616 if (lpfc_readl(phba->HSregaddr, &status)) {
3617 retval = 1;
3618 break;
3619 }
dea31012005-04-17 16:05:31 -05003620 }
dea31012005-04-17 16:05:31 -05003621
Jamie Wellnitz41415862006-02-28 19:25:27 -05003622 /* Check to see if any errors occurred during init */
3623 if ((status & HS_FFERM) || (i >= 20)) {
James Smarte40a02c2010-02-26 14:13:54 -05003624 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3625 "2751 Adapter failed to restart, "
3626 "status reg x%x, FW Data: A8 x%x AC x%x\n",
3627 status,
3628 readl(phba->MBslimaddr + 0xa8),
3629 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05003630 phba->link_state = LPFC_HBA_ERROR;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003631 retval = 1;
3632 }
dea31012005-04-17 16:05:31 -05003633
Jamie Wellnitz41415862006-02-28 19:25:27 -05003634 return retval;
dea31012005-04-17 16:05:31 -05003635}
3636
James Smartda0436e2009-05-22 14:51:39 -04003637/**
3638 * lpfc_sli_brdready_s4 - Check for sli4 host ready status
3639 * @phba: Pointer to HBA context object.
3640 * @mask: Bit mask to be checked.
3641 *
3642 * This function checks the host status register to check if HBA is
3643 * ready. This function will wait in a loop for the HBA to be ready
3644 * If the HBA is not ready , the function will will reset the HBA PCI
3645 * function again. The function returns 1 when HBA fail to be ready
3646 * otherwise returns zero.
3647 **/
3648static int
3649lpfc_sli_brdready_s4(struct lpfc_hba *phba, uint32_t mask)
3650{
3651 uint32_t status;
3652 int retval = 0;
3653
3654 /* Read the HBA Host Status Register */
3655 status = lpfc_sli4_post_status_check(phba);
3656
3657 if (status) {
3658 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
3659 lpfc_sli_brdrestart(phba);
3660 status = lpfc_sli4_post_status_check(phba);
3661 }
3662
3663 /* Check to see if any errors occurred during init */
3664 if (status) {
3665 phba->link_state = LPFC_HBA_ERROR;
3666 retval = 1;
3667 } else
3668 phba->sli4_hba.intr_enable = 0;
3669
3670 return retval;
3671}
3672
3673/**
3674 * lpfc_sli_brdready - Wrapper func for checking the hba readyness
3675 * @phba: Pointer to HBA context object.
3676 * @mask: Bit mask to be checked.
3677 *
3678 * This routine wraps the actual SLI3 or SLI4 hba readyness check routine
3679 * from the API jump table function pointer from the lpfc_hba struct.
3680 **/
3681int
3682lpfc_sli_brdready(struct lpfc_hba *phba, uint32_t mask)
3683{
3684 return phba->lpfc_sli_brdready(phba, mask);
3685}
3686
James Smart92908312006-03-07 15:04:13 -05003687#define BARRIER_TEST_PATTERN (0xdeadbeef)
3688
James Smarte59058c2008-08-24 21:49:00 -04003689/**
James Smart3621a712009-04-06 18:47:14 -04003690 * lpfc_reset_barrier - Make HBA ready for HBA reset
James Smarte59058c2008-08-24 21:49:00 -04003691 * @phba: Pointer to HBA context object.
3692 *
James Smart1b511972011-12-13 13:23:09 -05003693 * This function is called before resetting an HBA. This function is called
3694 * with hbalock held and requests HBA to quiesce DMAs before a reset.
James Smarte59058c2008-08-24 21:49:00 -04003695 **/
James Smart2e0fef82007-06-17 19:56:36 -05003696void lpfc_reset_barrier(struct lpfc_hba *phba)
James Smart92908312006-03-07 15:04:13 -05003697{
James Smart65a29c12006-07-06 15:50:50 -04003698 uint32_t __iomem *resp_buf;
3699 uint32_t __iomem *mbox_buf;
James Smart92908312006-03-07 15:04:13 -05003700 volatile uint32_t mbox;
James Smart9940b972011-03-11 16:06:12 -05003701 uint32_t hc_copy, ha_copy, resp_data;
James Smart92908312006-03-07 15:04:13 -05003702 int i;
3703 uint8_t hdrtype;
3704
3705 pci_read_config_byte(phba->pcidev, PCI_HEADER_TYPE, &hdrtype);
3706 if (hdrtype != 0x80 ||
3707 (FC_JEDEC_ID(phba->vpd.rev.biuRev) != HELIOS_JEDEC_ID &&
3708 FC_JEDEC_ID(phba->vpd.rev.biuRev) != THOR_JEDEC_ID))
3709 return;
3710
3711 /*
3712 * Tell the other part of the chip to suspend temporarily all
3713 * its DMA activity.
3714 */
James Smart65a29c12006-07-06 15:50:50 -04003715 resp_buf = phba->MBslimaddr;
James Smart92908312006-03-07 15:04:13 -05003716
3717 /* Disable the error attention */
James Smart9940b972011-03-11 16:06:12 -05003718 if (lpfc_readl(phba->HCregaddr, &hc_copy))
3719 return;
James Smart92908312006-03-07 15:04:13 -05003720 writel((hc_copy & ~HC_ERINT_ENA), phba->HCregaddr);
3721 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05003722 phba->link_flag |= LS_IGNORE_ERATT;
James Smart92908312006-03-07 15:04:13 -05003723
James Smart9940b972011-03-11 16:06:12 -05003724 if (lpfc_readl(phba->HAregaddr, &ha_copy))
3725 return;
3726 if (ha_copy & HA_ERATT) {
James Smart92908312006-03-07 15:04:13 -05003727 /* Clear Chip error bit */
3728 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05003729 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05003730 }
3731
3732 mbox = 0;
3733 ((MAILBOX_t *)&mbox)->mbxCommand = MBX_KILL_BOARD;
3734 ((MAILBOX_t *)&mbox)->mbxOwner = OWN_CHIP;
3735
3736 writel(BARRIER_TEST_PATTERN, (resp_buf + 1));
James Smart65a29c12006-07-06 15:50:50 -04003737 mbox_buf = phba->MBslimaddr;
James Smart92908312006-03-07 15:04:13 -05003738 writel(mbox, mbox_buf);
3739
James Smart9940b972011-03-11 16:06:12 -05003740 for (i = 0; i < 50; i++) {
3741 if (lpfc_readl((resp_buf + 1), &resp_data))
3742 return;
3743 if (resp_data != ~(BARRIER_TEST_PATTERN))
3744 mdelay(1);
3745 else
3746 break;
3747 }
3748 resp_data = 0;
3749 if (lpfc_readl((resp_buf + 1), &resp_data))
3750 return;
3751 if (resp_data != ~(BARRIER_TEST_PATTERN)) {
James Smartf4b4c682009-05-22 14:53:12 -04003752 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE ||
James Smart2e0fef82007-06-17 19:56:36 -05003753 phba->pport->stopped)
James Smart92908312006-03-07 15:04:13 -05003754 goto restore_hc;
3755 else
3756 goto clear_errat;
3757 }
3758
3759 ((MAILBOX_t *)&mbox)->mbxOwner = OWN_HOST;
James Smart9940b972011-03-11 16:06:12 -05003760 resp_data = 0;
3761 for (i = 0; i < 500; i++) {
3762 if (lpfc_readl(resp_buf, &resp_data))
3763 return;
3764 if (resp_data != mbox)
3765 mdelay(1);
3766 else
3767 break;
3768 }
James Smart92908312006-03-07 15:04:13 -05003769
3770clear_errat:
3771
James Smart9940b972011-03-11 16:06:12 -05003772 while (++i < 500) {
3773 if (lpfc_readl(phba->HAregaddr, &ha_copy))
3774 return;
3775 if (!(ha_copy & HA_ERATT))
3776 mdelay(1);
3777 else
3778 break;
3779 }
James Smart92908312006-03-07 15:04:13 -05003780
3781 if (readl(phba->HAregaddr) & HA_ERATT) {
3782 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05003783 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05003784 }
3785
3786restore_hc:
James Smart2e0fef82007-06-17 19:56:36 -05003787 phba->link_flag &= ~LS_IGNORE_ERATT;
James Smart92908312006-03-07 15:04:13 -05003788 writel(hc_copy, phba->HCregaddr);
3789 readl(phba->HCregaddr); /* flush */
3790}
3791
James Smarte59058c2008-08-24 21:49:00 -04003792/**
James Smart3621a712009-04-06 18:47:14 -04003793 * lpfc_sli_brdkill - Issue a kill_board mailbox command
James Smarte59058c2008-08-24 21:49:00 -04003794 * @phba: Pointer to HBA context object.
3795 *
3796 * This function issues a kill_board mailbox command and waits for
3797 * the error attention interrupt. This function is called for stopping
3798 * the firmware processing. The caller is not required to hold any
3799 * locks. This function calls lpfc_hba_down_post function to free
3800 * any pending commands after the kill. The function will return 1 when it
3801 * fails to kill the board else will return 0.
3802 **/
Jamie Wellnitz41415862006-02-28 19:25:27 -05003803int
James Smart2e0fef82007-06-17 19:56:36 -05003804lpfc_sli_brdkill(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05003805{
Jamie Wellnitz41415862006-02-28 19:25:27 -05003806 struct lpfc_sli *psli;
3807 LPFC_MBOXQ_t *pmb;
3808 uint32_t status;
3809 uint32_t ha_copy;
3810 int retval;
3811 int i = 0;
3812
3813 psli = &phba->sli;
3814
3815 /* Kill HBA */
James Smarted957682007-06-17 19:56:37 -05003816 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003817 "0329 Kill HBA Data: x%x x%x\n",
3818 phba->pport->port_state, psli->sli_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003819
James Smart98c9ea52007-10-27 13:37:33 -04003820 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3821 if (!pmb)
Jamie Wellnitz41415862006-02-28 19:25:27 -05003822 return 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003823
3824 /* Disable the error attention */
James Smart2e0fef82007-06-17 19:56:36 -05003825 spin_lock_irq(&phba->hbalock);
James Smart9940b972011-03-11 16:06:12 -05003826 if (lpfc_readl(phba->HCregaddr, &status)) {
3827 spin_unlock_irq(&phba->hbalock);
3828 mempool_free(pmb, phba->mbox_mem_pool);
3829 return 1;
3830 }
Jamie Wellnitz41415862006-02-28 19:25:27 -05003831 status &= ~HC_ERINT_ENA;
3832 writel(status, phba->HCregaddr);
3833 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05003834 phba->link_flag |= LS_IGNORE_ERATT;
3835 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003836
3837 lpfc_kill_board(phba, pmb);
3838 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
3839 retval = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
3840
3841 if (retval != MBX_SUCCESS) {
3842 if (retval != MBX_BUSY)
3843 mempool_free(pmb, phba->mbox_mem_pool);
James Smarte40a02c2010-02-26 14:13:54 -05003844 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3845 "2752 KILL_BOARD command failed retval %d\n",
3846 retval);
James Smart2e0fef82007-06-17 19:56:36 -05003847 spin_lock_irq(&phba->hbalock);
3848 phba->link_flag &= ~LS_IGNORE_ERATT;
3849 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003850 return 1;
3851 }
3852
James Smartf4b4c682009-05-22 14:53:12 -04003853 spin_lock_irq(&phba->hbalock);
3854 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
3855 spin_unlock_irq(&phba->hbalock);
James Smart92908312006-03-07 15:04:13 -05003856
Jamie Wellnitz41415862006-02-28 19:25:27 -05003857 mempool_free(pmb, phba->mbox_mem_pool);
3858
3859 /* There is no completion for a KILL_BOARD mbox cmd. Check for an error
3860 * attention every 100ms for 3 seconds. If we don't get ERATT after
3861 * 3 seconds we still set HBA_ERROR state because the status of the
3862 * board is now undefined.
3863 */
James Smart9940b972011-03-11 16:06:12 -05003864 if (lpfc_readl(phba->HAregaddr, &ha_copy))
3865 return 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003866 while ((i++ < 30) && !(ha_copy & HA_ERATT)) {
3867 mdelay(100);
James Smart9940b972011-03-11 16:06:12 -05003868 if (lpfc_readl(phba->HAregaddr, &ha_copy))
3869 return 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003870 }
3871
3872 del_timer_sync(&psli->mbox_tmo);
James Smart92908312006-03-07 15:04:13 -05003873 if (ha_copy & HA_ERATT) {
3874 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05003875 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05003876 }
James Smart2e0fef82007-06-17 19:56:36 -05003877 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003878 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart04c68492009-05-22 14:52:52 -04003879 psli->mbox_active = NULL;
James Smart2e0fef82007-06-17 19:56:36 -05003880 phba->link_flag &= ~LS_IGNORE_ERATT;
3881 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003882
Jamie Wellnitz41415862006-02-28 19:25:27 -05003883 lpfc_hba_down_post(phba);
James Smart2e0fef82007-06-17 19:56:36 -05003884 phba->link_state = LPFC_HBA_ERROR;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003885
James Smart2e0fef82007-06-17 19:56:36 -05003886 return ha_copy & HA_ERATT ? 0 : 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003887}
3888
James Smarte59058c2008-08-24 21:49:00 -04003889/**
James Smart3772a992009-05-22 14:50:54 -04003890 * lpfc_sli_brdreset - Reset a sli-2 or sli-3 HBA
James Smarte59058c2008-08-24 21:49:00 -04003891 * @phba: Pointer to HBA context object.
3892 *
3893 * This function resets the HBA by writing HC_INITFF to the control
3894 * register. After the HBA resets, this function resets all the iocb ring
3895 * indices. This function disables PCI layer parity checking during
3896 * the reset.
3897 * This function returns 0 always.
3898 * The caller is not required to hold any locks.
3899 **/
Jamie Wellnitz41415862006-02-28 19:25:27 -05003900int
James Smart2e0fef82007-06-17 19:56:36 -05003901lpfc_sli_brdreset(struct lpfc_hba *phba)
Jamie Wellnitz41415862006-02-28 19:25:27 -05003902{
3903 struct lpfc_sli *psli;
dea31012005-04-17 16:05:31 -05003904 struct lpfc_sli_ring *pring;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003905 uint16_t cfg_value;
dea31012005-04-17 16:05:31 -05003906 int i;
dea31012005-04-17 16:05:31 -05003907
Jamie Wellnitz41415862006-02-28 19:25:27 -05003908 psli = &phba->sli;
dea31012005-04-17 16:05:31 -05003909
Jamie Wellnitz41415862006-02-28 19:25:27 -05003910 /* Reset HBA */
3911 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003912 "0325 Reset HBA Data: x%x x%x\n",
James Smart2e0fef82007-06-17 19:56:36 -05003913 phba->pport->port_state, psli->sli_flag);
dea31012005-04-17 16:05:31 -05003914
3915 /* perform board reset */
3916 phba->fc_eventTag = 0;
James Smart4d9ab992009-10-02 15:16:39 -04003917 phba->link_events = 0;
James Smart2e0fef82007-06-17 19:56:36 -05003918 phba->pport->fc_myDID = 0;
3919 phba->pport->fc_prevDID = 0;
dea31012005-04-17 16:05:31 -05003920
Jamie Wellnitz41415862006-02-28 19:25:27 -05003921 /* Turn off parity checking and serr during the physical reset */
3922 pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
3923 pci_write_config_word(phba->pcidev, PCI_COMMAND,
3924 (cfg_value &
3925 ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
3926
James Smart3772a992009-05-22 14:50:54 -04003927 psli->sli_flag &= ~(LPFC_SLI_ACTIVE | LPFC_PROCESS_LA);
3928
Jamie Wellnitz41415862006-02-28 19:25:27 -05003929 /* Now toggle INITFF bit in the Host Control Register */
3930 writel(HC_INITFF, phba->HCregaddr);
3931 mdelay(1);
3932 readl(phba->HCregaddr); /* flush */
3933 writel(0, phba->HCregaddr);
3934 readl(phba->HCregaddr); /* flush */
3935
3936 /* Restore PCI cmd register */
3937 pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
dea31012005-04-17 16:05:31 -05003938
3939 /* Initialize relevant SLI info */
Jamie Wellnitz41415862006-02-28 19:25:27 -05003940 for (i = 0; i < psli->num_rings; i++) {
3941 pring = &psli->ring[i];
dea31012005-04-17 16:05:31 -05003942 pring->flag = 0;
James Smart7e56aa22012-08-03 12:35:34 -04003943 pring->sli.sli3.rspidx = 0;
3944 pring->sli.sli3.next_cmdidx = 0;
3945 pring->sli.sli3.local_getidx = 0;
3946 pring->sli.sli3.cmdidx = 0;
dea31012005-04-17 16:05:31 -05003947 pring->missbufcnt = 0;
3948 }
dea31012005-04-17 16:05:31 -05003949
James Smart2e0fef82007-06-17 19:56:36 -05003950 phba->link_state = LPFC_WARM_START;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003951 return 0;
3952}
3953
James Smarte59058c2008-08-24 21:49:00 -04003954/**
James Smartda0436e2009-05-22 14:51:39 -04003955 * lpfc_sli4_brdreset - Reset a sli-4 HBA
3956 * @phba: Pointer to HBA context object.
3957 *
3958 * This function resets a SLI4 HBA. This function disables PCI layer parity
3959 * checking during resets the device. The caller is not required to hold
3960 * any locks.
3961 *
3962 * This function returns 0 always.
3963 **/
3964int
3965lpfc_sli4_brdreset(struct lpfc_hba *phba)
3966{
3967 struct lpfc_sli *psli = &phba->sli;
3968 uint16_t cfg_value;
James Smart27b01b82012-05-09 21:19:44 -04003969 int rc;
James Smartda0436e2009-05-22 14:51:39 -04003970
3971 /* Reset HBA */
3972 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3973 "0295 Reset HBA Data: x%x x%x\n",
3974 phba->pport->port_state, psli->sli_flag);
3975
3976 /* perform board reset */
3977 phba->fc_eventTag = 0;
James Smart4d9ab992009-10-02 15:16:39 -04003978 phba->link_events = 0;
James Smartda0436e2009-05-22 14:51:39 -04003979 phba->pport->fc_myDID = 0;
3980 phba->pport->fc_prevDID = 0;
3981
James Smartda0436e2009-05-22 14:51:39 -04003982 spin_lock_irq(&phba->hbalock);
3983 psli->sli_flag &= ~(LPFC_PROCESS_LA);
3984 phba->fcf.fcf_flag = 0;
James Smartda0436e2009-05-22 14:51:39 -04003985 spin_unlock_irq(&phba->hbalock);
3986
3987 /* Now physically reset the device */
3988 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3989 "0389 Performing PCI function reset!\n");
James Smartbe858b62010-12-15 17:57:20 -05003990
3991 /* Turn off parity checking and serr during the physical reset */
3992 pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
3993 pci_write_config_word(phba->pcidev, PCI_COMMAND, (cfg_value &
3994 ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
3995
James Smart88318812012-09-29 11:29:29 -04003996 /* Perform FCoE PCI function reset before freeing queue memory */
James Smart27b01b82012-05-09 21:19:44 -04003997 rc = lpfc_pci_function_reset(phba);
James Smart88318812012-09-29 11:29:29 -04003998 lpfc_sli4_queue_destroy(phba);
James Smartda0436e2009-05-22 14:51:39 -04003999
James Smartbe858b62010-12-15 17:57:20 -05004000 /* Restore PCI cmd register */
4001 pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
4002
James Smart27b01b82012-05-09 21:19:44 -04004003 return rc;
James Smartda0436e2009-05-22 14:51:39 -04004004}
4005
4006/**
4007 * lpfc_sli_brdrestart_s3 - Restart a sli-3 hba
James Smarte59058c2008-08-24 21:49:00 -04004008 * @phba: Pointer to HBA context object.
4009 *
4010 * This function is called in the SLI initialization code path to
4011 * restart the HBA. The caller is not required to hold any lock.
4012 * This function writes MBX_RESTART mailbox command to the SLIM and
4013 * resets the HBA. At the end of the function, it calls lpfc_hba_down_post
4014 * function to free any pending commands. The function enables
4015 * POST only during the first initialization. The function returns zero.
4016 * The function does not guarantee completion of MBX_RESTART mailbox
4017 * command before the return of this function.
4018 **/
James Smartda0436e2009-05-22 14:51:39 -04004019static int
4020lpfc_sli_brdrestart_s3(struct lpfc_hba *phba)
Jamie Wellnitz41415862006-02-28 19:25:27 -05004021{
4022 MAILBOX_t *mb;
4023 struct lpfc_sli *psli;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004024 volatile uint32_t word0;
4025 void __iomem *to_slim;
James Smart0d878412009-10-02 15:16:56 -04004026 uint32_t hba_aer_enabled;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004027
James Smart2e0fef82007-06-17 19:56:36 -05004028 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004029
James Smart0d878412009-10-02 15:16:56 -04004030 /* Take PCIe device Advanced Error Reporting (AER) state */
4031 hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
4032
Jamie Wellnitz41415862006-02-28 19:25:27 -05004033 psli = &phba->sli;
4034
4035 /* Restart HBA */
4036 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04004037 "0337 Restart HBA Data: x%x x%x\n",
James Smart2e0fef82007-06-17 19:56:36 -05004038 phba->pport->port_state, psli->sli_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004039
4040 word0 = 0;
4041 mb = (MAILBOX_t *) &word0;
4042 mb->mbxCommand = MBX_RESTART;
4043 mb->mbxHc = 1;
4044
James Smart92908312006-03-07 15:04:13 -05004045 lpfc_reset_barrier(phba);
4046
Jamie Wellnitz41415862006-02-28 19:25:27 -05004047 to_slim = phba->MBslimaddr;
4048 writel(*(uint32_t *) mb, to_slim);
4049 readl(to_slim); /* flush */
4050
4051 /* Only skip post after fc_ffinit is completed */
James Smarteaf15d52008-12-04 22:39:29 -05004052 if (phba->pport->port_state)
Jamie Wellnitz41415862006-02-28 19:25:27 -05004053 word0 = 1; /* This is really setting up word1 */
James Smarteaf15d52008-12-04 22:39:29 -05004054 else
Jamie Wellnitz41415862006-02-28 19:25:27 -05004055 word0 = 0; /* This is really setting up word1 */
James Smart65a29c12006-07-06 15:50:50 -04004056 to_slim = phba->MBslimaddr + sizeof (uint32_t);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004057 writel(*(uint32_t *) mb, to_slim);
4058 readl(to_slim); /* flush */
4059
4060 lpfc_sli_brdreset(phba);
James Smart2e0fef82007-06-17 19:56:36 -05004061 phba->pport->stopped = 0;
4062 phba->link_state = LPFC_INIT_START;
James Smartda0436e2009-05-22 14:51:39 -04004063 phba->hba_flag = 0;
James Smart2e0fef82007-06-17 19:56:36 -05004064 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004065
James Smart64ba8812006-08-02 15:24:34 -04004066 memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
4067 psli->stats_start = get_seconds();
4068
James Smarteaf15d52008-12-04 22:39:29 -05004069 /* Give the INITFF and Post time to settle. */
4070 mdelay(100);
dea31012005-04-17 16:05:31 -05004071
James Smart0d878412009-10-02 15:16:56 -04004072 /* Reset HBA AER if it was enabled, note hba_flag was reset above */
4073 if (hba_aer_enabled)
4074 pci_disable_pcie_error_reporting(phba->pcidev);
4075
Jamie Wellnitz41415862006-02-28 19:25:27 -05004076 lpfc_hba_down_post(phba);
dea31012005-04-17 16:05:31 -05004077
4078 return 0;
4079}
4080
James Smarte59058c2008-08-24 21:49:00 -04004081/**
James Smartda0436e2009-05-22 14:51:39 -04004082 * lpfc_sli_brdrestart_s4 - Restart the sli-4 hba
4083 * @phba: Pointer to HBA context object.
4084 *
4085 * This function is called in the SLI initialization code path to restart
4086 * a SLI4 HBA. The caller is not required to hold any lock.
4087 * At the end of the function, it calls lpfc_hba_down_post function to
4088 * free any pending commands.
4089 **/
4090static int
4091lpfc_sli_brdrestart_s4(struct lpfc_hba *phba)
4092{
4093 struct lpfc_sli *psli = &phba->sli;
James Smart75baf692010-06-08 18:31:21 -04004094 uint32_t hba_aer_enabled;
James Smart27b01b82012-05-09 21:19:44 -04004095 int rc;
James Smartda0436e2009-05-22 14:51:39 -04004096
4097 /* Restart HBA */
4098 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4099 "0296 Restart HBA Data: x%x x%x\n",
4100 phba->pport->port_state, psli->sli_flag);
4101
James Smart75baf692010-06-08 18:31:21 -04004102 /* Take PCIe device Advanced Error Reporting (AER) state */
4103 hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
4104
James Smart27b01b82012-05-09 21:19:44 -04004105 rc = lpfc_sli4_brdreset(phba);
James Smartda0436e2009-05-22 14:51:39 -04004106
4107 spin_lock_irq(&phba->hbalock);
4108 phba->pport->stopped = 0;
4109 phba->link_state = LPFC_INIT_START;
4110 phba->hba_flag = 0;
4111 spin_unlock_irq(&phba->hbalock);
4112
4113 memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
4114 psli->stats_start = get_seconds();
4115
James Smart75baf692010-06-08 18:31:21 -04004116 /* Reset HBA AER if it was enabled, note hba_flag was reset above */
4117 if (hba_aer_enabled)
4118 pci_disable_pcie_error_reporting(phba->pcidev);
4119
James Smartda0436e2009-05-22 14:51:39 -04004120 lpfc_hba_down_post(phba);
4121
James Smart27b01b82012-05-09 21:19:44 -04004122 return rc;
James Smartda0436e2009-05-22 14:51:39 -04004123}
4124
4125/**
4126 * lpfc_sli_brdrestart - Wrapper func for restarting hba
4127 * @phba: Pointer to HBA context object.
4128 *
4129 * This routine wraps the actual SLI3 or SLI4 hba restart routine from the
4130 * API jump table function pointer from the lpfc_hba struct.
4131**/
4132int
4133lpfc_sli_brdrestart(struct lpfc_hba *phba)
4134{
4135 return phba->lpfc_sli_brdrestart(phba);
4136}
4137
4138/**
James Smart3621a712009-04-06 18:47:14 -04004139 * lpfc_sli_chipset_init - Wait for the restart of the HBA after a restart
James Smarte59058c2008-08-24 21:49:00 -04004140 * @phba: Pointer to HBA context object.
4141 *
4142 * This function is called after a HBA restart to wait for successful
4143 * restart of the HBA. Successful restart of the HBA is indicated by
4144 * HS_FFRDY and HS_MBRDY bits. If the HBA fails to restart even after 15
4145 * iteration, the function will restart the HBA again. The function returns
4146 * zero if HBA successfully restarted else returns negative error code.
4147 **/
dea31012005-04-17 16:05:31 -05004148static int
4149lpfc_sli_chipset_init(struct lpfc_hba *phba)
4150{
4151 uint32_t status, i = 0;
4152
4153 /* Read the HBA Host Status Register */
James Smart9940b972011-03-11 16:06:12 -05004154 if (lpfc_readl(phba->HSregaddr, &status))
4155 return -EIO;
dea31012005-04-17 16:05:31 -05004156
4157 /* Check status register to see what current state is */
4158 i = 0;
4159 while ((status & (HS_FFRDY | HS_MBRDY)) != (HS_FFRDY | HS_MBRDY)) {
4160
James Smartdcf2a4e2010-09-29 11:18:53 -04004161 /* Check every 10ms for 10 retries, then every 100ms for 90
4162 * retries, then every 1 sec for 50 retires for a total of
4163 * ~60 seconds before reset the board again and check every
4164 * 1 sec for 50 retries. The up to 60 seconds before the
4165 * board ready is required by the Falcon FIPS zeroization
4166 * complete, and any reset the board in between shall cause
4167 * restart of zeroization, further delay the board ready.
dea31012005-04-17 16:05:31 -05004168 */
James Smartdcf2a4e2010-09-29 11:18:53 -04004169 if (i++ >= 200) {
dea31012005-04-17 16:05:31 -05004170 /* Adapter failed to init, timeout, status reg
4171 <status> */
James Smarted957682007-06-17 19:56:37 -05004172 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004173 "0436 Adapter failed to init, "
James Smart09372822008-01-11 01:52:54 -05004174 "timeout, status reg x%x, "
4175 "FW Data: A8 x%x AC x%x\n", status,
4176 readl(phba->MBslimaddr + 0xa8),
4177 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05004178 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05004179 return -ETIMEDOUT;
4180 }
4181
4182 /* Check to see if any errors occurred during init */
4183 if (status & HS_FFERM) {
4184 /* ERROR: During chipset initialization */
4185 /* Adapter failed to init, chipset, status reg
4186 <status> */
James Smarted957682007-06-17 19:56:37 -05004187 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004188 "0437 Adapter failed to init, "
James Smart09372822008-01-11 01:52:54 -05004189 "chipset, status reg x%x, "
4190 "FW Data: A8 x%x AC x%x\n", status,
4191 readl(phba->MBslimaddr + 0xa8),
4192 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05004193 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05004194 return -EIO;
4195 }
4196
James Smartdcf2a4e2010-09-29 11:18:53 -04004197 if (i <= 10)
dea31012005-04-17 16:05:31 -05004198 msleep(10);
James Smartdcf2a4e2010-09-29 11:18:53 -04004199 else if (i <= 100)
4200 msleep(100);
4201 else
4202 msleep(1000);
dea31012005-04-17 16:05:31 -05004203
James Smartdcf2a4e2010-09-29 11:18:53 -04004204 if (i == 150) {
4205 /* Do post */
James Smart92d7f7b2007-06-17 19:56:38 -05004206 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004207 lpfc_sli_brdrestart(phba);
dea31012005-04-17 16:05:31 -05004208 }
4209 /* Read the HBA Host Status Register */
James Smart9940b972011-03-11 16:06:12 -05004210 if (lpfc_readl(phba->HSregaddr, &status))
4211 return -EIO;
dea31012005-04-17 16:05:31 -05004212 }
4213
4214 /* Check to see if any errors occurred during init */
4215 if (status & HS_FFERM) {
4216 /* ERROR: During chipset initialization */
4217 /* Adapter failed to init, chipset, status reg <status> */
James Smarted957682007-06-17 19:56:37 -05004218 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004219 "0438 Adapter failed to init, chipset, "
James Smart09372822008-01-11 01:52:54 -05004220 "status reg x%x, "
4221 "FW Data: A8 x%x AC x%x\n", status,
4222 readl(phba->MBslimaddr + 0xa8),
4223 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05004224 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05004225 return -EIO;
4226 }
4227
4228 /* Clear all interrupt enable conditions */
4229 writel(0, phba->HCregaddr);
4230 readl(phba->HCregaddr); /* flush */
4231
4232 /* setup host attn register */
4233 writel(0xffffffff, phba->HAregaddr);
4234 readl(phba->HAregaddr); /* flush */
4235 return 0;
4236}
4237
James Smarte59058c2008-08-24 21:49:00 -04004238/**
James Smart3621a712009-04-06 18:47:14 -04004239 * lpfc_sli_hbq_count - Get the number of HBQs to be configured
James Smarte59058c2008-08-24 21:49:00 -04004240 *
4241 * This function calculates and returns the number of HBQs required to be
4242 * configured.
4243 **/
James Smart78b2d852007-08-02 11:10:21 -04004244int
James Smarted957682007-06-17 19:56:37 -05004245lpfc_sli_hbq_count(void)
4246{
James Smart92d7f7b2007-06-17 19:56:38 -05004247 return ARRAY_SIZE(lpfc_hbq_defs);
James Smarted957682007-06-17 19:56:37 -05004248}
4249
James Smarte59058c2008-08-24 21:49:00 -04004250/**
James Smart3621a712009-04-06 18:47:14 -04004251 * lpfc_sli_hbq_entry_count - Calculate total number of hbq entries
James Smarte59058c2008-08-24 21:49:00 -04004252 *
4253 * This function adds the number of hbq entries in every HBQ to get
4254 * the total number of hbq entries required for the HBA and returns
4255 * the total count.
4256 **/
James Smarted957682007-06-17 19:56:37 -05004257static int
4258lpfc_sli_hbq_entry_count(void)
4259{
4260 int hbq_count = lpfc_sli_hbq_count();
4261 int count = 0;
4262 int i;
4263
4264 for (i = 0; i < hbq_count; ++i)
James Smart92d7f7b2007-06-17 19:56:38 -05004265 count += lpfc_hbq_defs[i]->entry_count;
James Smarted957682007-06-17 19:56:37 -05004266 return count;
4267}
4268
James Smarte59058c2008-08-24 21:49:00 -04004269/**
James Smart3621a712009-04-06 18:47:14 -04004270 * lpfc_sli_hbq_size - Calculate memory required for all hbq entries
James Smarte59058c2008-08-24 21:49:00 -04004271 *
4272 * This function calculates amount of memory required for all hbq entries
4273 * to be configured and returns the total memory required.
4274 **/
dea31012005-04-17 16:05:31 -05004275int
James Smarted957682007-06-17 19:56:37 -05004276lpfc_sli_hbq_size(void)
4277{
4278 return lpfc_sli_hbq_entry_count() * sizeof(struct lpfc_hbq_entry);
4279}
4280
James Smarte59058c2008-08-24 21:49:00 -04004281/**
James Smart3621a712009-04-06 18:47:14 -04004282 * lpfc_sli_hbq_setup - configure and initialize HBQs
James Smarte59058c2008-08-24 21:49:00 -04004283 * @phba: Pointer to HBA context object.
4284 *
4285 * This function is called during the SLI initialization to configure
4286 * all the HBQs and post buffers to the HBQ. The caller is not
4287 * required to hold any locks. This function will return zero if successful
4288 * else it will return negative error code.
4289 **/
James Smarted957682007-06-17 19:56:37 -05004290static int
4291lpfc_sli_hbq_setup(struct lpfc_hba *phba)
4292{
4293 int hbq_count = lpfc_sli_hbq_count();
4294 LPFC_MBOXQ_t *pmb;
4295 MAILBOX_t *pmbox;
4296 uint32_t hbqno;
4297 uint32_t hbq_entry_index;
James Smarted957682007-06-17 19:56:37 -05004298
James Smart92d7f7b2007-06-17 19:56:38 -05004299 /* Get a Mailbox buffer to setup mailbox
4300 * commands for HBA initialization
4301 */
James Smarted957682007-06-17 19:56:37 -05004302 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4303
4304 if (!pmb)
4305 return -ENOMEM;
4306
James Smart04c68492009-05-22 14:52:52 -04004307 pmbox = &pmb->u.mb;
James Smarted957682007-06-17 19:56:37 -05004308
4309 /* Initialize the struct lpfc_sli_hbq structure for each hbq */
4310 phba->link_state = LPFC_INIT_MBX_CMDS;
James Smart3163f722008-02-08 18:50:25 -05004311 phba->hbq_in_use = 1;
James Smarted957682007-06-17 19:56:37 -05004312
4313 hbq_entry_index = 0;
4314 for (hbqno = 0; hbqno < hbq_count; ++hbqno) {
4315 phba->hbqs[hbqno].next_hbqPutIdx = 0;
4316 phba->hbqs[hbqno].hbqPutIdx = 0;
4317 phba->hbqs[hbqno].local_hbqGetIdx = 0;
4318 phba->hbqs[hbqno].entry_count =
James Smart92d7f7b2007-06-17 19:56:38 -05004319 lpfc_hbq_defs[hbqno]->entry_count;
James Smart51ef4c22007-08-02 11:10:31 -04004320 lpfc_config_hbq(phba, hbqno, lpfc_hbq_defs[hbqno],
4321 hbq_entry_index, pmb);
James Smarted957682007-06-17 19:56:37 -05004322 hbq_entry_index += phba->hbqs[hbqno].entry_count;
4323
4324 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
4325 /* Adapter failed to init, mbxCmd <cmd> CFG_RING,
4326 mbxStatus <status>, ring <num> */
4327
4328 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -05004329 LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04004330 "1805 Adapter failed to init. "
James Smarted957682007-06-17 19:56:37 -05004331 "Data: x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04004332 pmbox->mbxCommand,
James Smarted957682007-06-17 19:56:37 -05004333 pmbox->mbxStatus, hbqno);
4334
4335 phba->link_state = LPFC_HBA_ERROR;
4336 mempool_free(pmb, phba->mbox_mem_pool);
James Smart6e7288d2010-06-07 15:23:35 -04004337 return -ENXIO;
James Smarted957682007-06-17 19:56:37 -05004338 }
4339 }
4340 phba->hbq_count = hbq_count;
4341
James Smarted957682007-06-17 19:56:37 -05004342 mempool_free(pmb, phba->mbox_mem_pool);
4343
James Smart92d7f7b2007-06-17 19:56:38 -05004344 /* Initially populate or replenish the HBQs */
James Smartd7c255b2008-08-24 21:50:00 -04004345 for (hbqno = 0; hbqno < hbq_count; ++hbqno)
4346 lpfc_sli_hbqbuf_init_hbqs(phba, hbqno);
James Smarted957682007-06-17 19:56:37 -05004347 return 0;
4348}
4349
James Smarte59058c2008-08-24 21:49:00 -04004350/**
James Smart4f774512009-05-22 14:52:35 -04004351 * lpfc_sli4_rb_setup - Initialize and post RBs to HBA
4352 * @phba: Pointer to HBA context object.
4353 *
4354 * This function is called during the SLI initialization to configure
4355 * all the HBQs and post buffers to the HBQ. The caller is not
4356 * required to hold any locks. This function will return zero if successful
4357 * else it will return negative error code.
4358 **/
4359static int
4360lpfc_sli4_rb_setup(struct lpfc_hba *phba)
4361{
4362 phba->hbq_in_use = 1;
4363 phba->hbqs[0].entry_count = lpfc_hbq_defs[0]->entry_count;
4364 phba->hbq_count = 1;
4365 /* Initially populate or replenish the HBQs */
4366 lpfc_sli_hbqbuf_init_hbqs(phba, 0);
4367 return 0;
4368}
4369
4370/**
James Smart3621a712009-04-06 18:47:14 -04004371 * lpfc_sli_config_port - Issue config port mailbox command
James Smarte59058c2008-08-24 21:49:00 -04004372 * @phba: Pointer to HBA context object.
4373 * @sli_mode: sli mode - 2/3
4374 *
4375 * This function is called by the sli intialization code path
4376 * to issue config_port mailbox command. This function restarts the
4377 * HBA firmware and issues a config_port mailbox command to configure
4378 * the SLI interface in the sli mode specified by sli_mode
4379 * variable. The caller is not required to hold any locks.
4380 * The function returns 0 if successful, else returns negative error
4381 * code.
4382 **/
James Smart93996272008-08-24 21:50:30 -04004383int
4384lpfc_sli_config_port(struct lpfc_hba *phba, int sli_mode)
dea31012005-04-17 16:05:31 -05004385{
4386 LPFC_MBOXQ_t *pmb;
4387 uint32_t resetcount = 0, rc = 0, done = 0;
4388
4389 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4390 if (!pmb) {
James Smart2e0fef82007-06-17 19:56:36 -05004391 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05004392 return -ENOMEM;
4393 }
4394
James Smarted957682007-06-17 19:56:37 -05004395 phba->sli_rev = sli_mode;
dea31012005-04-17 16:05:31 -05004396 while (resetcount < 2 && !done) {
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);
James Smart92d7f7b2007-06-17 19:56:38 -05004400 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004401 lpfc_sli_brdrestart(phba);
dea31012005-04-17 16:05:31 -05004402 rc = lpfc_sli_chipset_init(phba);
4403 if (rc)
4404 break;
4405
James Smart2e0fef82007-06-17 19:56:36 -05004406 spin_lock_irq(&phba->hbalock);
James Smart1c067a42006-08-01 07:33:52 -04004407 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05004408 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004409 resetcount++;
4410
James Smarted957682007-06-17 19:56:37 -05004411 /* Call pre CONFIG_PORT mailbox command initialization. A
4412 * value of 0 means the call was successful. Any other
4413 * nonzero value is a failure, but if ERESTART is returned,
4414 * the driver may reset the HBA and try again.
4415 */
dea31012005-04-17 16:05:31 -05004416 rc = lpfc_config_port_prep(phba);
4417 if (rc == -ERESTART) {
James Smarted957682007-06-17 19:56:37 -05004418 phba->link_state = LPFC_LINK_UNKNOWN;
dea31012005-04-17 16:05:31 -05004419 continue;
James Smart34b02dc2008-08-24 21:49:55 -04004420 } else if (rc)
dea31012005-04-17 16:05:31 -05004421 break;
James Smart6d368e52011-05-24 11:44:12 -04004422
James Smart2e0fef82007-06-17 19:56:36 -05004423 phba->link_state = LPFC_INIT_MBX_CMDS;
dea31012005-04-17 16:05:31 -05004424 lpfc_config_port(phba, pmb);
4425 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
James Smart34b02dc2008-08-24 21:49:55 -04004426 phba->sli3_options &= ~(LPFC_SLI3_NPIV_ENABLED |
4427 LPFC_SLI3_HBQ_ENABLED |
4428 LPFC_SLI3_CRP_ENABLED |
James Smartbc739052010-08-04 16:11:18 -04004429 LPFC_SLI3_BG_ENABLED |
4430 LPFC_SLI3_DSS_ENABLED);
James Smarted957682007-06-17 19:56:37 -05004431 if (rc != MBX_SUCCESS) {
dea31012005-04-17 16:05:31 -05004432 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004433 "0442 Adapter failed to init, mbxCmd x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05004434 "CONFIG_PORT, mbxStatus x%x Data: x%x\n",
James Smart04c68492009-05-22 14:52:52 -04004435 pmb->u.mb.mbxCommand, pmb->u.mb.mbxStatus, 0);
James Smart2e0fef82007-06-17 19:56:36 -05004436 spin_lock_irq(&phba->hbalock);
James Smart04c68492009-05-22 14:52:52 -04004437 phba->sli.sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05004438 spin_unlock_irq(&phba->hbalock);
4439 rc = -ENXIO;
James Smart04c68492009-05-22 14:52:52 -04004440 } else {
4441 /* Allow asynchronous mailbox command to go through */
4442 spin_lock_irq(&phba->hbalock);
4443 phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
4444 spin_unlock_irq(&phba->hbalock);
James Smarted957682007-06-17 19:56:37 -05004445 done = 1;
James Smartcb69f7d2011-12-13 13:21:57 -05004446
4447 if ((pmb->u.mb.un.varCfgPort.casabt == 1) &&
4448 (pmb->u.mb.un.varCfgPort.gasabt == 0))
4449 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
4450 "3110 Port did not grant ASABT\n");
James Smart04c68492009-05-22 14:52:52 -04004451 }
dea31012005-04-17 16:05:31 -05004452 }
James Smarted957682007-06-17 19:56:37 -05004453 if (!done) {
4454 rc = -EINVAL;
4455 goto do_prep_failed;
4456 }
James Smart04c68492009-05-22 14:52:52 -04004457 if (pmb->u.mb.un.varCfgPort.sli_mode == 3) {
4458 if (!pmb->u.mb.un.varCfgPort.cMA) {
James Smart34b02dc2008-08-24 21:49:55 -04004459 rc = -ENXIO;
4460 goto do_prep_failed;
4461 }
James Smart04c68492009-05-22 14:52:52 -04004462 if (phba->max_vpi && pmb->u.mb.un.varCfgPort.gmv) {
James Smart34b02dc2008-08-24 21:49:55 -04004463 phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
James Smart04c68492009-05-22 14:52:52 -04004464 phba->max_vpi = pmb->u.mb.un.varCfgPort.max_vpi;
4465 phba->max_vports = (phba->max_vpi > phba->max_vports) ?
4466 phba->max_vpi : phba->max_vports;
4467
James Smart34b02dc2008-08-24 21:49:55 -04004468 } else
4469 phba->max_vpi = 0;
James Smartbc739052010-08-04 16:11:18 -04004470 phba->fips_level = 0;
4471 phba->fips_spec_rev = 0;
4472 if (pmb->u.mb.un.varCfgPort.gdss) {
James Smart04c68492009-05-22 14:52:52 -04004473 phba->sli3_options |= LPFC_SLI3_DSS_ENABLED;
James Smartbc739052010-08-04 16:11:18 -04004474 phba->fips_level = pmb->u.mb.un.varCfgPort.fips_level;
4475 phba->fips_spec_rev = pmb->u.mb.un.varCfgPort.fips_rev;
4476 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4477 "2850 Security Crypto Active. FIPS x%d "
4478 "(Spec Rev: x%d)",
4479 phba->fips_level, phba->fips_spec_rev);
4480 }
4481 if (pmb->u.mb.un.varCfgPort.sec_err) {
4482 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4483 "2856 Config Port Security Crypto "
4484 "Error: x%x ",
4485 pmb->u.mb.un.varCfgPort.sec_err);
4486 }
James Smart04c68492009-05-22 14:52:52 -04004487 if (pmb->u.mb.un.varCfgPort.gerbm)
James Smart34b02dc2008-08-24 21:49:55 -04004488 phba->sli3_options |= LPFC_SLI3_HBQ_ENABLED;
James Smart04c68492009-05-22 14:52:52 -04004489 if (pmb->u.mb.un.varCfgPort.gcrp)
James Smart34b02dc2008-08-24 21:49:55 -04004490 phba->sli3_options |= LPFC_SLI3_CRP_ENABLED;
James Smart6e7288d2010-06-07 15:23:35 -04004491
4492 phba->hbq_get = phba->mbox->us.s3_pgp.hbq_get;
4493 phba->port_gp = phba->mbox->us.s3_pgp.port;
James Smarte2a0a9d2008-12-04 22:40:02 -05004494
4495 if (phba->cfg_enable_bg) {
James Smart04c68492009-05-22 14:52:52 -04004496 if (pmb->u.mb.un.varCfgPort.gbg)
James Smarte2a0a9d2008-12-04 22:40:02 -05004497 phba->sli3_options |= LPFC_SLI3_BG_ENABLED;
4498 else
4499 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4500 "0443 Adapter did not grant "
4501 "BlockGuard\n");
4502 }
James Smart34b02dc2008-08-24 21:49:55 -04004503 } else {
James Smart8f34f4c2008-12-04 22:39:23 -05004504 phba->hbq_get = NULL;
James Smart34b02dc2008-08-24 21:49:55 -04004505 phba->port_gp = phba->mbox->us.s2.port;
James Smartd7c255b2008-08-24 21:50:00 -04004506 phba->max_vpi = 0;
James Smarted957682007-06-17 19:56:37 -05004507 }
James Smart92d7f7b2007-06-17 19:56:38 -05004508do_prep_failed:
James Smarted957682007-06-17 19:56:37 -05004509 mempool_free(pmb, phba->mbox_mem_pool);
4510 return rc;
4511}
4512
James Smarte59058c2008-08-24 21:49:00 -04004513
4514/**
James Smart3621a712009-04-06 18:47:14 -04004515 * lpfc_sli_hba_setup - SLI intialization function
James Smarte59058c2008-08-24 21:49:00 -04004516 * @phba: Pointer to HBA context object.
4517 *
4518 * This function is the main SLI intialization function. This function
4519 * is called by the HBA intialization code, HBA reset code and HBA
4520 * error attention handler code. Caller is not required to hold any
4521 * locks. This function issues config_port mailbox command to configure
4522 * the SLI, setup iocb rings and HBQ rings. In the end the function
4523 * calls the config_port_post function to issue init_link mailbox
4524 * command and to start the discovery. The function will return zero
4525 * if successful, else it will return negative error code.
4526 **/
James Smarted957682007-06-17 19:56:37 -05004527int
4528lpfc_sli_hba_setup(struct lpfc_hba *phba)
4529{
4530 uint32_t rc;
James Smart6d368e52011-05-24 11:44:12 -04004531 int mode = 3, i;
4532 int longs;
James Smarted957682007-06-17 19:56:37 -05004533
4534 switch (lpfc_sli_mode) {
4535 case 2:
James Smart78b2d852007-08-02 11:10:21 -04004536 if (phba->cfg_enable_npiv) {
James Smart92d7f7b2007-06-17 19:56:38 -05004537 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04004538 "1824 NPIV enabled: Override lpfc_sli_mode "
James Smart92d7f7b2007-06-17 19:56:38 -05004539 "parameter (%d) to auto (0).\n",
James Smarte8b62012007-08-02 11:10:09 -04004540 lpfc_sli_mode);
James Smart92d7f7b2007-06-17 19:56:38 -05004541 break;
4542 }
James Smarted957682007-06-17 19:56:37 -05004543 mode = 2;
4544 break;
4545 case 0:
4546 case 3:
4547 break;
4548 default:
James Smart92d7f7b2007-06-17 19:56:38 -05004549 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04004550 "1819 Unrecognized lpfc_sli_mode "
4551 "parameter: %d.\n", lpfc_sli_mode);
James Smarted957682007-06-17 19:56:37 -05004552
4553 break;
4554 }
4555
James Smart93996272008-08-24 21:50:30 -04004556 rc = lpfc_sli_config_port(phba, mode);
4557
James Smarted957682007-06-17 19:56:37 -05004558 if (rc && lpfc_sli_mode == 3)
James Smart92d7f7b2007-06-17 19:56:38 -05004559 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04004560 "1820 Unable to select SLI-3. "
4561 "Not supported by adapter.\n");
James Smarted957682007-06-17 19:56:37 -05004562 if (rc && mode != 2)
James Smart93996272008-08-24 21:50:30 -04004563 rc = lpfc_sli_config_port(phba, 2);
James Smarted957682007-06-17 19:56:37 -05004564 if (rc)
dea31012005-04-17 16:05:31 -05004565 goto lpfc_sli_hba_setup_error;
4566
James Smart0d878412009-10-02 15:16:56 -04004567 /* Enable PCIe device Advanced Error Reporting (AER) if configured */
4568 if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
4569 rc = pci_enable_pcie_error_reporting(phba->pcidev);
4570 if (!rc) {
4571 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4572 "2709 This device supports "
4573 "Advanced Error Reporting (AER)\n");
4574 spin_lock_irq(&phba->hbalock);
4575 phba->hba_flag |= HBA_AER_ENABLED;
4576 spin_unlock_irq(&phba->hbalock);
4577 } else {
4578 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4579 "2708 This device does not support "
4580 "Advanced Error Reporting (AER)\n");
4581 phba->cfg_aer_support = 0;
4582 }
4583 }
4584
James Smarted957682007-06-17 19:56:37 -05004585 if (phba->sli_rev == 3) {
4586 phba->iocb_cmd_size = SLI3_IOCB_CMD_SIZE;
4587 phba->iocb_rsp_size = SLI3_IOCB_RSP_SIZE;
James Smarted957682007-06-17 19:56:37 -05004588 } else {
4589 phba->iocb_cmd_size = SLI2_IOCB_CMD_SIZE;
4590 phba->iocb_rsp_size = SLI2_IOCB_RSP_SIZE;
James Smart92d7f7b2007-06-17 19:56:38 -05004591 phba->sli3_options = 0;
James Smarted957682007-06-17 19:56:37 -05004592 }
4593
4594 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004595 "0444 Firmware in SLI %x mode. Max_vpi %d\n",
4596 phba->sli_rev, phba->max_vpi);
James Smarted957682007-06-17 19:56:37 -05004597 rc = lpfc_sli_ring_map(phba);
dea31012005-04-17 16:05:31 -05004598
4599 if (rc)
4600 goto lpfc_sli_hba_setup_error;
4601
James Smart6d368e52011-05-24 11:44:12 -04004602 /* Initialize VPIs. */
4603 if (phba->sli_rev == LPFC_SLI_REV3) {
4604 /*
4605 * The VPI bitmask and physical ID array are allocated
4606 * and initialized once only - at driver load. A port
4607 * reset doesn't need to reinitialize this memory.
4608 */
4609 if ((phba->vpi_bmask == NULL) && (phba->vpi_ids == NULL)) {
4610 longs = (phba->max_vpi + BITS_PER_LONG) / BITS_PER_LONG;
4611 phba->vpi_bmask = kzalloc(longs * sizeof(unsigned long),
4612 GFP_KERNEL);
4613 if (!phba->vpi_bmask) {
4614 rc = -ENOMEM;
4615 goto lpfc_sli_hba_setup_error;
4616 }
4617
4618 phba->vpi_ids = kzalloc(
4619 (phba->max_vpi+1) * sizeof(uint16_t),
4620 GFP_KERNEL);
4621 if (!phba->vpi_ids) {
4622 kfree(phba->vpi_bmask);
4623 rc = -ENOMEM;
4624 goto lpfc_sli_hba_setup_error;
4625 }
4626 for (i = 0; i < phba->max_vpi; i++)
4627 phba->vpi_ids[i] = i;
4628 }
4629 }
4630
James Smart93996272008-08-24 21:50:30 -04004631 /* Init HBQs */
James Smarted957682007-06-17 19:56:37 -05004632 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
4633 rc = lpfc_sli_hbq_setup(phba);
4634 if (rc)
4635 goto lpfc_sli_hba_setup_error;
4636 }
James Smart04c68492009-05-22 14:52:52 -04004637 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004638 phba->sli.sli_flag |= LPFC_PROCESS_LA;
James Smart04c68492009-05-22 14:52:52 -04004639 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004640
4641 rc = lpfc_config_port_post(phba);
4642 if (rc)
4643 goto lpfc_sli_hba_setup_error;
4644
James Smarted957682007-06-17 19:56:37 -05004645 return rc;
4646
James Smart92d7f7b2007-06-17 19:56:38 -05004647lpfc_sli_hba_setup_error:
James Smart2e0fef82007-06-17 19:56:36 -05004648 phba->link_state = LPFC_HBA_ERROR;
James Smarte40a02c2010-02-26 14:13:54 -05004649 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004650 "0445 Firmware initialization failed\n");
dea31012005-04-17 16:05:31 -05004651 return rc;
4652}
4653
James Smartda0436e2009-05-22 14:51:39 -04004654/**
4655 * lpfc_sli4_read_fcoe_params - Read fcoe params from conf region
4656 * @phba: Pointer to HBA context object.
4657 * @mboxq: mailbox pointer.
4658 * This function issue a dump mailbox command to read config region
4659 * 23 and parse the records in the region and populate driver
4660 * data structure.
4661 **/
4662static int
James Smartff78d8f2011-12-13 13:21:35 -05004663lpfc_sli4_read_fcoe_params(struct lpfc_hba *phba)
James Smartda0436e2009-05-22 14:51:39 -04004664{
James Smartff78d8f2011-12-13 13:21:35 -05004665 LPFC_MBOXQ_t *mboxq;
James Smartda0436e2009-05-22 14:51:39 -04004666 struct lpfc_dmabuf *mp;
4667 struct lpfc_mqe *mqe;
4668 uint32_t data_length;
4669 int rc;
4670
4671 /* Program the default value of vlan_id and fc_map */
4672 phba->valid_vlan = 0;
4673 phba->fc_map[0] = LPFC_FCOE_FCF_MAP0;
4674 phba->fc_map[1] = LPFC_FCOE_FCF_MAP1;
4675 phba->fc_map[2] = LPFC_FCOE_FCF_MAP2;
4676
James Smartff78d8f2011-12-13 13:21:35 -05004677 mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4678 if (!mboxq)
James Smartda0436e2009-05-22 14:51:39 -04004679 return -ENOMEM;
4680
James Smartff78d8f2011-12-13 13:21:35 -05004681 mqe = &mboxq->u.mqe;
4682 if (lpfc_sli4_dump_cfg_rg23(phba, mboxq)) {
4683 rc = -ENOMEM;
4684 goto out_free_mboxq;
4685 }
4686
James Smartda0436e2009-05-22 14:51:39 -04004687 mp = (struct lpfc_dmabuf *) mboxq->context1;
4688 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4689
4690 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
4691 "(%d):2571 Mailbox cmd x%x Status x%x "
4692 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x "
4693 "x%x x%x x%x x%x x%x x%x x%x x%x x%x "
4694 "CQ: x%x x%x x%x x%x\n",
4695 mboxq->vport ? mboxq->vport->vpi : 0,
4696 bf_get(lpfc_mqe_command, mqe),
4697 bf_get(lpfc_mqe_status, mqe),
4698 mqe->un.mb_words[0], mqe->un.mb_words[1],
4699 mqe->un.mb_words[2], mqe->un.mb_words[3],
4700 mqe->un.mb_words[4], mqe->un.mb_words[5],
4701 mqe->un.mb_words[6], mqe->un.mb_words[7],
4702 mqe->un.mb_words[8], mqe->un.mb_words[9],
4703 mqe->un.mb_words[10], mqe->un.mb_words[11],
4704 mqe->un.mb_words[12], mqe->un.mb_words[13],
4705 mqe->un.mb_words[14], mqe->un.mb_words[15],
4706 mqe->un.mb_words[16], mqe->un.mb_words[50],
4707 mboxq->mcqe.word0,
4708 mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
4709 mboxq->mcqe.trailer);
4710
4711 if (rc) {
4712 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4713 kfree(mp);
James Smartff78d8f2011-12-13 13:21:35 -05004714 rc = -EIO;
4715 goto out_free_mboxq;
James Smartda0436e2009-05-22 14:51:39 -04004716 }
4717 data_length = mqe->un.mb_words[5];
James Smarta0c87cb2009-07-19 10:01:10 -04004718 if (data_length > DMP_RGN23_SIZE) {
James Smartd11e31d2009-06-10 17:23:06 -04004719 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4720 kfree(mp);
James Smartff78d8f2011-12-13 13:21:35 -05004721 rc = -EIO;
4722 goto out_free_mboxq;
James Smartd11e31d2009-06-10 17:23:06 -04004723 }
James Smartda0436e2009-05-22 14:51:39 -04004724
4725 lpfc_parse_fcoe_conf(phba, mp->virt, data_length);
4726 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4727 kfree(mp);
James Smartff78d8f2011-12-13 13:21:35 -05004728 rc = 0;
4729
4730out_free_mboxq:
4731 mempool_free(mboxq, phba->mbox_mem_pool);
4732 return rc;
James Smartda0436e2009-05-22 14:51:39 -04004733}
4734
4735/**
4736 * lpfc_sli4_read_rev - Issue READ_REV and collect vpd data
4737 * @phba: pointer to lpfc hba data structure.
4738 * @mboxq: pointer to the LPFC_MBOXQ_t structure.
4739 * @vpd: pointer to the memory to hold resulting port vpd data.
4740 * @vpd_size: On input, the number of bytes allocated to @vpd.
4741 * On output, the number of data bytes in @vpd.
4742 *
4743 * This routine executes a READ_REV SLI4 mailbox command. In
4744 * addition, this routine gets the port vpd data.
4745 *
4746 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02004747 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -04004748 * -ENOMEM - could not allocated memory.
James Smartda0436e2009-05-22 14:51:39 -04004749 **/
4750static int
4751lpfc_sli4_read_rev(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
4752 uint8_t *vpd, uint32_t *vpd_size)
4753{
4754 int rc = 0;
4755 uint32_t dma_size;
4756 struct lpfc_dmabuf *dmabuf;
4757 struct lpfc_mqe *mqe;
4758
4759 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
4760 if (!dmabuf)
4761 return -ENOMEM;
4762
4763 /*
4764 * Get a DMA buffer for the vpd data resulting from the READ_REV
4765 * mailbox command.
4766 */
4767 dma_size = *vpd_size;
4768 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
4769 dma_size,
4770 &dmabuf->phys,
4771 GFP_KERNEL);
4772 if (!dmabuf->virt) {
4773 kfree(dmabuf);
4774 return -ENOMEM;
4775 }
4776 memset(dmabuf->virt, 0, dma_size);
4777
4778 /*
4779 * The SLI4 implementation of READ_REV conflicts at word1,
4780 * bits 31:16 and SLI4 adds vpd functionality not present
4781 * in SLI3. This code corrects the conflicts.
4782 */
4783 lpfc_read_rev(phba, mboxq);
4784 mqe = &mboxq->u.mqe;
4785 mqe->un.read_rev.vpd_paddr_high = putPaddrHigh(dmabuf->phys);
4786 mqe->un.read_rev.vpd_paddr_low = putPaddrLow(dmabuf->phys);
4787 mqe->un.read_rev.word1 &= 0x0000FFFF;
4788 bf_set(lpfc_mbx_rd_rev_vpd, &mqe->un.read_rev, 1);
4789 bf_set(lpfc_mbx_rd_rev_avail_len, &mqe->un.read_rev, dma_size);
4790
4791 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4792 if (rc) {
4793 dma_free_coherent(&phba->pcidev->dev, dma_size,
4794 dmabuf->virt, dmabuf->phys);
James Smartdef9c7a2009-12-21 17:02:28 -05004795 kfree(dmabuf);
James Smartda0436e2009-05-22 14:51:39 -04004796 return -EIO;
4797 }
4798
James Smartda0436e2009-05-22 14:51:39 -04004799 /*
4800 * The available vpd length cannot be bigger than the
4801 * DMA buffer passed to the port. Catch the less than
4802 * case and update the caller's size.
4803 */
4804 if (mqe->un.read_rev.avail_vpd_len < *vpd_size)
4805 *vpd_size = mqe->un.read_rev.avail_vpd_len;
4806
James Smartd7c47992010-06-08 18:31:54 -04004807 memcpy(vpd, dmabuf->virt, *vpd_size);
4808
James Smartda0436e2009-05-22 14:51:39 -04004809 dma_free_coherent(&phba->pcidev->dev, dma_size,
4810 dmabuf->virt, dmabuf->phys);
4811 kfree(dmabuf);
4812 return 0;
4813}
4814
4815/**
James Smartcd1c8302011-10-10 21:33:25 -04004816 * lpfc_sli4_retrieve_pport_name - Retrieve SLI4 device physical port name
4817 * @phba: pointer to lpfc hba data structure.
4818 *
4819 * This routine retrieves SLI4 device physical port name this PCI function
4820 * is attached to.
4821 *
4822 * Return codes
Anatol Pomozov4907cb72012-09-01 10:31:09 -07004823 * 0 - successful
James Smartcd1c8302011-10-10 21:33:25 -04004824 * otherwise - failed to retrieve physical port name
4825 **/
4826static int
4827lpfc_sli4_retrieve_pport_name(struct lpfc_hba *phba)
4828{
4829 LPFC_MBOXQ_t *mboxq;
James Smartcd1c8302011-10-10 21:33:25 -04004830 struct lpfc_mbx_get_cntl_attributes *mbx_cntl_attr;
4831 struct lpfc_controller_attribute *cntl_attr;
4832 struct lpfc_mbx_get_port_name *get_port_name;
4833 void *virtaddr = NULL;
4834 uint32_t alloclen, reqlen;
4835 uint32_t shdr_status, shdr_add_status;
4836 union lpfc_sli4_cfg_shdr *shdr;
4837 char cport_name = 0;
4838 int rc;
4839
4840 /* We assume nothing at this point */
4841 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_INVAL;
4842 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_NON;
4843
4844 mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4845 if (!mboxq)
4846 return -ENOMEM;
James Smartcd1c8302011-10-10 21:33:25 -04004847 /* obtain link type and link number via READ_CONFIG */
James Smartff78d8f2011-12-13 13:21:35 -05004848 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_INVAL;
4849 lpfc_sli4_read_config(phba);
4850 if (phba->sli4_hba.lnk_info.lnk_dv == LPFC_LNK_DAT_VAL)
4851 goto retrieve_ppname;
James Smartcd1c8302011-10-10 21:33:25 -04004852
4853 /* obtain link type and link number via COMMON_GET_CNTL_ATTRIBUTES */
4854 reqlen = sizeof(struct lpfc_mbx_get_cntl_attributes);
4855 alloclen = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
4856 LPFC_MBOX_OPCODE_GET_CNTL_ATTRIBUTES, reqlen,
4857 LPFC_SLI4_MBX_NEMBED);
4858 if (alloclen < reqlen) {
4859 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4860 "3084 Allocated DMA memory size (%d) is "
4861 "less than the requested DMA memory size "
4862 "(%d)\n", alloclen, reqlen);
4863 rc = -ENOMEM;
4864 goto out_free_mboxq;
4865 }
4866 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4867 virtaddr = mboxq->sge_array->addr[0];
4868 mbx_cntl_attr = (struct lpfc_mbx_get_cntl_attributes *)virtaddr;
4869 shdr = &mbx_cntl_attr->cfg_shdr;
4870 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
4871 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
4872 if (shdr_status || shdr_add_status || rc) {
4873 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
4874 "3085 Mailbox x%x (x%x/x%x) failed, "
4875 "rc:x%x, status:x%x, add_status:x%x\n",
4876 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
4877 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
4878 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
4879 rc, shdr_status, shdr_add_status);
4880 rc = -ENXIO;
4881 goto out_free_mboxq;
4882 }
4883 cntl_attr = &mbx_cntl_attr->cntl_attr;
4884 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_VAL;
4885 phba->sli4_hba.lnk_info.lnk_tp =
4886 bf_get(lpfc_cntl_attr_lnk_type, cntl_attr);
4887 phba->sli4_hba.lnk_info.lnk_no =
4888 bf_get(lpfc_cntl_attr_lnk_numb, cntl_attr);
4889 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4890 "3086 lnk_type:%d, lnk_numb:%d\n",
4891 phba->sli4_hba.lnk_info.lnk_tp,
4892 phba->sli4_hba.lnk_info.lnk_no);
4893
4894retrieve_ppname:
4895 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
4896 LPFC_MBOX_OPCODE_GET_PORT_NAME,
4897 sizeof(struct lpfc_mbx_get_port_name) -
4898 sizeof(struct lpfc_sli4_cfg_mhdr),
4899 LPFC_SLI4_MBX_EMBED);
4900 get_port_name = &mboxq->u.mqe.un.get_port_name;
4901 shdr = (union lpfc_sli4_cfg_shdr *)&get_port_name->header.cfg_shdr;
4902 bf_set(lpfc_mbox_hdr_version, &shdr->request, LPFC_OPCODE_VERSION_1);
4903 bf_set(lpfc_mbx_get_port_name_lnk_type, &get_port_name->u.request,
4904 phba->sli4_hba.lnk_info.lnk_tp);
4905 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4906 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
4907 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
4908 if (shdr_status || shdr_add_status || rc) {
4909 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
4910 "3087 Mailbox x%x (x%x/x%x) failed: "
4911 "rc:x%x, status:x%x, add_status:x%x\n",
4912 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
4913 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
4914 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
4915 rc, shdr_status, shdr_add_status);
4916 rc = -ENXIO;
4917 goto out_free_mboxq;
4918 }
4919 switch (phba->sli4_hba.lnk_info.lnk_no) {
4920 case LPFC_LINK_NUMBER_0:
4921 cport_name = bf_get(lpfc_mbx_get_port_name_name0,
4922 &get_port_name->u.response);
4923 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
4924 break;
4925 case LPFC_LINK_NUMBER_1:
4926 cport_name = bf_get(lpfc_mbx_get_port_name_name1,
4927 &get_port_name->u.response);
4928 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
4929 break;
4930 case LPFC_LINK_NUMBER_2:
4931 cport_name = bf_get(lpfc_mbx_get_port_name_name2,
4932 &get_port_name->u.response);
4933 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
4934 break;
4935 case LPFC_LINK_NUMBER_3:
4936 cport_name = bf_get(lpfc_mbx_get_port_name_name3,
4937 &get_port_name->u.response);
4938 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
4939 break;
4940 default:
4941 break;
4942 }
4943
4944 if (phba->sli4_hba.pport_name_sta == LPFC_SLI4_PPNAME_GET) {
4945 phba->Port[0] = cport_name;
4946 phba->Port[1] = '\0';
4947 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4948 "3091 SLI get port name: %s\n", phba->Port);
4949 }
4950
4951out_free_mboxq:
4952 if (rc != MBX_TIMEOUT) {
4953 if (bf_get(lpfc_mqe_command, &mboxq->u.mqe) == MBX_SLI4_CONFIG)
4954 lpfc_sli4_mbox_cmd_free(phba, mboxq);
4955 else
4956 mempool_free(mboxq, phba->mbox_mem_pool);
4957 }
4958 return rc;
4959}
4960
4961/**
James Smartda0436e2009-05-22 14:51:39 -04004962 * lpfc_sli4_arm_cqeq_intr - Arm sli-4 device completion and event queues
4963 * @phba: pointer to lpfc hba data structure.
4964 *
4965 * This routine is called to explicitly arm the SLI4 device's completion and
4966 * event queues
4967 **/
4968static void
4969lpfc_sli4_arm_cqeq_intr(struct lpfc_hba *phba)
4970{
James Smart962bc512013-01-03 15:44:00 -05004971 int fcp_eqidx;
James Smartda0436e2009-05-22 14:51:39 -04004972
4973 lpfc_sli4_cq_release(phba->sli4_hba.mbx_cq, LPFC_QUEUE_REARM);
4974 lpfc_sli4_cq_release(phba->sli4_hba.els_cq, LPFC_QUEUE_REARM);
James Smart05580562011-05-24 11:40:48 -04004975 fcp_eqidx = 0;
James Smart2e90f4b2011-12-13 13:22:37 -05004976 if (phba->sli4_hba.fcp_cq) {
James Smart67d12732012-08-03 12:36:13 -04004977 do {
James Smart2e90f4b2011-12-13 13:22:37 -05004978 lpfc_sli4_cq_release(phba->sli4_hba.fcp_cq[fcp_eqidx],
4979 LPFC_QUEUE_REARM);
James Smart67d12732012-08-03 12:36:13 -04004980 } while (++fcp_eqidx < phba->cfg_fcp_io_channel);
James Smart2e90f4b2011-12-13 13:22:37 -05004981 }
James Smart67d12732012-08-03 12:36:13 -04004982 if (phba->sli4_hba.hba_eq) {
4983 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_io_channel;
James Smart2e90f4b2011-12-13 13:22:37 -05004984 fcp_eqidx++)
James Smart67d12732012-08-03 12:36:13 -04004985 lpfc_sli4_eq_release(phba->sli4_hba.hba_eq[fcp_eqidx],
James Smart2e90f4b2011-12-13 13:22:37 -05004986 LPFC_QUEUE_REARM);
4987 }
James Smartda0436e2009-05-22 14:51:39 -04004988}
4989
4990/**
James Smart6d368e52011-05-24 11:44:12 -04004991 * lpfc_sli4_get_avail_extnt_rsrc - Get available resource extent count.
4992 * @phba: Pointer to HBA context object.
4993 * @type: The resource extent type.
James Smartb76f2dc2011-07-22 18:37:42 -04004994 * @extnt_count: buffer to hold port available extent count.
4995 * @extnt_size: buffer to hold element count per extent.
James Smart6d368e52011-05-24 11:44:12 -04004996 *
James Smartb76f2dc2011-07-22 18:37:42 -04004997 * This function calls the port and retrievs the number of available
4998 * extents and their size for a particular extent type.
4999 *
5000 * Returns: 0 if successful. Nonzero otherwise.
James Smart6d368e52011-05-24 11:44:12 -04005001 **/
James Smartb76f2dc2011-07-22 18:37:42 -04005002int
James Smart6d368e52011-05-24 11:44:12 -04005003lpfc_sli4_get_avail_extnt_rsrc(struct lpfc_hba *phba, uint16_t type,
5004 uint16_t *extnt_count, uint16_t *extnt_size)
5005{
5006 int rc = 0;
5007 uint32_t length;
5008 uint32_t mbox_tmo;
5009 struct lpfc_mbx_get_rsrc_extent_info *rsrc_info;
5010 LPFC_MBOXQ_t *mbox;
5011
5012 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5013 if (!mbox)
5014 return -ENOMEM;
5015
5016 /* Find out how many extents are available for this resource type */
5017 length = (sizeof(struct lpfc_mbx_get_rsrc_extent_info) -
5018 sizeof(struct lpfc_sli4_cfg_mhdr));
5019 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5020 LPFC_MBOX_OPCODE_GET_RSRC_EXTENT_INFO,
5021 length, LPFC_SLI4_MBX_EMBED);
5022
5023 /* Send an extents count of 0 - the GET doesn't use it. */
5024 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, 0, type,
5025 LPFC_SLI4_MBX_EMBED);
5026 if (unlikely(rc)) {
5027 rc = -EIO;
5028 goto err_exit;
5029 }
5030
5031 if (!phba->sli4_hba.intr_enable)
5032 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5033 else {
James Smarta183a152011-10-10 21:32:43 -04005034 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart6d368e52011-05-24 11:44:12 -04005035 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5036 }
5037 if (unlikely(rc)) {
5038 rc = -EIO;
5039 goto err_exit;
5040 }
5041
5042 rsrc_info = &mbox->u.mqe.un.rsrc_extent_info;
5043 if (bf_get(lpfc_mbox_hdr_status,
5044 &rsrc_info->header.cfg_shdr.response)) {
5045 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
5046 "2930 Failed to get resource extents "
5047 "Status 0x%x Add'l Status 0x%x\n",
5048 bf_get(lpfc_mbox_hdr_status,
5049 &rsrc_info->header.cfg_shdr.response),
5050 bf_get(lpfc_mbox_hdr_add_status,
5051 &rsrc_info->header.cfg_shdr.response));
5052 rc = -EIO;
5053 goto err_exit;
5054 }
5055
5056 *extnt_count = bf_get(lpfc_mbx_get_rsrc_extent_info_cnt,
5057 &rsrc_info->u.rsp);
5058 *extnt_size = bf_get(lpfc_mbx_get_rsrc_extent_info_size,
5059 &rsrc_info->u.rsp);
James Smart8a9d2e82012-05-09 21:16:12 -04005060
5061 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5062 "3162 Retrieved extents type-%d from port: count:%d, "
5063 "size:%d\n", type, *extnt_count, *extnt_size);
5064
5065err_exit:
James Smart6d368e52011-05-24 11:44:12 -04005066 mempool_free(mbox, phba->mbox_mem_pool);
5067 return rc;
5068}
5069
5070/**
5071 * lpfc_sli4_chk_avail_extnt_rsrc - Check for available SLI4 resource extents.
5072 * @phba: Pointer to HBA context object.
5073 * @type: The extent type to check.
5074 *
5075 * This function reads the current available extents from the port and checks
5076 * if the extent count or extent size has changed since the last access.
5077 * Callers use this routine post port reset to understand if there is a
5078 * extent reprovisioning requirement.
5079 *
5080 * Returns:
5081 * -Error: error indicates problem.
5082 * 1: Extent count or size has changed.
5083 * 0: No changes.
5084 **/
5085static int
5086lpfc_sli4_chk_avail_extnt_rsrc(struct lpfc_hba *phba, uint16_t type)
5087{
5088 uint16_t curr_ext_cnt, rsrc_ext_cnt;
5089 uint16_t size_diff, rsrc_ext_size;
5090 int rc = 0;
5091 struct lpfc_rsrc_blks *rsrc_entry;
5092 struct list_head *rsrc_blk_list = NULL;
5093
5094 size_diff = 0;
5095 curr_ext_cnt = 0;
5096 rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,
5097 &rsrc_ext_cnt,
5098 &rsrc_ext_size);
5099 if (unlikely(rc))
5100 return -EIO;
5101
5102 switch (type) {
5103 case LPFC_RSC_TYPE_FCOE_RPI:
5104 rsrc_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;
5105 break;
5106 case LPFC_RSC_TYPE_FCOE_VPI:
5107 rsrc_blk_list = &phba->lpfc_vpi_blk_list;
5108 break;
5109 case LPFC_RSC_TYPE_FCOE_XRI:
5110 rsrc_blk_list = &phba->sli4_hba.lpfc_xri_blk_list;
5111 break;
5112 case LPFC_RSC_TYPE_FCOE_VFI:
5113 rsrc_blk_list = &phba->sli4_hba.lpfc_vfi_blk_list;
5114 break;
5115 default:
5116 break;
5117 }
5118
5119 list_for_each_entry(rsrc_entry, rsrc_blk_list, list) {
5120 curr_ext_cnt++;
5121 if (rsrc_entry->rsrc_size != rsrc_ext_size)
5122 size_diff++;
5123 }
5124
5125 if (curr_ext_cnt != rsrc_ext_cnt || size_diff != 0)
5126 rc = 1;
5127
5128 return rc;
5129}
5130
5131/**
5132 * lpfc_sli4_cfg_post_extnts -
5133 * @phba: Pointer to HBA context object.
5134 * @extnt_cnt - number of available extents.
5135 * @type - the extent type (rpi, xri, vfi, vpi).
5136 * @emb - buffer to hold either MBX_EMBED or MBX_NEMBED operation.
5137 * @mbox - pointer to the caller's allocated mailbox structure.
5138 *
5139 * This function executes the extents allocation request. It also
5140 * takes care of the amount of memory needed to allocate or get the
5141 * allocated extents. It is the caller's responsibility to evaluate
5142 * the response.
5143 *
5144 * Returns:
5145 * -Error: Error value describes the condition found.
5146 * 0: if successful
5147 **/
5148static int
James Smart8a9d2e82012-05-09 21:16:12 -04005149lpfc_sli4_cfg_post_extnts(struct lpfc_hba *phba, uint16_t extnt_cnt,
James Smart6d368e52011-05-24 11:44:12 -04005150 uint16_t type, bool *emb, LPFC_MBOXQ_t *mbox)
5151{
5152 int rc = 0;
5153 uint32_t req_len;
5154 uint32_t emb_len;
5155 uint32_t alloc_len, mbox_tmo;
5156
5157 /* Calculate the total requested length of the dma memory */
James Smart8a9d2e82012-05-09 21:16:12 -04005158 req_len = extnt_cnt * sizeof(uint16_t);
James Smart6d368e52011-05-24 11:44:12 -04005159
5160 /*
5161 * Calculate the size of an embedded mailbox. The uint32_t
5162 * accounts for extents-specific word.
5163 */
5164 emb_len = sizeof(MAILBOX_t) - sizeof(struct mbox_header) -
5165 sizeof(uint32_t);
5166
5167 /*
5168 * Presume the allocation and response will fit into an embedded
5169 * mailbox. If not true, reconfigure to a non-embedded mailbox.
5170 */
5171 *emb = LPFC_SLI4_MBX_EMBED;
5172 if (req_len > emb_len) {
James Smart8a9d2e82012-05-09 21:16:12 -04005173 req_len = extnt_cnt * sizeof(uint16_t) +
James Smart6d368e52011-05-24 11:44:12 -04005174 sizeof(union lpfc_sli4_cfg_shdr) +
5175 sizeof(uint32_t);
5176 *emb = LPFC_SLI4_MBX_NEMBED;
5177 }
5178
5179 alloc_len = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5180 LPFC_MBOX_OPCODE_ALLOC_RSRC_EXTENT,
5181 req_len, *emb);
5182 if (alloc_len < req_len) {
5183 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartb76f2dc2011-07-22 18:37:42 -04005184 "2982 Allocated DMA memory size (x%x) is "
James Smart6d368e52011-05-24 11:44:12 -04005185 "less than the requested DMA memory "
5186 "size (x%x)\n", alloc_len, req_len);
5187 return -ENOMEM;
5188 }
James Smart8a9d2e82012-05-09 21:16:12 -04005189 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, extnt_cnt, type, *emb);
James Smart6d368e52011-05-24 11:44:12 -04005190 if (unlikely(rc))
5191 return -EIO;
5192
5193 if (!phba->sli4_hba.intr_enable)
5194 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5195 else {
James Smarta183a152011-10-10 21:32:43 -04005196 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart6d368e52011-05-24 11:44:12 -04005197 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5198 }
5199
5200 if (unlikely(rc))
5201 rc = -EIO;
5202 return rc;
5203}
5204
5205/**
5206 * lpfc_sli4_alloc_extent - Allocate an SLI4 resource extent.
5207 * @phba: Pointer to HBA context object.
5208 * @type: The resource extent type to allocate.
5209 *
5210 * This function allocates the number of elements for the specified
5211 * resource type.
5212 **/
5213static int
5214lpfc_sli4_alloc_extent(struct lpfc_hba *phba, uint16_t type)
5215{
5216 bool emb = false;
5217 uint16_t rsrc_id_cnt, rsrc_cnt, rsrc_size;
5218 uint16_t rsrc_id, rsrc_start, j, k;
5219 uint16_t *ids;
5220 int i, rc;
5221 unsigned long longs;
5222 unsigned long *bmask;
5223 struct lpfc_rsrc_blks *rsrc_blks;
5224 LPFC_MBOXQ_t *mbox;
5225 uint32_t length;
5226 struct lpfc_id_range *id_array = NULL;
5227 void *virtaddr = NULL;
5228 struct lpfc_mbx_nembed_rsrc_extent *n_rsrc;
5229 struct lpfc_mbx_alloc_rsrc_extents *rsrc_ext;
5230 struct list_head *ext_blk_list;
5231
5232 rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,
5233 &rsrc_cnt,
5234 &rsrc_size);
5235 if (unlikely(rc))
5236 return -EIO;
5237
5238 if ((rsrc_cnt == 0) || (rsrc_size == 0)) {
5239 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
5240 "3009 No available Resource Extents "
5241 "for resource type 0x%x: Count: 0x%x, "
5242 "Size 0x%x\n", type, rsrc_cnt,
5243 rsrc_size);
5244 return -ENOMEM;
5245 }
5246
James Smart8a9d2e82012-05-09 21:16:12 -04005247 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_INIT | LOG_SLI,
5248 "2903 Post resource extents type-0x%x: "
5249 "count:%d, size %d\n", type, rsrc_cnt, rsrc_size);
James Smart6d368e52011-05-24 11:44:12 -04005250
5251 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5252 if (!mbox)
5253 return -ENOMEM;
5254
James Smart8a9d2e82012-05-09 21:16:12 -04005255 rc = lpfc_sli4_cfg_post_extnts(phba, rsrc_cnt, type, &emb, mbox);
James Smart6d368e52011-05-24 11:44:12 -04005256 if (unlikely(rc)) {
5257 rc = -EIO;
5258 goto err_exit;
5259 }
5260
5261 /*
5262 * Figure out where the response is located. Then get local pointers
5263 * to the response data. The port does not guarantee to respond to
5264 * all extents counts request so update the local variable with the
5265 * allocated count from the port.
5266 */
5267 if (emb == LPFC_SLI4_MBX_EMBED) {
5268 rsrc_ext = &mbox->u.mqe.un.alloc_rsrc_extents;
5269 id_array = &rsrc_ext->u.rsp.id[0];
5270 rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, &rsrc_ext->u.rsp);
5271 } else {
5272 virtaddr = mbox->sge_array->addr[0];
5273 n_rsrc = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
5274 rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, n_rsrc);
5275 id_array = &n_rsrc->id;
5276 }
5277
5278 longs = ((rsrc_cnt * rsrc_size) + BITS_PER_LONG - 1) / BITS_PER_LONG;
5279 rsrc_id_cnt = rsrc_cnt * rsrc_size;
5280
5281 /*
5282 * Based on the resource size and count, correct the base and max
5283 * resource values.
5284 */
5285 length = sizeof(struct lpfc_rsrc_blks);
5286 switch (type) {
5287 case LPFC_RSC_TYPE_FCOE_RPI:
5288 phba->sli4_hba.rpi_bmask = kzalloc(longs *
5289 sizeof(unsigned long),
5290 GFP_KERNEL);
5291 if (unlikely(!phba->sli4_hba.rpi_bmask)) {
5292 rc = -ENOMEM;
5293 goto err_exit;
5294 }
5295 phba->sli4_hba.rpi_ids = kzalloc(rsrc_id_cnt *
5296 sizeof(uint16_t),
5297 GFP_KERNEL);
5298 if (unlikely(!phba->sli4_hba.rpi_ids)) {
5299 kfree(phba->sli4_hba.rpi_bmask);
5300 rc = -ENOMEM;
5301 goto err_exit;
5302 }
5303
5304 /*
5305 * The next_rpi was initialized with the maximum available
5306 * count but the port may allocate a smaller number. Catch
5307 * that case and update the next_rpi.
5308 */
5309 phba->sli4_hba.next_rpi = rsrc_id_cnt;
5310
5311 /* Initialize local ptrs for common extent processing later. */
5312 bmask = phba->sli4_hba.rpi_bmask;
5313 ids = phba->sli4_hba.rpi_ids;
5314 ext_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;
5315 break;
5316 case LPFC_RSC_TYPE_FCOE_VPI:
5317 phba->vpi_bmask = kzalloc(longs *
5318 sizeof(unsigned long),
5319 GFP_KERNEL);
5320 if (unlikely(!phba->vpi_bmask)) {
5321 rc = -ENOMEM;
5322 goto err_exit;
5323 }
5324 phba->vpi_ids = kzalloc(rsrc_id_cnt *
5325 sizeof(uint16_t),
5326 GFP_KERNEL);
5327 if (unlikely(!phba->vpi_ids)) {
5328 kfree(phba->vpi_bmask);
5329 rc = -ENOMEM;
5330 goto err_exit;
5331 }
5332
5333 /* Initialize local ptrs for common extent processing later. */
5334 bmask = phba->vpi_bmask;
5335 ids = phba->vpi_ids;
5336 ext_blk_list = &phba->lpfc_vpi_blk_list;
5337 break;
5338 case LPFC_RSC_TYPE_FCOE_XRI:
5339 phba->sli4_hba.xri_bmask = kzalloc(longs *
5340 sizeof(unsigned long),
5341 GFP_KERNEL);
5342 if (unlikely(!phba->sli4_hba.xri_bmask)) {
5343 rc = -ENOMEM;
5344 goto err_exit;
5345 }
James Smart8a9d2e82012-05-09 21:16:12 -04005346 phba->sli4_hba.max_cfg_param.xri_used = 0;
James Smart6d368e52011-05-24 11:44:12 -04005347 phba->sli4_hba.xri_ids = kzalloc(rsrc_id_cnt *
5348 sizeof(uint16_t),
5349 GFP_KERNEL);
5350 if (unlikely(!phba->sli4_hba.xri_ids)) {
5351 kfree(phba->sli4_hba.xri_bmask);
5352 rc = -ENOMEM;
5353 goto err_exit;
5354 }
5355
5356 /* Initialize local ptrs for common extent processing later. */
5357 bmask = phba->sli4_hba.xri_bmask;
5358 ids = phba->sli4_hba.xri_ids;
5359 ext_blk_list = &phba->sli4_hba.lpfc_xri_blk_list;
5360 break;
5361 case LPFC_RSC_TYPE_FCOE_VFI:
5362 phba->sli4_hba.vfi_bmask = kzalloc(longs *
5363 sizeof(unsigned long),
5364 GFP_KERNEL);
5365 if (unlikely(!phba->sli4_hba.vfi_bmask)) {
5366 rc = -ENOMEM;
5367 goto err_exit;
5368 }
5369 phba->sli4_hba.vfi_ids = kzalloc(rsrc_id_cnt *
5370 sizeof(uint16_t),
5371 GFP_KERNEL);
5372 if (unlikely(!phba->sli4_hba.vfi_ids)) {
5373 kfree(phba->sli4_hba.vfi_bmask);
5374 rc = -ENOMEM;
5375 goto err_exit;
5376 }
5377
5378 /* Initialize local ptrs for common extent processing later. */
5379 bmask = phba->sli4_hba.vfi_bmask;
5380 ids = phba->sli4_hba.vfi_ids;
5381 ext_blk_list = &phba->sli4_hba.lpfc_vfi_blk_list;
5382 break;
5383 default:
5384 /* Unsupported Opcode. Fail call. */
5385 id_array = NULL;
5386 bmask = NULL;
5387 ids = NULL;
5388 ext_blk_list = NULL;
5389 goto err_exit;
5390 }
5391
5392 /*
5393 * Complete initializing the extent configuration with the
5394 * allocated ids assigned to this function. The bitmask serves
5395 * as an index into the array and manages the available ids. The
5396 * array just stores the ids communicated to the port via the wqes.
5397 */
5398 for (i = 0, j = 0, k = 0; i < rsrc_cnt; i++) {
5399 if ((i % 2) == 0)
5400 rsrc_id = bf_get(lpfc_mbx_rsrc_id_word4_0,
5401 &id_array[k]);
5402 else
5403 rsrc_id = bf_get(lpfc_mbx_rsrc_id_word4_1,
5404 &id_array[k]);
5405
5406 rsrc_blks = kzalloc(length, GFP_KERNEL);
5407 if (unlikely(!rsrc_blks)) {
5408 rc = -ENOMEM;
5409 kfree(bmask);
5410 kfree(ids);
5411 goto err_exit;
5412 }
5413 rsrc_blks->rsrc_start = rsrc_id;
5414 rsrc_blks->rsrc_size = rsrc_size;
5415 list_add_tail(&rsrc_blks->list, ext_blk_list);
5416 rsrc_start = rsrc_id;
5417 if ((type == LPFC_RSC_TYPE_FCOE_XRI) && (j == 0))
5418 phba->sli4_hba.scsi_xri_start = rsrc_start +
5419 lpfc_sli4_get_els_iocb_cnt(phba);
5420
5421 while (rsrc_id < (rsrc_start + rsrc_size)) {
5422 ids[j] = rsrc_id;
5423 rsrc_id++;
5424 j++;
5425 }
5426 /* Entire word processed. Get next word.*/
5427 if ((i % 2) == 1)
5428 k++;
5429 }
5430 err_exit:
5431 lpfc_sli4_mbox_cmd_free(phba, mbox);
5432 return rc;
5433}
5434
5435/**
5436 * lpfc_sli4_dealloc_extent - Deallocate an SLI4 resource extent.
5437 * @phba: Pointer to HBA context object.
5438 * @type: the extent's type.
5439 *
5440 * This function deallocates all extents of a particular resource type.
5441 * SLI4 does not allow for deallocating a particular extent range. It
5442 * is the caller's responsibility to release all kernel memory resources.
5443 **/
5444static int
5445lpfc_sli4_dealloc_extent(struct lpfc_hba *phba, uint16_t type)
5446{
5447 int rc;
5448 uint32_t length, mbox_tmo = 0;
5449 LPFC_MBOXQ_t *mbox;
5450 struct lpfc_mbx_dealloc_rsrc_extents *dealloc_rsrc;
5451 struct lpfc_rsrc_blks *rsrc_blk, *rsrc_blk_next;
5452
5453 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5454 if (!mbox)
5455 return -ENOMEM;
5456
5457 /*
5458 * This function sends an embedded mailbox because it only sends the
5459 * the resource type. All extents of this type are released by the
5460 * port.
5461 */
5462 length = (sizeof(struct lpfc_mbx_dealloc_rsrc_extents) -
5463 sizeof(struct lpfc_sli4_cfg_mhdr));
5464 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5465 LPFC_MBOX_OPCODE_DEALLOC_RSRC_EXTENT,
5466 length, LPFC_SLI4_MBX_EMBED);
5467
5468 /* Send an extents count of 0 - the dealloc doesn't use it. */
5469 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, 0, type,
5470 LPFC_SLI4_MBX_EMBED);
5471 if (unlikely(rc)) {
5472 rc = -EIO;
5473 goto out_free_mbox;
5474 }
5475 if (!phba->sli4_hba.intr_enable)
5476 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5477 else {
James Smarta183a152011-10-10 21:32:43 -04005478 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart6d368e52011-05-24 11:44:12 -04005479 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5480 }
5481 if (unlikely(rc)) {
5482 rc = -EIO;
5483 goto out_free_mbox;
5484 }
5485
5486 dealloc_rsrc = &mbox->u.mqe.un.dealloc_rsrc_extents;
5487 if (bf_get(lpfc_mbox_hdr_status,
5488 &dealloc_rsrc->header.cfg_shdr.response)) {
5489 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
5490 "2919 Failed to release resource extents "
5491 "for type %d - Status 0x%x Add'l Status 0x%x. "
5492 "Resource memory not released.\n",
5493 type,
5494 bf_get(lpfc_mbox_hdr_status,
5495 &dealloc_rsrc->header.cfg_shdr.response),
5496 bf_get(lpfc_mbox_hdr_add_status,
5497 &dealloc_rsrc->header.cfg_shdr.response));
5498 rc = -EIO;
5499 goto out_free_mbox;
5500 }
5501
5502 /* Release kernel memory resources for the specific type. */
5503 switch (type) {
5504 case LPFC_RSC_TYPE_FCOE_VPI:
5505 kfree(phba->vpi_bmask);
5506 kfree(phba->vpi_ids);
5507 bf_set(lpfc_vpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5508 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5509 &phba->lpfc_vpi_blk_list, list) {
5510 list_del_init(&rsrc_blk->list);
5511 kfree(rsrc_blk);
5512 }
5513 break;
5514 case LPFC_RSC_TYPE_FCOE_XRI:
5515 kfree(phba->sli4_hba.xri_bmask);
5516 kfree(phba->sli4_hba.xri_ids);
James Smart6d368e52011-05-24 11:44:12 -04005517 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5518 &phba->sli4_hba.lpfc_xri_blk_list, list) {
5519 list_del_init(&rsrc_blk->list);
5520 kfree(rsrc_blk);
5521 }
5522 break;
5523 case LPFC_RSC_TYPE_FCOE_VFI:
5524 kfree(phba->sli4_hba.vfi_bmask);
5525 kfree(phba->sli4_hba.vfi_ids);
5526 bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5527 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5528 &phba->sli4_hba.lpfc_vfi_blk_list, list) {
5529 list_del_init(&rsrc_blk->list);
5530 kfree(rsrc_blk);
5531 }
5532 break;
5533 case LPFC_RSC_TYPE_FCOE_RPI:
5534 /* RPI bitmask and physical id array are cleaned up earlier. */
5535 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5536 &phba->sli4_hba.lpfc_rpi_blk_list, list) {
5537 list_del_init(&rsrc_blk->list);
5538 kfree(rsrc_blk);
5539 }
5540 break;
5541 default:
5542 break;
5543 }
5544
5545 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5546
5547 out_free_mbox:
5548 mempool_free(mbox, phba->mbox_mem_pool);
5549 return rc;
5550}
5551
5552/**
5553 * lpfc_sli4_alloc_resource_identifiers - Allocate all SLI4 resource extents.
5554 * @phba: Pointer to HBA context object.
5555 *
5556 * This function allocates all SLI4 resource identifiers.
5557 **/
5558int
5559lpfc_sli4_alloc_resource_identifiers(struct lpfc_hba *phba)
5560{
5561 int i, rc, error = 0;
5562 uint16_t count, base;
5563 unsigned long longs;
5564
James Smartff78d8f2011-12-13 13:21:35 -05005565 if (!phba->sli4_hba.rpi_hdrs_in_use)
5566 phba->sli4_hba.next_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
James Smart6d368e52011-05-24 11:44:12 -04005567 if (phba->sli4_hba.extents_in_use) {
5568 /*
5569 * The port supports resource extents. The XRI, VPI, VFI, RPI
5570 * resource extent count must be read and allocated before
5571 * provisioning the resource id arrays.
5572 */
5573 if (bf_get(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags) ==
5574 LPFC_IDX_RSRC_RDY) {
5575 /*
5576 * Extent-based resources are set - the driver could
5577 * be in a port reset. Figure out if any corrective
5578 * actions need to be taken.
5579 */
5580 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
5581 LPFC_RSC_TYPE_FCOE_VFI);
5582 if (rc != 0)
5583 error++;
5584 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
5585 LPFC_RSC_TYPE_FCOE_VPI);
5586 if (rc != 0)
5587 error++;
5588 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
5589 LPFC_RSC_TYPE_FCOE_XRI);
5590 if (rc != 0)
5591 error++;
5592 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
5593 LPFC_RSC_TYPE_FCOE_RPI);
5594 if (rc != 0)
5595 error++;
5596
5597 /*
5598 * It's possible that the number of resources
5599 * provided to this port instance changed between
5600 * resets. Detect this condition and reallocate
5601 * resources. Otherwise, there is no action.
5602 */
5603 if (error) {
5604 lpfc_printf_log(phba, KERN_INFO,
5605 LOG_MBOX | LOG_INIT,
5606 "2931 Detected extent resource "
5607 "change. Reallocating all "
5608 "extents.\n");
5609 rc = lpfc_sli4_dealloc_extent(phba,
5610 LPFC_RSC_TYPE_FCOE_VFI);
5611 rc = lpfc_sli4_dealloc_extent(phba,
5612 LPFC_RSC_TYPE_FCOE_VPI);
5613 rc = lpfc_sli4_dealloc_extent(phba,
5614 LPFC_RSC_TYPE_FCOE_XRI);
5615 rc = lpfc_sli4_dealloc_extent(phba,
5616 LPFC_RSC_TYPE_FCOE_RPI);
5617 } else
5618 return 0;
5619 }
5620
5621 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_VFI);
5622 if (unlikely(rc))
5623 goto err_exit;
5624
5625 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_VPI);
5626 if (unlikely(rc))
5627 goto err_exit;
5628
5629 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_RPI);
5630 if (unlikely(rc))
5631 goto err_exit;
5632
5633 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_XRI);
5634 if (unlikely(rc))
5635 goto err_exit;
5636 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags,
5637 LPFC_IDX_RSRC_RDY);
5638 return rc;
5639 } else {
5640 /*
5641 * The port does not support resource extents. The XRI, VPI,
5642 * VFI, RPI resource ids were determined from READ_CONFIG.
5643 * Just allocate the bitmasks and provision the resource id
5644 * arrays. If a port reset is active, the resources don't
5645 * need any action - just exit.
5646 */
5647 if (bf_get(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags) ==
James Smartff78d8f2011-12-13 13:21:35 -05005648 LPFC_IDX_RSRC_RDY) {
5649 lpfc_sli4_dealloc_resource_identifiers(phba);
5650 lpfc_sli4_remove_rpis(phba);
5651 }
James Smart6d368e52011-05-24 11:44:12 -04005652 /* RPIs. */
5653 count = phba->sli4_hba.max_cfg_param.max_rpi;
James Smart0a630c22013-01-03 15:44:09 -05005654 if (count <= 0) {
5655 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5656 "3279 Invalid provisioning of "
5657 "rpi:%d\n", count);
5658 rc = -EINVAL;
5659 goto err_exit;
5660 }
James Smart6d368e52011-05-24 11:44:12 -04005661 base = phba->sli4_hba.max_cfg_param.rpi_base;
5662 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
5663 phba->sli4_hba.rpi_bmask = kzalloc(longs *
5664 sizeof(unsigned long),
5665 GFP_KERNEL);
5666 if (unlikely(!phba->sli4_hba.rpi_bmask)) {
5667 rc = -ENOMEM;
5668 goto err_exit;
5669 }
5670 phba->sli4_hba.rpi_ids = kzalloc(count *
5671 sizeof(uint16_t),
5672 GFP_KERNEL);
5673 if (unlikely(!phba->sli4_hba.rpi_ids)) {
5674 rc = -ENOMEM;
5675 goto free_rpi_bmask;
5676 }
5677
5678 for (i = 0; i < count; i++)
5679 phba->sli4_hba.rpi_ids[i] = base + i;
5680
5681 /* VPIs. */
5682 count = phba->sli4_hba.max_cfg_param.max_vpi;
James Smart0a630c22013-01-03 15:44:09 -05005683 if (count <= 0) {
5684 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5685 "3280 Invalid provisioning of "
5686 "vpi:%d\n", count);
5687 rc = -EINVAL;
5688 goto free_rpi_ids;
5689 }
James Smart6d368e52011-05-24 11:44:12 -04005690 base = phba->sli4_hba.max_cfg_param.vpi_base;
5691 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
5692 phba->vpi_bmask = kzalloc(longs *
5693 sizeof(unsigned long),
5694 GFP_KERNEL);
5695 if (unlikely(!phba->vpi_bmask)) {
5696 rc = -ENOMEM;
5697 goto free_rpi_ids;
5698 }
5699 phba->vpi_ids = kzalloc(count *
5700 sizeof(uint16_t),
5701 GFP_KERNEL);
5702 if (unlikely(!phba->vpi_ids)) {
5703 rc = -ENOMEM;
5704 goto free_vpi_bmask;
5705 }
5706
5707 for (i = 0; i < count; i++)
5708 phba->vpi_ids[i] = base + i;
5709
5710 /* XRIs. */
5711 count = phba->sli4_hba.max_cfg_param.max_xri;
James Smart0a630c22013-01-03 15:44:09 -05005712 if (count <= 0) {
5713 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5714 "3281 Invalid provisioning of "
5715 "xri:%d\n", count);
5716 rc = -EINVAL;
5717 goto free_vpi_ids;
5718 }
James Smart6d368e52011-05-24 11:44:12 -04005719 base = phba->sli4_hba.max_cfg_param.xri_base;
5720 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
5721 phba->sli4_hba.xri_bmask = kzalloc(longs *
5722 sizeof(unsigned long),
5723 GFP_KERNEL);
5724 if (unlikely(!phba->sli4_hba.xri_bmask)) {
5725 rc = -ENOMEM;
5726 goto free_vpi_ids;
5727 }
James Smart41899be2012-03-01 22:34:19 -05005728 phba->sli4_hba.max_cfg_param.xri_used = 0;
James Smart6d368e52011-05-24 11:44:12 -04005729 phba->sli4_hba.xri_ids = kzalloc(count *
5730 sizeof(uint16_t),
5731 GFP_KERNEL);
5732 if (unlikely(!phba->sli4_hba.xri_ids)) {
5733 rc = -ENOMEM;
5734 goto free_xri_bmask;
5735 }
5736
5737 for (i = 0; i < count; i++)
5738 phba->sli4_hba.xri_ids[i] = base + i;
5739
5740 /* VFIs. */
5741 count = phba->sli4_hba.max_cfg_param.max_vfi;
James Smart0a630c22013-01-03 15:44:09 -05005742 if (count <= 0) {
5743 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5744 "3282 Invalid provisioning of "
5745 "vfi:%d\n", count);
5746 rc = -EINVAL;
5747 goto free_xri_ids;
5748 }
James Smart6d368e52011-05-24 11:44:12 -04005749 base = phba->sli4_hba.max_cfg_param.vfi_base;
5750 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
5751 phba->sli4_hba.vfi_bmask = kzalloc(longs *
5752 sizeof(unsigned long),
5753 GFP_KERNEL);
5754 if (unlikely(!phba->sli4_hba.vfi_bmask)) {
5755 rc = -ENOMEM;
5756 goto free_xri_ids;
5757 }
5758 phba->sli4_hba.vfi_ids = kzalloc(count *
5759 sizeof(uint16_t),
5760 GFP_KERNEL);
5761 if (unlikely(!phba->sli4_hba.vfi_ids)) {
5762 rc = -ENOMEM;
5763 goto free_vfi_bmask;
5764 }
5765
5766 for (i = 0; i < count; i++)
5767 phba->sli4_hba.vfi_ids[i] = base + i;
5768
5769 /*
5770 * Mark all resources ready. An HBA reset doesn't need
5771 * to reset the initialization.
5772 */
5773 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags,
5774 LPFC_IDX_RSRC_RDY);
5775 return 0;
5776 }
5777
5778 free_vfi_bmask:
5779 kfree(phba->sli4_hba.vfi_bmask);
5780 free_xri_ids:
5781 kfree(phba->sli4_hba.xri_ids);
5782 free_xri_bmask:
5783 kfree(phba->sli4_hba.xri_bmask);
5784 free_vpi_ids:
5785 kfree(phba->vpi_ids);
5786 free_vpi_bmask:
5787 kfree(phba->vpi_bmask);
5788 free_rpi_ids:
5789 kfree(phba->sli4_hba.rpi_ids);
5790 free_rpi_bmask:
5791 kfree(phba->sli4_hba.rpi_bmask);
5792 err_exit:
5793 return rc;
5794}
5795
5796/**
5797 * lpfc_sli4_dealloc_resource_identifiers - Deallocate all SLI4 resource extents.
5798 * @phba: Pointer to HBA context object.
5799 *
5800 * This function allocates the number of elements for the specified
5801 * resource type.
5802 **/
5803int
5804lpfc_sli4_dealloc_resource_identifiers(struct lpfc_hba *phba)
5805{
5806 if (phba->sli4_hba.extents_in_use) {
5807 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VPI);
5808 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_RPI);
5809 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_XRI);
5810 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VFI);
5811 } else {
5812 kfree(phba->vpi_bmask);
5813 kfree(phba->vpi_ids);
5814 bf_set(lpfc_vpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5815 kfree(phba->sli4_hba.xri_bmask);
5816 kfree(phba->sli4_hba.xri_ids);
James Smart6d368e52011-05-24 11:44:12 -04005817 kfree(phba->sli4_hba.vfi_bmask);
5818 kfree(phba->sli4_hba.vfi_ids);
5819 bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5820 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5821 }
5822
5823 return 0;
5824}
5825
5826/**
James Smartb76f2dc2011-07-22 18:37:42 -04005827 * lpfc_sli4_get_allocated_extnts - Get the port's allocated extents.
5828 * @phba: Pointer to HBA context object.
5829 * @type: The resource extent type.
5830 * @extnt_count: buffer to hold port extent count response
5831 * @extnt_size: buffer to hold port extent size response.
5832 *
5833 * This function calls the port to read the host allocated extents
5834 * for a particular type.
5835 **/
5836int
5837lpfc_sli4_get_allocated_extnts(struct lpfc_hba *phba, uint16_t type,
5838 uint16_t *extnt_cnt, uint16_t *extnt_size)
5839{
5840 bool emb;
5841 int rc = 0;
5842 uint16_t curr_blks = 0;
5843 uint32_t req_len, emb_len;
5844 uint32_t alloc_len, mbox_tmo;
5845 struct list_head *blk_list_head;
5846 struct lpfc_rsrc_blks *rsrc_blk;
5847 LPFC_MBOXQ_t *mbox;
5848 void *virtaddr = NULL;
5849 struct lpfc_mbx_nembed_rsrc_extent *n_rsrc;
5850 struct lpfc_mbx_alloc_rsrc_extents *rsrc_ext;
5851 union lpfc_sli4_cfg_shdr *shdr;
5852
5853 switch (type) {
5854 case LPFC_RSC_TYPE_FCOE_VPI:
5855 blk_list_head = &phba->lpfc_vpi_blk_list;
5856 break;
5857 case LPFC_RSC_TYPE_FCOE_XRI:
5858 blk_list_head = &phba->sli4_hba.lpfc_xri_blk_list;
5859 break;
5860 case LPFC_RSC_TYPE_FCOE_VFI:
5861 blk_list_head = &phba->sli4_hba.lpfc_vfi_blk_list;
5862 break;
5863 case LPFC_RSC_TYPE_FCOE_RPI:
5864 blk_list_head = &phba->sli4_hba.lpfc_rpi_blk_list;
5865 break;
5866 default:
5867 return -EIO;
5868 }
5869
5870 /* Count the number of extents currently allocatd for this type. */
5871 list_for_each_entry(rsrc_blk, blk_list_head, list) {
5872 if (curr_blks == 0) {
5873 /*
5874 * The GET_ALLOCATED mailbox does not return the size,
5875 * just the count. The size should be just the size
5876 * stored in the current allocated block and all sizes
5877 * for an extent type are the same so set the return
5878 * value now.
5879 */
5880 *extnt_size = rsrc_blk->rsrc_size;
5881 }
5882 curr_blks++;
5883 }
5884
5885 /* Calculate the total requested length of the dma memory. */
5886 req_len = curr_blks * sizeof(uint16_t);
5887
5888 /*
5889 * Calculate the size of an embedded mailbox. The uint32_t
5890 * accounts for extents-specific word.
5891 */
5892 emb_len = sizeof(MAILBOX_t) - sizeof(struct mbox_header) -
5893 sizeof(uint32_t);
5894
5895 /*
5896 * Presume the allocation and response will fit into an embedded
5897 * mailbox. If not true, reconfigure to a non-embedded mailbox.
5898 */
5899 emb = LPFC_SLI4_MBX_EMBED;
5900 req_len = emb_len;
5901 if (req_len > emb_len) {
5902 req_len = curr_blks * sizeof(uint16_t) +
5903 sizeof(union lpfc_sli4_cfg_shdr) +
5904 sizeof(uint32_t);
5905 emb = LPFC_SLI4_MBX_NEMBED;
5906 }
5907
5908 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5909 if (!mbox)
5910 return -ENOMEM;
5911 memset(mbox, 0, sizeof(LPFC_MBOXQ_t));
5912
5913 alloc_len = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5914 LPFC_MBOX_OPCODE_GET_ALLOC_RSRC_EXTENT,
5915 req_len, emb);
5916 if (alloc_len < req_len) {
5917 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5918 "2983 Allocated DMA memory size (x%x) is "
5919 "less than the requested DMA memory "
5920 "size (x%x)\n", alloc_len, req_len);
5921 rc = -ENOMEM;
5922 goto err_exit;
5923 }
5924 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, curr_blks, type, emb);
5925 if (unlikely(rc)) {
5926 rc = -EIO;
5927 goto err_exit;
5928 }
5929
5930 if (!phba->sli4_hba.intr_enable)
5931 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5932 else {
James Smarta183a152011-10-10 21:32:43 -04005933 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smartb76f2dc2011-07-22 18:37:42 -04005934 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5935 }
5936
5937 if (unlikely(rc)) {
5938 rc = -EIO;
5939 goto err_exit;
5940 }
5941
5942 /*
5943 * Figure out where the response is located. Then get local pointers
5944 * to the response data. The port does not guarantee to respond to
5945 * all extents counts request so update the local variable with the
5946 * allocated count from the port.
5947 */
5948 if (emb == LPFC_SLI4_MBX_EMBED) {
5949 rsrc_ext = &mbox->u.mqe.un.alloc_rsrc_extents;
5950 shdr = &rsrc_ext->header.cfg_shdr;
5951 *extnt_cnt = bf_get(lpfc_mbx_rsrc_cnt, &rsrc_ext->u.rsp);
5952 } else {
5953 virtaddr = mbox->sge_array->addr[0];
5954 n_rsrc = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
5955 shdr = &n_rsrc->cfg_shdr;
5956 *extnt_cnt = bf_get(lpfc_mbx_rsrc_cnt, n_rsrc);
5957 }
5958
5959 if (bf_get(lpfc_mbox_hdr_status, &shdr->response)) {
5960 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
5961 "2984 Failed to read allocated resources "
5962 "for type %d - Status 0x%x Add'l Status 0x%x.\n",
5963 type,
5964 bf_get(lpfc_mbox_hdr_status, &shdr->response),
5965 bf_get(lpfc_mbox_hdr_add_status, &shdr->response));
5966 rc = -EIO;
5967 goto err_exit;
5968 }
5969 err_exit:
5970 lpfc_sli4_mbox_cmd_free(phba, mbox);
5971 return rc;
5972}
5973
5974/**
James Smart8a9d2e82012-05-09 21:16:12 -04005975 * lpfc_sli4_repost_els_sgl_list - Repsot the els buffers sgl pages as block
5976 * @phba: pointer to lpfc hba data structure.
5977 *
5978 * This routine walks the list of els buffers that have been allocated and
5979 * repost them to the port by using SGL block post. This is needed after a
5980 * pci_function_reset/warm_start or start. It attempts to construct blocks
5981 * of els buffer sgls which contains contiguous xris and uses the non-embedded
5982 * SGL block post mailbox commands to post them to the port. For single els
5983 * buffer sgl with non-contiguous xri, if any, it shall use embedded SGL post
5984 * mailbox command for posting.
5985 *
5986 * Returns: 0 = success, non-zero failure.
5987 **/
5988static int
5989lpfc_sli4_repost_els_sgl_list(struct lpfc_hba *phba)
5990{
5991 struct lpfc_sglq *sglq_entry = NULL;
5992 struct lpfc_sglq *sglq_entry_next = NULL;
5993 struct lpfc_sglq *sglq_entry_first = NULL;
5994 int status, post_cnt = 0, num_posted = 0, block_cnt = 0;
5995 int last_xritag = NO_XRI;
5996 LIST_HEAD(prep_sgl_list);
5997 LIST_HEAD(blck_sgl_list);
5998 LIST_HEAD(allc_sgl_list);
5999 LIST_HEAD(post_sgl_list);
6000 LIST_HEAD(free_sgl_list);
6001
James Smart38c20672013-03-01 16:37:44 -05006002 spin_lock_irq(&phba->hbalock);
James Smart8a9d2e82012-05-09 21:16:12 -04006003 list_splice_init(&phba->sli4_hba.lpfc_sgl_list, &allc_sgl_list);
James Smart38c20672013-03-01 16:37:44 -05006004 spin_unlock_irq(&phba->hbalock);
James Smart8a9d2e82012-05-09 21:16:12 -04006005
6006 list_for_each_entry_safe(sglq_entry, sglq_entry_next,
6007 &allc_sgl_list, list) {
6008 list_del_init(&sglq_entry->list);
6009 block_cnt++;
6010 if ((last_xritag != NO_XRI) &&
6011 (sglq_entry->sli4_xritag != last_xritag + 1)) {
6012 /* a hole in xri block, form a sgl posting block */
6013 list_splice_init(&prep_sgl_list, &blck_sgl_list);
6014 post_cnt = block_cnt - 1;
6015 /* prepare list for next posting block */
6016 list_add_tail(&sglq_entry->list, &prep_sgl_list);
6017 block_cnt = 1;
6018 } else {
6019 /* prepare list for next posting block */
6020 list_add_tail(&sglq_entry->list, &prep_sgl_list);
6021 /* enough sgls for non-embed sgl mbox command */
6022 if (block_cnt == LPFC_NEMBED_MBOX_SGL_CNT) {
6023 list_splice_init(&prep_sgl_list,
6024 &blck_sgl_list);
6025 post_cnt = block_cnt;
6026 block_cnt = 0;
6027 }
6028 }
6029 num_posted++;
6030
6031 /* keep track of last sgl's xritag */
6032 last_xritag = sglq_entry->sli4_xritag;
6033
6034 /* end of repost sgl list condition for els buffers */
6035 if (num_posted == phba->sli4_hba.els_xri_cnt) {
6036 if (post_cnt == 0) {
6037 list_splice_init(&prep_sgl_list,
6038 &blck_sgl_list);
6039 post_cnt = block_cnt;
6040 } else if (block_cnt == 1) {
6041 status = lpfc_sli4_post_sgl(phba,
6042 sglq_entry->phys, 0,
6043 sglq_entry->sli4_xritag);
6044 if (!status) {
6045 /* successful, put sgl to posted list */
6046 list_add_tail(&sglq_entry->list,
6047 &post_sgl_list);
6048 } else {
6049 /* Failure, put sgl to free list */
6050 lpfc_printf_log(phba, KERN_WARNING,
6051 LOG_SLI,
6052 "3159 Failed to post els "
6053 "sgl, xritag:x%x\n",
6054 sglq_entry->sli4_xritag);
6055 list_add_tail(&sglq_entry->list,
6056 &free_sgl_list);
6057 spin_lock_irq(&phba->hbalock);
6058 phba->sli4_hba.els_xri_cnt--;
6059 spin_unlock_irq(&phba->hbalock);
6060 }
6061 }
6062 }
6063
6064 /* continue until a nembed page worth of sgls */
6065 if (post_cnt == 0)
6066 continue;
6067
6068 /* post the els buffer list sgls as a block */
6069 status = lpfc_sli4_post_els_sgl_list(phba, &blck_sgl_list,
6070 post_cnt);
6071
6072 if (!status) {
6073 /* success, put sgl list to posted sgl list */
6074 list_splice_init(&blck_sgl_list, &post_sgl_list);
6075 } else {
6076 /* Failure, put sgl list to free sgl list */
6077 sglq_entry_first = list_first_entry(&blck_sgl_list,
6078 struct lpfc_sglq,
6079 list);
6080 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
6081 "3160 Failed to post els sgl-list, "
6082 "xritag:x%x-x%x\n",
6083 sglq_entry_first->sli4_xritag,
6084 (sglq_entry_first->sli4_xritag +
6085 post_cnt - 1));
6086 list_splice_init(&blck_sgl_list, &free_sgl_list);
6087 spin_lock_irq(&phba->hbalock);
6088 phba->sli4_hba.els_xri_cnt -= post_cnt;
6089 spin_unlock_irq(&phba->hbalock);
6090 }
6091
6092 /* don't reset xirtag due to hole in xri block */
6093 if (block_cnt == 0)
6094 last_xritag = NO_XRI;
6095
6096 /* reset els sgl post count for next round of posting */
6097 post_cnt = 0;
6098 }
6099
6100 /* free the els sgls failed to post */
6101 lpfc_free_sgl_list(phba, &free_sgl_list);
6102
6103 /* push els sgls posted to the availble list */
6104 if (!list_empty(&post_sgl_list)) {
James Smart38c20672013-03-01 16:37:44 -05006105 spin_lock_irq(&phba->hbalock);
James Smart8a9d2e82012-05-09 21:16:12 -04006106 list_splice_init(&post_sgl_list,
6107 &phba->sli4_hba.lpfc_sgl_list);
James Smart38c20672013-03-01 16:37:44 -05006108 spin_unlock_irq(&phba->hbalock);
James Smart8a9d2e82012-05-09 21:16:12 -04006109 } else {
6110 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6111 "3161 Failure to post els sgl to port.\n");
6112 return -EIO;
6113 }
6114 return 0;
6115}
6116
6117/**
James Smartda0436e2009-05-22 14:51:39 -04006118 * lpfc_sli4_hba_setup - SLI4 device intialization PCI function
6119 * @phba: Pointer to HBA context object.
6120 *
6121 * This function is the main SLI4 device intialization PCI function. This
6122 * function is called by the HBA intialization code, HBA reset code and
6123 * HBA error attention handler code. Caller is not required to hold any
6124 * locks.
6125 **/
6126int
6127lpfc_sli4_hba_setup(struct lpfc_hba *phba)
6128{
6129 int rc;
6130 LPFC_MBOXQ_t *mboxq;
6131 struct lpfc_mqe *mqe;
6132 uint8_t *vpd;
6133 uint32_t vpd_size;
6134 uint32_t ftr_rsp = 0;
6135 struct Scsi_Host *shost = lpfc_shost_from_vport(phba->pport);
6136 struct lpfc_vport *vport = phba->pport;
6137 struct lpfc_dmabuf *mp;
6138
6139 /* Perform a PCI function reset to start from clean */
6140 rc = lpfc_pci_function_reset(phba);
6141 if (unlikely(rc))
6142 return -ENODEV;
6143
6144 /* Check the HBA Host Status Register for readyness */
6145 rc = lpfc_sli4_post_status_check(phba);
6146 if (unlikely(rc))
6147 return -ENODEV;
6148 else {
6149 spin_lock_irq(&phba->hbalock);
6150 phba->sli.sli_flag |= LPFC_SLI_ACTIVE;
6151 spin_unlock_irq(&phba->hbalock);
6152 }
6153
6154 /*
6155 * Allocate a single mailbox container for initializing the
6156 * port.
6157 */
6158 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6159 if (!mboxq)
6160 return -ENOMEM;
6161
James Smartda0436e2009-05-22 14:51:39 -04006162 /* Issue READ_REV to collect vpd and FW information. */
James Smart49198b32010-04-06 15:04:33 -04006163 vpd_size = SLI4_PAGE_SIZE;
James Smartda0436e2009-05-22 14:51:39 -04006164 vpd = kzalloc(vpd_size, GFP_KERNEL);
6165 if (!vpd) {
6166 rc = -ENOMEM;
6167 goto out_free_mbox;
6168 }
6169
6170 rc = lpfc_sli4_read_rev(phba, mboxq, vpd, &vpd_size);
James Smart76a95d72010-11-20 23:11:48 -05006171 if (unlikely(rc)) {
6172 kfree(vpd);
6173 goto out_free_mbox;
6174 }
James Smartda0436e2009-05-22 14:51:39 -04006175 mqe = &mboxq->u.mqe;
James Smartf1126682009-06-10 17:22:44 -04006176 phba->sli_rev = bf_get(lpfc_mbx_rd_rev_sli_lvl, &mqe->un.read_rev);
6177 if (bf_get(lpfc_mbx_rd_rev_fcoe, &mqe->un.read_rev))
James Smart76a95d72010-11-20 23:11:48 -05006178 phba->hba_flag |= HBA_FCOE_MODE;
6179 else
6180 phba->hba_flag &= ~HBA_FCOE_MODE;
James Smart45ed1192009-10-02 15:17:02 -04006181
6182 if (bf_get(lpfc_mbx_rd_rev_cee_ver, &mqe->un.read_rev) ==
6183 LPFC_DCBX_CEE_MODE)
6184 phba->hba_flag |= HBA_FIP_SUPPORT;
6185 else
6186 phba->hba_flag &= ~HBA_FIP_SUPPORT;
6187
James Smart4f2e66c2012-05-09 21:17:07 -04006188 phba->hba_flag &= ~HBA_FCP_IOQ_FLUSH;
6189
James Smartc31098c2011-04-16 11:03:33 -04006190 if (phba->sli_rev != LPFC_SLI_REV4) {
James Smartda0436e2009-05-22 14:51:39 -04006191 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6192 "0376 READ_REV Error. SLI Level %d "
6193 "FCoE enabled %d\n",
James Smart76a95d72010-11-20 23:11:48 -05006194 phba->sli_rev, phba->hba_flag & HBA_FCOE_MODE);
James Smartda0436e2009-05-22 14:51:39 -04006195 rc = -EIO;
James Smart76a95d72010-11-20 23:11:48 -05006196 kfree(vpd);
6197 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04006198 }
James Smartcd1c8302011-10-10 21:33:25 -04006199
6200 /*
James Smartff78d8f2011-12-13 13:21:35 -05006201 * Continue initialization with default values even if driver failed
6202 * to read FCoE param config regions, only read parameters if the
6203 * board is FCoE
6204 */
6205 if (phba->hba_flag & HBA_FCOE_MODE &&
6206 lpfc_sli4_read_fcoe_params(phba))
6207 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_INIT,
6208 "2570 Failed to read FCoE parameters\n");
6209
6210 /*
James Smartcd1c8302011-10-10 21:33:25 -04006211 * Retrieve sli4 device physical port name, failure of doing it
6212 * is considered as non-fatal.
6213 */
6214 rc = lpfc_sli4_retrieve_pport_name(phba);
6215 if (!rc)
6216 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
6217 "3080 Successful retrieving SLI4 device "
6218 "physical port name: %s.\n", phba->Port);
6219
James Smartda0436e2009-05-22 14:51:39 -04006220 /*
6221 * Evaluate the read rev and vpd data. Populate the driver
6222 * state with the results. If this routine fails, the failure
6223 * is not fatal as the driver will use generic values.
6224 */
6225 rc = lpfc_parse_vpd(phba, vpd, vpd_size);
6226 if (unlikely(!rc)) {
6227 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6228 "0377 Error %d parsing vpd. "
6229 "Using defaults.\n", rc);
6230 rc = 0;
6231 }
James Smart76a95d72010-11-20 23:11:48 -05006232 kfree(vpd);
James Smartda0436e2009-05-22 14:51:39 -04006233
James Smartf1126682009-06-10 17:22:44 -04006234 /* Save information as VPD data */
6235 phba->vpd.rev.biuRev = mqe->un.read_rev.first_hw_rev;
6236 phba->vpd.rev.smRev = mqe->un.read_rev.second_hw_rev;
6237 phba->vpd.rev.endecRev = mqe->un.read_rev.third_hw_rev;
6238 phba->vpd.rev.fcphHigh = bf_get(lpfc_mbx_rd_rev_fcph_high,
6239 &mqe->un.read_rev);
6240 phba->vpd.rev.fcphLow = bf_get(lpfc_mbx_rd_rev_fcph_low,
6241 &mqe->un.read_rev);
6242 phba->vpd.rev.feaLevelHigh = bf_get(lpfc_mbx_rd_rev_ftr_lvl_high,
6243 &mqe->un.read_rev);
6244 phba->vpd.rev.feaLevelLow = bf_get(lpfc_mbx_rd_rev_ftr_lvl_low,
6245 &mqe->un.read_rev);
6246 phba->vpd.rev.sli1FwRev = mqe->un.read_rev.fw_id_rev;
6247 memcpy(phba->vpd.rev.sli1FwName, mqe->un.read_rev.fw_name, 16);
6248 phba->vpd.rev.sli2FwRev = mqe->un.read_rev.ulp_fw_id_rev;
6249 memcpy(phba->vpd.rev.sli2FwName, mqe->un.read_rev.ulp_fw_name, 16);
6250 phba->vpd.rev.opFwRev = mqe->un.read_rev.fw_id_rev;
6251 memcpy(phba->vpd.rev.opFwName, mqe->un.read_rev.fw_name, 16);
6252 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
6253 "(%d):0380 READ_REV Status x%x "
6254 "fw_rev:%s fcphHi:%x fcphLo:%x flHi:%x flLo:%x\n",
6255 mboxq->vport ? mboxq->vport->vpi : 0,
6256 bf_get(lpfc_mqe_status, mqe),
6257 phba->vpd.rev.opFwName,
6258 phba->vpd.rev.fcphHigh, phba->vpd.rev.fcphLow,
6259 phba->vpd.rev.feaLevelHigh, phba->vpd.rev.feaLevelLow);
James Smartda0436e2009-05-22 14:51:39 -04006260
6261 /*
6262 * Discover the port's supported feature set and match it against the
6263 * hosts requests.
6264 */
6265 lpfc_request_features(phba, mboxq);
6266 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6267 if (unlikely(rc)) {
6268 rc = -EIO;
James Smart76a95d72010-11-20 23:11:48 -05006269 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04006270 }
6271
6272 /*
6273 * The port must support FCP initiator mode as this is the
6274 * only mode running in the host.
6275 */
6276 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_fcpi, &mqe->un.req_ftrs))) {
6277 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
6278 "0378 No support for fcpi mode.\n");
6279 ftr_rsp++;
6280 }
James Smartfedd3b72011-02-16 12:39:24 -05006281 if (bf_get(lpfc_mbx_rq_ftr_rsp_perfh, &mqe->un.req_ftrs))
6282 phba->sli3_options |= LPFC_SLI4_PERFH_ENABLED;
6283 else
6284 phba->sli3_options &= ~LPFC_SLI4_PERFH_ENABLED;
James Smartda0436e2009-05-22 14:51:39 -04006285 /*
6286 * If the port cannot support the host's requested features
6287 * then turn off the global config parameters to disable the
6288 * feature in the driver. This is not a fatal error.
6289 */
James Smartbf086112011-08-21 21:48:13 -04006290 phba->sli3_options &= ~LPFC_SLI3_BG_ENABLED;
6291 if (phba->cfg_enable_bg) {
6292 if (bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs))
6293 phba->sli3_options |= LPFC_SLI3_BG_ENABLED;
6294 else
6295 ftr_rsp++;
6296 }
James Smartda0436e2009-05-22 14:51:39 -04006297
6298 if (phba->max_vpi && phba->cfg_enable_npiv &&
6299 !(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
6300 ftr_rsp++;
6301
6302 if (ftr_rsp) {
6303 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
6304 "0379 Feature Mismatch Data: x%08x %08x "
6305 "x%x x%x x%x\n", mqe->un.req_ftrs.word2,
6306 mqe->un.req_ftrs.word3, phba->cfg_enable_bg,
6307 phba->cfg_enable_npiv, phba->max_vpi);
6308 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs)))
6309 phba->cfg_enable_bg = 0;
6310 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
6311 phba->cfg_enable_npiv = 0;
6312 }
6313
6314 /* These SLI3 features are assumed in SLI4 */
6315 spin_lock_irq(&phba->hbalock);
6316 phba->sli3_options |= (LPFC_SLI3_NPIV_ENABLED | LPFC_SLI3_HBQ_ENABLED);
6317 spin_unlock_irq(&phba->hbalock);
6318
James Smart6d368e52011-05-24 11:44:12 -04006319 /*
6320 * Allocate all resources (xri,rpi,vpi,vfi) now. Subsequent
6321 * calls depends on these resources to complete port setup.
6322 */
6323 rc = lpfc_sli4_alloc_resource_identifiers(phba);
6324 if (rc) {
6325 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6326 "2920 Failed to alloc Resource IDs "
6327 "rc = x%x\n", rc);
6328 goto out_free_mbox;
6329 }
6330
James Smartda0436e2009-05-22 14:51:39 -04006331 /* Read the port's service parameters. */
James Smart9f1177a2010-02-26 14:12:57 -05006332 rc = lpfc_read_sparam(phba, mboxq, vport->vpi);
6333 if (rc) {
6334 phba->link_state = LPFC_HBA_ERROR;
6335 rc = -ENOMEM;
James Smart76a95d72010-11-20 23:11:48 -05006336 goto out_free_mbox;
James Smart9f1177a2010-02-26 14:12:57 -05006337 }
6338
James Smartda0436e2009-05-22 14:51:39 -04006339 mboxq->vport = vport;
6340 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6341 mp = (struct lpfc_dmabuf *) mboxq->context1;
6342 if (rc == MBX_SUCCESS) {
6343 memcpy(&vport->fc_sparam, mp->virt, sizeof(struct serv_parm));
6344 rc = 0;
6345 }
6346
6347 /*
6348 * This memory was allocated by the lpfc_read_sparam routine. Release
6349 * it to the mbuf pool.
6350 */
6351 lpfc_mbuf_free(phba, mp->virt, mp->phys);
6352 kfree(mp);
6353 mboxq->context1 = NULL;
6354 if (unlikely(rc)) {
6355 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6356 "0382 READ_SPARAM command failed "
6357 "status %d, mbxStatus x%x\n",
6358 rc, bf_get(lpfc_mqe_status, mqe));
6359 phba->link_state = LPFC_HBA_ERROR;
6360 rc = -EIO;
James Smart76a95d72010-11-20 23:11:48 -05006361 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04006362 }
6363
James Smart05580562011-05-24 11:40:48 -04006364 lpfc_update_vport_wwn(vport);
James Smartda0436e2009-05-22 14:51:39 -04006365
6366 /* Update the fc_host data structures with new wwn. */
6367 fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
6368 fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
6369
James Smart8a9d2e82012-05-09 21:16:12 -04006370 /* update host els and scsi xri-sgl sizes and mappings */
6371 rc = lpfc_sli4_xri_sgl_update(phba);
6372 if (unlikely(rc)) {
6373 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6374 "1400 Failed to update xri-sgl size and "
6375 "mapping: %d\n", rc);
6376 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04006377 }
6378
James Smart8a9d2e82012-05-09 21:16:12 -04006379 /* register the els sgl pool to the port */
6380 rc = lpfc_sli4_repost_els_sgl_list(phba);
6381 if (unlikely(rc)) {
6382 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6383 "0582 Error %d during els sgl post "
6384 "operation\n", rc);
6385 rc = -ENODEV;
6386 goto out_free_mbox;
6387 }
6388
6389 /* register the allocated scsi sgl pool to the port */
James Smartda0436e2009-05-22 14:51:39 -04006390 rc = lpfc_sli4_repost_scsi_sgl_list(phba);
6391 if (unlikely(rc)) {
James Smart6d368e52011-05-24 11:44:12 -04006392 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smart6a9c52c2009-10-02 15:16:51 -04006393 "0383 Error %d during scsi sgl post "
6394 "operation\n", rc);
James Smartda0436e2009-05-22 14:51:39 -04006395 /* Some Scsi buffers were moved to the abort scsi list */
6396 /* A pci function reset will repost them */
6397 rc = -ENODEV;
James Smart76a95d72010-11-20 23:11:48 -05006398 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04006399 }
6400
6401 /* Post the rpi header region to the device. */
6402 rc = lpfc_sli4_post_all_rpi_hdrs(phba);
6403 if (unlikely(rc)) {
6404 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6405 "0393 Error %d during rpi post operation\n",
6406 rc);
6407 rc = -ENODEV;
James Smart76a95d72010-11-20 23:11:48 -05006408 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04006409 }
James Smart97f2ecf2012-03-01 22:35:23 -05006410 lpfc_sli4_node_prep(phba);
James Smartda0436e2009-05-22 14:51:39 -04006411
James Smart5350d872011-10-10 21:33:49 -04006412 /* Create all the SLI4 queues */
6413 rc = lpfc_sli4_queue_create(phba);
6414 if (rc) {
6415 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6416 "3089 Failed to allocate queues\n");
6417 rc = -ENODEV;
6418 goto out_stop_timers;
6419 }
James Smartda0436e2009-05-22 14:51:39 -04006420 /* Set up all the queues to the device */
6421 rc = lpfc_sli4_queue_setup(phba);
6422 if (unlikely(rc)) {
6423 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6424 "0381 Error %d during queue setup.\n ", rc);
James Smart5350d872011-10-10 21:33:49 -04006425 goto out_destroy_queue;
James Smartda0436e2009-05-22 14:51:39 -04006426 }
6427
6428 /* Arm the CQs and then EQs on device */
6429 lpfc_sli4_arm_cqeq_intr(phba);
6430
6431 /* Indicate device interrupt mode */
6432 phba->sli4_hba.intr_enable = 1;
6433
6434 /* Allow asynchronous mailbox command to go through */
6435 spin_lock_irq(&phba->hbalock);
6436 phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
6437 spin_unlock_irq(&phba->hbalock);
6438
6439 /* Post receive buffers to the device */
6440 lpfc_sli4_rb_setup(phba);
6441
James Smartfc2b9892010-02-26 14:15:29 -05006442 /* Reset HBA FCF states after HBA reset */
6443 phba->fcf.fcf_flag = 0;
6444 phba->fcf.current_rec.flag = 0;
6445
James Smartda0436e2009-05-22 14:51:39 -04006446 /* Start the ELS watchdog timer */
James Smart8fa38512009-07-19 10:01:03 -04006447 mod_timer(&vport->els_tmofunc,
6448 jiffies + HZ * (phba->fc_ratov * 2));
James Smartda0436e2009-05-22 14:51:39 -04006449
6450 /* Start heart beat timer */
6451 mod_timer(&phba->hb_tmofunc,
6452 jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
6453 phba->hb_outstanding = 0;
6454 phba->last_completion_time = jiffies;
6455
6456 /* Start error attention (ERATT) polling timer */
6457 mod_timer(&phba->eratt_poll, jiffies + HZ * LPFC_ERATT_POLL_INTERVAL);
6458
James Smart75baf692010-06-08 18:31:21 -04006459 /* Enable PCIe device Advanced Error Reporting (AER) if configured */
6460 if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
6461 rc = pci_enable_pcie_error_reporting(phba->pcidev);
6462 if (!rc) {
6463 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6464 "2829 This device supports "
6465 "Advanced Error Reporting (AER)\n");
6466 spin_lock_irq(&phba->hbalock);
6467 phba->hba_flag |= HBA_AER_ENABLED;
6468 spin_unlock_irq(&phba->hbalock);
6469 } else {
6470 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6471 "2830 This device does not support "
6472 "Advanced Error Reporting (AER)\n");
6473 phba->cfg_aer_support = 0;
6474 }
James Smart0a96e972011-07-22 18:37:28 -04006475 rc = 0;
James Smart75baf692010-06-08 18:31:21 -04006476 }
6477
James Smart76a95d72010-11-20 23:11:48 -05006478 if (!(phba->hba_flag & HBA_FCOE_MODE)) {
6479 /*
6480 * The FC Port needs to register FCFI (index 0)
6481 */
6482 lpfc_reg_fcfi(phba, mboxq);
6483 mboxq->vport = phba->pport;
6484 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
James Smart9589b0622011-04-16 11:03:17 -04006485 if (rc != MBX_SUCCESS)
James Smart76a95d72010-11-20 23:11:48 -05006486 goto out_unset_queue;
James Smart9589b0622011-04-16 11:03:17 -04006487 rc = 0;
6488 phba->fcf.fcfi = bf_get(lpfc_reg_fcfi_fcfi,
6489 &mboxq->u.mqe.un.reg_fcfi);
James Smart026abb82011-12-13 13:20:45 -05006490
6491 /* Check if the port is configured to be disabled */
6492 lpfc_sli_read_link_ste(phba);
James Smart76a95d72010-11-20 23:11:48 -05006493 }
James Smart026abb82011-12-13 13:20:45 -05006494
James Smartda0436e2009-05-22 14:51:39 -04006495 /*
6496 * The port is ready, set the host's link state to LINK_DOWN
6497 * in preparation for link interrupts.
6498 */
James Smartda0436e2009-05-22 14:51:39 -04006499 spin_lock_irq(&phba->hbalock);
6500 phba->link_state = LPFC_LINK_DOWN;
6501 spin_unlock_irq(&phba->hbalock);
James Smart026abb82011-12-13 13:20:45 -05006502 if (!(phba->hba_flag & HBA_FCOE_MODE) &&
6503 (phba->hba_flag & LINK_DISABLED)) {
6504 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_SLI,
6505 "3103 Adapter Link is disabled.\n");
6506 lpfc_down_link(phba, mboxq);
6507 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6508 if (rc != MBX_SUCCESS) {
6509 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_SLI,
6510 "3104 Adapter failed to issue "
6511 "DOWN_LINK mbox cmd, rc:x%x\n", rc);
6512 goto out_unset_queue;
6513 }
6514 } else if (phba->cfg_suppress_link_up == LPFC_INITIALIZE_LINK) {
James Smart1b511972011-12-13 13:23:09 -05006515 /* don't perform init_link on SLI4 FC port loopback test */
6516 if (!(phba->link_flag & LS_LOOPBACK_MODE)) {
6517 rc = phba->lpfc_hba_init_link(phba, MBX_NOWAIT);
6518 if (rc)
6519 goto out_unset_queue;
6520 }
James Smart5350d872011-10-10 21:33:49 -04006521 }
6522 mempool_free(mboxq, phba->mbox_mem_pool);
6523 return rc;
James Smart76a95d72010-11-20 23:11:48 -05006524out_unset_queue:
James Smartda0436e2009-05-22 14:51:39 -04006525 /* Unset all the queues set up in this routine when error out */
James Smart5350d872011-10-10 21:33:49 -04006526 lpfc_sli4_queue_unset(phba);
6527out_destroy_queue:
6528 lpfc_sli4_queue_destroy(phba);
James Smartda0436e2009-05-22 14:51:39 -04006529out_stop_timers:
James Smart5350d872011-10-10 21:33:49 -04006530 lpfc_stop_hba_timers(phba);
James Smartda0436e2009-05-22 14:51:39 -04006531out_free_mbox:
6532 mempool_free(mboxq, phba->mbox_mem_pool);
6533 return rc;
6534}
James Smarte59058c2008-08-24 21:49:00 -04006535
6536/**
James Smart3621a712009-04-06 18:47:14 -04006537 * lpfc_mbox_timeout - Timeout call back function for mbox timer
James Smarte59058c2008-08-24 21:49:00 -04006538 * @ptr: context object - pointer to hba structure.
dea31012005-04-17 16:05:31 -05006539 *
James Smarte59058c2008-08-24 21:49:00 -04006540 * This is the callback function for mailbox timer. The mailbox
6541 * timer is armed when a new mailbox command is issued and the timer
6542 * is deleted when the mailbox complete. The function is called by
6543 * the kernel timer code when a mailbox does not complete within
6544 * expected time. This function wakes up the worker thread to
6545 * process the mailbox timeout and returns. All the processing is
6546 * done by the worker thread function lpfc_mbox_timeout_handler.
6547 **/
dea31012005-04-17 16:05:31 -05006548void
6549lpfc_mbox_timeout(unsigned long ptr)
6550{
James Smart92d7f7b2007-06-17 19:56:38 -05006551 struct lpfc_hba *phba = (struct lpfc_hba *) ptr;
dea31012005-04-17 16:05:31 -05006552 unsigned long iflag;
James Smart2e0fef82007-06-17 19:56:36 -05006553 uint32_t tmo_posted;
dea31012005-04-17 16:05:31 -05006554
James Smart2e0fef82007-06-17 19:56:36 -05006555 spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
James Smart92d7f7b2007-06-17 19:56:38 -05006556 tmo_posted = phba->pport->work_port_events & WORKER_MBOX_TMO;
James Smart2e0fef82007-06-17 19:56:36 -05006557 if (!tmo_posted)
6558 phba->pport->work_port_events |= WORKER_MBOX_TMO;
6559 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
6560
James Smart5e9d9b82008-06-14 22:52:53 -04006561 if (!tmo_posted)
6562 lpfc_worker_wake_up(phba);
6563 return;
dea31012005-04-17 16:05:31 -05006564}
6565
James Smarte59058c2008-08-24 21:49:00 -04006566
6567/**
James Smart3621a712009-04-06 18:47:14 -04006568 * lpfc_mbox_timeout_handler - Worker thread function to handle mailbox timeout
James Smarte59058c2008-08-24 21:49:00 -04006569 * @phba: Pointer to HBA context object.
6570 *
6571 * This function is called from worker thread when a mailbox command times out.
6572 * The caller is not required to hold any locks. This function will reset the
6573 * HBA and recover all the pending commands.
6574 **/
dea31012005-04-17 16:05:31 -05006575void
6576lpfc_mbox_timeout_handler(struct lpfc_hba *phba)
6577{
James Smart2e0fef82007-06-17 19:56:36 -05006578 LPFC_MBOXQ_t *pmbox = phba->sli.mbox_active;
James Smart04c68492009-05-22 14:52:52 -04006579 MAILBOX_t *mb = &pmbox->u.mb;
James Smart1dcb58e2007-04-25 09:51:30 -04006580 struct lpfc_sli *psli = &phba->sli;
6581 struct lpfc_sli_ring *pring;
dea31012005-04-17 16:05:31 -05006582
James Smarta257bf92009-04-06 18:48:10 -04006583 /* Check the pmbox pointer first. There is a race condition
6584 * between the mbox timeout handler getting executed in the
6585 * worklist and the mailbox actually completing. When this
6586 * race condition occurs, the mbox_active will be NULL.
6587 */
6588 spin_lock_irq(&phba->hbalock);
6589 if (pmbox == NULL) {
6590 lpfc_printf_log(phba, KERN_WARNING,
6591 LOG_MBOX | LOG_SLI,
6592 "0353 Active Mailbox cleared - mailbox timeout "
6593 "exiting\n");
6594 spin_unlock_irq(&phba->hbalock);
6595 return;
6596 }
6597
dea31012005-04-17 16:05:31 -05006598 /* Mbox cmd <mbxCommand> timeout */
James Smarted957682007-06-17 19:56:37 -05006599 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04006600 "0310 Mailbox command x%x timeout Data: x%x x%x x%p\n",
James Smart92d7f7b2007-06-17 19:56:38 -05006601 mb->mbxCommand,
6602 phba->pport->port_state,
6603 phba->sli.sli_flag,
6604 phba->sli.mbox_active);
James Smarta257bf92009-04-06 18:48:10 -04006605 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05006606
James Smart1dcb58e2007-04-25 09:51:30 -04006607 /* Setting state unknown so lpfc_sli_abort_iocb_ring
6608 * would get IOCB_ERROR from lpfc_sli_issue_iocb, allowing
Lucas De Marchi25985ed2011-03-30 22:57:33 -03006609 * it to fail all outstanding SCSI IO.
James Smart1dcb58e2007-04-25 09:51:30 -04006610 */
James Smart2e0fef82007-06-17 19:56:36 -05006611 spin_lock_irq(&phba->pport->work_port_lock);
6612 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
6613 spin_unlock_irq(&phba->pport->work_port_lock);
6614 spin_lock_irq(&phba->hbalock);
6615 phba->link_state = LPFC_LINK_UNKNOWN;
James Smartf4b4c682009-05-22 14:53:12 -04006616 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05006617 spin_unlock_irq(&phba->hbalock);
James Smart1dcb58e2007-04-25 09:51:30 -04006618
6619 pring = &psli->ring[psli->fcp_ring];
6620 lpfc_sli_abort_iocb_ring(phba, pring);
6621
6622 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smart76bb24e2007-10-27 13:38:00 -04006623 "0345 Resetting board due to mailbox timeout\n");
James Smart3772a992009-05-22 14:50:54 -04006624
6625 /* Reset the HBA device */
6626 lpfc_reset_hba(phba);
dea31012005-04-17 16:05:31 -05006627}
6628
James Smarte59058c2008-08-24 21:49:00 -04006629/**
James Smart3772a992009-05-22 14:50:54 -04006630 * lpfc_sli_issue_mbox_s3 - Issue an SLI3 mailbox command to firmware
James Smarte59058c2008-08-24 21:49:00 -04006631 * @phba: Pointer to HBA context object.
6632 * @pmbox: Pointer to mailbox object.
6633 * @flag: Flag indicating how the mailbox need to be processed.
6634 *
6635 * This function is called by discovery code and HBA management code
James Smart3772a992009-05-22 14:50:54 -04006636 * to submit a mailbox command to firmware with SLI-3 interface spec. This
6637 * function gets the hbalock to protect the data structures.
James Smarte59058c2008-08-24 21:49:00 -04006638 * The mailbox command can be submitted in polling mode, in which case
6639 * this function will wait in a polling loop for the completion of the
6640 * mailbox.
6641 * If the mailbox is submitted in no_wait mode (not polling) the
6642 * function will submit the command and returns immediately without waiting
6643 * for the mailbox completion. The no_wait is supported only when HBA
6644 * is in SLI2/SLI3 mode - interrupts are enabled.
6645 * The SLI interface allows only one mailbox pending at a time. If the
6646 * mailbox is issued in polling mode and there is already a mailbox
6647 * pending, then the function will return an error. If the mailbox is issued
6648 * in NO_WAIT mode and there is a mailbox pending already, the function
6649 * will return MBX_BUSY after queuing the mailbox into mailbox queue.
6650 * The sli layer owns the mailbox object until the completion of mailbox
6651 * command if this function return MBX_BUSY or MBX_SUCCESS. For all other
6652 * return codes the caller owns the mailbox command after the return of
6653 * the function.
6654 **/
James Smart3772a992009-05-22 14:50:54 -04006655static int
6656lpfc_sli_issue_mbox_s3(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox,
6657 uint32_t flag)
dea31012005-04-17 16:05:31 -05006658{
Randy Dunlapbf07bde2013-02-22 10:23:54 -08006659 MAILBOX_t *mbx;
James Smart2e0fef82007-06-17 19:56:36 -05006660 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05006661 uint32_t status, evtctr;
James Smart9940b972011-03-11 16:06:12 -05006662 uint32_t ha_copy, hc_copy;
dea31012005-04-17 16:05:31 -05006663 int i;
James Smart09372822008-01-11 01:52:54 -05006664 unsigned long timeout;
dea31012005-04-17 16:05:31 -05006665 unsigned long drvr_flag = 0;
James Smart34b02dc2008-08-24 21:49:55 -04006666 uint32_t word0, ldata;
dea31012005-04-17 16:05:31 -05006667 void __iomem *to_slim;
James Smart58da1ff2008-04-07 10:15:56 -04006668 int processing_queue = 0;
6669
6670 spin_lock_irqsave(&phba->hbalock, drvr_flag);
6671 if (!pmbox) {
James Smart8568a4d2009-07-19 10:01:16 -04006672 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart58da1ff2008-04-07 10:15:56 -04006673 /* processing mbox queue from intr_handler */
James Smart3772a992009-05-22 14:50:54 -04006674 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
6675 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
6676 return MBX_SUCCESS;
6677 }
James Smart58da1ff2008-04-07 10:15:56 -04006678 processing_queue = 1;
James Smart58da1ff2008-04-07 10:15:56 -04006679 pmbox = lpfc_mbox_get(phba);
6680 if (!pmbox) {
6681 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
6682 return MBX_SUCCESS;
6683 }
6684 }
dea31012005-04-17 16:05:31 -05006685
James Smarted957682007-06-17 19:56:37 -05006686 if (pmbox->mbox_cmpl && pmbox->mbox_cmpl != lpfc_sli_def_mbox_cmpl &&
James Smart92d7f7b2007-06-17 19:56:38 -05006687 pmbox->mbox_cmpl != lpfc_sli_wake_mbox_wait) {
James Smarted957682007-06-17 19:56:37 -05006688 if(!pmbox->vport) {
James Smart58da1ff2008-04-07 10:15:56 -04006689 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
James Smarted957682007-06-17 19:56:37 -05006690 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -05006691 LOG_MBOX | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04006692 "1806 Mbox x%x failed. No vport\n",
James Smart3772a992009-05-22 14:50:54 -04006693 pmbox->u.mb.mbxCommand);
James Smarted957682007-06-17 19:56:37 -05006694 dump_stack();
James Smart58da1ff2008-04-07 10:15:56 -04006695 goto out_not_finished;
James Smarted957682007-06-17 19:56:37 -05006696 }
6697 }
6698
Linas Vepstas8d63f372007-02-14 14:28:36 -06006699 /* If the PCI channel is in offline state, do not post mbox. */
James Smart58da1ff2008-04-07 10:15:56 -04006700 if (unlikely(pci_channel_offline(phba->pcidev))) {
6701 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
6702 goto out_not_finished;
6703 }
Linas Vepstas8d63f372007-02-14 14:28:36 -06006704
James Smarta257bf92009-04-06 18:48:10 -04006705 /* If HBA has a deferred error attention, fail the iocb. */
6706 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
6707 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
6708 goto out_not_finished;
6709 }
6710
dea31012005-04-17 16:05:31 -05006711 psli = &phba->sli;
James Smart92d7f7b2007-06-17 19:56:38 -05006712
Randy Dunlapbf07bde2013-02-22 10:23:54 -08006713 mbx = &pmbox->u.mb;
dea31012005-04-17 16:05:31 -05006714 status = MBX_SUCCESS;
6715
James Smart2e0fef82007-06-17 19:56:36 -05006716 if (phba->link_state == LPFC_HBA_ERROR) {
6717 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05006718
6719 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04006720 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6721 "(%d):0311 Mailbox command x%x cannot "
6722 "issue Data: x%x x%x\n",
6723 pmbox->vport ? pmbox->vport->vpi : 0,
6724 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04006725 goto out_not_finished;
Jamie Wellnitz41415862006-02-28 19:25:27 -05006726 }
6727
Randy Dunlapbf07bde2013-02-22 10:23:54 -08006728 if (mbx->mbxCommand != MBX_KILL_BOARD && flag & MBX_NOWAIT) {
James Smart9940b972011-03-11 16:06:12 -05006729 if (lpfc_readl(phba->HCregaddr, &hc_copy) ||
6730 !(hc_copy & HC_MBINT_ENA)) {
6731 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
6732 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smart3772a992009-05-22 14:50:54 -04006733 "(%d):2528 Mailbox command x%x cannot "
6734 "issue Data: x%x x%x\n",
6735 pmbox->vport ? pmbox->vport->vpi : 0,
6736 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
James Smart9940b972011-03-11 16:06:12 -05006737 goto out_not_finished;
6738 }
James Smart92908312006-03-07 15:04:13 -05006739 }
6740
dea31012005-04-17 16:05:31 -05006741 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
6742 /* Polling for a mbox command when another one is already active
6743 * is not allowed in SLI. Also, the driver must have established
6744 * SLI2 mode to queue and process multiple mbox commands.
6745 */
6746
6747 if (flag & MBX_POLL) {
James Smart2e0fef82007-06-17 19:56:36 -05006748 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05006749
6750 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04006751 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6752 "(%d):2529 Mailbox command x%x "
6753 "cannot issue Data: x%x x%x\n",
6754 pmbox->vport ? pmbox->vport->vpi : 0,
6755 pmbox->u.mb.mbxCommand,
6756 psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04006757 goto out_not_finished;
dea31012005-04-17 16:05:31 -05006758 }
6759
James Smart3772a992009-05-22 14:50:54 -04006760 if (!(psli->sli_flag & LPFC_SLI_ACTIVE)) {
James Smart2e0fef82007-06-17 19:56:36 -05006761 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05006762 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04006763 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6764 "(%d):2530 Mailbox command x%x "
6765 "cannot issue Data: x%x x%x\n",
6766 pmbox->vport ? pmbox->vport->vpi : 0,
6767 pmbox->u.mb.mbxCommand,
6768 psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04006769 goto out_not_finished;
dea31012005-04-17 16:05:31 -05006770 }
6771
dea31012005-04-17 16:05:31 -05006772 /* Another mailbox command is still being processed, queue this
6773 * command to be processed later.
6774 */
6775 lpfc_mbox_put(phba, pmbox);
6776
6777 /* Mbox cmd issue - BUSY */
James Smarted957682007-06-17 19:56:37 -05006778 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04006779 "(%d):0308 Mbox cmd issue - BUSY Data: "
James Smart92d7f7b2007-06-17 19:56:38 -05006780 "x%x x%x x%x x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -05006781 pmbox->vport ? pmbox->vport->vpi : 0xffffff,
Randy Dunlapbf07bde2013-02-22 10:23:54 -08006782 mbx->mbxCommand, phba->pport->port_state,
James Smart92d7f7b2007-06-17 19:56:38 -05006783 psli->sli_flag, flag);
dea31012005-04-17 16:05:31 -05006784
6785 psli->slistat.mbox_busy++;
James Smart2e0fef82007-06-17 19:56:36 -05006786 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05006787
James Smart858c9f62007-06-17 19:56:39 -05006788 if (pmbox->vport) {
6789 lpfc_debugfs_disc_trc(pmbox->vport,
6790 LPFC_DISC_TRC_MBOX_VPORT,
6791 "MBOX Bsy vport: cmd:x%x mb:x%x x%x",
Randy Dunlapbf07bde2013-02-22 10:23:54 -08006792 (uint32_t)mbx->mbxCommand,
6793 mbx->un.varWords[0], mbx->un.varWords[1]);
James Smart858c9f62007-06-17 19:56:39 -05006794 }
6795 else {
6796 lpfc_debugfs_disc_trc(phba->pport,
6797 LPFC_DISC_TRC_MBOX,
6798 "MBOX Bsy: cmd:x%x mb:x%x x%x",
Randy Dunlapbf07bde2013-02-22 10:23:54 -08006799 (uint32_t)mbx->mbxCommand,
6800 mbx->un.varWords[0], mbx->un.varWords[1]);
James Smart858c9f62007-06-17 19:56:39 -05006801 }
6802
James Smart2e0fef82007-06-17 19:56:36 -05006803 return MBX_BUSY;
dea31012005-04-17 16:05:31 -05006804 }
6805
dea31012005-04-17 16:05:31 -05006806 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
6807
6808 /* If we are not polling, we MUST be in SLI2 mode */
6809 if (flag != MBX_POLL) {
James Smart3772a992009-05-22 14:50:54 -04006810 if (!(psli->sli_flag & LPFC_SLI_ACTIVE) &&
Randy Dunlapbf07bde2013-02-22 10:23:54 -08006811 (mbx->mbxCommand != MBX_KILL_BOARD)) {
dea31012005-04-17 16:05:31 -05006812 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05006813 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05006814 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04006815 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6816 "(%d):2531 Mailbox command x%x "
6817 "cannot issue Data: x%x x%x\n",
6818 pmbox->vport ? pmbox->vport->vpi : 0,
6819 pmbox->u.mb.mbxCommand,
6820 psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04006821 goto out_not_finished;
dea31012005-04-17 16:05:31 -05006822 }
6823 /* timeout active mbox command */
James Smarta309a6b2006-08-01 07:33:43 -04006824 mod_timer(&psli->mbox_tmo, (jiffies +
James Smarta183a152011-10-10 21:32:43 -04006825 (HZ * lpfc_mbox_tmo_val(phba, pmbox))));
dea31012005-04-17 16:05:31 -05006826 }
6827
6828 /* Mailbox cmd <cmd> issue */
James Smarted957682007-06-17 19:56:37 -05006829 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04006830 "(%d):0309 Mailbox cmd x%x issue Data: x%x x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05006831 "x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04006832 pmbox->vport ? pmbox->vport->vpi : 0,
Randy Dunlapbf07bde2013-02-22 10:23:54 -08006833 mbx->mbxCommand, phba->pport->port_state,
James Smart92d7f7b2007-06-17 19:56:38 -05006834 psli->sli_flag, flag);
dea31012005-04-17 16:05:31 -05006835
Randy Dunlapbf07bde2013-02-22 10:23:54 -08006836 if (mbx->mbxCommand != MBX_HEARTBEAT) {
James Smart858c9f62007-06-17 19:56:39 -05006837 if (pmbox->vport) {
6838 lpfc_debugfs_disc_trc(pmbox->vport,
6839 LPFC_DISC_TRC_MBOX_VPORT,
6840 "MBOX Send vport: cmd:x%x mb:x%x x%x",
Randy Dunlapbf07bde2013-02-22 10:23:54 -08006841 (uint32_t)mbx->mbxCommand,
6842 mbx->un.varWords[0], mbx->un.varWords[1]);
James Smart858c9f62007-06-17 19:56:39 -05006843 }
6844 else {
6845 lpfc_debugfs_disc_trc(phba->pport,
6846 LPFC_DISC_TRC_MBOX,
6847 "MBOX Send: cmd:x%x mb:x%x x%x",
Randy Dunlapbf07bde2013-02-22 10:23:54 -08006848 (uint32_t)mbx->mbxCommand,
6849 mbx->un.varWords[0], mbx->un.varWords[1]);
James Smart858c9f62007-06-17 19:56:39 -05006850 }
6851 }
6852
dea31012005-04-17 16:05:31 -05006853 psli->slistat.mbox_cmd++;
6854 evtctr = psli->slistat.mbox_event;
6855
6856 /* next set own bit for the adapter and copy over command word */
Randy Dunlapbf07bde2013-02-22 10:23:54 -08006857 mbx->mbxOwner = OWN_CHIP;
dea31012005-04-17 16:05:31 -05006858
James Smart3772a992009-05-22 14:50:54 -04006859 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
James Smart7a470272010-03-15 11:25:20 -04006860 /* Populate mbox extension offset word. */
6861 if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len) {
Randy Dunlapbf07bde2013-02-22 10:23:54 -08006862 *(((uint32_t *)mbx) + pmbox->mbox_offset_word)
James Smart7a470272010-03-15 11:25:20 -04006863 = (uint8_t *)phba->mbox_ext
6864 - (uint8_t *)phba->mbox;
6865 }
6866
6867 /* Copy the mailbox extension data */
6868 if (pmbox->in_ext_byte_len && pmbox->context2) {
6869 lpfc_sli_pcimem_bcopy(pmbox->context2,
6870 (uint8_t *)phba->mbox_ext,
6871 pmbox->in_ext_byte_len);
6872 }
6873 /* Copy command data to host SLIM area */
Randy Dunlapbf07bde2013-02-22 10:23:54 -08006874 lpfc_sli_pcimem_bcopy(mbx, phba->mbox, MAILBOX_CMD_SIZE);
dea31012005-04-17 16:05:31 -05006875 } else {
James Smart7a470272010-03-15 11:25:20 -04006876 /* Populate mbox extension offset word. */
6877 if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len)
Randy Dunlapbf07bde2013-02-22 10:23:54 -08006878 *(((uint32_t *)mbx) + pmbox->mbox_offset_word)
James Smart7a470272010-03-15 11:25:20 -04006879 = MAILBOX_HBA_EXT_OFFSET;
6880
6881 /* Copy the mailbox extension data */
6882 if (pmbox->in_ext_byte_len && pmbox->context2) {
6883 lpfc_memcpy_to_slim(phba->MBslimaddr +
6884 MAILBOX_HBA_EXT_OFFSET,
6885 pmbox->context2, pmbox->in_ext_byte_len);
6886
6887 }
Randy Dunlapbf07bde2013-02-22 10:23:54 -08006888 if (mbx->mbxCommand == MBX_CONFIG_PORT) {
dea31012005-04-17 16:05:31 -05006889 /* copy command data into host mbox for cmpl */
Randy Dunlapbf07bde2013-02-22 10:23:54 -08006890 lpfc_sli_pcimem_bcopy(mbx, phba->mbox, MAILBOX_CMD_SIZE);
dea31012005-04-17 16:05:31 -05006891 }
6892
6893 /* First copy mbox command data to HBA SLIM, skip past first
6894 word */
6895 to_slim = phba->MBslimaddr + sizeof (uint32_t);
Randy Dunlapbf07bde2013-02-22 10:23:54 -08006896 lpfc_memcpy_to_slim(to_slim, &mbx->un.varWords[0],
dea31012005-04-17 16:05:31 -05006897 MAILBOX_CMD_SIZE - sizeof (uint32_t));
6898
6899 /* Next copy over first word, with mbxOwner set */
Randy Dunlapbf07bde2013-02-22 10:23:54 -08006900 ldata = *((uint32_t *)mbx);
dea31012005-04-17 16:05:31 -05006901 to_slim = phba->MBslimaddr;
6902 writel(ldata, to_slim);
6903 readl(to_slim); /* flush */
6904
Randy Dunlapbf07bde2013-02-22 10:23:54 -08006905 if (mbx->mbxCommand == MBX_CONFIG_PORT) {
dea31012005-04-17 16:05:31 -05006906 /* switch over to host mailbox */
James Smart3772a992009-05-22 14:50:54 -04006907 psli->sli_flag |= LPFC_SLI_ACTIVE;
dea31012005-04-17 16:05:31 -05006908 }
6909 }
6910
6911 wmb();
dea31012005-04-17 16:05:31 -05006912
6913 switch (flag) {
6914 case MBX_NOWAIT:
James Smart09372822008-01-11 01:52:54 -05006915 /* Set up reference to mailbox command */
dea31012005-04-17 16:05:31 -05006916 psli->mbox_active = pmbox;
James Smart09372822008-01-11 01:52:54 -05006917 /* Interrupt board to do it */
6918 writel(CA_MBATT, phba->CAregaddr);
6919 readl(phba->CAregaddr); /* flush */
6920 /* Don't wait for it to finish, just return */
dea31012005-04-17 16:05:31 -05006921 break;
6922
6923 case MBX_POLL:
James Smart09372822008-01-11 01:52:54 -05006924 /* Set up null reference to mailbox command */
dea31012005-04-17 16:05:31 -05006925 psli->mbox_active = NULL;
James Smart09372822008-01-11 01:52:54 -05006926 /* Interrupt board to do it */
6927 writel(CA_MBATT, phba->CAregaddr);
6928 readl(phba->CAregaddr); /* flush */
6929
James Smart3772a992009-05-22 14:50:54 -04006930 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05006931 /* First read mbox status word */
James Smart34b02dc2008-08-24 21:49:55 -04006932 word0 = *((uint32_t *)phba->mbox);
dea31012005-04-17 16:05:31 -05006933 word0 = le32_to_cpu(word0);
6934 } else {
6935 /* First read mbox status word */
James Smart9940b972011-03-11 16:06:12 -05006936 if (lpfc_readl(phba->MBslimaddr, &word0)) {
6937 spin_unlock_irqrestore(&phba->hbalock,
6938 drvr_flag);
6939 goto out_not_finished;
6940 }
dea31012005-04-17 16:05:31 -05006941 }
6942
6943 /* Read the HBA Host Attention Register */
James Smart9940b972011-03-11 16:06:12 -05006944 if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
6945 spin_unlock_irqrestore(&phba->hbalock,
6946 drvr_flag);
6947 goto out_not_finished;
6948 }
James Smarta183a152011-10-10 21:32:43 -04006949 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, pmbox) *
6950 1000) + jiffies;
James Smart09372822008-01-11 01:52:54 -05006951 i = 0;
dea31012005-04-17 16:05:31 -05006952 /* Wait for command to complete */
Jamie Wellnitz41415862006-02-28 19:25:27 -05006953 while (((word0 & OWN_CHIP) == OWN_CHIP) ||
6954 (!(ha_copy & HA_MBATT) &&
James Smart2e0fef82007-06-17 19:56:36 -05006955 (phba->link_state > LPFC_WARM_START))) {
James Smart09372822008-01-11 01:52:54 -05006956 if (time_after(jiffies, timeout)) {
dea31012005-04-17 16:05:31 -05006957 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05006958 spin_unlock_irqrestore(&phba->hbalock,
dea31012005-04-17 16:05:31 -05006959 drvr_flag);
James Smart58da1ff2008-04-07 10:15:56 -04006960 goto out_not_finished;
dea31012005-04-17 16:05:31 -05006961 }
6962
6963 /* Check if we took a mbox interrupt while we were
6964 polling */
6965 if (((word0 & OWN_CHIP) != OWN_CHIP)
6966 && (evtctr != psli->slistat.mbox_event))
6967 break;
6968
James Smart09372822008-01-11 01:52:54 -05006969 if (i++ > 10) {
6970 spin_unlock_irqrestore(&phba->hbalock,
6971 drvr_flag);
6972 msleep(1);
6973 spin_lock_irqsave(&phba->hbalock, drvr_flag);
6974 }
dea31012005-04-17 16:05:31 -05006975
James Smart3772a992009-05-22 14:50:54 -04006976 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05006977 /* First copy command data */
James Smart34b02dc2008-08-24 21:49:55 -04006978 word0 = *((uint32_t *)phba->mbox);
dea31012005-04-17 16:05:31 -05006979 word0 = le32_to_cpu(word0);
Randy Dunlapbf07bde2013-02-22 10:23:54 -08006980 if (mbx->mbxCommand == MBX_CONFIG_PORT) {
dea31012005-04-17 16:05:31 -05006981 MAILBOX_t *slimmb;
James Smart34b02dc2008-08-24 21:49:55 -04006982 uint32_t slimword0;
dea31012005-04-17 16:05:31 -05006983 /* Check real SLIM for any errors */
6984 slimword0 = readl(phba->MBslimaddr);
6985 slimmb = (MAILBOX_t *) & slimword0;
6986 if (((slimword0 & OWN_CHIP) != OWN_CHIP)
6987 && slimmb->mbxStatus) {
6988 psli->sli_flag &=
James Smart3772a992009-05-22 14:50:54 -04006989 ~LPFC_SLI_ACTIVE;
dea31012005-04-17 16:05:31 -05006990 word0 = slimword0;
6991 }
6992 }
6993 } else {
6994 /* First copy command data */
6995 word0 = readl(phba->MBslimaddr);
6996 }
6997 /* Read the HBA Host Attention Register */
James Smart9940b972011-03-11 16:06:12 -05006998 if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
6999 spin_unlock_irqrestore(&phba->hbalock,
7000 drvr_flag);
7001 goto out_not_finished;
7002 }
dea31012005-04-17 16:05:31 -05007003 }
7004
James Smart3772a992009-05-22 14:50:54 -04007005 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05007006 /* copy results back to user */
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007007 lpfc_sli_pcimem_bcopy(phba->mbox, mbx, MAILBOX_CMD_SIZE);
James Smart7a470272010-03-15 11:25:20 -04007008 /* Copy the mailbox extension data */
7009 if (pmbox->out_ext_byte_len && pmbox->context2) {
7010 lpfc_sli_pcimem_bcopy(phba->mbox_ext,
7011 pmbox->context2,
7012 pmbox->out_ext_byte_len);
7013 }
dea31012005-04-17 16:05:31 -05007014 } else {
7015 /* First copy command data */
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007016 lpfc_memcpy_from_slim(mbx, phba->MBslimaddr,
dea31012005-04-17 16:05:31 -05007017 MAILBOX_CMD_SIZE);
James Smart7a470272010-03-15 11:25:20 -04007018 /* Copy the mailbox extension data */
7019 if (pmbox->out_ext_byte_len && pmbox->context2) {
7020 lpfc_memcpy_from_slim(pmbox->context2,
7021 phba->MBslimaddr +
7022 MAILBOX_HBA_EXT_OFFSET,
7023 pmbox->out_ext_byte_len);
dea31012005-04-17 16:05:31 -05007024 }
7025 }
7026
7027 writel(HA_MBATT, phba->HAregaddr);
7028 readl(phba->HAregaddr); /* flush */
7029
7030 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007031 status = mbx->mbxStatus;
dea31012005-04-17 16:05:31 -05007032 }
7033
James Smart2e0fef82007-06-17 19:56:36 -05007034 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7035 return status;
James Smart58da1ff2008-04-07 10:15:56 -04007036
7037out_not_finished:
7038 if (processing_queue) {
James Smartda0436e2009-05-22 14:51:39 -04007039 pmbox->u.mb.mbxStatus = MBX_NOT_FINISHED;
James Smart58da1ff2008-04-07 10:15:56 -04007040 lpfc_mbox_cmpl_put(phba, pmbox);
7041 }
7042 return MBX_NOT_FINISHED;
dea31012005-04-17 16:05:31 -05007043}
7044
James Smarte59058c2008-08-24 21:49:00 -04007045/**
James Smartf1126682009-06-10 17:22:44 -04007046 * lpfc_sli4_async_mbox_block - Block posting SLI4 asynchronous mailbox command
7047 * @phba: Pointer to HBA context object.
7048 *
7049 * The function blocks the posting of SLI4 asynchronous mailbox commands from
7050 * the driver internal pending mailbox queue. It will then try to wait out the
7051 * possible outstanding mailbox command before return.
7052 *
7053 * Returns:
7054 * 0 - the outstanding mailbox command completed; otherwise, the wait for
7055 * the outstanding mailbox command timed out.
7056 **/
7057static int
7058lpfc_sli4_async_mbox_block(struct lpfc_hba *phba)
7059{
7060 struct lpfc_sli *psli = &phba->sli;
James Smartf1126682009-06-10 17:22:44 -04007061 int rc = 0;
James Smarta183a152011-10-10 21:32:43 -04007062 unsigned long timeout = 0;
James Smartf1126682009-06-10 17:22:44 -04007063
7064 /* Mark the asynchronous mailbox command posting as blocked */
7065 spin_lock_irq(&phba->hbalock);
7066 psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
James Smartf1126682009-06-10 17:22:44 -04007067 /* Determine how long we might wait for the active mailbox
7068 * command to be gracefully completed by firmware.
7069 */
James Smarta183a152011-10-10 21:32:43 -04007070 if (phba->sli.mbox_active)
7071 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
7072 phba->sli.mbox_active) *
7073 1000) + jiffies;
7074 spin_unlock_irq(&phba->hbalock);
7075
James Smartf1126682009-06-10 17:22:44 -04007076 /* Wait for the outstnading mailbox command to complete */
7077 while (phba->sli.mbox_active) {
7078 /* Check active mailbox complete status every 2ms */
7079 msleep(2);
7080 if (time_after(jiffies, timeout)) {
7081 /* Timeout, marked the outstanding cmd not complete */
7082 rc = 1;
7083 break;
7084 }
7085 }
7086
7087 /* Can not cleanly block async mailbox command, fails it */
7088 if (rc) {
7089 spin_lock_irq(&phba->hbalock);
7090 psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
7091 spin_unlock_irq(&phba->hbalock);
7092 }
7093 return rc;
7094}
7095
7096/**
7097 * lpfc_sli4_async_mbox_unblock - Block posting SLI4 async mailbox command
7098 * @phba: Pointer to HBA context object.
7099 *
7100 * The function unblocks and resume posting of SLI4 asynchronous mailbox
7101 * commands from the driver internal pending mailbox queue. It makes sure
7102 * that there is no outstanding mailbox command before resuming posting
7103 * asynchronous mailbox commands. If, for any reason, there is outstanding
7104 * mailbox command, it will try to wait it out before resuming asynchronous
7105 * mailbox command posting.
7106 **/
7107static void
7108lpfc_sli4_async_mbox_unblock(struct lpfc_hba *phba)
7109{
7110 struct lpfc_sli *psli = &phba->sli;
7111
7112 spin_lock_irq(&phba->hbalock);
7113 if (!(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
7114 /* Asynchronous mailbox posting is not blocked, do nothing */
7115 spin_unlock_irq(&phba->hbalock);
7116 return;
7117 }
7118
7119 /* Outstanding synchronous mailbox command is guaranteed to be done,
7120 * successful or timeout, after timing-out the outstanding mailbox
7121 * command shall always be removed, so just unblock posting async
7122 * mailbox command and resume
7123 */
7124 psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
7125 spin_unlock_irq(&phba->hbalock);
7126
7127 /* wake up worker thread to post asynchronlous mailbox command */
7128 lpfc_worker_wake_up(phba);
7129}
7130
7131/**
James Smart2d843ed2012-09-29 11:29:06 -04007132 * lpfc_sli4_wait_bmbx_ready - Wait for bootstrap mailbox register ready
7133 * @phba: Pointer to HBA context object.
7134 * @mboxq: Pointer to mailbox object.
7135 *
7136 * The function waits for the bootstrap mailbox register ready bit from
7137 * port for twice the regular mailbox command timeout value.
7138 *
7139 * 0 - no timeout on waiting for bootstrap mailbox register ready.
7140 * MBXERR_ERROR - wait for bootstrap mailbox register timed out.
7141 **/
7142static int
7143lpfc_sli4_wait_bmbx_ready(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
7144{
7145 uint32_t db_ready;
7146 unsigned long timeout;
7147 struct lpfc_register bmbx_reg;
7148
7149 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mboxq)
7150 * 1000) + jiffies;
7151
7152 do {
7153 bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
7154 db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
7155 if (!db_ready)
7156 msleep(2);
7157
7158 if (time_after(jiffies, timeout))
7159 return MBXERR_ERROR;
7160 } while (!db_ready);
7161
7162 return 0;
7163}
7164
7165/**
James Smartda0436e2009-05-22 14:51:39 -04007166 * lpfc_sli4_post_sync_mbox - Post an SLI4 mailbox to the bootstrap mailbox
7167 * @phba: Pointer to HBA context object.
7168 * @mboxq: Pointer to mailbox object.
7169 *
7170 * The function posts a mailbox to the port. The mailbox is expected
7171 * to be comletely filled in and ready for the port to operate on it.
7172 * This routine executes a synchronous completion operation on the
7173 * mailbox by polling for its completion.
7174 *
7175 * The caller must not be holding any locks when calling this routine.
7176 *
7177 * Returns:
7178 * MBX_SUCCESS - mailbox posted successfully
7179 * Any of the MBX error values.
7180 **/
7181static int
7182lpfc_sli4_post_sync_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
7183{
7184 int rc = MBX_SUCCESS;
7185 unsigned long iflag;
James Smartda0436e2009-05-22 14:51:39 -04007186 uint32_t mcqe_status;
7187 uint32_t mbx_cmnd;
James Smartda0436e2009-05-22 14:51:39 -04007188 struct lpfc_sli *psli = &phba->sli;
7189 struct lpfc_mqe *mb = &mboxq->u.mqe;
7190 struct lpfc_bmbx_create *mbox_rgn;
7191 struct dma_address *dma_address;
James Smartda0436e2009-05-22 14:51:39 -04007192
7193 /*
7194 * Only one mailbox can be active to the bootstrap mailbox region
7195 * at a time and there is no queueing provided.
7196 */
7197 spin_lock_irqsave(&phba->hbalock, iflag);
7198 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
7199 spin_unlock_irqrestore(&phba->hbalock, iflag);
7200 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04007201 "(%d):2532 Mailbox command x%x (x%x/x%x) "
James Smartda0436e2009-05-22 14:51:39 -04007202 "cannot issue Data: x%x x%x\n",
7203 mboxq->vport ? mboxq->vport->vpi : 0,
7204 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04007205 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7206 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04007207 psli->sli_flag, MBX_POLL);
7208 return MBXERR_ERROR;
7209 }
7210 /* The server grabs the token and owns it until release */
7211 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
7212 phba->sli.mbox_active = mboxq;
7213 spin_unlock_irqrestore(&phba->hbalock, iflag);
7214
James Smart2d843ed2012-09-29 11:29:06 -04007215 /* wait for bootstrap mbox register for readyness */
7216 rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
7217 if (rc)
7218 goto exit;
7219
James Smartda0436e2009-05-22 14:51:39 -04007220 /*
7221 * Initialize the bootstrap memory region to avoid stale data areas
7222 * in the mailbox post. Then copy the caller's mailbox contents to
7223 * the bmbx mailbox region.
7224 */
7225 mbx_cmnd = bf_get(lpfc_mqe_command, mb);
7226 memset(phba->sli4_hba.bmbx.avirt, 0, sizeof(struct lpfc_bmbx_create));
7227 lpfc_sli_pcimem_bcopy(mb, phba->sli4_hba.bmbx.avirt,
7228 sizeof(struct lpfc_mqe));
7229
7230 /* Post the high mailbox dma address to the port and wait for ready. */
7231 dma_address = &phba->sli4_hba.bmbx.dma_address;
7232 writel(dma_address->addr_hi, phba->sli4_hba.BMBXregaddr);
7233
James Smart2d843ed2012-09-29 11:29:06 -04007234 /* wait for bootstrap mbox register for hi-address write done */
7235 rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
7236 if (rc)
7237 goto exit;
James Smartda0436e2009-05-22 14:51:39 -04007238
7239 /* Post the low mailbox dma address to the port. */
7240 writel(dma_address->addr_lo, phba->sli4_hba.BMBXregaddr);
James Smartda0436e2009-05-22 14:51:39 -04007241
James Smart2d843ed2012-09-29 11:29:06 -04007242 /* wait for bootstrap mbox register for low address write done */
7243 rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
7244 if (rc)
7245 goto exit;
James Smartda0436e2009-05-22 14:51:39 -04007246
7247 /*
7248 * Read the CQ to ensure the mailbox has completed.
7249 * If so, update the mailbox status so that the upper layers
7250 * can complete the request normally.
7251 */
7252 lpfc_sli_pcimem_bcopy(phba->sli4_hba.bmbx.avirt, mb,
7253 sizeof(struct lpfc_mqe));
7254 mbox_rgn = (struct lpfc_bmbx_create *) phba->sli4_hba.bmbx.avirt;
7255 lpfc_sli_pcimem_bcopy(&mbox_rgn->mcqe, &mboxq->mcqe,
7256 sizeof(struct lpfc_mcqe));
7257 mcqe_status = bf_get(lpfc_mcqe_status, &mbox_rgn->mcqe);
James Smart05580562011-05-24 11:40:48 -04007258 /*
7259 * When the CQE status indicates a failure and the mailbox status
7260 * indicates success then copy the CQE status into the mailbox status
7261 * (and prefix it with x4000).
7262 */
James Smartda0436e2009-05-22 14:51:39 -04007263 if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
James Smart05580562011-05-24 11:40:48 -04007264 if (bf_get(lpfc_mqe_status, mb) == MBX_SUCCESS)
7265 bf_set(lpfc_mqe_status, mb,
7266 (LPFC_MBX_ERROR_RANGE | mcqe_status));
James Smartda0436e2009-05-22 14:51:39 -04007267 rc = MBXERR_ERROR;
James Smartd7c47992010-06-08 18:31:54 -04007268 } else
7269 lpfc_sli4_swap_str(phba, mboxq);
James Smartda0436e2009-05-22 14:51:39 -04007270
7271 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04007272 "(%d):0356 Mailbox cmd x%x (x%x/x%x) Status x%x "
James Smartda0436e2009-05-22 14:51:39 -04007273 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x"
7274 " x%x x%x CQ: x%x x%x x%x x%x\n",
James Smarta183a152011-10-10 21:32:43 -04007275 mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
7276 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7277 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04007278 bf_get(lpfc_mqe_status, mb),
7279 mb->un.mb_words[0], mb->un.mb_words[1],
7280 mb->un.mb_words[2], mb->un.mb_words[3],
7281 mb->un.mb_words[4], mb->un.mb_words[5],
7282 mb->un.mb_words[6], mb->un.mb_words[7],
7283 mb->un.mb_words[8], mb->un.mb_words[9],
7284 mb->un.mb_words[10], mb->un.mb_words[11],
7285 mb->un.mb_words[12], mboxq->mcqe.word0,
7286 mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
7287 mboxq->mcqe.trailer);
7288exit:
7289 /* We are holding the token, no needed for lock when release */
7290 spin_lock_irqsave(&phba->hbalock, iflag);
7291 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
7292 phba->sli.mbox_active = NULL;
7293 spin_unlock_irqrestore(&phba->hbalock, iflag);
7294 return rc;
7295}
7296
7297/**
7298 * lpfc_sli_issue_mbox_s4 - Issue an SLI4 mailbox command to firmware
7299 * @phba: Pointer to HBA context object.
7300 * @pmbox: Pointer to mailbox object.
7301 * @flag: Flag indicating how the mailbox need to be processed.
7302 *
7303 * This function is called by discovery code and HBA management code to submit
7304 * a mailbox command to firmware with SLI-4 interface spec.
7305 *
7306 * Return codes the caller owns the mailbox command after the return of the
7307 * function.
7308 **/
7309static int
7310lpfc_sli_issue_mbox_s4(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
7311 uint32_t flag)
7312{
7313 struct lpfc_sli *psli = &phba->sli;
7314 unsigned long iflags;
7315 int rc;
7316
James Smartb76f2dc2011-07-22 18:37:42 -04007317 /* dump from issue mailbox command if setup */
7318 lpfc_idiag_mbxacc_dump_issue_mbox(phba, &mboxq->u.mb);
7319
James Smart8fa38512009-07-19 10:01:03 -04007320 rc = lpfc_mbox_dev_check(phba);
7321 if (unlikely(rc)) {
7322 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04007323 "(%d):2544 Mailbox command x%x (x%x/x%x) "
James Smart8fa38512009-07-19 10:01:03 -04007324 "cannot issue Data: x%x x%x\n",
7325 mboxq->vport ? mboxq->vport->vpi : 0,
7326 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04007327 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7328 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smart8fa38512009-07-19 10:01:03 -04007329 psli->sli_flag, flag);
7330 goto out_not_finished;
7331 }
7332
James Smartda0436e2009-05-22 14:51:39 -04007333 /* Detect polling mode and jump to a handler */
7334 if (!phba->sli4_hba.intr_enable) {
7335 if (flag == MBX_POLL)
7336 rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
7337 else
7338 rc = -EIO;
7339 if (rc != MBX_SUCCESS)
James Smart05580562011-05-24 11:40:48 -04007340 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
James Smartda0436e2009-05-22 14:51:39 -04007341 "(%d):2541 Mailbox command x%x "
James Smartcc459f12012-05-09 21:18:30 -04007342 "(x%x/x%x) failure: "
7343 "mqe_sta: x%x mcqe_sta: x%x/x%x "
7344 "Data: x%x x%x\n,",
James Smartda0436e2009-05-22 14:51:39 -04007345 mboxq->vport ? mboxq->vport->vpi : 0,
7346 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04007347 lpfc_sli_config_mbox_subsys_get(phba,
7348 mboxq),
7349 lpfc_sli_config_mbox_opcode_get(phba,
7350 mboxq),
James Smartcc459f12012-05-09 21:18:30 -04007351 bf_get(lpfc_mqe_status, &mboxq->u.mqe),
7352 bf_get(lpfc_mcqe_status, &mboxq->mcqe),
7353 bf_get(lpfc_mcqe_ext_status,
7354 &mboxq->mcqe),
James Smartda0436e2009-05-22 14:51:39 -04007355 psli->sli_flag, flag);
7356 return rc;
7357 } else if (flag == MBX_POLL) {
James Smartf1126682009-06-10 17:22:44 -04007358 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
7359 "(%d):2542 Try to issue mailbox command "
James Smarta183a152011-10-10 21:32:43 -04007360 "x%x (x%x/x%x) synchronously ahead of async"
James Smartf1126682009-06-10 17:22:44 -04007361 "mailbox command queue: x%x x%x\n",
James Smartda0436e2009-05-22 14:51:39 -04007362 mboxq->vport ? mboxq->vport->vpi : 0,
7363 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04007364 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7365 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04007366 psli->sli_flag, flag);
James Smartf1126682009-06-10 17:22:44 -04007367 /* Try to block the asynchronous mailbox posting */
7368 rc = lpfc_sli4_async_mbox_block(phba);
7369 if (!rc) {
7370 /* Successfully blocked, now issue sync mbox cmd */
7371 rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
7372 if (rc != MBX_SUCCESS)
James Smartcc459f12012-05-09 21:18:30 -04007373 lpfc_printf_log(phba, KERN_WARNING,
James Smarta183a152011-10-10 21:32:43 -04007374 LOG_MBOX | LOG_SLI,
James Smartcc459f12012-05-09 21:18:30 -04007375 "(%d):2597 Sync Mailbox command "
7376 "x%x (x%x/x%x) failure: "
7377 "mqe_sta: x%x mcqe_sta: x%x/x%x "
7378 "Data: x%x x%x\n,",
7379 mboxq->vport ? mboxq->vport->vpi : 0,
James Smarta183a152011-10-10 21:32:43 -04007380 mboxq->u.mb.mbxCommand,
7381 lpfc_sli_config_mbox_subsys_get(phba,
7382 mboxq),
7383 lpfc_sli_config_mbox_opcode_get(phba,
7384 mboxq),
James Smartcc459f12012-05-09 21:18:30 -04007385 bf_get(lpfc_mqe_status, &mboxq->u.mqe),
7386 bf_get(lpfc_mcqe_status, &mboxq->mcqe),
7387 bf_get(lpfc_mcqe_ext_status,
7388 &mboxq->mcqe),
James Smarta183a152011-10-10 21:32:43 -04007389 psli->sli_flag, flag);
James Smartf1126682009-06-10 17:22:44 -04007390 /* Unblock the async mailbox posting afterward */
7391 lpfc_sli4_async_mbox_unblock(phba);
7392 }
7393 return rc;
James Smartda0436e2009-05-22 14:51:39 -04007394 }
7395
7396 /* Now, interrupt mode asynchrous mailbox command */
7397 rc = lpfc_mbox_cmd_check(phba, mboxq);
7398 if (rc) {
7399 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04007400 "(%d):2543 Mailbox command x%x (x%x/x%x) "
James Smartda0436e2009-05-22 14:51:39 -04007401 "cannot issue Data: x%x x%x\n",
7402 mboxq->vport ? mboxq->vport->vpi : 0,
7403 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04007404 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7405 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04007406 psli->sli_flag, flag);
7407 goto out_not_finished;
7408 }
James Smartda0436e2009-05-22 14:51:39 -04007409
7410 /* Put the mailbox command to the driver internal FIFO */
7411 psli->slistat.mbox_busy++;
7412 spin_lock_irqsave(&phba->hbalock, iflags);
7413 lpfc_mbox_put(phba, mboxq);
7414 spin_unlock_irqrestore(&phba->hbalock, iflags);
7415 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
7416 "(%d):0354 Mbox cmd issue - Enqueue Data: "
James Smarta183a152011-10-10 21:32:43 -04007417 "x%x (x%x/x%x) x%x x%x x%x\n",
James Smartda0436e2009-05-22 14:51:39 -04007418 mboxq->vport ? mboxq->vport->vpi : 0xffffff,
7419 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
James Smarta183a152011-10-10 21:32:43 -04007420 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7421 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04007422 phba->pport->port_state,
7423 psli->sli_flag, MBX_NOWAIT);
7424 /* Wake up worker thread to transport mailbox command from head */
7425 lpfc_worker_wake_up(phba);
7426
7427 return MBX_BUSY;
7428
7429out_not_finished:
7430 return MBX_NOT_FINISHED;
7431}
7432
7433/**
7434 * lpfc_sli4_post_async_mbox - Post an SLI4 mailbox command to device
7435 * @phba: Pointer to HBA context object.
7436 *
7437 * This function is called by worker thread to send a mailbox command to
7438 * SLI4 HBA firmware.
7439 *
7440 **/
7441int
7442lpfc_sli4_post_async_mbox(struct lpfc_hba *phba)
7443{
7444 struct lpfc_sli *psli = &phba->sli;
7445 LPFC_MBOXQ_t *mboxq;
7446 int rc = MBX_SUCCESS;
7447 unsigned long iflags;
7448 struct lpfc_mqe *mqe;
7449 uint32_t mbx_cmnd;
7450
7451 /* Check interrupt mode before post async mailbox command */
7452 if (unlikely(!phba->sli4_hba.intr_enable))
7453 return MBX_NOT_FINISHED;
7454
7455 /* Check for mailbox command service token */
7456 spin_lock_irqsave(&phba->hbalock, iflags);
7457 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
7458 spin_unlock_irqrestore(&phba->hbalock, iflags);
7459 return MBX_NOT_FINISHED;
7460 }
7461 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
7462 spin_unlock_irqrestore(&phba->hbalock, iflags);
7463 return MBX_NOT_FINISHED;
7464 }
7465 if (unlikely(phba->sli.mbox_active)) {
7466 spin_unlock_irqrestore(&phba->hbalock, iflags);
7467 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7468 "0384 There is pending active mailbox cmd\n");
7469 return MBX_NOT_FINISHED;
7470 }
7471 /* Take the mailbox command service token */
7472 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
7473
7474 /* Get the next mailbox command from head of queue */
7475 mboxq = lpfc_mbox_get(phba);
7476
7477 /* If no more mailbox command waiting for post, we're done */
7478 if (!mboxq) {
7479 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
7480 spin_unlock_irqrestore(&phba->hbalock, iflags);
7481 return MBX_SUCCESS;
7482 }
7483 phba->sli.mbox_active = mboxq;
7484 spin_unlock_irqrestore(&phba->hbalock, iflags);
7485
7486 /* Check device readiness for posting mailbox command */
7487 rc = lpfc_mbox_dev_check(phba);
7488 if (unlikely(rc))
7489 /* Driver clean routine will clean up pending mailbox */
7490 goto out_not_finished;
7491
7492 /* Prepare the mbox command to be posted */
7493 mqe = &mboxq->u.mqe;
7494 mbx_cmnd = bf_get(lpfc_mqe_command, mqe);
7495
7496 /* Start timer for the mbox_tmo and log some mailbox post messages */
7497 mod_timer(&psli->mbox_tmo, (jiffies +
James Smarta183a152011-10-10 21:32:43 -04007498 (HZ * lpfc_mbox_tmo_val(phba, mboxq))));
James Smartda0436e2009-05-22 14:51:39 -04007499
7500 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04007501 "(%d):0355 Mailbox cmd x%x (x%x/x%x) issue Data: "
James Smartda0436e2009-05-22 14:51:39 -04007502 "x%x x%x\n",
7503 mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
James Smarta183a152011-10-10 21:32:43 -04007504 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7505 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04007506 phba->pport->port_state, psli->sli_flag);
7507
7508 if (mbx_cmnd != MBX_HEARTBEAT) {
7509 if (mboxq->vport) {
7510 lpfc_debugfs_disc_trc(mboxq->vport,
7511 LPFC_DISC_TRC_MBOX_VPORT,
7512 "MBOX Send vport: cmd:x%x mb:x%x x%x",
7513 mbx_cmnd, mqe->un.mb_words[0],
7514 mqe->un.mb_words[1]);
7515 } else {
7516 lpfc_debugfs_disc_trc(phba->pport,
7517 LPFC_DISC_TRC_MBOX,
7518 "MBOX Send: cmd:x%x mb:x%x x%x",
7519 mbx_cmnd, mqe->un.mb_words[0],
7520 mqe->un.mb_words[1]);
7521 }
7522 }
7523 psli->slistat.mbox_cmd++;
7524
7525 /* Post the mailbox command to the port */
7526 rc = lpfc_sli4_mq_put(phba->sli4_hba.mbx_wq, mqe);
7527 if (rc != MBX_SUCCESS) {
7528 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04007529 "(%d):2533 Mailbox command x%x (x%x/x%x) "
James Smartda0436e2009-05-22 14:51:39 -04007530 "cannot issue Data: x%x x%x\n",
7531 mboxq->vport ? mboxq->vport->vpi : 0,
7532 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04007533 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7534 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04007535 psli->sli_flag, MBX_NOWAIT);
7536 goto out_not_finished;
7537 }
7538
7539 return rc;
7540
7541out_not_finished:
7542 spin_lock_irqsave(&phba->hbalock, iflags);
James Smartd7069f02012-03-01 22:36:29 -05007543 if (phba->sli.mbox_active) {
7544 mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
7545 __lpfc_mbox_cmpl_put(phba, mboxq);
7546 /* Release the token */
7547 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
7548 phba->sli.mbox_active = NULL;
7549 }
James Smartda0436e2009-05-22 14:51:39 -04007550 spin_unlock_irqrestore(&phba->hbalock, iflags);
7551
7552 return MBX_NOT_FINISHED;
7553}
7554
7555/**
7556 * lpfc_sli_issue_mbox - Wrapper func for issuing mailbox command
7557 * @phba: Pointer to HBA context object.
7558 * @pmbox: Pointer to mailbox object.
7559 * @flag: Flag indicating how the mailbox need to be processed.
7560 *
7561 * This routine wraps the actual SLI3 or SLI4 mailbox issuing routine from
7562 * the API jump table function pointer from the lpfc_hba struct.
7563 *
7564 * Return codes the caller owns the mailbox command after the return of the
7565 * function.
7566 **/
7567int
7568lpfc_sli_issue_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox, uint32_t flag)
7569{
7570 return phba->lpfc_sli_issue_mbox(phba, pmbox, flag);
7571}
7572
7573/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03007574 * lpfc_mbox_api_table_setup - Set up mbox api function jump table
James Smartda0436e2009-05-22 14:51:39 -04007575 * @phba: The hba struct for which this call is being executed.
7576 * @dev_grp: The HBA PCI-Device group number.
7577 *
7578 * This routine sets up the mbox interface API function jump table in @phba
7579 * struct.
7580 * Returns: 0 - success, -ENODEV - failure.
7581 **/
7582int
7583lpfc_mbox_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
7584{
7585
7586 switch (dev_grp) {
7587 case LPFC_PCI_DEV_LP:
7588 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s3;
7589 phba->lpfc_sli_handle_slow_ring_event =
7590 lpfc_sli_handle_slow_ring_event_s3;
7591 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s3;
7592 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s3;
7593 phba->lpfc_sli_brdready = lpfc_sli_brdready_s3;
7594 break;
7595 case LPFC_PCI_DEV_OC:
7596 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s4;
7597 phba->lpfc_sli_handle_slow_ring_event =
7598 lpfc_sli_handle_slow_ring_event_s4;
7599 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s4;
7600 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s4;
7601 phba->lpfc_sli_brdready = lpfc_sli_brdready_s4;
7602 break;
7603 default:
7604 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7605 "1420 Invalid HBA PCI-device group: 0x%x\n",
7606 dev_grp);
7607 return -ENODEV;
7608 break;
7609 }
7610 return 0;
7611}
7612
7613/**
James Smart3621a712009-04-06 18:47:14 -04007614 * __lpfc_sli_ringtx_put - Add an iocb to the txq
James Smarte59058c2008-08-24 21:49:00 -04007615 * @phba: Pointer to HBA context object.
7616 * @pring: Pointer to driver SLI ring object.
7617 * @piocb: Pointer to address of newly added command iocb.
7618 *
7619 * This function is called with hbalock held to add a command
7620 * iocb to the txq when SLI layer cannot submit the command iocb
7621 * to the ring.
7622 **/
James Smart2a9bf3d2010-06-07 15:24:45 -04007623void
James Smart92d7f7b2007-06-17 19:56:38 -05007624__lpfc_sli_ringtx_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -05007625 struct lpfc_iocbq *piocb)
dea31012005-04-17 16:05:31 -05007626{
7627 /* Insert the caller's iocb in the txq tail for later processing. */
7628 list_add_tail(&piocb->list, &pring->txq);
dea31012005-04-17 16:05:31 -05007629}
7630
James Smarte59058c2008-08-24 21:49:00 -04007631/**
James Smart3621a712009-04-06 18:47:14 -04007632 * lpfc_sli_next_iocb - Get the next iocb in the txq
James Smarte59058c2008-08-24 21:49:00 -04007633 * @phba: Pointer to HBA context object.
7634 * @pring: Pointer to driver SLI ring object.
7635 * @piocb: Pointer to address of newly added command iocb.
7636 *
7637 * This function is called with hbalock held before a new
7638 * iocb is submitted to the firmware. This function checks
7639 * txq to flush the iocbs in txq to Firmware before
7640 * submitting new iocbs to the Firmware.
7641 * If there are iocbs in the txq which need to be submitted
7642 * to firmware, lpfc_sli_next_iocb returns the first element
7643 * of the txq after dequeuing it from txq.
7644 * If there is no iocb in the txq then the function will return
7645 * *piocb and *piocb is set to NULL. Caller needs to check
7646 * *piocb to find if there are more commands in the txq.
7647 **/
dea31012005-04-17 16:05:31 -05007648static struct lpfc_iocbq *
7649lpfc_sli_next_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -05007650 struct lpfc_iocbq **piocb)
dea31012005-04-17 16:05:31 -05007651{
7652 struct lpfc_iocbq * nextiocb;
7653
7654 nextiocb = lpfc_sli_ringtx_get(phba, pring);
7655 if (!nextiocb) {
7656 nextiocb = *piocb;
7657 *piocb = NULL;
7658 }
7659
7660 return nextiocb;
7661}
7662
James Smarte59058c2008-08-24 21:49:00 -04007663/**
James Smart3772a992009-05-22 14:50:54 -04007664 * __lpfc_sli_issue_iocb_s3 - SLI3 device lockless ver of lpfc_sli_issue_iocb
James Smarte59058c2008-08-24 21:49:00 -04007665 * @phba: Pointer to HBA context object.
James Smart3772a992009-05-22 14:50:54 -04007666 * @ring_number: SLI ring number to issue iocb on.
James Smarte59058c2008-08-24 21:49:00 -04007667 * @piocb: Pointer to command iocb.
7668 * @flag: Flag indicating if this command can be put into txq.
7669 *
James Smart3772a992009-05-22 14:50:54 -04007670 * __lpfc_sli_issue_iocb_s3 is used by other functions in the driver to issue
7671 * an iocb command to an HBA with SLI-3 interface spec. If the PCI slot is
7672 * recovering from error state, if HBA is resetting or if LPFC_STOP_IOCB_EVENT
7673 * flag is turned on, the function returns IOCB_ERROR. When the link is down,
7674 * this function allows only iocbs for posting buffers. This function finds
7675 * next available slot in the command ring and posts the command to the
7676 * available slot and writes the port attention register to request HBA start
7677 * processing new iocb. If there is no slot available in the ring and
7678 * flag & SLI_IOCB_RET_IOCB is set, the new iocb is added to the txq, otherwise
7679 * the function returns IOCB_BUSY.
James Smarte59058c2008-08-24 21:49:00 -04007680 *
James Smart3772a992009-05-22 14:50:54 -04007681 * This function is called with hbalock held. The function will return success
7682 * after it successfully submit the iocb to firmware or after adding to the
7683 * txq.
James Smarte59058c2008-08-24 21:49:00 -04007684 **/
James Smart98c9ea52007-10-27 13:37:33 -04007685static int
James Smart3772a992009-05-22 14:50:54 -04007686__lpfc_sli_issue_iocb_s3(struct lpfc_hba *phba, uint32_t ring_number,
dea31012005-04-17 16:05:31 -05007687 struct lpfc_iocbq *piocb, uint32_t flag)
7688{
7689 struct lpfc_iocbq *nextiocb;
7690 IOCB_t *iocb;
James Smart3772a992009-05-22 14:50:54 -04007691 struct lpfc_sli_ring *pring = &phba->sli.ring[ring_number];
dea31012005-04-17 16:05:31 -05007692
James Smart92d7f7b2007-06-17 19:56:38 -05007693 if (piocb->iocb_cmpl && (!piocb->vport) &&
7694 (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
7695 (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
7696 lpfc_printf_log(phba, KERN_ERR,
7697 LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04007698 "1807 IOCB x%x failed. No vport\n",
James Smart92d7f7b2007-06-17 19:56:38 -05007699 piocb->iocb.ulpCommand);
7700 dump_stack();
7701 return IOCB_ERROR;
7702 }
7703
7704
Linas Vepstas8d63f372007-02-14 14:28:36 -06007705 /* If the PCI channel is in offline state, do not post iocbs. */
7706 if (unlikely(pci_channel_offline(phba->pcidev)))
7707 return IOCB_ERROR;
7708
James Smarta257bf92009-04-06 18:48:10 -04007709 /* If HBA has a deferred error attention, fail the iocb. */
7710 if (unlikely(phba->hba_flag & DEFER_ERATT))
7711 return IOCB_ERROR;
7712
dea31012005-04-17 16:05:31 -05007713 /*
7714 * We should never get an IOCB if we are in a < LINK_DOWN state
7715 */
James Smart2e0fef82007-06-17 19:56:36 -05007716 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
dea31012005-04-17 16:05:31 -05007717 return IOCB_ERROR;
7718
7719 /*
7720 * Check to see if we are blocking IOCB processing because of a
James Smart0b727fe2007-10-27 13:37:25 -04007721 * outstanding event.
dea31012005-04-17 16:05:31 -05007722 */
James Smart0b727fe2007-10-27 13:37:25 -04007723 if (unlikely(pring->flag & LPFC_STOP_IOCB_EVENT))
dea31012005-04-17 16:05:31 -05007724 goto iocb_busy;
7725
James Smart2e0fef82007-06-17 19:56:36 -05007726 if (unlikely(phba->link_state == LPFC_LINK_DOWN)) {
dea31012005-04-17 16:05:31 -05007727 /*
James Smart2680eea2007-04-25 09:52:55 -04007728 * Only CREATE_XRI, CLOSE_XRI, and QUE_RING_BUF
dea31012005-04-17 16:05:31 -05007729 * can be issued if the link is not up.
7730 */
7731 switch (piocb->iocb.ulpCommand) {
James Smart84774a42008-08-24 21:50:06 -04007732 case CMD_GEN_REQUEST64_CR:
7733 case CMD_GEN_REQUEST64_CX:
7734 if (!(phba->sli.sli_flag & LPFC_MENLO_MAINT) ||
7735 (piocb->iocb.un.genreq64.w5.hcsw.Rctl !=
James Smart6a9c52c2009-10-02 15:16:51 -04007736 FC_RCTL_DD_UNSOL_CMD) ||
James Smart84774a42008-08-24 21:50:06 -04007737 (piocb->iocb.un.genreq64.w5.hcsw.Type !=
7738 MENLO_TRANSPORT_TYPE))
7739
7740 goto iocb_busy;
7741 break;
dea31012005-04-17 16:05:31 -05007742 case CMD_QUE_RING_BUF_CN:
7743 case CMD_QUE_RING_BUF64_CN:
dea31012005-04-17 16:05:31 -05007744 /*
7745 * For IOCBs, like QUE_RING_BUF, that have no rsp ring
7746 * completion, iocb_cmpl MUST be 0.
7747 */
7748 if (piocb->iocb_cmpl)
7749 piocb->iocb_cmpl = NULL;
7750 /*FALLTHROUGH*/
7751 case CMD_CREATE_XRI_CR:
James Smart2680eea2007-04-25 09:52:55 -04007752 case CMD_CLOSE_XRI_CN:
7753 case CMD_CLOSE_XRI_CX:
dea31012005-04-17 16:05:31 -05007754 break;
7755 default:
7756 goto iocb_busy;
7757 }
7758
7759 /*
7760 * For FCP commands, we must be in a state where we can process link
7761 * attention events.
7762 */
7763 } else if (unlikely(pring->ringno == phba->sli.fcp_ring &&
James Smart92d7f7b2007-06-17 19:56:38 -05007764 !(phba->sli.sli_flag & LPFC_PROCESS_LA))) {
dea31012005-04-17 16:05:31 -05007765 goto iocb_busy;
James Smart92d7f7b2007-06-17 19:56:38 -05007766 }
dea31012005-04-17 16:05:31 -05007767
dea31012005-04-17 16:05:31 -05007768 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
7769 (nextiocb = lpfc_sli_next_iocb(phba, pring, &piocb)))
7770 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
7771
7772 if (iocb)
7773 lpfc_sli_update_ring(phba, pring);
7774 else
7775 lpfc_sli_update_full_ring(phba, pring);
7776
7777 if (!piocb)
7778 return IOCB_SUCCESS;
7779
7780 goto out_busy;
7781
7782 iocb_busy:
7783 pring->stats.iocb_cmd_delay++;
7784
7785 out_busy:
7786
7787 if (!(flag & SLI_IOCB_RET_IOCB)) {
James Smart92d7f7b2007-06-17 19:56:38 -05007788 __lpfc_sli_ringtx_put(phba, pring, piocb);
dea31012005-04-17 16:05:31 -05007789 return IOCB_SUCCESS;
7790 }
7791
7792 return IOCB_BUSY;
7793}
7794
James Smart3772a992009-05-22 14:50:54 -04007795/**
James Smart4f774512009-05-22 14:52:35 -04007796 * lpfc_sli4_bpl2sgl - Convert the bpl/bde to a sgl.
7797 * @phba: Pointer to HBA context object.
7798 * @piocb: Pointer to command iocb.
7799 * @sglq: Pointer to the scatter gather queue object.
7800 *
7801 * This routine converts the bpl or bde that is in the IOCB
7802 * to a sgl list for the sli4 hardware. The physical address
7803 * of the bpl/bde is converted back to a virtual address.
7804 * If the IOCB contains a BPL then the list of BDE's is
7805 * converted to sli4_sge's. If the IOCB contains a single
7806 * BDE then it is converted to a single sli_sge.
7807 * The IOCB is still in cpu endianess so the contents of
7808 * the bpl can be used without byte swapping.
7809 *
7810 * Returns valid XRI = Success, NO_XRI = Failure.
7811**/
7812static uint16_t
7813lpfc_sli4_bpl2sgl(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq,
7814 struct lpfc_sglq *sglq)
7815{
7816 uint16_t xritag = NO_XRI;
7817 struct ulp_bde64 *bpl = NULL;
7818 struct ulp_bde64 bde;
7819 struct sli4_sge *sgl = NULL;
James Smart1b511972011-12-13 13:23:09 -05007820 struct lpfc_dmabuf *dmabuf;
James Smart4f774512009-05-22 14:52:35 -04007821 IOCB_t *icmd;
7822 int numBdes = 0;
7823 int i = 0;
James Smart63e801c2010-11-20 23:14:19 -05007824 uint32_t offset = 0; /* accumulated offset in the sg request list */
7825 int inbound = 0; /* number of sg reply entries inbound from firmware */
James Smart4f774512009-05-22 14:52:35 -04007826
7827 if (!piocbq || !sglq)
7828 return xritag;
7829
7830 sgl = (struct sli4_sge *)sglq->sgl;
7831 icmd = &piocbq->iocb;
James Smart6b5151f2012-01-18 16:24:06 -05007832 if (icmd->ulpCommand == CMD_XMIT_BLS_RSP64_CX)
7833 return sglq->sli4_xritag;
James Smart4f774512009-05-22 14:52:35 -04007834 if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
7835 numBdes = icmd->un.genreq64.bdl.bdeSize /
7836 sizeof(struct ulp_bde64);
7837 /* The addrHigh and addrLow fields within the IOCB
7838 * have not been byteswapped yet so there is no
7839 * need to swap them back.
7840 */
James Smart1b511972011-12-13 13:23:09 -05007841 if (piocbq->context3)
7842 dmabuf = (struct lpfc_dmabuf *)piocbq->context3;
7843 else
7844 return xritag;
James Smart4f774512009-05-22 14:52:35 -04007845
James Smart1b511972011-12-13 13:23:09 -05007846 bpl = (struct ulp_bde64 *)dmabuf->virt;
James Smart4f774512009-05-22 14:52:35 -04007847 if (!bpl)
7848 return xritag;
7849
7850 for (i = 0; i < numBdes; i++) {
7851 /* Should already be byte swapped. */
James Smart28baac72010-02-12 14:42:03 -05007852 sgl->addr_hi = bpl->addrHigh;
7853 sgl->addr_lo = bpl->addrLow;
7854
James Smart05580562011-05-24 11:40:48 -04007855 sgl->word2 = le32_to_cpu(sgl->word2);
James Smart4f774512009-05-22 14:52:35 -04007856 if ((i+1) == numBdes)
7857 bf_set(lpfc_sli4_sge_last, sgl, 1);
7858 else
7859 bf_set(lpfc_sli4_sge_last, sgl, 0);
James Smart28baac72010-02-12 14:42:03 -05007860 /* swap the size field back to the cpu so we
7861 * can assign it to the sgl.
7862 */
7863 bde.tus.w = le32_to_cpu(bpl->tus.w);
7864 sgl->sge_len = cpu_to_le32(bde.tus.f.bdeSize);
James Smart63e801c2010-11-20 23:14:19 -05007865 /* The offsets in the sgl need to be accumulated
7866 * separately for the request and reply lists.
7867 * The request is always first, the reply follows.
7868 */
7869 if (piocbq->iocb.ulpCommand == CMD_GEN_REQUEST64_CR) {
7870 /* add up the reply sg entries */
7871 if (bpl->tus.f.bdeFlags == BUFF_TYPE_BDE_64I)
7872 inbound++;
7873 /* first inbound? reset the offset */
7874 if (inbound == 1)
7875 offset = 0;
7876 bf_set(lpfc_sli4_sge_offset, sgl, offset);
James Smartf9bb2da2011-10-10 21:34:11 -04007877 bf_set(lpfc_sli4_sge_type, sgl,
7878 LPFC_SGE_TYPE_DATA);
James Smart63e801c2010-11-20 23:14:19 -05007879 offset += bde.tus.f.bdeSize;
7880 }
James Smart546fc852011-03-11 16:06:29 -05007881 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart4f774512009-05-22 14:52:35 -04007882 bpl++;
7883 sgl++;
7884 }
7885 } else if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BDE_64) {
7886 /* The addrHigh and addrLow fields of the BDE have not
7887 * been byteswapped yet so they need to be swapped
7888 * before putting them in the sgl.
7889 */
7890 sgl->addr_hi =
7891 cpu_to_le32(icmd->un.genreq64.bdl.addrHigh);
7892 sgl->addr_lo =
7893 cpu_to_le32(icmd->un.genreq64.bdl.addrLow);
James Smart05580562011-05-24 11:40:48 -04007894 sgl->word2 = le32_to_cpu(sgl->word2);
James Smart4f774512009-05-22 14:52:35 -04007895 bf_set(lpfc_sli4_sge_last, sgl, 1);
7896 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart28baac72010-02-12 14:42:03 -05007897 sgl->sge_len =
7898 cpu_to_le32(icmd->un.genreq64.bdl.bdeSize);
James Smart4f774512009-05-22 14:52:35 -04007899 }
7900 return sglq->sli4_xritag;
7901}
7902
7903/**
7904 * lpfc_sli4_scmd_to_wqidx_distr - scsi command to SLI4 WQ index distribution
7905 * @phba: Pointer to HBA context object.
James Smart4f774512009-05-22 14:52:35 -04007906 *
James Smarta93ff372010-10-22 11:06:08 -04007907 * This routine performs a roundrobin SCSI command to SLI4 FCP WQ index
James Smart8fa38512009-07-19 10:01:03 -04007908 * distribution. This is called by __lpfc_sli_issue_iocb_s4() with the hbalock
7909 * held.
James Smart4f774512009-05-22 14:52:35 -04007910 *
7911 * Return: index into SLI4 fast-path FCP queue index.
7912 **/
James Smart2a76a282012-08-03 12:35:54 -04007913static inline uint32_t
James Smart8fa38512009-07-19 10:01:03 -04007914lpfc_sli4_scmd_to_wqidx_distr(struct lpfc_hba *phba)
James Smart4f774512009-05-22 14:52:35 -04007915{
James Smart2a76a282012-08-03 12:35:54 -04007916 int i;
James Smart4f774512009-05-22 14:52:35 -04007917
James Smart49aa1432012-08-03 12:36:42 -04007918 if (phba->cfg_fcp_io_sched == LPFC_FCP_SCHED_BY_CPU)
7919 i = smp_processor_id();
7920 else
7921 i = atomic_add_return(1, &phba->fcp_qidx);
7922
James Smart67d12732012-08-03 12:36:13 -04007923 i = (i % phba->cfg_fcp_io_channel);
James Smart2a76a282012-08-03 12:35:54 -04007924 return i;
James Smart4f774512009-05-22 14:52:35 -04007925}
7926
7927/**
7928 * lpfc_sli_iocb2wqe - Convert the IOCB to a work queue entry.
7929 * @phba: Pointer to HBA context object.
7930 * @piocb: Pointer to command iocb.
7931 * @wqe: Pointer to the work queue entry.
7932 *
7933 * This routine converts the iocb command to its Work Queue Entry
7934 * equivalent. The wqe pointer should not have any fields set when
7935 * this routine is called because it will memcpy over them.
7936 * This routine does not set the CQ_ID or the WQEC bits in the
7937 * wqe.
7938 *
7939 * Returns: 0 = Success, IOCB_ERROR = Failure.
7940 **/
7941static int
7942lpfc_sli4_iocb2wqe(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq,
7943 union lpfc_wqe *wqe)
7944{
James Smart5ffc2662009-11-18 15:39:44 -05007945 uint32_t xmit_len = 0, total_len = 0;
James Smart4f774512009-05-22 14:52:35 -04007946 uint8_t ct = 0;
7947 uint32_t fip;
7948 uint32_t abort_tag;
7949 uint8_t command_type = ELS_COMMAND_NON_FIP;
7950 uint8_t cmnd;
7951 uint16_t xritag;
James Smartdcf2a4e2010-09-29 11:18:53 -04007952 uint16_t abrt_iotag;
7953 struct lpfc_iocbq *abrtiocbq;
James Smart4f774512009-05-22 14:52:35 -04007954 struct ulp_bde64 *bpl = NULL;
James Smartf0d9bcc2010-10-22 11:07:09 -04007955 uint32_t els_id = LPFC_ELS_ID_DEFAULT;
James Smart5ffc2662009-11-18 15:39:44 -05007956 int numBdes, i;
7957 struct ulp_bde64 bde;
James Smartc31098c2011-04-16 11:03:33 -04007958 struct lpfc_nodelist *ndlp;
James Smartff78d8f2011-12-13 13:21:35 -05007959 uint32_t *pcmd;
James Smart1b511972011-12-13 13:23:09 -05007960 uint32_t if_type;
James Smart4f774512009-05-22 14:52:35 -04007961
James Smart45ed1192009-10-02 15:17:02 -04007962 fip = phba->hba_flag & HBA_FIP_SUPPORT;
James Smart4f774512009-05-22 14:52:35 -04007963 /* The fcp commands will set command type */
James Smart0c287582009-06-10 17:22:56 -04007964 if (iocbq->iocb_flag & LPFC_IO_FCP)
James Smart4f774512009-05-22 14:52:35 -04007965 command_type = FCP_COMMAND;
James Smartc8685952009-11-18 15:39:16 -05007966 else if (fip && (iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK))
James Smart0c287582009-06-10 17:22:56 -04007967 command_type = ELS_COMMAND_FIP;
7968 else
7969 command_type = ELS_COMMAND_NON_FIP;
7970
James Smart4f774512009-05-22 14:52:35 -04007971 /* Some of the fields are in the right position already */
7972 memcpy(wqe, &iocbq->iocb, sizeof(union lpfc_wqe));
7973 abort_tag = (uint32_t) iocbq->iotag;
7974 xritag = iocbq->sli4_xritag;
James Smartf0d9bcc2010-10-22 11:07:09 -04007975 wqe->generic.wqe_com.word7 = 0; /* The ct field has moved so reset */
James Smart4f774512009-05-22 14:52:35 -04007976 /* words0-2 bpl convert bde */
7977 if (iocbq->iocb.un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
James Smart5ffc2662009-11-18 15:39:44 -05007978 numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
7979 sizeof(struct ulp_bde64);
James Smart4f774512009-05-22 14:52:35 -04007980 bpl = (struct ulp_bde64 *)
7981 ((struct lpfc_dmabuf *)iocbq->context3)->virt;
7982 if (!bpl)
7983 return IOCB_ERROR;
7984
7985 /* Should already be byte swapped. */
7986 wqe->generic.bde.addrHigh = le32_to_cpu(bpl->addrHigh);
7987 wqe->generic.bde.addrLow = le32_to_cpu(bpl->addrLow);
7988 /* swap the size field back to the cpu so we
7989 * can assign it to the sgl.
7990 */
7991 wqe->generic.bde.tus.w = le32_to_cpu(bpl->tus.w);
James Smart5ffc2662009-11-18 15:39:44 -05007992 xmit_len = wqe->generic.bde.tus.f.bdeSize;
7993 total_len = 0;
7994 for (i = 0; i < numBdes; i++) {
7995 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
7996 total_len += bde.tus.f.bdeSize;
7997 }
James Smart4f774512009-05-22 14:52:35 -04007998 } else
James Smart5ffc2662009-11-18 15:39:44 -05007999 xmit_len = iocbq->iocb.un.fcpi64.bdl.bdeSize;
James Smart4f774512009-05-22 14:52:35 -04008000
8001 iocbq->iocb.ulpIoTag = iocbq->iotag;
8002 cmnd = iocbq->iocb.ulpCommand;
8003
8004 switch (iocbq->iocb.ulpCommand) {
8005 case CMD_ELS_REQUEST64_CR:
James Smart93d1379e2012-05-09 21:19:34 -04008006 if (iocbq->iocb_flag & LPFC_IO_LIBDFC)
8007 ndlp = iocbq->context_un.ndlp;
8008 else
8009 ndlp = (struct lpfc_nodelist *)iocbq->context1;
James Smart4f774512009-05-22 14:52:35 -04008010 if (!iocbq->iocb.ulpLe) {
8011 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8012 "2007 Only Limited Edition cmd Format"
8013 " supported 0x%x\n",
8014 iocbq->iocb.ulpCommand);
8015 return IOCB_ERROR;
8016 }
James Smartff78d8f2011-12-13 13:21:35 -05008017
James Smart5ffc2662009-11-18 15:39:44 -05008018 wqe->els_req.payload_len = xmit_len;
James Smart4f774512009-05-22 14:52:35 -04008019 /* Els_reguest64 has a TMO */
8020 bf_set(wqe_tmo, &wqe->els_req.wqe_com,
8021 iocbq->iocb.ulpTimeout);
8022 /* Need a VF for word 4 set the vf bit*/
8023 bf_set(els_req64_vf, &wqe->els_req, 0);
8024 /* And a VFID for word 12 */
8025 bf_set(els_req64_vfid, &wqe->els_req, 0);
James Smart4f774512009-05-22 14:52:35 -04008026 ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
James Smartf0d9bcc2010-10-22 11:07:09 -04008027 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
8028 iocbq->iocb.ulpContext);
8029 bf_set(wqe_ct, &wqe->els_req.wqe_com, ct);
8030 bf_set(wqe_pu, &wqe->els_req.wqe_com, 0);
James Smart4f774512009-05-22 14:52:35 -04008031 /* CCP CCPE PV PRI in word10 were set in the memcpy */
James Smartff78d8f2011-12-13 13:21:35 -05008032 if (command_type == ELS_COMMAND_FIP)
James Smartc8685952009-11-18 15:39:16 -05008033 els_id = ((iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK)
8034 >> LPFC_FIP_ELS_ID_SHIFT);
James Smartff78d8f2011-12-13 13:21:35 -05008035 pcmd = (uint32_t *) (((struct lpfc_dmabuf *)
8036 iocbq->context2)->virt);
James Smart1b511972011-12-13 13:23:09 -05008037 if_type = bf_get(lpfc_sli_intf_if_type,
8038 &phba->sli4_hba.sli_intf);
8039 if (if_type == LPFC_SLI_INTF_IF_TYPE_2) {
James Smartff78d8f2011-12-13 13:21:35 -05008040 if (pcmd && (*pcmd == ELS_CMD_FLOGI ||
James Smartcb69f7d2011-12-13 13:21:57 -05008041 *pcmd == ELS_CMD_SCR ||
James Smart6b5151f2012-01-18 16:24:06 -05008042 *pcmd == ELS_CMD_FDISC ||
James Smartbdcd2b92012-03-01 22:33:52 -05008043 *pcmd == ELS_CMD_LOGO ||
James Smartff78d8f2011-12-13 13:21:35 -05008044 *pcmd == ELS_CMD_PLOGI)) {
8045 bf_set(els_req64_sp, &wqe->els_req, 1);
8046 bf_set(els_req64_sid, &wqe->els_req,
8047 iocbq->vport->fc_myDID);
James Smart939723a2012-05-09 21:19:03 -04008048 if ((*pcmd == ELS_CMD_FLOGI) &&
8049 !(phba->fc_topology ==
8050 LPFC_TOPOLOGY_LOOP))
8051 bf_set(els_req64_sid, &wqe->els_req, 0);
James Smartff78d8f2011-12-13 13:21:35 -05008052 bf_set(wqe_ct, &wqe->els_req.wqe_com, 1);
8053 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
James Smarta7dd9c02012-05-09 21:16:50 -04008054 phba->vpi_ids[iocbq->vport->vpi]);
James Smart3ef6d242012-01-18 16:23:48 -05008055 } else if (pcmd && iocbq->context1) {
James Smartff78d8f2011-12-13 13:21:35 -05008056 bf_set(wqe_ct, &wqe->els_req.wqe_com, 0);
8057 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
8058 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
8059 }
James Smartc8685952009-11-18 15:39:16 -05008060 }
James Smart6d368e52011-05-24 11:44:12 -04008061 bf_set(wqe_temp_rpi, &wqe->els_req.wqe_com,
8062 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
James Smartf0d9bcc2010-10-22 11:07:09 -04008063 bf_set(wqe_els_id, &wqe->els_req.wqe_com, els_id);
8064 bf_set(wqe_dbde, &wqe->els_req.wqe_com, 1);
8065 bf_set(wqe_iod, &wqe->els_req.wqe_com, LPFC_WQE_IOD_READ);
8066 bf_set(wqe_qosd, &wqe->els_req.wqe_com, 1);
8067 bf_set(wqe_lenloc, &wqe->els_req.wqe_com, LPFC_WQE_LENLOC_NONE);
8068 bf_set(wqe_ebde_cnt, &wqe->els_req.wqe_com, 0);
James Smart7851fe22011-07-22 18:36:52 -04008069 break;
James Smart5ffc2662009-11-18 15:39:44 -05008070 case CMD_XMIT_SEQUENCE64_CX:
James Smartf0d9bcc2010-10-22 11:07:09 -04008071 bf_set(wqe_ctxt_tag, &wqe->xmit_sequence.wqe_com,
8072 iocbq->iocb.un.ulpWord[3]);
8073 bf_set(wqe_rcvoxid, &wqe->xmit_sequence.wqe_com,
James Smart7851fe22011-07-22 18:36:52 -04008074 iocbq->iocb.unsli3.rcvsli3.ox_id);
James Smart5ffc2662009-11-18 15:39:44 -05008075 /* The entire sequence is transmitted for this IOCB */
8076 xmit_len = total_len;
8077 cmnd = CMD_XMIT_SEQUENCE64_CR;
James Smart1b511972011-12-13 13:23:09 -05008078 if (phba->link_flag & LS_LOOPBACK_MODE)
8079 bf_set(wqe_xo, &wqe->xmit_sequence.wge_ctl, 1);
James Smart4f774512009-05-22 14:52:35 -04008080 case CMD_XMIT_SEQUENCE64_CR:
James Smartf0d9bcc2010-10-22 11:07:09 -04008081 /* word3 iocb=io_tag32 wqe=reserved */
8082 wqe->xmit_sequence.rsvd3 = 0;
James Smart4f774512009-05-22 14:52:35 -04008083 /* word4 relative_offset memcpy */
8084 /* word5 r_ctl/df_ctl memcpy */
James Smartf0d9bcc2010-10-22 11:07:09 -04008085 bf_set(wqe_pu, &wqe->xmit_sequence.wqe_com, 0);
8086 bf_set(wqe_dbde, &wqe->xmit_sequence.wqe_com, 1);
8087 bf_set(wqe_iod, &wqe->xmit_sequence.wqe_com,
8088 LPFC_WQE_IOD_WRITE);
8089 bf_set(wqe_lenloc, &wqe->xmit_sequence.wqe_com,
8090 LPFC_WQE_LENLOC_WORD12);
8091 bf_set(wqe_ebde_cnt, &wqe->xmit_sequence.wqe_com, 0);
James Smart5ffc2662009-11-18 15:39:44 -05008092 wqe->xmit_sequence.xmit_len = xmit_len;
8093 command_type = OTHER_COMMAND;
James Smart7851fe22011-07-22 18:36:52 -04008094 break;
James Smart4f774512009-05-22 14:52:35 -04008095 case CMD_XMIT_BCAST64_CN:
James Smartf0d9bcc2010-10-22 11:07:09 -04008096 /* word3 iocb=iotag32 wqe=seq_payload_len */
8097 wqe->xmit_bcast64.seq_payload_len = xmit_len;
James Smart4f774512009-05-22 14:52:35 -04008098 /* word4 iocb=rsvd wqe=rsvd */
8099 /* word5 iocb=rctl/type/df_ctl wqe=rctl/type/df_ctl memcpy */
8100 /* word6 iocb=ctxt_tag/io_tag wqe=ctxt_tag/xri */
James Smartf0d9bcc2010-10-22 11:07:09 -04008101 bf_set(wqe_ct, &wqe->xmit_bcast64.wqe_com,
James Smart4f774512009-05-22 14:52:35 -04008102 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
James Smartf0d9bcc2010-10-22 11:07:09 -04008103 bf_set(wqe_dbde, &wqe->xmit_bcast64.wqe_com, 1);
8104 bf_set(wqe_iod, &wqe->xmit_bcast64.wqe_com, LPFC_WQE_IOD_WRITE);
8105 bf_set(wqe_lenloc, &wqe->xmit_bcast64.wqe_com,
8106 LPFC_WQE_LENLOC_WORD3);
8107 bf_set(wqe_ebde_cnt, &wqe->xmit_bcast64.wqe_com, 0);
James Smart7851fe22011-07-22 18:36:52 -04008108 break;
James Smart4f774512009-05-22 14:52:35 -04008109 case CMD_FCP_IWRITE64_CR:
8110 command_type = FCP_COMMAND_DATA_OUT;
James Smartf0d9bcc2010-10-22 11:07:09 -04008111 /* word3 iocb=iotag wqe=payload_offset_len */
8112 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
8113 wqe->fcp_iwrite.payload_offset_len =
James Smart5ffc2662009-11-18 15:39:44 -05008114 xmit_len + sizeof(struct fcp_rsp);
James Smartf0d9bcc2010-10-22 11:07:09 -04008115 /* word4 iocb=parameter wqe=total_xfer_length memcpy */
8116 /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
8117 bf_set(wqe_erp, &wqe->fcp_iwrite.wqe_com,
8118 iocbq->iocb.ulpFCP2Rcvy);
8119 bf_set(wqe_lnk, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpXS);
8120 /* Always open the exchange */
8121 bf_set(wqe_xc, &wqe->fcp_iwrite.wqe_com, 0);
James Smartf0d9bcc2010-10-22 11:07:09 -04008122 bf_set(wqe_iod, &wqe->fcp_iwrite.wqe_com, LPFC_WQE_IOD_WRITE);
8123 bf_set(wqe_lenloc, &wqe->fcp_iwrite.wqe_com,
8124 LPFC_WQE_LENLOC_WORD4);
8125 bf_set(wqe_ebde_cnt, &wqe->fcp_iwrite.wqe_com, 0);
8126 bf_set(wqe_pu, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpPU);
James Smartacd68592012-01-18 16:25:09 -05008127 bf_set(wqe_dbde, &wqe->fcp_iwrite.wqe_com, 1);
James Smart7851fe22011-07-22 18:36:52 -04008128 break;
James Smartf0d9bcc2010-10-22 11:07:09 -04008129 case CMD_FCP_IREAD64_CR:
8130 /* word3 iocb=iotag wqe=payload_offset_len */
8131 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
8132 wqe->fcp_iread.payload_offset_len =
8133 xmit_len + sizeof(struct fcp_rsp);
8134 /* word4 iocb=parameter wqe=total_xfer_length memcpy */
8135 /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
8136 bf_set(wqe_erp, &wqe->fcp_iread.wqe_com,
8137 iocbq->iocb.ulpFCP2Rcvy);
8138 bf_set(wqe_lnk, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpXS);
James Smart4f774512009-05-22 14:52:35 -04008139 /* Always open the exchange */
8140 bf_set(wqe_xc, &wqe->fcp_iread.wqe_com, 0);
James Smartf0d9bcc2010-10-22 11:07:09 -04008141 bf_set(wqe_iod, &wqe->fcp_iread.wqe_com, LPFC_WQE_IOD_READ);
8142 bf_set(wqe_lenloc, &wqe->fcp_iread.wqe_com,
8143 LPFC_WQE_LENLOC_WORD4);
8144 bf_set(wqe_ebde_cnt, &wqe->fcp_iread.wqe_com, 0);
8145 bf_set(wqe_pu, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpPU);
James Smartacd68592012-01-18 16:25:09 -05008146 bf_set(wqe_dbde, &wqe->fcp_iread.wqe_com, 1);
James Smart7851fe22011-07-22 18:36:52 -04008147 break;
James Smartf1126682009-06-10 17:22:44 -04008148 case CMD_FCP_ICMND64_CR:
James Smartf0d9bcc2010-10-22 11:07:09 -04008149 /* word3 iocb=IO_TAG wqe=reserved */
8150 wqe->fcp_icmd.rsrvd3 = 0;
8151 bf_set(wqe_pu, &wqe->fcp_icmd.wqe_com, 0);
James Smartf1126682009-06-10 17:22:44 -04008152 /* Always open the exchange */
James Smartf0d9bcc2010-10-22 11:07:09 -04008153 bf_set(wqe_xc, &wqe->fcp_icmd.wqe_com, 0);
8154 bf_set(wqe_dbde, &wqe->fcp_icmd.wqe_com, 1);
8155 bf_set(wqe_iod, &wqe->fcp_icmd.wqe_com, LPFC_WQE_IOD_WRITE);
8156 bf_set(wqe_qosd, &wqe->fcp_icmd.wqe_com, 1);
8157 bf_set(wqe_lenloc, &wqe->fcp_icmd.wqe_com,
8158 LPFC_WQE_LENLOC_NONE);
8159 bf_set(wqe_ebde_cnt, &wqe->fcp_icmd.wqe_com, 0);
James Smart2a94aea2012-09-29 11:30:31 -04008160 bf_set(wqe_erp, &wqe->fcp_icmd.wqe_com,
8161 iocbq->iocb.ulpFCP2Rcvy);
James Smart7851fe22011-07-22 18:36:52 -04008162 break;
James Smart4f774512009-05-22 14:52:35 -04008163 case CMD_GEN_REQUEST64_CR:
James Smart63e801c2010-11-20 23:14:19 -05008164 /* For this command calculate the xmit length of the
8165 * request bde.
8166 */
8167 xmit_len = 0;
8168 numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
8169 sizeof(struct ulp_bde64);
8170 for (i = 0; i < numBdes; i++) {
James Smart63e801c2010-11-20 23:14:19 -05008171 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
James Smart546fc852011-03-11 16:06:29 -05008172 if (bde.tus.f.bdeFlags != BUFF_TYPE_BDE_64)
8173 break;
James Smart63e801c2010-11-20 23:14:19 -05008174 xmit_len += bde.tus.f.bdeSize;
8175 }
James Smartf0d9bcc2010-10-22 11:07:09 -04008176 /* word3 iocb=IO_TAG wqe=request_payload_len */
8177 wqe->gen_req.request_payload_len = xmit_len;
8178 /* word4 iocb=parameter wqe=relative_offset memcpy */
8179 /* word5 [rctl, type, df_ctl, la] copied in memcpy */
James Smart4f774512009-05-22 14:52:35 -04008180 /* word6 context tag copied in memcpy */
8181 if (iocbq->iocb.ulpCt_h || iocbq->iocb.ulpCt_l) {
8182 ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
8183 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8184 "2015 Invalid CT %x command 0x%x\n",
8185 ct, iocbq->iocb.ulpCommand);
8186 return IOCB_ERROR;
8187 }
James Smartf0d9bcc2010-10-22 11:07:09 -04008188 bf_set(wqe_ct, &wqe->gen_req.wqe_com, 0);
8189 bf_set(wqe_tmo, &wqe->gen_req.wqe_com, iocbq->iocb.ulpTimeout);
8190 bf_set(wqe_pu, &wqe->gen_req.wqe_com, iocbq->iocb.ulpPU);
8191 bf_set(wqe_dbde, &wqe->gen_req.wqe_com, 1);
8192 bf_set(wqe_iod, &wqe->gen_req.wqe_com, LPFC_WQE_IOD_READ);
8193 bf_set(wqe_qosd, &wqe->gen_req.wqe_com, 1);
8194 bf_set(wqe_lenloc, &wqe->gen_req.wqe_com, LPFC_WQE_LENLOC_NONE);
8195 bf_set(wqe_ebde_cnt, &wqe->gen_req.wqe_com, 0);
James Smart4f774512009-05-22 14:52:35 -04008196 command_type = OTHER_COMMAND;
James Smart7851fe22011-07-22 18:36:52 -04008197 break;
James Smart4f774512009-05-22 14:52:35 -04008198 case CMD_XMIT_ELS_RSP64_CX:
James Smartc31098c2011-04-16 11:03:33 -04008199 ndlp = (struct lpfc_nodelist *)iocbq->context1;
James Smart4f774512009-05-22 14:52:35 -04008200 /* words0-2 BDE memcpy */
James Smartf0d9bcc2010-10-22 11:07:09 -04008201 /* word3 iocb=iotag32 wqe=response_payload_len */
8202 wqe->xmit_els_rsp.response_payload_len = xmit_len;
James Smart939723a2012-05-09 21:19:03 -04008203 /* word4 */
8204 wqe->xmit_els_rsp.word4 = 0;
James Smart4f774512009-05-22 14:52:35 -04008205 /* word5 iocb=rsvd wge=did */
8206 bf_set(wqe_els_did, &wqe->xmit_els_rsp.wqe_dest,
James Smart939723a2012-05-09 21:19:03 -04008207 iocbq->iocb.un.xseq64.xmit_els_remoteID);
8208
8209 if_type = bf_get(lpfc_sli_intf_if_type,
8210 &phba->sli4_hba.sli_intf);
8211 if (if_type == LPFC_SLI_INTF_IF_TYPE_2) {
8212 if (iocbq->vport->fc_flag & FC_PT2PT) {
8213 bf_set(els_rsp64_sp, &wqe->xmit_els_rsp, 1);
8214 bf_set(els_rsp64_sid, &wqe->xmit_els_rsp,
8215 iocbq->vport->fc_myDID);
8216 if (iocbq->vport->fc_myDID == Fabric_DID) {
8217 bf_set(wqe_els_did,
8218 &wqe->xmit_els_rsp.wqe_dest, 0);
8219 }
8220 }
8221 }
James Smartf0d9bcc2010-10-22 11:07:09 -04008222 bf_set(wqe_ct, &wqe->xmit_els_rsp.wqe_com,
8223 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
8224 bf_set(wqe_pu, &wqe->xmit_els_rsp.wqe_com, iocbq->iocb.ulpPU);
8225 bf_set(wqe_rcvoxid, &wqe->xmit_els_rsp.wqe_com,
James Smart7851fe22011-07-22 18:36:52 -04008226 iocbq->iocb.unsli3.rcvsli3.ox_id);
James Smart4f774512009-05-22 14:52:35 -04008227 if (!iocbq->iocb.ulpCt_h && iocbq->iocb.ulpCt_l)
James Smartf0d9bcc2010-10-22 11:07:09 -04008228 bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
James Smart6d368e52011-05-24 11:44:12 -04008229 phba->vpi_ids[iocbq->vport->vpi]);
James Smartf0d9bcc2010-10-22 11:07:09 -04008230 bf_set(wqe_dbde, &wqe->xmit_els_rsp.wqe_com, 1);
8231 bf_set(wqe_iod, &wqe->xmit_els_rsp.wqe_com, LPFC_WQE_IOD_WRITE);
8232 bf_set(wqe_qosd, &wqe->xmit_els_rsp.wqe_com, 1);
8233 bf_set(wqe_lenloc, &wqe->xmit_els_rsp.wqe_com,
8234 LPFC_WQE_LENLOC_WORD3);
8235 bf_set(wqe_ebde_cnt, &wqe->xmit_els_rsp.wqe_com, 0);
James Smart6d368e52011-05-24 11:44:12 -04008236 bf_set(wqe_rsp_temp_rpi, &wqe->xmit_els_rsp,
8237 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
James Smartff78d8f2011-12-13 13:21:35 -05008238 pcmd = (uint32_t *) (((struct lpfc_dmabuf *)
8239 iocbq->context2)->virt);
8240 if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
James Smart939723a2012-05-09 21:19:03 -04008241 bf_set(els_rsp64_sp, &wqe->xmit_els_rsp, 1);
8242 bf_set(els_rsp64_sid, &wqe->xmit_els_rsp,
James Smartff78d8f2011-12-13 13:21:35 -05008243 iocbq->vport->fc_myDID);
James Smart939723a2012-05-09 21:19:03 -04008244 bf_set(wqe_ct, &wqe->xmit_els_rsp.wqe_com, 1);
8245 bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
James Smartff78d8f2011-12-13 13:21:35 -05008246 phba->vpi_ids[phba->pport->vpi]);
8247 }
James Smart4f774512009-05-22 14:52:35 -04008248 command_type = OTHER_COMMAND;
James Smart7851fe22011-07-22 18:36:52 -04008249 break;
James Smart4f774512009-05-22 14:52:35 -04008250 case CMD_CLOSE_XRI_CN:
8251 case CMD_ABORT_XRI_CN:
8252 case CMD_ABORT_XRI_CX:
8253 /* words 0-2 memcpy should be 0 rserved */
8254 /* port will send abts */
James Smartdcf2a4e2010-09-29 11:18:53 -04008255 abrt_iotag = iocbq->iocb.un.acxri.abortContextTag;
8256 if (abrt_iotag != 0 && abrt_iotag <= phba->sli.last_iotag) {
8257 abrtiocbq = phba->sli.iocbq_lookup[abrt_iotag];
8258 fip = abrtiocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK;
8259 } else
8260 fip = 0;
8261
8262 if ((iocbq->iocb.ulpCommand == CMD_CLOSE_XRI_CN) || fip)
James Smart4f774512009-05-22 14:52:35 -04008263 /*
James Smartdcf2a4e2010-09-29 11:18:53 -04008264 * The link is down, or the command was ELS_FIP
8265 * so the fw does not need to send abts
James Smart4f774512009-05-22 14:52:35 -04008266 * on the wire.
8267 */
8268 bf_set(abort_cmd_ia, &wqe->abort_cmd, 1);
8269 else
8270 bf_set(abort_cmd_ia, &wqe->abort_cmd, 0);
8271 bf_set(abort_cmd_criteria, &wqe->abort_cmd, T_XRI_TAG);
James Smartf0d9bcc2010-10-22 11:07:09 -04008272 /* word5 iocb=CONTEXT_TAG|IO_TAG wqe=reserved */
8273 wqe->abort_cmd.rsrvd5 = 0;
8274 bf_set(wqe_ct, &wqe->abort_cmd.wqe_com,
James Smart4f774512009-05-22 14:52:35 -04008275 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
8276 abort_tag = iocbq->iocb.un.acxri.abortIoTag;
James Smart4f774512009-05-22 14:52:35 -04008277 /*
8278 * The abort handler will send us CMD_ABORT_XRI_CN or
8279 * CMD_CLOSE_XRI_CN and the fw only accepts CMD_ABORT_XRI_CX
8280 */
James Smartf0d9bcc2010-10-22 11:07:09 -04008281 bf_set(wqe_cmnd, &wqe->abort_cmd.wqe_com, CMD_ABORT_XRI_CX);
8282 bf_set(wqe_qosd, &wqe->abort_cmd.wqe_com, 1);
8283 bf_set(wqe_lenloc, &wqe->abort_cmd.wqe_com,
8284 LPFC_WQE_LENLOC_NONE);
James Smart4f774512009-05-22 14:52:35 -04008285 cmnd = CMD_ABORT_XRI_CX;
8286 command_type = OTHER_COMMAND;
8287 xritag = 0;
James Smart7851fe22011-07-22 18:36:52 -04008288 break;
James Smart6669f9b2009-10-02 15:16:45 -04008289 case CMD_XMIT_BLS_RSP64_CX:
James Smart6b5151f2012-01-18 16:24:06 -05008290 ndlp = (struct lpfc_nodelist *)iocbq->context1;
James Smart546fc852011-03-11 16:06:29 -05008291 /* As BLS ABTS RSP WQE is very different from other WQEs,
James Smart6669f9b2009-10-02 15:16:45 -04008292 * we re-construct this WQE here based on information in
8293 * iocbq from scratch.
8294 */
8295 memset(wqe, 0, sizeof(union lpfc_wqe));
James Smart5ffc2662009-11-18 15:39:44 -05008296 /* OX_ID is invariable to who sent ABTS to CT exchange */
James Smart6669f9b2009-10-02 15:16:45 -04008297 bf_set(xmit_bls_rsp64_oxid, &wqe->xmit_bls_rsp,
James Smart546fc852011-03-11 16:06:29 -05008298 bf_get(lpfc_abts_oxid, &iocbq->iocb.un.bls_rsp));
8299 if (bf_get(lpfc_abts_orig, &iocbq->iocb.un.bls_rsp) ==
James Smart5ffc2662009-11-18 15:39:44 -05008300 LPFC_ABTS_UNSOL_INT) {
8301 /* ABTS sent by initiator to CT exchange, the
8302 * RX_ID field will be filled with the newly
8303 * allocated responder XRI.
8304 */
8305 bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
8306 iocbq->sli4_xritag);
8307 } else {
8308 /* ABTS sent by responder to CT exchange, the
8309 * RX_ID field will be filled with the responder
8310 * RX_ID from ABTS.
8311 */
8312 bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
James Smart546fc852011-03-11 16:06:29 -05008313 bf_get(lpfc_abts_rxid, &iocbq->iocb.un.bls_rsp));
James Smart5ffc2662009-11-18 15:39:44 -05008314 }
James Smart6669f9b2009-10-02 15:16:45 -04008315 bf_set(xmit_bls_rsp64_seqcnthi, &wqe->xmit_bls_rsp, 0xffff);
8316 bf_set(wqe_xmit_bls_pt, &wqe->xmit_bls_rsp.wqe_dest, 0x1);
James Smart6b5151f2012-01-18 16:24:06 -05008317
8318 /* Use CT=VPI */
8319 bf_set(wqe_els_did, &wqe->xmit_bls_rsp.wqe_dest,
8320 ndlp->nlp_DID);
8321 bf_set(xmit_bls_rsp64_temprpi, &wqe->xmit_bls_rsp,
8322 iocbq->iocb.ulpContext);
8323 bf_set(wqe_ct, &wqe->xmit_bls_rsp.wqe_com, 1);
James Smart6669f9b2009-10-02 15:16:45 -04008324 bf_set(wqe_ctxt_tag, &wqe->xmit_bls_rsp.wqe_com,
James Smart6b5151f2012-01-18 16:24:06 -05008325 phba->vpi_ids[phba->pport->vpi]);
James Smartf0d9bcc2010-10-22 11:07:09 -04008326 bf_set(wqe_qosd, &wqe->xmit_bls_rsp.wqe_com, 1);
8327 bf_set(wqe_lenloc, &wqe->xmit_bls_rsp.wqe_com,
8328 LPFC_WQE_LENLOC_NONE);
James Smart6669f9b2009-10-02 15:16:45 -04008329 /* Overwrite the pre-set comnd type with OTHER_COMMAND */
8330 command_type = OTHER_COMMAND;
James Smart546fc852011-03-11 16:06:29 -05008331 if (iocbq->iocb.un.xseq64.w5.hcsw.Rctl == FC_RCTL_BA_RJT) {
8332 bf_set(xmit_bls_rsp64_rjt_vspec, &wqe->xmit_bls_rsp,
8333 bf_get(lpfc_vndr_code, &iocbq->iocb.un.bls_rsp));
8334 bf_set(xmit_bls_rsp64_rjt_expc, &wqe->xmit_bls_rsp,
8335 bf_get(lpfc_rsn_expln, &iocbq->iocb.un.bls_rsp));
8336 bf_set(xmit_bls_rsp64_rjt_rsnc, &wqe->xmit_bls_rsp,
8337 bf_get(lpfc_rsn_code, &iocbq->iocb.un.bls_rsp));
8338 }
8339
James Smart7851fe22011-07-22 18:36:52 -04008340 break;
James Smart4f774512009-05-22 14:52:35 -04008341 case CMD_XRI_ABORTED_CX:
8342 case CMD_CREATE_XRI_CR: /* Do we expect to use this? */
James Smart4f774512009-05-22 14:52:35 -04008343 case CMD_IOCB_FCP_IBIDIR64_CR: /* bidirectional xfer */
8344 case CMD_FCP_TSEND64_CX: /* Target mode send xfer-ready */
8345 case CMD_FCP_TRSP64_CX: /* Target mode rcv */
8346 case CMD_FCP_AUTO_TRSP_CX: /* Auto target rsp */
8347 default:
8348 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8349 "2014 Invalid command 0x%x\n",
8350 iocbq->iocb.ulpCommand);
8351 return IOCB_ERROR;
James Smart7851fe22011-07-22 18:36:52 -04008352 break;
James Smart4f774512009-05-22 14:52:35 -04008353 }
James Smart6d368e52011-05-24 11:44:12 -04008354
James Smart8012cc32012-10-31 14:44:49 -04008355 if (iocbq->iocb_flag & LPFC_IO_DIF_PASS)
8356 bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_PASSTHRU);
8357 else if (iocbq->iocb_flag & LPFC_IO_DIF_STRIP)
8358 bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_STRIP);
8359 else if (iocbq->iocb_flag & LPFC_IO_DIF_INSERT)
8360 bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_INSERT);
8361 iocbq->iocb_flag &= ~(LPFC_IO_DIF_PASS | LPFC_IO_DIF_STRIP |
8362 LPFC_IO_DIF_INSERT);
James Smartf0d9bcc2010-10-22 11:07:09 -04008363 bf_set(wqe_xri_tag, &wqe->generic.wqe_com, xritag);
8364 bf_set(wqe_reqtag, &wqe->generic.wqe_com, iocbq->iotag);
8365 wqe->generic.wqe_com.abort_tag = abort_tag;
8366 bf_set(wqe_cmd_type, &wqe->generic.wqe_com, command_type);
8367 bf_set(wqe_cmnd, &wqe->generic.wqe_com, cmnd);
8368 bf_set(wqe_class, &wqe->generic.wqe_com, iocbq->iocb.ulpClass);
8369 bf_set(wqe_cqid, &wqe->generic.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
James Smart4f774512009-05-22 14:52:35 -04008370 return 0;
8371}
8372
8373/**
8374 * __lpfc_sli_issue_iocb_s4 - SLI4 device lockless ver of lpfc_sli_issue_iocb
8375 * @phba: Pointer to HBA context object.
8376 * @ring_number: SLI ring number to issue iocb on.
8377 * @piocb: Pointer to command iocb.
8378 * @flag: Flag indicating if this command can be put into txq.
8379 *
8380 * __lpfc_sli_issue_iocb_s4 is used by other functions in the driver to issue
8381 * an iocb command to an HBA with SLI-4 interface spec.
8382 *
8383 * This function is called with hbalock held. The function will return success
8384 * after it successfully submit the iocb to firmware or after adding to the
8385 * txq.
8386 **/
8387static int
8388__lpfc_sli_issue_iocb_s4(struct lpfc_hba *phba, uint32_t ring_number,
8389 struct lpfc_iocbq *piocb, uint32_t flag)
8390{
8391 struct lpfc_sglq *sglq;
James Smart4f774512009-05-22 14:52:35 -04008392 union lpfc_wqe wqe;
8393 struct lpfc_sli_ring *pring = &phba->sli.ring[ring_number];
James Smart4f774512009-05-22 14:52:35 -04008394
8395 if (piocb->sli4_xritag == NO_XRI) {
8396 if (piocb->iocb.ulpCommand == CMD_ABORT_XRI_CN ||
James Smart6b5151f2012-01-18 16:24:06 -05008397 piocb->iocb.ulpCommand == CMD_CLOSE_XRI_CN)
James Smart4f774512009-05-22 14:52:35 -04008398 sglq = NULL;
8399 else {
James Smart0e9bb8d2013-03-01 16:35:12 -05008400 if (!list_empty(&pring->txq)) {
James Smart2a9bf3d2010-06-07 15:24:45 -04008401 if (!(flag & SLI_IOCB_RET_IOCB)) {
8402 __lpfc_sli_ringtx_put(phba,
8403 pring, piocb);
8404 return IOCB_SUCCESS;
8405 } else {
8406 return IOCB_BUSY;
8407 }
8408 } else {
James Smart6d368e52011-05-24 11:44:12 -04008409 sglq = __lpfc_sli_get_sglq(phba, piocb);
James Smart2a9bf3d2010-06-07 15:24:45 -04008410 if (!sglq) {
8411 if (!(flag & SLI_IOCB_RET_IOCB)) {
8412 __lpfc_sli_ringtx_put(phba,
8413 pring,
8414 piocb);
8415 return IOCB_SUCCESS;
8416 } else
8417 return IOCB_BUSY;
8418 }
8419 }
James Smart4f774512009-05-22 14:52:35 -04008420 }
8421 } else if (piocb->iocb_flag & LPFC_IO_FCP) {
James Smart6d368e52011-05-24 11:44:12 -04008422 /* These IO's already have an XRI and a mapped sgl. */
8423 sglq = NULL;
James Smart4f774512009-05-22 14:52:35 -04008424 } else {
James Smart6d368e52011-05-24 11:44:12 -04008425 /*
8426 * This is a continuation of a commandi,(CX) so this
James Smart4f774512009-05-22 14:52:35 -04008427 * sglq is on the active list
8428 */
James Smartedccdc12013-01-03 15:43:45 -05008429 sglq = __lpfc_get_active_sglq(phba, piocb->sli4_lxritag);
James Smart4f774512009-05-22 14:52:35 -04008430 if (!sglq)
8431 return IOCB_ERROR;
8432 }
8433
8434 if (sglq) {
James Smart6d368e52011-05-24 11:44:12 -04008435 piocb->sli4_lxritag = sglq->sli4_lxritag;
James Smart2a9bf3d2010-06-07 15:24:45 -04008436 piocb->sli4_xritag = sglq->sli4_xritag;
James Smart2a9bf3d2010-06-07 15:24:45 -04008437 if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocb, sglq))
James Smart4f774512009-05-22 14:52:35 -04008438 return IOCB_ERROR;
8439 }
8440
8441 if (lpfc_sli4_iocb2wqe(phba, piocb, &wqe))
8442 return IOCB_ERROR;
8443
James Smart341af102010-01-26 23:07:37 -05008444 if ((piocb->iocb_flag & LPFC_IO_FCP) ||
8445 (piocb->iocb_flag & LPFC_USE_FCPWQIDX)) {
James Smart5ffc2662009-11-18 15:39:44 -05008446 if (lpfc_sli4_wq_put(phba->sli4_hba.fcp_wq[piocb->fcp_wqidx],
8447 &wqe))
James Smart4f774512009-05-22 14:52:35 -04008448 return IOCB_ERROR;
8449 } else {
8450 if (lpfc_sli4_wq_put(phba->sli4_hba.els_wq, &wqe))
8451 return IOCB_ERROR;
8452 }
8453 lpfc_sli_ringtxcmpl_put(phba, pring, piocb);
8454
8455 return 0;
8456}
8457
8458/**
James Smart3772a992009-05-22 14:50:54 -04008459 * __lpfc_sli_issue_iocb - Wrapper func of lockless version for issuing iocb
8460 *
8461 * This routine wraps the actual lockless version for issusing IOCB function
8462 * pointer from the lpfc_hba struct.
8463 *
8464 * Return codes:
8465 * IOCB_ERROR - Error
8466 * IOCB_SUCCESS - Success
8467 * IOCB_BUSY - Busy
8468 **/
James Smart2a9bf3d2010-06-07 15:24:45 -04008469int
James Smart3772a992009-05-22 14:50:54 -04008470__lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
8471 struct lpfc_iocbq *piocb, uint32_t flag)
8472{
8473 return phba->__lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
8474}
8475
8476/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03008477 * lpfc_sli_api_table_setup - Set up sli api function jump table
James Smart3772a992009-05-22 14:50:54 -04008478 * @phba: The hba struct for which this call is being executed.
8479 * @dev_grp: The HBA PCI-Device group number.
8480 *
8481 * This routine sets up the SLI interface API function jump table in @phba
8482 * struct.
8483 * Returns: 0 - success, -ENODEV - failure.
8484 **/
8485int
8486lpfc_sli_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
8487{
8488
8489 switch (dev_grp) {
8490 case LPFC_PCI_DEV_LP:
8491 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s3;
8492 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s3;
8493 break;
James Smart4f774512009-05-22 14:52:35 -04008494 case LPFC_PCI_DEV_OC:
8495 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s4;
8496 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s4;
8497 break;
James Smart3772a992009-05-22 14:50:54 -04008498 default:
8499 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8500 "1419 Invalid HBA PCI-device group: 0x%x\n",
8501 dev_grp);
8502 return -ENODEV;
8503 break;
8504 }
8505 phba->lpfc_get_iocb_from_iocbq = lpfc_get_iocb_from_iocbq;
8506 return 0;
8507}
James Smart92d7f7b2007-06-17 19:56:38 -05008508
James Smarte59058c2008-08-24 21:49:00 -04008509/**
James Smart3621a712009-04-06 18:47:14 -04008510 * lpfc_sli_issue_iocb - Wrapper function for __lpfc_sli_issue_iocb
James Smarte59058c2008-08-24 21:49:00 -04008511 * @phba: Pointer to HBA context object.
8512 * @pring: Pointer to driver SLI ring object.
8513 * @piocb: Pointer to command iocb.
8514 * @flag: Flag indicating if this command can be put into txq.
8515 *
8516 * lpfc_sli_issue_iocb is a wrapper around __lpfc_sli_issue_iocb
8517 * function. This function gets the hbalock and calls
8518 * __lpfc_sli_issue_iocb function and will return the error returned
8519 * by __lpfc_sli_issue_iocb function. This wrapper is used by
8520 * functions which do not hold hbalock.
8521 **/
James Smart92d7f7b2007-06-17 19:56:38 -05008522int
James Smart3772a992009-05-22 14:50:54 -04008523lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
James Smart92d7f7b2007-06-17 19:56:38 -05008524 struct lpfc_iocbq *piocb, uint32_t flag)
8525{
James Smartba20c852012-08-03 12:36:52 -04008526 struct lpfc_fcp_eq_hdl *fcp_eq_hdl;
James Smart2a76a282012-08-03 12:35:54 -04008527 struct lpfc_sli_ring *pring;
James Smartba20c852012-08-03 12:36:52 -04008528 struct lpfc_queue *fpeq;
8529 struct lpfc_eqe *eqe;
James Smart92d7f7b2007-06-17 19:56:38 -05008530 unsigned long iflags;
James Smart2a76a282012-08-03 12:35:54 -04008531 int rc, idx;
James Smart92d7f7b2007-06-17 19:56:38 -05008532
James Smart7e56aa22012-08-03 12:35:34 -04008533 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smart2a76a282012-08-03 12:35:54 -04008534 if (piocb->iocb_flag & LPFC_IO_FCP) {
8535 if (unlikely(!phba->sli4_hba.fcp_wq))
8536 return IOCB_ERROR;
8537 idx = lpfc_sli4_scmd_to_wqidx_distr(phba);
8538 piocb->fcp_wqidx = idx;
8539 ring_number = MAX_SLI3_CONFIGURED_RINGS + idx;
James Smart92d7f7b2007-06-17 19:56:38 -05008540
James Smartba20c852012-08-03 12:36:52 -04008541 pring = &phba->sli.ring[ring_number];
8542 spin_lock_irqsave(&pring->ring_lock, iflags);
8543 rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb,
8544 flag);
8545 spin_unlock_irqrestore(&pring->ring_lock, iflags);
8546
8547 if (lpfc_fcp_look_ahead) {
8548 fcp_eq_hdl = &phba->sli4_hba.fcp_eq_hdl[idx];
8549
8550 if (atomic_dec_and_test(&fcp_eq_hdl->
8551 fcp_eq_in_use)) {
8552
8553 /* Get associated EQ with this index */
8554 fpeq = phba->sli4_hba.hba_eq[idx];
8555
8556 /* Turn off interrupts from this EQ */
8557 lpfc_sli4_eq_clr_intr(fpeq);
8558
8559 /*
8560 * Process all the events on FCP EQ
8561 */
8562 while ((eqe = lpfc_sli4_eq_get(fpeq))) {
8563 lpfc_sli4_hba_handle_eqe(phba,
8564 eqe, idx);
8565 fpeq->EQ_processed++;
8566 }
8567
8568 /* Always clear and re-arm the EQ */
8569 lpfc_sli4_eq_release(fpeq,
8570 LPFC_QUEUE_REARM);
8571 }
8572 atomic_inc(&fcp_eq_hdl->fcp_eq_in_use);
8573 }
8574 } else {
8575 pring = &phba->sli.ring[ring_number];
8576 spin_lock_irqsave(&pring->ring_lock, iflags);
8577 rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb,
8578 flag);
8579 spin_unlock_irqrestore(&pring->ring_lock, iflags);
8580
James Smart2a76a282012-08-03 12:35:54 -04008581 }
James Smart7e56aa22012-08-03 12:35:34 -04008582 } else {
8583 /* For now, SLI2/3 will still use hbalock */
8584 spin_lock_irqsave(&phba->hbalock, iflags);
8585 rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
8586 spin_unlock_irqrestore(&phba->hbalock, iflags);
8587 }
James Smart92d7f7b2007-06-17 19:56:38 -05008588 return rc;
8589}
8590
James Smarte59058c2008-08-24 21:49:00 -04008591/**
James Smart3621a712009-04-06 18:47:14 -04008592 * lpfc_extra_ring_setup - Extra ring setup function
James Smarte59058c2008-08-24 21:49:00 -04008593 * @phba: Pointer to HBA context object.
8594 *
8595 * This function is called while driver attaches with the
8596 * HBA to setup the extra ring. The extra ring is used
8597 * only when driver needs to support target mode functionality
8598 * or IP over FC functionalities.
8599 *
8600 * This function is called with no lock held.
8601 **/
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05008602static int
8603lpfc_extra_ring_setup( struct lpfc_hba *phba)
8604{
8605 struct lpfc_sli *psli;
8606 struct lpfc_sli_ring *pring;
8607
8608 psli = &phba->sli;
8609
8610 /* Adjust cmd/rsp ring iocb entries more evenly */
James Smarta4bc3372006-12-02 13:34:16 -05008611
8612 /* Take some away from the FCP ring */
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05008613 pring = &psli->ring[psli->fcp_ring];
James Smart7e56aa22012-08-03 12:35:34 -04008614 pring->sli.sli3.numCiocb -= SLI2_IOCB_CMD_R1XTRA_ENTRIES;
8615 pring->sli.sli3.numRiocb -= SLI2_IOCB_RSP_R1XTRA_ENTRIES;
8616 pring->sli.sli3.numCiocb -= SLI2_IOCB_CMD_R3XTRA_ENTRIES;
8617 pring->sli.sli3.numRiocb -= SLI2_IOCB_RSP_R3XTRA_ENTRIES;
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05008618
James Smarta4bc3372006-12-02 13:34:16 -05008619 /* and give them to the extra ring */
8620 pring = &psli->ring[psli->extra_ring];
8621
James Smart7e56aa22012-08-03 12:35:34 -04008622 pring->sli.sli3.numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
8623 pring->sli.sli3.numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
8624 pring->sli.sli3.numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
8625 pring->sli.sli3.numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05008626
8627 /* Setup default profile for this ring */
8628 pring->iotag_max = 4096;
8629 pring->num_mask = 1;
8630 pring->prt[0].profile = 0; /* Mask 0 */
James Smarta4bc3372006-12-02 13:34:16 -05008631 pring->prt[0].rctl = phba->cfg_multi_ring_rctl;
8632 pring->prt[0].type = phba->cfg_multi_ring_type;
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05008633 pring->prt[0].lpfc_sli_rcv_unsol_event = NULL;
8634 return 0;
8635}
8636
James Smartcb69f7d2011-12-13 13:21:57 -05008637/* lpfc_sli_abts_err_handler - handle a failed ABTS request from an SLI3 port.
8638 * @phba: Pointer to HBA context object.
8639 * @iocbq: Pointer to iocb object.
8640 *
8641 * The async_event handler calls this routine when it receives
8642 * an ASYNC_STATUS_CN event from the port. The port generates
8643 * this event when an Abort Sequence request to an rport fails
8644 * twice in succession. The abort could be originated by the
8645 * driver or by the port. The ABTS could have been for an ELS
8646 * or FCP IO. The port only generates this event when an ABTS
8647 * fails to complete after one retry.
8648 */
8649static void
8650lpfc_sli_abts_err_handler(struct lpfc_hba *phba,
8651 struct lpfc_iocbq *iocbq)
8652{
8653 struct lpfc_nodelist *ndlp = NULL;
8654 uint16_t rpi = 0, vpi = 0;
8655 struct lpfc_vport *vport = NULL;
8656
8657 /* The rpi in the ulpContext is vport-sensitive. */
8658 vpi = iocbq->iocb.un.asyncstat.sub_ctxt_tag;
8659 rpi = iocbq->iocb.ulpContext;
8660
8661 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
8662 "3092 Port generated ABTS async event "
8663 "on vpi %d rpi %d status 0x%x\n",
8664 vpi, rpi, iocbq->iocb.ulpStatus);
8665
8666 vport = lpfc_find_vport_by_vpid(phba, vpi);
8667 if (!vport)
8668 goto err_exit;
8669 ndlp = lpfc_findnode_rpi(vport, rpi);
8670 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp))
8671 goto err_exit;
8672
8673 if (iocbq->iocb.ulpStatus == IOSTAT_LOCAL_REJECT)
8674 lpfc_sli_abts_recover_port(vport, ndlp);
8675 return;
8676
8677 err_exit:
8678 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
8679 "3095 Event Context not found, no "
8680 "action on vpi %d rpi %d status 0x%x, reason 0x%x\n",
8681 iocbq->iocb.ulpContext, iocbq->iocb.ulpStatus,
8682 vpi, rpi);
8683}
8684
8685/* lpfc_sli4_abts_err_handler - handle a failed ABTS request from an SLI4 port.
8686 * @phba: pointer to HBA context object.
8687 * @ndlp: nodelist pointer for the impacted rport.
8688 * @axri: pointer to the wcqe containing the failed exchange.
8689 *
8690 * The driver calls this routine when it receives an ABORT_XRI_FCP CQE from the
8691 * port. The port generates this event when an abort exchange request to an
8692 * rport fails twice in succession with no reply. The abort could be originated
8693 * by the driver or by the port. The ABTS could have been for an ELS or FCP IO.
8694 */
8695void
8696lpfc_sli4_abts_err_handler(struct lpfc_hba *phba,
8697 struct lpfc_nodelist *ndlp,
8698 struct sli4_wcqe_xri_aborted *axri)
8699{
8700 struct lpfc_vport *vport;
James Smart5c1db2a2012-03-01 22:34:36 -05008701 uint32_t ext_status = 0;
James Smartcb69f7d2011-12-13 13:21:57 -05008702
James Smart6b5151f2012-01-18 16:24:06 -05008703 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
James Smartcb69f7d2011-12-13 13:21:57 -05008704 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
8705 "3115 Node Context not found, driver "
8706 "ignoring abts err event\n");
James Smart6b5151f2012-01-18 16:24:06 -05008707 return;
8708 }
8709
James Smartcb69f7d2011-12-13 13:21:57 -05008710 vport = ndlp->vport;
8711 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
8712 "3116 Port generated FCP XRI ABORT event on "
James Smart5c1db2a2012-03-01 22:34:36 -05008713 "vpi %d rpi %d xri x%x status 0x%x parameter x%x\n",
James Smartcb69f7d2011-12-13 13:21:57 -05008714 ndlp->vport->vpi, ndlp->nlp_rpi,
8715 bf_get(lpfc_wcqe_xa_xri, axri),
James Smart5c1db2a2012-03-01 22:34:36 -05008716 bf_get(lpfc_wcqe_xa_status, axri),
8717 axri->parameter);
James Smartcb69f7d2011-12-13 13:21:57 -05008718
James Smart5c1db2a2012-03-01 22:34:36 -05008719 /*
8720 * Catch the ABTS protocol failure case. Older OCe FW releases returned
8721 * LOCAL_REJECT and 0 for a failed ABTS exchange and later OCe and
8722 * LPe FW releases returned LOCAL_REJECT and SEQUENCE_TIMEOUT.
8723 */
James Smarte3d2b802012-08-14 14:25:43 -04008724 ext_status = axri->parameter & IOERR_PARAM_MASK;
James Smart5c1db2a2012-03-01 22:34:36 -05008725 if ((bf_get(lpfc_wcqe_xa_status, axri) == IOSTAT_LOCAL_REJECT) &&
8726 ((ext_status == IOERR_SEQUENCE_TIMEOUT) || (ext_status == 0)))
James Smartcb69f7d2011-12-13 13:21:57 -05008727 lpfc_sli_abts_recover_port(vport, ndlp);
8728}
8729
James Smarte59058c2008-08-24 21:49:00 -04008730/**
James Smart3621a712009-04-06 18:47:14 -04008731 * lpfc_sli_async_event_handler - ASYNC iocb handler function
James Smarte59058c2008-08-24 21:49:00 -04008732 * @phba: Pointer to HBA context object.
8733 * @pring: Pointer to driver SLI ring object.
8734 * @iocbq: Pointer to iocb object.
8735 *
8736 * This function is called by the slow ring event handler
8737 * function when there is an ASYNC event iocb in the ring.
8738 * This function is called with no lock held.
8739 * Currently this function handles only temperature related
8740 * ASYNC events. The function decodes the temperature sensor
8741 * event message and posts events for the management applications.
8742 **/
James Smart98c9ea52007-10-27 13:37:33 -04008743static void
James Smart57127f12007-10-27 13:37:05 -04008744lpfc_sli_async_event_handler(struct lpfc_hba * phba,
8745 struct lpfc_sli_ring * pring, struct lpfc_iocbq * iocbq)
8746{
8747 IOCB_t *icmd;
8748 uint16_t evt_code;
James Smart57127f12007-10-27 13:37:05 -04008749 struct temp_event temp_event_data;
8750 struct Scsi_Host *shost;
James Smarta257bf92009-04-06 18:48:10 -04008751 uint32_t *iocb_w;
James Smart57127f12007-10-27 13:37:05 -04008752
8753 icmd = &iocbq->iocb;
8754 evt_code = icmd->un.asyncstat.evt_code;
James Smart57127f12007-10-27 13:37:05 -04008755
James Smartcb69f7d2011-12-13 13:21:57 -05008756 switch (evt_code) {
8757 case ASYNC_TEMP_WARN:
8758 case ASYNC_TEMP_SAFE:
8759 temp_event_data.data = (uint32_t) icmd->ulpContext;
8760 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
8761 if (evt_code == ASYNC_TEMP_WARN) {
8762 temp_event_data.event_code = LPFC_THRESHOLD_TEMP;
8763 lpfc_printf_log(phba, KERN_ERR, LOG_TEMP,
8764 "0347 Adapter is very hot, please take "
8765 "corrective action. temperature : %d Celsius\n",
8766 (uint32_t) icmd->ulpContext);
8767 } else {
8768 temp_event_data.event_code = LPFC_NORMAL_TEMP;
8769 lpfc_printf_log(phba, KERN_ERR, LOG_TEMP,
8770 "0340 Adapter temperature is OK now. "
8771 "temperature : %d Celsius\n",
8772 (uint32_t) icmd->ulpContext);
8773 }
8774
8775 /* Send temperature change event to applications */
8776 shost = lpfc_shost_from_vport(phba->pport);
8777 fc_host_post_vendor_event(shost, fc_get_event_number(),
8778 sizeof(temp_event_data), (char *) &temp_event_data,
8779 LPFC_NL_VENDOR_ID);
8780 break;
8781 case ASYNC_STATUS_CN:
8782 lpfc_sli_abts_err_handler(phba, iocbq);
8783 break;
8784 default:
James Smarta257bf92009-04-06 18:48:10 -04008785 iocb_w = (uint32_t *) icmd;
James Smartcb69f7d2011-12-13 13:21:57 -05008786 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart76bb24e2007-10-27 13:38:00 -04008787 "0346 Ring %d handler: unexpected ASYNC_STATUS"
James Smarte4e74272009-07-19 10:01:38 -04008788 " evt_code 0x%x\n"
James Smarta257bf92009-04-06 18:48:10 -04008789 "W0 0x%08x W1 0x%08x W2 0x%08x W3 0x%08x\n"
8790 "W4 0x%08x W5 0x%08x W6 0x%08x W7 0x%08x\n"
8791 "W8 0x%08x W9 0x%08x W10 0x%08x W11 0x%08x\n"
8792 "W12 0x%08x W13 0x%08x W14 0x%08x W15 0x%08x\n",
James Smartcb69f7d2011-12-13 13:21:57 -05008793 pring->ringno, icmd->un.asyncstat.evt_code,
James Smarta257bf92009-04-06 18:48:10 -04008794 iocb_w[0], iocb_w[1], iocb_w[2], iocb_w[3],
8795 iocb_w[4], iocb_w[5], iocb_w[6], iocb_w[7],
8796 iocb_w[8], iocb_w[9], iocb_w[10], iocb_w[11],
8797 iocb_w[12], iocb_w[13], iocb_w[14], iocb_w[15]);
8798
James Smartcb69f7d2011-12-13 13:21:57 -05008799 break;
James Smart57127f12007-10-27 13:37:05 -04008800 }
James Smart57127f12007-10-27 13:37:05 -04008801}
8802
8803
James Smarte59058c2008-08-24 21:49:00 -04008804/**
James Smart3621a712009-04-06 18:47:14 -04008805 * lpfc_sli_setup - SLI ring setup function
James Smarte59058c2008-08-24 21:49:00 -04008806 * @phba: Pointer to HBA context object.
8807 *
8808 * lpfc_sli_setup sets up rings of the SLI interface with
8809 * number of iocbs per ring and iotags. This function is
8810 * called while driver attach to the HBA and before the
8811 * interrupts are enabled. So there is no need for locking.
8812 *
8813 * This function always returns 0.
8814 **/
dea31012005-04-17 16:05:31 -05008815int
8816lpfc_sli_setup(struct lpfc_hba *phba)
8817{
James Smarted957682007-06-17 19:56:37 -05008818 int i, totiocbsize = 0;
dea31012005-04-17 16:05:31 -05008819 struct lpfc_sli *psli = &phba->sli;
8820 struct lpfc_sli_ring *pring;
8821
James Smart2a76a282012-08-03 12:35:54 -04008822 psli->num_rings = MAX_SLI3_CONFIGURED_RINGS;
8823 if (phba->sli_rev == LPFC_SLI_REV4)
James Smart67d12732012-08-03 12:36:13 -04008824 psli->num_rings += phba->cfg_fcp_io_channel;
dea31012005-04-17 16:05:31 -05008825 psli->sli_flag = 0;
8826 psli->fcp_ring = LPFC_FCP_RING;
8827 psli->next_ring = LPFC_FCP_NEXT_RING;
James Smarta4bc3372006-12-02 13:34:16 -05008828 psli->extra_ring = LPFC_EXTRA_RING;
dea31012005-04-17 16:05:31 -05008829
James Bottomley604a3e32005-10-29 10:28:33 -05008830 psli->iocbq_lookup = NULL;
8831 psli->iocbq_lookup_len = 0;
8832 psli->last_iotag = 0;
8833
dea31012005-04-17 16:05:31 -05008834 for (i = 0; i < psli->num_rings; i++) {
8835 pring = &psli->ring[i];
8836 switch (i) {
8837 case LPFC_FCP_RING: /* ring 0 - FCP */
8838 /* numCiocb and numRiocb are used in config_port */
James Smart7e56aa22012-08-03 12:35:34 -04008839 pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R0_ENTRIES;
8840 pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R0_ENTRIES;
8841 pring->sli.sli3.numCiocb +=
8842 SLI2_IOCB_CMD_R1XTRA_ENTRIES;
8843 pring->sli.sli3.numRiocb +=
8844 SLI2_IOCB_RSP_R1XTRA_ENTRIES;
8845 pring->sli.sli3.numCiocb +=
8846 SLI2_IOCB_CMD_R3XTRA_ENTRIES;
8847 pring->sli.sli3.numRiocb +=
8848 SLI2_IOCB_RSP_R3XTRA_ENTRIES;
8849 pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05008850 SLI3_IOCB_CMD_SIZE :
8851 SLI2_IOCB_CMD_SIZE;
James Smart7e56aa22012-08-03 12:35:34 -04008852 pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05008853 SLI3_IOCB_RSP_SIZE :
8854 SLI2_IOCB_RSP_SIZE;
dea31012005-04-17 16:05:31 -05008855 pring->iotag_ctr = 0;
8856 pring->iotag_max =
James Smart92d7f7b2007-06-17 19:56:38 -05008857 (phba->cfg_hba_queue_depth * 2);
dea31012005-04-17 16:05:31 -05008858 pring->fast_iotag = pring->iotag_max;
8859 pring->num_mask = 0;
8860 break;
James Smarta4bc3372006-12-02 13:34:16 -05008861 case LPFC_EXTRA_RING: /* ring 1 - EXTRA */
dea31012005-04-17 16:05:31 -05008862 /* numCiocb and numRiocb are used in config_port */
James Smart7e56aa22012-08-03 12:35:34 -04008863 pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R1_ENTRIES;
8864 pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R1_ENTRIES;
8865 pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05008866 SLI3_IOCB_CMD_SIZE :
8867 SLI2_IOCB_CMD_SIZE;
James Smart7e56aa22012-08-03 12:35:34 -04008868 pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05008869 SLI3_IOCB_RSP_SIZE :
8870 SLI2_IOCB_RSP_SIZE;
James Smart2e0fef82007-06-17 19:56:36 -05008871 pring->iotag_max = phba->cfg_hba_queue_depth;
dea31012005-04-17 16:05:31 -05008872 pring->num_mask = 0;
8873 break;
8874 case LPFC_ELS_RING: /* ring 2 - ELS / CT */
8875 /* numCiocb and numRiocb are used in config_port */
James Smart7e56aa22012-08-03 12:35:34 -04008876 pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R2_ENTRIES;
8877 pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R2_ENTRIES;
8878 pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05008879 SLI3_IOCB_CMD_SIZE :
8880 SLI2_IOCB_CMD_SIZE;
James Smart7e56aa22012-08-03 12:35:34 -04008881 pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05008882 SLI3_IOCB_RSP_SIZE :
8883 SLI2_IOCB_RSP_SIZE;
dea31012005-04-17 16:05:31 -05008884 pring->fast_iotag = 0;
8885 pring->iotag_ctr = 0;
8886 pring->iotag_max = 4096;
James Smart57127f12007-10-27 13:37:05 -04008887 pring->lpfc_sli_rcv_async_status =
8888 lpfc_sli_async_event_handler;
James Smart6669f9b2009-10-02 15:16:45 -04008889 pring->num_mask = LPFC_MAX_RING_MASK;
dea31012005-04-17 16:05:31 -05008890 pring->prt[0].profile = 0; /* Mask 0 */
James Smart6a9c52c2009-10-02 15:16:51 -04008891 pring->prt[0].rctl = FC_RCTL_ELS_REQ;
8892 pring->prt[0].type = FC_TYPE_ELS;
dea31012005-04-17 16:05:31 -05008893 pring->prt[0].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05008894 lpfc_els_unsol_event;
dea31012005-04-17 16:05:31 -05008895 pring->prt[1].profile = 0; /* Mask 1 */
James Smart6a9c52c2009-10-02 15:16:51 -04008896 pring->prt[1].rctl = FC_RCTL_ELS_REP;
8897 pring->prt[1].type = FC_TYPE_ELS;
dea31012005-04-17 16:05:31 -05008898 pring->prt[1].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05008899 lpfc_els_unsol_event;
dea31012005-04-17 16:05:31 -05008900 pring->prt[2].profile = 0; /* Mask 2 */
8901 /* NameServer Inquiry */
James Smart6a9c52c2009-10-02 15:16:51 -04008902 pring->prt[2].rctl = FC_RCTL_DD_UNSOL_CTL;
dea31012005-04-17 16:05:31 -05008903 /* NameServer */
James Smart6a9c52c2009-10-02 15:16:51 -04008904 pring->prt[2].type = FC_TYPE_CT;
dea31012005-04-17 16:05:31 -05008905 pring->prt[2].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05008906 lpfc_ct_unsol_event;
dea31012005-04-17 16:05:31 -05008907 pring->prt[3].profile = 0; /* Mask 3 */
8908 /* NameServer response */
James Smart6a9c52c2009-10-02 15:16:51 -04008909 pring->prt[3].rctl = FC_RCTL_DD_SOL_CTL;
dea31012005-04-17 16:05:31 -05008910 /* NameServer */
James Smart6a9c52c2009-10-02 15:16:51 -04008911 pring->prt[3].type = FC_TYPE_CT;
dea31012005-04-17 16:05:31 -05008912 pring->prt[3].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05008913 lpfc_ct_unsol_event;
dea31012005-04-17 16:05:31 -05008914 break;
8915 }
James Smart7e56aa22012-08-03 12:35:34 -04008916 totiocbsize += (pring->sli.sli3.numCiocb *
8917 pring->sli.sli3.sizeCiocb) +
8918 (pring->sli.sli3.numRiocb * pring->sli.sli3.sizeRiocb);
dea31012005-04-17 16:05:31 -05008919 }
James Smarted957682007-06-17 19:56:37 -05008920 if (totiocbsize > MAX_SLIM_IOCB_SIZE) {
dea31012005-04-17 16:05:31 -05008921 /* Too many cmd / rsp ring entries in SLI2 SLIM */
James Smarte8b62012007-08-02 11:10:09 -04008922 printk(KERN_ERR "%d:0462 Too many cmd / rsp ring entries in "
8923 "SLI2 SLIM Data: x%x x%lx\n",
8924 phba->brd_no, totiocbsize,
8925 (unsigned long) MAX_SLIM_IOCB_SIZE);
dea31012005-04-17 16:05:31 -05008926 }
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05008927 if (phba->cfg_multi_ring_support == 2)
8928 lpfc_extra_ring_setup(phba);
dea31012005-04-17 16:05:31 -05008929
8930 return 0;
8931}
8932
James Smarte59058c2008-08-24 21:49:00 -04008933/**
James Smart3621a712009-04-06 18:47:14 -04008934 * lpfc_sli_queue_setup - Queue initialization function
James Smarte59058c2008-08-24 21:49:00 -04008935 * @phba: Pointer to HBA context object.
8936 *
8937 * lpfc_sli_queue_setup sets up mailbox queues and iocb queues for each
8938 * ring. This function also initializes ring indices of each ring.
8939 * This function is called during the initialization of the SLI
8940 * interface of an HBA.
8941 * This function is called with no lock held and always returns
8942 * 1.
8943 **/
dea31012005-04-17 16:05:31 -05008944int
James Smart2e0fef82007-06-17 19:56:36 -05008945lpfc_sli_queue_setup(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05008946{
8947 struct lpfc_sli *psli;
8948 struct lpfc_sli_ring *pring;
James Bottomley604a3e32005-10-29 10:28:33 -05008949 int i;
dea31012005-04-17 16:05:31 -05008950
8951 psli = &phba->sli;
James Smart2e0fef82007-06-17 19:56:36 -05008952 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05008953 INIT_LIST_HEAD(&psli->mboxq);
James Smart92d7f7b2007-06-17 19:56:38 -05008954 INIT_LIST_HEAD(&psli->mboxq_cmpl);
dea31012005-04-17 16:05:31 -05008955 /* Initialize list headers for txq and txcmplq as double linked lists */
8956 for (i = 0; i < psli->num_rings; i++) {
8957 pring = &psli->ring[i];
8958 pring->ringno = i;
James Smart7e56aa22012-08-03 12:35:34 -04008959 pring->sli.sli3.next_cmdidx = 0;
8960 pring->sli.sli3.local_getidx = 0;
8961 pring->sli.sli3.cmdidx = 0;
dea31012005-04-17 16:05:31 -05008962 INIT_LIST_HEAD(&pring->txq);
8963 INIT_LIST_HEAD(&pring->txcmplq);
8964 INIT_LIST_HEAD(&pring->iocb_continueq);
James Smart9c2face2008-01-11 01:53:18 -05008965 INIT_LIST_HEAD(&pring->iocb_continue_saveq);
dea31012005-04-17 16:05:31 -05008966 INIT_LIST_HEAD(&pring->postbufq);
James Smart7e56aa22012-08-03 12:35:34 -04008967 spin_lock_init(&pring->ring_lock);
dea31012005-04-17 16:05:31 -05008968 }
James Smart2e0fef82007-06-17 19:56:36 -05008969 spin_unlock_irq(&phba->hbalock);
8970 return 1;
dea31012005-04-17 16:05:31 -05008971}
8972
James Smarte59058c2008-08-24 21:49:00 -04008973/**
James Smart04c68492009-05-22 14:52:52 -04008974 * lpfc_sli_mbox_sys_flush - Flush mailbox command sub-system
8975 * @phba: Pointer to HBA context object.
8976 *
8977 * This routine flushes the mailbox command subsystem. It will unconditionally
8978 * flush all the mailbox commands in the three possible stages in the mailbox
8979 * command sub-system: pending mailbox command queue; the outstanding mailbox
8980 * command; and completed mailbox command queue. It is caller's responsibility
8981 * to make sure that the driver is in the proper state to flush the mailbox
8982 * command sub-system. Namely, the posting of mailbox commands into the
8983 * pending mailbox command queue from the various clients must be stopped;
8984 * either the HBA is in a state that it will never works on the outstanding
8985 * mailbox command (such as in EEH or ERATT conditions) or the outstanding
8986 * mailbox command has been completed.
8987 **/
8988static void
8989lpfc_sli_mbox_sys_flush(struct lpfc_hba *phba)
8990{
8991 LIST_HEAD(completions);
8992 struct lpfc_sli *psli = &phba->sli;
8993 LPFC_MBOXQ_t *pmb;
8994 unsigned long iflag;
8995
8996 /* Flush all the mailbox commands in the mbox system */
8997 spin_lock_irqsave(&phba->hbalock, iflag);
8998 /* The pending mailbox command queue */
8999 list_splice_init(&phba->sli.mboxq, &completions);
9000 /* The outstanding active mailbox command */
9001 if (psli->mbox_active) {
9002 list_add_tail(&psli->mbox_active->list, &completions);
9003 psli->mbox_active = NULL;
9004 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
9005 }
9006 /* The completed mailbox command queue */
9007 list_splice_init(&phba->sli.mboxq_cmpl, &completions);
9008 spin_unlock_irqrestore(&phba->hbalock, iflag);
9009
9010 /* Return all flushed mailbox commands with MBX_NOT_FINISHED status */
9011 while (!list_empty(&completions)) {
9012 list_remove_head(&completions, pmb, LPFC_MBOXQ_t, list);
9013 pmb->u.mb.mbxStatus = MBX_NOT_FINISHED;
9014 if (pmb->mbox_cmpl)
9015 pmb->mbox_cmpl(phba, pmb);
9016 }
9017}
9018
9019/**
James Smart3621a712009-04-06 18:47:14 -04009020 * lpfc_sli_host_down - Vport cleanup function
James Smarte59058c2008-08-24 21:49:00 -04009021 * @vport: Pointer to virtual port object.
9022 *
9023 * lpfc_sli_host_down is called to clean up the resources
9024 * associated with a vport before destroying virtual
9025 * port data structures.
9026 * This function does following operations:
9027 * - Free discovery resources associated with this virtual
9028 * port.
9029 * - Free iocbs associated with this virtual port in
9030 * the txq.
9031 * - Send abort for all iocb commands associated with this
9032 * vport in txcmplq.
9033 *
9034 * This function is called with no lock held and always returns 1.
9035 **/
dea31012005-04-17 16:05:31 -05009036int
James Smart92d7f7b2007-06-17 19:56:38 -05009037lpfc_sli_host_down(struct lpfc_vport *vport)
9038{
James Smart858c9f62007-06-17 19:56:39 -05009039 LIST_HEAD(completions);
James Smart92d7f7b2007-06-17 19:56:38 -05009040 struct lpfc_hba *phba = vport->phba;
9041 struct lpfc_sli *psli = &phba->sli;
9042 struct lpfc_sli_ring *pring;
9043 struct lpfc_iocbq *iocb, *next_iocb;
James Smart92d7f7b2007-06-17 19:56:38 -05009044 int i;
9045 unsigned long flags = 0;
9046 uint16_t prev_pring_flag;
9047
9048 lpfc_cleanup_discovery_resources(vport);
9049
9050 spin_lock_irqsave(&phba->hbalock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -05009051 for (i = 0; i < psli->num_rings; i++) {
9052 pring = &psli->ring[i];
9053 prev_pring_flag = pring->flag;
James Smart5e9d9b82008-06-14 22:52:53 -04009054 /* Only slow rings */
9055 if (pring->ringno == LPFC_ELS_RING) {
James Smart858c9f62007-06-17 19:56:39 -05009056 pring->flag |= LPFC_DEFERRED_RING_EVENT;
James Smart5e9d9b82008-06-14 22:52:53 -04009057 /* Set the lpfc data pending flag */
9058 set_bit(LPFC_DATA_READY, &phba->data_flags);
9059 }
James Smart92d7f7b2007-06-17 19:56:38 -05009060 /*
9061 * Error everything on the txq since these iocbs have not been
9062 * given to the FW yet.
9063 */
James Smart92d7f7b2007-06-17 19:56:38 -05009064 list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
9065 if (iocb->vport != vport)
9066 continue;
James Smart858c9f62007-06-17 19:56:39 -05009067 list_move_tail(&iocb->list, &completions);
James Smart92d7f7b2007-06-17 19:56:38 -05009068 }
9069
9070 /* Next issue ABTS for everything on the txcmplq */
9071 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq,
9072 list) {
9073 if (iocb->vport != vport)
9074 continue;
9075 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
9076 }
9077
9078 pring->flag = prev_pring_flag;
9079 }
9080
9081 spin_unlock_irqrestore(&phba->hbalock, flags);
9082
James Smarta257bf92009-04-06 18:48:10 -04009083 /* Cancel all the IOCBs from the completions list */
9084 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
9085 IOERR_SLI_DOWN);
James Smart92d7f7b2007-06-17 19:56:38 -05009086 return 1;
9087}
9088
James Smarte59058c2008-08-24 21:49:00 -04009089/**
James Smart3621a712009-04-06 18:47:14 -04009090 * lpfc_sli_hba_down - Resource cleanup function for the HBA
James Smarte59058c2008-08-24 21:49:00 -04009091 * @phba: Pointer to HBA context object.
9092 *
9093 * This function cleans up all iocb, buffers, mailbox commands
9094 * while shutting down the HBA. This function is called with no
9095 * lock held and always returns 1.
9096 * This function does the following to cleanup driver resources:
9097 * - Free discovery resources for each virtual port
9098 * - Cleanup any pending fabric iocbs
9099 * - Iterate through the iocb txq and free each entry
9100 * in the list.
9101 * - Free up any buffer posted to the HBA
9102 * - Free mailbox commands in the mailbox queue.
9103 **/
James Smart92d7f7b2007-06-17 19:56:38 -05009104int
James Smart2e0fef82007-06-17 19:56:36 -05009105lpfc_sli_hba_down(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05009106{
James Smart2534ba72007-04-25 09:52:20 -04009107 LIST_HEAD(completions);
James Smart2e0fef82007-06-17 19:56:36 -05009108 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05009109 struct lpfc_sli_ring *pring;
James Smart0ff10d42008-01-11 01:52:36 -05009110 struct lpfc_dmabuf *buf_ptr;
dea31012005-04-17 16:05:31 -05009111 unsigned long flags = 0;
James Smart04c68492009-05-22 14:52:52 -04009112 int i;
9113
9114 /* Shutdown the mailbox command sub-system */
James Smart618a5232012-06-12 13:54:36 -04009115 lpfc_sli_mbox_sys_shutdown(phba, LPFC_MBX_WAIT);
dea31012005-04-17 16:05:31 -05009116
dea31012005-04-17 16:05:31 -05009117 lpfc_hba_down_prep(phba);
9118
James Smart92d7f7b2007-06-17 19:56:38 -05009119 lpfc_fabric_abort_hba(phba);
9120
James Smart2e0fef82007-06-17 19:56:36 -05009121 spin_lock_irqsave(&phba->hbalock, flags);
dea31012005-04-17 16:05:31 -05009122 for (i = 0; i < psli->num_rings; i++) {
9123 pring = &psli->ring[i];
James Smart5e9d9b82008-06-14 22:52:53 -04009124 /* Only slow rings */
9125 if (pring->ringno == LPFC_ELS_RING) {
James Smart858c9f62007-06-17 19:56:39 -05009126 pring->flag |= LPFC_DEFERRED_RING_EVENT;
James Smart5e9d9b82008-06-14 22:52:53 -04009127 /* Set the lpfc data pending flag */
9128 set_bit(LPFC_DATA_READY, &phba->data_flags);
9129 }
dea31012005-04-17 16:05:31 -05009130
9131 /*
9132 * Error everything on the txq since these iocbs have not been
9133 * given to the FW yet.
9134 */
James Smart2534ba72007-04-25 09:52:20 -04009135 list_splice_init(&pring->txq, &completions);
dea31012005-04-17 16:05:31 -05009136 }
James Smart2e0fef82007-06-17 19:56:36 -05009137 spin_unlock_irqrestore(&phba->hbalock, flags);
dea31012005-04-17 16:05:31 -05009138
James Smarta257bf92009-04-06 18:48:10 -04009139 /* Cancel all the IOCBs from the completions list */
9140 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
9141 IOERR_SLI_DOWN);
James Smart2534ba72007-04-25 09:52:20 -04009142
James Smart0ff10d42008-01-11 01:52:36 -05009143 spin_lock_irqsave(&phba->hbalock, flags);
9144 list_splice_init(&phba->elsbuf, &completions);
9145 phba->elsbuf_cnt = 0;
9146 phba->elsbuf_prev_cnt = 0;
9147 spin_unlock_irqrestore(&phba->hbalock, flags);
9148
9149 while (!list_empty(&completions)) {
9150 list_remove_head(&completions, buf_ptr,
9151 struct lpfc_dmabuf, list);
9152 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
9153 kfree(buf_ptr);
9154 }
9155
dea31012005-04-17 16:05:31 -05009156 /* Return any active mbox cmds */
9157 del_timer_sync(&psli->mbox_tmo);
James Smart92d7f7b2007-06-17 19:56:38 -05009158
James Smartda0436e2009-05-22 14:51:39 -04009159 spin_lock_irqsave(&phba->pport->work_port_lock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -05009160 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
James Smartda0436e2009-05-22 14:51:39 -04009161 spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -05009162
James Smartda0436e2009-05-22 14:51:39 -04009163 return 1;
9164}
James Smart92d7f7b2007-06-17 19:56:38 -05009165
James Smartda0436e2009-05-22 14:51:39 -04009166/**
James Smart3621a712009-04-06 18:47:14 -04009167 * lpfc_sli_pcimem_bcopy - SLI memory copy function
James Smarte59058c2008-08-24 21:49:00 -04009168 * @srcp: Source memory pointer.
9169 * @destp: Destination memory pointer.
9170 * @cnt: Number of words required to be copied.
9171 *
9172 * This function is used for copying data between driver memory
9173 * and the SLI memory. This function also changes the endianness
9174 * of each word if native endianness is different from SLI
9175 * endianness. This function can be called with or without
9176 * lock.
9177 **/
dea31012005-04-17 16:05:31 -05009178void
9179lpfc_sli_pcimem_bcopy(void *srcp, void *destp, uint32_t cnt)
9180{
9181 uint32_t *src = srcp;
9182 uint32_t *dest = destp;
9183 uint32_t ldata;
9184 int i;
9185
9186 for (i = 0; i < (int)cnt; i += sizeof (uint32_t)) {
9187 ldata = *src;
9188 ldata = le32_to_cpu(ldata);
9189 *dest = ldata;
9190 src++;
9191 dest++;
9192 }
9193}
9194
James Smarte59058c2008-08-24 21:49:00 -04009195
9196/**
James Smarta0c87cb2009-07-19 10:01:10 -04009197 * lpfc_sli_bemem_bcopy - SLI memory copy function
9198 * @srcp: Source memory pointer.
9199 * @destp: Destination memory pointer.
9200 * @cnt: Number of words required to be copied.
9201 *
9202 * This function is used for copying data between a data structure
9203 * with big endian representation to local endianness.
9204 * This function can be called with or without lock.
9205 **/
9206void
9207lpfc_sli_bemem_bcopy(void *srcp, void *destp, uint32_t cnt)
9208{
9209 uint32_t *src = srcp;
9210 uint32_t *dest = destp;
9211 uint32_t ldata;
9212 int i;
9213
9214 for (i = 0; i < (int)cnt; i += sizeof(uint32_t)) {
9215 ldata = *src;
9216 ldata = be32_to_cpu(ldata);
9217 *dest = ldata;
9218 src++;
9219 dest++;
9220 }
9221}
9222
9223/**
James Smart3621a712009-04-06 18:47:14 -04009224 * lpfc_sli_ringpostbuf_put - Function to add a buffer to postbufq
James Smarte59058c2008-08-24 21:49:00 -04009225 * @phba: Pointer to HBA context object.
9226 * @pring: Pointer to driver SLI ring object.
9227 * @mp: Pointer to driver buffer object.
9228 *
9229 * This function is called with no lock held.
9230 * It always return zero after adding the buffer to the postbufq
9231 * buffer list.
9232 **/
dea31012005-04-17 16:05:31 -05009233int
James Smart2e0fef82007-06-17 19:56:36 -05009234lpfc_sli_ringpostbuf_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
9235 struct lpfc_dmabuf *mp)
dea31012005-04-17 16:05:31 -05009236{
9237 /* Stick struct lpfc_dmabuf at end of postbufq so driver can look it up
9238 later */
James Smart2e0fef82007-06-17 19:56:36 -05009239 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05009240 list_add_tail(&mp->list, &pring->postbufq);
dea31012005-04-17 16:05:31 -05009241 pring->postbufq_cnt++;
James Smart2e0fef82007-06-17 19:56:36 -05009242 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05009243 return 0;
9244}
9245
James Smarte59058c2008-08-24 21:49:00 -04009246/**
James Smart3621a712009-04-06 18:47:14 -04009247 * lpfc_sli_get_buffer_tag - allocates a tag for a CMD_QUE_XRI64_CX buffer
James Smarte59058c2008-08-24 21:49:00 -04009248 * @phba: Pointer to HBA context object.
9249 *
9250 * When HBQ is enabled, buffers are searched based on tags. This function
9251 * allocates a tag for buffer posted using CMD_QUE_XRI64_CX iocb. The
9252 * tag is bit wise or-ed with QUE_BUFTAG_BIT to make sure that the tag
9253 * does not conflict with tags of buffer posted for unsolicited events.
9254 * The function returns the allocated tag. The function is called with
9255 * no locks held.
9256 **/
James Smart76bb24e2007-10-27 13:38:00 -04009257uint32_t
9258lpfc_sli_get_buffer_tag(struct lpfc_hba *phba)
9259{
9260 spin_lock_irq(&phba->hbalock);
9261 phba->buffer_tag_count++;
9262 /*
9263 * Always set the QUE_BUFTAG_BIT to distiguish between
9264 * a tag assigned by HBQ.
9265 */
9266 phba->buffer_tag_count |= QUE_BUFTAG_BIT;
9267 spin_unlock_irq(&phba->hbalock);
9268 return phba->buffer_tag_count;
9269}
9270
James Smarte59058c2008-08-24 21:49:00 -04009271/**
James Smart3621a712009-04-06 18:47:14 -04009272 * lpfc_sli_ring_taggedbuf_get - find HBQ buffer associated with given tag
James Smarte59058c2008-08-24 21:49:00 -04009273 * @phba: Pointer to HBA context object.
9274 * @pring: Pointer to driver SLI ring object.
9275 * @tag: Buffer tag.
9276 *
9277 * Buffers posted using CMD_QUE_XRI64_CX iocb are in pring->postbufq
9278 * list. After HBA DMA data to these buffers, CMD_IOCB_RET_XRI64_CX
9279 * iocb is posted to the response ring with the tag of the buffer.
9280 * This function searches the pring->postbufq list using the tag
9281 * to find buffer associated with CMD_IOCB_RET_XRI64_CX
9282 * iocb. If the buffer is found then lpfc_dmabuf object of the
9283 * buffer is returned to the caller else NULL is returned.
9284 * This function is called with no lock held.
9285 **/
James Smart76bb24e2007-10-27 13:38:00 -04009286struct lpfc_dmabuf *
9287lpfc_sli_ring_taggedbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
9288 uint32_t tag)
9289{
9290 struct lpfc_dmabuf *mp, *next_mp;
9291 struct list_head *slp = &pring->postbufq;
9292
Lucas De Marchi25985ed2011-03-30 22:57:33 -03009293 /* Search postbufq, from the beginning, looking for a match on tag */
James Smart76bb24e2007-10-27 13:38:00 -04009294 spin_lock_irq(&phba->hbalock);
9295 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
9296 if (mp->buffer_tag == tag) {
9297 list_del_init(&mp->list);
9298 pring->postbufq_cnt--;
9299 spin_unlock_irq(&phba->hbalock);
9300 return mp;
9301 }
9302 }
9303
9304 spin_unlock_irq(&phba->hbalock);
9305 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartd7c255b2008-08-24 21:50:00 -04009306 "0402 Cannot find virtual addr for buffer tag on "
James Smart76bb24e2007-10-27 13:38:00 -04009307 "ring %d Data x%lx x%p x%p x%x\n",
9308 pring->ringno, (unsigned long) tag,
9309 slp->next, slp->prev, pring->postbufq_cnt);
9310
9311 return NULL;
9312}
dea31012005-04-17 16:05:31 -05009313
James Smarte59058c2008-08-24 21:49:00 -04009314/**
James Smart3621a712009-04-06 18:47:14 -04009315 * lpfc_sli_ringpostbuf_get - search buffers for unsolicited CT and ELS events
James Smarte59058c2008-08-24 21:49:00 -04009316 * @phba: Pointer to HBA context object.
9317 * @pring: Pointer to driver SLI ring object.
9318 * @phys: DMA address of the buffer.
9319 *
9320 * This function searches the buffer list using the dma_address
9321 * of unsolicited event to find the driver's lpfc_dmabuf object
9322 * corresponding to the dma_address. The function returns the
9323 * lpfc_dmabuf object if a buffer is found else it returns NULL.
9324 * This function is called by the ct and els unsolicited event
9325 * handlers to get the buffer associated with the unsolicited
9326 * event.
9327 *
9328 * This function is called with no lock held.
9329 **/
dea31012005-04-17 16:05:31 -05009330struct lpfc_dmabuf *
9331lpfc_sli_ringpostbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
9332 dma_addr_t phys)
9333{
9334 struct lpfc_dmabuf *mp, *next_mp;
9335 struct list_head *slp = &pring->postbufq;
9336
Lucas De Marchi25985ed2011-03-30 22:57:33 -03009337 /* Search postbufq, from the beginning, looking for a match on phys */
James Smart2e0fef82007-06-17 19:56:36 -05009338 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05009339 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
9340 if (mp->phys == phys) {
9341 list_del_init(&mp->list);
9342 pring->postbufq_cnt--;
James Smart2e0fef82007-06-17 19:56:36 -05009343 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05009344 return mp;
9345 }
9346 }
9347
James Smart2e0fef82007-06-17 19:56:36 -05009348 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05009349 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04009350 "0410 Cannot find virtual addr for mapped buf on "
dea31012005-04-17 16:05:31 -05009351 "ring %d Data x%llx x%p x%p x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04009352 pring->ringno, (unsigned long long)phys,
dea31012005-04-17 16:05:31 -05009353 slp->next, slp->prev, pring->postbufq_cnt);
9354 return NULL;
9355}
9356
James Smarte59058c2008-08-24 21:49:00 -04009357/**
James Smart3621a712009-04-06 18:47:14 -04009358 * lpfc_sli_abort_els_cmpl - Completion handler for the els abort iocbs
James Smarte59058c2008-08-24 21:49:00 -04009359 * @phba: Pointer to HBA context object.
9360 * @cmdiocb: Pointer to driver command iocb object.
9361 * @rspiocb: Pointer to driver response iocb object.
9362 *
9363 * This function is the completion handler for the abort iocbs for
9364 * ELS commands. This function is called from the ELS ring event
9365 * handler with no lock held. This function frees memory resources
9366 * associated with the abort iocb.
9367 **/
dea31012005-04-17 16:05:31 -05009368static void
James Smart2e0fef82007-06-17 19:56:36 -05009369lpfc_sli_abort_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
9370 struct lpfc_iocbq *rspiocb)
dea31012005-04-17 16:05:31 -05009371{
James Smart2e0fef82007-06-17 19:56:36 -05009372 IOCB_t *irsp = &rspiocb->iocb;
James Smart2680eea2007-04-25 09:52:55 -04009373 uint16_t abort_iotag, abort_context;
James Smartff78d8f2011-12-13 13:21:35 -05009374 struct lpfc_iocbq *abort_iocb = NULL;
James Smart2680eea2007-04-25 09:52:55 -04009375
9376 if (irsp->ulpStatus) {
James Smartff78d8f2011-12-13 13:21:35 -05009377
9378 /*
9379 * Assume that the port already completed and returned, or
9380 * will return the iocb. Just Log the message.
9381 */
James Smart2680eea2007-04-25 09:52:55 -04009382 abort_context = cmdiocb->iocb.un.acxri.abortContextTag;
9383 abort_iotag = cmdiocb->iocb.un.acxri.abortIoTag;
9384
James Smart2e0fef82007-06-17 19:56:36 -05009385 spin_lock_irq(&phba->hbalock);
James Smart45ed1192009-10-02 15:17:02 -04009386 if (phba->sli_rev < LPFC_SLI_REV4) {
9387 if (abort_iotag != 0 &&
9388 abort_iotag <= phba->sli.last_iotag)
9389 abort_iocb =
9390 phba->sli.iocbq_lookup[abort_iotag];
9391 } else
9392 /* For sli4 the abort_tag is the XRI,
9393 * so the abort routine puts the iotag of the iocb
9394 * being aborted in the context field of the abort
9395 * IOCB.
9396 */
9397 abort_iocb = phba->sli.iocbq_lookup[abort_context];
James Smart2680eea2007-04-25 09:52:55 -04009398
James Smart2a9bf3d2010-06-07 15:24:45 -04009399 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS | LOG_SLI,
9400 "0327 Cannot abort els iocb %p "
9401 "with tag %x context %x, abort status %x, "
9402 "abort code %x\n",
9403 abort_iocb, abort_iotag, abort_context,
9404 irsp->ulpStatus, irsp->un.ulpWord[4]);
James Smart2680eea2007-04-25 09:52:55 -04009405
James Smartff78d8f2011-12-13 13:21:35 -05009406 spin_unlock_irq(&phba->hbalock);
James Smart2680eea2007-04-25 09:52:55 -04009407 }
James Bottomley604a3e32005-10-29 10:28:33 -05009408 lpfc_sli_release_iocbq(phba, cmdiocb);
dea31012005-04-17 16:05:31 -05009409 return;
9410}
9411
James Smarte59058c2008-08-24 21:49:00 -04009412/**
James Smart3621a712009-04-06 18:47:14 -04009413 * lpfc_ignore_els_cmpl - Completion handler for aborted ELS command
James Smarte59058c2008-08-24 21:49:00 -04009414 * @phba: Pointer to HBA context object.
9415 * @cmdiocb: Pointer to driver command iocb object.
9416 * @rspiocb: Pointer to driver response iocb object.
9417 *
9418 * The function is called from SLI ring event handler with no
9419 * lock held. This function is the completion handler for ELS commands
9420 * which are aborted. The function frees memory resources used for
9421 * the aborted ELS commands.
9422 **/
James Smart92d7f7b2007-06-17 19:56:38 -05009423static void
9424lpfc_ignore_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
9425 struct lpfc_iocbq *rspiocb)
9426{
9427 IOCB_t *irsp = &rspiocb->iocb;
9428
9429 /* ELS cmd tag <ulpIoTag> completes */
9430 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
James Smartd7c255b2008-08-24 21:50:00 -04009431 "0139 Ignoring ELS cmd tag x%x completion Data: "
James Smart92d7f7b2007-06-17 19:56:38 -05009432 "x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04009433 irsp->ulpIoTag, irsp->ulpStatus,
James Smart92d7f7b2007-06-17 19:56:38 -05009434 irsp->un.ulpWord[4], irsp->ulpTimeout);
James Smart858c9f62007-06-17 19:56:39 -05009435 if (cmdiocb->iocb.ulpCommand == CMD_GEN_REQUEST64_CR)
9436 lpfc_ct_free_iocb(phba, cmdiocb);
9437 else
9438 lpfc_els_free_iocb(phba, cmdiocb);
James Smart92d7f7b2007-06-17 19:56:38 -05009439 return;
9440}
9441
James Smarte59058c2008-08-24 21:49:00 -04009442/**
James Smart5af5eee2010-10-22 11:06:38 -04009443 * lpfc_sli_abort_iotag_issue - Issue abort for a command iocb
James Smarte59058c2008-08-24 21:49:00 -04009444 * @phba: Pointer to HBA context object.
9445 * @pring: Pointer to driver SLI ring object.
9446 * @cmdiocb: Pointer to driver command iocb object.
9447 *
James Smart5af5eee2010-10-22 11:06:38 -04009448 * This function issues an abort iocb for the provided command iocb down to
9449 * the port. Other than the case the outstanding command iocb is an abort
9450 * request, this function issues abort out unconditionally. This function is
9451 * called with hbalock held. The function returns 0 when it fails due to
9452 * memory allocation failure or when the command iocb is an abort request.
James Smarte59058c2008-08-24 21:49:00 -04009453 **/
James Smart5af5eee2010-10-22 11:06:38 -04009454static int
9455lpfc_sli_abort_iotag_issue(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -05009456 struct lpfc_iocbq *cmdiocb)
dea31012005-04-17 16:05:31 -05009457{
James Smart2e0fef82007-06-17 19:56:36 -05009458 struct lpfc_vport *vport = cmdiocb->vport;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009459 struct lpfc_iocbq *abtsiocbp;
dea31012005-04-17 16:05:31 -05009460 IOCB_t *icmd = NULL;
9461 IOCB_t *iabt = NULL;
James Smart5af5eee2010-10-22 11:06:38 -04009462 int retval;
James Smart7e56aa22012-08-03 12:35:34 -04009463 unsigned long iflags;
James Smart07951072007-04-25 09:51:38 -04009464
James Smart92d7f7b2007-06-17 19:56:38 -05009465 /*
9466 * There are certain command types we don't want to abort. And we
9467 * don't want to abort commands that are already in the process of
9468 * being aborted.
James Smart07951072007-04-25 09:51:38 -04009469 */
9470 icmd = &cmdiocb->iocb;
James Smart2e0fef82007-06-17 19:56:36 -05009471 if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
James Smart92d7f7b2007-06-17 19:56:38 -05009472 icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
9473 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
James Smart07951072007-04-25 09:51:38 -04009474 return 0;
9475
dea31012005-04-17 16:05:31 -05009476 /* issue ABTS for this IOCB based on iotag */
James Smart92d7f7b2007-06-17 19:56:38 -05009477 abtsiocbp = __lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05009478 if (abtsiocbp == NULL)
9479 return 0;
dea31012005-04-17 16:05:31 -05009480
James Smart07951072007-04-25 09:51:38 -04009481 /* This signals the response to set the correct status
James Smart341af102010-01-26 23:07:37 -05009482 * before calling the completion handler
James Smart07951072007-04-25 09:51:38 -04009483 */
9484 cmdiocb->iocb_flag |= LPFC_DRIVER_ABORTED;
9485
dea31012005-04-17 16:05:31 -05009486 iabt = &abtsiocbp->iocb;
James Smart07951072007-04-25 09:51:38 -04009487 iabt->un.acxri.abortType = ABORT_TYPE_ABTS;
9488 iabt->un.acxri.abortContextTag = icmd->ulpContext;
James Smart45ed1192009-10-02 15:17:02 -04009489 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smartda0436e2009-05-22 14:51:39 -04009490 iabt->un.acxri.abortIoTag = cmdiocb->sli4_xritag;
James Smart45ed1192009-10-02 15:17:02 -04009491 iabt->un.acxri.abortContextTag = cmdiocb->iotag;
9492 }
James Smartda0436e2009-05-22 14:51:39 -04009493 else
9494 iabt->un.acxri.abortIoTag = icmd->ulpIoTag;
dea31012005-04-17 16:05:31 -05009495 iabt->ulpLe = 1;
James Smart07951072007-04-25 09:51:38 -04009496 iabt->ulpClass = icmd->ulpClass;
dea31012005-04-17 16:05:31 -05009497
James Smart5ffc2662009-11-18 15:39:44 -05009498 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
9499 abtsiocbp->fcp_wqidx = cmdiocb->fcp_wqidx;
James Smart341af102010-01-26 23:07:37 -05009500 if (cmdiocb->iocb_flag & LPFC_IO_FCP)
9501 abtsiocbp->iocb_flag |= LPFC_USE_FCPWQIDX;
James Smart5ffc2662009-11-18 15:39:44 -05009502
James Smart2e0fef82007-06-17 19:56:36 -05009503 if (phba->link_state >= LPFC_LINK_UP)
James Smart07951072007-04-25 09:51:38 -04009504 iabt->ulpCommand = CMD_ABORT_XRI_CN;
9505 else
9506 iabt->ulpCommand = CMD_CLOSE_XRI_CN;
9507
9508 abtsiocbp->iocb_cmpl = lpfc_sli_abort_els_cmpl;
James Smart5b8bd0c2007-04-25 09:52:49 -04009509
James Smarte8b62012007-08-02 11:10:09 -04009510 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
9511 "0339 Abort xri x%x, original iotag x%x, "
9512 "abort cmd iotag x%x\n",
James Smart2a9bf3d2010-06-07 15:24:45 -04009513 iabt->un.acxri.abortIoTag,
James Smarte8b62012007-08-02 11:10:09 -04009514 iabt->un.acxri.abortContextTag,
James Smart2a9bf3d2010-06-07 15:24:45 -04009515 abtsiocbp->iotag);
James Smart7e56aa22012-08-03 12:35:34 -04009516
9517 if (phba->sli_rev == LPFC_SLI_REV4) {
9518 /* Note: both hbalock and ring_lock need to be set here */
9519 spin_lock_irqsave(&pring->ring_lock, iflags);
9520 retval = __lpfc_sli_issue_iocb(phba, pring->ringno,
9521 abtsiocbp, 0);
9522 spin_unlock_irqrestore(&pring->ring_lock, iflags);
9523 } else {
9524 retval = __lpfc_sli_issue_iocb(phba, pring->ringno,
9525 abtsiocbp, 0);
9526 }
James Smart07951072007-04-25 09:51:38 -04009527
James Smartd7c255b2008-08-24 21:50:00 -04009528 if (retval)
9529 __lpfc_sli_release_iocbq(phba, abtsiocbp);
James Smart5af5eee2010-10-22 11:06:38 -04009530
9531 /*
9532 * Caller to this routine should check for IOCB_ERROR
9533 * and handle it properly. This routine no longer removes
9534 * iocb off txcmplq and call compl in case of IOCB_ERROR.
9535 */
9536 return retval;
9537}
9538
9539/**
9540 * lpfc_sli_issue_abort_iotag - Abort function for a command iocb
9541 * @phba: Pointer to HBA context object.
9542 * @pring: Pointer to driver SLI ring object.
9543 * @cmdiocb: Pointer to driver command iocb object.
9544 *
9545 * This function issues an abort iocb for the provided command iocb. In case
9546 * of unloading, the abort iocb will not be issued to commands on the ELS
9547 * ring. Instead, the callback function shall be changed to those commands
9548 * so that nothing happens when them finishes. This function is called with
9549 * hbalock held. The function returns 0 when the command iocb is an abort
9550 * request.
9551 **/
9552int
9553lpfc_sli_issue_abort_iotag(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
9554 struct lpfc_iocbq *cmdiocb)
9555{
9556 struct lpfc_vport *vport = cmdiocb->vport;
9557 int retval = IOCB_ERROR;
9558 IOCB_t *icmd = NULL;
9559
9560 /*
9561 * There are certain command types we don't want to abort. And we
9562 * don't want to abort commands that are already in the process of
9563 * being aborted.
9564 */
9565 icmd = &cmdiocb->iocb;
9566 if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
9567 icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
9568 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
9569 return 0;
9570
9571 /*
9572 * If we're unloading, don't abort iocb on the ELS ring, but change
9573 * the callback so that nothing happens when it finishes.
9574 */
9575 if ((vport->load_flag & FC_UNLOADING) &&
9576 (pring->ringno == LPFC_ELS_RING)) {
9577 if (cmdiocb->iocb_flag & LPFC_IO_FABRIC)
9578 cmdiocb->fabric_iocb_cmpl = lpfc_ignore_els_cmpl;
9579 else
9580 cmdiocb->iocb_cmpl = lpfc_ignore_els_cmpl;
9581 goto abort_iotag_exit;
9582 }
9583
9584 /* Now, we try to issue the abort to the cmdiocb out */
9585 retval = lpfc_sli_abort_iotag_issue(phba, pring, cmdiocb);
9586
James Smart07951072007-04-25 09:51:38 -04009587abort_iotag_exit:
James Smart2e0fef82007-06-17 19:56:36 -05009588 /*
9589 * Caller to this routine should check for IOCB_ERROR
9590 * and handle it properly. This routine no longer removes
9591 * iocb off txcmplq and call compl in case of IOCB_ERROR.
James Smart07951072007-04-25 09:51:38 -04009592 */
James Smart2e0fef82007-06-17 19:56:36 -05009593 return retval;
dea31012005-04-17 16:05:31 -05009594}
9595
James Smarte59058c2008-08-24 21:49:00 -04009596/**
James Smart5af5eee2010-10-22 11:06:38 -04009597 * lpfc_sli_iocb_ring_abort - Unconditionally abort all iocbs on an iocb ring
9598 * @phba: Pointer to HBA context object.
9599 * @pring: Pointer to driver SLI ring object.
9600 *
9601 * This function aborts all iocbs in the given ring and frees all the iocb
9602 * objects in txq. This function issues abort iocbs unconditionally for all
9603 * the iocb commands in txcmplq. The iocbs in the txcmplq is not guaranteed
9604 * to complete before the return of this function. The caller is not required
9605 * to hold any locks.
9606 **/
9607static void
9608lpfc_sli_iocb_ring_abort(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
9609{
9610 LIST_HEAD(completions);
9611 struct lpfc_iocbq *iocb, *next_iocb;
9612
9613 if (pring->ringno == LPFC_ELS_RING)
9614 lpfc_fabric_abort_hba(phba);
9615
9616 spin_lock_irq(&phba->hbalock);
9617
9618 /* Take off all the iocbs on txq for cancelling */
9619 list_splice_init(&pring->txq, &completions);
9620 pring->txq_cnt = 0;
9621
9622 /* Next issue ABTS for everything on the txcmplq */
9623 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
9624 lpfc_sli_abort_iotag_issue(phba, pring, iocb);
9625
9626 spin_unlock_irq(&phba->hbalock);
9627
9628 /* Cancel all the IOCBs from the completions list */
9629 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
9630 IOERR_SLI_ABORTED);
9631}
9632
9633/**
9634 * lpfc_sli_hba_iocb_abort - Abort all iocbs to an hba.
9635 * @phba: pointer to lpfc HBA data structure.
9636 *
9637 * This routine will abort all pending and outstanding iocbs to an HBA.
9638 **/
9639void
9640lpfc_sli_hba_iocb_abort(struct lpfc_hba *phba)
9641{
9642 struct lpfc_sli *psli = &phba->sli;
9643 struct lpfc_sli_ring *pring;
9644 int i;
9645
9646 for (i = 0; i < psli->num_rings; i++) {
9647 pring = &psli->ring[i];
9648 lpfc_sli_iocb_ring_abort(phba, pring);
9649 }
9650}
9651
9652/**
James Smart3621a712009-04-06 18:47:14 -04009653 * lpfc_sli_validate_fcp_iocb - find commands associated with a vport or LUN
James Smarte59058c2008-08-24 21:49:00 -04009654 * @iocbq: Pointer to driver iocb object.
9655 * @vport: Pointer to driver virtual port object.
9656 * @tgt_id: SCSI ID of the target.
9657 * @lun_id: LUN ID of the scsi device.
9658 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST
9659 *
James Smart3621a712009-04-06 18:47:14 -04009660 * This function acts as an iocb filter for functions which abort or count
James Smarte59058c2008-08-24 21:49:00 -04009661 * all FCP iocbs pending on a lun/SCSI target/SCSI host. It will return
9662 * 0 if the filtering criteria is met for the given iocb and will return
9663 * 1 if the filtering criteria is not met.
9664 * If ctx_cmd == LPFC_CTX_LUN, the function returns 0 only if the
9665 * given iocb is for the SCSI device specified by vport, tgt_id and
9666 * lun_id parameter.
9667 * If ctx_cmd == LPFC_CTX_TGT, the function returns 0 only if the
9668 * given iocb is for the SCSI target specified by vport and tgt_id
9669 * parameters.
9670 * If ctx_cmd == LPFC_CTX_HOST, the function returns 0 only if the
9671 * given iocb is for the SCSI host associated with the given vport.
9672 * This function is called with no locks held.
9673 **/
dea31012005-04-17 16:05:31 -05009674static int
James Smart51ef4c22007-08-02 11:10:31 -04009675lpfc_sli_validate_fcp_iocb(struct lpfc_iocbq *iocbq, struct lpfc_vport *vport,
9676 uint16_t tgt_id, uint64_t lun_id,
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009677 lpfc_ctx_cmd ctx_cmd)
dea31012005-04-17 16:05:31 -05009678{
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009679 struct lpfc_scsi_buf *lpfc_cmd;
dea31012005-04-17 16:05:31 -05009680 int rc = 1;
9681
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009682 if (!(iocbq->iocb_flag & LPFC_IO_FCP))
9683 return rc;
9684
James Smart51ef4c22007-08-02 11:10:31 -04009685 if (iocbq->vport != vport)
9686 return rc;
9687
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009688 lpfc_cmd = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009689
James Smart495a7142008-06-14 22:52:59 -04009690 if (lpfc_cmd->pCmd == NULL)
dea31012005-04-17 16:05:31 -05009691 return rc;
9692
9693 switch (ctx_cmd) {
9694 case LPFC_CTX_LUN:
James Smart495a7142008-06-14 22:52:59 -04009695 if ((lpfc_cmd->rdata->pnode) &&
9696 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id) &&
9697 (scsilun_to_int(&lpfc_cmd->fcp_cmnd->fcp_lun) == lun_id))
dea31012005-04-17 16:05:31 -05009698 rc = 0;
9699 break;
9700 case LPFC_CTX_TGT:
James Smart495a7142008-06-14 22:52:59 -04009701 if ((lpfc_cmd->rdata->pnode) &&
9702 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id))
dea31012005-04-17 16:05:31 -05009703 rc = 0;
9704 break;
dea31012005-04-17 16:05:31 -05009705 case LPFC_CTX_HOST:
9706 rc = 0;
9707 break;
9708 default:
9709 printk(KERN_ERR "%s: Unknown context cmd type, value %d\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07009710 __func__, ctx_cmd);
dea31012005-04-17 16:05:31 -05009711 break;
9712 }
9713
9714 return rc;
9715}
9716
James Smarte59058c2008-08-24 21:49:00 -04009717/**
James Smart3621a712009-04-06 18:47:14 -04009718 * lpfc_sli_sum_iocb - Function to count the number of FCP iocbs pending
James Smarte59058c2008-08-24 21:49:00 -04009719 * @vport: Pointer to virtual port.
9720 * @tgt_id: SCSI ID of the target.
9721 * @lun_id: LUN ID of the scsi device.
9722 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
9723 *
9724 * This function returns number of FCP commands pending for the vport.
9725 * When ctx_cmd == LPFC_CTX_LUN, the function returns number of FCP
9726 * commands pending on the vport associated with SCSI device specified
9727 * by tgt_id and lun_id parameters.
9728 * When ctx_cmd == LPFC_CTX_TGT, the function returns number of FCP
9729 * commands pending on the vport associated with SCSI target specified
9730 * by tgt_id parameter.
9731 * When ctx_cmd == LPFC_CTX_HOST, the function returns number of FCP
9732 * commands pending on the vport.
9733 * This function returns the number of iocbs which satisfy the filter.
9734 * This function is called without any lock held.
9735 **/
dea31012005-04-17 16:05:31 -05009736int
James Smart51ef4c22007-08-02 11:10:31 -04009737lpfc_sli_sum_iocb(struct lpfc_vport *vport, uint16_t tgt_id, uint64_t lun_id,
9738 lpfc_ctx_cmd ctx_cmd)
dea31012005-04-17 16:05:31 -05009739{
James Smart51ef4c22007-08-02 11:10:31 -04009740 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009741 struct lpfc_iocbq *iocbq;
9742 int sum, i;
dea31012005-04-17 16:05:31 -05009743
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009744 for (i = 1, sum = 0; i <= phba->sli.last_iotag; i++) {
9745 iocbq = phba->sli.iocbq_lookup[i];
dea31012005-04-17 16:05:31 -05009746
James Smart51ef4c22007-08-02 11:10:31 -04009747 if (lpfc_sli_validate_fcp_iocb (iocbq, vport, tgt_id, lun_id,
9748 ctx_cmd) == 0)
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009749 sum++;
dea31012005-04-17 16:05:31 -05009750 }
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009751
dea31012005-04-17 16:05:31 -05009752 return sum;
9753}
9754
James Smarte59058c2008-08-24 21:49:00 -04009755/**
James Smart3621a712009-04-06 18:47:14 -04009756 * lpfc_sli_abort_fcp_cmpl - Completion handler function for aborted FCP IOCBs
James Smarte59058c2008-08-24 21:49:00 -04009757 * @phba: Pointer to HBA context object
9758 * @cmdiocb: Pointer to command iocb object.
9759 * @rspiocb: Pointer to response iocb object.
9760 *
9761 * This function is called when an aborted FCP iocb completes. This
9762 * function is called by the ring event handler with no lock held.
9763 * This function frees the iocb.
9764 **/
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -04009765void
James Smart2e0fef82007-06-17 19:56:36 -05009766lpfc_sli_abort_fcp_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
9767 struct lpfc_iocbq *rspiocb)
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -04009768{
James Smartcb69f7d2011-12-13 13:21:57 -05009769 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
9770 "3096 ABORT_XRI_CN completing on xri x%x "
9771 "original iotag x%x, abort cmd iotag x%x "
9772 "status 0x%x, reason 0x%x\n",
9773 cmdiocb->iocb.un.acxri.abortContextTag,
9774 cmdiocb->iocb.un.acxri.abortIoTag,
9775 cmdiocb->iotag, rspiocb->iocb.ulpStatus,
9776 rspiocb->iocb.un.ulpWord[4]);
James Bottomley604a3e32005-10-29 10:28:33 -05009777 lpfc_sli_release_iocbq(phba, cmdiocb);
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -04009778 return;
9779}
9780
James Smarte59058c2008-08-24 21:49:00 -04009781/**
James Smart3621a712009-04-06 18:47:14 -04009782 * lpfc_sli_abort_iocb - issue abort for all commands on a host/target/LUN
James Smarte59058c2008-08-24 21:49:00 -04009783 * @vport: Pointer to virtual port.
9784 * @pring: Pointer to driver SLI ring object.
9785 * @tgt_id: SCSI ID of the target.
9786 * @lun_id: LUN ID of the scsi device.
9787 * @abort_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
9788 *
9789 * This function sends an abort command for every SCSI command
9790 * associated with the given virtual port pending on the ring
9791 * filtered by lpfc_sli_validate_fcp_iocb function.
9792 * When abort_cmd == LPFC_CTX_LUN, the function sends abort only to the
9793 * FCP iocbs associated with lun specified by tgt_id and lun_id
9794 * parameters
9795 * When abort_cmd == LPFC_CTX_TGT, the function sends abort only to the
9796 * FCP iocbs associated with SCSI target specified by tgt_id parameter.
9797 * When abort_cmd == LPFC_CTX_HOST, the function sends abort to all
9798 * FCP iocbs associated with virtual port.
9799 * This function returns number of iocbs it failed to abort.
9800 * This function is called with no locks held.
9801 **/
dea31012005-04-17 16:05:31 -05009802int
James Smart51ef4c22007-08-02 11:10:31 -04009803lpfc_sli_abort_iocb(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
9804 uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd abort_cmd)
dea31012005-04-17 16:05:31 -05009805{
James Smart51ef4c22007-08-02 11:10:31 -04009806 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009807 struct lpfc_iocbq *iocbq;
9808 struct lpfc_iocbq *abtsiocb;
dea31012005-04-17 16:05:31 -05009809 IOCB_t *cmd = NULL;
dea31012005-04-17 16:05:31 -05009810 int errcnt = 0, ret_val = 0;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009811 int i;
dea31012005-04-17 16:05:31 -05009812
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009813 for (i = 1; i <= phba->sli.last_iotag; i++) {
9814 iocbq = phba->sli.iocbq_lookup[i];
dea31012005-04-17 16:05:31 -05009815
James Smart51ef4c22007-08-02 11:10:31 -04009816 if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
James Smart2e0fef82007-06-17 19:56:36 -05009817 abort_cmd) != 0)
dea31012005-04-17 16:05:31 -05009818 continue;
9819
9820 /* issue ABTS for this IOCB based on iotag */
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009821 abtsiocb = lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05009822 if (abtsiocb == NULL) {
9823 errcnt++;
9824 continue;
9825 }
dea31012005-04-17 16:05:31 -05009826
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009827 cmd = &iocbq->iocb;
dea31012005-04-17 16:05:31 -05009828 abtsiocb->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
9829 abtsiocb->iocb.un.acxri.abortContextTag = cmd->ulpContext;
James Smartda0436e2009-05-22 14:51:39 -04009830 if (phba->sli_rev == LPFC_SLI_REV4)
9831 abtsiocb->iocb.un.acxri.abortIoTag = iocbq->sli4_xritag;
9832 else
9833 abtsiocb->iocb.un.acxri.abortIoTag = cmd->ulpIoTag;
dea31012005-04-17 16:05:31 -05009834 abtsiocb->iocb.ulpLe = 1;
9835 abtsiocb->iocb.ulpClass = cmd->ulpClass;
James Smart2e0fef82007-06-17 19:56:36 -05009836 abtsiocb->vport = phba->pport;
dea31012005-04-17 16:05:31 -05009837
James Smart5ffc2662009-11-18 15:39:44 -05009838 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
9839 abtsiocb->fcp_wqidx = iocbq->fcp_wqidx;
James Smart341af102010-01-26 23:07:37 -05009840 if (iocbq->iocb_flag & LPFC_IO_FCP)
9841 abtsiocb->iocb_flag |= LPFC_USE_FCPWQIDX;
James Smart5ffc2662009-11-18 15:39:44 -05009842
James Smart2e0fef82007-06-17 19:56:36 -05009843 if (lpfc_is_link_up(phba))
dea31012005-04-17 16:05:31 -05009844 abtsiocb->iocb.ulpCommand = CMD_ABORT_XRI_CN;
9845 else
9846 abtsiocb->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
9847
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -04009848 /* Setup callback routine and issue the command. */
9849 abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
James Smartda0436e2009-05-22 14:51:39 -04009850 ret_val = lpfc_sli_issue_iocb(phba, pring->ringno,
9851 abtsiocb, 0);
dea31012005-04-17 16:05:31 -05009852 if (ret_val == IOCB_ERROR) {
James Bottomley604a3e32005-10-29 10:28:33 -05009853 lpfc_sli_release_iocbq(phba, abtsiocb);
dea31012005-04-17 16:05:31 -05009854 errcnt++;
9855 continue;
9856 }
9857 }
9858
9859 return errcnt;
9860}
9861
James Smarte59058c2008-08-24 21:49:00 -04009862/**
James Smart3621a712009-04-06 18:47:14 -04009863 * lpfc_sli_wake_iocb_wait - lpfc_sli_issue_iocb_wait's completion handler
James Smarte59058c2008-08-24 21:49:00 -04009864 * @phba: Pointer to HBA context object.
9865 * @cmdiocbq: Pointer to command iocb.
9866 * @rspiocbq: Pointer to response iocb.
9867 *
9868 * This function is the completion handler for iocbs issued using
9869 * lpfc_sli_issue_iocb_wait function. This function is called by the
9870 * ring event handler function without any lock held. This function
9871 * can be called from both worker thread context and interrupt
9872 * context. This function also can be called from other thread which
9873 * cleans up the SLI layer objects.
9874 * This function copy the contents of the response iocb to the
9875 * response iocb memory object provided by the caller of
9876 * lpfc_sli_issue_iocb_wait and then wakes up the thread which
9877 * sleeps for the iocb completion.
9878 **/
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009879static void
9880lpfc_sli_wake_iocb_wait(struct lpfc_hba *phba,
9881 struct lpfc_iocbq *cmdiocbq,
9882 struct lpfc_iocbq *rspiocbq)
dea31012005-04-17 16:05:31 -05009883{
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009884 wait_queue_head_t *pdone_q;
9885 unsigned long iflags;
James Smart0f65ff62010-02-26 14:14:23 -05009886 struct lpfc_scsi_buf *lpfc_cmd;
dea31012005-04-17 16:05:31 -05009887
James Smart2e0fef82007-06-17 19:56:36 -05009888 spin_lock_irqsave(&phba->hbalock, iflags);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009889 cmdiocbq->iocb_flag |= LPFC_IO_WAKE;
9890 if (cmdiocbq->context2 && rspiocbq)
9891 memcpy(&((struct lpfc_iocbq *)cmdiocbq->context2)->iocb,
9892 &rspiocbq->iocb, sizeof(IOCB_t));
9893
James Smart0f65ff62010-02-26 14:14:23 -05009894 /* Set the exchange busy flag for task management commands */
9895 if ((cmdiocbq->iocb_flag & LPFC_IO_FCP) &&
9896 !(cmdiocbq->iocb_flag & LPFC_IO_LIBDFC)) {
9897 lpfc_cmd = container_of(cmdiocbq, struct lpfc_scsi_buf,
9898 cur_iocbq);
9899 lpfc_cmd->exch_busy = rspiocbq->iocb_flag & LPFC_EXCHANGE_BUSY;
9900 }
9901
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009902 pdone_q = cmdiocbq->context_un.wait_queue;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009903 if (pdone_q)
9904 wake_up(pdone_q);
James Smart858c9f62007-06-17 19:56:39 -05009905 spin_unlock_irqrestore(&phba->hbalock, iflags);
dea31012005-04-17 16:05:31 -05009906 return;
9907}
9908
James Smarte59058c2008-08-24 21:49:00 -04009909/**
James Smartd11e31d2009-06-10 17:23:06 -04009910 * lpfc_chk_iocb_flg - Test IOCB flag with lock held.
9911 * @phba: Pointer to HBA context object..
9912 * @piocbq: Pointer to command iocb.
9913 * @flag: Flag to test.
9914 *
9915 * This routine grabs the hbalock and then test the iocb_flag to
9916 * see if the passed in flag is set.
9917 * Returns:
9918 * 1 if flag is set.
9919 * 0 if flag is not set.
9920 **/
9921static int
9922lpfc_chk_iocb_flg(struct lpfc_hba *phba,
9923 struct lpfc_iocbq *piocbq, uint32_t flag)
9924{
9925 unsigned long iflags;
9926 int ret;
9927
9928 spin_lock_irqsave(&phba->hbalock, iflags);
9929 ret = piocbq->iocb_flag & flag;
9930 spin_unlock_irqrestore(&phba->hbalock, iflags);
9931 return ret;
9932
9933}
9934
9935/**
James Smart3621a712009-04-06 18:47:14 -04009936 * lpfc_sli_issue_iocb_wait - Synchronous function to issue iocb commands
James Smarte59058c2008-08-24 21:49:00 -04009937 * @phba: Pointer to HBA context object..
9938 * @pring: Pointer to sli ring.
9939 * @piocb: Pointer to command iocb.
9940 * @prspiocbq: Pointer to response iocb.
9941 * @timeout: Timeout in number of seconds.
9942 *
9943 * This function issues the iocb to firmware and waits for the
9944 * iocb to complete. If the iocb command is not
9945 * completed within timeout seconds, it returns IOCB_TIMEDOUT.
9946 * Caller should not free the iocb resources if this function
9947 * returns IOCB_TIMEDOUT.
9948 * The function waits for the iocb completion using an
9949 * non-interruptible wait.
9950 * This function will sleep while waiting for iocb completion.
9951 * So, this function should not be called from any context which
9952 * does not allow sleeping. Due to the same reason, this function
9953 * cannot be called with interrupt disabled.
9954 * This function assumes that the iocb completions occur while
9955 * this function sleep. So, this function cannot be called from
9956 * the thread which process iocb completion for this ring.
9957 * This function clears the iocb_flag of the iocb object before
9958 * issuing the iocb and the iocb completion handler sets this
9959 * flag and wakes this thread when the iocb completes.
9960 * The contents of the response iocb will be copied to prspiocbq
9961 * by the completion handler when the command completes.
9962 * This function returns IOCB_SUCCESS when success.
9963 * This function is called with no lock held.
9964 **/
dea31012005-04-17 16:05:31 -05009965int
James Smart2e0fef82007-06-17 19:56:36 -05009966lpfc_sli_issue_iocb_wait(struct lpfc_hba *phba,
James Smartda0436e2009-05-22 14:51:39 -04009967 uint32_t ring_number,
James Smart2e0fef82007-06-17 19:56:36 -05009968 struct lpfc_iocbq *piocb,
9969 struct lpfc_iocbq *prspiocbq,
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009970 uint32_t timeout)
dea31012005-04-17 16:05:31 -05009971{
Peter Zijlstra7259f0d2006-10-29 22:46:36 -08009972 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009973 long timeleft, timeout_req = 0;
9974 int retval = IOCB_SUCCESS;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05009975 uint32_t creg_val;
James Smart0e9bb8d2013-03-01 16:35:12 -05009976 struct lpfc_iocbq *iocb;
9977 int txq_cnt = 0;
9978 int txcmplq_cnt = 0;
James Smart2a9bf3d2010-06-07 15:24:45 -04009979 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
dea31012005-04-17 16:05:31 -05009980 /*
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009981 * If the caller has provided a response iocbq buffer, then context2
9982 * is NULL or its an error.
dea31012005-04-17 16:05:31 -05009983 */
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009984 if (prspiocbq) {
9985 if (piocb->context2)
9986 return IOCB_ERROR;
9987 piocb->context2 = prspiocbq;
dea31012005-04-17 16:05:31 -05009988 }
9989
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009990 piocb->iocb_cmpl = lpfc_sli_wake_iocb_wait;
9991 piocb->context_un.wait_queue = &done_q;
9992 piocb->iocb_flag &= ~LPFC_IO_WAKE;
dea31012005-04-17 16:05:31 -05009993
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05009994 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
James Smart9940b972011-03-11 16:06:12 -05009995 if (lpfc_readl(phba->HCregaddr, &creg_val))
9996 return IOCB_ERROR;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05009997 creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
9998 writel(creg_val, phba->HCregaddr);
9999 readl(phba->HCregaddr); /* flush */
10000 }
10001
James Smart2a9bf3d2010-06-07 15:24:45 -040010002 retval = lpfc_sli_issue_iocb(phba, ring_number, piocb,
10003 SLI_IOCB_RET_IOCB);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010004 if (retval == IOCB_SUCCESS) {
10005 timeout_req = timeout * HZ;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010006 timeleft = wait_event_timeout(done_q,
James Smartd11e31d2009-06-10 17:23:06 -040010007 lpfc_chk_iocb_flg(phba, piocb, LPFC_IO_WAKE),
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010008 timeout_req);
dea31012005-04-17 16:05:31 -050010009
James Smart7054a602007-04-25 09:52:34 -040010010 if (piocb->iocb_flag & LPFC_IO_WAKE) {
10011 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -040010012 "0331 IOCB wake signaled\n");
James Smart7054a602007-04-25 09:52:34 -040010013 } else if (timeleft == 0) {
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010014 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -040010015 "0338 IOCB wait timeout error - no "
10016 "wake response Data x%x\n", timeout);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010017 retval = IOCB_TIMEDOUT;
James Smart7054a602007-04-25 09:52:34 -040010018 } else {
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010019 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -040010020 "0330 IOCB wake NOT set, "
10021 "Data x%x x%lx\n",
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010022 timeout, (timeleft / jiffies));
10023 retval = IOCB_TIMEDOUT;
dea31012005-04-17 16:05:31 -050010024 }
James Smart2a9bf3d2010-06-07 15:24:45 -040010025 } else if (retval == IOCB_BUSY) {
James Smart0e9bb8d2013-03-01 16:35:12 -050010026 if (phba->cfg_log_verbose & LOG_SLI) {
10027 list_for_each_entry(iocb, &pring->txq, list) {
10028 txq_cnt++;
10029 }
10030 list_for_each_entry(iocb, &pring->txcmplq, list) {
10031 txcmplq_cnt++;
10032 }
10033 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
10034 "2818 Max IOCBs %d txq cnt %d txcmplq cnt %d\n",
10035 phba->iocb_cnt, txq_cnt, txcmplq_cnt);
10036 }
James Smart2a9bf3d2010-06-07 15:24:45 -040010037 return retval;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010038 } else {
10039 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smartd7c255b2008-08-24 21:50:00 -040010040 "0332 IOCB wait issue failed, Data x%x\n",
James Smarte8b62012007-08-02 11:10:09 -040010041 retval);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010042 retval = IOCB_ERROR;
dea31012005-04-17 16:05:31 -050010043 }
10044
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -050010045 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
James Smart9940b972011-03-11 16:06:12 -050010046 if (lpfc_readl(phba->HCregaddr, &creg_val))
10047 return IOCB_ERROR;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -050010048 creg_val &= ~(HC_R0INT_ENA << LPFC_FCP_RING);
10049 writel(creg_val, phba->HCregaddr);
10050 readl(phba->HCregaddr); /* flush */
10051 }
10052
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010053 if (prspiocbq)
10054 piocb->context2 = NULL;
10055
10056 piocb->context_un.wait_queue = NULL;
10057 piocb->iocb_cmpl = NULL;
dea31012005-04-17 16:05:31 -050010058 return retval;
10059}
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010060
James Smarte59058c2008-08-24 21:49:00 -040010061/**
James Smart3621a712009-04-06 18:47:14 -040010062 * lpfc_sli_issue_mbox_wait - Synchronous function to issue mailbox
James Smarte59058c2008-08-24 21:49:00 -040010063 * @phba: Pointer to HBA context object.
10064 * @pmboxq: Pointer to driver mailbox object.
10065 * @timeout: Timeout in number of seconds.
10066 *
10067 * This function issues the mailbox to firmware and waits for the
10068 * mailbox command to complete. If the mailbox command is not
10069 * completed within timeout seconds, it returns MBX_TIMEOUT.
10070 * The function waits for the mailbox completion using an
10071 * interruptible wait. If the thread is woken up due to a
10072 * signal, MBX_TIMEOUT error is returned to the caller. Caller
10073 * should not free the mailbox resources, if this function returns
10074 * MBX_TIMEOUT.
10075 * This function will sleep while waiting for mailbox completion.
10076 * So, this function should not be called from any context which
10077 * does not allow sleeping. Due to the same reason, this function
10078 * cannot be called with interrupt disabled.
10079 * This function assumes that the mailbox completion occurs while
10080 * this function sleep. So, this function cannot be called from
10081 * the worker thread which processes mailbox completion.
10082 * This function is called in the context of HBA management
10083 * applications.
10084 * This function returns MBX_SUCCESS when successful.
10085 * This function is called with no lock held.
10086 **/
dea31012005-04-17 16:05:31 -050010087int
James Smart2e0fef82007-06-17 19:56:36 -050010088lpfc_sli_issue_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq,
dea31012005-04-17 16:05:31 -050010089 uint32_t timeout)
10090{
Peter Zijlstra7259f0d2006-10-29 22:46:36 -080010091 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
dea31012005-04-17 16:05:31 -050010092 int retval;
James Smart858c9f62007-06-17 19:56:39 -050010093 unsigned long flag;
dea31012005-04-17 16:05:31 -050010094
10095 /* The caller must leave context1 empty. */
James Smart98c9ea52007-10-27 13:37:33 -040010096 if (pmboxq->context1)
James Smart2e0fef82007-06-17 19:56:36 -050010097 return MBX_NOT_FINISHED;
dea31012005-04-17 16:05:31 -050010098
James Smart495a7142008-06-14 22:52:59 -040010099 pmboxq->mbox_flag &= ~LPFC_MBX_WAKE;
dea31012005-04-17 16:05:31 -050010100 /* setup wake call as IOCB callback */
10101 pmboxq->mbox_cmpl = lpfc_sli_wake_mbox_wait;
10102 /* setup context field to pass wait_queue pointer to wake function */
10103 pmboxq->context1 = &done_q;
10104
dea31012005-04-17 16:05:31 -050010105 /* now issue the command */
10106 retval = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
dea31012005-04-17 16:05:31 -050010107 if (retval == MBX_BUSY || retval == MBX_SUCCESS) {
James Smart7054a602007-04-25 09:52:34 -040010108 wait_event_interruptible_timeout(done_q,
10109 pmboxq->mbox_flag & LPFC_MBX_WAKE,
10110 timeout * HZ);
10111
James Smart858c9f62007-06-17 19:56:39 -050010112 spin_lock_irqsave(&phba->hbalock, flag);
dea31012005-04-17 16:05:31 -050010113 pmboxq->context1 = NULL;
James Smart7054a602007-04-25 09:52:34 -040010114 /*
10115 * if LPFC_MBX_WAKE flag is set the mailbox is completed
10116 * else do not free the resources.
10117 */
James Smartd7c47992010-06-08 18:31:54 -040010118 if (pmboxq->mbox_flag & LPFC_MBX_WAKE) {
dea31012005-04-17 16:05:31 -050010119 retval = MBX_SUCCESS;
James Smartd7c47992010-06-08 18:31:54 -040010120 lpfc_sli4_swap_str(phba, pmboxq);
10121 } else {
James Smart7054a602007-04-25 09:52:34 -040010122 retval = MBX_TIMEOUT;
James Smart858c9f62007-06-17 19:56:39 -050010123 pmboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
10124 }
10125 spin_unlock_irqrestore(&phba->hbalock, flag);
dea31012005-04-17 16:05:31 -050010126 }
10127
dea31012005-04-17 16:05:31 -050010128 return retval;
10129}
10130
James Smarte59058c2008-08-24 21:49:00 -040010131/**
James Smart3772a992009-05-22 14:50:54 -040010132 * lpfc_sli_mbox_sys_shutdown - shutdown mailbox command sub-system
James Smarte59058c2008-08-24 21:49:00 -040010133 * @phba: Pointer to HBA context.
10134 *
James Smart3772a992009-05-22 14:50:54 -040010135 * This function is called to shutdown the driver's mailbox sub-system.
10136 * It first marks the mailbox sub-system is in a block state to prevent
10137 * the asynchronous mailbox command from issued off the pending mailbox
10138 * command queue. If the mailbox command sub-system shutdown is due to
10139 * HBA error conditions such as EEH or ERATT, this routine shall invoke
10140 * the mailbox sub-system flush routine to forcefully bring down the
10141 * mailbox sub-system. Otherwise, if it is due to normal condition (such
10142 * as with offline or HBA function reset), this routine will wait for the
10143 * outstanding mailbox command to complete before invoking the mailbox
10144 * sub-system flush routine to gracefully bring down mailbox sub-system.
James Smarte59058c2008-08-24 21:49:00 -040010145 **/
James Smart3772a992009-05-22 14:50:54 -040010146void
James Smart618a5232012-06-12 13:54:36 -040010147lpfc_sli_mbox_sys_shutdown(struct lpfc_hba *phba, int mbx_action)
James Smartb4c02652006-07-06 15:50:43 -040010148{
James Smart3772a992009-05-22 14:50:54 -040010149 struct lpfc_sli *psli = &phba->sli;
James Smart3772a992009-05-22 14:50:54 -040010150 unsigned long timeout;
10151
James Smart618a5232012-06-12 13:54:36 -040010152 if (mbx_action == LPFC_MBX_NO_WAIT) {
10153 /* delay 100ms for port state */
10154 msleep(100);
10155 lpfc_sli_mbox_sys_flush(phba);
10156 return;
10157 }
James Smarta183a152011-10-10 21:32:43 -040010158 timeout = msecs_to_jiffies(LPFC_MBOX_TMO * 1000) + jiffies;
James Smartd7069f02012-03-01 22:36:29 -050010159
James Smart3772a992009-05-22 14:50:54 -040010160 spin_lock_irq(&phba->hbalock);
10161 psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
James Smart3772a992009-05-22 14:50:54 -040010162
10163 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
James Smart3772a992009-05-22 14:50:54 -040010164 /* Determine how long we might wait for the active mailbox
10165 * command to be gracefully completed by firmware.
10166 */
James Smarta183a152011-10-10 21:32:43 -040010167 if (phba->sli.mbox_active)
10168 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
10169 phba->sli.mbox_active) *
10170 1000) + jiffies;
10171 spin_unlock_irq(&phba->hbalock);
10172
James Smart3772a992009-05-22 14:50:54 -040010173 while (phba->sli.mbox_active) {
10174 /* Check active mailbox complete status every 2ms */
10175 msleep(2);
10176 if (time_after(jiffies, timeout))
10177 /* Timeout, let the mailbox flush routine to
10178 * forcefully release active mailbox command
10179 */
10180 break;
10181 }
James Smartd7069f02012-03-01 22:36:29 -050010182 } else
10183 spin_unlock_irq(&phba->hbalock);
10184
James Smart3772a992009-05-22 14:50:54 -040010185 lpfc_sli_mbox_sys_flush(phba);
10186}
10187
10188/**
10189 * lpfc_sli_eratt_read - read sli-3 error attention events
10190 * @phba: Pointer to HBA context.
10191 *
10192 * This function is called to read the SLI3 device error attention registers
10193 * for possible error attention events. The caller must hold the hostlock
10194 * with spin_lock_irq().
10195 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030010196 * This function returns 1 when there is Error Attention in the Host Attention
James Smart3772a992009-05-22 14:50:54 -040010197 * Register and returns 0 otherwise.
10198 **/
10199static int
10200lpfc_sli_eratt_read(struct lpfc_hba *phba)
10201{
James Smarted957682007-06-17 19:56:37 -050010202 uint32_t ha_copy;
James Smartb4c02652006-07-06 15:50:43 -040010203
James Smart3772a992009-05-22 14:50:54 -040010204 /* Read chip Host Attention (HA) register */
James Smart9940b972011-03-11 16:06:12 -050010205 if (lpfc_readl(phba->HAregaddr, &ha_copy))
10206 goto unplug_err;
10207
James Smart3772a992009-05-22 14:50:54 -040010208 if (ha_copy & HA_ERATT) {
10209 /* Read host status register to retrieve error event */
James Smart9940b972011-03-11 16:06:12 -050010210 if (lpfc_sli_read_hs(phba))
10211 goto unplug_err;
James Smartb4c02652006-07-06 15:50:43 -040010212
James Smart3772a992009-05-22 14:50:54 -040010213 /* Check if there is a deferred error condition is active */
10214 if ((HS_FFER1 & phba->work_hs) &&
10215 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
James Smartdcf2a4e2010-09-29 11:18:53 -040010216 HS_FFER6 | HS_FFER7 | HS_FFER8) & phba->work_hs)) {
James Smart3772a992009-05-22 14:50:54 -040010217 phba->hba_flag |= DEFER_ERATT;
James Smart3772a992009-05-22 14:50:54 -040010218 /* Clear all interrupt enable conditions */
10219 writel(0, phba->HCregaddr);
10220 readl(phba->HCregaddr);
10221 }
10222
10223 /* Set the driver HA work bitmap */
James Smart3772a992009-05-22 14:50:54 -040010224 phba->work_ha |= HA_ERATT;
10225 /* Indicate polling handles this ERATT */
10226 phba->hba_flag |= HBA_ERATT_HANDLED;
James Smart3772a992009-05-22 14:50:54 -040010227 return 1;
James Smartb4c02652006-07-06 15:50:43 -040010228 }
James Smart3772a992009-05-22 14:50:54 -040010229 return 0;
James Smart9940b972011-03-11 16:06:12 -050010230
10231unplug_err:
10232 /* Set the driver HS work bitmap */
10233 phba->work_hs |= UNPLUG_ERR;
10234 /* Set the driver HA work bitmap */
10235 phba->work_ha |= HA_ERATT;
10236 /* Indicate polling handles this ERATT */
10237 phba->hba_flag |= HBA_ERATT_HANDLED;
10238 return 1;
James Smartb4c02652006-07-06 15:50:43 -040010239}
10240
James Smarte59058c2008-08-24 21:49:00 -040010241/**
James Smartda0436e2009-05-22 14:51:39 -040010242 * lpfc_sli4_eratt_read - read sli-4 error attention events
10243 * @phba: Pointer to HBA context.
10244 *
10245 * This function is called to read the SLI4 device error attention registers
10246 * for possible error attention events. The caller must hold the hostlock
10247 * with spin_lock_irq().
10248 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030010249 * This function returns 1 when there is Error Attention in the Host Attention
James Smartda0436e2009-05-22 14:51:39 -040010250 * Register and returns 0 otherwise.
10251 **/
10252static int
10253lpfc_sli4_eratt_read(struct lpfc_hba *phba)
10254{
10255 uint32_t uerr_sta_hi, uerr_sta_lo;
James Smart2fcee4b2010-12-15 17:57:46 -050010256 uint32_t if_type, portsmphr;
10257 struct lpfc_register portstat_reg;
James Smartda0436e2009-05-22 14:51:39 -040010258
James Smart2fcee4b2010-12-15 17:57:46 -050010259 /*
10260 * For now, use the SLI4 device internal unrecoverable error
James Smartda0436e2009-05-22 14:51:39 -040010261 * registers for error attention. This can be changed later.
10262 */
James Smart2fcee4b2010-12-15 17:57:46 -050010263 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
10264 switch (if_type) {
10265 case LPFC_SLI_INTF_IF_TYPE_0:
James Smart9940b972011-03-11 16:06:12 -050010266 if (lpfc_readl(phba->sli4_hba.u.if_type0.UERRLOregaddr,
10267 &uerr_sta_lo) ||
10268 lpfc_readl(phba->sli4_hba.u.if_type0.UERRHIregaddr,
10269 &uerr_sta_hi)) {
10270 phba->work_hs |= UNPLUG_ERR;
10271 phba->work_ha |= HA_ERATT;
10272 phba->hba_flag |= HBA_ERATT_HANDLED;
10273 return 1;
10274 }
James Smart2fcee4b2010-12-15 17:57:46 -050010275 if ((~phba->sli4_hba.ue_mask_lo & uerr_sta_lo) ||
10276 (~phba->sli4_hba.ue_mask_hi & uerr_sta_hi)) {
10277 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10278 "1423 HBA Unrecoverable error: "
10279 "uerr_lo_reg=0x%x, uerr_hi_reg=0x%x, "
10280 "ue_mask_lo_reg=0x%x, "
10281 "ue_mask_hi_reg=0x%x\n",
10282 uerr_sta_lo, uerr_sta_hi,
10283 phba->sli4_hba.ue_mask_lo,
10284 phba->sli4_hba.ue_mask_hi);
10285 phba->work_status[0] = uerr_sta_lo;
10286 phba->work_status[1] = uerr_sta_hi;
10287 phba->work_ha |= HA_ERATT;
10288 phba->hba_flag |= HBA_ERATT_HANDLED;
10289 return 1;
10290 }
10291 break;
10292 case LPFC_SLI_INTF_IF_TYPE_2:
James Smart9940b972011-03-11 16:06:12 -050010293 if (lpfc_readl(phba->sli4_hba.u.if_type2.STATUSregaddr,
10294 &portstat_reg.word0) ||
10295 lpfc_readl(phba->sli4_hba.PSMPHRregaddr,
10296 &portsmphr)){
10297 phba->work_hs |= UNPLUG_ERR;
10298 phba->work_ha |= HA_ERATT;
10299 phba->hba_flag |= HBA_ERATT_HANDLED;
10300 return 1;
10301 }
James Smart2fcee4b2010-12-15 17:57:46 -050010302 if (bf_get(lpfc_sliport_status_err, &portstat_reg)) {
10303 phba->work_status[0] =
10304 readl(phba->sli4_hba.u.if_type2.ERR1regaddr);
10305 phba->work_status[1] =
10306 readl(phba->sli4_hba.u.if_type2.ERR2regaddr);
10307 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart2e90f4b2011-12-13 13:22:37 -050010308 "2885 Port Status Event: "
James Smart2fcee4b2010-12-15 17:57:46 -050010309 "port status reg 0x%x, "
10310 "port smphr reg 0x%x, "
10311 "error 1=0x%x, error 2=0x%x\n",
10312 portstat_reg.word0,
10313 portsmphr,
10314 phba->work_status[0],
10315 phba->work_status[1]);
10316 phba->work_ha |= HA_ERATT;
10317 phba->hba_flag |= HBA_ERATT_HANDLED;
10318 return 1;
10319 }
10320 break;
10321 case LPFC_SLI_INTF_IF_TYPE_1:
10322 default:
James Smarta747c9c2009-11-18 15:41:10 -050010323 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart2fcee4b2010-12-15 17:57:46 -050010324 "2886 HBA Error Attention on unsupported "
10325 "if type %d.", if_type);
James Smarta747c9c2009-11-18 15:41:10 -050010326 return 1;
James Smartda0436e2009-05-22 14:51:39 -040010327 }
James Smart2fcee4b2010-12-15 17:57:46 -050010328
James Smartda0436e2009-05-22 14:51:39 -040010329 return 0;
10330}
10331
10332/**
James Smart3621a712009-04-06 18:47:14 -040010333 * lpfc_sli_check_eratt - check error attention events
James Smart93996272008-08-24 21:50:30 -040010334 * @phba: Pointer to HBA context.
10335 *
James Smart3772a992009-05-22 14:50:54 -040010336 * This function is called from timer soft interrupt context to check HBA's
James Smart93996272008-08-24 21:50:30 -040010337 * error attention register bit for error attention events.
10338 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030010339 * This function returns 1 when there is Error Attention in the Host Attention
James Smart93996272008-08-24 21:50:30 -040010340 * Register and returns 0 otherwise.
10341 **/
10342int
10343lpfc_sli_check_eratt(struct lpfc_hba *phba)
10344{
10345 uint32_t ha_copy;
10346
10347 /* If somebody is waiting to handle an eratt, don't process it
10348 * here. The brdkill function will do this.
10349 */
10350 if (phba->link_flag & LS_IGNORE_ERATT)
10351 return 0;
10352
10353 /* Check if interrupt handler handles this ERATT */
10354 spin_lock_irq(&phba->hbalock);
10355 if (phba->hba_flag & HBA_ERATT_HANDLED) {
10356 /* Interrupt handler has handled ERATT */
10357 spin_unlock_irq(&phba->hbalock);
10358 return 0;
10359 }
10360
James Smarta257bf92009-04-06 18:48:10 -040010361 /*
10362 * If there is deferred error attention, do not check for error
10363 * attention
10364 */
10365 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
10366 spin_unlock_irq(&phba->hbalock);
10367 return 0;
10368 }
10369
James Smart3772a992009-05-22 14:50:54 -040010370 /* If PCI channel is offline, don't process it */
10371 if (unlikely(pci_channel_offline(phba->pcidev))) {
James Smart93996272008-08-24 21:50:30 -040010372 spin_unlock_irq(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -040010373 return 0;
10374 }
10375
10376 switch (phba->sli_rev) {
10377 case LPFC_SLI_REV2:
10378 case LPFC_SLI_REV3:
10379 /* Read chip Host Attention (HA) register */
10380 ha_copy = lpfc_sli_eratt_read(phba);
10381 break;
James Smartda0436e2009-05-22 14:51:39 -040010382 case LPFC_SLI_REV4:
James Smart2fcee4b2010-12-15 17:57:46 -050010383 /* Read device Uncoverable Error (UERR) registers */
James Smartda0436e2009-05-22 14:51:39 -040010384 ha_copy = lpfc_sli4_eratt_read(phba);
10385 break;
James Smart3772a992009-05-22 14:50:54 -040010386 default:
10387 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10388 "0299 Invalid SLI revision (%d)\n",
10389 phba->sli_rev);
10390 ha_copy = 0;
10391 break;
James Smart93996272008-08-24 21:50:30 -040010392 }
10393 spin_unlock_irq(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -040010394
10395 return ha_copy;
10396}
10397
10398/**
10399 * lpfc_intr_state_check - Check device state for interrupt handling
10400 * @phba: Pointer to HBA context.
10401 *
10402 * This inline routine checks whether a device or its PCI slot is in a state
10403 * that the interrupt should be handled.
10404 *
10405 * This function returns 0 if the device or the PCI slot is in a state that
10406 * interrupt should be handled, otherwise -EIO.
10407 */
10408static inline int
10409lpfc_intr_state_check(struct lpfc_hba *phba)
10410{
10411 /* If the pci channel is offline, ignore all the interrupts */
10412 if (unlikely(pci_channel_offline(phba->pcidev)))
10413 return -EIO;
10414
10415 /* Update device level interrupt statistics */
10416 phba->sli.slistat.sli_intr++;
10417
10418 /* Ignore all interrupts during initialization. */
10419 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
10420 return -EIO;
10421
James Smart93996272008-08-24 21:50:30 -040010422 return 0;
10423}
10424
10425/**
James Smart3772a992009-05-22 14:50:54 -040010426 * lpfc_sli_sp_intr_handler - Slow-path interrupt handler to SLI-3 device
James Smarte59058c2008-08-24 21:49:00 -040010427 * @irq: Interrupt number.
10428 * @dev_id: The device context pointer.
10429 *
James Smart93996272008-08-24 21:50:30 -040010430 * This function is directly called from the PCI layer as an interrupt
James Smart3772a992009-05-22 14:50:54 -040010431 * service routine when device with SLI-3 interface spec is enabled with
10432 * MSI-X multi-message interrupt mode and there are slow-path events in
10433 * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
10434 * interrupt mode, this function is called as part of the device-level
10435 * interrupt handler. When the PCI slot is in error recovery or the HBA
10436 * is undergoing initialization, the interrupt handler will not process
10437 * the interrupt. The link attention and ELS ring attention events are
10438 * handled by the worker thread. The interrupt handler signals the worker
10439 * thread and returns for these events. This function is called without
10440 * any lock held. It gets the hbalock to access and update SLI data
James Smart93996272008-08-24 21:50:30 -040010441 * structures.
10442 *
10443 * This function returns IRQ_HANDLED when interrupt is handled else it
10444 * returns IRQ_NONE.
James Smarte59058c2008-08-24 21:49:00 -040010445 **/
dea31012005-04-17 16:05:31 -050010446irqreturn_t
James Smart3772a992009-05-22 14:50:54 -040010447lpfc_sli_sp_intr_handler(int irq, void *dev_id)
dea31012005-04-17 16:05:31 -050010448{
James Smart2e0fef82007-06-17 19:56:36 -050010449 struct lpfc_hba *phba;
James Smarta747c9c2009-11-18 15:41:10 -050010450 uint32_t ha_copy, hc_copy;
dea31012005-04-17 16:05:31 -050010451 uint32_t work_ha_copy;
10452 unsigned long status;
James Smart5b75da22008-12-04 22:39:35 -050010453 unsigned long iflag;
dea31012005-04-17 16:05:31 -050010454 uint32_t control;
10455
James Smart92d7f7b2007-06-17 19:56:38 -050010456 MAILBOX_t *mbox, *pmbox;
James Smart858c9f62007-06-17 19:56:39 -050010457 struct lpfc_vport *vport;
10458 struct lpfc_nodelist *ndlp;
10459 struct lpfc_dmabuf *mp;
James Smart92d7f7b2007-06-17 19:56:38 -050010460 LPFC_MBOXQ_t *pmb;
10461 int rc;
10462
dea31012005-04-17 16:05:31 -050010463 /*
10464 * Get the driver's phba structure from the dev_id and
10465 * assume the HBA is not interrupting.
10466 */
James Smart93996272008-08-24 21:50:30 -040010467 phba = (struct lpfc_hba *)dev_id;
dea31012005-04-17 16:05:31 -050010468
10469 if (unlikely(!phba))
10470 return IRQ_NONE;
10471
dea31012005-04-17 16:05:31 -050010472 /*
James Smart93996272008-08-24 21:50:30 -040010473 * Stuff needs to be attented to when this function is invoked as an
10474 * individual interrupt handler in MSI-X multi-message interrupt mode
dea31012005-04-17 16:05:31 -050010475 */
James Smart93996272008-08-24 21:50:30 -040010476 if (phba->intr_type == MSIX) {
James Smart3772a992009-05-22 14:50:54 -040010477 /* Check device state for handling interrupt */
10478 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -040010479 return IRQ_NONE;
10480 /* Need to read HA REG for slow-path events */
James Smart5b75da22008-12-04 22:39:35 -050010481 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart9940b972011-03-11 16:06:12 -050010482 if (lpfc_readl(phba->HAregaddr, &ha_copy))
10483 goto unplug_error;
James Smart93996272008-08-24 21:50:30 -040010484 /* If somebody is waiting to handle an eratt don't process it
10485 * here. The brdkill function will do this.
10486 */
10487 if (phba->link_flag & LS_IGNORE_ERATT)
10488 ha_copy &= ~HA_ERATT;
10489 /* Check the need for handling ERATT in interrupt handler */
10490 if (ha_copy & HA_ERATT) {
10491 if (phba->hba_flag & HBA_ERATT_HANDLED)
10492 /* ERATT polling has handled ERATT */
10493 ha_copy &= ~HA_ERATT;
10494 else
10495 /* Indicate interrupt handler handles ERATT */
10496 phba->hba_flag |= HBA_ERATT_HANDLED;
10497 }
James Smarta257bf92009-04-06 18:48:10 -040010498
10499 /*
10500 * If there is deferred error attention, do not check for any
10501 * interrupt.
10502 */
10503 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
James Smart3772a992009-05-22 14:50:54 -040010504 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -040010505 return IRQ_NONE;
10506 }
10507
James Smart93996272008-08-24 21:50:30 -040010508 /* Clear up only attention source related to slow-path */
James Smart9940b972011-03-11 16:06:12 -050010509 if (lpfc_readl(phba->HCregaddr, &hc_copy))
10510 goto unplug_error;
10511
James Smarta747c9c2009-11-18 15:41:10 -050010512 writel(hc_copy & ~(HC_MBINT_ENA | HC_R2INT_ENA |
10513 HC_LAINT_ENA | HC_ERINT_ENA),
10514 phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -040010515 writel((ha_copy & (HA_MBATT | HA_R2_CLR_MSK)),
10516 phba->HAregaddr);
James Smarta747c9c2009-11-18 15:41:10 -050010517 writel(hc_copy, phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -040010518 readl(phba->HAregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -050010519 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -040010520 } else
10521 ha_copy = phba->ha_copy;
dea31012005-04-17 16:05:31 -050010522
dea31012005-04-17 16:05:31 -050010523 work_ha_copy = ha_copy & phba->work_ha_mask;
10524
James Smart93996272008-08-24 21:50:30 -040010525 if (work_ha_copy) {
dea31012005-04-17 16:05:31 -050010526 if (work_ha_copy & HA_LATT) {
10527 if (phba->sli.sli_flag & LPFC_PROCESS_LA) {
10528 /*
10529 * Turn off Link Attention interrupts
10530 * until CLEAR_LA done
10531 */
James Smart5b75da22008-12-04 22:39:35 -050010532 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -050010533 phba->sli.sli_flag &= ~LPFC_PROCESS_LA;
James Smart9940b972011-03-11 16:06:12 -050010534 if (lpfc_readl(phba->HCregaddr, &control))
10535 goto unplug_error;
dea31012005-04-17 16:05:31 -050010536 control &= ~HC_LAINT_ENA;
10537 writel(control, phba->HCregaddr);
10538 readl(phba->HCregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -050010539 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -050010540 }
10541 else
10542 work_ha_copy &= ~HA_LATT;
10543 }
10544
James Smart93996272008-08-24 21:50:30 -040010545 if (work_ha_copy & ~(HA_ERATT | HA_MBATT | HA_LATT)) {
James Smart858c9f62007-06-17 19:56:39 -050010546 /*
10547 * Turn off Slow Rings interrupts, LPFC_ELS_RING is
10548 * the only slow ring.
10549 */
10550 status = (work_ha_copy &
10551 (HA_RXMASK << (4*LPFC_ELS_RING)));
10552 status >>= (4*LPFC_ELS_RING);
10553 if (status & HA_RXMASK) {
James Smart5b75da22008-12-04 22:39:35 -050010554 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart9940b972011-03-11 16:06:12 -050010555 if (lpfc_readl(phba->HCregaddr, &control))
10556 goto unplug_error;
James Smarta58cbd52007-08-02 11:09:43 -040010557
10558 lpfc_debugfs_slow_ring_trc(phba,
10559 "ISR slow ring: ctl:x%x stat:x%x isrcnt:x%x",
10560 control, status,
10561 (uint32_t)phba->sli.slistat.sli_intr);
10562
James Smart858c9f62007-06-17 19:56:39 -050010563 if (control & (HC_R0INT_ENA << LPFC_ELS_RING)) {
James Smarta58cbd52007-08-02 11:09:43 -040010564 lpfc_debugfs_slow_ring_trc(phba,
10565 "ISR Disable ring:"
10566 "pwork:x%x hawork:x%x wait:x%x",
10567 phba->work_ha, work_ha_copy,
10568 (uint32_t)((unsigned long)
James Smart5e9d9b82008-06-14 22:52:53 -040010569 &phba->work_waitq));
James Smarta58cbd52007-08-02 11:09:43 -040010570
James Smart858c9f62007-06-17 19:56:39 -050010571 control &=
10572 ~(HC_R0INT_ENA << LPFC_ELS_RING);
dea31012005-04-17 16:05:31 -050010573 writel(control, phba->HCregaddr);
10574 readl(phba->HCregaddr); /* flush */
dea31012005-04-17 16:05:31 -050010575 }
James Smarta58cbd52007-08-02 11:09:43 -040010576 else {
10577 lpfc_debugfs_slow_ring_trc(phba,
10578 "ISR slow ring: pwork:"
10579 "x%x hawork:x%x wait:x%x",
10580 phba->work_ha, work_ha_copy,
10581 (uint32_t)((unsigned long)
James Smart5e9d9b82008-06-14 22:52:53 -040010582 &phba->work_waitq));
James Smarta58cbd52007-08-02 11:09:43 -040010583 }
James Smart5b75da22008-12-04 22:39:35 -050010584 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -050010585 }
10586 }
James Smart5b75da22008-12-04 22:39:35 -050010587 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -040010588 if (work_ha_copy & HA_ERATT) {
James Smart9940b972011-03-11 16:06:12 -050010589 if (lpfc_sli_read_hs(phba))
10590 goto unplug_error;
James Smarta257bf92009-04-06 18:48:10 -040010591 /*
10592 * Check if there is a deferred error condition
10593 * is active
10594 */
10595 if ((HS_FFER1 & phba->work_hs) &&
10596 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
James Smartdcf2a4e2010-09-29 11:18:53 -040010597 HS_FFER6 | HS_FFER7 | HS_FFER8) &
10598 phba->work_hs)) {
James Smarta257bf92009-04-06 18:48:10 -040010599 phba->hba_flag |= DEFER_ERATT;
10600 /* Clear all interrupt enable conditions */
10601 writel(0, phba->HCregaddr);
10602 readl(phba->HCregaddr);
10603 }
10604 }
10605
James Smart93996272008-08-24 21:50:30 -040010606 if ((work_ha_copy & HA_MBATT) && (phba->sli.mbox_active)) {
James Smart92d7f7b2007-06-17 19:56:38 -050010607 pmb = phba->sli.mbox_active;
James Smart04c68492009-05-22 14:52:52 -040010608 pmbox = &pmb->u.mb;
James Smart34b02dc2008-08-24 21:49:55 -040010609 mbox = phba->mbox;
James Smart858c9f62007-06-17 19:56:39 -050010610 vport = pmb->vport;
James Smart92d7f7b2007-06-17 19:56:38 -050010611
10612 /* First check out the status word */
10613 lpfc_sli_pcimem_bcopy(mbox, pmbox, sizeof(uint32_t));
10614 if (pmbox->mbxOwner != OWN_HOST) {
James Smart5b75da22008-12-04 22:39:35 -050010615 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart92d7f7b2007-06-17 19:56:38 -050010616 /*
10617 * Stray Mailbox Interrupt, mbxCommand <cmd>
10618 * mbxStatus <status>
10619 */
James Smart09372822008-01-11 01:52:54 -050010620 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
James Smart92d7f7b2007-06-17 19:56:38 -050010621 LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -040010622 "(%d):0304 Stray Mailbox "
James Smart92d7f7b2007-06-17 19:56:38 -050010623 "Interrupt mbxCommand x%x "
10624 "mbxStatus x%x\n",
James Smarte8b62012007-08-02 11:10:09 -040010625 (vport ? vport->vpi : 0),
James Smart92d7f7b2007-06-17 19:56:38 -050010626 pmbox->mbxCommand,
10627 pmbox->mbxStatus);
James Smart09372822008-01-11 01:52:54 -050010628 /* clear mailbox attention bit */
10629 work_ha_copy &= ~HA_MBATT;
10630 } else {
James Smart97eab632008-04-07 10:16:05 -040010631 phba->sli.mbox_active = NULL;
James Smart5b75da22008-12-04 22:39:35 -050010632 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart09372822008-01-11 01:52:54 -050010633 phba->last_completion_time = jiffies;
10634 del_timer(&phba->sli.mbox_tmo);
James Smart09372822008-01-11 01:52:54 -050010635 if (pmb->mbox_cmpl) {
10636 lpfc_sli_pcimem_bcopy(mbox, pmbox,
10637 MAILBOX_CMD_SIZE);
James Smart7a470272010-03-15 11:25:20 -040010638 if (pmb->out_ext_byte_len &&
10639 pmb->context2)
10640 lpfc_sli_pcimem_bcopy(
10641 phba->mbox_ext,
10642 pmb->context2,
10643 pmb->out_ext_byte_len);
James Smart858c9f62007-06-17 19:56:39 -050010644 }
James Smart09372822008-01-11 01:52:54 -050010645 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
10646 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
10647
10648 lpfc_debugfs_disc_trc(vport,
10649 LPFC_DISC_TRC_MBOX_VPORT,
10650 "MBOX dflt rpi: : "
10651 "status:x%x rpi:x%x",
10652 (uint32_t)pmbox->mbxStatus,
10653 pmbox->un.varWords[0], 0);
10654
10655 if (!pmbox->mbxStatus) {
10656 mp = (struct lpfc_dmabuf *)
10657 (pmb->context1);
10658 ndlp = (struct lpfc_nodelist *)
10659 pmb->context2;
10660
10661 /* Reg_LOGIN of dflt RPI was
10662 * successful. new lets get
10663 * rid of the RPI using the
10664 * same mbox buffer.
10665 */
10666 lpfc_unreg_login(phba,
10667 vport->vpi,
10668 pmbox->un.varWords[0],
10669 pmb);
10670 pmb->mbox_cmpl =
10671 lpfc_mbx_cmpl_dflt_rpi;
10672 pmb->context1 = mp;
10673 pmb->context2 = ndlp;
10674 pmb->vport = vport;
James Smart58da1ff2008-04-07 10:15:56 -040010675 rc = lpfc_sli_issue_mbox(phba,
10676 pmb,
10677 MBX_NOWAIT);
10678 if (rc != MBX_BUSY)
10679 lpfc_printf_log(phba,
10680 KERN_ERR,
10681 LOG_MBOX | LOG_SLI,
James Smartd7c255b2008-08-24 21:50:00 -040010682 "0350 rc should have"
James Smart6a9c52c2009-10-02 15:16:51 -040010683 "been MBX_BUSY\n");
James Smart3772a992009-05-22 14:50:54 -040010684 if (rc != MBX_NOT_FINISHED)
10685 goto send_current_mbox;
James Smart09372822008-01-11 01:52:54 -050010686 }
10687 }
James Smart5b75da22008-12-04 22:39:35 -050010688 spin_lock_irqsave(
10689 &phba->pport->work_port_lock,
10690 iflag);
James Smart09372822008-01-11 01:52:54 -050010691 phba->pport->work_port_events &=
10692 ~WORKER_MBOX_TMO;
James Smart5b75da22008-12-04 22:39:35 -050010693 spin_unlock_irqrestore(
10694 &phba->pport->work_port_lock,
10695 iflag);
James Smart09372822008-01-11 01:52:54 -050010696 lpfc_mbox_cmpl_put(phba, pmb);
James Smart858c9f62007-06-17 19:56:39 -050010697 }
James Smart97eab632008-04-07 10:16:05 -040010698 } else
James Smart5b75da22008-12-04 22:39:35 -050010699 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -040010700
James Smart92d7f7b2007-06-17 19:56:38 -050010701 if ((work_ha_copy & HA_MBATT) &&
10702 (phba->sli.mbox_active == NULL)) {
James Smart858c9f62007-06-17 19:56:39 -050010703send_current_mbox:
James Smart92d7f7b2007-06-17 19:56:38 -050010704 /* Process next mailbox command if there is one */
James Smart58da1ff2008-04-07 10:15:56 -040010705 do {
10706 rc = lpfc_sli_issue_mbox(phba, NULL,
10707 MBX_NOWAIT);
10708 } while (rc == MBX_NOT_FINISHED);
10709 if (rc != MBX_SUCCESS)
10710 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
10711 LOG_SLI, "0349 rc should be "
James Smart6a9c52c2009-10-02 15:16:51 -040010712 "MBX_SUCCESS\n");
James Smart92d7f7b2007-06-17 19:56:38 -050010713 }
10714
James Smart5b75da22008-12-04 22:39:35 -050010715 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -050010716 phba->work_ha |= work_ha_copy;
James Smart5b75da22008-12-04 22:39:35 -050010717 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart5e9d9b82008-06-14 22:52:53 -040010718 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -050010719 }
James Smart93996272008-08-24 21:50:30 -040010720 return IRQ_HANDLED;
James Smart9940b972011-03-11 16:06:12 -050010721unplug_error:
10722 spin_unlock_irqrestore(&phba->hbalock, iflag);
10723 return IRQ_HANDLED;
dea31012005-04-17 16:05:31 -050010724
James Smart3772a992009-05-22 14:50:54 -040010725} /* lpfc_sli_sp_intr_handler */
James Smart93996272008-08-24 21:50:30 -040010726
10727/**
James Smart3772a992009-05-22 14:50:54 -040010728 * lpfc_sli_fp_intr_handler - Fast-path interrupt handler to SLI-3 device.
James Smart93996272008-08-24 21:50:30 -040010729 * @irq: Interrupt number.
10730 * @dev_id: The device context pointer.
10731 *
10732 * This function is directly called from the PCI layer as an interrupt
James Smart3772a992009-05-22 14:50:54 -040010733 * service routine when device with SLI-3 interface spec is enabled with
10734 * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
10735 * ring event in the HBA. However, when the device is enabled with either
10736 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
10737 * device-level interrupt handler. When the PCI slot is in error recovery
10738 * or the HBA is undergoing initialization, the interrupt handler will not
10739 * process the interrupt. The SCSI FCP fast-path ring event are handled in
10740 * the intrrupt context. This function is called without any lock held.
10741 * It gets the hbalock to access and update SLI data structures.
James Smart93996272008-08-24 21:50:30 -040010742 *
10743 * This function returns IRQ_HANDLED when interrupt is handled else it
10744 * returns IRQ_NONE.
10745 **/
10746irqreturn_t
James Smart3772a992009-05-22 14:50:54 -040010747lpfc_sli_fp_intr_handler(int irq, void *dev_id)
James Smart93996272008-08-24 21:50:30 -040010748{
10749 struct lpfc_hba *phba;
10750 uint32_t ha_copy;
10751 unsigned long status;
James Smart5b75da22008-12-04 22:39:35 -050010752 unsigned long iflag;
James Smart93996272008-08-24 21:50:30 -040010753
10754 /* Get the driver's phba structure from the dev_id and
10755 * assume the HBA is not interrupting.
10756 */
10757 phba = (struct lpfc_hba *) dev_id;
10758
10759 if (unlikely(!phba))
10760 return IRQ_NONE;
dea31012005-04-17 16:05:31 -050010761
10762 /*
James Smart93996272008-08-24 21:50:30 -040010763 * Stuff needs to be attented to when this function is invoked as an
10764 * individual interrupt handler in MSI-X multi-message interrupt mode
dea31012005-04-17 16:05:31 -050010765 */
James Smart93996272008-08-24 21:50:30 -040010766 if (phba->intr_type == MSIX) {
James Smart3772a992009-05-22 14:50:54 -040010767 /* Check device state for handling interrupt */
10768 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -040010769 return IRQ_NONE;
10770 /* Need to read HA REG for FCP ring and other ring events */
James Smart9940b972011-03-11 16:06:12 -050010771 if (lpfc_readl(phba->HAregaddr, &ha_copy))
10772 return IRQ_HANDLED;
James Smart93996272008-08-24 21:50:30 -040010773 /* Clear up only attention source related to fast-path */
James Smart5b75da22008-12-04 22:39:35 -050010774 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -040010775 /*
10776 * If there is deferred error attention, do not check for
10777 * any interrupt.
10778 */
10779 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
James Smart3772a992009-05-22 14:50:54 -040010780 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -040010781 return IRQ_NONE;
10782 }
James Smart93996272008-08-24 21:50:30 -040010783 writel((ha_copy & (HA_R0_CLR_MSK | HA_R1_CLR_MSK)),
10784 phba->HAregaddr);
10785 readl(phba->HAregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -050010786 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -040010787 } else
10788 ha_copy = phba->ha_copy;
10789
10790 /*
10791 * Process all events on FCP ring. Take the optimized path for FCP IO.
10792 */
10793 ha_copy &= ~(phba->work_ha_mask);
10794
10795 status = (ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
dea31012005-04-17 16:05:31 -050010796 status >>= (4*LPFC_FCP_RING);
James Smart858c9f62007-06-17 19:56:39 -050010797 if (status & HA_RXMASK)
dea31012005-04-17 16:05:31 -050010798 lpfc_sli_handle_fast_ring_event(phba,
10799 &phba->sli.ring[LPFC_FCP_RING],
10800 status);
James Smarta4bc3372006-12-02 13:34:16 -050010801
10802 if (phba->cfg_multi_ring_support == 2) {
10803 /*
James Smart93996272008-08-24 21:50:30 -040010804 * Process all events on extra ring. Take the optimized path
10805 * for extra ring IO.
James Smarta4bc3372006-12-02 13:34:16 -050010806 */
James Smart93996272008-08-24 21:50:30 -040010807 status = (ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
James Smarta4bc3372006-12-02 13:34:16 -050010808 status >>= (4*LPFC_EXTRA_RING);
James Smart858c9f62007-06-17 19:56:39 -050010809 if (status & HA_RXMASK) {
James Smarta4bc3372006-12-02 13:34:16 -050010810 lpfc_sli_handle_fast_ring_event(phba,
10811 &phba->sli.ring[LPFC_EXTRA_RING],
10812 status);
10813 }
10814 }
dea31012005-04-17 16:05:31 -050010815 return IRQ_HANDLED;
James Smart3772a992009-05-22 14:50:54 -040010816} /* lpfc_sli_fp_intr_handler */
dea31012005-04-17 16:05:31 -050010817
James Smart93996272008-08-24 21:50:30 -040010818/**
James Smart3772a992009-05-22 14:50:54 -040010819 * lpfc_sli_intr_handler - Device-level interrupt handler to SLI-3 device
James Smart93996272008-08-24 21:50:30 -040010820 * @irq: Interrupt number.
10821 * @dev_id: The device context pointer.
10822 *
James Smart3772a992009-05-22 14:50:54 -040010823 * This function is the HBA device-level interrupt handler to device with
10824 * SLI-3 interface spec, called from the PCI layer when either MSI or
10825 * Pin-IRQ interrupt mode is enabled and there is an event in the HBA which
10826 * requires driver attention. This function invokes the slow-path interrupt
10827 * attention handling function and fast-path interrupt attention handling
10828 * function in turn to process the relevant HBA attention events. This
10829 * function is called without any lock held. It gets the hbalock to access
10830 * and update SLI data structures.
James Smart93996272008-08-24 21:50:30 -040010831 *
10832 * This function returns IRQ_HANDLED when interrupt is handled, else it
10833 * returns IRQ_NONE.
10834 **/
10835irqreturn_t
James Smart3772a992009-05-22 14:50:54 -040010836lpfc_sli_intr_handler(int irq, void *dev_id)
James Smart93996272008-08-24 21:50:30 -040010837{
10838 struct lpfc_hba *phba;
10839 irqreturn_t sp_irq_rc, fp_irq_rc;
10840 unsigned long status1, status2;
James Smarta747c9c2009-11-18 15:41:10 -050010841 uint32_t hc_copy;
James Smart93996272008-08-24 21:50:30 -040010842
10843 /*
10844 * Get the driver's phba structure from the dev_id and
10845 * assume the HBA is not interrupting.
10846 */
10847 phba = (struct lpfc_hba *) dev_id;
10848
10849 if (unlikely(!phba))
10850 return IRQ_NONE;
10851
James Smart3772a992009-05-22 14:50:54 -040010852 /* Check device state for handling interrupt */
10853 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -040010854 return IRQ_NONE;
10855
10856 spin_lock(&phba->hbalock);
James Smart9940b972011-03-11 16:06:12 -050010857 if (lpfc_readl(phba->HAregaddr, &phba->ha_copy)) {
10858 spin_unlock(&phba->hbalock);
10859 return IRQ_HANDLED;
10860 }
10861
James Smart93996272008-08-24 21:50:30 -040010862 if (unlikely(!phba->ha_copy)) {
10863 spin_unlock(&phba->hbalock);
10864 return IRQ_NONE;
10865 } else if (phba->ha_copy & HA_ERATT) {
10866 if (phba->hba_flag & HBA_ERATT_HANDLED)
10867 /* ERATT polling has handled ERATT */
10868 phba->ha_copy &= ~HA_ERATT;
10869 else
10870 /* Indicate interrupt handler handles ERATT */
10871 phba->hba_flag |= HBA_ERATT_HANDLED;
10872 }
10873
James Smarta257bf92009-04-06 18:48:10 -040010874 /*
10875 * If there is deferred error attention, do not check for any interrupt.
10876 */
10877 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
Dan Carpenterec21b3b2010-08-08 00:15:17 +020010878 spin_unlock(&phba->hbalock);
James Smarta257bf92009-04-06 18:48:10 -040010879 return IRQ_NONE;
10880 }
10881
James Smart93996272008-08-24 21:50:30 -040010882 /* Clear attention sources except link and error attentions */
James Smart9940b972011-03-11 16:06:12 -050010883 if (lpfc_readl(phba->HCregaddr, &hc_copy)) {
10884 spin_unlock(&phba->hbalock);
10885 return IRQ_HANDLED;
10886 }
James Smarta747c9c2009-11-18 15:41:10 -050010887 writel(hc_copy & ~(HC_MBINT_ENA | HC_R0INT_ENA | HC_R1INT_ENA
10888 | HC_R2INT_ENA | HC_LAINT_ENA | HC_ERINT_ENA),
10889 phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -040010890 writel((phba->ha_copy & ~(HA_LATT | HA_ERATT)), phba->HAregaddr);
James Smarta747c9c2009-11-18 15:41:10 -050010891 writel(hc_copy, phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -040010892 readl(phba->HAregaddr); /* flush */
10893 spin_unlock(&phba->hbalock);
10894
10895 /*
10896 * Invokes slow-path host attention interrupt handling as appropriate.
10897 */
10898
10899 /* status of events with mailbox and link attention */
10900 status1 = phba->ha_copy & (HA_MBATT | HA_LATT | HA_ERATT);
10901
10902 /* status of events with ELS ring */
10903 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_ELS_RING)));
10904 status2 >>= (4*LPFC_ELS_RING);
10905
10906 if (status1 || (status2 & HA_RXMASK))
James Smart3772a992009-05-22 14:50:54 -040010907 sp_irq_rc = lpfc_sli_sp_intr_handler(irq, dev_id);
James Smart93996272008-08-24 21:50:30 -040010908 else
10909 sp_irq_rc = IRQ_NONE;
10910
10911 /*
10912 * Invoke fast-path host attention interrupt handling as appropriate.
10913 */
10914
10915 /* status of events with FCP ring */
10916 status1 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
10917 status1 >>= (4*LPFC_FCP_RING);
10918
10919 /* status of events with extra ring */
10920 if (phba->cfg_multi_ring_support == 2) {
10921 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
10922 status2 >>= (4*LPFC_EXTRA_RING);
10923 } else
10924 status2 = 0;
10925
10926 if ((status1 & HA_RXMASK) || (status2 & HA_RXMASK))
James Smart3772a992009-05-22 14:50:54 -040010927 fp_irq_rc = lpfc_sli_fp_intr_handler(irq, dev_id);
James Smart93996272008-08-24 21:50:30 -040010928 else
10929 fp_irq_rc = IRQ_NONE;
10930
10931 /* Return device-level interrupt handling status */
10932 return (sp_irq_rc == IRQ_HANDLED) ? sp_irq_rc : fp_irq_rc;
James Smart3772a992009-05-22 14:50:54 -040010933} /* lpfc_sli_intr_handler */
James Smart4f774512009-05-22 14:52:35 -040010934
10935/**
10936 * lpfc_sli4_fcp_xri_abort_event_proc - Process fcp xri abort event
10937 * @phba: pointer to lpfc hba data structure.
10938 *
10939 * This routine is invoked by the worker thread to process all the pending
10940 * SLI4 FCP abort XRI events.
10941 **/
10942void lpfc_sli4_fcp_xri_abort_event_proc(struct lpfc_hba *phba)
10943{
10944 struct lpfc_cq_event *cq_event;
10945
10946 /* First, declare the fcp xri abort event has been handled */
10947 spin_lock_irq(&phba->hbalock);
10948 phba->hba_flag &= ~FCP_XRI_ABORT_EVENT;
10949 spin_unlock_irq(&phba->hbalock);
10950 /* Now, handle all the fcp xri abort events */
10951 while (!list_empty(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue)) {
10952 /* Get the first event from the head of the event queue */
10953 spin_lock_irq(&phba->hbalock);
10954 list_remove_head(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue,
10955 cq_event, struct lpfc_cq_event, list);
10956 spin_unlock_irq(&phba->hbalock);
10957 /* Notify aborted XRI for FCP work queue */
10958 lpfc_sli4_fcp_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
10959 /* Free the event processed back to the free pool */
10960 lpfc_sli4_cq_event_release(phba, cq_event);
10961 }
10962}
10963
10964/**
10965 * lpfc_sli4_els_xri_abort_event_proc - Process els xri abort event
10966 * @phba: pointer to lpfc hba data structure.
10967 *
10968 * This routine is invoked by the worker thread to process all the pending
10969 * SLI4 els abort xri events.
10970 **/
10971void lpfc_sli4_els_xri_abort_event_proc(struct lpfc_hba *phba)
10972{
10973 struct lpfc_cq_event *cq_event;
10974
10975 /* First, declare the els xri abort event has been handled */
10976 spin_lock_irq(&phba->hbalock);
10977 phba->hba_flag &= ~ELS_XRI_ABORT_EVENT;
10978 spin_unlock_irq(&phba->hbalock);
10979 /* Now, handle all the els xri abort events */
10980 while (!list_empty(&phba->sli4_hba.sp_els_xri_aborted_work_queue)) {
10981 /* Get the first event from the head of the event queue */
10982 spin_lock_irq(&phba->hbalock);
10983 list_remove_head(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
10984 cq_event, struct lpfc_cq_event, list);
10985 spin_unlock_irq(&phba->hbalock);
10986 /* Notify aborted XRI for ELS work queue */
10987 lpfc_sli4_els_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
10988 /* Free the event processed back to the free pool */
10989 lpfc_sli4_cq_event_release(phba, cq_event);
10990 }
10991}
10992
James Smart341af102010-01-26 23:07:37 -050010993/**
10994 * lpfc_sli4_iocb_param_transfer - Transfer pIocbOut and cmpl status to pIocbIn
10995 * @phba: pointer to lpfc hba data structure
10996 * @pIocbIn: pointer to the rspiocbq
10997 * @pIocbOut: pointer to the cmdiocbq
10998 * @wcqe: pointer to the complete wcqe
10999 *
11000 * This routine transfers the fields of a command iocbq to a response iocbq
11001 * by copying all the IOCB fields from command iocbq and transferring the
11002 * completion status information from the complete wcqe.
11003 **/
James Smart4f774512009-05-22 14:52:35 -040011004static void
James Smart341af102010-01-26 23:07:37 -050011005lpfc_sli4_iocb_param_transfer(struct lpfc_hba *phba,
11006 struct lpfc_iocbq *pIocbIn,
James Smart4f774512009-05-22 14:52:35 -040011007 struct lpfc_iocbq *pIocbOut,
11008 struct lpfc_wcqe_complete *wcqe)
11009{
James Smart341af102010-01-26 23:07:37 -050011010 unsigned long iflags;
James Smartacd68592012-01-18 16:25:09 -050011011 uint32_t status;
James Smart4f774512009-05-22 14:52:35 -040011012 size_t offset = offsetof(struct lpfc_iocbq, iocb);
11013
11014 memcpy((char *)pIocbIn + offset, (char *)pIocbOut + offset,
11015 sizeof(struct lpfc_iocbq) - offset);
James Smart4f774512009-05-22 14:52:35 -040011016 /* Map WCQE parameters into irspiocb parameters */
James Smartacd68592012-01-18 16:25:09 -050011017 status = bf_get(lpfc_wcqe_c_status, wcqe);
11018 pIocbIn->iocb.ulpStatus = (status & LPFC_IOCB_STATUS_MASK);
James Smart4f774512009-05-22 14:52:35 -040011019 if (pIocbOut->iocb_flag & LPFC_IO_FCP)
11020 if (pIocbIn->iocb.ulpStatus == IOSTAT_FCP_RSP_ERROR)
11021 pIocbIn->iocb.un.fcpi.fcpi_parm =
11022 pIocbOut->iocb.un.fcpi.fcpi_parm -
11023 wcqe->total_data_placed;
11024 else
11025 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
James Smart695a8142010-01-26 23:08:03 -050011026 else {
James Smart4f774512009-05-22 14:52:35 -040011027 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
James Smart695a8142010-01-26 23:08:03 -050011028 pIocbIn->iocb.un.genreq64.bdl.bdeSize = wcqe->total_data_placed;
11029 }
James Smart341af102010-01-26 23:07:37 -050011030
James Smartacd68592012-01-18 16:25:09 -050011031 /* Convert BG errors for completion status */
11032 if (status == CQE_STATUS_DI_ERROR) {
11033 pIocbIn->iocb.ulpStatus = IOSTAT_LOCAL_REJECT;
11034
11035 if (bf_get(lpfc_wcqe_c_bg_edir, wcqe))
11036 pIocbIn->iocb.un.ulpWord[4] = IOERR_RX_DMA_FAILED;
11037 else
11038 pIocbIn->iocb.un.ulpWord[4] = IOERR_TX_DMA_FAILED;
11039
11040 pIocbIn->iocb.unsli3.sli3_bg.bgstat = 0;
11041 if (bf_get(lpfc_wcqe_c_bg_ge, wcqe)) /* Guard Check failed */
11042 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
11043 BGS_GUARD_ERR_MASK;
11044 if (bf_get(lpfc_wcqe_c_bg_ae, wcqe)) /* App Tag Check failed */
11045 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
11046 BGS_APPTAG_ERR_MASK;
11047 if (bf_get(lpfc_wcqe_c_bg_re, wcqe)) /* Ref Tag Check failed */
11048 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
11049 BGS_REFTAG_ERR_MASK;
11050
11051 /* Check to see if there was any good data before the error */
11052 if (bf_get(lpfc_wcqe_c_bg_tdpv, wcqe)) {
11053 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
11054 BGS_HI_WATER_MARK_PRESENT_MASK;
11055 pIocbIn->iocb.unsli3.sli3_bg.bghm =
11056 wcqe->total_data_placed;
11057 }
11058
11059 /*
11060 * Set ALL the error bits to indicate we don't know what
11061 * type of error it is.
11062 */
11063 if (!pIocbIn->iocb.unsli3.sli3_bg.bgstat)
11064 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
11065 (BGS_REFTAG_ERR_MASK | BGS_APPTAG_ERR_MASK |
11066 BGS_GUARD_ERR_MASK);
11067 }
11068
James Smart341af102010-01-26 23:07:37 -050011069 /* Pick up HBA exchange busy condition */
11070 if (bf_get(lpfc_wcqe_c_xb, wcqe)) {
11071 spin_lock_irqsave(&phba->hbalock, iflags);
11072 pIocbIn->iocb_flag |= LPFC_EXCHANGE_BUSY;
11073 spin_unlock_irqrestore(&phba->hbalock, iflags);
11074 }
James Smart4f774512009-05-22 14:52:35 -040011075}
11076
11077/**
James Smart45ed1192009-10-02 15:17:02 -040011078 * lpfc_sli4_els_wcqe_to_rspiocbq - Get response iocbq from els wcqe
11079 * @phba: Pointer to HBA context object.
11080 * @wcqe: Pointer to work-queue completion queue entry.
11081 *
11082 * This routine handles an ELS work-queue completion event and construct
11083 * a pseudo response ELS IODBQ from the SLI4 ELS WCQE for the common
11084 * discovery engine to handle.
11085 *
11086 * Return: Pointer to the receive IOCBQ, NULL otherwise.
11087 **/
11088static struct lpfc_iocbq *
11089lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *phba,
11090 struct lpfc_iocbq *irspiocbq)
11091{
11092 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
11093 struct lpfc_iocbq *cmdiocbq;
11094 struct lpfc_wcqe_complete *wcqe;
11095 unsigned long iflags;
11096
11097 wcqe = &irspiocbq->cq_event.cqe.wcqe_cmpl;
James Smart7e56aa22012-08-03 12:35:34 -040011098 spin_lock_irqsave(&pring->ring_lock, iflags);
James Smart45ed1192009-10-02 15:17:02 -040011099 pring->stats.iocb_event++;
11100 /* Look up the ELS command IOCB and create pseudo response IOCB */
11101 cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
11102 bf_get(lpfc_wcqe_c_request_tag, wcqe));
James Smart7e56aa22012-08-03 12:35:34 -040011103 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart45ed1192009-10-02 15:17:02 -040011104
11105 if (unlikely(!cmdiocbq)) {
11106 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11107 "0386 ELS complete with no corresponding "
11108 "cmdiocb: iotag (%d)\n",
11109 bf_get(lpfc_wcqe_c_request_tag, wcqe));
11110 lpfc_sli_release_iocbq(phba, irspiocbq);
11111 return NULL;
11112 }
11113
11114 /* Fake the irspiocbq and copy necessary response information */
James Smart341af102010-01-26 23:07:37 -050011115 lpfc_sli4_iocb_param_transfer(phba, irspiocbq, cmdiocbq, wcqe);
James Smart45ed1192009-10-02 15:17:02 -040011116
11117 return irspiocbq;
11118}
11119
11120/**
James Smart04c68492009-05-22 14:52:52 -040011121 * lpfc_sli4_sp_handle_async_event - Handle an asynchroous event
11122 * @phba: Pointer to HBA context object.
11123 * @cqe: Pointer to mailbox completion queue entry.
11124 *
11125 * This routine process a mailbox completion queue entry with asynchrous
11126 * event.
11127 *
11128 * Return: true if work posted to worker thread, otherwise false.
11129 **/
11130static bool
11131lpfc_sli4_sp_handle_async_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
11132{
11133 struct lpfc_cq_event *cq_event;
11134 unsigned long iflags;
11135
11136 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
11137 "0392 Async Event: word0:x%x, word1:x%x, "
11138 "word2:x%x, word3:x%x\n", mcqe->word0,
11139 mcqe->mcqe_tag0, mcqe->mcqe_tag1, mcqe->trailer);
11140
11141 /* Allocate a new internal CQ_EVENT entry */
11142 cq_event = lpfc_sli4_cq_event_alloc(phba);
11143 if (!cq_event) {
11144 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11145 "0394 Failed to allocate CQ_EVENT entry\n");
11146 return false;
11147 }
11148
11149 /* Move the CQE into an asynchronous event entry */
11150 memcpy(&cq_event->cqe, mcqe, sizeof(struct lpfc_mcqe));
11151 spin_lock_irqsave(&phba->hbalock, iflags);
11152 list_add_tail(&cq_event->list, &phba->sli4_hba.sp_asynce_work_queue);
11153 /* Set the async event flag */
11154 phba->hba_flag |= ASYNC_EVENT;
11155 spin_unlock_irqrestore(&phba->hbalock, iflags);
11156
11157 return true;
11158}
11159
11160/**
11161 * lpfc_sli4_sp_handle_mbox_event - Handle a mailbox completion event
11162 * @phba: Pointer to HBA context object.
11163 * @cqe: Pointer to mailbox completion queue entry.
11164 *
11165 * This routine process a mailbox completion queue entry with mailbox
11166 * completion event.
11167 *
11168 * Return: true if work posted to worker thread, otherwise false.
11169 **/
11170static bool
11171lpfc_sli4_sp_handle_mbox_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
11172{
11173 uint32_t mcqe_status;
11174 MAILBOX_t *mbox, *pmbox;
11175 struct lpfc_mqe *mqe;
11176 struct lpfc_vport *vport;
11177 struct lpfc_nodelist *ndlp;
11178 struct lpfc_dmabuf *mp;
11179 unsigned long iflags;
11180 LPFC_MBOXQ_t *pmb;
11181 bool workposted = false;
11182 int rc;
11183
11184 /* If not a mailbox complete MCQE, out by checking mailbox consume */
11185 if (!bf_get(lpfc_trailer_completed, mcqe))
11186 goto out_no_mqe_complete;
11187
11188 /* Get the reference to the active mbox command */
11189 spin_lock_irqsave(&phba->hbalock, iflags);
11190 pmb = phba->sli.mbox_active;
11191 if (unlikely(!pmb)) {
11192 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
11193 "1832 No pending MBOX command to handle\n");
11194 spin_unlock_irqrestore(&phba->hbalock, iflags);
11195 goto out_no_mqe_complete;
11196 }
11197 spin_unlock_irqrestore(&phba->hbalock, iflags);
11198 mqe = &pmb->u.mqe;
11199 pmbox = (MAILBOX_t *)&pmb->u.mqe;
11200 mbox = phba->mbox;
11201 vport = pmb->vport;
11202
11203 /* Reset heartbeat timer */
11204 phba->last_completion_time = jiffies;
11205 del_timer(&phba->sli.mbox_tmo);
11206
11207 /* Move mbox data to caller's mailbox region, do endian swapping */
11208 if (pmb->mbox_cmpl && mbox)
11209 lpfc_sli_pcimem_bcopy(mbox, mqe, sizeof(struct lpfc_mqe));
James Smart04c68492009-05-22 14:52:52 -040011210
James Smart73d91e52011-10-10 21:32:10 -040011211 /*
11212 * For mcqe errors, conditionally move a modified error code to
11213 * the mbox so that the error will not be missed.
11214 */
11215 mcqe_status = bf_get(lpfc_mcqe_status, mcqe);
11216 if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
11217 if (bf_get(lpfc_mqe_status, mqe) == MBX_SUCCESS)
11218 bf_set(lpfc_mqe_status, mqe,
11219 (LPFC_MBX_ERROR_RANGE | mcqe_status));
11220 }
James Smart04c68492009-05-22 14:52:52 -040011221 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
11222 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
11223 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_MBOX_VPORT,
11224 "MBOX dflt rpi: status:x%x rpi:x%x",
11225 mcqe_status,
11226 pmbox->un.varWords[0], 0);
11227 if (mcqe_status == MB_CQE_STATUS_SUCCESS) {
11228 mp = (struct lpfc_dmabuf *)(pmb->context1);
11229 ndlp = (struct lpfc_nodelist *)pmb->context2;
11230 /* Reg_LOGIN of dflt RPI was successful. Now lets get
11231 * RID of the PPI using the same mbox buffer.
11232 */
11233 lpfc_unreg_login(phba, vport->vpi,
11234 pmbox->un.varWords[0], pmb);
11235 pmb->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
11236 pmb->context1 = mp;
11237 pmb->context2 = ndlp;
11238 pmb->vport = vport;
11239 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
11240 if (rc != MBX_BUSY)
11241 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
11242 LOG_SLI, "0385 rc should "
11243 "have been MBX_BUSY\n");
11244 if (rc != MBX_NOT_FINISHED)
11245 goto send_current_mbox;
11246 }
11247 }
11248 spin_lock_irqsave(&phba->pport->work_port_lock, iflags);
11249 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
11250 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflags);
11251
11252 /* There is mailbox completion work to do */
11253 spin_lock_irqsave(&phba->hbalock, iflags);
11254 __lpfc_mbox_cmpl_put(phba, pmb);
11255 phba->work_ha |= HA_MBATT;
11256 spin_unlock_irqrestore(&phba->hbalock, iflags);
11257 workposted = true;
11258
11259send_current_mbox:
11260 spin_lock_irqsave(&phba->hbalock, iflags);
11261 /* Release the mailbox command posting token */
11262 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
11263 /* Setting active mailbox pointer need to be in sync to flag clear */
11264 phba->sli.mbox_active = NULL;
11265 spin_unlock_irqrestore(&phba->hbalock, iflags);
11266 /* Wake up worker thread to post the next pending mailbox command */
11267 lpfc_worker_wake_up(phba);
11268out_no_mqe_complete:
11269 if (bf_get(lpfc_trailer_consumed, mcqe))
11270 lpfc_sli4_mq_release(phba->sli4_hba.mbx_wq);
11271 return workposted;
11272}
11273
11274/**
11275 * lpfc_sli4_sp_handle_mcqe - Process a mailbox completion queue entry
11276 * @phba: Pointer to HBA context object.
11277 * @cqe: Pointer to mailbox completion queue entry.
11278 *
11279 * This routine process a mailbox completion queue entry, it invokes the
11280 * proper mailbox complete handling or asynchrous event handling routine
11281 * according to the MCQE's async bit.
11282 *
11283 * Return: true if work posted to worker thread, otherwise false.
11284 **/
11285static bool
11286lpfc_sli4_sp_handle_mcqe(struct lpfc_hba *phba, struct lpfc_cqe *cqe)
11287{
11288 struct lpfc_mcqe mcqe;
11289 bool workposted;
11290
11291 /* Copy the mailbox MCQE and convert endian order as needed */
11292 lpfc_sli_pcimem_bcopy(cqe, &mcqe, sizeof(struct lpfc_mcqe));
11293
11294 /* Invoke the proper event handling routine */
11295 if (!bf_get(lpfc_trailer_async, &mcqe))
11296 workposted = lpfc_sli4_sp_handle_mbox_event(phba, &mcqe);
11297 else
11298 workposted = lpfc_sli4_sp_handle_async_event(phba, &mcqe);
11299 return workposted;
11300}
11301
11302/**
James Smart4f774512009-05-22 14:52:35 -040011303 * lpfc_sli4_sp_handle_els_wcqe - Handle els work-queue completion event
11304 * @phba: Pointer to HBA context object.
James Smart2a76a282012-08-03 12:35:54 -040011305 * @cq: Pointer to associated CQ
James Smart4f774512009-05-22 14:52:35 -040011306 * @wcqe: Pointer to work-queue completion queue entry.
11307 *
11308 * This routine handles an ELS work-queue completion event.
11309 *
11310 * Return: true if work posted to worker thread, otherwise false.
11311 **/
11312static bool
James Smart2a76a282012-08-03 12:35:54 -040011313lpfc_sli4_sp_handle_els_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
James Smart4f774512009-05-22 14:52:35 -040011314 struct lpfc_wcqe_complete *wcqe)
11315{
James Smart4f774512009-05-22 14:52:35 -040011316 struct lpfc_iocbq *irspiocbq;
11317 unsigned long iflags;
James Smart2a76a282012-08-03 12:35:54 -040011318 struct lpfc_sli_ring *pring = cq->pring;
James Smart0e9bb8d2013-03-01 16:35:12 -050011319 int txq_cnt = 0;
11320 int txcmplq_cnt = 0;
11321 int fcp_txcmplq_cnt = 0;
James Smart4f774512009-05-22 14:52:35 -040011322
James Smart45ed1192009-10-02 15:17:02 -040011323 /* Get an irspiocbq for later ELS response processing use */
James Smart4f774512009-05-22 14:52:35 -040011324 irspiocbq = lpfc_sli_get_iocbq(phba);
11325 if (!irspiocbq) {
James Smart0e9bb8d2013-03-01 16:35:12 -050011326 if (!list_empty(&pring->txq))
11327 txq_cnt++;
11328 if (!list_empty(&pring->txcmplq))
11329 txcmplq_cnt++;
11330 if (!list_empty(&phba->sli.ring[LPFC_FCP_RING].txcmplq))
11331 fcp_txcmplq_cnt++;
James Smart4f774512009-05-22 14:52:35 -040011332 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart2a9bf3d2010-06-07 15:24:45 -040011333 "0387 NO IOCBQ data: txq_cnt=%d iocb_cnt=%d "
11334 "fcp_txcmplq_cnt=%d, els_txcmplq_cnt=%d\n",
James Smart0e9bb8d2013-03-01 16:35:12 -050011335 txq_cnt, phba->iocb_cnt,
11336 fcp_txcmplq_cnt,
11337 txcmplq_cnt);
James Smart45ed1192009-10-02 15:17:02 -040011338 return false;
James Smart4f774512009-05-22 14:52:35 -040011339 }
James Smart4f774512009-05-22 14:52:35 -040011340
James Smart45ed1192009-10-02 15:17:02 -040011341 /* Save off the slow-path queue event for work thread to process */
11342 memcpy(&irspiocbq->cq_event.cqe.wcqe_cmpl, wcqe, sizeof(*wcqe));
James Smart4f774512009-05-22 14:52:35 -040011343 spin_lock_irqsave(&phba->hbalock, iflags);
James Smart4d9ab992009-10-02 15:16:39 -040011344 list_add_tail(&irspiocbq->cq_event.list,
James Smart45ed1192009-10-02 15:17:02 -040011345 &phba->sli4_hba.sp_queue_event);
11346 phba->hba_flag |= HBA_SP_QUEUE_EVT;
James Smart4f774512009-05-22 14:52:35 -040011347 spin_unlock_irqrestore(&phba->hbalock, iflags);
James Smart4f774512009-05-22 14:52:35 -040011348
James Smart45ed1192009-10-02 15:17:02 -040011349 return true;
James Smart4f774512009-05-22 14:52:35 -040011350}
11351
11352/**
11353 * lpfc_sli4_sp_handle_rel_wcqe - Handle slow-path WQ entry consumed event
11354 * @phba: Pointer to HBA context object.
11355 * @wcqe: Pointer to work-queue completion queue entry.
11356 *
11357 * This routine handles slow-path WQ entry comsumed event by invoking the
11358 * proper WQ release routine to the slow-path WQ.
11359 **/
11360static void
11361lpfc_sli4_sp_handle_rel_wcqe(struct lpfc_hba *phba,
11362 struct lpfc_wcqe_release *wcqe)
11363{
James Smart2e90f4b2011-12-13 13:22:37 -050011364 /* sanity check on queue memory */
11365 if (unlikely(!phba->sli4_hba.els_wq))
11366 return;
James Smart4f774512009-05-22 14:52:35 -040011367 /* Check for the slow-path ELS work queue */
11368 if (bf_get(lpfc_wcqe_r_wq_id, wcqe) == phba->sli4_hba.els_wq->queue_id)
11369 lpfc_sli4_wq_release(phba->sli4_hba.els_wq,
11370 bf_get(lpfc_wcqe_r_wqe_index, wcqe));
11371 else
11372 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11373 "2579 Slow-path wqe consume event carries "
11374 "miss-matched qid: wcqe-qid=x%x, sp-qid=x%x\n",
11375 bf_get(lpfc_wcqe_r_wqe_index, wcqe),
11376 phba->sli4_hba.els_wq->queue_id);
11377}
11378
11379/**
11380 * lpfc_sli4_sp_handle_abort_xri_wcqe - Handle a xri abort event
11381 * @phba: Pointer to HBA context object.
11382 * @cq: Pointer to a WQ completion queue.
11383 * @wcqe: Pointer to work-queue completion queue entry.
11384 *
11385 * This routine handles an XRI abort event.
11386 *
11387 * Return: true if work posted to worker thread, otherwise false.
11388 **/
11389static bool
11390lpfc_sli4_sp_handle_abort_xri_wcqe(struct lpfc_hba *phba,
11391 struct lpfc_queue *cq,
11392 struct sli4_wcqe_xri_aborted *wcqe)
11393{
11394 bool workposted = false;
11395 struct lpfc_cq_event *cq_event;
11396 unsigned long iflags;
11397
11398 /* Allocate a new internal CQ_EVENT entry */
11399 cq_event = lpfc_sli4_cq_event_alloc(phba);
11400 if (!cq_event) {
11401 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11402 "0602 Failed to allocate CQ_EVENT entry\n");
11403 return false;
11404 }
11405
11406 /* Move the CQE into the proper xri abort event list */
11407 memcpy(&cq_event->cqe, wcqe, sizeof(struct sli4_wcqe_xri_aborted));
11408 switch (cq->subtype) {
11409 case LPFC_FCP:
11410 spin_lock_irqsave(&phba->hbalock, iflags);
11411 list_add_tail(&cq_event->list,
11412 &phba->sli4_hba.sp_fcp_xri_aborted_work_queue);
11413 /* Set the fcp xri abort event flag */
11414 phba->hba_flag |= FCP_XRI_ABORT_EVENT;
11415 spin_unlock_irqrestore(&phba->hbalock, iflags);
11416 workposted = true;
11417 break;
11418 case LPFC_ELS:
11419 spin_lock_irqsave(&phba->hbalock, iflags);
11420 list_add_tail(&cq_event->list,
11421 &phba->sli4_hba.sp_els_xri_aborted_work_queue);
11422 /* Set the els xri abort event flag */
11423 phba->hba_flag |= ELS_XRI_ABORT_EVENT;
11424 spin_unlock_irqrestore(&phba->hbalock, iflags);
11425 workposted = true;
11426 break;
11427 default:
11428 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11429 "0603 Invalid work queue CQE subtype (x%x)\n",
11430 cq->subtype);
11431 workposted = false;
11432 break;
11433 }
11434 return workposted;
11435}
11436
11437/**
James Smart4d9ab992009-10-02 15:16:39 -040011438 * lpfc_sli4_sp_handle_rcqe - Process a receive-queue completion queue entry
James Smart4f774512009-05-22 14:52:35 -040011439 * @phba: Pointer to HBA context object.
James Smart4d9ab992009-10-02 15:16:39 -040011440 * @rcqe: Pointer to receive-queue completion queue entry.
James Smart4f774512009-05-22 14:52:35 -040011441 *
James Smart4d9ab992009-10-02 15:16:39 -040011442 * This routine process a receive-queue completion queue entry.
James Smart4f774512009-05-22 14:52:35 -040011443 *
11444 * Return: true if work posted to worker thread, otherwise false.
11445 **/
11446static bool
James Smart4d9ab992009-10-02 15:16:39 -040011447lpfc_sli4_sp_handle_rcqe(struct lpfc_hba *phba, struct lpfc_rcqe *rcqe)
11448{
11449 bool workposted = false;
11450 struct lpfc_queue *hrq = phba->sli4_hba.hdr_rq;
11451 struct lpfc_queue *drq = phba->sli4_hba.dat_rq;
11452 struct hbq_dmabuf *dma_buf;
James Smart7851fe22011-07-22 18:36:52 -040011453 uint32_t status, rq_id;
James Smart4d9ab992009-10-02 15:16:39 -040011454 unsigned long iflags;
11455
James Smart2e90f4b2011-12-13 13:22:37 -050011456 /* sanity check on queue memory */
11457 if (unlikely(!hrq) || unlikely(!drq))
11458 return workposted;
11459
James Smart7851fe22011-07-22 18:36:52 -040011460 if (bf_get(lpfc_cqe_code, rcqe) == CQE_CODE_RECEIVE_V1)
11461 rq_id = bf_get(lpfc_rcqe_rq_id_v1, rcqe);
11462 else
11463 rq_id = bf_get(lpfc_rcqe_rq_id, rcqe);
11464 if (rq_id != hrq->queue_id)
James Smart4d9ab992009-10-02 15:16:39 -040011465 goto out;
11466
11467 status = bf_get(lpfc_rcqe_status, rcqe);
11468 switch (status) {
11469 case FC_STATUS_RQ_BUF_LEN_EXCEEDED:
11470 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11471 "2537 Receive Frame Truncated!!\n");
James Smartb84daac2012-08-03 12:35:13 -040011472 hrq->RQ_buf_trunc++;
James Smart4d9ab992009-10-02 15:16:39 -040011473 case FC_STATUS_RQ_SUCCESS:
James Smart5ffc2662009-11-18 15:39:44 -050011474 lpfc_sli4_rq_release(hrq, drq);
James Smart4d9ab992009-10-02 15:16:39 -040011475 spin_lock_irqsave(&phba->hbalock, iflags);
11476 dma_buf = lpfc_sli_hbqbuf_get(&phba->hbqs[0].hbq_buffer_list);
11477 if (!dma_buf) {
James Smartb84daac2012-08-03 12:35:13 -040011478 hrq->RQ_no_buf_found++;
James Smart4d9ab992009-10-02 15:16:39 -040011479 spin_unlock_irqrestore(&phba->hbalock, iflags);
11480 goto out;
11481 }
James Smartb84daac2012-08-03 12:35:13 -040011482 hrq->RQ_rcv_buf++;
James Smart4d9ab992009-10-02 15:16:39 -040011483 memcpy(&dma_buf->cq_event.cqe.rcqe_cmpl, rcqe, sizeof(*rcqe));
11484 /* save off the frame for the word thread to process */
11485 list_add_tail(&dma_buf->cq_event.list,
James Smart45ed1192009-10-02 15:17:02 -040011486 &phba->sli4_hba.sp_queue_event);
James Smart4d9ab992009-10-02 15:16:39 -040011487 /* Frame received */
James Smart45ed1192009-10-02 15:17:02 -040011488 phba->hba_flag |= HBA_SP_QUEUE_EVT;
James Smart4d9ab992009-10-02 15:16:39 -040011489 spin_unlock_irqrestore(&phba->hbalock, iflags);
11490 workposted = true;
11491 break;
11492 case FC_STATUS_INSUFF_BUF_NEED_BUF:
11493 case FC_STATUS_INSUFF_BUF_FRM_DISC:
James Smartb84daac2012-08-03 12:35:13 -040011494 hrq->RQ_no_posted_buf++;
James Smart4d9ab992009-10-02 15:16:39 -040011495 /* Post more buffers if possible */
11496 spin_lock_irqsave(&phba->hbalock, iflags);
11497 phba->hba_flag |= HBA_POST_RECEIVE_BUFFER;
11498 spin_unlock_irqrestore(&phba->hbalock, iflags);
11499 workposted = true;
11500 break;
11501 }
11502out:
11503 return workposted;
James Smart4d9ab992009-10-02 15:16:39 -040011504}
11505
11506/**
11507 * lpfc_sli4_sp_handle_cqe - Process a slow path completion queue entry
11508 * @phba: Pointer to HBA context object.
11509 * @cq: Pointer to the completion queue.
11510 * @wcqe: Pointer to a completion queue entry.
11511 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030011512 * This routine process a slow-path work-queue or receive queue completion queue
James Smart4d9ab992009-10-02 15:16:39 -040011513 * entry.
11514 *
11515 * Return: true if work posted to worker thread, otherwise false.
11516 **/
11517static bool
11518lpfc_sli4_sp_handle_cqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
James Smart4f774512009-05-22 14:52:35 -040011519 struct lpfc_cqe *cqe)
11520{
James Smart45ed1192009-10-02 15:17:02 -040011521 struct lpfc_cqe cqevt;
James Smart4f774512009-05-22 14:52:35 -040011522 bool workposted = false;
11523
11524 /* Copy the work queue CQE and convert endian order if needed */
James Smart45ed1192009-10-02 15:17:02 -040011525 lpfc_sli_pcimem_bcopy(cqe, &cqevt, sizeof(struct lpfc_cqe));
James Smart4f774512009-05-22 14:52:35 -040011526
11527 /* Check and process for different type of WCQE and dispatch */
James Smart45ed1192009-10-02 15:17:02 -040011528 switch (bf_get(lpfc_cqe_code, &cqevt)) {
James Smart4f774512009-05-22 14:52:35 -040011529 case CQE_CODE_COMPL_WQE:
James Smart45ed1192009-10-02 15:17:02 -040011530 /* Process the WQ/RQ complete event */
James Smartbc739052010-08-04 16:11:18 -040011531 phba->last_completion_time = jiffies;
James Smart2a76a282012-08-03 12:35:54 -040011532 workposted = lpfc_sli4_sp_handle_els_wcqe(phba, cq,
James Smart45ed1192009-10-02 15:17:02 -040011533 (struct lpfc_wcqe_complete *)&cqevt);
James Smart4f774512009-05-22 14:52:35 -040011534 break;
11535 case CQE_CODE_RELEASE_WQE:
11536 /* Process the WQ release event */
11537 lpfc_sli4_sp_handle_rel_wcqe(phba,
James Smart45ed1192009-10-02 15:17:02 -040011538 (struct lpfc_wcqe_release *)&cqevt);
James Smart4f774512009-05-22 14:52:35 -040011539 break;
11540 case CQE_CODE_XRI_ABORTED:
11541 /* Process the WQ XRI abort event */
James Smartbc739052010-08-04 16:11:18 -040011542 phba->last_completion_time = jiffies;
James Smart4f774512009-05-22 14:52:35 -040011543 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
James Smart45ed1192009-10-02 15:17:02 -040011544 (struct sli4_wcqe_xri_aborted *)&cqevt);
James Smart4f774512009-05-22 14:52:35 -040011545 break;
James Smart4d9ab992009-10-02 15:16:39 -040011546 case CQE_CODE_RECEIVE:
James Smart7851fe22011-07-22 18:36:52 -040011547 case CQE_CODE_RECEIVE_V1:
James Smart4d9ab992009-10-02 15:16:39 -040011548 /* Process the RQ event */
James Smartbc739052010-08-04 16:11:18 -040011549 phba->last_completion_time = jiffies;
James Smart4d9ab992009-10-02 15:16:39 -040011550 workposted = lpfc_sli4_sp_handle_rcqe(phba,
James Smart45ed1192009-10-02 15:17:02 -040011551 (struct lpfc_rcqe *)&cqevt);
James Smart4d9ab992009-10-02 15:16:39 -040011552 break;
James Smart4f774512009-05-22 14:52:35 -040011553 default:
11554 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11555 "0388 Not a valid WCQE code: x%x\n",
James Smart45ed1192009-10-02 15:17:02 -040011556 bf_get(lpfc_cqe_code, &cqevt));
James Smart4f774512009-05-22 14:52:35 -040011557 break;
11558 }
11559 return workposted;
11560}
11561
11562/**
James Smart4f774512009-05-22 14:52:35 -040011563 * lpfc_sli4_sp_handle_eqe - Process a slow-path event queue entry
11564 * @phba: Pointer to HBA context object.
11565 * @eqe: Pointer to fast-path event queue entry.
11566 *
11567 * This routine process a event queue entry from the slow-path event queue.
11568 * It will check the MajorCode and MinorCode to determine this is for a
11569 * completion event on a completion queue, if not, an error shall be logged
11570 * and just return. Otherwise, it will get to the corresponding completion
11571 * queue and process all the entries on that completion queue, rearm the
11572 * completion queue, and then return.
11573 *
11574 **/
11575static void
James Smart67d12732012-08-03 12:36:13 -040011576lpfc_sli4_sp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
11577 struct lpfc_queue *speq)
James Smart4f774512009-05-22 14:52:35 -040011578{
James Smart67d12732012-08-03 12:36:13 -040011579 struct lpfc_queue *cq = NULL, *childq;
James Smart4f774512009-05-22 14:52:35 -040011580 struct lpfc_cqe *cqe;
11581 bool workposted = false;
11582 int ecount = 0;
11583 uint16_t cqid;
11584
James Smart4f774512009-05-22 14:52:35 -040011585 /* Get the reference to the corresponding CQ */
James Smartcb5172e2010-03-15 11:25:07 -040011586 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
James Smart4f774512009-05-22 14:52:35 -040011587
James Smart4f774512009-05-22 14:52:35 -040011588 list_for_each_entry(childq, &speq->child_list, list) {
11589 if (childq->queue_id == cqid) {
11590 cq = childq;
11591 break;
11592 }
11593 }
11594 if (unlikely(!cq)) {
James Smart75baf692010-06-08 18:31:21 -040011595 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
11596 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11597 "0365 Slow-path CQ identifier "
11598 "(%d) does not exist\n", cqid);
James Smart4f774512009-05-22 14:52:35 -040011599 return;
11600 }
11601
11602 /* Process all the entries to the CQ */
11603 switch (cq->type) {
11604 case LPFC_MCQ:
11605 while ((cqe = lpfc_sli4_cq_get(cq))) {
11606 workposted |= lpfc_sli4_sp_handle_mcqe(phba, cqe);
James Smart73d91e52011-10-10 21:32:10 -040011607 if (!(++ecount % cq->entry_repost))
James Smart4f774512009-05-22 14:52:35 -040011608 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
James Smartb84daac2012-08-03 12:35:13 -040011609 cq->CQ_mbox++;
James Smart4f774512009-05-22 14:52:35 -040011610 }
11611 break;
11612 case LPFC_WCQ:
11613 while ((cqe = lpfc_sli4_cq_get(cq))) {
James Smart05580562011-05-24 11:40:48 -040011614 if (cq->subtype == LPFC_FCP)
11615 workposted |= lpfc_sli4_fp_handle_wcqe(phba, cq,
11616 cqe);
11617 else
11618 workposted |= lpfc_sli4_sp_handle_cqe(phba, cq,
11619 cqe);
James Smart73d91e52011-10-10 21:32:10 -040011620 if (!(++ecount % cq->entry_repost))
James Smart4f774512009-05-22 14:52:35 -040011621 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
11622 }
James Smartb84daac2012-08-03 12:35:13 -040011623
11624 /* Track the max number of CQEs processed in 1 EQ */
11625 if (ecount > cq->CQ_max_cqe)
11626 cq->CQ_max_cqe = ecount;
James Smart4f774512009-05-22 14:52:35 -040011627 break;
11628 default:
11629 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11630 "0370 Invalid completion queue type (%d)\n",
11631 cq->type);
11632 return;
11633 }
11634
11635 /* Catch the no cq entry condition, log an error */
11636 if (unlikely(ecount == 0))
11637 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11638 "0371 No entry from the CQ: identifier "
11639 "(x%x), type (%d)\n", cq->queue_id, cq->type);
11640
11641 /* In any case, flash and re-arm the RCQ */
11642 lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
11643
11644 /* wake up worker thread if there are works to be done */
11645 if (workposted)
11646 lpfc_worker_wake_up(phba);
11647}
11648
11649/**
11650 * lpfc_sli4_fp_handle_fcp_wcqe - Process fast-path work queue completion entry
James Smart2a76a282012-08-03 12:35:54 -040011651 * @phba: Pointer to HBA context object.
11652 * @cq: Pointer to associated CQ
11653 * @wcqe: Pointer to work-queue completion queue entry.
James Smart4f774512009-05-22 14:52:35 -040011654 *
11655 * This routine process a fast-path work queue completion entry from fast-path
11656 * event queue for FCP command response completion.
11657 **/
11658static void
James Smart2a76a282012-08-03 12:35:54 -040011659lpfc_sli4_fp_handle_fcp_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
James Smart4f774512009-05-22 14:52:35 -040011660 struct lpfc_wcqe_complete *wcqe)
11661{
James Smart2a76a282012-08-03 12:35:54 -040011662 struct lpfc_sli_ring *pring = cq->pring;
James Smart4f774512009-05-22 14:52:35 -040011663 struct lpfc_iocbq *cmdiocbq;
11664 struct lpfc_iocbq irspiocbq;
11665 unsigned long iflags;
11666
James Smart4f774512009-05-22 14:52:35 -040011667 /* Check for response status */
11668 if (unlikely(bf_get(lpfc_wcqe_c_status, wcqe))) {
11669 /* If resource errors reported from HBA, reduce queue
11670 * depth of the SCSI device.
11671 */
James Smarte3d2b802012-08-14 14:25:43 -040011672 if (((bf_get(lpfc_wcqe_c_status, wcqe) ==
11673 IOSTAT_LOCAL_REJECT)) &&
11674 ((wcqe->parameter & IOERR_PARAM_MASK) ==
11675 IOERR_NO_RESOURCES))
James Smart4f774512009-05-22 14:52:35 -040011676 phba->lpfc_rampdown_queue_depth(phba);
James Smarte3d2b802012-08-14 14:25:43 -040011677
James Smart4f774512009-05-22 14:52:35 -040011678 /* Log the error status */
11679 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11680 "0373 FCP complete error: status=x%x, "
11681 "hw_status=x%x, total_data_specified=%d, "
11682 "parameter=x%x, word3=x%x\n",
11683 bf_get(lpfc_wcqe_c_status, wcqe),
11684 bf_get(lpfc_wcqe_c_hw_status, wcqe),
11685 wcqe->total_data_placed, wcqe->parameter,
11686 wcqe->word3);
11687 }
11688
11689 /* Look up the FCP command IOCB and create pseudo response IOCB */
James Smart7e56aa22012-08-03 12:35:34 -040011690 spin_lock_irqsave(&pring->ring_lock, iflags);
11691 pring->stats.iocb_event++;
James Smart4f774512009-05-22 14:52:35 -040011692 cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
11693 bf_get(lpfc_wcqe_c_request_tag, wcqe));
James Smart7e56aa22012-08-03 12:35:34 -040011694 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart4f774512009-05-22 14:52:35 -040011695 if (unlikely(!cmdiocbq)) {
11696 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11697 "0374 FCP complete with no corresponding "
11698 "cmdiocb: iotag (%d)\n",
11699 bf_get(lpfc_wcqe_c_request_tag, wcqe));
11700 return;
11701 }
11702 if (unlikely(!cmdiocbq->iocb_cmpl)) {
11703 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11704 "0375 FCP cmdiocb not callback function "
11705 "iotag: (%d)\n",
11706 bf_get(lpfc_wcqe_c_request_tag, wcqe));
11707 return;
11708 }
11709
11710 /* Fake the irspiocb and copy necessary response information */
James Smart341af102010-01-26 23:07:37 -050011711 lpfc_sli4_iocb_param_transfer(phba, &irspiocbq, cmdiocbq, wcqe);
James Smart4f774512009-05-22 14:52:35 -040011712
James Smart0f65ff62010-02-26 14:14:23 -050011713 if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED) {
11714 spin_lock_irqsave(&phba->hbalock, iflags);
11715 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
11716 spin_unlock_irqrestore(&phba->hbalock, iflags);
11717 }
11718
James Smart4f774512009-05-22 14:52:35 -040011719 /* Pass the cmd_iocb and the rsp state to the upper layer */
11720 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq, &irspiocbq);
11721}
11722
11723/**
11724 * lpfc_sli4_fp_handle_rel_wcqe - Handle fast-path WQ entry consumed event
11725 * @phba: Pointer to HBA context object.
11726 * @cq: Pointer to completion queue.
11727 * @wcqe: Pointer to work-queue completion queue entry.
11728 *
11729 * This routine handles an fast-path WQ entry comsumed event by invoking the
11730 * proper WQ release routine to the slow-path WQ.
11731 **/
11732static void
11733lpfc_sli4_fp_handle_rel_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
11734 struct lpfc_wcqe_release *wcqe)
11735{
11736 struct lpfc_queue *childwq;
11737 bool wqid_matched = false;
11738 uint16_t fcp_wqid;
11739
11740 /* Check for fast-path FCP work queue release */
11741 fcp_wqid = bf_get(lpfc_wcqe_r_wq_id, wcqe);
11742 list_for_each_entry(childwq, &cq->child_list, list) {
11743 if (childwq->queue_id == fcp_wqid) {
11744 lpfc_sli4_wq_release(childwq,
11745 bf_get(lpfc_wcqe_r_wqe_index, wcqe));
11746 wqid_matched = true;
11747 break;
11748 }
11749 }
11750 /* Report warning log message if no match found */
11751 if (wqid_matched != true)
11752 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11753 "2580 Fast-path wqe consume event carries "
11754 "miss-matched qid: wcqe-qid=x%x\n", fcp_wqid);
11755}
11756
11757/**
11758 * lpfc_sli4_fp_handle_wcqe - Process fast-path work queue completion entry
11759 * @cq: Pointer to the completion queue.
11760 * @eqe: Pointer to fast-path completion queue entry.
11761 *
11762 * This routine process a fast-path work queue completion entry from fast-path
11763 * event queue for FCP command response completion.
11764 **/
11765static int
11766lpfc_sli4_fp_handle_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
11767 struct lpfc_cqe *cqe)
11768{
11769 struct lpfc_wcqe_release wcqe;
11770 bool workposted = false;
11771
11772 /* Copy the work queue CQE and convert endian order if needed */
11773 lpfc_sli_pcimem_bcopy(cqe, &wcqe, sizeof(struct lpfc_cqe));
11774
11775 /* Check and process for different type of WCQE and dispatch */
11776 switch (bf_get(lpfc_wcqe_c_code, &wcqe)) {
11777 case CQE_CODE_COMPL_WQE:
James Smartb84daac2012-08-03 12:35:13 -040011778 cq->CQ_wq++;
James Smart4f774512009-05-22 14:52:35 -040011779 /* Process the WQ complete event */
James Smart98fc5dd2010-06-07 15:24:29 -040011780 phba->last_completion_time = jiffies;
James Smart2a76a282012-08-03 12:35:54 -040011781 lpfc_sli4_fp_handle_fcp_wcqe(phba, cq,
James Smart4f774512009-05-22 14:52:35 -040011782 (struct lpfc_wcqe_complete *)&wcqe);
11783 break;
11784 case CQE_CODE_RELEASE_WQE:
James Smartb84daac2012-08-03 12:35:13 -040011785 cq->CQ_release_wqe++;
James Smart4f774512009-05-22 14:52:35 -040011786 /* Process the WQ release event */
11787 lpfc_sli4_fp_handle_rel_wcqe(phba, cq,
11788 (struct lpfc_wcqe_release *)&wcqe);
11789 break;
11790 case CQE_CODE_XRI_ABORTED:
James Smartb84daac2012-08-03 12:35:13 -040011791 cq->CQ_xri_aborted++;
James Smart4f774512009-05-22 14:52:35 -040011792 /* Process the WQ XRI abort event */
James Smartbc739052010-08-04 16:11:18 -040011793 phba->last_completion_time = jiffies;
James Smart4f774512009-05-22 14:52:35 -040011794 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
11795 (struct sli4_wcqe_xri_aborted *)&wcqe);
11796 break;
11797 default:
11798 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11799 "0144 Not a valid WCQE code: x%x\n",
11800 bf_get(lpfc_wcqe_c_code, &wcqe));
11801 break;
11802 }
11803 return workposted;
11804}
11805
11806/**
James Smart67d12732012-08-03 12:36:13 -040011807 * lpfc_sli4_hba_handle_eqe - Process a fast-path event queue entry
James Smart4f774512009-05-22 14:52:35 -040011808 * @phba: Pointer to HBA context object.
11809 * @eqe: Pointer to fast-path event queue entry.
11810 *
11811 * This routine process a event queue entry from the fast-path event queue.
11812 * It will check the MajorCode and MinorCode to determine this is for a
11813 * completion event on a completion queue, if not, an error shall be logged
11814 * and just return. Otherwise, it will get to the corresponding completion
11815 * queue and process all the entries on the completion queue, rearm the
11816 * completion queue, and then return.
11817 **/
11818static void
James Smart67d12732012-08-03 12:36:13 -040011819lpfc_sli4_hba_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
11820 uint32_t qidx)
James Smart4f774512009-05-22 14:52:35 -040011821{
11822 struct lpfc_queue *cq;
11823 struct lpfc_cqe *cqe;
11824 bool workposted = false;
11825 uint16_t cqid;
11826 int ecount = 0;
11827
James Smartcb5172e2010-03-15 11:25:07 -040011828 if (unlikely(bf_get_le32(lpfc_eqe_major_code, eqe) != 0)) {
James Smart4f774512009-05-22 14:52:35 -040011829 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart67d12732012-08-03 12:36:13 -040011830 "0366 Not a valid completion "
James Smart4f774512009-05-22 14:52:35 -040011831 "event: majorcode=x%x, minorcode=x%x\n",
James Smartcb5172e2010-03-15 11:25:07 -040011832 bf_get_le32(lpfc_eqe_major_code, eqe),
11833 bf_get_le32(lpfc_eqe_minor_code, eqe));
James Smart4f774512009-05-22 14:52:35 -040011834 return;
11835 }
11836
James Smart67d12732012-08-03 12:36:13 -040011837 /* Get the reference to the corresponding CQ */
11838 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
11839
11840 /* Check if this is a Slow path event */
11841 if (unlikely(cqid != phba->sli4_hba.fcp_cq_map[qidx])) {
11842 lpfc_sli4_sp_handle_eqe(phba, eqe,
11843 phba->sli4_hba.hba_eq[qidx]);
11844 return;
11845 }
11846
James Smart2e90f4b2011-12-13 13:22:37 -050011847 if (unlikely(!phba->sli4_hba.fcp_cq)) {
11848 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11849 "3146 Fast-path completion queues "
11850 "does not exist\n");
11851 return;
11852 }
James Smart67d12732012-08-03 12:36:13 -040011853 cq = phba->sli4_hba.fcp_cq[qidx];
James Smart4f774512009-05-22 14:52:35 -040011854 if (unlikely(!cq)) {
James Smart75baf692010-06-08 18:31:21 -040011855 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
11856 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11857 "0367 Fast-path completion queue "
James Smart67d12732012-08-03 12:36:13 -040011858 "(%d) does not exist\n", qidx);
James Smart4f774512009-05-22 14:52:35 -040011859 return;
11860 }
11861
James Smart4f774512009-05-22 14:52:35 -040011862 if (unlikely(cqid != cq->queue_id)) {
11863 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11864 "0368 Miss-matched fast-path completion "
11865 "queue identifier: eqcqid=%d, fcpcqid=%d\n",
11866 cqid, cq->queue_id);
11867 return;
11868 }
11869
11870 /* Process all the entries to the CQ */
11871 while ((cqe = lpfc_sli4_cq_get(cq))) {
11872 workposted |= lpfc_sli4_fp_handle_wcqe(phba, cq, cqe);
James Smart73d91e52011-10-10 21:32:10 -040011873 if (!(++ecount % cq->entry_repost))
James Smart4f774512009-05-22 14:52:35 -040011874 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
11875 }
11876
James Smartb84daac2012-08-03 12:35:13 -040011877 /* Track the max number of CQEs processed in 1 EQ */
11878 if (ecount > cq->CQ_max_cqe)
11879 cq->CQ_max_cqe = ecount;
11880
James Smart4f774512009-05-22 14:52:35 -040011881 /* Catch the no cq entry condition */
11882 if (unlikely(ecount == 0))
11883 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11884 "0369 No entry from fast-path completion "
11885 "queue fcpcqid=%d\n", cq->queue_id);
11886
11887 /* In any case, flash and re-arm the CQ */
11888 lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
11889
11890 /* wake up worker thread if there are works to be done */
11891 if (workposted)
11892 lpfc_worker_wake_up(phba);
11893}
11894
11895static void
11896lpfc_sli4_eq_flush(struct lpfc_hba *phba, struct lpfc_queue *eq)
11897{
11898 struct lpfc_eqe *eqe;
11899
11900 /* walk all the EQ entries and drop on the floor */
11901 while ((eqe = lpfc_sli4_eq_get(eq)))
11902 ;
11903
11904 /* Clear and re-arm the EQ */
11905 lpfc_sli4_eq_release(eq, LPFC_QUEUE_REARM);
11906}
11907
11908/**
James Smart67d12732012-08-03 12:36:13 -040011909 * lpfc_sli4_hba_intr_handler - HBA interrupt handler to SLI-4 device
James Smart4f774512009-05-22 14:52:35 -040011910 * @irq: Interrupt number.
11911 * @dev_id: The device context pointer.
11912 *
11913 * This function is directly called from the PCI layer as an interrupt
11914 * service routine when device with SLI-4 interface spec is enabled with
11915 * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
11916 * ring event in the HBA. However, when the device is enabled with either
11917 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
11918 * device-level interrupt handler. When the PCI slot is in error recovery
11919 * or the HBA is undergoing initialization, the interrupt handler will not
11920 * process the interrupt. The SCSI FCP fast-path ring event are handled in
11921 * the intrrupt context. This function is called without any lock held.
11922 * It gets the hbalock to access and update SLI data structures. Note that,
11923 * the FCP EQ to FCP CQ are one-to-one map such that the FCP EQ index is
11924 * equal to that of FCP CQ index.
11925 *
James Smart67d12732012-08-03 12:36:13 -040011926 * The link attention and ELS ring attention events are handled
11927 * by the worker thread. The interrupt handler signals the worker thread
11928 * and returns for these events. This function is called without any lock
11929 * held. It gets the hbalock to access and update SLI data structures.
11930 *
James Smart4f774512009-05-22 14:52:35 -040011931 * This function returns IRQ_HANDLED when interrupt is handled else it
11932 * returns IRQ_NONE.
11933 **/
11934irqreturn_t
James Smart67d12732012-08-03 12:36:13 -040011935lpfc_sli4_hba_intr_handler(int irq, void *dev_id)
James Smart4f774512009-05-22 14:52:35 -040011936{
11937 struct lpfc_hba *phba;
11938 struct lpfc_fcp_eq_hdl *fcp_eq_hdl;
11939 struct lpfc_queue *fpeq;
11940 struct lpfc_eqe *eqe;
11941 unsigned long iflag;
11942 int ecount = 0;
James Smart962bc512013-01-03 15:44:00 -050011943 int fcp_eqidx;
James Smart4f774512009-05-22 14:52:35 -040011944
11945 /* Get the driver's phba structure from the dev_id */
11946 fcp_eq_hdl = (struct lpfc_fcp_eq_hdl *)dev_id;
11947 phba = fcp_eq_hdl->phba;
11948 fcp_eqidx = fcp_eq_hdl->idx;
11949
11950 if (unlikely(!phba))
11951 return IRQ_NONE;
James Smart67d12732012-08-03 12:36:13 -040011952 if (unlikely(!phba->sli4_hba.hba_eq))
James Smart5350d872011-10-10 21:33:49 -040011953 return IRQ_NONE;
James Smart4f774512009-05-22 14:52:35 -040011954
11955 /* Get to the EQ struct associated with this vector */
James Smart67d12732012-08-03 12:36:13 -040011956 fpeq = phba->sli4_hba.hba_eq[fcp_eqidx];
James Smart2e90f4b2011-12-13 13:22:37 -050011957 if (unlikely(!fpeq))
11958 return IRQ_NONE;
James Smart4f774512009-05-22 14:52:35 -040011959
James Smartba20c852012-08-03 12:36:52 -040011960 if (lpfc_fcp_look_ahead) {
11961 if (atomic_dec_and_test(&fcp_eq_hdl->fcp_eq_in_use))
11962 lpfc_sli4_eq_clr_intr(fpeq);
11963 else {
11964 atomic_inc(&fcp_eq_hdl->fcp_eq_in_use);
11965 return IRQ_NONE;
11966 }
11967 }
11968
James Smart4f774512009-05-22 14:52:35 -040011969 /* Check device state for handling interrupt */
11970 if (unlikely(lpfc_intr_state_check(phba))) {
James Smartb84daac2012-08-03 12:35:13 -040011971 fpeq->EQ_badstate++;
James Smart4f774512009-05-22 14:52:35 -040011972 /* Check again for link_state with lock held */
11973 spin_lock_irqsave(&phba->hbalock, iflag);
11974 if (phba->link_state < LPFC_LINK_DOWN)
11975 /* Flush, clear interrupt, and rearm the EQ */
11976 lpfc_sli4_eq_flush(phba, fpeq);
11977 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smartba20c852012-08-03 12:36:52 -040011978 if (lpfc_fcp_look_ahead)
11979 atomic_inc(&fcp_eq_hdl->fcp_eq_in_use);
James Smart4f774512009-05-22 14:52:35 -040011980 return IRQ_NONE;
11981 }
11982
11983 /*
11984 * Process all the event on FCP fast-path EQ
11985 */
11986 while ((eqe = lpfc_sli4_eq_get(fpeq))) {
James Smart67d12732012-08-03 12:36:13 -040011987 lpfc_sli4_hba_handle_eqe(phba, eqe, fcp_eqidx);
James Smart73d91e52011-10-10 21:32:10 -040011988 if (!(++ecount % fpeq->entry_repost))
James Smart4f774512009-05-22 14:52:35 -040011989 lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_NOARM);
James Smartb84daac2012-08-03 12:35:13 -040011990 fpeq->EQ_processed++;
James Smart4f774512009-05-22 14:52:35 -040011991 }
11992
James Smartb84daac2012-08-03 12:35:13 -040011993 /* Track the max number of EQEs processed in 1 intr */
11994 if (ecount > fpeq->EQ_max_eqe)
11995 fpeq->EQ_max_eqe = ecount;
11996
James Smart4f774512009-05-22 14:52:35 -040011997 /* Always clear and re-arm the fast-path EQ */
11998 lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_REARM);
11999
12000 if (unlikely(ecount == 0)) {
James Smartb84daac2012-08-03 12:35:13 -040012001 fpeq->EQ_no_entry++;
James Smartba20c852012-08-03 12:36:52 -040012002
12003 if (lpfc_fcp_look_ahead) {
12004 atomic_inc(&fcp_eq_hdl->fcp_eq_in_use);
12005 return IRQ_NONE;
12006 }
12007
James Smart4f774512009-05-22 14:52:35 -040012008 if (phba->intr_type == MSIX)
12009 /* MSI-X treated interrupt served as no EQ share INT */
12010 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
12011 "0358 MSI-X interrupt with no EQE\n");
12012 else
12013 /* Non MSI-X treated on interrupt as EQ share INT */
12014 return IRQ_NONE;
12015 }
12016
James Smartba20c852012-08-03 12:36:52 -040012017 if (lpfc_fcp_look_ahead)
12018 atomic_inc(&fcp_eq_hdl->fcp_eq_in_use);
James Smart4f774512009-05-22 14:52:35 -040012019 return IRQ_HANDLED;
12020} /* lpfc_sli4_fp_intr_handler */
12021
12022/**
12023 * lpfc_sli4_intr_handler - Device-level interrupt handler for SLI-4 device
12024 * @irq: Interrupt number.
12025 * @dev_id: The device context pointer.
12026 *
12027 * This function is the device-level interrupt handler to device with SLI-4
12028 * interface spec, called from the PCI layer when either MSI or Pin-IRQ
12029 * interrupt mode is enabled and there is an event in the HBA which requires
12030 * driver attention. This function invokes the slow-path interrupt attention
12031 * handling function and fast-path interrupt attention handling function in
12032 * turn to process the relevant HBA attention events. This function is called
12033 * without any lock held. It gets the hbalock to access and update SLI data
12034 * structures.
12035 *
12036 * This function returns IRQ_HANDLED when interrupt is handled, else it
12037 * returns IRQ_NONE.
12038 **/
12039irqreturn_t
12040lpfc_sli4_intr_handler(int irq, void *dev_id)
12041{
12042 struct lpfc_hba *phba;
James Smart67d12732012-08-03 12:36:13 -040012043 irqreturn_t hba_irq_rc;
12044 bool hba_handled = false;
James Smart962bc512013-01-03 15:44:00 -050012045 int fcp_eqidx;
James Smart4f774512009-05-22 14:52:35 -040012046
12047 /* Get the driver's phba structure from the dev_id */
12048 phba = (struct lpfc_hba *)dev_id;
12049
12050 if (unlikely(!phba))
12051 return IRQ_NONE;
12052
12053 /*
James Smart4f774512009-05-22 14:52:35 -040012054 * Invoke fast-path host attention interrupt handling as appropriate.
12055 */
James Smart67d12732012-08-03 12:36:13 -040012056 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_io_channel; fcp_eqidx++) {
12057 hba_irq_rc = lpfc_sli4_hba_intr_handler(irq,
James Smart4f774512009-05-22 14:52:35 -040012058 &phba->sli4_hba.fcp_eq_hdl[fcp_eqidx]);
James Smart67d12732012-08-03 12:36:13 -040012059 if (hba_irq_rc == IRQ_HANDLED)
12060 hba_handled |= true;
James Smart4f774512009-05-22 14:52:35 -040012061 }
12062
James Smart67d12732012-08-03 12:36:13 -040012063 return (hba_handled == true) ? IRQ_HANDLED : IRQ_NONE;
James Smart4f774512009-05-22 14:52:35 -040012064} /* lpfc_sli4_intr_handler */
12065
12066/**
12067 * lpfc_sli4_queue_free - free a queue structure and associated memory
12068 * @queue: The queue structure to free.
12069 *
Uwe Kleine-Königb5950762010-11-01 15:38:34 -040012070 * This function frees a queue structure and the DMAable memory used for
James Smart4f774512009-05-22 14:52:35 -040012071 * the host resident queue. This function must be called after destroying the
12072 * queue on the HBA.
12073 **/
12074void
12075lpfc_sli4_queue_free(struct lpfc_queue *queue)
12076{
12077 struct lpfc_dmabuf *dmabuf;
12078
12079 if (!queue)
12080 return;
12081
12082 while (!list_empty(&queue->page_list)) {
12083 list_remove_head(&queue->page_list, dmabuf, struct lpfc_dmabuf,
12084 list);
James Smart49198b32010-04-06 15:04:33 -040012085 dma_free_coherent(&queue->phba->pcidev->dev, SLI4_PAGE_SIZE,
James Smart4f774512009-05-22 14:52:35 -040012086 dmabuf->virt, dmabuf->phys);
12087 kfree(dmabuf);
12088 }
12089 kfree(queue);
12090 return;
12091}
12092
12093/**
12094 * lpfc_sli4_queue_alloc - Allocate and initialize a queue structure
12095 * @phba: The HBA that this queue is being created on.
12096 * @entry_size: The size of each queue entry for this queue.
12097 * @entry count: The number of entries that this queue will handle.
12098 *
12099 * This function allocates a queue structure and the DMAable memory used for
12100 * the host resident queue. This function must be called before creating the
12101 * queue on the HBA.
12102 **/
12103struct lpfc_queue *
12104lpfc_sli4_queue_alloc(struct lpfc_hba *phba, uint32_t entry_size,
12105 uint32_t entry_count)
12106{
12107 struct lpfc_queue *queue;
12108 struct lpfc_dmabuf *dmabuf;
12109 int x, total_qe_count;
12110 void *dma_pointer;
James Smartcb5172e2010-03-15 11:25:07 -040012111 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smart4f774512009-05-22 14:52:35 -040012112
James Smartcb5172e2010-03-15 11:25:07 -040012113 if (!phba->sli4_hba.pc_sli4_params.supported)
12114 hw_page_size = SLI4_PAGE_SIZE;
12115
James Smart4f774512009-05-22 14:52:35 -040012116 queue = kzalloc(sizeof(struct lpfc_queue) +
12117 (sizeof(union sli4_qe) * entry_count), GFP_KERNEL);
12118 if (!queue)
12119 return NULL;
James Smartcb5172e2010-03-15 11:25:07 -040012120 queue->page_count = (ALIGN(entry_size * entry_count,
12121 hw_page_size))/hw_page_size;
James Smart4f774512009-05-22 14:52:35 -040012122 INIT_LIST_HEAD(&queue->list);
12123 INIT_LIST_HEAD(&queue->page_list);
12124 INIT_LIST_HEAD(&queue->child_list);
12125 for (x = 0, total_qe_count = 0; x < queue->page_count; x++) {
12126 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
12127 if (!dmabuf)
12128 goto out_fail;
12129 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
James Smartcb5172e2010-03-15 11:25:07 -040012130 hw_page_size, &dmabuf->phys,
James Smart4f774512009-05-22 14:52:35 -040012131 GFP_KERNEL);
12132 if (!dmabuf->virt) {
12133 kfree(dmabuf);
12134 goto out_fail;
12135 }
James Smartcb5172e2010-03-15 11:25:07 -040012136 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -040012137 dmabuf->buffer_tag = x;
12138 list_add_tail(&dmabuf->list, &queue->page_list);
12139 /* initialize queue's entry array */
12140 dma_pointer = dmabuf->virt;
12141 for (; total_qe_count < entry_count &&
James Smartcb5172e2010-03-15 11:25:07 -040012142 dma_pointer < (hw_page_size + dmabuf->virt);
James Smart4f774512009-05-22 14:52:35 -040012143 total_qe_count++, dma_pointer += entry_size) {
12144 queue->qe[total_qe_count].address = dma_pointer;
12145 }
12146 }
12147 queue->entry_size = entry_size;
12148 queue->entry_count = entry_count;
James Smart73d91e52011-10-10 21:32:10 -040012149
12150 /*
12151 * entry_repost is calculated based on the number of entries in the
12152 * queue. This works out except for RQs. If buffers are NOT initially
12153 * posted for every RQE, entry_repost should be adjusted accordingly.
12154 */
12155 queue->entry_repost = (entry_count >> 3);
12156 if (queue->entry_repost < LPFC_QUEUE_MIN_REPOST)
12157 queue->entry_repost = LPFC_QUEUE_MIN_REPOST;
James Smart4f774512009-05-22 14:52:35 -040012158 queue->phba = phba;
12159
12160 return queue;
12161out_fail:
12162 lpfc_sli4_queue_free(queue);
12163 return NULL;
12164}
12165
12166/**
James Smart962bc512013-01-03 15:44:00 -050012167 * lpfc_dual_chute_pci_bar_map - Map pci base address register to host memory
12168 * @phba: HBA structure that indicates port to create a queue on.
12169 * @pci_barset: PCI BAR set flag.
12170 *
12171 * This function shall perform iomap of the specified PCI BAR address to host
12172 * memory address if not already done so and return it. The returned host
12173 * memory address can be NULL.
12174 */
12175static void __iomem *
12176lpfc_dual_chute_pci_bar_map(struct lpfc_hba *phba, uint16_t pci_barset)
12177{
12178 struct pci_dev *pdev;
12179 unsigned long bar_map, bar_map_len;
12180
12181 if (!phba->pcidev)
12182 return NULL;
12183 else
12184 pdev = phba->pcidev;
12185
12186 switch (pci_barset) {
12187 case WQ_PCI_BAR_0_AND_1:
12188 if (!phba->pci_bar0_memmap_p) {
12189 bar_map = pci_resource_start(pdev, PCI_64BIT_BAR0);
12190 bar_map_len = pci_resource_len(pdev, PCI_64BIT_BAR0);
12191 phba->pci_bar0_memmap_p = ioremap(bar_map, bar_map_len);
12192 }
12193 return phba->pci_bar0_memmap_p;
12194 case WQ_PCI_BAR_2_AND_3:
12195 if (!phba->pci_bar2_memmap_p) {
12196 bar_map = pci_resource_start(pdev, PCI_64BIT_BAR2);
12197 bar_map_len = pci_resource_len(pdev, PCI_64BIT_BAR2);
12198 phba->pci_bar2_memmap_p = ioremap(bar_map, bar_map_len);
12199 }
12200 return phba->pci_bar2_memmap_p;
12201 case WQ_PCI_BAR_4_AND_5:
12202 if (!phba->pci_bar4_memmap_p) {
12203 bar_map = pci_resource_start(pdev, PCI_64BIT_BAR4);
12204 bar_map_len = pci_resource_len(pdev, PCI_64BIT_BAR4);
12205 phba->pci_bar4_memmap_p = ioremap(bar_map, bar_map_len);
12206 }
12207 return phba->pci_bar4_memmap_p;
12208 default:
12209 break;
12210 }
12211 return NULL;
12212}
12213
12214/**
James Smart173edbb2012-06-12 13:54:50 -040012215 * lpfc_modify_fcp_eq_delay - Modify Delay Multiplier on FCP EQs
12216 * @phba: HBA structure that indicates port to create a queue on.
12217 * @startq: The starting FCP EQ to modify
12218 *
12219 * This function sends an MODIFY_EQ_DELAY mailbox command to the HBA.
12220 *
12221 * The @phba struct is used to send mailbox command to HBA. The @startq
12222 * is used to get the starting FCP EQ to change.
12223 * This function is asynchronous and will wait for the mailbox
12224 * command to finish before continuing.
12225 *
12226 * On success this function will return a zero. If unable to allocate enough
12227 * memory this function will return -ENOMEM. If the queue create mailbox command
12228 * fails this function will return -ENXIO.
12229 **/
12230uint32_t
12231lpfc_modify_fcp_eq_delay(struct lpfc_hba *phba, uint16_t startq)
12232{
12233 struct lpfc_mbx_modify_eq_delay *eq_delay;
12234 LPFC_MBOXQ_t *mbox;
12235 struct lpfc_queue *eq;
12236 int cnt, rc, length, status = 0;
12237 uint32_t shdr_status, shdr_add_status;
James Smartee020062012-09-29 11:28:52 -040012238 uint32_t result;
James Smart173edbb2012-06-12 13:54:50 -040012239 int fcp_eqidx;
12240 union lpfc_sli4_cfg_shdr *shdr;
12241 uint16_t dmult;
12242
James Smart67d12732012-08-03 12:36:13 -040012243 if (startq >= phba->cfg_fcp_io_channel)
James Smart173edbb2012-06-12 13:54:50 -040012244 return 0;
12245
12246 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12247 if (!mbox)
12248 return -ENOMEM;
12249 length = (sizeof(struct lpfc_mbx_modify_eq_delay) -
12250 sizeof(struct lpfc_sli4_cfg_mhdr));
12251 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
12252 LPFC_MBOX_OPCODE_MODIFY_EQ_DELAY,
12253 length, LPFC_SLI4_MBX_EMBED);
12254 eq_delay = &mbox->u.mqe.un.eq_delay;
12255
12256 /* Calculate delay multiper from maximum interrupt per second */
James Smartee020062012-09-29 11:28:52 -040012257 result = phba->cfg_fcp_imax / phba->cfg_fcp_io_channel;
12258 if (result > LPFC_DMULT_CONST)
12259 dmult = 0;
12260 else
12261 dmult = LPFC_DMULT_CONST/result - 1;
James Smart173edbb2012-06-12 13:54:50 -040012262
12263 cnt = 0;
James Smart67d12732012-08-03 12:36:13 -040012264 for (fcp_eqidx = startq; fcp_eqidx < phba->cfg_fcp_io_channel;
James Smart173edbb2012-06-12 13:54:50 -040012265 fcp_eqidx++) {
James Smart67d12732012-08-03 12:36:13 -040012266 eq = phba->sli4_hba.hba_eq[fcp_eqidx];
James Smart173edbb2012-06-12 13:54:50 -040012267 if (!eq)
12268 continue;
12269 eq_delay->u.request.eq[cnt].eq_id = eq->queue_id;
12270 eq_delay->u.request.eq[cnt].phase = 0;
12271 eq_delay->u.request.eq[cnt].delay_multi = dmult;
12272 cnt++;
12273 if (cnt >= LPFC_MAX_EQ_DELAY)
12274 break;
12275 }
12276 eq_delay->u.request.num_eq = cnt;
12277
12278 mbox->vport = phba->pport;
12279 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
12280 mbox->context1 = NULL;
12281 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
12282 shdr = (union lpfc_sli4_cfg_shdr *) &eq_delay->header.cfg_shdr;
12283 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12284 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12285 if (shdr_status || shdr_add_status || rc) {
12286 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12287 "2512 MODIFY_EQ_DELAY mailbox failed with "
12288 "status x%x add_status x%x, mbx status x%x\n",
12289 shdr_status, shdr_add_status, rc);
12290 status = -ENXIO;
12291 }
12292 mempool_free(mbox, phba->mbox_mem_pool);
12293 return status;
12294}
12295
12296/**
James Smart4f774512009-05-22 14:52:35 -040012297 * lpfc_eq_create - Create an Event Queue on the HBA
12298 * @phba: HBA structure that indicates port to create a queue on.
12299 * @eq: The queue structure to use to create the event queue.
12300 * @imax: The maximum interrupt per second limit.
12301 *
12302 * This function creates an event queue, as detailed in @eq, on a port,
12303 * described by @phba by sending an EQ_CREATE mailbox command to the HBA.
12304 *
12305 * The @phba struct is used to send mailbox command to HBA. The @eq struct
12306 * is used to get the entry count and entry size that are necessary to
12307 * determine the number of pages to allocate and use for this queue. This
12308 * function will send the EQ_CREATE mailbox command to the HBA to setup the
12309 * event queue. This function is asynchronous and will wait for the mailbox
12310 * command to finish before continuing.
12311 *
12312 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040012313 * memory this function will return -ENOMEM. If the queue create mailbox command
12314 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040012315 **/
12316uint32_t
James Smartee020062012-09-29 11:28:52 -040012317lpfc_eq_create(struct lpfc_hba *phba, struct lpfc_queue *eq, uint32_t imax)
James Smart4f774512009-05-22 14:52:35 -040012318{
12319 struct lpfc_mbx_eq_create *eq_create;
12320 LPFC_MBOXQ_t *mbox;
12321 int rc, length, status = 0;
12322 struct lpfc_dmabuf *dmabuf;
12323 uint32_t shdr_status, shdr_add_status;
12324 union lpfc_sli4_cfg_shdr *shdr;
12325 uint16_t dmult;
James Smart49198b32010-04-06 15:04:33 -040012326 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
12327
James Smart2e90f4b2011-12-13 13:22:37 -050012328 /* sanity check on queue memory */
12329 if (!eq)
12330 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040012331 if (!phba->sli4_hba.pc_sli4_params.supported)
12332 hw_page_size = SLI4_PAGE_SIZE;
James Smart4f774512009-05-22 14:52:35 -040012333
12334 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12335 if (!mbox)
12336 return -ENOMEM;
12337 length = (sizeof(struct lpfc_mbx_eq_create) -
12338 sizeof(struct lpfc_sli4_cfg_mhdr));
12339 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
12340 LPFC_MBOX_OPCODE_EQ_CREATE,
12341 length, LPFC_SLI4_MBX_EMBED);
12342 eq_create = &mbox->u.mqe.un.eq_create;
12343 bf_set(lpfc_mbx_eq_create_num_pages, &eq_create->u.request,
12344 eq->page_count);
12345 bf_set(lpfc_eq_context_size, &eq_create->u.request.context,
12346 LPFC_EQE_SIZE);
12347 bf_set(lpfc_eq_context_valid, &eq_create->u.request.context, 1);
12348 /* Calculate delay multiper from maximum interrupt per second */
James Smartee020062012-09-29 11:28:52 -040012349 if (imax > LPFC_DMULT_CONST)
12350 dmult = 0;
12351 else
12352 dmult = LPFC_DMULT_CONST/imax - 1;
James Smart4f774512009-05-22 14:52:35 -040012353 bf_set(lpfc_eq_context_delay_multi, &eq_create->u.request.context,
12354 dmult);
12355 switch (eq->entry_count) {
12356 default:
12357 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12358 "0360 Unsupported EQ count. (%d)\n",
12359 eq->entry_count);
12360 if (eq->entry_count < 256)
12361 return -EINVAL;
12362 /* otherwise default to smallest count (drop through) */
12363 case 256:
12364 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
12365 LPFC_EQ_CNT_256);
12366 break;
12367 case 512:
12368 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
12369 LPFC_EQ_CNT_512);
12370 break;
12371 case 1024:
12372 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
12373 LPFC_EQ_CNT_1024);
12374 break;
12375 case 2048:
12376 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
12377 LPFC_EQ_CNT_2048);
12378 break;
12379 case 4096:
12380 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
12381 LPFC_EQ_CNT_4096);
12382 break;
12383 }
12384 list_for_each_entry(dmabuf, &eq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040012385 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -040012386 eq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
12387 putPaddrLow(dmabuf->phys);
12388 eq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
12389 putPaddrHigh(dmabuf->phys);
12390 }
12391 mbox->vport = phba->pport;
12392 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
12393 mbox->context1 = NULL;
12394 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
12395 shdr = (union lpfc_sli4_cfg_shdr *) &eq_create->header.cfg_shdr;
12396 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12397 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12398 if (shdr_status || shdr_add_status || rc) {
12399 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12400 "2500 EQ_CREATE mailbox failed with "
12401 "status x%x add_status x%x, mbx status x%x\n",
12402 shdr_status, shdr_add_status, rc);
12403 status = -ENXIO;
12404 }
12405 eq->type = LPFC_EQ;
12406 eq->subtype = LPFC_NONE;
12407 eq->queue_id = bf_get(lpfc_mbx_eq_create_q_id, &eq_create->u.response);
12408 if (eq->queue_id == 0xFFFF)
12409 status = -ENXIO;
12410 eq->host_index = 0;
12411 eq->hba_index = 0;
12412
James Smart8fa38512009-07-19 10:01:03 -040012413 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040012414 return status;
12415}
12416
12417/**
12418 * lpfc_cq_create - Create a Completion Queue on the HBA
12419 * @phba: HBA structure that indicates port to create a queue on.
12420 * @cq: The queue structure to use to create the completion queue.
12421 * @eq: The event queue to bind this completion queue to.
12422 *
12423 * This function creates a completion queue, as detailed in @wq, on a port,
12424 * described by @phba by sending a CQ_CREATE mailbox command to the HBA.
12425 *
12426 * The @phba struct is used to send mailbox command to HBA. The @cq struct
12427 * is used to get the entry count and entry size that are necessary to
12428 * determine the number of pages to allocate and use for this queue. The @eq
12429 * is used to indicate which event queue to bind this completion queue to. This
12430 * function will send the CQ_CREATE mailbox command to the HBA to setup the
12431 * completion queue. This function is asynchronous and will wait for the mailbox
12432 * command to finish before continuing.
12433 *
12434 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040012435 * memory this function will return -ENOMEM. If the queue create mailbox command
12436 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040012437 **/
12438uint32_t
12439lpfc_cq_create(struct lpfc_hba *phba, struct lpfc_queue *cq,
12440 struct lpfc_queue *eq, uint32_t type, uint32_t subtype)
12441{
12442 struct lpfc_mbx_cq_create *cq_create;
12443 struct lpfc_dmabuf *dmabuf;
12444 LPFC_MBOXQ_t *mbox;
12445 int rc, length, status = 0;
12446 uint32_t shdr_status, shdr_add_status;
12447 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040012448 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
12449
James Smart2e90f4b2011-12-13 13:22:37 -050012450 /* sanity check on queue memory */
12451 if (!cq || !eq)
12452 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040012453 if (!phba->sli4_hba.pc_sli4_params.supported)
12454 hw_page_size = SLI4_PAGE_SIZE;
12455
James Smart4f774512009-05-22 14:52:35 -040012456 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12457 if (!mbox)
12458 return -ENOMEM;
12459 length = (sizeof(struct lpfc_mbx_cq_create) -
12460 sizeof(struct lpfc_sli4_cfg_mhdr));
12461 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
12462 LPFC_MBOX_OPCODE_CQ_CREATE,
12463 length, LPFC_SLI4_MBX_EMBED);
12464 cq_create = &mbox->u.mqe.un.cq_create;
James Smart5a6f1332011-03-11 16:05:35 -050012465 shdr = (union lpfc_sli4_cfg_shdr *) &cq_create->header.cfg_shdr;
James Smart4f774512009-05-22 14:52:35 -040012466 bf_set(lpfc_mbx_cq_create_num_pages, &cq_create->u.request,
12467 cq->page_count);
12468 bf_set(lpfc_cq_context_event, &cq_create->u.request.context, 1);
12469 bf_set(lpfc_cq_context_valid, &cq_create->u.request.context, 1);
James Smart5a6f1332011-03-11 16:05:35 -050012470 bf_set(lpfc_mbox_hdr_version, &shdr->request,
12471 phba->sli4_hba.pc_sli4_params.cqv);
12472 if (phba->sli4_hba.pc_sli4_params.cqv == LPFC_Q_CREATE_VERSION_2) {
James Smartc31098c2011-04-16 11:03:33 -040012473 /* FW only supports 1. Should be PAGE_SIZE/SLI4_PAGE_SIZE */
12474 bf_set(lpfc_mbx_cq_create_page_size, &cq_create->u.request, 1);
James Smart5a6f1332011-03-11 16:05:35 -050012475 bf_set(lpfc_cq_eq_id_2, &cq_create->u.request.context,
12476 eq->queue_id);
12477 } else {
12478 bf_set(lpfc_cq_eq_id, &cq_create->u.request.context,
12479 eq->queue_id);
12480 }
James Smart4f774512009-05-22 14:52:35 -040012481 switch (cq->entry_count) {
12482 default:
12483 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12484 "0361 Unsupported CQ count. (%d)\n",
12485 cq->entry_count);
James Smart4f4c1862012-06-12 13:54:02 -040012486 if (cq->entry_count < 256) {
12487 status = -EINVAL;
12488 goto out;
12489 }
James Smart4f774512009-05-22 14:52:35 -040012490 /* otherwise default to smallest count (drop through) */
12491 case 256:
12492 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
12493 LPFC_CQ_CNT_256);
12494 break;
12495 case 512:
12496 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
12497 LPFC_CQ_CNT_512);
12498 break;
12499 case 1024:
12500 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
12501 LPFC_CQ_CNT_1024);
12502 break;
12503 }
12504 list_for_each_entry(dmabuf, &cq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040012505 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -040012506 cq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
12507 putPaddrLow(dmabuf->phys);
12508 cq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
12509 putPaddrHigh(dmabuf->phys);
12510 }
12511 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
12512
12513 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart4f774512009-05-22 14:52:35 -040012514 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12515 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12516 if (shdr_status || shdr_add_status || rc) {
12517 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12518 "2501 CQ_CREATE mailbox failed with "
12519 "status x%x add_status x%x, mbx status x%x\n",
12520 shdr_status, shdr_add_status, rc);
12521 status = -ENXIO;
12522 goto out;
12523 }
12524 cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
12525 if (cq->queue_id == 0xFFFF) {
12526 status = -ENXIO;
12527 goto out;
12528 }
12529 /* link the cq onto the parent eq child list */
12530 list_add_tail(&cq->list, &eq->child_list);
12531 /* Set up completion queue's type and subtype */
12532 cq->type = type;
12533 cq->subtype = subtype;
12534 cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
James Smart2a622bf2011-02-16 12:40:06 -050012535 cq->assoc_qid = eq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040012536 cq->host_index = 0;
12537 cq->hba_index = 0;
James Smart4f774512009-05-22 14:52:35 -040012538
James Smart8fa38512009-07-19 10:01:03 -040012539out:
12540 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040012541 return status;
12542}
12543
12544/**
James Smartb19a0612010-04-06 14:48:51 -040012545 * lpfc_mq_create_fb_init - Send MCC_CREATE without async events registration
James Smart04c68492009-05-22 14:52:52 -040012546 * @phba: HBA structure that indicates port to create a queue on.
12547 * @mq: The queue structure to use to create the mailbox queue.
James Smartb19a0612010-04-06 14:48:51 -040012548 * @mbox: An allocated pointer to type LPFC_MBOXQ_t
12549 * @cq: The completion queue to associate with this cq.
James Smart04c68492009-05-22 14:52:52 -040012550 *
James Smartb19a0612010-04-06 14:48:51 -040012551 * This function provides failback (fb) functionality when the
12552 * mq_create_ext fails on older FW generations. It's purpose is identical
12553 * to mq_create_ext otherwise.
James Smart04c68492009-05-22 14:52:52 -040012554 *
James Smartb19a0612010-04-06 14:48:51 -040012555 * This routine cannot fail as all attributes were previously accessed and
12556 * initialized in mq_create_ext.
James Smart04c68492009-05-22 14:52:52 -040012557 **/
James Smartb19a0612010-04-06 14:48:51 -040012558static void
12559lpfc_mq_create_fb_init(struct lpfc_hba *phba, struct lpfc_queue *mq,
12560 LPFC_MBOXQ_t *mbox, struct lpfc_queue *cq)
James Smart04c68492009-05-22 14:52:52 -040012561{
12562 struct lpfc_mbx_mq_create *mq_create;
12563 struct lpfc_dmabuf *dmabuf;
James Smartb19a0612010-04-06 14:48:51 -040012564 int length;
James Smart04c68492009-05-22 14:52:52 -040012565
James Smart04c68492009-05-22 14:52:52 -040012566 length = (sizeof(struct lpfc_mbx_mq_create) -
12567 sizeof(struct lpfc_sli4_cfg_mhdr));
12568 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
12569 LPFC_MBOX_OPCODE_MQ_CREATE,
12570 length, LPFC_SLI4_MBX_EMBED);
12571 mq_create = &mbox->u.mqe.un.mq_create;
12572 bf_set(lpfc_mbx_mq_create_num_pages, &mq_create->u.request,
James Smartb19a0612010-04-06 14:48:51 -040012573 mq->page_count);
James Smart04c68492009-05-22 14:52:52 -040012574 bf_set(lpfc_mq_context_cq_id, &mq_create->u.request.context,
James Smartb19a0612010-04-06 14:48:51 -040012575 cq->queue_id);
James Smart04c68492009-05-22 14:52:52 -040012576 bf_set(lpfc_mq_context_valid, &mq_create->u.request.context, 1);
12577 switch (mq->entry_count) {
James Smart04c68492009-05-22 14:52:52 -040012578 case 16:
James Smart5a6f1332011-03-11 16:05:35 -050012579 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
12580 LPFC_MQ_RING_SIZE_16);
James Smart04c68492009-05-22 14:52:52 -040012581 break;
12582 case 32:
James Smart5a6f1332011-03-11 16:05:35 -050012583 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
12584 LPFC_MQ_RING_SIZE_32);
James Smart04c68492009-05-22 14:52:52 -040012585 break;
12586 case 64:
James Smart5a6f1332011-03-11 16:05:35 -050012587 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
12588 LPFC_MQ_RING_SIZE_64);
James Smart04c68492009-05-22 14:52:52 -040012589 break;
12590 case 128:
James Smart5a6f1332011-03-11 16:05:35 -050012591 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
12592 LPFC_MQ_RING_SIZE_128);
James Smart04c68492009-05-22 14:52:52 -040012593 break;
12594 }
12595 list_for_each_entry(dmabuf, &mq->page_list, list) {
12596 mq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
James Smartb19a0612010-04-06 14:48:51 -040012597 putPaddrLow(dmabuf->phys);
James Smart04c68492009-05-22 14:52:52 -040012598 mq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
James Smartb19a0612010-04-06 14:48:51 -040012599 putPaddrHigh(dmabuf->phys);
12600 }
12601}
12602
12603/**
12604 * lpfc_mq_create - Create a mailbox Queue on the HBA
12605 * @phba: HBA structure that indicates port to create a queue on.
12606 * @mq: The queue structure to use to create the mailbox queue.
12607 * @cq: The completion queue to associate with this cq.
12608 * @subtype: The queue's subtype.
12609 *
12610 * This function creates a mailbox queue, as detailed in @mq, on a port,
12611 * described by @phba by sending a MQ_CREATE mailbox command to the HBA.
12612 *
12613 * The @phba struct is used to send mailbox command to HBA. The @cq struct
12614 * is used to get the entry count and entry size that are necessary to
12615 * determine the number of pages to allocate and use for this queue. This
12616 * function will send the MQ_CREATE mailbox command to the HBA to setup the
12617 * mailbox queue. This function is asynchronous and will wait for the mailbox
12618 * command to finish before continuing.
12619 *
12620 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040012621 * memory this function will return -ENOMEM. If the queue create mailbox command
12622 * fails this function will return -ENXIO.
James Smartb19a0612010-04-06 14:48:51 -040012623 **/
12624int32_t
12625lpfc_mq_create(struct lpfc_hba *phba, struct lpfc_queue *mq,
12626 struct lpfc_queue *cq, uint32_t subtype)
12627{
12628 struct lpfc_mbx_mq_create *mq_create;
12629 struct lpfc_mbx_mq_create_ext *mq_create_ext;
12630 struct lpfc_dmabuf *dmabuf;
12631 LPFC_MBOXQ_t *mbox;
12632 int rc, length, status = 0;
12633 uint32_t shdr_status, shdr_add_status;
12634 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040012635 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smartb19a0612010-04-06 14:48:51 -040012636
James Smart2e90f4b2011-12-13 13:22:37 -050012637 /* sanity check on queue memory */
12638 if (!mq || !cq)
12639 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040012640 if (!phba->sli4_hba.pc_sli4_params.supported)
12641 hw_page_size = SLI4_PAGE_SIZE;
James Smartb19a0612010-04-06 14:48:51 -040012642
12643 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12644 if (!mbox)
12645 return -ENOMEM;
12646 length = (sizeof(struct lpfc_mbx_mq_create_ext) -
12647 sizeof(struct lpfc_sli4_cfg_mhdr));
12648 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
12649 LPFC_MBOX_OPCODE_MQ_CREATE_EXT,
12650 length, LPFC_SLI4_MBX_EMBED);
12651
12652 mq_create_ext = &mbox->u.mqe.un.mq_create_ext;
James Smart5a6f1332011-03-11 16:05:35 -050012653 shdr = (union lpfc_sli4_cfg_shdr *) &mq_create_ext->header.cfg_shdr;
James Smart70f3c072010-12-15 17:57:33 -050012654 bf_set(lpfc_mbx_mq_create_ext_num_pages,
12655 &mq_create_ext->u.request, mq->page_count);
12656 bf_set(lpfc_mbx_mq_create_ext_async_evt_link,
12657 &mq_create_ext->u.request, 1);
12658 bf_set(lpfc_mbx_mq_create_ext_async_evt_fip,
James Smartb19a0612010-04-06 14:48:51 -040012659 &mq_create_ext->u.request, 1);
12660 bf_set(lpfc_mbx_mq_create_ext_async_evt_group5,
12661 &mq_create_ext->u.request, 1);
James Smart70f3c072010-12-15 17:57:33 -050012662 bf_set(lpfc_mbx_mq_create_ext_async_evt_fc,
12663 &mq_create_ext->u.request, 1);
12664 bf_set(lpfc_mbx_mq_create_ext_async_evt_sli,
12665 &mq_create_ext->u.request, 1);
James Smartb19a0612010-04-06 14:48:51 -040012666 bf_set(lpfc_mq_context_valid, &mq_create_ext->u.request.context, 1);
James Smart5a6f1332011-03-11 16:05:35 -050012667 bf_set(lpfc_mbox_hdr_version, &shdr->request,
12668 phba->sli4_hba.pc_sli4_params.mqv);
12669 if (phba->sli4_hba.pc_sli4_params.mqv == LPFC_Q_CREATE_VERSION_1)
12670 bf_set(lpfc_mbx_mq_create_ext_cq_id, &mq_create_ext->u.request,
12671 cq->queue_id);
12672 else
12673 bf_set(lpfc_mq_context_cq_id, &mq_create_ext->u.request.context,
12674 cq->queue_id);
James Smartb19a0612010-04-06 14:48:51 -040012675 switch (mq->entry_count) {
12676 default:
12677 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12678 "0362 Unsupported MQ count. (%d)\n",
12679 mq->entry_count);
James Smart4f4c1862012-06-12 13:54:02 -040012680 if (mq->entry_count < 16) {
12681 status = -EINVAL;
12682 goto out;
12683 }
James Smartb19a0612010-04-06 14:48:51 -040012684 /* otherwise default to smallest count (drop through) */
12685 case 16:
James Smart5a6f1332011-03-11 16:05:35 -050012686 bf_set(lpfc_mq_context_ring_size,
12687 &mq_create_ext->u.request.context,
12688 LPFC_MQ_RING_SIZE_16);
James Smartb19a0612010-04-06 14:48:51 -040012689 break;
12690 case 32:
James Smart5a6f1332011-03-11 16:05:35 -050012691 bf_set(lpfc_mq_context_ring_size,
12692 &mq_create_ext->u.request.context,
12693 LPFC_MQ_RING_SIZE_32);
James Smartb19a0612010-04-06 14:48:51 -040012694 break;
12695 case 64:
James Smart5a6f1332011-03-11 16:05:35 -050012696 bf_set(lpfc_mq_context_ring_size,
12697 &mq_create_ext->u.request.context,
12698 LPFC_MQ_RING_SIZE_64);
James Smartb19a0612010-04-06 14:48:51 -040012699 break;
12700 case 128:
James Smart5a6f1332011-03-11 16:05:35 -050012701 bf_set(lpfc_mq_context_ring_size,
12702 &mq_create_ext->u.request.context,
12703 LPFC_MQ_RING_SIZE_128);
James Smartb19a0612010-04-06 14:48:51 -040012704 break;
12705 }
12706 list_for_each_entry(dmabuf, &mq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040012707 memset(dmabuf->virt, 0, hw_page_size);
James Smartb19a0612010-04-06 14:48:51 -040012708 mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_lo =
12709 putPaddrLow(dmabuf->phys);
12710 mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_hi =
James Smart04c68492009-05-22 14:52:52 -040012711 putPaddrHigh(dmabuf->phys);
12712 }
12713 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
James Smartb19a0612010-04-06 14:48:51 -040012714 mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
12715 &mq_create_ext->u.response);
12716 if (rc != MBX_SUCCESS) {
12717 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
12718 "2795 MQ_CREATE_EXT failed with "
12719 "status x%x. Failback to MQ_CREATE.\n",
12720 rc);
12721 lpfc_mq_create_fb_init(phba, mq, mbox, cq);
12722 mq_create = &mbox->u.mqe.un.mq_create;
12723 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
12724 shdr = (union lpfc_sli4_cfg_shdr *) &mq_create->header.cfg_shdr;
12725 mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
12726 &mq_create->u.response);
12727 }
12728
James Smart04c68492009-05-22 14:52:52 -040012729 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart04c68492009-05-22 14:52:52 -040012730 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12731 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12732 if (shdr_status || shdr_add_status || rc) {
12733 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12734 "2502 MQ_CREATE mailbox failed with "
12735 "status x%x add_status x%x, mbx status x%x\n",
12736 shdr_status, shdr_add_status, rc);
12737 status = -ENXIO;
12738 goto out;
12739 }
James Smart04c68492009-05-22 14:52:52 -040012740 if (mq->queue_id == 0xFFFF) {
12741 status = -ENXIO;
12742 goto out;
12743 }
12744 mq->type = LPFC_MQ;
James Smart2a622bf2011-02-16 12:40:06 -050012745 mq->assoc_qid = cq->queue_id;
James Smart04c68492009-05-22 14:52:52 -040012746 mq->subtype = subtype;
12747 mq->host_index = 0;
12748 mq->hba_index = 0;
12749
12750 /* link the mq onto the parent cq child list */
12751 list_add_tail(&mq->list, &cq->child_list);
12752out:
James Smart8fa38512009-07-19 10:01:03 -040012753 mempool_free(mbox, phba->mbox_mem_pool);
James Smart04c68492009-05-22 14:52:52 -040012754 return status;
12755}
12756
12757/**
James Smart4f774512009-05-22 14:52:35 -040012758 * lpfc_wq_create - Create a Work Queue on the HBA
12759 * @phba: HBA structure that indicates port to create a queue on.
12760 * @wq: The queue structure to use to create the work queue.
12761 * @cq: The completion queue to bind this work queue to.
12762 * @subtype: The subtype of the work queue indicating its functionality.
12763 *
12764 * This function creates a work queue, as detailed in @wq, on a port, described
12765 * by @phba by sending a WQ_CREATE mailbox command to the HBA.
12766 *
12767 * The @phba struct is used to send mailbox command to HBA. The @wq struct
12768 * is used to get the entry count and entry size that are necessary to
12769 * determine the number of pages to allocate and use for this queue. The @cq
12770 * is used to indicate which completion queue to bind this work queue to. This
12771 * function will send the WQ_CREATE mailbox command to the HBA to setup the
12772 * work queue. This function is asynchronous and will wait for the mailbox
12773 * command to finish before continuing.
12774 *
12775 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040012776 * memory this function will return -ENOMEM. If the queue create mailbox command
12777 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040012778 **/
12779uint32_t
12780lpfc_wq_create(struct lpfc_hba *phba, struct lpfc_queue *wq,
12781 struct lpfc_queue *cq, uint32_t subtype)
12782{
12783 struct lpfc_mbx_wq_create *wq_create;
12784 struct lpfc_dmabuf *dmabuf;
12785 LPFC_MBOXQ_t *mbox;
12786 int rc, length, status = 0;
12787 uint32_t shdr_status, shdr_add_status;
12788 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040012789 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smart5a6f1332011-03-11 16:05:35 -050012790 struct dma_address *page;
James Smart962bc512013-01-03 15:44:00 -050012791 void __iomem *bar_memmap_p;
12792 uint32_t db_offset;
12793 uint16_t pci_barset;
James Smart49198b32010-04-06 15:04:33 -040012794
James Smart2e90f4b2011-12-13 13:22:37 -050012795 /* sanity check on queue memory */
12796 if (!wq || !cq)
12797 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040012798 if (!phba->sli4_hba.pc_sli4_params.supported)
12799 hw_page_size = SLI4_PAGE_SIZE;
James Smart4f774512009-05-22 14:52:35 -040012800
12801 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12802 if (!mbox)
12803 return -ENOMEM;
12804 length = (sizeof(struct lpfc_mbx_wq_create) -
12805 sizeof(struct lpfc_sli4_cfg_mhdr));
12806 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
12807 LPFC_MBOX_OPCODE_FCOE_WQ_CREATE,
12808 length, LPFC_SLI4_MBX_EMBED);
12809 wq_create = &mbox->u.mqe.un.wq_create;
James Smart5a6f1332011-03-11 16:05:35 -050012810 shdr = (union lpfc_sli4_cfg_shdr *) &wq_create->header.cfg_shdr;
James Smart4f774512009-05-22 14:52:35 -040012811 bf_set(lpfc_mbx_wq_create_num_pages, &wq_create->u.request,
12812 wq->page_count);
12813 bf_set(lpfc_mbx_wq_create_cq_id, &wq_create->u.request,
12814 cq->queue_id);
James Smart5a6f1332011-03-11 16:05:35 -050012815 bf_set(lpfc_mbox_hdr_version, &shdr->request,
12816 phba->sli4_hba.pc_sli4_params.wqv);
James Smart962bc512013-01-03 15:44:00 -050012817
James Smart5a6f1332011-03-11 16:05:35 -050012818 if (phba->sli4_hba.pc_sli4_params.wqv == LPFC_Q_CREATE_VERSION_1) {
12819 bf_set(lpfc_mbx_wq_create_wqe_count, &wq_create->u.request_1,
12820 wq->entry_count);
12821 switch (wq->entry_size) {
12822 default:
12823 case 64:
12824 bf_set(lpfc_mbx_wq_create_wqe_size,
12825 &wq_create->u.request_1,
12826 LPFC_WQ_WQE_SIZE_64);
12827 break;
12828 case 128:
12829 bf_set(lpfc_mbx_wq_create_wqe_size,
12830 &wq_create->u.request_1,
12831 LPFC_WQ_WQE_SIZE_128);
12832 break;
12833 }
12834 bf_set(lpfc_mbx_wq_create_page_size, &wq_create->u.request_1,
12835 (PAGE_SIZE/SLI4_PAGE_SIZE));
12836 page = wq_create->u.request_1.page;
12837 } else {
12838 page = wq_create->u.request.page;
12839 }
James Smart4f774512009-05-22 14:52:35 -040012840 list_for_each_entry(dmabuf, &wq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040012841 memset(dmabuf->virt, 0, hw_page_size);
James Smart5a6f1332011-03-11 16:05:35 -050012842 page[dmabuf->buffer_tag].addr_lo = putPaddrLow(dmabuf->phys);
12843 page[dmabuf->buffer_tag].addr_hi = putPaddrHigh(dmabuf->phys);
James Smart4f774512009-05-22 14:52:35 -040012844 }
James Smart962bc512013-01-03 15:44:00 -050012845
12846 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
12847 bf_set(lpfc_mbx_wq_create_dua, &wq_create->u.request, 1);
12848
James Smart4f774512009-05-22 14:52:35 -040012849 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
12850 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart4f774512009-05-22 14:52:35 -040012851 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12852 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12853 if (shdr_status || shdr_add_status || rc) {
12854 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12855 "2503 WQ_CREATE mailbox failed with "
12856 "status x%x add_status x%x, mbx status x%x\n",
12857 shdr_status, shdr_add_status, rc);
12858 status = -ENXIO;
12859 goto out;
12860 }
12861 wq->queue_id = bf_get(lpfc_mbx_wq_create_q_id, &wq_create->u.response);
12862 if (wq->queue_id == 0xFFFF) {
12863 status = -ENXIO;
12864 goto out;
12865 }
James Smart962bc512013-01-03 15:44:00 -050012866 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE) {
12867 wq->db_format = bf_get(lpfc_mbx_wq_create_db_format,
12868 &wq_create->u.response);
12869 if ((wq->db_format != LPFC_DB_LIST_FORMAT) &&
12870 (wq->db_format != LPFC_DB_RING_FORMAT)) {
12871 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12872 "3265 WQ[%d] doorbell format not "
12873 "supported: x%x\n", wq->queue_id,
12874 wq->db_format);
12875 status = -EINVAL;
12876 goto out;
12877 }
12878 pci_barset = bf_get(lpfc_mbx_wq_create_bar_set,
12879 &wq_create->u.response);
12880 bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba, pci_barset);
12881 if (!bar_memmap_p) {
12882 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12883 "3263 WQ[%d] failed to memmap pci "
12884 "barset:x%x\n", wq->queue_id,
12885 pci_barset);
12886 status = -ENOMEM;
12887 goto out;
12888 }
12889 db_offset = wq_create->u.response.doorbell_offset;
12890 if ((db_offset != LPFC_ULP0_WQ_DOORBELL) &&
12891 (db_offset != LPFC_ULP1_WQ_DOORBELL)) {
12892 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12893 "3252 WQ[%d] doorbell offset not "
12894 "supported: x%x\n", wq->queue_id,
12895 db_offset);
12896 status = -EINVAL;
12897 goto out;
12898 }
12899 wq->db_regaddr = bar_memmap_p + db_offset;
12900 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
12901 "3264 WQ[%d]: barset:x%x, offset:x%x\n",
12902 wq->queue_id, pci_barset, db_offset);
12903 } else {
12904 wq->db_format = LPFC_DB_LIST_FORMAT;
12905 wq->db_regaddr = phba->sli4_hba.WQDBregaddr;
12906 }
James Smart4f774512009-05-22 14:52:35 -040012907 wq->type = LPFC_WQ;
James Smart2a622bf2011-02-16 12:40:06 -050012908 wq->assoc_qid = cq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040012909 wq->subtype = subtype;
12910 wq->host_index = 0;
12911 wq->hba_index = 0;
James Smartff78d8f2011-12-13 13:21:35 -050012912 wq->entry_repost = LPFC_RELEASE_NOTIFICATION_INTERVAL;
James Smart4f774512009-05-22 14:52:35 -040012913
12914 /* link the wq onto the parent cq child list */
12915 list_add_tail(&wq->list, &cq->child_list);
12916out:
James Smart8fa38512009-07-19 10:01:03 -040012917 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040012918 return status;
12919}
12920
12921/**
James Smart73d91e52011-10-10 21:32:10 -040012922 * lpfc_rq_adjust_repost - Adjust entry_repost for an RQ
12923 * @phba: HBA structure that indicates port to create a queue on.
12924 * @rq: The queue structure to use for the receive queue.
12925 * @qno: The associated HBQ number
12926 *
12927 *
12928 * For SLI4 we need to adjust the RQ repost value based on
12929 * the number of buffers that are initially posted to the RQ.
12930 */
12931void
12932lpfc_rq_adjust_repost(struct lpfc_hba *phba, struct lpfc_queue *rq, int qno)
12933{
12934 uint32_t cnt;
12935
James Smart2e90f4b2011-12-13 13:22:37 -050012936 /* sanity check on queue memory */
12937 if (!rq)
12938 return;
James Smart73d91e52011-10-10 21:32:10 -040012939 cnt = lpfc_hbq_defs[qno]->entry_count;
12940
12941 /* Recalc repost for RQs based on buffers initially posted */
12942 cnt = (cnt >> 3);
12943 if (cnt < LPFC_QUEUE_MIN_REPOST)
12944 cnt = LPFC_QUEUE_MIN_REPOST;
12945
12946 rq->entry_repost = cnt;
12947}
12948
12949/**
James Smart4f774512009-05-22 14:52:35 -040012950 * lpfc_rq_create - Create a Receive Queue on the HBA
12951 * @phba: HBA structure that indicates port to create a queue on.
12952 * @hrq: The queue structure to use to create the header receive queue.
12953 * @drq: The queue structure to use to create the data receive queue.
12954 * @cq: The completion queue to bind this work queue to.
12955 *
12956 * This function creates a receive buffer queue pair , as detailed in @hrq and
12957 * @drq, on a port, described by @phba by sending a RQ_CREATE mailbox command
12958 * to the HBA.
12959 *
12960 * The @phba struct is used to send mailbox command to HBA. The @drq and @hrq
12961 * struct is used to get the entry count that is necessary to determine the
12962 * number of pages to use for this queue. The @cq is used to indicate which
12963 * completion queue to bind received buffers that are posted to these queues to.
12964 * This function will send the RQ_CREATE mailbox command to the HBA to setup the
12965 * receive queue pair. This function is asynchronous and will wait for the
12966 * mailbox command to finish before continuing.
12967 *
12968 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040012969 * memory this function will return -ENOMEM. If the queue create mailbox command
12970 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040012971 **/
12972uint32_t
12973lpfc_rq_create(struct lpfc_hba *phba, struct lpfc_queue *hrq,
12974 struct lpfc_queue *drq, struct lpfc_queue *cq, uint32_t subtype)
12975{
12976 struct lpfc_mbx_rq_create *rq_create;
12977 struct lpfc_dmabuf *dmabuf;
12978 LPFC_MBOXQ_t *mbox;
12979 int rc, length, status = 0;
12980 uint32_t shdr_status, shdr_add_status;
12981 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040012982 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smart962bc512013-01-03 15:44:00 -050012983 void __iomem *bar_memmap_p;
12984 uint32_t db_offset;
12985 uint16_t pci_barset;
James Smart49198b32010-04-06 15:04:33 -040012986
James Smart2e90f4b2011-12-13 13:22:37 -050012987 /* sanity check on queue memory */
12988 if (!hrq || !drq || !cq)
12989 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040012990 if (!phba->sli4_hba.pc_sli4_params.supported)
12991 hw_page_size = SLI4_PAGE_SIZE;
James Smart4f774512009-05-22 14:52:35 -040012992
12993 if (hrq->entry_count != drq->entry_count)
12994 return -EINVAL;
12995 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12996 if (!mbox)
12997 return -ENOMEM;
12998 length = (sizeof(struct lpfc_mbx_rq_create) -
12999 sizeof(struct lpfc_sli4_cfg_mhdr));
13000 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
13001 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
13002 length, LPFC_SLI4_MBX_EMBED);
13003 rq_create = &mbox->u.mqe.un.rq_create;
James Smart5a6f1332011-03-11 16:05:35 -050013004 shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
13005 bf_set(lpfc_mbox_hdr_version, &shdr->request,
13006 phba->sli4_hba.pc_sli4_params.rqv);
13007 if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
13008 bf_set(lpfc_rq_context_rqe_count_1,
13009 &rq_create->u.request.context,
13010 hrq->entry_count);
13011 rq_create->u.request.context.buffer_size = LPFC_HDR_BUF_SIZE;
James Smartc31098c2011-04-16 11:03:33 -040013012 bf_set(lpfc_rq_context_rqe_size,
13013 &rq_create->u.request.context,
13014 LPFC_RQE_SIZE_8);
13015 bf_set(lpfc_rq_context_page_size,
13016 &rq_create->u.request.context,
13017 (PAGE_SIZE/SLI4_PAGE_SIZE));
James Smart5a6f1332011-03-11 16:05:35 -050013018 } else {
13019 switch (hrq->entry_count) {
13020 default:
13021 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13022 "2535 Unsupported RQ count. (%d)\n",
13023 hrq->entry_count);
James Smart4f4c1862012-06-12 13:54:02 -040013024 if (hrq->entry_count < 512) {
13025 status = -EINVAL;
13026 goto out;
13027 }
James Smart5a6f1332011-03-11 16:05:35 -050013028 /* otherwise default to smallest count (drop through) */
13029 case 512:
13030 bf_set(lpfc_rq_context_rqe_count,
13031 &rq_create->u.request.context,
13032 LPFC_RQ_RING_SIZE_512);
13033 break;
13034 case 1024:
13035 bf_set(lpfc_rq_context_rqe_count,
13036 &rq_create->u.request.context,
13037 LPFC_RQ_RING_SIZE_1024);
13038 break;
13039 case 2048:
13040 bf_set(lpfc_rq_context_rqe_count,
13041 &rq_create->u.request.context,
13042 LPFC_RQ_RING_SIZE_2048);
13043 break;
13044 case 4096:
13045 bf_set(lpfc_rq_context_rqe_count,
13046 &rq_create->u.request.context,
13047 LPFC_RQ_RING_SIZE_4096);
13048 break;
13049 }
13050 bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
13051 LPFC_HDR_BUF_SIZE);
James Smart4f774512009-05-22 14:52:35 -040013052 }
13053 bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
13054 cq->queue_id);
13055 bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
13056 hrq->page_count);
James Smart4f774512009-05-22 14:52:35 -040013057 list_for_each_entry(dmabuf, &hrq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040013058 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -040013059 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
13060 putPaddrLow(dmabuf->phys);
13061 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
13062 putPaddrHigh(dmabuf->phys);
13063 }
James Smart962bc512013-01-03 15:44:00 -050013064 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
13065 bf_set(lpfc_mbx_rq_create_dua, &rq_create->u.request, 1);
13066
James Smart4f774512009-05-22 14:52:35 -040013067 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
13068 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart4f774512009-05-22 14:52:35 -040013069 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13070 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13071 if (shdr_status || shdr_add_status || rc) {
13072 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13073 "2504 RQ_CREATE mailbox failed with "
13074 "status x%x add_status x%x, mbx status x%x\n",
13075 shdr_status, shdr_add_status, rc);
13076 status = -ENXIO;
13077 goto out;
13078 }
13079 hrq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
13080 if (hrq->queue_id == 0xFFFF) {
13081 status = -ENXIO;
13082 goto out;
13083 }
James Smart962bc512013-01-03 15:44:00 -050013084
13085 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE) {
13086 hrq->db_format = bf_get(lpfc_mbx_rq_create_db_format,
13087 &rq_create->u.response);
13088 if ((hrq->db_format != LPFC_DB_LIST_FORMAT) &&
13089 (hrq->db_format != LPFC_DB_RING_FORMAT)) {
13090 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13091 "3262 RQ [%d] doorbell format not "
13092 "supported: x%x\n", hrq->queue_id,
13093 hrq->db_format);
13094 status = -EINVAL;
13095 goto out;
13096 }
13097
13098 pci_barset = bf_get(lpfc_mbx_rq_create_bar_set,
13099 &rq_create->u.response);
13100 bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba, pci_barset);
13101 if (!bar_memmap_p) {
13102 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13103 "3269 RQ[%d] failed to memmap pci "
13104 "barset:x%x\n", hrq->queue_id,
13105 pci_barset);
13106 status = -ENOMEM;
13107 goto out;
13108 }
13109
13110 db_offset = rq_create->u.response.doorbell_offset;
13111 if ((db_offset != LPFC_ULP0_RQ_DOORBELL) &&
13112 (db_offset != LPFC_ULP1_RQ_DOORBELL)) {
13113 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13114 "3270 RQ[%d] doorbell offset not "
13115 "supported: x%x\n", hrq->queue_id,
13116 db_offset);
13117 status = -EINVAL;
13118 goto out;
13119 }
13120 hrq->db_regaddr = bar_memmap_p + db_offset;
13121 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
13122 "3266 RQ[qid:%d]: barset:x%x, offset:x%x\n",
13123 hrq->queue_id, pci_barset, db_offset);
13124 } else {
13125 hrq->db_format = LPFC_DB_RING_FORMAT;
13126 hrq->db_regaddr = phba->sli4_hba.RQDBregaddr;
13127 }
James Smart4f774512009-05-22 14:52:35 -040013128 hrq->type = LPFC_HRQ;
James Smart2a622bf2011-02-16 12:40:06 -050013129 hrq->assoc_qid = cq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040013130 hrq->subtype = subtype;
13131 hrq->host_index = 0;
13132 hrq->hba_index = 0;
13133
13134 /* now create the data queue */
13135 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
13136 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
13137 length, LPFC_SLI4_MBX_EMBED);
James Smart5a6f1332011-03-11 16:05:35 -050013138 bf_set(lpfc_mbox_hdr_version, &shdr->request,
13139 phba->sli4_hba.pc_sli4_params.rqv);
13140 if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
13141 bf_set(lpfc_rq_context_rqe_count_1,
James Smartc31098c2011-04-16 11:03:33 -040013142 &rq_create->u.request.context, hrq->entry_count);
James Smart5a6f1332011-03-11 16:05:35 -050013143 rq_create->u.request.context.buffer_size = LPFC_DATA_BUF_SIZE;
James Smartc31098c2011-04-16 11:03:33 -040013144 bf_set(lpfc_rq_context_rqe_size, &rq_create->u.request.context,
13145 LPFC_RQE_SIZE_8);
13146 bf_set(lpfc_rq_context_page_size, &rq_create->u.request.context,
13147 (PAGE_SIZE/SLI4_PAGE_SIZE));
James Smart5a6f1332011-03-11 16:05:35 -050013148 } else {
13149 switch (drq->entry_count) {
13150 default:
13151 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13152 "2536 Unsupported RQ count. (%d)\n",
13153 drq->entry_count);
James Smart4f4c1862012-06-12 13:54:02 -040013154 if (drq->entry_count < 512) {
13155 status = -EINVAL;
13156 goto out;
13157 }
James Smart5a6f1332011-03-11 16:05:35 -050013158 /* otherwise default to smallest count (drop through) */
13159 case 512:
13160 bf_set(lpfc_rq_context_rqe_count,
13161 &rq_create->u.request.context,
13162 LPFC_RQ_RING_SIZE_512);
13163 break;
13164 case 1024:
13165 bf_set(lpfc_rq_context_rqe_count,
13166 &rq_create->u.request.context,
13167 LPFC_RQ_RING_SIZE_1024);
13168 break;
13169 case 2048:
13170 bf_set(lpfc_rq_context_rqe_count,
13171 &rq_create->u.request.context,
13172 LPFC_RQ_RING_SIZE_2048);
13173 break;
13174 case 4096:
13175 bf_set(lpfc_rq_context_rqe_count,
13176 &rq_create->u.request.context,
13177 LPFC_RQ_RING_SIZE_4096);
13178 break;
13179 }
13180 bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
13181 LPFC_DATA_BUF_SIZE);
James Smart4f774512009-05-22 14:52:35 -040013182 }
13183 bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
13184 cq->queue_id);
13185 bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
13186 drq->page_count);
James Smart4f774512009-05-22 14:52:35 -040013187 list_for_each_entry(dmabuf, &drq->page_list, list) {
13188 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
13189 putPaddrLow(dmabuf->phys);
13190 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
13191 putPaddrHigh(dmabuf->phys);
13192 }
James Smart962bc512013-01-03 15:44:00 -050013193 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
13194 bf_set(lpfc_mbx_rq_create_dua, &rq_create->u.request, 1);
James Smart4f774512009-05-22 14:52:35 -040013195 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
13196 /* The IOCTL status is embedded in the mailbox subheader. */
13197 shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
13198 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13199 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13200 if (shdr_status || shdr_add_status || rc) {
13201 status = -ENXIO;
13202 goto out;
13203 }
13204 drq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
13205 if (drq->queue_id == 0xFFFF) {
13206 status = -ENXIO;
13207 goto out;
13208 }
13209 drq->type = LPFC_DRQ;
James Smart2a622bf2011-02-16 12:40:06 -050013210 drq->assoc_qid = cq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040013211 drq->subtype = subtype;
13212 drq->host_index = 0;
13213 drq->hba_index = 0;
13214
13215 /* link the header and data RQs onto the parent cq child list */
13216 list_add_tail(&hrq->list, &cq->child_list);
13217 list_add_tail(&drq->list, &cq->child_list);
13218
13219out:
James Smart8fa38512009-07-19 10:01:03 -040013220 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040013221 return status;
13222}
13223
13224/**
13225 * lpfc_eq_destroy - Destroy an event Queue on the HBA
13226 * @eq: The queue structure associated with the queue to destroy.
13227 *
13228 * This function destroys a queue, as detailed in @eq by sending an mailbox
13229 * command, specific to the type of queue, to the HBA.
13230 *
13231 * The @eq struct is used to get the queue ID of the queue to destroy.
13232 *
13233 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040013234 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040013235 **/
13236uint32_t
13237lpfc_eq_destroy(struct lpfc_hba *phba, struct lpfc_queue *eq)
13238{
13239 LPFC_MBOXQ_t *mbox;
13240 int rc, length, status = 0;
13241 uint32_t shdr_status, shdr_add_status;
13242 union lpfc_sli4_cfg_shdr *shdr;
13243
James Smart2e90f4b2011-12-13 13:22:37 -050013244 /* sanity check on queue memory */
James Smart4f774512009-05-22 14:52:35 -040013245 if (!eq)
13246 return -ENODEV;
13247 mbox = mempool_alloc(eq->phba->mbox_mem_pool, GFP_KERNEL);
13248 if (!mbox)
13249 return -ENOMEM;
13250 length = (sizeof(struct lpfc_mbx_eq_destroy) -
13251 sizeof(struct lpfc_sli4_cfg_mhdr));
13252 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
13253 LPFC_MBOX_OPCODE_EQ_DESTROY,
13254 length, LPFC_SLI4_MBX_EMBED);
13255 bf_set(lpfc_mbx_eq_destroy_q_id, &mbox->u.mqe.un.eq_destroy.u.request,
13256 eq->queue_id);
13257 mbox->vport = eq->phba->pport;
13258 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
13259
13260 rc = lpfc_sli_issue_mbox(eq->phba, mbox, MBX_POLL);
13261 /* The IOCTL status is embedded in the mailbox subheader. */
13262 shdr = (union lpfc_sli4_cfg_shdr *)
13263 &mbox->u.mqe.un.eq_destroy.header.cfg_shdr;
13264 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13265 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13266 if (shdr_status || shdr_add_status || rc) {
13267 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13268 "2505 EQ_DESTROY mailbox failed with "
13269 "status x%x add_status x%x, mbx status x%x\n",
13270 shdr_status, shdr_add_status, rc);
13271 status = -ENXIO;
13272 }
13273
13274 /* Remove eq from any list */
13275 list_del_init(&eq->list);
James Smart8fa38512009-07-19 10:01:03 -040013276 mempool_free(mbox, eq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040013277 return status;
13278}
13279
13280/**
13281 * lpfc_cq_destroy - Destroy a Completion Queue on the HBA
13282 * @cq: The queue structure associated with the queue to destroy.
13283 *
13284 * This function destroys a queue, as detailed in @cq by sending an mailbox
13285 * command, specific to the type of queue, to the HBA.
13286 *
13287 * The @cq struct is used to get the queue ID of the queue to destroy.
13288 *
13289 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040013290 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040013291 **/
13292uint32_t
13293lpfc_cq_destroy(struct lpfc_hba *phba, struct lpfc_queue *cq)
13294{
13295 LPFC_MBOXQ_t *mbox;
13296 int rc, length, status = 0;
13297 uint32_t shdr_status, shdr_add_status;
13298 union lpfc_sli4_cfg_shdr *shdr;
13299
James Smart2e90f4b2011-12-13 13:22:37 -050013300 /* sanity check on queue memory */
James Smart4f774512009-05-22 14:52:35 -040013301 if (!cq)
13302 return -ENODEV;
13303 mbox = mempool_alloc(cq->phba->mbox_mem_pool, GFP_KERNEL);
13304 if (!mbox)
13305 return -ENOMEM;
13306 length = (sizeof(struct lpfc_mbx_cq_destroy) -
13307 sizeof(struct lpfc_sli4_cfg_mhdr));
13308 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
13309 LPFC_MBOX_OPCODE_CQ_DESTROY,
13310 length, LPFC_SLI4_MBX_EMBED);
13311 bf_set(lpfc_mbx_cq_destroy_q_id, &mbox->u.mqe.un.cq_destroy.u.request,
13312 cq->queue_id);
13313 mbox->vport = cq->phba->pport;
13314 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
13315 rc = lpfc_sli_issue_mbox(cq->phba, mbox, MBX_POLL);
13316 /* The IOCTL status is embedded in the mailbox subheader. */
13317 shdr = (union lpfc_sli4_cfg_shdr *)
13318 &mbox->u.mqe.un.wq_create.header.cfg_shdr;
13319 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13320 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13321 if (shdr_status || shdr_add_status || rc) {
13322 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13323 "2506 CQ_DESTROY mailbox failed with "
13324 "status x%x add_status x%x, mbx status x%x\n",
13325 shdr_status, shdr_add_status, rc);
13326 status = -ENXIO;
13327 }
13328 /* Remove cq from any list */
13329 list_del_init(&cq->list);
James Smart8fa38512009-07-19 10:01:03 -040013330 mempool_free(mbox, cq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040013331 return status;
13332}
13333
13334/**
James Smart04c68492009-05-22 14:52:52 -040013335 * lpfc_mq_destroy - Destroy a Mailbox Queue on the HBA
13336 * @qm: The queue structure associated with the queue to destroy.
13337 *
13338 * This function destroys a queue, as detailed in @mq by sending an mailbox
13339 * command, specific to the type of queue, to the HBA.
13340 *
13341 * The @mq struct is used to get the queue ID of the queue to destroy.
13342 *
13343 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040013344 * command fails this function will return -ENXIO.
James Smart04c68492009-05-22 14:52:52 -040013345 **/
13346uint32_t
13347lpfc_mq_destroy(struct lpfc_hba *phba, struct lpfc_queue *mq)
13348{
13349 LPFC_MBOXQ_t *mbox;
13350 int rc, length, status = 0;
13351 uint32_t shdr_status, shdr_add_status;
13352 union lpfc_sli4_cfg_shdr *shdr;
13353
James Smart2e90f4b2011-12-13 13:22:37 -050013354 /* sanity check on queue memory */
James Smart04c68492009-05-22 14:52:52 -040013355 if (!mq)
13356 return -ENODEV;
13357 mbox = mempool_alloc(mq->phba->mbox_mem_pool, GFP_KERNEL);
13358 if (!mbox)
13359 return -ENOMEM;
13360 length = (sizeof(struct lpfc_mbx_mq_destroy) -
13361 sizeof(struct lpfc_sli4_cfg_mhdr));
13362 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
13363 LPFC_MBOX_OPCODE_MQ_DESTROY,
13364 length, LPFC_SLI4_MBX_EMBED);
13365 bf_set(lpfc_mbx_mq_destroy_q_id, &mbox->u.mqe.un.mq_destroy.u.request,
13366 mq->queue_id);
13367 mbox->vport = mq->phba->pport;
13368 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
13369 rc = lpfc_sli_issue_mbox(mq->phba, mbox, MBX_POLL);
13370 /* The IOCTL status is embedded in the mailbox subheader. */
13371 shdr = (union lpfc_sli4_cfg_shdr *)
13372 &mbox->u.mqe.un.mq_destroy.header.cfg_shdr;
13373 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13374 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13375 if (shdr_status || shdr_add_status || rc) {
13376 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13377 "2507 MQ_DESTROY mailbox failed with "
13378 "status x%x add_status x%x, mbx status x%x\n",
13379 shdr_status, shdr_add_status, rc);
13380 status = -ENXIO;
13381 }
13382 /* Remove mq from any list */
13383 list_del_init(&mq->list);
James Smart8fa38512009-07-19 10:01:03 -040013384 mempool_free(mbox, mq->phba->mbox_mem_pool);
James Smart04c68492009-05-22 14:52:52 -040013385 return status;
13386}
13387
13388/**
James Smart4f774512009-05-22 14:52:35 -040013389 * lpfc_wq_destroy - Destroy a Work Queue on the HBA
13390 * @wq: The queue structure associated with the queue to destroy.
13391 *
13392 * This function destroys a queue, as detailed in @wq by sending an mailbox
13393 * command, specific to the type of queue, to the HBA.
13394 *
13395 * The @wq struct is used to get the queue ID of the queue to destroy.
13396 *
13397 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040013398 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040013399 **/
13400uint32_t
13401lpfc_wq_destroy(struct lpfc_hba *phba, struct lpfc_queue *wq)
13402{
13403 LPFC_MBOXQ_t *mbox;
13404 int rc, length, status = 0;
13405 uint32_t shdr_status, shdr_add_status;
13406 union lpfc_sli4_cfg_shdr *shdr;
13407
James Smart2e90f4b2011-12-13 13:22:37 -050013408 /* sanity check on queue memory */
James Smart4f774512009-05-22 14:52:35 -040013409 if (!wq)
13410 return -ENODEV;
13411 mbox = mempool_alloc(wq->phba->mbox_mem_pool, GFP_KERNEL);
13412 if (!mbox)
13413 return -ENOMEM;
13414 length = (sizeof(struct lpfc_mbx_wq_destroy) -
13415 sizeof(struct lpfc_sli4_cfg_mhdr));
13416 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
13417 LPFC_MBOX_OPCODE_FCOE_WQ_DESTROY,
13418 length, LPFC_SLI4_MBX_EMBED);
13419 bf_set(lpfc_mbx_wq_destroy_q_id, &mbox->u.mqe.un.wq_destroy.u.request,
13420 wq->queue_id);
13421 mbox->vport = wq->phba->pport;
13422 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
13423 rc = lpfc_sli_issue_mbox(wq->phba, mbox, MBX_POLL);
13424 shdr = (union lpfc_sli4_cfg_shdr *)
13425 &mbox->u.mqe.un.wq_destroy.header.cfg_shdr;
13426 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13427 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13428 if (shdr_status || shdr_add_status || rc) {
13429 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13430 "2508 WQ_DESTROY mailbox failed with "
13431 "status x%x add_status x%x, mbx status x%x\n",
13432 shdr_status, shdr_add_status, rc);
13433 status = -ENXIO;
13434 }
13435 /* Remove wq from any list */
13436 list_del_init(&wq->list);
James Smart8fa38512009-07-19 10:01:03 -040013437 mempool_free(mbox, wq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040013438 return status;
13439}
13440
13441/**
13442 * lpfc_rq_destroy - Destroy a Receive Queue on the HBA
13443 * @rq: The queue structure associated with the queue to destroy.
13444 *
13445 * This function destroys a queue, as detailed in @rq by sending an mailbox
13446 * command, specific to the type of queue, to the HBA.
13447 *
13448 * The @rq struct is used to get the queue ID of the queue to destroy.
13449 *
13450 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040013451 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040013452 **/
13453uint32_t
13454lpfc_rq_destroy(struct lpfc_hba *phba, struct lpfc_queue *hrq,
13455 struct lpfc_queue *drq)
13456{
13457 LPFC_MBOXQ_t *mbox;
13458 int rc, length, status = 0;
13459 uint32_t shdr_status, shdr_add_status;
13460 union lpfc_sli4_cfg_shdr *shdr;
13461
James Smart2e90f4b2011-12-13 13:22:37 -050013462 /* sanity check on queue memory */
James Smart4f774512009-05-22 14:52:35 -040013463 if (!hrq || !drq)
13464 return -ENODEV;
13465 mbox = mempool_alloc(hrq->phba->mbox_mem_pool, GFP_KERNEL);
13466 if (!mbox)
13467 return -ENOMEM;
13468 length = (sizeof(struct lpfc_mbx_rq_destroy) -
James Smartfedd3b72011-02-16 12:39:24 -050013469 sizeof(struct lpfc_sli4_cfg_mhdr));
James Smart4f774512009-05-22 14:52:35 -040013470 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
13471 LPFC_MBOX_OPCODE_FCOE_RQ_DESTROY,
13472 length, LPFC_SLI4_MBX_EMBED);
13473 bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
13474 hrq->queue_id);
13475 mbox->vport = hrq->phba->pport;
13476 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
13477 rc = lpfc_sli_issue_mbox(hrq->phba, mbox, MBX_POLL);
13478 /* The IOCTL status is embedded in the mailbox subheader. */
13479 shdr = (union lpfc_sli4_cfg_shdr *)
13480 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
13481 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13482 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13483 if (shdr_status || shdr_add_status || rc) {
13484 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13485 "2509 RQ_DESTROY mailbox failed with "
13486 "status x%x add_status x%x, mbx status x%x\n",
13487 shdr_status, shdr_add_status, rc);
13488 if (rc != MBX_TIMEOUT)
13489 mempool_free(mbox, hrq->phba->mbox_mem_pool);
13490 return -ENXIO;
13491 }
13492 bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
13493 drq->queue_id);
13494 rc = lpfc_sli_issue_mbox(drq->phba, mbox, MBX_POLL);
13495 shdr = (union lpfc_sli4_cfg_shdr *)
13496 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
13497 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13498 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13499 if (shdr_status || shdr_add_status || rc) {
13500 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13501 "2510 RQ_DESTROY mailbox failed with "
13502 "status x%x add_status x%x, mbx status x%x\n",
13503 shdr_status, shdr_add_status, rc);
13504 status = -ENXIO;
13505 }
13506 list_del_init(&hrq->list);
13507 list_del_init(&drq->list);
James Smart8fa38512009-07-19 10:01:03 -040013508 mempool_free(mbox, hrq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040013509 return status;
13510}
13511
13512/**
13513 * lpfc_sli4_post_sgl - Post scatter gather list for an XRI to HBA
13514 * @phba: The virtual port for which this call being executed.
13515 * @pdma_phys_addr0: Physical address of the 1st SGL page.
13516 * @pdma_phys_addr1: Physical address of the 2nd SGL page.
13517 * @xritag: the xritag that ties this io to the SGL pages.
13518 *
13519 * This routine will post the sgl pages for the IO that has the xritag
13520 * that is in the iocbq structure. The xritag is assigned during iocbq
13521 * creation and persists for as long as the driver is loaded.
13522 * if the caller has fewer than 256 scatter gather segments to map then
13523 * pdma_phys_addr1 should be 0.
13524 * If the caller needs to map more than 256 scatter gather segment then
13525 * pdma_phys_addr1 should be a valid physical address.
13526 * physical address for SGLs must be 64 byte aligned.
13527 * If you are going to map 2 SGL's then the first one must have 256 entries
13528 * the second sgl can have between 1 and 256 entries.
13529 *
13530 * Return codes:
13531 * 0 - Success
13532 * -ENXIO, -ENOMEM - Failure
13533 **/
13534int
13535lpfc_sli4_post_sgl(struct lpfc_hba *phba,
13536 dma_addr_t pdma_phys_addr0,
13537 dma_addr_t pdma_phys_addr1,
13538 uint16_t xritag)
13539{
13540 struct lpfc_mbx_post_sgl_pages *post_sgl_pages;
13541 LPFC_MBOXQ_t *mbox;
13542 int rc;
13543 uint32_t shdr_status, shdr_add_status;
James Smart6d368e52011-05-24 11:44:12 -040013544 uint32_t mbox_tmo;
James Smart4f774512009-05-22 14:52:35 -040013545 union lpfc_sli4_cfg_shdr *shdr;
13546
13547 if (xritag == NO_XRI) {
13548 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13549 "0364 Invalid param:\n");
13550 return -EINVAL;
13551 }
13552
13553 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
13554 if (!mbox)
13555 return -ENOMEM;
13556
13557 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
13558 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES,
13559 sizeof(struct lpfc_mbx_post_sgl_pages) -
James Smartfedd3b72011-02-16 12:39:24 -050013560 sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
James Smart4f774512009-05-22 14:52:35 -040013561
13562 post_sgl_pages = (struct lpfc_mbx_post_sgl_pages *)
13563 &mbox->u.mqe.un.post_sgl_pages;
13564 bf_set(lpfc_post_sgl_pages_xri, post_sgl_pages, xritag);
13565 bf_set(lpfc_post_sgl_pages_xricnt, post_sgl_pages, 1);
13566
13567 post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_lo =
13568 cpu_to_le32(putPaddrLow(pdma_phys_addr0));
13569 post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_hi =
13570 cpu_to_le32(putPaddrHigh(pdma_phys_addr0));
13571
13572 post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_lo =
13573 cpu_to_le32(putPaddrLow(pdma_phys_addr1));
13574 post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_hi =
13575 cpu_to_le32(putPaddrHigh(pdma_phys_addr1));
13576 if (!phba->sli4_hba.intr_enable)
13577 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
James Smart6d368e52011-05-24 11:44:12 -040013578 else {
James Smarta183a152011-10-10 21:32:43 -040013579 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart6d368e52011-05-24 11:44:12 -040013580 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
13581 }
James Smart4f774512009-05-22 14:52:35 -040013582 /* The IOCTL status is embedded in the mailbox subheader. */
13583 shdr = (union lpfc_sli4_cfg_shdr *) &post_sgl_pages->header.cfg_shdr;
13584 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13585 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13586 if (rc != MBX_TIMEOUT)
13587 mempool_free(mbox, phba->mbox_mem_pool);
13588 if (shdr_status || shdr_add_status || rc) {
13589 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13590 "2511 POST_SGL mailbox failed with "
13591 "status x%x add_status x%x, mbx status x%x\n",
13592 shdr_status, shdr_add_status, rc);
13593 rc = -ENXIO;
13594 }
13595 return 0;
13596}
James Smart4f774512009-05-22 14:52:35 -040013597
13598/**
James Smart88a2cfb2011-07-22 18:36:33 -040013599 * lpfc_sli4_alloc_xri - Get an available rpi in the device's range
James Smart6d368e52011-05-24 11:44:12 -040013600 * @phba: pointer to lpfc hba data structure.
13601 *
13602 * This routine is invoked to post rpi header templates to the
James Smart88a2cfb2011-07-22 18:36:33 -040013603 * HBA consistent with the SLI-4 interface spec. This routine
13604 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
13605 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
James Smart6d368e52011-05-24 11:44:12 -040013606 *
James Smart88a2cfb2011-07-22 18:36:33 -040013607 * Returns
13608 * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
13609 * LPFC_RPI_ALLOC_ERROR if no rpis are available.
13610 **/
James Smart6d368e52011-05-24 11:44:12 -040013611uint16_t
13612lpfc_sli4_alloc_xri(struct lpfc_hba *phba)
13613{
13614 unsigned long xri;
13615
13616 /*
13617 * Fetch the next logical xri. Because this index is logical,
13618 * the driver starts at 0 each time.
13619 */
13620 spin_lock_irq(&phba->hbalock);
13621 xri = find_next_zero_bit(phba->sli4_hba.xri_bmask,
13622 phba->sli4_hba.max_cfg_param.max_xri, 0);
13623 if (xri >= phba->sli4_hba.max_cfg_param.max_xri) {
13624 spin_unlock_irq(&phba->hbalock);
13625 return NO_XRI;
13626 } else {
13627 set_bit(xri, phba->sli4_hba.xri_bmask);
13628 phba->sli4_hba.max_cfg_param.xri_used++;
James Smart6d368e52011-05-24 11:44:12 -040013629 }
James Smart6d368e52011-05-24 11:44:12 -040013630 spin_unlock_irq(&phba->hbalock);
13631 return xri;
13632}
13633
13634/**
13635 * lpfc_sli4_free_xri - Release an xri for reuse.
13636 * @phba: pointer to lpfc hba data structure.
13637 *
13638 * This routine is invoked to release an xri to the pool of
13639 * available rpis maintained by the driver.
13640 **/
13641void
13642__lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
13643{
13644 if (test_and_clear_bit(xri, phba->sli4_hba.xri_bmask)) {
James Smart6d368e52011-05-24 11:44:12 -040013645 phba->sli4_hba.max_cfg_param.xri_used--;
13646 }
13647}
13648
13649/**
13650 * lpfc_sli4_free_xri - Release an xri for reuse.
13651 * @phba: pointer to lpfc hba data structure.
13652 *
13653 * This routine is invoked to release an xri to the pool of
13654 * available rpis maintained by the driver.
13655 **/
13656void
13657lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
13658{
13659 spin_lock_irq(&phba->hbalock);
13660 __lpfc_sli4_free_xri(phba, xri);
13661 spin_unlock_irq(&phba->hbalock);
13662}
13663
13664/**
James Smart4f774512009-05-22 14:52:35 -040013665 * lpfc_sli4_next_xritag - Get an xritag for the io
13666 * @phba: Pointer to HBA context object.
13667 *
13668 * This function gets an xritag for the iocb. If there is no unused xritag
13669 * it will return 0xffff.
13670 * The function returns the allocated xritag if successful, else returns zero.
13671 * Zero is not a valid xritag.
13672 * The caller is not required to hold any lock.
13673 **/
13674uint16_t
13675lpfc_sli4_next_xritag(struct lpfc_hba *phba)
13676{
James Smart6d368e52011-05-24 11:44:12 -040013677 uint16_t xri_index;
James Smart4f774512009-05-22 14:52:35 -040013678
James Smart6d368e52011-05-24 11:44:12 -040013679 xri_index = lpfc_sli4_alloc_xri(phba);
James Smart81378052012-05-09 21:17:37 -040013680 if (xri_index == NO_XRI)
13681 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13682 "2004 Failed to allocate XRI.last XRITAG is %d"
13683 " Max XRI is %d, Used XRI is %d\n",
13684 xri_index,
13685 phba->sli4_hba.max_cfg_param.max_xri,
13686 phba->sli4_hba.max_cfg_param.xri_used);
13687 return xri_index;
James Smart4f774512009-05-22 14:52:35 -040013688}
13689
13690/**
James Smart6d368e52011-05-24 11:44:12 -040013691 * lpfc_sli4_post_els_sgl_list - post a block of ELS sgls to the port.
James Smart4f774512009-05-22 14:52:35 -040013692 * @phba: pointer to lpfc hba data structure.
James Smart8a9d2e82012-05-09 21:16:12 -040013693 * @post_sgl_list: pointer to els sgl entry list.
13694 * @count: number of els sgl entries on the list.
James Smart4f774512009-05-22 14:52:35 -040013695 *
13696 * This routine is invoked to post a block of driver's sgl pages to the
13697 * HBA using non-embedded mailbox command. No Lock is held. This routine
13698 * is only called when the driver is loading and after all IO has been
13699 * stopped.
13700 **/
James Smart8a9d2e82012-05-09 21:16:12 -040013701static int
13702lpfc_sli4_post_els_sgl_list(struct lpfc_hba *phba,
13703 struct list_head *post_sgl_list,
13704 int post_cnt)
James Smart4f774512009-05-22 14:52:35 -040013705{
James Smart8a9d2e82012-05-09 21:16:12 -040013706 struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
James Smart4f774512009-05-22 14:52:35 -040013707 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
13708 struct sgl_page_pairs *sgl_pg_pairs;
13709 void *viraddr;
13710 LPFC_MBOXQ_t *mbox;
13711 uint32_t reqlen, alloclen, pg_pairs;
13712 uint32_t mbox_tmo;
James Smart8a9d2e82012-05-09 21:16:12 -040013713 uint16_t xritag_start = 0;
13714 int rc = 0;
James Smart4f774512009-05-22 14:52:35 -040013715 uint32_t shdr_status, shdr_add_status;
13716 union lpfc_sli4_cfg_shdr *shdr;
13717
James Smart8a9d2e82012-05-09 21:16:12 -040013718 reqlen = phba->sli4_hba.els_xri_cnt * sizeof(struct sgl_page_pairs) +
James Smart4f774512009-05-22 14:52:35 -040013719 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
James Smart49198b32010-04-06 15:04:33 -040013720 if (reqlen > SLI4_PAGE_SIZE) {
James Smart4f774512009-05-22 14:52:35 -040013721 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
13722 "2559 Block sgl registration required DMA "
13723 "size (%d) great than a page\n", reqlen);
13724 return -ENOMEM;
13725 }
13726 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
James Smart6d368e52011-05-24 11:44:12 -040013727 if (!mbox)
James Smart4f774512009-05-22 14:52:35 -040013728 return -ENOMEM;
James Smart4f774512009-05-22 14:52:35 -040013729
13730 /* Allocate DMA memory and set up the non-embedded mailbox command */
13731 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
13732 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
13733 LPFC_SLI4_MBX_NEMBED);
13734
13735 if (alloclen < reqlen) {
13736 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13737 "0285 Allocated DMA memory size (%d) is "
13738 "less than the requested DMA memory "
13739 "size (%d)\n", alloclen, reqlen);
13740 lpfc_sli4_mbox_cmd_free(phba, mbox);
13741 return -ENOMEM;
13742 }
James Smart4f774512009-05-22 14:52:35 -040013743 /* Set up the SGL pages in the non-embedded DMA pages */
James Smart6d368e52011-05-24 11:44:12 -040013744 viraddr = mbox->sge_array->addr[0];
James Smart4f774512009-05-22 14:52:35 -040013745 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
13746 sgl_pg_pairs = &sgl->sgl_pg_pairs;
13747
James Smart8a9d2e82012-05-09 21:16:12 -040013748 pg_pairs = 0;
13749 list_for_each_entry_safe(sglq_entry, sglq_next, post_sgl_list, list) {
James Smart4f774512009-05-22 14:52:35 -040013750 /* Set up the sge entry */
13751 sgl_pg_pairs->sgl_pg0_addr_lo =
13752 cpu_to_le32(putPaddrLow(sglq_entry->phys));
13753 sgl_pg_pairs->sgl_pg0_addr_hi =
13754 cpu_to_le32(putPaddrHigh(sglq_entry->phys));
13755 sgl_pg_pairs->sgl_pg1_addr_lo =
13756 cpu_to_le32(putPaddrLow(0));
13757 sgl_pg_pairs->sgl_pg1_addr_hi =
13758 cpu_to_le32(putPaddrHigh(0));
James Smart6d368e52011-05-24 11:44:12 -040013759
James Smart4f774512009-05-22 14:52:35 -040013760 /* Keep the first xritag on the list */
13761 if (pg_pairs == 0)
13762 xritag_start = sglq_entry->sli4_xritag;
13763 sgl_pg_pairs++;
James Smart8a9d2e82012-05-09 21:16:12 -040013764 pg_pairs++;
James Smart4f774512009-05-22 14:52:35 -040013765 }
James Smart6d368e52011-05-24 11:44:12 -040013766
13767 /* Complete initialization and perform endian conversion. */
James Smart4f774512009-05-22 14:52:35 -040013768 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
James Smart8a9d2e82012-05-09 21:16:12 -040013769 bf_set(lpfc_post_sgl_pages_xricnt, sgl, phba->sli4_hba.els_xri_cnt);
James Smart4f774512009-05-22 14:52:35 -040013770 sgl->word0 = cpu_to_le32(sgl->word0);
James Smart4f774512009-05-22 14:52:35 -040013771 if (!phba->sli4_hba.intr_enable)
13772 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
13773 else {
James Smarta183a152011-10-10 21:32:43 -040013774 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart4f774512009-05-22 14:52:35 -040013775 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
13776 }
13777 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
13778 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13779 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13780 if (rc != MBX_TIMEOUT)
13781 lpfc_sli4_mbox_cmd_free(phba, mbox);
13782 if (shdr_status || shdr_add_status || rc) {
13783 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13784 "2513 POST_SGL_BLOCK mailbox command failed "
13785 "status x%x add_status x%x mbx status x%x\n",
13786 shdr_status, shdr_add_status, rc);
13787 rc = -ENXIO;
13788 }
13789 return rc;
13790}
13791
13792/**
13793 * lpfc_sli4_post_scsi_sgl_block - post a block of scsi sgl list to firmware
13794 * @phba: pointer to lpfc hba data structure.
13795 * @sblist: pointer to scsi buffer list.
13796 * @count: number of scsi buffers on the list.
13797 *
13798 * This routine is invoked to post a block of @count scsi sgl pages from a
13799 * SCSI buffer list @sblist to the HBA using non-embedded mailbox command.
13800 * No Lock is held.
13801 *
13802 **/
13803int
James Smart8a9d2e82012-05-09 21:16:12 -040013804lpfc_sli4_post_scsi_sgl_block(struct lpfc_hba *phba,
13805 struct list_head *sblist,
13806 int count)
James Smart4f774512009-05-22 14:52:35 -040013807{
13808 struct lpfc_scsi_buf *psb;
13809 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
13810 struct sgl_page_pairs *sgl_pg_pairs;
13811 void *viraddr;
13812 LPFC_MBOXQ_t *mbox;
13813 uint32_t reqlen, alloclen, pg_pairs;
13814 uint32_t mbox_tmo;
13815 uint16_t xritag_start = 0;
13816 int rc = 0;
13817 uint32_t shdr_status, shdr_add_status;
13818 dma_addr_t pdma_phys_bpl1;
13819 union lpfc_sli4_cfg_shdr *shdr;
13820
13821 /* Calculate the requested length of the dma memory */
James Smart8a9d2e82012-05-09 21:16:12 -040013822 reqlen = count * sizeof(struct sgl_page_pairs) +
James Smart4f774512009-05-22 14:52:35 -040013823 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
James Smart49198b32010-04-06 15:04:33 -040013824 if (reqlen > SLI4_PAGE_SIZE) {
James Smart4f774512009-05-22 14:52:35 -040013825 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
13826 "0217 Block sgl registration required DMA "
13827 "size (%d) great than a page\n", reqlen);
13828 return -ENOMEM;
13829 }
13830 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
13831 if (!mbox) {
13832 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13833 "0283 Failed to allocate mbox cmd memory\n");
13834 return -ENOMEM;
13835 }
13836
13837 /* Allocate DMA memory and set up the non-embedded mailbox command */
13838 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
13839 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
13840 LPFC_SLI4_MBX_NEMBED);
13841
13842 if (alloclen < reqlen) {
13843 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13844 "2561 Allocated DMA memory size (%d) is "
13845 "less than the requested DMA memory "
13846 "size (%d)\n", alloclen, reqlen);
13847 lpfc_sli4_mbox_cmd_free(phba, mbox);
13848 return -ENOMEM;
13849 }
James Smart6d368e52011-05-24 11:44:12 -040013850
James Smart4f774512009-05-22 14:52:35 -040013851 /* Get the first SGE entry from the non-embedded DMA memory */
James Smart4f774512009-05-22 14:52:35 -040013852 viraddr = mbox->sge_array->addr[0];
13853
13854 /* Set up the SGL pages in the non-embedded DMA pages */
13855 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
13856 sgl_pg_pairs = &sgl->sgl_pg_pairs;
13857
13858 pg_pairs = 0;
13859 list_for_each_entry(psb, sblist, list) {
13860 /* Set up the sge entry */
13861 sgl_pg_pairs->sgl_pg0_addr_lo =
13862 cpu_to_le32(putPaddrLow(psb->dma_phys_bpl));
13863 sgl_pg_pairs->sgl_pg0_addr_hi =
13864 cpu_to_le32(putPaddrHigh(psb->dma_phys_bpl));
13865 if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
13866 pdma_phys_bpl1 = psb->dma_phys_bpl + SGL_PAGE_SIZE;
13867 else
13868 pdma_phys_bpl1 = 0;
13869 sgl_pg_pairs->sgl_pg1_addr_lo =
13870 cpu_to_le32(putPaddrLow(pdma_phys_bpl1));
13871 sgl_pg_pairs->sgl_pg1_addr_hi =
13872 cpu_to_le32(putPaddrHigh(pdma_phys_bpl1));
13873 /* Keep the first xritag on the list */
13874 if (pg_pairs == 0)
13875 xritag_start = psb->cur_iocbq.sli4_xritag;
13876 sgl_pg_pairs++;
13877 pg_pairs++;
13878 }
13879 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
13880 bf_set(lpfc_post_sgl_pages_xricnt, sgl, pg_pairs);
13881 /* Perform endian conversion if necessary */
13882 sgl->word0 = cpu_to_le32(sgl->word0);
13883
13884 if (!phba->sli4_hba.intr_enable)
13885 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
13886 else {
James Smarta183a152011-10-10 21:32:43 -040013887 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart4f774512009-05-22 14:52:35 -040013888 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
13889 }
13890 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
13891 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13892 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13893 if (rc != MBX_TIMEOUT)
13894 lpfc_sli4_mbox_cmd_free(phba, mbox);
13895 if (shdr_status || shdr_add_status || rc) {
13896 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13897 "2564 POST_SGL_BLOCK mailbox command failed "
13898 "status x%x add_status x%x mbx status x%x\n",
13899 shdr_status, shdr_add_status, rc);
13900 rc = -ENXIO;
13901 }
13902 return rc;
13903}
13904
13905/**
13906 * lpfc_fc_frame_check - Check that this frame is a valid frame to handle
13907 * @phba: pointer to lpfc_hba struct that the frame was received on
13908 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
13909 *
13910 * This function checks the fields in the @fc_hdr to see if the FC frame is a
13911 * valid type of frame that the LPFC driver will handle. This function will
13912 * return a zero if the frame is a valid frame or a non zero value when the
13913 * frame does not pass the check.
13914 **/
13915static int
13916lpfc_fc_frame_check(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr)
13917{
Tomas Henzl474ffb72010-12-22 16:52:40 +010013918 /* make rctl_names static to save stack space */
13919 static char *rctl_names[] = FC_RCTL_NAMES_INIT;
James Smart4f774512009-05-22 14:52:35 -040013920 char *type_names[] = FC_TYPE_NAMES_INIT;
13921 struct fc_vft_header *fc_vft_hdr;
James Smart546fc852011-03-11 16:06:29 -050013922 uint32_t *header = (uint32_t *) fc_hdr;
James Smart4f774512009-05-22 14:52:35 -040013923
13924 switch (fc_hdr->fh_r_ctl) {
13925 case FC_RCTL_DD_UNCAT: /* uncategorized information */
13926 case FC_RCTL_DD_SOL_DATA: /* solicited data */
13927 case FC_RCTL_DD_UNSOL_CTL: /* unsolicited control */
13928 case FC_RCTL_DD_SOL_CTL: /* solicited control or reply */
13929 case FC_RCTL_DD_UNSOL_DATA: /* unsolicited data */
13930 case FC_RCTL_DD_DATA_DESC: /* data descriptor */
13931 case FC_RCTL_DD_UNSOL_CMD: /* unsolicited command */
13932 case FC_RCTL_DD_CMD_STATUS: /* command status */
13933 case FC_RCTL_ELS_REQ: /* extended link services request */
13934 case FC_RCTL_ELS_REP: /* extended link services reply */
13935 case FC_RCTL_ELS4_REQ: /* FC-4 ELS request */
13936 case FC_RCTL_ELS4_REP: /* FC-4 ELS reply */
13937 case FC_RCTL_BA_NOP: /* basic link service NOP */
13938 case FC_RCTL_BA_ABTS: /* basic link service abort */
13939 case FC_RCTL_BA_RMC: /* remove connection */
13940 case FC_RCTL_BA_ACC: /* basic accept */
13941 case FC_RCTL_BA_RJT: /* basic reject */
13942 case FC_RCTL_BA_PRMT:
13943 case FC_RCTL_ACK_1: /* acknowledge_1 */
13944 case FC_RCTL_ACK_0: /* acknowledge_0 */
13945 case FC_RCTL_P_RJT: /* port reject */
13946 case FC_RCTL_F_RJT: /* fabric reject */
13947 case FC_RCTL_P_BSY: /* port busy */
13948 case FC_RCTL_F_BSY: /* fabric busy to data frame */
13949 case FC_RCTL_F_BSYL: /* fabric busy to link control frame */
13950 case FC_RCTL_LCR: /* link credit reset */
13951 case FC_RCTL_END: /* end */
13952 break;
13953 case FC_RCTL_VFTH: /* Virtual Fabric tagging Header */
13954 fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
13955 fc_hdr = &((struct fc_frame_header *)fc_vft_hdr)[1];
13956 return lpfc_fc_frame_check(phba, fc_hdr);
13957 default:
13958 goto drop;
13959 }
13960 switch (fc_hdr->fh_type) {
13961 case FC_TYPE_BLS:
13962 case FC_TYPE_ELS:
13963 case FC_TYPE_FCP:
13964 case FC_TYPE_CT:
13965 break;
13966 case FC_TYPE_IP:
13967 case FC_TYPE_ILS:
13968 default:
13969 goto drop;
13970 }
James Smart546fc852011-03-11 16:06:29 -050013971
James Smart4f774512009-05-22 14:52:35 -040013972 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
James Smart546fc852011-03-11 16:06:29 -050013973 "2538 Received frame rctl:%s type:%s "
13974 "Frame Data:%08x %08x %08x %08x %08x %08x\n",
James Smart4f774512009-05-22 14:52:35 -040013975 rctl_names[fc_hdr->fh_r_ctl],
James Smart546fc852011-03-11 16:06:29 -050013976 type_names[fc_hdr->fh_type],
13977 be32_to_cpu(header[0]), be32_to_cpu(header[1]),
13978 be32_to_cpu(header[2]), be32_to_cpu(header[3]),
13979 be32_to_cpu(header[4]), be32_to_cpu(header[5]));
James Smart4f774512009-05-22 14:52:35 -040013980 return 0;
13981drop:
13982 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
13983 "2539 Dropped frame rctl:%s type:%s\n",
13984 rctl_names[fc_hdr->fh_r_ctl],
13985 type_names[fc_hdr->fh_type]);
13986 return 1;
13987}
13988
13989/**
13990 * lpfc_fc_hdr_get_vfi - Get the VFI from an FC frame
13991 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
13992 *
13993 * This function processes the FC header to retrieve the VFI from the VF
13994 * header, if one exists. This function will return the VFI if one exists
13995 * or 0 if no VSAN Header exists.
13996 **/
13997static uint32_t
13998lpfc_fc_hdr_get_vfi(struct fc_frame_header *fc_hdr)
13999{
14000 struct fc_vft_header *fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
14001
14002 if (fc_hdr->fh_r_ctl != FC_RCTL_VFTH)
14003 return 0;
14004 return bf_get(fc_vft_hdr_vf_id, fc_vft_hdr);
14005}
14006
14007/**
14008 * lpfc_fc_frame_to_vport - Finds the vport that a frame is destined to
14009 * @phba: Pointer to the HBA structure to search for the vport on
14010 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
14011 * @fcfi: The FC Fabric ID that the frame came from
14012 *
14013 * This function searches the @phba for a vport that matches the content of the
14014 * @fc_hdr passed in and the @fcfi. This function uses the @fc_hdr to fetch the
14015 * VFI, if the Virtual Fabric Tagging Header exists, and the DID. This function
14016 * returns the matching vport pointer or NULL if unable to match frame to a
14017 * vport.
14018 **/
14019static struct lpfc_vport *
14020lpfc_fc_frame_to_vport(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr,
14021 uint16_t fcfi)
14022{
14023 struct lpfc_vport **vports;
14024 struct lpfc_vport *vport = NULL;
14025 int i;
14026 uint32_t did = (fc_hdr->fh_d_id[0] << 16 |
14027 fc_hdr->fh_d_id[1] << 8 |
14028 fc_hdr->fh_d_id[2]);
James Smart939723a2012-05-09 21:19:03 -040014029
James Smartbf086112011-08-21 21:48:13 -040014030 if (did == Fabric_DID)
14031 return phba->pport;
James Smart939723a2012-05-09 21:19:03 -040014032 if ((phba->pport->fc_flag & FC_PT2PT) &&
14033 !(phba->link_state == LPFC_HBA_READY))
14034 return phba->pport;
14035
James Smart4f774512009-05-22 14:52:35 -040014036 vports = lpfc_create_vport_work_array(phba);
14037 if (vports != NULL)
14038 for (i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
14039 if (phba->fcf.fcfi == fcfi &&
14040 vports[i]->vfi == lpfc_fc_hdr_get_vfi(fc_hdr) &&
14041 vports[i]->fc_myDID == did) {
14042 vport = vports[i];
14043 break;
14044 }
14045 }
14046 lpfc_destroy_vport_work_array(phba, vports);
14047 return vport;
14048}
14049
14050/**
James Smart45ed1192009-10-02 15:17:02 -040014051 * lpfc_update_rcv_time_stamp - Update vport's rcv seq time stamp
14052 * @vport: The vport to work on.
14053 *
14054 * This function updates the receive sequence time stamp for this vport. The
14055 * receive sequence time stamp indicates the time that the last frame of the
14056 * the sequence that has been idle for the longest amount of time was received.
14057 * the driver uses this time stamp to indicate if any received sequences have
14058 * timed out.
14059 **/
14060void
14061lpfc_update_rcv_time_stamp(struct lpfc_vport *vport)
14062{
14063 struct lpfc_dmabuf *h_buf;
14064 struct hbq_dmabuf *dmabuf = NULL;
14065
14066 /* get the oldest sequence on the rcv list */
14067 h_buf = list_get_first(&vport->rcv_buffer_list,
14068 struct lpfc_dmabuf, list);
14069 if (!h_buf)
14070 return;
14071 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
14072 vport->rcv_buffer_time_stamp = dmabuf->time_stamp;
14073}
14074
14075/**
14076 * lpfc_cleanup_rcv_buffers - Cleans up all outstanding receive sequences.
14077 * @vport: The vport that the received sequences were sent to.
14078 *
14079 * This function cleans up all outstanding received sequences. This is called
14080 * by the driver when a link event or user action invalidates all the received
14081 * sequences.
14082 **/
14083void
14084lpfc_cleanup_rcv_buffers(struct lpfc_vport *vport)
14085{
14086 struct lpfc_dmabuf *h_buf, *hnext;
14087 struct lpfc_dmabuf *d_buf, *dnext;
14088 struct hbq_dmabuf *dmabuf = NULL;
14089
14090 /* start with the oldest sequence on the rcv list */
14091 list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
14092 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
14093 list_del_init(&dmabuf->hbuf.list);
14094 list_for_each_entry_safe(d_buf, dnext,
14095 &dmabuf->dbuf.list, list) {
14096 list_del_init(&d_buf->list);
14097 lpfc_in_buf_free(vport->phba, d_buf);
14098 }
14099 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
14100 }
14101}
14102
14103/**
14104 * lpfc_rcv_seq_check_edtov - Cleans up timed out receive sequences.
14105 * @vport: The vport that the received sequences were sent to.
14106 *
14107 * This function determines whether any received sequences have timed out by
14108 * first checking the vport's rcv_buffer_time_stamp. If this time_stamp
14109 * indicates that there is at least one timed out sequence this routine will
14110 * go through the received sequences one at a time from most inactive to most
14111 * active to determine which ones need to be cleaned up. Once it has determined
14112 * that a sequence needs to be cleaned up it will simply free up the resources
14113 * without sending an abort.
14114 **/
14115void
14116lpfc_rcv_seq_check_edtov(struct lpfc_vport *vport)
14117{
14118 struct lpfc_dmabuf *h_buf, *hnext;
14119 struct lpfc_dmabuf *d_buf, *dnext;
14120 struct hbq_dmabuf *dmabuf = NULL;
14121 unsigned long timeout;
14122 int abort_count = 0;
14123
14124 timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
14125 vport->rcv_buffer_time_stamp);
14126 if (list_empty(&vport->rcv_buffer_list) ||
14127 time_before(jiffies, timeout))
14128 return;
14129 /* start with the oldest sequence on the rcv list */
14130 list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
14131 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
14132 timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
14133 dmabuf->time_stamp);
14134 if (time_before(jiffies, timeout))
14135 break;
14136 abort_count++;
14137 list_del_init(&dmabuf->hbuf.list);
14138 list_for_each_entry_safe(d_buf, dnext,
14139 &dmabuf->dbuf.list, list) {
14140 list_del_init(&d_buf->list);
14141 lpfc_in_buf_free(vport->phba, d_buf);
14142 }
14143 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
14144 }
14145 if (abort_count)
14146 lpfc_update_rcv_time_stamp(vport);
14147}
14148
14149/**
James Smart4f774512009-05-22 14:52:35 -040014150 * lpfc_fc_frame_add - Adds a frame to the vport's list of received sequences
14151 * @dmabuf: pointer to a dmabuf that describes the hdr and data of the FC frame
14152 *
14153 * This function searches through the existing incomplete sequences that have
14154 * been sent to this @vport. If the frame matches one of the incomplete
14155 * sequences then the dbuf in the @dmabuf is added to the list of frames that
14156 * make up that sequence. If no sequence is found that matches this frame then
14157 * the function will add the hbuf in the @dmabuf to the @vport's rcv_buffer_list
14158 * This function returns a pointer to the first dmabuf in the sequence list that
14159 * the frame was linked to.
14160 **/
14161static struct hbq_dmabuf *
14162lpfc_fc_frame_add(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
14163{
14164 struct fc_frame_header *new_hdr;
14165 struct fc_frame_header *temp_hdr;
14166 struct lpfc_dmabuf *d_buf;
14167 struct lpfc_dmabuf *h_buf;
14168 struct hbq_dmabuf *seq_dmabuf = NULL;
14169 struct hbq_dmabuf *temp_dmabuf = NULL;
14170
James Smart4d9ab992009-10-02 15:16:39 -040014171 INIT_LIST_HEAD(&dmabuf->dbuf.list);
James Smart45ed1192009-10-02 15:17:02 -040014172 dmabuf->time_stamp = jiffies;
James Smart4f774512009-05-22 14:52:35 -040014173 new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
14174 /* Use the hdr_buf to find the sequence that this frame belongs to */
14175 list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
14176 temp_hdr = (struct fc_frame_header *)h_buf->virt;
14177 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
14178 (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
14179 (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
14180 continue;
14181 /* found a pending sequence that matches this frame */
14182 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
14183 break;
14184 }
14185 if (!seq_dmabuf) {
14186 /*
14187 * This indicates first frame received for this sequence.
14188 * Queue the buffer on the vport's rcv_buffer_list.
14189 */
14190 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
James Smart45ed1192009-10-02 15:17:02 -040014191 lpfc_update_rcv_time_stamp(vport);
James Smart4f774512009-05-22 14:52:35 -040014192 return dmabuf;
14193 }
14194 temp_hdr = seq_dmabuf->hbuf.virt;
James Smarteeead812009-12-21 17:01:23 -050014195 if (be16_to_cpu(new_hdr->fh_seq_cnt) <
14196 be16_to_cpu(temp_hdr->fh_seq_cnt)) {
James Smart4d9ab992009-10-02 15:16:39 -040014197 list_del_init(&seq_dmabuf->hbuf.list);
14198 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
14199 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
James Smart45ed1192009-10-02 15:17:02 -040014200 lpfc_update_rcv_time_stamp(vport);
James Smart4f774512009-05-22 14:52:35 -040014201 return dmabuf;
14202 }
James Smart45ed1192009-10-02 15:17:02 -040014203 /* move this sequence to the tail to indicate a young sequence */
14204 list_move_tail(&seq_dmabuf->hbuf.list, &vport->rcv_buffer_list);
14205 seq_dmabuf->time_stamp = jiffies;
14206 lpfc_update_rcv_time_stamp(vport);
James Smarteeead812009-12-21 17:01:23 -050014207 if (list_empty(&seq_dmabuf->dbuf.list)) {
14208 temp_hdr = dmabuf->hbuf.virt;
14209 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
14210 return seq_dmabuf;
14211 }
James Smart4f774512009-05-22 14:52:35 -040014212 /* find the correct place in the sequence to insert this frame */
14213 list_for_each_entry_reverse(d_buf, &seq_dmabuf->dbuf.list, list) {
14214 temp_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
14215 temp_hdr = (struct fc_frame_header *)temp_dmabuf->hbuf.virt;
14216 /*
14217 * If the frame's sequence count is greater than the frame on
14218 * the list then insert the frame right after this frame
14219 */
James Smarteeead812009-12-21 17:01:23 -050014220 if (be16_to_cpu(new_hdr->fh_seq_cnt) >
14221 be16_to_cpu(temp_hdr->fh_seq_cnt)) {
James Smart4f774512009-05-22 14:52:35 -040014222 list_add(&dmabuf->dbuf.list, &temp_dmabuf->dbuf.list);
14223 return seq_dmabuf;
14224 }
14225 }
14226 return NULL;
14227}
14228
14229/**
James Smart6669f9b2009-10-02 15:16:45 -040014230 * lpfc_sli4_abort_partial_seq - Abort partially assembled unsol sequence
14231 * @vport: pointer to a vitural port
14232 * @dmabuf: pointer to a dmabuf that describes the FC sequence
14233 *
14234 * This function tries to abort from the partially assembed sequence, described
14235 * by the information from basic abbort @dmabuf. It checks to see whether such
14236 * partially assembled sequence held by the driver. If so, it shall free up all
14237 * the frames from the partially assembled sequence.
14238 *
14239 * Return
14240 * true -- if there is matching partially assembled sequence present and all
14241 * the frames freed with the sequence;
14242 * false -- if there is no matching partially assembled sequence present so
14243 * nothing got aborted in the lower layer driver
14244 **/
14245static bool
14246lpfc_sli4_abort_partial_seq(struct lpfc_vport *vport,
14247 struct hbq_dmabuf *dmabuf)
14248{
14249 struct fc_frame_header *new_hdr;
14250 struct fc_frame_header *temp_hdr;
14251 struct lpfc_dmabuf *d_buf, *n_buf, *h_buf;
14252 struct hbq_dmabuf *seq_dmabuf = NULL;
14253
14254 /* Use the hdr_buf to find the sequence that matches this frame */
14255 INIT_LIST_HEAD(&dmabuf->dbuf.list);
14256 INIT_LIST_HEAD(&dmabuf->hbuf.list);
14257 new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
14258 list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
14259 temp_hdr = (struct fc_frame_header *)h_buf->virt;
14260 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
14261 (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
14262 (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
14263 continue;
14264 /* found a pending sequence that matches this frame */
14265 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
14266 break;
14267 }
14268
14269 /* Free up all the frames from the partially assembled sequence */
14270 if (seq_dmabuf) {
14271 list_for_each_entry_safe(d_buf, n_buf,
14272 &seq_dmabuf->dbuf.list, list) {
14273 list_del_init(&d_buf->list);
14274 lpfc_in_buf_free(vport->phba, d_buf);
14275 }
14276 return true;
14277 }
14278 return false;
14279}
14280
14281/**
James Smart6dd9e312013-01-03 15:43:37 -050014282 * lpfc_sli4_abort_ulp_seq - Abort assembled unsol sequence from ulp
14283 * @vport: pointer to a vitural port
14284 * @dmabuf: pointer to a dmabuf that describes the FC sequence
14285 *
14286 * This function tries to abort from the assembed sequence from upper level
14287 * protocol, described by the information from basic abbort @dmabuf. It
14288 * checks to see whether such pending context exists at upper level protocol.
14289 * If so, it shall clean up the pending context.
14290 *
14291 * Return
14292 * true -- if there is matching pending context of the sequence cleaned
14293 * at ulp;
14294 * false -- if there is no matching pending context of the sequence present
14295 * at ulp.
14296 **/
14297static bool
14298lpfc_sli4_abort_ulp_seq(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
14299{
14300 struct lpfc_hba *phba = vport->phba;
14301 int handled;
14302
14303 /* Accepting abort at ulp with SLI4 only */
14304 if (phba->sli_rev < LPFC_SLI_REV4)
14305 return false;
14306
14307 /* Register all caring upper level protocols to attend abort */
14308 handled = lpfc_ct_handle_unsol_abort(phba, dmabuf);
14309 if (handled)
14310 return true;
14311
14312 return false;
14313}
14314
14315/**
James Smart546fc852011-03-11 16:06:29 -050014316 * lpfc_sli4_seq_abort_rsp_cmpl - BLS ABORT RSP seq abort iocb complete handler
James Smart6669f9b2009-10-02 15:16:45 -040014317 * @phba: Pointer to HBA context object.
14318 * @cmd_iocbq: pointer to the command iocbq structure.
14319 * @rsp_iocbq: pointer to the response iocbq structure.
14320 *
James Smart546fc852011-03-11 16:06:29 -050014321 * This function handles the sequence abort response iocb command complete
James Smart6669f9b2009-10-02 15:16:45 -040014322 * event. It properly releases the memory allocated to the sequence abort
14323 * accept iocb.
14324 **/
14325static void
James Smart546fc852011-03-11 16:06:29 -050014326lpfc_sli4_seq_abort_rsp_cmpl(struct lpfc_hba *phba,
James Smart6669f9b2009-10-02 15:16:45 -040014327 struct lpfc_iocbq *cmd_iocbq,
14328 struct lpfc_iocbq *rsp_iocbq)
14329{
James Smart6dd9e312013-01-03 15:43:37 -050014330 struct lpfc_nodelist *ndlp;
14331
14332 if (cmd_iocbq) {
14333 ndlp = (struct lpfc_nodelist *)cmd_iocbq->context1;
14334 lpfc_nlp_put(ndlp);
14335 lpfc_nlp_not_used(ndlp);
James Smart6669f9b2009-10-02 15:16:45 -040014336 lpfc_sli_release_iocbq(phba, cmd_iocbq);
James Smart6dd9e312013-01-03 15:43:37 -050014337 }
James Smart6b5151f2012-01-18 16:24:06 -050014338
14339 /* Failure means BLS ABORT RSP did not get delivered to remote node*/
14340 if (rsp_iocbq && rsp_iocbq->iocb.ulpStatus)
14341 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14342 "3154 BLS ABORT RSP failed, data: x%x/x%x\n",
14343 rsp_iocbq->iocb.ulpStatus,
14344 rsp_iocbq->iocb.un.ulpWord[4]);
James Smart6669f9b2009-10-02 15:16:45 -040014345}
14346
14347/**
James Smart6d368e52011-05-24 11:44:12 -040014348 * lpfc_sli4_xri_inrange - check xri is in range of xris owned by driver.
14349 * @phba: Pointer to HBA context object.
14350 * @xri: xri id in transaction.
14351 *
14352 * This function validates the xri maps to the known range of XRIs allocated an
14353 * used by the driver.
14354 **/
James Smart7851fe22011-07-22 18:36:52 -040014355uint16_t
James Smart6d368e52011-05-24 11:44:12 -040014356lpfc_sli4_xri_inrange(struct lpfc_hba *phba,
14357 uint16_t xri)
14358{
14359 int i;
14360
14361 for (i = 0; i < phba->sli4_hba.max_cfg_param.max_xri; i++) {
14362 if (xri == phba->sli4_hba.xri_ids[i])
14363 return i;
14364 }
14365 return NO_XRI;
14366}
14367
James Smart6d368e52011-05-24 11:44:12 -040014368/**
James Smart546fc852011-03-11 16:06:29 -050014369 * lpfc_sli4_seq_abort_rsp - bls rsp to sequence abort
James Smart6669f9b2009-10-02 15:16:45 -040014370 * @phba: Pointer to HBA context object.
14371 * @fc_hdr: pointer to a FC frame header.
14372 *
James Smart546fc852011-03-11 16:06:29 -050014373 * This function sends a basic response to a previous unsol sequence abort
James Smart6669f9b2009-10-02 15:16:45 -040014374 * event after aborting the sequence handling.
14375 **/
14376static void
James Smart6dd9e312013-01-03 15:43:37 -050014377lpfc_sli4_seq_abort_rsp(struct lpfc_vport *vport,
14378 struct fc_frame_header *fc_hdr, bool aborted)
James Smart6669f9b2009-10-02 15:16:45 -040014379{
James Smart6dd9e312013-01-03 15:43:37 -050014380 struct lpfc_hba *phba = vport->phba;
James Smart6669f9b2009-10-02 15:16:45 -040014381 struct lpfc_iocbq *ctiocb = NULL;
14382 struct lpfc_nodelist *ndlp;
James Smartee0f4fe2012-05-09 21:19:14 -040014383 uint16_t oxid, rxid, xri, lxri;
James Smart5ffc2662009-11-18 15:39:44 -050014384 uint32_t sid, fctl;
James Smart6669f9b2009-10-02 15:16:45 -040014385 IOCB_t *icmd;
James Smart546fc852011-03-11 16:06:29 -050014386 int rc;
James Smart6669f9b2009-10-02 15:16:45 -040014387
14388 if (!lpfc_is_link_up(phba))
14389 return;
14390
14391 sid = sli4_sid_from_fc_hdr(fc_hdr);
14392 oxid = be16_to_cpu(fc_hdr->fh_ox_id);
James Smart5ffc2662009-11-18 15:39:44 -050014393 rxid = be16_to_cpu(fc_hdr->fh_rx_id);
James Smart6669f9b2009-10-02 15:16:45 -040014394
James Smart6dd9e312013-01-03 15:43:37 -050014395 ndlp = lpfc_findnode_did(vport, sid);
James Smart6669f9b2009-10-02 15:16:45 -040014396 if (!ndlp) {
James Smart6dd9e312013-01-03 15:43:37 -050014397 ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL);
14398 if (!ndlp) {
14399 lpfc_printf_vlog(vport, KERN_WARNING, LOG_ELS,
14400 "1268 Failed to allocate ndlp for "
14401 "oxid:x%x SID:x%x\n", oxid, sid);
14402 return;
14403 }
14404 lpfc_nlp_init(vport, ndlp, sid);
14405 /* Put ndlp onto pport node list */
14406 lpfc_enqueue_node(vport, ndlp);
14407 } else if (!NLP_CHK_NODE_ACT(ndlp)) {
14408 /* re-setup ndlp without removing from node list */
14409 ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_UNUSED_NODE);
14410 if (!ndlp) {
14411 lpfc_printf_vlog(vport, KERN_WARNING, LOG_ELS,
14412 "3275 Failed to active ndlp found "
14413 "for oxid:x%x SID:x%x\n", oxid, sid);
14414 return;
14415 }
James Smart6669f9b2009-10-02 15:16:45 -040014416 }
14417
James Smart546fc852011-03-11 16:06:29 -050014418 /* Allocate buffer for rsp iocb */
James Smart6669f9b2009-10-02 15:16:45 -040014419 ctiocb = lpfc_sli_get_iocbq(phba);
14420 if (!ctiocb)
14421 return;
14422
James Smart5ffc2662009-11-18 15:39:44 -050014423 /* Extract the F_CTL field from FC_HDR */
14424 fctl = sli4_fctl_from_fc_hdr(fc_hdr);
14425
James Smart6669f9b2009-10-02 15:16:45 -040014426 icmd = &ctiocb->iocb;
James Smart6669f9b2009-10-02 15:16:45 -040014427 icmd->un.xseq64.bdl.bdeSize = 0;
James Smart5ffc2662009-11-18 15:39:44 -050014428 icmd->un.xseq64.bdl.ulpIoTag32 = 0;
James Smart6669f9b2009-10-02 15:16:45 -040014429 icmd->un.xseq64.w5.hcsw.Dfctl = 0;
14430 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_ACC;
14431 icmd->un.xseq64.w5.hcsw.Type = FC_TYPE_BLS;
14432
14433 /* Fill in the rest of iocb fields */
14434 icmd->ulpCommand = CMD_XMIT_BLS_RSP64_CX;
14435 icmd->ulpBdeCount = 0;
14436 icmd->ulpLe = 1;
14437 icmd->ulpClass = CLASS3;
James Smart6d368e52011-05-24 11:44:12 -040014438 icmd->ulpContext = phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
James Smart6dd9e312013-01-03 15:43:37 -050014439 ctiocb->context1 = lpfc_nlp_get(ndlp);
James Smart6669f9b2009-10-02 15:16:45 -040014440
James Smart6669f9b2009-10-02 15:16:45 -040014441 ctiocb->iocb_cmpl = NULL;
14442 ctiocb->vport = phba->pport;
James Smart546fc852011-03-11 16:06:29 -050014443 ctiocb->iocb_cmpl = lpfc_sli4_seq_abort_rsp_cmpl;
James Smart6d368e52011-05-24 11:44:12 -040014444 ctiocb->sli4_lxritag = NO_XRI;
James Smart546fc852011-03-11 16:06:29 -050014445 ctiocb->sli4_xritag = NO_XRI;
14446
James Smartee0f4fe2012-05-09 21:19:14 -040014447 if (fctl & FC_FC_EX_CTX)
14448 /* Exchange responder sent the abort so we
14449 * own the oxid.
14450 */
14451 xri = oxid;
14452 else
14453 xri = rxid;
14454 lxri = lpfc_sli4_xri_inrange(phba, xri);
14455 if (lxri != NO_XRI)
14456 lpfc_set_rrq_active(phba, ndlp, lxri,
14457 (xri == oxid) ? rxid : oxid, 0);
James Smart6dd9e312013-01-03 15:43:37 -050014458 /* For BA_ABTS from exchange responder, if the logical xri with
14459 * the oxid maps to the FCP XRI range, the port no longer has
14460 * that exchange context, send a BLS_RJT. Override the IOCB for
14461 * a BA_RJT.
James Smart546fc852011-03-11 16:06:29 -050014462 */
James Smart6dd9e312013-01-03 15:43:37 -050014463 if ((fctl & FC_FC_EX_CTX) &&
14464 (lxri > lpfc_sli4_get_els_iocb_cnt(phba))) {
14465 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_RJT;
14466 bf_set(lpfc_vndr_code, &icmd->un.bls_rsp, 0);
14467 bf_set(lpfc_rsn_expln, &icmd->un.bls_rsp, FC_BA_RJT_INV_XID);
14468 bf_set(lpfc_rsn_code, &icmd->un.bls_rsp, FC_BA_RJT_UNABLE);
14469 }
14470
14471 /* If BA_ABTS failed to abort a partially assembled receive sequence,
14472 * the driver no longer has that exchange, send a BLS_RJT. Override
14473 * the IOCB for a BA_RJT.
14474 */
14475 if (aborted == false) {
James Smart546fc852011-03-11 16:06:29 -050014476 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_RJT;
14477 bf_set(lpfc_vndr_code, &icmd->un.bls_rsp, 0);
14478 bf_set(lpfc_rsn_expln, &icmd->un.bls_rsp, FC_BA_RJT_INV_XID);
14479 bf_set(lpfc_rsn_code, &icmd->un.bls_rsp, FC_BA_RJT_UNABLE);
14480 }
James Smart6669f9b2009-10-02 15:16:45 -040014481
James Smart5ffc2662009-11-18 15:39:44 -050014482 if (fctl & FC_FC_EX_CTX) {
14483 /* ABTS sent by responder to CT exchange, construction
14484 * of BA_ACC will use OX_ID from ABTS for the XRI_TAG
14485 * field and RX_ID from ABTS for RX_ID field.
14486 */
James Smart546fc852011-03-11 16:06:29 -050014487 bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_RSP);
James Smart5ffc2662009-11-18 15:39:44 -050014488 } else {
14489 /* ABTS sent by initiator to CT exchange, construction
14490 * of BA_ACC will need to allocate a new XRI as for the
James Smartf09c3ac2012-03-01 22:33:29 -050014491 * XRI_TAG field.
James Smart5ffc2662009-11-18 15:39:44 -050014492 */
James Smart546fc852011-03-11 16:06:29 -050014493 bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_INT);
James Smart5ffc2662009-11-18 15:39:44 -050014494 }
James Smartf09c3ac2012-03-01 22:33:29 -050014495 bf_set(lpfc_abts_rxid, &icmd->un.bls_rsp, rxid);
James Smart546fc852011-03-11 16:06:29 -050014496 bf_set(lpfc_abts_oxid, &icmd->un.bls_rsp, oxid);
James Smart5ffc2662009-11-18 15:39:44 -050014497
James Smart546fc852011-03-11 16:06:29 -050014498 /* Xmit CT abts response on exchange <xid> */
James Smart6dd9e312013-01-03 15:43:37 -050014499 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
14500 "1200 Send BLS cmd x%x on oxid x%x Data: x%x\n",
14501 icmd->un.xseq64.w5.hcsw.Rctl, oxid, phba->link_state);
James Smart546fc852011-03-11 16:06:29 -050014502
14503 rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, ctiocb, 0);
14504 if (rc == IOCB_ERROR) {
James Smart6dd9e312013-01-03 15:43:37 -050014505 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
14506 "2925 Failed to issue CT ABTS RSP x%x on "
14507 "xri x%x, Data x%x\n",
14508 icmd->un.xseq64.w5.hcsw.Rctl, oxid,
14509 phba->link_state);
14510 lpfc_nlp_put(ndlp);
14511 ctiocb->context1 = NULL;
James Smart546fc852011-03-11 16:06:29 -050014512 lpfc_sli_release_iocbq(phba, ctiocb);
14513 }
James Smart6669f9b2009-10-02 15:16:45 -040014514}
14515
14516/**
14517 * lpfc_sli4_handle_unsol_abort - Handle sli-4 unsolicited abort event
14518 * @vport: Pointer to the vport on which this sequence was received
14519 * @dmabuf: pointer to a dmabuf that describes the FC sequence
14520 *
14521 * This function handles an SLI-4 unsolicited abort event. If the unsolicited
14522 * receive sequence is only partially assembed by the driver, it shall abort
14523 * the partially assembled frames for the sequence. Otherwise, if the
14524 * unsolicited receive sequence has been completely assembled and passed to
14525 * the Upper Layer Protocol (UPL), it then mark the per oxid status for the
14526 * unsolicited sequence has been aborted. After that, it will issue a basic
14527 * accept to accept the abort.
14528 **/
14529void
14530lpfc_sli4_handle_unsol_abort(struct lpfc_vport *vport,
14531 struct hbq_dmabuf *dmabuf)
14532{
14533 struct lpfc_hba *phba = vport->phba;
14534 struct fc_frame_header fc_hdr;
James Smart5ffc2662009-11-18 15:39:44 -050014535 uint32_t fctl;
James Smart6dd9e312013-01-03 15:43:37 -050014536 bool aborted;
James Smart6669f9b2009-10-02 15:16:45 -040014537
James Smart6669f9b2009-10-02 15:16:45 -040014538 /* Make a copy of fc_hdr before the dmabuf being released */
14539 memcpy(&fc_hdr, dmabuf->hbuf.virt, sizeof(struct fc_frame_header));
James Smart5ffc2662009-11-18 15:39:44 -050014540 fctl = sli4_fctl_from_fc_hdr(&fc_hdr);
James Smart6669f9b2009-10-02 15:16:45 -040014541
James Smart5ffc2662009-11-18 15:39:44 -050014542 if (fctl & FC_FC_EX_CTX) {
James Smart6dd9e312013-01-03 15:43:37 -050014543 /* ABTS by responder to exchange, no cleanup needed */
14544 aborted = true;
James Smart5ffc2662009-11-18 15:39:44 -050014545 } else {
James Smart6dd9e312013-01-03 15:43:37 -050014546 /* ABTS by initiator to exchange, need to do cleanup */
14547 aborted = lpfc_sli4_abort_partial_seq(vport, dmabuf);
14548 if (aborted == false)
14549 aborted = lpfc_sli4_abort_ulp_seq(vport, dmabuf);
James Smart5ffc2662009-11-18 15:39:44 -050014550 }
James Smart6dd9e312013-01-03 15:43:37 -050014551 lpfc_in_buf_free(phba, &dmabuf->dbuf);
14552
14553 /* Respond with BA_ACC or BA_RJT accordingly */
14554 lpfc_sli4_seq_abort_rsp(vport, &fc_hdr, aborted);
James Smart6669f9b2009-10-02 15:16:45 -040014555}
14556
14557/**
James Smart4f774512009-05-22 14:52:35 -040014558 * lpfc_seq_complete - Indicates if a sequence is complete
14559 * @dmabuf: pointer to a dmabuf that describes the FC sequence
14560 *
14561 * This function checks the sequence, starting with the frame described by
14562 * @dmabuf, to see if all the frames associated with this sequence are present.
14563 * the frames associated with this sequence are linked to the @dmabuf using the
14564 * dbuf list. This function looks for two major things. 1) That the first frame
14565 * has a sequence count of zero. 2) There is a frame with last frame of sequence
14566 * set. 3) That there are no holes in the sequence count. The function will
14567 * return 1 when the sequence is complete, otherwise it will return 0.
14568 **/
14569static int
14570lpfc_seq_complete(struct hbq_dmabuf *dmabuf)
14571{
14572 struct fc_frame_header *hdr;
14573 struct lpfc_dmabuf *d_buf;
14574 struct hbq_dmabuf *seq_dmabuf;
14575 uint32_t fctl;
14576 int seq_count = 0;
14577
14578 hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
14579 /* make sure first fame of sequence has a sequence count of zero */
14580 if (hdr->fh_seq_cnt != seq_count)
14581 return 0;
14582 fctl = (hdr->fh_f_ctl[0] << 16 |
14583 hdr->fh_f_ctl[1] << 8 |
14584 hdr->fh_f_ctl[2]);
14585 /* If last frame of sequence we can return success. */
14586 if (fctl & FC_FC_END_SEQ)
14587 return 1;
14588 list_for_each_entry(d_buf, &dmabuf->dbuf.list, list) {
14589 seq_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
14590 hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
14591 /* If there is a hole in the sequence count then fail. */
James Smarteeead812009-12-21 17:01:23 -050014592 if (++seq_count != be16_to_cpu(hdr->fh_seq_cnt))
James Smart4f774512009-05-22 14:52:35 -040014593 return 0;
14594 fctl = (hdr->fh_f_ctl[0] << 16 |
14595 hdr->fh_f_ctl[1] << 8 |
14596 hdr->fh_f_ctl[2]);
14597 /* If last frame of sequence we can return success. */
14598 if (fctl & FC_FC_END_SEQ)
14599 return 1;
14600 }
14601 return 0;
14602}
14603
14604/**
14605 * lpfc_prep_seq - Prep sequence for ULP processing
14606 * @vport: Pointer to the vport on which this sequence was received
14607 * @dmabuf: pointer to a dmabuf that describes the FC sequence
14608 *
14609 * This function takes a sequence, described by a list of frames, and creates
14610 * a list of iocbq structures to describe the sequence. This iocbq list will be
14611 * used to issue to the generic unsolicited sequence handler. This routine
14612 * returns a pointer to the first iocbq in the list. If the function is unable
14613 * to allocate an iocbq then it throw out the received frames that were not
14614 * able to be described and return a pointer to the first iocbq. If unable to
14615 * allocate any iocbqs (including the first) this function will return NULL.
14616 **/
14617static struct lpfc_iocbq *
14618lpfc_prep_seq(struct lpfc_vport *vport, struct hbq_dmabuf *seq_dmabuf)
14619{
James Smart7851fe22011-07-22 18:36:52 -040014620 struct hbq_dmabuf *hbq_buf;
James Smart4f774512009-05-22 14:52:35 -040014621 struct lpfc_dmabuf *d_buf, *n_buf;
14622 struct lpfc_iocbq *first_iocbq, *iocbq;
14623 struct fc_frame_header *fc_hdr;
14624 uint32_t sid;
James Smart7851fe22011-07-22 18:36:52 -040014625 uint32_t len, tot_len;
James Smarteeead812009-12-21 17:01:23 -050014626 struct ulp_bde64 *pbde;
James Smart4f774512009-05-22 14:52:35 -040014627
14628 fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
14629 /* remove from receive buffer list */
14630 list_del_init(&seq_dmabuf->hbuf.list);
James Smart45ed1192009-10-02 15:17:02 -040014631 lpfc_update_rcv_time_stamp(vport);
James Smart4f774512009-05-22 14:52:35 -040014632 /* get the Remote Port's SID */
James Smart6669f9b2009-10-02 15:16:45 -040014633 sid = sli4_sid_from_fc_hdr(fc_hdr);
James Smart7851fe22011-07-22 18:36:52 -040014634 tot_len = 0;
James Smart4f774512009-05-22 14:52:35 -040014635 /* Get an iocbq struct to fill in. */
14636 first_iocbq = lpfc_sli_get_iocbq(vport->phba);
14637 if (first_iocbq) {
14638 /* Initialize the first IOCB. */
James Smart8fa38512009-07-19 10:01:03 -040014639 first_iocbq->iocb.unsli3.rcvsli3.acc_len = 0;
James Smart4f774512009-05-22 14:52:35 -040014640 first_iocbq->iocb.ulpStatus = IOSTAT_SUCCESS;
James Smart939723a2012-05-09 21:19:03 -040014641
14642 /* Check FC Header to see what TYPE of frame we are rcv'ing */
14643 if (sli4_type_from_fc_hdr(fc_hdr) == FC_TYPE_ELS) {
14644 first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_ELS64_CX;
14645 first_iocbq->iocb.un.rcvels.parmRo =
14646 sli4_did_from_fc_hdr(fc_hdr);
14647 first_iocbq->iocb.ulpPU = PARM_NPIV_DID;
14648 } else
14649 first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_SEQ64_CX;
James Smart7851fe22011-07-22 18:36:52 -040014650 first_iocbq->iocb.ulpContext = NO_XRI;
14651 first_iocbq->iocb.unsli3.rcvsli3.ox_id =
14652 be16_to_cpu(fc_hdr->fh_ox_id);
14653 /* iocbq is prepped for internal consumption. Physical vpi. */
14654 first_iocbq->iocb.unsli3.rcvsli3.vpi =
14655 vport->phba->vpi_ids[vport->vpi];
James Smart4f774512009-05-22 14:52:35 -040014656 /* put the first buffer into the first IOCBq */
14657 first_iocbq->context2 = &seq_dmabuf->dbuf;
14658 first_iocbq->context3 = NULL;
14659 first_iocbq->iocb.ulpBdeCount = 1;
14660 first_iocbq->iocb.un.cont64[0].tus.f.bdeSize =
14661 LPFC_DATA_BUF_SIZE;
14662 first_iocbq->iocb.un.rcvels.remoteID = sid;
James Smart7851fe22011-07-22 18:36:52 -040014663 tot_len = bf_get(lpfc_rcqe_length,
James Smart4d9ab992009-10-02 15:16:39 -040014664 &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
James Smart7851fe22011-07-22 18:36:52 -040014665 first_iocbq->iocb.unsli3.rcvsli3.acc_len = tot_len;
James Smart4f774512009-05-22 14:52:35 -040014666 }
14667 iocbq = first_iocbq;
14668 /*
14669 * Each IOCBq can have two Buffers assigned, so go through the list
14670 * of buffers for this sequence and save two buffers in each IOCBq
14671 */
14672 list_for_each_entry_safe(d_buf, n_buf, &seq_dmabuf->dbuf.list, list) {
14673 if (!iocbq) {
14674 lpfc_in_buf_free(vport->phba, d_buf);
14675 continue;
14676 }
14677 if (!iocbq->context3) {
14678 iocbq->context3 = d_buf;
14679 iocbq->iocb.ulpBdeCount++;
James Smarteeead812009-12-21 17:01:23 -050014680 pbde = (struct ulp_bde64 *)
14681 &iocbq->iocb.unsli3.sli3Words[4];
14682 pbde->tus.f.bdeSize = LPFC_DATA_BUF_SIZE;
James Smart7851fe22011-07-22 18:36:52 -040014683
14684 /* We need to get the size out of the right CQE */
14685 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
14686 len = bf_get(lpfc_rcqe_length,
14687 &hbq_buf->cq_event.cqe.rcqe_cmpl);
14688 iocbq->iocb.unsli3.rcvsli3.acc_len += len;
14689 tot_len += len;
James Smart4f774512009-05-22 14:52:35 -040014690 } else {
14691 iocbq = lpfc_sli_get_iocbq(vport->phba);
14692 if (!iocbq) {
14693 if (first_iocbq) {
14694 first_iocbq->iocb.ulpStatus =
14695 IOSTAT_FCP_RSP_ERROR;
14696 first_iocbq->iocb.un.ulpWord[4] =
14697 IOERR_NO_RESOURCES;
14698 }
14699 lpfc_in_buf_free(vport->phba, d_buf);
14700 continue;
14701 }
14702 iocbq->context2 = d_buf;
14703 iocbq->context3 = NULL;
14704 iocbq->iocb.ulpBdeCount = 1;
14705 iocbq->iocb.un.cont64[0].tus.f.bdeSize =
14706 LPFC_DATA_BUF_SIZE;
James Smart7851fe22011-07-22 18:36:52 -040014707
14708 /* We need to get the size out of the right CQE */
14709 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
14710 len = bf_get(lpfc_rcqe_length,
14711 &hbq_buf->cq_event.cqe.rcqe_cmpl);
14712 tot_len += len;
14713 iocbq->iocb.unsli3.rcvsli3.acc_len = tot_len;
14714
James Smart4f774512009-05-22 14:52:35 -040014715 iocbq->iocb.un.rcvels.remoteID = sid;
14716 list_add_tail(&iocbq->list, &first_iocbq->list);
14717 }
14718 }
14719 return first_iocbq;
14720}
14721
James Smart6669f9b2009-10-02 15:16:45 -040014722static void
14723lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *vport,
14724 struct hbq_dmabuf *seq_dmabuf)
14725{
14726 struct fc_frame_header *fc_hdr;
14727 struct lpfc_iocbq *iocbq, *curr_iocb, *next_iocb;
14728 struct lpfc_hba *phba = vport->phba;
14729
14730 fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
14731 iocbq = lpfc_prep_seq(vport, seq_dmabuf);
14732 if (!iocbq) {
14733 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14734 "2707 Ring %d handler: Failed to allocate "
14735 "iocb Rctl x%x Type x%x received\n",
14736 LPFC_ELS_RING,
14737 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
14738 return;
14739 }
14740 if (!lpfc_complete_unsol_iocb(phba,
14741 &phba->sli.ring[LPFC_ELS_RING],
14742 iocbq, fc_hdr->fh_r_ctl,
14743 fc_hdr->fh_type))
James Smart6d368e52011-05-24 11:44:12 -040014744 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart6669f9b2009-10-02 15:16:45 -040014745 "2540 Ring %d handler: unexpected Rctl "
14746 "x%x Type x%x received\n",
14747 LPFC_ELS_RING,
14748 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
14749
14750 /* Free iocb created in lpfc_prep_seq */
14751 list_for_each_entry_safe(curr_iocb, next_iocb,
14752 &iocbq->list, list) {
14753 list_del_init(&curr_iocb->list);
14754 lpfc_sli_release_iocbq(phba, curr_iocb);
14755 }
14756 lpfc_sli_release_iocbq(phba, iocbq);
14757}
14758
James Smart4f774512009-05-22 14:52:35 -040014759/**
14760 * lpfc_sli4_handle_received_buffer - Handle received buffers from firmware
14761 * @phba: Pointer to HBA context object.
14762 *
14763 * This function is called with no lock held. This function processes all
14764 * the received buffers and gives it to upper layers when a received buffer
14765 * indicates that it is the final frame in the sequence. The interrupt
14766 * service routine processes received buffers at interrupt contexts and adds
14767 * received dma buffers to the rb_pend_list queue and signals the worker thread.
14768 * Worker thread calls lpfc_sli4_handle_received_buffer, which will call the
14769 * appropriate receive function when the final frame in a sequence is received.
14770 **/
James Smart4d9ab992009-10-02 15:16:39 -040014771void
14772lpfc_sli4_handle_received_buffer(struct lpfc_hba *phba,
14773 struct hbq_dmabuf *dmabuf)
James Smart4f774512009-05-22 14:52:35 -040014774{
James Smart4d9ab992009-10-02 15:16:39 -040014775 struct hbq_dmabuf *seq_dmabuf;
James Smart4f774512009-05-22 14:52:35 -040014776 struct fc_frame_header *fc_hdr;
14777 struct lpfc_vport *vport;
14778 uint32_t fcfi;
James Smart939723a2012-05-09 21:19:03 -040014779 uint32_t did;
James Smart4f774512009-05-22 14:52:35 -040014780
James Smart4f774512009-05-22 14:52:35 -040014781 /* Process each received buffer */
James Smart4d9ab992009-10-02 15:16:39 -040014782 fc_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
14783 /* check to see if this a valid type of frame */
14784 if (lpfc_fc_frame_check(phba, fc_hdr)) {
14785 lpfc_in_buf_free(phba, &dmabuf->dbuf);
14786 return;
14787 }
James Smart7851fe22011-07-22 18:36:52 -040014788 if ((bf_get(lpfc_cqe_code,
14789 &dmabuf->cq_event.cqe.rcqe_cmpl) == CQE_CODE_RECEIVE_V1))
14790 fcfi = bf_get(lpfc_rcqe_fcf_id_v1,
14791 &dmabuf->cq_event.cqe.rcqe_cmpl);
14792 else
14793 fcfi = bf_get(lpfc_rcqe_fcf_id,
14794 &dmabuf->cq_event.cqe.rcqe_cmpl);
James Smart939723a2012-05-09 21:19:03 -040014795
James Smart4d9ab992009-10-02 15:16:39 -040014796 vport = lpfc_fc_frame_to_vport(phba, fc_hdr, fcfi);
James Smart939723a2012-05-09 21:19:03 -040014797 if (!vport) {
James Smart4d9ab992009-10-02 15:16:39 -040014798 /* throw out the frame */
14799 lpfc_in_buf_free(phba, &dmabuf->dbuf);
14800 return;
14801 }
James Smart939723a2012-05-09 21:19:03 -040014802
14803 /* d_id this frame is directed to */
14804 did = sli4_did_from_fc_hdr(fc_hdr);
14805
14806 /* vport is registered unless we rcv a FLOGI directed to Fabric_DID */
14807 if (!(vport->vpi_state & LPFC_VPI_REGISTERED) &&
14808 (did != Fabric_DID)) {
14809 /*
14810 * Throw out the frame if we are not pt2pt.
14811 * The pt2pt protocol allows for discovery frames
14812 * to be received without a registered VPI.
14813 */
14814 if (!(vport->fc_flag & FC_PT2PT) ||
14815 (phba->link_state == LPFC_HBA_READY)) {
14816 lpfc_in_buf_free(phba, &dmabuf->dbuf);
14817 return;
14818 }
14819 }
14820
James Smart6669f9b2009-10-02 15:16:45 -040014821 /* Handle the basic abort sequence (BA_ABTS) event */
14822 if (fc_hdr->fh_r_ctl == FC_RCTL_BA_ABTS) {
14823 lpfc_sli4_handle_unsol_abort(vport, dmabuf);
14824 return;
14825 }
14826
James Smart4d9ab992009-10-02 15:16:39 -040014827 /* Link this frame */
14828 seq_dmabuf = lpfc_fc_frame_add(vport, dmabuf);
14829 if (!seq_dmabuf) {
14830 /* unable to add frame to vport - throw it out */
14831 lpfc_in_buf_free(phba, &dmabuf->dbuf);
14832 return;
14833 }
14834 /* If not last frame in sequence continue processing frames. */
James Smartdef9c7a2009-12-21 17:02:28 -050014835 if (!lpfc_seq_complete(seq_dmabuf))
James Smart4d9ab992009-10-02 15:16:39 -040014836 return;
James Smartdef9c7a2009-12-21 17:02:28 -050014837
James Smart6669f9b2009-10-02 15:16:45 -040014838 /* Send the complete sequence to the upper layer protocol */
14839 lpfc_sli4_send_seq_to_ulp(vport, seq_dmabuf);
James Smart4f774512009-05-22 14:52:35 -040014840}
James Smart6fb120a2009-05-22 14:52:59 -040014841
14842/**
14843 * lpfc_sli4_post_all_rpi_hdrs - Post the rpi header memory region to the port
14844 * @phba: pointer to lpfc hba data structure.
14845 *
14846 * This routine is invoked to post rpi header templates to the
14847 * HBA consistent with the SLI-4 interface spec. This routine
James Smart49198b32010-04-06 15:04:33 -040014848 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
14849 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
James Smart6fb120a2009-05-22 14:52:59 -040014850 *
14851 * This routine does not require any locks. It's usage is expected
14852 * to be driver load or reset recovery when the driver is
14853 * sequential.
14854 *
14855 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020014856 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -040014857 * -EIO - The mailbox failed to complete successfully.
James Smart6fb120a2009-05-22 14:52:59 -040014858 * When this error occurs, the driver is not guaranteed
14859 * to have any rpi regions posted to the device and
14860 * must either attempt to repost the regions or take a
14861 * fatal error.
14862 **/
14863int
14864lpfc_sli4_post_all_rpi_hdrs(struct lpfc_hba *phba)
14865{
14866 struct lpfc_rpi_hdr *rpi_page;
14867 uint32_t rc = 0;
James Smart6d368e52011-05-24 11:44:12 -040014868 uint16_t lrpi = 0;
James Smart6fb120a2009-05-22 14:52:59 -040014869
James Smart6d368e52011-05-24 11:44:12 -040014870 /* SLI4 ports that support extents do not require RPI headers. */
14871 if (!phba->sli4_hba.rpi_hdrs_in_use)
14872 goto exit;
14873 if (phba->sli4_hba.extents_in_use)
14874 return -EIO;
14875
James Smart6fb120a2009-05-22 14:52:59 -040014876 list_for_each_entry(rpi_page, &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
James Smart6d368e52011-05-24 11:44:12 -040014877 /*
14878 * Assign the rpi headers a physical rpi only if the driver
14879 * has not initialized those resources. A port reset only
14880 * needs the headers posted.
14881 */
14882 if (bf_get(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags) !=
14883 LPFC_RPI_RSRC_RDY)
14884 rpi_page->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
14885
James Smart6fb120a2009-05-22 14:52:59 -040014886 rc = lpfc_sli4_post_rpi_hdr(phba, rpi_page);
14887 if (rc != MBX_SUCCESS) {
14888 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14889 "2008 Error %d posting all rpi "
14890 "headers\n", rc);
14891 rc = -EIO;
14892 break;
14893 }
14894 }
14895
James Smart6d368e52011-05-24 11:44:12 -040014896 exit:
14897 bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags,
14898 LPFC_RPI_RSRC_RDY);
James Smart6fb120a2009-05-22 14:52:59 -040014899 return rc;
14900}
14901
14902/**
14903 * lpfc_sli4_post_rpi_hdr - Post an rpi header memory region to the port
14904 * @phba: pointer to lpfc hba data structure.
14905 * @rpi_page: pointer to the rpi memory region.
14906 *
14907 * This routine is invoked to post a single rpi header to the
14908 * HBA consistent with the SLI-4 interface spec. This memory region
14909 * maps up to 64 rpi context regions.
14910 *
14911 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020014912 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -040014913 * -ENOMEM - No available memory
14914 * -EIO - The mailbox failed to complete successfully.
James Smart6fb120a2009-05-22 14:52:59 -040014915 **/
14916int
14917lpfc_sli4_post_rpi_hdr(struct lpfc_hba *phba, struct lpfc_rpi_hdr *rpi_page)
14918{
14919 LPFC_MBOXQ_t *mboxq;
14920 struct lpfc_mbx_post_hdr_tmpl *hdr_tmpl;
14921 uint32_t rc = 0;
James Smart6fb120a2009-05-22 14:52:59 -040014922 uint32_t shdr_status, shdr_add_status;
14923 union lpfc_sli4_cfg_shdr *shdr;
14924
James Smart6d368e52011-05-24 11:44:12 -040014925 /* SLI4 ports that support extents do not require RPI headers. */
14926 if (!phba->sli4_hba.rpi_hdrs_in_use)
14927 return rc;
14928 if (phba->sli4_hba.extents_in_use)
14929 return -EIO;
14930
James Smart6fb120a2009-05-22 14:52:59 -040014931 /* The port is notified of the header region via a mailbox command. */
14932 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14933 if (!mboxq) {
14934 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14935 "2001 Unable to allocate memory for issuing "
14936 "SLI_CONFIG_SPECIAL mailbox command\n");
14937 return -ENOMEM;
14938 }
14939
14940 /* Post all rpi memory regions to the port. */
14941 hdr_tmpl = &mboxq->u.mqe.un.hdr_tmpl;
James Smart6fb120a2009-05-22 14:52:59 -040014942 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
14943 LPFC_MBOX_OPCODE_FCOE_POST_HDR_TEMPLATE,
14944 sizeof(struct lpfc_mbx_post_hdr_tmpl) -
James Smartfedd3b72011-02-16 12:39:24 -050014945 sizeof(struct lpfc_sli4_cfg_mhdr),
14946 LPFC_SLI4_MBX_EMBED);
James Smart6d368e52011-05-24 11:44:12 -040014947
14948
14949 /* Post the physical rpi to the port for this rpi header. */
James Smart6fb120a2009-05-22 14:52:59 -040014950 bf_set(lpfc_mbx_post_hdr_tmpl_rpi_offset, hdr_tmpl,
14951 rpi_page->start_rpi);
James Smart6d368e52011-05-24 11:44:12 -040014952 bf_set(lpfc_mbx_post_hdr_tmpl_page_cnt,
14953 hdr_tmpl, rpi_page->page_count);
14954
James Smart6fb120a2009-05-22 14:52:59 -040014955 hdr_tmpl->rpi_paddr_lo = putPaddrLow(rpi_page->dmabuf->phys);
14956 hdr_tmpl->rpi_paddr_hi = putPaddrHigh(rpi_page->dmabuf->phys);
James Smartf1126682009-06-10 17:22:44 -040014957 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
James Smart6fb120a2009-05-22 14:52:59 -040014958 shdr = (union lpfc_sli4_cfg_shdr *) &hdr_tmpl->header.cfg_shdr;
14959 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14960 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14961 if (rc != MBX_TIMEOUT)
14962 mempool_free(mboxq, phba->mbox_mem_pool);
14963 if (shdr_status || shdr_add_status || rc) {
14964 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14965 "2514 POST_RPI_HDR mailbox failed with "
14966 "status x%x add_status x%x, mbx status x%x\n",
14967 shdr_status, shdr_add_status, rc);
14968 rc = -ENXIO;
14969 }
14970 return rc;
14971}
14972
14973/**
14974 * lpfc_sli4_alloc_rpi - Get an available rpi in the device's range
14975 * @phba: pointer to lpfc hba data structure.
14976 *
14977 * This routine is invoked to post rpi header templates to the
14978 * HBA consistent with the SLI-4 interface spec. This routine
James Smart49198b32010-04-06 15:04:33 -040014979 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
14980 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
James Smart6fb120a2009-05-22 14:52:59 -040014981 *
14982 * Returns
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020014983 * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
James Smart6fb120a2009-05-22 14:52:59 -040014984 * LPFC_RPI_ALLOC_ERROR if no rpis are available.
14985 **/
14986int
14987lpfc_sli4_alloc_rpi(struct lpfc_hba *phba)
14988{
James Smart6d368e52011-05-24 11:44:12 -040014989 unsigned long rpi;
14990 uint16_t max_rpi, rpi_limit;
14991 uint16_t rpi_remaining, lrpi = 0;
James Smart6fb120a2009-05-22 14:52:59 -040014992 struct lpfc_rpi_hdr *rpi_hdr;
14993
14994 max_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
James Smart6fb120a2009-05-22 14:52:59 -040014995 rpi_limit = phba->sli4_hba.next_rpi;
14996
14997 /*
James Smart6d368e52011-05-24 11:44:12 -040014998 * Fetch the next logical rpi. Because this index is logical,
14999 * the driver starts at 0 each time.
James Smart6fb120a2009-05-22 14:52:59 -040015000 */
15001 spin_lock_irq(&phba->hbalock);
James Smart6d368e52011-05-24 11:44:12 -040015002 rpi = find_next_zero_bit(phba->sli4_hba.rpi_bmask, rpi_limit, 0);
15003 if (rpi >= rpi_limit)
James Smart6fb120a2009-05-22 14:52:59 -040015004 rpi = LPFC_RPI_ALLOC_ERROR;
15005 else {
15006 set_bit(rpi, phba->sli4_hba.rpi_bmask);
15007 phba->sli4_hba.max_cfg_param.rpi_used++;
15008 phba->sli4_hba.rpi_count++;
15009 }
15010
15011 /*
15012 * Don't try to allocate more rpi header regions if the device limit
James Smart6d368e52011-05-24 11:44:12 -040015013 * has been exhausted.
James Smart6fb120a2009-05-22 14:52:59 -040015014 */
15015 if ((rpi == LPFC_RPI_ALLOC_ERROR) &&
15016 (phba->sli4_hba.rpi_count >= max_rpi)) {
15017 spin_unlock_irq(&phba->hbalock);
15018 return rpi;
15019 }
15020
15021 /*
James Smart6d368e52011-05-24 11:44:12 -040015022 * RPI header postings are not required for SLI4 ports capable of
15023 * extents.
15024 */
15025 if (!phba->sli4_hba.rpi_hdrs_in_use) {
15026 spin_unlock_irq(&phba->hbalock);
15027 return rpi;
15028 }
15029
15030 /*
James Smart6fb120a2009-05-22 14:52:59 -040015031 * If the driver is running low on rpi resources, allocate another
15032 * page now. Note that the next_rpi value is used because
15033 * it represents how many are actually in use whereas max_rpi notes
15034 * how many are supported max by the device.
15035 */
James Smart6d368e52011-05-24 11:44:12 -040015036 rpi_remaining = phba->sli4_hba.next_rpi - phba->sli4_hba.rpi_count;
James Smart6fb120a2009-05-22 14:52:59 -040015037 spin_unlock_irq(&phba->hbalock);
15038 if (rpi_remaining < LPFC_RPI_LOW_WATER_MARK) {
15039 rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
15040 if (!rpi_hdr) {
15041 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15042 "2002 Error Could not grow rpi "
15043 "count\n");
15044 } else {
James Smart6d368e52011-05-24 11:44:12 -040015045 lrpi = rpi_hdr->start_rpi;
15046 rpi_hdr->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
James Smart6fb120a2009-05-22 14:52:59 -040015047 lpfc_sli4_post_rpi_hdr(phba, rpi_hdr);
15048 }
15049 }
15050
15051 return rpi;
15052}
15053
15054/**
15055 * lpfc_sli4_free_rpi - Release an rpi for reuse.
15056 * @phba: pointer to lpfc hba data structure.
15057 *
15058 * This routine is invoked to release an rpi to the pool of
15059 * available rpis maintained by the driver.
15060 **/
15061void
James Smartd7c47992010-06-08 18:31:54 -040015062__lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
15063{
15064 if (test_and_clear_bit(rpi, phba->sli4_hba.rpi_bmask)) {
15065 phba->sli4_hba.rpi_count--;
15066 phba->sli4_hba.max_cfg_param.rpi_used--;
15067 }
15068}
15069
15070/**
15071 * lpfc_sli4_free_rpi - Release an rpi for reuse.
15072 * @phba: pointer to lpfc hba data structure.
15073 *
15074 * This routine is invoked to release an rpi to the pool of
15075 * available rpis maintained by the driver.
15076 **/
15077void
James Smart6fb120a2009-05-22 14:52:59 -040015078lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
15079{
15080 spin_lock_irq(&phba->hbalock);
James Smartd7c47992010-06-08 18:31:54 -040015081 __lpfc_sli4_free_rpi(phba, rpi);
James Smart6fb120a2009-05-22 14:52:59 -040015082 spin_unlock_irq(&phba->hbalock);
15083}
15084
15085/**
15086 * lpfc_sli4_remove_rpis - Remove the rpi bitmask region
15087 * @phba: pointer to lpfc hba data structure.
15088 *
15089 * This routine is invoked to remove the memory region that
15090 * provided rpi via a bitmask.
15091 **/
15092void
15093lpfc_sli4_remove_rpis(struct lpfc_hba *phba)
15094{
15095 kfree(phba->sli4_hba.rpi_bmask);
James Smart6d368e52011-05-24 11:44:12 -040015096 kfree(phba->sli4_hba.rpi_ids);
15097 bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
James Smart6fb120a2009-05-22 14:52:59 -040015098}
15099
15100/**
15101 * lpfc_sli4_resume_rpi - Remove the rpi bitmask region
15102 * @phba: pointer to lpfc hba data structure.
15103 *
15104 * This routine is invoked to remove the memory region that
15105 * provided rpi via a bitmask.
15106 **/
15107int
James Smart6b5151f2012-01-18 16:24:06 -050015108lpfc_sli4_resume_rpi(struct lpfc_nodelist *ndlp,
15109 void (*cmpl)(struct lpfc_hba *, LPFC_MBOXQ_t *), void *arg)
James Smart6fb120a2009-05-22 14:52:59 -040015110{
15111 LPFC_MBOXQ_t *mboxq;
15112 struct lpfc_hba *phba = ndlp->phba;
15113 int rc;
15114
15115 /* The port is notified of the header region via a mailbox command. */
15116 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15117 if (!mboxq)
15118 return -ENOMEM;
15119
15120 /* Post all rpi memory regions to the port. */
15121 lpfc_resume_rpi(mboxq, ndlp);
James Smart6b5151f2012-01-18 16:24:06 -050015122 if (cmpl) {
15123 mboxq->mbox_cmpl = cmpl;
15124 mboxq->context1 = arg;
15125 mboxq->context2 = ndlp;
James Smart72859902012-01-18 16:25:38 -050015126 } else
15127 mboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart6b5151f2012-01-18 16:24:06 -050015128 mboxq->vport = ndlp->vport;
James Smart6fb120a2009-05-22 14:52:59 -040015129 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
15130 if (rc == MBX_NOT_FINISHED) {
15131 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15132 "2010 Resume RPI Mailbox failed "
15133 "status %d, mbxStatus x%x\n", rc,
15134 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
15135 mempool_free(mboxq, phba->mbox_mem_pool);
15136 return -EIO;
15137 }
15138 return 0;
15139}
15140
15141/**
15142 * lpfc_sli4_init_vpi - Initialize a vpi with the port
James Smart76a95d72010-11-20 23:11:48 -050015143 * @vport: Pointer to the vport for which the vpi is being initialized
James Smart6fb120a2009-05-22 14:52:59 -040015144 *
James Smart76a95d72010-11-20 23:11:48 -050015145 * This routine is invoked to activate a vpi with the port.
James Smart6fb120a2009-05-22 14:52:59 -040015146 *
15147 * Returns:
15148 * 0 success
15149 * -Evalue otherwise
15150 **/
15151int
James Smart76a95d72010-11-20 23:11:48 -050015152lpfc_sli4_init_vpi(struct lpfc_vport *vport)
James Smart6fb120a2009-05-22 14:52:59 -040015153{
15154 LPFC_MBOXQ_t *mboxq;
15155 int rc = 0;
James Smart6a9c52c2009-10-02 15:16:51 -040015156 int retval = MBX_SUCCESS;
James Smart6fb120a2009-05-22 14:52:59 -040015157 uint32_t mbox_tmo;
James Smart76a95d72010-11-20 23:11:48 -050015158 struct lpfc_hba *phba = vport->phba;
James Smart6fb120a2009-05-22 14:52:59 -040015159 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15160 if (!mboxq)
15161 return -ENOMEM;
James Smart76a95d72010-11-20 23:11:48 -050015162 lpfc_init_vpi(phba, mboxq, vport->vpi);
James Smarta183a152011-10-10 21:32:43 -040015163 mbox_tmo = lpfc_mbox_tmo_val(phba, mboxq);
James Smart6fb120a2009-05-22 14:52:59 -040015164 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
James Smart6fb120a2009-05-22 14:52:59 -040015165 if (rc != MBX_SUCCESS) {
James Smart76a95d72010-11-20 23:11:48 -050015166 lpfc_printf_vlog(vport, KERN_ERR, LOG_SLI,
James Smart6fb120a2009-05-22 14:52:59 -040015167 "2022 INIT VPI Mailbox failed "
15168 "status %d, mbxStatus x%x\n", rc,
15169 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
James Smart6a9c52c2009-10-02 15:16:51 -040015170 retval = -EIO;
James Smart6fb120a2009-05-22 14:52:59 -040015171 }
James Smart6a9c52c2009-10-02 15:16:51 -040015172 if (rc != MBX_TIMEOUT)
James Smart76a95d72010-11-20 23:11:48 -050015173 mempool_free(mboxq, vport->phba->mbox_mem_pool);
James Smart6a9c52c2009-10-02 15:16:51 -040015174
15175 return retval;
James Smart6fb120a2009-05-22 14:52:59 -040015176}
15177
15178/**
15179 * lpfc_mbx_cmpl_add_fcf_record - add fcf mbox completion handler.
15180 * @phba: pointer to lpfc hba data structure.
15181 * @mboxq: Pointer to mailbox object.
15182 *
15183 * This routine is invoked to manually add a single FCF record. The caller
15184 * must pass a completely initialized FCF_Record. This routine takes
15185 * care of the nonembedded mailbox operations.
15186 **/
15187static void
15188lpfc_mbx_cmpl_add_fcf_record(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
15189{
15190 void *virt_addr;
15191 union lpfc_sli4_cfg_shdr *shdr;
15192 uint32_t shdr_status, shdr_add_status;
15193
15194 virt_addr = mboxq->sge_array->addr[0];
15195 /* The IOCTL status is embedded in the mailbox subheader. */
15196 shdr = (union lpfc_sli4_cfg_shdr *) virt_addr;
15197 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15198 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15199
15200 if ((shdr_status || shdr_add_status) &&
15201 (shdr_status != STATUS_FCF_IN_USE))
15202 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15203 "2558 ADD_FCF_RECORD mailbox failed with "
15204 "status x%x add_status x%x\n",
15205 shdr_status, shdr_add_status);
15206
15207 lpfc_sli4_mbox_cmd_free(phba, mboxq);
15208}
15209
15210/**
15211 * lpfc_sli4_add_fcf_record - Manually add an FCF Record.
15212 * @phba: pointer to lpfc hba data structure.
15213 * @fcf_record: pointer to the initialized fcf record to add.
15214 *
15215 * This routine is invoked to manually add a single FCF record. The caller
15216 * must pass a completely initialized FCF_Record. This routine takes
15217 * care of the nonembedded mailbox operations.
15218 **/
15219int
15220lpfc_sli4_add_fcf_record(struct lpfc_hba *phba, struct fcf_record *fcf_record)
15221{
15222 int rc = 0;
15223 LPFC_MBOXQ_t *mboxq;
15224 uint8_t *bytep;
15225 void *virt_addr;
15226 dma_addr_t phys_addr;
15227 struct lpfc_mbx_sge sge;
15228 uint32_t alloc_len, req_len;
15229 uint32_t fcfindex;
15230
15231 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15232 if (!mboxq) {
15233 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15234 "2009 Failed to allocate mbox for ADD_FCF cmd\n");
15235 return -ENOMEM;
15236 }
15237
15238 req_len = sizeof(struct fcf_record) + sizeof(union lpfc_sli4_cfg_shdr) +
15239 sizeof(uint32_t);
15240
15241 /* Allocate DMA memory and set up the non-embedded mailbox command */
15242 alloc_len = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
15243 LPFC_MBOX_OPCODE_FCOE_ADD_FCF,
15244 req_len, LPFC_SLI4_MBX_NEMBED);
15245 if (alloc_len < req_len) {
15246 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15247 "2523 Allocated DMA memory size (x%x) is "
15248 "less than the requested DMA memory "
15249 "size (x%x)\n", alloc_len, req_len);
15250 lpfc_sli4_mbox_cmd_free(phba, mboxq);
15251 return -ENOMEM;
15252 }
15253
15254 /*
15255 * Get the first SGE entry from the non-embedded DMA memory. This
15256 * routine only uses a single SGE.
15257 */
15258 lpfc_sli4_mbx_sge_get(mboxq, 0, &sge);
15259 phys_addr = getPaddr(sge.pa_hi, sge.pa_lo);
James Smart6fb120a2009-05-22 14:52:59 -040015260 virt_addr = mboxq->sge_array->addr[0];
15261 /*
15262 * Configure the FCF record for FCFI 0. This is the driver's
15263 * hardcoded default and gets used in nonFIP mode.
15264 */
15265 fcfindex = bf_get(lpfc_fcf_record_fcf_index, fcf_record);
15266 bytep = virt_addr + sizeof(union lpfc_sli4_cfg_shdr);
15267 lpfc_sli_pcimem_bcopy(&fcfindex, bytep, sizeof(uint32_t));
15268
15269 /*
15270 * Copy the fcf_index and the FCF Record Data. The data starts after
15271 * the FCoE header plus word10. The data copy needs to be endian
15272 * correct.
15273 */
15274 bytep += sizeof(uint32_t);
15275 lpfc_sli_pcimem_bcopy(fcf_record, bytep, sizeof(struct fcf_record));
15276 mboxq->vport = phba->pport;
15277 mboxq->mbox_cmpl = lpfc_mbx_cmpl_add_fcf_record;
15278 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
15279 if (rc == MBX_NOT_FINISHED) {
15280 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15281 "2515 ADD_FCF_RECORD mailbox failed with "
15282 "status 0x%x\n", rc);
15283 lpfc_sli4_mbox_cmd_free(phba, mboxq);
15284 rc = -EIO;
15285 } else
15286 rc = 0;
15287
15288 return rc;
15289}
15290
15291/**
15292 * lpfc_sli4_build_dflt_fcf_record - Build the driver's default FCF Record.
15293 * @phba: pointer to lpfc hba data structure.
15294 * @fcf_record: pointer to the fcf record to write the default data.
15295 * @fcf_index: FCF table entry index.
15296 *
15297 * This routine is invoked to build the driver's default FCF record. The
15298 * values used are hardcoded. This routine handles memory initialization.
15299 *
15300 **/
15301void
15302lpfc_sli4_build_dflt_fcf_record(struct lpfc_hba *phba,
15303 struct fcf_record *fcf_record,
15304 uint16_t fcf_index)
15305{
15306 memset(fcf_record, 0, sizeof(struct fcf_record));
15307 fcf_record->max_rcv_size = LPFC_FCOE_MAX_RCV_SIZE;
15308 fcf_record->fka_adv_period = LPFC_FCOE_FKA_ADV_PER;
15309 fcf_record->fip_priority = LPFC_FCOE_FIP_PRIORITY;
15310 bf_set(lpfc_fcf_record_mac_0, fcf_record, phba->fc_map[0]);
15311 bf_set(lpfc_fcf_record_mac_1, fcf_record, phba->fc_map[1]);
15312 bf_set(lpfc_fcf_record_mac_2, fcf_record, phba->fc_map[2]);
15313 bf_set(lpfc_fcf_record_mac_3, fcf_record, LPFC_FCOE_FCF_MAC3);
15314 bf_set(lpfc_fcf_record_mac_4, fcf_record, LPFC_FCOE_FCF_MAC4);
15315 bf_set(lpfc_fcf_record_mac_5, fcf_record, LPFC_FCOE_FCF_MAC5);
15316 bf_set(lpfc_fcf_record_fc_map_0, fcf_record, phba->fc_map[0]);
15317 bf_set(lpfc_fcf_record_fc_map_1, fcf_record, phba->fc_map[1]);
15318 bf_set(lpfc_fcf_record_fc_map_2, fcf_record, phba->fc_map[2]);
15319 bf_set(lpfc_fcf_record_fcf_valid, fcf_record, 1);
James Smart0c287582009-06-10 17:22:56 -040015320 bf_set(lpfc_fcf_record_fcf_avail, fcf_record, 1);
James Smart6fb120a2009-05-22 14:52:59 -040015321 bf_set(lpfc_fcf_record_fcf_index, fcf_record, fcf_index);
15322 bf_set(lpfc_fcf_record_mac_addr_prov, fcf_record,
15323 LPFC_FCF_FPMA | LPFC_FCF_SPMA);
15324 /* Set the VLAN bit map */
15325 if (phba->valid_vlan) {
15326 fcf_record->vlan_bitmap[phba->vlan_id / 8]
15327 = 1 << (phba->vlan_id % 8);
15328 }
15329}
15330
15331/**
James Smart0c9ab6f2010-02-26 14:15:57 -050015332 * lpfc_sli4_fcf_scan_read_fcf_rec - Read hba fcf record for fcf scan.
James Smart6fb120a2009-05-22 14:52:59 -040015333 * @phba: pointer to lpfc hba data structure.
15334 * @fcf_index: FCF table entry offset.
15335 *
James Smart0c9ab6f2010-02-26 14:15:57 -050015336 * This routine is invoked to scan the entire FCF table by reading FCF
15337 * record and processing it one at a time starting from the @fcf_index
15338 * for initial FCF discovery or fast FCF failover rediscovery.
15339 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030015340 * Return 0 if the mailbox command is submitted successfully, none 0
James Smart0c9ab6f2010-02-26 14:15:57 -050015341 * otherwise.
James Smart6fb120a2009-05-22 14:52:59 -040015342 **/
15343int
James Smart0c9ab6f2010-02-26 14:15:57 -050015344lpfc_sli4_fcf_scan_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
James Smart6fb120a2009-05-22 14:52:59 -040015345{
15346 int rc = 0, error;
15347 LPFC_MBOXQ_t *mboxq;
James Smart6fb120a2009-05-22 14:52:59 -040015348
James Smart32b97932009-07-19 10:01:21 -040015349 phba->fcoe_eventtag_at_fcf_scan = phba->fcoe_eventtag;
James Smart80c17842012-03-01 22:35:45 -050015350 phba->fcoe_cvl_eventtag_attn = phba->fcoe_cvl_eventtag;
James Smart6fb120a2009-05-22 14:52:59 -040015351 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15352 if (!mboxq) {
15353 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15354 "2000 Failed to allocate mbox for "
15355 "READ_FCF cmd\n");
James Smart4d9ab992009-10-02 15:16:39 -040015356 error = -ENOMEM;
James Smart0c9ab6f2010-02-26 14:15:57 -050015357 goto fail_fcf_scan;
James Smart6fb120a2009-05-22 14:52:59 -040015358 }
James Smartecfd03c2010-02-12 14:41:27 -050015359 /* Construct the read FCF record mailbox command */
James Smart0c9ab6f2010-02-26 14:15:57 -050015360 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
James Smartecfd03c2010-02-12 14:41:27 -050015361 if (rc) {
15362 error = -EINVAL;
James Smart0c9ab6f2010-02-26 14:15:57 -050015363 goto fail_fcf_scan;
James Smart6fb120a2009-05-22 14:52:59 -040015364 }
James Smartecfd03c2010-02-12 14:41:27 -050015365 /* Issue the mailbox command asynchronously */
James Smart6fb120a2009-05-22 14:52:59 -040015366 mboxq->vport = phba->pport;
James Smart0c9ab6f2010-02-26 14:15:57 -050015367 mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_scan_read_fcf_rec;
James Smarta93ff372010-10-22 11:06:08 -040015368
15369 spin_lock_irq(&phba->hbalock);
15370 phba->hba_flag |= FCF_TS_INPROG;
15371 spin_unlock_irq(&phba->hbalock);
15372
James Smart6fb120a2009-05-22 14:52:59 -040015373 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
James Smartecfd03c2010-02-12 14:41:27 -050015374 if (rc == MBX_NOT_FINISHED)
James Smart6fb120a2009-05-22 14:52:59 -040015375 error = -EIO;
James Smartecfd03c2010-02-12 14:41:27 -050015376 else {
James Smart38b92ef2010-08-04 16:11:39 -040015377 /* Reset eligible FCF count for new scan */
15378 if (fcf_index == LPFC_FCOE_FCF_GET_FIRST)
James Smart999d8132010-03-15 11:24:56 -040015379 phba->fcf.eligible_fcf_cnt = 0;
James Smart6fb120a2009-05-22 14:52:59 -040015380 error = 0;
James Smart32b97932009-07-19 10:01:21 -040015381 }
James Smart0c9ab6f2010-02-26 14:15:57 -050015382fail_fcf_scan:
James Smart4d9ab992009-10-02 15:16:39 -040015383 if (error) {
15384 if (mboxq)
15385 lpfc_sli4_mbox_cmd_free(phba, mboxq);
James Smarta93ff372010-10-22 11:06:08 -040015386 /* FCF scan failed, clear FCF_TS_INPROG flag */
James Smart4d9ab992009-10-02 15:16:39 -040015387 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -040015388 phba->hba_flag &= ~FCF_TS_INPROG;
James Smart4d9ab992009-10-02 15:16:39 -040015389 spin_unlock_irq(&phba->hbalock);
15390 }
James Smart6fb120a2009-05-22 14:52:59 -040015391 return error;
15392}
James Smarta0c87cb2009-07-19 10:01:10 -040015393
15394/**
James Smarta93ff372010-10-22 11:06:08 -040015395 * lpfc_sli4_fcf_rr_read_fcf_rec - Read hba fcf record for roundrobin fcf.
James Smart0c9ab6f2010-02-26 14:15:57 -050015396 * @phba: pointer to lpfc hba data structure.
15397 * @fcf_index: FCF table entry offset.
15398 *
15399 * This routine is invoked to read an FCF record indicated by @fcf_index
James Smarta93ff372010-10-22 11:06:08 -040015400 * and to use it for FLOGI roundrobin FCF failover.
James Smart0c9ab6f2010-02-26 14:15:57 -050015401 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030015402 * Return 0 if the mailbox command is submitted successfully, none 0
James Smart0c9ab6f2010-02-26 14:15:57 -050015403 * otherwise.
15404 **/
15405int
15406lpfc_sli4_fcf_rr_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
15407{
15408 int rc = 0, error;
15409 LPFC_MBOXQ_t *mboxq;
15410
15411 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15412 if (!mboxq) {
15413 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
15414 "2763 Failed to allocate mbox for "
15415 "READ_FCF cmd\n");
15416 error = -ENOMEM;
15417 goto fail_fcf_read;
15418 }
15419 /* Construct the read FCF record mailbox command */
15420 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
15421 if (rc) {
15422 error = -EINVAL;
15423 goto fail_fcf_read;
15424 }
15425 /* Issue the mailbox command asynchronously */
15426 mboxq->vport = phba->pport;
15427 mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_rr_read_fcf_rec;
15428 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
15429 if (rc == MBX_NOT_FINISHED)
15430 error = -EIO;
15431 else
15432 error = 0;
15433
15434fail_fcf_read:
15435 if (error && mboxq)
15436 lpfc_sli4_mbox_cmd_free(phba, mboxq);
15437 return error;
15438}
15439
15440/**
15441 * lpfc_sli4_read_fcf_rec - Read hba fcf record for update eligible fcf bmask.
15442 * @phba: pointer to lpfc hba data structure.
15443 * @fcf_index: FCF table entry offset.
15444 *
15445 * This routine is invoked to read an FCF record indicated by @fcf_index to
James Smarta93ff372010-10-22 11:06:08 -040015446 * determine whether it's eligible for FLOGI roundrobin failover list.
James Smart0c9ab6f2010-02-26 14:15:57 -050015447 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030015448 * Return 0 if the mailbox command is submitted successfully, none 0
James Smart0c9ab6f2010-02-26 14:15:57 -050015449 * otherwise.
15450 **/
15451int
15452lpfc_sli4_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
15453{
15454 int rc = 0, error;
15455 LPFC_MBOXQ_t *mboxq;
15456
15457 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15458 if (!mboxq) {
15459 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
15460 "2758 Failed to allocate mbox for "
15461 "READ_FCF cmd\n");
15462 error = -ENOMEM;
15463 goto fail_fcf_read;
15464 }
15465 /* Construct the read FCF record mailbox command */
15466 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
15467 if (rc) {
15468 error = -EINVAL;
15469 goto fail_fcf_read;
15470 }
15471 /* Issue the mailbox command asynchronously */
15472 mboxq->vport = phba->pport;
15473 mboxq->mbox_cmpl = lpfc_mbx_cmpl_read_fcf_rec;
15474 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
15475 if (rc == MBX_NOT_FINISHED)
15476 error = -EIO;
15477 else
15478 error = 0;
15479
15480fail_fcf_read:
15481 if (error && mboxq)
15482 lpfc_sli4_mbox_cmd_free(phba, mboxq);
15483 return error;
15484}
15485
15486/**
James Smart7d791df2011-07-22 18:37:52 -040015487 * lpfc_check_next_fcf_pri
15488 * phba pointer to the lpfc_hba struct for this port.
15489 * This routine is called from the lpfc_sli4_fcf_rr_next_index_get
15490 * routine when the rr_bmask is empty. The FCF indecies are put into the
15491 * rr_bmask based on their priority level. Starting from the highest priority
15492 * to the lowest. The most likely FCF candidate will be in the highest
15493 * priority group. When this routine is called it searches the fcf_pri list for
15494 * next lowest priority group and repopulates the rr_bmask with only those
15495 * fcf_indexes.
15496 * returns:
15497 * 1=success 0=failure
15498 **/
15499int
15500lpfc_check_next_fcf_pri_level(struct lpfc_hba *phba)
15501{
15502 uint16_t next_fcf_pri;
15503 uint16_t last_index;
15504 struct lpfc_fcf_pri *fcf_pri;
15505 int rc;
15506 int ret = 0;
15507
15508 last_index = find_first_bit(phba->fcf.fcf_rr_bmask,
15509 LPFC_SLI4_FCF_TBL_INDX_MAX);
15510 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
15511 "3060 Last IDX %d\n", last_index);
James Smart25626692013-03-01 16:36:54 -050015512
15513 /* Verify the priority list has 2 or more entries */
15514 spin_lock_irq(&phba->hbalock);
15515 if (list_empty(&phba->fcf.fcf_pri_list) ||
15516 list_is_singular(&phba->fcf.fcf_pri_list)) {
15517 spin_unlock_irq(&phba->hbalock);
James Smart7d791df2011-07-22 18:37:52 -040015518 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
15519 "3061 Last IDX %d\n", last_index);
15520 return 0; /* Empty rr list */
15521 }
James Smart25626692013-03-01 16:36:54 -050015522 spin_unlock_irq(&phba->hbalock);
15523
James Smart7d791df2011-07-22 18:37:52 -040015524 next_fcf_pri = 0;
15525 /*
15526 * Clear the rr_bmask and set all of the bits that are at this
15527 * priority.
15528 */
15529 memset(phba->fcf.fcf_rr_bmask, 0,
15530 sizeof(*phba->fcf.fcf_rr_bmask));
15531 spin_lock_irq(&phba->hbalock);
15532 list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
15533 if (fcf_pri->fcf_rec.flag & LPFC_FCF_FLOGI_FAILED)
15534 continue;
15535 /*
15536 * the 1st priority that has not FLOGI failed
15537 * will be the highest.
15538 */
15539 if (!next_fcf_pri)
15540 next_fcf_pri = fcf_pri->fcf_rec.priority;
15541 spin_unlock_irq(&phba->hbalock);
15542 if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
15543 rc = lpfc_sli4_fcf_rr_index_set(phba,
15544 fcf_pri->fcf_rec.fcf_index);
15545 if (rc)
15546 return 0;
15547 }
15548 spin_lock_irq(&phba->hbalock);
15549 }
15550 /*
15551 * if next_fcf_pri was not set above and the list is not empty then
15552 * we have failed flogis on all of them. So reset flogi failed
Anatol Pomozov4907cb72012-09-01 10:31:09 -070015553 * and start at the beginning.
James Smart7d791df2011-07-22 18:37:52 -040015554 */
15555 if (!next_fcf_pri && !list_empty(&phba->fcf.fcf_pri_list)) {
15556 list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
15557 fcf_pri->fcf_rec.flag &= ~LPFC_FCF_FLOGI_FAILED;
15558 /*
15559 * the 1st priority that has not FLOGI failed
15560 * will be the highest.
15561 */
15562 if (!next_fcf_pri)
15563 next_fcf_pri = fcf_pri->fcf_rec.priority;
15564 spin_unlock_irq(&phba->hbalock);
15565 if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
15566 rc = lpfc_sli4_fcf_rr_index_set(phba,
15567 fcf_pri->fcf_rec.fcf_index);
15568 if (rc)
15569 return 0;
15570 }
15571 spin_lock_irq(&phba->hbalock);
15572 }
15573 } else
15574 ret = 1;
15575 spin_unlock_irq(&phba->hbalock);
15576
15577 return ret;
15578}
15579/**
James Smart0c9ab6f2010-02-26 14:15:57 -050015580 * lpfc_sli4_fcf_rr_next_index_get - Get next eligible fcf record index
15581 * @phba: pointer to lpfc hba data structure.
15582 *
15583 * This routine is to get the next eligible FCF record index in a round
15584 * robin fashion. If the next eligible FCF record index equals to the
James Smarta93ff372010-10-22 11:06:08 -040015585 * initial roundrobin FCF record index, LPFC_FCOE_FCF_NEXT_NONE (0xFFFF)
James Smart0c9ab6f2010-02-26 14:15:57 -050015586 * shall be returned, otherwise, the next eligible FCF record's index
15587 * shall be returned.
15588 **/
15589uint16_t
15590lpfc_sli4_fcf_rr_next_index_get(struct lpfc_hba *phba)
15591{
15592 uint16_t next_fcf_index;
15593
James Smart421c6622013-01-03 15:44:16 -050015594initial_priority:
James Smart3804dc82010-07-14 15:31:37 -040015595 /* Search start from next bit of currently registered FCF index */
James Smart421c6622013-01-03 15:44:16 -050015596 next_fcf_index = phba->fcf.current_rec.fcf_indx;
15597
James Smart7d791df2011-07-22 18:37:52 -040015598next_priority:
James Smart421c6622013-01-03 15:44:16 -050015599 /* Determine the next fcf index to check */
15600 next_fcf_index = (next_fcf_index + 1) % LPFC_SLI4_FCF_TBL_INDX_MAX;
James Smart0c9ab6f2010-02-26 14:15:57 -050015601 next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
15602 LPFC_SLI4_FCF_TBL_INDX_MAX,
James Smart3804dc82010-07-14 15:31:37 -040015603 next_fcf_index);
15604
James Smart0c9ab6f2010-02-26 14:15:57 -050015605 /* Wrap around condition on phba->fcf.fcf_rr_bmask */
James Smart7d791df2011-07-22 18:37:52 -040015606 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
15607 /*
15608 * If we have wrapped then we need to clear the bits that
15609 * have been tested so that we can detect when we should
15610 * change the priority level.
15611 */
James Smart0c9ab6f2010-02-26 14:15:57 -050015612 next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
15613 LPFC_SLI4_FCF_TBL_INDX_MAX, 0);
James Smart7d791df2011-07-22 18:37:52 -040015614 }
15615
James Smart0c9ab6f2010-02-26 14:15:57 -050015616
James Smart3804dc82010-07-14 15:31:37 -040015617 /* Check roundrobin failover list empty condition */
James Smart7d791df2011-07-22 18:37:52 -040015618 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX ||
15619 next_fcf_index == phba->fcf.current_rec.fcf_indx) {
15620 /*
15621 * If next fcf index is not found check if there are lower
15622 * Priority level fcf's in the fcf_priority list.
15623 * Set up the rr_bmask with all of the avaiable fcf bits
15624 * at that level and continue the selection process.
15625 */
15626 if (lpfc_check_next_fcf_pri_level(phba))
James Smart421c6622013-01-03 15:44:16 -050015627 goto initial_priority;
James Smart3804dc82010-07-14 15:31:37 -040015628 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
15629 "2844 No roundrobin failover FCF available\n");
James Smart7d791df2011-07-22 18:37:52 -040015630 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX)
15631 return LPFC_FCOE_FCF_NEXT_NONE;
15632 else {
15633 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
15634 "3063 Only FCF available idx %d, flag %x\n",
15635 next_fcf_index,
15636 phba->fcf.fcf_pri[next_fcf_index].fcf_rec.flag);
15637 return next_fcf_index;
15638 }
James Smart3804dc82010-07-14 15:31:37 -040015639 }
15640
James Smart7d791df2011-07-22 18:37:52 -040015641 if (next_fcf_index < LPFC_SLI4_FCF_TBL_INDX_MAX &&
15642 phba->fcf.fcf_pri[next_fcf_index].fcf_rec.flag &
15643 LPFC_FCF_FLOGI_FAILED)
15644 goto next_priority;
15645
James Smart3804dc82010-07-14 15:31:37 -040015646 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040015647 "2845 Get next roundrobin failover FCF (x%x)\n",
15648 next_fcf_index);
15649
James Smart0c9ab6f2010-02-26 14:15:57 -050015650 return next_fcf_index;
15651}
15652
15653/**
15654 * lpfc_sli4_fcf_rr_index_set - Set bmask with eligible fcf record index
15655 * @phba: pointer to lpfc hba data structure.
15656 *
15657 * This routine sets the FCF record index in to the eligible bmask for
James Smarta93ff372010-10-22 11:06:08 -040015658 * roundrobin failover search. It checks to make sure that the index
James Smart0c9ab6f2010-02-26 14:15:57 -050015659 * does not go beyond the range of the driver allocated bmask dimension
15660 * before setting the bit.
15661 *
15662 * Returns 0 if the index bit successfully set, otherwise, it returns
15663 * -EINVAL.
15664 **/
15665int
15666lpfc_sli4_fcf_rr_index_set(struct lpfc_hba *phba, uint16_t fcf_index)
15667{
15668 if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
15669 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040015670 "2610 FCF (x%x) reached driver's book "
15671 "keeping dimension:x%x\n",
James Smart0c9ab6f2010-02-26 14:15:57 -050015672 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
15673 return -EINVAL;
15674 }
15675 /* Set the eligible FCF record index bmask */
15676 set_bit(fcf_index, phba->fcf.fcf_rr_bmask);
15677
James Smart3804dc82010-07-14 15:31:37 -040015678 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040015679 "2790 Set FCF (x%x) to roundrobin FCF failover "
James Smart3804dc82010-07-14 15:31:37 -040015680 "bmask\n", fcf_index);
15681
James Smart0c9ab6f2010-02-26 14:15:57 -050015682 return 0;
15683}
15684
15685/**
James Smart3804dc82010-07-14 15:31:37 -040015686 * lpfc_sli4_fcf_rr_index_clear - Clear bmask from eligible fcf record index
James Smart0c9ab6f2010-02-26 14:15:57 -050015687 * @phba: pointer to lpfc hba data structure.
15688 *
15689 * This routine clears the FCF record index from the eligible bmask for
James Smarta93ff372010-10-22 11:06:08 -040015690 * roundrobin failover search. It checks to make sure that the index
James Smart0c9ab6f2010-02-26 14:15:57 -050015691 * does not go beyond the range of the driver allocated bmask dimension
15692 * before clearing the bit.
15693 **/
15694void
15695lpfc_sli4_fcf_rr_index_clear(struct lpfc_hba *phba, uint16_t fcf_index)
15696{
James Smart7d791df2011-07-22 18:37:52 -040015697 struct lpfc_fcf_pri *fcf_pri;
James Smart0c9ab6f2010-02-26 14:15:57 -050015698 if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
15699 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040015700 "2762 FCF (x%x) reached driver's book "
15701 "keeping dimension:x%x\n",
James Smart0c9ab6f2010-02-26 14:15:57 -050015702 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
15703 return;
15704 }
15705 /* Clear the eligible FCF record index bmask */
James Smart7d791df2011-07-22 18:37:52 -040015706 spin_lock_irq(&phba->hbalock);
15707 list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
15708 if (fcf_pri->fcf_rec.fcf_index == fcf_index) {
15709 list_del_init(&fcf_pri->list);
15710 break;
15711 }
15712 }
15713 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -050015714 clear_bit(fcf_index, phba->fcf.fcf_rr_bmask);
James Smart3804dc82010-07-14 15:31:37 -040015715
15716 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040015717 "2791 Clear FCF (x%x) from roundrobin failover "
James Smart3804dc82010-07-14 15:31:37 -040015718 "bmask\n", fcf_index);
James Smart0c9ab6f2010-02-26 14:15:57 -050015719}
15720
15721/**
James Smartecfd03c2010-02-12 14:41:27 -050015722 * lpfc_mbx_cmpl_redisc_fcf_table - completion routine for rediscover FCF table
15723 * @phba: pointer to lpfc hba data structure.
15724 *
15725 * This routine is the completion routine for the rediscover FCF table mailbox
15726 * command. If the mailbox command returned failure, it will try to stop the
15727 * FCF rediscover wait timer.
15728 **/
15729void
15730lpfc_mbx_cmpl_redisc_fcf_table(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
15731{
15732 struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
15733 uint32_t shdr_status, shdr_add_status;
15734
15735 redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
15736
15737 shdr_status = bf_get(lpfc_mbox_hdr_status,
15738 &redisc_fcf->header.cfg_shdr.response);
15739 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
15740 &redisc_fcf->header.cfg_shdr.response);
15741 if (shdr_status || shdr_add_status) {
James Smart0c9ab6f2010-02-26 14:15:57 -050015742 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smartecfd03c2010-02-12 14:41:27 -050015743 "2746 Requesting for FCF rediscovery failed "
15744 "status x%x add_status x%x\n",
15745 shdr_status, shdr_add_status);
James Smart0c9ab6f2010-02-26 14:15:57 -050015746 if (phba->fcf.fcf_flag & FCF_ACVL_DISC) {
James Smartfc2b9892010-02-26 14:15:29 -050015747 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -050015748 phba->fcf.fcf_flag &= ~FCF_ACVL_DISC;
James Smartfc2b9892010-02-26 14:15:29 -050015749 spin_unlock_irq(&phba->hbalock);
15750 /*
15751 * CVL event triggered FCF rediscover request failed,
15752 * last resort to re-try current registered FCF entry.
15753 */
15754 lpfc_retry_pport_discovery(phba);
15755 } else {
15756 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -050015757 phba->fcf.fcf_flag &= ~FCF_DEAD_DISC;
James Smartfc2b9892010-02-26 14:15:29 -050015758 spin_unlock_irq(&phba->hbalock);
15759 /*
15760 * DEAD FCF event triggered FCF rediscover request
15761 * failed, last resort to fail over as a link down
15762 * to FCF registration.
15763 */
15764 lpfc_sli4_fcf_dead_failthrough(phba);
15765 }
James Smart0c9ab6f2010-02-26 14:15:57 -050015766 } else {
15767 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040015768 "2775 Start FCF rediscover quiescent timer\n");
James Smartecfd03c2010-02-12 14:41:27 -050015769 /*
15770 * Start FCF rediscovery wait timer for pending FCF
15771 * before rescan FCF record table.
15772 */
15773 lpfc_fcf_redisc_wait_start_timer(phba);
James Smart0c9ab6f2010-02-26 14:15:57 -050015774 }
James Smartecfd03c2010-02-12 14:41:27 -050015775
15776 mempool_free(mbox, phba->mbox_mem_pool);
15777}
15778
15779/**
James Smart3804dc82010-07-14 15:31:37 -040015780 * lpfc_sli4_redisc_fcf_table - Request to rediscover entire FCF table by port.
James Smartecfd03c2010-02-12 14:41:27 -050015781 * @phba: pointer to lpfc hba data structure.
15782 *
15783 * This routine is invoked to request for rediscovery of the entire FCF table
15784 * by the port.
15785 **/
15786int
15787lpfc_sli4_redisc_fcf_table(struct lpfc_hba *phba)
15788{
15789 LPFC_MBOXQ_t *mbox;
15790 struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
15791 int rc, length;
15792
James Smart0c9ab6f2010-02-26 14:15:57 -050015793 /* Cancel retry delay timers to all vports before FCF rediscover */
15794 lpfc_cancel_all_vport_retry_delay_timer(phba);
15795
James Smartecfd03c2010-02-12 14:41:27 -050015796 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15797 if (!mbox) {
15798 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15799 "2745 Failed to allocate mbox for "
15800 "requesting FCF rediscover.\n");
15801 return -ENOMEM;
15802 }
15803
15804 length = (sizeof(struct lpfc_mbx_redisc_fcf_tbl) -
15805 sizeof(struct lpfc_sli4_cfg_mhdr));
15806 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15807 LPFC_MBOX_OPCODE_FCOE_REDISCOVER_FCF,
15808 length, LPFC_SLI4_MBX_EMBED);
15809
15810 redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
15811 /* Set count to 0 for invalidating the entire FCF database */
15812 bf_set(lpfc_mbx_redisc_fcf_count, redisc_fcf, 0);
15813
15814 /* Issue the mailbox command asynchronously */
15815 mbox->vport = phba->pport;
15816 mbox->mbox_cmpl = lpfc_mbx_cmpl_redisc_fcf_table;
15817 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
15818
15819 if (rc == MBX_NOT_FINISHED) {
15820 mempool_free(mbox, phba->mbox_mem_pool);
15821 return -EIO;
15822 }
15823 return 0;
15824}
15825
15826/**
James Smartfc2b9892010-02-26 14:15:29 -050015827 * lpfc_sli4_fcf_dead_failthrough - Failthrough routine to fcf dead event
15828 * @phba: pointer to lpfc hba data structure.
15829 *
15830 * This function is the failover routine as a last resort to the FCF DEAD
15831 * event when driver failed to perform fast FCF failover.
15832 **/
15833void
15834lpfc_sli4_fcf_dead_failthrough(struct lpfc_hba *phba)
15835{
15836 uint32_t link_state;
15837
15838 /*
15839 * Last resort as FCF DEAD event failover will treat this as
15840 * a link down, but save the link state because we don't want
15841 * it to be changed to Link Down unless it is already down.
15842 */
15843 link_state = phba->link_state;
15844 lpfc_linkdown(phba);
15845 phba->link_state = link_state;
15846
15847 /* Unregister FCF if no devices connected to it */
15848 lpfc_unregister_unused_fcf(phba);
15849}
15850
15851/**
James Smart026abb82011-12-13 13:20:45 -050015852 * lpfc_sli_get_config_region23 - Get sli3 port region 23 data.
James Smarta0c87cb2009-07-19 10:01:10 -040015853 * @phba: pointer to lpfc hba data structure.
James Smart026abb82011-12-13 13:20:45 -050015854 * @rgn23_data: pointer to configure region 23 data.
James Smarta0c87cb2009-07-19 10:01:10 -040015855 *
James Smart026abb82011-12-13 13:20:45 -050015856 * This function gets SLI3 port configure region 23 data through memory dump
15857 * mailbox command. When it successfully retrieves data, the size of the data
15858 * will be returned, otherwise, 0 will be returned.
James Smarta0c87cb2009-07-19 10:01:10 -040015859 **/
James Smart026abb82011-12-13 13:20:45 -050015860static uint32_t
15861lpfc_sli_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)
James Smarta0c87cb2009-07-19 10:01:10 -040015862{
15863 LPFC_MBOXQ_t *pmb = NULL;
15864 MAILBOX_t *mb;
James Smart026abb82011-12-13 13:20:45 -050015865 uint32_t offset = 0;
James Smarta0c87cb2009-07-19 10:01:10 -040015866 int rc;
15867
James Smart026abb82011-12-13 13:20:45 -050015868 if (!rgn23_data)
15869 return 0;
15870
James Smarta0c87cb2009-07-19 10:01:10 -040015871 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15872 if (!pmb) {
15873 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart026abb82011-12-13 13:20:45 -050015874 "2600 failed to allocate mailbox memory\n");
15875 return 0;
James Smarta0c87cb2009-07-19 10:01:10 -040015876 }
15877 mb = &pmb->u.mb;
15878
James Smarta0c87cb2009-07-19 10:01:10 -040015879 do {
15880 lpfc_dump_mem(phba, pmb, offset, DMP_REGION_23);
15881 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
15882
15883 if (rc != MBX_SUCCESS) {
15884 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smart026abb82011-12-13 13:20:45 -050015885 "2601 failed to read config "
15886 "region 23, rc 0x%x Status 0x%x\n",
15887 rc, mb->mbxStatus);
James Smarta0c87cb2009-07-19 10:01:10 -040015888 mb->un.varDmp.word_cnt = 0;
15889 }
15890 /*
15891 * dump mem may return a zero when finished or we got a
15892 * mailbox error, either way we are done.
15893 */
15894 if (mb->un.varDmp.word_cnt == 0)
15895 break;
15896 if (mb->un.varDmp.word_cnt > DMP_RGN23_SIZE - offset)
15897 mb->un.varDmp.word_cnt = DMP_RGN23_SIZE - offset;
15898
15899 lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
James Smart026abb82011-12-13 13:20:45 -050015900 rgn23_data + offset,
15901 mb->un.varDmp.word_cnt);
James Smarta0c87cb2009-07-19 10:01:10 -040015902 offset += mb->un.varDmp.word_cnt;
15903 } while (mb->un.varDmp.word_cnt && offset < DMP_RGN23_SIZE);
15904
James Smart026abb82011-12-13 13:20:45 -050015905 mempool_free(pmb, phba->mbox_mem_pool);
15906 return offset;
15907}
15908
15909/**
15910 * lpfc_sli4_get_config_region23 - Get sli4 port region 23 data.
15911 * @phba: pointer to lpfc hba data structure.
15912 * @rgn23_data: pointer to configure region 23 data.
15913 *
15914 * This function gets SLI4 port configure region 23 data through memory dump
15915 * mailbox command. When it successfully retrieves data, the size of the data
15916 * will be returned, otherwise, 0 will be returned.
15917 **/
15918static uint32_t
15919lpfc_sli4_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)
15920{
15921 LPFC_MBOXQ_t *mboxq = NULL;
15922 struct lpfc_dmabuf *mp = NULL;
15923 struct lpfc_mqe *mqe;
15924 uint32_t data_length = 0;
15925 int rc;
15926
15927 if (!rgn23_data)
15928 return 0;
15929
15930 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15931 if (!mboxq) {
15932 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15933 "3105 failed to allocate mailbox memory\n");
15934 return 0;
15935 }
15936
15937 if (lpfc_sli4_dump_cfg_rg23(phba, mboxq))
15938 goto out;
15939 mqe = &mboxq->u.mqe;
15940 mp = (struct lpfc_dmabuf *) mboxq->context1;
15941 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
15942 if (rc)
15943 goto out;
15944 data_length = mqe->un.mb_words[5];
15945 if (data_length == 0)
15946 goto out;
15947 if (data_length > DMP_RGN23_SIZE) {
15948 data_length = 0;
15949 goto out;
15950 }
15951 lpfc_sli_pcimem_bcopy((char *)mp->virt, rgn23_data, data_length);
15952out:
15953 mempool_free(mboxq, phba->mbox_mem_pool);
15954 if (mp) {
15955 lpfc_mbuf_free(phba, mp->virt, mp->phys);
15956 kfree(mp);
15957 }
15958 return data_length;
15959}
15960
15961/**
15962 * lpfc_sli_read_link_ste - Read region 23 to decide if link is disabled.
15963 * @phba: pointer to lpfc hba data structure.
15964 *
15965 * This function read region 23 and parse TLV for port status to
15966 * decide if the user disaled the port. If the TLV indicates the
15967 * port is disabled, the hba_flag is set accordingly.
15968 **/
15969void
15970lpfc_sli_read_link_ste(struct lpfc_hba *phba)
15971{
15972 uint8_t *rgn23_data = NULL;
15973 uint32_t if_type, data_size, sub_tlv_len, tlv_offset;
15974 uint32_t offset = 0;
15975
15976 /* Get adapter Region 23 data */
15977 rgn23_data = kzalloc(DMP_RGN23_SIZE, GFP_KERNEL);
15978 if (!rgn23_data)
15979 goto out;
15980
15981 if (phba->sli_rev < LPFC_SLI_REV4)
15982 data_size = lpfc_sli_get_config_region23(phba, rgn23_data);
15983 else {
15984 if_type = bf_get(lpfc_sli_intf_if_type,
15985 &phba->sli4_hba.sli_intf);
15986 if (if_type == LPFC_SLI_INTF_IF_TYPE_0)
15987 goto out;
15988 data_size = lpfc_sli4_get_config_region23(phba, rgn23_data);
15989 }
James Smarta0c87cb2009-07-19 10:01:10 -040015990
15991 if (!data_size)
15992 goto out;
15993
15994 /* Check the region signature first */
15995 if (memcmp(&rgn23_data[offset], LPFC_REGION23_SIGNATURE, 4)) {
15996 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15997 "2619 Config region 23 has bad signature\n");
15998 goto out;
15999 }
16000 offset += 4;
16001
16002 /* Check the data structure version */
16003 if (rgn23_data[offset] != LPFC_REGION23_VERSION) {
16004 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16005 "2620 Config region 23 has bad version\n");
16006 goto out;
16007 }
16008 offset += 4;
16009
16010 /* Parse TLV entries in the region */
16011 while (offset < data_size) {
16012 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC)
16013 break;
16014 /*
16015 * If the TLV is not driver specific TLV or driver id is
16016 * not linux driver id, skip the record.
16017 */
16018 if ((rgn23_data[offset] != DRIVER_SPECIFIC_TYPE) ||
16019 (rgn23_data[offset + 2] != LINUX_DRIVER_ID) ||
16020 (rgn23_data[offset + 3] != 0)) {
16021 offset += rgn23_data[offset + 1] * 4 + 4;
16022 continue;
16023 }
16024
16025 /* Driver found a driver specific TLV in the config region */
16026 sub_tlv_len = rgn23_data[offset + 1] * 4;
16027 offset += 4;
16028 tlv_offset = 0;
16029
16030 /*
16031 * Search for configured port state sub-TLV.
16032 */
16033 while ((offset < data_size) &&
16034 (tlv_offset < sub_tlv_len)) {
16035 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC) {
16036 offset += 4;
16037 tlv_offset += 4;
16038 break;
16039 }
16040 if (rgn23_data[offset] != PORT_STE_TYPE) {
16041 offset += rgn23_data[offset + 1] * 4 + 4;
16042 tlv_offset += rgn23_data[offset + 1] * 4 + 4;
16043 continue;
16044 }
16045
16046 /* This HBA contains PORT_STE configured */
16047 if (!rgn23_data[offset + 2])
16048 phba->hba_flag |= LINK_DISABLED;
16049
16050 goto out;
16051 }
16052 }
James Smart026abb82011-12-13 13:20:45 -050016053
James Smarta0c87cb2009-07-19 10:01:10 -040016054out:
James Smarta0c87cb2009-07-19 10:01:10 -040016055 kfree(rgn23_data);
16056 return;
16057}
James Smart695a8142010-01-26 23:08:03 -050016058
16059/**
James Smart52d52442011-05-24 11:42:45 -040016060 * lpfc_wr_object - write an object to the firmware
16061 * @phba: HBA structure that indicates port to create a queue on.
16062 * @dmabuf_list: list of dmabufs to write to the port.
16063 * @size: the total byte value of the objects to write to the port.
16064 * @offset: the current offset to be used to start the transfer.
16065 *
16066 * This routine will create a wr_object mailbox command to send to the port.
16067 * the mailbox command will be constructed using the dma buffers described in
16068 * @dmabuf_list to create a list of BDEs. This routine will fill in as many
16069 * BDEs that the imbedded mailbox can support. The @offset variable will be
16070 * used to indicate the starting offset of the transfer and will also return
16071 * the offset after the write object mailbox has completed. @size is used to
16072 * determine the end of the object and whether the eof bit should be set.
16073 *
16074 * Return 0 is successful and offset will contain the the new offset to use
16075 * for the next write.
16076 * Return negative value for error cases.
16077 **/
16078int
16079lpfc_wr_object(struct lpfc_hba *phba, struct list_head *dmabuf_list,
16080 uint32_t size, uint32_t *offset)
16081{
16082 struct lpfc_mbx_wr_object *wr_object;
16083 LPFC_MBOXQ_t *mbox;
16084 int rc = 0, i = 0;
16085 uint32_t shdr_status, shdr_add_status;
16086 uint32_t mbox_tmo;
16087 union lpfc_sli4_cfg_shdr *shdr;
16088 struct lpfc_dmabuf *dmabuf;
16089 uint32_t written = 0;
16090
16091 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16092 if (!mbox)
16093 return -ENOMEM;
16094
16095 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
16096 LPFC_MBOX_OPCODE_WRITE_OBJECT,
16097 sizeof(struct lpfc_mbx_wr_object) -
16098 sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
16099
16100 wr_object = (struct lpfc_mbx_wr_object *)&mbox->u.mqe.un.wr_object;
16101 wr_object->u.request.write_offset = *offset;
16102 sprintf((uint8_t *)wr_object->u.request.object_name, "/");
16103 wr_object->u.request.object_name[0] =
16104 cpu_to_le32(wr_object->u.request.object_name[0]);
16105 bf_set(lpfc_wr_object_eof, &wr_object->u.request, 0);
16106 list_for_each_entry(dmabuf, dmabuf_list, list) {
16107 if (i >= LPFC_MBX_WR_CONFIG_MAX_BDE || written >= size)
16108 break;
16109 wr_object->u.request.bde[i].addrLow = putPaddrLow(dmabuf->phys);
16110 wr_object->u.request.bde[i].addrHigh =
16111 putPaddrHigh(dmabuf->phys);
16112 if (written + SLI4_PAGE_SIZE >= size) {
16113 wr_object->u.request.bde[i].tus.f.bdeSize =
16114 (size - written);
16115 written += (size - written);
16116 bf_set(lpfc_wr_object_eof, &wr_object->u.request, 1);
16117 } else {
16118 wr_object->u.request.bde[i].tus.f.bdeSize =
16119 SLI4_PAGE_SIZE;
16120 written += SLI4_PAGE_SIZE;
16121 }
16122 i++;
16123 }
16124 wr_object->u.request.bde_count = i;
16125 bf_set(lpfc_wr_object_write_length, &wr_object->u.request, written);
16126 if (!phba->sli4_hba.intr_enable)
16127 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
16128 else {
James Smarta183a152011-10-10 21:32:43 -040016129 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart52d52442011-05-24 11:42:45 -040016130 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
16131 }
16132 /* The IOCTL status is embedded in the mailbox subheader. */
16133 shdr = (union lpfc_sli4_cfg_shdr *) &wr_object->header.cfg_shdr;
16134 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16135 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16136 if (rc != MBX_TIMEOUT)
16137 mempool_free(mbox, phba->mbox_mem_pool);
16138 if (shdr_status || shdr_add_status || rc) {
16139 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16140 "3025 Write Object mailbox failed with "
16141 "status x%x add_status x%x, mbx status x%x\n",
16142 shdr_status, shdr_add_status, rc);
16143 rc = -ENXIO;
16144 } else
16145 *offset += wr_object->u.response.actual_write_length;
16146 return rc;
16147}
16148
16149/**
James Smart695a8142010-01-26 23:08:03 -050016150 * lpfc_cleanup_pending_mbox - Free up vport discovery mailbox commands.
16151 * @vport: pointer to vport data structure.
16152 *
16153 * This function iterate through the mailboxq and clean up all REG_LOGIN
16154 * and REG_VPI mailbox commands associated with the vport. This function
16155 * is called when driver want to restart discovery of the vport due to
16156 * a Clear Virtual Link event.
16157 **/
16158void
16159lpfc_cleanup_pending_mbox(struct lpfc_vport *vport)
16160{
16161 struct lpfc_hba *phba = vport->phba;
16162 LPFC_MBOXQ_t *mb, *nextmb;
16163 struct lpfc_dmabuf *mp;
James Smart78730cf2010-04-06 15:06:30 -040016164 struct lpfc_nodelist *ndlp;
James Smartd439d282010-09-29 11:18:45 -040016165 struct lpfc_nodelist *act_mbx_ndlp = NULL;
James Smart589a52d2010-07-14 15:30:54 -040016166 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smartd439d282010-09-29 11:18:45 -040016167 LIST_HEAD(mbox_cmd_list);
James Smart63e801c2010-11-20 23:14:19 -050016168 uint8_t restart_loop;
James Smart695a8142010-01-26 23:08:03 -050016169
James Smartd439d282010-09-29 11:18:45 -040016170 /* Clean up internally queued mailbox commands with the vport */
James Smart695a8142010-01-26 23:08:03 -050016171 spin_lock_irq(&phba->hbalock);
16172 list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
16173 if (mb->vport != vport)
16174 continue;
16175
16176 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
16177 (mb->u.mb.mbxCommand != MBX_REG_VPI))
16178 continue;
16179
James Smartd439d282010-09-29 11:18:45 -040016180 list_del(&mb->list);
16181 list_add_tail(&mb->list, &mbox_cmd_list);
16182 }
16183 /* Clean up active mailbox command with the vport */
16184 mb = phba->sli.mbox_active;
16185 if (mb && (mb->vport == vport)) {
16186 if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) ||
16187 (mb->u.mb.mbxCommand == MBX_REG_VPI))
16188 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
16189 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
16190 act_mbx_ndlp = (struct lpfc_nodelist *)mb->context2;
16191 /* Put reference count for delayed processing */
16192 act_mbx_ndlp = lpfc_nlp_get(act_mbx_ndlp);
16193 /* Unregister the RPI when mailbox complete */
16194 mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
16195 }
16196 }
James Smart63e801c2010-11-20 23:14:19 -050016197 /* Cleanup any mailbox completions which are not yet processed */
16198 do {
16199 restart_loop = 0;
16200 list_for_each_entry(mb, &phba->sli.mboxq_cmpl, list) {
16201 /*
16202 * If this mailox is already processed or it is
16203 * for another vport ignore it.
16204 */
16205 if ((mb->vport != vport) ||
16206 (mb->mbox_flag & LPFC_MBX_IMED_UNREG))
16207 continue;
16208
16209 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
16210 (mb->u.mb.mbxCommand != MBX_REG_VPI))
16211 continue;
16212
16213 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
16214 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
16215 ndlp = (struct lpfc_nodelist *)mb->context2;
16216 /* Unregister the RPI when mailbox complete */
16217 mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
16218 restart_loop = 1;
16219 spin_unlock_irq(&phba->hbalock);
16220 spin_lock(shost->host_lock);
16221 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
16222 spin_unlock(shost->host_lock);
16223 spin_lock_irq(&phba->hbalock);
16224 break;
16225 }
16226 }
16227 } while (restart_loop);
16228
James Smartd439d282010-09-29 11:18:45 -040016229 spin_unlock_irq(&phba->hbalock);
16230
16231 /* Release the cleaned-up mailbox commands */
16232 while (!list_empty(&mbox_cmd_list)) {
16233 list_remove_head(&mbox_cmd_list, mb, LPFC_MBOXQ_t, list);
James Smart695a8142010-01-26 23:08:03 -050016234 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
16235 mp = (struct lpfc_dmabuf *) (mb->context1);
16236 if (mp) {
16237 __lpfc_mbuf_free(phba, mp->virt, mp->phys);
16238 kfree(mp);
16239 }
James Smart78730cf2010-04-06 15:06:30 -040016240 ndlp = (struct lpfc_nodelist *) mb->context2;
James Smartd439d282010-09-29 11:18:45 -040016241 mb->context2 = NULL;
James Smart78730cf2010-04-06 15:06:30 -040016242 if (ndlp) {
Dan Carpenterec21b3b2010-08-08 00:15:17 +020016243 spin_lock(shost->host_lock);
James Smart589a52d2010-07-14 15:30:54 -040016244 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
Dan Carpenterec21b3b2010-08-08 00:15:17 +020016245 spin_unlock(shost->host_lock);
James Smart78730cf2010-04-06 15:06:30 -040016246 lpfc_nlp_put(ndlp);
James Smart78730cf2010-04-06 15:06:30 -040016247 }
James Smart695a8142010-01-26 23:08:03 -050016248 }
James Smart695a8142010-01-26 23:08:03 -050016249 mempool_free(mb, phba->mbox_mem_pool);
16250 }
James Smartd439d282010-09-29 11:18:45 -040016251
16252 /* Release the ndlp with the cleaned-up active mailbox command */
16253 if (act_mbx_ndlp) {
16254 spin_lock(shost->host_lock);
16255 act_mbx_ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
16256 spin_unlock(shost->host_lock);
16257 lpfc_nlp_put(act_mbx_ndlp);
James Smart695a8142010-01-26 23:08:03 -050016258 }
James Smart695a8142010-01-26 23:08:03 -050016259}
16260
James Smart2a9bf3d2010-06-07 15:24:45 -040016261/**
16262 * lpfc_drain_txq - Drain the txq
16263 * @phba: Pointer to HBA context object.
16264 *
16265 * This function attempt to submit IOCBs on the txq
16266 * to the adapter. For SLI4 adapters, the txq contains
16267 * ELS IOCBs that have been deferred because the there
16268 * are no SGLs. This congestion can occur with large
16269 * vport counts during node discovery.
16270 **/
16271
16272uint32_t
16273lpfc_drain_txq(struct lpfc_hba *phba)
16274{
16275 LIST_HEAD(completions);
16276 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
16277 struct lpfc_iocbq *piocbq = 0;
16278 unsigned long iflags = 0;
16279 char *fail_msg = NULL;
16280 struct lpfc_sglq *sglq;
16281 union lpfc_wqe wqe;
James Smart0e9bb8d2013-03-01 16:35:12 -050016282 int txq_cnt = 0;
James Smart2a9bf3d2010-06-07 15:24:45 -040016283
16284 spin_lock_irqsave(&phba->hbalock, iflags);
James Smart0e9bb8d2013-03-01 16:35:12 -050016285 list_for_each_entry(piocbq, &pring->txq, list) {
16286 txq_cnt++;
16287 }
16288
16289 if (txq_cnt > pring->txq_max)
16290 pring->txq_max = txq_cnt;
James Smart2a9bf3d2010-06-07 15:24:45 -040016291
16292 spin_unlock_irqrestore(&phba->hbalock, iflags);
16293
James Smart0e9bb8d2013-03-01 16:35:12 -050016294 while (!list_empty(&pring->txq)) {
James Smart2a9bf3d2010-06-07 15:24:45 -040016295 spin_lock_irqsave(&phba->hbalock, iflags);
16296
James Smart19ca7602010-11-20 23:11:55 -050016297 piocbq = lpfc_sli_ringtx_get(phba, pring);
James Smarta6298522012-06-12 13:54:11 -040016298 if (!piocbq) {
16299 spin_unlock_irqrestore(&phba->hbalock, iflags);
16300 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
16301 "2823 txq empty and txq_cnt is %d\n ",
James Smart0e9bb8d2013-03-01 16:35:12 -050016302 txq_cnt);
James Smarta6298522012-06-12 13:54:11 -040016303 break;
16304 }
James Smart19ca7602010-11-20 23:11:55 -050016305 sglq = __lpfc_sli_get_sglq(phba, piocbq);
James Smart2a9bf3d2010-06-07 15:24:45 -040016306 if (!sglq) {
James Smart19ca7602010-11-20 23:11:55 -050016307 __lpfc_sli_ringtx_put(phba, pring, piocbq);
James Smart2a9bf3d2010-06-07 15:24:45 -040016308 spin_unlock_irqrestore(&phba->hbalock, iflags);
16309 break;
James Smart2a9bf3d2010-06-07 15:24:45 -040016310 }
James Smart0e9bb8d2013-03-01 16:35:12 -050016311 txq_cnt--;
James Smart2a9bf3d2010-06-07 15:24:45 -040016312
16313 /* The xri and iocb resources secured,
16314 * attempt to issue request
16315 */
James Smart6d368e52011-05-24 11:44:12 -040016316 piocbq->sli4_lxritag = sglq->sli4_lxritag;
James Smart2a9bf3d2010-06-07 15:24:45 -040016317 piocbq->sli4_xritag = sglq->sli4_xritag;
16318 if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocbq, sglq))
16319 fail_msg = "to convert bpl to sgl";
16320 else if (lpfc_sli4_iocb2wqe(phba, piocbq, &wqe))
16321 fail_msg = "to convert iocb to wqe";
16322 else if (lpfc_sli4_wq_put(phba->sli4_hba.els_wq, &wqe))
16323 fail_msg = " - Wq is full";
16324 else
16325 lpfc_sli_ringtxcmpl_put(phba, pring, piocbq);
16326
16327 if (fail_msg) {
16328 /* Failed means we can't issue and need to cancel */
16329 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
16330 "2822 IOCB failed %s iotag 0x%x "
16331 "xri 0x%x\n",
16332 fail_msg,
16333 piocbq->iotag, piocbq->sli4_xritag);
16334 list_add_tail(&piocbq->list, &completions);
16335 }
16336 spin_unlock_irqrestore(&phba->hbalock, iflags);
16337 }
16338
James Smart2a9bf3d2010-06-07 15:24:45 -040016339 /* Cancel all the IOCBs that cannot be issued */
16340 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
16341 IOERR_SLI_ABORTED);
16342
James Smart0e9bb8d2013-03-01 16:35:12 -050016343 return txq_cnt;
James Smart2a9bf3d2010-06-07 15:24:45 -040016344}