blob: 296a6f8473e0007b3f0f738764eb10fc3b13c9a8 [file] [log] [blame]
dea31012005-04-17 16:05:31 -05001/*******************************************************************
2 * This file is part of the Emulex Linux Device Driver for *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04003 * Fibre Channel Host Bus Adapters. *
James Smartbdcd2b92012-03-01 22:33:52 -05004 * Copyright (C) 2004-2012 Emulex. All rights reserved. *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04005 * EMULEX and SLI are trademarks of Emulex. *
dea31012005-04-17 16:05:31 -05006 * www.emulex.com *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04007 * Portions Copyright (C) 2004-2005 Christoph Hellwig *
dea31012005-04-17 16:05:31 -05008 * *
9 * This program is free software; you can redistribute it and/or *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -040010 * modify it under the terms of version 2 of the GNU General *
11 * Public License as published by the Free Software Foundation. *
12 * This program is distributed in the hope that it will be useful. *
13 * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND *
14 * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, *
15 * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE *
16 * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
17 * TO BE LEGALLY INVALID. See the GNU General Public License for *
18 * more details, a copy of which can be found in the file COPYING *
19 * included with this package. *
dea31012005-04-17 16:05:31 -050020 *******************************************************************/
21
dea31012005-04-17 16:05:31 -050022#include <linux/blkdev.h>
23#include <linux/pci.h>
24#include <linux/interrupt.h>
25#include <linux/delay.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090026#include <linux/slab.h>
dea31012005-04-17 16:05:31 -050027
James.Smart@Emulex.Com91886522005-08-10 15:03:09 -040028#include <scsi/scsi.h>
dea31012005-04-17 16:05:31 -050029#include <scsi/scsi_cmnd.h>
30#include <scsi/scsi_device.h>
31#include <scsi/scsi_host.h>
James.Smart@Emulex.Comf888ba32005-08-10 15:03:01 -040032#include <scsi/scsi_transport_fc.h>
James Smartda0436e2009-05-22 14:51:39 -040033#include <scsi/fc/fc_fs.h>
James Smart0d878412009-10-02 15:16:56 -040034#include <linux/aer.h>
dea31012005-04-17 16:05:31 -050035
James Smartda0436e2009-05-22 14:51:39 -040036#include "lpfc_hw4.h"
dea31012005-04-17 16:05:31 -050037#include "lpfc_hw.h"
38#include "lpfc_sli.h"
James Smartda0436e2009-05-22 14:51:39 -040039#include "lpfc_sli4.h"
James Smartea2151b2008-09-07 11:52:10 -040040#include "lpfc_nl.h"
dea31012005-04-17 16:05:31 -050041#include "lpfc_disc.h"
42#include "lpfc_scsi.h"
43#include "lpfc.h"
44#include "lpfc_crtn.h"
45#include "lpfc_logmsg.h"
46#include "lpfc_compat.h"
James Smart858c9f62007-06-17 19:56:39 -050047#include "lpfc_debugfs.h"
James Smart04c68492009-05-22 14:52:52 -040048#include "lpfc_vport.h"
dea31012005-04-17 16:05:31 -050049
50/* There are only four IOCB completion types. */
51typedef enum _lpfc_iocb_type {
52 LPFC_UNKNOWN_IOCB,
53 LPFC_UNSOL_IOCB,
54 LPFC_SOL_IOCB,
55 LPFC_ABORT_IOCB
56} lpfc_iocb_type;
57
James Smart4f774512009-05-22 14:52:35 -040058
59/* Provide function prototypes local to this module. */
60static int lpfc_sli_issue_mbox_s4(struct lpfc_hba *, LPFC_MBOXQ_t *,
61 uint32_t);
62static int lpfc_sli4_read_rev(struct lpfc_hba *, LPFC_MBOXQ_t *,
James Smart45ed1192009-10-02 15:17:02 -040063 uint8_t *, uint32_t *);
64static struct lpfc_iocbq *lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *,
65 struct lpfc_iocbq *);
James Smart6669f9b2009-10-02 15:16:45 -040066static void lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *,
67 struct hbq_dmabuf *);
James Smart05580562011-05-24 11:40:48 -040068static int lpfc_sli4_fp_handle_wcqe(struct lpfc_hba *, struct lpfc_queue *,
69 struct lpfc_cqe *);
James Smart8a9d2e82012-05-09 21:16:12 -040070static int lpfc_sli4_post_els_sgl_list(struct lpfc_hba *, struct list_head *,
71 int);
James Smart05580562011-05-24 11:40:48 -040072
James Smart4f774512009-05-22 14:52:35 -040073static IOCB_t *
74lpfc_get_iocb_from_iocbq(struct lpfc_iocbq *iocbq)
75{
76 return &iocbq->iocb;
77}
78
79/**
80 * lpfc_sli4_wq_put - Put a Work Queue Entry on an Work Queue
81 * @q: The Work Queue to operate on.
82 * @wqe: The work Queue Entry to put on the Work queue.
83 *
84 * This routine will copy the contents of @wqe to the next available entry on
85 * the @q. This function will then ring the Work Queue Doorbell to signal the
86 * HBA to start processing the Work Queue Entry. This function returns 0 if
87 * successful. If no entries are available on @q then this function will return
88 * -ENOMEM.
89 * The caller is expected to hold the hbalock when calling this routine.
90 **/
91static uint32_t
92lpfc_sli4_wq_put(struct lpfc_queue *q, union lpfc_wqe *wqe)
93{
James Smart2e90f4b2011-12-13 13:22:37 -050094 union lpfc_wqe *temp_wqe;
James Smart4f774512009-05-22 14:52:35 -040095 struct lpfc_register doorbell;
96 uint32_t host_index;
97
James Smart2e90f4b2011-12-13 13:22:37 -050098 /* sanity check on queue memory */
99 if (unlikely(!q))
100 return -ENOMEM;
101 temp_wqe = q->qe[q->host_index].wqe;
102
James Smart4f774512009-05-22 14:52:35 -0400103 /* If the host has not yet processed the next entry then we are done */
James Smartb84daac2012-08-03 12:35:13 -0400104 if (((q->host_index + 1) % q->entry_count) == q->hba_index) {
105 q->WQ_overflow++;
James Smart4f774512009-05-22 14:52:35 -0400106 return -ENOMEM;
James Smartb84daac2012-08-03 12:35:13 -0400107 }
108 q->WQ_posted++;
James Smart4f774512009-05-22 14:52:35 -0400109 /* set consumption flag every once in a while */
James Smartff78d8f2011-12-13 13:21:35 -0500110 if (!((q->host_index + 1) % q->entry_repost))
James Smartf0d9bcc2010-10-22 11:07:09 -0400111 bf_set(wqe_wqec, &wqe->generic.wqe_com, 1);
James Smartfedd3b72011-02-16 12:39:24 -0500112 if (q->phba->sli3_options & LPFC_SLI4_PHWQ_ENABLED)
113 bf_set(wqe_wqid, &wqe->generic.wqe_com, q->queue_id);
James Smart4f774512009-05-22 14:52:35 -0400114 lpfc_sli_pcimem_bcopy(wqe, temp_wqe, q->entry_size);
115
116 /* Update the host index before invoking device */
117 host_index = q->host_index;
118 q->host_index = ((q->host_index + 1) % q->entry_count);
119
120 /* Ring Doorbell */
121 doorbell.word0 = 0;
122 bf_set(lpfc_wq_doorbell_num_posted, &doorbell, 1);
123 bf_set(lpfc_wq_doorbell_index, &doorbell, host_index);
124 bf_set(lpfc_wq_doorbell_id, &doorbell, q->queue_id);
125 writel(doorbell.word0, q->phba->sli4_hba.WQDBregaddr);
126 readl(q->phba->sli4_hba.WQDBregaddr); /* Flush */
127
128 return 0;
129}
130
131/**
132 * lpfc_sli4_wq_release - Updates internal hba index for WQ
133 * @q: The Work Queue to operate on.
134 * @index: The index to advance the hba index to.
135 *
136 * This routine will update the HBA index of a queue to reflect consumption of
137 * Work Queue Entries by the HBA. When the HBA indicates that it has consumed
138 * an entry the host calls this function to update the queue's internal
139 * pointers. This routine returns the number of entries that were consumed by
140 * the HBA.
141 **/
142static uint32_t
143lpfc_sli4_wq_release(struct lpfc_queue *q, uint32_t index)
144{
145 uint32_t released = 0;
146
James Smart2e90f4b2011-12-13 13:22:37 -0500147 /* sanity check on queue memory */
148 if (unlikely(!q))
149 return 0;
150
James Smart4f774512009-05-22 14:52:35 -0400151 if (q->hba_index == index)
152 return 0;
153 do {
154 q->hba_index = ((q->hba_index + 1) % q->entry_count);
155 released++;
156 } while (q->hba_index != index);
157 return released;
158}
159
160/**
161 * lpfc_sli4_mq_put - Put a Mailbox Queue Entry on an Mailbox Queue
162 * @q: The Mailbox Queue to operate on.
163 * @wqe: The Mailbox Queue Entry to put on the Work queue.
164 *
165 * This routine will copy the contents of @mqe to the next available entry on
166 * the @q. This function will then ring the Work Queue Doorbell to signal the
167 * HBA to start processing the Work Queue Entry. This function returns 0 if
168 * successful. If no entries are available on @q then this function will return
169 * -ENOMEM.
170 * The caller is expected to hold the hbalock when calling this routine.
171 **/
172static uint32_t
173lpfc_sli4_mq_put(struct lpfc_queue *q, struct lpfc_mqe *mqe)
174{
James Smart2e90f4b2011-12-13 13:22:37 -0500175 struct lpfc_mqe *temp_mqe;
James Smart4f774512009-05-22 14:52:35 -0400176 struct lpfc_register doorbell;
177 uint32_t host_index;
178
James Smart2e90f4b2011-12-13 13:22:37 -0500179 /* sanity check on queue memory */
180 if (unlikely(!q))
181 return -ENOMEM;
182 temp_mqe = q->qe[q->host_index].mqe;
183
James Smart4f774512009-05-22 14:52:35 -0400184 /* If the host has not yet processed the next entry then we are done */
185 if (((q->host_index + 1) % q->entry_count) == q->hba_index)
186 return -ENOMEM;
187 lpfc_sli_pcimem_bcopy(mqe, temp_mqe, q->entry_size);
188 /* Save off the mailbox pointer for completion */
189 q->phba->mbox = (MAILBOX_t *)temp_mqe;
190
191 /* Update the host index before invoking device */
192 host_index = q->host_index;
193 q->host_index = ((q->host_index + 1) % q->entry_count);
194
195 /* Ring Doorbell */
196 doorbell.word0 = 0;
197 bf_set(lpfc_mq_doorbell_num_posted, &doorbell, 1);
198 bf_set(lpfc_mq_doorbell_id, &doorbell, q->queue_id);
199 writel(doorbell.word0, q->phba->sli4_hba.MQDBregaddr);
200 readl(q->phba->sli4_hba.MQDBregaddr); /* Flush */
201 return 0;
202}
203
204/**
205 * lpfc_sli4_mq_release - Updates internal hba index for MQ
206 * @q: The Mailbox Queue to operate on.
207 *
208 * This routine will update the HBA index of a queue to reflect consumption of
209 * a Mailbox Queue Entry by the HBA. When the HBA indicates that it has consumed
210 * an entry the host calls this function to update the queue's internal
211 * pointers. This routine returns the number of entries that were consumed by
212 * the HBA.
213 **/
214static uint32_t
215lpfc_sli4_mq_release(struct lpfc_queue *q)
216{
James Smart2e90f4b2011-12-13 13:22:37 -0500217 /* sanity check on queue memory */
218 if (unlikely(!q))
219 return 0;
220
James Smart4f774512009-05-22 14:52:35 -0400221 /* Clear the mailbox pointer for completion */
222 q->phba->mbox = NULL;
223 q->hba_index = ((q->hba_index + 1) % q->entry_count);
224 return 1;
225}
226
227/**
228 * lpfc_sli4_eq_get - Gets the next valid EQE from a EQ
229 * @q: The Event Queue to get the first valid EQE from
230 *
231 * This routine will get the first valid Event Queue Entry from @q, update
232 * the queue's internal hba index, and return the EQE. If no valid EQEs are in
233 * the Queue (no more work to do), or the Queue is full of EQEs that have been
234 * processed, but not popped back to the HBA then this routine will return NULL.
235 **/
236static struct lpfc_eqe *
237lpfc_sli4_eq_get(struct lpfc_queue *q)
238{
James Smart2e90f4b2011-12-13 13:22:37 -0500239 struct lpfc_eqe *eqe;
240
241 /* sanity check on queue memory */
242 if (unlikely(!q))
243 return NULL;
244 eqe = q->qe[q->hba_index].eqe;
James Smart4f774512009-05-22 14:52:35 -0400245
246 /* If the next EQE is not valid then we are done */
James Smartcb5172e2010-03-15 11:25:07 -0400247 if (!bf_get_le32(lpfc_eqe_valid, eqe))
James Smart4f774512009-05-22 14:52:35 -0400248 return NULL;
249 /* If the host has not yet processed the next entry then we are done */
250 if (((q->hba_index + 1) % q->entry_count) == q->host_index)
251 return NULL;
252
253 q->hba_index = ((q->hba_index + 1) % q->entry_count);
254 return eqe;
255}
256
257/**
258 * lpfc_sli4_eq_release - Indicates the host has finished processing an EQ
259 * @q: The Event Queue that the host has completed processing for.
260 * @arm: Indicates whether the host wants to arms this CQ.
261 *
262 * This routine will mark all Event Queue Entries on @q, from the last
263 * known completed entry to the last entry that was processed, as completed
264 * by clearing the valid bit for each completion queue entry. Then it will
265 * notify the HBA, by ringing the doorbell, that the EQEs have been processed.
266 * The internal host index in the @q will be updated by this routine to indicate
267 * that the host has finished processing the entries. The @arm parameter
268 * indicates that the queue should be rearmed when ringing the doorbell.
269 *
270 * This function will return the number of EQEs that were popped.
271 **/
272uint32_t
273lpfc_sli4_eq_release(struct lpfc_queue *q, bool arm)
274{
275 uint32_t released = 0;
276 struct lpfc_eqe *temp_eqe;
277 struct lpfc_register doorbell;
278
James Smart2e90f4b2011-12-13 13:22:37 -0500279 /* sanity check on queue memory */
280 if (unlikely(!q))
281 return 0;
282
James Smart4f774512009-05-22 14:52:35 -0400283 /* while there are valid entries */
284 while (q->hba_index != q->host_index) {
285 temp_eqe = q->qe[q->host_index].eqe;
James Smartcb5172e2010-03-15 11:25:07 -0400286 bf_set_le32(lpfc_eqe_valid, temp_eqe, 0);
James Smart4f774512009-05-22 14:52:35 -0400287 released++;
288 q->host_index = ((q->host_index + 1) % q->entry_count);
289 }
290 if (unlikely(released == 0 && !arm))
291 return 0;
292
293 /* ring doorbell for number popped */
294 doorbell.word0 = 0;
295 if (arm) {
296 bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);
297 bf_set(lpfc_eqcq_doorbell_eqci, &doorbell, 1);
298 }
299 bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, released);
300 bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_EVENT);
James Smart6b5151f2012-01-18 16:24:06 -0500301 bf_set(lpfc_eqcq_doorbell_eqid_hi, &doorbell,
302 (q->queue_id >> LPFC_EQID_HI_FIELD_SHIFT));
303 bf_set(lpfc_eqcq_doorbell_eqid_lo, &doorbell, q->queue_id);
James Smart4f774512009-05-22 14:52:35 -0400304 writel(doorbell.word0, q->phba->sli4_hba.EQCQDBregaddr);
James Smarta747c9c2009-11-18 15:41:10 -0500305 /* PCI read to flush PCI pipeline on re-arming for INTx mode */
306 if ((q->phba->intr_type == INTx) && (arm == LPFC_QUEUE_REARM))
307 readl(q->phba->sli4_hba.EQCQDBregaddr);
James Smart4f774512009-05-22 14:52:35 -0400308 return released;
309}
310
311/**
312 * lpfc_sli4_cq_get - Gets the next valid CQE from a CQ
313 * @q: The Completion Queue to get the first valid CQE from
314 *
315 * This routine will get the first valid Completion Queue Entry from @q, update
316 * the queue's internal hba index, and return the CQE. If no valid CQEs are in
317 * the Queue (no more work to do), or the Queue is full of CQEs that have been
318 * processed, but not popped back to the HBA then this routine will return NULL.
319 **/
320static struct lpfc_cqe *
321lpfc_sli4_cq_get(struct lpfc_queue *q)
322{
323 struct lpfc_cqe *cqe;
324
James Smart2e90f4b2011-12-13 13:22:37 -0500325 /* sanity check on queue memory */
326 if (unlikely(!q))
327 return NULL;
328
James Smart4f774512009-05-22 14:52:35 -0400329 /* If the next CQE is not valid then we are done */
James Smartcb5172e2010-03-15 11:25:07 -0400330 if (!bf_get_le32(lpfc_cqe_valid, q->qe[q->hba_index].cqe))
James Smart4f774512009-05-22 14:52:35 -0400331 return NULL;
332 /* If the host has not yet processed the next entry then we are done */
333 if (((q->hba_index + 1) % q->entry_count) == q->host_index)
334 return NULL;
335
336 cqe = q->qe[q->hba_index].cqe;
337 q->hba_index = ((q->hba_index + 1) % q->entry_count);
338 return cqe;
339}
340
341/**
342 * lpfc_sli4_cq_release - Indicates the host has finished processing a CQ
343 * @q: The Completion Queue that the host has completed processing for.
344 * @arm: Indicates whether the host wants to arms this CQ.
345 *
346 * This routine will mark all Completion queue entries on @q, from the last
347 * known completed entry to the last entry that was processed, as completed
348 * by clearing the valid bit for each completion queue entry. Then it will
349 * notify the HBA, by ringing the doorbell, that the CQEs have been processed.
350 * The internal host index in the @q will be updated by this routine to indicate
351 * that the host has finished processing the entries. The @arm parameter
352 * indicates that the queue should be rearmed when ringing the doorbell.
353 *
354 * This function will return the number of CQEs that were released.
355 **/
356uint32_t
357lpfc_sli4_cq_release(struct lpfc_queue *q, bool arm)
358{
359 uint32_t released = 0;
360 struct lpfc_cqe *temp_qe;
361 struct lpfc_register doorbell;
362
James Smart2e90f4b2011-12-13 13:22:37 -0500363 /* sanity check on queue memory */
364 if (unlikely(!q))
365 return 0;
James Smart4f774512009-05-22 14:52:35 -0400366 /* while there are valid entries */
367 while (q->hba_index != q->host_index) {
368 temp_qe = q->qe[q->host_index].cqe;
James Smartcb5172e2010-03-15 11:25:07 -0400369 bf_set_le32(lpfc_cqe_valid, temp_qe, 0);
James Smart4f774512009-05-22 14:52:35 -0400370 released++;
371 q->host_index = ((q->host_index + 1) % q->entry_count);
372 }
373 if (unlikely(released == 0 && !arm))
374 return 0;
375
376 /* ring doorbell for number popped */
377 doorbell.word0 = 0;
378 if (arm)
379 bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);
380 bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, released);
381 bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_COMPLETION);
James Smart6b5151f2012-01-18 16:24:06 -0500382 bf_set(lpfc_eqcq_doorbell_cqid_hi, &doorbell,
383 (q->queue_id >> LPFC_CQID_HI_FIELD_SHIFT));
384 bf_set(lpfc_eqcq_doorbell_cqid_lo, &doorbell, q->queue_id);
James Smart4f774512009-05-22 14:52:35 -0400385 writel(doorbell.word0, q->phba->sli4_hba.EQCQDBregaddr);
386 return released;
387}
388
389/**
390 * lpfc_sli4_rq_put - Put a Receive Buffer Queue Entry on a Receive Queue
391 * @q: The Header Receive Queue to operate on.
392 * @wqe: The Receive Queue Entry to put on the Receive queue.
393 *
394 * This routine will copy the contents of @wqe to the next available entry on
395 * the @q. This function will then ring the Receive Queue Doorbell to signal the
396 * HBA to start processing the Receive Queue Entry. This function returns the
397 * index that the rqe was copied to if successful. If no entries are available
398 * on @q then this function will return -ENOMEM.
399 * The caller is expected to hold the hbalock when calling this routine.
400 **/
401static int
402lpfc_sli4_rq_put(struct lpfc_queue *hq, struct lpfc_queue *dq,
403 struct lpfc_rqe *hrqe, struct lpfc_rqe *drqe)
404{
James Smart2e90f4b2011-12-13 13:22:37 -0500405 struct lpfc_rqe *temp_hrqe;
406 struct lpfc_rqe *temp_drqe;
James Smart4f774512009-05-22 14:52:35 -0400407 struct lpfc_register doorbell;
408 int put_index = hq->host_index;
409
James Smart2e90f4b2011-12-13 13:22:37 -0500410 /* sanity check on queue memory */
411 if (unlikely(!hq) || unlikely(!dq))
412 return -ENOMEM;
413 temp_hrqe = hq->qe[hq->host_index].rqe;
414 temp_drqe = dq->qe[dq->host_index].rqe;
415
James Smart4f774512009-05-22 14:52:35 -0400416 if (hq->type != LPFC_HRQ || dq->type != LPFC_DRQ)
417 return -EINVAL;
418 if (hq->host_index != dq->host_index)
419 return -EINVAL;
420 /* If the host has not yet processed the next entry then we are done */
421 if (((hq->host_index + 1) % hq->entry_count) == hq->hba_index)
422 return -EBUSY;
423 lpfc_sli_pcimem_bcopy(hrqe, temp_hrqe, hq->entry_size);
424 lpfc_sli_pcimem_bcopy(drqe, temp_drqe, dq->entry_size);
425
426 /* Update the host index to point to the next slot */
427 hq->host_index = ((hq->host_index + 1) % hq->entry_count);
428 dq->host_index = ((dq->host_index + 1) % dq->entry_count);
429
430 /* Ring The Header Receive Queue Doorbell */
James Smart73d91e52011-10-10 21:32:10 -0400431 if (!(hq->host_index % hq->entry_repost)) {
James Smart4f774512009-05-22 14:52:35 -0400432 doorbell.word0 = 0;
433 bf_set(lpfc_rq_doorbell_num_posted, &doorbell,
James Smart73d91e52011-10-10 21:32:10 -0400434 hq->entry_repost);
James Smart4f774512009-05-22 14:52:35 -0400435 bf_set(lpfc_rq_doorbell_id, &doorbell, hq->queue_id);
436 writel(doorbell.word0, hq->phba->sli4_hba.RQDBregaddr);
437 }
438 return put_index;
439}
440
441/**
442 * lpfc_sli4_rq_release - Updates internal hba index for RQ
443 * @q: The Header Receive Queue to operate on.
444 *
445 * This routine will update the HBA index of a queue to reflect consumption of
446 * one Receive Queue Entry by the HBA. When the HBA indicates that it has
447 * consumed an entry the host calls this function to update the queue's
448 * internal pointers. This routine returns the number of entries that were
449 * consumed by the HBA.
450 **/
451static uint32_t
452lpfc_sli4_rq_release(struct lpfc_queue *hq, struct lpfc_queue *dq)
453{
James Smart2e90f4b2011-12-13 13:22:37 -0500454 /* sanity check on queue memory */
455 if (unlikely(!hq) || unlikely(!dq))
456 return 0;
457
James Smart4f774512009-05-22 14:52:35 -0400458 if ((hq->type != LPFC_HRQ) || (dq->type != LPFC_DRQ))
459 return 0;
460 hq->hba_index = ((hq->hba_index + 1) % hq->entry_count);
461 dq->hba_index = ((dq->hba_index + 1) % dq->entry_count);
462 return 1;
463}
464
James Smarte59058c2008-08-24 21:49:00 -0400465/**
James Smart3621a712009-04-06 18:47:14 -0400466 * lpfc_cmd_iocb - Get next command iocb entry in the ring
James Smarte59058c2008-08-24 21:49:00 -0400467 * @phba: Pointer to HBA context object.
468 * @pring: Pointer to driver SLI ring object.
469 *
470 * This function returns pointer to next command iocb entry
471 * in the command ring. The caller must hold hbalock to prevent
472 * other threads consume the next command iocb.
473 * SLI-2/SLI-3 provide different sized iocbs.
474 **/
James Smarted957682007-06-17 19:56:37 -0500475static inline IOCB_t *
476lpfc_cmd_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
477{
478 return (IOCB_t *) (((char *) pring->cmdringaddr) +
479 pring->cmdidx * phba->iocb_cmd_size);
480}
481
James Smarte59058c2008-08-24 21:49:00 -0400482/**
James Smart3621a712009-04-06 18:47:14 -0400483 * lpfc_resp_iocb - Get next response iocb entry in the ring
James Smarte59058c2008-08-24 21:49:00 -0400484 * @phba: Pointer to HBA context object.
485 * @pring: Pointer to driver SLI ring object.
486 *
487 * This function returns pointer to next response iocb entry
488 * in the response ring. The caller must hold hbalock to make sure
489 * that no other thread consume the next response iocb.
490 * SLI-2/SLI-3 provide different sized iocbs.
491 **/
James Smarted957682007-06-17 19:56:37 -0500492static inline IOCB_t *
493lpfc_resp_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
494{
495 return (IOCB_t *) (((char *) pring->rspringaddr) +
496 pring->rspidx * phba->iocb_rsp_size);
497}
498
James Smarte59058c2008-08-24 21:49:00 -0400499/**
James Smart3621a712009-04-06 18:47:14 -0400500 * __lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
James Smarte59058c2008-08-24 21:49:00 -0400501 * @phba: Pointer to HBA context object.
502 *
503 * This function is called with hbalock held. This function
504 * allocates a new driver iocb object from the iocb pool. If the
505 * allocation is successful, it returns pointer to the newly
506 * allocated iocb object else it returns NULL.
507 **/
James Smart4f2e66c2012-05-09 21:17:07 -0400508struct lpfc_iocbq *
James Smart2e0fef82007-06-17 19:56:36 -0500509__lpfc_sli_get_iocbq(struct lpfc_hba *phba)
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400510{
511 struct list_head *lpfc_iocb_list = &phba->lpfc_iocb_list;
512 struct lpfc_iocbq * iocbq = NULL;
513
514 list_remove_head(lpfc_iocb_list, iocbq, struct lpfc_iocbq, list);
James Smart2a9bf3d2010-06-07 15:24:45 -0400515 if (iocbq)
516 phba->iocb_cnt++;
517 if (phba->iocb_cnt > phba->iocb_max)
518 phba->iocb_max = phba->iocb_cnt;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400519 return iocbq;
520}
521
James Smarte59058c2008-08-24 21:49:00 -0400522/**
James Smartda0436e2009-05-22 14:51:39 -0400523 * __lpfc_clear_active_sglq - Remove the active sglq for this XRI.
524 * @phba: Pointer to HBA context object.
525 * @xritag: XRI value.
526 *
527 * This function clears the sglq pointer from the array of acive
528 * sglq's. The xritag that is passed in is used to index into the
529 * array. Before the xritag can be used it needs to be adjusted
530 * by subtracting the xribase.
531 *
532 * Returns sglq ponter = success, NULL = Failure.
533 **/
534static struct lpfc_sglq *
535__lpfc_clear_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
536{
James Smartda0436e2009-05-22 14:51:39 -0400537 struct lpfc_sglq *sglq;
James Smart6d368e52011-05-24 11:44:12 -0400538
539 sglq = phba->sli4_hba.lpfc_sglq_active_list[xritag];
540 phba->sli4_hba.lpfc_sglq_active_list[xritag] = NULL;
James Smartda0436e2009-05-22 14:51:39 -0400541 return sglq;
542}
543
544/**
545 * __lpfc_get_active_sglq - Get the active sglq for this XRI.
546 * @phba: Pointer to HBA context object.
547 * @xritag: XRI value.
548 *
549 * This function returns the sglq pointer from the array of acive
550 * sglq's. The xritag that is passed in is used to index into the
551 * array. Before the xritag can be used it needs to be adjusted
552 * by subtracting the xribase.
553 *
554 * Returns sglq ponter = success, NULL = Failure.
555 **/
James Smart0f65ff62010-02-26 14:14:23 -0500556struct lpfc_sglq *
James Smartda0436e2009-05-22 14:51:39 -0400557__lpfc_get_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
558{
James Smartda0436e2009-05-22 14:51:39 -0400559 struct lpfc_sglq *sglq;
James Smart6d368e52011-05-24 11:44:12 -0400560
561 sglq = phba->sli4_hba.lpfc_sglq_active_list[xritag];
James Smartda0436e2009-05-22 14:51:39 -0400562 return sglq;
563}
564
565/**
James Smart1151e3e2011-02-16 12:39:35 -0500566 * lpfc_clr_rrq_active - Clears RRQ active bit in xri_bitmap.
James Smart19ca7602010-11-20 23:11:55 -0500567 * @phba: Pointer to HBA context object.
568 * @xritag: xri used in this exchange.
569 * @rrq: The RRQ to be cleared.
570 *
James Smart19ca7602010-11-20 23:11:55 -0500571 **/
James Smart1151e3e2011-02-16 12:39:35 -0500572void
573lpfc_clr_rrq_active(struct lpfc_hba *phba,
574 uint16_t xritag,
575 struct lpfc_node_rrq *rrq)
James Smart19ca7602010-11-20 23:11:55 -0500576{
James Smart1151e3e2011-02-16 12:39:35 -0500577 struct lpfc_nodelist *ndlp = NULL;
James Smart19ca7602010-11-20 23:11:55 -0500578
James Smart1151e3e2011-02-16 12:39:35 -0500579 if ((rrq->vport) && NLP_CHK_NODE_ACT(rrq->ndlp))
580 ndlp = lpfc_findnode_did(rrq->vport, rrq->nlp_DID);
James Smart19ca7602010-11-20 23:11:55 -0500581
582 /* The target DID could have been swapped (cable swap)
583 * we should use the ndlp from the findnode if it is
584 * available.
585 */
James Smart1151e3e2011-02-16 12:39:35 -0500586 if ((!ndlp) && rrq->ndlp)
James Smart19ca7602010-11-20 23:11:55 -0500587 ndlp = rrq->ndlp;
588
James Smart1151e3e2011-02-16 12:39:35 -0500589 if (!ndlp)
590 goto out;
591
James Smart6d368e52011-05-24 11:44:12 -0400592 if (test_and_clear_bit(xritag, ndlp->active_rrqs.xri_bitmap)) {
James Smart19ca7602010-11-20 23:11:55 -0500593 rrq->send_rrq = 0;
594 rrq->xritag = 0;
595 rrq->rrq_stop_time = 0;
596 }
James Smart1151e3e2011-02-16 12:39:35 -0500597out:
James Smart19ca7602010-11-20 23:11:55 -0500598 mempool_free(rrq, phba->rrq_pool);
599}
600
601/**
602 * lpfc_handle_rrq_active - Checks if RRQ has waithed RATOV.
603 * @phba: Pointer to HBA context object.
604 *
605 * This function is called with hbalock held. This function
606 * Checks if stop_time (ratov from setting rrq active) has
607 * been reached, if it has and the send_rrq flag is set then
608 * it will call lpfc_send_rrq. If the send_rrq flag is not set
609 * then it will just call the routine to clear the rrq and
610 * free the rrq resource.
611 * The timer is set to the next rrq that is going to expire before
612 * leaving the routine.
613 *
614 **/
615void
616lpfc_handle_rrq_active(struct lpfc_hba *phba)
617{
618 struct lpfc_node_rrq *rrq;
619 struct lpfc_node_rrq *nextrrq;
620 unsigned long next_time;
621 unsigned long iflags;
James Smart1151e3e2011-02-16 12:39:35 -0500622 LIST_HEAD(send_rrq);
James Smart19ca7602010-11-20 23:11:55 -0500623
624 spin_lock_irqsave(&phba->hbalock, iflags);
625 phba->hba_flag &= ~HBA_RRQ_ACTIVE;
626 next_time = jiffies + HZ * (phba->fc_ratov + 1);
627 list_for_each_entry_safe(rrq, nextrrq,
James Smart1151e3e2011-02-16 12:39:35 -0500628 &phba->active_rrq_list, list) {
629 if (time_after(jiffies, rrq->rrq_stop_time))
630 list_move(&rrq->list, &send_rrq);
631 else if (time_before(rrq->rrq_stop_time, next_time))
James Smart19ca7602010-11-20 23:11:55 -0500632 next_time = rrq->rrq_stop_time;
633 }
634 spin_unlock_irqrestore(&phba->hbalock, iflags);
635 if (!list_empty(&phba->active_rrq_list))
636 mod_timer(&phba->rrq_tmr, next_time);
James Smart1151e3e2011-02-16 12:39:35 -0500637 list_for_each_entry_safe(rrq, nextrrq, &send_rrq, list) {
638 list_del(&rrq->list);
639 if (!rrq->send_rrq)
640 /* this call will free the rrq */
641 lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
642 else if (lpfc_send_rrq(phba, rrq)) {
643 /* if we send the rrq then the completion handler
644 * will clear the bit in the xribitmap.
645 */
646 lpfc_clr_rrq_active(phba, rrq->xritag,
647 rrq);
648 }
649 }
James Smart19ca7602010-11-20 23:11:55 -0500650}
651
652/**
653 * lpfc_get_active_rrq - Get the active RRQ for this exchange.
654 * @vport: Pointer to vport context object.
655 * @xri: The xri used in the exchange.
656 * @did: The targets DID for this exchange.
657 *
658 * returns NULL = rrq not found in the phba->active_rrq_list.
659 * rrq = rrq for this xri and target.
660 **/
661struct lpfc_node_rrq *
662lpfc_get_active_rrq(struct lpfc_vport *vport, uint16_t xri, uint32_t did)
663{
664 struct lpfc_hba *phba = vport->phba;
665 struct lpfc_node_rrq *rrq;
666 struct lpfc_node_rrq *nextrrq;
667 unsigned long iflags;
668
669 if (phba->sli_rev != LPFC_SLI_REV4)
670 return NULL;
671 spin_lock_irqsave(&phba->hbalock, iflags);
672 list_for_each_entry_safe(rrq, nextrrq, &phba->active_rrq_list, list) {
673 if (rrq->vport == vport && rrq->xritag == xri &&
674 rrq->nlp_DID == did){
675 list_del(&rrq->list);
676 spin_unlock_irqrestore(&phba->hbalock, iflags);
677 return rrq;
678 }
679 }
680 spin_unlock_irqrestore(&phba->hbalock, iflags);
681 return NULL;
682}
683
684/**
685 * lpfc_cleanup_vports_rrqs - Remove and clear the active RRQ for this vport.
686 * @vport: Pointer to vport context object.
James Smart1151e3e2011-02-16 12:39:35 -0500687 * @ndlp: Pointer to the lpfc_node_list structure.
688 * If ndlp is NULL Remove all active RRQs for this vport from the
689 * phba->active_rrq_list and clear the rrq.
690 * If ndlp is not NULL then only remove rrqs for this vport & this ndlp.
James Smart19ca7602010-11-20 23:11:55 -0500691 **/
692void
James Smart1151e3e2011-02-16 12:39:35 -0500693lpfc_cleanup_vports_rrqs(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
James Smart19ca7602010-11-20 23:11:55 -0500694
695{
696 struct lpfc_hba *phba = vport->phba;
697 struct lpfc_node_rrq *rrq;
698 struct lpfc_node_rrq *nextrrq;
699 unsigned long iflags;
James Smart1151e3e2011-02-16 12:39:35 -0500700 LIST_HEAD(rrq_list);
James Smart19ca7602010-11-20 23:11:55 -0500701
702 if (phba->sli_rev != LPFC_SLI_REV4)
703 return;
James Smart1151e3e2011-02-16 12:39:35 -0500704 if (!ndlp) {
705 lpfc_sli4_vport_delete_els_xri_aborted(vport);
706 lpfc_sli4_vport_delete_fcp_xri_aborted(vport);
James Smart19ca7602010-11-20 23:11:55 -0500707 }
James Smart1151e3e2011-02-16 12:39:35 -0500708 spin_lock_irqsave(&phba->hbalock, iflags);
709 list_for_each_entry_safe(rrq, nextrrq, &phba->active_rrq_list, list)
710 if ((rrq->vport == vport) && (!ndlp || rrq->ndlp == ndlp))
711 list_move(&rrq->list, &rrq_list);
James Smart19ca7602010-11-20 23:11:55 -0500712 spin_unlock_irqrestore(&phba->hbalock, iflags);
James Smart1151e3e2011-02-16 12:39:35 -0500713
714 list_for_each_entry_safe(rrq, nextrrq, &rrq_list, list) {
715 list_del(&rrq->list);
716 lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
717 }
James Smart19ca7602010-11-20 23:11:55 -0500718}
719
720/**
721 * lpfc_cleanup_wt_rrqs - Remove all rrq's from the active list.
722 * @phba: Pointer to HBA context object.
723 *
724 * Remove all rrqs from the phba->active_rrq_list and free them by
725 * calling __lpfc_clr_active_rrq
726 *
727 **/
728void
729lpfc_cleanup_wt_rrqs(struct lpfc_hba *phba)
730{
731 struct lpfc_node_rrq *rrq;
732 struct lpfc_node_rrq *nextrrq;
733 unsigned long next_time;
734 unsigned long iflags;
James Smart1151e3e2011-02-16 12:39:35 -0500735 LIST_HEAD(rrq_list);
James Smart19ca7602010-11-20 23:11:55 -0500736
737 if (phba->sli_rev != LPFC_SLI_REV4)
738 return;
739 spin_lock_irqsave(&phba->hbalock, iflags);
740 phba->hba_flag &= ~HBA_RRQ_ACTIVE;
741 next_time = jiffies + HZ * (phba->fc_ratov * 2);
James Smart1151e3e2011-02-16 12:39:35 -0500742 list_splice_init(&phba->active_rrq_list, &rrq_list);
James Smart19ca7602010-11-20 23:11:55 -0500743 spin_unlock_irqrestore(&phba->hbalock, iflags);
James Smart1151e3e2011-02-16 12:39:35 -0500744
745 list_for_each_entry_safe(rrq, nextrrq, &rrq_list, list) {
746 list_del(&rrq->list);
747 lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
748 }
James Smart19ca7602010-11-20 23:11:55 -0500749 if (!list_empty(&phba->active_rrq_list))
750 mod_timer(&phba->rrq_tmr, next_time);
751}
752
753
754/**
James Smart1151e3e2011-02-16 12:39:35 -0500755 * lpfc_test_rrq_active - Test RRQ bit in xri_bitmap.
James Smart19ca7602010-11-20 23:11:55 -0500756 * @phba: Pointer to HBA context object.
757 * @ndlp: Targets nodelist pointer for this exchange.
758 * @xritag the xri in the bitmap to test.
759 *
760 * This function is called with hbalock held. This function
761 * returns 0 = rrq not active for this xri
762 * 1 = rrq is valid for this xri.
763 **/
James Smart1151e3e2011-02-16 12:39:35 -0500764int
765lpfc_test_rrq_active(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
James Smart19ca7602010-11-20 23:11:55 -0500766 uint16_t xritag)
767{
James Smart19ca7602010-11-20 23:11:55 -0500768 if (!ndlp)
769 return 0;
James Smart6d368e52011-05-24 11:44:12 -0400770 if (test_bit(xritag, ndlp->active_rrqs.xri_bitmap))
James Smart19ca7602010-11-20 23:11:55 -0500771 return 1;
772 else
773 return 0;
774}
775
776/**
777 * lpfc_set_rrq_active - set RRQ active bit in xri_bitmap.
778 * @phba: Pointer to HBA context object.
779 * @ndlp: nodelist pointer for this target.
780 * @xritag: xri used in this exchange.
781 * @rxid: Remote Exchange ID.
782 * @send_rrq: Flag used to determine if we should send rrq els cmd.
783 *
784 * This function takes the hbalock.
785 * The active bit is always set in the active rrq xri_bitmap even
786 * if there is no slot avaiable for the other rrq information.
787 *
788 * returns 0 rrq actived for this xri
789 * < 0 No memory or invalid ndlp.
790 **/
791int
792lpfc_set_rrq_active(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
James Smartb42c07c2012-01-18 16:25:55 -0500793 uint16_t xritag, uint16_t rxid, uint16_t send_rrq)
James Smart19ca7602010-11-20 23:11:55 -0500794{
James Smart19ca7602010-11-20 23:11:55 -0500795 unsigned long iflags;
James Smartb42c07c2012-01-18 16:25:55 -0500796 struct lpfc_node_rrq *rrq;
797 int empty;
798
799 if (!ndlp)
800 return -EINVAL;
801
802 if (!phba->cfg_enable_rrq)
803 return -EINVAL;
James Smart19ca7602010-11-20 23:11:55 -0500804
805 spin_lock_irqsave(&phba->hbalock, iflags);
James Smartb42c07c2012-01-18 16:25:55 -0500806 if (phba->pport->load_flag & FC_UNLOADING) {
807 phba->hba_flag &= ~HBA_RRQ_ACTIVE;
808 goto out;
809 }
810
811 /*
812 * set the active bit even if there is no mem available.
813 */
814 if (NLP_CHK_FREE_REQ(ndlp))
815 goto out;
816
817 if (ndlp->vport && (ndlp->vport->load_flag & FC_UNLOADING))
818 goto out;
819
820 if (test_and_set_bit(xritag, ndlp->active_rrqs.xri_bitmap))
821 goto out;
822
James Smart19ca7602010-11-20 23:11:55 -0500823 spin_unlock_irqrestore(&phba->hbalock, iflags);
James Smartb42c07c2012-01-18 16:25:55 -0500824 rrq = mempool_alloc(phba->rrq_pool, GFP_KERNEL);
825 if (!rrq) {
826 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
827 "3155 Unable to allocate RRQ xri:0x%x rxid:0x%x"
828 " DID:0x%x Send:%d\n",
829 xritag, rxid, ndlp->nlp_DID, send_rrq);
830 return -EINVAL;
831 }
832 rrq->send_rrq = send_rrq;
833 rrq->xritag = xritag;
834 rrq->rrq_stop_time = jiffies + HZ * (phba->fc_ratov + 1);
835 rrq->ndlp = ndlp;
836 rrq->nlp_DID = ndlp->nlp_DID;
837 rrq->vport = ndlp->vport;
838 rrq->rxid = rxid;
839 rrq->send_rrq = send_rrq;
840 spin_lock_irqsave(&phba->hbalock, iflags);
841 empty = list_empty(&phba->active_rrq_list);
842 list_add_tail(&rrq->list, &phba->active_rrq_list);
843 phba->hba_flag |= HBA_RRQ_ACTIVE;
844 if (empty)
845 lpfc_worker_wake_up(phba);
846 spin_unlock_irqrestore(&phba->hbalock, iflags);
847 return 0;
848out:
849 spin_unlock_irqrestore(&phba->hbalock, iflags);
850 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
851 "2921 Can't set rrq active xri:0x%x rxid:0x%x"
852 " DID:0x%x Send:%d\n",
853 xritag, rxid, ndlp->nlp_DID, send_rrq);
854 return -EINVAL;
James Smart19ca7602010-11-20 23:11:55 -0500855}
856
857/**
James Smartda0436e2009-05-22 14:51:39 -0400858 * __lpfc_sli_get_sglq - Allocates an iocb object from sgl pool
859 * @phba: Pointer to HBA context object.
James Smart19ca7602010-11-20 23:11:55 -0500860 * @piocb: Pointer to the iocbq.
James Smartda0436e2009-05-22 14:51:39 -0400861 *
862 * This function is called with hbalock held. This function
James Smart6d368e52011-05-24 11:44:12 -0400863 * gets a new driver sglq object from the sglq list. If the
James Smartda0436e2009-05-22 14:51:39 -0400864 * list is not empty then it is successful, it returns pointer to the newly
865 * allocated sglq object else it returns NULL.
866 **/
867static struct lpfc_sglq *
James Smart19ca7602010-11-20 23:11:55 -0500868__lpfc_sli_get_sglq(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq)
James Smartda0436e2009-05-22 14:51:39 -0400869{
870 struct list_head *lpfc_sgl_list = &phba->sli4_hba.lpfc_sgl_list;
871 struct lpfc_sglq *sglq = NULL;
James Smart19ca7602010-11-20 23:11:55 -0500872 struct lpfc_sglq *start_sglq = NULL;
James Smart19ca7602010-11-20 23:11:55 -0500873 struct lpfc_scsi_buf *lpfc_cmd;
874 struct lpfc_nodelist *ndlp;
875 int found = 0;
876
877 if (piocbq->iocb_flag & LPFC_IO_FCP) {
878 lpfc_cmd = (struct lpfc_scsi_buf *) piocbq->context1;
879 ndlp = lpfc_cmd->rdata->pnode;
James Smartbe858b62010-12-15 17:57:20 -0500880 } else if ((piocbq->iocb.ulpCommand == CMD_GEN_REQUEST64_CR) &&
881 !(piocbq->iocb_flag & LPFC_IO_LIBDFC))
James Smart19ca7602010-11-20 23:11:55 -0500882 ndlp = piocbq->context_un.ndlp;
James Smart93d1379e2012-05-09 21:19:34 -0400883 else if ((piocbq->iocb.ulpCommand == CMD_ELS_REQUEST64_CR) &&
884 (piocbq->iocb_flag & LPFC_IO_LIBDFC))
885 ndlp = piocbq->context_un.ndlp;
James Smart19ca7602010-11-20 23:11:55 -0500886 else
887 ndlp = piocbq->context1;
888
James Smartda0436e2009-05-22 14:51:39 -0400889 list_remove_head(lpfc_sgl_list, sglq, struct lpfc_sglq, list);
James Smart19ca7602010-11-20 23:11:55 -0500890 start_sglq = sglq;
891 while (!found) {
892 if (!sglq)
893 return NULL;
James Smartee0f4fe2012-05-09 21:19:14 -0400894 if (lpfc_test_rrq_active(phba, ndlp, sglq->sli4_lxritag)) {
James Smart19ca7602010-11-20 23:11:55 -0500895 /* This xri has an rrq outstanding for this DID.
896 * put it back in the list and get another xri.
897 */
898 list_add_tail(&sglq->list, lpfc_sgl_list);
899 sglq = NULL;
900 list_remove_head(lpfc_sgl_list, sglq,
901 struct lpfc_sglq, list);
902 if (sglq == start_sglq) {
903 sglq = NULL;
904 break;
905 } else
906 continue;
907 }
908 sglq->ndlp = ndlp;
909 found = 1;
James Smart6d368e52011-05-24 11:44:12 -0400910 phba->sli4_hba.lpfc_sglq_active_list[sglq->sli4_lxritag] = sglq;
James Smart19ca7602010-11-20 23:11:55 -0500911 sglq->state = SGL_ALLOCATED;
912 }
James Smartda0436e2009-05-22 14:51:39 -0400913 return sglq;
914}
915
916/**
James Smart3621a712009-04-06 18:47:14 -0400917 * lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
James Smarte59058c2008-08-24 21:49:00 -0400918 * @phba: Pointer to HBA context object.
919 *
920 * This function is called with no lock held. This function
921 * allocates a new driver iocb object from the iocb pool. If the
922 * allocation is successful, it returns pointer to the newly
923 * allocated iocb object else it returns NULL.
924 **/
James Smart2e0fef82007-06-17 19:56:36 -0500925struct lpfc_iocbq *
926lpfc_sli_get_iocbq(struct lpfc_hba *phba)
James Bottomley604a3e32005-10-29 10:28:33 -0500927{
James Smart2e0fef82007-06-17 19:56:36 -0500928 struct lpfc_iocbq * iocbq = NULL;
929 unsigned long iflags;
930
931 spin_lock_irqsave(&phba->hbalock, iflags);
932 iocbq = __lpfc_sli_get_iocbq(phba);
933 spin_unlock_irqrestore(&phba->hbalock, iflags);
934 return iocbq;
935}
936
James Smarte59058c2008-08-24 21:49:00 -0400937/**
James Smart4f774512009-05-22 14:52:35 -0400938 * __lpfc_sli_release_iocbq_s4 - Release iocb to the iocb pool
939 * @phba: Pointer to HBA context object.
940 * @iocbq: Pointer to driver iocb object.
941 *
942 * This function is called with hbalock held to release driver
943 * iocb object to the iocb pool. The iotag in the iocb object
944 * does not change for each use of the iocb object. This function
945 * clears all other fields of the iocb object when it is freed.
946 * The sqlq structure that holds the xritag and phys and virtual
947 * mappings for the scatter gather list is retrieved from the
948 * active array of sglq. The get of the sglq pointer also clears
949 * the entry in the array. If the status of the IO indiactes that
950 * this IO was aborted then the sglq entry it put on the
951 * lpfc_abts_els_sgl_list until the CQ_ABORTED_XRI is received. If the
952 * IO has good status or fails for any other reason then the sglq
953 * entry is added to the free list (lpfc_sgl_list).
954 **/
955static void
956__lpfc_sli_release_iocbq_s4(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
957{
958 struct lpfc_sglq *sglq;
959 size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
James Smart2a9bf3d2010-06-07 15:24:45 -0400960 unsigned long iflag = 0;
961 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
James Smart4f774512009-05-22 14:52:35 -0400962
963 if (iocbq->sli4_xritag == NO_XRI)
964 sglq = NULL;
965 else
James Smart6d368e52011-05-24 11:44:12 -0400966 sglq = __lpfc_clear_active_sglq(phba, iocbq->sli4_lxritag);
967
James Smart4f774512009-05-22 14:52:35 -0400968 if (sglq) {
James Smart0f65ff62010-02-26 14:14:23 -0500969 if ((iocbq->iocb_flag & LPFC_EXCHANGE_BUSY) &&
970 (sglq->state != SGL_XRI_ABORTED)) {
James Smart4f774512009-05-22 14:52:35 -0400971 spin_lock_irqsave(&phba->sli4_hba.abts_sgl_list_lock,
972 iflag);
973 list_add(&sglq->list,
974 &phba->sli4_hba.lpfc_abts_els_sgl_list);
975 spin_unlock_irqrestore(
976 &phba->sli4_hba.abts_sgl_list_lock, iflag);
James Smart0f65ff62010-02-26 14:14:23 -0500977 } else {
978 sglq->state = SGL_FREED;
James Smart19ca7602010-11-20 23:11:55 -0500979 sglq->ndlp = NULL;
James Smartfedd3b72011-02-16 12:39:24 -0500980 list_add_tail(&sglq->list,
981 &phba->sli4_hba.lpfc_sgl_list);
James Smart2a9bf3d2010-06-07 15:24:45 -0400982
983 /* Check if TXQ queue needs to be serviced */
James Smart589a52d2010-07-14 15:30:54 -0400984 if (pring->txq_cnt)
James Smart2a9bf3d2010-06-07 15:24:45 -0400985 lpfc_worker_wake_up(phba);
James Smart0f65ff62010-02-26 14:14:23 -0500986 }
James Smart4f774512009-05-22 14:52:35 -0400987 }
988
989
990 /*
991 * Clean all volatile data fields, preserve iotag and node struct.
992 */
993 memset((char *)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
James Smart6d368e52011-05-24 11:44:12 -0400994 iocbq->sli4_lxritag = NO_XRI;
James Smart4f774512009-05-22 14:52:35 -0400995 iocbq->sli4_xritag = NO_XRI;
996 list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
997}
998
James Smart2a9bf3d2010-06-07 15:24:45 -0400999
James Smart4f774512009-05-22 14:52:35 -04001000/**
James Smart3772a992009-05-22 14:50:54 -04001001 * __lpfc_sli_release_iocbq_s3 - Release iocb to the iocb pool
1002 * @phba: Pointer to HBA context object.
1003 * @iocbq: Pointer to driver iocb object.
1004 *
1005 * This function is called with hbalock held to release driver
1006 * iocb object to the iocb pool. The iotag in the iocb object
1007 * does not change for each use of the iocb object. This function
1008 * clears all other fields of the iocb object when it is freed.
1009 **/
1010static void
1011__lpfc_sli_release_iocbq_s3(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1012{
1013 size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
1014
1015 /*
1016 * Clean all volatile data fields, preserve iotag and node struct.
1017 */
1018 memset((char*)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
1019 iocbq->sli4_xritag = NO_XRI;
1020 list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
1021}
1022
1023/**
James Smart3621a712009-04-06 18:47:14 -04001024 * __lpfc_sli_release_iocbq - Release iocb to the iocb pool
James Smarte59058c2008-08-24 21:49:00 -04001025 * @phba: Pointer to HBA context object.
1026 * @iocbq: Pointer to driver iocb object.
1027 *
1028 * This function is called with hbalock held to release driver
1029 * iocb object to the iocb pool. The iotag in the iocb object
1030 * does not change for each use of the iocb object. This function
1031 * clears all other fields of the iocb object when it is freed.
1032 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01001033static void
James Smart2e0fef82007-06-17 19:56:36 -05001034__lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1035{
James Smart3772a992009-05-22 14:50:54 -04001036 phba->__lpfc_sli_release_iocbq(phba, iocbq);
James Smart2a9bf3d2010-06-07 15:24:45 -04001037 phba->iocb_cnt--;
James Bottomley604a3e32005-10-29 10:28:33 -05001038}
1039
James Smarte59058c2008-08-24 21:49:00 -04001040/**
James Smart3621a712009-04-06 18:47:14 -04001041 * lpfc_sli_release_iocbq - Release iocb to the iocb pool
James Smarte59058c2008-08-24 21:49:00 -04001042 * @phba: Pointer to HBA context object.
1043 * @iocbq: Pointer to driver iocb object.
1044 *
1045 * This function is called with no lock held to release the iocb to
1046 * iocb pool.
1047 **/
James Smart2e0fef82007-06-17 19:56:36 -05001048void
1049lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1050{
1051 unsigned long iflags;
1052
1053 /*
1054 * Clean all volatile data fields, preserve iotag and node struct.
1055 */
1056 spin_lock_irqsave(&phba->hbalock, iflags);
1057 __lpfc_sli_release_iocbq(phba, iocbq);
1058 spin_unlock_irqrestore(&phba->hbalock, iflags);
1059}
1060
James Smarte59058c2008-08-24 21:49:00 -04001061/**
James Smarta257bf92009-04-06 18:48:10 -04001062 * lpfc_sli_cancel_iocbs - Cancel all iocbs from a list.
1063 * @phba: Pointer to HBA context object.
1064 * @iocblist: List of IOCBs.
1065 * @ulpstatus: ULP status in IOCB command field.
1066 * @ulpWord4: ULP word-4 in IOCB command field.
1067 *
1068 * This function is called with a list of IOCBs to cancel. It cancels the IOCB
1069 * on the list by invoking the complete callback function associated with the
1070 * IOCB with the provided @ulpstatus and @ulpword4 set to the IOCB commond
1071 * fields.
1072 **/
1073void
1074lpfc_sli_cancel_iocbs(struct lpfc_hba *phba, struct list_head *iocblist,
1075 uint32_t ulpstatus, uint32_t ulpWord4)
1076{
1077 struct lpfc_iocbq *piocb;
1078
1079 while (!list_empty(iocblist)) {
1080 list_remove_head(iocblist, piocb, struct lpfc_iocbq, list);
1081
1082 if (!piocb->iocb_cmpl)
1083 lpfc_sli_release_iocbq(phba, piocb);
1084 else {
1085 piocb->iocb.ulpStatus = ulpstatus;
1086 piocb->iocb.un.ulpWord[4] = ulpWord4;
1087 (piocb->iocb_cmpl) (phba, piocb, piocb);
1088 }
1089 }
1090 return;
1091}
1092
1093/**
James Smart3621a712009-04-06 18:47:14 -04001094 * lpfc_sli_iocb_cmd_type - Get the iocb type
1095 * @iocb_cmnd: iocb command code.
James Smarte59058c2008-08-24 21:49:00 -04001096 *
1097 * This function is called by ring event handler function to get the iocb type.
1098 * This function translates the iocb command to an iocb command type used to
1099 * decide the final disposition of each completed IOCB.
1100 * The function returns
1101 * LPFC_UNKNOWN_IOCB if it is an unsupported iocb
1102 * LPFC_SOL_IOCB if it is a solicited iocb completion
1103 * LPFC_ABORT_IOCB if it is an abort iocb
1104 * LPFC_UNSOL_IOCB if it is an unsolicited iocb
1105 *
1106 * The caller is not required to hold any lock.
1107 **/
dea31012005-04-17 16:05:31 -05001108static lpfc_iocb_type
1109lpfc_sli_iocb_cmd_type(uint8_t iocb_cmnd)
1110{
1111 lpfc_iocb_type type = LPFC_UNKNOWN_IOCB;
1112
1113 if (iocb_cmnd > CMD_MAX_IOCB_CMD)
1114 return 0;
1115
1116 switch (iocb_cmnd) {
1117 case CMD_XMIT_SEQUENCE_CR:
1118 case CMD_XMIT_SEQUENCE_CX:
1119 case CMD_XMIT_BCAST_CN:
1120 case CMD_XMIT_BCAST_CX:
1121 case CMD_ELS_REQUEST_CR:
1122 case CMD_ELS_REQUEST_CX:
1123 case CMD_CREATE_XRI_CR:
1124 case CMD_CREATE_XRI_CX:
1125 case CMD_GET_RPI_CN:
1126 case CMD_XMIT_ELS_RSP_CX:
1127 case CMD_GET_RPI_CR:
1128 case CMD_FCP_IWRITE_CR:
1129 case CMD_FCP_IWRITE_CX:
1130 case CMD_FCP_IREAD_CR:
1131 case CMD_FCP_IREAD_CX:
1132 case CMD_FCP_ICMND_CR:
1133 case CMD_FCP_ICMND_CX:
James Smartf5603512006-12-02 13:35:43 -05001134 case CMD_FCP_TSEND_CX:
1135 case CMD_FCP_TRSP_CX:
1136 case CMD_FCP_TRECEIVE_CX:
1137 case CMD_FCP_AUTO_TRSP_CX:
dea31012005-04-17 16:05:31 -05001138 case CMD_ADAPTER_MSG:
1139 case CMD_ADAPTER_DUMP:
1140 case CMD_XMIT_SEQUENCE64_CR:
1141 case CMD_XMIT_SEQUENCE64_CX:
1142 case CMD_XMIT_BCAST64_CN:
1143 case CMD_XMIT_BCAST64_CX:
1144 case CMD_ELS_REQUEST64_CR:
1145 case CMD_ELS_REQUEST64_CX:
1146 case CMD_FCP_IWRITE64_CR:
1147 case CMD_FCP_IWRITE64_CX:
1148 case CMD_FCP_IREAD64_CR:
1149 case CMD_FCP_IREAD64_CX:
1150 case CMD_FCP_ICMND64_CR:
1151 case CMD_FCP_ICMND64_CX:
James Smartf5603512006-12-02 13:35:43 -05001152 case CMD_FCP_TSEND64_CX:
1153 case CMD_FCP_TRSP64_CX:
1154 case CMD_FCP_TRECEIVE64_CX:
dea31012005-04-17 16:05:31 -05001155 case CMD_GEN_REQUEST64_CR:
1156 case CMD_GEN_REQUEST64_CX:
1157 case CMD_XMIT_ELS_RSP64_CX:
James Smartda0436e2009-05-22 14:51:39 -04001158 case DSSCMD_IWRITE64_CR:
1159 case DSSCMD_IWRITE64_CX:
1160 case DSSCMD_IREAD64_CR:
1161 case DSSCMD_IREAD64_CX:
dea31012005-04-17 16:05:31 -05001162 type = LPFC_SOL_IOCB;
1163 break;
1164 case CMD_ABORT_XRI_CN:
1165 case CMD_ABORT_XRI_CX:
1166 case CMD_CLOSE_XRI_CN:
1167 case CMD_CLOSE_XRI_CX:
1168 case CMD_XRI_ABORTED_CX:
1169 case CMD_ABORT_MXRI64_CN:
James Smart6669f9b2009-10-02 15:16:45 -04001170 case CMD_XMIT_BLS_RSP64_CX:
dea31012005-04-17 16:05:31 -05001171 type = LPFC_ABORT_IOCB;
1172 break;
1173 case CMD_RCV_SEQUENCE_CX:
1174 case CMD_RCV_ELS_REQ_CX:
1175 case CMD_RCV_SEQUENCE64_CX:
1176 case CMD_RCV_ELS_REQ64_CX:
James Smart57127f12007-10-27 13:37:05 -04001177 case CMD_ASYNC_STATUS:
James Smarted957682007-06-17 19:56:37 -05001178 case CMD_IOCB_RCV_SEQ64_CX:
1179 case CMD_IOCB_RCV_ELS64_CX:
1180 case CMD_IOCB_RCV_CONT64_CX:
James Smart3163f722008-02-08 18:50:25 -05001181 case CMD_IOCB_RET_XRI64_CX:
dea31012005-04-17 16:05:31 -05001182 type = LPFC_UNSOL_IOCB;
1183 break;
James Smart3163f722008-02-08 18:50:25 -05001184 case CMD_IOCB_XMIT_MSEQ64_CR:
1185 case CMD_IOCB_XMIT_MSEQ64_CX:
1186 case CMD_IOCB_RCV_SEQ_LIST64_CX:
1187 case CMD_IOCB_RCV_ELS_LIST64_CX:
1188 case CMD_IOCB_CLOSE_EXTENDED_CN:
1189 case CMD_IOCB_ABORT_EXTENDED_CN:
1190 case CMD_IOCB_RET_HBQE64_CN:
1191 case CMD_IOCB_FCP_IBIDIR64_CR:
1192 case CMD_IOCB_FCP_IBIDIR64_CX:
1193 case CMD_IOCB_FCP_ITASKMGT64_CX:
1194 case CMD_IOCB_LOGENTRY_CN:
1195 case CMD_IOCB_LOGENTRY_ASYNC_CN:
1196 printk("%s - Unhandled SLI-3 Command x%x\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07001197 __func__, iocb_cmnd);
James Smart3163f722008-02-08 18:50:25 -05001198 type = LPFC_UNKNOWN_IOCB;
1199 break;
dea31012005-04-17 16:05:31 -05001200 default:
1201 type = LPFC_UNKNOWN_IOCB;
1202 break;
1203 }
1204
1205 return type;
1206}
1207
James Smarte59058c2008-08-24 21:49:00 -04001208/**
James Smart3621a712009-04-06 18:47:14 -04001209 * lpfc_sli_ring_map - Issue config_ring mbox for all rings
James Smarte59058c2008-08-24 21:49:00 -04001210 * @phba: Pointer to HBA context object.
1211 *
1212 * This function is called from SLI initialization code
1213 * to configure every ring of the HBA's SLI interface. The
1214 * caller is not required to hold any lock. This function issues
1215 * a config_ring mailbox command for each ring.
1216 * This function returns zero if successful else returns a negative
1217 * error code.
1218 **/
dea31012005-04-17 16:05:31 -05001219static int
James Smarted957682007-06-17 19:56:37 -05001220lpfc_sli_ring_map(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05001221{
1222 struct lpfc_sli *psli = &phba->sli;
James Smarted957682007-06-17 19:56:37 -05001223 LPFC_MBOXQ_t *pmb;
1224 MAILBOX_t *pmbox;
1225 int i, rc, ret = 0;
dea31012005-04-17 16:05:31 -05001226
James Smarted957682007-06-17 19:56:37 -05001227 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1228 if (!pmb)
1229 return -ENOMEM;
James Smart04c68492009-05-22 14:52:52 -04001230 pmbox = &pmb->u.mb;
James Smarted957682007-06-17 19:56:37 -05001231 phba->link_state = LPFC_INIT_MBX_CMDS;
dea31012005-04-17 16:05:31 -05001232 for (i = 0; i < psli->num_rings; i++) {
dea31012005-04-17 16:05:31 -05001233 lpfc_config_ring(phba, i, pmb);
1234 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
1235 if (rc != MBX_SUCCESS) {
James Smart92d7f7b2007-06-17 19:56:38 -05001236 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04001237 "0446 Adapter failed to init (%d), "
dea31012005-04-17 16:05:31 -05001238 "mbxCmd x%x CFG_RING, mbxStatus x%x, "
1239 "ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04001240 rc, pmbox->mbxCommand,
1241 pmbox->mbxStatus, i);
James Smart2e0fef82007-06-17 19:56:36 -05001242 phba->link_state = LPFC_HBA_ERROR;
James Smarted957682007-06-17 19:56:37 -05001243 ret = -ENXIO;
1244 break;
dea31012005-04-17 16:05:31 -05001245 }
1246 }
James Smarted957682007-06-17 19:56:37 -05001247 mempool_free(pmb, phba->mbox_mem_pool);
1248 return ret;
dea31012005-04-17 16:05:31 -05001249}
1250
James Smarte59058c2008-08-24 21:49:00 -04001251/**
James Smart3621a712009-04-06 18:47:14 -04001252 * lpfc_sli_ringtxcmpl_put - Adds new iocb to the txcmplq
James Smarte59058c2008-08-24 21:49:00 -04001253 * @phba: Pointer to HBA context object.
1254 * @pring: Pointer to driver SLI ring object.
1255 * @piocb: Pointer to the driver iocb object.
1256 *
1257 * This function is called with hbalock held. The function adds the
1258 * new iocb to txcmplq of the given ring. This function always returns
1259 * 0. If this function is called for ELS ring, this function checks if
1260 * there is a vport associated with the ELS command. This function also
1261 * starts els_tmofunc timer if this is an ELS command.
1262 **/
dea31012005-04-17 16:05:31 -05001263static int
James Smart2e0fef82007-06-17 19:56:36 -05001264lpfc_sli_ringtxcmpl_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1265 struct lpfc_iocbq *piocb)
dea31012005-04-17 16:05:31 -05001266{
dea31012005-04-17 16:05:31 -05001267 list_add_tail(&piocb->list, &pring->txcmplq);
James Smart4f2e66c2012-05-09 21:17:07 -04001268 piocb->iocb_flag |= LPFC_IO_ON_TXCMPLQ;
dea31012005-04-17 16:05:31 -05001269 pring->txcmplq_cnt++;
James Smart2a9bf3d2010-06-07 15:24:45 -04001270 if (pring->txcmplq_cnt > pring->txcmplq_max)
1271 pring->txcmplq_max = pring->txcmplq_cnt;
1272
James Smart92d7f7b2007-06-17 19:56:38 -05001273 if ((unlikely(pring->ringno == LPFC_ELS_RING)) &&
1274 (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
1275 (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
1276 if (!piocb->vport)
1277 BUG();
1278 else
1279 mod_timer(&piocb->vport->els_tmofunc,
1280 jiffies + HZ * (phba->fc_ratov << 1));
1281 }
1282
dea31012005-04-17 16:05:31 -05001283
James Smart2e0fef82007-06-17 19:56:36 -05001284 return 0;
dea31012005-04-17 16:05:31 -05001285}
1286
James Smarte59058c2008-08-24 21:49:00 -04001287/**
James Smart3621a712009-04-06 18:47:14 -04001288 * lpfc_sli_ringtx_get - Get first element of the txq
James Smarte59058c2008-08-24 21:49:00 -04001289 * @phba: Pointer to HBA context object.
1290 * @pring: Pointer to driver SLI ring object.
1291 *
1292 * This function is called with hbalock held to get next
1293 * iocb in txq of the given ring. If there is any iocb in
1294 * the txq, the function returns first iocb in the list after
1295 * removing the iocb from the list, else it returns NULL.
1296 **/
James Smart2a9bf3d2010-06-07 15:24:45 -04001297struct lpfc_iocbq *
James Smart2e0fef82007-06-17 19:56:36 -05001298lpfc_sli_ringtx_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -05001299{
dea31012005-04-17 16:05:31 -05001300 struct lpfc_iocbq *cmd_iocb;
1301
James Smart858c9f62007-06-17 19:56:39 -05001302 list_remove_head((&pring->txq), cmd_iocb, struct lpfc_iocbq, list);
1303 if (cmd_iocb != NULL)
dea31012005-04-17 16:05:31 -05001304 pring->txq_cnt--;
James Smart2e0fef82007-06-17 19:56:36 -05001305 return cmd_iocb;
dea31012005-04-17 16:05:31 -05001306}
1307
James Smarte59058c2008-08-24 21:49:00 -04001308/**
James Smart3621a712009-04-06 18:47:14 -04001309 * lpfc_sli_next_iocb_slot - Get next iocb slot in the ring
James Smarte59058c2008-08-24 21:49:00 -04001310 * @phba: Pointer to HBA context object.
1311 * @pring: Pointer to driver SLI ring object.
1312 *
1313 * This function is called with hbalock held and the caller must post the
1314 * iocb without releasing the lock. If the caller releases the lock,
1315 * iocb slot returned by the function is not guaranteed to be available.
1316 * The function returns pointer to the next available iocb slot if there
1317 * is available slot in the ring, else it returns NULL.
1318 * If the get index of the ring is ahead of the put index, the function
1319 * will post an error attention event to the worker thread to take the
1320 * HBA to offline state.
1321 **/
dea31012005-04-17 16:05:31 -05001322static IOCB_t *
1323lpfc_sli_next_iocb_slot (struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
1324{
James Smart34b02dc2008-08-24 21:49:55 -04001325 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
dea31012005-04-17 16:05:31 -05001326 uint32_t max_cmd_idx = pring->numCiocb;
dea31012005-04-17 16:05:31 -05001327 if ((pring->next_cmdidx == pring->cmdidx) &&
1328 (++pring->next_cmdidx >= max_cmd_idx))
1329 pring->next_cmdidx = 0;
1330
1331 if (unlikely(pring->local_getidx == pring->next_cmdidx)) {
1332
1333 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
1334
1335 if (unlikely(pring->local_getidx >= max_cmd_idx)) {
1336 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001337 "0315 Ring %d issue: portCmdGet %d "
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02001338 "is bigger than cmd ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04001339 pring->ringno,
dea31012005-04-17 16:05:31 -05001340 pring->local_getidx, max_cmd_idx);
1341
James Smart2e0fef82007-06-17 19:56:36 -05001342 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05001343 /*
1344 * All error attention handlers are posted to
1345 * worker thread
1346 */
1347 phba->work_ha |= HA_ERATT;
1348 phba->work_hs = HS_FFER3;
James Smart92d7f7b2007-06-17 19:56:38 -05001349
James Smart5e9d9b82008-06-14 22:52:53 -04001350 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -05001351
1352 return NULL;
1353 }
1354
1355 if (pring->local_getidx == pring->next_cmdidx)
1356 return NULL;
1357 }
1358
James Smarted957682007-06-17 19:56:37 -05001359 return lpfc_cmd_iocb(phba, pring);
dea31012005-04-17 16:05:31 -05001360}
1361
James Smarte59058c2008-08-24 21:49:00 -04001362/**
James Smart3621a712009-04-06 18:47:14 -04001363 * lpfc_sli_next_iotag - Get an iotag for the iocb
James Smarte59058c2008-08-24 21:49:00 -04001364 * @phba: Pointer to HBA context object.
1365 * @iocbq: Pointer to driver iocb object.
1366 *
1367 * This function gets an iotag for the iocb. If there is no unused iotag and
1368 * the iocbq_lookup_len < 0xffff, this function allocates a bigger iotag_lookup
1369 * array and assigns a new iotag.
1370 * The function returns the allocated iotag if successful, else returns zero.
1371 * Zero is not a valid iotag.
1372 * The caller is not required to hold any lock.
1373 **/
James Bottomley604a3e32005-10-29 10:28:33 -05001374uint16_t
James Smart2e0fef82007-06-17 19:56:36 -05001375lpfc_sli_next_iotag(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
dea31012005-04-17 16:05:31 -05001376{
James Smart2e0fef82007-06-17 19:56:36 -05001377 struct lpfc_iocbq **new_arr;
1378 struct lpfc_iocbq **old_arr;
James Bottomley604a3e32005-10-29 10:28:33 -05001379 size_t new_len;
1380 struct lpfc_sli *psli = &phba->sli;
1381 uint16_t iotag;
dea31012005-04-17 16:05:31 -05001382
James Smart2e0fef82007-06-17 19:56:36 -05001383 spin_lock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001384 iotag = psli->last_iotag;
1385 if(++iotag < psli->iocbq_lookup_len) {
1386 psli->last_iotag = iotag;
1387 psli->iocbq_lookup[iotag] = iocbq;
James Smart2e0fef82007-06-17 19:56:36 -05001388 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001389 iocbq->iotag = iotag;
1390 return iotag;
James Smart2e0fef82007-06-17 19:56:36 -05001391 } else if (psli->iocbq_lookup_len < (0xffff
James Bottomley604a3e32005-10-29 10:28:33 -05001392 - LPFC_IOCBQ_LOOKUP_INCREMENT)) {
1393 new_len = psli->iocbq_lookup_len + LPFC_IOCBQ_LOOKUP_INCREMENT;
James Smart2e0fef82007-06-17 19:56:36 -05001394 spin_unlock_irq(&phba->hbalock);
1395 new_arr = kzalloc(new_len * sizeof (struct lpfc_iocbq *),
James Bottomley604a3e32005-10-29 10:28:33 -05001396 GFP_KERNEL);
1397 if (new_arr) {
James Smart2e0fef82007-06-17 19:56:36 -05001398 spin_lock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001399 old_arr = psli->iocbq_lookup;
1400 if (new_len <= psli->iocbq_lookup_len) {
1401 /* highly unprobable case */
1402 kfree(new_arr);
1403 iotag = psli->last_iotag;
1404 if(++iotag < psli->iocbq_lookup_len) {
1405 psli->last_iotag = iotag;
1406 psli->iocbq_lookup[iotag] = iocbq;
James Smart2e0fef82007-06-17 19:56:36 -05001407 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001408 iocbq->iotag = iotag;
1409 return iotag;
1410 }
James Smart2e0fef82007-06-17 19:56:36 -05001411 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001412 return 0;
1413 }
1414 if (psli->iocbq_lookup)
1415 memcpy(new_arr, old_arr,
1416 ((psli->last_iotag + 1) *
James Smart311464e2007-08-02 11:10:37 -04001417 sizeof (struct lpfc_iocbq *)));
James Bottomley604a3e32005-10-29 10:28:33 -05001418 psli->iocbq_lookup = new_arr;
1419 psli->iocbq_lookup_len = new_len;
1420 psli->last_iotag = iotag;
1421 psli->iocbq_lookup[iotag] = iocbq;
James Smart2e0fef82007-06-17 19:56:36 -05001422 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001423 iocbq->iotag = iotag;
1424 kfree(old_arr);
1425 return iotag;
1426 }
James Smart8f6d98d2006-08-01 07:34:00 -04001427 } else
James Smart2e0fef82007-06-17 19:56:36 -05001428 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001429
James Smartbc739052010-08-04 16:11:18 -04001430 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001431 "0318 Failed to allocate IOTAG.last IOTAG is %d\n",
1432 psli->last_iotag);
dea31012005-04-17 16:05:31 -05001433
James Bottomley604a3e32005-10-29 10:28:33 -05001434 return 0;
dea31012005-04-17 16:05:31 -05001435}
1436
James Smarte59058c2008-08-24 21:49:00 -04001437/**
James Smart3621a712009-04-06 18:47:14 -04001438 * lpfc_sli_submit_iocb - Submit an iocb to the firmware
James Smarte59058c2008-08-24 21:49:00 -04001439 * @phba: Pointer to HBA context object.
1440 * @pring: Pointer to driver SLI ring object.
1441 * @iocb: Pointer to iocb slot in the ring.
1442 * @nextiocb: Pointer to driver iocb object which need to be
1443 * posted to firmware.
1444 *
1445 * This function is called with hbalock held to post a new iocb to
1446 * the firmware. This function copies the new iocb to ring iocb slot and
1447 * updates the ring pointers. It adds the new iocb to txcmplq if there is
1448 * a completion call back for this iocb else the function will free the
1449 * iocb object.
1450 **/
dea31012005-04-17 16:05:31 -05001451static void
1452lpfc_sli_submit_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1453 IOCB_t *iocb, struct lpfc_iocbq *nextiocb)
1454{
1455 /*
James Bottomley604a3e32005-10-29 10:28:33 -05001456 * Set up an iotag
dea31012005-04-17 16:05:31 -05001457 */
James Bottomley604a3e32005-10-29 10:28:33 -05001458 nextiocb->iocb.ulpIoTag = (nextiocb->iocb_cmpl) ? nextiocb->iotag : 0;
dea31012005-04-17 16:05:31 -05001459
James Smarte2a0a9d2008-12-04 22:40:02 -05001460
James Smarta58cbd52007-08-02 11:09:43 -04001461 if (pring->ringno == LPFC_ELS_RING) {
1462 lpfc_debugfs_slow_ring_trc(phba,
1463 "IOCB cmd ring: wd4:x%08x wd6:x%08x wd7:x%08x",
1464 *(((uint32_t *) &nextiocb->iocb) + 4),
1465 *(((uint32_t *) &nextiocb->iocb) + 6),
1466 *(((uint32_t *) &nextiocb->iocb) + 7));
1467 }
1468
dea31012005-04-17 16:05:31 -05001469 /*
1470 * Issue iocb command to adapter
1471 */
James Smart92d7f7b2007-06-17 19:56:38 -05001472 lpfc_sli_pcimem_bcopy(&nextiocb->iocb, iocb, phba->iocb_cmd_size);
dea31012005-04-17 16:05:31 -05001473 wmb();
1474 pring->stats.iocb_cmd++;
1475
1476 /*
1477 * If there is no completion routine to call, we can release the
1478 * IOCB buffer back right now. For IOCBs, like QUE_RING_BUF,
1479 * that have no rsp ring completion, iocb_cmpl MUST be NULL.
1480 */
1481 if (nextiocb->iocb_cmpl)
1482 lpfc_sli_ringtxcmpl_put(phba, pring, nextiocb);
James Bottomley604a3e32005-10-29 10:28:33 -05001483 else
James Smart2e0fef82007-06-17 19:56:36 -05001484 __lpfc_sli_release_iocbq(phba, nextiocb);
dea31012005-04-17 16:05:31 -05001485
1486 /*
1487 * Let the HBA know what IOCB slot will be the next one the
1488 * driver will put a command into.
1489 */
1490 pring->cmdidx = pring->next_cmdidx;
James Smarted957682007-06-17 19:56:37 -05001491 writel(pring->cmdidx, &phba->host_gp[pring->ringno].cmdPutInx);
dea31012005-04-17 16:05:31 -05001492}
1493
James Smarte59058c2008-08-24 21:49:00 -04001494/**
James Smart3621a712009-04-06 18:47:14 -04001495 * lpfc_sli_update_full_ring - Update the chip attention register
James Smarte59058c2008-08-24 21:49:00 -04001496 * @phba: Pointer to HBA context object.
1497 * @pring: Pointer to driver SLI ring object.
1498 *
1499 * The caller is not required to hold any lock for calling this function.
1500 * This function updates the chip attention bits for the ring to inform firmware
1501 * that there are pending work to be done for this ring and requests an
1502 * interrupt when there is space available in the ring. This function is
1503 * called when the driver is unable to post more iocbs to the ring due
1504 * to unavailability of space in the ring.
1505 **/
dea31012005-04-17 16:05:31 -05001506static void
James Smart2e0fef82007-06-17 19:56:36 -05001507lpfc_sli_update_full_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -05001508{
1509 int ringno = pring->ringno;
1510
1511 pring->flag |= LPFC_CALL_RING_AVAILABLE;
1512
1513 wmb();
1514
1515 /*
1516 * Set ring 'ringno' to SET R0CE_REQ in Chip Att register.
1517 * The HBA will tell us when an IOCB entry is available.
1518 */
1519 writel((CA_R0ATT|CA_R0CE_REQ) << (ringno*4), phba->CAregaddr);
1520 readl(phba->CAregaddr); /* flush */
1521
1522 pring->stats.iocb_cmd_full++;
1523}
1524
James Smarte59058c2008-08-24 21:49:00 -04001525/**
James Smart3621a712009-04-06 18:47:14 -04001526 * lpfc_sli_update_ring - Update chip attention register
James Smarte59058c2008-08-24 21:49:00 -04001527 * @phba: Pointer to HBA context object.
1528 * @pring: Pointer to driver SLI ring object.
1529 *
1530 * This function updates the chip attention register bit for the
1531 * given ring to inform HBA that there is more work to be done
1532 * in this ring. The caller is not required to hold any lock.
1533 **/
dea31012005-04-17 16:05:31 -05001534static void
James Smart2e0fef82007-06-17 19:56:36 -05001535lpfc_sli_update_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -05001536{
1537 int ringno = pring->ringno;
1538
1539 /*
1540 * Tell the HBA that there is work to do in this ring.
1541 */
James Smart34b02dc2008-08-24 21:49:55 -04001542 if (!(phba->sli3_options & LPFC_SLI3_CRP_ENABLED)) {
1543 wmb();
1544 writel(CA_R0ATT << (ringno * 4), phba->CAregaddr);
1545 readl(phba->CAregaddr); /* flush */
1546 }
dea31012005-04-17 16:05:31 -05001547}
1548
James Smarte59058c2008-08-24 21:49:00 -04001549/**
James Smart3621a712009-04-06 18:47:14 -04001550 * lpfc_sli_resume_iocb - Process iocbs in the txq
James Smarte59058c2008-08-24 21:49:00 -04001551 * @phba: Pointer to HBA context object.
1552 * @pring: Pointer to driver SLI ring object.
1553 *
1554 * This function is called with hbalock held to post pending iocbs
1555 * in the txq to the firmware. This function is called when driver
1556 * detects space available in the ring.
1557 **/
dea31012005-04-17 16:05:31 -05001558static void
James Smart2e0fef82007-06-17 19:56:36 -05001559lpfc_sli_resume_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -05001560{
1561 IOCB_t *iocb;
1562 struct lpfc_iocbq *nextiocb;
1563
1564 /*
1565 * Check to see if:
1566 * (a) there is anything on the txq to send
1567 * (b) link is up
1568 * (c) link attention events can be processed (fcp ring only)
1569 * (d) IOCB processing is not blocked by the outstanding mbox command.
1570 */
1571 if (pring->txq_cnt &&
James Smart2e0fef82007-06-17 19:56:36 -05001572 lpfc_is_link_up(phba) &&
dea31012005-04-17 16:05:31 -05001573 (pring->ringno != phba->sli.fcp_ring ||
James Smart0b727fe2007-10-27 13:37:25 -04001574 phba->sli.sli_flag & LPFC_PROCESS_LA)) {
dea31012005-04-17 16:05:31 -05001575
1576 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
1577 (nextiocb = lpfc_sli_ringtx_get(phba, pring)))
1578 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
1579
1580 if (iocb)
1581 lpfc_sli_update_ring(phba, pring);
1582 else
1583 lpfc_sli_update_full_ring(phba, pring);
1584 }
1585
1586 return;
1587}
1588
James Smarte59058c2008-08-24 21:49:00 -04001589/**
James Smart3621a712009-04-06 18:47:14 -04001590 * lpfc_sli_next_hbq_slot - Get next hbq entry for the HBQ
James Smarte59058c2008-08-24 21:49:00 -04001591 * @phba: Pointer to HBA context object.
1592 * @hbqno: HBQ number.
1593 *
1594 * This function is called with hbalock held to get the next
1595 * available slot for the given HBQ. If there is free slot
1596 * available for the HBQ it will return pointer to the next available
1597 * HBQ entry else it will return NULL.
1598 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01001599static struct lpfc_hbq_entry *
James Smarted957682007-06-17 19:56:37 -05001600lpfc_sli_next_hbq_slot(struct lpfc_hba *phba, uint32_t hbqno)
1601{
1602 struct hbq_s *hbqp = &phba->hbqs[hbqno];
1603
1604 if (hbqp->next_hbqPutIdx == hbqp->hbqPutIdx &&
1605 ++hbqp->next_hbqPutIdx >= hbqp->entry_count)
1606 hbqp->next_hbqPutIdx = 0;
1607
1608 if (unlikely(hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)) {
James Smart92d7f7b2007-06-17 19:56:38 -05001609 uint32_t raw_index = phba->hbq_get[hbqno];
James Smarted957682007-06-17 19:56:37 -05001610 uint32_t getidx = le32_to_cpu(raw_index);
1611
1612 hbqp->local_hbqGetIdx = getidx;
1613
1614 if (unlikely(hbqp->local_hbqGetIdx >= hbqp->entry_count)) {
1615 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -05001616 LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04001617 "1802 HBQ %d: local_hbqGetIdx "
James Smarted957682007-06-17 19:56:37 -05001618 "%u is > than hbqp->entry_count %u\n",
James Smarte8b62012007-08-02 11:10:09 -04001619 hbqno, hbqp->local_hbqGetIdx,
James Smarted957682007-06-17 19:56:37 -05001620 hbqp->entry_count);
1621
1622 phba->link_state = LPFC_HBA_ERROR;
1623 return NULL;
1624 }
1625
1626 if (hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)
1627 return NULL;
1628 }
1629
James Smart51ef4c22007-08-02 11:10:31 -04001630 return (struct lpfc_hbq_entry *) phba->hbqs[hbqno].hbq_virt +
1631 hbqp->hbqPutIdx;
James Smarted957682007-06-17 19:56:37 -05001632}
1633
James Smarte59058c2008-08-24 21:49:00 -04001634/**
James Smart3621a712009-04-06 18:47:14 -04001635 * lpfc_sli_hbqbuf_free_all - Free all the hbq buffers
James Smarte59058c2008-08-24 21:49:00 -04001636 * @phba: Pointer to HBA context object.
1637 *
1638 * This function is called with no lock held to free all the
1639 * hbq buffers while uninitializing the SLI interface. It also
1640 * frees the HBQ buffers returned by the firmware but not yet
1641 * processed by the upper layers.
1642 **/
James Smarted957682007-06-17 19:56:37 -05001643void
1644lpfc_sli_hbqbuf_free_all(struct lpfc_hba *phba)
1645{
James Smart92d7f7b2007-06-17 19:56:38 -05001646 struct lpfc_dmabuf *dmabuf, *next_dmabuf;
1647 struct hbq_dmabuf *hbq_buf;
James Smart3163f722008-02-08 18:50:25 -05001648 unsigned long flags;
James Smart51ef4c22007-08-02 11:10:31 -04001649 int i, hbq_count;
James Smart3163f722008-02-08 18:50:25 -05001650 uint32_t hbqno;
James Smarted957682007-06-17 19:56:37 -05001651
James Smart51ef4c22007-08-02 11:10:31 -04001652 hbq_count = lpfc_sli_hbq_count();
James Smarted957682007-06-17 19:56:37 -05001653 /* Return all memory used by all HBQs */
James Smart3163f722008-02-08 18:50:25 -05001654 spin_lock_irqsave(&phba->hbalock, flags);
James Smart51ef4c22007-08-02 11:10:31 -04001655 for (i = 0; i < hbq_count; ++i) {
1656 list_for_each_entry_safe(dmabuf, next_dmabuf,
1657 &phba->hbqs[i].hbq_buffer_list, list) {
1658 hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
1659 list_del(&hbq_buf->dbuf.list);
1660 (phba->hbqs[i].hbq_free_buffer)(phba, hbq_buf);
1661 }
James Smarta8adb832007-10-27 13:37:53 -04001662 phba->hbqs[i].buffer_count = 0;
James Smarted957682007-06-17 19:56:37 -05001663 }
James Smart3163f722008-02-08 18:50:25 -05001664 /* Return all HBQ buffer that are in-fly */
James Smart3772a992009-05-22 14:50:54 -04001665 list_for_each_entry_safe(dmabuf, next_dmabuf, &phba->rb_pend_list,
1666 list) {
James Smart3163f722008-02-08 18:50:25 -05001667 hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
1668 list_del(&hbq_buf->dbuf.list);
1669 if (hbq_buf->tag == -1) {
1670 (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
1671 (phba, hbq_buf);
1672 } else {
1673 hbqno = hbq_buf->tag >> 16;
1674 if (hbqno >= LPFC_MAX_HBQS)
1675 (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
1676 (phba, hbq_buf);
1677 else
1678 (phba->hbqs[hbqno].hbq_free_buffer)(phba,
1679 hbq_buf);
1680 }
1681 }
1682
1683 /* Mark the HBQs not in use */
1684 phba->hbq_in_use = 0;
1685 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smarted957682007-06-17 19:56:37 -05001686}
1687
James Smarte59058c2008-08-24 21:49:00 -04001688/**
James Smart3621a712009-04-06 18:47:14 -04001689 * lpfc_sli_hbq_to_firmware - Post the hbq buffer to firmware
James Smarte59058c2008-08-24 21:49:00 -04001690 * @phba: Pointer to HBA context object.
1691 * @hbqno: HBQ number.
1692 * @hbq_buf: Pointer to HBQ buffer.
1693 *
1694 * This function is called with the hbalock held to post a
1695 * hbq buffer to the firmware. If the function finds an empty
1696 * slot in the HBQ, it will post the buffer. The function will return
1697 * pointer to the hbq entry if it successfully post the buffer
1698 * else it will return NULL.
1699 **/
James Smart3772a992009-05-22 14:50:54 -04001700static int
James Smarted957682007-06-17 19:56:37 -05001701lpfc_sli_hbq_to_firmware(struct lpfc_hba *phba, uint32_t hbqno,
James Smart92d7f7b2007-06-17 19:56:38 -05001702 struct hbq_dmabuf *hbq_buf)
James Smarted957682007-06-17 19:56:37 -05001703{
James Smart3772a992009-05-22 14:50:54 -04001704 return phba->lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buf);
1705}
1706
1707/**
1708 * lpfc_sli_hbq_to_firmware_s3 - Post the hbq buffer to SLI3 firmware
1709 * @phba: Pointer to HBA context object.
1710 * @hbqno: HBQ number.
1711 * @hbq_buf: Pointer to HBQ buffer.
1712 *
1713 * This function is called with the hbalock held to post a hbq buffer to the
1714 * firmware. If the function finds an empty slot in the HBQ, it will post the
1715 * buffer and place it on the hbq_buffer_list. The function will return zero if
1716 * it successfully post the buffer else it will return an error.
1717 **/
1718static int
1719lpfc_sli_hbq_to_firmware_s3(struct lpfc_hba *phba, uint32_t hbqno,
1720 struct hbq_dmabuf *hbq_buf)
1721{
James Smarted957682007-06-17 19:56:37 -05001722 struct lpfc_hbq_entry *hbqe;
James Smart92d7f7b2007-06-17 19:56:38 -05001723 dma_addr_t physaddr = hbq_buf->dbuf.phys;
James Smarted957682007-06-17 19:56:37 -05001724
1725 /* Get next HBQ entry slot to use */
1726 hbqe = lpfc_sli_next_hbq_slot(phba, hbqno);
1727 if (hbqe) {
1728 struct hbq_s *hbqp = &phba->hbqs[hbqno];
1729
James Smart92d7f7b2007-06-17 19:56:38 -05001730 hbqe->bde.addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
1731 hbqe->bde.addrLow = le32_to_cpu(putPaddrLow(physaddr));
James Smart51ef4c22007-08-02 11:10:31 -04001732 hbqe->bde.tus.f.bdeSize = hbq_buf->size;
James Smarted957682007-06-17 19:56:37 -05001733 hbqe->bde.tus.f.bdeFlags = 0;
James Smart92d7f7b2007-06-17 19:56:38 -05001734 hbqe->bde.tus.w = le32_to_cpu(hbqe->bde.tus.w);
1735 hbqe->buffer_tag = le32_to_cpu(hbq_buf->tag);
1736 /* Sync SLIM */
James Smarted957682007-06-17 19:56:37 -05001737 hbqp->hbqPutIdx = hbqp->next_hbqPutIdx;
1738 writel(hbqp->hbqPutIdx, phba->hbq_put + hbqno);
James Smart92d7f7b2007-06-17 19:56:38 -05001739 /* flush */
James Smarted957682007-06-17 19:56:37 -05001740 readl(phba->hbq_put + hbqno);
James Smart51ef4c22007-08-02 11:10:31 -04001741 list_add_tail(&hbq_buf->dbuf.list, &hbqp->hbq_buffer_list);
James Smart3772a992009-05-22 14:50:54 -04001742 return 0;
1743 } else
1744 return -ENOMEM;
James Smarted957682007-06-17 19:56:37 -05001745}
1746
James Smart4f774512009-05-22 14:52:35 -04001747/**
1748 * lpfc_sli_hbq_to_firmware_s4 - Post the hbq buffer to SLI4 firmware
1749 * @phba: Pointer to HBA context object.
1750 * @hbqno: HBQ number.
1751 * @hbq_buf: Pointer to HBQ buffer.
1752 *
1753 * This function is called with the hbalock held to post an RQE to the SLI4
1754 * firmware. If able to post the RQE to the RQ it will queue the hbq entry to
1755 * the hbq_buffer_list and return zero, otherwise it will return an error.
1756 **/
1757static int
1758lpfc_sli_hbq_to_firmware_s4(struct lpfc_hba *phba, uint32_t hbqno,
1759 struct hbq_dmabuf *hbq_buf)
1760{
1761 int rc;
1762 struct lpfc_rqe hrqe;
1763 struct lpfc_rqe drqe;
1764
1765 hrqe.address_lo = putPaddrLow(hbq_buf->hbuf.phys);
1766 hrqe.address_hi = putPaddrHigh(hbq_buf->hbuf.phys);
1767 drqe.address_lo = putPaddrLow(hbq_buf->dbuf.phys);
1768 drqe.address_hi = putPaddrHigh(hbq_buf->dbuf.phys);
1769 rc = lpfc_sli4_rq_put(phba->sli4_hba.hdr_rq, phba->sli4_hba.dat_rq,
1770 &hrqe, &drqe);
1771 if (rc < 0)
1772 return rc;
1773 hbq_buf->tag = rc;
1774 list_add_tail(&hbq_buf->dbuf.list, &phba->hbqs[hbqno].hbq_buffer_list);
1775 return 0;
1776}
1777
James Smarte59058c2008-08-24 21:49:00 -04001778/* HBQ for ELS and CT traffic. */
James Smart92d7f7b2007-06-17 19:56:38 -05001779static struct lpfc_hbq_init lpfc_els_hbq = {
1780 .rn = 1,
James Smartdef9c7a2009-12-21 17:02:28 -05001781 .entry_count = 256,
James Smart92d7f7b2007-06-17 19:56:38 -05001782 .mask_count = 0,
1783 .profile = 0,
James Smart51ef4c22007-08-02 11:10:31 -04001784 .ring_mask = (1 << LPFC_ELS_RING),
James Smart92d7f7b2007-06-17 19:56:38 -05001785 .buffer_count = 0,
James Smarta257bf92009-04-06 18:48:10 -04001786 .init_count = 40,
1787 .add_count = 40,
James Smart92d7f7b2007-06-17 19:56:38 -05001788};
James Smarted957682007-06-17 19:56:37 -05001789
James Smarte59058c2008-08-24 21:49:00 -04001790/* HBQ for the extra ring if needed */
James Smart51ef4c22007-08-02 11:10:31 -04001791static struct lpfc_hbq_init lpfc_extra_hbq = {
1792 .rn = 1,
1793 .entry_count = 200,
1794 .mask_count = 0,
1795 .profile = 0,
1796 .ring_mask = (1 << LPFC_EXTRA_RING),
1797 .buffer_count = 0,
1798 .init_count = 0,
1799 .add_count = 5,
1800};
1801
James Smarte59058c2008-08-24 21:49:00 -04001802/* Array of HBQs */
James Smart78b2d852007-08-02 11:10:21 -04001803struct lpfc_hbq_init *lpfc_hbq_defs[] = {
James Smart92d7f7b2007-06-17 19:56:38 -05001804 &lpfc_els_hbq,
James Smart51ef4c22007-08-02 11:10:31 -04001805 &lpfc_extra_hbq,
James Smart92d7f7b2007-06-17 19:56:38 -05001806};
1807
James Smarte59058c2008-08-24 21:49:00 -04001808/**
James Smart3621a712009-04-06 18:47:14 -04001809 * lpfc_sli_hbqbuf_fill_hbqs - Post more hbq buffers to HBQ
James Smarte59058c2008-08-24 21:49:00 -04001810 * @phba: Pointer to HBA context object.
1811 * @hbqno: HBQ number.
1812 * @count: Number of HBQ buffers to be posted.
1813 *
James Smartd7c255b2008-08-24 21:50:00 -04001814 * This function is called with no lock held to post more hbq buffers to the
1815 * given HBQ. The function returns the number of HBQ buffers successfully
1816 * posted.
James Smarte59058c2008-08-24 21:49:00 -04001817 **/
James Smart311464e2007-08-02 11:10:37 -04001818static int
James Smart92d7f7b2007-06-17 19:56:38 -05001819lpfc_sli_hbqbuf_fill_hbqs(struct lpfc_hba *phba, uint32_t hbqno, uint32_t count)
1820{
James Smartd7c255b2008-08-24 21:50:00 -04001821 uint32_t i, posted = 0;
James Smart3163f722008-02-08 18:50:25 -05001822 unsigned long flags;
James Smart92d7f7b2007-06-17 19:56:38 -05001823 struct hbq_dmabuf *hbq_buffer;
James Smartd7c255b2008-08-24 21:50:00 -04001824 LIST_HEAD(hbq_buf_list);
Matthew Wilcoxeafe1df2008-02-21 05:44:33 -07001825 if (!phba->hbqs[hbqno].hbq_alloc_buffer)
James Smart51ef4c22007-08-02 11:10:31 -04001826 return 0;
James Smart51ef4c22007-08-02 11:10:31 -04001827
James Smartd7c255b2008-08-24 21:50:00 -04001828 if ((phba->hbqs[hbqno].buffer_count + count) >
1829 lpfc_hbq_defs[hbqno]->entry_count)
1830 count = lpfc_hbq_defs[hbqno]->entry_count -
1831 phba->hbqs[hbqno].buffer_count;
1832 if (!count)
1833 return 0;
1834 /* Allocate HBQ entries */
1835 for (i = 0; i < count; i++) {
1836 hbq_buffer = (phba->hbqs[hbqno].hbq_alloc_buffer)(phba);
1837 if (!hbq_buffer)
1838 break;
1839 list_add_tail(&hbq_buffer->dbuf.list, &hbq_buf_list);
1840 }
James Smart3163f722008-02-08 18:50:25 -05001841 /* Check whether HBQ is still in use */
1842 spin_lock_irqsave(&phba->hbalock, flags);
Matthew Wilcoxeafe1df2008-02-21 05:44:33 -07001843 if (!phba->hbq_in_use)
James Smartd7c255b2008-08-24 21:50:00 -04001844 goto err;
1845 while (!list_empty(&hbq_buf_list)) {
1846 list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
1847 dbuf.list);
1848 hbq_buffer->tag = (phba->hbqs[hbqno].buffer_count |
1849 (hbqno << 16));
James Smart3772a992009-05-22 14:50:54 -04001850 if (!lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer)) {
James Smarta8adb832007-10-27 13:37:53 -04001851 phba->hbqs[hbqno].buffer_count++;
James Smartd7c255b2008-08-24 21:50:00 -04001852 posted++;
1853 } else
James Smart51ef4c22007-08-02 11:10:31 -04001854 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
James Smart92d7f7b2007-06-17 19:56:38 -05001855 }
James Smart3163f722008-02-08 18:50:25 -05001856 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smartd7c255b2008-08-24 21:50:00 -04001857 return posted;
1858err:
1859 spin_unlock_irqrestore(&phba->hbalock, flags);
1860 while (!list_empty(&hbq_buf_list)) {
1861 list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
1862 dbuf.list);
1863 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
1864 }
James Smart92d7f7b2007-06-17 19:56:38 -05001865 return 0;
James Smarted957682007-06-17 19:56:37 -05001866}
1867
James Smarte59058c2008-08-24 21:49:00 -04001868/**
James Smart3621a712009-04-06 18:47:14 -04001869 * lpfc_sli_hbqbuf_add_hbqs - Post more HBQ buffers to firmware
James Smarte59058c2008-08-24 21:49:00 -04001870 * @phba: Pointer to HBA context object.
1871 * @qno: HBQ number.
1872 *
1873 * This function posts more buffers to the HBQ. This function
James Smartd7c255b2008-08-24 21:50:00 -04001874 * is called with no lock held. The function returns the number of HBQ entries
1875 * successfully allocated.
James Smarte59058c2008-08-24 21:49:00 -04001876 **/
James Smarted957682007-06-17 19:56:37 -05001877int
James Smart92d7f7b2007-06-17 19:56:38 -05001878lpfc_sli_hbqbuf_add_hbqs(struct lpfc_hba *phba, uint32_t qno)
James Smarted957682007-06-17 19:56:37 -05001879{
James Smartdef9c7a2009-12-21 17:02:28 -05001880 if (phba->sli_rev == LPFC_SLI_REV4)
1881 return 0;
1882 else
1883 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
1884 lpfc_hbq_defs[qno]->add_count);
James Smarted957682007-06-17 19:56:37 -05001885}
1886
James Smarte59058c2008-08-24 21:49:00 -04001887/**
James Smart3621a712009-04-06 18:47:14 -04001888 * lpfc_sli_hbqbuf_init_hbqs - Post initial buffers to the HBQ
James Smarte59058c2008-08-24 21:49:00 -04001889 * @phba: Pointer to HBA context object.
1890 * @qno: HBQ queue number.
1891 *
1892 * This function is called from SLI initialization code path with
1893 * no lock held to post initial HBQ buffers to firmware. The
James Smartd7c255b2008-08-24 21:50:00 -04001894 * function returns the number of HBQ entries successfully allocated.
James Smarte59058c2008-08-24 21:49:00 -04001895 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01001896static int
James Smart92d7f7b2007-06-17 19:56:38 -05001897lpfc_sli_hbqbuf_init_hbqs(struct lpfc_hba *phba, uint32_t qno)
James Smarted957682007-06-17 19:56:37 -05001898{
James Smartdef9c7a2009-12-21 17:02:28 -05001899 if (phba->sli_rev == LPFC_SLI_REV4)
1900 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
James Smart73d91e52011-10-10 21:32:10 -04001901 lpfc_hbq_defs[qno]->entry_count);
James Smartdef9c7a2009-12-21 17:02:28 -05001902 else
1903 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
1904 lpfc_hbq_defs[qno]->init_count);
James Smarted957682007-06-17 19:56:37 -05001905}
1906
James Smarte59058c2008-08-24 21:49:00 -04001907/**
James Smart3772a992009-05-22 14:50:54 -04001908 * lpfc_sli_hbqbuf_get - Remove the first hbq off of an hbq list
1909 * @phba: Pointer to HBA context object.
1910 * @hbqno: HBQ number.
1911 *
1912 * This function removes the first hbq buffer on an hbq list and returns a
1913 * pointer to that buffer. If it finds no buffers on the list it returns NULL.
1914 **/
1915static struct hbq_dmabuf *
1916lpfc_sli_hbqbuf_get(struct list_head *rb_list)
1917{
1918 struct lpfc_dmabuf *d_buf;
1919
1920 list_remove_head(rb_list, d_buf, struct lpfc_dmabuf, list);
1921 if (!d_buf)
1922 return NULL;
1923 return container_of(d_buf, struct hbq_dmabuf, dbuf);
1924}
1925
1926/**
James Smart3621a712009-04-06 18:47:14 -04001927 * lpfc_sli_hbqbuf_find - Find the hbq buffer associated with a tag
James Smarte59058c2008-08-24 21:49:00 -04001928 * @phba: Pointer to HBA context object.
1929 * @tag: Tag of the hbq buffer.
1930 *
1931 * This function is called with hbalock held. This function searches
1932 * for the hbq buffer associated with the given tag in the hbq buffer
1933 * list. If it finds the hbq buffer, it returns the hbq_buffer other wise
1934 * it returns NULL.
1935 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01001936static struct hbq_dmabuf *
James Smarted957682007-06-17 19:56:37 -05001937lpfc_sli_hbqbuf_find(struct lpfc_hba *phba, uint32_t tag)
1938{
James Smart92d7f7b2007-06-17 19:56:38 -05001939 struct lpfc_dmabuf *d_buf;
1940 struct hbq_dmabuf *hbq_buf;
James Smart51ef4c22007-08-02 11:10:31 -04001941 uint32_t hbqno;
James Smarted957682007-06-17 19:56:37 -05001942
James Smart51ef4c22007-08-02 11:10:31 -04001943 hbqno = tag >> 16;
Jesper Juhla0a74e452007-08-09 20:47:15 +02001944 if (hbqno >= LPFC_MAX_HBQS)
James Smart51ef4c22007-08-02 11:10:31 -04001945 return NULL;
1946
James Smart3772a992009-05-22 14:50:54 -04001947 spin_lock_irq(&phba->hbalock);
James Smart51ef4c22007-08-02 11:10:31 -04001948 list_for_each_entry(d_buf, &phba->hbqs[hbqno].hbq_buffer_list, list) {
James Smart92d7f7b2007-06-17 19:56:38 -05001949 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
James Smart51ef4c22007-08-02 11:10:31 -04001950 if (hbq_buf->tag == tag) {
James Smart3772a992009-05-22 14:50:54 -04001951 spin_unlock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -05001952 return hbq_buf;
James Smarted957682007-06-17 19:56:37 -05001953 }
1954 }
James Smart3772a992009-05-22 14:50:54 -04001955 spin_unlock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -05001956 lpfc_printf_log(phba, KERN_ERR, LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04001957 "1803 Bad hbq tag. Data: x%x x%x\n",
James Smarta8adb832007-10-27 13:37:53 -04001958 tag, phba->hbqs[tag >> 16].buffer_count);
James Smart92d7f7b2007-06-17 19:56:38 -05001959 return NULL;
James Smarted957682007-06-17 19:56:37 -05001960}
1961
James Smarte59058c2008-08-24 21:49:00 -04001962/**
James Smart3621a712009-04-06 18:47:14 -04001963 * lpfc_sli_free_hbq - Give back the hbq buffer to firmware
James Smarte59058c2008-08-24 21:49:00 -04001964 * @phba: Pointer to HBA context object.
1965 * @hbq_buffer: Pointer to HBQ buffer.
1966 *
1967 * This function is called with hbalock. This function gives back
1968 * the hbq buffer to firmware. If the HBQ does not have space to
1969 * post the buffer, it will free the buffer.
1970 **/
James Smarted957682007-06-17 19:56:37 -05001971void
James Smart51ef4c22007-08-02 11:10:31 -04001972lpfc_sli_free_hbq(struct lpfc_hba *phba, struct hbq_dmabuf *hbq_buffer)
James Smarted957682007-06-17 19:56:37 -05001973{
1974 uint32_t hbqno;
1975
James Smart51ef4c22007-08-02 11:10:31 -04001976 if (hbq_buffer) {
1977 hbqno = hbq_buffer->tag >> 16;
James Smart3772a992009-05-22 14:50:54 -04001978 if (lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer))
James Smart51ef4c22007-08-02 11:10:31 -04001979 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
James Smarted957682007-06-17 19:56:37 -05001980 }
1981}
1982
James Smarte59058c2008-08-24 21:49:00 -04001983/**
James Smart3621a712009-04-06 18:47:14 -04001984 * lpfc_sli_chk_mbx_command - Check if the mailbox is a legitimate mailbox
James Smarte59058c2008-08-24 21:49:00 -04001985 * @mbxCommand: mailbox command code.
1986 *
1987 * This function is called by the mailbox event handler function to verify
1988 * that the completed mailbox command is a legitimate mailbox command. If the
1989 * completed mailbox is not known to the function, it will return MBX_SHUTDOWN
1990 * and the mailbox event handler will take the HBA offline.
1991 **/
dea31012005-04-17 16:05:31 -05001992static int
1993lpfc_sli_chk_mbx_command(uint8_t mbxCommand)
1994{
1995 uint8_t ret;
1996
1997 switch (mbxCommand) {
1998 case MBX_LOAD_SM:
1999 case MBX_READ_NV:
2000 case MBX_WRITE_NV:
James Smarta8adb832007-10-27 13:37:53 -04002001 case MBX_WRITE_VPARMS:
dea31012005-04-17 16:05:31 -05002002 case MBX_RUN_BIU_DIAG:
2003 case MBX_INIT_LINK:
2004 case MBX_DOWN_LINK:
2005 case MBX_CONFIG_LINK:
2006 case MBX_CONFIG_RING:
2007 case MBX_RESET_RING:
2008 case MBX_READ_CONFIG:
2009 case MBX_READ_RCONFIG:
2010 case MBX_READ_SPARM:
2011 case MBX_READ_STATUS:
2012 case MBX_READ_RPI:
2013 case MBX_READ_XRI:
2014 case MBX_READ_REV:
2015 case MBX_READ_LNK_STAT:
2016 case MBX_REG_LOGIN:
2017 case MBX_UNREG_LOGIN:
dea31012005-04-17 16:05:31 -05002018 case MBX_CLEAR_LA:
2019 case MBX_DUMP_MEMORY:
2020 case MBX_DUMP_CONTEXT:
2021 case MBX_RUN_DIAGS:
2022 case MBX_RESTART:
2023 case MBX_UPDATE_CFG:
2024 case MBX_DOWN_LOAD:
2025 case MBX_DEL_LD_ENTRY:
2026 case MBX_RUN_PROGRAM:
2027 case MBX_SET_MASK:
James Smart09372822008-01-11 01:52:54 -05002028 case MBX_SET_VARIABLE:
dea31012005-04-17 16:05:31 -05002029 case MBX_UNREG_D_ID:
Jamie Wellnitz41415862006-02-28 19:25:27 -05002030 case MBX_KILL_BOARD:
dea31012005-04-17 16:05:31 -05002031 case MBX_CONFIG_FARP:
Jamie Wellnitz41415862006-02-28 19:25:27 -05002032 case MBX_BEACON:
dea31012005-04-17 16:05:31 -05002033 case MBX_LOAD_AREA:
2034 case MBX_RUN_BIU_DIAG64:
2035 case MBX_CONFIG_PORT:
2036 case MBX_READ_SPARM64:
2037 case MBX_READ_RPI64:
2038 case MBX_REG_LOGIN64:
James Smart76a95d72010-11-20 23:11:48 -05002039 case MBX_READ_TOPOLOGY:
James Smart09372822008-01-11 01:52:54 -05002040 case MBX_WRITE_WWN:
dea31012005-04-17 16:05:31 -05002041 case MBX_SET_DEBUG:
2042 case MBX_LOAD_EXP_ROM:
James Smart57127f12007-10-27 13:37:05 -04002043 case MBX_ASYNCEVT_ENABLE:
James Smart92d7f7b2007-06-17 19:56:38 -05002044 case MBX_REG_VPI:
2045 case MBX_UNREG_VPI:
James Smart858c9f62007-06-17 19:56:39 -05002046 case MBX_HEARTBEAT:
James Smart84774a42008-08-24 21:50:06 -04002047 case MBX_PORT_CAPABILITIES:
2048 case MBX_PORT_IOV_CONTROL:
James Smart04c68492009-05-22 14:52:52 -04002049 case MBX_SLI4_CONFIG:
2050 case MBX_SLI4_REQ_FTRS:
2051 case MBX_REG_FCFI:
2052 case MBX_UNREG_FCFI:
2053 case MBX_REG_VFI:
2054 case MBX_UNREG_VFI:
2055 case MBX_INIT_VPI:
2056 case MBX_INIT_VFI:
2057 case MBX_RESUME_RPI:
James Smartc7495932010-04-06 15:05:28 -04002058 case MBX_READ_EVENT_LOG_STATUS:
2059 case MBX_READ_EVENT_LOG:
James Smartdcf2a4e2010-09-29 11:18:53 -04002060 case MBX_SECURITY_MGMT:
2061 case MBX_AUTH_PORT:
dea31012005-04-17 16:05:31 -05002062 ret = mbxCommand;
2063 break;
2064 default:
2065 ret = MBX_SHUTDOWN;
2066 break;
2067 }
James Smart2e0fef82007-06-17 19:56:36 -05002068 return ret;
dea31012005-04-17 16:05:31 -05002069}
James Smarte59058c2008-08-24 21:49:00 -04002070
2071/**
James Smart3621a712009-04-06 18:47:14 -04002072 * lpfc_sli_wake_mbox_wait - lpfc_sli_issue_mbox_wait mbox completion handler
James Smarte59058c2008-08-24 21:49:00 -04002073 * @phba: Pointer to HBA context object.
2074 * @pmboxq: Pointer to mailbox command.
2075 *
2076 * This is completion handler function for mailbox commands issued from
2077 * lpfc_sli_issue_mbox_wait function. This function is called by the
2078 * mailbox event handler function with no lock held. This function
2079 * will wake up thread waiting on the wait queue pointed by context1
2080 * of the mailbox.
2081 **/
James Smart04c68492009-05-22 14:52:52 -04002082void
James Smart2e0fef82007-06-17 19:56:36 -05002083lpfc_sli_wake_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
dea31012005-04-17 16:05:31 -05002084{
2085 wait_queue_head_t *pdone_q;
James Smart858c9f62007-06-17 19:56:39 -05002086 unsigned long drvr_flag;
dea31012005-04-17 16:05:31 -05002087
2088 /*
2089 * If pdone_q is empty, the driver thread gave up waiting and
2090 * continued running.
2091 */
James Smart7054a602007-04-25 09:52:34 -04002092 pmboxq->mbox_flag |= LPFC_MBX_WAKE;
James Smart858c9f62007-06-17 19:56:39 -05002093 spin_lock_irqsave(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05002094 pdone_q = (wait_queue_head_t *) pmboxq->context1;
2095 if (pdone_q)
2096 wake_up_interruptible(pdone_q);
James Smart858c9f62007-06-17 19:56:39 -05002097 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05002098 return;
2099}
2100
James Smarte59058c2008-08-24 21:49:00 -04002101
2102/**
James Smart3621a712009-04-06 18:47:14 -04002103 * lpfc_sli_def_mbox_cmpl - Default mailbox completion handler
James Smarte59058c2008-08-24 21:49:00 -04002104 * @phba: Pointer to HBA context object.
2105 * @pmb: Pointer to mailbox object.
2106 *
2107 * This function is the default mailbox completion handler. It
2108 * frees the memory resources associated with the completed mailbox
2109 * command. If the completed command is a REG_LOGIN mailbox command,
2110 * this function will issue a UREG_LOGIN to re-claim the RPI.
2111 **/
dea31012005-04-17 16:05:31 -05002112void
James Smart2e0fef82007-06-17 19:56:36 -05002113lpfc_sli_def_mbox_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05002114{
James Smartd439d282010-09-29 11:18:45 -04002115 struct lpfc_vport *vport = pmb->vport;
dea31012005-04-17 16:05:31 -05002116 struct lpfc_dmabuf *mp;
James Smartd439d282010-09-29 11:18:45 -04002117 struct lpfc_nodelist *ndlp;
James Smart5af5eee2010-10-22 11:06:38 -04002118 struct Scsi_Host *shost;
James Smart04c68492009-05-22 14:52:52 -04002119 uint16_t rpi, vpi;
James Smart7054a602007-04-25 09:52:34 -04002120 int rc;
2121
dea31012005-04-17 16:05:31 -05002122 mp = (struct lpfc_dmabuf *) (pmb->context1);
James Smart7054a602007-04-25 09:52:34 -04002123
dea31012005-04-17 16:05:31 -05002124 if (mp) {
2125 lpfc_mbuf_free(phba, mp->virt, mp->phys);
2126 kfree(mp);
2127 }
James Smart7054a602007-04-25 09:52:34 -04002128
2129 /*
2130 * If a REG_LOGIN succeeded after node is destroyed or node
2131 * is in re-discovery driver need to cleanup the RPI.
2132 */
James Smart2e0fef82007-06-17 19:56:36 -05002133 if (!(phba->pport->load_flag & FC_UNLOADING) &&
James Smart04c68492009-05-22 14:52:52 -04002134 pmb->u.mb.mbxCommand == MBX_REG_LOGIN64 &&
2135 !pmb->u.mb.mbxStatus) {
2136 rpi = pmb->u.mb.un.varWords[0];
James Smart6d368e52011-05-24 11:44:12 -04002137 vpi = pmb->u.mb.un.varRegLogin.vpi;
James Smart04c68492009-05-22 14:52:52 -04002138 lpfc_unreg_login(phba, vpi, rpi, pmb);
James Smart92d7f7b2007-06-17 19:56:38 -05002139 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart7054a602007-04-25 09:52:34 -04002140 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
2141 if (rc != MBX_NOT_FINISHED)
2142 return;
2143 }
2144
James Smart695a8142010-01-26 23:08:03 -05002145 if ((pmb->u.mb.mbxCommand == MBX_REG_VPI) &&
2146 !(phba->pport->load_flag & FC_UNLOADING) &&
2147 !pmb->u.mb.mbxStatus) {
James Smart5af5eee2010-10-22 11:06:38 -04002148 shost = lpfc_shost_from_vport(vport);
2149 spin_lock_irq(shost->host_lock);
2150 vport->vpi_state |= LPFC_VPI_REGISTERED;
2151 vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
2152 spin_unlock_irq(shost->host_lock);
James Smart695a8142010-01-26 23:08:03 -05002153 }
2154
James Smartd439d282010-09-29 11:18:45 -04002155 if (pmb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
2156 ndlp = (struct lpfc_nodelist *)pmb->context2;
2157 lpfc_nlp_put(ndlp);
2158 pmb->context2 = NULL;
2159 }
2160
James Smartdcf2a4e2010-09-29 11:18:53 -04002161 /* Check security permission status on INIT_LINK mailbox command */
2162 if ((pmb->u.mb.mbxCommand == MBX_INIT_LINK) &&
2163 (pmb->u.mb.mbxStatus == MBXERR_SEC_NO_PERMISSION))
2164 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
2165 "2860 SLI authentication is required "
2166 "for INIT_LINK but has not done yet\n");
2167
James Smart04c68492009-05-22 14:52:52 -04002168 if (bf_get(lpfc_mqe_command, &pmb->u.mqe) == MBX_SLI4_CONFIG)
2169 lpfc_sli4_mbox_cmd_free(phba, pmb);
2170 else
2171 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05002172}
2173
James Smarte59058c2008-08-24 21:49:00 -04002174/**
James Smart3621a712009-04-06 18:47:14 -04002175 * lpfc_sli_handle_mb_event - Handle mailbox completions from firmware
James Smarte59058c2008-08-24 21:49:00 -04002176 * @phba: Pointer to HBA context object.
2177 *
2178 * This function is called with no lock held. This function processes all
2179 * the completed mailbox commands and gives it to upper layers. The interrupt
2180 * service routine processes mailbox completion interrupt and adds completed
2181 * mailbox commands to the mboxq_cmpl queue and signals the worker thread.
2182 * Worker thread call lpfc_sli_handle_mb_event, which will return the
2183 * completed mailbox commands in mboxq_cmpl queue to the upper layers. This
2184 * function returns the mailbox commands to the upper layer by calling the
2185 * completion handler function of each mailbox.
2186 **/
dea31012005-04-17 16:05:31 -05002187int
James Smart2e0fef82007-06-17 19:56:36 -05002188lpfc_sli_handle_mb_event(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05002189{
James Smart92d7f7b2007-06-17 19:56:38 -05002190 MAILBOX_t *pmbox;
dea31012005-04-17 16:05:31 -05002191 LPFC_MBOXQ_t *pmb;
James Smart92d7f7b2007-06-17 19:56:38 -05002192 int rc;
2193 LIST_HEAD(cmplq);
dea31012005-04-17 16:05:31 -05002194
2195 phba->sli.slistat.mbox_event++;
2196
James Smart92d7f7b2007-06-17 19:56:38 -05002197 /* Get all completed mailboxe buffers into the cmplq */
2198 spin_lock_irq(&phba->hbalock);
2199 list_splice_init(&phba->sli.mboxq_cmpl, &cmplq);
2200 spin_unlock_irq(&phba->hbalock);
2201
dea31012005-04-17 16:05:31 -05002202 /* Get a Mailbox buffer to setup mailbox commands for callback */
James Smart92d7f7b2007-06-17 19:56:38 -05002203 do {
2204 list_remove_head(&cmplq, pmb, LPFC_MBOXQ_t, list);
2205 if (pmb == NULL)
2206 break;
2207
James Smart04c68492009-05-22 14:52:52 -04002208 pmbox = &pmb->u.mb;
dea31012005-04-17 16:05:31 -05002209
James Smart858c9f62007-06-17 19:56:39 -05002210 if (pmbox->mbxCommand != MBX_HEARTBEAT) {
2211 if (pmb->vport) {
2212 lpfc_debugfs_disc_trc(pmb->vport,
2213 LPFC_DISC_TRC_MBOX_VPORT,
2214 "MBOX cmpl vport: cmd:x%x mb:x%x x%x",
2215 (uint32_t)pmbox->mbxCommand,
2216 pmbox->un.varWords[0],
2217 pmbox->un.varWords[1]);
2218 }
2219 else {
2220 lpfc_debugfs_disc_trc(phba->pport,
2221 LPFC_DISC_TRC_MBOX,
2222 "MBOX cmpl: cmd:x%x mb:x%x x%x",
2223 (uint32_t)pmbox->mbxCommand,
2224 pmbox->un.varWords[0],
2225 pmbox->un.varWords[1]);
2226 }
2227 }
2228
dea31012005-04-17 16:05:31 -05002229 /*
2230 * It is a fatal error if unknown mbox command completion.
2231 */
2232 if (lpfc_sli_chk_mbx_command(pmbox->mbxCommand) ==
2233 MBX_SHUTDOWN) {
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02002234 /* Unknown mailbox command compl */
James Smart92d7f7b2007-06-17 19:56:38 -05002235 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002236 "(%d):0323 Unknown Mailbox command "
James Smarta183a152011-10-10 21:32:43 -04002237 "x%x (x%x/x%x) Cmpl\n",
James Smart92d7f7b2007-06-17 19:56:38 -05002238 pmb->vport ? pmb->vport->vpi : 0,
James Smart04c68492009-05-22 14:52:52 -04002239 pmbox->mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04002240 lpfc_sli_config_mbox_subsys_get(phba,
2241 pmb),
2242 lpfc_sli_config_mbox_opcode_get(phba,
2243 pmb));
James Smart2e0fef82007-06-17 19:56:36 -05002244 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05002245 phba->work_hs = HS_FFER3;
2246 lpfc_handle_eratt(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05002247 continue;
dea31012005-04-17 16:05:31 -05002248 }
2249
dea31012005-04-17 16:05:31 -05002250 if (pmbox->mbxStatus) {
2251 phba->sli.slistat.mbox_stat_err++;
2252 if (pmbox->mbxStatus == MBXERR_NO_RESOURCES) {
2253 /* Mbox cmd cmpl error - RETRYing */
James Smart92d7f7b2007-06-17 19:56:38 -05002254 lpfc_printf_log(phba, KERN_INFO,
James Smarta183a152011-10-10 21:32:43 -04002255 LOG_MBOX | LOG_SLI,
2256 "(%d):0305 Mbox cmd cmpl "
2257 "error - RETRYing Data: x%x "
2258 "(x%x/x%x) x%x x%x x%x\n",
2259 pmb->vport ? pmb->vport->vpi : 0,
2260 pmbox->mbxCommand,
2261 lpfc_sli_config_mbox_subsys_get(phba,
2262 pmb),
2263 lpfc_sli_config_mbox_opcode_get(phba,
2264 pmb),
2265 pmbox->mbxStatus,
2266 pmbox->un.varWords[0],
2267 pmb->vport->port_state);
dea31012005-04-17 16:05:31 -05002268 pmbox->mbxStatus = 0;
2269 pmbox->mbxOwner = OWN_HOST;
dea31012005-04-17 16:05:31 -05002270 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
James Smart04c68492009-05-22 14:52:52 -04002271 if (rc != MBX_NOT_FINISHED)
James Smart92d7f7b2007-06-17 19:56:38 -05002272 continue;
dea31012005-04-17 16:05:31 -05002273 }
2274 }
2275
2276 /* Mailbox cmd <cmd> Cmpl <cmpl> */
James Smart92d7f7b2007-06-17 19:56:38 -05002277 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04002278 "(%d):0307 Mailbox cmd x%x (x%x/x%x) Cmpl x%p "
dea31012005-04-17 16:05:31 -05002279 "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 -05002280 pmb->vport ? pmb->vport->vpi : 0,
dea31012005-04-17 16:05:31 -05002281 pmbox->mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04002282 lpfc_sli_config_mbox_subsys_get(phba, pmb),
2283 lpfc_sli_config_mbox_opcode_get(phba, pmb),
dea31012005-04-17 16:05:31 -05002284 pmb->mbox_cmpl,
2285 *((uint32_t *) pmbox),
2286 pmbox->un.varWords[0],
2287 pmbox->un.varWords[1],
2288 pmbox->un.varWords[2],
2289 pmbox->un.varWords[3],
2290 pmbox->un.varWords[4],
2291 pmbox->un.varWords[5],
2292 pmbox->un.varWords[6],
2293 pmbox->un.varWords[7]);
2294
James Smart92d7f7b2007-06-17 19:56:38 -05002295 if (pmb->mbox_cmpl)
dea31012005-04-17 16:05:31 -05002296 pmb->mbox_cmpl(phba,pmb);
James Smart92d7f7b2007-06-17 19:56:38 -05002297 } while (1);
James Smart2e0fef82007-06-17 19:56:36 -05002298 return 0;
dea31012005-04-17 16:05:31 -05002299}
James Smart92d7f7b2007-06-17 19:56:38 -05002300
James Smarte59058c2008-08-24 21:49:00 -04002301/**
James Smart3621a712009-04-06 18:47:14 -04002302 * lpfc_sli_get_buff - Get the buffer associated with the buffer tag
James Smarte59058c2008-08-24 21:49:00 -04002303 * @phba: Pointer to HBA context object.
2304 * @pring: Pointer to driver SLI ring object.
2305 * @tag: buffer tag.
2306 *
2307 * This function is called with no lock held. When QUE_BUFTAG_BIT bit
2308 * is set in the tag the buffer is posted for a particular exchange,
2309 * the function will return the buffer without replacing the buffer.
2310 * If the buffer is for unsolicited ELS or CT traffic, this function
2311 * returns the buffer and also posts another buffer to the firmware.
2312 **/
James Smart76bb24e2007-10-27 13:38:00 -04002313static struct lpfc_dmabuf *
2314lpfc_sli_get_buff(struct lpfc_hba *phba,
James Smart9f1e1b52008-12-04 22:39:40 -05002315 struct lpfc_sli_ring *pring,
2316 uint32_t tag)
James Smart76bb24e2007-10-27 13:38:00 -04002317{
James Smart9f1e1b52008-12-04 22:39:40 -05002318 struct hbq_dmabuf *hbq_entry;
2319
James Smart76bb24e2007-10-27 13:38:00 -04002320 if (tag & QUE_BUFTAG_BIT)
2321 return lpfc_sli_ring_taggedbuf_get(phba, pring, tag);
James Smart9f1e1b52008-12-04 22:39:40 -05002322 hbq_entry = lpfc_sli_hbqbuf_find(phba, tag);
2323 if (!hbq_entry)
2324 return NULL;
2325 return &hbq_entry->dbuf;
James Smart76bb24e2007-10-27 13:38:00 -04002326}
James Smart57127f12007-10-27 13:37:05 -04002327
James Smart3772a992009-05-22 14:50:54 -04002328/**
2329 * lpfc_complete_unsol_iocb - Complete an unsolicited sequence
2330 * @phba: Pointer to HBA context object.
2331 * @pring: Pointer to driver SLI ring object.
2332 * @saveq: Pointer to the iocbq struct representing the sequence starting frame.
2333 * @fch_r_ctl: the r_ctl for the first frame of the sequence.
2334 * @fch_type: the type for the first frame of the sequence.
2335 *
2336 * This function is called with no lock held. This function uses the r_ctl and
2337 * type of the received sequence to find the correct callback function to call
2338 * to process the sequence.
2339 **/
2340static int
2341lpfc_complete_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2342 struct lpfc_iocbq *saveq, uint32_t fch_r_ctl,
2343 uint32_t fch_type)
2344{
2345 int i;
2346
2347 /* unSolicited Responses */
2348 if (pring->prt[0].profile) {
2349 if (pring->prt[0].lpfc_sli_rcv_unsol_event)
2350 (pring->prt[0].lpfc_sli_rcv_unsol_event) (phba, pring,
2351 saveq);
2352 return 1;
2353 }
2354 /* We must search, based on rctl / type
2355 for the right routine */
2356 for (i = 0; i < pring->num_mask; i++) {
2357 if ((pring->prt[i].rctl == fch_r_ctl) &&
2358 (pring->prt[i].type == fch_type)) {
2359 if (pring->prt[i].lpfc_sli_rcv_unsol_event)
2360 (pring->prt[i].lpfc_sli_rcv_unsol_event)
2361 (phba, pring, saveq);
2362 return 1;
2363 }
2364 }
2365 return 0;
2366}
James Smarte59058c2008-08-24 21:49:00 -04002367
2368/**
James Smart3621a712009-04-06 18:47:14 -04002369 * lpfc_sli_process_unsol_iocb - Unsolicited iocb handler
James Smarte59058c2008-08-24 21:49:00 -04002370 * @phba: Pointer to HBA context object.
2371 * @pring: Pointer to driver SLI ring object.
2372 * @saveq: Pointer to the unsolicited iocb.
2373 *
2374 * This function is called with no lock held by the ring event handler
2375 * when there is an unsolicited iocb posted to the response ring by the
2376 * firmware. This function gets the buffer associated with the iocbs
2377 * and calls the event handler for the ring. This function handles both
2378 * qring buffers and hbq buffers.
2379 * When the function returns 1 the caller can free the iocb object otherwise
2380 * upper layer functions will free the iocb objects.
2381 **/
dea31012005-04-17 16:05:31 -05002382static int
2383lpfc_sli_process_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2384 struct lpfc_iocbq *saveq)
2385{
2386 IOCB_t * irsp;
2387 WORD5 * w5p;
2388 uint32_t Rctl, Type;
James Smart3772a992009-05-22 14:50:54 -04002389 uint32_t match;
James Smart76bb24e2007-10-27 13:38:00 -04002390 struct lpfc_iocbq *iocbq;
James Smart3163f722008-02-08 18:50:25 -05002391 struct lpfc_dmabuf *dmzbuf;
dea31012005-04-17 16:05:31 -05002392
2393 match = 0;
2394 irsp = &(saveq->iocb);
James Smart57127f12007-10-27 13:37:05 -04002395
2396 if (irsp->ulpCommand == CMD_ASYNC_STATUS) {
2397 if (pring->lpfc_sli_rcv_async_status)
2398 pring->lpfc_sli_rcv_async_status(phba, pring, saveq);
2399 else
2400 lpfc_printf_log(phba,
2401 KERN_WARNING,
2402 LOG_SLI,
2403 "0316 Ring %d handler: unexpected "
2404 "ASYNC_STATUS iocb received evt_code "
2405 "0x%x\n",
2406 pring->ringno,
2407 irsp->un.asyncstat.evt_code);
2408 return 1;
2409 }
2410
James Smart3163f722008-02-08 18:50:25 -05002411 if ((irsp->ulpCommand == CMD_IOCB_RET_XRI64_CX) &&
2412 (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)) {
2413 if (irsp->ulpBdeCount > 0) {
2414 dmzbuf = lpfc_sli_get_buff(phba, pring,
2415 irsp->un.ulpWord[3]);
2416 lpfc_in_buf_free(phba, dmzbuf);
2417 }
2418
2419 if (irsp->ulpBdeCount > 1) {
2420 dmzbuf = lpfc_sli_get_buff(phba, pring,
2421 irsp->unsli3.sli3Words[3]);
2422 lpfc_in_buf_free(phba, dmzbuf);
2423 }
2424
2425 if (irsp->ulpBdeCount > 2) {
2426 dmzbuf = lpfc_sli_get_buff(phba, pring,
2427 irsp->unsli3.sli3Words[7]);
2428 lpfc_in_buf_free(phba, dmzbuf);
2429 }
2430
2431 return 1;
2432 }
2433
James Smart92d7f7b2007-06-17 19:56:38 -05002434 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
James Smart76bb24e2007-10-27 13:38:00 -04002435 if (irsp->ulpBdeCount != 0) {
2436 saveq->context2 = lpfc_sli_get_buff(phba, pring,
James Smart92d7f7b2007-06-17 19:56:38 -05002437 irsp->un.ulpWord[3]);
James Smart76bb24e2007-10-27 13:38:00 -04002438 if (!saveq->context2)
2439 lpfc_printf_log(phba,
2440 KERN_ERR,
2441 LOG_SLI,
2442 "0341 Ring %d Cannot find buffer for "
2443 "an unsolicited iocb. tag 0x%x\n",
2444 pring->ringno,
2445 irsp->un.ulpWord[3]);
James Smart76bb24e2007-10-27 13:38:00 -04002446 }
2447 if (irsp->ulpBdeCount == 2) {
2448 saveq->context3 = lpfc_sli_get_buff(phba, pring,
James Smart51ef4c22007-08-02 11:10:31 -04002449 irsp->unsli3.sli3Words[7]);
James Smart76bb24e2007-10-27 13:38:00 -04002450 if (!saveq->context3)
2451 lpfc_printf_log(phba,
2452 KERN_ERR,
2453 LOG_SLI,
2454 "0342 Ring %d Cannot find buffer for an"
2455 " unsolicited iocb. tag 0x%x\n",
2456 pring->ringno,
2457 irsp->unsli3.sli3Words[7]);
2458 }
2459 list_for_each_entry(iocbq, &saveq->list, list) {
James Smart76bb24e2007-10-27 13:38:00 -04002460 irsp = &(iocbq->iocb);
James Smart76bb24e2007-10-27 13:38:00 -04002461 if (irsp->ulpBdeCount != 0) {
2462 iocbq->context2 = lpfc_sli_get_buff(phba, pring,
2463 irsp->un.ulpWord[3]);
James Smart9c2face2008-01-11 01:53:18 -05002464 if (!iocbq->context2)
James Smart76bb24e2007-10-27 13:38:00 -04002465 lpfc_printf_log(phba,
2466 KERN_ERR,
2467 LOG_SLI,
2468 "0343 Ring %d Cannot find "
2469 "buffer for an unsolicited iocb"
2470 ". tag 0x%x\n", pring->ringno,
2471 irsp->un.ulpWord[3]);
2472 }
2473 if (irsp->ulpBdeCount == 2) {
2474 iocbq->context3 = lpfc_sli_get_buff(phba, pring,
2475 irsp->unsli3.sli3Words[7]);
James Smart9c2face2008-01-11 01:53:18 -05002476 if (!iocbq->context3)
James Smart76bb24e2007-10-27 13:38:00 -04002477 lpfc_printf_log(phba,
2478 KERN_ERR,
2479 LOG_SLI,
2480 "0344 Ring %d Cannot find "
2481 "buffer for an unsolicited "
2482 "iocb. tag 0x%x\n",
2483 pring->ringno,
2484 irsp->unsli3.sli3Words[7]);
2485 }
2486 }
James Smart92d7f7b2007-06-17 19:56:38 -05002487 }
James Smart9c2face2008-01-11 01:53:18 -05002488 if (irsp->ulpBdeCount != 0 &&
2489 (irsp->ulpCommand == CMD_IOCB_RCV_CONT64_CX ||
2490 irsp->ulpStatus == IOSTAT_INTERMED_RSP)) {
2491 int found = 0;
2492
2493 /* search continue save q for same XRI */
2494 list_for_each_entry(iocbq, &pring->iocb_continue_saveq, clist) {
James Smart7851fe22011-07-22 18:36:52 -04002495 if (iocbq->iocb.unsli3.rcvsli3.ox_id ==
2496 saveq->iocb.unsli3.rcvsli3.ox_id) {
James Smart9c2face2008-01-11 01:53:18 -05002497 list_add_tail(&saveq->list, &iocbq->list);
2498 found = 1;
2499 break;
2500 }
2501 }
2502 if (!found)
2503 list_add_tail(&saveq->clist,
2504 &pring->iocb_continue_saveq);
2505 if (saveq->iocb.ulpStatus != IOSTAT_INTERMED_RSP) {
2506 list_del_init(&iocbq->clist);
2507 saveq = iocbq;
2508 irsp = &(saveq->iocb);
2509 } else
2510 return 0;
2511 }
2512 if ((irsp->ulpCommand == CMD_RCV_ELS_REQ64_CX) ||
2513 (irsp->ulpCommand == CMD_RCV_ELS_REQ_CX) ||
2514 (irsp->ulpCommand == CMD_IOCB_RCV_ELS64_CX)) {
James Smart6a9c52c2009-10-02 15:16:51 -04002515 Rctl = FC_RCTL_ELS_REQ;
2516 Type = FC_TYPE_ELS;
James Smart9c2face2008-01-11 01:53:18 -05002517 } else {
2518 w5p = (WORD5 *)&(saveq->iocb.un.ulpWord[5]);
2519 Rctl = w5p->hcsw.Rctl;
2520 Type = w5p->hcsw.Type;
2521
2522 /* Firmware Workaround */
2523 if ((Rctl == 0) && (pring->ringno == LPFC_ELS_RING) &&
2524 (irsp->ulpCommand == CMD_RCV_SEQUENCE64_CX ||
2525 irsp->ulpCommand == CMD_IOCB_RCV_SEQ64_CX)) {
James Smart6a9c52c2009-10-02 15:16:51 -04002526 Rctl = FC_RCTL_ELS_REQ;
2527 Type = FC_TYPE_ELS;
James Smart9c2face2008-01-11 01:53:18 -05002528 w5p->hcsw.Rctl = Rctl;
2529 w5p->hcsw.Type = Type;
2530 }
2531 }
James Smart92d7f7b2007-06-17 19:56:38 -05002532
James Smart3772a992009-05-22 14:50:54 -04002533 if (!lpfc_complete_unsol_iocb(phba, pring, saveq, Rctl, Type))
James Smart92d7f7b2007-06-17 19:56:38 -05002534 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002535 "0313 Ring %d handler: unexpected Rctl x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05002536 "Type x%x received\n",
James Smarte8b62012007-08-02 11:10:09 -04002537 pring->ringno, Rctl, Type);
James Smart3772a992009-05-22 14:50:54 -04002538
James Smart92d7f7b2007-06-17 19:56:38 -05002539 return 1;
dea31012005-04-17 16:05:31 -05002540}
2541
James Smarte59058c2008-08-24 21:49:00 -04002542/**
James Smart3621a712009-04-06 18:47:14 -04002543 * lpfc_sli_iocbq_lookup - Find command iocb for the given response iocb
James Smarte59058c2008-08-24 21:49:00 -04002544 * @phba: Pointer to HBA context object.
2545 * @pring: Pointer to driver SLI ring object.
2546 * @prspiocb: Pointer to response iocb object.
2547 *
2548 * This function looks up the iocb_lookup table to get the command iocb
2549 * corresponding to the given response iocb using the iotag of the
2550 * response iocb. This function is called with the hbalock held.
2551 * This function returns the command iocb object if it finds the command
2552 * iocb else returns NULL.
2553 **/
dea31012005-04-17 16:05:31 -05002554static struct lpfc_iocbq *
James Smart2e0fef82007-06-17 19:56:36 -05002555lpfc_sli_iocbq_lookup(struct lpfc_hba *phba,
2556 struct lpfc_sli_ring *pring,
2557 struct lpfc_iocbq *prspiocb)
dea31012005-04-17 16:05:31 -05002558{
dea31012005-04-17 16:05:31 -05002559 struct lpfc_iocbq *cmd_iocb = NULL;
2560 uint16_t iotag;
2561
James Bottomley604a3e32005-10-29 10:28:33 -05002562 iotag = prspiocb->iocb.ulpIoTag;
dea31012005-04-17 16:05:31 -05002563
James Bottomley604a3e32005-10-29 10:28:33 -05002564 if (iotag != 0 && iotag <= phba->sli.last_iotag) {
2565 cmd_iocb = phba->sli.iocbq_lookup[iotag];
James Smart92d7f7b2007-06-17 19:56:38 -05002566 list_del_init(&cmd_iocb->list);
James Smart4f2e66c2012-05-09 21:17:07 -04002567 if (cmd_iocb->iocb_flag & LPFC_IO_ON_TXCMPLQ) {
James Smart2a9bf3d2010-06-07 15:24:45 -04002568 pring->txcmplq_cnt--;
James Smart4f2e66c2012-05-09 21:17:07 -04002569 cmd_iocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
James Smart2a9bf3d2010-06-07 15:24:45 -04002570 }
James Bottomley604a3e32005-10-29 10:28:33 -05002571 return cmd_iocb;
dea31012005-04-17 16:05:31 -05002572 }
2573
dea31012005-04-17 16:05:31 -05002574 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002575 "0317 iotag x%x is out off "
James Bottomley604a3e32005-10-29 10:28:33 -05002576 "range: max iotag x%x wd0 x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04002577 iotag, phba->sli.last_iotag,
James Bottomley604a3e32005-10-29 10:28:33 -05002578 *(((uint32_t *) &prspiocb->iocb) + 7));
dea31012005-04-17 16:05:31 -05002579 return NULL;
2580}
2581
James Smarte59058c2008-08-24 21:49:00 -04002582/**
James Smart3772a992009-05-22 14:50:54 -04002583 * lpfc_sli_iocbq_lookup_by_tag - Find command iocb for the iotag
2584 * @phba: Pointer to HBA context object.
2585 * @pring: Pointer to driver SLI ring object.
2586 * @iotag: IOCB tag.
2587 *
2588 * This function looks up the iocb_lookup table to get the command iocb
2589 * corresponding to the given iotag. This function is called with the
2590 * hbalock held.
2591 * This function returns the command iocb object if it finds the command
2592 * iocb else returns NULL.
2593 **/
2594static struct lpfc_iocbq *
2595lpfc_sli_iocbq_lookup_by_tag(struct lpfc_hba *phba,
2596 struct lpfc_sli_ring *pring, uint16_t iotag)
2597{
2598 struct lpfc_iocbq *cmd_iocb;
2599
2600 if (iotag != 0 && iotag <= phba->sli.last_iotag) {
2601 cmd_iocb = phba->sli.iocbq_lookup[iotag];
James Smart4f2e66c2012-05-09 21:17:07 -04002602 if (cmd_iocb->iocb_flag & LPFC_IO_ON_TXCMPLQ) {
2603 /* remove from txcmpl queue list */
2604 list_del_init(&cmd_iocb->list);
2605 cmd_iocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
James Smart2a9bf3d2010-06-07 15:24:45 -04002606 pring->txcmplq_cnt--;
James Smart4f2e66c2012-05-09 21:17:07 -04002607 return cmd_iocb;
James Smart2a9bf3d2010-06-07 15:24:45 -04002608 }
James Smart3772a992009-05-22 14:50:54 -04002609 }
James Smart3772a992009-05-22 14:50:54 -04002610 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2611 "0372 iotag x%x is out off range: max iotag (x%x)\n",
2612 iotag, phba->sli.last_iotag);
2613 return NULL;
2614}
2615
2616/**
James Smart3621a712009-04-06 18:47:14 -04002617 * lpfc_sli_process_sol_iocb - process solicited iocb completion
James Smarte59058c2008-08-24 21:49:00 -04002618 * @phba: Pointer to HBA context object.
2619 * @pring: Pointer to driver SLI ring object.
2620 * @saveq: Pointer to the response iocb to be processed.
2621 *
2622 * This function is called by the ring event handler for non-fcp
2623 * rings when there is a new response iocb in the response ring.
2624 * The caller is not required to hold any locks. This function
2625 * gets the command iocb associated with the response iocb and
2626 * calls the completion handler for the command iocb. If there
2627 * is no completion handler, the function will free the resources
2628 * associated with command iocb. If the response iocb is for
2629 * an already aborted command iocb, the status of the completion
2630 * is changed to IOSTAT_LOCAL_REJECT/IOERR_SLI_ABORTED.
2631 * This function always returns 1.
2632 **/
dea31012005-04-17 16:05:31 -05002633static int
James Smart2e0fef82007-06-17 19:56:36 -05002634lpfc_sli_process_sol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
dea31012005-04-17 16:05:31 -05002635 struct lpfc_iocbq *saveq)
2636{
James Smart2e0fef82007-06-17 19:56:36 -05002637 struct lpfc_iocbq *cmdiocbp;
dea31012005-04-17 16:05:31 -05002638 int rc = 1;
2639 unsigned long iflag;
2640
2641 /* Based on the iotag field, get the cmd IOCB from the txcmplq */
James Smart2e0fef82007-06-17 19:56:36 -05002642 spin_lock_irqsave(&phba->hbalock, iflag);
James Bottomley604a3e32005-10-29 10:28:33 -05002643 cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring, saveq);
James Smart2e0fef82007-06-17 19:56:36 -05002644 spin_unlock_irqrestore(&phba->hbalock, iflag);
2645
dea31012005-04-17 16:05:31 -05002646 if (cmdiocbp) {
2647 if (cmdiocbp->iocb_cmpl) {
2648 /*
James Smartea2151b2008-09-07 11:52:10 -04002649 * If an ELS command failed send an event to mgmt
2650 * application.
2651 */
2652 if (saveq->iocb.ulpStatus &&
2653 (pring->ringno == LPFC_ELS_RING) &&
2654 (cmdiocbp->iocb.ulpCommand ==
2655 CMD_ELS_REQUEST64_CR))
2656 lpfc_send_els_failure_event(phba,
2657 cmdiocbp, saveq);
2658
2659 /*
dea31012005-04-17 16:05:31 -05002660 * Post all ELS completions to the worker thread.
2661 * All other are passed to the completion callback.
2662 */
2663 if (pring->ringno == LPFC_ELS_RING) {
James Smart341af102010-01-26 23:07:37 -05002664 if ((phba->sli_rev < LPFC_SLI_REV4) &&
2665 (cmdiocbp->iocb_flag &
2666 LPFC_DRIVER_ABORTED)) {
2667 spin_lock_irqsave(&phba->hbalock,
2668 iflag);
James Smart07951072007-04-25 09:51:38 -04002669 cmdiocbp->iocb_flag &=
2670 ~LPFC_DRIVER_ABORTED;
James Smart341af102010-01-26 23:07:37 -05002671 spin_unlock_irqrestore(&phba->hbalock,
2672 iflag);
James Smart07951072007-04-25 09:51:38 -04002673 saveq->iocb.ulpStatus =
2674 IOSTAT_LOCAL_REJECT;
2675 saveq->iocb.un.ulpWord[4] =
2676 IOERR_SLI_ABORTED;
James Smart0ff10d42008-01-11 01:52:36 -05002677
2678 /* Firmware could still be in progress
2679 * of DMAing payload, so don't free data
2680 * buffer till after a hbeat.
2681 */
James Smart341af102010-01-26 23:07:37 -05002682 spin_lock_irqsave(&phba->hbalock,
2683 iflag);
James Smart0ff10d42008-01-11 01:52:36 -05002684 saveq->iocb_flag |= LPFC_DELAY_MEM_FREE;
James Smart341af102010-01-26 23:07:37 -05002685 spin_unlock_irqrestore(&phba->hbalock,
2686 iflag);
2687 }
James Smart0f65ff62010-02-26 14:14:23 -05002688 if (phba->sli_rev == LPFC_SLI_REV4) {
2689 if (saveq->iocb_flag &
2690 LPFC_EXCHANGE_BUSY) {
2691 /* Set cmdiocb flag for the
2692 * exchange busy so sgl (xri)
2693 * will not be released until
2694 * the abort xri is received
2695 * from hba.
2696 */
2697 spin_lock_irqsave(
2698 &phba->hbalock, iflag);
2699 cmdiocbp->iocb_flag |=
2700 LPFC_EXCHANGE_BUSY;
2701 spin_unlock_irqrestore(
2702 &phba->hbalock, iflag);
2703 }
2704 if (cmdiocbp->iocb_flag &
2705 LPFC_DRIVER_ABORTED) {
2706 /*
2707 * Clear LPFC_DRIVER_ABORTED
2708 * bit in case it was driver
2709 * initiated abort.
2710 */
2711 spin_lock_irqsave(
2712 &phba->hbalock, iflag);
2713 cmdiocbp->iocb_flag &=
2714 ~LPFC_DRIVER_ABORTED;
2715 spin_unlock_irqrestore(
2716 &phba->hbalock, iflag);
2717 cmdiocbp->iocb.ulpStatus =
2718 IOSTAT_LOCAL_REJECT;
2719 cmdiocbp->iocb.un.ulpWord[4] =
2720 IOERR_ABORT_REQUESTED;
2721 /*
2722 * For SLI4, irsiocb contains
2723 * NO_XRI in sli_xritag, it
2724 * shall not affect releasing
2725 * sgl (xri) process.
2726 */
2727 saveq->iocb.ulpStatus =
2728 IOSTAT_LOCAL_REJECT;
2729 saveq->iocb.un.ulpWord[4] =
2730 IOERR_SLI_ABORTED;
2731 spin_lock_irqsave(
2732 &phba->hbalock, iflag);
2733 saveq->iocb_flag |=
2734 LPFC_DELAY_MEM_FREE;
2735 spin_unlock_irqrestore(
2736 &phba->hbalock, iflag);
2737 }
James Smart07951072007-04-25 09:51:38 -04002738 }
dea31012005-04-17 16:05:31 -05002739 }
James Smart2e0fef82007-06-17 19:56:36 -05002740 (cmdiocbp->iocb_cmpl) (phba, cmdiocbp, saveq);
James Bottomley604a3e32005-10-29 10:28:33 -05002741 } else
2742 lpfc_sli_release_iocbq(phba, cmdiocbp);
dea31012005-04-17 16:05:31 -05002743 } else {
2744 /*
2745 * Unknown initiating command based on the response iotag.
2746 * This could be the case on the ELS ring because of
2747 * lpfc_els_abort().
2748 */
2749 if (pring->ringno != LPFC_ELS_RING) {
2750 /*
2751 * Ring <ringno> handler: unexpected completion IoTag
2752 * <IoTag>
2753 */
James Smarta257bf92009-04-06 18:48:10 -04002754 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002755 "0322 Ring %d handler: "
2756 "unexpected completion IoTag x%x "
2757 "Data: x%x x%x x%x x%x\n",
2758 pring->ringno,
2759 saveq->iocb.ulpIoTag,
2760 saveq->iocb.ulpStatus,
2761 saveq->iocb.un.ulpWord[4],
2762 saveq->iocb.ulpCommand,
2763 saveq->iocb.ulpContext);
dea31012005-04-17 16:05:31 -05002764 }
2765 }
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04002766
dea31012005-04-17 16:05:31 -05002767 return rc;
2768}
2769
James Smarte59058c2008-08-24 21:49:00 -04002770/**
James Smart3621a712009-04-06 18:47:14 -04002771 * lpfc_sli_rsp_pointers_error - Response ring pointer error handler
James Smarte59058c2008-08-24 21:49:00 -04002772 * @phba: Pointer to HBA context object.
2773 * @pring: Pointer to driver SLI ring object.
2774 *
2775 * This function is called from the iocb ring event handlers when
2776 * put pointer is ahead of the get pointer for a ring. This function signal
2777 * an error attention condition to the worker thread and the worker
2778 * thread will transition the HBA to offline state.
2779 **/
James Smart2e0fef82007-06-17 19:56:36 -05002780static void
2781lpfc_sli_rsp_pointers_error(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002782{
James Smart34b02dc2008-08-24 21:49:55 -04002783 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002784 /*
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02002785 * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002786 * rsp ring <portRspMax>
2787 */
2788 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002789 "0312 Ring %d handler: portRspPut %d "
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02002790 "is bigger than rsp ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04002791 pring->ringno, le32_to_cpu(pgp->rspPutInx),
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002792 pring->numRiocb);
2793
James Smart2e0fef82007-06-17 19:56:36 -05002794 phba->link_state = LPFC_HBA_ERROR;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002795
2796 /*
2797 * All error attention handlers are posted to
2798 * worker thread
2799 */
2800 phba->work_ha |= HA_ERATT;
2801 phba->work_hs = HS_FFER3;
James Smart92d7f7b2007-06-17 19:56:38 -05002802
James Smart5e9d9b82008-06-14 22:52:53 -04002803 lpfc_worker_wake_up(phba);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002804
2805 return;
2806}
2807
James Smarte59058c2008-08-24 21:49:00 -04002808/**
James Smart3621a712009-04-06 18:47:14 -04002809 * lpfc_poll_eratt - Error attention polling timer timeout handler
James Smart93996272008-08-24 21:50:30 -04002810 * @ptr: Pointer to address of HBA context object.
2811 *
2812 * This function is invoked by the Error Attention polling timer when the
2813 * timer times out. It will check the SLI Error Attention register for
2814 * possible attention events. If so, it will post an Error Attention event
2815 * and wake up worker thread to process it. Otherwise, it will set up the
2816 * Error Attention polling timer for the next poll.
2817 **/
2818void lpfc_poll_eratt(unsigned long ptr)
2819{
2820 struct lpfc_hba *phba;
2821 uint32_t eratt = 0;
2822
2823 phba = (struct lpfc_hba *)ptr;
2824
2825 /* Check chip HA register for error event */
2826 eratt = lpfc_sli_check_eratt(phba);
2827
2828 if (eratt)
2829 /* Tell the worker thread there is work to do */
2830 lpfc_worker_wake_up(phba);
2831 else
2832 /* Restart the timer for next eratt poll */
2833 mod_timer(&phba->eratt_poll, jiffies +
2834 HZ * LPFC_ERATT_POLL_INTERVAL);
2835 return;
2836}
2837
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002838
James Smarte59058c2008-08-24 21:49:00 -04002839/**
James Smart3621a712009-04-06 18:47:14 -04002840 * lpfc_sli_handle_fast_ring_event - Handle ring events on FCP ring
James Smarte59058c2008-08-24 21:49:00 -04002841 * @phba: Pointer to HBA context object.
2842 * @pring: Pointer to driver SLI ring object.
2843 * @mask: Host attention register mask for this ring.
2844 *
2845 * This function is called from the interrupt context when there is a ring
2846 * event for the fcp ring. The caller does not hold any lock.
2847 * The function processes each response iocb in the response ring until it
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002848 * finds an iocb with LE bit set and chains all the iocbs up to the iocb with
James Smarte59058c2008-08-24 21:49:00 -04002849 * LE bit set. The function will call the completion handler of the command iocb
2850 * if the response iocb indicates a completion for a command iocb or it is
2851 * an abort completion. The function will call lpfc_sli_process_unsol_iocb
2852 * function if this is an unsolicited iocb.
dea31012005-04-17 16:05:31 -05002853 * This routine presumes LPFC_FCP_RING handling and doesn't bother
James Smart45ed1192009-10-02 15:17:02 -04002854 * to check it explicitly.
2855 */
2856int
James Smart2e0fef82007-06-17 19:56:36 -05002857lpfc_sli_handle_fast_ring_event(struct lpfc_hba *phba,
2858 struct lpfc_sli_ring *pring, uint32_t mask)
dea31012005-04-17 16:05:31 -05002859{
James Smart34b02dc2008-08-24 21:49:55 -04002860 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
dea31012005-04-17 16:05:31 -05002861 IOCB_t *irsp = NULL;
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04002862 IOCB_t *entry = NULL;
dea31012005-04-17 16:05:31 -05002863 struct lpfc_iocbq *cmdiocbq = NULL;
2864 struct lpfc_iocbq rspiocbq;
dea31012005-04-17 16:05:31 -05002865 uint32_t status;
2866 uint32_t portRspPut, portRspMax;
2867 int rc = 1;
2868 lpfc_iocb_type type;
2869 unsigned long iflag;
2870 uint32_t rsp_cmpl = 0;
dea31012005-04-17 16:05:31 -05002871
James Smart2e0fef82007-06-17 19:56:36 -05002872 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05002873 pring->stats.iocb_event++;
2874
dea31012005-04-17 16:05:31 -05002875 /*
2876 * The next available response entry should never exceed the maximum
2877 * entries. If it does, treat it as an adapter hardware error.
2878 */
2879 portRspMax = pring->numRiocb;
2880 portRspPut = le32_to_cpu(pgp->rspPutInx);
2881 if (unlikely(portRspPut >= portRspMax)) {
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002882 lpfc_sli_rsp_pointers_error(phba, pring);
James Smart2e0fef82007-06-17 19:56:36 -05002883 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05002884 return 1;
2885 }
James Smart45ed1192009-10-02 15:17:02 -04002886 if (phba->fcp_ring_in_use) {
2887 spin_unlock_irqrestore(&phba->hbalock, iflag);
2888 return 1;
2889 } else
2890 phba->fcp_ring_in_use = 1;
dea31012005-04-17 16:05:31 -05002891
2892 rmb();
2893 while (pring->rspidx != portRspPut) {
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04002894 /*
2895 * Fetch an entry off the ring and copy it into a local data
2896 * structure. The copy involves a byte-swap since the
2897 * network byte order and pci byte orders are different.
2898 */
James Smarted957682007-06-17 19:56:37 -05002899 entry = lpfc_resp_iocb(phba, pring);
James Smart858c9f62007-06-17 19:56:39 -05002900 phba->last_completion_time = jiffies;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002901
2902 if (++pring->rspidx >= portRspMax)
2903 pring->rspidx = 0;
2904
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04002905 lpfc_sli_pcimem_bcopy((uint32_t *) entry,
2906 (uint32_t *) &rspiocbq.iocb,
James Smarted957682007-06-17 19:56:37 -05002907 phba->iocb_rsp_size);
James Smarta4bc3372006-12-02 13:34:16 -05002908 INIT_LIST_HEAD(&(rspiocbq.list));
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04002909 irsp = &rspiocbq.iocb;
2910
dea31012005-04-17 16:05:31 -05002911 type = lpfc_sli_iocb_cmd_type(irsp->ulpCommand & CMD_IOCB_MASK);
2912 pring->stats.iocb_rsp++;
2913 rsp_cmpl++;
2914
2915 if (unlikely(irsp->ulpStatus)) {
James Smart92d7f7b2007-06-17 19:56:38 -05002916 /*
2917 * If resource errors reported from HBA, reduce
2918 * queuedepths of the SCSI device.
2919 */
2920 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
2921 (irsp->un.ulpWord[4] == IOERR_NO_RESOURCES)) {
2922 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart3772a992009-05-22 14:50:54 -04002923 phba->lpfc_rampdown_queue_depth(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05002924 spin_lock_irqsave(&phba->hbalock, iflag);
2925 }
2926
dea31012005-04-17 16:05:31 -05002927 /* Rsp ring <ringno> error: IOCB */
2928 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002929 "0336 Rsp Ring %d error: IOCB Data: "
James Smart92d7f7b2007-06-17 19:56:38 -05002930 "x%x x%x x%x x%x x%x x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04002931 pring->ringno,
James Smart92d7f7b2007-06-17 19:56:38 -05002932 irsp->un.ulpWord[0],
2933 irsp->un.ulpWord[1],
2934 irsp->un.ulpWord[2],
2935 irsp->un.ulpWord[3],
2936 irsp->un.ulpWord[4],
2937 irsp->un.ulpWord[5],
James Smartd7c255b2008-08-24 21:50:00 -04002938 *(uint32_t *)&irsp->un1,
2939 *((uint32_t *)&irsp->un1 + 1));
dea31012005-04-17 16:05:31 -05002940 }
2941
2942 switch (type) {
2943 case LPFC_ABORT_IOCB:
2944 case LPFC_SOL_IOCB:
2945 /*
2946 * Idle exchange closed via ABTS from port. No iocb
2947 * resources need to be recovered.
2948 */
2949 if (unlikely(irsp->ulpCommand == CMD_XRI_ABORTED_CX)) {
James Smartdca94792006-08-01 07:34:08 -04002950 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002951 "0333 IOCB cmd 0x%x"
James Smartdca94792006-08-01 07:34:08 -04002952 " processed. Skipping"
James Smart92d7f7b2007-06-17 19:56:38 -05002953 " completion\n",
James Smartdca94792006-08-01 07:34:08 -04002954 irsp->ulpCommand);
dea31012005-04-17 16:05:31 -05002955 break;
2956 }
2957
James Bottomley604a3e32005-10-29 10:28:33 -05002958 cmdiocbq = lpfc_sli_iocbq_lookup(phba, pring,
2959 &rspiocbq);
James Smart0f65ff62010-02-26 14:14:23 -05002960 if (unlikely(!cmdiocbq))
2961 break;
2962 if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED)
2963 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
2964 if (cmdiocbq->iocb_cmpl) {
2965 spin_unlock_irqrestore(&phba->hbalock, iflag);
2966 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
2967 &rspiocbq);
2968 spin_lock_irqsave(&phba->hbalock, iflag);
2969 }
dea31012005-04-17 16:05:31 -05002970 break;
James Smarta4bc3372006-12-02 13:34:16 -05002971 case LPFC_UNSOL_IOCB:
James Smart2e0fef82007-06-17 19:56:36 -05002972 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta4bc3372006-12-02 13:34:16 -05002973 lpfc_sli_process_unsol_iocb(phba, pring, &rspiocbq);
James Smart2e0fef82007-06-17 19:56:36 -05002974 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta4bc3372006-12-02 13:34:16 -05002975 break;
dea31012005-04-17 16:05:31 -05002976 default:
2977 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
2978 char adaptermsg[LPFC_MAX_ADPTMSG];
2979 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
2980 memcpy(&adaptermsg[0], (uint8_t *) irsp,
2981 MAX_MSG_DATA);
Joe Perches898eb712007-10-18 03:06:30 -07002982 dev_warn(&((phba->pcidev)->dev),
2983 "lpfc%d: %s\n",
dea31012005-04-17 16:05:31 -05002984 phba->brd_no, adaptermsg);
2985 } else {
2986 /* Unknown IOCB command */
2987 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002988 "0334 Unknown IOCB command "
James Smart92d7f7b2007-06-17 19:56:38 -05002989 "Data: x%x, x%x x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04002990 type, irsp->ulpCommand,
James Smart92d7f7b2007-06-17 19:56:38 -05002991 irsp->ulpStatus,
2992 irsp->ulpIoTag,
2993 irsp->ulpContext);
dea31012005-04-17 16:05:31 -05002994 }
2995 break;
2996 }
2997
2998 /*
2999 * The response IOCB has been processed. Update the ring
3000 * pointer in SLIM. If the port response put pointer has not
3001 * been updated, sync the pgp->rspPutInx and fetch the new port
3002 * response put pointer.
3003 */
James Smarted957682007-06-17 19:56:37 -05003004 writel(pring->rspidx, &phba->host_gp[pring->ringno].rspGetInx);
dea31012005-04-17 16:05:31 -05003005
3006 if (pring->rspidx == portRspPut)
3007 portRspPut = le32_to_cpu(pgp->rspPutInx);
3008 }
3009
3010 if ((rsp_cmpl > 0) && (mask & HA_R0RE_REQ)) {
3011 pring->stats.iocb_rsp_full++;
3012 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
3013 writel(status, phba->CAregaddr);
3014 readl(phba->CAregaddr);
3015 }
3016 if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
3017 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
3018 pring->stats.iocb_cmd_empty++;
3019
3020 /* Force update of the local copy of cmdGetInx */
3021 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
3022 lpfc_sli_resume_iocb(phba, pring);
3023
3024 if ((pring->lpfc_sli_cmd_available))
3025 (pring->lpfc_sli_cmd_available) (phba, pring);
3026
3027 }
3028
James Smart45ed1192009-10-02 15:17:02 -04003029 phba->fcp_ring_in_use = 0;
James Smart2e0fef82007-06-17 19:56:36 -05003030 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003031 return rc;
3032}
3033
James Smarte59058c2008-08-24 21:49:00 -04003034/**
James Smart3772a992009-05-22 14:50:54 -04003035 * lpfc_sli_sp_handle_rspiocb - Handle slow-path response iocb
3036 * @phba: Pointer to HBA context object.
3037 * @pring: Pointer to driver SLI ring object.
3038 * @rspiocbp: Pointer to driver response IOCB object.
3039 *
3040 * This function is called from the worker thread when there is a slow-path
3041 * response IOCB to process. This function chains all the response iocbs until
3042 * seeing the iocb with the LE bit set. The function will call
3043 * lpfc_sli_process_sol_iocb function if the response iocb indicates a
3044 * completion of a command iocb. The function will call the
3045 * lpfc_sli_process_unsol_iocb function if this is an unsolicited iocb.
3046 * The function frees the resources or calls the completion handler if this
3047 * iocb is an abort completion. The function returns NULL when the response
3048 * iocb has the LE bit set and all the chained iocbs are processed, otherwise
3049 * this function shall chain the iocb on to the iocb_continueq and return the
3050 * response iocb passed in.
3051 **/
3052static struct lpfc_iocbq *
3053lpfc_sli_sp_handle_rspiocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
3054 struct lpfc_iocbq *rspiocbp)
3055{
3056 struct lpfc_iocbq *saveq;
3057 struct lpfc_iocbq *cmdiocbp;
3058 struct lpfc_iocbq *next_iocb;
3059 IOCB_t *irsp = NULL;
3060 uint32_t free_saveq;
3061 uint8_t iocb_cmd_type;
3062 lpfc_iocb_type type;
3063 unsigned long iflag;
3064 int rc;
3065
3066 spin_lock_irqsave(&phba->hbalock, iflag);
3067 /* First add the response iocb to the countinueq list */
3068 list_add_tail(&rspiocbp->list, &(pring->iocb_continueq));
3069 pring->iocb_continueq_cnt++;
3070
Justin P. Mattock70f23fd2011-05-10 10:16:21 +02003071 /* Now, determine whether the list is completed for processing */
James Smart3772a992009-05-22 14:50:54 -04003072 irsp = &rspiocbp->iocb;
3073 if (irsp->ulpLe) {
3074 /*
3075 * By default, the driver expects to free all resources
3076 * associated with this iocb completion.
3077 */
3078 free_saveq = 1;
3079 saveq = list_get_first(&pring->iocb_continueq,
3080 struct lpfc_iocbq, list);
3081 irsp = &(saveq->iocb);
3082 list_del_init(&pring->iocb_continueq);
3083 pring->iocb_continueq_cnt = 0;
3084
3085 pring->stats.iocb_rsp++;
3086
3087 /*
3088 * If resource errors reported from HBA, reduce
3089 * queuedepths of the SCSI device.
3090 */
3091 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
3092 (irsp->un.ulpWord[4] == IOERR_NO_RESOURCES)) {
3093 spin_unlock_irqrestore(&phba->hbalock, iflag);
3094 phba->lpfc_rampdown_queue_depth(phba);
3095 spin_lock_irqsave(&phba->hbalock, iflag);
3096 }
3097
3098 if (irsp->ulpStatus) {
3099 /* Rsp ring <ringno> error: IOCB */
3100 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
3101 "0328 Rsp Ring %d error: "
3102 "IOCB Data: "
3103 "x%x x%x x%x x%x "
3104 "x%x x%x x%x x%x "
3105 "x%x x%x x%x x%x "
3106 "x%x x%x x%x x%x\n",
3107 pring->ringno,
3108 irsp->un.ulpWord[0],
3109 irsp->un.ulpWord[1],
3110 irsp->un.ulpWord[2],
3111 irsp->un.ulpWord[3],
3112 irsp->un.ulpWord[4],
3113 irsp->un.ulpWord[5],
3114 *(((uint32_t *) irsp) + 6),
3115 *(((uint32_t *) irsp) + 7),
3116 *(((uint32_t *) irsp) + 8),
3117 *(((uint32_t *) irsp) + 9),
3118 *(((uint32_t *) irsp) + 10),
3119 *(((uint32_t *) irsp) + 11),
3120 *(((uint32_t *) irsp) + 12),
3121 *(((uint32_t *) irsp) + 13),
3122 *(((uint32_t *) irsp) + 14),
3123 *(((uint32_t *) irsp) + 15));
3124 }
3125
3126 /*
3127 * Fetch the IOCB command type and call the correct completion
3128 * routine. Solicited and Unsolicited IOCBs on the ELS ring
3129 * get freed back to the lpfc_iocb_list by the discovery
3130 * kernel thread.
3131 */
3132 iocb_cmd_type = irsp->ulpCommand & CMD_IOCB_MASK;
3133 type = lpfc_sli_iocb_cmd_type(iocb_cmd_type);
3134 switch (type) {
3135 case LPFC_SOL_IOCB:
3136 spin_unlock_irqrestore(&phba->hbalock, iflag);
3137 rc = lpfc_sli_process_sol_iocb(phba, pring, saveq);
3138 spin_lock_irqsave(&phba->hbalock, iflag);
3139 break;
3140
3141 case LPFC_UNSOL_IOCB:
3142 spin_unlock_irqrestore(&phba->hbalock, iflag);
3143 rc = lpfc_sli_process_unsol_iocb(phba, pring, saveq);
3144 spin_lock_irqsave(&phba->hbalock, iflag);
3145 if (!rc)
3146 free_saveq = 0;
3147 break;
3148
3149 case LPFC_ABORT_IOCB:
3150 cmdiocbp = NULL;
3151 if (irsp->ulpCommand != CMD_XRI_ABORTED_CX)
3152 cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring,
3153 saveq);
3154 if (cmdiocbp) {
3155 /* Call the specified completion routine */
3156 if (cmdiocbp->iocb_cmpl) {
3157 spin_unlock_irqrestore(&phba->hbalock,
3158 iflag);
3159 (cmdiocbp->iocb_cmpl)(phba, cmdiocbp,
3160 saveq);
3161 spin_lock_irqsave(&phba->hbalock,
3162 iflag);
3163 } else
3164 __lpfc_sli_release_iocbq(phba,
3165 cmdiocbp);
3166 }
3167 break;
3168
3169 case LPFC_UNKNOWN_IOCB:
3170 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
3171 char adaptermsg[LPFC_MAX_ADPTMSG];
3172 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
3173 memcpy(&adaptermsg[0], (uint8_t *)irsp,
3174 MAX_MSG_DATA);
3175 dev_warn(&((phba->pcidev)->dev),
3176 "lpfc%d: %s\n",
3177 phba->brd_no, adaptermsg);
3178 } else {
3179 /* Unknown IOCB command */
3180 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3181 "0335 Unknown IOCB "
3182 "command Data: x%x "
3183 "x%x x%x x%x\n",
3184 irsp->ulpCommand,
3185 irsp->ulpStatus,
3186 irsp->ulpIoTag,
3187 irsp->ulpContext);
3188 }
3189 break;
3190 }
3191
3192 if (free_saveq) {
3193 list_for_each_entry_safe(rspiocbp, next_iocb,
3194 &saveq->list, list) {
3195 list_del(&rspiocbp->list);
3196 __lpfc_sli_release_iocbq(phba, rspiocbp);
3197 }
3198 __lpfc_sli_release_iocbq(phba, saveq);
3199 }
3200 rspiocbp = NULL;
3201 }
3202 spin_unlock_irqrestore(&phba->hbalock, iflag);
3203 return rspiocbp;
3204}
3205
3206/**
3207 * lpfc_sli_handle_slow_ring_event - Wrapper func for handling slow-path iocbs
James Smarte59058c2008-08-24 21:49:00 -04003208 * @phba: Pointer to HBA context object.
3209 * @pring: Pointer to driver SLI ring object.
3210 * @mask: Host attention register mask for this ring.
3211 *
James Smart3772a992009-05-22 14:50:54 -04003212 * This routine wraps the actual slow_ring event process routine from the
3213 * API jump table function pointer from the lpfc_hba struct.
James Smarte59058c2008-08-24 21:49:00 -04003214 **/
James Smart3772a992009-05-22 14:50:54 -04003215void
James Smart2e0fef82007-06-17 19:56:36 -05003216lpfc_sli_handle_slow_ring_event(struct lpfc_hba *phba,
3217 struct lpfc_sli_ring *pring, uint32_t mask)
dea31012005-04-17 16:05:31 -05003218{
James Smart3772a992009-05-22 14:50:54 -04003219 phba->lpfc_sli_handle_slow_ring_event(phba, pring, mask);
3220}
3221
3222/**
3223 * lpfc_sli_handle_slow_ring_event_s3 - Handle SLI3 ring event for non-FCP rings
3224 * @phba: Pointer to HBA context object.
3225 * @pring: Pointer to driver SLI ring object.
3226 * @mask: Host attention register mask for this ring.
3227 *
3228 * This function is called from the worker thread when there is a ring event
3229 * for non-fcp rings. The caller does not hold any lock. The function will
3230 * remove each response iocb in the response ring and calls the handle
3231 * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
3232 **/
3233static void
3234lpfc_sli_handle_slow_ring_event_s3(struct lpfc_hba *phba,
3235 struct lpfc_sli_ring *pring, uint32_t mask)
3236{
James Smart34b02dc2008-08-24 21:49:55 -04003237 struct lpfc_pgp *pgp;
dea31012005-04-17 16:05:31 -05003238 IOCB_t *entry;
3239 IOCB_t *irsp = NULL;
3240 struct lpfc_iocbq *rspiocbp = NULL;
dea31012005-04-17 16:05:31 -05003241 uint32_t portRspPut, portRspMax;
dea31012005-04-17 16:05:31 -05003242 unsigned long iflag;
James Smart3772a992009-05-22 14:50:54 -04003243 uint32_t status;
dea31012005-04-17 16:05:31 -05003244
James Smart34b02dc2008-08-24 21:49:55 -04003245 pgp = &phba->port_gp[pring->ringno];
James Smart2e0fef82007-06-17 19:56:36 -05003246 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003247 pring->stats.iocb_event++;
3248
dea31012005-04-17 16:05:31 -05003249 /*
3250 * The next available response entry should never exceed the maximum
3251 * entries. If it does, treat it as an adapter hardware error.
3252 */
3253 portRspMax = pring->numRiocb;
3254 portRspPut = le32_to_cpu(pgp->rspPutInx);
3255 if (portRspPut >= portRspMax) {
3256 /*
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02003257 * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
dea31012005-04-17 16:05:31 -05003258 * rsp ring <portRspMax>
3259 */
James Smarted957682007-06-17 19:56:37 -05003260 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003261 "0303 Ring %d handler: portRspPut %d "
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02003262 "is bigger than rsp ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04003263 pring->ringno, portRspPut, portRspMax);
dea31012005-04-17 16:05:31 -05003264
James Smart2e0fef82007-06-17 19:56:36 -05003265 phba->link_state = LPFC_HBA_ERROR;
3266 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003267
3268 phba->work_hs = HS_FFER3;
3269 lpfc_handle_eratt(phba);
3270
James Smart3772a992009-05-22 14:50:54 -04003271 return;
dea31012005-04-17 16:05:31 -05003272 }
3273
3274 rmb();
dea31012005-04-17 16:05:31 -05003275 while (pring->rspidx != portRspPut) {
3276 /*
3277 * Build a completion list and call the appropriate handler.
3278 * The process is to get the next available response iocb, get
3279 * a free iocb from the list, copy the response data into the
3280 * free iocb, insert to the continuation list, and update the
3281 * next response index to slim. This process makes response
3282 * iocb's in the ring available to DMA as fast as possible but
3283 * pays a penalty for a copy operation. Since the iocb is
3284 * only 32 bytes, this penalty is considered small relative to
3285 * the PCI reads for register values and a slim write. When
3286 * the ulpLe field is set, the entire Command has been
3287 * received.
3288 */
James Smarted957682007-06-17 19:56:37 -05003289 entry = lpfc_resp_iocb(phba, pring);
3290
James Smart858c9f62007-06-17 19:56:39 -05003291 phba->last_completion_time = jiffies;
James Smart2e0fef82007-06-17 19:56:36 -05003292 rspiocbp = __lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05003293 if (rspiocbp == NULL) {
3294 printk(KERN_ERR "%s: out of buffers! Failing "
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07003295 "completion.\n", __func__);
dea31012005-04-17 16:05:31 -05003296 break;
3297 }
3298
James Smarted957682007-06-17 19:56:37 -05003299 lpfc_sli_pcimem_bcopy(entry, &rspiocbp->iocb,
3300 phba->iocb_rsp_size);
dea31012005-04-17 16:05:31 -05003301 irsp = &rspiocbp->iocb;
3302
3303 if (++pring->rspidx >= portRspMax)
3304 pring->rspidx = 0;
3305
James Smarta58cbd52007-08-02 11:09:43 -04003306 if (pring->ringno == LPFC_ELS_RING) {
3307 lpfc_debugfs_slow_ring_trc(phba,
3308 "IOCB rsp ring: wd4:x%08x wd6:x%08x wd7:x%08x",
3309 *(((uint32_t *) irsp) + 4),
3310 *(((uint32_t *) irsp) + 6),
3311 *(((uint32_t *) irsp) + 7));
3312 }
3313
James Smarted957682007-06-17 19:56:37 -05003314 writel(pring->rspidx, &phba->host_gp[pring->ringno].rspGetInx);
dea31012005-04-17 16:05:31 -05003315
James Smart3772a992009-05-22 14:50:54 -04003316 spin_unlock_irqrestore(&phba->hbalock, iflag);
3317 /* Handle the response IOCB */
3318 rspiocbp = lpfc_sli_sp_handle_rspiocb(phba, pring, rspiocbp);
3319 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003320
3321 /*
3322 * If the port response put pointer has not been updated, sync
3323 * the pgp->rspPutInx in the MAILBOX_tand fetch the new port
3324 * response put pointer.
3325 */
3326 if (pring->rspidx == portRspPut) {
3327 portRspPut = le32_to_cpu(pgp->rspPutInx);
3328 }
3329 } /* while (pring->rspidx != portRspPut) */
3330
James Smart92d7f7b2007-06-17 19:56:38 -05003331 if ((rspiocbp != NULL) && (mask & HA_R0RE_REQ)) {
dea31012005-04-17 16:05:31 -05003332 /* At least one response entry has been freed */
3333 pring->stats.iocb_rsp_full++;
3334 /* SET RxRE_RSP in Chip Att register */
3335 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
3336 writel(status, phba->CAregaddr);
3337 readl(phba->CAregaddr); /* flush */
3338 }
3339 if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
3340 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
3341 pring->stats.iocb_cmd_empty++;
3342
3343 /* Force update of the local copy of cmdGetInx */
3344 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
3345 lpfc_sli_resume_iocb(phba, pring);
3346
3347 if ((pring->lpfc_sli_cmd_available))
3348 (pring->lpfc_sli_cmd_available) (phba, pring);
3349
3350 }
3351
James Smart2e0fef82007-06-17 19:56:36 -05003352 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart3772a992009-05-22 14:50:54 -04003353 return;
dea31012005-04-17 16:05:31 -05003354}
3355
James Smarte59058c2008-08-24 21:49:00 -04003356/**
James Smart4f774512009-05-22 14:52:35 -04003357 * lpfc_sli_handle_slow_ring_event_s4 - Handle SLI4 slow-path els events
3358 * @phba: Pointer to HBA context object.
3359 * @pring: Pointer to driver SLI ring object.
3360 * @mask: Host attention register mask for this ring.
3361 *
3362 * This function is called from the worker thread when there is a pending
3363 * ELS response iocb on the driver internal slow-path response iocb worker
3364 * queue. The caller does not hold any lock. The function will remove each
3365 * response iocb from the response worker queue and calls the handle
3366 * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
3367 **/
3368static void
3369lpfc_sli_handle_slow_ring_event_s4(struct lpfc_hba *phba,
3370 struct lpfc_sli_ring *pring, uint32_t mask)
3371{
3372 struct lpfc_iocbq *irspiocbq;
James Smart4d9ab992009-10-02 15:16:39 -04003373 struct hbq_dmabuf *dmabuf;
3374 struct lpfc_cq_event *cq_event;
James Smart4f774512009-05-22 14:52:35 -04003375 unsigned long iflag;
3376
James Smart45ed1192009-10-02 15:17:02 -04003377 spin_lock_irqsave(&phba->hbalock, iflag);
3378 phba->hba_flag &= ~HBA_SP_QUEUE_EVT;
3379 spin_unlock_irqrestore(&phba->hbalock, iflag);
3380 while (!list_empty(&phba->sli4_hba.sp_queue_event)) {
James Smart4f774512009-05-22 14:52:35 -04003381 /* Get the response iocb from the head of work queue */
3382 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart45ed1192009-10-02 15:17:02 -04003383 list_remove_head(&phba->sli4_hba.sp_queue_event,
James Smart4d9ab992009-10-02 15:16:39 -04003384 cq_event, struct lpfc_cq_event, list);
James Smart4f774512009-05-22 14:52:35 -04003385 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart4d9ab992009-10-02 15:16:39 -04003386
3387 switch (bf_get(lpfc_wcqe_c_code, &cq_event->cqe.wcqe_cmpl)) {
3388 case CQE_CODE_COMPL_WQE:
3389 irspiocbq = container_of(cq_event, struct lpfc_iocbq,
3390 cq_event);
James Smart45ed1192009-10-02 15:17:02 -04003391 /* Translate ELS WCQE to response IOCBQ */
3392 irspiocbq = lpfc_sli4_els_wcqe_to_rspiocbq(phba,
3393 irspiocbq);
3394 if (irspiocbq)
3395 lpfc_sli_sp_handle_rspiocb(phba, pring,
3396 irspiocbq);
James Smart4d9ab992009-10-02 15:16:39 -04003397 break;
3398 case CQE_CODE_RECEIVE:
James Smart7851fe22011-07-22 18:36:52 -04003399 case CQE_CODE_RECEIVE_V1:
James Smart4d9ab992009-10-02 15:16:39 -04003400 dmabuf = container_of(cq_event, struct hbq_dmabuf,
3401 cq_event);
3402 lpfc_sli4_handle_received_buffer(phba, dmabuf);
3403 break;
3404 default:
3405 break;
3406 }
James Smart4f774512009-05-22 14:52:35 -04003407 }
3408}
3409
3410/**
James Smart3621a712009-04-06 18:47:14 -04003411 * lpfc_sli_abort_iocb_ring - Abort all iocbs in the ring
James Smarte59058c2008-08-24 21:49:00 -04003412 * @phba: Pointer to HBA context object.
3413 * @pring: Pointer to driver SLI ring object.
3414 *
3415 * This function aborts all iocbs in the given ring and frees all the iocb
3416 * objects in txq. This function issues an abort iocb for all the iocb commands
3417 * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
3418 * the return of this function. The caller is not required to hold any locks.
3419 **/
James Smart2e0fef82007-06-17 19:56:36 -05003420void
dea31012005-04-17 16:05:31 -05003421lpfc_sli_abort_iocb_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
3422{
James Smart2534ba72007-04-25 09:52:20 -04003423 LIST_HEAD(completions);
dea31012005-04-17 16:05:31 -05003424 struct lpfc_iocbq *iocb, *next_iocb;
dea31012005-04-17 16:05:31 -05003425
James Smart92d7f7b2007-06-17 19:56:38 -05003426 if (pring->ringno == LPFC_ELS_RING) {
3427 lpfc_fabric_abort_hba(phba);
3428 }
3429
dea31012005-04-17 16:05:31 -05003430 /* Error everything on txq and txcmplq
3431 * First do the txq.
3432 */
James Smart2e0fef82007-06-17 19:56:36 -05003433 spin_lock_irq(&phba->hbalock);
James Smart2534ba72007-04-25 09:52:20 -04003434 list_splice_init(&pring->txq, &completions);
dea31012005-04-17 16:05:31 -05003435 pring->txq_cnt = 0;
dea31012005-04-17 16:05:31 -05003436
3437 /* Next issue ABTS for everything on the txcmplq */
James Smart2534ba72007-04-25 09:52:20 -04003438 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
3439 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
3440
James Smart2e0fef82007-06-17 19:56:36 -05003441 spin_unlock_irq(&phba->hbalock);
James Smart2534ba72007-04-25 09:52:20 -04003442
James Smarta257bf92009-04-06 18:48:10 -04003443 /* Cancel all the IOCBs from the completions list */
3444 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
3445 IOERR_SLI_ABORTED);
dea31012005-04-17 16:05:31 -05003446}
3447
James Smarte59058c2008-08-24 21:49:00 -04003448/**
James Smart3621a712009-04-06 18:47:14 -04003449 * lpfc_sli_flush_fcp_rings - flush all iocbs in the fcp ring
James Smarta8e497d2008-08-24 21:50:11 -04003450 * @phba: Pointer to HBA context object.
3451 *
3452 * This function flushes all iocbs in the fcp ring and frees all the iocb
3453 * objects in txq and txcmplq. This function will not issue abort iocbs
3454 * for all the iocb commands in txcmplq, they will just be returned with
3455 * IOERR_SLI_DOWN. This function is invoked with EEH when device's PCI
3456 * slot has been permanently disabled.
3457 **/
3458void
3459lpfc_sli_flush_fcp_rings(struct lpfc_hba *phba)
3460{
3461 LIST_HEAD(txq);
3462 LIST_HEAD(txcmplq);
James Smarta8e497d2008-08-24 21:50:11 -04003463 struct lpfc_sli *psli = &phba->sli;
3464 struct lpfc_sli_ring *pring;
3465
3466 /* Currently, only one fcp ring */
3467 pring = &psli->ring[psli->fcp_ring];
3468
3469 spin_lock_irq(&phba->hbalock);
3470 /* Retrieve everything on txq */
3471 list_splice_init(&pring->txq, &txq);
3472 pring->txq_cnt = 0;
3473
3474 /* Retrieve everything on the txcmplq */
3475 list_splice_init(&pring->txcmplq, &txcmplq);
3476 pring->txcmplq_cnt = 0;
James Smart4f2e66c2012-05-09 21:17:07 -04003477
3478 /* Indicate the I/O queues are flushed */
3479 phba->hba_flag |= HBA_FCP_IOQ_FLUSH;
James Smarta8e497d2008-08-24 21:50:11 -04003480 spin_unlock_irq(&phba->hbalock);
3481
3482 /* Flush the txq */
James Smarta257bf92009-04-06 18:48:10 -04003483 lpfc_sli_cancel_iocbs(phba, &txq, IOSTAT_LOCAL_REJECT,
3484 IOERR_SLI_DOWN);
James Smarta8e497d2008-08-24 21:50:11 -04003485
3486 /* Flush the txcmpq */
James Smarta257bf92009-04-06 18:48:10 -04003487 lpfc_sli_cancel_iocbs(phba, &txcmplq, IOSTAT_LOCAL_REJECT,
3488 IOERR_SLI_DOWN);
James Smarta8e497d2008-08-24 21:50:11 -04003489}
3490
3491/**
James Smart3772a992009-05-22 14:50:54 -04003492 * lpfc_sli_brdready_s3 - Check for sli3 host ready status
James Smarte59058c2008-08-24 21:49:00 -04003493 * @phba: Pointer to HBA context object.
3494 * @mask: Bit mask to be checked.
3495 *
3496 * This function reads the host status register and compares
3497 * with the provided bit mask to check if HBA completed
3498 * the restart. This function will wait in a loop for the
3499 * HBA to complete restart. If the HBA does not restart within
3500 * 15 iterations, the function will reset the HBA again. The
3501 * function returns 1 when HBA fail to restart otherwise returns
3502 * zero.
3503 **/
James Smart3772a992009-05-22 14:50:54 -04003504static int
3505lpfc_sli_brdready_s3(struct lpfc_hba *phba, uint32_t mask)
dea31012005-04-17 16:05:31 -05003506{
Jamie Wellnitz41415862006-02-28 19:25:27 -05003507 uint32_t status;
3508 int i = 0;
3509 int retval = 0;
dea31012005-04-17 16:05:31 -05003510
Jamie Wellnitz41415862006-02-28 19:25:27 -05003511 /* Read the HBA Host Status Register */
James Smart9940b972011-03-11 16:06:12 -05003512 if (lpfc_readl(phba->HSregaddr, &status))
3513 return 1;
dea31012005-04-17 16:05:31 -05003514
Jamie Wellnitz41415862006-02-28 19:25:27 -05003515 /*
3516 * Check status register every 100ms for 5 retries, then every
3517 * 500ms for 5, then every 2.5 sec for 5, then reset board and
3518 * every 2.5 sec for 4.
3519 * Break our of the loop if errors occurred during init.
3520 */
3521 while (((status & mask) != mask) &&
3522 !(status & HS_FFERM) &&
3523 i++ < 20) {
dea31012005-04-17 16:05:31 -05003524
Jamie Wellnitz41415862006-02-28 19:25:27 -05003525 if (i <= 5)
3526 msleep(10);
3527 else if (i <= 10)
3528 msleep(500);
3529 else
3530 msleep(2500);
dea31012005-04-17 16:05:31 -05003531
Jamie Wellnitz41415862006-02-28 19:25:27 -05003532 if (i == 15) {
James Smart2e0fef82007-06-17 19:56:36 -05003533 /* Do post */
James Smart92d7f7b2007-06-17 19:56:38 -05003534 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003535 lpfc_sli_brdrestart(phba);
3536 }
3537 /* Read the HBA Host Status Register */
James Smart9940b972011-03-11 16:06:12 -05003538 if (lpfc_readl(phba->HSregaddr, &status)) {
3539 retval = 1;
3540 break;
3541 }
dea31012005-04-17 16:05:31 -05003542 }
dea31012005-04-17 16:05:31 -05003543
Jamie Wellnitz41415862006-02-28 19:25:27 -05003544 /* Check to see if any errors occurred during init */
3545 if ((status & HS_FFERM) || (i >= 20)) {
James Smarte40a02c2010-02-26 14:13:54 -05003546 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3547 "2751 Adapter failed to restart, "
3548 "status reg x%x, FW Data: A8 x%x AC x%x\n",
3549 status,
3550 readl(phba->MBslimaddr + 0xa8),
3551 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05003552 phba->link_state = LPFC_HBA_ERROR;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003553 retval = 1;
3554 }
dea31012005-04-17 16:05:31 -05003555
Jamie Wellnitz41415862006-02-28 19:25:27 -05003556 return retval;
dea31012005-04-17 16:05:31 -05003557}
3558
James Smartda0436e2009-05-22 14:51:39 -04003559/**
3560 * lpfc_sli_brdready_s4 - Check for sli4 host ready status
3561 * @phba: Pointer to HBA context object.
3562 * @mask: Bit mask to be checked.
3563 *
3564 * This function checks the host status register to check if HBA is
3565 * ready. This function will wait in a loop for the HBA to be ready
3566 * If the HBA is not ready , the function will will reset the HBA PCI
3567 * function again. The function returns 1 when HBA fail to be ready
3568 * otherwise returns zero.
3569 **/
3570static int
3571lpfc_sli_brdready_s4(struct lpfc_hba *phba, uint32_t mask)
3572{
3573 uint32_t status;
3574 int retval = 0;
3575
3576 /* Read the HBA Host Status Register */
3577 status = lpfc_sli4_post_status_check(phba);
3578
3579 if (status) {
3580 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
3581 lpfc_sli_brdrestart(phba);
3582 status = lpfc_sli4_post_status_check(phba);
3583 }
3584
3585 /* Check to see if any errors occurred during init */
3586 if (status) {
3587 phba->link_state = LPFC_HBA_ERROR;
3588 retval = 1;
3589 } else
3590 phba->sli4_hba.intr_enable = 0;
3591
3592 return retval;
3593}
3594
3595/**
3596 * lpfc_sli_brdready - Wrapper func for checking the hba readyness
3597 * @phba: Pointer to HBA context object.
3598 * @mask: Bit mask to be checked.
3599 *
3600 * This routine wraps the actual SLI3 or SLI4 hba readyness check routine
3601 * from the API jump table function pointer from the lpfc_hba struct.
3602 **/
3603int
3604lpfc_sli_brdready(struct lpfc_hba *phba, uint32_t mask)
3605{
3606 return phba->lpfc_sli_brdready(phba, mask);
3607}
3608
James Smart92908312006-03-07 15:04:13 -05003609#define BARRIER_TEST_PATTERN (0xdeadbeef)
3610
James Smarte59058c2008-08-24 21:49:00 -04003611/**
James Smart3621a712009-04-06 18:47:14 -04003612 * lpfc_reset_barrier - Make HBA ready for HBA reset
James Smarte59058c2008-08-24 21:49:00 -04003613 * @phba: Pointer to HBA context object.
3614 *
James Smart1b511972011-12-13 13:23:09 -05003615 * This function is called before resetting an HBA. This function is called
3616 * with hbalock held and requests HBA to quiesce DMAs before a reset.
James Smarte59058c2008-08-24 21:49:00 -04003617 **/
James Smart2e0fef82007-06-17 19:56:36 -05003618void lpfc_reset_barrier(struct lpfc_hba *phba)
James Smart92908312006-03-07 15:04:13 -05003619{
James Smart65a29c12006-07-06 15:50:50 -04003620 uint32_t __iomem *resp_buf;
3621 uint32_t __iomem *mbox_buf;
James Smart92908312006-03-07 15:04:13 -05003622 volatile uint32_t mbox;
James Smart9940b972011-03-11 16:06:12 -05003623 uint32_t hc_copy, ha_copy, resp_data;
James Smart92908312006-03-07 15:04:13 -05003624 int i;
3625 uint8_t hdrtype;
3626
3627 pci_read_config_byte(phba->pcidev, PCI_HEADER_TYPE, &hdrtype);
3628 if (hdrtype != 0x80 ||
3629 (FC_JEDEC_ID(phba->vpd.rev.biuRev) != HELIOS_JEDEC_ID &&
3630 FC_JEDEC_ID(phba->vpd.rev.biuRev) != THOR_JEDEC_ID))
3631 return;
3632
3633 /*
3634 * Tell the other part of the chip to suspend temporarily all
3635 * its DMA activity.
3636 */
James Smart65a29c12006-07-06 15:50:50 -04003637 resp_buf = phba->MBslimaddr;
James Smart92908312006-03-07 15:04:13 -05003638
3639 /* Disable the error attention */
James Smart9940b972011-03-11 16:06:12 -05003640 if (lpfc_readl(phba->HCregaddr, &hc_copy))
3641 return;
James Smart92908312006-03-07 15:04:13 -05003642 writel((hc_copy & ~HC_ERINT_ENA), phba->HCregaddr);
3643 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05003644 phba->link_flag |= LS_IGNORE_ERATT;
James Smart92908312006-03-07 15:04:13 -05003645
James Smart9940b972011-03-11 16:06:12 -05003646 if (lpfc_readl(phba->HAregaddr, &ha_copy))
3647 return;
3648 if (ha_copy & HA_ERATT) {
James Smart92908312006-03-07 15:04:13 -05003649 /* Clear Chip error bit */
3650 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05003651 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05003652 }
3653
3654 mbox = 0;
3655 ((MAILBOX_t *)&mbox)->mbxCommand = MBX_KILL_BOARD;
3656 ((MAILBOX_t *)&mbox)->mbxOwner = OWN_CHIP;
3657
3658 writel(BARRIER_TEST_PATTERN, (resp_buf + 1));
James Smart65a29c12006-07-06 15:50:50 -04003659 mbox_buf = phba->MBslimaddr;
James Smart92908312006-03-07 15:04:13 -05003660 writel(mbox, mbox_buf);
3661
James Smart9940b972011-03-11 16:06:12 -05003662 for (i = 0; i < 50; i++) {
3663 if (lpfc_readl((resp_buf + 1), &resp_data))
3664 return;
3665 if (resp_data != ~(BARRIER_TEST_PATTERN))
3666 mdelay(1);
3667 else
3668 break;
3669 }
3670 resp_data = 0;
3671 if (lpfc_readl((resp_buf + 1), &resp_data))
3672 return;
3673 if (resp_data != ~(BARRIER_TEST_PATTERN)) {
James Smartf4b4c682009-05-22 14:53:12 -04003674 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE ||
James Smart2e0fef82007-06-17 19:56:36 -05003675 phba->pport->stopped)
James Smart92908312006-03-07 15:04:13 -05003676 goto restore_hc;
3677 else
3678 goto clear_errat;
3679 }
3680
3681 ((MAILBOX_t *)&mbox)->mbxOwner = OWN_HOST;
James Smart9940b972011-03-11 16:06:12 -05003682 resp_data = 0;
3683 for (i = 0; i < 500; i++) {
3684 if (lpfc_readl(resp_buf, &resp_data))
3685 return;
3686 if (resp_data != mbox)
3687 mdelay(1);
3688 else
3689 break;
3690 }
James Smart92908312006-03-07 15:04:13 -05003691
3692clear_errat:
3693
James Smart9940b972011-03-11 16:06:12 -05003694 while (++i < 500) {
3695 if (lpfc_readl(phba->HAregaddr, &ha_copy))
3696 return;
3697 if (!(ha_copy & HA_ERATT))
3698 mdelay(1);
3699 else
3700 break;
3701 }
James Smart92908312006-03-07 15:04:13 -05003702
3703 if (readl(phba->HAregaddr) & HA_ERATT) {
3704 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05003705 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05003706 }
3707
3708restore_hc:
James Smart2e0fef82007-06-17 19:56:36 -05003709 phba->link_flag &= ~LS_IGNORE_ERATT;
James Smart92908312006-03-07 15:04:13 -05003710 writel(hc_copy, phba->HCregaddr);
3711 readl(phba->HCregaddr); /* flush */
3712}
3713
James Smarte59058c2008-08-24 21:49:00 -04003714/**
James Smart3621a712009-04-06 18:47:14 -04003715 * lpfc_sli_brdkill - Issue a kill_board mailbox command
James Smarte59058c2008-08-24 21:49:00 -04003716 * @phba: Pointer to HBA context object.
3717 *
3718 * This function issues a kill_board mailbox command and waits for
3719 * the error attention interrupt. This function is called for stopping
3720 * the firmware processing. The caller is not required to hold any
3721 * locks. This function calls lpfc_hba_down_post function to free
3722 * any pending commands after the kill. The function will return 1 when it
3723 * fails to kill the board else will return 0.
3724 **/
Jamie Wellnitz41415862006-02-28 19:25:27 -05003725int
James Smart2e0fef82007-06-17 19:56:36 -05003726lpfc_sli_brdkill(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05003727{
Jamie Wellnitz41415862006-02-28 19:25:27 -05003728 struct lpfc_sli *psli;
3729 LPFC_MBOXQ_t *pmb;
3730 uint32_t status;
3731 uint32_t ha_copy;
3732 int retval;
3733 int i = 0;
3734
3735 psli = &phba->sli;
3736
3737 /* Kill HBA */
James Smarted957682007-06-17 19:56:37 -05003738 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003739 "0329 Kill HBA Data: x%x x%x\n",
3740 phba->pport->port_state, psli->sli_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003741
James Smart98c9ea52007-10-27 13:37:33 -04003742 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3743 if (!pmb)
Jamie Wellnitz41415862006-02-28 19:25:27 -05003744 return 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003745
3746 /* Disable the error attention */
James Smart2e0fef82007-06-17 19:56:36 -05003747 spin_lock_irq(&phba->hbalock);
James Smart9940b972011-03-11 16:06:12 -05003748 if (lpfc_readl(phba->HCregaddr, &status)) {
3749 spin_unlock_irq(&phba->hbalock);
3750 mempool_free(pmb, phba->mbox_mem_pool);
3751 return 1;
3752 }
Jamie Wellnitz41415862006-02-28 19:25:27 -05003753 status &= ~HC_ERINT_ENA;
3754 writel(status, phba->HCregaddr);
3755 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05003756 phba->link_flag |= LS_IGNORE_ERATT;
3757 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003758
3759 lpfc_kill_board(phba, pmb);
3760 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
3761 retval = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
3762
3763 if (retval != MBX_SUCCESS) {
3764 if (retval != MBX_BUSY)
3765 mempool_free(pmb, phba->mbox_mem_pool);
James Smarte40a02c2010-02-26 14:13:54 -05003766 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3767 "2752 KILL_BOARD command failed retval %d\n",
3768 retval);
James Smart2e0fef82007-06-17 19:56:36 -05003769 spin_lock_irq(&phba->hbalock);
3770 phba->link_flag &= ~LS_IGNORE_ERATT;
3771 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003772 return 1;
3773 }
3774
James Smartf4b4c682009-05-22 14:53:12 -04003775 spin_lock_irq(&phba->hbalock);
3776 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
3777 spin_unlock_irq(&phba->hbalock);
James Smart92908312006-03-07 15:04:13 -05003778
Jamie Wellnitz41415862006-02-28 19:25:27 -05003779 mempool_free(pmb, phba->mbox_mem_pool);
3780
3781 /* There is no completion for a KILL_BOARD mbox cmd. Check for an error
3782 * attention every 100ms for 3 seconds. If we don't get ERATT after
3783 * 3 seconds we still set HBA_ERROR state because the status of the
3784 * board is now undefined.
3785 */
James Smart9940b972011-03-11 16:06:12 -05003786 if (lpfc_readl(phba->HAregaddr, &ha_copy))
3787 return 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003788 while ((i++ < 30) && !(ha_copy & HA_ERATT)) {
3789 mdelay(100);
James Smart9940b972011-03-11 16:06:12 -05003790 if (lpfc_readl(phba->HAregaddr, &ha_copy))
3791 return 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003792 }
3793
3794 del_timer_sync(&psli->mbox_tmo);
James Smart92908312006-03-07 15:04:13 -05003795 if (ha_copy & HA_ERATT) {
3796 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05003797 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05003798 }
James Smart2e0fef82007-06-17 19:56:36 -05003799 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003800 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart04c68492009-05-22 14:52:52 -04003801 psli->mbox_active = NULL;
James Smart2e0fef82007-06-17 19:56:36 -05003802 phba->link_flag &= ~LS_IGNORE_ERATT;
3803 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003804
Jamie Wellnitz41415862006-02-28 19:25:27 -05003805 lpfc_hba_down_post(phba);
James Smart2e0fef82007-06-17 19:56:36 -05003806 phba->link_state = LPFC_HBA_ERROR;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003807
James Smart2e0fef82007-06-17 19:56:36 -05003808 return ha_copy & HA_ERATT ? 0 : 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003809}
3810
James Smarte59058c2008-08-24 21:49:00 -04003811/**
James Smart3772a992009-05-22 14:50:54 -04003812 * lpfc_sli_brdreset - Reset a sli-2 or sli-3 HBA
James Smarte59058c2008-08-24 21:49:00 -04003813 * @phba: Pointer to HBA context object.
3814 *
3815 * This function resets the HBA by writing HC_INITFF to the control
3816 * register. After the HBA resets, this function resets all the iocb ring
3817 * indices. This function disables PCI layer parity checking during
3818 * the reset.
3819 * This function returns 0 always.
3820 * The caller is not required to hold any locks.
3821 **/
Jamie Wellnitz41415862006-02-28 19:25:27 -05003822int
James Smart2e0fef82007-06-17 19:56:36 -05003823lpfc_sli_brdreset(struct lpfc_hba *phba)
Jamie Wellnitz41415862006-02-28 19:25:27 -05003824{
3825 struct lpfc_sli *psli;
dea31012005-04-17 16:05:31 -05003826 struct lpfc_sli_ring *pring;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003827 uint16_t cfg_value;
dea31012005-04-17 16:05:31 -05003828 int i;
dea31012005-04-17 16:05:31 -05003829
Jamie Wellnitz41415862006-02-28 19:25:27 -05003830 psli = &phba->sli;
dea31012005-04-17 16:05:31 -05003831
Jamie Wellnitz41415862006-02-28 19:25:27 -05003832 /* Reset HBA */
3833 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003834 "0325 Reset HBA Data: x%x x%x\n",
James Smart2e0fef82007-06-17 19:56:36 -05003835 phba->pport->port_state, psli->sli_flag);
dea31012005-04-17 16:05:31 -05003836
3837 /* perform board reset */
3838 phba->fc_eventTag = 0;
James Smart4d9ab992009-10-02 15:16:39 -04003839 phba->link_events = 0;
James Smart2e0fef82007-06-17 19:56:36 -05003840 phba->pport->fc_myDID = 0;
3841 phba->pport->fc_prevDID = 0;
dea31012005-04-17 16:05:31 -05003842
Jamie Wellnitz41415862006-02-28 19:25:27 -05003843 /* Turn off parity checking and serr during the physical reset */
3844 pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
3845 pci_write_config_word(phba->pcidev, PCI_COMMAND,
3846 (cfg_value &
3847 ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
3848
James Smart3772a992009-05-22 14:50:54 -04003849 psli->sli_flag &= ~(LPFC_SLI_ACTIVE | LPFC_PROCESS_LA);
3850
Jamie Wellnitz41415862006-02-28 19:25:27 -05003851 /* Now toggle INITFF bit in the Host Control Register */
3852 writel(HC_INITFF, phba->HCregaddr);
3853 mdelay(1);
3854 readl(phba->HCregaddr); /* flush */
3855 writel(0, phba->HCregaddr);
3856 readl(phba->HCregaddr); /* flush */
3857
3858 /* Restore PCI cmd register */
3859 pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
dea31012005-04-17 16:05:31 -05003860
3861 /* Initialize relevant SLI info */
Jamie Wellnitz41415862006-02-28 19:25:27 -05003862 for (i = 0; i < psli->num_rings; i++) {
3863 pring = &psli->ring[i];
dea31012005-04-17 16:05:31 -05003864 pring->flag = 0;
3865 pring->rspidx = 0;
3866 pring->next_cmdidx = 0;
3867 pring->local_getidx = 0;
3868 pring->cmdidx = 0;
3869 pring->missbufcnt = 0;
3870 }
dea31012005-04-17 16:05:31 -05003871
James Smart2e0fef82007-06-17 19:56:36 -05003872 phba->link_state = LPFC_WARM_START;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003873 return 0;
3874}
3875
James Smarte59058c2008-08-24 21:49:00 -04003876/**
James Smartda0436e2009-05-22 14:51:39 -04003877 * lpfc_sli4_brdreset - Reset a sli-4 HBA
3878 * @phba: Pointer to HBA context object.
3879 *
3880 * This function resets a SLI4 HBA. This function disables PCI layer parity
3881 * checking during resets the device. The caller is not required to hold
3882 * any locks.
3883 *
3884 * This function returns 0 always.
3885 **/
3886int
3887lpfc_sli4_brdreset(struct lpfc_hba *phba)
3888{
3889 struct lpfc_sli *psli = &phba->sli;
3890 uint16_t cfg_value;
James Smart27b01b82012-05-09 21:19:44 -04003891 int rc;
James Smartda0436e2009-05-22 14:51:39 -04003892
3893 /* Reset HBA */
3894 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3895 "0295 Reset HBA Data: x%x x%x\n",
3896 phba->pport->port_state, psli->sli_flag);
3897
3898 /* perform board reset */
3899 phba->fc_eventTag = 0;
James Smart4d9ab992009-10-02 15:16:39 -04003900 phba->link_events = 0;
James Smartda0436e2009-05-22 14:51:39 -04003901 phba->pport->fc_myDID = 0;
3902 phba->pport->fc_prevDID = 0;
3903
James Smartda0436e2009-05-22 14:51:39 -04003904 spin_lock_irq(&phba->hbalock);
3905 psli->sli_flag &= ~(LPFC_PROCESS_LA);
3906 phba->fcf.fcf_flag = 0;
James Smartda0436e2009-05-22 14:51:39 -04003907 spin_unlock_irq(&phba->hbalock);
3908
3909 /* Now physically reset the device */
3910 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3911 "0389 Performing PCI function reset!\n");
James Smartbe858b62010-12-15 17:57:20 -05003912
3913 /* Turn off parity checking and serr during the physical reset */
3914 pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
3915 pci_write_config_word(phba->pcidev, PCI_COMMAND, (cfg_value &
3916 ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
3917
James Smartda0436e2009-05-22 14:51:39 -04003918 /* Perform FCoE PCI function reset */
James Smart2e90f4b2011-12-13 13:22:37 -05003919 lpfc_sli4_queue_destroy(phba);
James Smart27b01b82012-05-09 21:19:44 -04003920 rc = lpfc_pci_function_reset(phba);
James Smartda0436e2009-05-22 14:51:39 -04003921
James Smartbe858b62010-12-15 17:57:20 -05003922 /* Restore PCI cmd register */
3923 pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
3924
James Smart27b01b82012-05-09 21:19:44 -04003925 return rc;
James Smartda0436e2009-05-22 14:51:39 -04003926}
3927
3928/**
3929 * lpfc_sli_brdrestart_s3 - Restart a sli-3 hba
James Smarte59058c2008-08-24 21:49:00 -04003930 * @phba: Pointer to HBA context object.
3931 *
3932 * This function is called in the SLI initialization code path to
3933 * restart the HBA. The caller is not required to hold any lock.
3934 * This function writes MBX_RESTART mailbox command to the SLIM and
3935 * resets the HBA. At the end of the function, it calls lpfc_hba_down_post
3936 * function to free any pending commands. The function enables
3937 * POST only during the first initialization. The function returns zero.
3938 * The function does not guarantee completion of MBX_RESTART mailbox
3939 * command before the return of this function.
3940 **/
James Smartda0436e2009-05-22 14:51:39 -04003941static int
3942lpfc_sli_brdrestart_s3(struct lpfc_hba *phba)
Jamie Wellnitz41415862006-02-28 19:25:27 -05003943{
3944 MAILBOX_t *mb;
3945 struct lpfc_sli *psli;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003946 volatile uint32_t word0;
3947 void __iomem *to_slim;
James Smart0d878412009-10-02 15:16:56 -04003948 uint32_t hba_aer_enabled;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003949
James Smart2e0fef82007-06-17 19:56:36 -05003950 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003951
James Smart0d878412009-10-02 15:16:56 -04003952 /* Take PCIe device Advanced Error Reporting (AER) state */
3953 hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
3954
Jamie Wellnitz41415862006-02-28 19:25:27 -05003955 psli = &phba->sli;
3956
3957 /* Restart HBA */
3958 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003959 "0337 Restart HBA Data: x%x x%x\n",
James Smart2e0fef82007-06-17 19:56:36 -05003960 phba->pport->port_state, psli->sli_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003961
3962 word0 = 0;
3963 mb = (MAILBOX_t *) &word0;
3964 mb->mbxCommand = MBX_RESTART;
3965 mb->mbxHc = 1;
3966
James Smart92908312006-03-07 15:04:13 -05003967 lpfc_reset_barrier(phba);
3968
Jamie Wellnitz41415862006-02-28 19:25:27 -05003969 to_slim = phba->MBslimaddr;
3970 writel(*(uint32_t *) mb, to_slim);
3971 readl(to_slim); /* flush */
3972
3973 /* Only skip post after fc_ffinit is completed */
James Smarteaf15d52008-12-04 22:39:29 -05003974 if (phba->pport->port_state)
Jamie Wellnitz41415862006-02-28 19:25:27 -05003975 word0 = 1; /* This is really setting up word1 */
James Smarteaf15d52008-12-04 22:39:29 -05003976 else
Jamie Wellnitz41415862006-02-28 19:25:27 -05003977 word0 = 0; /* This is really setting up word1 */
James Smart65a29c12006-07-06 15:50:50 -04003978 to_slim = phba->MBslimaddr + sizeof (uint32_t);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003979 writel(*(uint32_t *) mb, to_slim);
3980 readl(to_slim); /* flush */
3981
3982 lpfc_sli_brdreset(phba);
James Smart2e0fef82007-06-17 19:56:36 -05003983 phba->pport->stopped = 0;
3984 phba->link_state = LPFC_INIT_START;
James Smartda0436e2009-05-22 14:51:39 -04003985 phba->hba_flag = 0;
James Smart2e0fef82007-06-17 19:56:36 -05003986 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003987
James Smart64ba8812006-08-02 15:24:34 -04003988 memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
3989 psli->stats_start = get_seconds();
3990
James Smarteaf15d52008-12-04 22:39:29 -05003991 /* Give the INITFF and Post time to settle. */
3992 mdelay(100);
dea31012005-04-17 16:05:31 -05003993
James Smart0d878412009-10-02 15:16:56 -04003994 /* Reset HBA AER if it was enabled, note hba_flag was reset above */
3995 if (hba_aer_enabled)
3996 pci_disable_pcie_error_reporting(phba->pcidev);
3997
Jamie Wellnitz41415862006-02-28 19:25:27 -05003998 lpfc_hba_down_post(phba);
dea31012005-04-17 16:05:31 -05003999
4000 return 0;
4001}
4002
James Smarte59058c2008-08-24 21:49:00 -04004003/**
James Smartda0436e2009-05-22 14:51:39 -04004004 * lpfc_sli_brdrestart_s4 - Restart the sli-4 hba
4005 * @phba: Pointer to HBA context object.
4006 *
4007 * This function is called in the SLI initialization code path to restart
4008 * a SLI4 HBA. The caller is not required to hold any lock.
4009 * At the end of the function, it calls lpfc_hba_down_post function to
4010 * free any pending commands.
4011 **/
4012static int
4013lpfc_sli_brdrestart_s4(struct lpfc_hba *phba)
4014{
4015 struct lpfc_sli *psli = &phba->sli;
James Smart75baf692010-06-08 18:31:21 -04004016 uint32_t hba_aer_enabled;
James Smart27b01b82012-05-09 21:19:44 -04004017 int rc;
James Smartda0436e2009-05-22 14:51:39 -04004018
4019 /* Restart HBA */
4020 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4021 "0296 Restart HBA Data: x%x x%x\n",
4022 phba->pport->port_state, psli->sli_flag);
4023
James Smart75baf692010-06-08 18:31:21 -04004024 /* Take PCIe device Advanced Error Reporting (AER) state */
4025 hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
4026
James Smart27b01b82012-05-09 21:19:44 -04004027 rc = lpfc_sli4_brdreset(phba);
James Smartda0436e2009-05-22 14:51:39 -04004028
4029 spin_lock_irq(&phba->hbalock);
4030 phba->pport->stopped = 0;
4031 phba->link_state = LPFC_INIT_START;
4032 phba->hba_flag = 0;
4033 spin_unlock_irq(&phba->hbalock);
4034
4035 memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
4036 psli->stats_start = get_seconds();
4037
James Smart75baf692010-06-08 18:31:21 -04004038 /* Reset HBA AER if it was enabled, note hba_flag was reset above */
4039 if (hba_aer_enabled)
4040 pci_disable_pcie_error_reporting(phba->pcidev);
4041
James Smartda0436e2009-05-22 14:51:39 -04004042 lpfc_hba_down_post(phba);
4043
James Smart27b01b82012-05-09 21:19:44 -04004044 return rc;
James Smartda0436e2009-05-22 14:51:39 -04004045}
4046
4047/**
4048 * lpfc_sli_brdrestart - Wrapper func for restarting hba
4049 * @phba: Pointer to HBA context object.
4050 *
4051 * This routine wraps the actual SLI3 or SLI4 hba restart routine from the
4052 * API jump table function pointer from the lpfc_hba struct.
4053**/
4054int
4055lpfc_sli_brdrestart(struct lpfc_hba *phba)
4056{
4057 return phba->lpfc_sli_brdrestart(phba);
4058}
4059
4060/**
James Smart3621a712009-04-06 18:47:14 -04004061 * lpfc_sli_chipset_init - Wait for the restart of the HBA after a restart
James Smarte59058c2008-08-24 21:49:00 -04004062 * @phba: Pointer to HBA context object.
4063 *
4064 * This function is called after a HBA restart to wait for successful
4065 * restart of the HBA. Successful restart of the HBA is indicated by
4066 * HS_FFRDY and HS_MBRDY bits. If the HBA fails to restart even after 15
4067 * iteration, the function will restart the HBA again. The function returns
4068 * zero if HBA successfully restarted else returns negative error code.
4069 **/
dea31012005-04-17 16:05:31 -05004070static int
4071lpfc_sli_chipset_init(struct lpfc_hba *phba)
4072{
4073 uint32_t status, i = 0;
4074
4075 /* Read the HBA Host Status Register */
James Smart9940b972011-03-11 16:06:12 -05004076 if (lpfc_readl(phba->HSregaddr, &status))
4077 return -EIO;
dea31012005-04-17 16:05:31 -05004078
4079 /* Check status register to see what current state is */
4080 i = 0;
4081 while ((status & (HS_FFRDY | HS_MBRDY)) != (HS_FFRDY | HS_MBRDY)) {
4082
James Smartdcf2a4e2010-09-29 11:18:53 -04004083 /* Check every 10ms for 10 retries, then every 100ms for 90
4084 * retries, then every 1 sec for 50 retires for a total of
4085 * ~60 seconds before reset the board again and check every
4086 * 1 sec for 50 retries. The up to 60 seconds before the
4087 * board ready is required by the Falcon FIPS zeroization
4088 * complete, and any reset the board in between shall cause
4089 * restart of zeroization, further delay the board ready.
dea31012005-04-17 16:05:31 -05004090 */
James Smartdcf2a4e2010-09-29 11:18:53 -04004091 if (i++ >= 200) {
dea31012005-04-17 16:05:31 -05004092 /* Adapter failed to init, timeout, status reg
4093 <status> */
James Smarted957682007-06-17 19:56:37 -05004094 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004095 "0436 Adapter failed to init, "
James Smart09372822008-01-11 01:52:54 -05004096 "timeout, status reg x%x, "
4097 "FW Data: A8 x%x AC x%x\n", status,
4098 readl(phba->MBslimaddr + 0xa8),
4099 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05004100 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05004101 return -ETIMEDOUT;
4102 }
4103
4104 /* Check to see if any errors occurred during init */
4105 if (status & HS_FFERM) {
4106 /* ERROR: During chipset initialization */
4107 /* Adapter failed to init, chipset, status reg
4108 <status> */
James Smarted957682007-06-17 19:56:37 -05004109 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004110 "0437 Adapter failed to init, "
James Smart09372822008-01-11 01:52:54 -05004111 "chipset, status reg x%x, "
4112 "FW Data: A8 x%x AC x%x\n", status,
4113 readl(phba->MBslimaddr + 0xa8),
4114 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05004115 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05004116 return -EIO;
4117 }
4118
James Smartdcf2a4e2010-09-29 11:18:53 -04004119 if (i <= 10)
dea31012005-04-17 16:05:31 -05004120 msleep(10);
James Smartdcf2a4e2010-09-29 11:18:53 -04004121 else if (i <= 100)
4122 msleep(100);
4123 else
4124 msleep(1000);
dea31012005-04-17 16:05:31 -05004125
James Smartdcf2a4e2010-09-29 11:18:53 -04004126 if (i == 150) {
4127 /* Do post */
James Smart92d7f7b2007-06-17 19:56:38 -05004128 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004129 lpfc_sli_brdrestart(phba);
dea31012005-04-17 16:05:31 -05004130 }
4131 /* Read the HBA Host Status Register */
James Smart9940b972011-03-11 16:06:12 -05004132 if (lpfc_readl(phba->HSregaddr, &status))
4133 return -EIO;
dea31012005-04-17 16:05:31 -05004134 }
4135
4136 /* Check to see if any errors occurred during init */
4137 if (status & HS_FFERM) {
4138 /* ERROR: During chipset initialization */
4139 /* Adapter failed to init, chipset, status reg <status> */
James Smarted957682007-06-17 19:56:37 -05004140 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004141 "0438 Adapter failed to init, chipset, "
James Smart09372822008-01-11 01:52:54 -05004142 "status reg x%x, "
4143 "FW Data: A8 x%x AC x%x\n", status,
4144 readl(phba->MBslimaddr + 0xa8),
4145 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05004146 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05004147 return -EIO;
4148 }
4149
4150 /* Clear all interrupt enable conditions */
4151 writel(0, phba->HCregaddr);
4152 readl(phba->HCregaddr); /* flush */
4153
4154 /* setup host attn register */
4155 writel(0xffffffff, phba->HAregaddr);
4156 readl(phba->HAregaddr); /* flush */
4157 return 0;
4158}
4159
James Smarte59058c2008-08-24 21:49:00 -04004160/**
James Smart3621a712009-04-06 18:47:14 -04004161 * lpfc_sli_hbq_count - Get the number of HBQs to be configured
James Smarte59058c2008-08-24 21:49:00 -04004162 *
4163 * This function calculates and returns the number of HBQs required to be
4164 * configured.
4165 **/
James Smart78b2d852007-08-02 11:10:21 -04004166int
James Smarted957682007-06-17 19:56:37 -05004167lpfc_sli_hbq_count(void)
4168{
James Smart92d7f7b2007-06-17 19:56:38 -05004169 return ARRAY_SIZE(lpfc_hbq_defs);
James Smarted957682007-06-17 19:56:37 -05004170}
4171
James Smarte59058c2008-08-24 21:49:00 -04004172/**
James Smart3621a712009-04-06 18:47:14 -04004173 * lpfc_sli_hbq_entry_count - Calculate total number of hbq entries
James Smarte59058c2008-08-24 21:49:00 -04004174 *
4175 * This function adds the number of hbq entries in every HBQ to get
4176 * the total number of hbq entries required for the HBA and returns
4177 * the total count.
4178 **/
James Smarted957682007-06-17 19:56:37 -05004179static int
4180lpfc_sli_hbq_entry_count(void)
4181{
4182 int hbq_count = lpfc_sli_hbq_count();
4183 int count = 0;
4184 int i;
4185
4186 for (i = 0; i < hbq_count; ++i)
James Smart92d7f7b2007-06-17 19:56:38 -05004187 count += lpfc_hbq_defs[i]->entry_count;
James Smarted957682007-06-17 19:56:37 -05004188 return count;
4189}
4190
James Smarte59058c2008-08-24 21:49:00 -04004191/**
James Smart3621a712009-04-06 18:47:14 -04004192 * lpfc_sli_hbq_size - Calculate memory required for all hbq entries
James Smarte59058c2008-08-24 21:49:00 -04004193 *
4194 * This function calculates amount of memory required for all hbq entries
4195 * to be configured and returns the total memory required.
4196 **/
dea31012005-04-17 16:05:31 -05004197int
James Smarted957682007-06-17 19:56:37 -05004198lpfc_sli_hbq_size(void)
4199{
4200 return lpfc_sli_hbq_entry_count() * sizeof(struct lpfc_hbq_entry);
4201}
4202
James Smarte59058c2008-08-24 21:49:00 -04004203/**
James Smart3621a712009-04-06 18:47:14 -04004204 * lpfc_sli_hbq_setup - configure and initialize HBQs
James Smarte59058c2008-08-24 21:49:00 -04004205 * @phba: Pointer to HBA context object.
4206 *
4207 * This function is called during the SLI initialization to configure
4208 * all the HBQs and post buffers to the HBQ. The caller is not
4209 * required to hold any locks. This function will return zero if successful
4210 * else it will return negative error code.
4211 **/
James Smarted957682007-06-17 19:56:37 -05004212static int
4213lpfc_sli_hbq_setup(struct lpfc_hba *phba)
4214{
4215 int hbq_count = lpfc_sli_hbq_count();
4216 LPFC_MBOXQ_t *pmb;
4217 MAILBOX_t *pmbox;
4218 uint32_t hbqno;
4219 uint32_t hbq_entry_index;
James Smarted957682007-06-17 19:56:37 -05004220
James Smart92d7f7b2007-06-17 19:56:38 -05004221 /* Get a Mailbox buffer to setup mailbox
4222 * commands for HBA initialization
4223 */
James Smarted957682007-06-17 19:56:37 -05004224 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4225
4226 if (!pmb)
4227 return -ENOMEM;
4228
James Smart04c68492009-05-22 14:52:52 -04004229 pmbox = &pmb->u.mb;
James Smarted957682007-06-17 19:56:37 -05004230
4231 /* Initialize the struct lpfc_sli_hbq structure for each hbq */
4232 phba->link_state = LPFC_INIT_MBX_CMDS;
James Smart3163f722008-02-08 18:50:25 -05004233 phba->hbq_in_use = 1;
James Smarted957682007-06-17 19:56:37 -05004234
4235 hbq_entry_index = 0;
4236 for (hbqno = 0; hbqno < hbq_count; ++hbqno) {
4237 phba->hbqs[hbqno].next_hbqPutIdx = 0;
4238 phba->hbqs[hbqno].hbqPutIdx = 0;
4239 phba->hbqs[hbqno].local_hbqGetIdx = 0;
4240 phba->hbqs[hbqno].entry_count =
James Smart92d7f7b2007-06-17 19:56:38 -05004241 lpfc_hbq_defs[hbqno]->entry_count;
James Smart51ef4c22007-08-02 11:10:31 -04004242 lpfc_config_hbq(phba, hbqno, lpfc_hbq_defs[hbqno],
4243 hbq_entry_index, pmb);
James Smarted957682007-06-17 19:56:37 -05004244 hbq_entry_index += phba->hbqs[hbqno].entry_count;
4245
4246 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
4247 /* Adapter failed to init, mbxCmd <cmd> CFG_RING,
4248 mbxStatus <status>, ring <num> */
4249
4250 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -05004251 LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04004252 "1805 Adapter failed to init. "
James Smarted957682007-06-17 19:56:37 -05004253 "Data: x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04004254 pmbox->mbxCommand,
James Smarted957682007-06-17 19:56:37 -05004255 pmbox->mbxStatus, hbqno);
4256
4257 phba->link_state = LPFC_HBA_ERROR;
4258 mempool_free(pmb, phba->mbox_mem_pool);
James Smart6e7288d2010-06-07 15:23:35 -04004259 return -ENXIO;
James Smarted957682007-06-17 19:56:37 -05004260 }
4261 }
4262 phba->hbq_count = hbq_count;
4263
James Smarted957682007-06-17 19:56:37 -05004264 mempool_free(pmb, phba->mbox_mem_pool);
4265
James Smart92d7f7b2007-06-17 19:56:38 -05004266 /* Initially populate or replenish the HBQs */
James Smartd7c255b2008-08-24 21:50:00 -04004267 for (hbqno = 0; hbqno < hbq_count; ++hbqno)
4268 lpfc_sli_hbqbuf_init_hbqs(phba, hbqno);
James Smarted957682007-06-17 19:56:37 -05004269 return 0;
4270}
4271
James Smarte59058c2008-08-24 21:49:00 -04004272/**
James Smart4f774512009-05-22 14:52:35 -04004273 * lpfc_sli4_rb_setup - Initialize and post RBs to HBA
4274 * @phba: Pointer to HBA context object.
4275 *
4276 * This function is called during the SLI initialization to configure
4277 * all the HBQs and post buffers to the HBQ. The caller is not
4278 * required to hold any locks. This function will return zero if successful
4279 * else it will return negative error code.
4280 **/
4281static int
4282lpfc_sli4_rb_setup(struct lpfc_hba *phba)
4283{
4284 phba->hbq_in_use = 1;
4285 phba->hbqs[0].entry_count = lpfc_hbq_defs[0]->entry_count;
4286 phba->hbq_count = 1;
4287 /* Initially populate or replenish the HBQs */
4288 lpfc_sli_hbqbuf_init_hbqs(phba, 0);
4289 return 0;
4290}
4291
4292/**
James Smart3621a712009-04-06 18:47:14 -04004293 * lpfc_sli_config_port - Issue config port mailbox command
James Smarte59058c2008-08-24 21:49:00 -04004294 * @phba: Pointer to HBA context object.
4295 * @sli_mode: sli mode - 2/3
4296 *
4297 * This function is called by the sli intialization code path
4298 * to issue config_port mailbox command. This function restarts the
4299 * HBA firmware and issues a config_port mailbox command to configure
4300 * the SLI interface in the sli mode specified by sli_mode
4301 * variable. The caller is not required to hold any locks.
4302 * The function returns 0 if successful, else returns negative error
4303 * code.
4304 **/
James Smart93996272008-08-24 21:50:30 -04004305int
4306lpfc_sli_config_port(struct lpfc_hba *phba, int sli_mode)
dea31012005-04-17 16:05:31 -05004307{
4308 LPFC_MBOXQ_t *pmb;
4309 uint32_t resetcount = 0, rc = 0, done = 0;
4310
4311 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4312 if (!pmb) {
James Smart2e0fef82007-06-17 19:56:36 -05004313 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05004314 return -ENOMEM;
4315 }
4316
James Smarted957682007-06-17 19:56:37 -05004317 phba->sli_rev = sli_mode;
dea31012005-04-17 16:05:31 -05004318 while (resetcount < 2 && !done) {
James Smart2e0fef82007-06-17 19:56:36 -05004319 spin_lock_irq(&phba->hbalock);
James Smart1c067a42006-08-01 07:33:52 -04004320 phba->sli.sli_flag |= LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05004321 spin_unlock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -05004322 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004323 lpfc_sli_brdrestart(phba);
dea31012005-04-17 16:05:31 -05004324 rc = lpfc_sli_chipset_init(phba);
4325 if (rc)
4326 break;
4327
James Smart2e0fef82007-06-17 19:56:36 -05004328 spin_lock_irq(&phba->hbalock);
James Smart1c067a42006-08-01 07:33:52 -04004329 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05004330 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004331 resetcount++;
4332
James Smarted957682007-06-17 19:56:37 -05004333 /* Call pre CONFIG_PORT mailbox command initialization. A
4334 * value of 0 means the call was successful. Any other
4335 * nonzero value is a failure, but if ERESTART is returned,
4336 * the driver may reset the HBA and try again.
4337 */
dea31012005-04-17 16:05:31 -05004338 rc = lpfc_config_port_prep(phba);
4339 if (rc == -ERESTART) {
James Smarted957682007-06-17 19:56:37 -05004340 phba->link_state = LPFC_LINK_UNKNOWN;
dea31012005-04-17 16:05:31 -05004341 continue;
James Smart34b02dc2008-08-24 21:49:55 -04004342 } else if (rc)
dea31012005-04-17 16:05:31 -05004343 break;
James Smart6d368e52011-05-24 11:44:12 -04004344
James Smart2e0fef82007-06-17 19:56:36 -05004345 phba->link_state = LPFC_INIT_MBX_CMDS;
dea31012005-04-17 16:05:31 -05004346 lpfc_config_port(phba, pmb);
4347 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
James Smart34b02dc2008-08-24 21:49:55 -04004348 phba->sli3_options &= ~(LPFC_SLI3_NPIV_ENABLED |
4349 LPFC_SLI3_HBQ_ENABLED |
4350 LPFC_SLI3_CRP_ENABLED |
James Smartbc739052010-08-04 16:11:18 -04004351 LPFC_SLI3_BG_ENABLED |
4352 LPFC_SLI3_DSS_ENABLED);
James Smarted957682007-06-17 19:56:37 -05004353 if (rc != MBX_SUCCESS) {
dea31012005-04-17 16:05:31 -05004354 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004355 "0442 Adapter failed to init, mbxCmd x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05004356 "CONFIG_PORT, mbxStatus x%x Data: x%x\n",
James Smart04c68492009-05-22 14:52:52 -04004357 pmb->u.mb.mbxCommand, pmb->u.mb.mbxStatus, 0);
James Smart2e0fef82007-06-17 19:56:36 -05004358 spin_lock_irq(&phba->hbalock);
James Smart04c68492009-05-22 14:52:52 -04004359 phba->sli.sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05004360 spin_unlock_irq(&phba->hbalock);
4361 rc = -ENXIO;
James Smart04c68492009-05-22 14:52:52 -04004362 } else {
4363 /* Allow asynchronous mailbox command to go through */
4364 spin_lock_irq(&phba->hbalock);
4365 phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
4366 spin_unlock_irq(&phba->hbalock);
James Smarted957682007-06-17 19:56:37 -05004367 done = 1;
James Smartcb69f7d2011-12-13 13:21:57 -05004368
4369 if ((pmb->u.mb.un.varCfgPort.casabt == 1) &&
4370 (pmb->u.mb.un.varCfgPort.gasabt == 0))
4371 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
4372 "3110 Port did not grant ASABT\n");
James Smart04c68492009-05-22 14:52:52 -04004373 }
dea31012005-04-17 16:05:31 -05004374 }
James Smarted957682007-06-17 19:56:37 -05004375 if (!done) {
4376 rc = -EINVAL;
4377 goto do_prep_failed;
4378 }
James Smart04c68492009-05-22 14:52:52 -04004379 if (pmb->u.mb.un.varCfgPort.sli_mode == 3) {
4380 if (!pmb->u.mb.un.varCfgPort.cMA) {
James Smart34b02dc2008-08-24 21:49:55 -04004381 rc = -ENXIO;
4382 goto do_prep_failed;
4383 }
James Smart04c68492009-05-22 14:52:52 -04004384 if (phba->max_vpi && pmb->u.mb.un.varCfgPort.gmv) {
James Smart34b02dc2008-08-24 21:49:55 -04004385 phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
James Smart04c68492009-05-22 14:52:52 -04004386 phba->max_vpi = pmb->u.mb.un.varCfgPort.max_vpi;
4387 phba->max_vports = (phba->max_vpi > phba->max_vports) ?
4388 phba->max_vpi : phba->max_vports;
4389
James Smart34b02dc2008-08-24 21:49:55 -04004390 } else
4391 phba->max_vpi = 0;
James Smartbc739052010-08-04 16:11:18 -04004392 phba->fips_level = 0;
4393 phba->fips_spec_rev = 0;
4394 if (pmb->u.mb.un.varCfgPort.gdss) {
James Smart04c68492009-05-22 14:52:52 -04004395 phba->sli3_options |= LPFC_SLI3_DSS_ENABLED;
James Smartbc739052010-08-04 16:11:18 -04004396 phba->fips_level = pmb->u.mb.un.varCfgPort.fips_level;
4397 phba->fips_spec_rev = pmb->u.mb.un.varCfgPort.fips_rev;
4398 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4399 "2850 Security Crypto Active. FIPS x%d "
4400 "(Spec Rev: x%d)",
4401 phba->fips_level, phba->fips_spec_rev);
4402 }
4403 if (pmb->u.mb.un.varCfgPort.sec_err) {
4404 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4405 "2856 Config Port Security Crypto "
4406 "Error: x%x ",
4407 pmb->u.mb.un.varCfgPort.sec_err);
4408 }
James Smart04c68492009-05-22 14:52:52 -04004409 if (pmb->u.mb.un.varCfgPort.gerbm)
James Smart34b02dc2008-08-24 21:49:55 -04004410 phba->sli3_options |= LPFC_SLI3_HBQ_ENABLED;
James Smart04c68492009-05-22 14:52:52 -04004411 if (pmb->u.mb.un.varCfgPort.gcrp)
James Smart34b02dc2008-08-24 21:49:55 -04004412 phba->sli3_options |= LPFC_SLI3_CRP_ENABLED;
James Smart6e7288d2010-06-07 15:23:35 -04004413
4414 phba->hbq_get = phba->mbox->us.s3_pgp.hbq_get;
4415 phba->port_gp = phba->mbox->us.s3_pgp.port;
James Smarte2a0a9d2008-12-04 22:40:02 -05004416
4417 if (phba->cfg_enable_bg) {
James Smart04c68492009-05-22 14:52:52 -04004418 if (pmb->u.mb.un.varCfgPort.gbg)
James Smarte2a0a9d2008-12-04 22:40:02 -05004419 phba->sli3_options |= LPFC_SLI3_BG_ENABLED;
4420 else
4421 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4422 "0443 Adapter did not grant "
4423 "BlockGuard\n");
4424 }
James Smart34b02dc2008-08-24 21:49:55 -04004425 } else {
James Smart8f34f4c2008-12-04 22:39:23 -05004426 phba->hbq_get = NULL;
James Smart34b02dc2008-08-24 21:49:55 -04004427 phba->port_gp = phba->mbox->us.s2.port;
James Smartd7c255b2008-08-24 21:50:00 -04004428 phba->max_vpi = 0;
James Smarted957682007-06-17 19:56:37 -05004429 }
James Smart92d7f7b2007-06-17 19:56:38 -05004430do_prep_failed:
James Smarted957682007-06-17 19:56:37 -05004431 mempool_free(pmb, phba->mbox_mem_pool);
4432 return rc;
4433}
4434
James Smarte59058c2008-08-24 21:49:00 -04004435
4436/**
James Smart3621a712009-04-06 18:47:14 -04004437 * lpfc_sli_hba_setup - SLI intialization function
James Smarte59058c2008-08-24 21:49:00 -04004438 * @phba: Pointer to HBA context object.
4439 *
4440 * This function is the main SLI intialization function. This function
4441 * is called by the HBA intialization code, HBA reset code and HBA
4442 * error attention handler code. Caller is not required to hold any
4443 * locks. This function issues config_port mailbox command to configure
4444 * the SLI, setup iocb rings and HBQ rings. In the end the function
4445 * calls the config_port_post function to issue init_link mailbox
4446 * command and to start the discovery. The function will return zero
4447 * if successful, else it will return negative error code.
4448 **/
James Smarted957682007-06-17 19:56:37 -05004449int
4450lpfc_sli_hba_setup(struct lpfc_hba *phba)
4451{
4452 uint32_t rc;
James Smart6d368e52011-05-24 11:44:12 -04004453 int mode = 3, i;
4454 int longs;
James Smarted957682007-06-17 19:56:37 -05004455
4456 switch (lpfc_sli_mode) {
4457 case 2:
James Smart78b2d852007-08-02 11:10:21 -04004458 if (phba->cfg_enable_npiv) {
James Smart92d7f7b2007-06-17 19:56:38 -05004459 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04004460 "1824 NPIV enabled: Override lpfc_sli_mode "
James Smart92d7f7b2007-06-17 19:56:38 -05004461 "parameter (%d) to auto (0).\n",
James Smarte8b62012007-08-02 11:10:09 -04004462 lpfc_sli_mode);
James Smart92d7f7b2007-06-17 19:56:38 -05004463 break;
4464 }
James Smarted957682007-06-17 19:56:37 -05004465 mode = 2;
4466 break;
4467 case 0:
4468 case 3:
4469 break;
4470 default:
James Smart92d7f7b2007-06-17 19:56:38 -05004471 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04004472 "1819 Unrecognized lpfc_sli_mode "
4473 "parameter: %d.\n", lpfc_sli_mode);
James Smarted957682007-06-17 19:56:37 -05004474
4475 break;
4476 }
4477
James Smart93996272008-08-24 21:50:30 -04004478 rc = lpfc_sli_config_port(phba, mode);
4479
James Smarted957682007-06-17 19:56:37 -05004480 if (rc && lpfc_sli_mode == 3)
James Smart92d7f7b2007-06-17 19:56:38 -05004481 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04004482 "1820 Unable to select SLI-3. "
4483 "Not supported by adapter.\n");
James Smarted957682007-06-17 19:56:37 -05004484 if (rc && mode != 2)
James Smart93996272008-08-24 21:50:30 -04004485 rc = lpfc_sli_config_port(phba, 2);
James Smarted957682007-06-17 19:56:37 -05004486 if (rc)
dea31012005-04-17 16:05:31 -05004487 goto lpfc_sli_hba_setup_error;
4488
James Smart0d878412009-10-02 15:16:56 -04004489 /* Enable PCIe device Advanced Error Reporting (AER) if configured */
4490 if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
4491 rc = pci_enable_pcie_error_reporting(phba->pcidev);
4492 if (!rc) {
4493 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4494 "2709 This device supports "
4495 "Advanced Error Reporting (AER)\n");
4496 spin_lock_irq(&phba->hbalock);
4497 phba->hba_flag |= HBA_AER_ENABLED;
4498 spin_unlock_irq(&phba->hbalock);
4499 } else {
4500 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4501 "2708 This device does not support "
4502 "Advanced Error Reporting (AER)\n");
4503 phba->cfg_aer_support = 0;
4504 }
4505 }
4506
James Smarted957682007-06-17 19:56:37 -05004507 if (phba->sli_rev == 3) {
4508 phba->iocb_cmd_size = SLI3_IOCB_CMD_SIZE;
4509 phba->iocb_rsp_size = SLI3_IOCB_RSP_SIZE;
James Smarted957682007-06-17 19:56:37 -05004510 } else {
4511 phba->iocb_cmd_size = SLI2_IOCB_CMD_SIZE;
4512 phba->iocb_rsp_size = SLI2_IOCB_RSP_SIZE;
James Smart92d7f7b2007-06-17 19:56:38 -05004513 phba->sli3_options = 0;
James Smarted957682007-06-17 19:56:37 -05004514 }
4515
4516 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004517 "0444 Firmware in SLI %x mode. Max_vpi %d\n",
4518 phba->sli_rev, phba->max_vpi);
James Smarted957682007-06-17 19:56:37 -05004519 rc = lpfc_sli_ring_map(phba);
dea31012005-04-17 16:05:31 -05004520
4521 if (rc)
4522 goto lpfc_sli_hba_setup_error;
4523
James Smart6d368e52011-05-24 11:44:12 -04004524 /* Initialize VPIs. */
4525 if (phba->sli_rev == LPFC_SLI_REV3) {
4526 /*
4527 * The VPI bitmask and physical ID array are allocated
4528 * and initialized once only - at driver load. A port
4529 * reset doesn't need to reinitialize this memory.
4530 */
4531 if ((phba->vpi_bmask == NULL) && (phba->vpi_ids == NULL)) {
4532 longs = (phba->max_vpi + BITS_PER_LONG) / BITS_PER_LONG;
4533 phba->vpi_bmask = kzalloc(longs * sizeof(unsigned long),
4534 GFP_KERNEL);
4535 if (!phba->vpi_bmask) {
4536 rc = -ENOMEM;
4537 goto lpfc_sli_hba_setup_error;
4538 }
4539
4540 phba->vpi_ids = kzalloc(
4541 (phba->max_vpi+1) * sizeof(uint16_t),
4542 GFP_KERNEL);
4543 if (!phba->vpi_ids) {
4544 kfree(phba->vpi_bmask);
4545 rc = -ENOMEM;
4546 goto lpfc_sli_hba_setup_error;
4547 }
4548 for (i = 0; i < phba->max_vpi; i++)
4549 phba->vpi_ids[i] = i;
4550 }
4551 }
4552
James Smart93996272008-08-24 21:50:30 -04004553 /* Init HBQs */
James Smarted957682007-06-17 19:56:37 -05004554 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
4555 rc = lpfc_sli_hbq_setup(phba);
4556 if (rc)
4557 goto lpfc_sli_hba_setup_error;
4558 }
James Smart04c68492009-05-22 14:52:52 -04004559 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004560 phba->sli.sli_flag |= LPFC_PROCESS_LA;
James Smart04c68492009-05-22 14:52:52 -04004561 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004562
4563 rc = lpfc_config_port_post(phba);
4564 if (rc)
4565 goto lpfc_sli_hba_setup_error;
4566
James Smarted957682007-06-17 19:56:37 -05004567 return rc;
4568
James Smart92d7f7b2007-06-17 19:56:38 -05004569lpfc_sli_hba_setup_error:
James Smart2e0fef82007-06-17 19:56:36 -05004570 phba->link_state = LPFC_HBA_ERROR;
James Smarte40a02c2010-02-26 14:13:54 -05004571 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004572 "0445 Firmware initialization failed\n");
dea31012005-04-17 16:05:31 -05004573 return rc;
4574}
4575
James Smartda0436e2009-05-22 14:51:39 -04004576/**
4577 * lpfc_sli4_read_fcoe_params - Read fcoe params from conf region
4578 * @phba: Pointer to HBA context object.
4579 * @mboxq: mailbox pointer.
4580 * This function issue a dump mailbox command to read config region
4581 * 23 and parse the records in the region and populate driver
4582 * data structure.
4583 **/
4584static int
James Smartff78d8f2011-12-13 13:21:35 -05004585lpfc_sli4_read_fcoe_params(struct lpfc_hba *phba)
James Smartda0436e2009-05-22 14:51:39 -04004586{
James Smartff78d8f2011-12-13 13:21:35 -05004587 LPFC_MBOXQ_t *mboxq;
James Smartda0436e2009-05-22 14:51:39 -04004588 struct lpfc_dmabuf *mp;
4589 struct lpfc_mqe *mqe;
4590 uint32_t data_length;
4591 int rc;
4592
4593 /* Program the default value of vlan_id and fc_map */
4594 phba->valid_vlan = 0;
4595 phba->fc_map[0] = LPFC_FCOE_FCF_MAP0;
4596 phba->fc_map[1] = LPFC_FCOE_FCF_MAP1;
4597 phba->fc_map[2] = LPFC_FCOE_FCF_MAP2;
4598
James Smartff78d8f2011-12-13 13:21:35 -05004599 mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4600 if (!mboxq)
James Smartda0436e2009-05-22 14:51:39 -04004601 return -ENOMEM;
4602
James Smartff78d8f2011-12-13 13:21:35 -05004603 mqe = &mboxq->u.mqe;
4604 if (lpfc_sli4_dump_cfg_rg23(phba, mboxq)) {
4605 rc = -ENOMEM;
4606 goto out_free_mboxq;
4607 }
4608
James Smartda0436e2009-05-22 14:51:39 -04004609 mp = (struct lpfc_dmabuf *) mboxq->context1;
4610 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4611
4612 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
4613 "(%d):2571 Mailbox cmd x%x Status x%x "
4614 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x "
4615 "x%x x%x x%x x%x x%x x%x x%x x%x x%x "
4616 "CQ: x%x x%x x%x x%x\n",
4617 mboxq->vport ? mboxq->vport->vpi : 0,
4618 bf_get(lpfc_mqe_command, mqe),
4619 bf_get(lpfc_mqe_status, mqe),
4620 mqe->un.mb_words[0], mqe->un.mb_words[1],
4621 mqe->un.mb_words[2], mqe->un.mb_words[3],
4622 mqe->un.mb_words[4], mqe->un.mb_words[5],
4623 mqe->un.mb_words[6], mqe->un.mb_words[7],
4624 mqe->un.mb_words[8], mqe->un.mb_words[9],
4625 mqe->un.mb_words[10], mqe->un.mb_words[11],
4626 mqe->un.mb_words[12], mqe->un.mb_words[13],
4627 mqe->un.mb_words[14], mqe->un.mb_words[15],
4628 mqe->un.mb_words[16], mqe->un.mb_words[50],
4629 mboxq->mcqe.word0,
4630 mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
4631 mboxq->mcqe.trailer);
4632
4633 if (rc) {
4634 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4635 kfree(mp);
James Smartff78d8f2011-12-13 13:21:35 -05004636 rc = -EIO;
4637 goto out_free_mboxq;
James Smartda0436e2009-05-22 14:51:39 -04004638 }
4639 data_length = mqe->un.mb_words[5];
James Smarta0c87cb2009-07-19 10:01:10 -04004640 if (data_length > DMP_RGN23_SIZE) {
James Smartd11e31d2009-06-10 17:23:06 -04004641 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4642 kfree(mp);
James Smartff78d8f2011-12-13 13:21:35 -05004643 rc = -EIO;
4644 goto out_free_mboxq;
James Smartd11e31d2009-06-10 17:23:06 -04004645 }
James Smartda0436e2009-05-22 14:51:39 -04004646
4647 lpfc_parse_fcoe_conf(phba, mp->virt, data_length);
4648 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4649 kfree(mp);
James Smartff78d8f2011-12-13 13:21:35 -05004650 rc = 0;
4651
4652out_free_mboxq:
4653 mempool_free(mboxq, phba->mbox_mem_pool);
4654 return rc;
James Smartda0436e2009-05-22 14:51:39 -04004655}
4656
4657/**
4658 * lpfc_sli4_read_rev - Issue READ_REV and collect vpd data
4659 * @phba: pointer to lpfc hba data structure.
4660 * @mboxq: pointer to the LPFC_MBOXQ_t structure.
4661 * @vpd: pointer to the memory to hold resulting port vpd data.
4662 * @vpd_size: On input, the number of bytes allocated to @vpd.
4663 * On output, the number of data bytes in @vpd.
4664 *
4665 * This routine executes a READ_REV SLI4 mailbox command. In
4666 * addition, this routine gets the port vpd data.
4667 *
4668 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02004669 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -04004670 * -ENOMEM - could not allocated memory.
James Smartda0436e2009-05-22 14:51:39 -04004671 **/
4672static int
4673lpfc_sli4_read_rev(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
4674 uint8_t *vpd, uint32_t *vpd_size)
4675{
4676 int rc = 0;
4677 uint32_t dma_size;
4678 struct lpfc_dmabuf *dmabuf;
4679 struct lpfc_mqe *mqe;
4680
4681 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
4682 if (!dmabuf)
4683 return -ENOMEM;
4684
4685 /*
4686 * Get a DMA buffer for the vpd data resulting from the READ_REV
4687 * mailbox command.
4688 */
4689 dma_size = *vpd_size;
4690 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
4691 dma_size,
4692 &dmabuf->phys,
4693 GFP_KERNEL);
4694 if (!dmabuf->virt) {
4695 kfree(dmabuf);
4696 return -ENOMEM;
4697 }
4698 memset(dmabuf->virt, 0, dma_size);
4699
4700 /*
4701 * The SLI4 implementation of READ_REV conflicts at word1,
4702 * bits 31:16 and SLI4 adds vpd functionality not present
4703 * in SLI3. This code corrects the conflicts.
4704 */
4705 lpfc_read_rev(phba, mboxq);
4706 mqe = &mboxq->u.mqe;
4707 mqe->un.read_rev.vpd_paddr_high = putPaddrHigh(dmabuf->phys);
4708 mqe->un.read_rev.vpd_paddr_low = putPaddrLow(dmabuf->phys);
4709 mqe->un.read_rev.word1 &= 0x0000FFFF;
4710 bf_set(lpfc_mbx_rd_rev_vpd, &mqe->un.read_rev, 1);
4711 bf_set(lpfc_mbx_rd_rev_avail_len, &mqe->un.read_rev, dma_size);
4712
4713 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4714 if (rc) {
4715 dma_free_coherent(&phba->pcidev->dev, dma_size,
4716 dmabuf->virt, dmabuf->phys);
James Smartdef9c7a2009-12-21 17:02:28 -05004717 kfree(dmabuf);
James Smartda0436e2009-05-22 14:51:39 -04004718 return -EIO;
4719 }
4720
James Smartda0436e2009-05-22 14:51:39 -04004721 /*
4722 * The available vpd length cannot be bigger than the
4723 * DMA buffer passed to the port. Catch the less than
4724 * case and update the caller's size.
4725 */
4726 if (mqe->un.read_rev.avail_vpd_len < *vpd_size)
4727 *vpd_size = mqe->un.read_rev.avail_vpd_len;
4728
James Smartd7c47992010-06-08 18:31:54 -04004729 memcpy(vpd, dmabuf->virt, *vpd_size);
4730
James Smartda0436e2009-05-22 14:51:39 -04004731 dma_free_coherent(&phba->pcidev->dev, dma_size,
4732 dmabuf->virt, dmabuf->phys);
4733 kfree(dmabuf);
4734 return 0;
4735}
4736
4737/**
James Smartcd1c8302011-10-10 21:33:25 -04004738 * lpfc_sli4_retrieve_pport_name - Retrieve SLI4 device physical port name
4739 * @phba: pointer to lpfc hba data structure.
4740 *
4741 * This routine retrieves SLI4 device physical port name this PCI function
4742 * is attached to.
4743 *
4744 * Return codes
4745 * 0 - sucessful
4746 * otherwise - failed to retrieve physical port name
4747 **/
4748static int
4749lpfc_sli4_retrieve_pport_name(struct lpfc_hba *phba)
4750{
4751 LPFC_MBOXQ_t *mboxq;
James Smartcd1c8302011-10-10 21:33:25 -04004752 struct lpfc_mbx_get_cntl_attributes *mbx_cntl_attr;
4753 struct lpfc_controller_attribute *cntl_attr;
4754 struct lpfc_mbx_get_port_name *get_port_name;
4755 void *virtaddr = NULL;
4756 uint32_t alloclen, reqlen;
4757 uint32_t shdr_status, shdr_add_status;
4758 union lpfc_sli4_cfg_shdr *shdr;
4759 char cport_name = 0;
4760 int rc;
4761
4762 /* We assume nothing at this point */
4763 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_INVAL;
4764 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_NON;
4765
4766 mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4767 if (!mboxq)
4768 return -ENOMEM;
James Smartcd1c8302011-10-10 21:33:25 -04004769 /* obtain link type and link number via READ_CONFIG */
James Smartff78d8f2011-12-13 13:21:35 -05004770 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_INVAL;
4771 lpfc_sli4_read_config(phba);
4772 if (phba->sli4_hba.lnk_info.lnk_dv == LPFC_LNK_DAT_VAL)
4773 goto retrieve_ppname;
James Smartcd1c8302011-10-10 21:33:25 -04004774
4775 /* obtain link type and link number via COMMON_GET_CNTL_ATTRIBUTES */
4776 reqlen = sizeof(struct lpfc_mbx_get_cntl_attributes);
4777 alloclen = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
4778 LPFC_MBOX_OPCODE_GET_CNTL_ATTRIBUTES, reqlen,
4779 LPFC_SLI4_MBX_NEMBED);
4780 if (alloclen < reqlen) {
4781 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4782 "3084 Allocated DMA memory size (%d) is "
4783 "less than the requested DMA memory size "
4784 "(%d)\n", alloclen, reqlen);
4785 rc = -ENOMEM;
4786 goto out_free_mboxq;
4787 }
4788 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4789 virtaddr = mboxq->sge_array->addr[0];
4790 mbx_cntl_attr = (struct lpfc_mbx_get_cntl_attributes *)virtaddr;
4791 shdr = &mbx_cntl_attr->cfg_shdr;
4792 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
4793 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
4794 if (shdr_status || shdr_add_status || rc) {
4795 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
4796 "3085 Mailbox x%x (x%x/x%x) failed, "
4797 "rc:x%x, status:x%x, add_status:x%x\n",
4798 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
4799 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
4800 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
4801 rc, shdr_status, shdr_add_status);
4802 rc = -ENXIO;
4803 goto out_free_mboxq;
4804 }
4805 cntl_attr = &mbx_cntl_attr->cntl_attr;
4806 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_VAL;
4807 phba->sli4_hba.lnk_info.lnk_tp =
4808 bf_get(lpfc_cntl_attr_lnk_type, cntl_attr);
4809 phba->sli4_hba.lnk_info.lnk_no =
4810 bf_get(lpfc_cntl_attr_lnk_numb, cntl_attr);
4811 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4812 "3086 lnk_type:%d, lnk_numb:%d\n",
4813 phba->sli4_hba.lnk_info.lnk_tp,
4814 phba->sli4_hba.lnk_info.lnk_no);
4815
4816retrieve_ppname:
4817 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
4818 LPFC_MBOX_OPCODE_GET_PORT_NAME,
4819 sizeof(struct lpfc_mbx_get_port_name) -
4820 sizeof(struct lpfc_sli4_cfg_mhdr),
4821 LPFC_SLI4_MBX_EMBED);
4822 get_port_name = &mboxq->u.mqe.un.get_port_name;
4823 shdr = (union lpfc_sli4_cfg_shdr *)&get_port_name->header.cfg_shdr;
4824 bf_set(lpfc_mbox_hdr_version, &shdr->request, LPFC_OPCODE_VERSION_1);
4825 bf_set(lpfc_mbx_get_port_name_lnk_type, &get_port_name->u.request,
4826 phba->sli4_hba.lnk_info.lnk_tp);
4827 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4828 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
4829 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
4830 if (shdr_status || shdr_add_status || rc) {
4831 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
4832 "3087 Mailbox x%x (x%x/x%x) failed: "
4833 "rc:x%x, status:x%x, add_status:x%x\n",
4834 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
4835 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
4836 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
4837 rc, shdr_status, shdr_add_status);
4838 rc = -ENXIO;
4839 goto out_free_mboxq;
4840 }
4841 switch (phba->sli4_hba.lnk_info.lnk_no) {
4842 case LPFC_LINK_NUMBER_0:
4843 cport_name = bf_get(lpfc_mbx_get_port_name_name0,
4844 &get_port_name->u.response);
4845 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
4846 break;
4847 case LPFC_LINK_NUMBER_1:
4848 cport_name = bf_get(lpfc_mbx_get_port_name_name1,
4849 &get_port_name->u.response);
4850 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
4851 break;
4852 case LPFC_LINK_NUMBER_2:
4853 cport_name = bf_get(lpfc_mbx_get_port_name_name2,
4854 &get_port_name->u.response);
4855 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
4856 break;
4857 case LPFC_LINK_NUMBER_3:
4858 cport_name = bf_get(lpfc_mbx_get_port_name_name3,
4859 &get_port_name->u.response);
4860 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
4861 break;
4862 default:
4863 break;
4864 }
4865
4866 if (phba->sli4_hba.pport_name_sta == LPFC_SLI4_PPNAME_GET) {
4867 phba->Port[0] = cport_name;
4868 phba->Port[1] = '\0';
4869 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4870 "3091 SLI get port name: %s\n", phba->Port);
4871 }
4872
4873out_free_mboxq:
4874 if (rc != MBX_TIMEOUT) {
4875 if (bf_get(lpfc_mqe_command, &mboxq->u.mqe) == MBX_SLI4_CONFIG)
4876 lpfc_sli4_mbox_cmd_free(phba, mboxq);
4877 else
4878 mempool_free(mboxq, phba->mbox_mem_pool);
4879 }
4880 return rc;
4881}
4882
4883/**
James Smartda0436e2009-05-22 14:51:39 -04004884 * lpfc_sli4_arm_cqeq_intr - Arm sli-4 device completion and event queues
4885 * @phba: pointer to lpfc hba data structure.
4886 *
4887 * This routine is called to explicitly arm the SLI4 device's completion and
4888 * event queues
4889 **/
4890static void
4891lpfc_sli4_arm_cqeq_intr(struct lpfc_hba *phba)
4892{
4893 uint8_t fcp_eqidx;
4894
4895 lpfc_sli4_cq_release(phba->sli4_hba.mbx_cq, LPFC_QUEUE_REARM);
4896 lpfc_sli4_cq_release(phba->sli4_hba.els_cq, LPFC_QUEUE_REARM);
James Smart05580562011-05-24 11:40:48 -04004897 fcp_eqidx = 0;
James Smart2e90f4b2011-12-13 13:22:37 -05004898 if (phba->sli4_hba.fcp_cq) {
4899 do
4900 lpfc_sli4_cq_release(phba->sli4_hba.fcp_cq[fcp_eqidx],
4901 LPFC_QUEUE_REARM);
4902 while (++fcp_eqidx < phba->cfg_fcp_eq_count);
4903 }
James Smartda0436e2009-05-22 14:51:39 -04004904 lpfc_sli4_eq_release(phba->sli4_hba.sp_eq, LPFC_QUEUE_REARM);
James Smart2e90f4b2011-12-13 13:22:37 -05004905 if (phba->sli4_hba.fp_eq) {
4906 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count;
4907 fcp_eqidx++)
4908 lpfc_sli4_eq_release(phba->sli4_hba.fp_eq[fcp_eqidx],
4909 LPFC_QUEUE_REARM);
4910 }
James Smartda0436e2009-05-22 14:51:39 -04004911}
4912
4913/**
James Smart6d368e52011-05-24 11:44:12 -04004914 * lpfc_sli4_get_avail_extnt_rsrc - Get available resource extent count.
4915 * @phba: Pointer to HBA context object.
4916 * @type: The resource extent type.
James Smartb76f2dc2011-07-22 18:37:42 -04004917 * @extnt_count: buffer to hold port available extent count.
4918 * @extnt_size: buffer to hold element count per extent.
James Smart6d368e52011-05-24 11:44:12 -04004919 *
James Smartb76f2dc2011-07-22 18:37:42 -04004920 * This function calls the port and retrievs the number of available
4921 * extents and their size for a particular extent type.
4922 *
4923 * Returns: 0 if successful. Nonzero otherwise.
James Smart6d368e52011-05-24 11:44:12 -04004924 **/
James Smartb76f2dc2011-07-22 18:37:42 -04004925int
James Smart6d368e52011-05-24 11:44:12 -04004926lpfc_sli4_get_avail_extnt_rsrc(struct lpfc_hba *phba, uint16_t type,
4927 uint16_t *extnt_count, uint16_t *extnt_size)
4928{
4929 int rc = 0;
4930 uint32_t length;
4931 uint32_t mbox_tmo;
4932 struct lpfc_mbx_get_rsrc_extent_info *rsrc_info;
4933 LPFC_MBOXQ_t *mbox;
4934
4935 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4936 if (!mbox)
4937 return -ENOMEM;
4938
4939 /* Find out how many extents are available for this resource type */
4940 length = (sizeof(struct lpfc_mbx_get_rsrc_extent_info) -
4941 sizeof(struct lpfc_sli4_cfg_mhdr));
4942 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
4943 LPFC_MBOX_OPCODE_GET_RSRC_EXTENT_INFO,
4944 length, LPFC_SLI4_MBX_EMBED);
4945
4946 /* Send an extents count of 0 - the GET doesn't use it. */
4947 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, 0, type,
4948 LPFC_SLI4_MBX_EMBED);
4949 if (unlikely(rc)) {
4950 rc = -EIO;
4951 goto err_exit;
4952 }
4953
4954 if (!phba->sli4_hba.intr_enable)
4955 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
4956 else {
James Smarta183a152011-10-10 21:32:43 -04004957 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart6d368e52011-05-24 11:44:12 -04004958 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
4959 }
4960 if (unlikely(rc)) {
4961 rc = -EIO;
4962 goto err_exit;
4963 }
4964
4965 rsrc_info = &mbox->u.mqe.un.rsrc_extent_info;
4966 if (bf_get(lpfc_mbox_hdr_status,
4967 &rsrc_info->header.cfg_shdr.response)) {
4968 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
4969 "2930 Failed to get resource extents "
4970 "Status 0x%x Add'l Status 0x%x\n",
4971 bf_get(lpfc_mbox_hdr_status,
4972 &rsrc_info->header.cfg_shdr.response),
4973 bf_get(lpfc_mbox_hdr_add_status,
4974 &rsrc_info->header.cfg_shdr.response));
4975 rc = -EIO;
4976 goto err_exit;
4977 }
4978
4979 *extnt_count = bf_get(lpfc_mbx_get_rsrc_extent_info_cnt,
4980 &rsrc_info->u.rsp);
4981 *extnt_size = bf_get(lpfc_mbx_get_rsrc_extent_info_size,
4982 &rsrc_info->u.rsp);
James Smart8a9d2e82012-05-09 21:16:12 -04004983
4984 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4985 "3162 Retrieved extents type-%d from port: count:%d, "
4986 "size:%d\n", type, *extnt_count, *extnt_size);
4987
4988err_exit:
James Smart6d368e52011-05-24 11:44:12 -04004989 mempool_free(mbox, phba->mbox_mem_pool);
4990 return rc;
4991}
4992
4993/**
4994 * lpfc_sli4_chk_avail_extnt_rsrc - Check for available SLI4 resource extents.
4995 * @phba: Pointer to HBA context object.
4996 * @type: The extent type to check.
4997 *
4998 * This function reads the current available extents from the port and checks
4999 * if the extent count or extent size has changed since the last access.
5000 * Callers use this routine post port reset to understand if there is a
5001 * extent reprovisioning requirement.
5002 *
5003 * Returns:
5004 * -Error: error indicates problem.
5005 * 1: Extent count or size has changed.
5006 * 0: No changes.
5007 **/
5008static int
5009lpfc_sli4_chk_avail_extnt_rsrc(struct lpfc_hba *phba, uint16_t type)
5010{
5011 uint16_t curr_ext_cnt, rsrc_ext_cnt;
5012 uint16_t size_diff, rsrc_ext_size;
5013 int rc = 0;
5014 struct lpfc_rsrc_blks *rsrc_entry;
5015 struct list_head *rsrc_blk_list = NULL;
5016
5017 size_diff = 0;
5018 curr_ext_cnt = 0;
5019 rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,
5020 &rsrc_ext_cnt,
5021 &rsrc_ext_size);
5022 if (unlikely(rc))
5023 return -EIO;
5024
5025 switch (type) {
5026 case LPFC_RSC_TYPE_FCOE_RPI:
5027 rsrc_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;
5028 break;
5029 case LPFC_RSC_TYPE_FCOE_VPI:
5030 rsrc_blk_list = &phba->lpfc_vpi_blk_list;
5031 break;
5032 case LPFC_RSC_TYPE_FCOE_XRI:
5033 rsrc_blk_list = &phba->sli4_hba.lpfc_xri_blk_list;
5034 break;
5035 case LPFC_RSC_TYPE_FCOE_VFI:
5036 rsrc_blk_list = &phba->sli4_hba.lpfc_vfi_blk_list;
5037 break;
5038 default:
5039 break;
5040 }
5041
5042 list_for_each_entry(rsrc_entry, rsrc_blk_list, list) {
5043 curr_ext_cnt++;
5044 if (rsrc_entry->rsrc_size != rsrc_ext_size)
5045 size_diff++;
5046 }
5047
5048 if (curr_ext_cnt != rsrc_ext_cnt || size_diff != 0)
5049 rc = 1;
5050
5051 return rc;
5052}
5053
5054/**
5055 * lpfc_sli4_cfg_post_extnts -
5056 * @phba: Pointer to HBA context object.
5057 * @extnt_cnt - number of available extents.
5058 * @type - the extent type (rpi, xri, vfi, vpi).
5059 * @emb - buffer to hold either MBX_EMBED or MBX_NEMBED operation.
5060 * @mbox - pointer to the caller's allocated mailbox structure.
5061 *
5062 * This function executes the extents allocation request. It also
5063 * takes care of the amount of memory needed to allocate or get the
5064 * allocated extents. It is the caller's responsibility to evaluate
5065 * the response.
5066 *
5067 * Returns:
5068 * -Error: Error value describes the condition found.
5069 * 0: if successful
5070 **/
5071static int
James Smart8a9d2e82012-05-09 21:16:12 -04005072lpfc_sli4_cfg_post_extnts(struct lpfc_hba *phba, uint16_t extnt_cnt,
James Smart6d368e52011-05-24 11:44:12 -04005073 uint16_t type, bool *emb, LPFC_MBOXQ_t *mbox)
5074{
5075 int rc = 0;
5076 uint32_t req_len;
5077 uint32_t emb_len;
5078 uint32_t alloc_len, mbox_tmo;
5079
5080 /* Calculate the total requested length of the dma memory */
James Smart8a9d2e82012-05-09 21:16:12 -04005081 req_len = extnt_cnt * sizeof(uint16_t);
James Smart6d368e52011-05-24 11:44:12 -04005082
5083 /*
5084 * Calculate the size of an embedded mailbox. The uint32_t
5085 * accounts for extents-specific word.
5086 */
5087 emb_len = sizeof(MAILBOX_t) - sizeof(struct mbox_header) -
5088 sizeof(uint32_t);
5089
5090 /*
5091 * Presume the allocation and response will fit into an embedded
5092 * mailbox. If not true, reconfigure to a non-embedded mailbox.
5093 */
5094 *emb = LPFC_SLI4_MBX_EMBED;
5095 if (req_len > emb_len) {
James Smart8a9d2e82012-05-09 21:16:12 -04005096 req_len = extnt_cnt * sizeof(uint16_t) +
James Smart6d368e52011-05-24 11:44:12 -04005097 sizeof(union lpfc_sli4_cfg_shdr) +
5098 sizeof(uint32_t);
5099 *emb = LPFC_SLI4_MBX_NEMBED;
5100 }
5101
5102 alloc_len = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5103 LPFC_MBOX_OPCODE_ALLOC_RSRC_EXTENT,
5104 req_len, *emb);
5105 if (alloc_len < req_len) {
5106 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartb76f2dc2011-07-22 18:37:42 -04005107 "2982 Allocated DMA memory size (x%x) is "
James Smart6d368e52011-05-24 11:44:12 -04005108 "less than the requested DMA memory "
5109 "size (x%x)\n", alloc_len, req_len);
5110 return -ENOMEM;
5111 }
James Smart8a9d2e82012-05-09 21:16:12 -04005112 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, extnt_cnt, type, *emb);
James Smart6d368e52011-05-24 11:44:12 -04005113 if (unlikely(rc))
5114 return -EIO;
5115
5116 if (!phba->sli4_hba.intr_enable)
5117 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5118 else {
James Smarta183a152011-10-10 21:32:43 -04005119 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart6d368e52011-05-24 11:44:12 -04005120 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5121 }
5122
5123 if (unlikely(rc))
5124 rc = -EIO;
5125 return rc;
5126}
5127
5128/**
5129 * lpfc_sli4_alloc_extent - Allocate an SLI4 resource extent.
5130 * @phba: Pointer to HBA context object.
5131 * @type: The resource extent type to allocate.
5132 *
5133 * This function allocates the number of elements for the specified
5134 * resource type.
5135 **/
5136static int
5137lpfc_sli4_alloc_extent(struct lpfc_hba *phba, uint16_t type)
5138{
5139 bool emb = false;
5140 uint16_t rsrc_id_cnt, rsrc_cnt, rsrc_size;
5141 uint16_t rsrc_id, rsrc_start, j, k;
5142 uint16_t *ids;
5143 int i, rc;
5144 unsigned long longs;
5145 unsigned long *bmask;
5146 struct lpfc_rsrc_blks *rsrc_blks;
5147 LPFC_MBOXQ_t *mbox;
5148 uint32_t length;
5149 struct lpfc_id_range *id_array = NULL;
5150 void *virtaddr = NULL;
5151 struct lpfc_mbx_nembed_rsrc_extent *n_rsrc;
5152 struct lpfc_mbx_alloc_rsrc_extents *rsrc_ext;
5153 struct list_head *ext_blk_list;
5154
5155 rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,
5156 &rsrc_cnt,
5157 &rsrc_size);
5158 if (unlikely(rc))
5159 return -EIO;
5160
5161 if ((rsrc_cnt == 0) || (rsrc_size == 0)) {
5162 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
5163 "3009 No available Resource Extents "
5164 "for resource type 0x%x: Count: 0x%x, "
5165 "Size 0x%x\n", type, rsrc_cnt,
5166 rsrc_size);
5167 return -ENOMEM;
5168 }
5169
James Smart8a9d2e82012-05-09 21:16:12 -04005170 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_INIT | LOG_SLI,
5171 "2903 Post resource extents type-0x%x: "
5172 "count:%d, size %d\n", type, rsrc_cnt, rsrc_size);
James Smart6d368e52011-05-24 11:44:12 -04005173
5174 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5175 if (!mbox)
5176 return -ENOMEM;
5177
James Smart8a9d2e82012-05-09 21:16:12 -04005178 rc = lpfc_sli4_cfg_post_extnts(phba, rsrc_cnt, type, &emb, mbox);
James Smart6d368e52011-05-24 11:44:12 -04005179 if (unlikely(rc)) {
5180 rc = -EIO;
5181 goto err_exit;
5182 }
5183
5184 /*
5185 * Figure out where the response is located. Then get local pointers
5186 * to the response data. The port does not guarantee to respond to
5187 * all extents counts request so update the local variable with the
5188 * allocated count from the port.
5189 */
5190 if (emb == LPFC_SLI4_MBX_EMBED) {
5191 rsrc_ext = &mbox->u.mqe.un.alloc_rsrc_extents;
5192 id_array = &rsrc_ext->u.rsp.id[0];
5193 rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, &rsrc_ext->u.rsp);
5194 } else {
5195 virtaddr = mbox->sge_array->addr[0];
5196 n_rsrc = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
5197 rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, n_rsrc);
5198 id_array = &n_rsrc->id;
5199 }
5200
5201 longs = ((rsrc_cnt * rsrc_size) + BITS_PER_LONG - 1) / BITS_PER_LONG;
5202 rsrc_id_cnt = rsrc_cnt * rsrc_size;
5203
5204 /*
5205 * Based on the resource size and count, correct the base and max
5206 * resource values.
5207 */
5208 length = sizeof(struct lpfc_rsrc_blks);
5209 switch (type) {
5210 case LPFC_RSC_TYPE_FCOE_RPI:
5211 phba->sli4_hba.rpi_bmask = kzalloc(longs *
5212 sizeof(unsigned long),
5213 GFP_KERNEL);
5214 if (unlikely(!phba->sli4_hba.rpi_bmask)) {
5215 rc = -ENOMEM;
5216 goto err_exit;
5217 }
5218 phba->sli4_hba.rpi_ids = kzalloc(rsrc_id_cnt *
5219 sizeof(uint16_t),
5220 GFP_KERNEL);
5221 if (unlikely(!phba->sli4_hba.rpi_ids)) {
5222 kfree(phba->sli4_hba.rpi_bmask);
5223 rc = -ENOMEM;
5224 goto err_exit;
5225 }
5226
5227 /*
5228 * The next_rpi was initialized with the maximum available
5229 * count but the port may allocate a smaller number. Catch
5230 * that case and update the next_rpi.
5231 */
5232 phba->sli4_hba.next_rpi = rsrc_id_cnt;
5233
5234 /* Initialize local ptrs for common extent processing later. */
5235 bmask = phba->sli4_hba.rpi_bmask;
5236 ids = phba->sli4_hba.rpi_ids;
5237 ext_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;
5238 break;
5239 case LPFC_RSC_TYPE_FCOE_VPI:
5240 phba->vpi_bmask = kzalloc(longs *
5241 sizeof(unsigned long),
5242 GFP_KERNEL);
5243 if (unlikely(!phba->vpi_bmask)) {
5244 rc = -ENOMEM;
5245 goto err_exit;
5246 }
5247 phba->vpi_ids = kzalloc(rsrc_id_cnt *
5248 sizeof(uint16_t),
5249 GFP_KERNEL);
5250 if (unlikely(!phba->vpi_ids)) {
5251 kfree(phba->vpi_bmask);
5252 rc = -ENOMEM;
5253 goto err_exit;
5254 }
5255
5256 /* Initialize local ptrs for common extent processing later. */
5257 bmask = phba->vpi_bmask;
5258 ids = phba->vpi_ids;
5259 ext_blk_list = &phba->lpfc_vpi_blk_list;
5260 break;
5261 case LPFC_RSC_TYPE_FCOE_XRI:
5262 phba->sli4_hba.xri_bmask = kzalloc(longs *
5263 sizeof(unsigned long),
5264 GFP_KERNEL);
5265 if (unlikely(!phba->sli4_hba.xri_bmask)) {
5266 rc = -ENOMEM;
5267 goto err_exit;
5268 }
James Smart8a9d2e82012-05-09 21:16:12 -04005269 phba->sli4_hba.max_cfg_param.xri_used = 0;
James Smart6d368e52011-05-24 11:44:12 -04005270 phba->sli4_hba.xri_ids = kzalloc(rsrc_id_cnt *
5271 sizeof(uint16_t),
5272 GFP_KERNEL);
5273 if (unlikely(!phba->sli4_hba.xri_ids)) {
5274 kfree(phba->sli4_hba.xri_bmask);
5275 rc = -ENOMEM;
5276 goto err_exit;
5277 }
5278
5279 /* Initialize local ptrs for common extent processing later. */
5280 bmask = phba->sli4_hba.xri_bmask;
5281 ids = phba->sli4_hba.xri_ids;
5282 ext_blk_list = &phba->sli4_hba.lpfc_xri_blk_list;
5283 break;
5284 case LPFC_RSC_TYPE_FCOE_VFI:
5285 phba->sli4_hba.vfi_bmask = kzalloc(longs *
5286 sizeof(unsigned long),
5287 GFP_KERNEL);
5288 if (unlikely(!phba->sli4_hba.vfi_bmask)) {
5289 rc = -ENOMEM;
5290 goto err_exit;
5291 }
5292 phba->sli4_hba.vfi_ids = kzalloc(rsrc_id_cnt *
5293 sizeof(uint16_t),
5294 GFP_KERNEL);
5295 if (unlikely(!phba->sli4_hba.vfi_ids)) {
5296 kfree(phba->sli4_hba.vfi_bmask);
5297 rc = -ENOMEM;
5298 goto err_exit;
5299 }
5300
5301 /* Initialize local ptrs for common extent processing later. */
5302 bmask = phba->sli4_hba.vfi_bmask;
5303 ids = phba->sli4_hba.vfi_ids;
5304 ext_blk_list = &phba->sli4_hba.lpfc_vfi_blk_list;
5305 break;
5306 default:
5307 /* Unsupported Opcode. Fail call. */
5308 id_array = NULL;
5309 bmask = NULL;
5310 ids = NULL;
5311 ext_blk_list = NULL;
5312 goto err_exit;
5313 }
5314
5315 /*
5316 * Complete initializing the extent configuration with the
5317 * allocated ids assigned to this function. The bitmask serves
5318 * as an index into the array and manages the available ids. The
5319 * array just stores the ids communicated to the port via the wqes.
5320 */
5321 for (i = 0, j = 0, k = 0; i < rsrc_cnt; i++) {
5322 if ((i % 2) == 0)
5323 rsrc_id = bf_get(lpfc_mbx_rsrc_id_word4_0,
5324 &id_array[k]);
5325 else
5326 rsrc_id = bf_get(lpfc_mbx_rsrc_id_word4_1,
5327 &id_array[k]);
5328
5329 rsrc_blks = kzalloc(length, GFP_KERNEL);
5330 if (unlikely(!rsrc_blks)) {
5331 rc = -ENOMEM;
5332 kfree(bmask);
5333 kfree(ids);
5334 goto err_exit;
5335 }
5336 rsrc_blks->rsrc_start = rsrc_id;
5337 rsrc_blks->rsrc_size = rsrc_size;
5338 list_add_tail(&rsrc_blks->list, ext_blk_list);
5339 rsrc_start = rsrc_id;
5340 if ((type == LPFC_RSC_TYPE_FCOE_XRI) && (j == 0))
5341 phba->sli4_hba.scsi_xri_start = rsrc_start +
5342 lpfc_sli4_get_els_iocb_cnt(phba);
5343
5344 while (rsrc_id < (rsrc_start + rsrc_size)) {
5345 ids[j] = rsrc_id;
5346 rsrc_id++;
5347 j++;
5348 }
5349 /* Entire word processed. Get next word.*/
5350 if ((i % 2) == 1)
5351 k++;
5352 }
5353 err_exit:
5354 lpfc_sli4_mbox_cmd_free(phba, mbox);
5355 return rc;
5356}
5357
5358/**
5359 * lpfc_sli4_dealloc_extent - Deallocate an SLI4 resource extent.
5360 * @phba: Pointer to HBA context object.
5361 * @type: the extent's type.
5362 *
5363 * This function deallocates all extents of a particular resource type.
5364 * SLI4 does not allow for deallocating a particular extent range. It
5365 * is the caller's responsibility to release all kernel memory resources.
5366 **/
5367static int
5368lpfc_sli4_dealloc_extent(struct lpfc_hba *phba, uint16_t type)
5369{
5370 int rc;
5371 uint32_t length, mbox_tmo = 0;
5372 LPFC_MBOXQ_t *mbox;
5373 struct lpfc_mbx_dealloc_rsrc_extents *dealloc_rsrc;
5374 struct lpfc_rsrc_blks *rsrc_blk, *rsrc_blk_next;
5375
5376 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5377 if (!mbox)
5378 return -ENOMEM;
5379
5380 /*
5381 * This function sends an embedded mailbox because it only sends the
5382 * the resource type. All extents of this type are released by the
5383 * port.
5384 */
5385 length = (sizeof(struct lpfc_mbx_dealloc_rsrc_extents) -
5386 sizeof(struct lpfc_sli4_cfg_mhdr));
5387 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5388 LPFC_MBOX_OPCODE_DEALLOC_RSRC_EXTENT,
5389 length, LPFC_SLI4_MBX_EMBED);
5390
5391 /* Send an extents count of 0 - the dealloc doesn't use it. */
5392 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, 0, type,
5393 LPFC_SLI4_MBX_EMBED);
5394 if (unlikely(rc)) {
5395 rc = -EIO;
5396 goto out_free_mbox;
5397 }
5398 if (!phba->sli4_hba.intr_enable)
5399 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5400 else {
James Smarta183a152011-10-10 21:32:43 -04005401 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart6d368e52011-05-24 11:44:12 -04005402 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5403 }
5404 if (unlikely(rc)) {
5405 rc = -EIO;
5406 goto out_free_mbox;
5407 }
5408
5409 dealloc_rsrc = &mbox->u.mqe.un.dealloc_rsrc_extents;
5410 if (bf_get(lpfc_mbox_hdr_status,
5411 &dealloc_rsrc->header.cfg_shdr.response)) {
5412 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
5413 "2919 Failed to release resource extents "
5414 "for type %d - Status 0x%x Add'l Status 0x%x. "
5415 "Resource memory not released.\n",
5416 type,
5417 bf_get(lpfc_mbox_hdr_status,
5418 &dealloc_rsrc->header.cfg_shdr.response),
5419 bf_get(lpfc_mbox_hdr_add_status,
5420 &dealloc_rsrc->header.cfg_shdr.response));
5421 rc = -EIO;
5422 goto out_free_mbox;
5423 }
5424
5425 /* Release kernel memory resources for the specific type. */
5426 switch (type) {
5427 case LPFC_RSC_TYPE_FCOE_VPI:
5428 kfree(phba->vpi_bmask);
5429 kfree(phba->vpi_ids);
5430 bf_set(lpfc_vpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5431 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5432 &phba->lpfc_vpi_blk_list, list) {
5433 list_del_init(&rsrc_blk->list);
5434 kfree(rsrc_blk);
5435 }
5436 break;
5437 case LPFC_RSC_TYPE_FCOE_XRI:
5438 kfree(phba->sli4_hba.xri_bmask);
5439 kfree(phba->sli4_hba.xri_ids);
James Smart6d368e52011-05-24 11:44:12 -04005440 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5441 &phba->sli4_hba.lpfc_xri_blk_list, list) {
5442 list_del_init(&rsrc_blk->list);
5443 kfree(rsrc_blk);
5444 }
5445 break;
5446 case LPFC_RSC_TYPE_FCOE_VFI:
5447 kfree(phba->sli4_hba.vfi_bmask);
5448 kfree(phba->sli4_hba.vfi_ids);
5449 bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5450 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5451 &phba->sli4_hba.lpfc_vfi_blk_list, list) {
5452 list_del_init(&rsrc_blk->list);
5453 kfree(rsrc_blk);
5454 }
5455 break;
5456 case LPFC_RSC_TYPE_FCOE_RPI:
5457 /* RPI bitmask and physical id array are cleaned up earlier. */
5458 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5459 &phba->sli4_hba.lpfc_rpi_blk_list, list) {
5460 list_del_init(&rsrc_blk->list);
5461 kfree(rsrc_blk);
5462 }
5463 break;
5464 default:
5465 break;
5466 }
5467
5468 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5469
5470 out_free_mbox:
5471 mempool_free(mbox, phba->mbox_mem_pool);
5472 return rc;
5473}
5474
5475/**
5476 * lpfc_sli4_alloc_resource_identifiers - Allocate all SLI4 resource extents.
5477 * @phba: Pointer to HBA context object.
5478 *
5479 * This function allocates all SLI4 resource identifiers.
5480 **/
5481int
5482lpfc_sli4_alloc_resource_identifiers(struct lpfc_hba *phba)
5483{
5484 int i, rc, error = 0;
5485 uint16_t count, base;
5486 unsigned long longs;
5487
James Smartff78d8f2011-12-13 13:21:35 -05005488 if (!phba->sli4_hba.rpi_hdrs_in_use)
5489 phba->sli4_hba.next_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
James Smart6d368e52011-05-24 11:44:12 -04005490 if (phba->sli4_hba.extents_in_use) {
5491 /*
5492 * The port supports resource extents. The XRI, VPI, VFI, RPI
5493 * resource extent count must be read and allocated before
5494 * provisioning the resource id arrays.
5495 */
5496 if (bf_get(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags) ==
5497 LPFC_IDX_RSRC_RDY) {
5498 /*
5499 * Extent-based resources are set - the driver could
5500 * be in a port reset. Figure out if any corrective
5501 * actions need to be taken.
5502 */
5503 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
5504 LPFC_RSC_TYPE_FCOE_VFI);
5505 if (rc != 0)
5506 error++;
5507 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
5508 LPFC_RSC_TYPE_FCOE_VPI);
5509 if (rc != 0)
5510 error++;
5511 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
5512 LPFC_RSC_TYPE_FCOE_XRI);
5513 if (rc != 0)
5514 error++;
5515 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
5516 LPFC_RSC_TYPE_FCOE_RPI);
5517 if (rc != 0)
5518 error++;
5519
5520 /*
5521 * It's possible that the number of resources
5522 * provided to this port instance changed between
5523 * resets. Detect this condition and reallocate
5524 * resources. Otherwise, there is no action.
5525 */
5526 if (error) {
5527 lpfc_printf_log(phba, KERN_INFO,
5528 LOG_MBOX | LOG_INIT,
5529 "2931 Detected extent resource "
5530 "change. Reallocating all "
5531 "extents.\n");
5532 rc = lpfc_sli4_dealloc_extent(phba,
5533 LPFC_RSC_TYPE_FCOE_VFI);
5534 rc = lpfc_sli4_dealloc_extent(phba,
5535 LPFC_RSC_TYPE_FCOE_VPI);
5536 rc = lpfc_sli4_dealloc_extent(phba,
5537 LPFC_RSC_TYPE_FCOE_XRI);
5538 rc = lpfc_sli4_dealloc_extent(phba,
5539 LPFC_RSC_TYPE_FCOE_RPI);
5540 } else
5541 return 0;
5542 }
5543
5544 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_VFI);
5545 if (unlikely(rc))
5546 goto err_exit;
5547
5548 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_VPI);
5549 if (unlikely(rc))
5550 goto err_exit;
5551
5552 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_RPI);
5553 if (unlikely(rc))
5554 goto err_exit;
5555
5556 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_XRI);
5557 if (unlikely(rc))
5558 goto err_exit;
5559 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags,
5560 LPFC_IDX_RSRC_RDY);
5561 return rc;
5562 } else {
5563 /*
5564 * The port does not support resource extents. The XRI, VPI,
5565 * VFI, RPI resource ids were determined from READ_CONFIG.
5566 * Just allocate the bitmasks and provision the resource id
5567 * arrays. If a port reset is active, the resources don't
5568 * need any action - just exit.
5569 */
5570 if (bf_get(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags) ==
James Smartff78d8f2011-12-13 13:21:35 -05005571 LPFC_IDX_RSRC_RDY) {
5572 lpfc_sli4_dealloc_resource_identifiers(phba);
5573 lpfc_sli4_remove_rpis(phba);
5574 }
James Smart6d368e52011-05-24 11:44:12 -04005575 /* RPIs. */
5576 count = phba->sli4_hba.max_cfg_param.max_rpi;
5577 base = phba->sli4_hba.max_cfg_param.rpi_base;
5578 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
5579 phba->sli4_hba.rpi_bmask = kzalloc(longs *
5580 sizeof(unsigned long),
5581 GFP_KERNEL);
5582 if (unlikely(!phba->sli4_hba.rpi_bmask)) {
5583 rc = -ENOMEM;
5584 goto err_exit;
5585 }
5586 phba->sli4_hba.rpi_ids = kzalloc(count *
5587 sizeof(uint16_t),
5588 GFP_KERNEL);
5589 if (unlikely(!phba->sli4_hba.rpi_ids)) {
5590 rc = -ENOMEM;
5591 goto free_rpi_bmask;
5592 }
5593
5594 for (i = 0; i < count; i++)
5595 phba->sli4_hba.rpi_ids[i] = base + i;
5596
5597 /* VPIs. */
5598 count = phba->sli4_hba.max_cfg_param.max_vpi;
5599 base = phba->sli4_hba.max_cfg_param.vpi_base;
5600 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
5601 phba->vpi_bmask = kzalloc(longs *
5602 sizeof(unsigned long),
5603 GFP_KERNEL);
5604 if (unlikely(!phba->vpi_bmask)) {
5605 rc = -ENOMEM;
5606 goto free_rpi_ids;
5607 }
5608 phba->vpi_ids = kzalloc(count *
5609 sizeof(uint16_t),
5610 GFP_KERNEL);
5611 if (unlikely(!phba->vpi_ids)) {
5612 rc = -ENOMEM;
5613 goto free_vpi_bmask;
5614 }
5615
5616 for (i = 0; i < count; i++)
5617 phba->vpi_ids[i] = base + i;
5618
5619 /* XRIs. */
5620 count = phba->sli4_hba.max_cfg_param.max_xri;
5621 base = phba->sli4_hba.max_cfg_param.xri_base;
5622 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
5623 phba->sli4_hba.xri_bmask = kzalloc(longs *
5624 sizeof(unsigned long),
5625 GFP_KERNEL);
5626 if (unlikely(!phba->sli4_hba.xri_bmask)) {
5627 rc = -ENOMEM;
5628 goto free_vpi_ids;
5629 }
James Smart41899be2012-03-01 22:34:19 -05005630 phba->sli4_hba.max_cfg_param.xri_used = 0;
James Smart6d368e52011-05-24 11:44:12 -04005631 phba->sli4_hba.xri_ids = kzalloc(count *
5632 sizeof(uint16_t),
5633 GFP_KERNEL);
5634 if (unlikely(!phba->sli4_hba.xri_ids)) {
5635 rc = -ENOMEM;
5636 goto free_xri_bmask;
5637 }
5638
5639 for (i = 0; i < count; i++)
5640 phba->sli4_hba.xri_ids[i] = base + i;
5641
5642 /* VFIs. */
5643 count = phba->sli4_hba.max_cfg_param.max_vfi;
5644 base = phba->sli4_hba.max_cfg_param.vfi_base;
5645 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
5646 phba->sli4_hba.vfi_bmask = kzalloc(longs *
5647 sizeof(unsigned long),
5648 GFP_KERNEL);
5649 if (unlikely(!phba->sli4_hba.vfi_bmask)) {
5650 rc = -ENOMEM;
5651 goto free_xri_ids;
5652 }
5653 phba->sli4_hba.vfi_ids = kzalloc(count *
5654 sizeof(uint16_t),
5655 GFP_KERNEL);
5656 if (unlikely(!phba->sli4_hba.vfi_ids)) {
5657 rc = -ENOMEM;
5658 goto free_vfi_bmask;
5659 }
5660
5661 for (i = 0; i < count; i++)
5662 phba->sli4_hba.vfi_ids[i] = base + i;
5663
5664 /*
5665 * Mark all resources ready. An HBA reset doesn't need
5666 * to reset the initialization.
5667 */
5668 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags,
5669 LPFC_IDX_RSRC_RDY);
5670 return 0;
5671 }
5672
5673 free_vfi_bmask:
5674 kfree(phba->sli4_hba.vfi_bmask);
5675 free_xri_ids:
5676 kfree(phba->sli4_hba.xri_ids);
5677 free_xri_bmask:
5678 kfree(phba->sli4_hba.xri_bmask);
5679 free_vpi_ids:
5680 kfree(phba->vpi_ids);
5681 free_vpi_bmask:
5682 kfree(phba->vpi_bmask);
5683 free_rpi_ids:
5684 kfree(phba->sli4_hba.rpi_ids);
5685 free_rpi_bmask:
5686 kfree(phba->sli4_hba.rpi_bmask);
5687 err_exit:
5688 return rc;
5689}
5690
5691/**
5692 * lpfc_sli4_dealloc_resource_identifiers - Deallocate all SLI4 resource extents.
5693 * @phba: Pointer to HBA context object.
5694 *
5695 * This function allocates the number of elements for the specified
5696 * resource type.
5697 **/
5698int
5699lpfc_sli4_dealloc_resource_identifiers(struct lpfc_hba *phba)
5700{
5701 if (phba->sli4_hba.extents_in_use) {
5702 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VPI);
5703 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_RPI);
5704 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_XRI);
5705 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VFI);
5706 } else {
5707 kfree(phba->vpi_bmask);
5708 kfree(phba->vpi_ids);
5709 bf_set(lpfc_vpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5710 kfree(phba->sli4_hba.xri_bmask);
5711 kfree(phba->sli4_hba.xri_ids);
James Smart6d368e52011-05-24 11:44:12 -04005712 kfree(phba->sli4_hba.vfi_bmask);
5713 kfree(phba->sli4_hba.vfi_ids);
5714 bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5715 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5716 }
5717
5718 return 0;
5719}
5720
5721/**
James Smartb76f2dc2011-07-22 18:37:42 -04005722 * lpfc_sli4_get_allocated_extnts - Get the port's allocated extents.
5723 * @phba: Pointer to HBA context object.
5724 * @type: The resource extent type.
5725 * @extnt_count: buffer to hold port extent count response
5726 * @extnt_size: buffer to hold port extent size response.
5727 *
5728 * This function calls the port to read the host allocated extents
5729 * for a particular type.
5730 **/
5731int
5732lpfc_sli4_get_allocated_extnts(struct lpfc_hba *phba, uint16_t type,
5733 uint16_t *extnt_cnt, uint16_t *extnt_size)
5734{
5735 bool emb;
5736 int rc = 0;
5737 uint16_t curr_blks = 0;
5738 uint32_t req_len, emb_len;
5739 uint32_t alloc_len, mbox_tmo;
5740 struct list_head *blk_list_head;
5741 struct lpfc_rsrc_blks *rsrc_blk;
5742 LPFC_MBOXQ_t *mbox;
5743 void *virtaddr = NULL;
5744 struct lpfc_mbx_nembed_rsrc_extent *n_rsrc;
5745 struct lpfc_mbx_alloc_rsrc_extents *rsrc_ext;
5746 union lpfc_sli4_cfg_shdr *shdr;
5747
5748 switch (type) {
5749 case LPFC_RSC_TYPE_FCOE_VPI:
5750 blk_list_head = &phba->lpfc_vpi_blk_list;
5751 break;
5752 case LPFC_RSC_TYPE_FCOE_XRI:
5753 blk_list_head = &phba->sli4_hba.lpfc_xri_blk_list;
5754 break;
5755 case LPFC_RSC_TYPE_FCOE_VFI:
5756 blk_list_head = &phba->sli4_hba.lpfc_vfi_blk_list;
5757 break;
5758 case LPFC_RSC_TYPE_FCOE_RPI:
5759 blk_list_head = &phba->sli4_hba.lpfc_rpi_blk_list;
5760 break;
5761 default:
5762 return -EIO;
5763 }
5764
5765 /* Count the number of extents currently allocatd for this type. */
5766 list_for_each_entry(rsrc_blk, blk_list_head, list) {
5767 if (curr_blks == 0) {
5768 /*
5769 * The GET_ALLOCATED mailbox does not return the size,
5770 * just the count. The size should be just the size
5771 * stored in the current allocated block and all sizes
5772 * for an extent type are the same so set the return
5773 * value now.
5774 */
5775 *extnt_size = rsrc_blk->rsrc_size;
5776 }
5777 curr_blks++;
5778 }
5779
5780 /* Calculate the total requested length of the dma memory. */
5781 req_len = curr_blks * sizeof(uint16_t);
5782
5783 /*
5784 * Calculate the size of an embedded mailbox. The uint32_t
5785 * accounts for extents-specific word.
5786 */
5787 emb_len = sizeof(MAILBOX_t) - sizeof(struct mbox_header) -
5788 sizeof(uint32_t);
5789
5790 /*
5791 * Presume the allocation and response will fit into an embedded
5792 * mailbox. If not true, reconfigure to a non-embedded mailbox.
5793 */
5794 emb = LPFC_SLI4_MBX_EMBED;
5795 req_len = emb_len;
5796 if (req_len > emb_len) {
5797 req_len = curr_blks * sizeof(uint16_t) +
5798 sizeof(union lpfc_sli4_cfg_shdr) +
5799 sizeof(uint32_t);
5800 emb = LPFC_SLI4_MBX_NEMBED;
5801 }
5802
5803 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5804 if (!mbox)
5805 return -ENOMEM;
5806 memset(mbox, 0, sizeof(LPFC_MBOXQ_t));
5807
5808 alloc_len = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5809 LPFC_MBOX_OPCODE_GET_ALLOC_RSRC_EXTENT,
5810 req_len, emb);
5811 if (alloc_len < req_len) {
5812 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5813 "2983 Allocated DMA memory size (x%x) is "
5814 "less than the requested DMA memory "
5815 "size (x%x)\n", alloc_len, req_len);
5816 rc = -ENOMEM;
5817 goto err_exit;
5818 }
5819 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, curr_blks, type, emb);
5820 if (unlikely(rc)) {
5821 rc = -EIO;
5822 goto err_exit;
5823 }
5824
5825 if (!phba->sli4_hba.intr_enable)
5826 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5827 else {
James Smarta183a152011-10-10 21:32:43 -04005828 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smartb76f2dc2011-07-22 18:37:42 -04005829 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5830 }
5831
5832 if (unlikely(rc)) {
5833 rc = -EIO;
5834 goto err_exit;
5835 }
5836
5837 /*
5838 * Figure out where the response is located. Then get local pointers
5839 * to the response data. The port does not guarantee to respond to
5840 * all extents counts request so update the local variable with the
5841 * allocated count from the port.
5842 */
5843 if (emb == LPFC_SLI4_MBX_EMBED) {
5844 rsrc_ext = &mbox->u.mqe.un.alloc_rsrc_extents;
5845 shdr = &rsrc_ext->header.cfg_shdr;
5846 *extnt_cnt = bf_get(lpfc_mbx_rsrc_cnt, &rsrc_ext->u.rsp);
5847 } else {
5848 virtaddr = mbox->sge_array->addr[0];
5849 n_rsrc = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
5850 shdr = &n_rsrc->cfg_shdr;
5851 *extnt_cnt = bf_get(lpfc_mbx_rsrc_cnt, n_rsrc);
5852 }
5853
5854 if (bf_get(lpfc_mbox_hdr_status, &shdr->response)) {
5855 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
5856 "2984 Failed to read allocated resources "
5857 "for type %d - Status 0x%x Add'l Status 0x%x.\n",
5858 type,
5859 bf_get(lpfc_mbox_hdr_status, &shdr->response),
5860 bf_get(lpfc_mbox_hdr_add_status, &shdr->response));
5861 rc = -EIO;
5862 goto err_exit;
5863 }
5864 err_exit:
5865 lpfc_sli4_mbox_cmd_free(phba, mbox);
5866 return rc;
5867}
5868
5869/**
James Smart8a9d2e82012-05-09 21:16:12 -04005870 * lpfc_sli4_repost_els_sgl_list - Repsot the els buffers sgl pages as block
5871 * @phba: pointer to lpfc hba data structure.
5872 *
5873 * This routine walks the list of els buffers that have been allocated and
5874 * repost them to the port by using SGL block post. This is needed after a
5875 * pci_function_reset/warm_start or start. It attempts to construct blocks
5876 * of els buffer sgls which contains contiguous xris and uses the non-embedded
5877 * SGL block post mailbox commands to post them to the port. For single els
5878 * buffer sgl with non-contiguous xri, if any, it shall use embedded SGL post
5879 * mailbox command for posting.
5880 *
5881 * Returns: 0 = success, non-zero failure.
5882 **/
5883static int
5884lpfc_sli4_repost_els_sgl_list(struct lpfc_hba *phba)
5885{
5886 struct lpfc_sglq *sglq_entry = NULL;
5887 struct lpfc_sglq *sglq_entry_next = NULL;
5888 struct lpfc_sglq *sglq_entry_first = NULL;
5889 int status, post_cnt = 0, num_posted = 0, block_cnt = 0;
5890 int last_xritag = NO_XRI;
5891 LIST_HEAD(prep_sgl_list);
5892 LIST_HEAD(blck_sgl_list);
5893 LIST_HEAD(allc_sgl_list);
5894 LIST_HEAD(post_sgl_list);
5895 LIST_HEAD(free_sgl_list);
5896
5897 spin_lock(&phba->hbalock);
5898 list_splice_init(&phba->sli4_hba.lpfc_sgl_list, &allc_sgl_list);
5899 spin_unlock(&phba->hbalock);
5900
5901 list_for_each_entry_safe(sglq_entry, sglq_entry_next,
5902 &allc_sgl_list, list) {
5903 list_del_init(&sglq_entry->list);
5904 block_cnt++;
5905 if ((last_xritag != NO_XRI) &&
5906 (sglq_entry->sli4_xritag != last_xritag + 1)) {
5907 /* a hole in xri block, form a sgl posting block */
5908 list_splice_init(&prep_sgl_list, &blck_sgl_list);
5909 post_cnt = block_cnt - 1;
5910 /* prepare list for next posting block */
5911 list_add_tail(&sglq_entry->list, &prep_sgl_list);
5912 block_cnt = 1;
5913 } else {
5914 /* prepare list for next posting block */
5915 list_add_tail(&sglq_entry->list, &prep_sgl_list);
5916 /* enough sgls for non-embed sgl mbox command */
5917 if (block_cnt == LPFC_NEMBED_MBOX_SGL_CNT) {
5918 list_splice_init(&prep_sgl_list,
5919 &blck_sgl_list);
5920 post_cnt = block_cnt;
5921 block_cnt = 0;
5922 }
5923 }
5924 num_posted++;
5925
5926 /* keep track of last sgl's xritag */
5927 last_xritag = sglq_entry->sli4_xritag;
5928
5929 /* end of repost sgl list condition for els buffers */
5930 if (num_posted == phba->sli4_hba.els_xri_cnt) {
5931 if (post_cnt == 0) {
5932 list_splice_init(&prep_sgl_list,
5933 &blck_sgl_list);
5934 post_cnt = block_cnt;
5935 } else if (block_cnt == 1) {
5936 status = lpfc_sli4_post_sgl(phba,
5937 sglq_entry->phys, 0,
5938 sglq_entry->sli4_xritag);
5939 if (!status) {
5940 /* successful, put sgl to posted list */
5941 list_add_tail(&sglq_entry->list,
5942 &post_sgl_list);
5943 } else {
5944 /* Failure, put sgl to free list */
5945 lpfc_printf_log(phba, KERN_WARNING,
5946 LOG_SLI,
5947 "3159 Failed to post els "
5948 "sgl, xritag:x%x\n",
5949 sglq_entry->sli4_xritag);
5950 list_add_tail(&sglq_entry->list,
5951 &free_sgl_list);
5952 spin_lock_irq(&phba->hbalock);
5953 phba->sli4_hba.els_xri_cnt--;
5954 spin_unlock_irq(&phba->hbalock);
5955 }
5956 }
5957 }
5958
5959 /* continue until a nembed page worth of sgls */
5960 if (post_cnt == 0)
5961 continue;
5962
5963 /* post the els buffer list sgls as a block */
5964 status = lpfc_sli4_post_els_sgl_list(phba, &blck_sgl_list,
5965 post_cnt);
5966
5967 if (!status) {
5968 /* success, put sgl list to posted sgl list */
5969 list_splice_init(&blck_sgl_list, &post_sgl_list);
5970 } else {
5971 /* Failure, put sgl list to free sgl list */
5972 sglq_entry_first = list_first_entry(&blck_sgl_list,
5973 struct lpfc_sglq,
5974 list);
5975 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
5976 "3160 Failed to post els sgl-list, "
5977 "xritag:x%x-x%x\n",
5978 sglq_entry_first->sli4_xritag,
5979 (sglq_entry_first->sli4_xritag +
5980 post_cnt - 1));
5981 list_splice_init(&blck_sgl_list, &free_sgl_list);
5982 spin_lock_irq(&phba->hbalock);
5983 phba->sli4_hba.els_xri_cnt -= post_cnt;
5984 spin_unlock_irq(&phba->hbalock);
5985 }
5986
5987 /* don't reset xirtag due to hole in xri block */
5988 if (block_cnt == 0)
5989 last_xritag = NO_XRI;
5990
5991 /* reset els sgl post count for next round of posting */
5992 post_cnt = 0;
5993 }
5994
5995 /* free the els sgls failed to post */
5996 lpfc_free_sgl_list(phba, &free_sgl_list);
5997
5998 /* push els sgls posted to the availble list */
5999 if (!list_empty(&post_sgl_list)) {
6000 spin_lock(&phba->hbalock);
6001 list_splice_init(&post_sgl_list,
6002 &phba->sli4_hba.lpfc_sgl_list);
6003 spin_unlock(&phba->hbalock);
6004 } else {
6005 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6006 "3161 Failure to post els sgl to port.\n");
6007 return -EIO;
6008 }
6009 return 0;
6010}
6011
6012/**
James Smartda0436e2009-05-22 14:51:39 -04006013 * lpfc_sli4_hba_setup - SLI4 device intialization PCI function
6014 * @phba: Pointer to HBA context object.
6015 *
6016 * This function is the main SLI4 device intialization PCI function. This
6017 * function is called by the HBA intialization code, HBA reset code and
6018 * HBA error attention handler code. Caller is not required to hold any
6019 * locks.
6020 **/
6021int
6022lpfc_sli4_hba_setup(struct lpfc_hba *phba)
6023{
6024 int rc;
6025 LPFC_MBOXQ_t *mboxq;
6026 struct lpfc_mqe *mqe;
6027 uint8_t *vpd;
6028 uint32_t vpd_size;
6029 uint32_t ftr_rsp = 0;
6030 struct Scsi_Host *shost = lpfc_shost_from_vport(phba->pport);
6031 struct lpfc_vport *vport = phba->pport;
6032 struct lpfc_dmabuf *mp;
6033
6034 /* Perform a PCI function reset to start from clean */
6035 rc = lpfc_pci_function_reset(phba);
6036 if (unlikely(rc))
6037 return -ENODEV;
6038
6039 /* Check the HBA Host Status Register for readyness */
6040 rc = lpfc_sli4_post_status_check(phba);
6041 if (unlikely(rc))
6042 return -ENODEV;
6043 else {
6044 spin_lock_irq(&phba->hbalock);
6045 phba->sli.sli_flag |= LPFC_SLI_ACTIVE;
6046 spin_unlock_irq(&phba->hbalock);
6047 }
6048
6049 /*
6050 * Allocate a single mailbox container for initializing the
6051 * port.
6052 */
6053 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6054 if (!mboxq)
6055 return -ENOMEM;
6056
James Smartda0436e2009-05-22 14:51:39 -04006057 /* Issue READ_REV to collect vpd and FW information. */
James Smart49198b32010-04-06 15:04:33 -04006058 vpd_size = SLI4_PAGE_SIZE;
James Smartda0436e2009-05-22 14:51:39 -04006059 vpd = kzalloc(vpd_size, GFP_KERNEL);
6060 if (!vpd) {
6061 rc = -ENOMEM;
6062 goto out_free_mbox;
6063 }
6064
6065 rc = lpfc_sli4_read_rev(phba, mboxq, vpd, &vpd_size);
James Smart76a95d72010-11-20 23:11:48 -05006066 if (unlikely(rc)) {
6067 kfree(vpd);
6068 goto out_free_mbox;
6069 }
James Smartda0436e2009-05-22 14:51:39 -04006070 mqe = &mboxq->u.mqe;
James Smartf1126682009-06-10 17:22:44 -04006071 phba->sli_rev = bf_get(lpfc_mbx_rd_rev_sli_lvl, &mqe->un.read_rev);
6072 if (bf_get(lpfc_mbx_rd_rev_fcoe, &mqe->un.read_rev))
James Smart76a95d72010-11-20 23:11:48 -05006073 phba->hba_flag |= HBA_FCOE_MODE;
6074 else
6075 phba->hba_flag &= ~HBA_FCOE_MODE;
James Smart45ed1192009-10-02 15:17:02 -04006076
6077 if (bf_get(lpfc_mbx_rd_rev_cee_ver, &mqe->un.read_rev) ==
6078 LPFC_DCBX_CEE_MODE)
6079 phba->hba_flag |= HBA_FIP_SUPPORT;
6080 else
6081 phba->hba_flag &= ~HBA_FIP_SUPPORT;
6082
James Smart4f2e66c2012-05-09 21:17:07 -04006083 phba->hba_flag &= ~HBA_FCP_IOQ_FLUSH;
6084
James Smartc31098c2011-04-16 11:03:33 -04006085 if (phba->sli_rev != LPFC_SLI_REV4) {
James Smartda0436e2009-05-22 14:51:39 -04006086 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6087 "0376 READ_REV Error. SLI Level %d "
6088 "FCoE enabled %d\n",
James Smart76a95d72010-11-20 23:11:48 -05006089 phba->sli_rev, phba->hba_flag & HBA_FCOE_MODE);
James Smartda0436e2009-05-22 14:51:39 -04006090 rc = -EIO;
James Smart76a95d72010-11-20 23:11:48 -05006091 kfree(vpd);
6092 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04006093 }
James Smartcd1c8302011-10-10 21:33:25 -04006094
6095 /*
James Smartff78d8f2011-12-13 13:21:35 -05006096 * Continue initialization with default values even if driver failed
6097 * to read FCoE param config regions, only read parameters if the
6098 * board is FCoE
6099 */
6100 if (phba->hba_flag & HBA_FCOE_MODE &&
6101 lpfc_sli4_read_fcoe_params(phba))
6102 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_INIT,
6103 "2570 Failed to read FCoE parameters\n");
6104
6105 /*
James Smartcd1c8302011-10-10 21:33:25 -04006106 * Retrieve sli4 device physical port name, failure of doing it
6107 * is considered as non-fatal.
6108 */
6109 rc = lpfc_sli4_retrieve_pport_name(phba);
6110 if (!rc)
6111 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
6112 "3080 Successful retrieving SLI4 device "
6113 "physical port name: %s.\n", phba->Port);
6114
James Smartda0436e2009-05-22 14:51:39 -04006115 /*
6116 * Evaluate the read rev and vpd data. Populate the driver
6117 * state with the results. If this routine fails, the failure
6118 * is not fatal as the driver will use generic values.
6119 */
6120 rc = lpfc_parse_vpd(phba, vpd, vpd_size);
6121 if (unlikely(!rc)) {
6122 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6123 "0377 Error %d parsing vpd. "
6124 "Using defaults.\n", rc);
6125 rc = 0;
6126 }
James Smart76a95d72010-11-20 23:11:48 -05006127 kfree(vpd);
James Smartda0436e2009-05-22 14:51:39 -04006128
James Smartf1126682009-06-10 17:22:44 -04006129 /* Save information as VPD data */
6130 phba->vpd.rev.biuRev = mqe->un.read_rev.first_hw_rev;
6131 phba->vpd.rev.smRev = mqe->un.read_rev.second_hw_rev;
6132 phba->vpd.rev.endecRev = mqe->un.read_rev.third_hw_rev;
6133 phba->vpd.rev.fcphHigh = bf_get(lpfc_mbx_rd_rev_fcph_high,
6134 &mqe->un.read_rev);
6135 phba->vpd.rev.fcphLow = bf_get(lpfc_mbx_rd_rev_fcph_low,
6136 &mqe->un.read_rev);
6137 phba->vpd.rev.feaLevelHigh = bf_get(lpfc_mbx_rd_rev_ftr_lvl_high,
6138 &mqe->un.read_rev);
6139 phba->vpd.rev.feaLevelLow = bf_get(lpfc_mbx_rd_rev_ftr_lvl_low,
6140 &mqe->un.read_rev);
6141 phba->vpd.rev.sli1FwRev = mqe->un.read_rev.fw_id_rev;
6142 memcpy(phba->vpd.rev.sli1FwName, mqe->un.read_rev.fw_name, 16);
6143 phba->vpd.rev.sli2FwRev = mqe->un.read_rev.ulp_fw_id_rev;
6144 memcpy(phba->vpd.rev.sli2FwName, mqe->un.read_rev.ulp_fw_name, 16);
6145 phba->vpd.rev.opFwRev = mqe->un.read_rev.fw_id_rev;
6146 memcpy(phba->vpd.rev.opFwName, mqe->un.read_rev.fw_name, 16);
6147 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
6148 "(%d):0380 READ_REV Status x%x "
6149 "fw_rev:%s fcphHi:%x fcphLo:%x flHi:%x flLo:%x\n",
6150 mboxq->vport ? mboxq->vport->vpi : 0,
6151 bf_get(lpfc_mqe_status, mqe),
6152 phba->vpd.rev.opFwName,
6153 phba->vpd.rev.fcphHigh, phba->vpd.rev.fcphLow,
6154 phba->vpd.rev.feaLevelHigh, phba->vpd.rev.feaLevelLow);
James Smartda0436e2009-05-22 14:51:39 -04006155
6156 /*
6157 * Discover the port's supported feature set and match it against the
6158 * hosts requests.
6159 */
6160 lpfc_request_features(phba, mboxq);
6161 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6162 if (unlikely(rc)) {
6163 rc = -EIO;
James Smart76a95d72010-11-20 23:11:48 -05006164 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04006165 }
6166
6167 /*
6168 * The port must support FCP initiator mode as this is the
6169 * only mode running in the host.
6170 */
6171 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_fcpi, &mqe->un.req_ftrs))) {
6172 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
6173 "0378 No support for fcpi mode.\n");
6174 ftr_rsp++;
6175 }
James Smartfedd3b72011-02-16 12:39:24 -05006176 if (bf_get(lpfc_mbx_rq_ftr_rsp_perfh, &mqe->un.req_ftrs))
6177 phba->sli3_options |= LPFC_SLI4_PERFH_ENABLED;
6178 else
6179 phba->sli3_options &= ~LPFC_SLI4_PERFH_ENABLED;
James Smartda0436e2009-05-22 14:51:39 -04006180 /*
6181 * If the port cannot support the host's requested features
6182 * then turn off the global config parameters to disable the
6183 * feature in the driver. This is not a fatal error.
6184 */
James Smartbf086112011-08-21 21:48:13 -04006185 phba->sli3_options &= ~LPFC_SLI3_BG_ENABLED;
6186 if (phba->cfg_enable_bg) {
6187 if (bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs))
6188 phba->sli3_options |= LPFC_SLI3_BG_ENABLED;
6189 else
6190 ftr_rsp++;
6191 }
James Smartda0436e2009-05-22 14:51:39 -04006192
6193 if (phba->max_vpi && phba->cfg_enable_npiv &&
6194 !(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
6195 ftr_rsp++;
6196
6197 if (ftr_rsp) {
6198 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
6199 "0379 Feature Mismatch Data: x%08x %08x "
6200 "x%x x%x x%x\n", mqe->un.req_ftrs.word2,
6201 mqe->un.req_ftrs.word3, phba->cfg_enable_bg,
6202 phba->cfg_enable_npiv, phba->max_vpi);
6203 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs)))
6204 phba->cfg_enable_bg = 0;
6205 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
6206 phba->cfg_enable_npiv = 0;
6207 }
6208
6209 /* These SLI3 features are assumed in SLI4 */
6210 spin_lock_irq(&phba->hbalock);
6211 phba->sli3_options |= (LPFC_SLI3_NPIV_ENABLED | LPFC_SLI3_HBQ_ENABLED);
6212 spin_unlock_irq(&phba->hbalock);
6213
James Smart6d368e52011-05-24 11:44:12 -04006214 /*
6215 * Allocate all resources (xri,rpi,vpi,vfi) now. Subsequent
6216 * calls depends on these resources to complete port setup.
6217 */
6218 rc = lpfc_sli4_alloc_resource_identifiers(phba);
6219 if (rc) {
6220 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6221 "2920 Failed to alloc Resource IDs "
6222 "rc = x%x\n", rc);
6223 goto out_free_mbox;
6224 }
6225
James Smartda0436e2009-05-22 14:51:39 -04006226 /* Read the port's service parameters. */
James Smart9f1177a2010-02-26 14:12:57 -05006227 rc = lpfc_read_sparam(phba, mboxq, vport->vpi);
6228 if (rc) {
6229 phba->link_state = LPFC_HBA_ERROR;
6230 rc = -ENOMEM;
James Smart76a95d72010-11-20 23:11:48 -05006231 goto out_free_mbox;
James Smart9f1177a2010-02-26 14:12:57 -05006232 }
6233
James Smartda0436e2009-05-22 14:51:39 -04006234 mboxq->vport = vport;
6235 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6236 mp = (struct lpfc_dmabuf *) mboxq->context1;
6237 if (rc == MBX_SUCCESS) {
6238 memcpy(&vport->fc_sparam, mp->virt, sizeof(struct serv_parm));
6239 rc = 0;
6240 }
6241
6242 /*
6243 * This memory was allocated by the lpfc_read_sparam routine. Release
6244 * it to the mbuf pool.
6245 */
6246 lpfc_mbuf_free(phba, mp->virt, mp->phys);
6247 kfree(mp);
6248 mboxq->context1 = NULL;
6249 if (unlikely(rc)) {
6250 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6251 "0382 READ_SPARAM command failed "
6252 "status %d, mbxStatus x%x\n",
6253 rc, bf_get(lpfc_mqe_status, mqe));
6254 phba->link_state = LPFC_HBA_ERROR;
6255 rc = -EIO;
James Smart76a95d72010-11-20 23:11:48 -05006256 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04006257 }
6258
James Smart05580562011-05-24 11:40:48 -04006259 lpfc_update_vport_wwn(vport);
James Smartda0436e2009-05-22 14:51:39 -04006260
6261 /* Update the fc_host data structures with new wwn. */
6262 fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
6263 fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
6264
James Smart8a9d2e82012-05-09 21:16:12 -04006265 /* update host els and scsi xri-sgl sizes and mappings */
6266 rc = lpfc_sli4_xri_sgl_update(phba);
6267 if (unlikely(rc)) {
6268 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6269 "1400 Failed to update xri-sgl size and "
6270 "mapping: %d\n", rc);
6271 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04006272 }
6273
James Smart8a9d2e82012-05-09 21:16:12 -04006274 /* register the els sgl pool to the port */
6275 rc = lpfc_sli4_repost_els_sgl_list(phba);
6276 if (unlikely(rc)) {
6277 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6278 "0582 Error %d during els sgl post "
6279 "operation\n", rc);
6280 rc = -ENODEV;
6281 goto out_free_mbox;
6282 }
6283
6284 /* register the allocated scsi sgl pool to the port */
James Smartda0436e2009-05-22 14:51:39 -04006285 rc = lpfc_sli4_repost_scsi_sgl_list(phba);
6286 if (unlikely(rc)) {
James Smart6d368e52011-05-24 11:44:12 -04006287 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smart6a9c52c2009-10-02 15:16:51 -04006288 "0383 Error %d during scsi sgl post "
6289 "operation\n", rc);
James Smartda0436e2009-05-22 14:51:39 -04006290 /* Some Scsi buffers were moved to the abort scsi list */
6291 /* A pci function reset will repost them */
6292 rc = -ENODEV;
James Smart76a95d72010-11-20 23:11:48 -05006293 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04006294 }
6295
6296 /* Post the rpi header region to the device. */
6297 rc = lpfc_sli4_post_all_rpi_hdrs(phba);
6298 if (unlikely(rc)) {
6299 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6300 "0393 Error %d during rpi post operation\n",
6301 rc);
6302 rc = -ENODEV;
James Smart76a95d72010-11-20 23:11:48 -05006303 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04006304 }
James Smart97f2ecf2012-03-01 22:35:23 -05006305 lpfc_sli4_node_prep(phba);
James Smartda0436e2009-05-22 14:51:39 -04006306
James Smart5350d872011-10-10 21:33:49 -04006307 /* Create all the SLI4 queues */
6308 rc = lpfc_sli4_queue_create(phba);
6309 if (rc) {
6310 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6311 "3089 Failed to allocate queues\n");
6312 rc = -ENODEV;
6313 goto out_stop_timers;
6314 }
James Smartda0436e2009-05-22 14:51:39 -04006315 /* Set up all the queues to the device */
6316 rc = lpfc_sli4_queue_setup(phba);
6317 if (unlikely(rc)) {
6318 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6319 "0381 Error %d during queue setup.\n ", rc);
James Smart5350d872011-10-10 21:33:49 -04006320 goto out_destroy_queue;
James Smartda0436e2009-05-22 14:51:39 -04006321 }
6322
6323 /* Arm the CQs and then EQs on device */
6324 lpfc_sli4_arm_cqeq_intr(phba);
6325
6326 /* Indicate device interrupt mode */
6327 phba->sli4_hba.intr_enable = 1;
6328
6329 /* Allow asynchronous mailbox command to go through */
6330 spin_lock_irq(&phba->hbalock);
6331 phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
6332 spin_unlock_irq(&phba->hbalock);
6333
6334 /* Post receive buffers to the device */
6335 lpfc_sli4_rb_setup(phba);
6336
James Smartfc2b9892010-02-26 14:15:29 -05006337 /* Reset HBA FCF states after HBA reset */
6338 phba->fcf.fcf_flag = 0;
6339 phba->fcf.current_rec.flag = 0;
6340
James Smartda0436e2009-05-22 14:51:39 -04006341 /* Start the ELS watchdog timer */
James Smart8fa38512009-07-19 10:01:03 -04006342 mod_timer(&vport->els_tmofunc,
6343 jiffies + HZ * (phba->fc_ratov * 2));
James Smartda0436e2009-05-22 14:51:39 -04006344
6345 /* Start heart beat timer */
6346 mod_timer(&phba->hb_tmofunc,
6347 jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
6348 phba->hb_outstanding = 0;
6349 phba->last_completion_time = jiffies;
6350
6351 /* Start error attention (ERATT) polling timer */
6352 mod_timer(&phba->eratt_poll, jiffies + HZ * LPFC_ERATT_POLL_INTERVAL);
6353
James Smart75baf692010-06-08 18:31:21 -04006354 /* Enable PCIe device Advanced Error Reporting (AER) if configured */
6355 if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
6356 rc = pci_enable_pcie_error_reporting(phba->pcidev);
6357 if (!rc) {
6358 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6359 "2829 This device supports "
6360 "Advanced Error Reporting (AER)\n");
6361 spin_lock_irq(&phba->hbalock);
6362 phba->hba_flag |= HBA_AER_ENABLED;
6363 spin_unlock_irq(&phba->hbalock);
6364 } else {
6365 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6366 "2830 This device does not support "
6367 "Advanced Error Reporting (AER)\n");
6368 phba->cfg_aer_support = 0;
6369 }
James Smart0a96e972011-07-22 18:37:28 -04006370 rc = 0;
James Smart75baf692010-06-08 18:31:21 -04006371 }
6372
James Smart76a95d72010-11-20 23:11:48 -05006373 if (!(phba->hba_flag & HBA_FCOE_MODE)) {
6374 /*
6375 * The FC Port needs to register FCFI (index 0)
6376 */
6377 lpfc_reg_fcfi(phba, mboxq);
6378 mboxq->vport = phba->pport;
6379 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
James Smart9589b0622011-04-16 11:03:17 -04006380 if (rc != MBX_SUCCESS)
James Smart76a95d72010-11-20 23:11:48 -05006381 goto out_unset_queue;
James Smart9589b0622011-04-16 11:03:17 -04006382 rc = 0;
6383 phba->fcf.fcfi = bf_get(lpfc_reg_fcfi_fcfi,
6384 &mboxq->u.mqe.un.reg_fcfi);
James Smart026abb82011-12-13 13:20:45 -05006385
6386 /* Check if the port is configured to be disabled */
6387 lpfc_sli_read_link_ste(phba);
James Smart76a95d72010-11-20 23:11:48 -05006388 }
James Smart026abb82011-12-13 13:20:45 -05006389
James Smartda0436e2009-05-22 14:51:39 -04006390 /*
6391 * The port is ready, set the host's link state to LINK_DOWN
6392 * in preparation for link interrupts.
6393 */
James Smartda0436e2009-05-22 14:51:39 -04006394 spin_lock_irq(&phba->hbalock);
6395 phba->link_state = LPFC_LINK_DOWN;
6396 spin_unlock_irq(&phba->hbalock);
James Smart026abb82011-12-13 13:20:45 -05006397 if (!(phba->hba_flag & HBA_FCOE_MODE) &&
6398 (phba->hba_flag & LINK_DISABLED)) {
6399 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_SLI,
6400 "3103 Adapter Link is disabled.\n");
6401 lpfc_down_link(phba, mboxq);
6402 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6403 if (rc != MBX_SUCCESS) {
6404 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_SLI,
6405 "3104 Adapter failed to issue "
6406 "DOWN_LINK mbox cmd, rc:x%x\n", rc);
6407 goto out_unset_queue;
6408 }
6409 } else if (phba->cfg_suppress_link_up == LPFC_INITIALIZE_LINK) {
James Smart1b511972011-12-13 13:23:09 -05006410 /* don't perform init_link on SLI4 FC port loopback test */
6411 if (!(phba->link_flag & LS_LOOPBACK_MODE)) {
6412 rc = phba->lpfc_hba_init_link(phba, MBX_NOWAIT);
6413 if (rc)
6414 goto out_unset_queue;
6415 }
James Smart5350d872011-10-10 21:33:49 -04006416 }
6417 mempool_free(mboxq, phba->mbox_mem_pool);
6418 return rc;
James Smart76a95d72010-11-20 23:11:48 -05006419out_unset_queue:
James Smartda0436e2009-05-22 14:51:39 -04006420 /* Unset all the queues set up in this routine when error out */
James Smart5350d872011-10-10 21:33:49 -04006421 lpfc_sli4_queue_unset(phba);
6422out_destroy_queue:
6423 lpfc_sli4_queue_destroy(phba);
James Smartda0436e2009-05-22 14:51:39 -04006424out_stop_timers:
James Smart5350d872011-10-10 21:33:49 -04006425 lpfc_stop_hba_timers(phba);
James Smartda0436e2009-05-22 14:51:39 -04006426out_free_mbox:
6427 mempool_free(mboxq, phba->mbox_mem_pool);
6428 return rc;
6429}
James Smarte59058c2008-08-24 21:49:00 -04006430
6431/**
James Smart3621a712009-04-06 18:47:14 -04006432 * lpfc_mbox_timeout - Timeout call back function for mbox timer
James Smarte59058c2008-08-24 21:49:00 -04006433 * @ptr: context object - pointer to hba structure.
dea31012005-04-17 16:05:31 -05006434 *
James Smarte59058c2008-08-24 21:49:00 -04006435 * This is the callback function for mailbox timer. The mailbox
6436 * timer is armed when a new mailbox command is issued and the timer
6437 * is deleted when the mailbox complete. The function is called by
6438 * the kernel timer code when a mailbox does not complete within
6439 * expected time. This function wakes up the worker thread to
6440 * process the mailbox timeout and returns. All the processing is
6441 * done by the worker thread function lpfc_mbox_timeout_handler.
6442 **/
dea31012005-04-17 16:05:31 -05006443void
6444lpfc_mbox_timeout(unsigned long ptr)
6445{
James Smart92d7f7b2007-06-17 19:56:38 -05006446 struct lpfc_hba *phba = (struct lpfc_hba *) ptr;
dea31012005-04-17 16:05:31 -05006447 unsigned long iflag;
James Smart2e0fef82007-06-17 19:56:36 -05006448 uint32_t tmo_posted;
dea31012005-04-17 16:05:31 -05006449
James Smart2e0fef82007-06-17 19:56:36 -05006450 spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
James Smart92d7f7b2007-06-17 19:56:38 -05006451 tmo_posted = phba->pport->work_port_events & WORKER_MBOX_TMO;
James Smart2e0fef82007-06-17 19:56:36 -05006452 if (!tmo_posted)
6453 phba->pport->work_port_events |= WORKER_MBOX_TMO;
6454 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
6455
James Smart5e9d9b82008-06-14 22:52:53 -04006456 if (!tmo_posted)
6457 lpfc_worker_wake_up(phba);
6458 return;
dea31012005-04-17 16:05:31 -05006459}
6460
James Smarte59058c2008-08-24 21:49:00 -04006461
6462/**
James Smart3621a712009-04-06 18:47:14 -04006463 * lpfc_mbox_timeout_handler - Worker thread function to handle mailbox timeout
James Smarte59058c2008-08-24 21:49:00 -04006464 * @phba: Pointer to HBA context object.
6465 *
6466 * This function is called from worker thread when a mailbox command times out.
6467 * The caller is not required to hold any locks. This function will reset the
6468 * HBA and recover all the pending commands.
6469 **/
dea31012005-04-17 16:05:31 -05006470void
6471lpfc_mbox_timeout_handler(struct lpfc_hba *phba)
6472{
James Smart2e0fef82007-06-17 19:56:36 -05006473 LPFC_MBOXQ_t *pmbox = phba->sli.mbox_active;
James Smart04c68492009-05-22 14:52:52 -04006474 MAILBOX_t *mb = &pmbox->u.mb;
James Smart1dcb58e2007-04-25 09:51:30 -04006475 struct lpfc_sli *psli = &phba->sli;
6476 struct lpfc_sli_ring *pring;
dea31012005-04-17 16:05:31 -05006477
James Smarta257bf92009-04-06 18:48:10 -04006478 /* Check the pmbox pointer first. There is a race condition
6479 * between the mbox timeout handler getting executed in the
6480 * worklist and the mailbox actually completing. When this
6481 * race condition occurs, the mbox_active will be NULL.
6482 */
6483 spin_lock_irq(&phba->hbalock);
6484 if (pmbox == NULL) {
6485 lpfc_printf_log(phba, KERN_WARNING,
6486 LOG_MBOX | LOG_SLI,
6487 "0353 Active Mailbox cleared - mailbox timeout "
6488 "exiting\n");
6489 spin_unlock_irq(&phba->hbalock);
6490 return;
6491 }
6492
dea31012005-04-17 16:05:31 -05006493 /* Mbox cmd <mbxCommand> timeout */
James Smarted957682007-06-17 19:56:37 -05006494 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04006495 "0310 Mailbox command x%x timeout Data: x%x x%x x%p\n",
James Smart92d7f7b2007-06-17 19:56:38 -05006496 mb->mbxCommand,
6497 phba->pport->port_state,
6498 phba->sli.sli_flag,
6499 phba->sli.mbox_active);
James Smarta257bf92009-04-06 18:48:10 -04006500 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05006501
James Smart1dcb58e2007-04-25 09:51:30 -04006502 /* Setting state unknown so lpfc_sli_abort_iocb_ring
6503 * would get IOCB_ERROR from lpfc_sli_issue_iocb, allowing
Lucas De Marchi25985ed2011-03-30 22:57:33 -03006504 * it to fail all outstanding SCSI IO.
James Smart1dcb58e2007-04-25 09:51:30 -04006505 */
James Smart2e0fef82007-06-17 19:56:36 -05006506 spin_lock_irq(&phba->pport->work_port_lock);
6507 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
6508 spin_unlock_irq(&phba->pport->work_port_lock);
6509 spin_lock_irq(&phba->hbalock);
6510 phba->link_state = LPFC_LINK_UNKNOWN;
James Smartf4b4c682009-05-22 14:53:12 -04006511 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05006512 spin_unlock_irq(&phba->hbalock);
James Smart1dcb58e2007-04-25 09:51:30 -04006513
6514 pring = &psli->ring[psli->fcp_ring];
6515 lpfc_sli_abort_iocb_ring(phba, pring);
6516
6517 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smart76bb24e2007-10-27 13:38:00 -04006518 "0345 Resetting board due to mailbox timeout\n");
James Smart3772a992009-05-22 14:50:54 -04006519
6520 /* Reset the HBA device */
6521 lpfc_reset_hba(phba);
dea31012005-04-17 16:05:31 -05006522}
6523
James Smarte59058c2008-08-24 21:49:00 -04006524/**
James Smart3772a992009-05-22 14:50:54 -04006525 * lpfc_sli_issue_mbox_s3 - Issue an SLI3 mailbox command to firmware
James Smarte59058c2008-08-24 21:49:00 -04006526 * @phba: Pointer to HBA context object.
6527 * @pmbox: Pointer to mailbox object.
6528 * @flag: Flag indicating how the mailbox need to be processed.
6529 *
6530 * This function is called by discovery code and HBA management code
James Smart3772a992009-05-22 14:50:54 -04006531 * to submit a mailbox command to firmware with SLI-3 interface spec. This
6532 * function gets the hbalock to protect the data structures.
James Smarte59058c2008-08-24 21:49:00 -04006533 * The mailbox command can be submitted in polling mode, in which case
6534 * this function will wait in a polling loop for the completion of the
6535 * mailbox.
6536 * If the mailbox is submitted in no_wait mode (not polling) the
6537 * function will submit the command and returns immediately without waiting
6538 * for the mailbox completion. The no_wait is supported only when HBA
6539 * is in SLI2/SLI3 mode - interrupts are enabled.
6540 * The SLI interface allows only one mailbox pending at a time. If the
6541 * mailbox is issued in polling mode and there is already a mailbox
6542 * pending, then the function will return an error. If the mailbox is issued
6543 * in NO_WAIT mode and there is a mailbox pending already, the function
6544 * will return MBX_BUSY after queuing the mailbox into mailbox queue.
6545 * The sli layer owns the mailbox object until the completion of mailbox
6546 * command if this function return MBX_BUSY or MBX_SUCCESS. For all other
6547 * return codes the caller owns the mailbox command after the return of
6548 * the function.
6549 **/
James Smart3772a992009-05-22 14:50:54 -04006550static int
6551lpfc_sli_issue_mbox_s3(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox,
6552 uint32_t flag)
dea31012005-04-17 16:05:31 -05006553{
dea31012005-04-17 16:05:31 -05006554 MAILBOX_t *mb;
James Smart2e0fef82007-06-17 19:56:36 -05006555 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05006556 uint32_t status, evtctr;
James Smart9940b972011-03-11 16:06:12 -05006557 uint32_t ha_copy, hc_copy;
dea31012005-04-17 16:05:31 -05006558 int i;
James Smart09372822008-01-11 01:52:54 -05006559 unsigned long timeout;
dea31012005-04-17 16:05:31 -05006560 unsigned long drvr_flag = 0;
James Smart34b02dc2008-08-24 21:49:55 -04006561 uint32_t word0, ldata;
dea31012005-04-17 16:05:31 -05006562 void __iomem *to_slim;
James Smart58da1ff2008-04-07 10:15:56 -04006563 int processing_queue = 0;
6564
6565 spin_lock_irqsave(&phba->hbalock, drvr_flag);
6566 if (!pmbox) {
James Smart8568a4d2009-07-19 10:01:16 -04006567 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart58da1ff2008-04-07 10:15:56 -04006568 /* processing mbox queue from intr_handler */
James Smart3772a992009-05-22 14:50:54 -04006569 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
6570 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
6571 return MBX_SUCCESS;
6572 }
James Smart58da1ff2008-04-07 10:15:56 -04006573 processing_queue = 1;
James Smart58da1ff2008-04-07 10:15:56 -04006574 pmbox = lpfc_mbox_get(phba);
6575 if (!pmbox) {
6576 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
6577 return MBX_SUCCESS;
6578 }
6579 }
dea31012005-04-17 16:05:31 -05006580
James Smarted957682007-06-17 19:56:37 -05006581 if (pmbox->mbox_cmpl && pmbox->mbox_cmpl != lpfc_sli_def_mbox_cmpl &&
James Smart92d7f7b2007-06-17 19:56:38 -05006582 pmbox->mbox_cmpl != lpfc_sli_wake_mbox_wait) {
James Smarted957682007-06-17 19:56:37 -05006583 if(!pmbox->vport) {
James Smart58da1ff2008-04-07 10:15:56 -04006584 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
James Smarted957682007-06-17 19:56:37 -05006585 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -05006586 LOG_MBOX | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04006587 "1806 Mbox x%x failed. No vport\n",
James Smart3772a992009-05-22 14:50:54 -04006588 pmbox->u.mb.mbxCommand);
James Smarted957682007-06-17 19:56:37 -05006589 dump_stack();
James Smart58da1ff2008-04-07 10:15:56 -04006590 goto out_not_finished;
James Smarted957682007-06-17 19:56:37 -05006591 }
6592 }
6593
Linas Vepstas8d63f372007-02-14 14:28:36 -06006594 /* If the PCI channel is in offline state, do not post mbox. */
James Smart58da1ff2008-04-07 10:15:56 -04006595 if (unlikely(pci_channel_offline(phba->pcidev))) {
6596 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
6597 goto out_not_finished;
6598 }
Linas Vepstas8d63f372007-02-14 14:28:36 -06006599
James Smarta257bf92009-04-06 18:48:10 -04006600 /* If HBA has a deferred error attention, fail the iocb. */
6601 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
6602 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
6603 goto out_not_finished;
6604 }
6605
dea31012005-04-17 16:05:31 -05006606 psli = &phba->sli;
James Smart92d7f7b2007-06-17 19:56:38 -05006607
James Smart3772a992009-05-22 14:50:54 -04006608 mb = &pmbox->u.mb;
dea31012005-04-17 16:05:31 -05006609 status = MBX_SUCCESS;
6610
James Smart2e0fef82007-06-17 19:56:36 -05006611 if (phba->link_state == LPFC_HBA_ERROR) {
6612 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05006613
6614 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04006615 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6616 "(%d):0311 Mailbox command x%x cannot "
6617 "issue Data: x%x x%x\n",
6618 pmbox->vport ? pmbox->vport->vpi : 0,
6619 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04006620 goto out_not_finished;
Jamie Wellnitz41415862006-02-28 19:25:27 -05006621 }
6622
James Smart9940b972011-03-11 16:06:12 -05006623 if (mb->mbxCommand != MBX_KILL_BOARD && flag & MBX_NOWAIT) {
6624 if (lpfc_readl(phba->HCregaddr, &hc_copy) ||
6625 !(hc_copy & HC_MBINT_ENA)) {
6626 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
6627 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smart3772a992009-05-22 14:50:54 -04006628 "(%d):2528 Mailbox command x%x cannot "
6629 "issue Data: x%x x%x\n",
6630 pmbox->vport ? pmbox->vport->vpi : 0,
6631 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
James Smart9940b972011-03-11 16:06:12 -05006632 goto out_not_finished;
6633 }
James Smart92908312006-03-07 15:04:13 -05006634 }
6635
dea31012005-04-17 16:05:31 -05006636 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
6637 /* Polling for a mbox command when another one is already active
6638 * is not allowed in SLI. Also, the driver must have established
6639 * SLI2 mode to queue and process multiple mbox commands.
6640 */
6641
6642 if (flag & MBX_POLL) {
James Smart2e0fef82007-06-17 19:56:36 -05006643 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05006644
6645 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04006646 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6647 "(%d):2529 Mailbox command x%x "
6648 "cannot issue Data: x%x x%x\n",
6649 pmbox->vport ? pmbox->vport->vpi : 0,
6650 pmbox->u.mb.mbxCommand,
6651 psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04006652 goto out_not_finished;
dea31012005-04-17 16:05:31 -05006653 }
6654
James Smart3772a992009-05-22 14:50:54 -04006655 if (!(psli->sli_flag & LPFC_SLI_ACTIVE)) {
James Smart2e0fef82007-06-17 19:56:36 -05006656 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05006657 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04006658 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6659 "(%d):2530 Mailbox command x%x "
6660 "cannot issue Data: x%x x%x\n",
6661 pmbox->vport ? pmbox->vport->vpi : 0,
6662 pmbox->u.mb.mbxCommand,
6663 psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04006664 goto out_not_finished;
dea31012005-04-17 16:05:31 -05006665 }
6666
dea31012005-04-17 16:05:31 -05006667 /* Another mailbox command is still being processed, queue this
6668 * command to be processed later.
6669 */
6670 lpfc_mbox_put(phba, pmbox);
6671
6672 /* Mbox cmd issue - BUSY */
James Smarted957682007-06-17 19:56:37 -05006673 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04006674 "(%d):0308 Mbox cmd issue - BUSY Data: "
James Smart92d7f7b2007-06-17 19:56:38 -05006675 "x%x x%x x%x x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -05006676 pmbox->vport ? pmbox->vport->vpi : 0xffffff,
6677 mb->mbxCommand, phba->pport->port_state,
6678 psli->sli_flag, flag);
dea31012005-04-17 16:05:31 -05006679
6680 psli->slistat.mbox_busy++;
James Smart2e0fef82007-06-17 19:56:36 -05006681 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05006682
James Smart858c9f62007-06-17 19:56:39 -05006683 if (pmbox->vport) {
6684 lpfc_debugfs_disc_trc(pmbox->vport,
6685 LPFC_DISC_TRC_MBOX_VPORT,
6686 "MBOX Bsy vport: cmd:x%x mb:x%x x%x",
6687 (uint32_t)mb->mbxCommand,
6688 mb->un.varWords[0], mb->un.varWords[1]);
6689 }
6690 else {
6691 lpfc_debugfs_disc_trc(phba->pport,
6692 LPFC_DISC_TRC_MBOX,
6693 "MBOX Bsy: cmd:x%x mb:x%x x%x",
6694 (uint32_t)mb->mbxCommand,
6695 mb->un.varWords[0], mb->un.varWords[1]);
6696 }
6697
James Smart2e0fef82007-06-17 19:56:36 -05006698 return MBX_BUSY;
dea31012005-04-17 16:05:31 -05006699 }
6700
dea31012005-04-17 16:05:31 -05006701 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
6702
6703 /* If we are not polling, we MUST be in SLI2 mode */
6704 if (flag != MBX_POLL) {
James Smart3772a992009-05-22 14:50:54 -04006705 if (!(psli->sli_flag & LPFC_SLI_ACTIVE) &&
Jamie Wellnitz41415862006-02-28 19:25:27 -05006706 (mb->mbxCommand != MBX_KILL_BOARD)) {
dea31012005-04-17 16:05:31 -05006707 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05006708 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05006709 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04006710 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6711 "(%d):2531 Mailbox command x%x "
6712 "cannot issue Data: x%x x%x\n",
6713 pmbox->vport ? pmbox->vport->vpi : 0,
6714 pmbox->u.mb.mbxCommand,
6715 psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04006716 goto out_not_finished;
dea31012005-04-17 16:05:31 -05006717 }
6718 /* timeout active mbox command */
James Smarta309a6b2006-08-01 07:33:43 -04006719 mod_timer(&psli->mbox_tmo, (jiffies +
James Smarta183a152011-10-10 21:32:43 -04006720 (HZ * lpfc_mbox_tmo_val(phba, pmbox))));
dea31012005-04-17 16:05:31 -05006721 }
6722
6723 /* Mailbox cmd <cmd> issue */
James Smarted957682007-06-17 19:56:37 -05006724 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04006725 "(%d):0309 Mailbox cmd x%x issue Data: x%x x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05006726 "x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04006727 pmbox->vport ? pmbox->vport->vpi : 0,
James Smart92d7f7b2007-06-17 19:56:38 -05006728 mb->mbxCommand, phba->pport->port_state,
6729 psli->sli_flag, flag);
dea31012005-04-17 16:05:31 -05006730
James Smart858c9f62007-06-17 19:56:39 -05006731 if (mb->mbxCommand != MBX_HEARTBEAT) {
6732 if (pmbox->vport) {
6733 lpfc_debugfs_disc_trc(pmbox->vport,
6734 LPFC_DISC_TRC_MBOX_VPORT,
6735 "MBOX Send vport: cmd:x%x mb:x%x x%x",
6736 (uint32_t)mb->mbxCommand,
6737 mb->un.varWords[0], mb->un.varWords[1]);
6738 }
6739 else {
6740 lpfc_debugfs_disc_trc(phba->pport,
6741 LPFC_DISC_TRC_MBOX,
6742 "MBOX Send: cmd:x%x mb:x%x x%x",
6743 (uint32_t)mb->mbxCommand,
6744 mb->un.varWords[0], mb->un.varWords[1]);
6745 }
6746 }
6747
dea31012005-04-17 16:05:31 -05006748 psli->slistat.mbox_cmd++;
6749 evtctr = psli->slistat.mbox_event;
6750
6751 /* next set own bit for the adapter and copy over command word */
6752 mb->mbxOwner = OWN_CHIP;
6753
James Smart3772a992009-05-22 14:50:54 -04006754 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
James Smart7a470272010-03-15 11:25:20 -04006755 /* Populate mbox extension offset word. */
6756 if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len) {
6757 *(((uint32_t *)mb) + pmbox->mbox_offset_word)
6758 = (uint8_t *)phba->mbox_ext
6759 - (uint8_t *)phba->mbox;
6760 }
6761
6762 /* Copy the mailbox extension data */
6763 if (pmbox->in_ext_byte_len && pmbox->context2) {
6764 lpfc_sli_pcimem_bcopy(pmbox->context2,
6765 (uint8_t *)phba->mbox_ext,
6766 pmbox->in_ext_byte_len);
6767 }
6768 /* Copy command data to host SLIM area */
James Smart34b02dc2008-08-24 21:49:55 -04006769 lpfc_sli_pcimem_bcopy(mb, phba->mbox, MAILBOX_CMD_SIZE);
dea31012005-04-17 16:05:31 -05006770 } else {
James Smart7a470272010-03-15 11:25:20 -04006771 /* Populate mbox extension offset word. */
6772 if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len)
6773 *(((uint32_t *)mb) + pmbox->mbox_offset_word)
6774 = MAILBOX_HBA_EXT_OFFSET;
6775
6776 /* Copy the mailbox extension data */
6777 if (pmbox->in_ext_byte_len && pmbox->context2) {
6778 lpfc_memcpy_to_slim(phba->MBslimaddr +
6779 MAILBOX_HBA_EXT_OFFSET,
6780 pmbox->context2, pmbox->in_ext_byte_len);
6781
6782 }
James Smart92908312006-03-07 15:04:13 -05006783 if (mb->mbxCommand == MBX_CONFIG_PORT) {
dea31012005-04-17 16:05:31 -05006784 /* copy command data into host mbox for cmpl */
James Smart34b02dc2008-08-24 21:49:55 -04006785 lpfc_sli_pcimem_bcopy(mb, phba->mbox, MAILBOX_CMD_SIZE);
dea31012005-04-17 16:05:31 -05006786 }
6787
6788 /* First copy mbox command data to HBA SLIM, skip past first
6789 word */
6790 to_slim = phba->MBslimaddr + sizeof (uint32_t);
6791 lpfc_memcpy_to_slim(to_slim, &mb->un.varWords[0],
6792 MAILBOX_CMD_SIZE - sizeof (uint32_t));
6793
6794 /* Next copy over first word, with mbxOwner set */
James Smart34b02dc2008-08-24 21:49:55 -04006795 ldata = *((uint32_t *)mb);
dea31012005-04-17 16:05:31 -05006796 to_slim = phba->MBslimaddr;
6797 writel(ldata, to_slim);
6798 readl(to_slim); /* flush */
6799
6800 if (mb->mbxCommand == MBX_CONFIG_PORT) {
6801 /* switch over to host mailbox */
James Smart3772a992009-05-22 14:50:54 -04006802 psli->sli_flag |= LPFC_SLI_ACTIVE;
dea31012005-04-17 16:05:31 -05006803 }
6804 }
6805
6806 wmb();
dea31012005-04-17 16:05:31 -05006807
6808 switch (flag) {
6809 case MBX_NOWAIT:
James Smart09372822008-01-11 01:52:54 -05006810 /* Set up reference to mailbox command */
dea31012005-04-17 16:05:31 -05006811 psli->mbox_active = pmbox;
James Smart09372822008-01-11 01:52:54 -05006812 /* Interrupt board to do it */
6813 writel(CA_MBATT, phba->CAregaddr);
6814 readl(phba->CAregaddr); /* flush */
6815 /* Don't wait for it to finish, just return */
dea31012005-04-17 16:05:31 -05006816 break;
6817
6818 case MBX_POLL:
James Smart09372822008-01-11 01:52:54 -05006819 /* Set up null reference to mailbox command */
dea31012005-04-17 16:05:31 -05006820 psli->mbox_active = NULL;
James Smart09372822008-01-11 01:52:54 -05006821 /* Interrupt board to do it */
6822 writel(CA_MBATT, phba->CAregaddr);
6823 readl(phba->CAregaddr); /* flush */
6824
James Smart3772a992009-05-22 14:50:54 -04006825 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05006826 /* First read mbox status word */
James Smart34b02dc2008-08-24 21:49:55 -04006827 word0 = *((uint32_t *)phba->mbox);
dea31012005-04-17 16:05:31 -05006828 word0 = le32_to_cpu(word0);
6829 } else {
6830 /* First read mbox status word */
James Smart9940b972011-03-11 16:06:12 -05006831 if (lpfc_readl(phba->MBslimaddr, &word0)) {
6832 spin_unlock_irqrestore(&phba->hbalock,
6833 drvr_flag);
6834 goto out_not_finished;
6835 }
dea31012005-04-17 16:05:31 -05006836 }
6837
6838 /* Read the HBA Host Attention Register */
James Smart9940b972011-03-11 16:06:12 -05006839 if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
6840 spin_unlock_irqrestore(&phba->hbalock,
6841 drvr_flag);
6842 goto out_not_finished;
6843 }
James Smarta183a152011-10-10 21:32:43 -04006844 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, pmbox) *
6845 1000) + jiffies;
James Smart09372822008-01-11 01:52:54 -05006846 i = 0;
dea31012005-04-17 16:05:31 -05006847 /* Wait for command to complete */
Jamie Wellnitz41415862006-02-28 19:25:27 -05006848 while (((word0 & OWN_CHIP) == OWN_CHIP) ||
6849 (!(ha_copy & HA_MBATT) &&
James Smart2e0fef82007-06-17 19:56:36 -05006850 (phba->link_state > LPFC_WARM_START))) {
James Smart09372822008-01-11 01:52:54 -05006851 if (time_after(jiffies, timeout)) {
dea31012005-04-17 16:05:31 -05006852 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05006853 spin_unlock_irqrestore(&phba->hbalock,
dea31012005-04-17 16:05:31 -05006854 drvr_flag);
James Smart58da1ff2008-04-07 10:15:56 -04006855 goto out_not_finished;
dea31012005-04-17 16:05:31 -05006856 }
6857
6858 /* Check if we took a mbox interrupt while we were
6859 polling */
6860 if (((word0 & OWN_CHIP) != OWN_CHIP)
6861 && (evtctr != psli->slistat.mbox_event))
6862 break;
6863
James Smart09372822008-01-11 01:52:54 -05006864 if (i++ > 10) {
6865 spin_unlock_irqrestore(&phba->hbalock,
6866 drvr_flag);
6867 msleep(1);
6868 spin_lock_irqsave(&phba->hbalock, drvr_flag);
6869 }
dea31012005-04-17 16:05:31 -05006870
James Smart3772a992009-05-22 14:50:54 -04006871 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05006872 /* First copy command data */
James Smart34b02dc2008-08-24 21:49:55 -04006873 word0 = *((uint32_t *)phba->mbox);
dea31012005-04-17 16:05:31 -05006874 word0 = le32_to_cpu(word0);
6875 if (mb->mbxCommand == MBX_CONFIG_PORT) {
6876 MAILBOX_t *slimmb;
James Smart34b02dc2008-08-24 21:49:55 -04006877 uint32_t slimword0;
dea31012005-04-17 16:05:31 -05006878 /* Check real SLIM for any errors */
6879 slimword0 = readl(phba->MBslimaddr);
6880 slimmb = (MAILBOX_t *) & slimword0;
6881 if (((slimword0 & OWN_CHIP) != OWN_CHIP)
6882 && slimmb->mbxStatus) {
6883 psli->sli_flag &=
James Smart3772a992009-05-22 14:50:54 -04006884 ~LPFC_SLI_ACTIVE;
dea31012005-04-17 16:05:31 -05006885 word0 = slimword0;
6886 }
6887 }
6888 } else {
6889 /* First copy command data */
6890 word0 = readl(phba->MBslimaddr);
6891 }
6892 /* Read the HBA Host Attention Register */
James Smart9940b972011-03-11 16:06:12 -05006893 if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
6894 spin_unlock_irqrestore(&phba->hbalock,
6895 drvr_flag);
6896 goto out_not_finished;
6897 }
dea31012005-04-17 16:05:31 -05006898 }
6899
James Smart3772a992009-05-22 14:50:54 -04006900 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05006901 /* copy results back to user */
James Smart34b02dc2008-08-24 21:49:55 -04006902 lpfc_sli_pcimem_bcopy(phba->mbox, mb, MAILBOX_CMD_SIZE);
James Smart7a470272010-03-15 11:25:20 -04006903 /* Copy the mailbox extension data */
6904 if (pmbox->out_ext_byte_len && pmbox->context2) {
6905 lpfc_sli_pcimem_bcopy(phba->mbox_ext,
6906 pmbox->context2,
6907 pmbox->out_ext_byte_len);
6908 }
dea31012005-04-17 16:05:31 -05006909 } else {
6910 /* First copy command data */
6911 lpfc_memcpy_from_slim(mb, phba->MBslimaddr,
6912 MAILBOX_CMD_SIZE);
James Smart7a470272010-03-15 11:25:20 -04006913 /* Copy the mailbox extension data */
6914 if (pmbox->out_ext_byte_len && pmbox->context2) {
6915 lpfc_memcpy_from_slim(pmbox->context2,
6916 phba->MBslimaddr +
6917 MAILBOX_HBA_EXT_OFFSET,
6918 pmbox->out_ext_byte_len);
dea31012005-04-17 16:05:31 -05006919 }
6920 }
6921
6922 writel(HA_MBATT, phba->HAregaddr);
6923 readl(phba->HAregaddr); /* flush */
6924
6925 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
6926 status = mb->mbxStatus;
6927 }
6928
James Smart2e0fef82007-06-17 19:56:36 -05006929 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
6930 return status;
James Smart58da1ff2008-04-07 10:15:56 -04006931
6932out_not_finished:
6933 if (processing_queue) {
James Smartda0436e2009-05-22 14:51:39 -04006934 pmbox->u.mb.mbxStatus = MBX_NOT_FINISHED;
James Smart58da1ff2008-04-07 10:15:56 -04006935 lpfc_mbox_cmpl_put(phba, pmbox);
6936 }
6937 return MBX_NOT_FINISHED;
dea31012005-04-17 16:05:31 -05006938}
6939
James Smarte59058c2008-08-24 21:49:00 -04006940/**
James Smartf1126682009-06-10 17:22:44 -04006941 * lpfc_sli4_async_mbox_block - Block posting SLI4 asynchronous mailbox command
6942 * @phba: Pointer to HBA context object.
6943 *
6944 * The function blocks the posting of SLI4 asynchronous mailbox commands from
6945 * the driver internal pending mailbox queue. It will then try to wait out the
6946 * possible outstanding mailbox command before return.
6947 *
6948 * Returns:
6949 * 0 - the outstanding mailbox command completed; otherwise, the wait for
6950 * the outstanding mailbox command timed out.
6951 **/
6952static int
6953lpfc_sli4_async_mbox_block(struct lpfc_hba *phba)
6954{
6955 struct lpfc_sli *psli = &phba->sli;
James Smartf1126682009-06-10 17:22:44 -04006956 int rc = 0;
James Smarta183a152011-10-10 21:32:43 -04006957 unsigned long timeout = 0;
James Smartf1126682009-06-10 17:22:44 -04006958
6959 /* Mark the asynchronous mailbox command posting as blocked */
6960 spin_lock_irq(&phba->hbalock);
6961 psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
James Smartf1126682009-06-10 17:22:44 -04006962 /* Determine how long we might wait for the active mailbox
6963 * command to be gracefully completed by firmware.
6964 */
James Smarta183a152011-10-10 21:32:43 -04006965 if (phba->sli.mbox_active)
6966 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
6967 phba->sli.mbox_active) *
6968 1000) + jiffies;
6969 spin_unlock_irq(&phba->hbalock);
6970
James Smartf1126682009-06-10 17:22:44 -04006971 /* Wait for the outstnading mailbox command to complete */
6972 while (phba->sli.mbox_active) {
6973 /* Check active mailbox complete status every 2ms */
6974 msleep(2);
6975 if (time_after(jiffies, timeout)) {
6976 /* Timeout, marked the outstanding cmd not complete */
6977 rc = 1;
6978 break;
6979 }
6980 }
6981
6982 /* Can not cleanly block async mailbox command, fails it */
6983 if (rc) {
6984 spin_lock_irq(&phba->hbalock);
6985 psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
6986 spin_unlock_irq(&phba->hbalock);
6987 }
6988 return rc;
6989}
6990
6991/**
6992 * lpfc_sli4_async_mbox_unblock - Block posting SLI4 async mailbox command
6993 * @phba: Pointer to HBA context object.
6994 *
6995 * The function unblocks and resume posting of SLI4 asynchronous mailbox
6996 * commands from the driver internal pending mailbox queue. It makes sure
6997 * that there is no outstanding mailbox command before resuming posting
6998 * asynchronous mailbox commands. If, for any reason, there is outstanding
6999 * mailbox command, it will try to wait it out before resuming asynchronous
7000 * mailbox command posting.
7001 **/
7002static void
7003lpfc_sli4_async_mbox_unblock(struct lpfc_hba *phba)
7004{
7005 struct lpfc_sli *psli = &phba->sli;
7006
7007 spin_lock_irq(&phba->hbalock);
7008 if (!(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
7009 /* Asynchronous mailbox posting is not blocked, do nothing */
7010 spin_unlock_irq(&phba->hbalock);
7011 return;
7012 }
7013
7014 /* Outstanding synchronous mailbox command is guaranteed to be done,
7015 * successful or timeout, after timing-out the outstanding mailbox
7016 * command shall always be removed, so just unblock posting async
7017 * mailbox command and resume
7018 */
7019 psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
7020 spin_unlock_irq(&phba->hbalock);
7021
7022 /* wake up worker thread to post asynchronlous mailbox command */
7023 lpfc_worker_wake_up(phba);
7024}
7025
7026/**
James Smartda0436e2009-05-22 14:51:39 -04007027 * lpfc_sli4_post_sync_mbox - Post an SLI4 mailbox to the bootstrap mailbox
7028 * @phba: Pointer to HBA context object.
7029 * @mboxq: Pointer to mailbox object.
7030 *
7031 * The function posts a mailbox to the port. The mailbox is expected
7032 * to be comletely filled in and ready for the port to operate on it.
7033 * This routine executes a synchronous completion operation on the
7034 * mailbox by polling for its completion.
7035 *
7036 * The caller must not be holding any locks when calling this routine.
7037 *
7038 * Returns:
7039 * MBX_SUCCESS - mailbox posted successfully
7040 * Any of the MBX error values.
7041 **/
7042static int
7043lpfc_sli4_post_sync_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
7044{
7045 int rc = MBX_SUCCESS;
7046 unsigned long iflag;
7047 uint32_t db_ready;
7048 uint32_t mcqe_status;
7049 uint32_t mbx_cmnd;
7050 unsigned long timeout;
7051 struct lpfc_sli *psli = &phba->sli;
7052 struct lpfc_mqe *mb = &mboxq->u.mqe;
7053 struct lpfc_bmbx_create *mbox_rgn;
7054 struct dma_address *dma_address;
7055 struct lpfc_register bmbx_reg;
7056
7057 /*
7058 * Only one mailbox can be active to the bootstrap mailbox region
7059 * at a time and there is no queueing provided.
7060 */
7061 spin_lock_irqsave(&phba->hbalock, iflag);
7062 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
7063 spin_unlock_irqrestore(&phba->hbalock, iflag);
7064 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04007065 "(%d):2532 Mailbox command x%x (x%x/x%x) "
James Smartda0436e2009-05-22 14:51:39 -04007066 "cannot issue Data: x%x x%x\n",
7067 mboxq->vport ? mboxq->vport->vpi : 0,
7068 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04007069 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7070 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04007071 psli->sli_flag, MBX_POLL);
7072 return MBXERR_ERROR;
7073 }
7074 /* The server grabs the token and owns it until release */
7075 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
7076 phba->sli.mbox_active = mboxq;
7077 spin_unlock_irqrestore(&phba->hbalock, iflag);
7078
7079 /*
7080 * Initialize the bootstrap memory region to avoid stale data areas
7081 * in the mailbox post. Then copy the caller's mailbox contents to
7082 * the bmbx mailbox region.
7083 */
7084 mbx_cmnd = bf_get(lpfc_mqe_command, mb);
7085 memset(phba->sli4_hba.bmbx.avirt, 0, sizeof(struct lpfc_bmbx_create));
7086 lpfc_sli_pcimem_bcopy(mb, phba->sli4_hba.bmbx.avirt,
7087 sizeof(struct lpfc_mqe));
7088
7089 /* Post the high mailbox dma address to the port and wait for ready. */
7090 dma_address = &phba->sli4_hba.bmbx.dma_address;
7091 writel(dma_address->addr_hi, phba->sli4_hba.BMBXregaddr);
7092
James Smarta183a152011-10-10 21:32:43 -04007093 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mboxq)
James Smartda0436e2009-05-22 14:51:39 -04007094 * 1000) + jiffies;
7095 do {
7096 bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
7097 db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
7098 if (!db_ready)
7099 msleep(2);
7100
7101 if (time_after(jiffies, timeout)) {
7102 rc = MBXERR_ERROR;
7103 goto exit;
7104 }
7105 } while (!db_ready);
7106
7107 /* Post the low mailbox dma address to the port. */
7108 writel(dma_address->addr_lo, phba->sli4_hba.BMBXregaddr);
James Smarta183a152011-10-10 21:32:43 -04007109 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mboxq)
James Smartda0436e2009-05-22 14:51:39 -04007110 * 1000) + jiffies;
7111 do {
7112 bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
7113 db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
7114 if (!db_ready)
7115 msleep(2);
7116
7117 if (time_after(jiffies, timeout)) {
7118 rc = MBXERR_ERROR;
7119 goto exit;
7120 }
7121 } while (!db_ready);
7122
7123 /*
7124 * Read the CQ to ensure the mailbox has completed.
7125 * If so, update the mailbox status so that the upper layers
7126 * can complete the request normally.
7127 */
7128 lpfc_sli_pcimem_bcopy(phba->sli4_hba.bmbx.avirt, mb,
7129 sizeof(struct lpfc_mqe));
7130 mbox_rgn = (struct lpfc_bmbx_create *) phba->sli4_hba.bmbx.avirt;
7131 lpfc_sli_pcimem_bcopy(&mbox_rgn->mcqe, &mboxq->mcqe,
7132 sizeof(struct lpfc_mcqe));
7133 mcqe_status = bf_get(lpfc_mcqe_status, &mbox_rgn->mcqe);
James Smart05580562011-05-24 11:40:48 -04007134 /*
7135 * When the CQE status indicates a failure and the mailbox status
7136 * indicates success then copy the CQE status into the mailbox status
7137 * (and prefix it with x4000).
7138 */
James Smartda0436e2009-05-22 14:51:39 -04007139 if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
James Smart05580562011-05-24 11:40:48 -04007140 if (bf_get(lpfc_mqe_status, mb) == MBX_SUCCESS)
7141 bf_set(lpfc_mqe_status, mb,
7142 (LPFC_MBX_ERROR_RANGE | mcqe_status));
James Smartda0436e2009-05-22 14:51:39 -04007143 rc = MBXERR_ERROR;
James Smartd7c47992010-06-08 18:31:54 -04007144 } else
7145 lpfc_sli4_swap_str(phba, mboxq);
James Smartda0436e2009-05-22 14:51:39 -04007146
7147 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04007148 "(%d):0356 Mailbox cmd x%x (x%x/x%x) Status x%x "
James Smartda0436e2009-05-22 14:51:39 -04007149 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x"
7150 " x%x x%x CQ: x%x x%x x%x x%x\n",
James Smarta183a152011-10-10 21:32:43 -04007151 mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
7152 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7153 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04007154 bf_get(lpfc_mqe_status, mb),
7155 mb->un.mb_words[0], mb->un.mb_words[1],
7156 mb->un.mb_words[2], mb->un.mb_words[3],
7157 mb->un.mb_words[4], mb->un.mb_words[5],
7158 mb->un.mb_words[6], mb->un.mb_words[7],
7159 mb->un.mb_words[8], mb->un.mb_words[9],
7160 mb->un.mb_words[10], mb->un.mb_words[11],
7161 mb->un.mb_words[12], mboxq->mcqe.word0,
7162 mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
7163 mboxq->mcqe.trailer);
7164exit:
7165 /* We are holding the token, no needed for lock when release */
7166 spin_lock_irqsave(&phba->hbalock, iflag);
7167 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
7168 phba->sli.mbox_active = NULL;
7169 spin_unlock_irqrestore(&phba->hbalock, iflag);
7170 return rc;
7171}
7172
7173/**
7174 * lpfc_sli_issue_mbox_s4 - Issue an SLI4 mailbox command to firmware
7175 * @phba: Pointer to HBA context object.
7176 * @pmbox: Pointer to mailbox object.
7177 * @flag: Flag indicating how the mailbox need to be processed.
7178 *
7179 * This function is called by discovery code and HBA management code to submit
7180 * a mailbox command to firmware with SLI-4 interface spec.
7181 *
7182 * Return codes the caller owns the mailbox command after the return of the
7183 * function.
7184 **/
7185static int
7186lpfc_sli_issue_mbox_s4(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
7187 uint32_t flag)
7188{
7189 struct lpfc_sli *psli = &phba->sli;
7190 unsigned long iflags;
7191 int rc;
7192
James Smartb76f2dc2011-07-22 18:37:42 -04007193 /* dump from issue mailbox command if setup */
7194 lpfc_idiag_mbxacc_dump_issue_mbox(phba, &mboxq->u.mb);
7195
James Smart8fa38512009-07-19 10:01:03 -04007196 rc = lpfc_mbox_dev_check(phba);
7197 if (unlikely(rc)) {
7198 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04007199 "(%d):2544 Mailbox command x%x (x%x/x%x) "
James Smart8fa38512009-07-19 10:01:03 -04007200 "cannot issue Data: x%x x%x\n",
7201 mboxq->vport ? mboxq->vport->vpi : 0,
7202 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04007203 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7204 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smart8fa38512009-07-19 10:01:03 -04007205 psli->sli_flag, flag);
7206 goto out_not_finished;
7207 }
7208
James Smartda0436e2009-05-22 14:51:39 -04007209 /* Detect polling mode and jump to a handler */
7210 if (!phba->sli4_hba.intr_enable) {
7211 if (flag == MBX_POLL)
7212 rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
7213 else
7214 rc = -EIO;
7215 if (rc != MBX_SUCCESS)
James Smart05580562011-05-24 11:40:48 -04007216 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
James Smartda0436e2009-05-22 14:51:39 -04007217 "(%d):2541 Mailbox command x%x "
James Smartcc459f12012-05-09 21:18:30 -04007218 "(x%x/x%x) failure: "
7219 "mqe_sta: x%x mcqe_sta: x%x/x%x "
7220 "Data: x%x x%x\n,",
James Smartda0436e2009-05-22 14:51:39 -04007221 mboxq->vport ? mboxq->vport->vpi : 0,
7222 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04007223 lpfc_sli_config_mbox_subsys_get(phba,
7224 mboxq),
7225 lpfc_sli_config_mbox_opcode_get(phba,
7226 mboxq),
James Smartcc459f12012-05-09 21:18:30 -04007227 bf_get(lpfc_mqe_status, &mboxq->u.mqe),
7228 bf_get(lpfc_mcqe_status, &mboxq->mcqe),
7229 bf_get(lpfc_mcqe_ext_status,
7230 &mboxq->mcqe),
James Smartda0436e2009-05-22 14:51:39 -04007231 psli->sli_flag, flag);
7232 return rc;
7233 } else if (flag == MBX_POLL) {
James Smartf1126682009-06-10 17:22:44 -04007234 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
7235 "(%d):2542 Try to issue mailbox command "
James Smarta183a152011-10-10 21:32:43 -04007236 "x%x (x%x/x%x) synchronously ahead of async"
James Smartf1126682009-06-10 17:22:44 -04007237 "mailbox command queue: x%x x%x\n",
James Smartda0436e2009-05-22 14:51:39 -04007238 mboxq->vport ? mboxq->vport->vpi : 0,
7239 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04007240 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7241 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04007242 psli->sli_flag, flag);
James Smartf1126682009-06-10 17:22:44 -04007243 /* Try to block the asynchronous mailbox posting */
7244 rc = lpfc_sli4_async_mbox_block(phba);
7245 if (!rc) {
7246 /* Successfully blocked, now issue sync mbox cmd */
7247 rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
7248 if (rc != MBX_SUCCESS)
James Smartcc459f12012-05-09 21:18:30 -04007249 lpfc_printf_log(phba, KERN_WARNING,
James Smarta183a152011-10-10 21:32:43 -04007250 LOG_MBOX | LOG_SLI,
James Smartcc459f12012-05-09 21:18:30 -04007251 "(%d):2597 Sync Mailbox command "
7252 "x%x (x%x/x%x) failure: "
7253 "mqe_sta: x%x mcqe_sta: x%x/x%x "
7254 "Data: x%x x%x\n,",
7255 mboxq->vport ? mboxq->vport->vpi : 0,
James Smarta183a152011-10-10 21:32:43 -04007256 mboxq->u.mb.mbxCommand,
7257 lpfc_sli_config_mbox_subsys_get(phba,
7258 mboxq),
7259 lpfc_sli_config_mbox_opcode_get(phba,
7260 mboxq),
James Smartcc459f12012-05-09 21:18:30 -04007261 bf_get(lpfc_mqe_status, &mboxq->u.mqe),
7262 bf_get(lpfc_mcqe_status, &mboxq->mcqe),
7263 bf_get(lpfc_mcqe_ext_status,
7264 &mboxq->mcqe),
James Smarta183a152011-10-10 21:32:43 -04007265 psli->sli_flag, flag);
James Smartf1126682009-06-10 17:22:44 -04007266 /* Unblock the async mailbox posting afterward */
7267 lpfc_sli4_async_mbox_unblock(phba);
7268 }
7269 return rc;
James Smartda0436e2009-05-22 14:51:39 -04007270 }
7271
7272 /* Now, interrupt mode asynchrous mailbox command */
7273 rc = lpfc_mbox_cmd_check(phba, mboxq);
7274 if (rc) {
7275 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04007276 "(%d):2543 Mailbox command x%x (x%x/x%x) "
James Smartda0436e2009-05-22 14:51:39 -04007277 "cannot issue Data: x%x x%x\n",
7278 mboxq->vport ? mboxq->vport->vpi : 0,
7279 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04007280 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7281 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04007282 psli->sli_flag, flag);
7283 goto out_not_finished;
7284 }
James Smartda0436e2009-05-22 14:51:39 -04007285
7286 /* Put the mailbox command to the driver internal FIFO */
7287 psli->slistat.mbox_busy++;
7288 spin_lock_irqsave(&phba->hbalock, iflags);
7289 lpfc_mbox_put(phba, mboxq);
7290 spin_unlock_irqrestore(&phba->hbalock, iflags);
7291 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
7292 "(%d):0354 Mbox cmd issue - Enqueue Data: "
James Smarta183a152011-10-10 21:32:43 -04007293 "x%x (x%x/x%x) x%x x%x x%x\n",
James Smartda0436e2009-05-22 14:51:39 -04007294 mboxq->vport ? mboxq->vport->vpi : 0xffffff,
7295 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
James Smarta183a152011-10-10 21:32:43 -04007296 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7297 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04007298 phba->pport->port_state,
7299 psli->sli_flag, MBX_NOWAIT);
7300 /* Wake up worker thread to transport mailbox command from head */
7301 lpfc_worker_wake_up(phba);
7302
7303 return MBX_BUSY;
7304
7305out_not_finished:
7306 return MBX_NOT_FINISHED;
7307}
7308
7309/**
7310 * lpfc_sli4_post_async_mbox - Post an SLI4 mailbox command to device
7311 * @phba: Pointer to HBA context object.
7312 *
7313 * This function is called by worker thread to send a mailbox command to
7314 * SLI4 HBA firmware.
7315 *
7316 **/
7317int
7318lpfc_sli4_post_async_mbox(struct lpfc_hba *phba)
7319{
7320 struct lpfc_sli *psli = &phba->sli;
7321 LPFC_MBOXQ_t *mboxq;
7322 int rc = MBX_SUCCESS;
7323 unsigned long iflags;
7324 struct lpfc_mqe *mqe;
7325 uint32_t mbx_cmnd;
7326
7327 /* Check interrupt mode before post async mailbox command */
7328 if (unlikely(!phba->sli4_hba.intr_enable))
7329 return MBX_NOT_FINISHED;
7330
7331 /* Check for mailbox command service token */
7332 spin_lock_irqsave(&phba->hbalock, iflags);
7333 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
7334 spin_unlock_irqrestore(&phba->hbalock, iflags);
7335 return MBX_NOT_FINISHED;
7336 }
7337 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
7338 spin_unlock_irqrestore(&phba->hbalock, iflags);
7339 return MBX_NOT_FINISHED;
7340 }
7341 if (unlikely(phba->sli.mbox_active)) {
7342 spin_unlock_irqrestore(&phba->hbalock, iflags);
7343 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7344 "0384 There is pending active mailbox cmd\n");
7345 return MBX_NOT_FINISHED;
7346 }
7347 /* Take the mailbox command service token */
7348 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
7349
7350 /* Get the next mailbox command from head of queue */
7351 mboxq = lpfc_mbox_get(phba);
7352
7353 /* If no more mailbox command waiting for post, we're done */
7354 if (!mboxq) {
7355 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
7356 spin_unlock_irqrestore(&phba->hbalock, iflags);
7357 return MBX_SUCCESS;
7358 }
7359 phba->sli.mbox_active = mboxq;
7360 spin_unlock_irqrestore(&phba->hbalock, iflags);
7361
7362 /* Check device readiness for posting mailbox command */
7363 rc = lpfc_mbox_dev_check(phba);
7364 if (unlikely(rc))
7365 /* Driver clean routine will clean up pending mailbox */
7366 goto out_not_finished;
7367
7368 /* Prepare the mbox command to be posted */
7369 mqe = &mboxq->u.mqe;
7370 mbx_cmnd = bf_get(lpfc_mqe_command, mqe);
7371
7372 /* Start timer for the mbox_tmo and log some mailbox post messages */
7373 mod_timer(&psli->mbox_tmo, (jiffies +
James Smarta183a152011-10-10 21:32:43 -04007374 (HZ * lpfc_mbox_tmo_val(phba, mboxq))));
James Smartda0436e2009-05-22 14:51:39 -04007375
7376 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04007377 "(%d):0355 Mailbox cmd x%x (x%x/x%x) issue Data: "
James Smartda0436e2009-05-22 14:51:39 -04007378 "x%x x%x\n",
7379 mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
James Smarta183a152011-10-10 21:32:43 -04007380 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7381 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04007382 phba->pport->port_state, psli->sli_flag);
7383
7384 if (mbx_cmnd != MBX_HEARTBEAT) {
7385 if (mboxq->vport) {
7386 lpfc_debugfs_disc_trc(mboxq->vport,
7387 LPFC_DISC_TRC_MBOX_VPORT,
7388 "MBOX Send vport: cmd:x%x mb:x%x x%x",
7389 mbx_cmnd, mqe->un.mb_words[0],
7390 mqe->un.mb_words[1]);
7391 } else {
7392 lpfc_debugfs_disc_trc(phba->pport,
7393 LPFC_DISC_TRC_MBOX,
7394 "MBOX Send: cmd:x%x mb:x%x x%x",
7395 mbx_cmnd, mqe->un.mb_words[0],
7396 mqe->un.mb_words[1]);
7397 }
7398 }
7399 psli->slistat.mbox_cmd++;
7400
7401 /* Post the mailbox command to the port */
7402 rc = lpfc_sli4_mq_put(phba->sli4_hba.mbx_wq, mqe);
7403 if (rc != MBX_SUCCESS) {
7404 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04007405 "(%d):2533 Mailbox command x%x (x%x/x%x) "
James Smartda0436e2009-05-22 14:51:39 -04007406 "cannot issue Data: x%x x%x\n",
7407 mboxq->vport ? mboxq->vport->vpi : 0,
7408 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04007409 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7410 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04007411 psli->sli_flag, MBX_NOWAIT);
7412 goto out_not_finished;
7413 }
7414
7415 return rc;
7416
7417out_not_finished:
7418 spin_lock_irqsave(&phba->hbalock, iflags);
James Smartd7069f02012-03-01 22:36:29 -05007419 if (phba->sli.mbox_active) {
7420 mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
7421 __lpfc_mbox_cmpl_put(phba, mboxq);
7422 /* Release the token */
7423 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
7424 phba->sli.mbox_active = NULL;
7425 }
James Smartda0436e2009-05-22 14:51:39 -04007426 spin_unlock_irqrestore(&phba->hbalock, iflags);
7427
7428 return MBX_NOT_FINISHED;
7429}
7430
7431/**
7432 * lpfc_sli_issue_mbox - Wrapper func for issuing mailbox command
7433 * @phba: Pointer to HBA context object.
7434 * @pmbox: Pointer to mailbox object.
7435 * @flag: Flag indicating how the mailbox need to be processed.
7436 *
7437 * This routine wraps the actual SLI3 or SLI4 mailbox issuing routine from
7438 * the API jump table function pointer from the lpfc_hba struct.
7439 *
7440 * Return codes the caller owns the mailbox command after the return of the
7441 * function.
7442 **/
7443int
7444lpfc_sli_issue_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox, uint32_t flag)
7445{
7446 return phba->lpfc_sli_issue_mbox(phba, pmbox, flag);
7447}
7448
7449/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03007450 * lpfc_mbox_api_table_setup - Set up mbox api function jump table
James Smartda0436e2009-05-22 14:51:39 -04007451 * @phba: The hba struct for which this call is being executed.
7452 * @dev_grp: The HBA PCI-Device group number.
7453 *
7454 * This routine sets up the mbox interface API function jump table in @phba
7455 * struct.
7456 * Returns: 0 - success, -ENODEV - failure.
7457 **/
7458int
7459lpfc_mbox_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
7460{
7461
7462 switch (dev_grp) {
7463 case LPFC_PCI_DEV_LP:
7464 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s3;
7465 phba->lpfc_sli_handle_slow_ring_event =
7466 lpfc_sli_handle_slow_ring_event_s3;
7467 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s3;
7468 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s3;
7469 phba->lpfc_sli_brdready = lpfc_sli_brdready_s3;
7470 break;
7471 case LPFC_PCI_DEV_OC:
7472 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s4;
7473 phba->lpfc_sli_handle_slow_ring_event =
7474 lpfc_sli_handle_slow_ring_event_s4;
7475 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s4;
7476 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s4;
7477 phba->lpfc_sli_brdready = lpfc_sli_brdready_s4;
7478 break;
7479 default:
7480 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7481 "1420 Invalid HBA PCI-device group: 0x%x\n",
7482 dev_grp);
7483 return -ENODEV;
7484 break;
7485 }
7486 return 0;
7487}
7488
7489/**
James Smart3621a712009-04-06 18:47:14 -04007490 * __lpfc_sli_ringtx_put - Add an iocb to the txq
James Smarte59058c2008-08-24 21:49:00 -04007491 * @phba: Pointer to HBA context object.
7492 * @pring: Pointer to driver SLI ring object.
7493 * @piocb: Pointer to address of newly added command iocb.
7494 *
7495 * This function is called with hbalock held to add a command
7496 * iocb to the txq when SLI layer cannot submit the command iocb
7497 * to the ring.
7498 **/
James Smart2a9bf3d2010-06-07 15:24:45 -04007499void
James Smart92d7f7b2007-06-17 19:56:38 -05007500__lpfc_sli_ringtx_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -05007501 struct lpfc_iocbq *piocb)
dea31012005-04-17 16:05:31 -05007502{
7503 /* Insert the caller's iocb in the txq tail for later processing. */
7504 list_add_tail(&piocb->list, &pring->txq);
7505 pring->txq_cnt++;
dea31012005-04-17 16:05:31 -05007506}
7507
James Smarte59058c2008-08-24 21:49:00 -04007508/**
James Smart3621a712009-04-06 18:47:14 -04007509 * lpfc_sli_next_iocb - Get the next iocb in the txq
James Smarte59058c2008-08-24 21:49:00 -04007510 * @phba: Pointer to HBA context object.
7511 * @pring: Pointer to driver SLI ring object.
7512 * @piocb: Pointer to address of newly added command iocb.
7513 *
7514 * This function is called with hbalock held before a new
7515 * iocb is submitted to the firmware. This function checks
7516 * txq to flush the iocbs in txq to Firmware before
7517 * submitting new iocbs to the Firmware.
7518 * If there are iocbs in the txq which need to be submitted
7519 * to firmware, lpfc_sli_next_iocb returns the first element
7520 * of the txq after dequeuing it from txq.
7521 * If there is no iocb in the txq then the function will return
7522 * *piocb and *piocb is set to NULL. Caller needs to check
7523 * *piocb to find if there are more commands in the txq.
7524 **/
dea31012005-04-17 16:05:31 -05007525static struct lpfc_iocbq *
7526lpfc_sli_next_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -05007527 struct lpfc_iocbq **piocb)
dea31012005-04-17 16:05:31 -05007528{
7529 struct lpfc_iocbq * nextiocb;
7530
7531 nextiocb = lpfc_sli_ringtx_get(phba, pring);
7532 if (!nextiocb) {
7533 nextiocb = *piocb;
7534 *piocb = NULL;
7535 }
7536
7537 return nextiocb;
7538}
7539
James Smarte59058c2008-08-24 21:49:00 -04007540/**
James Smart3772a992009-05-22 14:50:54 -04007541 * __lpfc_sli_issue_iocb_s3 - SLI3 device lockless ver of lpfc_sli_issue_iocb
James Smarte59058c2008-08-24 21:49:00 -04007542 * @phba: Pointer to HBA context object.
James Smart3772a992009-05-22 14:50:54 -04007543 * @ring_number: SLI ring number to issue iocb on.
James Smarte59058c2008-08-24 21:49:00 -04007544 * @piocb: Pointer to command iocb.
7545 * @flag: Flag indicating if this command can be put into txq.
7546 *
James Smart3772a992009-05-22 14:50:54 -04007547 * __lpfc_sli_issue_iocb_s3 is used by other functions in the driver to issue
7548 * an iocb command to an HBA with SLI-3 interface spec. If the PCI slot is
7549 * recovering from error state, if HBA is resetting or if LPFC_STOP_IOCB_EVENT
7550 * flag is turned on, the function returns IOCB_ERROR. When the link is down,
7551 * this function allows only iocbs for posting buffers. This function finds
7552 * next available slot in the command ring and posts the command to the
7553 * available slot and writes the port attention register to request HBA start
7554 * processing new iocb. If there is no slot available in the ring and
7555 * flag & SLI_IOCB_RET_IOCB is set, the new iocb is added to the txq, otherwise
7556 * the function returns IOCB_BUSY.
James Smarte59058c2008-08-24 21:49:00 -04007557 *
James Smart3772a992009-05-22 14:50:54 -04007558 * This function is called with hbalock held. The function will return success
7559 * after it successfully submit the iocb to firmware or after adding to the
7560 * txq.
James Smarte59058c2008-08-24 21:49:00 -04007561 **/
James Smart98c9ea52007-10-27 13:37:33 -04007562static int
James Smart3772a992009-05-22 14:50:54 -04007563__lpfc_sli_issue_iocb_s3(struct lpfc_hba *phba, uint32_t ring_number,
dea31012005-04-17 16:05:31 -05007564 struct lpfc_iocbq *piocb, uint32_t flag)
7565{
7566 struct lpfc_iocbq *nextiocb;
7567 IOCB_t *iocb;
James Smart3772a992009-05-22 14:50:54 -04007568 struct lpfc_sli_ring *pring = &phba->sli.ring[ring_number];
dea31012005-04-17 16:05:31 -05007569
James Smart92d7f7b2007-06-17 19:56:38 -05007570 if (piocb->iocb_cmpl && (!piocb->vport) &&
7571 (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
7572 (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
7573 lpfc_printf_log(phba, KERN_ERR,
7574 LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04007575 "1807 IOCB x%x failed. No vport\n",
James Smart92d7f7b2007-06-17 19:56:38 -05007576 piocb->iocb.ulpCommand);
7577 dump_stack();
7578 return IOCB_ERROR;
7579 }
7580
7581
Linas Vepstas8d63f372007-02-14 14:28:36 -06007582 /* If the PCI channel is in offline state, do not post iocbs. */
7583 if (unlikely(pci_channel_offline(phba->pcidev)))
7584 return IOCB_ERROR;
7585
James Smarta257bf92009-04-06 18:48:10 -04007586 /* If HBA has a deferred error attention, fail the iocb. */
7587 if (unlikely(phba->hba_flag & DEFER_ERATT))
7588 return IOCB_ERROR;
7589
dea31012005-04-17 16:05:31 -05007590 /*
7591 * We should never get an IOCB if we are in a < LINK_DOWN state
7592 */
James Smart2e0fef82007-06-17 19:56:36 -05007593 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
dea31012005-04-17 16:05:31 -05007594 return IOCB_ERROR;
7595
7596 /*
7597 * Check to see if we are blocking IOCB processing because of a
James Smart0b727fe2007-10-27 13:37:25 -04007598 * outstanding event.
dea31012005-04-17 16:05:31 -05007599 */
James Smart0b727fe2007-10-27 13:37:25 -04007600 if (unlikely(pring->flag & LPFC_STOP_IOCB_EVENT))
dea31012005-04-17 16:05:31 -05007601 goto iocb_busy;
7602
James Smart2e0fef82007-06-17 19:56:36 -05007603 if (unlikely(phba->link_state == LPFC_LINK_DOWN)) {
dea31012005-04-17 16:05:31 -05007604 /*
James Smart2680eea2007-04-25 09:52:55 -04007605 * Only CREATE_XRI, CLOSE_XRI, and QUE_RING_BUF
dea31012005-04-17 16:05:31 -05007606 * can be issued if the link is not up.
7607 */
7608 switch (piocb->iocb.ulpCommand) {
James Smart84774a42008-08-24 21:50:06 -04007609 case CMD_GEN_REQUEST64_CR:
7610 case CMD_GEN_REQUEST64_CX:
7611 if (!(phba->sli.sli_flag & LPFC_MENLO_MAINT) ||
7612 (piocb->iocb.un.genreq64.w5.hcsw.Rctl !=
James Smart6a9c52c2009-10-02 15:16:51 -04007613 FC_RCTL_DD_UNSOL_CMD) ||
James Smart84774a42008-08-24 21:50:06 -04007614 (piocb->iocb.un.genreq64.w5.hcsw.Type !=
7615 MENLO_TRANSPORT_TYPE))
7616
7617 goto iocb_busy;
7618 break;
dea31012005-04-17 16:05:31 -05007619 case CMD_QUE_RING_BUF_CN:
7620 case CMD_QUE_RING_BUF64_CN:
dea31012005-04-17 16:05:31 -05007621 /*
7622 * For IOCBs, like QUE_RING_BUF, that have no rsp ring
7623 * completion, iocb_cmpl MUST be 0.
7624 */
7625 if (piocb->iocb_cmpl)
7626 piocb->iocb_cmpl = NULL;
7627 /*FALLTHROUGH*/
7628 case CMD_CREATE_XRI_CR:
James Smart2680eea2007-04-25 09:52:55 -04007629 case CMD_CLOSE_XRI_CN:
7630 case CMD_CLOSE_XRI_CX:
dea31012005-04-17 16:05:31 -05007631 break;
7632 default:
7633 goto iocb_busy;
7634 }
7635
7636 /*
7637 * For FCP commands, we must be in a state where we can process link
7638 * attention events.
7639 */
7640 } else if (unlikely(pring->ringno == phba->sli.fcp_ring &&
James Smart92d7f7b2007-06-17 19:56:38 -05007641 !(phba->sli.sli_flag & LPFC_PROCESS_LA))) {
dea31012005-04-17 16:05:31 -05007642 goto iocb_busy;
James Smart92d7f7b2007-06-17 19:56:38 -05007643 }
dea31012005-04-17 16:05:31 -05007644
dea31012005-04-17 16:05:31 -05007645 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
7646 (nextiocb = lpfc_sli_next_iocb(phba, pring, &piocb)))
7647 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
7648
7649 if (iocb)
7650 lpfc_sli_update_ring(phba, pring);
7651 else
7652 lpfc_sli_update_full_ring(phba, pring);
7653
7654 if (!piocb)
7655 return IOCB_SUCCESS;
7656
7657 goto out_busy;
7658
7659 iocb_busy:
7660 pring->stats.iocb_cmd_delay++;
7661
7662 out_busy:
7663
7664 if (!(flag & SLI_IOCB_RET_IOCB)) {
James Smart92d7f7b2007-06-17 19:56:38 -05007665 __lpfc_sli_ringtx_put(phba, pring, piocb);
dea31012005-04-17 16:05:31 -05007666 return IOCB_SUCCESS;
7667 }
7668
7669 return IOCB_BUSY;
7670}
7671
James Smart3772a992009-05-22 14:50:54 -04007672/**
James Smart4f774512009-05-22 14:52:35 -04007673 * lpfc_sli4_bpl2sgl - Convert the bpl/bde to a sgl.
7674 * @phba: Pointer to HBA context object.
7675 * @piocb: Pointer to command iocb.
7676 * @sglq: Pointer to the scatter gather queue object.
7677 *
7678 * This routine converts the bpl or bde that is in the IOCB
7679 * to a sgl list for the sli4 hardware. The physical address
7680 * of the bpl/bde is converted back to a virtual address.
7681 * If the IOCB contains a BPL then the list of BDE's is
7682 * converted to sli4_sge's. If the IOCB contains a single
7683 * BDE then it is converted to a single sli_sge.
7684 * The IOCB is still in cpu endianess so the contents of
7685 * the bpl can be used without byte swapping.
7686 *
7687 * Returns valid XRI = Success, NO_XRI = Failure.
7688**/
7689static uint16_t
7690lpfc_sli4_bpl2sgl(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq,
7691 struct lpfc_sglq *sglq)
7692{
7693 uint16_t xritag = NO_XRI;
7694 struct ulp_bde64 *bpl = NULL;
7695 struct ulp_bde64 bde;
7696 struct sli4_sge *sgl = NULL;
James Smart1b511972011-12-13 13:23:09 -05007697 struct lpfc_dmabuf *dmabuf;
James Smart4f774512009-05-22 14:52:35 -04007698 IOCB_t *icmd;
7699 int numBdes = 0;
7700 int i = 0;
James Smart63e801c2010-11-20 23:14:19 -05007701 uint32_t offset = 0; /* accumulated offset in the sg request list */
7702 int inbound = 0; /* number of sg reply entries inbound from firmware */
James Smart4f774512009-05-22 14:52:35 -04007703
7704 if (!piocbq || !sglq)
7705 return xritag;
7706
7707 sgl = (struct sli4_sge *)sglq->sgl;
7708 icmd = &piocbq->iocb;
James Smart6b5151f2012-01-18 16:24:06 -05007709 if (icmd->ulpCommand == CMD_XMIT_BLS_RSP64_CX)
7710 return sglq->sli4_xritag;
James Smart4f774512009-05-22 14:52:35 -04007711 if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
7712 numBdes = icmd->un.genreq64.bdl.bdeSize /
7713 sizeof(struct ulp_bde64);
7714 /* The addrHigh and addrLow fields within the IOCB
7715 * have not been byteswapped yet so there is no
7716 * need to swap them back.
7717 */
James Smart1b511972011-12-13 13:23:09 -05007718 if (piocbq->context3)
7719 dmabuf = (struct lpfc_dmabuf *)piocbq->context3;
7720 else
7721 return xritag;
James Smart4f774512009-05-22 14:52:35 -04007722
James Smart1b511972011-12-13 13:23:09 -05007723 bpl = (struct ulp_bde64 *)dmabuf->virt;
James Smart4f774512009-05-22 14:52:35 -04007724 if (!bpl)
7725 return xritag;
7726
7727 for (i = 0; i < numBdes; i++) {
7728 /* Should already be byte swapped. */
James Smart28baac72010-02-12 14:42:03 -05007729 sgl->addr_hi = bpl->addrHigh;
7730 sgl->addr_lo = bpl->addrLow;
7731
James Smart05580562011-05-24 11:40:48 -04007732 sgl->word2 = le32_to_cpu(sgl->word2);
James Smart4f774512009-05-22 14:52:35 -04007733 if ((i+1) == numBdes)
7734 bf_set(lpfc_sli4_sge_last, sgl, 1);
7735 else
7736 bf_set(lpfc_sli4_sge_last, sgl, 0);
James Smart28baac72010-02-12 14:42:03 -05007737 /* swap the size field back to the cpu so we
7738 * can assign it to the sgl.
7739 */
7740 bde.tus.w = le32_to_cpu(bpl->tus.w);
7741 sgl->sge_len = cpu_to_le32(bde.tus.f.bdeSize);
James Smart63e801c2010-11-20 23:14:19 -05007742 /* The offsets in the sgl need to be accumulated
7743 * separately for the request and reply lists.
7744 * The request is always first, the reply follows.
7745 */
7746 if (piocbq->iocb.ulpCommand == CMD_GEN_REQUEST64_CR) {
7747 /* add up the reply sg entries */
7748 if (bpl->tus.f.bdeFlags == BUFF_TYPE_BDE_64I)
7749 inbound++;
7750 /* first inbound? reset the offset */
7751 if (inbound == 1)
7752 offset = 0;
7753 bf_set(lpfc_sli4_sge_offset, sgl, offset);
James Smartf9bb2da2011-10-10 21:34:11 -04007754 bf_set(lpfc_sli4_sge_type, sgl,
7755 LPFC_SGE_TYPE_DATA);
James Smart63e801c2010-11-20 23:14:19 -05007756 offset += bde.tus.f.bdeSize;
7757 }
James Smart546fc852011-03-11 16:06:29 -05007758 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart4f774512009-05-22 14:52:35 -04007759 bpl++;
7760 sgl++;
7761 }
7762 } else if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BDE_64) {
7763 /* The addrHigh and addrLow fields of the BDE have not
7764 * been byteswapped yet so they need to be swapped
7765 * before putting them in the sgl.
7766 */
7767 sgl->addr_hi =
7768 cpu_to_le32(icmd->un.genreq64.bdl.addrHigh);
7769 sgl->addr_lo =
7770 cpu_to_le32(icmd->un.genreq64.bdl.addrLow);
James Smart05580562011-05-24 11:40:48 -04007771 sgl->word2 = le32_to_cpu(sgl->word2);
James Smart4f774512009-05-22 14:52:35 -04007772 bf_set(lpfc_sli4_sge_last, sgl, 1);
7773 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart28baac72010-02-12 14:42:03 -05007774 sgl->sge_len =
7775 cpu_to_le32(icmd->un.genreq64.bdl.bdeSize);
James Smart4f774512009-05-22 14:52:35 -04007776 }
7777 return sglq->sli4_xritag;
7778}
7779
7780/**
7781 * lpfc_sli4_scmd_to_wqidx_distr - scsi command to SLI4 WQ index distribution
7782 * @phba: Pointer to HBA context object.
James Smart4f774512009-05-22 14:52:35 -04007783 *
James Smarta93ff372010-10-22 11:06:08 -04007784 * This routine performs a roundrobin SCSI command to SLI4 FCP WQ index
James Smart8fa38512009-07-19 10:01:03 -04007785 * distribution. This is called by __lpfc_sli_issue_iocb_s4() with the hbalock
7786 * held.
James Smart4f774512009-05-22 14:52:35 -04007787 *
7788 * Return: index into SLI4 fast-path FCP queue index.
7789 **/
7790static uint32_t
James Smart8fa38512009-07-19 10:01:03 -04007791lpfc_sli4_scmd_to_wqidx_distr(struct lpfc_hba *phba)
James Smart4f774512009-05-22 14:52:35 -04007792{
James Smart8fa38512009-07-19 10:01:03 -04007793 ++phba->fcp_qidx;
7794 if (phba->fcp_qidx >= phba->cfg_fcp_wq_count)
7795 phba->fcp_qidx = 0;
James Smart4f774512009-05-22 14:52:35 -04007796
James Smart8fa38512009-07-19 10:01:03 -04007797 return phba->fcp_qidx;
James Smart4f774512009-05-22 14:52:35 -04007798}
7799
7800/**
7801 * lpfc_sli_iocb2wqe - Convert the IOCB to a work queue entry.
7802 * @phba: Pointer to HBA context object.
7803 * @piocb: Pointer to command iocb.
7804 * @wqe: Pointer to the work queue entry.
7805 *
7806 * This routine converts the iocb command to its Work Queue Entry
7807 * equivalent. The wqe pointer should not have any fields set when
7808 * this routine is called because it will memcpy over them.
7809 * This routine does not set the CQ_ID or the WQEC bits in the
7810 * wqe.
7811 *
7812 * Returns: 0 = Success, IOCB_ERROR = Failure.
7813 **/
7814static int
7815lpfc_sli4_iocb2wqe(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq,
7816 union lpfc_wqe *wqe)
7817{
James Smart5ffc2662009-11-18 15:39:44 -05007818 uint32_t xmit_len = 0, total_len = 0;
James Smart4f774512009-05-22 14:52:35 -04007819 uint8_t ct = 0;
7820 uint32_t fip;
7821 uint32_t abort_tag;
7822 uint8_t command_type = ELS_COMMAND_NON_FIP;
7823 uint8_t cmnd;
7824 uint16_t xritag;
James Smartdcf2a4e2010-09-29 11:18:53 -04007825 uint16_t abrt_iotag;
7826 struct lpfc_iocbq *abrtiocbq;
James Smart4f774512009-05-22 14:52:35 -04007827 struct ulp_bde64 *bpl = NULL;
James Smartf0d9bcc2010-10-22 11:07:09 -04007828 uint32_t els_id = LPFC_ELS_ID_DEFAULT;
James Smart5ffc2662009-11-18 15:39:44 -05007829 int numBdes, i;
7830 struct ulp_bde64 bde;
James Smartc31098c2011-04-16 11:03:33 -04007831 struct lpfc_nodelist *ndlp;
James Smartff78d8f2011-12-13 13:21:35 -05007832 uint32_t *pcmd;
James Smart1b511972011-12-13 13:23:09 -05007833 uint32_t if_type;
James Smart4f774512009-05-22 14:52:35 -04007834
James Smart45ed1192009-10-02 15:17:02 -04007835 fip = phba->hba_flag & HBA_FIP_SUPPORT;
James Smart4f774512009-05-22 14:52:35 -04007836 /* The fcp commands will set command type */
James Smart0c287582009-06-10 17:22:56 -04007837 if (iocbq->iocb_flag & LPFC_IO_FCP)
James Smart4f774512009-05-22 14:52:35 -04007838 command_type = FCP_COMMAND;
James Smartc8685952009-11-18 15:39:16 -05007839 else if (fip && (iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK))
James Smart0c287582009-06-10 17:22:56 -04007840 command_type = ELS_COMMAND_FIP;
7841 else
7842 command_type = ELS_COMMAND_NON_FIP;
7843
James Smart4f774512009-05-22 14:52:35 -04007844 /* Some of the fields are in the right position already */
7845 memcpy(wqe, &iocbq->iocb, sizeof(union lpfc_wqe));
7846 abort_tag = (uint32_t) iocbq->iotag;
7847 xritag = iocbq->sli4_xritag;
James Smartf0d9bcc2010-10-22 11:07:09 -04007848 wqe->generic.wqe_com.word7 = 0; /* The ct field has moved so reset */
James Smart4f774512009-05-22 14:52:35 -04007849 /* words0-2 bpl convert bde */
7850 if (iocbq->iocb.un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
James Smart5ffc2662009-11-18 15:39:44 -05007851 numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
7852 sizeof(struct ulp_bde64);
James Smart4f774512009-05-22 14:52:35 -04007853 bpl = (struct ulp_bde64 *)
7854 ((struct lpfc_dmabuf *)iocbq->context3)->virt;
7855 if (!bpl)
7856 return IOCB_ERROR;
7857
7858 /* Should already be byte swapped. */
7859 wqe->generic.bde.addrHigh = le32_to_cpu(bpl->addrHigh);
7860 wqe->generic.bde.addrLow = le32_to_cpu(bpl->addrLow);
7861 /* swap the size field back to the cpu so we
7862 * can assign it to the sgl.
7863 */
7864 wqe->generic.bde.tus.w = le32_to_cpu(bpl->tus.w);
James Smart5ffc2662009-11-18 15:39:44 -05007865 xmit_len = wqe->generic.bde.tus.f.bdeSize;
7866 total_len = 0;
7867 for (i = 0; i < numBdes; i++) {
7868 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
7869 total_len += bde.tus.f.bdeSize;
7870 }
James Smart4f774512009-05-22 14:52:35 -04007871 } else
James Smart5ffc2662009-11-18 15:39:44 -05007872 xmit_len = iocbq->iocb.un.fcpi64.bdl.bdeSize;
James Smart4f774512009-05-22 14:52:35 -04007873
7874 iocbq->iocb.ulpIoTag = iocbq->iotag;
7875 cmnd = iocbq->iocb.ulpCommand;
7876
7877 switch (iocbq->iocb.ulpCommand) {
7878 case CMD_ELS_REQUEST64_CR:
James Smart93d1379e2012-05-09 21:19:34 -04007879 if (iocbq->iocb_flag & LPFC_IO_LIBDFC)
7880 ndlp = iocbq->context_un.ndlp;
7881 else
7882 ndlp = (struct lpfc_nodelist *)iocbq->context1;
James Smart4f774512009-05-22 14:52:35 -04007883 if (!iocbq->iocb.ulpLe) {
7884 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
7885 "2007 Only Limited Edition cmd Format"
7886 " supported 0x%x\n",
7887 iocbq->iocb.ulpCommand);
7888 return IOCB_ERROR;
7889 }
James Smartff78d8f2011-12-13 13:21:35 -05007890
James Smart5ffc2662009-11-18 15:39:44 -05007891 wqe->els_req.payload_len = xmit_len;
James Smart4f774512009-05-22 14:52:35 -04007892 /* Els_reguest64 has a TMO */
7893 bf_set(wqe_tmo, &wqe->els_req.wqe_com,
7894 iocbq->iocb.ulpTimeout);
7895 /* Need a VF for word 4 set the vf bit*/
7896 bf_set(els_req64_vf, &wqe->els_req, 0);
7897 /* And a VFID for word 12 */
7898 bf_set(els_req64_vfid, &wqe->els_req, 0);
James Smart4f774512009-05-22 14:52:35 -04007899 ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
James Smartf0d9bcc2010-10-22 11:07:09 -04007900 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
7901 iocbq->iocb.ulpContext);
7902 bf_set(wqe_ct, &wqe->els_req.wqe_com, ct);
7903 bf_set(wqe_pu, &wqe->els_req.wqe_com, 0);
James Smart4f774512009-05-22 14:52:35 -04007904 /* CCP CCPE PV PRI in word10 were set in the memcpy */
James Smartff78d8f2011-12-13 13:21:35 -05007905 if (command_type == ELS_COMMAND_FIP)
James Smartc8685952009-11-18 15:39:16 -05007906 els_id = ((iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK)
7907 >> LPFC_FIP_ELS_ID_SHIFT);
James Smartff78d8f2011-12-13 13:21:35 -05007908 pcmd = (uint32_t *) (((struct lpfc_dmabuf *)
7909 iocbq->context2)->virt);
James Smart1b511972011-12-13 13:23:09 -05007910 if_type = bf_get(lpfc_sli_intf_if_type,
7911 &phba->sli4_hba.sli_intf);
7912 if (if_type == LPFC_SLI_INTF_IF_TYPE_2) {
James Smartff78d8f2011-12-13 13:21:35 -05007913 if (pcmd && (*pcmd == ELS_CMD_FLOGI ||
James Smartcb69f7d2011-12-13 13:21:57 -05007914 *pcmd == ELS_CMD_SCR ||
James Smart6b5151f2012-01-18 16:24:06 -05007915 *pcmd == ELS_CMD_FDISC ||
James Smartbdcd2b92012-03-01 22:33:52 -05007916 *pcmd == ELS_CMD_LOGO ||
James Smartff78d8f2011-12-13 13:21:35 -05007917 *pcmd == ELS_CMD_PLOGI)) {
7918 bf_set(els_req64_sp, &wqe->els_req, 1);
7919 bf_set(els_req64_sid, &wqe->els_req,
7920 iocbq->vport->fc_myDID);
James Smart939723a2012-05-09 21:19:03 -04007921 if ((*pcmd == ELS_CMD_FLOGI) &&
7922 !(phba->fc_topology ==
7923 LPFC_TOPOLOGY_LOOP))
7924 bf_set(els_req64_sid, &wqe->els_req, 0);
James Smartff78d8f2011-12-13 13:21:35 -05007925 bf_set(wqe_ct, &wqe->els_req.wqe_com, 1);
7926 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
James Smarta7dd9c02012-05-09 21:16:50 -04007927 phba->vpi_ids[iocbq->vport->vpi]);
James Smart3ef6d242012-01-18 16:23:48 -05007928 } else if (pcmd && iocbq->context1) {
James Smartff78d8f2011-12-13 13:21:35 -05007929 bf_set(wqe_ct, &wqe->els_req.wqe_com, 0);
7930 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
7931 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
7932 }
James Smartc8685952009-11-18 15:39:16 -05007933 }
James Smart6d368e52011-05-24 11:44:12 -04007934 bf_set(wqe_temp_rpi, &wqe->els_req.wqe_com,
7935 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
James Smartf0d9bcc2010-10-22 11:07:09 -04007936 bf_set(wqe_els_id, &wqe->els_req.wqe_com, els_id);
7937 bf_set(wqe_dbde, &wqe->els_req.wqe_com, 1);
7938 bf_set(wqe_iod, &wqe->els_req.wqe_com, LPFC_WQE_IOD_READ);
7939 bf_set(wqe_qosd, &wqe->els_req.wqe_com, 1);
7940 bf_set(wqe_lenloc, &wqe->els_req.wqe_com, LPFC_WQE_LENLOC_NONE);
7941 bf_set(wqe_ebde_cnt, &wqe->els_req.wqe_com, 0);
James Smart7851fe22011-07-22 18:36:52 -04007942 break;
James Smart5ffc2662009-11-18 15:39:44 -05007943 case CMD_XMIT_SEQUENCE64_CX:
James Smartf0d9bcc2010-10-22 11:07:09 -04007944 bf_set(wqe_ctxt_tag, &wqe->xmit_sequence.wqe_com,
7945 iocbq->iocb.un.ulpWord[3]);
7946 bf_set(wqe_rcvoxid, &wqe->xmit_sequence.wqe_com,
James Smart7851fe22011-07-22 18:36:52 -04007947 iocbq->iocb.unsli3.rcvsli3.ox_id);
James Smart5ffc2662009-11-18 15:39:44 -05007948 /* The entire sequence is transmitted for this IOCB */
7949 xmit_len = total_len;
7950 cmnd = CMD_XMIT_SEQUENCE64_CR;
James Smart1b511972011-12-13 13:23:09 -05007951 if (phba->link_flag & LS_LOOPBACK_MODE)
7952 bf_set(wqe_xo, &wqe->xmit_sequence.wge_ctl, 1);
James Smart4f774512009-05-22 14:52:35 -04007953 case CMD_XMIT_SEQUENCE64_CR:
James Smartf0d9bcc2010-10-22 11:07:09 -04007954 /* word3 iocb=io_tag32 wqe=reserved */
7955 wqe->xmit_sequence.rsvd3 = 0;
James Smart4f774512009-05-22 14:52:35 -04007956 /* word4 relative_offset memcpy */
7957 /* word5 r_ctl/df_ctl memcpy */
James Smartf0d9bcc2010-10-22 11:07:09 -04007958 bf_set(wqe_pu, &wqe->xmit_sequence.wqe_com, 0);
7959 bf_set(wqe_dbde, &wqe->xmit_sequence.wqe_com, 1);
7960 bf_set(wqe_iod, &wqe->xmit_sequence.wqe_com,
7961 LPFC_WQE_IOD_WRITE);
7962 bf_set(wqe_lenloc, &wqe->xmit_sequence.wqe_com,
7963 LPFC_WQE_LENLOC_WORD12);
7964 bf_set(wqe_ebde_cnt, &wqe->xmit_sequence.wqe_com, 0);
James Smart5ffc2662009-11-18 15:39:44 -05007965 wqe->xmit_sequence.xmit_len = xmit_len;
7966 command_type = OTHER_COMMAND;
James Smart7851fe22011-07-22 18:36:52 -04007967 break;
James Smart4f774512009-05-22 14:52:35 -04007968 case CMD_XMIT_BCAST64_CN:
James Smartf0d9bcc2010-10-22 11:07:09 -04007969 /* word3 iocb=iotag32 wqe=seq_payload_len */
7970 wqe->xmit_bcast64.seq_payload_len = xmit_len;
James Smart4f774512009-05-22 14:52:35 -04007971 /* word4 iocb=rsvd wqe=rsvd */
7972 /* word5 iocb=rctl/type/df_ctl wqe=rctl/type/df_ctl memcpy */
7973 /* word6 iocb=ctxt_tag/io_tag wqe=ctxt_tag/xri */
James Smartf0d9bcc2010-10-22 11:07:09 -04007974 bf_set(wqe_ct, &wqe->xmit_bcast64.wqe_com,
James Smart4f774512009-05-22 14:52:35 -04007975 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
James Smartf0d9bcc2010-10-22 11:07:09 -04007976 bf_set(wqe_dbde, &wqe->xmit_bcast64.wqe_com, 1);
7977 bf_set(wqe_iod, &wqe->xmit_bcast64.wqe_com, LPFC_WQE_IOD_WRITE);
7978 bf_set(wqe_lenloc, &wqe->xmit_bcast64.wqe_com,
7979 LPFC_WQE_LENLOC_WORD3);
7980 bf_set(wqe_ebde_cnt, &wqe->xmit_bcast64.wqe_com, 0);
James Smart7851fe22011-07-22 18:36:52 -04007981 break;
James Smart4f774512009-05-22 14:52:35 -04007982 case CMD_FCP_IWRITE64_CR:
7983 command_type = FCP_COMMAND_DATA_OUT;
James Smartf0d9bcc2010-10-22 11:07:09 -04007984 /* word3 iocb=iotag wqe=payload_offset_len */
7985 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
7986 wqe->fcp_iwrite.payload_offset_len =
James Smart5ffc2662009-11-18 15:39:44 -05007987 xmit_len + sizeof(struct fcp_rsp);
James Smartf0d9bcc2010-10-22 11:07:09 -04007988 /* word4 iocb=parameter wqe=total_xfer_length memcpy */
7989 /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
7990 bf_set(wqe_erp, &wqe->fcp_iwrite.wqe_com,
7991 iocbq->iocb.ulpFCP2Rcvy);
7992 bf_set(wqe_lnk, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpXS);
7993 /* Always open the exchange */
7994 bf_set(wqe_xc, &wqe->fcp_iwrite.wqe_com, 0);
James Smartf0d9bcc2010-10-22 11:07:09 -04007995 bf_set(wqe_iod, &wqe->fcp_iwrite.wqe_com, LPFC_WQE_IOD_WRITE);
7996 bf_set(wqe_lenloc, &wqe->fcp_iwrite.wqe_com,
7997 LPFC_WQE_LENLOC_WORD4);
7998 bf_set(wqe_ebde_cnt, &wqe->fcp_iwrite.wqe_com, 0);
7999 bf_set(wqe_pu, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpPU);
James Smartacd68592012-01-18 16:25:09 -05008000 if (iocbq->iocb_flag & LPFC_IO_DIF) {
8001 iocbq->iocb_flag &= ~LPFC_IO_DIF;
8002 bf_set(wqe_dif, &wqe->generic.wqe_com, 1);
8003 }
8004 bf_set(wqe_dbde, &wqe->fcp_iwrite.wqe_com, 1);
James Smart7851fe22011-07-22 18:36:52 -04008005 break;
James Smartf0d9bcc2010-10-22 11:07:09 -04008006 case CMD_FCP_IREAD64_CR:
8007 /* word3 iocb=iotag wqe=payload_offset_len */
8008 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
8009 wqe->fcp_iread.payload_offset_len =
8010 xmit_len + sizeof(struct fcp_rsp);
8011 /* word4 iocb=parameter wqe=total_xfer_length memcpy */
8012 /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
8013 bf_set(wqe_erp, &wqe->fcp_iread.wqe_com,
8014 iocbq->iocb.ulpFCP2Rcvy);
8015 bf_set(wqe_lnk, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpXS);
James Smart4f774512009-05-22 14:52:35 -04008016 /* Always open the exchange */
8017 bf_set(wqe_xc, &wqe->fcp_iread.wqe_com, 0);
James Smartf0d9bcc2010-10-22 11:07:09 -04008018 bf_set(wqe_iod, &wqe->fcp_iread.wqe_com, LPFC_WQE_IOD_READ);
8019 bf_set(wqe_lenloc, &wqe->fcp_iread.wqe_com,
8020 LPFC_WQE_LENLOC_WORD4);
8021 bf_set(wqe_ebde_cnt, &wqe->fcp_iread.wqe_com, 0);
8022 bf_set(wqe_pu, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpPU);
James Smartacd68592012-01-18 16:25:09 -05008023 if (iocbq->iocb_flag & LPFC_IO_DIF) {
8024 iocbq->iocb_flag &= ~LPFC_IO_DIF;
8025 bf_set(wqe_dif, &wqe->generic.wqe_com, 1);
8026 }
8027 bf_set(wqe_dbde, &wqe->fcp_iread.wqe_com, 1);
James Smart7851fe22011-07-22 18:36:52 -04008028 break;
James Smartf1126682009-06-10 17:22:44 -04008029 case CMD_FCP_ICMND64_CR:
James Smartf0d9bcc2010-10-22 11:07:09 -04008030 /* word3 iocb=IO_TAG wqe=reserved */
8031 wqe->fcp_icmd.rsrvd3 = 0;
8032 bf_set(wqe_pu, &wqe->fcp_icmd.wqe_com, 0);
James Smartf1126682009-06-10 17:22:44 -04008033 /* Always open the exchange */
James Smartf0d9bcc2010-10-22 11:07:09 -04008034 bf_set(wqe_xc, &wqe->fcp_icmd.wqe_com, 0);
8035 bf_set(wqe_dbde, &wqe->fcp_icmd.wqe_com, 1);
8036 bf_set(wqe_iod, &wqe->fcp_icmd.wqe_com, LPFC_WQE_IOD_WRITE);
8037 bf_set(wqe_qosd, &wqe->fcp_icmd.wqe_com, 1);
8038 bf_set(wqe_lenloc, &wqe->fcp_icmd.wqe_com,
8039 LPFC_WQE_LENLOC_NONE);
8040 bf_set(wqe_ebde_cnt, &wqe->fcp_icmd.wqe_com, 0);
James Smart7851fe22011-07-22 18:36:52 -04008041 break;
James Smart4f774512009-05-22 14:52:35 -04008042 case CMD_GEN_REQUEST64_CR:
James Smart63e801c2010-11-20 23:14:19 -05008043 /* For this command calculate the xmit length of the
8044 * request bde.
8045 */
8046 xmit_len = 0;
8047 numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
8048 sizeof(struct ulp_bde64);
8049 for (i = 0; i < numBdes; i++) {
James Smart63e801c2010-11-20 23:14:19 -05008050 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
James Smart546fc852011-03-11 16:06:29 -05008051 if (bde.tus.f.bdeFlags != BUFF_TYPE_BDE_64)
8052 break;
James Smart63e801c2010-11-20 23:14:19 -05008053 xmit_len += bde.tus.f.bdeSize;
8054 }
James Smartf0d9bcc2010-10-22 11:07:09 -04008055 /* word3 iocb=IO_TAG wqe=request_payload_len */
8056 wqe->gen_req.request_payload_len = xmit_len;
8057 /* word4 iocb=parameter wqe=relative_offset memcpy */
8058 /* word5 [rctl, type, df_ctl, la] copied in memcpy */
James Smart4f774512009-05-22 14:52:35 -04008059 /* word6 context tag copied in memcpy */
8060 if (iocbq->iocb.ulpCt_h || iocbq->iocb.ulpCt_l) {
8061 ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
8062 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8063 "2015 Invalid CT %x command 0x%x\n",
8064 ct, iocbq->iocb.ulpCommand);
8065 return IOCB_ERROR;
8066 }
James Smartf0d9bcc2010-10-22 11:07:09 -04008067 bf_set(wqe_ct, &wqe->gen_req.wqe_com, 0);
8068 bf_set(wqe_tmo, &wqe->gen_req.wqe_com, iocbq->iocb.ulpTimeout);
8069 bf_set(wqe_pu, &wqe->gen_req.wqe_com, iocbq->iocb.ulpPU);
8070 bf_set(wqe_dbde, &wqe->gen_req.wqe_com, 1);
8071 bf_set(wqe_iod, &wqe->gen_req.wqe_com, LPFC_WQE_IOD_READ);
8072 bf_set(wqe_qosd, &wqe->gen_req.wqe_com, 1);
8073 bf_set(wqe_lenloc, &wqe->gen_req.wqe_com, LPFC_WQE_LENLOC_NONE);
8074 bf_set(wqe_ebde_cnt, &wqe->gen_req.wqe_com, 0);
James Smart4f774512009-05-22 14:52:35 -04008075 command_type = OTHER_COMMAND;
James Smart7851fe22011-07-22 18:36:52 -04008076 break;
James Smart4f774512009-05-22 14:52:35 -04008077 case CMD_XMIT_ELS_RSP64_CX:
James Smartc31098c2011-04-16 11:03:33 -04008078 ndlp = (struct lpfc_nodelist *)iocbq->context1;
James Smart4f774512009-05-22 14:52:35 -04008079 /* words0-2 BDE memcpy */
James Smartf0d9bcc2010-10-22 11:07:09 -04008080 /* word3 iocb=iotag32 wqe=response_payload_len */
8081 wqe->xmit_els_rsp.response_payload_len = xmit_len;
James Smart939723a2012-05-09 21:19:03 -04008082 /* word4 */
8083 wqe->xmit_els_rsp.word4 = 0;
James Smart4f774512009-05-22 14:52:35 -04008084 /* word5 iocb=rsvd wge=did */
8085 bf_set(wqe_els_did, &wqe->xmit_els_rsp.wqe_dest,
James Smart939723a2012-05-09 21:19:03 -04008086 iocbq->iocb.un.xseq64.xmit_els_remoteID);
8087
8088 if_type = bf_get(lpfc_sli_intf_if_type,
8089 &phba->sli4_hba.sli_intf);
8090 if (if_type == LPFC_SLI_INTF_IF_TYPE_2) {
8091 if (iocbq->vport->fc_flag & FC_PT2PT) {
8092 bf_set(els_rsp64_sp, &wqe->xmit_els_rsp, 1);
8093 bf_set(els_rsp64_sid, &wqe->xmit_els_rsp,
8094 iocbq->vport->fc_myDID);
8095 if (iocbq->vport->fc_myDID == Fabric_DID) {
8096 bf_set(wqe_els_did,
8097 &wqe->xmit_els_rsp.wqe_dest, 0);
8098 }
8099 }
8100 }
James Smartf0d9bcc2010-10-22 11:07:09 -04008101 bf_set(wqe_ct, &wqe->xmit_els_rsp.wqe_com,
8102 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
8103 bf_set(wqe_pu, &wqe->xmit_els_rsp.wqe_com, iocbq->iocb.ulpPU);
8104 bf_set(wqe_rcvoxid, &wqe->xmit_els_rsp.wqe_com,
James Smart7851fe22011-07-22 18:36:52 -04008105 iocbq->iocb.unsli3.rcvsli3.ox_id);
James Smart4f774512009-05-22 14:52:35 -04008106 if (!iocbq->iocb.ulpCt_h && iocbq->iocb.ulpCt_l)
James Smartf0d9bcc2010-10-22 11:07:09 -04008107 bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
James Smart6d368e52011-05-24 11:44:12 -04008108 phba->vpi_ids[iocbq->vport->vpi]);
James Smartf0d9bcc2010-10-22 11:07:09 -04008109 bf_set(wqe_dbde, &wqe->xmit_els_rsp.wqe_com, 1);
8110 bf_set(wqe_iod, &wqe->xmit_els_rsp.wqe_com, LPFC_WQE_IOD_WRITE);
8111 bf_set(wqe_qosd, &wqe->xmit_els_rsp.wqe_com, 1);
8112 bf_set(wqe_lenloc, &wqe->xmit_els_rsp.wqe_com,
8113 LPFC_WQE_LENLOC_WORD3);
8114 bf_set(wqe_ebde_cnt, &wqe->xmit_els_rsp.wqe_com, 0);
James Smart6d368e52011-05-24 11:44:12 -04008115 bf_set(wqe_rsp_temp_rpi, &wqe->xmit_els_rsp,
8116 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
James Smartff78d8f2011-12-13 13:21:35 -05008117 pcmd = (uint32_t *) (((struct lpfc_dmabuf *)
8118 iocbq->context2)->virt);
8119 if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
James Smart939723a2012-05-09 21:19:03 -04008120 bf_set(els_rsp64_sp, &wqe->xmit_els_rsp, 1);
8121 bf_set(els_rsp64_sid, &wqe->xmit_els_rsp,
James Smartff78d8f2011-12-13 13:21:35 -05008122 iocbq->vport->fc_myDID);
James Smart939723a2012-05-09 21:19:03 -04008123 bf_set(wqe_ct, &wqe->xmit_els_rsp.wqe_com, 1);
8124 bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
James Smartff78d8f2011-12-13 13:21:35 -05008125 phba->vpi_ids[phba->pport->vpi]);
8126 }
James Smart4f774512009-05-22 14:52:35 -04008127 command_type = OTHER_COMMAND;
James Smart7851fe22011-07-22 18:36:52 -04008128 break;
James Smart4f774512009-05-22 14:52:35 -04008129 case CMD_CLOSE_XRI_CN:
8130 case CMD_ABORT_XRI_CN:
8131 case CMD_ABORT_XRI_CX:
8132 /* words 0-2 memcpy should be 0 rserved */
8133 /* port will send abts */
James Smartdcf2a4e2010-09-29 11:18:53 -04008134 abrt_iotag = iocbq->iocb.un.acxri.abortContextTag;
8135 if (abrt_iotag != 0 && abrt_iotag <= phba->sli.last_iotag) {
8136 abrtiocbq = phba->sli.iocbq_lookup[abrt_iotag];
8137 fip = abrtiocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK;
8138 } else
8139 fip = 0;
8140
8141 if ((iocbq->iocb.ulpCommand == CMD_CLOSE_XRI_CN) || fip)
James Smart4f774512009-05-22 14:52:35 -04008142 /*
James Smartdcf2a4e2010-09-29 11:18:53 -04008143 * The link is down, or the command was ELS_FIP
8144 * so the fw does not need to send abts
James Smart4f774512009-05-22 14:52:35 -04008145 * on the wire.
8146 */
8147 bf_set(abort_cmd_ia, &wqe->abort_cmd, 1);
8148 else
8149 bf_set(abort_cmd_ia, &wqe->abort_cmd, 0);
8150 bf_set(abort_cmd_criteria, &wqe->abort_cmd, T_XRI_TAG);
James Smartf0d9bcc2010-10-22 11:07:09 -04008151 /* word5 iocb=CONTEXT_TAG|IO_TAG wqe=reserved */
8152 wqe->abort_cmd.rsrvd5 = 0;
8153 bf_set(wqe_ct, &wqe->abort_cmd.wqe_com,
James Smart4f774512009-05-22 14:52:35 -04008154 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
8155 abort_tag = iocbq->iocb.un.acxri.abortIoTag;
James Smart4f774512009-05-22 14:52:35 -04008156 /*
8157 * The abort handler will send us CMD_ABORT_XRI_CN or
8158 * CMD_CLOSE_XRI_CN and the fw only accepts CMD_ABORT_XRI_CX
8159 */
James Smartf0d9bcc2010-10-22 11:07:09 -04008160 bf_set(wqe_cmnd, &wqe->abort_cmd.wqe_com, CMD_ABORT_XRI_CX);
8161 bf_set(wqe_qosd, &wqe->abort_cmd.wqe_com, 1);
8162 bf_set(wqe_lenloc, &wqe->abort_cmd.wqe_com,
8163 LPFC_WQE_LENLOC_NONE);
James Smart4f774512009-05-22 14:52:35 -04008164 cmnd = CMD_ABORT_XRI_CX;
8165 command_type = OTHER_COMMAND;
8166 xritag = 0;
James Smart7851fe22011-07-22 18:36:52 -04008167 break;
James Smart6669f9b2009-10-02 15:16:45 -04008168 case CMD_XMIT_BLS_RSP64_CX:
James Smart6b5151f2012-01-18 16:24:06 -05008169 ndlp = (struct lpfc_nodelist *)iocbq->context1;
James Smart546fc852011-03-11 16:06:29 -05008170 /* As BLS ABTS RSP WQE is very different from other WQEs,
James Smart6669f9b2009-10-02 15:16:45 -04008171 * we re-construct this WQE here based on information in
8172 * iocbq from scratch.
8173 */
8174 memset(wqe, 0, sizeof(union lpfc_wqe));
James Smart5ffc2662009-11-18 15:39:44 -05008175 /* OX_ID is invariable to who sent ABTS to CT exchange */
James Smart6669f9b2009-10-02 15:16:45 -04008176 bf_set(xmit_bls_rsp64_oxid, &wqe->xmit_bls_rsp,
James Smart546fc852011-03-11 16:06:29 -05008177 bf_get(lpfc_abts_oxid, &iocbq->iocb.un.bls_rsp));
8178 if (bf_get(lpfc_abts_orig, &iocbq->iocb.un.bls_rsp) ==
James Smart5ffc2662009-11-18 15:39:44 -05008179 LPFC_ABTS_UNSOL_INT) {
8180 /* ABTS sent by initiator to CT exchange, the
8181 * RX_ID field will be filled with the newly
8182 * allocated responder XRI.
8183 */
8184 bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
8185 iocbq->sli4_xritag);
8186 } else {
8187 /* ABTS sent by responder to CT exchange, the
8188 * RX_ID field will be filled with the responder
8189 * RX_ID from ABTS.
8190 */
8191 bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
James Smart546fc852011-03-11 16:06:29 -05008192 bf_get(lpfc_abts_rxid, &iocbq->iocb.un.bls_rsp));
James Smart5ffc2662009-11-18 15:39:44 -05008193 }
James Smart6669f9b2009-10-02 15:16:45 -04008194 bf_set(xmit_bls_rsp64_seqcnthi, &wqe->xmit_bls_rsp, 0xffff);
8195 bf_set(wqe_xmit_bls_pt, &wqe->xmit_bls_rsp.wqe_dest, 0x1);
James Smart6b5151f2012-01-18 16:24:06 -05008196
8197 /* Use CT=VPI */
8198 bf_set(wqe_els_did, &wqe->xmit_bls_rsp.wqe_dest,
8199 ndlp->nlp_DID);
8200 bf_set(xmit_bls_rsp64_temprpi, &wqe->xmit_bls_rsp,
8201 iocbq->iocb.ulpContext);
8202 bf_set(wqe_ct, &wqe->xmit_bls_rsp.wqe_com, 1);
James Smart6669f9b2009-10-02 15:16:45 -04008203 bf_set(wqe_ctxt_tag, &wqe->xmit_bls_rsp.wqe_com,
James Smart6b5151f2012-01-18 16:24:06 -05008204 phba->vpi_ids[phba->pport->vpi]);
James Smartf0d9bcc2010-10-22 11:07:09 -04008205 bf_set(wqe_qosd, &wqe->xmit_bls_rsp.wqe_com, 1);
8206 bf_set(wqe_lenloc, &wqe->xmit_bls_rsp.wqe_com,
8207 LPFC_WQE_LENLOC_NONE);
James Smart6669f9b2009-10-02 15:16:45 -04008208 /* Overwrite the pre-set comnd type with OTHER_COMMAND */
8209 command_type = OTHER_COMMAND;
James Smart546fc852011-03-11 16:06:29 -05008210 if (iocbq->iocb.un.xseq64.w5.hcsw.Rctl == FC_RCTL_BA_RJT) {
8211 bf_set(xmit_bls_rsp64_rjt_vspec, &wqe->xmit_bls_rsp,
8212 bf_get(lpfc_vndr_code, &iocbq->iocb.un.bls_rsp));
8213 bf_set(xmit_bls_rsp64_rjt_expc, &wqe->xmit_bls_rsp,
8214 bf_get(lpfc_rsn_expln, &iocbq->iocb.un.bls_rsp));
8215 bf_set(xmit_bls_rsp64_rjt_rsnc, &wqe->xmit_bls_rsp,
8216 bf_get(lpfc_rsn_code, &iocbq->iocb.un.bls_rsp));
8217 }
8218
James Smart7851fe22011-07-22 18:36:52 -04008219 break;
James Smart4f774512009-05-22 14:52:35 -04008220 case CMD_XRI_ABORTED_CX:
8221 case CMD_CREATE_XRI_CR: /* Do we expect to use this? */
James Smart4f774512009-05-22 14:52:35 -04008222 case CMD_IOCB_FCP_IBIDIR64_CR: /* bidirectional xfer */
8223 case CMD_FCP_TSEND64_CX: /* Target mode send xfer-ready */
8224 case CMD_FCP_TRSP64_CX: /* Target mode rcv */
8225 case CMD_FCP_AUTO_TRSP_CX: /* Auto target rsp */
8226 default:
8227 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8228 "2014 Invalid command 0x%x\n",
8229 iocbq->iocb.ulpCommand);
8230 return IOCB_ERROR;
James Smart7851fe22011-07-22 18:36:52 -04008231 break;
James Smart4f774512009-05-22 14:52:35 -04008232 }
James Smart6d368e52011-05-24 11:44:12 -04008233
James Smartf0d9bcc2010-10-22 11:07:09 -04008234 bf_set(wqe_xri_tag, &wqe->generic.wqe_com, xritag);
8235 bf_set(wqe_reqtag, &wqe->generic.wqe_com, iocbq->iotag);
8236 wqe->generic.wqe_com.abort_tag = abort_tag;
8237 bf_set(wqe_cmd_type, &wqe->generic.wqe_com, command_type);
8238 bf_set(wqe_cmnd, &wqe->generic.wqe_com, cmnd);
8239 bf_set(wqe_class, &wqe->generic.wqe_com, iocbq->iocb.ulpClass);
8240 bf_set(wqe_cqid, &wqe->generic.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
James Smart4f774512009-05-22 14:52:35 -04008241 return 0;
8242}
8243
8244/**
8245 * __lpfc_sli_issue_iocb_s4 - SLI4 device lockless ver of lpfc_sli_issue_iocb
8246 * @phba: Pointer to HBA context object.
8247 * @ring_number: SLI ring number to issue iocb on.
8248 * @piocb: Pointer to command iocb.
8249 * @flag: Flag indicating if this command can be put into txq.
8250 *
8251 * __lpfc_sli_issue_iocb_s4 is used by other functions in the driver to issue
8252 * an iocb command to an HBA with SLI-4 interface spec.
8253 *
8254 * This function is called with hbalock held. The function will return success
8255 * after it successfully submit the iocb to firmware or after adding to the
8256 * txq.
8257 **/
8258static int
8259__lpfc_sli_issue_iocb_s4(struct lpfc_hba *phba, uint32_t ring_number,
8260 struct lpfc_iocbq *piocb, uint32_t flag)
8261{
8262 struct lpfc_sglq *sglq;
James Smart4f774512009-05-22 14:52:35 -04008263 union lpfc_wqe wqe;
8264 struct lpfc_sli_ring *pring = &phba->sli.ring[ring_number];
James Smart4f774512009-05-22 14:52:35 -04008265
8266 if (piocb->sli4_xritag == NO_XRI) {
8267 if (piocb->iocb.ulpCommand == CMD_ABORT_XRI_CN ||
James Smart6b5151f2012-01-18 16:24:06 -05008268 piocb->iocb.ulpCommand == CMD_CLOSE_XRI_CN)
James Smart4f774512009-05-22 14:52:35 -04008269 sglq = NULL;
8270 else {
James Smart2a9bf3d2010-06-07 15:24:45 -04008271 if (pring->txq_cnt) {
8272 if (!(flag & SLI_IOCB_RET_IOCB)) {
8273 __lpfc_sli_ringtx_put(phba,
8274 pring, piocb);
8275 return IOCB_SUCCESS;
8276 } else {
8277 return IOCB_BUSY;
8278 }
8279 } else {
James Smart6d368e52011-05-24 11:44:12 -04008280 sglq = __lpfc_sli_get_sglq(phba, piocb);
James Smart2a9bf3d2010-06-07 15:24:45 -04008281 if (!sglq) {
8282 if (!(flag & SLI_IOCB_RET_IOCB)) {
8283 __lpfc_sli_ringtx_put(phba,
8284 pring,
8285 piocb);
8286 return IOCB_SUCCESS;
8287 } else
8288 return IOCB_BUSY;
8289 }
8290 }
James Smart4f774512009-05-22 14:52:35 -04008291 }
8292 } else if (piocb->iocb_flag & LPFC_IO_FCP) {
James Smart6d368e52011-05-24 11:44:12 -04008293 /* These IO's already have an XRI and a mapped sgl. */
8294 sglq = NULL;
James Smart4f774512009-05-22 14:52:35 -04008295 } else {
James Smart6d368e52011-05-24 11:44:12 -04008296 /*
8297 * This is a continuation of a commandi,(CX) so this
James Smart4f774512009-05-22 14:52:35 -04008298 * sglq is on the active list
8299 */
8300 sglq = __lpfc_get_active_sglq(phba, piocb->sli4_xritag);
8301 if (!sglq)
8302 return IOCB_ERROR;
8303 }
8304
8305 if (sglq) {
James Smart6d368e52011-05-24 11:44:12 -04008306 piocb->sli4_lxritag = sglq->sli4_lxritag;
James Smart2a9bf3d2010-06-07 15:24:45 -04008307 piocb->sli4_xritag = sglq->sli4_xritag;
James Smart2a9bf3d2010-06-07 15:24:45 -04008308 if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocb, sglq))
James Smart4f774512009-05-22 14:52:35 -04008309 return IOCB_ERROR;
8310 }
8311
8312 if (lpfc_sli4_iocb2wqe(phba, piocb, &wqe))
8313 return IOCB_ERROR;
8314
James Smart341af102010-01-26 23:07:37 -05008315 if ((piocb->iocb_flag & LPFC_IO_FCP) ||
8316 (piocb->iocb_flag & LPFC_USE_FCPWQIDX)) {
James Smart5ffc2662009-11-18 15:39:44 -05008317 /*
8318 * For FCP command IOCB, get a new WQ index to distribute
8319 * WQE across the WQsr. On the other hand, for abort IOCB,
8320 * it carries the same WQ index to the original command
8321 * IOCB.
8322 */
James Smart341af102010-01-26 23:07:37 -05008323 if (piocb->iocb_flag & LPFC_IO_FCP)
James Smart5ffc2662009-11-18 15:39:44 -05008324 piocb->fcp_wqidx = lpfc_sli4_scmd_to_wqidx_distr(phba);
James Smart2e90f4b2011-12-13 13:22:37 -05008325 if (unlikely(!phba->sli4_hba.fcp_wq))
8326 return IOCB_ERROR;
James Smart5ffc2662009-11-18 15:39:44 -05008327 if (lpfc_sli4_wq_put(phba->sli4_hba.fcp_wq[piocb->fcp_wqidx],
8328 &wqe))
James Smart4f774512009-05-22 14:52:35 -04008329 return IOCB_ERROR;
8330 } else {
8331 if (lpfc_sli4_wq_put(phba->sli4_hba.els_wq, &wqe))
8332 return IOCB_ERROR;
8333 }
8334 lpfc_sli_ringtxcmpl_put(phba, pring, piocb);
8335
8336 return 0;
8337}
8338
8339/**
James Smart3772a992009-05-22 14:50:54 -04008340 * __lpfc_sli_issue_iocb - Wrapper func of lockless version for issuing iocb
8341 *
8342 * This routine wraps the actual lockless version for issusing IOCB function
8343 * pointer from the lpfc_hba struct.
8344 *
8345 * Return codes:
8346 * IOCB_ERROR - Error
8347 * IOCB_SUCCESS - Success
8348 * IOCB_BUSY - Busy
8349 **/
James Smart2a9bf3d2010-06-07 15:24:45 -04008350int
James Smart3772a992009-05-22 14:50:54 -04008351__lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
8352 struct lpfc_iocbq *piocb, uint32_t flag)
8353{
8354 return phba->__lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
8355}
8356
8357/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03008358 * lpfc_sli_api_table_setup - Set up sli api function jump table
James Smart3772a992009-05-22 14:50:54 -04008359 * @phba: The hba struct for which this call is being executed.
8360 * @dev_grp: The HBA PCI-Device group number.
8361 *
8362 * This routine sets up the SLI interface API function jump table in @phba
8363 * struct.
8364 * Returns: 0 - success, -ENODEV - failure.
8365 **/
8366int
8367lpfc_sli_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
8368{
8369
8370 switch (dev_grp) {
8371 case LPFC_PCI_DEV_LP:
8372 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s3;
8373 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s3;
8374 break;
James Smart4f774512009-05-22 14:52:35 -04008375 case LPFC_PCI_DEV_OC:
8376 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s4;
8377 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s4;
8378 break;
James Smart3772a992009-05-22 14:50:54 -04008379 default:
8380 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8381 "1419 Invalid HBA PCI-device group: 0x%x\n",
8382 dev_grp);
8383 return -ENODEV;
8384 break;
8385 }
8386 phba->lpfc_get_iocb_from_iocbq = lpfc_get_iocb_from_iocbq;
8387 return 0;
8388}
James Smart92d7f7b2007-06-17 19:56:38 -05008389
James Smarte59058c2008-08-24 21:49:00 -04008390/**
James Smart3621a712009-04-06 18:47:14 -04008391 * lpfc_sli_issue_iocb - Wrapper function for __lpfc_sli_issue_iocb
James Smarte59058c2008-08-24 21:49:00 -04008392 * @phba: Pointer to HBA context object.
8393 * @pring: Pointer to driver SLI ring object.
8394 * @piocb: Pointer to command iocb.
8395 * @flag: Flag indicating if this command can be put into txq.
8396 *
8397 * lpfc_sli_issue_iocb is a wrapper around __lpfc_sli_issue_iocb
8398 * function. This function gets the hbalock and calls
8399 * __lpfc_sli_issue_iocb function and will return the error returned
8400 * by __lpfc_sli_issue_iocb function. This wrapper is used by
8401 * functions which do not hold hbalock.
8402 **/
James Smart92d7f7b2007-06-17 19:56:38 -05008403int
James Smart3772a992009-05-22 14:50:54 -04008404lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
James Smart92d7f7b2007-06-17 19:56:38 -05008405 struct lpfc_iocbq *piocb, uint32_t flag)
8406{
8407 unsigned long iflags;
8408 int rc;
8409
8410 spin_lock_irqsave(&phba->hbalock, iflags);
James Smart3772a992009-05-22 14:50:54 -04008411 rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
James Smart92d7f7b2007-06-17 19:56:38 -05008412 spin_unlock_irqrestore(&phba->hbalock, iflags);
8413
8414 return rc;
8415}
8416
James Smarte59058c2008-08-24 21:49:00 -04008417/**
James Smart3621a712009-04-06 18:47:14 -04008418 * lpfc_extra_ring_setup - Extra ring setup function
James Smarte59058c2008-08-24 21:49:00 -04008419 * @phba: Pointer to HBA context object.
8420 *
8421 * This function is called while driver attaches with the
8422 * HBA to setup the extra ring. The extra ring is used
8423 * only when driver needs to support target mode functionality
8424 * or IP over FC functionalities.
8425 *
8426 * This function is called with no lock held.
8427 **/
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05008428static int
8429lpfc_extra_ring_setup( struct lpfc_hba *phba)
8430{
8431 struct lpfc_sli *psli;
8432 struct lpfc_sli_ring *pring;
8433
8434 psli = &phba->sli;
8435
8436 /* Adjust cmd/rsp ring iocb entries more evenly */
James Smarta4bc3372006-12-02 13:34:16 -05008437
8438 /* Take some away from the FCP ring */
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05008439 pring = &psli->ring[psli->fcp_ring];
8440 pring->numCiocb -= SLI2_IOCB_CMD_R1XTRA_ENTRIES;
8441 pring->numRiocb -= SLI2_IOCB_RSP_R1XTRA_ENTRIES;
8442 pring->numCiocb -= SLI2_IOCB_CMD_R3XTRA_ENTRIES;
8443 pring->numRiocb -= SLI2_IOCB_RSP_R3XTRA_ENTRIES;
8444
James Smarta4bc3372006-12-02 13:34:16 -05008445 /* and give them to the extra ring */
8446 pring = &psli->ring[psli->extra_ring];
8447
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05008448 pring->numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
8449 pring->numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
8450 pring->numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
8451 pring->numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
8452
8453 /* Setup default profile for this ring */
8454 pring->iotag_max = 4096;
8455 pring->num_mask = 1;
8456 pring->prt[0].profile = 0; /* Mask 0 */
James Smarta4bc3372006-12-02 13:34:16 -05008457 pring->prt[0].rctl = phba->cfg_multi_ring_rctl;
8458 pring->prt[0].type = phba->cfg_multi_ring_type;
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05008459 pring->prt[0].lpfc_sli_rcv_unsol_event = NULL;
8460 return 0;
8461}
8462
James Smartcb69f7d2011-12-13 13:21:57 -05008463/* lpfc_sli_abts_recover_port - Recover a port that failed an ABTS.
8464 * @vport: pointer to virtual port object.
8465 * @ndlp: nodelist pointer for the impacted rport.
8466 *
8467 * The driver calls this routine in response to a XRI ABORT CQE
8468 * event from the port. In this event, the driver is required to
8469 * recover its login to the rport even though its login may be valid
8470 * from the driver's perspective. The failed ABTS notice from the
8471 * port indicates the rport is not responding.
8472 */
8473static void
8474lpfc_sli_abts_recover_port(struct lpfc_vport *vport,
8475 struct lpfc_nodelist *ndlp)
8476{
8477 struct Scsi_Host *shost;
8478 struct lpfc_hba *phba;
8479 unsigned long flags = 0;
8480
8481 shost = lpfc_shost_from_vport(vport);
8482 phba = vport->phba;
8483 if (ndlp->nlp_state != NLP_STE_MAPPED_NODE) {
8484 lpfc_printf_log(phba, KERN_INFO,
8485 LOG_SLI, "3093 No rport recovery needed. "
8486 "rport in state 0x%x\n",
8487 ndlp->nlp_state);
8488 return;
8489 }
8490 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8491 "3094 Start rport recovery on shost id 0x%x "
8492 "fc_id 0x%06x vpi 0x%x rpi 0x%x state 0x%x "
8493 "flags 0x%x\n",
8494 shost->host_no, ndlp->nlp_DID,
8495 vport->vpi, ndlp->nlp_rpi, ndlp->nlp_state,
8496 ndlp->nlp_flag);
8497 /*
8498 * The rport is not responding. Don't attempt ADISC recovery.
8499 * Remove the FCP-2 flag to force a PLOGI.
8500 */
8501 spin_lock_irqsave(shost->host_lock, flags);
8502 ndlp->nlp_fcp_info &= ~NLP_FCP_2_DEVICE;
8503 spin_unlock_irqrestore(shost->host_lock, flags);
8504 lpfc_disc_state_machine(vport, ndlp, NULL,
8505 NLP_EVT_DEVICE_RECOVERY);
8506 lpfc_cancel_retry_delay_tmo(vport, ndlp);
8507 spin_lock_irqsave(shost->host_lock, flags);
8508 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
8509 spin_unlock_irqrestore(shost->host_lock, flags);
8510 lpfc_disc_start(vport);
8511}
8512
8513/* lpfc_sli_abts_err_handler - handle a failed ABTS request from an SLI3 port.
8514 * @phba: Pointer to HBA context object.
8515 * @iocbq: Pointer to iocb object.
8516 *
8517 * The async_event handler calls this routine when it receives
8518 * an ASYNC_STATUS_CN event from the port. The port generates
8519 * this event when an Abort Sequence request to an rport fails
8520 * twice in succession. The abort could be originated by the
8521 * driver or by the port. The ABTS could have been for an ELS
8522 * or FCP IO. The port only generates this event when an ABTS
8523 * fails to complete after one retry.
8524 */
8525static void
8526lpfc_sli_abts_err_handler(struct lpfc_hba *phba,
8527 struct lpfc_iocbq *iocbq)
8528{
8529 struct lpfc_nodelist *ndlp = NULL;
8530 uint16_t rpi = 0, vpi = 0;
8531 struct lpfc_vport *vport = NULL;
8532
8533 /* The rpi in the ulpContext is vport-sensitive. */
8534 vpi = iocbq->iocb.un.asyncstat.sub_ctxt_tag;
8535 rpi = iocbq->iocb.ulpContext;
8536
8537 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
8538 "3092 Port generated ABTS async event "
8539 "on vpi %d rpi %d status 0x%x\n",
8540 vpi, rpi, iocbq->iocb.ulpStatus);
8541
8542 vport = lpfc_find_vport_by_vpid(phba, vpi);
8543 if (!vport)
8544 goto err_exit;
8545 ndlp = lpfc_findnode_rpi(vport, rpi);
8546 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp))
8547 goto err_exit;
8548
8549 if (iocbq->iocb.ulpStatus == IOSTAT_LOCAL_REJECT)
8550 lpfc_sli_abts_recover_port(vport, ndlp);
8551 return;
8552
8553 err_exit:
8554 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
8555 "3095 Event Context not found, no "
8556 "action on vpi %d rpi %d status 0x%x, reason 0x%x\n",
8557 iocbq->iocb.ulpContext, iocbq->iocb.ulpStatus,
8558 vpi, rpi);
8559}
8560
8561/* lpfc_sli4_abts_err_handler - handle a failed ABTS request from an SLI4 port.
8562 * @phba: pointer to HBA context object.
8563 * @ndlp: nodelist pointer for the impacted rport.
8564 * @axri: pointer to the wcqe containing the failed exchange.
8565 *
8566 * The driver calls this routine when it receives an ABORT_XRI_FCP CQE from the
8567 * port. The port generates this event when an abort exchange request to an
8568 * rport fails twice in succession with no reply. The abort could be originated
8569 * by the driver or by the port. The ABTS could have been for an ELS or FCP IO.
8570 */
8571void
8572lpfc_sli4_abts_err_handler(struct lpfc_hba *phba,
8573 struct lpfc_nodelist *ndlp,
8574 struct sli4_wcqe_xri_aborted *axri)
8575{
8576 struct lpfc_vport *vport;
James Smart5c1db2a2012-03-01 22:34:36 -05008577 uint32_t ext_status = 0;
James Smartcb69f7d2011-12-13 13:21:57 -05008578
James Smart6b5151f2012-01-18 16:24:06 -05008579 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
James Smartcb69f7d2011-12-13 13:21:57 -05008580 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
8581 "3115 Node Context not found, driver "
8582 "ignoring abts err event\n");
James Smart6b5151f2012-01-18 16:24:06 -05008583 return;
8584 }
8585
James Smartcb69f7d2011-12-13 13:21:57 -05008586 vport = ndlp->vport;
8587 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
8588 "3116 Port generated FCP XRI ABORT event on "
James Smart5c1db2a2012-03-01 22:34:36 -05008589 "vpi %d rpi %d xri x%x status 0x%x parameter x%x\n",
James Smartcb69f7d2011-12-13 13:21:57 -05008590 ndlp->vport->vpi, ndlp->nlp_rpi,
8591 bf_get(lpfc_wcqe_xa_xri, axri),
James Smart5c1db2a2012-03-01 22:34:36 -05008592 bf_get(lpfc_wcqe_xa_status, axri),
8593 axri->parameter);
James Smartcb69f7d2011-12-13 13:21:57 -05008594
James Smart5c1db2a2012-03-01 22:34:36 -05008595 /*
8596 * Catch the ABTS protocol failure case. Older OCe FW releases returned
8597 * LOCAL_REJECT and 0 for a failed ABTS exchange and later OCe and
8598 * LPe FW releases returned LOCAL_REJECT and SEQUENCE_TIMEOUT.
8599 */
8600 ext_status = axri->parameter & WCQE_PARAM_MASK;
8601 if ((bf_get(lpfc_wcqe_xa_status, axri) == IOSTAT_LOCAL_REJECT) &&
8602 ((ext_status == IOERR_SEQUENCE_TIMEOUT) || (ext_status == 0)))
James Smartcb69f7d2011-12-13 13:21:57 -05008603 lpfc_sli_abts_recover_port(vport, ndlp);
8604}
8605
James Smarte59058c2008-08-24 21:49:00 -04008606/**
James Smart3621a712009-04-06 18:47:14 -04008607 * lpfc_sli_async_event_handler - ASYNC iocb handler function
James Smarte59058c2008-08-24 21:49:00 -04008608 * @phba: Pointer to HBA context object.
8609 * @pring: Pointer to driver SLI ring object.
8610 * @iocbq: Pointer to iocb object.
8611 *
8612 * This function is called by the slow ring event handler
8613 * function when there is an ASYNC event iocb in the ring.
8614 * This function is called with no lock held.
8615 * Currently this function handles only temperature related
8616 * ASYNC events. The function decodes the temperature sensor
8617 * event message and posts events for the management applications.
8618 **/
James Smart98c9ea52007-10-27 13:37:33 -04008619static void
James Smart57127f12007-10-27 13:37:05 -04008620lpfc_sli_async_event_handler(struct lpfc_hba * phba,
8621 struct lpfc_sli_ring * pring, struct lpfc_iocbq * iocbq)
8622{
8623 IOCB_t *icmd;
8624 uint16_t evt_code;
James Smart57127f12007-10-27 13:37:05 -04008625 struct temp_event temp_event_data;
8626 struct Scsi_Host *shost;
James Smarta257bf92009-04-06 18:48:10 -04008627 uint32_t *iocb_w;
James Smart57127f12007-10-27 13:37:05 -04008628
8629 icmd = &iocbq->iocb;
8630 evt_code = icmd->un.asyncstat.evt_code;
James Smart57127f12007-10-27 13:37:05 -04008631
James Smartcb69f7d2011-12-13 13:21:57 -05008632 switch (evt_code) {
8633 case ASYNC_TEMP_WARN:
8634 case ASYNC_TEMP_SAFE:
8635 temp_event_data.data = (uint32_t) icmd->ulpContext;
8636 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
8637 if (evt_code == ASYNC_TEMP_WARN) {
8638 temp_event_data.event_code = LPFC_THRESHOLD_TEMP;
8639 lpfc_printf_log(phba, KERN_ERR, LOG_TEMP,
8640 "0347 Adapter is very hot, please take "
8641 "corrective action. temperature : %d Celsius\n",
8642 (uint32_t) icmd->ulpContext);
8643 } else {
8644 temp_event_data.event_code = LPFC_NORMAL_TEMP;
8645 lpfc_printf_log(phba, KERN_ERR, LOG_TEMP,
8646 "0340 Adapter temperature is OK now. "
8647 "temperature : %d Celsius\n",
8648 (uint32_t) icmd->ulpContext);
8649 }
8650
8651 /* Send temperature change event to applications */
8652 shost = lpfc_shost_from_vport(phba->pport);
8653 fc_host_post_vendor_event(shost, fc_get_event_number(),
8654 sizeof(temp_event_data), (char *) &temp_event_data,
8655 LPFC_NL_VENDOR_ID);
8656 break;
8657 case ASYNC_STATUS_CN:
8658 lpfc_sli_abts_err_handler(phba, iocbq);
8659 break;
8660 default:
James Smarta257bf92009-04-06 18:48:10 -04008661 iocb_w = (uint32_t *) icmd;
James Smartcb69f7d2011-12-13 13:21:57 -05008662 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart76bb24e2007-10-27 13:38:00 -04008663 "0346 Ring %d handler: unexpected ASYNC_STATUS"
James Smarte4e74272009-07-19 10:01:38 -04008664 " evt_code 0x%x\n"
James Smarta257bf92009-04-06 18:48:10 -04008665 "W0 0x%08x W1 0x%08x W2 0x%08x W3 0x%08x\n"
8666 "W4 0x%08x W5 0x%08x W6 0x%08x W7 0x%08x\n"
8667 "W8 0x%08x W9 0x%08x W10 0x%08x W11 0x%08x\n"
8668 "W12 0x%08x W13 0x%08x W14 0x%08x W15 0x%08x\n",
James Smartcb69f7d2011-12-13 13:21:57 -05008669 pring->ringno, icmd->un.asyncstat.evt_code,
James Smarta257bf92009-04-06 18:48:10 -04008670 iocb_w[0], iocb_w[1], iocb_w[2], iocb_w[3],
8671 iocb_w[4], iocb_w[5], iocb_w[6], iocb_w[7],
8672 iocb_w[8], iocb_w[9], iocb_w[10], iocb_w[11],
8673 iocb_w[12], iocb_w[13], iocb_w[14], iocb_w[15]);
8674
James Smartcb69f7d2011-12-13 13:21:57 -05008675 break;
James Smart57127f12007-10-27 13:37:05 -04008676 }
James Smart57127f12007-10-27 13:37:05 -04008677}
8678
8679
James Smarte59058c2008-08-24 21:49:00 -04008680/**
James Smart3621a712009-04-06 18:47:14 -04008681 * lpfc_sli_setup - SLI ring setup function
James Smarte59058c2008-08-24 21:49:00 -04008682 * @phba: Pointer to HBA context object.
8683 *
8684 * lpfc_sli_setup sets up rings of the SLI interface with
8685 * number of iocbs per ring and iotags. This function is
8686 * called while driver attach to the HBA and before the
8687 * interrupts are enabled. So there is no need for locking.
8688 *
8689 * This function always returns 0.
8690 **/
dea31012005-04-17 16:05:31 -05008691int
8692lpfc_sli_setup(struct lpfc_hba *phba)
8693{
James Smarted957682007-06-17 19:56:37 -05008694 int i, totiocbsize = 0;
dea31012005-04-17 16:05:31 -05008695 struct lpfc_sli *psli = &phba->sli;
8696 struct lpfc_sli_ring *pring;
8697
8698 psli->num_rings = MAX_CONFIGURED_RINGS;
8699 psli->sli_flag = 0;
8700 psli->fcp_ring = LPFC_FCP_RING;
8701 psli->next_ring = LPFC_FCP_NEXT_RING;
James Smarta4bc3372006-12-02 13:34:16 -05008702 psli->extra_ring = LPFC_EXTRA_RING;
dea31012005-04-17 16:05:31 -05008703
James Bottomley604a3e32005-10-29 10:28:33 -05008704 psli->iocbq_lookup = NULL;
8705 psli->iocbq_lookup_len = 0;
8706 psli->last_iotag = 0;
8707
dea31012005-04-17 16:05:31 -05008708 for (i = 0; i < psli->num_rings; i++) {
8709 pring = &psli->ring[i];
8710 switch (i) {
8711 case LPFC_FCP_RING: /* ring 0 - FCP */
8712 /* numCiocb and numRiocb are used in config_port */
8713 pring->numCiocb = SLI2_IOCB_CMD_R0_ENTRIES;
8714 pring->numRiocb = SLI2_IOCB_RSP_R0_ENTRIES;
8715 pring->numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
8716 pring->numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
8717 pring->numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
8718 pring->numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
James Smarted957682007-06-17 19:56:37 -05008719 pring->sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05008720 SLI3_IOCB_CMD_SIZE :
8721 SLI2_IOCB_CMD_SIZE;
James Smarted957682007-06-17 19:56:37 -05008722 pring->sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05008723 SLI3_IOCB_RSP_SIZE :
8724 SLI2_IOCB_RSP_SIZE;
dea31012005-04-17 16:05:31 -05008725 pring->iotag_ctr = 0;
8726 pring->iotag_max =
James Smart92d7f7b2007-06-17 19:56:38 -05008727 (phba->cfg_hba_queue_depth * 2);
dea31012005-04-17 16:05:31 -05008728 pring->fast_iotag = pring->iotag_max;
8729 pring->num_mask = 0;
8730 break;
James Smarta4bc3372006-12-02 13:34:16 -05008731 case LPFC_EXTRA_RING: /* ring 1 - EXTRA */
dea31012005-04-17 16:05:31 -05008732 /* numCiocb and numRiocb are used in config_port */
8733 pring->numCiocb = SLI2_IOCB_CMD_R1_ENTRIES;
8734 pring->numRiocb = SLI2_IOCB_RSP_R1_ENTRIES;
James Smarted957682007-06-17 19:56:37 -05008735 pring->sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05008736 SLI3_IOCB_CMD_SIZE :
8737 SLI2_IOCB_CMD_SIZE;
James Smarted957682007-06-17 19:56:37 -05008738 pring->sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05008739 SLI3_IOCB_RSP_SIZE :
8740 SLI2_IOCB_RSP_SIZE;
James Smart2e0fef82007-06-17 19:56:36 -05008741 pring->iotag_max = phba->cfg_hba_queue_depth;
dea31012005-04-17 16:05:31 -05008742 pring->num_mask = 0;
8743 break;
8744 case LPFC_ELS_RING: /* ring 2 - ELS / CT */
8745 /* numCiocb and numRiocb are used in config_port */
8746 pring->numCiocb = SLI2_IOCB_CMD_R2_ENTRIES;
8747 pring->numRiocb = SLI2_IOCB_RSP_R2_ENTRIES;
James Smarted957682007-06-17 19:56:37 -05008748 pring->sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05008749 SLI3_IOCB_CMD_SIZE :
8750 SLI2_IOCB_CMD_SIZE;
James Smarted957682007-06-17 19:56:37 -05008751 pring->sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05008752 SLI3_IOCB_RSP_SIZE :
8753 SLI2_IOCB_RSP_SIZE;
dea31012005-04-17 16:05:31 -05008754 pring->fast_iotag = 0;
8755 pring->iotag_ctr = 0;
8756 pring->iotag_max = 4096;
James Smart57127f12007-10-27 13:37:05 -04008757 pring->lpfc_sli_rcv_async_status =
8758 lpfc_sli_async_event_handler;
James Smart6669f9b2009-10-02 15:16:45 -04008759 pring->num_mask = LPFC_MAX_RING_MASK;
dea31012005-04-17 16:05:31 -05008760 pring->prt[0].profile = 0; /* Mask 0 */
James Smart6a9c52c2009-10-02 15:16:51 -04008761 pring->prt[0].rctl = FC_RCTL_ELS_REQ;
8762 pring->prt[0].type = FC_TYPE_ELS;
dea31012005-04-17 16:05:31 -05008763 pring->prt[0].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05008764 lpfc_els_unsol_event;
dea31012005-04-17 16:05:31 -05008765 pring->prt[1].profile = 0; /* Mask 1 */
James Smart6a9c52c2009-10-02 15:16:51 -04008766 pring->prt[1].rctl = FC_RCTL_ELS_REP;
8767 pring->prt[1].type = FC_TYPE_ELS;
dea31012005-04-17 16:05:31 -05008768 pring->prt[1].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05008769 lpfc_els_unsol_event;
dea31012005-04-17 16:05:31 -05008770 pring->prt[2].profile = 0; /* Mask 2 */
8771 /* NameServer Inquiry */
James Smart6a9c52c2009-10-02 15:16:51 -04008772 pring->prt[2].rctl = FC_RCTL_DD_UNSOL_CTL;
dea31012005-04-17 16:05:31 -05008773 /* NameServer */
James Smart6a9c52c2009-10-02 15:16:51 -04008774 pring->prt[2].type = FC_TYPE_CT;
dea31012005-04-17 16:05:31 -05008775 pring->prt[2].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05008776 lpfc_ct_unsol_event;
dea31012005-04-17 16:05:31 -05008777 pring->prt[3].profile = 0; /* Mask 3 */
8778 /* NameServer response */
James Smart6a9c52c2009-10-02 15:16:51 -04008779 pring->prt[3].rctl = FC_RCTL_DD_SOL_CTL;
dea31012005-04-17 16:05:31 -05008780 /* NameServer */
James Smart6a9c52c2009-10-02 15:16:51 -04008781 pring->prt[3].type = FC_TYPE_CT;
dea31012005-04-17 16:05:31 -05008782 pring->prt[3].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05008783 lpfc_ct_unsol_event;
James Smart6669f9b2009-10-02 15:16:45 -04008784 /* abort unsolicited sequence */
8785 pring->prt[4].profile = 0; /* Mask 4 */
8786 pring->prt[4].rctl = FC_RCTL_BA_ABTS;
8787 pring->prt[4].type = FC_TYPE_BLS;
8788 pring->prt[4].lpfc_sli_rcv_unsol_event =
8789 lpfc_sli4_ct_abort_unsol_event;
dea31012005-04-17 16:05:31 -05008790 break;
8791 }
James Smarted957682007-06-17 19:56:37 -05008792 totiocbsize += (pring->numCiocb * pring->sizeCiocb) +
James Smart92d7f7b2007-06-17 19:56:38 -05008793 (pring->numRiocb * pring->sizeRiocb);
dea31012005-04-17 16:05:31 -05008794 }
James Smarted957682007-06-17 19:56:37 -05008795 if (totiocbsize > MAX_SLIM_IOCB_SIZE) {
dea31012005-04-17 16:05:31 -05008796 /* Too many cmd / rsp ring entries in SLI2 SLIM */
James Smarte8b62012007-08-02 11:10:09 -04008797 printk(KERN_ERR "%d:0462 Too many cmd / rsp ring entries in "
8798 "SLI2 SLIM Data: x%x x%lx\n",
8799 phba->brd_no, totiocbsize,
8800 (unsigned long) MAX_SLIM_IOCB_SIZE);
dea31012005-04-17 16:05:31 -05008801 }
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05008802 if (phba->cfg_multi_ring_support == 2)
8803 lpfc_extra_ring_setup(phba);
dea31012005-04-17 16:05:31 -05008804
8805 return 0;
8806}
8807
James Smarte59058c2008-08-24 21:49:00 -04008808/**
James Smart3621a712009-04-06 18:47:14 -04008809 * lpfc_sli_queue_setup - Queue initialization function
James Smarte59058c2008-08-24 21:49:00 -04008810 * @phba: Pointer to HBA context object.
8811 *
8812 * lpfc_sli_queue_setup sets up mailbox queues and iocb queues for each
8813 * ring. This function also initializes ring indices of each ring.
8814 * This function is called during the initialization of the SLI
8815 * interface of an HBA.
8816 * This function is called with no lock held and always returns
8817 * 1.
8818 **/
dea31012005-04-17 16:05:31 -05008819int
James Smart2e0fef82007-06-17 19:56:36 -05008820lpfc_sli_queue_setup(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05008821{
8822 struct lpfc_sli *psli;
8823 struct lpfc_sli_ring *pring;
James Bottomley604a3e32005-10-29 10:28:33 -05008824 int i;
dea31012005-04-17 16:05:31 -05008825
8826 psli = &phba->sli;
James Smart2e0fef82007-06-17 19:56:36 -05008827 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05008828 INIT_LIST_HEAD(&psli->mboxq);
James Smart92d7f7b2007-06-17 19:56:38 -05008829 INIT_LIST_HEAD(&psli->mboxq_cmpl);
dea31012005-04-17 16:05:31 -05008830 /* Initialize list headers for txq and txcmplq as double linked lists */
8831 for (i = 0; i < psli->num_rings; i++) {
8832 pring = &psli->ring[i];
8833 pring->ringno = i;
8834 pring->next_cmdidx = 0;
8835 pring->local_getidx = 0;
8836 pring->cmdidx = 0;
8837 INIT_LIST_HEAD(&pring->txq);
8838 INIT_LIST_HEAD(&pring->txcmplq);
8839 INIT_LIST_HEAD(&pring->iocb_continueq);
James Smart9c2face2008-01-11 01:53:18 -05008840 INIT_LIST_HEAD(&pring->iocb_continue_saveq);
dea31012005-04-17 16:05:31 -05008841 INIT_LIST_HEAD(&pring->postbufq);
dea31012005-04-17 16:05:31 -05008842 }
James Smart2e0fef82007-06-17 19:56:36 -05008843 spin_unlock_irq(&phba->hbalock);
8844 return 1;
dea31012005-04-17 16:05:31 -05008845}
8846
James Smarte59058c2008-08-24 21:49:00 -04008847/**
James Smart04c68492009-05-22 14:52:52 -04008848 * lpfc_sli_mbox_sys_flush - Flush mailbox command sub-system
8849 * @phba: Pointer to HBA context object.
8850 *
8851 * This routine flushes the mailbox command subsystem. It will unconditionally
8852 * flush all the mailbox commands in the three possible stages in the mailbox
8853 * command sub-system: pending mailbox command queue; the outstanding mailbox
8854 * command; and completed mailbox command queue. It is caller's responsibility
8855 * to make sure that the driver is in the proper state to flush the mailbox
8856 * command sub-system. Namely, the posting of mailbox commands into the
8857 * pending mailbox command queue from the various clients must be stopped;
8858 * either the HBA is in a state that it will never works on the outstanding
8859 * mailbox command (such as in EEH or ERATT conditions) or the outstanding
8860 * mailbox command has been completed.
8861 **/
8862static void
8863lpfc_sli_mbox_sys_flush(struct lpfc_hba *phba)
8864{
8865 LIST_HEAD(completions);
8866 struct lpfc_sli *psli = &phba->sli;
8867 LPFC_MBOXQ_t *pmb;
8868 unsigned long iflag;
8869
8870 /* Flush all the mailbox commands in the mbox system */
8871 spin_lock_irqsave(&phba->hbalock, iflag);
8872 /* The pending mailbox command queue */
8873 list_splice_init(&phba->sli.mboxq, &completions);
8874 /* The outstanding active mailbox command */
8875 if (psli->mbox_active) {
8876 list_add_tail(&psli->mbox_active->list, &completions);
8877 psli->mbox_active = NULL;
8878 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
8879 }
8880 /* The completed mailbox command queue */
8881 list_splice_init(&phba->sli.mboxq_cmpl, &completions);
8882 spin_unlock_irqrestore(&phba->hbalock, iflag);
8883
8884 /* Return all flushed mailbox commands with MBX_NOT_FINISHED status */
8885 while (!list_empty(&completions)) {
8886 list_remove_head(&completions, pmb, LPFC_MBOXQ_t, list);
8887 pmb->u.mb.mbxStatus = MBX_NOT_FINISHED;
8888 if (pmb->mbox_cmpl)
8889 pmb->mbox_cmpl(phba, pmb);
8890 }
8891}
8892
8893/**
James Smart3621a712009-04-06 18:47:14 -04008894 * lpfc_sli_host_down - Vport cleanup function
James Smarte59058c2008-08-24 21:49:00 -04008895 * @vport: Pointer to virtual port object.
8896 *
8897 * lpfc_sli_host_down is called to clean up the resources
8898 * associated with a vport before destroying virtual
8899 * port data structures.
8900 * This function does following operations:
8901 * - Free discovery resources associated with this virtual
8902 * port.
8903 * - Free iocbs associated with this virtual port in
8904 * the txq.
8905 * - Send abort for all iocb commands associated with this
8906 * vport in txcmplq.
8907 *
8908 * This function is called with no lock held and always returns 1.
8909 **/
dea31012005-04-17 16:05:31 -05008910int
James Smart92d7f7b2007-06-17 19:56:38 -05008911lpfc_sli_host_down(struct lpfc_vport *vport)
8912{
James Smart858c9f62007-06-17 19:56:39 -05008913 LIST_HEAD(completions);
James Smart92d7f7b2007-06-17 19:56:38 -05008914 struct lpfc_hba *phba = vport->phba;
8915 struct lpfc_sli *psli = &phba->sli;
8916 struct lpfc_sli_ring *pring;
8917 struct lpfc_iocbq *iocb, *next_iocb;
James Smart92d7f7b2007-06-17 19:56:38 -05008918 int i;
8919 unsigned long flags = 0;
8920 uint16_t prev_pring_flag;
8921
8922 lpfc_cleanup_discovery_resources(vport);
8923
8924 spin_lock_irqsave(&phba->hbalock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -05008925 for (i = 0; i < psli->num_rings; i++) {
8926 pring = &psli->ring[i];
8927 prev_pring_flag = pring->flag;
James Smart5e9d9b82008-06-14 22:52:53 -04008928 /* Only slow rings */
8929 if (pring->ringno == LPFC_ELS_RING) {
James Smart858c9f62007-06-17 19:56:39 -05008930 pring->flag |= LPFC_DEFERRED_RING_EVENT;
James Smart5e9d9b82008-06-14 22:52:53 -04008931 /* Set the lpfc data pending flag */
8932 set_bit(LPFC_DATA_READY, &phba->data_flags);
8933 }
James Smart92d7f7b2007-06-17 19:56:38 -05008934 /*
8935 * Error everything on the txq since these iocbs have not been
8936 * given to the FW yet.
8937 */
James Smart92d7f7b2007-06-17 19:56:38 -05008938 list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
8939 if (iocb->vport != vport)
8940 continue;
James Smart858c9f62007-06-17 19:56:39 -05008941 list_move_tail(&iocb->list, &completions);
James Smart92d7f7b2007-06-17 19:56:38 -05008942 pring->txq_cnt--;
James Smart92d7f7b2007-06-17 19:56:38 -05008943 }
8944
8945 /* Next issue ABTS for everything on the txcmplq */
8946 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq,
8947 list) {
8948 if (iocb->vport != vport)
8949 continue;
8950 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
8951 }
8952
8953 pring->flag = prev_pring_flag;
8954 }
8955
8956 spin_unlock_irqrestore(&phba->hbalock, flags);
8957
James Smarta257bf92009-04-06 18:48:10 -04008958 /* Cancel all the IOCBs from the completions list */
8959 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
8960 IOERR_SLI_DOWN);
James Smart92d7f7b2007-06-17 19:56:38 -05008961 return 1;
8962}
8963
James Smarte59058c2008-08-24 21:49:00 -04008964/**
James Smart3621a712009-04-06 18:47:14 -04008965 * lpfc_sli_hba_down - Resource cleanup function for the HBA
James Smarte59058c2008-08-24 21:49:00 -04008966 * @phba: Pointer to HBA context object.
8967 *
8968 * This function cleans up all iocb, buffers, mailbox commands
8969 * while shutting down the HBA. This function is called with no
8970 * lock held and always returns 1.
8971 * This function does the following to cleanup driver resources:
8972 * - Free discovery resources for each virtual port
8973 * - Cleanup any pending fabric iocbs
8974 * - Iterate through the iocb txq and free each entry
8975 * in the list.
8976 * - Free up any buffer posted to the HBA
8977 * - Free mailbox commands in the mailbox queue.
8978 **/
James Smart92d7f7b2007-06-17 19:56:38 -05008979int
James Smart2e0fef82007-06-17 19:56:36 -05008980lpfc_sli_hba_down(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05008981{
James Smart2534ba72007-04-25 09:52:20 -04008982 LIST_HEAD(completions);
James Smart2e0fef82007-06-17 19:56:36 -05008983 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05008984 struct lpfc_sli_ring *pring;
James Smart0ff10d42008-01-11 01:52:36 -05008985 struct lpfc_dmabuf *buf_ptr;
dea31012005-04-17 16:05:31 -05008986 unsigned long flags = 0;
James Smart04c68492009-05-22 14:52:52 -04008987 int i;
8988
8989 /* Shutdown the mailbox command sub-system */
James Smart618a5232012-06-12 13:54:36 -04008990 lpfc_sli_mbox_sys_shutdown(phba, LPFC_MBX_WAIT);
dea31012005-04-17 16:05:31 -05008991
dea31012005-04-17 16:05:31 -05008992 lpfc_hba_down_prep(phba);
8993
James Smart92d7f7b2007-06-17 19:56:38 -05008994 lpfc_fabric_abort_hba(phba);
8995
James Smart2e0fef82007-06-17 19:56:36 -05008996 spin_lock_irqsave(&phba->hbalock, flags);
dea31012005-04-17 16:05:31 -05008997 for (i = 0; i < psli->num_rings; i++) {
8998 pring = &psli->ring[i];
James Smart5e9d9b82008-06-14 22:52:53 -04008999 /* Only slow rings */
9000 if (pring->ringno == LPFC_ELS_RING) {
James Smart858c9f62007-06-17 19:56:39 -05009001 pring->flag |= LPFC_DEFERRED_RING_EVENT;
James Smart5e9d9b82008-06-14 22:52:53 -04009002 /* Set the lpfc data pending flag */
9003 set_bit(LPFC_DATA_READY, &phba->data_flags);
9004 }
dea31012005-04-17 16:05:31 -05009005
9006 /*
9007 * Error everything on the txq since these iocbs have not been
9008 * given to the FW yet.
9009 */
James Smart2534ba72007-04-25 09:52:20 -04009010 list_splice_init(&pring->txq, &completions);
dea31012005-04-17 16:05:31 -05009011 pring->txq_cnt = 0;
9012
dea31012005-04-17 16:05:31 -05009013 }
James Smart2e0fef82007-06-17 19:56:36 -05009014 spin_unlock_irqrestore(&phba->hbalock, flags);
dea31012005-04-17 16:05:31 -05009015
James Smarta257bf92009-04-06 18:48:10 -04009016 /* Cancel all the IOCBs from the completions list */
9017 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
9018 IOERR_SLI_DOWN);
James Smart2534ba72007-04-25 09:52:20 -04009019
James Smart0ff10d42008-01-11 01:52:36 -05009020 spin_lock_irqsave(&phba->hbalock, flags);
9021 list_splice_init(&phba->elsbuf, &completions);
9022 phba->elsbuf_cnt = 0;
9023 phba->elsbuf_prev_cnt = 0;
9024 spin_unlock_irqrestore(&phba->hbalock, flags);
9025
9026 while (!list_empty(&completions)) {
9027 list_remove_head(&completions, buf_ptr,
9028 struct lpfc_dmabuf, list);
9029 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
9030 kfree(buf_ptr);
9031 }
9032
dea31012005-04-17 16:05:31 -05009033 /* Return any active mbox cmds */
9034 del_timer_sync(&psli->mbox_tmo);
James Smart92d7f7b2007-06-17 19:56:38 -05009035
James Smartda0436e2009-05-22 14:51:39 -04009036 spin_lock_irqsave(&phba->pport->work_port_lock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -05009037 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
James Smartda0436e2009-05-22 14:51:39 -04009038 spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -05009039
James Smartda0436e2009-05-22 14:51:39 -04009040 return 1;
9041}
James Smart92d7f7b2007-06-17 19:56:38 -05009042
James Smartda0436e2009-05-22 14:51:39 -04009043/**
James Smart3621a712009-04-06 18:47:14 -04009044 * lpfc_sli_pcimem_bcopy - SLI memory copy function
James Smarte59058c2008-08-24 21:49:00 -04009045 * @srcp: Source memory pointer.
9046 * @destp: Destination memory pointer.
9047 * @cnt: Number of words required to be copied.
9048 *
9049 * This function is used for copying data between driver memory
9050 * and the SLI memory. This function also changes the endianness
9051 * of each word if native endianness is different from SLI
9052 * endianness. This function can be called with or without
9053 * lock.
9054 **/
dea31012005-04-17 16:05:31 -05009055void
9056lpfc_sli_pcimem_bcopy(void *srcp, void *destp, uint32_t cnt)
9057{
9058 uint32_t *src = srcp;
9059 uint32_t *dest = destp;
9060 uint32_t ldata;
9061 int i;
9062
9063 for (i = 0; i < (int)cnt; i += sizeof (uint32_t)) {
9064 ldata = *src;
9065 ldata = le32_to_cpu(ldata);
9066 *dest = ldata;
9067 src++;
9068 dest++;
9069 }
9070}
9071
James Smarte59058c2008-08-24 21:49:00 -04009072
9073/**
James Smarta0c87cb2009-07-19 10:01:10 -04009074 * lpfc_sli_bemem_bcopy - SLI memory copy function
9075 * @srcp: Source memory pointer.
9076 * @destp: Destination memory pointer.
9077 * @cnt: Number of words required to be copied.
9078 *
9079 * This function is used for copying data between a data structure
9080 * with big endian representation to local endianness.
9081 * This function can be called with or without lock.
9082 **/
9083void
9084lpfc_sli_bemem_bcopy(void *srcp, void *destp, uint32_t cnt)
9085{
9086 uint32_t *src = srcp;
9087 uint32_t *dest = destp;
9088 uint32_t ldata;
9089 int i;
9090
9091 for (i = 0; i < (int)cnt; i += sizeof(uint32_t)) {
9092 ldata = *src;
9093 ldata = be32_to_cpu(ldata);
9094 *dest = ldata;
9095 src++;
9096 dest++;
9097 }
9098}
9099
9100/**
James Smart3621a712009-04-06 18:47:14 -04009101 * lpfc_sli_ringpostbuf_put - Function to add a buffer to postbufq
James Smarte59058c2008-08-24 21:49:00 -04009102 * @phba: Pointer to HBA context object.
9103 * @pring: Pointer to driver SLI ring object.
9104 * @mp: Pointer to driver buffer object.
9105 *
9106 * This function is called with no lock held.
9107 * It always return zero after adding the buffer to the postbufq
9108 * buffer list.
9109 **/
dea31012005-04-17 16:05:31 -05009110int
James Smart2e0fef82007-06-17 19:56:36 -05009111lpfc_sli_ringpostbuf_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
9112 struct lpfc_dmabuf *mp)
dea31012005-04-17 16:05:31 -05009113{
9114 /* Stick struct lpfc_dmabuf at end of postbufq so driver can look it up
9115 later */
James Smart2e0fef82007-06-17 19:56:36 -05009116 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05009117 list_add_tail(&mp->list, &pring->postbufq);
dea31012005-04-17 16:05:31 -05009118 pring->postbufq_cnt++;
James Smart2e0fef82007-06-17 19:56:36 -05009119 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05009120 return 0;
9121}
9122
James Smarte59058c2008-08-24 21:49:00 -04009123/**
James Smart3621a712009-04-06 18:47:14 -04009124 * lpfc_sli_get_buffer_tag - allocates a tag for a CMD_QUE_XRI64_CX buffer
James Smarte59058c2008-08-24 21:49:00 -04009125 * @phba: Pointer to HBA context object.
9126 *
9127 * When HBQ is enabled, buffers are searched based on tags. This function
9128 * allocates a tag for buffer posted using CMD_QUE_XRI64_CX iocb. The
9129 * tag is bit wise or-ed with QUE_BUFTAG_BIT to make sure that the tag
9130 * does not conflict with tags of buffer posted for unsolicited events.
9131 * The function returns the allocated tag. The function is called with
9132 * no locks held.
9133 **/
James Smart76bb24e2007-10-27 13:38:00 -04009134uint32_t
9135lpfc_sli_get_buffer_tag(struct lpfc_hba *phba)
9136{
9137 spin_lock_irq(&phba->hbalock);
9138 phba->buffer_tag_count++;
9139 /*
9140 * Always set the QUE_BUFTAG_BIT to distiguish between
9141 * a tag assigned by HBQ.
9142 */
9143 phba->buffer_tag_count |= QUE_BUFTAG_BIT;
9144 spin_unlock_irq(&phba->hbalock);
9145 return phba->buffer_tag_count;
9146}
9147
James Smarte59058c2008-08-24 21:49:00 -04009148/**
James Smart3621a712009-04-06 18:47:14 -04009149 * lpfc_sli_ring_taggedbuf_get - find HBQ buffer associated with given tag
James Smarte59058c2008-08-24 21:49:00 -04009150 * @phba: Pointer to HBA context object.
9151 * @pring: Pointer to driver SLI ring object.
9152 * @tag: Buffer tag.
9153 *
9154 * Buffers posted using CMD_QUE_XRI64_CX iocb are in pring->postbufq
9155 * list. After HBA DMA data to these buffers, CMD_IOCB_RET_XRI64_CX
9156 * iocb is posted to the response ring with the tag of the buffer.
9157 * This function searches the pring->postbufq list using the tag
9158 * to find buffer associated with CMD_IOCB_RET_XRI64_CX
9159 * iocb. If the buffer is found then lpfc_dmabuf object of the
9160 * buffer is returned to the caller else NULL is returned.
9161 * This function is called with no lock held.
9162 **/
James Smart76bb24e2007-10-27 13:38:00 -04009163struct lpfc_dmabuf *
9164lpfc_sli_ring_taggedbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
9165 uint32_t tag)
9166{
9167 struct lpfc_dmabuf *mp, *next_mp;
9168 struct list_head *slp = &pring->postbufq;
9169
Lucas De Marchi25985ed2011-03-30 22:57:33 -03009170 /* Search postbufq, from the beginning, looking for a match on tag */
James Smart76bb24e2007-10-27 13:38:00 -04009171 spin_lock_irq(&phba->hbalock);
9172 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
9173 if (mp->buffer_tag == tag) {
9174 list_del_init(&mp->list);
9175 pring->postbufq_cnt--;
9176 spin_unlock_irq(&phba->hbalock);
9177 return mp;
9178 }
9179 }
9180
9181 spin_unlock_irq(&phba->hbalock);
9182 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartd7c255b2008-08-24 21:50:00 -04009183 "0402 Cannot find virtual addr for buffer tag on "
James Smart76bb24e2007-10-27 13:38:00 -04009184 "ring %d Data x%lx x%p x%p x%x\n",
9185 pring->ringno, (unsigned long) tag,
9186 slp->next, slp->prev, pring->postbufq_cnt);
9187
9188 return NULL;
9189}
dea31012005-04-17 16:05:31 -05009190
James Smarte59058c2008-08-24 21:49:00 -04009191/**
James Smart3621a712009-04-06 18:47:14 -04009192 * lpfc_sli_ringpostbuf_get - search buffers for unsolicited CT and ELS events
James Smarte59058c2008-08-24 21:49:00 -04009193 * @phba: Pointer to HBA context object.
9194 * @pring: Pointer to driver SLI ring object.
9195 * @phys: DMA address of the buffer.
9196 *
9197 * This function searches the buffer list using the dma_address
9198 * of unsolicited event to find the driver's lpfc_dmabuf object
9199 * corresponding to the dma_address. The function returns the
9200 * lpfc_dmabuf object if a buffer is found else it returns NULL.
9201 * This function is called by the ct and els unsolicited event
9202 * handlers to get the buffer associated with the unsolicited
9203 * event.
9204 *
9205 * This function is called with no lock held.
9206 **/
dea31012005-04-17 16:05:31 -05009207struct lpfc_dmabuf *
9208lpfc_sli_ringpostbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
9209 dma_addr_t phys)
9210{
9211 struct lpfc_dmabuf *mp, *next_mp;
9212 struct list_head *slp = &pring->postbufq;
9213
Lucas De Marchi25985ed2011-03-30 22:57:33 -03009214 /* Search postbufq, from the beginning, looking for a match on phys */
James Smart2e0fef82007-06-17 19:56:36 -05009215 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05009216 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
9217 if (mp->phys == phys) {
9218 list_del_init(&mp->list);
9219 pring->postbufq_cnt--;
James Smart2e0fef82007-06-17 19:56:36 -05009220 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05009221 return mp;
9222 }
9223 }
9224
James Smart2e0fef82007-06-17 19:56:36 -05009225 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05009226 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04009227 "0410 Cannot find virtual addr for mapped buf on "
dea31012005-04-17 16:05:31 -05009228 "ring %d Data x%llx x%p x%p x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04009229 pring->ringno, (unsigned long long)phys,
dea31012005-04-17 16:05:31 -05009230 slp->next, slp->prev, pring->postbufq_cnt);
9231 return NULL;
9232}
9233
James Smarte59058c2008-08-24 21:49:00 -04009234/**
James Smart3621a712009-04-06 18:47:14 -04009235 * lpfc_sli_abort_els_cmpl - Completion handler for the els abort iocbs
James Smarte59058c2008-08-24 21:49:00 -04009236 * @phba: Pointer to HBA context object.
9237 * @cmdiocb: Pointer to driver command iocb object.
9238 * @rspiocb: Pointer to driver response iocb object.
9239 *
9240 * This function is the completion handler for the abort iocbs for
9241 * ELS commands. This function is called from the ELS ring event
9242 * handler with no lock held. This function frees memory resources
9243 * associated with the abort iocb.
9244 **/
dea31012005-04-17 16:05:31 -05009245static void
James Smart2e0fef82007-06-17 19:56:36 -05009246lpfc_sli_abort_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
9247 struct lpfc_iocbq *rspiocb)
dea31012005-04-17 16:05:31 -05009248{
James Smart2e0fef82007-06-17 19:56:36 -05009249 IOCB_t *irsp = &rspiocb->iocb;
James Smart2680eea2007-04-25 09:52:55 -04009250 uint16_t abort_iotag, abort_context;
James Smartff78d8f2011-12-13 13:21:35 -05009251 struct lpfc_iocbq *abort_iocb = NULL;
James Smart2680eea2007-04-25 09:52:55 -04009252
9253 if (irsp->ulpStatus) {
James Smartff78d8f2011-12-13 13:21:35 -05009254
9255 /*
9256 * Assume that the port already completed and returned, or
9257 * will return the iocb. Just Log the message.
9258 */
James Smart2680eea2007-04-25 09:52:55 -04009259 abort_context = cmdiocb->iocb.un.acxri.abortContextTag;
9260 abort_iotag = cmdiocb->iocb.un.acxri.abortIoTag;
9261
James Smart2e0fef82007-06-17 19:56:36 -05009262 spin_lock_irq(&phba->hbalock);
James Smart45ed1192009-10-02 15:17:02 -04009263 if (phba->sli_rev < LPFC_SLI_REV4) {
9264 if (abort_iotag != 0 &&
9265 abort_iotag <= phba->sli.last_iotag)
9266 abort_iocb =
9267 phba->sli.iocbq_lookup[abort_iotag];
9268 } else
9269 /* For sli4 the abort_tag is the XRI,
9270 * so the abort routine puts the iotag of the iocb
9271 * being aborted in the context field of the abort
9272 * IOCB.
9273 */
9274 abort_iocb = phba->sli.iocbq_lookup[abort_context];
James Smart2680eea2007-04-25 09:52:55 -04009275
James Smart2a9bf3d2010-06-07 15:24:45 -04009276 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS | LOG_SLI,
9277 "0327 Cannot abort els iocb %p "
9278 "with tag %x context %x, abort status %x, "
9279 "abort code %x\n",
9280 abort_iocb, abort_iotag, abort_context,
9281 irsp->ulpStatus, irsp->un.ulpWord[4]);
James Smart2680eea2007-04-25 09:52:55 -04009282
James Smartff78d8f2011-12-13 13:21:35 -05009283 spin_unlock_irq(&phba->hbalock);
James Smart2680eea2007-04-25 09:52:55 -04009284 }
James Bottomley604a3e32005-10-29 10:28:33 -05009285 lpfc_sli_release_iocbq(phba, cmdiocb);
dea31012005-04-17 16:05:31 -05009286 return;
9287}
9288
James Smarte59058c2008-08-24 21:49:00 -04009289/**
James Smart3621a712009-04-06 18:47:14 -04009290 * lpfc_ignore_els_cmpl - Completion handler for aborted ELS command
James Smarte59058c2008-08-24 21:49:00 -04009291 * @phba: Pointer to HBA context object.
9292 * @cmdiocb: Pointer to driver command iocb object.
9293 * @rspiocb: Pointer to driver response iocb object.
9294 *
9295 * The function is called from SLI ring event handler with no
9296 * lock held. This function is the completion handler for ELS commands
9297 * which are aborted. The function frees memory resources used for
9298 * the aborted ELS commands.
9299 **/
James Smart92d7f7b2007-06-17 19:56:38 -05009300static void
9301lpfc_ignore_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
9302 struct lpfc_iocbq *rspiocb)
9303{
9304 IOCB_t *irsp = &rspiocb->iocb;
9305
9306 /* ELS cmd tag <ulpIoTag> completes */
9307 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
James Smartd7c255b2008-08-24 21:50:00 -04009308 "0139 Ignoring ELS cmd tag x%x completion Data: "
James Smart92d7f7b2007-06-17 19:56:38 -05009309 "x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04009310 irsp->ulpIoTag, irsp->ulpStatus,
James Smart92d7f7b2007-06-17 19:56:38 -05009311 irsp->un.ulpWord[4], irsp->ulpTimeout);
James Smart858c9f62007-06-17 19:56:39 -05009312 if (cmdiocb->iocb.ulpCommand == CMD_GEN_REQUEST64_CR)
9313 lpfc_ct_free_iocb(phba, cmdiocb);
9314 else
9315 lpfc_els_free_iocb(phba, cmdiocb);
James Smart92d7f7b2007-06-17 19:56:38 -05009316 return;
9317}
9318
James Smarte59058c2008-08-24 21:49:00 -04009319/**
James Smart5af5eee2010-10-22 11:06:38 -04009320 * lpfc_sli_abort_iotag_issue - Issue abort for a command iocb
James Smarte59058c2008-08-24 21:49:00 -04009321 * @phba: Pointer to HBA context object.
9322 * @pring: Pointer to driver SLI ring object.
9323 * @cmdiocb: Pointer to driver command iocb object.
9324 *
James Smart5af5eee2010-10-22 11:06:38 -04009325 * This function issues an abort iocb for the provided command iocb down to
9326 * the port. Other than the case the outstanding command iocb is an abort
9327 * request, this function issues abort out unconditionally. This function is
9328 * called with hbalock held. The function returns 0 when it fails due to
9329 * memory allocation failure or when the command iocb is an abort request.
James Smarte59058c2008-08-24 21:49:00 -04009330 **/
James Smart5af5eee2010-10-22 11:06:38 -04009331static int
9332lpfc_sli_abort_iotag_issue(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -05009333 struct lpfc_iocbq *cmdiocb)
dea31012005-04-17 16:05:31 -05009334{
James Smart2e0fef82007-06-17 19:56:36 -05009335 struct lpfc_vport *vport = cmdiocb->vport;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009336 struct lpfc_iocbq *abtsiocbp;
dea31012005-04-17 16:05:31 -05009337 IOCB_t *icmd = NULL;
9338 IOCB_t *iabt = NULL;
James Smart5af5eee2010-10-22 11:06:38 -04009339 int retval;
James Smart07951072007-04-25 09:51:38 -04009340
James Smart92d7f7b2007-06-17 19:56:38 -05009341 /*
9342 * There are certain command types we don't want to abort. And we
9343 * don't want to abort commands that are already in the process of
9344 * being aborted.
James Smart07951072007-04-25 09:51:38 -04009345 */
9346 icmd = &cmdiocb->iocb;
James Smart2e0fef82007-06-17 19:56:36 -05009347 if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
James Smart92d7f7b2007-06-17 19:56:38 -05009348 icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
9349 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
James Smart07951072007-04-25 09:51:38 -04009350 return 0;
9351
dea31012005-04-17 16:05:31 -05009352 /* issue ABTS for this IOCB based on iotag */
James Smart92d7f7b2007-06-17 19:56:38 -05009353 abtsiocbp = __lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05009354 if (abtsiocbp == NULL)
9355 return 0;
dea31012005-04-17 16:05:31 -05009356
James Smart07951072007-04-25 09:51:38 -04009357 /* This signals the response to set the correct status
James Smart341af102010-01-26 23:07:37 -05009358 * before calling the completion handler
James Smart07951072007-04-25 09:51:38 -04009359 */
9360 cmdiocb->iocb_flag |= LPFC_DRIVER_ABORTED;
9361
dea31012005-04-17 16:05:31 -05009362 iabt = &abtsiocbp->iocb;
James Smart07951072007-04-25 09:51:38 -04009363 iabt->un.acxri.abortType = ABORT_TYPE_ABTS;
9364 iabt->un.acxri.abortContextTag = icmd->ulpContext;
James Smart45ed1192009-10-02 15:17:02 -04009365 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smartda0436e2009-05-22 14:51:39 -04009366 iabt->un.acxri.abortIoTag = cmdiocb->sli4_xritag;
James Smart45ed1192009-10-02 15:17:02 -04009367 iabt->un.acxri.abortContextTag = cmdiocb->iotag;
9368 }
James Smartda0436e2009-05-22 14:51:39 -04009369 else
9370 iabt->un.acxri.abortIoTag = icmd->ulpIoTag;
dea31012005-04-17 16:05:31 -05009371 iabt->ulpLe = 1;
James Smart07951072007-04-25 09:51:38 -04009372 iabt->ulpClass = icmd->ulpClass;
dea31012005-04-17 16:05:31 -05009373
James Smart5ffc2662009-11-18 15:39:44 -05009374 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
9375 abtsiocbp->fcp_wqidx = cmdiocb->fcp_wqidx;
James Smart341af102010-01-26 23:07:37 -05009376 if (cmdiocb->iocb_flag & LPFC_IO_FCP)
9377 abtsiocbp->iocb_flag |= LPFC_USE_FCPWQIDX;
James Smart5ffc2662009-11-18 15:39:44 -05009378
James Smart2e0fef82007-06-17 19:56:36 -05009379 if (phba->link_state >= LPFC_LINK_UP)
James Smart07951072007-04-25 09:51:38 -04009380 iabt->ulpCommand = CMD_ABORT_XRI_CN;
9381 else
9382 iabt->ulpCommand = CMD_CLOSE_XRI_CN;
9383
9384 abtsiocbp->iocb_cmpl = lpfc_sli_abort_els_cmpl;
James Smart5b8bd0c2007-04-25 09:52:49 -04009385
James Smarte8b62012007-08-02 11:10:09 -04009386 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
9387 "0339 Abort xri x%x, original iotag x%x, "
9388 "abort cmd iotag x%x\n",
James Smart2a9bf3d2010-06-07 15:24:45 -04009389 iabt->un.acxri.abortIoTag,
James Smarte8b62012007-08-02 11:10:09 -04009390 iabt->un.acxri.abortContextTag,
James Smart2a9bf3d2010-06-07 15:24:45 -04009391 abtsiocbp->iotag);
James Smartda0436e2009-05-22 14:51:39 -04009392 retval = __lpfc_sli_issue_iocb(phba, pring->ringno, abtsiocbp, 0);
James Smart07951072007-04-25 09:51:38 -04009393
James Smartd7c255b2008-08-24 21:50:00 -04009394 if (retval)
9395 __lpfc_sli_release_iocbq(phba, abtsiocbp);
James Smart5af5eee2010-10-22 11:06:38 -04009396
9397 /*
9398 * Caller to this routine should check for IOCB_ERROR
9399 * and handle it properly. This routine no longer removes
9400 * iocb off txcmplq and call compl in case of IOCB_ERROR.
9401 */
9402 return retval;
9403}
9404
9405/**
9406 * lpfc_sli_issue_abort_iotag - Abort function for a command iocb
9407 * @phba: Pointer to HBA context object.
9408 * @pring: Pointer to driver SLI ring object.
9409 * @cmdiocb: Pointer to driver command iocb object.
9410 *
9411 * This function issues an abort iocb for the provided command iocb. In case
9412 * of unloading, the abort iocb will not be issued to commands on the ELS
9413 * ring. Instead, the callback function shall be changed to those commands
9414 * so that nothing happens when them finishes. This function is called with
9415 * hbalock held. The function returns 0 when the command iocb is an abort
9416 * request.
9417 **/
9418int
9419lpfc_sli_issue_abort_iotag(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
9420 struct lpfc_iocbq *cmdiocb)
9421{
9422 struct lpfc_vport *vport = cmdiocb->vport;
9423 int retval = IOCB_ERROR;
9424 IOCB_t *icmd = NULL;
9425
9426 /*
9427 * There are certain command types we don't want to abort. And we
9428 * don't want to abort commands that are already in the process of
9429 * being aborted.
9430 */
9431 icmd = &cmdiocb->iocb;
9432 if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
9433 icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
9434 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
9435 return 0;
9436
9437 /*
9438 * If we're unloading, don't abort iocb on the ELS ring, but change
9439 * the callback so that nothing happens when it finishes.
9440 */
9441 if ((vport->load_flag & FC_UNLOADING) &&
9442 (pring->ringno == LPFC_ELS_RING)) {
9443 if (cmdiocb->iocb_flag & LPFC_IO_FABRIC)
9444 cmdiocb->fabric_iocb_cmpl = lpfc_ignore_els_cmpl;
9445 else
9446 cmdiocb->iocb_cmpl = lpfc_ignore_els_cmpl;
9447 goto abort_iotag_exit;
9448 }
9449
9450 /* Now, we try to issue the abort to the cmdiocb out */
9451 retval = lpfc_sli_abort_iotag_issue(phba, pring, cmdiocb);
9452
James Smart07951072007-04-25 09:51:38 -04009453abort_iotag_exit:
James Smart2e0fef82007-06-17 19:56:36 -05009454 /*
9455 * Caller to this routine should check for IOCB_ERROR
9456 * and handle it properly. This routine no longer removes
9457 * iocb off txcmplq and call compl in case of IOCB_ERROR.
James Smart07951072007-04-25 09:51:38 -04009458 */
James Smart2e0fef82007-06-17 19:56:36 -05009459 return retval;
dea31012005-04-17 16:05:31 -05009460}
9461
James Smarte59058c2008-08-24 21:49:00 -04009462/**
James Smart5af5eee2010-10-22 11:06:38 -04009463 * lpfc_sli_iocb_ring_abort - Unconditionally abort all iocbs on an iocb ring
9464 * @phba: Pointer to HBA context object.
9465 * @pring: Pointer to driver SLI ring object.
9466 *
9467 * This function aborts all iocbs in the given ring and frees all the iocb
9468 * objects in txq. This function issues abort iocbs unconditionally for all
9469 * the iocb commands in txcmplq. The iocbs in the txcmplq is not guaranteed
9470 * to complete before the return of this function. The caller is not required
9471 * to hold any locks.
9472 **/
9473static void
9474lpfc_sli_iocb_ring_abort(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
9475{
9476 LIST_HEAD(completions);
9477 struct lpfc_iocbq *iocb, *next_iocb;
9478
9479 if (pring->ringno == LPFC_ELS_RING)
9480 lpfc_fabric_abort_hba(phba);
9481
9482 spin_lock_irq(&phba->hbalock);
9483
9484 /* Take off all the iocbs on txq for cancelling */
9485 list_splice_init(&pring->txq, &completions);
9486 pring->txq_cnt = 0;
9487
9488 /* Next issue ABTS for everything on the txcmplq */
9489 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
9490 lpfc_sli_abort_iotag_issue(phba, pring, iocb);
9491
9492 spin_unlock_irq(&phba->hbalock);
9493
9494 /* Cancel all the IOCBs from the completions list */
9495 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
9496 IOERR_SLI_ABORTED);
9497}
9498
9499/**
9500 * lpfc_sli_hba_iocb_abort - Abort all iocbs to an hba.
9501 * @phba: pointer to lpfc HBA data structure.
9502 *
9503 * This routine will abort all pending and outstanding iocbs to an HBA.
9504 **/
9505void
9506lpfc_sli_hba_iocb_abort(struct lpfc_hba *phba)
9507{
9508 struct lpfc_sli *psli = &phba->sli;
9509 struct lpfc_sli_ring *pring;
9510 int i;
9511
9512 for (i = 0; i < psli->num_rings; i++) {
9513 pring = &psli->ring[i];
9514 lpfc_sli_iocb_ring_abort(phba, pring);
9515 }
9516}
9517
9518/**
James Smart3621a712009-04-06 18:47:14 -04009519 * lpfc_sli_validate_fcp_iocb - find commands associated with a vport or LUN
James Smarte59058c2008-08-24 21:49:00 -04009520 * @iocbq: Pointer to driver iocb object.
9521 * @vport: Pointer to driver virtual port object.
9522 * @tgt_id: SCSI ID of the target.
9523 * @lun_id: LUN ID of the scsi device.
9524 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST
9525 *
James Smart3621a712009-04-06 18:47:14 -04009526 * This function acts as an iocb filter for functions which abort or count
James Smarte59058c2008-08-24 21:49:00 -04009527 * all FCP iocbs pending on a lun/SCSI target/SCSI host. It will return
9528 * 0 if the filtering criteria is met for the given iocb and will return
9529 * 1 if the filtering criteria is not met.
9530 * If ctx_cmd == LPFC_CTX_LUN, the function returns 0 only if the
9531 * given iocb is for the SCSI device specified by vport, tgt_id and
9532 * lun_id parameter.
9533 * If ctx_cmd == LPFC_CTX_TGT, the function returns 0 only if the
9534 * given iocb is for the SCSI target specified by vport and tgt_id
9535 * parameters.
9536 * If ctx_cmd == LPFC_CTX_HOST, the function returns 0 only if the
9537 * given iocb is for the SCSI host associated with the given vport.
9538 * This function is called with no locks held.
9539 **/
dea31012005-04-17 16:05:31 -05009540static int
James Smart51ef4c22007-08-02 11:10:31 -04009541lpfc_sli_validate_fcp_iocb(struct lpfc_iocbq *iocbq, struct lpfc_vport *vport,
9542 uint16_t tgt_id, uint64_t lun_id,
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009543 lpfc_ctx_cmd ctx_cmd)
dea31012005-04-17 16:05:31 -05009544{
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009545 struct lpfc_scsi_buf *lpfc_cmd;
dea31012005-04-17 16:05:31 -05009546 int rc = 1;
9547
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009548 if (!(iocbq->iocb_flag & LPFC_IO_FCP))
9549 return rc;
9550
James Smart51ef4c22007-08-02 11:10:31 -04009551 if (iocbq->vport != vport)
9552 return rc;
9553
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009554 lpfc_cmd = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009555
James Smart495a7142008-06-14 22:52:59 -04009556 if (lpfc_cmd->pCmd == NULL)
dea31012005-04-17 16:05:31 -05009557 return rc;
9558
9559 switch (ctx_cmd) {
9560 case LPFC_CTX_LUN:
James Smart495a7142008-06-14 22:52:59 -04009561 if ((lpfc_cmd->rdata->pnode) &&
9562 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id) &&
9563 (scsilun_to_int(&lpfc_cmd->fcp_cmnd->fcp_lun) == lun_id))
dea31012005-04-17 16:05:31 -05009564 rc = 0;
9565 break;
9566 case LPFC_CTX_TGT:
James Smart495a7142008-06-14 22:52:59 -04009567 if ((lpfc_cmd->rdata->pnode) &&
9568 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id))
dea31012005-04-17 16:05:31 -05009569 rc = 0;
9570 break;
dea31012005-04-17 16:05:31 -05009571 case LPFC_CTX_HOST:
9572 rc = 0;
9573 break;
9574 default:
9575 printk(KERN_ERR "%s: Unknown context cmd type, value %d\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07009576 __func__, ctx_cmd);
dea31012005-04-17 16:05:31 -05009577 break;
9578 }
9579
9580 return rc;
9581}
9582
James Smarte59058c2008-08-24 21:49:00 -04009583/**
James Smart3621a712009-04-06 18:47:14 -04009584 * lpfc_sli_sum_iocb - Function to count the number of FCP iocbs pending
James Smarte59058c2008-08-24 21:49:00 -04009585 * @vport: Pointer to virtual port.
9586 * @tgt_id: SCSI ID of the target.
9587 * @lun_id: LUN ID of the scsi device.
9588 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
9589 *
9590 * This function returns number of FCP commands pending for the vport.
9591 * When ctx_cmd == LPFC_CTX_LUN, the function returns number of FCP
9592 * commands pending on the vport associated with SCSI device specified
9593 * by tgt_id and lun_id parameters.
9594 * When ctx_cmd == LPFC_CTX_TGT, the function returns number of FCP
9595 * commands pending on the vport associated with SCSI target specified
9596 * by tgt_id parameter.
9597 * When ctx_cmd == LPFC_CTX_HOST, the function returns number of FCP
9598 * commands pending on the vport.
9599 * This function returns the number of iocbs which satisfy the filter.
9600 * This function is called without any lock held.
9601 **/
dea31012005-04-17 16:05:31 -05009602int
James Smart51ef4c22007-08-02 11:10:31 -04009603lpfc_sli_sum_iocb(struct lpfc_vport *vport, uint16_t tgt_id, uint64_t lun_id,
9604 lpfc_ctx_cmd ctx_cmd)
dea31012005-04-17 16:05:31 -05009605{
James Smart51ef4c22007-08-02 11:10:31 -04009606 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009607 struct lpfc_iocbq *iocbq;
9608 int sum, i;
dea31012005-04-17 16:05:31 -05009609
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009610 for (i = 1, sum = 0; i <= phba->sli.last_iotag; i++) {
9611 iocbq = phba->sli.iocbq_lookup[i];
dea31012005-04-17 16:05:31 -05009612
James Smart51ef4c22007-08-02 11:10:31 -04009613 if (lpfc_sli_validate_fcp_iocb (iocbq, vport, tgt_id, lun_id,
9614 ctx_cmd) == 0)
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009615 sum++;
dea31012005-04-17 16:05:31 -05009616 }
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009617
dea31012005-04-17 16:05:31 -05009618 return sum;
9619}
9620
James Smarte59058c2008-08-24 21:49:00 -04009621/**
James Smart3621a712009-04-06 18:47:14 -04009622 * lpfc_sli_abort_fcp_cmpl - Completion handler function for aborted FCP IOCBs
James Smarte59058c2008-08-24 21:49:00 -04009623 * @phba: Pointer to HBA context object
9624 * @cmdiocb: Pointer to command iocb object.
9625 * @rspiocb: Pointer to response iocb object.
9626 *
9627 * This function is called when an aborted FCP iocb completes. This
9628 * function is called by the ring event handler with no lock held.
9629 * This function frees the iocb.
9630 **/
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -04009631void
James Smart2e0fef82007-06-17 19:56:36 -05009632lpfc_sli_abort_fcp_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
9633 struct lpfc_iocbq *rspiocb)
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -04009634{
James Smartcb69f7d2011-12-13 13:21:57 -05009635 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
9636 "3096 ABORT_XRI_CN completing on xri x%x "
9637 "original iotag x%x, abort cmd iotag x%x "
9638 "status 0x%x, reason 0x%x\n",
9639 cmdiocb->iocb.un.acxri.abortContextTag,
9640 cmdiocb->iocb.un.acxri.abortIoTag,
9641 cmdiocb->iotag, rspiocb->iocb.ulpStatus,
9642 rspiocb->iocb.un.ulpWord[4]);
James Bottomley604a3e32005-10-29 10:28:33 -05009643 lpfc_sli_release_iocbq(phba, cmdiocb);
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -04009644 return;
9645}
9646
James Smarte59058c2008-08-24 21:49:00 -04009647/**
James Smart3621a712009-04-06 18:47:14 -04009648 * lpfc_sli_abort_iocb - issue abort for all commands on a host/target/LUN
James Smarte59058c2008-08-24 21:49:00 -04009649 * @vport: Pointer to virtual port.
9650 * @pring: Pointer to driver SLI ring object.
9651 * @tgt_id: SCSI ID of the target.
9652 * @lun_id: LUN ID of the scsi device.
9653 * @abort_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
9654 *
9655 * This function sends an abort command for every SCSI command
9656 * associated with the given virtual port pending on the ring
9657 * filtered by lpfc_sli_validate_fcp_iocb function.
9658 * When abort_cmd == LPFC_CTX_LUN, the function sends abort only to the
9659 * FCP iocbs associated with lun specified by tgt_id and lun_id
9660 * parameters
9661 * When abort_cmd == LPFC_CTX_TGT, the function sends abort only to the
9662 * FCP iocbs associated with SCSI target specified by tgt_id parameter.
9663 * When abort_cmd == LPFC_CTX_HOST, the function sends abort to all
9664 * FCP iocbs associated with virtual port.
9665 * This function returns number of iocbs it failed to abort.
9666 * This function is called with no locks held.
9667 **/
dea31012005-04-17 16:05:31 -05009668int
James Smart51ef4c22007-08-02 11:10:31 -04009669lpfc_sli_abort_iocb(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
9670 uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd abort_cmd)
dea31012005-04-17 16:05:31 -05009671{
James Smart51ef4c22007-08-02 11:10:31 -04009672 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009673 struct lpfc_iocbq *iocbq;
9674 struct lpfc_iocbq *abtsiocb;
dea31012005-04-17 16:05:31 -05009675 IOCB_t *cmd = NULL;
dea31012005-04-17 16:05:31 -05009676 int errcnt = 0, ret_val = 0;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009677 int i;
dea31012005-04-17 16:05:31 -05009678
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009679 for (i = 1; i <= phba->sli.last_iotag; i++) {
9680 iocbq = phba->sli.iocbq_lookup[i];
dea31012005-04-17 16:05:31 -05009681
James Smart51ef4c22007-08-02 11:10:31 -04009682 if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
James Smart2e0fef82007-06-17 19:56:36 -05009683 abort_cmd) != 0)
dea31012005-04-17 16:05:31 -05009684 continue;
9685
9686 /* issue ABTS for this IOCB based on iotag */
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009687 abtsiocb = lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05009688 if (abtsiocb == NULL) {
9689 errcnt++;
9690 continue;
9691 }
dea31012005-04-17 16:05:31 -05009692
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009693 cmd = &iocbq->iocb;
dea31012005-04-17 16:05:31 -05009694 abtsiocb->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
9695 abtsiocb->iocb.un.acxri.abortContextTag = cmd->ulpContext;
James Smartda0436e2009-05-22 14:51:39 -04009696 if (phba->sli_rev == LPFC_SLI_REV4)
9697 abtsiocb->iocb.un.acxri.abortIoTag = iocbq->sli4_xritag;
9698 else
9699 abtsiocb->iocb.un.acxri.abortIoTag = cmd->ulpIoTag;
dea31012005-04-17 16:05:31 -05009700 abtsiocb->iocb.ulpLe = 1;
9701 abtsiocb->iocb.ulpClass = cmd->ulpClass;
James Smart2e0fef82007-06-17 19:56:36 -05009702 abtsiocb->vport = phba->pport;
dea31012005-04-17 16:05:31 -05009703
James Smart5ffc2662009-11-18 15:39:44 -05009704 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
9705 abtsiocb->fcp_wqidx = iocbq->fcp_wqidx;
James Smart341af102010-01-26 23:07:37 -05009706 if (iocbq->iocb_flag & LPFC_IO_FCP)
9707 abtsiocb->iocb_flag |= LPFC_USE_FCPWQIDX;
James Smart5ffc2662009-11-18 15:39:44 -05009708
James Smart2e0fef82007-06-17 19:56:36 -05009709 if (lpfc_is_link_up(phba))
dea31012005-04-17 16:05:31 -05009710 abtsiocb->iocb.ulpCommand = CMD_ABORT_XRI_CN;
9711 else
9712 abtsiocb->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
9713
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -04009714 /* Setup callback routine and issue the command. */
9715 abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
James Smartda0436e2009-05-22 14:51:39 -04009716 ret_val = lpfc_sli_issue_iocb(phba, pring->ringno,
9717 abtsiocb, 0);
dea31012005-04-17 16:05:31 -05009718 if (ret_val == IOCB_ERROR) {
James Bottomley604a3e32005-10-29 10:28:33 -05009719 lpfc_sli_release_iocbq(phba, abtsiocb);
dea31012005-04-17 16:05:31 -05009720 errcnt++;
9721 continue;
9722 }
9723 }
9724
9725 return errcnt;
9726}
9727
James Smarte59058c2008-08-24 21:49:00 -04009728/**
James Smart3621a712009-04-06 18:47:14 -04009729 * lpfc_sli_wake_iocb_wait - lpfc_sli_issue_iocb_wait's completion handler
James Smarte59058c2008-08-24 21:49:00 -04009730 * @phba: Pointer to HBA context object.
9731 * @cmdiocbq: Pointer to command iocb.
9732 * @rspiocbq: Pointer to response iocb.
9733 *
9734 * This function is the completion handler for iocbs issued using
9735 * lpfc_sli_issue_iocb_wait function. This function is called by the
9736 * ring event handler function without any lock held. This function
9737 * can be called from both worker thread context and interrupt
9738 * context. This function also can be called from other thread which
9739 * cleans up the SLI layer objects.
9740 * This function copy the contents of the response iocb to the
9741 * response iocb memory object provided by the caller of
9742 * lpfc_sli_issue_iocb_wait and then wakes up the thread which
9743 * sleeps for the iocb completion.
9744 **/
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009745static void
9746lpfc_sli_wake_iocb_wait(struct lpfc_hba *phba,
9747 struct lpfc_iocbq *cmdiocbq,
9748 struct lpfc_iocbq *rspiocbq)
dea31012005-04-17 16:05:31 -05009749{
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009750 wait_queue_head_t *pdone_q;
9751 unsigned long iflags;
James Smart0f65ff62010-02-26 14:14:23 -05009752 struct lpfc_scsi_buf *lpfc_cmd;
dea31012005-04-17 16:05:31 -05009753
James Smart2e0fef82007-06-17 19:56:36 -05009754 spin_lock_irqsave(&phba->hbalock, iflags);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009755 cmdiocbq->iocb_flag |= LPFC_IO_WAKE;
9756 if (cmdiocbq->context2 && rspiocbq)
9757 memcpy(&((struct lpfc_iocbq *)cmdiocbq->context2)->iocb,
9758 &rspiocbq->iocb, sizeof(IOCB_t));
9759
James Smart0f65ff62010-02-26 14:14:23 -05009760 /* Set the exchange busy flag for task management commands */
9761 if ((cmdiocbq->iocb_flag & LPFC_IO_FCP) &&
9762 !(cmdiocbq->iocb_flag & LPFC_IO_LIBDFC)) {
9763 lpfc_cmd = container_of(cmdiocbq, struct lpfc_scsi_buf,
9764 cur_iocbq);
9765 lpfc_cmd->exch_busy = rspiocbq->iocb_flag & LPFC_EXCHANGE_BUSY;
9766 }
9767
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009768 pdone_q = cmdiocbq->context_un.wait_queue;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009769 if (pdone_q)
9770 wake_up(pdone_q);
James Smart858c9f62007-06-17 19:56:39 -05009771 spin_unlock_irqrestore(&phba->hbalock, iflags);
dea31012005-04-17 16:05:31 -05009772 return;
9773}
9774
James Smarte59058c2008-08-24 21:49:00 -04009775/**
James Smartd11e31d2009-06-10 17:23:06 -04009776 * lpfc_chk_iocb_flg - Test IOCB flag with lock held.
9777 * @phba: Pointer to HBA context object..
9778 * @piocbq: Pointer to command iocb.
9779 * @flag: Flag to test.
9780 *
9781 * This routine grabs the hbalock and then test the iocb_flag to
9782 * see if the passed in flag is set.
9783 * Returns:
9784 * 1 if flag is set.
9785 * 0 if flag is not set.
9786 **/
9787static int
9788lpfc_chk_iocb_flg(struct lpfc_hba *phba,
9789 struct lpfc_iocbq *piocbq, uint32_t flag)
9790{
9791 unsigned long iflags;
9792 int ret;
9793
9794 spin_lock_irqsave(&phba->hbalock, iflags);
9795 ret = piocbq->iocb_flag & flag;
9796 spin_unlock_irqrestore(&phba->hbalock, iflags);
9797 return ret;
9798
9799}
9800
9801/**
James Smart3621a712009-04-06 18:47:14 -04009802 * lpfc_sli_issue_iocb_wait - Synchronous function to issue iocb commands
James Smarte59058c2008-08-24 21:49:00 -04009803 * @phba: Pointer to HBA context object..
9804 * @pring: Pointer to sli ring.
9805 * @piocb: Pointer to command iocb.
9806 * @prspiocbq: Pointer to response iocb.
9807 * @timeout: Timeout in number of seconds.
9808 *
9809 * This function issues the iocb to firmware and waits for the
9810 * iocb to complete. If the iocb command is not
9811 * completed within timeout seconds, it returns IOCB_TIMEDOUT.
9812 * Caller should not free the iocb resources if this function
9813 * returns IOCB_TIMEDOUT.
9814 * The function waits for the iocb completion using an
9815 * non-interruptible wait.
9816 * This function will sleep while waiting for iocb completion.
9817 * So, this function should not be called from any context which
9818 * does not allow sleeping. Due to the same reason, this function
9819 * cannot be called with interrupt disabled.
9820 * This function assumes that the iocb completions occur while
9821 * this function sleep. So, this function cannot be called from
9822 * the thread which process iocb completion for this ring.
9823 * This function clears the iocb_flag of the iocb object before
9824 * issuing the iocb and the iocb completion handler sets this
9825 * flag and wakes this thread when the iocb completes.
9826 * The contents of the response iocb will be copied to prspiocbq
9827 * by the completion handler when the command completes.
9828 * This function returns IOCB_SUCCESS when success.
9829 * This function is called with no lock held.
9830 **/
dea31012005-04-17 16:05:31 -05009831int
James Smart2e0fef82007-06-17 19:56:36 -05009832lpfc_sli_issue_iocb_wait(struct lpfc_hba *phba,
James Smartda0436e2009-05-22 14:51:39 -04009833 uint32_t ring_number,
James Smart2e0fef82007-06-17 19:56:36 -05009834 struct lpfc_iocbq *piocb,
9835 struct lpfc_iocbq *prspiocbq,
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009836 uint32_t timeout)
dea31012005-04-17 16:05:31 -05009837{
Peter Zijlstra7259f0d2006-10-29 22:46:36 -08009838 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009839 long timeleft, timeout_req = 0;
9840 int retval = IOCB_SUCCESS;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05009841 uint32_t creg_val;
James Smart2a9bf3d2010-06-07 15:24:45 -04009842 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
dea31012005-04-17 16:05:31 -05009843 /*
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009844 * If the caller has provided a response iocbq buffer, then context2
9845 * is NULL or its an error.
dea31012005-04-17 16:05:31 -05009846 */
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009847 if (prspiocbq) {
9848 if (piocb->context2)
9849 return IOCB_ERROR;
9850 piocb->context2 = prspiocbq;
dea31012005-04-17 16:05:31 -05009851 }
9852
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009853 piocb->iocb_cmpl = lpfc_sli_wake_iocb_wait;
9854 piocb->context_un.wait_queue = &done_q;
9855 piocb->iocb_flag &= ~LPFC_IO_WAKE;
dea31012005-04-17 16:05:31 -05009856
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05009857 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
James Smart9940b972011-03-11 16:06:12 -05009858 if (lpfc_readl(phba->HCregaddr, &creg_val))
9859 return IOCB_ERROR;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05009860 creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
9861 writel(creg_val, phba->HCregaddr);
9862 readl(phba->HCregaddr); /* flush */
9863 }
9864
James Smart2a9bf3d2010-06-07 15:24:45 -04009865 retval = lpfc_sli_issue_iocb(phba, ring_number, piocb,
9866 SLI_IOCB_RET_IOCB);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009867 if (retval == IOCB_SUCCESS) {
9868 timeout_req = timeout * HZ;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009869 timeleft = wait_event_timeout(done_q,
James Smartd11e31d2009-06-10 17:23:06 -04009870 lpfc_chk_iocb_flg(phba, piocb, LPFC_IO_WAKE),
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009871 timeout_req);
dea31012005-04-17 16:05:31 -05009872
James Smart7054a602007-04-25 09:52:34 -04009873 if (piocb->iocb_flag & LPFC_IO_WAKE) {
9874 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04009875 "0331 IOCB wake signaled\n");
James Smart7054a602007-04-25 09:52:34 -04009876 } else if (timeleft == 0) {
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009877 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04009878 "0338 IOCB wait timeout error - no "
9879 "wake response Data x%x\n", timeout);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009880 retval = IOCB_TIMEDOUT;
James Smart7054a602007-04-25 09:52:34 -04009881 } else {
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009882 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04009883 "0330 IOCB wake NOT set, "
9884 "Data x%x x%lx\n",
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009885 timeout, (timeleft / jiffies));
9886 retval = IOCB_TIMEDOUT;
dea31012005-04-17 16:05:31 -05009887 }
James Smart2a9bf3d2010-06-07 15:24:45 -04009888 } else if (retval == IOCB_BUSY) {
9889 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
9890 "2818 Max IOCBs %d txq cnt %d txcmplq cnt %d\n",
9891 phba->iocb_cnt, pring->txq_cnt, pring->txcmplq_cnt);
9892 return retval;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009893 } else {
9894 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smartd7c255b2008-08-24 21:50:00 -04009895 "0332 IOCB wait issue failed, Data x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04009896 retval);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009897 retval = IOCB_ERROR;
dea31012005-04-17 16:05:31 -05009898 }
9899
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05009900 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
James Smart9940b972011-03-11 16:06:12 -05009901 if (lpfc_readl(phba->HCregaddr, &creg_val))
9902 return IOCB_ERROR;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05009903 creg_val &= ~(HC_R0INT_ENA << LPFC_FCP_RING);
9904 writel(creg_val, phba->HCregaddr);
9905 readl(phba->HCregaddr); /* flush */
9906 }
9907
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009908 if (prspiocbq)
9909 piocb->context2 = NULL;
9910
9911 piocb->context_un.wait_queue = NULL;
9912 piocb->iocb_cmpl = NULL;
dea31012005-04-17 16:05:31 -05009913 return retval;
9914}
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009915
James Smarte59058c2008-08-24 21:49:00 -04009916/**
James Smart3621a712009-04-06 18:47:14 -04009917 * lpfc_sli_issue_mbox_wait - Synchronous function to issue mailbox
James Smarte59058c2008-08-24 21:49:00 -04009918 * @phba: Pointer to HBA context object.
9919 * @pmboxq: Pointer to driver mailbox object.
9920 * @timeout: Timeout in number of seconds.
9921 *
9922 * This function issues the mailbox to firmware and waits for the
9923 * mailbox command to complete. If the mailbox command is not
9924 * completed within timeout seconds, it returns MBX_TIMEOUT.
9925 * The function waits for the mailbox completion using an
9926 * interruptible wait. If the thread is woken up due to a
9927 * signal, MBX_TIMEOUT error is returned to the caller. Caller
9928 * should not free the mailbox resources, if this function returns
9929 * MBX_TIMEOUT.
9930 * This function will sleep while waiting for mailbox completion.
9931 * So, this function should not be called from any context which
9932 * does not allow sleeping. Due to the same reason, this function
9933 * cannot be called with interrupt disabled.
9934 * This function assumes that the mailbox completion occurs while
9935 * this function sleep. So, this function cannot be called from
9936 * the worker thread which processes mailbox completion.
9937 * This function is called in the context of HBA management
9938 * applications.
9939 * This function returns MBX_SUCCESS when successful.
9940 * This function is called with no lock held.
9941 **/
dea31012005-04-17 16:05:31 -05009942int
James Smart2e0fef82007-06-17 19:56:36 -05009943lpfc_sli_issue_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq,
dea31012005-04-17 16:05:31 -05009944 uint32_t timeout)
9945{
Peter Zijlstra7259f0d2006-10-29 22:46:36 -08009946 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
dea31012005-04-17 16:05:31 -05009947 int retval;
James Smart858c9f62007-06-17 19:56:39 -05009948 unsigned long flag;
dea31012005-04-17 16:05:31 -05009949
9950 /* The caller must leave context1 empty. */
James Smart98c9ea52007-10-27 13:37:33 -04009951 if (pmboxq->context1)
James Smart2e0fef82007-06-17 19:56:36 -05009952 return MBX_NOT_FINISHED;
dea31012005-04-17 16:05:31 -05009953
James Smart495a7142008-06-14 22:52:59 -04009954 pmboxq->mbox_flag &= ~LPFC_MBX_WAKE;
dea31012005-04-17 16:05:31 -05009955 /* setup wake call as IOCB callback */
9956 pmboxq->mbox_cmpl = lpfc_sli_wake_mbox_wait;
9957 /* setup context field to pass wait_queue pointer to wake function */
9958 pmboxq->context1 = &done_q;
9959
dea31012005-04-17 16:05:31 -05009960 /* now issue the command */
9961 retval = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
dea31012005-04-17 16:05:31 -05009962 if (retval == MBX_BUSY || retval == MBX_SUCCESS) {
James Smart7054a602007-04-25 09:52:34 -04009963 wait_event_interruptible_timeout(done_q,
9964 pmboxq->mbox_flag & LPFC_MBX_WAKE,
9965 timeout * HZ);
9966
James Smart858c9f62007-06-17 19:56:39 -05009967 spin_lock_irqsave(&phba->hbalock, flag);
dea31012005-04-17 16:05:31 -05009968 pmboxq->context1 = NULL;
James Smart7054a602007-04-25 09:52:34 -04009969 /*
9970 * if LPFC_MBX_WAKE flag is set the mailbox is completed
9971 * else do not free the resources.
9972 */
James Smartd7c47992010-06-08 18:31:54 -04009973 if (pmboxq->mbox_flag & LPFC_MBX_WAKE) {
dea31012005-04-17 16:05:31 -05009974 retval = MBX_SUCCESS;
James Smartd7c47992010-06-08 18:31:54 -04009975 lpfc_sli4_swap_str(phba, pmboxq);
9976 } else {
James Smart7054a602007-04-25 09:52:34 -04009977 retval = MBX_TIMEOUT;
James Smart858c9f62007-06-17 19:56:39 -05009978 pmboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
9979 }
9980 spin_unlock_irqrestore(&phba->hbalock, flag);
dea31012005-04-17 16:05:31 -05009981 }
9982
dea31012005-04-17 16:05:31 -05009983 return retval;
9984}
9985
James Smarte59058c2008-08-24 21:49:00 -04009986/**
James Smart3772a992009-05-22 14:50:54 -04009987 * lpfc_sli_mbox_sys_shutdown - shutdown mailbox command sub-system
James Smarte59058c2008-08-24 21:49:00 -04009988 * @phba: Pointer to HBA context.
9989 *
James Smart3772a992009-05-22 14:50:54 -04009990 * This function is called to shutdown the driver's mailbox sub-system.
9991 * It first marks the mailbox sub-system is in a block state to prevent
9992 * the asynchronous mailbox command from issued off the pending mailbox
9993 * command queue. If the mailbox command sub-system shutdown is due to
9994 * HBA error conditions such as EEH or ERATT, this routine shall invoke
9995 * the mailbox sub-system flush routine to forcefully bring down the
9996 * mailbox sub-system. Otherwise, if it is due to normal condition (such
9997 * as with offline or HBA function reset), this routine will wait for the
9998 * outstanding mailbox command to complete before invoking the mailbox
9999 * sub-system flush routine to gracefully bring down mailbox sub-system.
James Smarte59058c2008-08-24 21:49:00 -040010000 **/
James Smart3772a992009-05-22 14:50:54 -040010001void
James Smart618a5232012-06-12 13:54:36 -040010002lpfc_sli_mbox_sys_shutdown(struct lpfc_hba *phba, int mbx_action)
James Smartb4c02652006-07-06 15:50:43 -040010003{
James Smart3772a992009-05-22 14:50:54 -040010004 struct lpfc_sli *psli = &phba->sli;
James Smart3772a992009-05-22 14:50:54 -040010005 unsigned long timeout;
10006
James Smart618a5232012-06-12 13:54:36 -040010007 if (mbx_action == LPFC_MBX_NO_WAIT) {
10008 /* delay 100ms for port state */
10009 msleep(100);
10010 lpfc_sli_mbox_sys_flush(phba);
10011 return;
10012 }
James Smarta183a152011-10-10 21:32:43 -040010013 timeout = msecs_to_jiffies(LPFC_MBOX_TMO * 1000) + jiffies;
James Smartd7069f02012-03-01 22:36:29 -050010014
James Smart3772a992009-05-22 14:50:54 -040010015 spin_lock_irq(&phba->hbalock);
10016 psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
James Smart3772a992009-05-22 14:50:54 -040010017
10018 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
James Smart3772a992009-05-22 14:50:54 -040010019 /* Determine how long we might wait for the active mailbox
10020 * command to be gracefully completed by firmware.
10021 */
James Smarta183a152011-10-10 21:32:43 -040010022 if (phba->sli.mbox_active)
10023 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
10024 phba->sli.mbox_active) *
10025 1000) + jiffies;
10026 spin_unlock_irq(&phba->hbalock);
10027
James Smart3772a992009-05-22 14:50:54 -040010028 while (phba->sli.mbox_active) {
10029 /* Check active mailbox complete status every 2ms */
10030 msleep(2);
10031 if (time_after(jiffies, timeout))
10032 /* Timeout, let the mailbox flush routine to
10033 * forcefully release active mailbox command
10034 */
10035 break;
10036 }
James Smartd7069f02012-03-01 22:36:29 -050010037 } else
10038 spin_unlock_irq(&phba->hbalock);
10039
James Smart3772a992009-05-22 14:50:54 -040010040 lpfc_sli_mbox_sys_flush(phba);
10041}
10042
10043/**
10044 * lpfc_sli_eratt_read - read sli-3 error attention events
10045 * @phba: Pointer to HBA context.
10046 *
10047 * This function is called to read the SLI3 device error attention registers
10048 * for possible error attention events. The caller must hold the hostlock
10049 * with spin_lock_irq().
10050 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030010051 * This function returns 1 when there is Error Attention in the Host Attention
James Smart3772a992009-05-22 14:50:54 -040010052 * Register and returns 0 otherwise.
10053 **/
10054static int
10055lpfc_sli_eratt_read(struct lpfc_hba *phba)
10056{
James Smarted957682007-06-17 19:56:37 -050010057 uint32_t ha_copy;
James Smartb4c02652006-07-06 15:50:43 -040010058
James Smart3772a992009-05-22 14:50:54 -040010059 /* Read chip Host Attention (HA) register */
James Smart9940b972011-03-11 16:06:12 -050010060 if (lpfc_readl(phba->HAregaddr, &ha_copy))
10061 goto unplug_err;
10062
James Smart3772a992009-05-22 14:50:54 -040010063 if (ha_copy & HA_ERATT) {
10064 /* Read host status register to retrieve error event */
James Smart9940b972011-03-11 16:06:12 -050010065 if (lpfc_sli_read_hs(phba))
10066 goto unplug_err;
James Smartb4c02652006-07-06 15:50:43 -040010067
James Smart3772a992009-05-22 14:50:54 -040010068 /* Check if there is a deferred error condition is active */
10069 if ((HS_FFER1 & phba->work_hs) &&
10070 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
James Smartdcf2a4e2010-09-29 11:18:53 -040010071 HS_FFER6 | HS_FFER7 | HS_FFER8) & phba->work_hs)) {
James Smart3772a992009-05-22 14:50:54 -040010072 phba->hba_flag |= DEFER_ERATT;
James Smart3772a992009-05-22 14:50:54 -040010073 /* Clear all interrupt enable conditions */
10074 writel(0, phba->HCregaddr);
10075 readl(phba->HCregaddr);
10076 }
10077
10078 /* Set the driver HA work bitmap */
James Smart3772a992009-05-22 14:50:54 -040010079 phba->work_ha |= HA_ERATT;
10080 /* Indicate polling handles this ERATT */
10081 phba->hba_flag |= HBA_ERATT_HANDLED;
James Smart3772a992009-05-22 14:50:54 -040010082 return 1;
James Smartb4c02652006-07-06 15:50:43 -040010083 }
James Smart3772a992009-05-22 14:50:54 -040010084 return 0;
James Smart9940b972011-03-11 16:06:12 -050010085
10086unplug_err:
10087 /* Set the driver HS work bitmap */
10088 phba->work_hs |= UNPLUG_ERR;
10089 /* Set the driver HA work bitmap */
10090 phba->work_ha |= HA_ERATT;
10091 /* Indicate polling handles this ERATT */
10092 phba->hba_flag |= HBA_ERATT_HANDLED;
10093 return 1;
James Smartb4c02652006-07-06 15:50:43 -040010094}
10095
James Smarte59058c2008-08-24 21:49:00 -040010096/**
James Smartda0436e2009-05-22 14:51:39 -040010097 * lpfc_sli4_eratt_read - read sli-4 error attention events
10098 * @phba: Pointer to HBA context.
10099 *
10100 * This function is called to read the SLI4 device error attention registers
10101 * for possible error attention events. The caller must hold the hostlock
10102 * with spin_lock_irq().
10103 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030010104 * This function returns 1 when there is Error Attention in the Host Attention
James Smartda0436e2009-05-22 14:51:39 -040010105 * Register and returns 0 otherwise.
10106 **/
10107static int
10108lpfc_sli4_eratt_read(struct lpfc_hba *phba)
10109{
10110 uint32_t uerr_sta_hi, uerr_sta_lo;
James Smart2fcee4b2010-12-15 17:57:46 -050010111 uint32_t if_type, portsmphr;
10112 struct lpfc_register portstat_reg;
James Smartda0436e2009-05-22 14:51:39 -040010113
James Smart2fcee4b2010-12-15 17:57:46 -050010114 /*
10115 * For now, use the SLI4 device internal unrecoverable error
James Smartda0436e2009-05-22 14:51:39 -040010116 * registers for error attention. This can be changed later.
10117 */
James Smart2fcee4b2010-12-15 17:57:46 -050010118 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
10119 switch (if_type) {
10120 case LPFC_SLI_INTF_IF_TYPE_0:
James Smart9940b972011-03-11 16:06:12 -050010121 if (lpfc_readl(phba->sli4_hba.u.if_type0.UERRLOregaddr,
10122 &uerr_sta_lo) ||
10123 lpfc_readl(phba->sli4_hba.u.if_type0.UERRHIregaddr,
10124 &uerr_sta_hi)) {
10125 phba->work_hs |= UNPLUG_ERR;
10126 phba->work_ha |= HA_ERATT;
10127 phba->hba_flag |= HBA_ERATT_HANDLED;
10128 return 1;
10129 }
James Smart2fcee4b2010-12-15 17:57:46 -050010130 if ((~phba->sli4_hba.ue_mask_lo & uerr_sta_lo) ||
10131 (~phba->sli4_hba.ue_mask_hi & uerr_sta_hi)) {
10132 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10133 "1423 HBA Unrecoverable error: "
10134 "uerr_lo_reg=0x%x, uerr_hi_reg=0x%x, "
10135 "ue_mask_lo_reg=0x%x, "
10136 "ue_mask_hi_reg=0x%x\n",
10137 uerr_sta_lo, uerr_sta_hi,
10138 phba->sli4_hba.ue_mask_lo,
10139 phba->sli4_hba.ue_mask_hi);
10140 phba->work_status[0] = uerr_sta_lo;
10141 phba->work_status[1] = uerr_sta_hi;
10142 phba->work_ha |= HA_ERATT;
10143 phba->hba_flag |= HBA_ERATT_HANDLED;
10144 return 1;
10145 }
10146 break;
10147 case LPFC_SLI_INTF_IF_TYPE_2:
James Smart9940b972011-03-11 16:06:12 -050010148 if (lpfc_readl(phba->sli4_hba.u.if_type2.STATUSregaddr,
10149 &portstat_reg.word0) ||
10150 lpfc_readl(phba->sli4_hba.PSMPHRregaddr,
10151 &portsmphr)){
10152 phba->work_hs |= UNPLUG_ERR;
10153 phba->work_ha |= HA_ERATT;
10154 phba->hba_flag |= HBA_ERATT_HANDLED;
10155 return 1;
10156 }
James Smart2fcee4b2010-12-15 17:57:46 -050010157 if (bf_get(lpfc_sliport_status_err, &portstat_reg)) {
10158 phba->work_status[0] =
10159 readl(phba->sli4_hba.u.if_type2.ERR1regaddr);
10160 phba->work_status[1] =
10161 readl(phba->sli4_hba.u.if_type2.ERR2regaddr);
10162 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart2e90f4b2011-12-13 13:22:37 -050010163 "2885 Port Status Event: "
James Smart2fcee4b2010-12-15 17:57:46 -050010164 "port status reg 0x%x, "
10165 "port smphr reg 0x%x, "
10166 "error 1=0x%x, error 2=0x%x\n",
10167 portstat_reg.word0,
10168 portsmphr,
10169 phba->work_status[0],
10170 phba->work_status[1]);
10171 phba->work_ha |= HA_ERATT;
10172 phba->hba_flag |= HBA_ERATT_HANDLED;
10173 return 1;
10174 }
10175 break;
10176 case LPFC_SLI_INTF_IF_TYPE_1:
10177 default:
James Smarta747c9c2009-11-18 15:41:10 -050010178 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart2fcee4b2010-12-15 17:57:46 -050010179 "2886 HBA Error Attention on unsupported "
10180 "if type %d.", if_type);
James Smarta747c9c2009-11-18 15:41:10 -050010181 return 1;
James Smartda0436e2009-05-22 14:51:39 -040010182 }
James Smart2fcee4b2010-12-15 17:57:46 -050010183
James Smartda0436e2009-05-22 14:51:39 -040010184 return 0;
10185}
10186
10187/**
James Smart3621a712009-04-06 18:47:14 -040010188 * lpfc_sli_check_eratt - check error attention events
James Smart93996272008-08-24 21:50:30 -040010189 * @phba: Pointer to HBA context.
10190 *
James Smart3772a992009-05-22 14:50:54 -040010191 * This function is called from timer soft interrupt context to check HBA's
James Smart93996272008-08-24 21:50:30 -040010192 * error attention register bit for error attention events.
10193 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030010194 * This function returns 1 when there is Error Attention in the Host Attention
James Smart93996272008-08-24 21:50:30 -040010195 * Register and returns 0 otherwise.
10196 **/
10197int
10198lpfc_sli_check_eratt(struct lpfc_hba *phba)
10199{
10200 uint32_t ha_copy;
10201
10202 /* If somebody is waiting to handle an eratt, don't process it
10203 * here. The brdkill function will do this.
10204 */
10205 if (phba->link_flag & LS_IGNORE_ERATT)
10206 return 0;
10207
10208 /* Check if interrupt handler handles this ERATT */
10209 spin_lock_irq(&phba->hbalock);
10210 if (phba->hba_flag & HBA_ERATT_HANDLED) {
10211 /* Interrupt handler has handled ERATT */
10212 spin_unlock_irq(&phba->hbalock);
10213 return 0;
10214 }
10215
James Smarta257bf92009-04-06 18:48:10 -040010216 /*
10217 * If there is deferred error attention, do not check for error
10218 * attention
10219 */
10220 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
10221 spin_unlock_irq(&phba->hbalock);
10222 return 0;
10223 }
10224
James Smart3772a992009-05-22 14:50:54 -040010225 /* If PCI channel is offline, don't process it */
10226 if (unlikely(pci_channel_offline(phba->pcidev))) {
James Smart93996272008-08-24 21:50:30 -040010227 spin_unlock_irq(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -040010228 return 0;
10229 }
10230
10231 switch (phba->sli_rev) {
10232 case LPFC_SLI_REV2:
10233 case LPFC_SLI_REV3:
10234 /* Read chip Host Attention (HA) register */
10235 ha_copy = lpfc_sli_eratt_read(phba);
10236 break;
James Smartda0436e2009-05-22 14:51:39 -040010237 case LPFC_SLI_REV4:
James Smart2fcee4b2010-12-15 17:57:46 -050010238 /* Read device Uncoverable Error (UERR) registers */
James Smartda0436e2009-05-22 14:51:39 -040010239 ha_copy = lpfc_sli4_eratt_read(phba);
10240 break;
James Smart3772a992009-05-22 14:50:54 -040010241 default:
10242 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10243 "0299 Invalid SLI revision (%d)\n",
10244 phba->sli_rev);
10245 ha_copy = 0;
10246 break;
James Smart93996272008-08-24 21:50:30 -040010247 }
10248 spin_unlock_irq(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -040010249
10250 return ha_copy;
10251}
10252
10253/**
10254 * lpfc_intr_state_check - Check device state for interrupt handling
10255 * @phba: Pointer to HBA context.
10256 *
10257 * This inline routine checks whether a device or its PCI slot is in a state
10258 * that the interrupt should be handled.
10259 *
10260 * This function returns 0 if the device or the PCI slot is in a state that
10261 * interrupt should be handled, otherwise -EIO.
10262 */
10263static inline int
10264lpfc_intr_state_check(struct lpfc_hba *phba)
10265{
10266 /* If the pci channel is offline, ignore all the interrupts */
10267 if (unlikely(pci_channel_offline(phba->pcidev)))
10268 return -EIO;
10269
10270 /* Update device level interrupt statistics */
10271 phba->sli.slistat.sli_intr++;
10272
10273 /* Ignore all interrupts during initialization. */
10274 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
10275 return -EIO;
10276
James Smart93996272008-08-24 21:50:30 -040010277 return 0;
10278}
10279
10280/**
James Smart3772a992009-05-22 14:50:54 -040010281 * lpfc_sli_sp_intr_handler - Slow-path interrupt handler to SLI-3 device
James Smarte59058c2008-08-24 21:49:00 -040010282 * @irq: Interrupt number.
10283 * @dev_id: The device context pointer.
10284 *
James Smart93996272008-08-24 21:50:30 -040010285 * This function is directly called from the PCI layer as an interrupt
James Smart3772a992009-05-22 14:50:54 -040010286 * service routine when device with SLI-3 interface spec is enabled with
10287 * MSI-X multi-message interrupt mode and there are slow-path events in
10288 * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
10289 * interrupt mode, this function is called as part of the device-level
10290 * interrupt handler. When the PCI slot is in error recovery or the HBA
10291 * is undergoing initialization, the interrupt handler will not process
10292 * the interrupt. The link attention and ELS ring attention events are
10293 * handled by the worker thread. The interrupt handler signals the worker
10294 * thread and returns for these events. This function is called without
10295 * any lock held. It gets the hbalock to access and update SLI data
James Smart93996272008-08-24 21:50:30 -040010296 * structures.
10297 *
10298 * This function returns IRQ_HANDLED when interrupt is handled else it
10299 * returns IRQ_NONE.
James Smarte59058c2008-08-24 21:49:00 -040010300 **/
dea31012005-04-17 16:05:31 -050010301irqreturn_t
James Smart3772a992009-05-22 14:50:54 -040010302lpfc_sli_sp_intr_handler(int irq, void *dev_id)
dea31012005-04-17 16:05:31 -050010303{
James Smart2e0fef82007-06-17 19:56:36 -050010304 struct lpfc_hba *phba;
James Smarta747c9c2009-11-18 15:41:10 -050010305 uint32_t ha_copy, hc_copy;
dea31012005-04-17 16:05:31 -050010306 uint32_t work_ha_copy;
10307 unsigned long status;
James Smart5b75da22008-12-04 22:39:35 -050010308 unsigned long iflag;
dea31012005-04-17 16:05:31 -050010309 uint32_t control;
10310
James Smart92d7f7b2007-06-17 19:56:38 -050010311 MAILBOX_t *mbox, *pmbox;
James Smart858c9f62007-06-17 19:56:39 -050010312 struct lpfc_vport *vport;
10313 struct lpfc_nodelist *ndlp;
10314 struct lpfc_dmabuf *mp;
James Smart92d7f7b2007-06-17 19:56:38 -050010315 LPFC_MBOXQ_t *pmb;
10316 int rc;
10317
dea31012005-04-17 16:05:31 -050010318 /*
10319 * Get the driver's phba structure from the dev_id and
10320 * assume the HBA is not interrupting.
10321 */
James Smart93996272008-08-24 21:50:30 -040010322 phba = (struct lpfc_hba *)dev_id;
dea31012005-04-17 16:05:31 -050010323
10324 if (unlikely(!phba))
10325 return IRQ_NONE;
10326
dea31012005-04-17 16:05:31 -050010327 /*
James Smart93996272008-08-24 21:50:30 -040010328 * Stuff needs to be attented to when this function is invoked as an
10329 * individual interrupt handler in MSI-X multi-message interrupt mode
dea31012005-04-17 16:05:31 -050010330 */
James Smart93996272008-08-24 21:50:30 -040010331 if (phba->intr_type == MSIX) {
James Smart3772a992009-05-22 14:50:54 -040010332 /* Check device state for handling interrupt */
10333 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -040010334 return IRQ_NONE;
10335 /* Need to read HA REG for slow-path events */
James Smart5b75da22008-12-04 22:39:35 -050010336 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart9940b972011-03-11 16:06:12 -050010337 if (lpfc_readl(phba->HAregaddr, &ha_copy))
10338 goto unplug_error;
James Smart93996272008-08-24 21:50:30 -040010339 /* If somebody is waiting to handle an eratt don't process it
10340 * here. The brdkill function will do this.
10341 */
10342 if (phba->link_flag & LS_IGNORE_ERATT)
10343 ha_copy &= ~HA_ERATT;
10344 /* Check the need for handling ERATT in interrupt handler */
10345 if (ha_copy & HA_ERATT) {
10346 if (phba->hba_flag & HBA_ERATT_HANDLED)
10347 /* ERATT polling has handled ERATT */
10348 ha_copy &= ~HA_ERATT;
10349 else
10350 /* Indicate interrupt handler handles ERATT */
10351 phba->hba_flag |= HBA_ERATT_HANDLED;
10352 }
James Smarta257bf92009-04-06 18:48:10 -040010353
10354 /*
10355 * If there is deferred error attention, do not check for any
10356 * interrupt.
10357 */
10358 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
James Smart3772a992009-05-22 14:50:54 -040010359 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -040010360 return IRQ_NONE;
10361 }
10362
James Smart93996272008-08-24 21:50:30 -040010363 /* Clear up only attention source related to slow-path */
James Smart9940b972011-03-11 16:06:12 -050010364 if (lpfc_readl(phba->HCregaddr, &hc_copy))
10365 goto unplug_error;
10366
James Smarta747c9c2009-11-18 15:41:10 -050010367 writel(hc_copy & ~(HC_MBINT_ENA | HC_R2INT_ENA |
10368 HC_LAINT_ENA | HC_ERINT_ENA),
10369 phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -040010370 writel((ha_copy & (HA_MBATT | HA_R2_CLR_MSK)),
10371 phba->HAregaddr);
James Smarta747c9c2009-11-18 15:41:10 -050010372 writel(hc_copy, phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -040010373 readl(phba->HAregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -050010374 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -040010375 } else
10376 ha_copy = phba->ha_copy;
dea31012005-04-17 16:05:31 -050010377
dea31012005-04-17 16:05:31 -050010378 work_ha_copy = ha_copy & phba->work_ha_mask;
10379
James Smart93996272008-08-24 21:50:30 -040010380 if (work_ha_copy) {
dea31012005-04-17 16:05:31 -050010381 if (work_ha_copy & HA_LATT) {
10382 if (phba->sli.sli_flag & LPFC_PROCESS_LA) {
10383 /*
10384 * Turn off Link Attention interrupts
10385 * until CLEAR_LA done
10386 */
James Smart5b75da22008-12-04 22:39:35 -050010387 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -050010388 phba->sli.sli_flag &= ~LPFC_PROCESS_LA;
James Smart9940b972011-03-11 16:06:12 -050010389 if (lpfc_readl(phba->HCregaddr, &control))
10390 goto unplug_error;
dea31012005-04-17 16:05:31 -050010391 control &= ~HC_LAINT_ENA;
10392 writel(control, phba->HCregaddr);
10393 readl(phba->HCregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -050010394 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -050010395 }
10396 else
10397 work_ha_copy &= ~HA_LATT;
10398 }
10399
James Smart93996272008-08-24 21:50:30 -040010400 if (work_ha_copy & ~(HA_ERATT | HA_MBATT | HA_LATT)) {
James Smart858c9f62007-06-17 19:56:39 -050010401 /*
10402 * Turn off Slow Rings interrupts, LPFC_ELS_RING is
10403 * the only slow ring.
10404 */
10405 status = (work_ha_copy &
10406 (HA_RXMASK << (4*LPFC_ELS_RING)));
10407 status >>= (4*LPFC_ELS_RING);
10408 if (status & HA_RXMASK) {
James Smart5b75da22008-12-04 22:39:35 -050010409 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart9940b972011-03-11 16:06:12 -050010410 if (lpfc_readl(phba->HCregaddr, &control))
10411 goto unplug_error;
James Smarta58cbd52007-08-02 11:09:43 -040010412
10413 lpfc_debugfs_slow_ring_trc(phba,
10414 "ISR slow ring: ctl:x%x stat:x%x isrcnt:x%x",
10415 control, status,
10416 (uint32_t)phba->sli.slistat.sli_intr);
10417
James Smart858c9f62007-06-17 19:56:39 -050010418 if (control & (HC_R0INT_ENA << LPFC_ELS_RING)) {
James Smarta58cbd52007-08-02 11:09:43 -040010419 lpfc_debugfs_slow_ring_trc(phba,
10420 "ISR Disable ring:"
10421 "pwork:x%x hawork:x%x wait:x%x",
10422 phba->work_ha, work_ha_copy,
10423 (uint32_t)((unsigned long)
James Smart5e9d9b82008-06-14 22:52:53 -040010424 &phba->work_waitq));
James Smarta58cbd52007-08-02 11:09:43 -040010425
James Smart858c9f62007-06-17 19:56:39 -050010426 control &=
10427 ~(HC_R0INT_ENA << LPFC_ELS_RING);
dea31012005-04-17 16:05:31 -050010428 writel(control, phba->HCregaddr);
10429 readl(phba->HCregaddr); /* flush */
dea31012005-04-17 16:05:31 -050010430 }
James Smarta58cbd52007-08-02 11:09:43 -040010431 else {
10432 lpfc_debugfs_slow_ring_trc(phba,
10433 "ISR slow ring: pwork:"
10434 "x%x hawork:x%x wait:x%x",
10435 phba->work_ha, work_ha_copy,
10436 (uint32_t)((unsigned long)
James Smart5e9d9b82008-06-14 22:52:53 -040010437 &phba->work_waitq));
James Smarta58cbd52007-08-02 11:09:43 -040010438 }
James Smart5b75da22008-12-04 22:39:35 -050010439 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -050010440 }
10441 }
James Smart5b75da22008-12-04 22:39:35 -050010442 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -040010443 if (work_ha_copy & HA_ERATT) {
James Smart9940b972011-03-11 16:06:12 -050010444 if (lpfc_sli_read_hs(phba))
10445 goto unplug_error;
James Smarta257bf92009-04-06 18:48:10 -040010446 /*
10447 * Check if there is a deferred error condition
10448 * is active
10449 */
10450 if ((HS_FFER1 & phba->work_hs) &&
10451 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
James Smartdcf2a4e2010-09-29 11:18:53 -040010452 HS_FFER6 | HS_FFER7 | HS_FFER8) &
10453 phba->work_hs)) {
James Smarta257bf92009-04-06 18:48:10 -040010454 phba->hba_flag |= DEFER_ERATT;
10455 /* Clear all interrupt enable conditions */
10456 writel(0, phba->HCregaddr);
10457 readl(phba->HCregaddr);
10458 }
10459 }
10460
James Smart93996272008-08-24 21:50:30 -040010461 if ((work_ha_copy & HA_MBATT) && (phba->sli.mbox_active)) {
James Smart92d7f7b2007-06-17 19:56:38 -050010462 pmb = phba->sli.mbox_active;
James Smart04c68492009-05-22 14:52:52 -040010463 pmbox = &pmb->u.mb;
James Smart34b02dc2008-08-24 21:49:55 -040010464 mbox = phba->mbox;
James Smart858c9f62007-06-17 19:56:39 -050010465 vport = pmb->vport;
James Smart92d7f7b2007-06-17 19:56:38 -050010466
10467 /* First check out the status word */
10468 lpfc_sli_pcimem_bcopy(mbox, pmbox, sizeof(uint32_t));
10469 if (pmbox->mbxOwner != OWN_HOST) {
James Smart5b75da22008-12-04 22:39:35 -050010470 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart92d7f7b2007-06-17 19:56:38 -050010471 /*
10472 * Stray Mailbox Interrupt, mbxCommand <cmd>
10473 * mbxStatus <status>
10474 */
James Smart09372822008-01-11 01:52:54 -050010475 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
James Smart92d7f7b2007-06-17 19:56:38 -050010476 LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -040010477 "(%d):0304 Stray Mailbox "
James Smart92d7f7b2007-06-17 19:56:38 -050010478 "Interrupt mbxCommand x%x "
10479 "mbxStatus x%x\n",
James Smarte8b62012007-08-02 11:10:09 -040010480 (vport ? vport->vpi : 0),
James Smart92d7f7b2007-06-17 19:56:38 -050010481 pmbox->mbxCommand,
10482 pmbox->mbxStatus);
James Smart09372822008-01-11 01:52:54 -050010483 /* clear mailbox attention bit */
10484 work_ha_copy &= ~HA_MBATT;
10485 } else {
James Smart97eab632008-04-07 10:16:05 -040010486 phba->sli.mbox_active = NULL;
James Smart5b75da22008-12-04 22:39:35 -050010487 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart09372822008-01-11 01:52:54 -050010488 phba->last_completion_time = jiffies;
10489 del_timer(&phba->sli.mbox_tmo);
James Smart09372822008-01-11 01:52:54 -050010490 if (pmb->mbox_cmpl) {
10491 lpfc_sli_pcimem_bcopy(mbox, pmbox,
10492 MAILBOX_CMD_SIZE);
James Smart7a470272010-03-15 11:25:20 -040010493 if (pmb->out_ext_byte_len &&
10494 pmb->context2)
10495 lpfc_sli_pcimem_bcopy(
10496 phba->mbox_ext,
10497 pmb->context2,
10498 pmb->out_ext_byte_len);
James Smart858c9f62007-06-17 19:56:39 -050010499 }
James Smart09372822008-01-11 01:52:54 -050010500 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
10501 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
10502
10503 lpfc_debugfs_disc_trc(vport,
10504 LPFC_DISC_TRC_MBOX_VPORT,
10505 "MBOX dflt rpi: : "
10506 "status:x%x rpi:x%x",
10507 (uint32_t)pmbox->mbxStatus,
10508 pmbox->un.varWords[0], 0);
10509
10510 if (!pmbox->mbxStatus) {
10511 mp = (struct lpfc_dmabuf *)
10512 (pmb->context1);
10513 ndlp = (struct lpfc_nodelist *)
10514 pmb->context2;
10515
10516 /* Reg_LOGIN of dflt RPI was
10517 * successful. new lets get
10518 * rid of the RPI using the
10519 * same mbox buffer.
10520 */
10521 lpfc_unreg_login(phba,
10522 vport->vpi,
10523 pmbox->un.varWords[0],
10524 pmb);
10525 pmb->mbox_cmpl =
10526 lpfc_mbx_cmpl_dflt_rpi;
10527 pmb->context1 = mp;
10528 pmb->context2 = ndlp;
10529 pmb->vport = vport;
James Smart58da1ff2008-04-07 10:15:56 -040010530 rc = lpfc_sli_issue_mbox(phba,
10531 pmb,
10532 MBX_NOWAIT);
10533 if (rc != MBX_BUSY)
10534 lpfc_printf_log(phba,
10535 KERN_ERR,
10536 LOG_MBOX | LOG_SLI,
James Smartd7c255b2008-08-24 21:50:00 -040010537 "0350 rc should have"
James Smart6a9c52c2009-10-02 15:16:51 -040010538 "been MBX_BUSY\n");
James Smart3772a992009-05-22 14:50:54 -040010539 if (rc != MBX_NOT_FINISHED)
10540 goto send_current_mbox;
James Smart09372822008-01-11 01:52:54 -050010541 }
10542 }
James Smart5b75da22008-12-04 22:39:35 -050010543 spin_lock_irqsave(
10544 &phba->pport->work_port_lock,
10545 iflag);
James Smart09372822008-01-11 01:52:54 -050010546 phba->pport->work_port_events &=
10547 ~WORKER_MBOX_TMO;
James Smart5b75da22008-12-04 22:39:35 -050010548 spin_unlock_irqrestore(
10549 &phba->pport->work_port_lock,
10550 iflag);
James Smart09372822008-01-11 01:52:54 -050010551 lpfc_mbox_cmpl_put(phba, pmb);
James Smart858c9f62007-06-17 19:56:39 -050010552 }
James Smart97eab632008-04-07 10:16:05 -040010553 } else
James Smart5b75da22008-12-04 22:39:35 -050010554 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -040010555
James Smart92d7f7b2007-06-17 19:56:38 -050010556 if ((work_ha_copy & HA_MBATT) &&
10557 (phba->sli.mbox_active == NULL)) {
James Smart858c9f62007-06-17 19:56:39 -050010558send_current_mbox:
James Smart92d7f7b2007-06-17 19:56:38 -050010559 /* Process next mailbox command if there is one */
James Smart58da1ff2008-04-07 10:15:56 -040010560 do {
10561 rc = lpfc_sli_issue_mbox(phba, NULL,
10562 MBX_NOWAIT);
10563 } while (rc == MBX_NOT_FINISHED);
10564 if (rc != MBX_SUCCESS)
10565 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
10566 LOG_SLI, "0349 rc should be "
James Smart6a9c52c2009-10-02 15:16:51 -040010567 "MBX_SUCCESS\n");
James Smart92d7f7b2007-06-17 19:56:38 -050010568 }
10569
James Smart5b75da22008-12-04 22:39:35 -050010570 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -050010571 phba->work_ha |= work_ha_copy;
James Smart5b75da22008-12-04 22:39:35 -050010572 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart5e9d9b82008-06-14 22:52:53 -040010573 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -050010574 }
James Smart93996272008-08-24 21:50:30 -040010575 return IRQ_HANDLED;
James Smart9940b972011-03-11 16:06:12 -050010576unplug_error:
10577 spin_unlock_irqrestore(&phba->hbalock, iflag);
10578 return IRQ_HANDLED;
dea31012005-04-17 16:05:31 -050010579
James Smart3772a992009-05-22 14:50:54 -040010580} /* lpfc_sli_sp_intr_handler */
James Smart93996272008-08-24 21:50:30 -040010581
10582/**
James Smart3772a992009-05-22 14:50:54 -040010583 * lpfc_sli_fp_intr_handler - Fast-path interrupt handler to SLI-3 device.
James Smart93996272008-08-24 21:50:30 -040010584 * @irq: Interrupt number.
10585 * @dev_id: The device context pointer.
10586 *
10587 * This function is directly called from the PCI layer as an interrupt
James Smart3772a992009-05-22 14:50:54 -040010588 * service routine when device with SLI-3 interface spec is enabled with
10589 * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
10590 * ring event in the HBA. However, when the device is enabled with either
10591 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
10592 * device-level interrupt handler. When the PCI slot is in error recovery
10593 * or the HBA is undergoing initialization, the interrupt handler will not
10594 * process the interrupt. The SCSI FCP fast-path ring event are handled in
10595 * the intrrupt context. This function is called without any lock held.
10596 * It gets the hbalock to access and update SLI data structures.
James Smart93996272008-08-24 21:50:30 -040010597 *
10598 * This function returns IRQ_HANDLED when interrupt is handled else it
10599 * returns IRQ_NONE.
10600 **/
10601irqreturn_t
James Smart3772a992009-05-22 14:50:54 -040010602lpfc_sli_fp_intr_handler(int irq, void *dev_id)
James Smart93996272008-08-24 21:50:30 -040010603{
10604 struct lpfc_hba *phba;
10605 uint32_t ha_copy;
10606 unsigned long status;
James Smart5b75da22008-12-04 22:39:35 -050010607 unsigned long iflag;
James Smart93996272008-08-24 21:50:30 -040010608
10609 /* Get the driver's phba structure from the dev_id and
10610 * assume the HBA is not interrupting.
10611 */
10612 phba = (struct lpfc_hba *) dev_id;
10613
10614 if (unlikely(!phba))
10615 return IRQ_NONE;
dea31012005-04-17 16:05:31 -050010616
10617 /*
James Smart93996272008-08-24 21:50:30 -040010618 * Stuff needs to be attented to when this function is invoked as an
10619 * individual interrupt handler in MSI-X multi-message interrupt mode
dea31012005-04-17 16:05:31 -050010620 */
James Smart93996272008-08-24 21:50:30 -040010621 if (phba->intr_type == MSIX) {
James Smart3772a992009-05-22 14:50:54 -040010622 /* Check device state for handling interrupt */
10623 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -040010624 return IRQ_NONE;
10625 /* Need to read HA REG for FCP ring and other ring events */
James Smart9940b972011-03-11 16:06:12 -050010626 if (lpfc_readl(phba->HAregaddr, &ha_copy))
10627 return IRQ_HANDLED;
James Smart93996272008-08-24 21:50:30 -040010628 /* Clear up only attention source related to fast-path */
James Smart5b75da22008-12-04 22:39:35 -050010629 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -040010630 /*
10631 * If there is deferred error attention, do not check for
10632 * any interrupt.
10633 */
10634 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
James Smart3772a992009-05-22 14:50:54 -040010635 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -040010636 return IRQ_NONE;
10637 }
James Smart93996272008-08-24 21:50:30 -040010638 writel((ha_copy & (HA_R0_CLR_MSK | HA_R1_CLR_MSK)),
10639 phba->HAregaddr);
10640 readl(phba->HAregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -050010641 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -040010642 } else
10643 ha_copy = phba->ha_copy;
10644
10645 /*
10646 * Process all events on FCP ring. Take the optimized path for FCP IO.
10647 */
10648 ha_copy &= ~(phba->work_ha_mask);
10649
10650 status = (ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
dea31012005-04-17 16:05:31 -050010651 status >>= (4*LPFC_FCP_RING);
James Smart858c9f62007-06-17 19:56:39 -050010652 if (status & HA_RXMASK)
dea31012005-04-17 16:05:31 -050010653 lpfc_sli_handle_fast_ring_event(phba,
10654 &phba->sli.ring[LPFC_FCP_RING],
10655 status);
James Smarta4bc3372006-12-02 13:34:16 -050010656
10657 if (phba->cfg_multi_ring_support == 2) {
10658 /*
James Smart93996272008-08-24 21:50:30 -040010659 * Process all events on extra ring. Take the optimized path
10660 * for extra ring IO.
James Smarta4bc3372006-12-02 13:34:16 -050010661 */
James Smart93996272008-08-24 21:50:30 -040010662 status = (ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
James Smarta4bc3372006-12-02 13:34:16 -050010663 status >>= (4*LPFC_EXTRA_RING);
James Smart858c9f62007-06-17 19:56:39 -050010664 if (status & HA_RXMASK) {
James Smarta4bc3372006-12-02 13:34:16 -050010665 lpfc_sli_handle_fast_ring_event(phba,
10666 &phba->sli.ring[LPFC_EXTRA_RING],
10667 status);
10668 }
10669 }
dea31012005-04-17 16:05:31 -050010670 return IRQ_HANDLED;
James Smart3772a992009-05-22 14:50:54 -040010671} /* lpfc_sli_fp_intr_handler */
dea31012005-04-17 16:05:31 -050010672
James Smart93996272008-08-24 21:50:30 -040010673/**
James Smart3772a992009-05-22 14:50:54 -040010674 * lpfc_sli_intr_handler - Device-level interrupt handler to SLI-3 device
James Smart93996272008-08-24 21:50:30 -040010675 * @irq: Interrupt number.
10676 * @dev_id: The device context pointer.
10677 *
James Smart3772a992009-05-22 14:50:54 -040010678 * This function is the HBA device-level interrupt handler to device with
10679 * SLI-3 interface spec, called from the PCI layer when either MSI or
10680 * Pin-IRQ interrupt mode is enabled and there is an event in the HBA which
10681 * requires driver attention. This function invokes the slow-path interrupt
10682 * attention handling function and fast-path interrupt attention handling
10683 * function in turn to process the relevant HBA attention events. This
10684 * function is called without any lock held. It gets the hbalock to access
10685 * and update SLI data structures.
James Smart93996272008-08-24 21:50:30 -040010686 *
10687 * This function returns IRQ_HANDLED when interrupt is handled, else it
10688 * returns IRQ_NONE.
10689 **/
10690irqreturn_t
James Smart3772a992009-05-22 14:50:54 -040010691lpfc_sli_intr_handler(int irq, void *dev_id)
James Smart93996272008-08-24 21:50:30 -040010692{
10693 struct lpfc_hba *phba;
10694 irqreturn_t sp_irq_rc, fp_irq_rc;
10695 unsigned long status1, status2;
James Smarta747c9c2009-11-18 15:41:10 -050010696 uint32_t hc_copy;
James Smart93996272008-08-24 21:50:30 -040010697
10698 /*
10699 * Get the driver's phba structure from the dev_id and
10700 * assume the HBA is not interrupting.
10701 */
10702 phba = (struct lpfc_hba *) dev_id;
10703
10704 if (unlikely(!phba))
10705 return IRQ_NONE;
10706
James Smart3772a992009-05-22 14:50:54 -040010707 /* Check device state for handling interrupt */
10708 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -040010709 return IRQ_NONE;
10710
10711 spin_lock(&phba->hbalock);
James Smart9940b972011-03-11 16:06:12 -050010712 if (lpfc_readl(phba->HAregaddr, &phba->ha_copy)) {
10713 spin_unlock(&phba->hbalock);
10714 return IRQ_HANDLED;
10715 }
10716
James Smart93996272008-08-24 21:50:30 -040010717 if (unlikely(!phba->ha_copy)) {
10718 spin_unlock(&phba->hbalock);
10719 return IRQ_NONE;
10720 } else if (phba->ha_copy & HA_ERATT) {
10721 if (phba->hba_flag & HBA_ERATT_HANDLED)
10722 /* ERATT polling has handled ERATT */
10723 phba->ha_copy &= ~HA_ERATT;
10724 else
10725 /* Indicate interrupt handler handles ERATT */
10726 phba->hba_flag |= HBA_ERATT_HANDLED;
10727 }
10728
James Smarta257bf92009-04-06 18:48:10 -040010729 /*
10730 * If there is deferred error attention, do not check for any interrupt.
10731 */
10732 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
Dan Carpenterec21b3b2010-08-08 00:15:17 +020010733 spin_unlock(&phba->hbalock);
James Smarta257bf92009-04-06 18:48:10 -040010734 return IRQ_NONE;
10735 }
10736
James Smart93996272008-08-24 21:50:30 -040010737 /* Clear attention sources except link and error attentions */
James Smart9940b972011-03-11 16:06:12 -050010738 if (lpfc_readl(phba->HCregaddr, &hc_copy)) {
10739 spin_unlock(&phba->hbalock);
10740 return IRQ_HANDLED;
10741 }
James Smarta747c9c2009-11-18 15:41:10 -050010742 writel(hc_copy & ~(HC_MBINT_ENA | HC_R0INT_ENA | HC_R1INT_ENA
10743 | HC_R2INT_ENA | HC_LAINT_ENA | HC_ERINT_ENA),
10744 phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -040010745 writel((phba->ha_copy & ~(HA_LATT | HA_ERATT)), phba->HAregaddr);
James Smarta747c9c2009-11-18 15:41:10 -050010746 writel(hc_copy, phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -040010747 readl(phba->HAregaddr); /* flush */
10748 spin_unlock(&phba->hbalock);
10749
10750 /*
10751 * Invokes slow-path host attention interrupt handling as appropriate.
10752 */
10753
10754 /* status of events with mailbox and link attention */
10755 status1 = phba->ha_copy & (HA_MBATT | HA_LATT | HA_ERATT);
10756
10757 /* status of events with ELS ring */
10758 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_ELS_RING)));
10759 status2 >>= (4*LPFC_ELS_RING);
10760
10761 if (status1 || (status2 & HA_RXMASK))
James Smart3772a992009-05-22 14:50:54 -040010762 sp_irq_rc = lpfc_sli_sp_intr_handler(irq, dev_id);
James Smart93996272008-08-24 21:50:30 -040010763 else
10764 sp_irq_rc = IRQ_NONE;
10765
10766 /*
10767 * Invoke fast-path host attention interrupt handling as appropriate.
10768 */
10769
10770 /* status of events with FCP ring */
10771 status1 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
10772 status1 >>= (4*LPFC_FCP_RING);
10773
10774 /* status of events with extra ring */
10775 if (phba->cfg_multi_ring_support == 2) {
10776 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
10777 status2 >>= (4*LPFC_EXTRA_RING);
10778 } else
10779 status2 = 0;
10780
10781 if ((status1 & HA_RXMASK) || (status2 & HA_RXMASK))
James Smart3772a992009-05-22 14:50:54 -040010782 fp_irq_rc = lpfc_sli_fp_intr_handler(irq, dev_id);
James Smart93996272008-08-24 21:50:30 -040010783 else
10784 fp_irq_rc = IRQ_NONE;
10785
10786 /* Return device-level interrupt handling status */
10787 return (sp_irq_rc == IRQ_HANDLED) ? sp_irq_rc : fp_irq_rc;
James Smart3772a992009-05-22 14:50:54 -040010788} /* lpfc_sli_intr_handler */
James Smart4f774512009-05-22 14:52:35 -040010789
10790/**
10791 * lpfc_sli4_fcp_xri_abort_event_proc - Process fcp xri abort event
10792 * @phba: pointer to lpfc hba data structure.
10793 *
10794 * This routine is invoked by the worker thread to process all the pending
10795 * SLI4 FCP abort XRI events.
10796 **/
10797void lpfc_sli4_fcp_xri_abort_event_proc(struct lpfc_hba *phba)
10798{
10799 struct lpfc_cq_event *cq_event;
10800
10801 /* First, declare the fcp xri abort event has been handled */
10802 spin_lock_irq(&phba->hbalock);
10803 phba->hba_flag &= ~FCP_XRI_ABORT_EVENT;
10804 spin_unlock_irq(&phba->hbalock);
10805 /* Now, handle all the fcp xri abort events */
10806 while (!list_empty(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue)) {
10807 /* Get the first event from the head of the event queue */
10808 spin_lock_irq(&phba->hbalock);
10809 list_remove_head(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue,
10810 cq_event, struct lpfc_cq_event, list);
10811 spin_unlock_irq(&phba->hbalock);
10812 /* Notify aborted XRI for FCP work queue */
10813 lpfc_sli4_fcp_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
10814 /* Free the event processed back to the free pool */
10815 lpfc_sli4_cq_event_release(phba, cq_event);
10816 }
10817}
10818
10819/**
10820 * lpfc_sli4_els_xri_abort_event_proc - Process els xri abort event
10821 * @phba: pointer to lpfc hba data structure.
10822 *
10823 * This routine is invoked by the worker thread to process all the pending
10824 * SLI4 els abort xri events.
10825 **/
10826void lpfc_sli4_els_xri_abort_event_proc(struct lpfc_hba *phba)
10827{
10828 struct lpfc_cq_event *cq_event;
10829
10830 /* First, declare the els xri abort event has been handled */
10831 spin_lock_irq(&phba->hbalock);
10832 phba->hba_flag &= ~ELS_XRI_ABORT_EVENT;
10833 spin_unlock_irq(&phba->hbalock);
10834 /* Now, handle all the els xri abort events */
10835 while (!list_empty(&phba->sli4_hba.sp_els_xri_aborted_work_queue)) {
10836 /* Get the first event from the head of the event queue */
10837 spin_lock_irq(&phba->hbalock);
10838 list_remove_head(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
10839 cq_event, struct lpfc_cq_event, list);
10840 spin_unlock_irq(&phba->hbalock);
10841 /* Notify aborted XRI for ELS work queue */
10842 lpfc_sli4_els_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
10843 /* Free the event processed back to the free pool */
10844 lpfc_sli4_cq_event_release(phba, cq_event);
10845 }
10846}
10847
James Smart341af102010-01-26 23:07:37 -050010848/**
10849 * lpfc_sli4_iocb_param_transfer - Transfer pIocbOut and cmpl status to pIocbIn
10850 * @phba: pointer to lpfc hba data structure
10851 * @pIocbIn: pointer to the rspiocbq
10852 * @pIocbOut: pointer to the cmdiocbq
10853 * @wcqe: pointer to the complete wcqe
10854 *
10855 * This routine transfers the fields of a command iocbq to a response iocbq
10856 * by copying all the IOCB fields from command iocbq and transferring the
10857 * completion status information from the complete wcqe.
10858 **/
James Smart4f774512009-05-22 14:52:35 -040010859static void
James Smart341af102010-01-26 23:07:37 -050010860lpfc_sli4_iocb_param_transfer(struct lpfc_hba *phba,
10861 struct lpfc_iocbq *pIocbIn,
James Smart4f774512009-05-22 14:52:35 -040010862 struct lpfc_iocbq *pIocbOut,
10863 struct lpfc_wcqe_complete *wcqe)
10864{
James Smart341af102010-01-26 23:07:37 -050010865 unsigned long iflags;
James Smartacd68592012-01-18 16:25:09 -050010866 uint32_t status;
James Smart4f774512009-05-22 14:52:35 -040010867 size_t offset = offsetof(struct lpfc_iocbq, iocb);
10868
10869 memcpy((char *)pIocbIn + offset, (char *)pIocbOut + offset,
10870 sizeof(struct lpfc_iocbq) - offset);
James Smart4f774512009-05-22 14:52:35 -040010871 /* Map WCQE parameters into irspiocb parameters */
James Smartacd68592012-01-18 16:25:09 -050010872 status = bf_get(lpfc_wcqe_c_status, wcqe);
10873 pIocbIn->iocb.ulpStatus = (status & LPFC_IOCB_STATUS_MASK);
James Smart4f774512009-05-22 14:52:35 -040010874 if (pIocbOut->iocb_flag & LPFC_IO_FCP)
10875 if (pIocbIn->iocb.ulpStatus == IOSTAT_FCP_RSP_ERROR)
10876 pIocbIn->iocb.un.fcpi.fcpi_parm =
10877 pIocbOut->iocb.un.fcpi.fcpi_parm -
10878 wcqe->total_data_placed;
10879 else
10880 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
James Smart695a8142010-01-26 23:08:03 -050010881 else {
James Smart4f774512009-05-22 14:52:35 -040010882 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
James Smart695a8142010-01-26 23:08:03 -050010883 pIocbIn->iocb.un.genreq64.bdl.bdeSize = wcqe->total_data_placed;
10884 }
James Smart341af102010-01-26 23:07:37 -050010885
James Smartacd68592012-01-18 16:25:09 -050010886 /* Convert BG errors for completion status */
10887 if (status == CQE_STATUS_DI_ERROR) {
10888 pIocbIn->iocb.ulpStatus = IOSTAT_LOCAL_REJECT;
10889
10890 if (bf_get(lpfc_wcqe_c_bg_edir, wcqe))
10891 pIocbIn->iocb.un.ulpWord[4] = IOERR_RX_DMA_FAILED;
10892 else
10893 pIocbIn->iocb.un.ulpWord[4] = IOERR_TX_DMA_FAILED;
10894
10895 pIocbIn->iocb.unsli3.sli3_bg.bgstat = 0;
10896 if (bf_get(lpfc_wcqe_c_bg_ge, wcqe)) /* Guard Check failed */
10897 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
10898 BGS_GUARD_ERR_MASK;
10899 if (bf_get(lpfc_wcqe_c_bg_ae, wcqe)) /* App Tag Check failed */
10900 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
10901 BGS_APPTAG_ERR_MASK;
10902 if (bf_get(lpfc_wcqe_c_bg_re, wcqe)) /* Ref Tag Check failed */
10903 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
10904 BGS_REFTAG_ERR_MASK;
10905
10906 /* Check to see if there was any good data before the error */
10907 if (bf_get(lpfc_wcqe_c_bg_tdpv, wcqe)) {
10908 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
10909 BGS_HI_WATER_MARK_PRESENT_MASK;
10910 pIocbIn->iocb.unsli3.sli3_bg.bghm =
10911 wcqe->total_data_placed;
10912 }
10913
10914 /*
10915 * Set ALL the error bits to indicate we don't know what
10916 * type of error it is.
10917 */
10918 if (!pIocbIn->iocb.unsli3.sli3_bg.bgstat)
10919 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
10920 (BGS_REFTAG_ERR_MASK | BGS_APPTAG_ERR_MASK |
10921 BGS_GUARD_ERR_MASK);
10922 }
10923
James Smart341af102010-01-26 23:07:37 -050010924 /* Pick up HBA exchange busy condition */
10925 if (bf_get(lpfc_wcqe_c_xb, wcqe)) {
10926 spin_lock_irqsave(&phba->hbalock, iflags);
10927 pIocbIn->iocb_flag |= LPFC_EXCHANGE_BUSY;
10928 spin_unlock_irqrestore(&phba->hbalock, iflags);
10929 }
James Smart4f774512009-05-22 14:52:35 -040010930}
10931
10932/**
James Smart45ed1192009-10-02 15:17:02 -040010933 * lpfc_sli4_els_wcqe_to_rspiocbq - Get response iocbq from els wcqe
10934 * @phba: Pointer to HBA context object.
10935 * @wcqe: Pointer to work-queue completion queue entry.
10936 *
10937 * This routine handles an ELS work-queue completion event and construct
10938 * a pseudo response ELS IODBQ from the SLI4 ELS WCQE for the common
10939 * discovery engine to handle.
10940 *
10941 * Return: Pointer to the receive IOCBQ, NULL otherwise.
10942 **/
10943static struct lpfc_iocbq *
10944lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *phba,
10945 struct lpfc_iocbq *irspiocbq)
10946{
10947 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
10948 struct lpfc_iocbq *cmdiocbq;
10949 struct lpfc_wcqe_complete *wcqe;
10950 unsigned long iflags;
10951
10952 wcqe = &irspiocbq->cq_event.cqe.wcqe_cmpl;
10953 spin_lock_irqsave(&phba->hbalock, iflags);
10954 pring->stats.iocb_event++;
10955 /* Look up the ELS command IOCB and create pseudo response IOCB */
10956 cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
10957 bf_get(lpfc_wcqe_c_request_tag, wcqe));
10958 spin_unlock_irqrestore(&phba->hbalock, iflags);
10959
10960 if (unlikely(!cmdiocbq)) {
10961 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
10962 "0386 ELS complete with no corresponding "
10963 "cmdiocb: iotag (%d)\n",
10964 bf_get(lpfc_wcqe_c_request_tag, wcqe));
10965 lpfc_sli_release_iocbq(phba, irspiocbq);
10966 return NULL;
10967 }
10968
10969 /* Fake the irspiocbq and copy necessary response information */
James Smart341af102010-01-26 23:07:37 -050010970 lpfc_sli4_iocb_param_transfer(phba, irspiocbq, cmdiocbq, wcqe);
James Smart45ed1192009-10-02 15:17:02 -040010971
10972 return irspiocbq;
10973}
10974
10975/**
James Smart04c68492009-05-22 14:52:52 -040010976 * lpfc_sli4_sp_handle_async_event - Handle an asynchroous event
10977 * @phba: Pointer to HBA context object.
10978 * @cqe: Pointer to mailbox completion queue entry.
10979 *
10980 * This routine process a mailbox completion queue entry with asynchrous
10981 * event.
10982 *
10983 * Return: true if work posted to worker thread, otherwise false.
10984 **/
10985static bool
10986lpfc_sli4_sp_handle_async_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
10987{
10988 struct lpfc_cq_event *cq_event;
10989 unsigned long iflags;
10990
10991 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
10992 "0392 Async Event: word0:x%x, word1:x%x, "
10993 "word2:x%x, word3:x%x\n", mcqe->word0,
10994 mcqe->mcqe_tag0, mcqe->mcqe_tag1, mcqe->trailer);
10995
10996 /* Allocate a new internal CQ_EVENT entry */
10997 cq_event = lpfc_sli4_cq_event_alloc(phba);
10998 if (!cq_event) {
10999 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11000 "0394 Failed to allocate CQ_EVENT entry\n");
11001 return false;
11002 }
11003
11004 /* Move the CQE into an asynchronous event entry */
11005 memcpy(&cq_event->cqe, mcqe, sizeof(struct lpfc_mcqe));
11006 spin_lock_irqsave(&phba->hbalock, iflags);
11007 list_add_tail(&cq_event->list, &phba->sli4_hba.sp_asynce_work_queue);
11008 /* Set the async event flag */
11009 phba->hba_flag |= ASYNC_EVENT;
11010 spin_unlock_irqrestore(&phba->hbalock, iflags);
11011
11012 return true;
11013}
11014
11015/**
11016 * lpfc_sli4_sp_handle_mbox_event - Handle a mailbox completion event
11017 * @phba: Pointer to HBA context object.
11018 * @cqe: Pointer to mailbox completion queue entry.
11019 *
11020 * This routine process a mailbox completion queue entry with mailbox
11021 * completion event.
11022 *
11023 * Return: true if work posted to worker thread, otherwise false.
11024 **/
11025static bool
11026lpfc_sli4_sp_handle_mbox_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
11027{
11028 uint32_t mcqe_status;
11029 MAILBOX_t *mbox, *pmbox;
11030 struct lpfc_mqe *mqe;
11031 struct lpfc_vport *vport;
11032 struct lpfc_nodelist *ndlp;
11033 struct lpfc_dmabuf *mp;
11034 unsigned long iflags;
11035 LPFC_MBOXQ_t *pmb;
11036 bool workposted = false;
11037 int rc;
11038
11039 /* If not a mailbox complete MCQE, out by checking mailbox consume */
11040 if (!bf_get(lpfc_trailer_completed, mcqe))
11041 goto out_no_mqe_complete;
11042
11043 /* Get the reference to the active mbox command */
11044 spin_lock_irqsave(&phba->hbalock, iflags);
11045 pmb = phba->sli.mbox_active;
11046 if (unlikely(!pmb)) {
11047 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
11048 "1832 No pending MBOX command to handle\n");
11049 spin_unlock_irqrestore(&phba->hbalock, iflags);
11050 goto out_no_mqe_complete;
11051 }
11052 spin_unlock_irqrestore(&phba->hbalock, iflags);
11053 mqe = &pmb->u.mqe;
11054 pmbox = (MAILBOX_t *)&pmb->u.mqe;
11055 mbox = phba->mbox;
11056 vport = pmb->vport;
11057
11058 /* Reset heartbeat timer */
11059 phba->last_completion_time = jiffies;
11060 del_timer(&phba->sli.mbox_tmo);
11061
11062 /* Move mbox data to caller's mailbox region, do endian swapping */
11063 if (pmb->mbox_cmpl && mbox)
11064 lpfc_sli_pcimem_bcopy(mbox, mqe, sizeof(struct lpfc_mqe));
James Smart04c68492009-05-22 14:52:52 -040011065
James Smart73d91e52011-10-10 21:32:10 -040011066 /*
11067 * For mcqe errors, conditionally move a modified error code to
11068 * the mbox so that the error will not be missed.
11069 */
11070 mcqe_status = bf_get(lpfc_mcqe_status, mcqe);
11071 if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
11072 if (bf_get(lpfc_mqe_status, mqe) == MBX_SUCCESS)
11073 bf_set(lpfc_mqe_status, mqe,
11074 (LPFC_MBX_ERROR_RANGE | mcqe_status));
11075 }
James Smart04c68492009-05-22 14:52:52 -040011076 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
11077 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
11078 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_MBOX_VPORT,
11079 "MBOX dflt rpi: status:x%x rpi:x%x",
11080 mcqe_status,
11081 pmbox->un.varWords[0], 0);
11082 if (mcqe_status == MB_CQE_STATUS_SUCCESS) {
11083 mp = (struct lpfc_dmabuf *)(pmb->context1);
11084 ndlp = (struct lpfc_nodelist *)pmb->context2;
11085 /* Reg_LOGIN of dflt RPI was successful. Now lets get
11086 * RID of the PPI using the same mbox buffer.
11087 */
11088 lpfc_unreg_login(phba, vport->vpi,
11089 pmbox->un.varWords[0], pmb);
11090 pmb->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
11091 pmb->context1 = mp;
11092 pmb->context2 = ndlp;
11093 pmb->vport = vport;
11094 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
11095 if (rc != MBX_BUSY)
11096 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
11097 LOG_SLI, "0385 rc should "
11098 "have been MBX_BUSY\n");
11099 if (rc != MBX_NOT_FINISHED)
11100 goto send_current_mbox;
11101 }
11102 }
11103 spin_lock_irqsave(&phba->pport->work_port_lock, iflags);
11104 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
11105 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflags);
11106
11107 /* There is mailbox completion work to do */
11108 spin_lock_irqsave(&phba->hbalock, iflags);
11109 __lpfc_mbox_cmpl_put(phba, pmb);
11110 phba->work_ha |= HA_MBATT;
11111 spin_unlock_irqrestore(&phba->hbalock, iflags);
11112 workposted = true;
11113
11114send_current_mbox:
11115 spin_lock_irqsave(&phba->hbalock, iflags);
11116 /* Release the mailbox command posting token */
11117 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
11118 /* Setting active mailbox pointer need to be in sync to flag clear */
11119 phba->sli.mbox_active = NULL;
11120 spin_unlock_irqrestore(&phba->hbalock, iflags);
11121 /* Wake up worker thread to post the next pending mailbox command */
11122 lpfc_worker_wake_up(phba);
11123out_no_mqe_complete:
11124 if (bf_get(lpfc_trailer_consumed, mcqe))
11125 lpfc_sli4_mq_release(phba->sli4_hba.mbx_wq);
11126 return workposted;
11127}
11128
11129/**
11130 * lpfc_sli4_sp_handle_mcqe - Process a mailbox completion queue entry
11131 * @phba: Pointer to HBA context object.
11132 * @cqe: Pointer to mailbox completion queue entry.
11133 *
11134 * This routine process a mailbox completion queue entry, it invokes the
11135 * proper mailbox complete handling or asynchrous event handling routine
11136 * according to the MCQE's async bit.
11137 *
11138 * Return: true if work posted to worker thread, otherwise false.
11139 **/
11140static bool
11141lpfc_sli4_sp_handle_mcqe(struct lpfc_hba *phba, struct lpfc_cqe *cqe)
11142{
11143 struct lpfc_mcqe mcqe;
11144 bool workposted;
11145
11146 /* Copy the mailbox MCQE and convert endian order as needed */
11147 lpfc_sli_pcimem_bcopy(cqe, &mcqe, sizeof(struct lpfc_mcqe));
11148
11149 /* Invoke the proper event handling routine */
11150 if (!bf_get(lpfc_trailer_async, &mcqe))
11151 workposted = lpfc_sli4_sp_handle_mbox_event(phba, &mcqe);
11152 else
11153 workposted = lpfc_sli4_sp_handle_async_event(phba, &mcqe);
11154 return workposted;
11155}
11156
11157/**
James Smart4f774512009-05-22 14:52:35 -040011158 * lpfc_sli4_sp_handle_els_wcqe - Handle els work-queue completion event
11159 * @phba: Pointer to HBA context object.
11160 * @wcqe: Pointer to work-queue completion queue entry.
11161 *
11162 * This routine handles an ELS work-queue completion event.
11163 *
11164 * Return: true if work posted to worker thread, otherwise false.
11165 **/
11166static bool
11167lpfc_sli4_sp_handle_els_wcqe(struct lpfc_hba *phba,
11168 struct lpfc_wcqe_complete *wcqe)
11169{
James Smart4f774512009-05-22 14:52:35 -040011170 struct lpfc_iocbq *irspiocbq;
11171 unsigned long iflags;
James Smart2a9bf3d2010-06-07 15:24:45 -040011172 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_FCP_RING];
James Smart4f774512009-05-22 14:52:35 -040011173
James Smart45ed1192009-10-02 15:17:02 -040011174 /* Get an irspiocbq for later ELS response processing use */
James Smart4f774512009-05-22 14:52:35 -040011175 irspiocbq = lpfc_sli_get_iocbq(phba);
11176 if (!irspiocbq) {
11177 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart2a9bf3d2010-06-07 15:24:45 -040011178 "0387 NO IOCBQ data: txq_cnt=%d iocb_cnt=%d "
11179 "fcp_txcmplq_cnt=%d, els_txcmplq_cnt=%d\n",
11180 pring->txq_cnt, phba->iocb_cnt,
11181 phba->sli.ring[LPFC_FCP_RING].txcmplq_cnt,
11182 phba->sli.ring[LPFC_ELS_RING].txcmplq_cnt);
James Smart45ed1192009-10-02 15:17:02 -040011183 return false;
James Smart4f774512009-05-22 14:52:35 -040011184 }
James Smart4f774512009-05-22 14:52:35 -040011185
James Smart45ed1192009-10-02 15:17:02 -040011186 /* Save off the slow-path queue event for work thread to process */
11187 memcpy(&irspiocbq->cq_event.cqe.wcqe_cmpl, wcqe, sizeof(*wcqe));
James Smart4f774512009-05-22 14:52:35 -040011188 spin_lock_irqsave(&phba->hbalock, iflags);
James Smart4d9ab992009-10-02 15:16:39 -040011189 list_add_tail(&irspiocbq->cq_event.list,
James Smart45ed1192009-10-02 15:17:02 -040011190 &phba->sli4_hba.sp_queue_event);
11191 phba->hba_flag |= HBA_SP_QUEUE_EVT;
James Smart4f774512009-05-22 14:52:35 -040011192 spin_unlock_irqrestore(&phba->hbalock, iflags);
James Smart4f774512009-05-22 14:52:35 -040011193
James Smart45ed1192009-10-02 15:17:02 -040011194 return true;
James Smart4f774512009-05-22 14:52:35 -040011195}
11196
11197/**
11198 * lpfc_sli4_sp_handle_rel_wcqe - Handle slow-path WQ entry consumed event
11199 * @phba: Pointer to HBA context object.
11200 * @wcqe: Pointer to work-queue completion queue entry.
11201 *
11202 * This routine handles slow-path WQ entry comsumed event by invoking the
11203 * proper WQ release routine to the slow-path WQ.
11204 **/
11205static void
11206lpfc_sli4_sp_handle_rel_wcqe(struct lpfc_hba *phba,
11207 struct lpfc_wcqe_release *wcqe)
11208{
James Smart2e90f4b2011-12-13 13:22:37 -050011209 /* sanity check on queue memory */
11210 if (unlikely(!phba->sli4_hba.els_wq))
11211 return;
James Smart4f774512009-05-22 14:52:35 -040011212 /* Check for the slow-path ELS work queue */
11213 if (bf_get(lpfc_wcqe_r_wq_id, wcqe) == phba->sli4_hba.els_wq->queue_id)
11214 lpfc_sli4_wq_release(phba->sli4_hba.els_wq,
11215 bf_get(lpfc_wcqe_r_wqe_index, wcqe));
11216 else
11217 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11218 "2579 Slow-path wqe consume event carries "
11219 "miss-matched qid: wcqe-qid=x%x, sp-qid=x%x\n",
11220 bf_get(lpfc_wcqe_r_wqe_index, wcqe),
11221 phba->sli4_hba.els_wq->queue_id);
11222}
11223
11224/**
11225 * lpfc_sli4_sp_handle_abort_xri_wcqe - Handle a xri abort event
11226 * @phba: Pointer to HBA context object.
11227 * @cq: Pointer to a WQ completion queue.
11228 * @wcqe: Pointer to work-queue completion queue entry.
11229 *
11230 * This routine handles an XRI abort event.
11231 *
11232 * Return: true if work posted to worker thread, otherwise false.
11233 **/
11234static bool
11235lpfc_sli4_sp_handle_abort_xri_wcqe(struct lpfc_hba *phba,
11236 struct lpfc_queue *cq,
11237 struct sli4_wcqe_xri_aborted *wcqe)
11238{
11239 bool workposted = false;
11240 struct lpfc_cq_event *cq_event;
11241 unsigned long iflags;
11242
11243 /* Allocate a new internal CQ_EVENT entry */
11244 cq_event = lpfc_sli4_cq_event_alloc(phba);
11245 if (!cq_event) {
11246 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11247 "0602 Failed to allocate CQ_EVENT entry\n");
11248 return false;
11249 }
11250
11251 /* Move the CQE into the proper xri abort event list */
11252 memcpy(&cq_event->cqe, wcqe, sizeof(struct sli4_wcqe_xri_aborted));
11253 switch (cq->subtype) {
11254 case LPFC_FCP:
11255 spin_lock_irqsave(&phba->hbalock, iflags);
11256 list_add_tail(&cq_event->list,
11257 &phba->sli4_hba.sp_fcp_xri_aborted_work_queue);
11258 /* Set the fcp xri abort event flag */
11259 phba->hba_flag |= FCP_XRI_ABORT_EVENT;
11260 spin_unlock_irqrestore(&phba->hbalock, iflags);
11261 workposted = true;
11262 break;
11263 case LPFC_ELS:
11264 spin_lock_irqsave(&phba->hbalock, iflags);
11265 list_add_tail(&cq_event->list,
11266 &phba->sli4_hba.sp_els_xri_aborted_work_queue);
11267 /* Set the els xri abort event flag */
11268 phba->hba_flag |= ELS_XRI_ABORT_EVENT;
11269 spin_unlock_irqrestore(&phba->hbalock, iflags);
11270 workposted = true;
11271 break;
11272 default:
11273 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11274 "0603 Invalid work queue CQE subtype (x%x)\n",
11275 cq->subtype);
11276 workposted = false;
11277 break;
11278 }
11279 return workposted;
11280}
11281
11282/**
James Smart4d9ab992009-10-02 15:16:39 -040011283 * lpfc_sli4_sp_handle_rcqe - Process a receive-queue completion queue entry
James Smart4f774512009-05-22 14:52:35 -040011284 * @phba: Pointer to HBA context object.
James Smart4d9ab992009-10-02 15:16:39 -040011285 * @rcqe: Pointer to receive-queue completion queue entry.
James Smart4f774512009-05-22 14:52:35 -040011286 *
James Smart4d9ab992009-10-02 15:16:39 -040011287 * This routine process a receive-queue completion queue entry.
James Smart4f774512009-05-22 14:52:35 -040011288 *
11289 * Return: true if work posted to worker thread, otherwise false.
11290 **/
11291static bool
James Smart4d9ab992009-10-02 15:16:39 -040011292lpfc_sli4_sp_handle_rcqe(struct lpfc_hba *phba, struct lpfc_rcqe *rcqe)
11293{
11294 bool workposted = false;
11295 struct lpfc_queue *hrq = phba->sli4_hba.hdr_rq;
11296 struct lpfc_queue *drq = phba->sli4_hba.dat_rq;
11297 struct hbq_dmabuf *dma_buf;
James Smart7851fe22011-07-22 18:36:52 -040011298 uint32_t status, rq_id;
James Smart4d9ab992009-10-02 15:16:39 -040011299 unsigned long iflags;
11300
James Smart2e90f4b2011-12-13 13:22:37 -050011301 /* sanity check on queue memory */
11302 if (unlikely(!hrq) || unlikely(!drq))
11303 return workposted;
11304
James Smart7851fe22011-07-22 18:36:52 -040011305 if (bf_get(lpfc_cqe_code, rcqe) == CQE_CODE_RECEIVE_V1)
11306 rq_id = bf_get(lpfc_rcqe_rq_id_v1, rcqe);
11307 else
11308 rq_id = bf_get(lpfc_rcqe_rq_id, rcqe);
11309 if (rq_id != hrq->queue_id)
James Smart4d9ab992009-10-02 15:16:39 -040011310 goto out;
11311
11312 status = bf_get(lpfc_rcqe_status, rcqe);
11313 switch (status) {
11314 case FC_STATUS_RQ_BUF_LEN_EXCEEDED:
11315 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11316 "2537 Receive Frame Truncated!!\n");
James Smartb84daac2012-08-03 12:35:13 -040011317 hrq->RQ_buf_trunc++;
James Smart4d9ab992009-10-02 15:16:39 -040011318 case FC_STATUS_RQ_SUCCESS:
James Smart5ffc2662009-11-18 15:39:44 -050011319 lpfc_sli4_rq_release(hrq, drq);
James Smart4d9ab992009-10-02 15:16:39 -040011320 spin_lock_irqsave(&phba->hbalock, iflags);
11321 dma_buf = lpfc_sli_hbqbuf_get(&phba->hbqs[0].hbq_buffer_list);
11322 if (!dma_buf) {
James Smartb84daac2012-08-03 12:35:13 -040011323 hrq->RQ_no_buf_found++;
James Smart4d9ab992009-10-02 15:16:39 -040011324 spin_unlock_irqrestore(&phba->hbalock, iflags);
11325 goto out;
11326 }
James Smartb84daac2012-08-03 12:35:13 -040011327 hrq->RQ_rcv_buf++;
James Smart4d9ab992009-10-02 15:16:39 -040011328 memcpy(&dma_buf->cq_event.cqe.rcqe_cmpl, rcqe, sizeof(*rcqe));
11329 /* save off the frame for the word thread to process */
11330 list_add_tail(&dma_buf->cq_event.list,
James Smart45ed1192009-10-02 15:17:02 -040011331 &phba->sli4_hba.sp_queue_event);
James Smart4d9ab992009-10-02 15:16:39 -040011332 /* Frame received */
James Smart45ed1192009-10-02 15:17:02 -040011333 phba->hba_flag |= HBA_SP_QUEUE_EVT;
James Smart4d9ab992009-10-02 15:16:39 -040011334 spin_unlock_irqrestore(&phba->hbalock, iflags);
11335 workposted = true;
11336 break;
11337 case FC_STATUS_INSUFF_BUF_NEED_BUF:
11338 case FC_STATUS_INSUFF_BUF_FRM_DISC:
James Smartb84daac2012-08-03 12:35:13 -040011339 hrq->RQ_no_posted_buf++;
James Smart4d9ab992009-10-02 15:16:39 -040011340 /* Post more buffers if possible */
11341 spin_lock_irqsave(&phba->hbalock, iflags);
11342 phba->hba_flag |= HBA_POST_RECEIVE_BUFFER;
11343 spin_unlock_irqrestore(&phba->hbalock, iflags);
11344 workposted = true;
11345 break;
11346 }
11347out:
11348 return workposted;
James Smart4d9ab992009-10-02 15:16:39 -040011349}
11350
11351/**
11352 * lpfc_sli4_sp_handle_cqe - Process a slow path completion queue entry
11353 * @phba: Pointer to HBA context object.
11354 * @cq: Pointer to the completion queue.
11355 * @wcqe: Pointer to a completion queue entry.
11356 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030011357 * This routine process a slow-path work-queue or receive queue completion queue
James Smart4d9ab992009-10-02 15:16:39 -040011358 * entry.
11359 *
11360 * Return: true if work posted to worker thread, otherwise false.
11361 **/
11362static bool
11363lpfc_sli4_sp_handle_cqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
James Smart4f774512009-05-22 14:52:35 -040011364 struct lpfc_cqe *cqe)
11365{
James Smart45ed1192009-10-02 15:17:02 -040011366 struct lpfc_cqe cqevt;
James Smart4f774512009-05-22 14:52:35 -040011367 bool workposted = false;
11368
11369 /* Copy the work queue CQE and convert endian order if needed */
James Smart45ed1192009-10-02 15:17:02 -040011370 lpfc_sli_pcimem_bcopy(cqe, &cqevt, sizeof(struct lpfc_cqe));
James Smart4f774512009-05-22 14:52:35 -040011371
11372 /* Check and process for different type of WCQE and dispatch */
James Smart45ed1192009-10-02 15:17:02 -040011373 switch (bf_get(lpfc_cqe_code, &cqevt)) {
James Smart4f774512009-05-22 14:52:35 -040011374 case CQE_CODE_COMPL_WQE:
James Smart45ed1192009-10-02 15:17:02 -040011375 /* Process the WQ/RQ complete event */
James Smartbc739052010-08-04 16:11:18 -040011376 phba->last_completion_time = jiffies;
James Smart4f774512009-05-22 14:52:35 -040011377 workposted = lpfc_sli4_sp_handle_els_wcqe(phba,
James Smart45ed1192009-10-02 15:17:02 -040011378 (struct lpfc_wcqe_complete *)&cqevt);
James Smart4f774512009-05-22 14:52:35 -040011379 break;
11380 case CQE_CODE_RELEASE_WQE:
11381 /* Process the WQ release event */
11382 lpfc_sli4_sp_handle_rel_wcqe(phba,
James Smart45ed1192009-10-02 15:17:02 -040011383 (struct lpfc_wcqe_release *)&cqevt);
James Smart4f774512009-05-22 14:52:35 -040011384 break;
11385 case CQE_CODE_XRI_ABORTED:
11386 /* Process the WQ XRI abort event */
James Smartbc739052010-08-04 16:11:18 -040011387 phba->last_completion_time = jiffies;
James Smart4f774512009-05-22 14:52:35 -040011388 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
James Smart45ed1192009-10-02 15:17:02 -040011389 (struct sli4_wcqe_xri_aborted *)&cqevt);
James Smart4f774512009-05-22 14:52:35 -040011390 break;
James Smart4d9ab992009-10-02 15:16:39 -040011391 case CQE_CODE_RECEIVE:
James Smart7851fe22011-07-22 18:36:52 -040011392 case CQE_CODE_RECEIVE_V1:
James Smart4d9ab992009-10-02 15:16:39 -040011393 /* Process the RQ event */
James Smartbc739052010-08-04 16:11:18 -040011394 phba->last_completion_time = jiffies;
James Smart4d9ab992009-10-02 15:16:39 -040011395 workposted = lpfc_sli4_sp_handle_rcqe(phba,
James Smart45ed1192009-10-02 15:17:02 -040011396 (struct lpfc_rcqe *)&cqevt);
James Smart4d9ab992009-10-02 15:16:39 -040011397 break;
James Smart4f774512009-05-22 14:52:35 -040011398 default:
11399 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11400 "0388 Not a valid WCQE code: x%x\n",
James Smart45ed1192009-10-02 15:17:02 -040011401 bf_get(lpfc_cqe_code, &cqevt));
James Smart4f774512009-05-22 14:52:35 -040011402 break;
11403 }
11404 return workposted;
11405}
11406
11407/**
James Smart4f774512009-05-22 14:52:35 -040011408 * lpfc_sli4_sp_handle_eqe - Process a slow-path event queue entry
11409 * @phba: Pointer to HBA context object.
11410 * @eqe: Pointer to fast-path event queue entry.
11411 *
11412 * This routine process a event queue entry from the slow-path event queue.
11413 * It will check the MajorCode and MinorCode to determine this is for a
11414 * completion event on a completion queue, if not, an error shall be logged
11415 * and just return. Otherwise, it will get to the corresponding completion
11416 * queue and process all the entries on that completion queue, rearm the
11417 * completion queue, and then return.
11418 *
11419 **/
11420static void
11421lpfc_sli4_sp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe)
11422{
11423 struct lpfc_queue *cq = NULL, *childq, *speq;
11424 struct lpfc_cqe *cqe;
11425 bool workposted = false;
11426 int ecount = 0;
11427 uint16_t cqid;
11428
James Smartcb5172e2010-03-15 11:25:07 -040011429 if (bf_get_le32(lpfc_eqe_major_code, eqe) != 0) {
James Smart4f774512009-05-22 14:52:35 -040011430 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11431 "0359 Not a valid slow-path completion "
11432 "event: majorcode=x%x, minorcode=x%x\n",
James Smartcb5172e2010-03-15 11:25:07 -040011433 bf_get_le32(lpfc_eqe_major_code, eqe),
11434 bf_get_le32(lpfc_eqe_minor_code, eqe));
James Smart4f774512009-05-22 14:52:35 -040011435 return;
11436 }
11437
11438 /* Get the reference to the corresponding CQ */
James Smartcb5172e2010-03-15 11:25:07 -040011439 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
James Smart4f774512009-05-22 14:52:35 -040011440
11441 /* Search for completion queue pointer matching this cqid */
11442 speq = phba->sli4_hba.sp_eq;
James Smart2e90f4b2011-12-13 13:22:37 -050011443 /* sanity check on queue memory */
11444 if (unlikely(!speq))
11445 return;
James Smart4f774512009-05-22 14:52:35 -040011446 list_for_each_entry(childq, &speq->child_list, list) {
11447 if (childq->queue_id == cqid) {
11448 cq = childq;
11449 break;
11450 }
11451 }
11452 if (unlikely(!cq)) {
James Smart75baf692010-06-08 18:31:21 -040011453 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
11454 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11455 "0365 Slow-path CQ identifier "
11456 "(%d) does not exist\n", cqid);
James Smart4f774512009-05-22 14:52:35 -040011457 return;
11458 }
11459
11460 /* Process all the entries to the CQ */
11461 switch (cq->type) {
11462 case LPFC_MCQ:
11463 while ((cqe = lpfc_sli4_cq_get(cq))) {
11464 workposted |= lpfc_sli4_sp_handle_mcqe(phba, cqe);
James Smart73d91e52011-10-10 21:32:10 -040011465 if (!(++ecount % cq->entry_repost))
James Smart4f774512009-05-22 14:52:35 -040011466 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
James Smartb84daac2012-08-03 12:35:13 -040011467 cq->CQ_mbox++;
James Smart4f774512009-05-22 14:52:35 -040011468 }
11469 break;
11470 case LPFC_WCQ:
11471 while ((cqe = lpfc_sli4_cq_get(cq))) {
James Smart05580562011-05-24 11:40:48 -040011472 if (cq->subtype == LPFC_FCP)
11473 workposted |= lpfc_sli4_fp_handle_wcqe(phba, cq,
11474 cqe);
11475 else
11476 workposted |= lpfc_sli4_sp_handle_cqe(phba, cq,
11477 cqe);
James Smart73d91e52011-10-10 21:32:10 -040011478 if (!(++ecount % cq->entry_repost))
James Smart4f774512009-05-22 14:52:35 -040011479 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
11480 }
James Smartb84daac2012-08-03 12:35:13 -040011481
11482 /* Track the max number of CQEs processed in 1 EQ */
11483 if (ecount > cq->CQ_max_cqe)
11484 cq->CQ_max_cqe = ecount;
James Smart4f774512009-05-22 14:52:35 -040011485 break;
11486 default:
11487 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11488 "0370 Invalid completion queue type (%d)\n",
11489 cq->type);
11490 return;
11491 }
11492
11493 /* Catch the no cq entry condition, log an error */
11494 if (unlikely(ecount == 0))
11495 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11496 "0371 No entry from the CQ: identifier "
11497 "(x%x), type (%d)\n", cq->queue_id, cq->type);
11498
11499 /* In any case, flash and re-arm the RCQ */
11500 lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
11501
11502 /* wake up worker thread if there are works to be done */
11503 if (workposted)
11504 lpfc_worker_wake_up(phba);
11505}
11506
11507/**
11508 * lpfc_sli4_fp_handle_fcp_wcqe - Process fast-path work queue completion entry
11509 * @eqe: Pointer to fast-path completion queue entry.
11510 *
11511 * This routine process a fast-path work queue completion entry from fast-path
11512 * event queue for FCP command response completion.
11513 **/
11514static void
11515lpfc_sli4_fp_handle_fcp_wcqe(struct lpfc_hba *phba,
11516 struct lpfc_wcqe_complete *wcqe)
11517{
11518 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_FCP_RING];
11519 struct lpfc_iocbq *cmdiocbq;
11520 struct lpfc_iocbq irspiocbq;
11521 unsigned long iflags;
11522
11523 spin_lock_irqsave(&phba->hbalock, iflags);
11524 pring->stats.iocb_event++;
11525 spin_unlock_irqrestore(&phba->hbalock, iflags);
11526
11527 /* Check for response status */
11528 if (unlikely(bf_get(lpfc_wcqe_c_status, wcqe))) {
11529 /* If resource errors reported from HBA, reduce queue
11530 * depth of the SCSI device.
11531 */
11532 if ((bf_get(lpfc_wcqe_c_status, wcqe) ==
11533 IOSTAT_LOCAL_REJECT) &&
11534 (wcqe->parameter == IOERR_NO_RESOURCES)) {
11535 phba->lpfc_rampdown_queue_depth(phba);
11536 }
11537 /* Log the error status */
11538 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11539 "0373 FCP complete error: status=x%x, "
11540 "hw_status=x%x, total_data_specified=%d, "
11541 "parameter=x%x, word3=x%x\n",
11542 bf_get(lpfc_wcqe_c_status, wcqe),
11543 bf_get(lpfc_wcqe_c_hw_status, wcqe),
11544 wcqe->total_data_placed, wcqe->parameter,
11545 wcqe->word3);
11546 }
11547
11548 /* Look up the FCP command IOCB and create pseudo response IOCB */
11549 spin_lock_irqsave(&phba->hbalock, iflags);
11550 cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
11551 bf_get(lpfc_wcqe_c_request_tag, wcqe));
11552 spin_unlock_irqrestore(&phba->hbalock, iflags);
11553 if (unlikely(!cmdiocbq)) {
11554 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11555 "0374 FCP complete with no corresponding "
11556 "cmdiocb: iotag (%d)\n",
11557 bf_get(lpfc_wcqe_c_request_tag, wcqe));
11558 return;
11559 }
11560 if (unlikely(!cmdiocbq->iocb_cmpl)) {
11561 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11562 "0375 FCP cmdiocb not callback function "
11563 "iotag: (%d)\n",
11564 bf_get(lpfc_wcqe_c_request_tag, wcqe));
11565 return;
11566 }
11567
11568 /* Fake the irspiocb and copy necessary response information */
James Smart341af102010-01-26 23:07:37 -050011569 lpfc_sli4_iocb_param_transfer(phba, &irspiocbq, cmdiocbq, wcqe);
James Smart4f774512009-05-22 14:52:35 -040011570
James Smart0f65ff62010-02-26 14:14:23 -050011571 if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED) {
11572 spin_lock_irqsave(&phba->hbalock, iflags);
11573 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
11574 spin_unlock_irqrestore(&phba->hbalock, iflags);
11575 }
11576
James Smart4f774512009-05-22 14:52:35 -040011577 /* Pass the cmd_iocb and the rsp state to the upper layer */
11578 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq, &irspiocbq);
11579}
11580
11581/**
11582 * lpfc_sli4_fp_handle_rel_wcqe - Handle fast-path WQ entry consumed event
11583 * @phba: Pointer to HBA context object.
11584 * @cq: Pointer to completion queue.
11585 * @wcqe: Pointer to work-queue completion queue entry.
11586 *
11587 * This routine handles an fast-path WQ entry comsumed event by invoking the
11588 * proper WQ release routine to the slow-path WQ.
11589 **/
11590static void
11591lpfc_sli4_fp_handle_rel_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
11592 struct lpfc_wcqe_release *wcqe)
11593{
11594 struct lpfc_queue *childwq;
11595 bool wqid_matched = false;
11596 uint16_t fcp_wqid;
11597
11598 /* Check for fast-path FCP work queue release */
11599 fcp_wqid = bf_get(lpfc_wcqe_r_wq_id, wcqe);
11600 list_for_each_entry(childwq, &cq->child_list, list) {
11601 if (childwq->queue_id == fcp_wqid) {
11602 lpfc_sli4_wq_release(childwq,
11603 bf_get(lpfc_wcqe_r_wqe_index, wcqe));
11604 wqid_matched = true;
11605 break;
11606 }
11607 }
11608 /* Report warning log message if no match found */
11609 if (wqid_matched != true)
11610 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11611 "2580 Fast-path wqe consume event carries "
11612 "miss-matched qid: wcqe-qid=x%x\n", fcp_wqid);
11613}
11614
11615/**
11616 * lpfc_sli4_fp_handle_wcqe - Process fast-path work queue completion entry
11617 * @cq: Pointer to the completion queue.
11618 * @eqe: Pointer to fast-path completion queue entry.
11619 *
11620 * This routine process a fast-path work queue completion entry from fast-path
11621 * event queue for FCP command response completion.
11622 **/
11623static int
11624lpfc_sli4_fp_handle_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
11625 struct lpfc_cqe *cqe)
11626{
11627 struct lpfc_wcqe_release wcqe;
11628 bool workposted = false;
11629
11630 /* Copy the work queue CQE and convert endian order if needed */
11631 lpfc_sli_pcimem_bcopy(cqe, &wcqe, sizeof(struct lpfc_cqe));
11632
11633 /* Check and process for different type of WCQE and dispatch */
11634 switch (bf_get(lpfc_wcqe_c_code, &wcqe)) {
11635 case CQE_CODE_COMPL_WQE:
James Smartb84daac2012-08-03 12:35:13 -040011636 cq->CQ_wq++;
James Smart4f774512009-05-22 14:52:35 -040011637 /* Process the WQ complete event */
James Smart98fc5dd2010-06-07 15:24:29 -040011638 phba->last_completion_time = jiffies;
James Smart4f774512009-05-22 14:52:35 -040011639 lpfc_sli4_fp_handle_fcp_wcqe(phba,
11640 (struct lpfc_wcqe_complete *)&wcqe);
11641 break;
11642 case CQE_CODE_RELEASE_WQE:
James Smartb84daac2012-08-03 12:35:13 -040011643 cq->CQ_release_wqe++;
James Smart4f774512009-05-22 14:52:35 -040011644 /* Process the WQ release event */
11645 lpfc_sli4_fp_handle_rel_wcqe(phba, cq,
11646 (struct lpfc_wcqe_release *)&wcqe);
11647 break;
11648 case CQE_CODE_XRI_ABORTED:
James Smartb84daac2012-08-03 12:35:13 -040011649 cq->CQ_xri_aborted++;
James Smart4f774512009-05-22 14:52:35 -040011650 /* Process the WQ XRI abort event */
James Smartbc739052010-08-04 16:11:18 -040011651 phba->last_completion_time = jiffies;
James Smart4f774512009-05-22 14:52:35 -040011652 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
11653 (struct sli4_wcqe_xri_aborted *)&wcqe);
11654 break;
11655 default:
11656 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11657 "0144 Not a valid WCQE code: x%x\n",
11658 bf_get(lpfc_wcqe_c_code, &wcqe));
11659 break;
11660 }
11661 return workposted;
11662}
11663
11664/**
11665 * lpfc_sli4_fp_handle_eqe - Process a fast-path event queue entry
11666 * @phba: Pointer to HBA context object.
11667 * @eqe: Pointer to fast-path event queue entry.
11668 *
11669 * This routine process a event queue entry from the fast-path event queue.
11670 * It will check the MajorCode and MinorCode to determine this is for a
11671 * completion event on a completion queue, if not, an error shall be logged
11672 * and just return. Otherwise, it will get to the corresponding completion
11673 * queue and process all the entries on the completion queue, rearm the
11674 * completion queue, and then return.
11675 **/
11676static void
11677lpfc_sli4_fp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
11678 uint32_t fcp_cqidx)
11679{
11680 struct lpfc_queue *cq;
11681 struct lpfc_cqe *cqe;
11682 bool workposted = false;
11683 uint16_t cqid;
11684 int ecount = 0;
11685
James Smartcb5172e2010-03-15 11:25:07 -040011686 if (unlikely(bf_get_le32(lpfc_eqe_major_code, eqe) != 0)) {
James Smart4f774512009-05-22 14:52:35 -040011687 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11688 "0366 Not a valid fast-path completion "
11689 "event: majorcode=x%x, minorcode=x%x\n",
James Smartcb5172e2010-03-15 11:25:07 -040011690 bf_get_le32(lpfc_eqe_major_code, eqe),
11691 bf_get_le32(lpfc_eqe_minor_code, eqe));
James Smart4f774512009-05-22 14:52:35 -040011692 return;
11693 }
11694
James Smart2e90f4b2011-12-13 13:22:37 -050011695 if (unlikely(!phba->sli4_hba.fcp_cq)) {
11696 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11697 "3146 Fast-path completion queues "
11698 "does not exist\n");
11699 return;
11700 }
James Smart4f774512009-05-22 14:52:35 -040011701 cq = phba->sli4_hba.fcp_cq[fcp_cqidx];
11702 if (unlikely(!cq)) {
James Smart75baf692010-06-08 18:31:21 -040011703 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
11704 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11705 "0367 Fast-path completion queue "
James Smart2e90f4b2011-12-13 13:22:37 -050011706 "(%d) does not exist\n", fcp_cqidx);
James Smart4f774512009-05-22 14:52:35 -040011707 return;
11708 }
11709
11710 /* Get the reference to the corresponding CQ */
James Smartcb5172e2010-03-15 11:25:07 -040011711 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
James Smart4f774512009-05-22 14:52:35 -040011712 if (unlikely(cqid != cq->queue_id)) {
11713 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11714 "0368 Miss-matched fast-path completion "
11715 "queue identifier: eqcqid=%d, fcpcqid=%d\n",
11716 cqid, cq->queue_id);
11717 return;
11718 }
11719
11720 /* Process all the entries to the CQ */
11721 while ((cqe = lpfc_sli4_cq_get(cq))) {
11722 workposted |= lpfc_sli4_fp_handle_wcqe(phba, cq, cqe);
James Smart73d91e52011-10-10 21:32:10 -040011723 if (!(++ecount % cq->entry_repost))
James Smart4f774512009-05-22 14:52:35 -040011724 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
11725 }
11726
James Smartb84daac2012-08-03 12:35:13 -040011727 /* Track the max number of CQEs processed in 1 EQ */
11728 if (ecount > cq->CQ_max_cqe)
11729 cq->CQ_max_cqe = ecount;
11730
James Smart4f774512009-05-22 14:52:35 -040011731 /* Catch the no cq entry condition */
11732 if (unlikely(ecount == 0))
11733 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11734 "0369 No entry from fast-path completion "
11735 "queue fcpcqid=%d\n", cq->queue_id);
11736
11737 /* In any case, flash and re-arm the CQ */
11738 lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
11739
11740 /* wake up worker thread if there are works to be done */
11741 if (workposted)
11742 lpfc_worker_wake_up(phba);
11743}
11744
11745static void
11746lpfc_sli4_eq_flush(struct lpfc_hba *phba, struct lpfc_queue *eq)
11747{
11748 struct lpfc_eqe *eqe;
11749
11750 /* walk all the EQ entries and drop on the floor */
11751 while ((eqe = lpfc_sli4_eq_get(eq)))
11752 ;
11753
11754 /* Clear and re-arm the EQ */
11755 lpfc_sli4_eq_release(eq, LPFC_QUEUE_REARM);
11756}
11757
11758/**
11759 * lpfc_sli4_sp_intr_handler - Slow-path interrupt handler to SLI-4 device
11760 * @irq: Interrupt number.
11761 * @dev_id: The device context pointer.
11762 *
11763 * This function is directly called from the PCI layer as an interrupt
11764 * service routine when device with SLI-4 interface spec is enabled with
11765 * MSI-X multi-message interrupt mode and there are slow-path events in
11766 * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
11767 * interrupt mode, this function is called as part of the device-level
11768 * interrupt handler. When the PCI slot is in error recovery or the HBA is
11769 * undergoing initialization, the interrupt handler will not process the
11770 * interrupt. The link attention and ELS ring attention events are handled
11771 * by the worker thread. The interrupt handler signals the worker thread
11772 * and returns for these events. This function is called without any lock
11773 * held. It gets the hbalock to access and update SLI data structures.
11774 *
11775 * This function returns IRQ_HANDLED when interrupt is handled else it
11776 * returns IRQ_NONE.
11777 **/
11778irqreturn_t
11779lpfc_sli4_sp_intr_handler(int irq, void *dev_id)
11780{
11781 struct lpfc_hba *phba;
11782 struct lpfc_queue *speq;
11783 struct lpfc_eqe *eqe;
11784 unsigned long iflag;
11785 int ecount = 0;
11786
11787 /*
11788 * Get the driver's phba structure from the dev_id
11789 */
11790 phba = (struct lpfc_hba *)dev_id;
11791
11792 if (unlikely(!phba))
11793 return IRQ_NONE;
11794
11795 /* Get to the EQ struct associated with this vector */
11796 speq = phba->sli4_hba.sp_eq;
James Smart5350d872011-10-10 21:33:49 -040011797 if (unlikely(!speq))
11798 return IRQ_NONE;
James Smart4f774512009-05-22 14:52:35 -040011799
11800 /* Check device state for handling interrupt */
11801 if (unlikely(lpfc_intr_state_check(phba))) {
James Smartb84daac2012-08-03 12:35:13 -040011802 speq->EQ_badstate++;
James Smart4f774512009-05-22 14:52:35 -040011803 /* Check again for link_state with lock held */
11804 spin_lock_irqsave(&phba->hbalock, iflag);
11805 if (phba->link_state < LPFC_LINK_DOWN)
11806 /* Flush, clear interrupt, and rearm the EQ */
11807 lpfc_sli4_eq_flush(phba, speq);
11808 spin_unlock_irqrestore(&phba->hbalock, iflag);
11809 return IRQ_NONE;
11810 }
11811
11812 /*
11813 * Process all the event on FCP slow-path EQ
11814 */
11815 while ((eqe = lpfc_sli4_eq_get(speq))) {
11816 lpfc_sli4_sp_handle_eqe(phba, eqe);
James Smart73d91e52011-10-10 21:32:10 -040011817 if (!(++ecount % speq->entry_repost))
James Smart4f774512009-05-22 14:52:35 -040011818 lpfc_sli4_eq_release(speq, LPFC_QUEUE_NOARM);
James Smartb84daac2012-08-03 12:35:13 -040011819 speq->EQ_processed++;
James Smart4f774512009-05-22 14:52:35 -040011820 }
11821
James Smartb84daac2012-08-03 12:35:13 -040011822 /* Track the max number of EQEs processed in 1 intr */
11823 if (ecount > speq->EQ_max_eqe)
11824 speq->EQ_max_eqe = ecount;
11825
James Smart4f774512009-05-22 14:52:35 -040011826 /* Always clear and re-arm the slow-path EQ */
11827 lpfc_sli4_eq_release(speq, LPFC_QUEUE_REARM);
11828
11829 /* Catch the no cq entry condition */
11830 if (unlikely(ecount == 0)) {
James Smartb84daac2012-08-03 12:35:13 -040011831 speq->EQ_no_entry++;
James Smart4f774512009-05-22 14:52:35 -040011832 if (phba->intr_type == MSIX)
11833 /* MSI-X treated interrupt served as no EQ share INT */
11834 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11835 "0357 MSI-X interrupt with no EQE\n");
11836 else
11837 /* Non MSI-X treated on interrupt as EQ share INT */
11838 return IRQ_NONE;
11839 }
11840
11841 return IRQ_HANDLED;
11842} /* lpfc_sli4_sp_intr_handler */
11843
11844/**
11845 * lpfc_sli4_fp_intr_handler - Fast-path interrupt handler to SLI-4 device
11846 * @irq: Interrupt number.
11847 * @dev_id: The device context pointer.
11848 *
11849 * This function is directly called from the PCI layer as an interrupt
11850 * service routine when device with SLI-4 interface spec is enabled with
11851 * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
11852 * ring event in the HBA. However, when the device is enabled with either
11853 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
11854 * device-level interrupt handler. When the PCI slot is in error recovery
11855 * or the HBA is undergoing initialization, the interrupt handler will not
11856 * process the interrupt. The SCSI FCP fast-path ring event are handled in
11857 * the intrrupt context. This function is called without any lock held.
11858 * It gets the hbalock to access and update SLI data structures. Note that,
11859 * the FCP EQ to FCP CQ are one-to-one map such that the FCP EQ index is
11860 * equal to that of FCP CQ index.
11861 *
11862 * This function returns IRQ_HANDLED when interrupt is handled else it
11863 * returns IRQ_NONE.
11864 **/
11865irqreturn_t
11866lpfc_sli4_fp_intr_handler(int irq, void *dev_id)
11867{
11868 struct lpfc_hba *phba;
11869 struct lpfc_fcp_eq_hdl *fcp_eq_hdl;
11870 struct lpfc_queue *fpeq;
11871 struct lpfc_eqe *eqe;
11872 unsigned long iflag;
11873 int ecount = 0;
11874 uint32_t fcp_eqidx;
11875
11876 /* Get the driver's phba structure from the dev_id */
11877 fcp_eq_hdl = (struct lpfc_fcp_eq_hdl *)dev_id;
11878 phba = fcp_eq_hdl->phba;
11879 fcp_eqidx = fcp_eq_hdl->idx;
11880
11881 if (unlikely(!phba))
11882 return IRQ_NONE;
James Smart5350d872011-10-10 21:33:49 -040011883 if (unlikely(!phba->sli4_hba.fp_eq))
11884 return IRQ_NONE;
James Smart4f774512009-05-22 14:52:35 -040011885
11886 /* Get to the EQ struct associated with this vector */
11887 fpeq = phba->sli4_hba.fp_eq[fcp_eqidx];
James Smart2e90f4b2011-12-13 13:22:37 -050011888 if (unlikely(!fpeq))
11889 return IRQ_NONE;
James Smart4f774512009-05-22 14:52:35 -040011890
11891 /* Check device state for handling interrupt */
11892 if (unlikely(lpfc_intr_state_check(phba))) {
James Smartb84daac2012-08-03 12:35:13 -040011893 fpeq->EQ_badstate++;
James Smart4f774512009-05-22 14:52:35 -040011894 /* Check again for link_state with lock held */
11895 spin_lock_irqsave(&phba->hbalock, iflag);
11896 if (phba->link_state < LPFC_LINK_DOWN)
11897 /* Flush, clear interrupt, and rearm the EQ */
11898 lpfc_sli4_eq_flush(phba, fpeq);
11899 spin_unlock_irqrestore(&phba->hbalock, iflag);
11900 return IRQ_NONE;
11901 }
11902
11903 /*
11904 * Process all the event on FCP fast-path EQ
11905 */
11906 while ((eqe = lpfc_sli4_eq_get(fpeq))) {
11907 lpfc_sli4_fp_handle_eqe(phba, eqe, fcp_eqidx);
James Smart73d91e52011-10-10 21:32:10 -040011908 if (!(++ecount % fpeq->entry_repost))
James Smart4f774512009-05-22 14:52:35 -040011909 lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_NOARM);
James Smartb84daac2012-08-03 12:35:13 -040011910 fpeq->EQ_processed++;
James Smart4f774512009-05-22 14:52:35 -040011911 }
11912
James Smartb84daac2012-08-03 12:35:13 -040011913 /* Track the max number of EQEs processed in 1 intr */
11914 if (ecount > fpeq->EQ_max_eqe)
11915 fpeq->EQ_max_eqe = ecount;
11916
James Smart4f774512009-05-22 14:52:35 -040011917 /* Always clear and re-arm the fast-path EQ */
11918 lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_REARM);
11919
11920 if (unlikely(ecount == 0)) {
James Smartb84daac2012-08-03 12:35:13 -040011921 fpeq->EQ_no_entry++;
James Smart4f774512009-05-22 14:52:35 -040011922 if (phba->intr_type == MSIX)
11923 /* MSI-X treated interrupt served as no EQ share INT */
11924 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11925 "0358 MSI-X interrupt with no EQE\n");
11926 else
11927 /* Non MSI-X treated on interrupt as EQ share INT */
11928 return IRQ_NONE;
11929 }
11930
11931 return IRQ_HANDLED;
11932} /* lpfc_sli4_fp_intr_handler */
11933
11934/**
11935 * lpfc_sli4_intr_handler - Device-level interrupt handler for SLI-4 device
11936 * @irq: Interrupt number.
11937 * @dev_id: The device context pointer.
11938 *
11939 * This function is the device-level interrupt handler to device with SLI-4
11940 * interface spec, called from the PCI layer when either MSI or Pin-IRQ
11941 * interrupt mode is enabled and there is an event in the HBA which requires
11942 * driver attention. This function invokes the slow-path interrupt attention
11943 * handling function and fast-path interrupt attention handling function in
11944 * turn to process the relevant HBA attention events. This function is called
11945 * without any lock held. It gets the hbalock to access and update SLI data
11946 * structures.
11947 *
11948 * This function returns IRQ_HANDLED when interrupt is handled, else it
11949 * returns IRQ_NONE.
11950 **/
11951irqreturn_t
11952lpfc_sli4_intr_handler(int irq, void *dev_id)
11953{
11954 struct lpfc_hba *phba;
11955 irqreturn_t sp_irq_rc, fp_irq_rc;
11956 bool fp_handled = false;
11957 uint32_t fcp_eqidx;
11958
11959 /* Get the driver's phba structure from the dev_id */
11960 phba = (struct lpfc_hba *)dev_id;
11961
11962 if (unlikely(!phba))
11963 return IRQ_NONE;
11964
11965 /*
11966 * Invokes slow-path host attention interrupt handling as appropriate.
11967 */
11968 sp_irq_rc = lpfc_sli4_sp_intr_handler(irq, dev_id);
11969
11970 /*
11971 * Invoke fast-path host attention interrupt handling as appropriate.
11972 */
11973 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count; fcp_eqidx++) {
11974 fp_irq_rc = lpfc_sli4_fp_intr_handler(irq,
11975 &phba->sli4_hba.fcp_eq_hdl[fcp_eqidx]);
11976 if (fp_irq_rc == IRQ_HANDLED)
11977 fp_handled |= true;
11978 }
11979
11980 return (fp_handled == true) ? IRQ_HANDLED : sp_irq_rc;
11981} /* lpfc_sli4_intr_handler */
11982
11983/**
11984 * lpfc_sli4_queue_free - free a queue structure and associated memory
11985 * @queue: The queue structure to free.
11986 *
Uwe Kleine-Königb5950762010-11-01 15:38:34 -040011987 * This function frees a queue structure and the DMAable memory used for
James Smart4f774512009-05-22 14:52:35 -040011988 * the host resident queue. This function must be called after destroying the
11989 * queue on the HBA.
11990 **/
11991void
11992lpfc_sli4_queue_free(struct lpfc_queue *queue)
11993{
11994 struct lpfc_dmabuf *dmabuf;
11995
11996 if (!queue)
11997 return;
11998
11999 while (!list_empty(&queue->page_list)) {
12000 list_remove_head(&queue->page_list, dmabuf, struct lpfc_dmabuf,
12001 list);
James Smart49198b32010-04-06 15:04:33 -040012002 dma_free_coherent(&queue->phba->pcidev->dev, SLI4_PAGE_SIZE,
James Smart4f774512009-05-22 14:52:35 -040012003 dmabuf->virt, dmabuf->phys);
12004 kfree(dmabuf);
12005 }
12006 kfree(queue);
12007 return;
12008}
12009
12010/**
12011 * lpfc_sli4_queue_alloc - Allocate and initialize a queue structure
12012 * @phba: The HBA that this queue is being created on.
12013 * @entry_size: The size of each queue entry for this queue.
12014 * @entry count: The number of entries that this queue will handle.
12015 *
12016 * This function allocates a queue structure and the DMAable memory used for
12017 * the host resident queue. This function must be called before creating the
12018 * queue on the HBA.
12019 **/
12020struct lpfc_queue *
12021lpfc_sli4_queue_alloc(struct lpfc_hba *phba, uint32_t entry_size,
12022 uint32_t entry_count)
12023{
12024 struct lpfc_queue *queue;
12025 struct lpfc_dmabuf *dmabuf;
12026 int x, total_qe_count;
12027 void *dma_pointer;
James Smartcb5172e2010-03-15 11:25:07 -040012028 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smart4f774512009-05-22 14:52:35 -040012029
James Smartcb5172e2010-03-15 11:25:07 -040012030 if (!phba->sli4_hba.pc_sli4_params.supported)
12031 hw_page_size = SLI4_PAGE_SIZE;
12032
James Smart4f774512009-05-22 14:52:35 -040012033 queue = kzalloc(sizeof(struct lpfc_queue) +
12034 (sizeof(union sli4_qe) * entry_count), GFP_KERNEL);
12035 if (!queue)
12036 return NULL;
James Smartcb5172e2010-03-15 11:25:07 -040012037 queue->page_count = (ALIGN(entry_size * entry_count,
12038 hw_page_size))/hw_page_size;
James Smart4f774512009-05-22 14:52:35 -040012039 INIT_LIST_HEAD(&queue->list);
12040 INIT_LIST_HEAD(&queue->page_list);
12041 INIT_LIST_HEAD(&queue->child_list);
12042 for (x = 0, total_qe_count = 0; x < queue->page_count; x++) {
12043 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
12044 if (!dmabuf)
12045 goto out_fail;
12046 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
James Smartcb5172e2010-03-15 11:25:07 -040012047 hw_page_size, &dmabuf->phys,
James Smart4f774512009-05-22 14:52:35 -040012048 GFP_KERNEL);
12049 if (!dmabuf->virt) {
12050 kfree(dmabuf);
12051 goto out_fail;
12052 }
James Smartcb5172e2010-03-15 11:25:07 -040012053 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -040012054 dmabuf->buffer_tag = x;
12055 list_add_tail(&dmabuf->list, &queue->page_list);
12056 /* initialize queue's entry array */
12057 dma_pointer = dmabuf->virt;
12058 for (; total_qe_count < entry_count &&
James Smartcb5172e2010-03-15 11:25:07 -040012059 dma_pointer < (hw_page_size + dmabuf->virt);
James Smart4f774512009-05-22 14:52:35 -040012060 total_qe_count++, dma_pointer += entry_size) {
12061 queue->qe[total_qe_count].address = dma_pointer;
12062 }
12063 }
12064 queue->entry_size = entry_size;
12065 queue->entry_count = entry_count;
James Smart73d91e52011-10-10 21:32:10 -040012066
12067 /*
12068 * entry_repost is calculated based on the number of entries in the
12069 * queue. This works out except for RQs. If buffers are NOT initially
12070 * posted for every RQE, entry_repost should be adjusted accordingly.
12071 */
12072 queue->entry_repost = (entry_count >> 3);
12073 if (queue->entry_repost < LPFC_QUEUE_MIN_REPOST)
12074 queue->entry_repost = LPFC_QUEUE_MIN_REPOST;
James Smart4f774512009-05-22 14:52:35 -040012075 queue->phba = phba;
12076
12077 return queue;
12078out_fail:
12079 lpfc_sli4_queue_free(queue);
12080 return NULL;
12081}
12082
12083/**
James Smart173edbb2012-06-12 13:54:50 -040012084 * lpfc_modify_fcp_eq_delay - Modify Delay Multiplier on FCP EQs
12085 * @phba: HBA structure that indicates port to create a queue on.
12086 * @startq: The starting FCP EQ to modify
12087 *
12088 * This function sends an MODIFY_EQ_DELAY mailbox command to the HBA.
12089 *
12090 * The @phba struct is used to send mailbox command to HBA. The @startq
12091 * is used to get the starting FCP EQ to change.
12092 * This function is asynchronous and will wait for the mailbox
12093 * command to finish before continuing.
12094 *
12095 * On success this function will return a zero. If unable to allocate enough
12096 * memory this function will return -ENOMEM. If the queue create mailbox command
12097 * fails this function will return -ENXIO.
12098 **/
12099uint32_t
12100lpfc_modify_fcp_eq_delay(struct lpfc_hba *phba, uint16_t startq)
12101{
12102 struct lpfc_mbx_modify_eq_delay *eq_delay;
12103 LPFC_MBOXQ_t *mbox;
12104 struct lpfc_queue *eq;
12105 int cnt, rc, length, status = 0;
12106 uint32_t shdr_status, shdr_add_status;
12107 int fcp_eqidx;
12108 union lpfc_sli4_cfg_shdr *shdr;
12109 uint16_t dmult;
12110
12111 if (startq >= phba->cfg_fcp_eq_count)
12112 return 0;
12113
12114 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12115 if (!mbox)
12116 return -ENOMEM;
12117 length = (sizeof(struct lpfc_mbx_modify_eq_delay) -
12118 sizeof(struct lpfc_sli4_cfg_mhdr));
12119 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
12120 LPFC_MBOX_OPCODE_MODIFY_EQ_DELAY,
12121 length, LPFC_SLI4_MBX_EMBED);
12122 eq_delay = &mbox->u.mqe.un.eq_delay;
12123
12124 /* Calculate delay multiper from maximum interrupt per second */
12125 dmult = LPFC_DMULT_CONST/phba->cfg_fcp_imax - 1;
12126
12127 cnt = 0;
12128 for (fcp_eqidx = startq; fcp_eqidx < phba->cfg_fcp_eq_count;
12129 fcp_eqidx++) {
12130 eq = phba->sli4_hba.fp_eq[fcp_eqidx];
12131 if (!eq)
12132 continue;
12133 eq_delay->u.request.eq[cnt].eq_id = eq->queue_id;
12134 eq_delay->u.request.eq[cnt].phase = 0;
12135 eq_delay->u.request.eq[cnt].delay_multi = dmult;
12136 cnt++;
12137 if (cnt >= LPFC_MAX_EQ_DELAY)
12138 break;
12139 }
12140 eq_delay->u.request.num_eq = cnt;
12141
12142 mbox->vport = phba->pport;
12143 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
12144 mbox->context1 = NULL;
12145 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
12146 shdr = (union lpfc_sli4_cfg_shdr *) &eq_delay->header.cfg_shdr;
12147 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12148 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12149 if (shdr_status || shdr_add_status || rc) {
12150 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12151 "2512 MODIFY_EQ_DELAY mailbox failed with "
12152 "status x%x add_status x%x, mbx status x%x\n",
12153 shdr_status, shdr_add_status, rc);
12154 status = -ENXIO;
12155 }
12156 mempool_free(mbox, phba->mbox_mem_pool);
12157 return status;
12158}
12159
12160/**
James Smart4f774512009-05-22 14:52:35 -040012161 * lpfc_eq_create - Create an Event Queue on the HBA
12162 * @phba: HBA structure that indicates port to create a queue on.
12163 * @eq: The queue structure to use to create the event queue.
12164 * @imax: The maximum interrupt per second limit.
12165 *
12166 * This function creates an event queue, as detailed in @eq, on a port,
12167 * described by @phba by sending an EQ_CREATE mailbox command to the HBA.
12168 *
12169 * The @phba struct is used to send mailbox command to HBA. The @eq struct
12170 * is used to get the entry count and entry size that are necessary to
12171 * determine the number of pages to allocate and use for this queue. This
12172 * function will send the EQ_CREATE mailbox command to the HBA to setup the
12173 * event queue. This function is asynchronous and will wait for the mailbox
12174 * command to finish before continuing.
12175 *
12176 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040012177 * memory this function will return -ENOMEM. If the queue create mailbox command
12178 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040012179 **/
12180uint32_t
12181lpfc_eq_create(struct lpfc_hba *phba, struct lpfc_queue *eq, uint16_t imax)
12182{
12183 struct lpfc_mbx_eq_create *eq_create;
12184 LPFC_MBOXQ_t *mbox;
12185 int rc, length, status = 0;
12186 struct lpfc_dmabuf *dmabuf;
12187 uint32_t shdr_status, shdr_add_status;
12188 union lpfc_sli4_cfg_shdr *shdr;
12189 uint16_t dmult;
James Smart49198b32010-04-06 15:04:33 -040012190 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
12191
James Smart2e90f4b2011-12-13 13:22:37 -050012192 /* sanity check on queue memory */
12193 if (!eq)
12194 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040012195 if (!phba->sli4_hba.pc_sli4_params.supported)
12196 hw_page_size = SLI4_PAGE_SIZE;
James Smart4f774512009-05-22 14:52:35 -040012197
12198 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12199 if (!mbox)
12200 return -ENOMEM;
12201 length = (sizeof(struct lpfc_mbx_eq_create) -
12202 sizeof(struct lpfc_sli4_cfg_mhdr));
12203 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
12204 LPFC_MBOX_OPCODE_EQ_CREATE,
12205 length, LPFC_SLI4_MBX_EMBED);
12206 eq_create = &mbox->u.mqe.un.eq_create;
12207 bf_set(lpfc_mbx_eq_create_num_pages, &eq_create->u.request,
12208 eq->page_count);
12209 bf_set(lpfc_eq_context_size, &eq_create->u.request.context,
12210 LPFC_EQE_SIZE);
12211 bf_set(lpfc_eq_context_valid, &eq_create->u.request.context, 1);
12212 /* Calculate delay multiper from maximum interrupt per second */
12213 dmult = LPFC_DMULT_CONST/imax - 1;
12214 bf_set(lpfc_eq_context_delay_multi, &eq_create->u.request.context,
12215 dmult);
12216 switch (eq->entry_count) {
12217 default:
12218 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12219 "0360 Unsupported EQ count. (%d)\n",
12220 eq->entry_count);
12221 if (eq->entry_count < 256)
12222 return -EINVAL;
12223 /* otherwise default to smallest count (drop through) */
12224 case 256:
12225 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
12226 LPFC_EQ_CNT_256);
12227 break;
12228 case 512:
12229 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
12230 LPFC_EQ_CNT_512);
12231 break;
12232 case 1024:
12233 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
12234 LPFC_EQ_CNT_1024);
12235 break;
12236 case 2048:
12237 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
12238 LPFC_EQ_CNT_2048);
12239 break;
12240 case 4096:
12241 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
12242 LPFC_EQ_CNT_4096);
12243 break;
12244 }
12245 list_for_each_entry(dmabuf, &eq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040012246 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -040012247 eq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
12248 putPaddrLow(dmabuf->phys);
12249 eq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
12250 putPaddrHigh(dmabuf->phys);
12251 }
12252 mbox->vport = phba->pport;
12253 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
12254 mbox->context1 = NULL;
12255 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
12256 shdr = (union lpfc_sli4_cfg_shdr *) &eq_create->header.cfg_shdr;
12257 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12258 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12259 if (shdr_status || shdr_add_status || rc) {
12260 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12261 "2500 EQ_CREATE mailbox failed with "
12262 "status x%x add_status x%x, mbx status x%x\n",
12263 shdr_status, shdr_add_status, rc);
12264 status = -ENXIO;
12265 }
12266 eq->type = LPFC_EQ;
12267 eq->subtype = LPFC_NONE;
12268 eq->queue_id = bf_get(lpfc_mbx_eq_create_q_id, &eq_create->u.response);
12269 if (eq->queue_id == 0xFFFF)
12270 status = -ENXIO;
12271 eq->host_index = 0;
12272 eq->hba_index = 0;
12273
James Smart8fa38512009-07-19 10:01:03 -040012274 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040012275 return status;
12276}
12277
12278/**
12279 * lpfc_cq_create - Create a Completion Queue on the HBA
12280 * @phba: HBA structure that indicates port to create a queue on.
12281 * @cq: The queue structure to use to create the completion queue.
12282 * @eq: The event queue to bind this completion queue to.
12283 *
12284 * This function creates a completion queue, as detailed in @wq, on a port,
12285 * described by @phba by sending a CQ_CREATE mailbox command to the HBA.
12286 *
12287 * The @phba struct is used to send mailbox command to HBA. The @cq struct
12288 * is used to get the entry count and entry size that are necessary to
12289 * determine the number of pages to allocate and use for this queue. The @eq
12290 * is used to indicate which event queue to bind this completion queue to. This
12291 * function will send the CQ_CREATE mailbox command to the HBA to setup the
12292 * completion queue. This function is asynchronous and will wait for the mailbox
12293 * command to finish before continuing.
12294 *
12295 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040012296 * memory this function will return -ENOMEM. If the queue create mailbox command
12297 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040012298 **/
12299uint32_t
12300lpfc_cq_create(struct lpfc_hba *phba, struct lpfc_queue *cq,
12301 struct lpfc_queue *eq, uint32_t type, uint32_t subtype)
12302{
12303 struct lpfc_mbx_cq_create *cq_create;
12304 struct lpfc_dmabuf *dmabuf;
12305 LPFC_MBOXQ_t *mbox;
12306 int rc, length, status = 0;
12307 uint32_t shdr_status, shdr_add_status;
12308 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040012309 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
12310
James Smart2e90f4b2011-12-13 13:22:37 -050012311 /* sanity check on queue memory */
12312 if (!cq || !eq)
12313 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040012314 if (!phba->sli4_hba.pc_sli4_params.supported)
12315 hw_page_size = SLI4_PAGE_SIZE;
12316
James Smart4f774512009-05-22 14:52:35 -040012317 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12318 if (!mbox)
12319 return -ENOMEM;
12320 length = (sizeof(struct lpfc_mbx_cq_create) -
12321 sizeof(struct lpfc_sli4_cfg_mhdr));
12322 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
12323 LPFC_MBOX_OPCODE_CQ_CREATE,
12324 length, LPFC_SLI4_MBX_EMBED);
12325 cq_create = &mbox->u.mqe.un.cq_create;
James Smart5a6f1332011-03-11 16:05:35 -050012326 shdr = (union lpfc_sli4_cfg_shdr *) &cq_create->header.cfg_shdr;
James Smart4f774512009-05-22 14:52:35 -040012327 bf_set(lpfc_mbx_cq_create_num_pages, &cq_create->u.request,
12328 cq->page_count);
12329 bf_set(lpfc_cq_context_event, &cq_create->u.request.context, 1);
12330 bf_set(lpfc_cq_context_valid, &cq_create->u.request.context, 1);
James Smart5a6f1332011-03-11 16:05:35 -050012331 bf_set(lpfc_mbox_hdr_version, &shdr->request,
12332 phba->sli4_hba.pc_sli4_params.cqv);
12333 if (phba->sli4_hba.pc_sli4_params.cqv == LPFC_Q_CREATE_VERSION_2) {
James Smartc31098c2011-04-16 11:03:33 -040012334 /* FW only supports 1. Should be PAGE_SIZE/SLI4_PAGE_SIZE */
12335 bf_set(lpfc_mbx_cq_create_page_size, &cq_create->u.request, 1);
James Smart5a6f1332011-03-11 16:05:35 -050012336 bf_set(lpfc_cq_eq_id_2, &cq_create->u.request.context,
12337 eq->queue_id);
12338 } else {
12339 bf_set(lpfc_cq_eq_id, &cq_create->u.request.context,
12340 eq->queue_id);
12341 }
James Smart4f774512009-05-22 14:52:35 -040012342 switch (cq->entry_count) {
12343 default:
12344 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12345 "0361 Unsupported CQ count. (%d)\n",
12346 cq->entry_count);
James Smart4f4c1862012-06-12 13:54:02 -040012347 if (cq->entry_count < 256) {
12348 status = -EINVAL;
12349 goto out;
12350 }
James Smart4f774512009-05-22 14:52:35 -040012351 /* otherwise default to smallest count (drop through) */
12352 case 256:
12353 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
12354 LPFC_CQ_CNT_256);
12355 break;
12356 case 512:
12357 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
12358 LPFC_CQ_CNT_512);
12359 break;
12360 case 1024:
12361 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
12362 LPFC_CQ_CNT_1024);
12363 break;
12364 }
12365 list_for_each_entry(dmabuf, &cq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040012366 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -040012367 cq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
12368 putPaddrLow(dmabuf->phys);
12369 cq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
12370 putPaddrHigh(dmabuf->phys);
12371 }
12372 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
12373
12374 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart4f774512009-05-22 14:52:35 -040012375 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12376 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12377 if (shdr_status || shdr_add_status || rc) {
12378 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12379 "2501 CQ_CREATE mailbox failed with "
12380 "status x%x add_status x%x, mbx status x%x\n",
12381 shdr_status, shdr_add_status, rc);
12382 status = -ENXIO;
12383 goto out;
12384 }
12385 cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
12386 if (cq->queue_id == 0xFFFF) {
12387 status = -ENXIO;
12388 goto out;
12389 }
12390 /* link the cq onto the parent eq child list */
12391 list_add_tail(&cq->list, &eq->child_list);
12392 /* Set up completion queue's type and subtype */
12393 cq->type = type;
12394 cq->subtype = subtype;
12395 cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
James Smart2a622bf2011-02-16 12:40:06 -050012396 cq->assoc_qid = eq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040012397 cq->host_index = 0;
12398 cq->hba_index = 0;
James Smart4f774512009-05-22 14:52:35 -040012399
James Smart8fa38512009-07-19 10:01:03 -040012400out:
12401 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040012402 return status;
12403}
12404
12405/**
James Smartb19a0612010-04-06 14:48:51 -040012406 * lpfc_mq_create_fb_init - Send MCC_CREATE without async events registration
James Smart04c68492009-05-22 14:52:52 -040012407 * @phba: HBA structure that indicates port to create a queue on.
12408 * @mq: The queue structure to use to create the mailbox queue.
James Smartb19a0612010-04-06 14:48:51 -040012409 * @mbox: An allocated pointer to type LPFC_MBOXQ_t
12410 * @cq: The completion queue to associate with this cq.
James Smart04c68492009-05-22 14:52:52 -040012411 *
James Smartb19a0612010-04-06 14:48:51 -040012412 * This function provides failback (fb) functionality when the
12413 * mq_create_ext fails on older FW generations. It's purpose is identical
12414 * to mq_create_ext otherwise.
James Smart04c68492009-05-22 14:52:52 -040012415 *
James Smartb19a0612010-04-06 14:48:51 -040012416 * This routine cannot fail as all attributes were previously accessed and
12417 * initialized in mq_create_ext.
James Smart04c68492009-05-22 14:52:52 -040012418 **/
James Smartb19a0612010-04-06 14:48:51 -040012419static void
12420lpfc_mq_create_fb_init(struct lpfc_hba *phba, struct lpfc_queue *mq,
12421 LPFC_MBOXQ_t *mbox, struct lpfc_queue *cq)
James Smart04c68492009-05-22 14:52:52 -040012422{
12423 struct lpfc_mbx_mq_create *mq_create;
12424 struct lpfc_dmabuf *dmabuf;
James Smartb19a0612010-04-06 14:48:51 -040012425 int length;
James Smart04c68492009-05-22 14:52:52 -040012426
James Smart04c68492009-05-22 14:52:52 -040012427 length = (sizeof(struct lpfc_mbx_mq_create) -
12428 sizeof(struct lpfc_sli4_cfg_mhdr));
12429 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
12430 LPFC_MBOX_OPCODE_MQ_CREATE,
12431 length, LPFC_SLI4_MBX_EMBED);
12432 mq_create = &mbox->u.mqe.un.mq_create;
12433 bf_set(lpfc_mbx_mq_create_num_pages, &mq_create->u.request,
James Smartb19a0612010-04-06 14:48:51 -040012434 mq->page_count);
James Smart04c68492009-05-22 14:52:52 -040012435 bf_set(lpfc_mq_context_cq_id, &mq_create->u.request.context,
James Smartb19a0612010-04-06 14:48:51 -040012436 cq->queue_id);
James Smart04c68492009-05-22 14:52:52 -040012437 bf_set(lpfc_mq_context_valid, &mq_create->u.request.context, 1);
12438 switch (mq->entry_count) {
James Smart04c68492009-05-22 14:52:52 -040012439 case 16:
James Smart5a6f1332011-03-11 16:05:35 -050012440 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
12441 LPFC_MQ_RING_SIZE_16);
James Smart04c68492009-05-22 14:52:52 -040012442 break;
12443 case 32:
James Smart5a6f1332011-03-11 16:05:35 -050012444 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
12445 LPFC_MQ_RING_SIZE_32);
James Smart04c68492009-05-22 14:52:52 -040012446 break;
12447 case 64:
James Smart5a6f1332011-03-11 16:05:35 -050012448 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
12449 LPFC_MQ_RING_SIZE_64);
James Smart04c68492009-05-22 14:52:52 -040012450 break;
12451 case 128:
James Smart5a6f1332011-03-11 16:05:35 -050012452 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
12453 LPFC_MQ_RING_SIZE_128);
James Smart04c68492009-05-22 14:52:52 -040012454 break;
12455 }
12456 list_for_each_entry(dmabuf, &mq->page_list, list) {
12457 mq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
James Smartb19a0612010-04-06 14:48:51 -040012458 putPaddrLow(dmabuf->phys);
James Smart04c68492009-05-22 14:52:52 -040012459 mq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
James Smartb19a0612010-04-06 14:48:51 -040012460 putPaddrHigh(dmabuf->phys);
12461 }
12462}
12463
12464/**
12465 * lpfc_mq_create - Create a mailbox Queue on the HBA
12466 * @phba: HBA structure that indicates port to create a queue on.
12467 * @mq: The queue structure to use to create the mailbox queue.
12468 * @cq: The completion queue to associate with this cq.
12469 * @subtype: The queue's subtype.
12470 *
12471 * This function creates a mailbox queue, as detailed in @mq, on a port,
12472 * described by @phba by sending a MQ_CREATE mailbox command to the HBA.
12473 *
12474 * The @phba struct is used to send mailbox command to HBA. The @cq struct
12475 * is used to get the entry count and entry size that are necessary to
12476 * determine the number of pages to allocate and use for this queue. This
12477 * function will send the MQ_CREATE mailbox command to the HBA to setup the
12478 * mailbox queue. This function is asynchronous and will wait for the mailbox
12479 * command to finish before continuing.
12480 *
12481 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040012482 * memory this function will return -ENOMEM. If the queue create mailbox command
12483 * fails this function will return -ENXIO.
James Smartb19a0612010-04-06 14:48:51 -040012484 **/
12485int32_t
12486lpfc_mq_create(struct lpfc_hba *phba, struct lpfc_queue *mq,
12487 struct lpfc_queue *cq, uint32_t subtype)
12488{
12489 struct lpfc_mbx_mq_create *mq_create;
12490 struct lpfc_mbx_mq_create_ext *mq_create_ext;
12491 struct lpfc_dmabuf *dmabuf;
12492 LPFC_MBOXQ_t *mbox;
12493 int rc, length, status = 0;
12494 uint32_t shdr_status, shdr_add_status;
12495 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040012496 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smartb19a0612010-04-06 14:48:51 -040012497
James Smart2e90f4b2011-12-13 13:22:37 -050012498 /* sanity check on queue memory */
12499 if (!mq || !cq)
12500 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040012501 if (!phba->sli4_hba.pc_sli4_params.supported)
12502 hw_page_size = SLI4_PAGE_SIZE;
James Smartb19a0612010-04-06 14:48:51 -040012503
12504 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12505 if (!mbox)
12506 return -ENOMEM;
12507 length = (sizeof(struct lpfc_mbx_mq_create_ext) -
12508 sizeof(struct lpfc_sli4_cfg_mhdr));
12509 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
12510 LPFC_MBOX_OPCODE_MQ_CREATE_EXT,
12511 length, LPFC_SLI4_MBX_EMBED);
12512
12513 mq_create_ext = &mbox->u.mqe.un.mq_create_ext;
James Smart5a6f1332011-03-11 16:05:35 -050012514 shdr = (union lpfc_sli4_cfg_shdr *) &mq_create_ext->header.cfg_shdr;
James Smart70f3c072010-12-15 17:57:33 -050012515 bf_set(lpfc_mbx_mq_create_ext_num_pages,
12516 &mq_create_ext->u.request, mq->page_count);
12517 bf_set(lpfc_mbx_mq_create_ext_async_evt_link,
12518 &mq_create_ext->u.request, 1);
12519 bf_set(lpfc_mbx_mq_create_ext_async_evt_fip,
James Smartb19a0612010-04-06 14:48:51 -040012520 &mq_create_ext->u.request, 1);
12521 bf_set(lpfc_mbx_mq_create_ext_async_evt_group5,
12522 &mq_create_ext->u.request, 1);
James Smart70f3c072010-12-15 17:57:33 -050012523 bf_set(lpfc_mbx_mq_create_ext_async_evt_fc,
12524 &mq_create_ext->u.request, 1);
12525 bf_set(lpfc_mbx_mq_create_ext_async_evt_sli,
12526 &mq_create_ext->u.request, 1);
James Smartb19a0612010-04-06 14:48:51 -040012527 bf_set(lpfc_mq_context_valid, &mq_create_ext->u.request.context, 1);
James Smart5a6f1332011-03-11 16:05:35 -050012528 bf_set(lpfc_mbox_hdr_version, &shdr->request,
12529 phba->sli4_hba.pc_sli4_params.mqv);
12530 if (phba->sli4_hba.pc_sli4_params.mqv == LPFC_Q_CREATE_VERSION_1)
12531 bf_set(lpfc_mbx_mq_create_ext_cq_id, &mq_create_ext->u.request,
12532 cq->queue_id);
12533 else
12534 bf_set(lpfc_mq_context_cq_id, &mq_create_ext->u.request.context,
12535 cq->queue_id);
James Smartb19a0612010-04-06 14:48:51 -040012536 switch (mq->entry_count) {
12537 default:
12538 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12539 "0362 Unsupported MQ count. (%d)\n",
12540 mq->entry_count);
James Smart4f4c1862012-06-12 13:54:02 -040012541 if (mq->entry_count < 16) {
12542 status = -EINVAL;
12543 goto out;
12544 }
James Smartb19a0612010-04-06 14:48:51 -040012545 /* otherwise default to smallest count (drop through) */
12546 case 16:
James Smart5a6f1332011-03-11 16:05:35 -050012547 bf_set(lpfc_mq_context_ring_size,
12548 &mq_create_ext->u.request.context,
12549 LPFC_MQ_RING_SIZE_16);
James Smartb19a0612010-04-06 14:48:51 -040012550 break;
12551 case 32:
James Smart5a6f1332011-03-11 16:05:35 -050012552 bf_set(lpfc_mq_context_ring_size,
12553 &mq_create_ext->u.request.context,
12554 LPFC_MQ_RING_SIZE_32);
James Smartb19a0612010-04-06 14:48:51 -040012555 break;
12556 case 64:
James Smart5a6f1332011-03-11 16:05:35 -050012557 bf_set(lpfc_mq_context_ring_size,
12558 &mq_create_ext->u.request.context,
12559 LPFC_MQ_RING_SIZE_64);
James Smartb19a0612010-04-06 14:48:51 -040012560 break;
12561 case 128:
James Smart5a6f1332011-03-11 16:05:35 -050012562 bf_set(lpfc_mq_context_ring_size,
12563 &mq_create_ext->u.request.context,
12564 LPFC_MQ_RING_SIZE_128);
James Smartb19a0612010-04-06 14:48:51 -040012565 break;
12566 }
12567 list_for_each_entry(dmabuf, &mq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040012568 memset(dmabuf->virt, 0, hw_page_size);
James Smartb19a0612010-04-06 14:48:51 -040012569 mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_lo =
12570 putPaddrLow(dmabuf->phys);
12571 mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_hi =
James Smart04c68492009-05-22 14:52:52 -040012572 putPaddrHigh(dmabuf->phys);
12573 }
12574 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
James Smartb19a0612010-04-06 14:48:51 -040012575 mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
12576 &mq_create_ext->u.response);
12577 if (rc != MBX_SUCCESS) {
12578 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
12579 "2795 MQ_CREATE_EXT failed with "
12580 "status x%x. Failback to MQ_CREATE.\n",
12581 rc);
12582 lpfc_mq_create_fb_init(phba, mq, mbox, cq);
12583 mq_create = &mbox->u.mqe.un.mq_create;
12584 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
12585 shdr = (union lpfc_sli4_cfg_shdr *) &mq_create->header.cfg_shdr;
12586 mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
12587 &mq_create->u.response);
12588 }
12589
James Smart04c68492009-05-22 14:52:52 -040012590 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart04c68492009-05-22 14:52:52 -040012591 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12592 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12593 if (shdr_status || shdr_add_status || rc) {
12594 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12595 "2502 MQ_CREATE mailbox failed with "
12596 "status x%x add_status x%x, mbx status x%x\n",
12597 shdr_status, shdr_add_status, rc);
12598 status = -ENXIO;
12599 goto out;
12600 }
James Smart04c68492009-05-22 14:52:52 -040012601 if (mq->queue_id == 0xFFFF) {
12602 status = -ENXIO;
12603 goto out;
12604 }
12605 mq->type = LPFC_MQ;
James Smart2a622bf2011-02-16 12:40:06 -050012606 mq->assoc_qid = cq->queue_id;
James Smart04c68492009-05-22 14:52:52 -040012607 mq->subtype = subtype;
12608 mq->host_index = 0;
12609 mq->hba_index = 0;
12610
12611 /* link the mq onto the parent cq child list */
12612 list_add_tail(&mq->list, &cq->child_list);
12613out:
James Smart8fa38512009-07-19 10:01:03 -040012614 mempool_free(mbox, phba->mbox_mem_pool);
James Smart04c68492009-05-22 14:52:52 -040012615 return status;
12616}
12617
12618/**
James Smart4f774512009-05-22 14:52:35 -040012619 * lpfc_wq_create - Create a Work Queue on the HBA
12620 * @phba: HBA structure that indicates port to create a queue on.
12621 * @wq: The queue structure to use to create the work queue.
12622 * @cq: The completion queue to bind this work queue to.
12623 * @subtype: The subtype of the work queue indicating its functionality.
12624 *
12625 * This function creates a work queue, as detailed in @wq, on a port, described
12626 * by @phba by sending a WQ_CREATE mailbox command to the HBA.
12627 *
12628 * The @phba struct is used to send mailbox command to HBA. The @wq struct
12629 * is used to get the entry count and entry size that are necessary to
12630 * determine the number of pages to allocate and use for this queue. The @cq
12631 * is used to indicate which completion queue to bind this work queue to. This
12632 * function will send the WQ_CREATE mailbox command to the HBA to setup the
12633 * work queue. This function is asynchronous and will wait for the mailbox
12634 * command to finish before continuing.
12635 *
12636 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040012637 * memory this function will return -ENOMEM. If the queue create mailbox command
12638 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040012639 **/
12640uint32_t
12641lpfc_wq_create(struct lpfc_hba *phba, struct lpfc_queue *wq,
12642 struct lpfc_queue *cq, uint32_t subtype)
12643{
12644 struct lpfc_mbx_wq_create *wq_create;
12645 struct lpfc_dmabuf *dmabuf;
12646 LPFC_MBOXQ_t *mbox;
12647 int rc, length, status = 0;
12648 uint32_t shdr_status, shdr_add_status;
12649 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040012650 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smart5a6f1332011-03-11 16:05:35 -050012651 struct dma_address *page;
James Smart49198b32010-04-06 15:04:33 -040012652
James Smart2e90f4b2011-12-13 13:22:37 -050012653 /* sanity check on queue memory */
12654 if (!wq || !cq)
12655 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040012656 if (!phba->sli4_hba.pc_sli4_params.supported)
12657 hw_page_size = SLI4_PAGE_SIZE;
James Smart4f774512009-05-22 14:52:35 -040012658
12659 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12660 if (!mbox)
12661 return -ENOMEM;
12662 length = (sizeof(struct lpfc_mbx_wq_create) -
12663 sizeof(struct lpfc_sli4_cfg_mhdr));
12664 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
12665 LPFC_MBOX_OPCODE_FCOE_WQ_CREATE,
12666 length, LPFC_SLI4_MBX_EMBED);
12667 wq_create = &mbox->u.mqe.un.wq_create;
James Smart5a6f1332011-03-11 16:05:35 -050012668 shdr = (union lpfc_sli4_cfg_shdr *) &wq_create->header.cfg_shdr;
James Smart4f774512009-05-22 14:52:35 -040012669 bf_set(lpfc_mbx_wq_create_num_pages, &wq_create->u.request,
12670 wq->page_count);
12671 bf_set(lpfc_mbx_wq_create_cq_id, &wq_create->u.request,
12672 cq->queue_id);
James Smart5a6f1332011-03-11 16:05:35 -050012673 bf_set(lpfc_mbox_hdr_version, &shdr->request,
12674 phba->sli4_hba.pc_sli4_params.wqv);
12675 if (phba->sli4_hba.pc_sli4_params.wqv == LPFC_Q_CREATE_VERSION_1) {
12676 bf_set(lpfc_mbx_wq_create_wqe_count, &wq_create->u.request_1,
12677 wq->entry_count);
12678 switch (wq->entry_size) {
12679 default:
12680 case 64:
12681 bf_set(lpfc_mbx_wq_create_wqe_size,
12682 &wq_create->u.request_1,
12683 LPFC_WQ_WQE_SIZE_64);
12684 break;
12685 case 128:
12686 bf_set(lpfc_mbx_wq_create_wqe_size,
12687 &wq_create->u.request_1,
12688 LPFC_WQ_WQE_SIZE_128);
12689 break;
12690 }
12691 bf_set(lpfc_mbx_wq_create_page_size, &wq_create->u.request_1,
12692 (PAGE_SIZE/SLI4_PAGE_SIZE));
12693 page = wq_create->u.request_1.page;
12694 } else {
12695 page = wq_create->u.request.page;
12696 }
James Smart4f774512009-05-22 14:52:35 -040012697 list_for_each_entry(dmabuf, &wq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040012698 memset(dmabuf->virt, 0, hw_page_size);
James Smart5a6f1332011-03-11 16:05:35 -050012699 page[dmabuf->buffer_tag].addr_lo = putPaddrLow(dmabuf->phys);
12700 page[dmabuf->buffer_tag].addr_hi = putPaddrHigh(dmabuf->phys);
James Smart4f774512009-05-22 14:52:35 -040012701 }
12702 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
12703 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart4f774512009-05-22 14:52:35 -040012704 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12705 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12706 if (shdr_status || shdr_add_status || rc) {
12707 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12708 "2503 WQ_CREATE mailbox failed with "
12709 "status x%x add_status x%x, mbx status x%x\n",
12710 shdr_status, shdr_add_status, rc);
12711 status = -ENXIO;
12712 goto out;
12713 }
12714 wq->queue_id = bf_get(lpfc_mbx_wq_create_q_id, &wq_create->u.response);
12715 if (wq->queue_id == 0xFFFF) {
12716 status = -ENXIO;
12717 goto out;
12718 }
12719 wq->type = LPFC_WQ;
James Smart2a622bf2011-02-16 12:40:06 -050012720 wq->assoc_qid = cq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040012721 wq->subtype = subtype;
12722 wq->host_index = 0;
12723 wq->hba_index = 0;
James Smartff78d8f2011-12-13 13:21:35 -050012724 wq->entry_repost = LPFC_RELEASE_NOTIFICATION_INTERVAL;
James Smart4f774512009-05-22 14:52:35 -040012725
12726 /* link the wq onto the parent cq child list */
12727 list_add_tail(&wq->list, &cq->child_list);
12728out:
James Smart8fa38512009-07-19 10:01:03 -040012729 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040012730 return status;
12731}
12732
12733/**
James Smart73d91e52011-10-10 21:32:10 -040012734 * lpfc_rq_adjust_repost - Adjust entry_repost for an RQ
12735 * @phba: HBA structure that indicates port to create a queue on.
12736 * @rq: The queue structure to use for the receive queue.
12737 * @qno: The associated HBQ number
12738 *
12739 *
12740 * For SLI4 we need to adjust the RQ repost value based on
12741 * the number of buffers that are initially posted to the RQ.
12742 */
12743void
12744lpfc_rq_adjust_repost(struct lpfc_hba *phba, struct lpfc_queue *rq, int qno)
12745{
12746 uint32_t cnt;
12747
James Smart2e90f4b2011-12-13 13:22:37 -050012748 /* sanity check on queue memory */
12749 if (!rq)
12750 return;
James Smart73d91e52011-10-10 21:32:10 -040012751 cnt = lpfc_hbq_defs[qno]->entry_count;
12752
12753 /* Recalc repost for RQs based on buffers initially posted */
12754 cnt = (cnt >> 3);
12755 if (cnt < LPFC_QUEUE_MIN_REPOST)
12756 cnt = LPFC_QUEUE_MIN_REPOST;
12757
12758 rq->entry_repost = cnt;
12759}
12760
12761/**
James Smart4f774512009-05-22 14:52:35 -040012762 * lpfc_rq_create - Create a Receive Queue on the HBA
12763 * @phba: HBA structure that indicates port to create a queue on.
12764 * @hrq: The queue structure to use to create the header receive queue.
12765 * @drq: The queue structure to use to create the data receive queue.
12766 * @cq: The completion queue to bind this work queue to.
12767 *
12768 * This function creates a receive buffer queue pair , as detailed in @hrq and
12769 * @drq, on a port, described by @phba by sending a RQ_CREATE mailbox command
12770 * to the HBA.
12771 *
12772 * The @phba struct is used to send mailbox command to HBA. The @drq and @hrq
12773 * struct is used to get the entry count that is necessary to determine the
12774 * number of pages to use for this queue. The @cq is used to indicate which
12775 * completion queue to bind received buffers that are posted to these queues to.
12776 * This function will send the RQ_CREATE mailbox command to the HBA to setup the
12777 * receive queue pair. This function is asynchronous and will wait for the
12778 * mailbox command to finish before continuing.
12779 *
12780 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040012781 * memory this function will return -ENOMEM. If the queue create mailbox command
12782 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040012783 **/
12784uint32_t
12785lpfc_rq_create(struct lpfc_hba *phba, struct lpfc_queue *hrq,
12786 struct lpfc_queue *drq, struct lpfc_queue *cq, uint32_t subtype)
12787{
12788 struct lpfc_mbx_rq_create *rq_create;
12789 struct lpfc_dmabuf *dmabuf;
12790 LPFC_MBOXQ_t *mbox;
12791 int rc, length, status = 0;
12792 uint32_t shdr_status, shdr_add_status;
12793 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040012794 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
12795
James Smart2e90f4b2011-12-13 13:22:37 -050012796 /* sanity check on queue memory */
12797 if (!hrq || !drq || !cq)
12798 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040012799 if (!phba->sli4_hba.pc_sli4_params.supported)
12800 hw_page_size = SLI4_PAGE_SIZE;
James Smart4f774512009-05-22 14:52:35 -040012801
12802 if (hrq->entry_count != drq->entry_count)
12803 return -EINVAL;
12804 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12805 if (!mbox)
12806 return -ENOMEM;
12807 length = (sizeof(struct lpfc_mbx_rq_create) -
12808 sizeof(struct lpfc_sli4_cfg_mhdr));
12809 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
12810 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
12811 length, LPFC_SLI4_MBX_EMBED);
12812 rq_create = &mbox->u.mqe.un.rq_create;
James Smart5a6f1332011-03-11 16:05:35 -050012813 shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
12814 bf_set(lpfc_mbox_hdr_version, &shdr->request,
12815 phba->sli4_hba.pc_sli4_params.rqv);
12816 if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
12817 bf_set(lpfc_rq_context_rqe_count_1,
12818 &rq_create->u.request.context,
12819 hrq->entry_count);
12820 rq_create->u.request.context.buffer_size = LPFC_HDR_BUF_SIZE;
James Smartc31098c2011-04-16 11:03:33 -040012821 bf_set(lpfc_rq_context_rqe_size,
12822 &rq_create->u.request.context,
12823 LPFC_RQE_SIZE_8);
12824 bf_set(lpfc_rq_context_page_size,
12825 &rq_create->u.request.context,
12826 (PAGE_SIZE/SLI4_PAGE_SIZE));
James Smart5a6f1332011-03-11 16:05:35 -050012827 } else {
12828 switch (hrq->entry_count) {
12829 default:
12830 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12831 "2535 Unsupported RQ count. (%d)\n",
12832 hrq->entry_count);
James Smart4f4c1862012-06-12 13:54:02 -040012833 if (hrq->entry_count < 512) {
12834 status = -EINVAL;
12835 goto out;
12836 }
James Smart5a6f1332011-03-11 16:05:35 -050012837 /* otherwise default to smallest count (drop through) */
12838 case 512:
12839 bf_set(lpfc_rq_context_rqe_count,
12840 &rq_create->u.request.context,
12841 LPFC_RQ_RING_SIZE_512);
12842 break;
12843 case 1024:
12844 bf_set(lpfc_rq_context_rqe_count,
12845 &rq_create->u.request.context,
12846 LPFC_RQ_RING_SIZE_1024);
12847 break;
12848 case 2048:
12849 bf_set(lpfc_rq_context_rqe_count,
12850 &rq_create->u.request.context,
12851 LPFC_RQ_RING_SIZE_2048);
12852 break;
12853 case 4096:
12854 bf_set(lpfc_rq_context_rqe_count,
12855 &rq_create->u.request.context,
12856 LPFC_RQ_RING_SIZE_4096);
12857 break;
12858 }
12859 bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
12860 LPFC_HDR_BUF_SIZE);
James Smart4f774512009-05-22 14:52:35 -040012861 }
12862 bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
12863 cq->queue_id);
12864 bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
12865 hrq->page_count);
James Smart4f774512009-05-22 14:52:35 -040012866 list_for_each_entry(dmabuf, &hrq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040012867 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -040012868 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
12869 putPaddrLow(dmabuf->phys);
12870 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
12871 putPaddrHigh(dmabuf->phys);
12872 }
12873 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
12874 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart4f774512009-05-22 14:52:35 -040012875 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12876 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12877 if (shdr_status || shdr_add_status || rc) {
12878 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12879 "2504 RQ_CREATE mailbox failed with "
12880 "status x%x add_status x%x, mbx status x%x\n",
12881 shdr_status, shdr_add_status, rc);
12882 status = -ENXIO;
12883 goto out;
12884 }
12885 hrq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
12886 if (hrq->queue_id == 0xFFFF) {
12887 status = -ENXIO;
12888 goto out;
12889 }
12890 hrq->type = LPFC_HRQ;
James Smart2a622bf2011-02-16 12:40:06 -050012891 hrq->assoc_qid = cq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040012892 hrq->subtype = subtype;
12893 hrq->host_index = 0;
12894 hrq->hba_index = 0;
12895
12896 /* now create the data queue */
12897 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
12898 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
12899 length, LPFC_SLI4_MBX_EMBED);
James Smart5a6f1332011-03-11 16:05:35 -050012900 bf_set(lpfc_mbox_hdr_version, &shdr->request,
12901 phba->sli4_hba.pc_sli4_params.rqv);
12902 if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
12903 bf_set(lpfc_rq_context_rqe_count_1,
James Smartc31098c2011-04-16 11:03:33 -040012904 &rq_create->u.request.context, hrq->entry_count);
James Smart5a6f1332011-03-11 16:05:35 -050012905 rq_create->u.request.context.buffer_size = LPFC_DATA_BUF_SIZE;
James Smartc31098c2011-04-16 11:03:33 -040012906 bf_set(lpfc_rq_context_rqe_size, &rq_create->u.request.context,
12907 LPFC_RQE_SIZE_8);
12908 bf_set(lpfc_rq_context_page_size, &rq_create->u.request.context,
12909 (PAGE_SIZE/SLI4_PAGE_SIZE));
James Smart5a6f1332011-03-11 16:05:35 -050012910 } else {
12911 switch (drq->entry_count) {
12912 default:
12913 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12914 "2536 Unsupported RQ count. (%d)\n",
12915 drq->entry_count);
James Smart4f4c1862012-06-12 13:54:02 -040012916 if (drq->entry_count < 512) {
12917 status = -EINVAL;
12918 goto out;
12919 }
James Smart5a6f1332011-03-11 16:05:35 -050012920 /* otherwise default to smallest count (drop through) */
12921 case 512:
12922 bf_set(lpfc_rq_context_rqe_count,
12923 &rq_create->u.request.context,
12924 LPFC_RQ_RING_SIZE_512);
12925 break;
12926 case 1024:
12927 bf_set(lpfc_rq_context_rqe_count,
12928 &rq_create->u.request.context,
12929 LPFC_RQ_RING_SIZE_1024);
12930 break;
12931 case 2048:
12932 bf_set(lpfc_rq_context_rqe_count,
12933 &rq_create->u.request.context,
12934 LPFC_RQ_RING_SIZE_2048);
12935 break;
12936 case 4096:
12937 bf_set(lpfc_rq_context_rqe_count,
12938 &rq_create->u.request.context,
12939 LPFC_RQ_RING_SIZE_4096);
12940 break;
12941 }
12942 bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
12943 LPFC_DATA_BUF_SIZE);
James Smart4f774512009-05-22 14:52:35 -040012944 }
12945 bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
12946 cq->queue_id);
12947 bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
12948 drq->page_count);
James Smart4f774512009-05-22 14:52:35 -040012949 list_for_each_entry(dmabuf, &drq->page_list, list) {
12950 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
12951 putPaddrLow(dmabuf->phys);
12952 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
12953 putPaddrHigh(dmabuf->phys);
12954 }
12955 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
12956 /* The IOCTL status is embedded in the mailbox subheader. */
12957 shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
12958 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12959 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12960 if (shdr_status || shdr_add_status || rc) {
12961 status = -ENXIO;
12962 goto out;
12963 }
12964 drq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
12965 if (drq->queue_id == 0xFFFF) {
12966 status = -ENXIO;
12967 goto out;
12968 }
12969 drq->type = LPFC_DRQ;
James Smart2a622bf2011-02-16 12:40:06 -050012970 drq->assoc_qid = cq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040012971 drq->subtype = subtype;
12972 drq->host_index = 0;
12973 drq->hba_index = 0;
12974
12975 /* link the header and data RQs onto the parent cq child list */
12976 list_add_tail(&hrq->list, &cq->child_list);
12977 list_add_tail(&drq->list, &cq->child_list);
12978
12979out:
James Smart8fa38512009-07-19 10:01:03 -040012980 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040012981 return status;
12982}
12983
12984/**
12985 * lpfc_eq_destroy - Destroy an event Queue on the HBA
12986 * @eq: The queue structure associated with the queue to destroy.
12987 *
12988 * This function destroys a queue, as detailed in @eq by sending an mailbox
12989 * command, specific to the type of queue, to the HBA.
12990 *
12991 * The @eq struct is used to get the queue ID of the queue to destroy.
12992 *
12993 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040012994 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040012995 **/
12996uint32_t
12997lpfc_eq_destroy(struct lpfc_hba *phba, struct lpfc_queue *eq)
12998{
12999 LPFC_MBOXQ_t *mbox;
13000 int rc, length, status = 0;
13001 uint32_t shdr_status, shdr_add_status;
13002 union lpfc_sli4_cfg_shdr *shdr;
13003
James Smart2e90f4b2011-12-13 13:22:37 -050013004 /* sanity check on queue memory */
James Smart4f774512009-05-22 14:52:35 -040013005 if (!eq)
13006 return -ENODEV;
13007 mbox = mempool_alloc(eq->phba->mbox_mem_pool, GFP_KERNEL);
13008 if (!mbox)
13009 return -ENOMEM;
13010 length = (sizeof(struct lpfc_mbx_eq_destroy) -
13011 sizeof(struct lpfc_sli4_cfg_mhdr));
13012 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
13013 LPFC_MBOX_OPCODE_EQ_DESTROY,
13014 length, LPFC_SLI4_MBX_EMBED);
13015 bf_set(lpfc_mbx_eq_destroy_q_id, &mbox->u.mqe.un.eq_destroy.u.request,
13016 eq->queue_id);
13017 mbox->vport = eq->phba->pport;
13018 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
13019
13020 rc = lpfc_sli_issue_mbox(eq->phba, mbox, MBX_POLL);
13021 /* The IOCTL status is embedded in the mailbox subheader. */
13022 shdr = (union lpfc_sli4_cfg_shdr *)
13023 &mbox->u.mqe.un.eq_destroy.header.cfg_shdr;
13024 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13025 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13026 if (shdr_status || shdr_add_status || rc) {
13027 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13028 "2505 EQ_DESTROY mailbox failed with "
13029 "status x%x add_status x%x, mbx status x%x\n",
13030 shdr_status, shdr_add_status, rc);
13031 status = -ENXIO;
13032 }
13033
13034 /* Remove eq from any list */
13035 list_del_init(&eq->list);
James Smart8fa38512009-07-19 10:01:03 -040013036 mempool_free(mbox, eq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040013037 return status;
13038}
13039
13040/**
13041 * lpfc_cq_destroy - Destroy a Completion Queue on the HBA
13042 * @cq: The queue structure associated with the queue to destroy.
13043 *
13044 * This function destroys a queue, as detailed in @cq by sending an mailbox
13045 * command, specific to the type of queue, to the HBA.
13046 *
13047 * The @cq struct is used to get the queue ID of the queue to destroy.
13048 *
13049 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040013050 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040013051 **/
13052uint32_t
13053lpfc_cq_destroy(struct lpfc_hba *phba, struct lpfc_queue *cq)
13054{
13055 LPFC_MBOXQ_t *mbox;
13056 int rc, length, status = 0;
13057 uint32_t shdr_status, shdr_add_status;
13058 union lpfc_sli4_cfg_shdr *shdr;
13059
James Smart2e90f4b2011-12-13 13:22:37 -050013060 /* sanity check on queue memory */
James Smart4f774512009-05-22 14:52:35 -040013061 if (!cq)
13062 return -ENODEV;
13063 mbox = mempool_alloc(cq->phba->mbox_mem_pool, GFP_KERNEL);
13064 if (!mbox)
13065 return -ENOMEM;
13066 length = (sizeof(struct lpfc_mbx_cq_destroy) -
13067 sizeof(struct lpfc_sli4_cfg_mhdr));
13068 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
13069 LPFC_MBOX_OPCODE_CQ_DESTROY,
13070 length, LPFC_SLI4_MBX_EMBED);
13071 bf_set(lpfc_mbx_cq_destroy_q_id, &mbox->u.mqe.un.cq_destroy.u.request,
13072 cq->queue_id);
13073 mbox->vport = cq->phba->pport;
13074 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
13075 rc = lpfc_sli_issue_mbox(cq->phba, mbox, MBX_POLL);
13076 /* The IOCTL status is embedded in the mailbox subheader. */
13077 shdr = (union lpfc_sli4_cfg_shdr *)
13078 &mbox->u.mqe.un.wq_create.header.cfg_shdr;
13079 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13080 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13081 if (shdr_status || shdr_add_status || rc) {
13082 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13083 "2506 CQ_DESTROY mailbox failed with "
13084 "status x%x add_status x%x, mbx status x%x\n",
13085 shdr_status, shdr_add_status, rc);
13086 status = -ENXIO;
13087 }
13088 /* Remove cq from any list */
13089 list_del_init(&cq->list);
James Smart8fa38512009-07-19 10:01:03 -040013090 mempool_free(mbox, cq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040013091 return status;
13092}
13093
13094/**
James Smart04c68492009-05-22 14:52:52 -040013095 * lpfc_mq_destroy - Destroy a Mailbox Queue on the HBA
13096 * @qm: The queue structure associated with the queue to destroy.
13097 *
13098 * This function destroys a queue, as detailed in @mq by sending an mailbox
13099 * command, specific to the type of queue, to the HBA.
13100 *
13101 * The @mq struct is used to get the queue ID of the queue to destroy.
13102 *
13103 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040013104 * command fails this function will return -ENXIO.
James Smart04c68492009-05-22 14:52:52 -040013105 **/
13106uint32_t
13107lpfc_mq_destroy(struct lpfc_hba *phba, struct lpfc_queue *mq)
13108{
13109 LPFC_MBOXQ_t *mbox;
13110 int rc, length, status = 0;
13111 uint32_t shdr_status, shdr_add_status;
13112 union lpfc_sli4_cfg_shdr *shdr;
13113
James Smart2e90f4b2011-12-13 13:22:37 -050013114 /* sanity check on queue memory */
James Smart04c68492009-05-22 14:52:52 -040013115 if (!mq)
13116 return -ENODEV;
13117 mbox = mempool_alloc(mq->phba->mbox_mem_pool, GFP_KERNEL);
13118 if (!mbox)
13119 return -ENOMEM;
13120 length = (sizeof(struct lpfc_mbx_mq_destroy) -
13121 sizeof(struct lpfc_sli4_cfg_mhdr));
13122 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
13123 LPFC_MBOX_OPCODE_MQ_DESTROY,
13124 length, LPFC_SLI4_MBX_EMBED);
13125 bf_set(lpfc_mbx_mq_destroy_q_id, &mbox->u.mqe.un.mq_destroy.u.request,
13126 mq->queue_id);
13127 mbox->vport = mq->phba->pport;
13128 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
13129 rc = lpfc_sli_issue_mbox(mq->phba, mbox, MBX_POLL);
13130 /* The IOCTL status is embedded in the mailbox subheader. */
13131 shdr = (union lpfc_sli4_cfg_shdr *)
13132 &mbox->u.mqe.un.mq_destroy.header.cfg_shdr;
13133 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13134 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13135 if (shdr_status || shdr_add_status || rc) {
13136 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13137 "2507 MQ_DESTROY mailbox failed with "
13138 "status x%x add_status x%x, mbx status x%x\n",
13139 shdr_status, shdr_add_status, rc);
13140 status = -ENXIO;
13141 }
13142 /* Remove mq from any list */
13143 list_del_init(&mq->list);
James Smart8fa38512009-07-19 10:01:03 -040013144 mempool_free(mbox, mq->phba->mbox_mem_pool);
James Smart04c68492009-05-22 14:52:52 -040013145 return status;
13146}
13147
13148/**
James Smart4f774512009-05-22 14:52:35 -040013149 * lpfc_wq_destroy - Destroy a Work Queue on the HBA
13150 * @wq: The queue structure associated with the queue to destroy.
13151 *
13152 * This function destroys a queue, as detailed in @wq by sending an mailbox
13153 * command, specific to the type of queue, to the HBA.
13154 *
13155 * The @wq struct is used to get the queue ID of the queue to destroy.
13156 *
13157 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040013158 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040013159 **/
13160uint32_t
13161lpfc_wq_destroy(struct lpfc_hba *phba, struct lpfc_queue *wq)
13162{
13163 LPFC_MBOXQ_t *mbox;
13164 int rc, length, status = 0;
13165 uint32_t shdr_status, shdr_add_status;
13166 union lpfc_sli4_cfg_shdr *shdr;
13167
James Smart2e90f4b2011-12-13 13:22:37 -050013168 /* sanity check on queue memory */
James Smart4f774512009-05-22 14:52:35 -040013169 if (!wq)
13170 return -ENODEV;
13171 mbox = mempool_alloc(wq->phba->mbox_mem_pool, GFP_KERNEL);
13172 if (!mbox)
13173 return -ENOMEM;
13174 length = (sizeof(struct lpfc_mbx_wq_destroy) -
13175 sizeof(struct lpfc_sli4_cfg_mhdr));
13176 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
13177 LPFC_MBOX_OPCODE_FCOE_WQ_DESTROY,
13178 length, LPFC_SLI4_MBX_EMBED);
13179 bf_set(lpfc_mbx_wq_destroy_q_id, &mbox->u.mqe.un.wq_destroy.u.request,
13180 wq->queue_id);
13181 mbox->vport = wq->phba->pport;
13182 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
13183 rc = lpfc_sli_issue_mbox(wq->phba, mbox, MBX_POLL);
13184 shdr = (union lpfc_sli4_cfg_shdr *)
13185 &mbox->u.mqe.un.wq_destroy.header.cfg_shdr;
13186 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13187 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13188 if (shdr_status || shdr_add_status || rc) {
13189 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13190 "2508 WQ_DESTROY mailbox failed with "
13191 "status x%x add_status x%x, mbx status x%x\n",
13192 shdr_status, shdr_add_status, rc);
13193 status = -ENXIO;
13194 }
13195 /* Remove wq from any list */
13196 list_del_init(&wq->list);
James Smart8fa38512009-07-19 10:01:03 -040013197 mempool_free(mbox, wq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040013198 return status;
13199}
13200
13201/**
13202 * lpfc_rq_destroy - Destroy a Receive Queue on the HBA
13203 * @rq: The queue structure associated with the queue to destroy.
13204 *
13205 * This function destroys a queue, as detailed in @rq by sending an mailbox
13206 * command, specific to the type of queue, to the HBA.
13207 *
13208 * The @rq struct is used to get the queue ID of the queue to destroy.
13209 *
13210 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040013211 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040013212 **/
13213uint32_t
13214lpfc_rq_destroy(struct lpfc_hba *phba, struct lpfc_queue *hrq,
13215 struct lpfc_queue *drq)
13216{
13217 LPFC_MBOXQ_t *mbox;
13218 int rc, length, status = 0;
13219 uint32_t shdr_status, shdr_add_status;
13220 union lpfc_sli4_cfg_shdr *shdr;
13221
James Smart2e90f4b2011-12-13 13:22:37 -050013222 /* sanity check on queue memory */
James Smart4f774512009-05-22 14:52:35 -040013223 if (!hrq || !drq)
13224 return -ENODEV;
13225 mbox = mempool_alloc(hrq->phba->mbox_mem_pool, GFP_KERNEL);
13226 if (!mbox)
13227 return -ENOMEM;
13228 length = (sizeof(struct lpfc_mbx_rq_destroy) -
James Smartfedd3b72011-02-16 12:39:24 -050013229 sizeof(struct lpfc_sli4_cfg_mhdr));
James Smart4f774512009-05-22 14:52:35 -040013230 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
13231 LPFC_MBOX_OPCODE_FCOE_RQ_DESTROY,
13232 length, LPFC_SLI4_MBX_EMBED);
13233 bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
13234 hrq->queue_id);
13235 mbox->vport = hrq->phba->pport;
13236 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
13237 rc = lpfc_sli_issue_mbox(hrq->phba, mbox, MBX_POLL);
13238 /* The IOCTL status is embedded in the mailbox subheader. */
13239 shdr = (union lpfc_sli4_cfg_shdr *)
13240 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
13241 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13242 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13243 if (shdr_status || shdr_add_status || rc) {
13244 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13245 "2509 RQ_DESTROY mailbox failed with "
13246 "status x%x add_status x%x, mbx status x%x\n",
13247 shdr_status, shdr_add_status, rc);
13248 if (rc != MBX_TIMEOUT)
13249 mempool_free(mbox, hrq->phba->mbox_mem_pool);
13250 return -ENXIO;
13251 }
13252 bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
13253 drq->queue_id);
13254 rc = lpfc_sli_issue_mbox(drq->phba, mbox, MBX_POLL);
13255 shdr = (union lpfc_sli4_cfg_shdr *)
13256 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
13257 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13258 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13259 if (shdr_status || shdr_add_status || rc) {
13260 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13261 "2510 RQ_DESTROY mailbox failed with "
13262 "status x%x add_status x%x, mbx status x%x\n",
13263 shdr_status, shdr_add_status, rc);
13264 status = -ENXIO;
13265 }
13266 list_del_init(&hrq->list);
13267 list_del_init(&drq->list);
James Smart8fa38512009-07-19 10:01:03 -040013268 mempool_free(mbox, hrq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040013269 return status;
13270}
13271
13272/**
13273 * lpfc_sli4_post_sgl - Post scatter gather list for an XRI to HBA
13274 * @phba: The virtual port for which this call being executed.
13275 * @pdma_phys_addr0: Physical address of the 1st SGL page.
13276 * @pdma_phys_addr1: Physical address of the 2nd SGL page.
13277 * @xritag: the xritag that ties this io to the SGL pages.
13278 *
13279 * This routine will post the sgl pages for the IO that has the xritag
13280 * that is in the iocbq structure. The xritag is assigned during iocbq
13281 * creation and persists for as long as the driver is loaded.
13282 * if the caller has fewer than 256 scatter gather segments to map then
13283 * pdma_phys_addr1 should be 0.
13284 * If the caller needs to map more than 256 scatter gather segment then
13285 * pdma_phys_addr1 should be a valid physical address.
13286 * physical address for SGLs must be 64 byte aligned.
13287 * If you are going to map 2 SGL's then the first one must have 256 entries
13288 * the second sgl can have between 1 and 256 entries.
13289 *
13290 * Return codes:
13291 * 0 - Success
13292 * -ENXIO, -ENOMEM - Failure
13293 **/
13294int
13295lpfc_sli4_post_sgl(struct lpfc_hba *phba,
13296 dma_addr_t pdma_phys_addr0,
13297 dma_addr_t pdma_phys_addr1,
13298 uint16_t xritag)
13299{
13300 struct lpfc_mbx_post_sgl_pages *post_sgl_pages;
13301 LPFC_MBOXQ_t *mbox;
13302 int rc;
13303 uint32_t shdr_status, shdr_add_status;
James Smart6d368e52011-05-24 11:44:12 -040013304 uint32_t mbox_tmo;
James Smart4f774512009-05-22 14:52:35 -040013305 union lpfc_sli4_cfg_shdr *shdr;
13306
13307 if (xritag == NO_XRI) {
13308 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13309 "0364 Invalid param:\n");
13310 return -EINVAL;
13311 }
13312
13313 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
13314 if (!mbox)
13315 return -ENOMEM;
13316
13317 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
13318 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES,
13319 sizeof(struct lpfc_mbx_post_sgl_pages) -
James Smartfedd3b72011-02-16 12:39:24 -050013320 sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
James Smart4f774512009-05-22 14:52:35 -040013321
13322 post_sgl_pages = (struct lpfc_mbx_post_sgl_pages *)
13323 &mbox->u.mqe.un.post_sgl_pages;
13324 bf_set(lpfc_post_sgl_pages_xri, post_sgl_pages, xritag);
13325 bf_set(lpfc_post_sgl_pages_xricnt, post_sgl_pages, 1);
13326
13327 post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_lo =
13328 cpu_to_le32(putPaddrLow(pdma_phys_addr0));
13329 post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_hi =
13330 cpu_to_le32(putPaddrHigh(pdma_phys_addr0));
13331
13332 post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_lo =
13333 cpu_to_le32(putPaddrLow(pdma_phys_addr1));
13334 post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_hi =
13335 cpu_to_le32(putPaddrHigh(pdma_phys_addr1));
13336 if (!phba->sli4_hba.intr_enable)
13337 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
James Smart6d368e52011-05-24 11:44:12 -040013338 else {
James Smarta183a152011-10-10 21:32:43 -040013339 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart6d368e52011-05-24 11:44:12 -040013340 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
13341 }
James Smart4f774512009-05-22 14:52:35 -040013342 /* The IOCTL status is embedded in the mailbox subheader. */
13343 shdr = (union lpfc_sli4_cfg_shdr *) &post_sgl_pages->header.cfg_shdr;
13344 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13345 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13346 if (rc != MBX_TIMEOUT)
13347 mempool_free(mbox, phba->mbox_mem_pool);
13348 if (shdr_status || shdr_add_status || rc) {
13349 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13350 "2511 POST_SGL mailbox failed with "
13351 "status x%x add_status x%x, mbx status x%x\n",
13352 shdr_status, shdr_add_status, rc);
13353 rc = -ENXIO;
13354 }
13355 return 0;
13356}
James Smart4f774512009-05-22 14:52:35 -040013357
13358/**
James Smart88a2cfb2011-07-22 18:36:33 -040013359 * lpfc_sli4_alloc_xri - Get an available rpi in the device's range
James Smart6d368e52011-05-24 11:44:12 -040013360 * @phba: pointer to lpfc hba data structure.
13361 *
13362 * This routine is invoked to post rpi header templates to the
James Smart88a2cfb2011-07-22 18:36:33 -040013363 * HBA consistent with the SLI-4 interface spec. This routine
13364 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
13365 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
James Smart6d368e52011-05-24 11:44:12 -040013366 *
James Smart88a2cfb2011-07-22 18:36:33 -040013367 * Returns
13368 * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
13369 * LPFC_RPI_ALLOC_ERROR if no rpis are available.
13370 **/
James Smart6d368e52011-05-24 11:44:12 -040013371uint16_t
13372lpfc_sli4_alloc_xri(struct lpfc_hba *phba)
13373{
13374 unsigned long xri;
13375
13376 /*
13377 * Fetch the next logical xri. Because this index is logical,
13378 * the driver starts at 0 each time.
13379 */
13380 spin_lock_irq(&phba->hbalock);
13381 xri = find_next_zero_bit(phba->sli4_hba.xri_bmask,
13382 phba->sli4_hba.max_cfg_param.max_xri, 0);
13383 if (xri >= phba->sli4_hba.max_cfg_param.max_xri) {
13384 spin_unlock_irq(&phba->hbalock);
13385 return NO_XRI;
13386 } else {
13387 set_bit(xri, phba->sli4_hba.xri_bmask);
13388 phba->sli4_hba.max_cfg_param.xri_used++;
James Smart6d368e52011-05-24 11:44:12 -040013389 }
James Smart6d368e52011-05-24 11:44:12 -040013390 spin_unlock_irq(&phba->hbalock);
13391 return xri;
13392}
13393
13394/**
13395 * lpfc_sli4_free_xri - Release an xri for reuse.
13396 * @phba: pointer to lpfc hba data structure.
13397 *
13398 * This routine is invoked to release an xri to the pool of
13399 * available rpis maintained by the driver.
13400 **/
13401void
13402__lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
13403{
13404 if (test_and_clear_bit(xri, phba->sli4_hba.xri_bmask)) {
James Smart6d368e52011-05-24 11:44:12 -040013405 phba->sli4_hba.max_cfg_param.xri_used--;
13406 }
13407}
13408
13409/**
13410 * lpfc_sli4_free_xri - Release an xri for reuse.
13411 * @phba: pointer to lpfc hba data structure.
13412 *
13413 * This routine is invoked to release an xri to the pool of
13414 * available rpis maintained by the driver.
13415 **/
13416void
13417lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
13418{
13419 spin_lock_irq(&phba->hbalock);
13420 __lpfc_sli4_free_xri(phba, xri);
13421 spin_unlock_irq(&phba->hbalock);
13422}
13423
13424/**
James Smart4f774512009-05-22 14:52:35 -040013425 * lpfc_sli4_next_xritag - Get an xritag for the io
13426 * @phba: Pointer to HBA context object.
13427 *
13428 * This function gets an xritag for the iocb. If there is no unused xritag
13429 * it will return 0xffff.
13430 * The function returns the allocated xritag if successful, else returns zero.
13431 * Zero is not a valid xritag.
13432 * The caller is not required to hold any lock.
13433 **/
13434uint16_t
13435lpfc_sli4_next_xritag(struct lpfc_hba *phba)
13436{
James Smart6d368e52011-05-24 11:44:12 -040013437 uint16_t xri_index;
James Smart4f774512009-05-22 14:52:35 -040013438
James Smart6d368e52011-05-24 11:44:12 -040013439 xri_index = lpfc_sli4_alloc_xri(phba);
James Smart81378052012-05-09 21:17:37 -040013440 if (xri_index == NO_XRI)
13441 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13442 "2004 Failed to allocate XRI.last XRITAG is %d"
13443 " Max XRI is %d, Used XRI is %d\n",
13444 xri_index,
13445 phba->sli4_hba.max_cfg_param.max_xri,
13446 phba->sli4_hba.max_cfg_param.xri_used);
13447 return xri_index;
James Smart4f774512009-05-22 14:52:35 -040013448}
13449
13450/**
James Smart6d368e52011-05-24 11:44:12 -040013451 * lpfc_sli4_post_els_sgl_list - post a block of ELS sgls to the port.
James Smart4f774512009-05-22 14:52:35 -040013452 * @phba: pointer to lpfc hba data structure.
James Smart8a9d2e82012-05-09 21:16:12 -040013453 * @post_sgl_list: pointer to els sgl entry list.
13454 * @count: number of els sgl entries on the list.
James Smart4f774512009-05-22 14:52:35 -040013455 *
13456 * This routine is invoked to post a block of driver's sgl pages to the
13457 * HBA using non-embedded mailbox command. No Lock is held. This routine
13458 * is only called when the driver is loading and after all IO has been
13459 * stopped.
13460 **/
James Smart8a9d2e82012-05-09 21:16:12 -040013461static int
13462lpfc_sli4_post_els_sgl_list(struct lpfc_hba *phba,
13463 struct list_head *post_sgl_list,
13464 int post_cnt)
James Smart4f774512009-05-22 14:52:35 -040013465{
James Smart8a9d2e82012-05-09 21:16:12 -040013466 struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
James Smart4f774512009-05-22 14:52:35 -040013467 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
13468 struct sgl_page_pairs *sgl_pg_pairs;
13469 void *viraddr;
13470 LPFC_MBOXQ_t *mbox;
13471 uint32_t reqlen, alloclen, pg_pairs;
13472 uint32_t mbox_tmo;
James Smart8a9d2e82012-05-09 21:16:12 -040013473 uint16_t xritag_start = 0;
13474 int rc = 0;
James Smart4f774512009-05-22 14:52:35 -040013475 uint32_t shdr_status, shdr_add_status;
13476 union lpfc_sli4_cfg_shdr *shdr;
13477
James Smart8a9d2e82012-05-09 21:16:12 -040013478 reqlen = phba->sli4_hba.els_xri_cnt * sizeof(struct sgl_page_pairs) +
James Smart4f774512009-05-22 14:52:35 -040013479 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
James Smart49198b32010-04-06 15:04:33 -040013480 if (reqlen > SLI4_PAGE_SIZE) {
James Smart4f774512009-05-22 14:52:35 -040013481 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
13482 "2559 Block sgl registration required DMA "
13483 "size (%d) great than a page\n", reqlen);
13484 return -ENOMEM;
13485 }
13486 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
James Smart6d368e52011-05-24 11:44:12 -040013487 if (!mbox)
James Smart4f774512009-05-22 14:52:35 -040013488 return -ENOMEM;
James Smart4f774512009-05-22 14:52:35 -040013489
13490 /* Allocate DMA memory and set up the non-embedded mailbox command */
13491 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
13492 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
13493 LPFC_SLI4_MBX_NEMBED);
13494
13495 if (alloclen < reqlen) {
13496 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13497 "0285 Allocated DMA memory size (%d) is "
13498 "less than the requested DMA memory "
13499 "size (%d)\n", alloclen, reqlen);
13500 lpfc_sli4_mbox_cmd_free(phba, mbox);
13501 return -ENOMEM;
13502 }
James Smart4f774512009-05-22 14:52:35 -040013503 /* Set up the SGL pages in the non-embedded DMA pages */
James Smart6d368e52011-05-24 11:44:12 -040013504 viraddr = mbox->sge_array->addr[0];
James Smart4f774512009-05-22 14:52:35 -040013505 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
13506 sgl_pg_pairs = &sgl->sgl_pg_pairs;
13507
James Smart8a9d2e82012-05-09 21:16:12 -040013508 pg_pairs = 0;
13509 list_for_each_entry_safe(sglq_entry, sglq_next, post_sgl_list, list) {
James Smart4f774512009-05-22 14:52:35 -040013510 /* Set up the sge entry */
13511 sgl_pg_pairs->sgl_pg0_addr_lo =
13512 cpu_to_le32(putPaddrLow(sglq_entry->phys));
13513 sgl_pg_pairs->sgl_pg0_addr_hi =
13514 cpu_to_le32(putPaddrHigh(sglq_entry->phys));
13515 sgl_pg_pairs->sgl_pg1_addr_lo =
13516 cpu_to_le32(putPaddrLow(0));
13517 sgl_pg_pairs->sgl_pg1_addr_hi =
13518 cpu_to_le32(putPaddrHigh(0));
James Smart6d368e52011-05-24 11:44:12 -040013519
James Smart4f774512009-05-22 14:52:35 -040013520 /* Keep the first xritag on the list */
13521 if (pg_pairs == 0)
13522 xritag_start = sglq_entry->sli4_xritag;
13523 sgl_pg_pairs++;
James Smart8a9d2e82012-05-09 21:16:12 -040013524 pg_pairs++;
James Smart4f774512009-05-22 14:52:35 -040013525 }
James Smart6d368e52011-05-24 11:44:12 -040013526
13527 /* Complete initialization and perform endian conversion. */
James Smart4f774512009-05-22 14:52:35 -040013528 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
James Smart8a9d2e82012-05-09 21:16:12 -040013529 bf_set(lpfc_post_sgl_pages_xricnt, sgl, phba->sli4_hba.els_xri_cnt);
James Smart4f774512009-05-22 14:52:35 -040013530 sgl->word0 = cpu_to_le32(sgl->word0);
James Smart4f774512009-05-22 14:52:35 -040013531 if (!phba->sli4_hba.intr_enable)
13532 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
13533 else {
James Smarta183a152011-10-10 21:32:43 -040013534 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart4f774512009-05-22 14:52:35 -040013535 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
13536 }
13537 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
13538 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13539 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13540 if (rc != MBX_TIMEOUT)
13541 lpfc_sli4_mbox_cmd_free(phba, mbox);
13542 if (shdr_status || shdr_add_status || rc) {
13543 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13544 "2513 POST_SGL_BLOCK mailbox command failed "
13545 "status x%x add_status x%x mbx status x%x\n",
13546 shdr_status, shdr_add_status, rc);
13547 rc = -ENXIO;
13548 }
13549 return rc;
13550}
13551
13552/**
13553 * lpfc_sli4_post_scsi_sgl_block - post a block of scsi sgl list to firmware
13554 * @phba: pointer to lpfc hba data structure.
13555 * @sblist: pointer to scsi buffer list.
13556 * @count: number of scsi buffers on the list.
13557 *
13558 * This routine is invoked to post a block of @count scsi sgl pages from a
13559 * SCSI buffer list @sblist to the HBA using non-embedded mailbox command.
13560 * No Lock is held.
13561 *
13562 **/
13563int
James Smart8a9d2e82012-05-09 21:16:12 -040013564lpfc_sli4_post_scsi_sgl_block(struct lpfc_hba *phba,
13565 struct list_head *sblist,
13566 int count)
James Smart4f774512009-05-22 14:52:35 -040013567{
13568 struct lpfc_scsi_buf *psb;
13569 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
13570 struct sgl_page_pairs *sgl_pg_pairs;
13571 void *viraddr;
13572 LPFC_MBOXQ_t *mbox;
13573 uint32_t reqlen, alloclen, pg_pairs;
13574 uint32_t mbox_tmo;
13575 uint16_t xritag_start = 0;
13576 int rc = 0;
13577 uint32_t shdr_status, shdr_add_status;
13578 dma_addr_t pdma_phys_bpl1;
13579 union lpfc_sli4_cfg_shdr *shdr;
13580
13581 /* Calculate the requested length of the dma memory */
James Smart8a9d2e82012-05-09 21:16:12 -040013582 reqlen = count * sizeof(struct sgl_page_pairs) +
James Smart4f774512009-05-22 14:52:35 -040013583 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
James Smart49198b32010-04-06 15:04:33 -040013584 if (reqlen > SLI4_PAGE_SIZE) {
James Smart4f774512009-05-22 14:52:35 -040013585 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
13586 "0217 Block sgl registration required DMA "
13587 "size (%d) great than a page\n", reqlen);
13588 return -ENOMEM;
13589 }
13590 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
13591 if (!mbox) {
13592 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13593 "0283 Failed to allocate mbox cmd memory\n");
13594 return -ENOMEM;
13595 }
13596
13597 /* Allocate DMA memory and set up the non-embedded mailbox command */
13598 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
13599 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
13600 LPFC_SLI4_MBX_NEMBED);
13601
13602 if (alloclen < reqlen) {
13603 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13604 "2561 Allocated DMA memory size (%d) is "
13605 "less than the requested DMA memory "
13606 "size (%d)\n", alloclen, reqlen);
13607 lpfc_sli4_mbox_cmd_free(phba, mbox);
13608 return -ENOMEM;
13609 }
James Smart6d368e52011-05-24 11:44:12 -040013610
James Smart4f774512009-05-22 14:52:35 -040013611 /* Get the first SGE entry from the non-embedded DMA memory */
James Smart4f774512009-05-22 14:52:35 -040013612 viraddr = mbox->sge_array->addr[0];
13613
13614 /* Set up the SGL pages in the non-embedded DMA pages */
13615 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
13616 sgl_pg_pairs = &sgl->sgl_pg_pairs;
13617
13618 pg_pairs = 0;
13619 list_for_each_entry(psb, sblist, list) {
13620 /* Set up the sge entry */
13621 sgl_pg_pairs->sgl_pg0_addr_lo =
13622 cpu_to_le32(putPaddrLow(psb->dma_phys_bpl));
13623 sgl_pg_pairs->sgl_pg0_addr_hi =
13624 cpu_to_le32(putPaddrHigh(psb->dma_phys_bpl));
13625 if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
13626 pdma_phys_bpl1 = psb->dma_phys_bpl + SGL_PAGE_SIZE;
13627 else
13628 pdma_phys_bpl1 = 0;
13629 sgl_pg_pairs->sgl_pg1_addr_lo =
13630 cpu_to_le32(putPaddrLow(pdma_phys_bpl1));
13631 sgl_pg_pairs->sgl_pg1_addr_hi =
13632 cpu_to_le32(putPaddrHigh(pdma_phys_bpl1));
13633 /* Keep the first xritag on the list */
13634 if (pg_pairs == 0)
13635 xritag_start = psb->cur_iocbq.sli4_xritag;
13636 sgl_pg_pairs++;
13637 pg_pairs++;
13638 }
13639 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
13640 bf_set(lpfc_post_sgl_pages_xricnt, sgl, pg_pairs);
13641 /* Perform endian conversion if necessary */
13642 sgl->word0 = cpu_to_le32(sgl->word0);
13643
13644 if (!phba->sli4_hba.intr_enable)
13645 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
13646 else {
James Smarta183a152011-10-10 21:32:43 -040013647 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart4f774512009-05-22 14:52:35 -040013648 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
13649 }
13650 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
13651 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13652 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13653 if (rc != MBX_TIMEOUT)
13654 lpfc_sli4_mbox_cmd_free(phba, mbox);
13655 if (shdr_status || shdr_add_status || rc) {
13656 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13657 "2564 POST_SGL_BLOCK mailbox command failed "
13658 "status x%x add_status x%x mbx status x%x\n",
13659 shdr_status, shdr_add_status, rc);
13660 rc = -ENXIO;
13661 }
13662 return rc;
13663}
13664
13665/**
13666 * lpfc_fc_frame_check - Check that this frame is a valid frame to handle
13667 * @phba: pointer to lpfc_hba struct that the frame was received on
13668 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
13669 *
13670 * This function checks the fields in the @fc_hdr to see if the FC frame is a
13671 * valid type of frame that the LPFC driver will handle. This function will
13672 * return a zero if the frame is a valid frame or a non zero value when the
13673 * frame does not pass the check.
13674 **/
13675static int
13676lpfc_fc_frame_check(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr)
13677{
Tomas Henzl474ffb72010-12-22 16:52:40 +010013678 /* make rctl_names static to save stack space */
13679 static char *rctl_names[] = FC_RCTL_NAMES_INIT;
James Smart4f774512009-05-22 14:52:35 -040013680 char *type_names[] = FC_TYPE_NAMES_INIT;
13681 struct fc_vft_header *fc_vft_hdr;
James Smart546fc852011-03-11 16:06:29 -050013682 uint32_t *header = (uint32_t *) fc_hdr;
James Smart4f774512009-05-22 14:52:35 -040013683
13684 switch (fc_hdr->fh_r_ctl) {
13685 case FC_RCTL_DD_UNCAT: /* uncategorized information */
13686 case FC_RCTL_DD_SOL_DATA: /* solicited data */
13687 case FC_RCTL_DD_UNSOL_CTL: /* unsolicited control */
13688 case FC_RCTL_DD_SOL_CTL: /* solicited control or reply */
13689 case FC_RCTL_DD_UNSOL_DATA: /* unsolicited data */
13690 case FC_RCTL_DD_DATA_DESC: /* data descriptor */
13691 case FC_RCTL_DD_UNSOL_CMD: /* unsolicited command */
13692 case FC_RCTL_DD_CMD_STATUS: /* command status */
13693 case FC_RCTL_ELS_REQ: /* extended link services request */
13694 case FC_RCTL_ELS_REP: /* extended link services reply */
13695 case FC_RCTL_ELS4_REQ: /* FC-4 ELS request */
13696 case FC_RCTL_ELS4_REP: /* FC-4 ELS reply */
13697 case FC_RCTL_BA_NOP: /* basic link service NOP */
13698 case FC_RCTL_BA_ABTS: /* basic link service abort */
13699 case FC_RCTL_BA_RMC: /* remove connection */
13700 case FC_RCTL_BA_ACC: /* basic accept */
13701 case FC_RCTL_BA_RJT: /* basic reject */
13702 case FC_RCTL_BA_PRMT:
13703 case FC_RCTL_ACK_1: /* acknowledge_1 */
13704 case FC_RCTL_ACK_0: /* acknowledge_0 */
13705 case FC_RCTL_P_RJT: /* port reject */
13706 case FC_RCTL_F_RJT: /* fabric reject */
13707 case FC_RCTL_P_BSY: /* port busy */
13708 case FC_RCTL_F_BSY: /* fabric busy to data frame */
13709 case FC_RCTL_F_BSYL: /* fabric busy to link control frame */
13710 case FC_RCTL_LCR: /* link credit reset */
13711 case FC_RCTL_END: /* end */
13712 break;
13713 case FC_RCTL_VFTH: /* Virtual Fabric tagging Header */
13714 fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
13715 fc_hdr = &((struct fc_frame_header *)fc_vft_hdr)[1];
13716 return lpfc_fc_frame_check(phba, fc_hdr);
13717 default:
13718 goto drop;
13719 }
13720 switch (fc_hdr->fh_type) {
13721 case FC_TYPE_BLS:
13722 case FC_TYPE_ELS:
13723 case FC_TYPE_FCP:
13724 case FC_TYPE_CT:
13725 break;
13726 case FC_TYPE_IP:
13727 case FC_TYPE_ILS:
13728 default:
13729 goto drop;
13730 }
James Smart546fc852011-03-11 16:06:29 -050013731
James Smart4f774512009-05-22 14:52:35 -040013732 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
James Smart546fc852011-03-11 16:06:29 -050013733 "2538 Received frame rctl:%s type:%s "
13734 "Frame Data:%08x %08x %08x %08x %08x %08x\n",
James Smart4f774512009-05-22 14:52:35 -040013735 rctl_names[fc_hdr->fh_r_ctl],
James Smart546fc852011-03-11 16:06:29 -050013736 type_names[fc_hdr->fh_type],
13737 be32_to_cpu(header[0]), be32_to_cpu(header[1]),
13738 be32_to_cpu(header[2]), be32_to_cpu(header[3]),
13739 be32_to_cpu(header[4]), be32_to_cpu(header[5]));
James Smart4f774512009-05-22 14:52:35 -040013740 return 0;
13741drop:
13742 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
13743 "2539 Dropped frame rctl:%s type:%s\n",
13744 rctl_names[fc_hdr->fh_r_ctl],
13745 type_names[fc_hdr->fh_type]);
13746 return 1;
13747}
13748
13749/**
13750 * lpfc_fc_hdr_get_vfi - Get the VFI from an FC frame
13751 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
13752 *
13753 * This function processes the FC header to retrieve the VFI from the VF
13754 * header, if one exists. This function will return the VFI if one exists
13755 * or 0 if no VSAN Header exists.
13756 **/
13757static uint32_t
13758lpfc_fc_hdr_get_vfi(struct fc_frame_header *fc_hdr)
13759{
13760 struct fc_vft_header *fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
13761
13762 if (fc_hdr->fh_r_ctl != FC_RCTL_VFTH)
13763 return 0;
13764 return bf_get(fc_vft_hdr_vf_id, fc_vft_hdr);
13765}
13766
13767/**
13768 * lpfc_fc_frame_to_vport - Finds the vport that a frame is destined to
13769 * @phba: Pointer to the HBA structure to search for the vport on
13770 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
13771 * @fcfi: The FC Fabric ID that the frame came from
13772 *
13773 * This function searches the @phba for a vport that matches the content of the
13774 * @fc_hdr passed in and the @fcfi. This function uses the @fc_hdr to fetch the
13775 * VFI, if the Virtual Fabric Tagging Header exists, and the DID. This function
13776 * returns the matching vport pointer or NULL if unable to match frame to a
13777 * vport.
13778 **/
13779static struct lpfc_vport *
13780lpfc_fc_frame_to_vport(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr,
13781 uint16_t fcfi)
13782{
13783 struct lpfc_vport **vports;
13784 struct lpfc_vport *vport = NULL;
13785 int i;
13786 uint32_t did = (fc_hdr->fh_d_id[0] << 16 |
13787 fc_hdr->fh_d_id[1] << 8 |
13788 fc_hdr->fh_d_id[2]);
James Smart939723a2012-05-09 21:19:03 -040013789
James Smartbf086112011-08-21 21:48:13 -040013790 if (did == Fabric_DID)
13791 return phba->pport;
James Smart939723a2012-05-09 21:19:03 -040013792 if ((phba->pport->fc_flag & FC_PT2PT) &&
13793 !(phba->link_state == LPFC_HBA_READY))
13794 return phba->pport;
13795
James Smart4f774512009-05-22 14:52:35 -040013796 vports = lpfc_create_vport_work_array(phba);
13797 if (vports != NULL)
13798 for (i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
13799 if (phba->fcf.fcfi == fcfi &&
13800 vports[i]->vfi == lpfc_fc_hdr_get_vfi(fc_hdr) &&
13801 vports[i]->fc_myDID == did) {
13802 vport = vports[i];
13803 break;
13804 }
13805 }
13806 lpfc_destroy_vport_work_array(phba, vports);
13807 return vport;
13808}
13809
13810/**
James Smart45ed1192009-10-02 15:17:02 -040013811 * lpfc_update_rcv_time_stamp - Update vport's rcv seq time stamp
13812 * @vport: The vport to work on.
13813 *
13814 * This function updates the receive sequence time stamp for this vport. The
13815 * receive sequence time stamp indicates the time that the last frame of the
13816 * the sequence that has been idle for the longest amount of time was received.
13817 * the driver uses this time stamp to indicate if any received sequences have
13818 * timed out.
13819 **/
13820void
13821lpfc_update_rcv_time_stamp(struct lpfc_vport *vport)
13822{
13823 struct lpfc_dmabuf *h_buf;
13824 struct hbq_dmabuf *dmabuf = NULL;
13825
13826 /* get the oldest sequence on the rcv list */
13827 h_buf = list_get_first(&vport->rcv_buffer_list,
13828 struct lpfc_dmabuf, list);
13829 if (!h_buf)
13830 return;
13831 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
13832 vport->rcv_buffer_time_stamp = dmabuf->time_stamp;
13833}
13834
13835/**
13836 * lpfc_cleanup_rcv_buffers - Cleans up all outstanding receive sequences.
13837 * @vport: The vport that the received sequences were sent to.
13838 *
13839 * This function cleans up all outstanding received sequences. This is called
13840 * by the driver when a link event or user action invalidates all the received
13841 * sequences.
13842 **/
13843void
13844lpfc_cleanup_rcv_buffers(struct lpfc_vport *vport)
13845{
13846 struct lpfc_dmabuf *h_buf, *hnext;
13847 struct lpfc_dmabuf *d_buf, *dnext;
13848 struct hbq_dmabuf *dmabuf = NULL;
13849
13850 /* start with the oldest sequence on the rcv list */
13851 list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
13852 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
13853 list_del_init(&dmabuf->hbuf.list);
13854 list_for_each_entry_safe(d_buf, dnext,
13855 &dmabuf->dbuf.list, list) {
13856 list_del_init(&d_buf->list);
13857 lpfc_in_buf_free(vport->phba, d_buf);
13858 }
13859 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
13860 }
13861}
13862
13863/**
13864 * lpfc_rcv_seq_check_edtov - Cleans up timed out receive sequences.
13865 * @vport: The vport that the received sequences were sent to.
13866 *
13867 * This function determines whether any received sequences have timed out by
13868 * first checking the vport's rcv_buffer_time_stamp. If this time_stamp
13869 * indicates that there is at least one timed out sequence this routine will
13870 * go through the received sequences one at a time from most inactive to most
13871 * active to determine which ones need to be cleaned up. Once it has determined
13872 * that a sequence needs to be cleaned up it will simply free up the resources
13873 * without sending an abort.
13874 **/
13875void
13876lpfc_rcv_seq_check_edtov(struct lpfc_vport *vport)
13877{
13878 struct lpfc_dmabuf *h_buf, *hnext;
13879 struct lpfc_dmabuf *d_buf, *dnext;
13880 struct hbq_dmabuf *dmabuf = NULL;
13881 unsigned long timeout;
13882 int abort_count = 0;
13883
13884 timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
13885 vport->rcv_buffer_time_stamp);
13886 if (list_empty(&vport->rcv_buffer_list) ||
13887 time_before(jiffies, timeout))
13888 return;
13889 /* start with the oldest sequence on the rcv list */
13890 list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
13891 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
13892 timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
13893 dmabuf->time_stamp);
13894 if (time_before(jiffies, timeout))
13895 break;
13896 abort_count++;
13897 list_del_init(&dmabuf->hbuf.list);
13898 list_for_each_entry_safe(d_buf, dnext,
13899 &dmabuf->dbuf.list, list) {
13900 list_del_init(&d_buf->list);
13901 lpfc_in_buf_free(vport->phba, d_buf);
13902 }
13903 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
13904 }
13905 if (abort_count)
13906 lpfc_update_rcv_time_stamp(vport);
13907}
13908
13909/**
James Smart4f774512009-05-22 14:52:35 -040013910 * lpfc_fc_frame_add - Adds a frame to the vport's list of received sequences
13911 * @dmabuf: pointer to a dmabuf that describes the hdr and data of the FC frame
13912 *
13913 * This function searches through the existing incomplete sequences that have
13914 * been sent to this @vport. If the frame matches one of the incomplete
13915 * sequences then the dbuf in the @dmabuf is added to the list of frames that
13916 * make up that sequence. If no sequence is found that matches this frame then
13917 * the function will add the hbuf in the @dmabuf to the @vport's rcv_buffer_list
13918 * This function returns a pointer to the first dmabuf in the sequence list that
13919 * the frame was linked to.
13920 **/
13921static struct hbq_dmabuf *
13922lpfc_fc_frame_add(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
13923{
13924 struct fc_frame_header *new_hdr;
13925 struct fc_frame_header *temp_hdr;
13926 struct lpfc_dmabuf *d_buf;
13927 struct lpfc_dmabuf *h_buf;
13928 struct hbq_dmabuf *seq_dmabuf = NULL;
13929 struct hbq_dmabuf *temp_dmabuf = NULL;
13930
James Smart4d9ab992009-10-02 15:16:39 -040013931 INIT_LIST_HEAD(&dmabuf->dbuf.list);
James Smart45ed1192009-10-02 15:17:02 -040013932 dmabuf->time_stamp = jiffies;
James Smart4f774512009-05-22 14:52:35 -040013933 new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
13934 /* Use the hdr_buf to find the sequence that this frame belongs to */
13935 list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
13936 temp_hdr = (struct fc_frame_header *)h_buf->virt;
13937 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
13938 (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
13939 (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
13940 continue;
13941 /* found a pending sequence that matches this frame */
13942 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
13943 break;
13944 }
13945 if (!seq_dmabuf) {
13946 /*
13947 * This indicates first frame received for this sequence.
13948 * Queue the buffer on the vport's rcv_buffer_list.
13949 */
13950 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
James Smart45ed1192009-10-02 15:17:02 -040013951 lpfc_update_rcv_time_stamp(vport);
James Smart4f774512009-05-22 14:52:35 -040013952 return dmabuf;
13953 }
13954 temp_hdr = seq_dmabuf->hbuf.virt;
James Smarteeead812009-12-21 17:01:23 -050013955 if (be16_to_cpu(new_hdr->fh_seq_cnt) <
13956 be16_to_cpu(temp_hdr->fh_seq_cnt)) {
James Smart4d9ab992009-10-02 15:16:39 -040013957 list_del_init(&seq_dmabuf->hbuf.list);
13958 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
13959 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
James Smart45ed1192009-10-02 15:17:02 -040013960 lpfc_update_rcv_time_stamp(vport);
James Smart4f774512009-05-22 14:52:35 -040013961 return dmabuf;
13962 }
James Smart45ed1192009-10-02 15:17:02 -040013963 /* move this sequence to the tail to indicate a young sequence */
13964 list_move_tail(&seq_dmabuf->hbuf.list, &vport->rcv_buffer_list);
13965 seq_dmabuf->time_stamp = jiffies;
13966 lpfc_update_rcv_time_stamp(vport);
James Smarteeead812009-12-21 17:01:23 -050013967 if (list_empty(&seq_dmabuf->dbuf.list)) {
13968 temp_hdr = dmabuf->hbuf.virt;
13969 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
13970 return seq_dmabuf;
13971 }
James Smart4f774512009-05-22 14:52:35 -040013972 /* find the correct place in the sequence to insert this frame */
13973 list_for_each_entry_reverse(d_buf, &seq_dmabuf->dbuf.list, list) {
13974 temp_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
13975 temp_hdr = (struct fc_frame_header *)temp_dmabuf->hbuf.virt;
13976 /*
13977 * If the frame's sequence count is greater than the frame on
13978 * the list then insert the frame right after this frame
13979 */
James Smarteeead812009-12-21 17:01:23 -050013980 if (be16_to_cpu(new_hdr->fh_seq_cnt) >
13981 be16_to_cpu(temp_hdr->fh_seq_cnt)) {
James Smart4f774512009-05-22 14:52:35 -040013982 list_add(&dmabuf->dbuf.list, &temp_dmabuf->dbuf.list);
13983 return seq_dmabuf;
13984 }
13985 }
13986 return NULL;
13987}
13988
13989/**
James Smart6669f9b2009-10-02 15:16:45 -040013990 * lpfc_sli4_abort_partial_seq - Abort partially assembled unsol sequence
13991 * @vport: pointer to a vitural port
13992 * @dmabuf: pointer to a dmabuf that describes the FC sequence
13993 *
13994 * This function tries to abort from the partially assembed sequence, described
13995 * by the information from basic abbort @dmabuf. It checks to see whether such
13996 * partially assembled sequence held by the driver. If so, it shall free up all
13997 * the frames from the partially assembled sequence.
13998 *
13999 * Return
14000 * true -- if there is matching partially assembled sequence present and all
14001 * the frames freed with the sequence;
14002 * false -- if there is no matching partially assembled sequence present so
14003 * nothing got aborted in the lower layer driver
14004 **/
14005static bool
14006lpfc_sli4_abort_partial_seq(struct lpfc_vport *vport,
14007 struct hbq_dmabuf *dmabuf)
14008{
14009 struct fc_frame_header *new_hdr;
14010 struct fc_frame_header *temp_hdr;
14011 struct lpfc_dmabuf *d_buf, *n_buf, *h_buf;
14012 struct hbq_dmabuf *seq_dmabuf = NULL;
14013
14014 /* Use the hdr_buf to find the sequence that matches this frame */
14015 INIT_LIST_HEAD(&dmabuf->dbuf.list);
14016 INIT_LIST_HEAD(&dmabuf->hbuf.list);
14017 new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
14018 list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
14019 temp_hdr = (struct fc_frame_header *)h_buf->virt;
14020 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
14021 (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
14022 (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
14023 continue;
14024 /* found a pending sequence that matches this frame */
14025 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
14026 break;
14027 }
14028
14029 /* Free up all the frames from the partially assembled sequence */
14030 if (seq_dmabuf) {
14031 list_for_each_entry_safe(d_buf, n_buf,
14032 &seq_dmabuf->dbuf.list, list) {
14033 list_del_init(&d_buf->list);
14034 lpfc_in_buf_free(vport->phba, d_buf);
14035 }
14036 return true;
14037 }
14038 return false;
14039}
14040
14041/**
James Smart546fc852011-03-11 16:06:29 -050014042 * lpfc_sli4_seq_abort_rsp_cmpl - BLS ABORT RSP seq abort iocb complete handler
James Smart6669f9b2009-10-02 15:16:45 -040014043 * @phba: Pointer to HBA context object.
14044 * @cmd_iocbq: pointer to the command iocbq structure.
14045 * @rsp_iocbq: pointer to the response iocbq structure.
14046 *
James Smart546fc852011-03-11 16:06:29 -050014047 * This function handles the sequence abort response iocb command complete
James Smart6669f9b2009-10-02 15:16:45 -040014048 * event. It properly releases the memory allocated to the sequence abort
14049 * accept iocb.
14050 **/
14051static void
James Smart546fc852011-03-11 16:06:29 -050014052lpfc_sli4_seq_abort_rsp_cmpl(struct lpfc_hba *phba,
James Smart6669f9b2009-10-02 15:16:45 -040014053 struct lpfc_iocbq *cmd_iocbq,
14054 struct lpfc_iocbq *rsp_iocbq)
14055{
14056 if (cmd_iocbq)
14057 lpfc_sli_release_iocbq(phba, cmd_iocbq);
James Smart6b5151f2012-01-18 16:24:06 -050014058
14059 /* Failure means BLS ABORT RSP did not get delivered to remote node*/
14060 if (rsp_iocbq && rsp_iocbq->iocb.ulpStatus)
14061 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14062 "3154 BLS ABORT RSP failed, data: x%x/x%x\n",
14063 rsp_iocbq->iocb.ulpStatus,
14064 rsp_iocbq->iocb.un.ulpWord[4]);
James Smart6669f9b2009-10-02 15:16:45 -040014065}
14066
14067/**
James Smart6d368e52011-05-24 11:44:12 -040014068 * lpfc_sli4_xri_inrange - check xri is in range of xris owned by driver.
14069 * @phba: Pointer to HBA context object.
14070 * @xri: xri id in transaction.
14071 *
14072 * This function validates the xri maps to the known range of XRIs allocated an
14073 * used by the driver.
14074 **/
James Smart7851fe22011-07-22 18:36:52 -040014075uint16_t
James Smart6d368e52011-05-24 11:44:12 -040014076lpfc_sli4_xri_inrange(struct lpfc_hba *phba,
14077 uint16_t xri)
14078{
14079 int i;
14080
14081 for (i = 0; i < phba->sli4_hba.max_cfg_param.max_xri; i++) {
14082 if (xri == phba->sli4_hba.xri_ids[i])
14083 return i;
14084 }
14085 return NO_XRI;
14086}
14087
James Smart6d368e52011-05-24 11:44:12 -040014088/**
James Smart546fc852011-03-11 16:06:29 -050014089 * lpfc_sli4_seq_abort_rsp - bls rsp to sequence abort
James Smart6669f9b2009-10-02 15:16:45 -040014090 * @phba: Pointer to HBA context object.
14091 * @fc_hdr: pointer to a FC frame header.
14092 *
James Smart546fc852011-03-11 16:06:29 -050014093 * This function sends a basic response to a previous unsol sequence abort
James Smart6669f9b2009-10-02 15:16:45 -040014094 * event after aborting the sequence handling.
14095 **/
14096static void
James Smart546fc852011-03-11 16:06:29 -050014097lpfc_sli4_seq_abort_rsp(struct lpfc_hba *phba,
James Smart6669f9b2009-10-02 15:16:45 -040014098 struct fc_frame_header *fc_hdr)
14099{
14100 struct lpfc_iocbq *ctiocb = NULL;
14101 struct lpfc_nodelist *ndlp;
James Smartee0f4fe2012-05-09 21:19:14 -040014102 uint16_t oxid, rxid, xri, lxri;
James Smart5ffc2662009-11-18 15:39:44 -050014103 uint32_t sid, fctl;
James Smart6669f9b2009-10-02 15:16:45 -040014104 IOCB_t *icmd;
James Smart546fc852011-03-11 16:06:29 -050014105 int rc;
James Smart6669f9b2009-10-02 15:16:45 -040014106
14107 if (!lpfc_is_link_up(phba))
14108 return;
14109
14110 sid = sli4_sid_from_fc_hdr(fc_hdr);
14111 oxid = be16_to_cpu(fc_hdr->fh_ox_id);
James Smart5ffc2662009-11-18 15:39:44 -050014112 rxid = be16_to_cpu(fc_hdr->fh_rx_id);
James Smart6669f9b2009-10-02 15:16:45 -040014113
14114 ndlp = lpfc_findnode_did(phba->pport, sid);
14115 if (!ndlp) {
14116 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
14117 "1268 Find ndlp returned NULL for oxid:x%x "
14118 "SID:x%x\n", oxid, sid);
14119 return;
14120 }
14121
James Smart546fc852011-03-11 16:06:29 -050014122 /* Allocate buffer for rsp iocb */
James Smart6669f9b2009-10-02 15:16:45 -040014123 ctiocb = lpfc_sli_get_iocbq(phba);
14124 if (!ctiocb)
14125 return;
14126
James Smart5ffc2662009-11-18 15:39:44 -050014127 /* Extract the F_CTL field from FC_HDR */
14128 fctl = sli4_fctl_from_fc_hdr(fc_hdr);
14129
James Smart6669f9b2009-10-02 15:16:45 -040014130 icmd = &ctiocb->iocb;
James Smart6669f9b2009-10-02 15:16:45 -040014131 icmd->un.xseq64.bdl.bdeSize = 0;
James Smart5ffc2662009-11-18 15:39:44 -050014132 icmd->un.xseq64.bdl.ulpIoTag32 = 0;
James Smart6669f9b2009-10-02 15:16:45 -040014133 icmd->un.xseq64.w5.hcsw.Dfctl = 0;
14134 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_ACC;
14135 icmd->un.xseq64.w5.hcsw.Type = FC_TYPE_BLS;
14136
14137 /* Fill in the rest of iocb fields */
14138 icmd->ulpCommand = CMD_XMIT_BLS_RSP64_CX;
14139 icmd->ulpBdeCount = 0;
14140 icmd->ulpLe = 1;
14141 icmd->ulpClass = CLASS3;
James Smart6d368e52011-05-24 11:44:12 -040014142 icmd->ulpContext = phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
James Smartbe858b62010-12-15 17:57:20 -050014143 ctiocb->context1 = ndlp;
James Smart6669f9b2009-10-02 15:16:45 -040014144
James Smart6669f9b2009-10-02 15:16:45 -040014145 ctiocb->iocb_cmpl = NULL;
14146 ctiocb->vport = phba->pport;
James Smart546fc852011-03-11 16:06:29 -050014147 ctiocb->iocb_cmpl = lpfc_sli4_seq_abort_rsp_cmpl;
James Smart6d368e52011-05-24 11:44:12 -040014148 ctiocb->sli4_lxritag = NO_XRI;
James Smart546fc852011-03-11 16:06:29 -050014149 ctiocb->sli4_xritag = NO_XRI;
14150
James Smartee0f4fe2012-05-09 21:19:14 -040014151 if (fctl & FC_FC_EX_CTX)
14152 /* Exchange responder sent the abort so we
14153 * own the oxid.
14154 */
14155 xri = oxid;
14156 else
14157 xri = rxid;
14158 lxri = lpfc_sli4_xri_inrange(phba, xri);
14159 if (lxri != NO_XRI)
14160 lpfc_set_rrq_active(phba, ndlp, lxri,
14161 (xri == oxid) ? rxid : oxid, 0);
James Smart546fc852011-03-11 16:06:29 -050014162 /* If the oxid maps to the FCP XRI range or if it is out of range,
14163 * send a BLS_RJT. The driver no longer has that exchange.
14164 * Override the IOCB for a BA_RJT.
14165 */
James Smartee0f4fe2012-05-09 21:19:14 -040014166 if (xri > (phba->sli4_hba.max_cfg_param.max_xri +
James Smart546fc852011-03-11 16:06:29 -050014167 phba->sli4_hba.max_cfg_param.xri_base) ||
James Smartee0f4fe2012-05-09 21:19:14 -040014168 xri > (lpfc_sli4_get_els_iocb_cnt(phba) +
James Smart546fc852011-03-11 16:06:29 -050014169 phba->sli4_hba.max_cfg_param.xri_base)) {
14170 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_RJT;
14171 bf_set(lpfc_vndr_code, &icmd->un.bls_rsp, 0);
14172 bf_set(lpfc_rsn_expln, &icmd->un.bls_rsp, FC_BA_RJT_INV_XID);
14173 bf_set(lpfc_rsn_code, &icmd->un.bls_rsp, FC_BA_RJT_UNABLE);
14174 }
James Smart6669f9b2009-10-02 15:16:45 -040014175
James Smart5ffc2662009-11-18 15:39:44 -050014176 if (fctl & FC_FC_EX_CTX) {
14177 /* ABTS sent by responder to CT exchange, construction
14178 * of BA_ACC will use OX_ID from ABTS for the XRI_TAG
14179 * field and RX_ID from ABTS for RX_ID field.
14180 */
James Smart546fc852011-03-11 16:06:29 -050014181 bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_RSP);
James Smart5ffc2662009-11-18 15:39:44 -050014182 } else {
14183 /* ABTS sent by initiator to CT exchange, construction
14184 * of BA_ACC will need to allocate a new XRI as for the
James Smartf09c3ac2012-03-01 22:33:29 -050014185 * XRI_TAG field.
James Smart5ffc2662009-11-18 15:39:44 -050014186 */
James Smart546fc852011-03-11 16:06:29 -050014187 bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_INT);
James Smart5ffc2662009-11-18 15:39:44 -050014188 }
James Smartf09c3ac2012-03-01 22:33:29 -050014189 bf_set(lpfc_abts_rxid, &icmd->un.bls_rsp, rxid);
James Smart546fc852011-03-11 16:06:29 -050014190 bf_set(lpfc_abts_oxid, &icmd->un.bls_rsp, oxid);
James Smart5ffc2662009-11-18 15:39:44 -050014191
James Smart546fc852011-03-11 16:06:29 -050014192 /* Xmit CT abts response on exchange <xid> */
James Smart6669f9b2009-10-02 15:16:45 -040014193 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
James Smart546fc852011-03-11 16:06:29 -050014194 "1200 Send BLS cmd x%x on oxid x%x Data: x%x\n",
14195 icmd->un.xseq64.w5.hcsw.Rctl, oxid, phba->link_state);
14196
14197 rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, ctiocb, 0);
14198 if (rc == IOCB_ERROR) {
14199 lpfc_printf_log(phba, KERN_ERR, LOG_ELS,
14200 "2925 Failed to issue CT ABTS RSP x%x on "
14201 "xri x%x, Data x%x\n",
14202 icmd->un.xseq64.w5.hcsw.Rctl, oxid,
14203 phba->link_state);
14204 lpfc_sli_release_iocbq(phba, ctiocb);
14205 }
James Smart6669f9b2009-10-02 15:16:45 -040014206}
14207
14208/**
14209 * lpfc_sli4_handle_unsol_abort - Handle sli-4 unsolicited abort event
14210 * @vport: Pointer to the vport on which this sequence was received
14211 * @dmabuf: pointer to a dmabuf that describes the FC sequence
14212 *
14213 * This function handles an SLI-4 unsolicited abort event. If the unsolicited
14214 * receive sequence is only partially assembed by the driver, it shall abort
14215 * the partially assembled frames for the sequence. Otherwise, if the
14216 * unsolicited receive sequence has been completely assembled and passed to
14217 * the Upper Layer Protocol (UPL), it then mark the per oxid status for the
14218 * unsolicited sequence has been aborted. After that, it will issue a basic
14219 * accept to accept the abort.
14220 **/
14221void
14222lpfc_sli4_handle_unsol_abort(struct lpfc_vport *vport,
14223 struct hbq_dmabuf *dmabuf)
14224{
14225 struct lpfc_hba *phba = vport->phba;
14226 struct fc_frame_header fc_hdr;
James Smart5ffc2662009-11-18 15:39:44 -050014227 uint32_t fctl;
James Smart6669f9b2009-10-02 15:16:45 -040014228 bool abts_par;
14229
James Smart6669f9b2009-10-02 15:16:45 -040014230 /* Make a copy of fc_hdr before the dmabuf being released */
14231 memcpy(&fc_hdr, dmabuf->hbuf.virt, sizeof(struct fc_frame_header));
James Smart5ffc2662009-11-18 15:39:44 -050014232 fctl = sli4_fctl_from_fc_hdr(&fc_hdr);
James Smart6669f9b2009-10-02 15:16:45 -040014233
James Smart5ffc2662009-11-18 15:39:44 -050014234 if (fctl & FC_FC_EX_CTX) {
14235 /*
14236 * ABTS sent by responder to exchange, just free the buffer
14237 */
James Smart6669f9b2009-10-02 15:16:45 -040014238 lpfc_in_buf_free(phba, &dmabuf->dbuf);
James Smart5ffc2662009-11-18 15:39:44 -050014239 } else {
14240 /*
14241 * ABTS sent by initiator to exchange, need to do cleanup
14242 */
14243 /* Try to abort partially assembled seq */
14244 abts_par = lpfc_sli4_abort_partial_seq(vport, dmabuf);
14245
14246 /* Send abort to ULP if partially seq abort failed */
14247 if (abts_par == false)
14248 lpfc_sli4_send_seq_to_ulp(vport, dmabuf);
14249 else
14250 lpfc_in_buf_free(phba, &dmabuf->dbuf);
14251 }
James Smart6669f9b2009-10-02 15:16:45 -040014252 /* Send basic accept (BA_ACC) to the abort requester */
James Smart546fc852011-03-11 16:06:29 -050014253 lpfc_sli4_seq_abort_rsp(phba, &fc_hdr);
James Smart6669f9b2009-10-02 15:16:45 -040014254}
14255
14256/**
James Smart4f774512009-05-22 14:52:35 -040014257 * lpfc_seq_complete - Indicates if a sequence is complete
14258 * @dmabuf: pointer to a dmabuf that describes the FC sequence
14259 *
14260 * This function checks the sequence, starting with the frame described by
14261 * @dmabuf, to see if all the frames associated with this sequence are present.
14262 * the frames associated with this sequence are linked to the @dmabuf using the
14263 * dbuf list. This function looks for two major things. 1) That the first frame
14264 * has a sequence count of zero. 2) There is a frame with last frame of sequence
14265 * set. 3) That there are no holes in the sequence count. The function will
14266 * return 1 when the sequence is complete, otherwise it will return 0.
14267 **/
14268static int
14269lpfc_seq_complete(struct hbq_dmabuf *dmabuf)
14270{
14271 struct fc_frame_header *hdr;
14272 struct lpfc_dmabuf *d_buf;
14273 struct hbq_dmabuf *seq_dmabuf;
14274 uint32_t fctl;
14275 int seq_count = 0;
14276
14277 hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
14278 /* make sure first fame of sequence has a sequence count of zero */
14279 if (hdr->fh_seq_cnt != seq_count)
14280 return 0;
14281 fctl = (hdr->fh_f_ctl[0] << 16 |
14282 hdr->fh_f_ctl[1] << 8 |
14283 hdr->fh_f_ctl[2]);
14284 /* If last frame of sequence we can return success. */
14285 if (fctl & FC_FC_END_SEQ)
14286 return 1;
14287 list_for_each_entry(d_buf, &dmabuf->dbuf.list, list) {
14288 seq_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
14289 hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
14290 /* If there is a hole in the sequence count then fail. */
James Smarteeead812009-12-21 17:01:23 -050014291 if (++seq_count != be16_to_cpu(hdr->fh_seq_cnt))
James Smart4f774512009-05-22 14:52:35 -040014292 return 0;
14293 fctl = (hdr->fh_f_ctl[0] << 16 |
14294 hdr->fh_f_ctl[1] << 8 |
14295 hdr->fh_f_ctl[2]);
14296 /* If last frame of sequence we can return success. */
14297 if (fctl & FC_FC_END_SEQ)
14298 return 1;
14299 }
14300 return 0;
14301}
14302
14303/**
14304 * lpfc_prep_seq - Prep sequence for ULP processing
14305 * @vport: Pointer to the vport on which this sequence was received
14306 * @dmabuf: pointer to a dmabuf that describes the FC sequence
14307 *
14308 * This function takes a sequence, described by a list of frames, and creates
14309 * a list of iocbq structures to describe the sequence. This iocbq list will be
14310 * used to issue to the generic unsolicited sequence handler. This routine
14311 * returns a pointer to the first iocbq in the list. If the function is unable
14312 * to allocate an iocbq then it throw out the received frames that were not
14313 * able to be described and return a pointer to the first iocbq. If unable to
14314 * allocate any iocbqs (including the first) this function will return NULL.
14315 **/
14316static struct lpfc_iocbq *
14317lpfc_prep_seq(struct lpfc_vport *vport, struct hbq_dmabuf *seq_dmabuf)
14318{
James Smart7851fe22011-07-22 18:36:52 -040014319 struct hbq_dmabuf *hbq_buf;
James Smart4f774512009-05-22 14:52:35 -040014320 struct lpfc_dmabuf *d_buf, *n_buf;
14321 struct lpfc_iocbq *first_iocbq, *iocbq;
14322 struct fc_frame_header *fc_hdr;
14323 uint32_t sid;
James Smart7851fe22011-07-22 18:36:52 -040014324 uint32_t len, tot_len;
James Smarteeead812009-12-21 17:01:23 -050014325 struct ulp_bde64 *pbde;
James Smart4f774512009-05-22 14:52:35 -040014326
14327 fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
14328 /* remove from receive buffer list */
14329 list_del_init(&seq_dmabuf->hbuf.list);
James Smart45ed1192009-10-02 15:17:02 -040014330 lpfc_update_rcv_time_stamp(vport);
James Smart4f774512009-05-22 14:52:35 -040014331 /* get the Remote Port's SID */
James Smart6669f9b2009-10-02 15:16:45 -040014332 sid = sli4_sid_from_fc_hdr(fc_hdr);
James Smart7851fe22011-07-22 18:36:52 -040014333 tot_len = 0;
James Smart4f774512009-05-22 14:52:35 -040014334 /* Get an iocbq struct to fill in. */
14335 first_iocbq = lpfc_sli_get_iocbq(vport->phba);
14336 if (first_iocbq) {
14337 /* Initialize the first IOCB. */
James Smart8fa38512009-07-19 10:01:03 -040014338 first_iocbq->iocb.unsli3.rcvsli3.acc_len = 0;
James Smart4f774512009-05-22 14:52:35 -040014339 first_iocbq->iocb.ulpStatus = IOSTAT_SUCCESS;
James Smart939723a2012-05-09 21:19:03 -040014340
14341 /* Check FC Header to see what TYPE of frame we are rcv'ing */
14342 if (sli4_type_from_fc_hdr(fc_hdr) == FC_TYPE_ELS) {
14343 first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_ELS64_CX;
14344 first_iocbq->iocb.un.rcvels.parmRo =
14345 sli4_did_from_fc_hdr(fc_hdr);
14346 first_iocbq->iocb.ulpPU = PARM_NPIV_DID;
14347 } else
14348 first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_SEQ64_CX;
James Smart7851fe22011-07-22 18:36:52 -040014349 first_iocbq->iocb.ulpContext = NO_XRI;
14350 first_iocbq->iocb.unsli3.rcvsli3.ox_id =
14351 be16_to_cpu(fc_hdr->fh_ox_id);
14352 /* iocbq is prepped for internal consumption. Physical vpi. */
14353 first_iocbq->iocb.unsli3.rcvsli3.vpi =
14354 vport->phba->vpi_ids[vport->vpi];
James Smart4f774512009-05-22 14:52:35 -040014355 /* put the first buffer into the first IOCBq */
14356 first_iocbq->context2 = &seq_dmabuf->dbuf;
14357 first_iocbq->context3 = NULL;
14358 first_iocbq->iocb.ulpBdeCount = 1;
14359 first_iocbq->iocb.un.cont64[0].tus.f.bdeSize =
14360 LPFC_DATA_BUF_SIZE;
14361 first_iocbq->iocb.un.rcvels.remoteID = sid;
James Smart7851fe22011-07-22 18:36:52 -040014362 tot_len = bf_get(lpfc_rcqe_length,
James Smart4d9ab992009-10-02 15:16:39 -040014363 &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
James Smart7851fe22011-07-22 18:36:52 -040014364 first_iocbq->iocb.unsli3.rcvsli3.acc_len = tot_len;
James Smart4f774512009-05-22 14:52:35 -040014365 }
14366 iocbq = first_iocbq;
14367 /*
14368 * Each IOCBq can have two Buffers assigned, so go through the list
14369 * of buffers for this sequence and save two buffers in each IOCBq
14370 */
14371 list_for_each_entry_safe(d_buf, n_buf, &seq_dmabuf->dbuf.list, list) {
14372 if (!iocbq) {
14373 lpfc_in_buf_free(vport->phba, d_buf);
14374 continue;
14375 }
14376 if (!iocbq->context3) {
14377 iocbq->context3 = d_buf;
14378 iocbq->iocb.ulpBdeCount++;
James Smarteeead812009-12-21 17:01:23 -050014379 pbde = (struct ulp_bde64 *)
14380 &iocbq->iocb.unsli3.sli3Words[4];
14381 pbde->tus.f.bdeSize = LPFC_DATA_BUF_SIZE;
James Smart7851fe22011-07-22 18:36:52 -040014382
14383 /* We need to get the size out of the right CQE */
14384 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
14385 len = bf_get(lpfc_rcqe_length,
14386 &hbq_buf->cq_event.cqe.rcqe_cmpl);
14387 iocbq->iocb.unsli3.rcvsli3.acc_len += len;
14388 tot_len += len;
James Smart4f774512009-05-22 14:52:35 -040014389 } else {
14390 iocbq = lpfc_sli_get_iocbq(vport->phba);
14391 if (!iocbq) {
14392 if (first_iocbq) {
14393 first_iocbq->iocb.ulpStatus =
14394 IOSTAT_FCP_RSP_ERROR;
14395 first_iocbq->iocb.un.ulpWord[4] =
14396 IOERR_NO_RESOURCES;
14397 }
14398 lpfc_in_buf_free(vport->phba, d_buf);
14399 continue;
14400 }
14401 iocbq->context2 = d_buf;
14402 iocbq->context3 = NULL;
14403 iocbq->iocb.ulpBdeCount = 1;
14404 iocbq->iocb.un.cont64[0].tus.f.bdeSize =
14405 LPFC_DATA_BUF_SIZE;
James Smart7851fe22011-07-22 18:36:52 -040014406
14407 /* We need to get the size out of the right CQE */
14408 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
14409 len = bf_get(lpfc_rcqe_length,
14410 &hbq_buf->cq_event.cqe.rcqe_cmpl);
14411 tot_len += len;
14412 iocbq->iocb.unsli3.rcvsli3.acc_len = tot_len;
14413
James Smart4f774512009-05-22 14:52:35 -040014414 iocbq->iocb.un.rcvels.remoteID = sid;
14415 list_add_tail(&iocbq->list, &first_iocbq->list);
14416 }
14417 }
14418 return first_iocbq;
14419}
14420
James Smart6669f9b2009-10-02 15:16:45 -040014421static void
14422lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *vport,
14423 struct hbq_dmabuf *seq_dmabuf)
14424{
14425 struct fc_frame_header *fc_hdr;
14426 struct lpfc_iocbq *iocbq, *curr_iocb, *next_iocb;
14427 struct lpfc_hba *phba = vport->phba;
14428
14429 fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
14430 iocbq = lpfc_prep_seq(vport, seq_dmabuf);
14431 if (!iocbq) {
14432 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14433 "2707 Ring %d handler: Failed to allocate "
14434 "iocb Rctl x%x Type x%x received\n",
14435 LPFC_ELS_RING,
14436 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
14437 return;
14438 }
14439 if (!lpfc_complete_unsol_iocb(phba,
14440 &phba->sli.ring[LPFC_ELS_RING],
14441 iocbq, fc_hdr->fh_r_ctl,
14442 fc_hdr->fh_type))
James Smart6d368e52011-05-24 11:44:12 -040014443 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart6669f9b2009-10-02 15:16:45 -040014444 "2540 Ring %d handler: unexpected Rctl "
14445 "x%x Type x%x received\n",
14446 LPFC_ELS_RING,
14447 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
14448
14449 /* Free iocb created in lpfc_prep_seq */
14450 list_for_each_entry_safe(curr_iocb, next_iocb,
14451 &iocbq->list, list) {
14452 list_del_init(&curr_iocb->list);
14453 lpfc_sli_release_iocbq(phba, curr_iocb);
14454 }
14455 lpfc_sli_release_iocbq(phba, iocbq);
14456}
14457
James Smart4f774512009-05-22 14:52:35 -040014458/**
14459 * lpfc_sli4_handle_received_buffer - Handle received buffers from firmware
14460 * @phba: Pointer to HBA context object.
14461 *
14462 * This function is called with no lock held. This function processes all
14463 * the received buffers and gives it to upper layers when a received buffer
14464 * indicates that it is the final frame in the sequence. The interrupt
14465 * service routine processes received buffers at interrupt contexts and adds
14466 * received dma buffers to the rb_pend_list queue and signals the worker thread.
14467 * Worker thread calls lpfc_sli4_handle_received_buffer, which will call the
14468 * appropriate receive function when the final frame in a sequence is received.
14469 **/
James Smart4d9ab992009-10-02 15:16:39 -040014470void
14471lpfc_sli4_handle_received_buffer(struct lpfc_hba *phba,
14472 struct hbq_dmabuf *dmabuf)
James Smart4f774512009-05-22 14:52:35 -040014473{
James Smart4d9ab992009-10-02 15:16:39 -040014474 struct hbq_dmabuf *seq_dmabuf;
James Smart4f774512009-05-22 14:52:35 -040014475 struct fc_frame_header *fc_hdr;
14476 struct lpfc_vport *vport;
14477 uint32_t fcfi;
James Smart939723a2012-05-09 21:19:03 -040014478 uint32_t did;
James Smart4f774512009-05-22 14:52:35 -040014479
James Smart4f774512009-05-22 14:52:35 -040014480 /* Process each received buffer */
James Smart4d9ab992009-10-02 15:16:39 -040014481 fc_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
14482 /* check to see if this a valid type of frame */
14483 if (lpfc_fc_frame_check(phba, fc_hdr)) {
14484 lpfc_in_buf_free(phba, &dmabuf->dbuf);
14485 return;
14486 }
James Smart7851fe22011-07-22 18:36:52 -040014487 if ((bf_get(lpfc_cqe_code,
14488 &dmabuf->cq_event.cqe.rcqe_cmpl) == CQE_CODE_RECEIVE_V1))
14489 fcfi = bf_get(lpfc_rcqe_fcf_id_v1,
14490 &dmabuf->cq_event.cqe.rcqe_cmpl);
14491 else
14492 fcfi = bf_get(lpfc_rcqe_fcf_id,
14493 &dmabuf->cq_event.cqe.rcqe_cmpl);
James Smart939723a2012-05-09 21:19:03 -040014494
James Smart4d9ab992009-10-02 15:16:39 -040014495 vport = lpfc_fc_frame_to_vport(phba, fc_hdr, fcfi);
James Smart939723a2012-05-09 21:19:03 -040014496 if (!vport) {
James Smart4d9ab992009-10-02 15:16:39 -040014497 /* throw out the frame */
14498 lpfc_in_buf_free(phba, &dmabuf->dbuf);
14499 return;
14500 }
James Smart939723a2012-05-09 21:19:03 -040014501
14502 /* d_id this frame is directed to */
14503 did = sli4_did_from_fc_hdr(fc_hdr);
14504
14505 /* vport is registered unless we rcv a FLOGI directed to Fabric_DID */
14506 if (!(vport->vpi_state & LPFC_VPI_REGISTERED) &&
14507 (did != Fabric_DID)) {
14508 /*
14509 * Throw out the frame if we are not pt2pt.
14510 * The pt2pt protocol allows for discovery frames
14511 * to be received without a registered VPI.
14512 */
14513 if (!(vport->fc_flag & FC_PT2PT) ||
14514 (phba->link_state == LPFC_HBA_READY)) {
14515 lpfc_in_buf_free(phba, &dmabuf->dbuf);
14516 return;
14517 }
14518 }
14519
James Smart6669f9b2009-10-02 15:16:45 -040014520 /* Handle the basic abort sequence (BA_ABTS) event */
14521 if (fc_hdr->fh_r_ctl == FC_RCTL_BA_ABTS) {
14522 lpfc_sli4_handle_unsol_abort(vport, dmabuf);
14523 return;
14524 }
14525
James Smart4d9ab992009-10-02 15:16:39 -040014526 /* Link this frame */
14527 seq_dmabuf = lpfc_fc_frame_add(vport, dmabuf);
14528 if (!seq_dmabuf) {
14529 /* unable to add frame to vport - throw it out */
14530 lpfc_in_buf_free(phba, &dmabuf->dbuf);
14531 return;
14532 }
14533 /* If not last frame in sequence continue processing frames. */
James Smartdef9c7a2009-12-21 17:02:28 -050014534 if (!lpfc_seq_complete(seq_dmabuf))
James Smart4d9ab992009-10-02 15:16:39 -040014535 return;
James Smartdef9c7a2009-12-21 17:02:28 -050014536
James Smart6669f9b2009-10-02 15:16:45 -040014537 /* Send the complete sequence to the upper layer protocol */
14538 lpfc_sli4_send_seq_to_ulp(vport, seq_dmabuf);
James Smart4f774512009-05-22 14:52:35 -040014539}
James Smart6fb120a2009-05-22 14:52:59 -040014540
14541/**
14542 * lpfc_sli4_post_all_rpi_hdrs - Post the rpi header memory region to the port
14543 * @phba: pointer to lpfc hba data structure.
14544 *
14545 * This routine is invoked to post rpi header templates to the
14546 * HBA consistent with the SLI-4 interface spec. This routine
James Smart49198b32010-04-06 15:04:33 -040014547 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
14548 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
James Smart6fb120a2009-05-22 14:52:59 -040014549 *
14550 * This routine does not require any locks. It's usage is expected
14551 * to be driver load or reset recovery when the driver is
14552 * sequential.
14553 *
14554 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020014555 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -040014556 * -EIO - The mailbox failed to complete successfully.
James Smart6fb120a2009-05-22 14:52:59 -040014557 * When this error occurs, the driver is not guaranteed
14558 * to have any rpi regions posted to the device and
14559 * must either attempt to repost the regions or take a
14560 * fatal error.
14561 **/
14562int
14563lpfc_sli4_post_all_rpi_hdrs(struct lpfc_hba *phba)
14564{
14565 struct lpfc_rpi_hdr *rpi_page;
14566 uint32_t rc = 0;
James Smart6d368e52011-05-24 11:44:12 -040014567 uint16_t lrpi = 0;
James Smart6fb120a2009-05-22 14:52:59 -040014568
James Smart6d368e52011-05-24 11:44:12 -040014569 /* SLI4 ports that support extents do not require RPI headers. */
14570 if (!phba->sli4_hba.rpi_hdrs_in_use)
14571 goto exit;
14572 if (phba->sli4_hba.extents_in_use)
14573 return -EIO;
14574
James Smart6fb120a2009-05-22 14:52:59 -040014575 list_for_each_entry(rpi_page, &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
James Smart6d368e52011-05-24 11:44:12 -040014576 /*
14577 * Assign the rpi headers a physical rpi only if the driver
14578 * has not initialized those resources. A port reset only
14579 * needs the headers posted.
14580 */
14581 if (bf_get(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags) !=
14582 LPFC_RPI_RSRC_RDY)
14583 rpi_page->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
14584
James Smart6fb120a2009-05-22 14:52:59 -040014585 rc = lpfc_sli4_post_rpi_hdr(phba, rpi_page);
14586 if (rc != MBX_SUCCESS) {
14587 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14588 "2008 Error %d posting all rpi "
14589 "headers\n", rc);
14590 rc = -EIO;
14591 break;
14592 }
14593 }
14594
James Smart6d368e52011-05-24 11:44:12 -040014595 exit:
14596 bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags,
14597 LPFC_RPI_RSRC_RDY);
James Smart6fb120a2009-05-22 14:52:59 -040014598 return rc;
14599}
14600
14601/**
14602 * lpfc_sli4_post_rpi_hdr - Post an rpi header memory region to the port
14603 * @phba: pointer to lpfc hba data structure.
14604 * @rpi_page: pointer to the rpi memory region.
14605 *
14606 * This routine is invoked to post a single rpi header to the
14607 * HBA consistent with the SLI-4 interface spec. This memory region
14608 * maps up to 64 rpi context regions.
14609 *
14610 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020014611 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -040014612 * -ENOMEM - No available memory
14613 * -EIO - The mailbox failed to complete successfully.
James Smart6fb120a2009-05-22 14:52:59 -040014614 **/
14615int
14616lpfc_sli4_post_rpi_hdr(struct lpfc_hba *phba, struct lpfc_rpi_hdr *rpi_page)
14617{
14618 LPFC_MBOXQ_t *mboxq;
14619 struct lpfc_mbx_post_hdr_tmpl *hdr_tmpl;
14620 uint32_t rc = 0;
James Smart6fb120a2009-05-22 14:52:59 -040014621 uint32_t shdr_status, shdr_add_status;
14622 union lpfc_sli4_cfg_shdr *shdr;
14623
James Smart6d368e52011-05-24 11:44:12 -040014624 /* SLI4 ports that support extents do not require RPI headers. */
14625 if (!phba->sli4_hba.rpi_hdrs_in_use)
14626 return rc;
14627 if (phba->sli4_hba.extents_in_use)
14628 return -EIO;
14629
James Smart6fb120a2009-05-22 14:52:59 -040014630 /* The port is notified of the header region via a mailbox command. */
14631 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14632 if (!mboxq) {
14633 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14634 "2001 Unable to allocate memory for issuing "
14635 "SLI_CONFIG_SPECIAL mailbox command\n");
14636 return -ENOMEM;
14637 }
14638
14639 /* Post all rpi memory regions to the port. */
14640 hdr_tmpl = &mboxq->u.mqe.un.hdr_tmpl;
James Smart6fb120a2009-05-22 14:52:59 -040014641 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
14642 LPFC_MBOX_OPCODE_FCOE_POST_HDR_TEMPLATE,
14643 sizeof(struct lpfc_mbx_post_hdr_tmpl) -
James Smartfedd3b72011-02-16 12:39:24 -050014644 sizeof(struct lpfc_sli4_cfg_mhdr),
14645 LPFC_SLI4_MBX_EMBED);
James Smart6d368e52011-05-24 11:44:12 -040014646
14647
14648 /* Post the physical rpi to the port for this rpi header. */
James Smart6fb120a2009-05-22 14:52:59 -040014649 bf_set(lpfc_mbx_post_hdr_tmpl_rpi_offset, hdr_tmpl,
14650 rpi_page->start_rpi);
James Smart6d368e52011-05-24 11:44:12 -040014651 bf_set(lpfc_mbx_post_hdr_tmpl_page_cnt,
14652 hdr_tmpl, rpi_page->page_count);
14653
James Smart6fb120a2009-05-22 14:52:59 -040014654 hdr_tmpl->rpi_paddr_lo = putPaddrLow(rpi_page->dmabuf->phys);
14655 hdr_tmpl->rpi_paddr_hi = putPaddrHigh(rpi_page->dmabuf->phys);
James Smartf1126682009-06-10 17:22:44 -040014656 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
James Smart6fb120a2009-05-22 14:52:59 -040014657 shdr = (union lpfc_sli4_cfg_shdr *) &hdr_tmpl->header.cfg_shdr;
14658 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14659 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14660 if (rc != MBX_TIMEOUT)
14661 mempool_free(mboxq, phba->mbox_mem_pool);
14662 if (shdr_status || shdr_add_status || rc) {
14663 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14664 "2514 POST_RPI_HDR mailbox failed with "
14665 "status x%x add_status x%x, mbx status x%x\n",
14666 shdr_status, shdr_add_status, rc);
14667 rc = -ENXIO;
14668 }
14669 return rc;
14670}
14671
14672/**
14673 * lpfc_sli4_alloc_rpi - Get an available rpi in the device's range
14674 * @phba: pointer to lpfc hba data structure.
14675 *
14676 * This routine is invoked to post rpi header templates to the
14677 * HBA consistent with the SLI-4 interface spec. This routine
James Smart49198b32010-04-06 15:04:33 -040014678 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
14679 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
James Smart6fb120a2009-05-22 14:52:59 -040014680 *
14681 * Returns
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020014682 * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
James Smart6fb120a2009-05-22 14:52:59 -040014683 * LPFC_RPI_ALLOC_ERROR if no rpis are available.
14684 **/
14685int
14686lpfc_sli4_alloc_rpi(struct lpfc_hba *phba)
14687{
James Smart6d368e52011-05-24 11:44:12 -040014688 unsigned long rpi;
14689 uint16_t max_rpi, rpi_limit;
14690 uint16_t rpi_remaining, lrpi = 0;
James Smart6fb120a2009-05-22 14:52:59 -040014691 struct lpfc_rpi_hdr *rpi_hdr;
14692
14693 max_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
James Smart6fb120a2009-05-22 14:52:59 -040014694 rpi_limit = phba->sli4_hba.next_rpi;
14695
14696 /*
James Smart6d368e52011-05-24 11:44:12 -040014697 * Fetch the next logical rpi. Because this index is logical,
14698 * the driver starts at 0 each time.
James Smart6fb120a2009-05-22 14:52:59 -040014699 */
14700 spin_lock_irq(&phba->hbalock);
James Smart6d368e52011-05-24 11:44:12 -040014701 rpi = find_next_zero_bit(phba->sli4_hba.rpi_bmask, rpi_limit, 0);
14702 if (rpi >= rpi_limit)
James Smart6fb120a2009-05-22 14:52:59 -040014703 rpi = LPFC_RPI_ALLOC_ERROR;
14704 else {
14705 set_bit(rpi, phba->sli4_hba.rpi_bmask);
14706 phba->sli4_hba.max_cfg_param.rpi_used++;
14707 phba->sli4_hba.rpi_count++;
14708 }
14709
14710 /*
14711 * Don't try to allocate more rpi header regions if the device limit
James Smart6d368e52011-05-24 11:44:12 -040014712 * has been exhausted.
James Smart6fb120a2009-05-22 14:52:59 -040014713 */
14714 if ((rpi == LPFC_RPI_ALLOC_ERROR) &&
14715 (phba->sli4_hba.rpi_count >= max_rpi)) {
14716 spin_unlock_irq(&phba->hbalock);
14717 return rpi;
14718 }
14719
14720 /*
James Smart6d368e52011-05-24 11:44:12 -040014721 * RPI header postings are not required for SLI4 ports capable of
14722 * extents.
14723 */
14724 if (!phba->sli4_hba.rpi_hdrs_in_use) {
14725 spin_unlock_irq(&phba->hbalock);
14726 return rpi;
14727 }
14728
14729 /*
James Smart6fb120a2009-05-22 14:52:59 -040014730 * If the driver is running low on rpi resources, allocate another
14731 * page now. Note that the next_rpi value is used because
14732 * it represents how many are actually in use whereas max_rpi notes
14733 * how many are supported max by the device.
14734 */
James Smart6d368e52011-05-24 11:44:12 -040014735 rpi_remaining = phba->sli4_hba.next_rpi - phba->sli4_hba.rpi_count;
James Smart6fb120a2009-05-22 14:52:59 -040014736 spin_unlock_irq(&phba->hbalock);
14737 if (rpi_remaining < LPFC_RPI_LOW_WATER_MARK) {
14738 rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
14739 if (!rpi_hdr) {
14740 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14741 "2002 Error Could not grow rpi "
14742 "count\n");
14743 } else {
James Smart6d368e52011-05-24 11:44:12 -040014744 lrpi = rpi_hdr->start_rpi;
14745 rpi_hdr->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
James Smart6fb120a2009-05-22 14:52:59 -040014746 lpfc_sli4_post_rpi_hdr(phba, rpi_hdr);
14747 }
14748 }
14749
14750 return rpi;
14751}
14752
14753/**
14754 * lpfc_sli4_free_rpi - Release an rpi for reuse.
14755 * @phba: pointer to lpfc hba data structure.
14756 *
14757 * This routine is invoked to release an rpi to the pool of
14758 * available rpis maintained by the driver.
14759 **/
14760void
James Smartd7c47992010-06-08 18:31:54 -040014761__lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
14762{
14763 if (test_and_clear_bit(rpi, phba->sli4_hba.rpi_bmask)) {
14764 phba->sli4_hba.rpi_count--;
14765 phba->sli4_hba.max_cfg_param.rpi_used--;
14766 }
14767}
14768
14769/**
14770 * lpfc_sli4_free_rpi - Release an rpi for reuse.
14771 * @phba: pointer to lpfc hba data structure.
14772 *
14773 * This routine is invoked to release an rpi to the pool of
14774 * available rpis maintained by the driver.
14775 **/
14776void
James Smart6fb120a2009-05-22 14:52:59 -040014777lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
14778{
14779 spin_lock_irq(&phba->hbalock);
James Smartd7c47992010-06-08 18:31:54 -040014780 __lpfc_sli4_free_rpi(phba, rpi);
James Smart6fb120a2009-05-22 14:52:59 -040014781 spin_unlock_irq(&phba->hbalock);
14782}
14783
14784/**
14785 * lpfc_sli4_remove_rpis - Remove the rpi bitmask region
14786 * @phba: pointer to lpfc hba data structure.
14787 *
14788 * This routine is invoked to remove the memory region that
14789 * provided rpi via a bitmask.
14790 **/
14791void
14792lpfc_sli4_remove_rpis(struct lpfc_hba *phba)
14793{
14794 kfree(phba->sli4_hba.rpi_bmask);
James Smart6d368e52011-05-24 11:44:12 -040014795 kfree(phba->sli4_hba.rpi_ids);
14796 bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
James Smart6fb120a2009-05-22 14:52:59 -040014797}
14798
14799/**
14800 * lpfc_sli4_resume_rpi - Remove the rpi bitmask region
14801 * @phba: pointer to lpfc hba data structure.
14802 *
14803 * This routine is invoked to remove the memory region that
14804 * provided rpi via a bitmask.
14805 **/
14806int
James Smart6b5151f2012-01-18 16:24:06 -050014807lpfc_sli4_resume_rpi(struct lpfc_nodelist *ndlp,
14808 void (*cmpl)(struct lpfc_hba *, LPFC_MBOXQ_t *), void *arg)
James Smart6fb120a2009-05-22 14:52:59 -040014809{
14810 LPFC_MBOXQ_t *mboxq;
14811 struct lpfc_hba *phba = ndlp->phba;
14812 int rc;
14813
14814 /* The port is notified of the header region via a mailbox command. */
14815 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14816 if (!mboxq)
14817 return -ENOMEM;
14818
14819 /* Post all rpi memory regions to the port. */
14820 lpfc_resume_rpi(mboxq, ndlp);
James Smart6b5151f2012-01-18 16:24:06 -050014821 if (cmpl) {
14822 mboxq->mbox_cmpl = cmpl;
14823 mboxq->context1 = arg;
14824 mboxq->context2 = ndlp;
James Smart72859902012-01-18 16:25:38 -050014825 } else
14826 mboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart6b5151f2012-01-18 16:24:06 -050014827 mboxq->vport = ndlp->vport;
James Smart6fb120a2009-05-22 14:52:59 -040014828 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
14829 if (rc == MBX_NOT_FINISHED) {
14830 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14831 "2010 Resume RPI Mailbox failed "
14832 "status %d, mbxStatus x%x\n", rc,
14833 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
14834 mempool_free(mboxq, phba->mbox_mem_pool);
14835 return -EIO;
14836 }
14837 return 0;
14838}
14839
14840/**
14841 * lpfc_sli4_init_vpi - Initialize a vpi with the port
James Smart76a95d72010-11-20 23:11:48 -050014842 * @vport: Pointer to the vport for which the vpi is being initialized
James Smart6fb120a2009-05-22 14:52:59 -040014843 *
James Smart76a95d72010-11-20 23:11:48 -050014844 * This routine is invoked to activate a vpi with the port.
James Smart6fb120a2009-05-22 14:52:59 -040014845 *
14846 * Returns:
14847 * 0 success
14848 * -Evalue otherwise
14849 **/
14850int
James Smart76a95d72010-11-20 23:11:48 -050014851lpfc_sli4_init_vpi(struct lpfc_vport *vport)
James Smart6fb120a2009-05-22 14:52:59 -040014852{
14853 LPFC_MBOXQ_t *mboxq;
14854 int rc = 0;
James Smart6a9c52c2009-10-02 15:16:51 -040014855 int retval = MBX_SUCCESS;
James Smart6fb120a2009-05-22 14:52:59 -040014856 uint32_t mbox_tmo;
James Smart76a95d72010-11-20 23:11:48 -050014857 struct lpfc_hba *phba = vport->phba;
James Smart6fb120a2009-05-22 14:52:59 -040014858 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14859 if (!mboxq)
14860 return -ENOMEM;
James Smart76a95d72010-11-20 23:11:48 -050014861 lpfc_init_vpi(phba, mboxq, vport->vpi);
James Smarta183a152011-10-10 21:32:43 -040014862 mbox_tmo = lpfc_mbox_tmo_val(phba, mboxq);
James Smart6fb120a2009-05-22 14:52:59 -040014863 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
James Smart6fb120a2009-05-22 14:52:59 -040014864 if (rc != MBX_SUCCESS) {
James Smart76a95d72010-11-20 23:11:48 -050014865 lpfc_printf_vlog(vport, KERN_ERR, LOG_SLI,
James Smart6fb120a2009-05-22 14:52:59 -040014866 "2022 INIT VPI Mailbox failed "
14867 "status %d, mbxStatus x%x\n", rc,
14868 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
James Smart6a9c52c2009-10-02 15:16:51 -040014869 retval = -EIO;
James Smart6fb120a2009-05-22 14:52:59 -040014870 }
James Smart6a9c52c2009-10-02 15:16:51 -040014871 if (rc != MBX_TIMEOUT)
James Smart76a95d72010-11-20 23:11:48 -050014872 mempool_free(mboxq, vport->phba->mbox_mem_pool);
James Smart6a9c52c2009-10-02 15:16:51 -040014873
14874 return retval;
James Smart6fb120a2009-05-22 14:52:59 -040014875}
14876
14877/**
14878 * lpfc_mbx_cmpl_add_fcf_record - add fcf mbox completion handler.
14879 * @phba: pointer to lpfc hba data structure.
14880 * @mboxq: Pointer to mailbox object.
14881 *
14882 * This routine is invoked to manually add a single FCF record. The caller
14883 * must pass a completely initialized FCF_Record. This routine takes
14884 * care of the nonembedded mailbox operations.
14885 **/
14886static void
14887lpfc_mbx_cmpl_add_fcf_record(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
14888{
14889 void *virt_addr;
14890 union lpfc_sli4_cfg_shdr *shdr;
14891 uint32_t shdr_status, shdr_add_status;
14892
14893 virt_addr = mboxq->sge_array->addr[0];
14894 /* The IOCTL status is embedded in the mailbox subheader. */
14895 shdr = (union lpfc_sli4_cfg_shdr *) virt_addr;
14896 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14897 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14898
14899 if ((shdr_status || shdr_add_status) &&
14900 (shdr_status != STATUS_FCF_IN_USE))
14901 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14902 "2558 ADD_FCF_RECORD mailbox failed with "
14903 "status x%x add_status x%x\n",
14904 shdr_status, shdr_add_status);
14905
14906 lpfc_sli4_mbox_cmd_free(phba, mboxq);
14907}
14908
14909/**
14910 * lpfc_sli4_add_fcf_record - Manually add an FCF Record.
14911 * @phba: pointer to lpfc hba data structure.
14912 * @fcf_record: pointer to the initialized fcf record to add.
14913 *
14914 * This routine is invoked to manually add a single FCF record. The caller
14915 * must pass a completely initialized FCF_Record. This routine takes
14916 * care of the nonembedded mailbox operations.
14917 **/
14918int
14919lpfc_sli4_add_fcf_record(struct lpfc_hba *phba, struct fcf_record *fcf_record)
14920{
14921 int rc = 0;
14922 LPFC_MBOXQ_t *mboxq;
14923 uint8_t *bytep;
14924 void *virt_addr;
14925 dma_addr_t phys_addr;
14926 struct lpfc_mbx_sge sge;
14927 uint32_t alloc_len, req_len;
14928 uint32_t fcfindex;
14929
14930 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14931 if (!mboxq) {
14932 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14933 "2009 Failed to allocate mbox for ADD_FCF cmd\n");
14934 return -ENOMEM;
14935 }
14936
14937 req_len = sizeof(struct fcf_record) + sizeof(union lpfc_sli4_cfg_shdr) +
14938 sizeof(uint32_t);
14939
14940 /* Allocate DMA memory and set up the non-embedded mailbox command */
14941 alloc_len = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
14942 LPFC_MBOX_OPCODE_FCOE_ADD_FCF,
14943 req_len, LPFC_SLI4_MBX_NEMBED);
14944 if (alloc_len < req_len) {
14945 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14946 "2523 Allocated DMA memory size (x%x) is "
14947 "less than the requested DMA memory "
14948 "size (x%x)\n", alloc_len, req_len);
14949 lpfc_sli4_mbox_cmd_free(phba, mboxq);
14950 return -ENOMEM;
14951 }
14952
14953 /*
14954 * Get the first SGE entry from the non-embedded DMA memory. This
14955 * routine only uses a single SGE.
14956 */
14957 lpfc_sli4_mbx_sge_get(mboxq, 0, &sge);
14958 phys_addr = getPaddr(sge.pa_hi, sge.pa_lo);
James Smart6fb120a2009-05-22 14:52:59 -040014959 virt_addr = mboxq->sge_array->addr[0];
14960 /*
14961 * Configure the FCF record for FCFI 0. This is the driver's
14962 * hardcoded default and gets used in nonFIP mode.
14963 */
14964 fcfindex = bf_get(lpfc_fcf_record_fcf_index, fcf_record);
14965 bytep = virt_addr + sizeof(union lpfc_sli4_cfg_shdr);
14966 lpfc_sli_pcimem_bcopy(&fcfindex, bytep, sizeof(uint32_t));
14967
14968 /*
14969 * Copy the fcf_index and the FCF Record Data. The data starts after
14970 * the FCoE header plus word10. The data copy needs to be endian
14971 * correct.
14972 */
14973 bytep += sizeof(uint32_t);
14974 lpfc_sli_pcimem_bcopy(fcf_record, bytep, sizeof(struct fcf_record));
14975 mboxq->vport = phba->pport;
14976 mboxq->mbox_cmpl = lpfc_mbx_cmpl_add_fcf_record;
14977 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
14978 if (rc == MBX_NOT_FINISHED) {
14979 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14980 "2515 ADD_FCF_RECORD mailbox failed with "
14981 "status 0x%x\n", rc);
14982 lpfc_sli4_mbox_cmd_free(phba, mboxq);
14983 rc = -EIO;
14984 } else
14985 rc = 0;
14986
14987 return rc;
14988}
14989
14990/**
14991 * lpfc_sli4_build_dflt_fcf_record - Build the driver's default FCF Record.
14992 * @phba: pointer to lpfc hba data structure.
14993 * @fcf_record: pointer to the fcf record to write the default data.
14994 * @fcf_index: FCF table entry index.
14995 *
14996 * This routine is invoked to build the driver's default FCF record. The
14997 * values used are hardcoded. This routine handles memory initialization.
14998 *
14999 **/
15000void
15001lpfc_sli4_build_dflt_fcf_record(struct lpfc_hba *phba,
15002 struct fcf_record *fcf_record,
15003 uint16_t fcf_index)
15004{
15005 memset(fcf_record, 0, sizeof(struct fcf_record));
15006 fcf_record->max_rcv_size = LPFC_FCOE_MAX_RCV_SIZE;
15007 fcf_record->fka_adv_period = LPFC_FCOE_FKA_ADV_PER;
15008 fcf_record->fip_priority = LPFC_FCOE_FIP_PRIORITY;
15009 bf_set(lpfc_fcf_record_mac_0, fcf_record, phba->fc_map[0]);
15010 bf_set(lpfc_fcf_record_mac_1, fcf_record, phba->fc_map[1]);
15011 bf_set(lpfc_fcf_record_mac_2, fcf_record, phba->fc_map[2]);
15012 bf_set(lpfc_fcf_record_mac_3, fcf_record, LPFC_FCOE_FCF_MAC3);
15013 bf_set(lpfc_fcf_record_mac_4, fcf_record, LPFC_FCOE_FCF_MAC4);
15014 bf_set(lpfc_fcf_record_mac_5, fcf_record, LPFC_FCOE_FCF_MAC5);
15015 bf_set(lpfc_fcf_record_fc_map_0, fcf_record, phba->fc_map[0]);
15016 bf_set(lpfc_fcf_record_fc_map_1, fcf_record, phba->fc_map[1]);
15017 bf_set(lpfc_fcf_record_fc_map_2, fcf_record, phba->fc_map[2]);
15018 bf_set(lpfc_fcf_record_fcf_valid, fcf_record, 1);
James Smart0c287582009-06-10 17:22:56 -040015019 bf_set(lpfc_fcf_record_fcf_avail, fcf_record, 1);
James Smart6fb120a2009-05-22 14:52:59 -040015020 bf_set(lpfc_fcf_record_fcf_index, fcf_record, fcf_index);
15021 bf_set(lpfc_fcf_record_mac_addr_prov, fcf_record,
15022 LPFC_FCF_FPMA | LPFC_FCF_SPMA);
15023 /* Set the VLAN bit map */
15024 if (phba->valid_vlan) {
15025 fcf_record->vlan_bitmap[phba->vlan_id / 8]
15026 = 1 << (phba->vlan_id % 8);
15027 }
15028}
15029
15030/**
James Smart0c9ab6f2010-02-26 14:15:57 -050015031 * lpfc_sli4_fcf_scan_read_fcf_rec - Read hba fcf record for fcf scan.
James Smart6fb120a2009-05-22 14:52:59 -040015032 * @phba: pointer to lpfc hba data structure.
15033 * @fcf_index: FCF table entry offset.
15034 *
James Smart0c9ab6f2010-02-26 14:15:57 -050015035 * This routine is invoked to scan the entire FCF table by reading FCF
15036 * record and processing it one at a time starting from the @fcf_index
15037 * for initial FCF discovery or fast FCF failover rediscovery.
15038 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030015039 * Return 0 if the mailbox command is submitted successfully, none 0
James Smart0c9ab6f2010-02-26 14:15:57 -050015040 * otherwise.
James Smart6fb120a2009-05-22 14:52:59 -040015041 **/
15042int
James Smart0c9ab6f2010-02-26 14:15:57 -050015043lpfc_sli4_fcf_scan_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
James Smart6fb120a2009-05-22 14:52:59 -040015044{
15045 int rc = 0, error;
15046 LPFC_MBOXQ_t *mboxq;
James Smart6fb120a2009-05-22 14:52:59 -040015047
James Smart32b97932009-07-19 10:01:21 -040015048 phba->fcoe_eventtag_at_fcf_scan = phba->fcoe_eventtag;
James Smart80c17842012-03-01 22:35:45 -050015049 phba->fcoe_cvl_eventtag_attn = phba->fcoe_cvl_eventtag;
James Smart6fb120a2009-05-22 14:52:59 -040015050 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15051 if (!mboxq) {
15052 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15053 "2000 Failed to allocate mbox for "
15054 "READ_FCF cmd\n");
James Smart4d9ab992009-10-02 15:16:39 -040015055 error = -ENOMEM;
James Smart0c9ab6f2010-02-26 14:15:57 -050015056 goto fail_fcf_scan;
James Smart6fb120a2009-05-22 14:52:59 -040015057 }
James Smartecfd03c2010-02-12 14:41:27 -050015058 /* Construct the read FCF record mailbox command */
James Smart0c9ab6f2010-02-26 14:15:57 -050015059 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
James Smartecfd03c2010-02-12 14:41:27 -050015060 if (rc) {
15061 error = -EINVAL;
James Smart0c9ab6f2010-02-26 14:15:57 -050015062 goto fail_fcf_scan;
James Smart6fb120a2009-05-22 14:52:59 -040015063 }
James Smartecfd03c2010-02-12 14:41:27 -050015064 /* Issue the mailbox command asynchronously */
James Smart6fb120a2009-05-22 14:52:59 -040015065 mboxq->vport = phba->pport;
James Smart0c9ab6f2010-02-26 14:15:57 -050015066 mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_scan_read_fcf_rec;
James Smarta93ff372010-10-22 11:06:08 -040015067
15068 spin_lock_irq(&phba->hbalock);
15069 phba->hba_flag |= FCF_TS_INPROG;
15070 spin_unlock_irq(&phba->hbalock);
15071
James Smart6fb120a2009-05-22 14:52:59 -040015072 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
James Smartecfd03c2010-02-12 14:41:27 -050015073 if (rc == MBX_NOT_FINISHED)
James Smart6fb120a2009-05-22 14:52:59 -040015074 error = -EIO;
James Smartecfd03c2010-02-12 14:41:27 -050015075 else {
James Smart38b92ef2010-08-04 16:11:39 -040015076 /* Reset eligible FCF count for new scan */
15077 if (fcf_index == LPFC_FCOE_FCF_GET_FIRST)
James Smart999d8132010-03-15 11:24:56 -040015078 phba->fcf.eligible_fcf_cnt = 0;
James Smart6fb120a2009-05-22 14:52:59 -040015079 error = 0;
James Smart32b97932009-07-19 10:01:21 -040015080 }
James Smart0c9ab6f2010-02-26 14:15:57 -050015081fail_fcf_scan:
James Smart4d9ab992009-10-02 15:16:39 -040015082 if (error) {
15083 if (mboxq)
15084 lpfc_sli4_mbox_cmd_free(phba, mboxq);
James Smarta93ff372010-10-22 11:06:08 -040015085 /* FCF scan failed, clear FCF_TS_INPROG flag */
James Smart4d9ab992009-10-02 15:16:39 -040015086 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -040015087 phba->hba_flag &= ~FCF_TS_INPROG;
James Smart4d9ab992009-10-02 15:16:39 -040015088 spin_unlock_irq(&phba->hbalock);
15089 }
James Smart6fb120a2009-05-22 14:52:59 -040015090 return error;
15091}
James Smarta0c87cb2009-07-19 10:01:10 -040015092
15093/**
James Smarta93ff372010-10-22 11:06:08 -040015094 * lpfc_sli4_fcf_rr_read_fcf_rec - Read hba fcf record for roundrobin fcf.
James Smart0c9ab6f2010-02-26 14:15:57 -050015095 * @phba: pointer to lpfc hba data structure.
15096 * @fcf_index: FCF table entry offset.
15097 *
15098 * This routine is invoked to read an FCF record indicated by @fcf_index
James Smarta93ff372010-10-22 11:06:08 -040015099 * and to use it for FLOGI roundrobin FCF failover.
James Smart0c9ab6f2010-02-26 14:15:57 -050015100 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030015101 * Return 0 if the mailbox command is submitted successfully, none 0
James Smart0c9ab6f2010-02-26 14:15:57 -050015102 * otherwise.
15103 **/
15104int
15105lpfc_sli4_fcf_rr_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
15106{
15107 int rc = 0, error;
15108 LPFC_MBOXQ_t *mboxq;
15109
15110 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15111 if (!mboxq) {
15112 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
15113 "2763 Failed to allocate mbox for "
15114 "READ_FCF cmd\n");
15115 error = -ENOMEM;
15116 goto fail_fcf_read;
15117 }
15118 /* Construct the read FCF record mailbox command */
15119 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
15120 if (rc) {
15121 error = -EINVAL;
15122 goto fail_fcf_read;
15123 }
15124 /* Issue the mailbox command asynchronously */
15125 mboxq->vport = phba->pport;
15126 mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_rr_read_fcf_rec;
15127 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
15128 if (rc == MBX_NOT_FINISHED)
15129 error = -EIO;
15130 else
15131 error = 0;
15132
15133fail_fcf_read:
15134 if (error && mboxq)
15135 lpfc_sli4_mbox_cmd_free(phba, mboxq);
15136 return error;
15137}
15138
15139/**
15140 * lpfc_sli4_read_fcf_rec - Read hba fcf record for update eligible fcf bmask.
15141 * @phba: pointer to lpfc hba data structure.
15142 * @fcf_index: FCF table entry offset.
15143 *
15144 * This routine is invoked to read an FCF record indicated by @fcf_index to
James Smarta93ff372010-10-22 11:06:08 -040015145 * determine whether it's eligible for FLOGI roundrobin failover list.
James Smart0c9ab6f2010-02-26 14:15:57 -050015146 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030015147 * Return 0 if the mailbox command is submitted successfully, none 0
James Smart0c9ab6f2010-02-26 14:15:57 -050015148 * otherwise.
15149 **/
15150int
15151lpfc_sli4_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
15152{
15153 int rc = 0, error;
15154 LPFC_MBOXQ_t *mboxq;
15155
15156 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15157 if (!mboxq) {
15158 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
15159 "2758 Failed to allocate mbox for "
15160 "READ_FCF cmd\n");
15161 error = -ENOMEM;
15162 goto fail_fcf_read;
15163 }
15164 /* Construct the read FCF record mailbox command */
15165 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
15166 if (rc) {
15167 error = -EINVAL;
15168 goto fail_fcf_read;
15169 }
15170 /* Issue the mailbox command asynchronously */
15171 mboxq->vport = phba->pport;
15172 mboxq->mbox_cmpl = lpfc_mbx_cmpl_read_fcf_rec;
15173 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
15174 if (rc == MBX_NOT_FINISHED)
15175 error = -EIO;
15176 else
15177 error = 0;
15178
15179fail_fcf_read:
15180 if (error && mboxq)
15181 lpfc_sli4_mbox_cmd_free(phba, mboxq);
15182 return error;
15183}
15184
15185/**
James Smart7d791df2011-07-22 18:37:52 -040015186 * lpfc_check_next_fcf_pri
15187 * phba pointer to the lpfc_hba struct for this port.
15188 * This routine is called from the lpfc_sli4_fcf_rr_next_index_get
15189 * routine when the rr_bmask is empty. The FCF indecies are put into the
15190 * rr_bmask based on their priority level. Starting from the highest priority
15191 * to the lowest. The most likely FCF candidate will be in the highest
15192 * priority group. When this routine is called it searches the fcf_pri list for
15193 * next lowest priority group and repopulates the rr_bmask with only those
15194 * fcf_indexes.
15195 * returns:
15196 * 1=success 0=failure
15197 **/
15198int
15199lpfc_check_next_fcf_pri_level(struct lpfc_hba *phba)
15200{
15201 uint16_t next_fcf_pri;
15202 uint16_t last_index;
15203 struct lpfc_fcf_pri *fcf_pri;
15204 int rc;
15205 int ret = 0;
15206
15207 last_index = find_first_bit(phba->fcf.fcf_rr_bmask,
15208 LPFC_SLI4_FCF_TBL_INDX_MAX);
15209 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
15210 "3060 Last IDX %d\n", last_index);
15211 if (list_empty(&phba->fcf.fcf_pri_list)) {
15212 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
15213 "3061 Last IDX %d\n", last_index);
15214 return 0; /* Empty rr list */
15215 }
15216 next_fcf_pri = 0;
15217 /*
15218 * Clear the rr_bmask and set all of the bits that are at this
15219 * priority.
15220 */
15221 memset(phba->fcf.fcf_rr_bmask, 0,
15222 sizeof(*phba->fcf.fcf_rr_bmask));
15223 spin_lock_irq(&phba->hbalock);
15224 list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
15225 if (fcf_pri->fcf_rec.flag & LPFC_FCF_FLOGI_FAILED)
15226 continue;
15227 /*
15228 * the 1st priority that has not FLOGI failed
15229 * will be the highest.
15230 */
15231 if (!next_fcf_pri)
15232 next_fcf_pri = fcf_pri->fcf_rec.priority;
15233 spin_unlock_irq(&phba->hbalock);
15234 if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
15235 rc = lpfc_sli4_fcf_rr_index_set(phba,
15236 fcf_pri->fcf_rec.fcf_index);
15237 if (rc)
15238 return 0;
15239 }
15240 spin_lock_irq(&phba->hbalock);
15241 }
15242 /*
15243 * if next_fcf_pri was not set above and the list is not empty then
15244 * we have failed flogis on all of them. So reset flogi failed
15245 * and start at the begining.
15246 */
15247 if (!next_fcf_pri && !list_empty(&phba->fcf.fcf_pri_list)) {
15248 list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
15249 fcf_pri->fcf_rec.flag &= ~LPFC_FCF_FLOGI_FAILED;
15250 /*
15251 * the 1st priority that has not FLOGI failed
15252 * will be the highest.
15253 */
15254 if (!next_fcf_pri)
15255 next_fcf_pri = fcf_pri->fcf_rec.priority;
15256 spin_unlock_irq(&phba->hbalock);
15257 if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
15258 rc = lpfc_sli4_fcf_rr_index_set(phba,
15259 fcf_pri->fcf_rec.fcf_index);
15260 if (rc)
15261 return 0;
15262 }
15263 spin_lock_irq(&phba->hbalock);
15264 }
15265 } else
15266 ret = 1;
15267 spin_unlock_irq(&phba->hbalock);
15268
15269 return ret;
15270}
15271/**
James Smart0c9ab6f2010-02-26 14:15:57 -050015272 * lpfc_sli4_fcf_rr_next_index_get - Get next eligible fcf record index
15273 * @phba: pointer to lpfc hba data structure.
15274 *
15275 * This routine is to get the next eligible FCF record index in a round
15276 * robin fashion. If the next eligible FCF record index equals to the
James Smarta93ff372010-10-22 11:06:08 -040015277 * initial roundrobin FCF record index, LPFC_FCOE_FCF_NEXT_NONE (0xFFFF)
James Smart0c9ab6f2010-02-26 14:15:57 -050015278 * shall be returned, otherwise, the next eligible FCF record's index
15279 * shall be returned.
15280 **/
15281uint16_t
15282lpfc_sli4_fcf_rr_next_index_get(struct lpfc_hba *phba)
15283{
15284 uint16_t next_fcf_index;
15285
James Smart3804dc82010-07-14 15:31:37 -040015286 /* Search start from next bit of currently registered FCF index */
James Smart7d791df2011-07-22 18:37:52 -040015287next_priority:
James Smart3804dc82010-07-14 15:31:37 -040015288 next_fcf_index = (phba->fcf.current_rec.fcf_indx + 1) %
15289 LPFC_SLI4_FCF_TBL_INDX_MAX;
James Smart0c9ab6f2010-02-26 14:15:57 -050015290 next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
15291 LPFC_SLI4_FCF_TBL_INDX_MAX,
James Smart3804dc82010-07-14 15:31:37 -040015292 next_fcf_index);
15293
James Smart0c9ab6f2010-02-26 14:15:57 -050015294 /* Wrap around condition on phba->fcf.fcf_rr_bmask */
James Smart7d791df2011-07-22 18:37:52 -040015295 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
15296 /*
15297 * If we have wrapped then we need to clear the bits that
15298 * have been tested so that we can detect when we should
15299 * change the priority level.
15300 */
James Smart0c9ab6f2010-02-26 14:15:57 -050015301 next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
15302 LPFC_SLI4_FCF_TBL_INDX_MAX, 0);
James Smart7d791df2011-07-22 18:37:52 -040015303 }
15304
James Smart0c9ab6f2010-02-26 14:15:57 -050015305
James Smart3804dc82010-07-14 15:31:37 -040015306 /* Check roundrobin failover list empty condition */
James Smart7d791df2011-07-22 18:37:52 -040015307 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX ||
15308 next_fcf_index == phba->fcf.current_rec.fcf_indx) {
15309 /*
15310 * If next fcf index is not found check if there are lower
15311 * Priority level fcf's in the fcf_priority list.
15312 * Set up the rr_bmask with all of the avaiable fcf bits
15313 * at that level and continue the selection process.
15314 */
15315 if (lpfc_check_next_fcf_pri_level(phba))
15316 goto next_priority;
James Smart3804dc82010-07-14 15:31:37 -040015317 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
15318 "2844 No roundrobin failover FCF available\n");
James Smart7d791df2011-07-22 18:37:52 -040015319 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX)
15320 return LPFC_FCOE_FCF_NEXT_NONE;
15321 else {
15322 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
15323 "3063 Only FCF available idx %d, flag %x\n",
15324 next_fcf_index,
15325 phba->fcf.fcf_pri[next_fcf_index].fcf_rec.flag);
15326 return next_fcf_index;
15327 }
James Smart3804dc82010-07-14 15:31:37 -040015328 }
15329
James Smart7d791df2011-07-22 18:37:52 -040015330 if (next_fcf_index < LPFC_SLI4_FCF_TBL_INDX_MAX &&
15331 phba->fcf.fcf_pri[next_fcf_index].fcf_rec.flag &
15332 LPFC_FCF_FLOGI_FAILED)
15333 goto next_priority;
15334
James Smart3804dc82010-07-14 15:31:37 -040015335 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040015336 "2845 Get next roundrobin failover FCF (x%x)\n",
15337 next_fcf_index);
15338
James Smart0c9ab6f2010-02-26 14:15:57 -050015339 return next_fcf_index;
15340}
15341
15342/**
15343 * lpfc_sli4_fcf_rr_index_set - Set bmask with eligible fcf record index
15344 * @phba: pointer to lpfc hba data structure.
15345 *
15346 * This routine sets the FCF record index in to the eligible bmask for
James Smarta93ff372010-10-22 11:06:08 -040015347 * roundrobin failover search. It checks to make sure that the index
James Smart0c9ab6f2010-02-26 14:15:57 -050015348 * does not go beyond the range of the driver allocated bmask dimension
15349 * before setting the bit.
15350 *
15351 * Returns 0 if the index bit successfully set, otherwise, it returns
15352 * -EINVAL.
15353 **/
15354int
15355lpfc_sli4_fcf_rr_index_set(struct lpfc_hba *phba, uint16_t fcf_index)
15356{
15357 if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
15358 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040015359 "2610 FCF (x%x) reached driver's book "
15360 "keeping dimension:x%x\n",
James Smart0c9ab6f2010-02-26 14:15:57 -050015361 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
15362 return -EINVAL;
15363 }
15364 /* Set the eligible FCF record index bmask */
15365 set_bit(fcf_index, phba->fcf.fcf_rr_bmask);
15366
James Smart3804dc82010-07-14 15:31:37 -040015367 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040015368 "2790 Set FCF (x%x) to roundrobin FCF failover "
James Smart3804dc82010-07-14 15:31:37 -040015369 "bmask\n", fcf_index);
15370
James Smart0c9ab6f2010-02-26 14:15:57 -050015371 return 0;
15372}
15373
15374/**
James Smart3804dc82010-07-14 15:31:37 -040015375 * lpfc_sli4_fcf_rr_index_clear - Clear bmask from eligible fcf record index
James Smart0c9ab6f2010-02-26 14:15:57 -050015376 * @phba: pointer to lpfc hba data structure.
15377 *
15378 * This routine clears the FCF record index from the eligible bmask for
James Smarta93ff372010-10-22 11:06:08 -040015379 * roundrobin failover search. It checks to make sure that the index
James Smart0c9ab6f2010-02-26 14:15:57 -050015380 * does not go beyond the range of the driver allocated bmask dimension
15381 * before clearing the bit.
15382 **/
15383void
15384lpfc_sli4_fcf_rr_index_clear(struct lpfc_hba *phba, uint16_t fcf_index)
15385{
James Smart7d791df2011-07-22 18:37:52 -040015386 struct lpfc_fcf_pri *fcf_pri;
James Smart0c9ab6f2010-02-26 14:15:57 -050015387 if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
15388 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040015389 "2762 FCF (x%x) reached driver's book "
15390 "keeping dimension:x%x\n",
James Smart0c9ab6f2010-02-26 14:15:57 -050015391 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
15392 return;
15393 }
15394 /* Clear the eligible FCF record index bmask */
James Smart7d791df2011-07-22 18:37:52 -040015395 spin_lock_irq(&phba->hbalock);
15396 list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
15397 if (fcf_pri->fcf_rec.fcf_index == fcf_index) {
15398 list_del_init(&fcf_pri->list);
15399 break;
15400 }
15401 }
15402 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -050015403 clear_bit(fcf_index, phba->fcf.fcf_rr_bmask);
James Smart3804dc82010-07-14 15:31:37 -040015404
15405 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040015406 "2791 Clear FCF (x%x) from roundrobin failover "
James Smart3804dc82010-07-14 15:31:37 -040015407 "bmask\n", fcf_index);
James Smart0c9ab6f2010-02-26 14:15:57 -050015408}
15409
15410/**
James Smartecfd03c2010-02-12 14:41:27 -050015411 * lpfc_mbx_cmpl_redisc_fcf_table - completion routine for rediscover FCF table
15412 * @phba: pointer to lpfc hba data structure.
15413 *
15414 * This routine is the completion routine for the rediscover FCF table mailbox
15415 * command. If the mailbox command returned failure, it will try to stop the
15416 * FCF rediscover wait timer.
15417 **/
15418void
15419lpfc_mbx_cmpl_redisc_fcf_table(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
15420{
15421 struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
15422 uint32_t shdr_status, shdr_add_status;
15423
15424 redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
15425
15426 shdr_status = bf_get(lpfc_mbox_hdr_status,
15427 &redisc_fcf->header.cfg_shdr.response);
15428 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
15429 &redisc_fcf->header.cfg_shdr.response);
15430 if (shdr_status || shdr_add_status) {
James Smart0c9ab6f2010-02-26 14:15:57 -050015431 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smartecfd03c2010-02-12 14:41:27 -050015432 "2746 Requesting for FCF rediscovery failed "
15433 "status x%x add_status x%x\n",
15434 shdr_status, shdr_add_status);
James Smart0c9ab6f2010-02-26 14:15:57 -050015435 if (phba->fcf.fcf_flag & FCF_ACVL_DISC) {
James Smartfc2b9892010-02-26 14:15:29 -050015436 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -050015437 phba->fcf.fcf_flag &= ~FCF_ACVL_DISC;
James Smartfc2b9892010-02-26 14:15:29 -050015438 spin_unlock_irq(&phba->hbalock);
15439 /*
15440 * CVL event triggered FCF rediscover request failed,
15441 * last resort to re-try current registered FCF entry.
15442 */
15443 lpfc_retry_pport_discovery(phba);
15444 } else {
15445 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -050015446 phba->fcf.fcf_flag &= ~FCF_DEAD_DISC;
James Smartfc2b9892010-02-26 14:15:29 -050015447 spin_unlock_irq(&phba->hbalock);
15448 /*
15449 * DEAD FCF event triggered FCF rediscover request
15450 * failed, last resort to fail over as a link down
15451 * to FCF registration.
15452 */
15453 lpfc_sli4_fcf_dead_failthrough(phba);
15454 }
James Smart0c9ab6f2010-02-26 14:15:57 -050015455 } else {
15456 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040015457 "2775 Start FCF rediscover quiescent timer\n");
James Smartecfd03c2010-02-12 14:41:27 -050015458 /*
15459 * Start FCF rediscovery wait timer for pending FCF
15460 * before rescan FCF record table.
15461 */
15462 lpfc_fcf_redisc_wait_start_timer(phba);
James Smart0c9ab6f2010-02-26 14:15:57 -050015463 }
James Smartecfd03c2010-02-12 14:41:27 -050015464
15465 mempool_free(mbox, phba->mbox_mem_pool);
15466}
15467
15468/**
James Smart3804dc82010-07-14 15:31:37 -040015469 * lpfc_sli4_redisc_fcf_table - Request to rediscover entire FCF table by port.
James Smartecfd03c2010-02-12 14:41:27 -050015470 * @phba: pointer to lpfc hba data structure.
15471 *
15472 * This routine is invoked to request for rediscovery of the entire FCF table
15473 * by the port.
15474 **/
15475int
15476lpfc_sli4_redisc_fcf_table(struct lpfc_hba *phba)
15477{
15478 LPFC_MBOXQ_t *mbox;
15479 struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
15480 int rc, length;
15481
James Smart0c9ab6f2010-02-26 14:15:57 -050015482 /* Cancel retry delay timers to all vports before FCF rediscover */
15483 lpfc_cancel_all_vport_retry_delay_timer(phba);
15484
James Smartecfd03c2010-02-12 14:41:27 -050015485 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15486 if (!mbox) {
15487 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15488 "2745 Failed to allocate mbox for "
15489 "requesting FCF rediscover.\n");
15490 return -ENOMEM;
15491 }
15492
15493 length = (sizeof(struct lpfc_mbx_redisc_fcf_tbl) -
15494 sizeof(struct lpfc_sli4_cfg_mhdr));
15495 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15496 LPFC_MBOX_OPCODE_FCOE_REDISCOVER_FCF,
15497 length, LPFC_SLI4_MBX_EMBED);
15498
15499 redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
15500 /* Set count to 0 for invalidating the entire FCF database */
15501 bf_set(lpfc_mbx_redisc_fcf_count, redisc_fcf, 0);
15502
15503 /* Issue the mailbox command asynchronously */
15504 mbox->vport = phba->pport;
15505 mbox->mbox_cmpl = lpfc_mbx_cmpl_redisc_fcf_table;
15506 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
15507
15508 if (rc == MBX_NOT_FINISHED) {
15509 mempool_free(mbox, phba->mbox_mem_pool);
15510 return -EIO;
15511 }
15512 return 0;
15513}
15514
15515/**
James Smartfc2b9892010-02-26 14:15:29 -050015516 * lpfc_sli4_fcf_dead_failthrough - Failthrough routine to fcf dead event
15517 * @phba: pointer to lpfc hba data structure.
15518 *
15519 * This function is the failover routine as a last resort to the FCF DEAD
15520 * event when driver failed to perform fast FCF failover.
15521 **/
15522void
15523lpfc_sli4_fcf_dead_failthrough(struct lpfc_hba *phba)
15524{
15525 uint32_t link_state;
15526
15527 /*
15528 * Last resort as FCF DEAD event failover will treat this as
15529 * a link down, but save the link state because we don't want
15530 * it to be changed to Link Down unless it is already down.
15531 */
15532 link_state = phba->link_state;
15533 lpfc_linkdown(phba);
15534 phba->link_state = link_state;
15535
15536 /* Unregister FCF if no devices connected to it */
15537 lpfc_unregister_unused_fcf(phba);
15538}
15539
15540/**
James Smart026abb82011-12-13 13:20:45 -050015541 * lpfc_sli_get_config_region23 - Get sli3 port region 23 data.
James Smarta0c87cb2009-07-19 10:01:10 -040015542 * @phba: pointer to lpfc hba data structure.
James Smart026abb82011-12-13 13:20:45 -050015543 * @rgn23_data: pointer to configure region 23 data.
James Smarta0c87cb2009-07-19 10:01:10 -040015544 *
James Smart026abb82011-12-13 13:20:45 -050015545 * This function gets SLI3 port configure region 23 data through memory dump
15546 * mailbox command. When it successfully retrieves data, the size of the data
15547 * will be returned, otherwise, 0 will be returned.
James Smarta0c87cb2009-07-19 10:01:10 -040015548 **/
James Smart026abb82011-12-13 13:20:45 -050015549static uint32_t
15550lpfc_sli_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)
James Smarta0c87cb2009-07-19 10:01:10 -040015551{
15552 LPFC_MBOXQ_t *pmb = NULL;
15553 MAILBOX_t *mb;
James Smart026abb82011-12-13 13:20:45 -050015554 uint32_t offset = 0;
James Smarta0c87cb2009-07-19 10:01:10 -040015555 int rc;
15556
James Smart026abb82011-12-13 13:20:45 -050015557 if (!rgn23_data)
15558 return 0;
15559
James Smarta0c87cb2009-07-19 10:01:10 -040015560 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15561 if (!pmb) {
15562 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart026abb82011-12-13 13:20:45 -050015563 "2600 failed to allocate mailbox memory\n");
15564 return 0;
James Smarta0c87cb2009-07-19 10:01:10 -040015565 }
15566 mb = &pmb->u.mb;
15567
James Smarta0c87cb2009-07-19 10:01:10 -040015568 do {
15569 lpfc_dump_mem(phba, pmb, offset, DMP_REGION_23);
15570 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
15571
15572 if (rc != MBX_SUCCESS) {
15573 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smart026abb82011-12-13 13:20:45 -050015574 "2601 failed to read config "
15575 "region 23, rc 0x%x Status 0x%x\n",
15576 rc, mb->mbxStatus);
James Smarta0c87cb2009-07-19 10:01:10 -040015577 mb->un.varDmp.word_cnt = 0;
15578 }
15579 /*
15580 * dump mem may return a zero when finished or we got a
15581 * mailbox error, either way we are done.
15582 */
15583 if (mb->un.varDmp.word_cnt == 0)
15584 break;
15585 if (mb->un.varDmp.word_cnt > DMP_RGN23_SIZE - offset)
15586 mb->un.varDmp.word_cnt = DMP_RGN23_SIZE - offset;
15587
15588 lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
James Smart026abb82011-12-13 13:20:45 -050015589 rgn23_data + offset,
15590 mb->un.varDmp.word_cnt);
James Smarta0c87cb2009-07-19 10:01:10 -040015591 offset += mb->un.varDmp.word_cnt;
15592 } while (mb->un.varDmp.word_cnt && offset < DMP_RGN23_SIZE);
15593
James Smart026abb82011-12-13 13:20:45 -050015594 mempool_free(pmb, phba->mbox_mem_pool);
15595 return offset;
15596}
15597
15598/**
15599 * lpfc_sli4_get_config_region23 - Get sli4 port region 23 data.
15600 * @phba: pointer to lpfc hba data structure.
15601 * @rgn23_data: pointer to configure region 23 data.
15602 *
15603 * This function gets SLI4 port configure region 23 data through memory dump
15604 * mailbox command. When it successfully retrieves data, the size of the data
15605 * will be returned, otherwise, 0 will be returned.
15606 **/
15607static uint32_t
15608lpfc_sli4_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)
15609{
15610 LPFC_MBOXQ_t *mboxq = NULL;
15611 struct lpfc_dmabuf *mp = NULL;
15612 struct lpfc_mqe *mqe;
15613 uint32_t data_length = 0;
15614 int rc;
15615
15616 if (!rgn23_data)
15617 return 0;
15618
15619 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15620 if (!mboxq) {
15621 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15622 "3105 failed to allocate mailbox memory\n");
15623 return 0;
15624 }
15625
15626 if (lpfc_sli4_dump_cfg_rg23(phba, mboxq))
15627 goto out;
15628 mqe = &mboxq->u.mqe;
15629 mp = (struct lpfc_dmabuf *) mboxq->context1;
15630 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
15631 if (rc)
15632 goto out;
15633 data_length = mqe->un.mb_words[5];
15634 if (data_length == 0)
15635 goto out;
15636 if (data_length > DMP_RGN23_SIZE) {
15637 data_length = 0;
15638 goto out;
15639 }
15640 lpfc_sli_pcimem_bcopy((char *)mp->virt, rgn23_data, data_length);
15641out:
15642 mempool_free(mboxq, phba->mbox_mem_pool);
15643 if (mp) {
15644 lpfc_mbuf_free(phba, mp->virt, mp->phys);
15645 kfree(mp);
15646 }
15647 return data_length;
15648}
15649
15650/**
15651 * lpfc_sli_read_link_ste - Read region 23 to decide if link is disabled.
15652 * @phba: pointer to lpfc hba data structure.
15653 *
15654 * This function read region 23 and parse TLV for port status to
15655 * decide if the user disaled the port. If the TLV indicates the
15656 * port is disabled, the hba_flag is set accordingly.
15657 **/
15658void
15659lpfc_sli_read_link_ste(struct lpfc_hba *phba)
15660{
15661 uint8_t *rgn23_data = NULL;
15662 uint32_t if_type, data_size, sub_tlv_len, tlv_offset;
15663 uint32_t offset = 0;
15664
15665 /* Get adapter Region 23 data */
15666 rgn23_data = kzalloc(DMP_RGN23_SIZE, GFP_KERNEL);
15667 if (!rgn23_data)
15668 goto out;
15669
15670 if (phba->sli_rev < LPFC_SLI_REV4)
15671 data_size = lpfc_sli_get_config_region23(phba, rgn23_data);
15672 else {
15673 if_type = bf_get(lpfc_sli_intf_if_type,
15674 &phba->sli4_hba.sli_intf);
15675 if (if_type == LPFC_SLI_INTF_IF_TYPE_0)
15676 goto out;
15677 data_size = lpfc_sli4_get_config_region23(phba, rgn23_data);
15678 }
James Smarta0c87cb2009-07-19 10:01:10 -040015679
15680 if (!data_size)
15681 goto out;
15682
15683 /* Check the region signature first */
15684 if (memcmp(&rgn23_data[offset], LPFC_REGION23_SIGNATURE, 4)) {
15685 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15686 "2619 Config region 23 has bad signature\n");
15687 goto out;
15688 }
15689 offset += 4;
15690
15691 /* Check the data structure version */
15692 if (rgn23_data[offset] != LPFC_REGION23_VERSION) {
15693 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15694 "2620 Config region 23 has bad version\n");
15695 goto out;
15696 }
15697 offset += 4;
15698
15699 /* Parse TLV entries in the region */
15700 while (offset < data_size) {
15701 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC)
15702 break;
15703 /*
15704 * If the TLV is not driver specific TLV or driver id is
15705 * not linux driver id, skip the record.
15706 */
15707 if ((rgn23_data[offset] != DRIVER_SPECIFIC_TYPE) ||
15708 (rgn23_data[offset + 2] != LINUX_DRIVER_ID) ||
15709 (rgn23_data[offset + 3] != 0)) {
15710 offset += rgn23_data[offset + 1] * 4 + 4;
15711 continue;
15712 }
15713
15714 /* Driver found a driver specific TLV in the config region */
15715 sub_tlv_len = rgn23_data[offset + 1] * 4;
15716 offset += 4;
15717 tlv_offset = 0;
15718
15719 /*
15720 * Search for configured port state sub-TLV.
15721 */
15722 while ((offset < data_size) &&
15723 (tlv_offset < sub_tlv_len)) {
15724 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC) {
15725 offset += 4;
15726 tlv_offset += 4;
15727 break;
15728 }
15729 if (rgn23_data[offset] != PORT_STE_TYPE) {
15730 offset += rgn23_data[offset + 1] * 4 + 4;
15731 tlv_offset += rgn23_data[offset + 1] * 4 + 4;
15732 continue;
15733 }
15734
15735 /* This HBA contains PORT_STE configured */
15736 if (!rgn23_data[offset + 2])
15737 phba->hba_flag |= LINK_DISABLED;
15738
15739 goto out;
15740 }
15741 }
James Smart026abb82011-12-13 13:20:45 -050015742
James Smarta0c87cb2009-07-19 10:01:10 -040015743out:
James Smarta0c87cb2009-07-19 10:01:10 -040015744 kfree(rgn23_data);
15745 return;
15746}
James Smart695a8142010-01-26 23:08:03 -050015747
15748/**
James Smart52d52442011-05-24 11:42:45 -040015749 * lpfc_wr_object - write an object to the firmware
15750 * @phba: HBA structure that indicates port to create a queue on.
15751 * @dmabuf_list: list of dmabufs to write to the port.
15752 * @size: the total byte value of the objects to write to the port.
15753 * @offset: the current offset to be used to start the transfer.
15754 *
15755 * This routine will create a wr_object mailbox command to send to the port.
15756 * the mailbox command will be constructed using the dma buffers described in
15757 * @dmabuf_list to create a list of BDEs. This routine will fill in as many
15758 * BDEs that the imbedded mailbox can support. The @offset variable will be
15759 * used to indicate the starting offset of the transfer and will also return
15760 * the offset after the write object mailbox has completed. @size is used to
15761 * determine the end of the object and whether the eof bit should be set.
15762 *
15763 * Return 0 is successful and offset will contain the the new offset to use
15764 * for the next write.
15765 * Return negative value for error cases.
15766 **/
15767int
15768lpfc_wr_object(struct lpfc_hba *phba, struct list_head *dmabuf_list,
15769 uint32_t size, uint32_t *offset)
15770{
15771 struct lpfc_mbx_wr_object *wr_object;
15772 LPFC_MBOXQ_t *mbox;
15773 int rc = 0, i = 0;
15774 uint32_t shdr_status, shdr_add_status;
15775 uint32_t mbox_tmo;
15776 union lpfc_sli4_cfg_shdr *shdr;
15777 struct lpfc_dmabuf *dmabuf;
15778 uint32_t written = 0;
15779
15780 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15781 if (!mbox)
15782 return -ENOMEM;
15783
15784 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
15785 LPFC_MBOX_OPCODE_WRITE_OBJECT,
15786 sizeof(struct lpfc_mbx_wr_object) -
15787 sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
15788
15789 wr_object = (struct lpfc_mbx_wr_object *)&mbox->u.mqe.un.wr_object;
15790 wr_object->u.request.write_offset = *offset;
15791 sprintf((uint8_t *)wr_object->u.request.object_name, "/");
15792 wr_object->u.request.object_name[0] =
15793 cpu_to_le32(wr_object->u.request.object_name[0]);
15794 bf_set(lpfc_wr_object_eof, &wr_object->u.request, 0);
15795 list_for_each_entry(dmabuf, dmabuf_list, list) {
15796 if (i >= LPFC_MBX_WR_CONFIG_MAX_BDE || written >= size)
15797 break;
15798 wr_object->u.request.bde[i].addrLow = putPaddrLow(dmabuf->phys);
15799 wr_object->u.request.bde[i].addrHigh =
15800 putPaddrHigh(dmabuf->phys);
15801 if (written + SLI4_PAGE_SIZE >= size) {
15802 wr_object->u.request.bde[i].tus.f.bdeSize =
15803 (size - written);
15804 written += (size - written);
15805 bf_set(lpfc_wr_object_eof, &wr_object->u.request, 1);
15806 } else {
15807 wr_object->u.request.bde[i].tus.f.bdeSize =
15808 SLI4_PAGE_SIZE;
15809 written += SLI4_PAGE_SIZE;
15810 }
15811 i++;
15812 }
15813 wr_object->u.request.bde_count = i;
15814 bf_set(lpfc_wr_object_write_length, &wr_object->u.request, written);
15815 if (!phba->sli4_hba.intr_enable)
15816 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15817 else {
James Smarta183a152011-10-10 21:32:43 -040015818 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart52d52442011-05-24 11:42:45 -040015819 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
15820 }
15821 /* The IOCTL status is embedded in the mailbox subheader. */
15822 shdr = (union lpfc_sli4_cfg_shdr *) &wr_object->header.cfg_shdr;
15823 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15824 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15825 if (rc != MBX_TIMEOUT)
15826 mempool_free(mbox, phba->mbox_mem_pool);
15827 if (shdr_status || shdr_add_status || rc) {
15828 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15829 "3025 Write Object mailbox failed with "
15830 "status x%x add_status x%x, mbx status x%x\n",
15831 shdr_status, shdr_add_status, rc);
15832 rc = -ENXIO;
15833 } else
15834 *offset += wr_object->u.response.actual_write_length;
15835 return rc;
15836}
15837
15838/**
James Smart695a8142010-01-26 23:08:03 -050015839 * lpfc_cleanup_pending_mbox - Free up vport discovery mailbox commands.
15840 * @vport: pointer to vport data structure.
15841 *
15842 * This function iterate through the mailboxq and clean up all REG_LOGIN
15843 * and REG_VPI mailbox commands associated with the vport. This function
15844 * is called when driver want to restart discovery of the vport due to
15845 * a Clear Virtual Link event.
15846 **/
15847void
15848lpfc_cleanup_pending_mbox(struct lpfc_vport *vport)
15849{
15850 struct lpfc_hba *phba = vport->phba;
15851 LPFC_MBOXQ_t *mb, *nextmb;
15852 struct lpfc_dmabuf *mp;
James Smart78730cf2010-04-06 15:06:30 -040015853 struct lpfc_nodelist *ndlp;
James Smartd439d282010-09-29 11:18:45 -040015854 struct lpfc_nodelist *act_mbx_ndlp = NULL;
James Smart589a52d2010-07-14 15:30:54 -040015855 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smartd439d282010-09-29 11:18:45 -040015856 LIST_HEAD(mbox_cmd_list);
James Smart63e801c2010-11-20 23:14:19 -050015857 uint8_t restart_loop;
James Smart695a8142010-01-26 23:08:03 -050015858
James Smartd439d282010-09-29 11:18:45 -040015859 /* Clean up internally queued mailbox commands with the vport */
James Smart695a8142010-01-26 23:08:03 -050015860 spin_lock_irq(&phba->hbalock);
15861 list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
15862 if (mb->vport != vport)
15863 continue;
15864
15865 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
15866 (mb->u.mb.mbxCommand != MBX_REG_VPI))
15867 continue;
15868
James Smartd439d282010-09-29 11:18:45 -040015869 list_del(&mb->list);
15870 list_add_tail(&mb->list, &mbox_cmd_list);
15871 }
15872 /* Clean up active mailbox command with the vport */
15873 mb = phba->sli.mbox_active;
15874 if (mb && (mb->vport == vport)) {
15875 if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) ||
15876 (mb->u.mb.mbxCommand == MBX_REG_VPI))
15877 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
15878 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
15879 act_mbx_ndlp = (struct lpfc_nodelist *)mb->context2;
15880 /* Put reference count for delayed processing */
15881 act_mbx_ndlp = lpfc_nlp_get(act_mbx_ndlp);
15882 /* Unregister the RPI when mailbox complete */
15883 mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
15884 }
15885 }
James Smart63e801c2010-11-20 23:14:19 -050015886 /* Cleanup any mailbox completions which are not yet processed */
15887 do {
15888 restart_loop = 0;
15889 list_for_each_entry(mb, &phba->sli.mboxq_cmpl, list) {
15890 /*
15891 * If this mailox is already processed or it is
15892 * for another vport ignore it.
15893 */
15894 if ((mb->vport != vport) ||
15895 (mb->mbox_flag & LPFC_MBX_IMED_UNREG))
15896 continue;
15897
15898 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
15899 (mb->u.mb.mbxCommand != MBX_REG_VPI))
15900 continue;
15901
15902 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
15903 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
15904 ndlp = (struct lpfc_nodelist *)mb->context2;
15905 /* Unregister the RPI when mailbox complete */
15906 mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
15907 restart_loop = 1;
15908 spin_unlock_irq(&phba->hbalock);
15909 spin_lock(shost->host_lock);
15910 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
15911 spin_unlock(shost->host_lock);
15912 spin_lock_irq(&phba->hbalock);
15913 break;
15914 }
15915 }
15916 } while (restart_loop);
15917
James Smartd439d282010-09-29 11:18:45 -040015918 spin_unlock_irq(&phba->hbalock);
15919
15920 /* Release the cleaned-up mailbox commands */
15921 while (!list_empty(&mbox_cmd_list)) {
15922 list_remove_head(&mbox_cmd_list, mb, LPFC_MBOXQ_t, list);
James Smart695a8142010-01-26 23:08:03 -050015923 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
15924 mp = (struct lpfc_dmabuf *) (mb->context1);
15925 if (mp) {
15926 __lpfc_mbuf_free(phba, mp->virt, mp->phys);
15927 kfree(mp);
15928 }
James Smart78730cf2010-04-06 15:06:30 -040015929 ndlp = (struct lpfc_nodelist *) mb->context2;
James Smartd439d282010-09-29 11:18:45 -040015930 mb->context2 = NULL;
James Smart78730cf2010-04-06 15:06:30 -040015931 if (ndlp) {
Dan Carpenterec21b3b2010-08-08 00:15:17 +020015932 spin_lock(shost->host_lock);
James Smart589a52d2010-07-14 15:30:54 -040015933 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
Dan Carpenterec21b3b2010-08-08 00:15:17 +020015934 spin_unlock(shost->host_lock);
James Smart78730cf2010-04-06 15:06:30 -040015935 lpfc_nlp_put(ndlp);
James Smart78730cf2010-04-06 15:06:30 -040015936 }
James Smart695a8142010-01-26 23:08:03 -050015937 }
James Smart695a8142010-01-26 23:08:03 -050015938 mempool_free(mb, phba->mbox_mem_pool);
15939 }
James Smartd439d282010-09-29 11:18:45 -040015940
15941 /* Release the ndlp with the cleaned-up active mailbox command */
15942 if (act_mbx_ndlp) {
15943 spin_lock(shost->host_lock);
15944 act_mbx_ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
15945 spin_unlock(shost->host_lock);
15946 lpfc_nlp_put(act_mbx_ndlp);
James Smart695a8142010-01-26 23:08:03 -050015947 }
James Smart695a8142010-01-26 23:08:03 -050015948}
15949
James Smart2a9bf3d2010-06-07 15:24:45 -040015950/**
15951 * lpfc_drain_txq - Drain the txq
15952 * @phba: Pointer to HBA context object.
15953 *
15954 * This function attempt to submit IOCBs on the txq
15955 * to the adapter. For SLI4 adapters, the txq contains
15956 * ELS IOCBs that have been deferred because the there
15957 * are no SGLs. This congestion can occur with large
15958 * vport counts during node discovery.
15959 **/
15960
15961uint32_t
15962lpfc_drain_txq(struct lpfc_hba *phba)
15963{
15964 LIST_HEAD(completions);
15965 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
15966 struct lpfc_iocbq *piocbq = 0;
15967 unsigned long iflags = 0;
15968 char *fail_msg = NULL;
15969 struct lpfc_sglq *sglq;
15970 union lpfc_wqe wqe;
15971
15972 spin_lock_irqsave(&phba->hbalock, iflags);
15973 if (pring->txq_cnt > pring->txq_max)
15974 pring->txq_max = pring->txq_cnt;
15975
15976 spin_unlock_irqrestore(&phba->hbalock, iflags);
15977
15978 while (pring->txq_cnt) {
15979 spin_lock_irqsave(&phba->hbalock, iflags);
15980
James Smart19ca7602010-11-20 23:11:55 -050015981 piocbq = lpfc_sli_ringtx_get(phba, pring);
James Smarta6298522012-06-12 13:54:11 -040015982 if (!piocbq) {
15983 spin_unlock_irqrestore(&phba->hbalock, iflags);
15984 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15985 "2823 txq empty and txq_cnt is %d\n ",
15986 pring->txq_cnt);
15987 break;
15988 }
James Smart19ca7602010-11-20 23:11:55 -050015989 sglq = __lpfc_sli_get_sglq(phba, piocbq);
James Smart2a9bf3d2010-06-07 15:24:45 -040015990 if (!sglq) {
James Smart19ca7602010-11-20 23:11:55 -050015991 __lpfc_sli_ringtx_put(phba, pring, piocbq);
James Smart2a9bf3d2010-06-07 15:24:45 -040015992 spin_unlock_irqrestore(&phba->hbalock, iflags);
15993 break;
James Smart2a9bf3d2010-06-07 15:24:45 -040015994 }
15995
15996 /* The xri and iocb resources secured,
15997 * attempt to issue request
15998 */
James Smart6d368e52011-05-24 11:44:12 -040015999 piocbq->sli4_lxritag = sglq->sli4_lxritag;
James Smart2a9bf3d2010-06-07 15:24:45 -040016000 piocbq->sli4_xritag = sglq->sli4_xritag;
16001 if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocbq, sglq))
16002 fail_msg = "to convert bpl to sgl";
16003 else if (lpfc_sli4_iocb2wqe(phba, piocbq, &wqe))
16004 fail_msg = "to convert iocb to wqe";
16005 else if (lpfc_sli4_wq_put(phba->sli4_hba.els_wq, &wqe))
16006 fail_msg = " - Wq is full";
16007 else
16008 lpfc_sli_ringtxcmpl_put(phba, pring, piocbq);
16009
16010 if (fail_msg) {
16011 /* Failed means we can't issue and need to cancel */
16012 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
16013 "2822 IOCB failed %s iotag 0x%x "
16014 "xri 0x%x\n",
16015 fail_msg,
16016 piocbq->iotag, piocbq->sli4_xritag);
16017 list_add_tail(&piocbq->list, &completions);
16018 }
16019 spin_unlock_irqrestore(&phba->hbalock, iflags);
16020 }
16021
James Smart2a9bf3d2010-06-07 15:24:45 -040016022 /* Cancel all the IOCBs that cannot be issued */
16023 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
16024 IOERR_SLI_ABORTED);
16025
16026 return pring->txq_cnt;
16027}