blob: 51c7fc746de1f5b53d55ba4ece1dc87efb1a1529 [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{
James Smart7e56aa22012-08-03 12:35:34 -0400478 return (IOCB_t *) (((char *) pring->sli.sli3.cmdringaddr) +
479 pring->sli.sli3.cmdidx * phba->iocb_cmd_size);
James Smarted957682007-06-17 19:56:37 -0500480}
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{
James Smart7e56aa22012-08-03 12:35:34 -0400495 return (IOCB_t *) (((char *) pring->sli.sli3.rspringaddr) +
496 pring->sli.sli3.rspidx * phba->iocb_rsp_size);
James Smarted957682007-06-17 19:56:37 -0500497}
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];
James Smart7e56aa22012-08-03 12:35:34 -04001326 uint32_t max_cmd_idx = pring->sli.sli3.numCiocb;
1327 if ((pring->sli.sli3.next_cmdidx == pring->sli.sli3.cmdidx) &&
1328 (++pring->sli.sli3.next_cmdidx >= max_cmd_idx))
1329 pring->sli.sli3.next_cmdidx = 0;
dea31012005-04-17 16:05:31 -05001330
James Smart7e56aa22012-08-03 12:35:34 -04001331 if (unlikely(pring->sli.sli3.local_getidx ==
1332 pring->sli.sli3.next_cmdidx)) {
dea31012005-04-17 16:05:31 -05001333
James Smart7e56aa22012-08-03 12:35:34 -04001334 pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
dea31012005-04-17 16:05:31 -05001335
James Smart7e56aa22012-08-03 12:35:34 -04001336 if (unlikely(pring->sli.sli3.local_getidx >= max_cmd_idx)) {
dea31012005-04-17 16:05:31 -05001337 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001338 "0315 Ring %d issue: portCmdGet %d "
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02001339 "is bigger than cmd ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04001340 pring->ringno,
James Smart7e56aa22012-08-03 12:35:34 -04001341 pring->sli.sli3.local_getidx,
1342 max_cmd_idx);
dea31012005-04-17 16:05:31 -05001343
James Smart2e0fef82007-06-17 19:56:36 -05001344 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05001345 /*
1346 * All error attention handlers are posted to
1347 * worker thread
1348 */
1349 phba->work_ha |= HA_ERATT;
1350 phba->work_hs = HS_FFER3;
James Smart92d7f7b2007-06-17 19:56:38 -05001351
James Smart5e9d9b82008-06-14 22:52:53 -04001352 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -05001353
1354 return NULL;
1355 }
1356
James Smart7e56aa22012-08-03 12:35:34 -04001357 if (pring->sli.sli3.local_getidx == pring->sli.sli3.next_cmdidx)
dea31012005-04-17 16:05:31 -05001358 return NULL;
1359 }
1360
James Smarted957682007-06-17 19:56:37 -05001361 return lpfc_cmd_iocb(phba, pring);
dea31012005-04-17 16:05:31 -05001362}
1363
James Smarte59058c2008-08-24 21:49:00 -04001364/**
James Smart3621a712009-04-06 18:47:14 -04001365 * lpfc_sli_next_iotag - Get an iotag for the iocb
James Smarte59058c2008-08-24 21:49:00 -04001366 * @phba: Pointer to HBA context object.
1367 * @iocbq: Pointer to driver iocb object.
1368 *
1369 * This function gets an iotag for the iocb. If there is no unused iotag and
1370 * the iocbq_lookup_len < 0xffff, this function allocates a bigger iotag_lookup
1371 * array and assigns a new iotag.
1372 * The function returns the allocated iotag if successful, else returns zero.
1373 * Zero is not a valid iotag.
1374 * The caller is not required to hold any lock.
1375 **/
James Bottomley604a3e32005-10-29 10:28:33 -05001376uint16_t
James Smart2e0fef82007-06-17 19:56:36 -05001377lpfc_sli_next_iotag(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
dea31012005-04-17 16:05:31 -05001378{
James Smart2e0fef82007-06-17 19:56:36 -05001379 struct lpfc_iocbq **new_arr;
1380 struct lpfc_iocbq **old_arr;
James Bottomley604a3e32005-10-29 10:28:33 -05001381 size_t new_len;
1382 struct lpfc_sli *psli = &phba->sli;
1383 uint16_t iotag;
dea31012005-04-17 16:05:31 -05001384
James Smart2e0fef82007-06-17 19:56:36 -05001385 spin_lock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001386 iotag = psli->last_iotag;
1387 if(++iotag < psli->iocbq_lookup_len) {
1388 psli->last_iotag = iotag;
1389 psli->iocbq_lookup[iotag] = iocbq;
James Smart2e0fef82007-06-17 19:56:36 -05001390 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001391 iocbq->iotag = iotag;
1392 return iotag;
James Smart2e0fef82007-06-17 19:56:36 -05001393 } else if (psli->iocbq_lookup_len < (0xffff
James Bottomley604a3e32005-10-29 10:28:33 -05001394 - LPFC_IOCBQ_LOOKUP_INCREMENT)) {
1395 new_len = psli->iocbq_lookup_len + LPFC_IOCBQ_LOOKUP_INCREMENT;
James Smart2e0fef82007-06-17 19:56:36 -05001396 spin_unlock_irq(&phba->hbalock);
1397 new_arr = kzalloc(new_len * sizeof (struct lpfc_iocbq *),
James Bottomley604a3e32005-10-29 10:28:33 -05001398 GFP_KERNEL);
1399 if (new_arr) {
James Smart2e0fef82007-06-17 19:56:36 -05001400 spin_lock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001401 old_arr = psli->iocbq_lookup;
1402 if (new_len <= psli->iocbq_lookup_len) {
1403 /* highly unprobable case */
1404 kfree(new_arr);
1405 iotag = psli->last_iotag;
1406 if(++iotag < psli->iocbq_lookup_len) {
1407 psli->last_iotag = iotag;
1408 psli->iocbq_lookup[iotag] = iocbq;
James Smart2e0fef82007-06-17 19:56:36 -05001409 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001410 iocbq->iotag = iotag;
1411 return iotag;
1412 }
James Smart2e0fef82007-06-17 19:56:36 -05001413 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001414 return 0;
1415 }
1416 if (psli->iocbq_lookup)
1417 memcpy(new_arr, old_arr,
1418 ((psli->last_iotag + 1) *
James Smart311464e2007-08-02 11:10:37 -04001419 sizeof (struct lpfc_iocbq *)));
James Bottomley604a3e32005-10-29 10:28:33 -05001420 psli->iocbq_lookup = new_arr;
1421 psli->iocbq_lookup_len = new_len;
1422 psli->last_iotag = iotag;
1423 psli->iocbq_lookup[iotag] = iocbq;
James Smart2e0fef82007-06-17 19:56:36 -05001424 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001425 iocbq->iotag = iotag;
1426 kfree(old_arr);
1427 return iotag;
1428 }
James Smart8f6d98d2006-08-01 07:34:00 -04001429 } else
James Smart2e0fef82007-06-17 19:56:36 -05001430 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001431
James Smartbc739052010-08-04 16:11:18 -04001432 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001433 "0318 Failed to allocate IOTAG.last IOTAG is %d\n",
1434 psli->last_iotag);
dea31012005-04-17 16:05:31 -05001435
James Bottomley604a3e32005-10-29 10:28:33 -05001436 return 0;
dea31012005-04-17 16:05:31 -05001437}
1438
James Smarte59058c2008-08-24 21:49:00 -04001439/**
James Smart3621a712009-04-06 18:47:14 -04001440 * lpfc_sli_submit_iocb - Submit an iocb to the firmware
James Smarte59058c2008-08-24 21:49:00 -04001441 * @phba: Pointer to HBA context object.
1442 * @pring: Pointer to driver SLI ring object.
1443 * @iocb: Pointer to iocb slot in the ring.
1444 * @nextiocb: Pointer to driver iocb object which need to be
1445 * posted to firmware.
1446 *
1447 * This function is called with hbalock held to post a new iocb to
1448 * the firmware. This function copies the new iocb to ring iocb slot and
1449 * updates the ring pointers. It adds the new iocb to txcmplq if there is
1450 * a completion call back for this iocb else the function will free the
1451 * iocb object.
1452 **/
dea31012005-04-17 16:05:31 -05001453static void
1454lpfc_sli_submit_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1455 IOCB_t *iocb, struct lpfc_iocbq *nextiocb)
1456{
1457 /*
James Bottomley604a3e32005-10-29 10:28:33 -05001458 * Set up an iotag
dea31012005-04-17 16:05:31 -05001459 */
James Bottomley604a3e32005-10-29 10:28:33 -05001460 nextiocb->iocb.ulpIoTag = (nextiocb->iocb_cmpl) ? nextiocb->iotag : 0;
dea31012005-04-17 16:05:31 -05001461
James Smarte2a0a9d2008-12-04 22:40:02 -05001462
James Smarta58cbd52007-08-02 11:09:43 -04001463 if (pring->ringno == LPFC_ELS_RING) {
1464 lpfc_debugfs_slow_ring_trc(phba,
1465 "IOCB cmd ring: wd4:x%08x wd6:x%08x wd7:x%08x",
1466 *(((uint32_t *) &nextiocb->iocb) + 4),
1467 *(((uint32_t *) &nextiocb->iocb) + 6),
1468 *(((uint32_t *) &nextiocb->iocb) + 7));
1469 }
1470
dea31012005-04-17 16:05:31 -05001471 /*
1472 * Issue iocb command to adapter
1473 */
James Smart92d7f7b2007-06-17 19:56:38 -05001474 lpfc_sli_pcimem_bcopy(&nextiocb->iocb, iocb, phba->iocb_cmd_size);
dea31012005-04-17 16:05:31 -05001475 wmb();
1476 pring->stats.iocb_cmd++;
1477
1478 /*
1479 * If there is no completion routine to call, we can release the
1480 * IOCB buffer back right now. For IOCBs, like QUE_RING_BUF,
1481 * that have no rsp ring completion, iocb_cmpl MUST be NULL.
1482 */
1483 if (nextiocb->iocb_cmpl)
1484 lpfc_sli_ringtxcmpl_put(phba, pring, nextiocb);
James Bottomley604a3e32005-10-29 10:28:33 -05001485 else
James Smart2e0fef82007-06-17 19:56:36 -05001486 __lpfc_sli_release_iocbq(phba, nextiocb);
dea31012005-04-17 16:05:31 -05001487
1488 /*
1489 * Let the HBA know what IOCB slot will be the next one the
1490 * driver will put a command into.
1491 */
James Smart7e56aa22012-08-03 12:35:34 -04001492 pring->sli.sli3.cmdidx = pring->sli.sli3.next_cmdidx;
1493 writel(pring->sli.sli3.cmdidx, &phba->host_gp[pring->ringno].cmdPutInx);
dea31012005-04-17 16:05:31 -05001494}
1495
James Smarte59058c2008-08-24 21:49:00 -04001496/**
James Smart3621a712009-04-06 18:47:14 -04001497 * lpfc_sli_update_full_ring - Update the chip attention register
James Smarte59058c2008-08-24 21:49:00 -04001498 * @phba: Pointer to HBA context object.
1499 * @pring: Pointer to driver SLI ring object.
1500 *
1501 * The caller is not required to hold any lock for calling this function.
1502 * This function updates the chip attention bits for the ring to inform firmware
1503 * that there are pending work to be done for this ring and requests an
1504 * interrupt when there is space available in the ring. This function is
1505 * called when the driver is unable to post more iocbs to the ring due
1506 * to unavailability of space in the ring.
1507 **/
dea31012005-04-17 16:05:31 -05001508static void
James Smart2e0fef82007-06-17 19:56:36 -05001509lpfc_sli_update_full_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -05001510{
1511 int ringno = pring->ringno;
1512
1513 pring->flag |= LPFC_CALL_RING_AVAILABLE;
1514
1515 wmb();
1516
1517 /*
1518 * Set ring 'ringno' to SET R0CE_REQ in Chip Att register.
1519 * The HBA will tell us when an IOCB entry is available.
1520 */
1521 writel((CA_R0ATT|CA_R0CE_REQ) << (ringno*4), phba->CAregaddr);
1522 readl(phba->CAregaddr); /* flush */
1523
1524 pring->stats.iocb_cmd_full++;
1525}
1526
James Smarte59058c2008-08-24 21:49:00 -04001527/**
James Smart3621a712009-04-06 18:47:14 -04001528 * lpfc_sli_update_ring - Update chip attention register
James Smarte59058c2008-08-24 21:49:00 -04001529 * @phba: Pointer to HBA context object.
1530 * @pring: Pointer to driver SLI ring object.
1531 *
1532 * This function updates the chip attention register bit for the
1533 * given ring to inform HBA that there is more work to be done
1534 * in this ring. The caller is not required to hold any lock.
1535 **/
dea31012005-04-17 16:05:31 -05001536static void
James Smart2e0fef82007-06-17 19:56:36 -05001537lpfc_sli_update_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -05001538{
1539 int ringno = pring->ringno;
1540
1541 /*
1542 * Tell the HBA that there is work to do in this ring.
1543 */
James Smart34b02dc2008-08-24 21:49:55 -04001544 if (!(phba->sli3_options & LPFC_SLI3_CRP_ENABLED)) {
1545 wmb();
1546 writel(CA_R0ATT << (ringno * 4), phba->CAregaddr);
1547 readl(phba->CAregaddr); /* flush */
1548 }
dea31012005-04-17 16:05:31 -05001549}
1550
James Smarte59058c2008-08-24 21:49:00 -04001551/**
James Smart3621a712009-04-06 18:47:14 -04001552 * lpfc_sli_resume_iocb - Process iocbs in the txq
James Smarte59058c2008-08-24 21:49:00 -04001553 * @phba: Pointer to HBA context object.
1554 * @pring: Pointer to driver SLI ring object.
1555 *
1556 * This function is called with hbalock held to post pending iocbs
1557 * in the txq to the firmware. This function is called when driver
1558 * detects space available in the ring.
1559 **/
dea31012005-04-17 16:05:31 -05001560static void
James Smart2e0fef82007-06-17 19:56:36 -05001561lpfc_sli_resume_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -05001562{
1563 IOCB_t *iocb;
1564 struct lpfc_iocbq *nextiocb;
1565
1566 /*
1567 * Check to see if:
1568 * (a) there is anything on the txq to send
1569 * (b) link is up
1570 * (c) link attention events can be processed (fcp ring only)
1571 * (d) IOCB processing is not blocked by the outstanding mbox command.
1572 */
1573 if (pring->txq_cnt &&
James Smart2e0fef82007-06-17 19:56:36 -05001574 lpfc_is_link_up(phba) &&
dea31012005-04-17 16:05:31 -05001575 (pring->ringno != phba->sli.fcp_ring ||
James Smart0b727fe2007-10-27 13:37:25 -04001576 phba->sli.sli_flag & LPFC_PROCESS_LA)) {
dea31012005-04-17 16:05:31 -05001577
1578 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
1579 (nextiocb = lpfc_sli_ringtx_get(phba, pring)))
1580 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
1581
1582 if (iocb)
1583 lpfc_sli_update_ring(phba, pring);
1584 else
1585 lpfc_sli_update_full_ring(phba, pring);
1586 }
1587
1588 return;
1589}
1590
James Smarte59058c2008-08-24 21:49:00 -04001591/**
James Smart3621a712009-04-06 18:47:14 -04001592 * lpfc_sli_next_hbq_slot - Get next hbq entry for the HBQ
James Smarte59058c2008-08-24 21:49:00 -04001593 * @phba: Pointer to HBA context object.
1594 * @hbqno: HBQ number.
1595 *
1596 * This function is called with hbalock held to get the next
1597 * available slot for the given HBQ. If there is free slot
1598 * available for the HBQ it will return pointer to the next available
1599 * HBQ entry else it will return NULL.
1600 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01001601static struct lpfc_hbq_entry *
James Smarted957682007-06-17 19:56:37 -05001602lpfc_sli_next_hbq_slot(struct lpfc_hba *phba, uint32_t hbqno)
1603{
1604 struct hbq_s *hbqp = &phba->hbqs[hbqno];
1605
1606 if (hbqp->next_hbqPutIdx == hbqp->hbqPutIdx &&
1607 ++hbqp->next_hbqPutIdx >= hbqp->entry_count)
1608 hbqp->next_hbqPutIdx = 0;
1609
1610 if (unlikely(hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)) {
James Smart92d7f7b2007-06-17 19:56:38 -05001611 uint32_t raw_index = phba->hbq_get[hbqno];
James Smarted957682007-06-17 19:56:37 -05001612 uint32_t getidx = le32_to_cpu(raw_index);
1613
1614 hbqp->local_hbqGetIdx = getidx;
1615
1616 if (unlikely(hbqp->local_hbqGetIdx >= hbqp->entry_count)) {
1617 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -05001618 LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04001619 "1802 HBQ %d: local_hbqGetIdx "
James Smarted957682007-06-17 19:56:37 -05001620 "%u is > than hbqp->entry_count %u\n",
James Smarte8b62012007-08-02 11:10:09 -04001621 hbqno, hbqp->local_hbqGetIdx,
James Smarted957682007-06-17 19:56:37 -05001622 hbqp->entry_count);
1623
1624 phba->link_state = LPFC_HBA_ERROR;
1625 return NULL;
1626 }
1627
1628 if (hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)
1629 return NULL;
1630 }
1631
James Smart51ef4c22007-08-02 11:10:31 -04001632 return (struct lpfc_hbq_entry *) phba->hbqs[hbqno].hbq_virt +
1633 hbqp->hbqPutIdx;
James Smarted957682007-06-17 19:56:37 -05001634}
1635
James Smarte59058c2008-08-24 21:49:00 -04001636/**
James Smart3621a712009-04-06 18:47:14 -04001637 * lpfc_sli_hbqbuf_free_all - Free all the hbq buffers
James Smarte59058c2008-08-24 21:49:00 -04001638 * @phba: Pointer to HBA context object.
1639 *
1640 * This function is called with no lock held to free all the
1641 * hbq buffers while uninitializing the SLI interface. It also
1642 * frees the HBQ buffers returned by the firmware but not yet
1643 * processed by the upper layers.
1644 **/
James Smarted957682007-06-17 19:56:37 -05001645void
1646lpfc_sli_hbqbuf_free_all(struct lpfc_hba *phba)
1647{
James Smart92d7f7b2007-06-17 19:56:38 -05001648 struct lpfc_dmabuf *dmabuf, *next_dmabuf;
1649 struct hbq_dmabuf *hbq_buf;
James Smart3163f722008-02-08 18:50:25 -05001650 unsigned long flags;
James Smart51ef4c22007-08-02 11:10:31 -04001651 int i, hbq_count;
James Smart3163f722008-02-08 18:50:25 -05001652 uint32_t hbqno;
James Smarted957682007-06-17 19:56:37 -05001653
James Smart51ef4c22007-08-02 11:10:31 -04001654 hbq_count = lpfc_sli_hbq_count();
James Smarted957682007-06-17 19:56:37 -05001655 /* Return all memory used by all HBQs */
James Smart3163f722008-02-08 18:50:25 -05001656 spin_lock_irqsave(&phba->hbalock, flags);
James Smart51ef4c22007-08-02 11:10:31 -04001657 for (i = 0; i < hbq_count; ++i) {
1658 list_for_each_entry_safe(dmabuf, next_dmabuf,
1659 &phba->hbqs[i].hbq_buffer_list, list) {
1660 hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
1661 list_del(&hbq_buf->dbuf.list);
1662 (phba->hbqs[i].hbq_free_buffer)(phba, hbq_buf);
1663 }
James Smarta8adb832007-10-27 13:37:53 -04001664 phba->hbqs[i].buffer_count = 0;
James Smarted957682007-06-17 19:56:37 -05001665 }
James Smart3163f722008-02-08 18:50:25 -05001666 /* Return all HBQ buffer that are in-fly */
James Smart3772a992009-05-22 14:50:54 -04001667 list_for_each_entry_safe(dmabuf, next_dmabuf, &phba->rb_pend_list,
1668 list) {
James Smart3163f722008-02-08 18:50:25 -05001669 hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
1670 list_del(&hbq_buf->dbuf.list);
1671 if (hbq_buf->tag == -1) {
1672 (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
1673 (phba, hbq_buf);
1674 } else {
1675 hbqno = hbq_buf->tag >> 16;
1676 if (hbqno >= LPFC_MAX_HBQS)
1677 (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
1678 (phba, hbq_buf);
1679 else
1680 (phba->hbqs[hbqno].hbq_free_buffer)(phba,
1681 hbq_buf);
1682 }
1683 }
1684
1685 /* Mark the HBQs not in use */
1686 phba->hbq_in_use = 0;
1687 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smarted957682007-06-17 19:56:37 -05001688}
1689
James Smarte59058c2008-08-24 21:49:00 -04001690/**
James Smart3621a712009-04-06 18:47:14 -04001691 * lpfc_sli_hbq_to_firmware - Post the hbq buffer to firmware
James Smarte59058c2008-08-24 21:49:00 -04001692 * @phba: Pointer to HBA context object.
1693 * @hbqno: HBQ number.
1694 * @hbq_buf: Pointer to HBQ buffer.
1695 *
1696 * This function is called with the hbalock held to post a
1697 * hbq buffer to the firmware. If the function finds an empty
1698 * slot in the HBQ, it will post the buffer. The function will return
1699 * pointer to the hbq entry if it successfully post the buffer
1700 * else it will return NULL.
1701 **/
James Smart3772a992009-05-22 14:50:54 -04001702static int
James Smarted957682007-06-17 19:56:37 -05001703lpfc_sli_hbq_to_firmware(struct lpfc_hba *phba, uint32_t hbqno,
James Smart92d7f7b2007-06-17 19:56:38 -05001704 struct hbq_dmabuf *hbq_buf)
James Smarted957682007-06-17 19:56:37 -05001705{
James Smart3772a992009-05-22 14:50:54 -04001706 return phba->lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buf);
1707}
1708
1709/**
1710 * lpfc_sli_hbq_to_firmware_s3 - Post the hbq buffer to SLI3 firmware
1711 * @phba: Pointer to HBA context object.
1712 * @hbqno: HBQ number.
1713 * @hbq_buf: Pointer to HBQ buffer.
1714 *
1715 * This function is called with the hbalock held to post a hbq buffer to the
1716 * firmware. If the function finds an empty slot in the HBQ, it will post the
1717 * buffer and place it on the hbq_buffer_list. The function will return zero if
1718 * it successfully post the buffer else it will return an error.
1719 **/
1720static int
1721lpfc_sli_hbq_to_firmware_s3(struct lpfc_hba *phba, uint32_t hbqno,
1722 struct hbq_dmabuf *hbq_buf)
1723{
James Smarted957682007-06-17 19:56:37 -05001724 struct lpfc_hbq_entry *hbqe;
James Smart92d7f7b2007-06-17 19:56:38 -05001725 dma_addr_t physaddr = hbq_buf->dbuf.phys;
James Smarted957682007-06-17 19:56:37 -05001726
1727 /* Get next HBQ entry slot to use */
1728 hbqe = lpfc_sli_next_hbq_slot(phba, hbqno);
1729 if (hbqe) {
1730 struct hbq_s *hbqp = &phba->hbqs[hbqno];
1731
James Smart92d7f7b2007-06-17 19:56:38 -05001732 hbqe->bde.addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
1733 hbqe->bde.addrLow = le32_to_cpu(putPaddrLow(physaddr));
James Smart51ef4c22007-08-02 11:10:31 -04001734 hbqe->bde.tus.f.bdeSize = hbq_buf->size;
James Smarted957682007-06-17 19:56:37 -05001735 hbqe->bde.tus.f.bdeFlags = 0;
James Smart92d7f7b2007-06-17 19:56:38 -05001736 hbqe->bde.tus.w = le32_to_cpu(hbqe->bde.tus.w);
1737 hbqe->buffer_tag = le32_to_cpu(hbq_buf->tag);
1738 /* Sync SLIM */
James Smarted957682007-06-17 19:56:37 -05001739 hbqp->hbqPutIdx = hbqp->next_hbqPutIdx;
1740 writel(hbqp->hbqPutIdx, phba->hbq_put + hbqno);
James Smart92d7f7b2007-06-17 19:56:38 -05001741 /* flush */
James Smarted957682007-06-17 19:56:37 -05001742 readl(phba->hbq_put + hbqno);
James Smart51ef4c22007-08-02 11:10:31 -04001743 list_add_tail(&hbq_buf->dbuf.list, &hbqp->hbq_buffer_list);
James Smart3772a992009-05-22 14:50:54 -04001744 return 0;
1745 } else
1746 return -ENOMEM;
James Smarted957682007-06-17 19:56:37 -05001747}
1748
James Smart4f774512009-05-22 14:52:35 -04001749/**
1750 * lpfc_sli_hbq_to_firmware_s4 - Post the hbq buffer to SLI4 firmware
1751 * @phba: Pointer to HBA context object.
1752 * @hbqno: HBQ number.
1753 * @hbq_buf: Pointer to HBQ buffer.
1754 *
1755 * This function is called with the hbalock held to post an RQE to the SLI4
1756 * firmware. If able to post the RQE to the RQ it will queue the hbq entry to
1757 * the hbq_buffer_list and return zero, otherwise it will return an error.
1758 **/
1759static int
1760lpfc_sli_hbq_to_firmware_s4(struct lpfc_hba *phba, uint32_t hbqno,
1761 struct hbq_dmabuf *hbq_buf)
1762{
1763 int rc;
1764 struct lpfc_rqe hrqe;
1765 struct lpfc_rqe drqe;
1766
1767 hrqe.address_lo = putPaddrLow(hbq_buf->hbuf.phys);
1768 hrqe.address_hi = putPaddrHigh(hbq_buf->hbuf.phys);
1769 drqe.address_lo = putPaddrLow(hbq_buf->dbuf.phys);
1770 drqe.address_hi = putPaddrHigh(hbq_buf->dbuf.phys);
1771 rc = lpfc_sli4_rq_put(phba->sli4_hba.hdr_rq, phba->sli4_hba.dat_rq,
1772 &hrqe, &drqe);
1773 if (rc < 0)
1774 return rc;
1775 hbq_buf->tag = rc;
1776 list_add_tail(&hbq_buf->dbuf.list, &phba->hbqs[hbqno].hbq_buffer_list);
1777 return 0;
1778}
1779
James Smarte59058c2008-08-24 21:49:00 -04001780/* HBQ for ELS and CT traffic. */
James Smart92d7f7b2007-06-17 19:56:38 -05001781static struct lpfc_hbq_init lpfc_els_hbq = {
1782 .rn = 1,
James Smartdef9c7a2009-12-21 17:02:28 -05001783 .entry_count = 256,
James Smart92d7f7b2007-06-17 19:56:38 -05001784 .mask_count = 0,
1785 .profile = 0,
James Smart51ef4c22007-08-02 11:10:31 -04001786 .ring_mask = (1 << LPFC_ELS_RING),
James Smart92d7f7b2007-06-17 19:56:38 -05001787 .buffer_count = 0,
James Smarta257bf92009-04-06 18:48:10 -04001788 .init_count = 40,
1789 .add_count = 40,
James Smart92d7f7b2007-06-17 19:56:38 -05001790};
James Smarted957682007-06-17 19:56:37 -05001791
James Smarte59058c2008-08-24 21:49:00 -04001792/* HBQ for the extra ring if needed */
James Smart51ef4c22007-08-02 11:10:31 -04001793static struct lpfc_hbq_init lpfc_extra_hbq = {
1794 .rn = 1,
1795 .entry_count = 200,
1796 .mask_count = 0,
1797 .profile = 0,
1798 .ring_mask = (1 << LPFC_EXTRA_RING),
1799 .buffer_count = 0,
1800 .init_count = 0,
1801 .add_count = 5,
1802};
1803
James Smarte59058c2008-08-24 21:49:00 -04001804/* Array of HBQs */
James Smart78b2d852007-08-02 11:10:21 -04001805struct lpfc_hbq_init *lpfc_hbq_defs[] = {
James Smart92d7f7b2007-06-17 19:56:38 -05001806 &lpfc_els_hbq,
James Smart51ef4c22007-08-02 11:10:31 -04001807 &lpfc_extra_hbq,
James Smart92d7f7b2007-06-17 19:56:38 -05001808};
1809
James Smarte59058c2008-08-24 21:49:00 -04001810/**
James Smart3621a712009-04-06 18:47:14 -04001811 * lpfc_sli_hbqbuf_fill_hbqs - Post more hbq buffers to HBQ
James Smarte59058c2008-08-24 21:49:00 -04001812 * @phba: Pointer to HBA context object.
1813 * @hbqno: HBQ number.
1814 * @count: Number of HBQ buffers to be posted.
1815 *
James Smartd7c255b2008-08-24 21:50:00 -04001816 * This function is called with no lock held to post more hbq buffers to the
1817 * given HBQ. The function returns the number of HBQ buffers successfully
1818 * posted.
James Smarte59058c2008-08-24 21:49:00 -04001819 **/
James Smart311464e2007-08-02 11:10:37 -04001820static int
James Smart92d7f7b2007-06-17 19:56:38 -05001821lpfc_sli_hbqbuf_fill_hbqs(struct lpfc_hba *phba, uint32_t hbqno, uint32_t count)
1822{
James Smartd7c255b2008-08-24 21:50:00 -04001823 uint32_t i, posted = 0;
James Smart3163f722008-02-08 18:50:25 -05001824 unsigned long flags;
James Smart92d7f7b2007-06-17 19:56:38 -05001825 struct hbq_dmabuf *hbq_buffer;
James Smartd7c255b2008-08-24 21:50:00 -04001826 LIST_HEAD(hbq_buf_list);
Matthew Wilcoxeafe1df2008-02-21 05:44:33 -07001827 if (!phba->hbqs[hbqno].hbq_alloc_buffer)
James Smart51ef4c22007-08-02 11:10:31 -04001828 return 0;
James Smart51ef4c22007-08-02 11:10:31 -04001829
James Smartd7c255b2008-08-24 21:50:00 -04001830 if ((phba->hbqs[hbqno].buffer_count + count) >
1831 lpfc_hbq_defs[hbqno]->entry_count)
1832 count = lpfc_hbq_defs[hbqno]->entry_count -
1833 phba->hbqs[hbqno].buffer_count;
1834 if (!count)
1835 return 0;
1836 /* Allocate HBQ entries */
1837 for (i = 0; i < count; i++) {
1838 hbq_buffer = (phba->hbqs[hbqno].hbq_alloc_buffer)(phba);
1839 if (!hbq_buffer)
1840 break;
1841 list_add_tail(&hbq_buffer->dbuf.list, &hbq_buf_list);
1842 }
James Smart3163f722008-02-08 18:50:25 -05001843 /* Check whether HBQ is still in use */
1844 spin_lock_irqsave(&phba->hbalock, flags);
Matthew Wilcoxeafe1df2008-02-21 05:44:33 -07001845 if (!phba->hbq_in_use)
James Smartd7c255b2008-08-24 21:50:00 -04001846 goto err;
1847 while (!list_empty(&hbq_buf_list)) {
1848 list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
1849 dbuf.list);
1850 hbq_buffer->tag = (phba->hbqs[hbqno].buffer_count |
1851 (hbqno << 16));
James Smart3772a992009-05-22 14:50:54 -04001852 if (!lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer)) {
James Smarta8adb832007-10-27 13:37:53 -04001853 phba->hbqs[hbqno].buffer_count++;
James Smartd7c255b2008-08-24 21:50:00 -04001854 posted++;
1855 } else
James Smart51ef4c22007-08-02 11:10:31 -04001856 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
James Smart92d7f7b2007-06-17 19:56:38 -05001857 }
James Smart3163f722008-02-08 18:50:25 -05001858 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smartd7c255b2008-08-24 21:50:00 -04001859 return posted;
1860err:
1861 spin_unlock_irqrestore(&phba->hbalock, flags);
1862 while (!list_empty(&hbq_buf_list)) {
1863 list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
1864 dbuf.list);
1865 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
1866 }
James Smart92d7f7b2007-06-17 19:56:38 -05001867 return 0;
James Smarted957682007-06-17 19:56:37 -05001868}
1869
James Smarte59058c2008-08-24 21:49:00 -04001870/**
James Smart3621a712009-04-06 18:47:14 -04001871 * lpfc_sli_hbqbuf_add_hbqs - Post more HBQ buffers to firmware
James Smarte59058c2008-08-24 21:49:00 -04001872 * @phba: Pointer to HBA context object.
1873 * @qno: HBQ number.
1874 *
1875 * This function posts more buffers to the HBQ. This function
James Smartd7c255b2008-08-24 21:50:00 -04001876 * is called with no lock held. The function returns the number of HBQ entries
1877 * successfully allocated.
James Smarte59058c2008-08-24 21:49:00 -04001878 **/
James Smarted957682007-06-17 19:56:37 -05001879int
James Smart92d7f7b2007-06-17 19:56:38 -05001880lpfc_sli_hbqbuf_add_hbqs(struct lpfc_hba *phba, uint32_t qno)
James Smarted957682007-06-17 19:56:37 -05001881{
James Smartdef9c7a2009-12-21 17:02:28 -05001882 if (phba->sli_rev == LPFC_SLI_REV4)
1883 return 0;
1884 else
1885 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
1886 lpfc_hbq_defs[qno]->add_count);
James Smarted957682007-06-17 19:56:37 -05001887}
1888
James Smarte59058c2008-08-24 21:49:00 -04001889/**
James Smart3621a712009-04-06 18:47:14 -04001890 * lpfc_sli_hbqbuf_init_hbqs - Post initial buffers to the HBQ
James Smarte59058c2008-08-24 21:49:00 -04001891 * @phba: Pointer to HBA context object.
1892 * @qno: HBQ queue number.
1893 *
1894 * This function is called from SLI initialization code path with
1895 * no lock held to post initial HBQ buffers to firmware. The
James Smartd7c255b2008-08-24 21:50:00 -04001896 * function returns the number of HBQ entries successfully allocated.
James Smarte59058c2008-08-24 21:49:00 -04001897 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01001898static int
James Smart92d7f7b2007-06-17 19:56:38 -05001899lpfc_sli_hbqbuf_init_hbqs(struct lpfc_hba *phba, uint32_t qno)
James Smarted957682007-06-17 19:56:37 -05001900{
James Smartdef9c7a2009-12-21 17:02:28 -05001901 if (phba->sli_rev == LPFC_SLI_REV4)
1902 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
James Smart73d91e52011-10-10 21:32:10 -04001903 lpfc_hbq_defs[qno]->entry_count);
James Smartdef9c7a2009-12-21 17:02:28 -05001904 else
1905 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
1906 lpfc_hbq_defs[qno]->init_count);
James Smarted957682007-06-17 19:56:37 -05001907}
1908
James Smarte59058c2008-08-24 21:49:00 -04001909/**
James Smart3772a992009-05-22 14:50:54 -04001910 * lpfc_sli_hbqbuf_get - Remove the first hbq off of an hbq list
1911 * @phba: Pointer to HBA context object.
1912 * @hbqno: HBQ number.
1913 *
1914 * This function removes the first hbq buffer on an hbq list and returns a
1915 * pointer to that buffer. If it finds no buffers on the list it returns NULL.
1916 **/
1917static struct hbq_dmabuf *
1918lpfc_sli_hbqbuf_get(struct list_head *rb_list)
1919{
1920 struct lpfc_dmabuf *d_buf;
1921
1922 list_remove_head(rb_list, d_buf, struct lpfc_dmabuf, list);
1923 if (!d_buf)
1924 return NULL;
1925 return container_of(d_buf, struct hbq_dmabuf, dbuf);
1926}
1927
1928/**
James Smart3621a712009-04-06 18:47:14 -04001929 * lpfc_sli_hbqbuf_find - Find the hbq buffer associated with a tag
James Smarte59058c2008-08-24 21:49:00 -04001930 * @phba: Pointer to HBA context object.
1931 * @tag: Tag of the hbq buffer.
1932 *
1933 * This function is called with hbalock held. This function searches
1934 * for the hbq buffer associated with the given tag in the hbq buffer
1935 * list. If it finds the hbq buffer, it returns the hbq_buffer other wise
1936 * it returns NULL.
1937 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01001938static struct hbq_dmabuf *
James Smarted957682007-06-17 19:56:37 -05001939lpfc_sli_hbqbuf_find(struct lpfc_hba *phba, uint32_t tag)
1940{
James Smart92d7f7b2007-06-17 19:56:38 -05001941 struct lpfc_dmabuf *d_buf;
1942 struct hbq_dmabuf *hbq_buf;
James Smart51ef4c22007-08-02 11:10:31 -04001943 uint32_t hbqno;
James Smarted957682007-06-17 19:56:37 -05001944
James Smart51ef4c22007-08-02 11:10:31 -04001945 hbqno = tag >> 16;
Jesper Juhla0a74e452007-08-09 20:47:15 +02001946 if (hbqno >= LPFC_MAX_HBQS)
James Smart51ef4c22007-08-02 11:10:31 -04001947 return NULL;
1948
James Smart3772a992009-05-22 14:50:54 -04001949 spin_lock_irq(&phba->hbalock);
James Smart51ef4c22007-08-02 11:10:31 -04001950 list_for_each_entry(d_buf, &phba->hbqs[hbqno].hbq_buffer_list, list) {
James Smart92d7f7b2007-06-17 19:56:38 -05001951 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
James Smart51ef4c22007-08-02 11:10:31 -04001952 if (hbq_buf->tag == tag) {
James Smart3772a992009-05-22 14:50:54 -04001953 spin_unlock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -05001954 return hbq_buf;
James Smarted957682007-06-17 19:56:37 -05001955 }
1956 }
James Smart3772a992009-05-22 14:50:54 -04001957 spin_unlock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -05001958 lpfc_printf_log(phba, KERN_ERR, LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04001959 "1803 Bad hbq tag. Data: x%x x%x\n",
James Smarta8adb832007-10-27 13:37:53 -04001960 tag, phba->hbqs[tag >> 16].buffer_count);
James Smart92d7f7b2007-06-17 19:56:38 -05001961 return NULL;
James Smarted957682007-06-17 19:56:37 -05001962}
1963
James Smarte59058c2008-08-24 21:49:00 -04001964/**
James Smart3621a712009-04-06 18:47:14 -04001965 * lpfc_sli_free_hbq - Give back the hbq buffer to firmware
James Smarte59058c2008-08-24 21:49:00 -04001966 * @phba: Pointer to HBA context object.
1967 * @hbq_buffer: Pointer to HBQ buffer.
1968 *
1969 * This function is called with hbalock. This function gives back
1970 * the hbq buffer to firmware. If the HBQ does not have space to
1971 * post the buffer, it will free the buffer.
1972 **/
James Smarted957682007-06-17 19:56:37 -05001973void
James Smart51ef4c22007-08-02 11:10:31 -04001974lpfc_sli_free_hbq(struct lpfc_hba *phba, struct hbq_dmabuf *hbq_buffer)
James Smarted957682007-06-17 19:56:37 -05001975{
1976 uint32_t hbqno;
1977
James Smart51ef4c22007-08-02 11:10:31 -04001978 if (hbq_buffer) {
1979 hbqno = hbq_buffer->tag >> 16;
James Smart3772a992009-05-22 14:50:54 -04001980 if (lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer))
James Smart51ef4c22007-08-02 11:10:31 -04001981 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
James Smarted957682007-06-17 19:56:37 -05001982 }
1983}
1984
James Smarte59058c2008-08-24 21:49:00 -04001985/**
James Smart3621a712009-04-06 18:47:14 -04001986 * lpfc_sli_chk_mbx_command - Check if the mailbox is a legitimate mailbox
James Smarte59058c2008-08-24 21:49:00 -04001987 * @mbxCommand: mailbox command code.
1988 *
1989 * This function is called by the mailbox event handler function to verify
1990 * that the completed mailbox command is a legitimate mailbox command. If the
1991 * completed mailbox is not known to the function, it will return MBX_SHUTDOWN
1992 * and the mailbox event handler will take the HBA offline.
1993 **/
dea31012005-04-17 16:05:31 -05001994static int
1995lpfc_sli_chk_mbx_command(uint8_t mbxCommand)
1996{
1997 uint8_t ret;
1998
1999 switch (mbxCommand) {
2000 case MBX_LOAD_SM:
2001 case MBX_READ_NV:
2002 case MBX_WRITE_NV:
James Smarta8adb832007-10-27 13:37:53 -04002003 case MBX_WRITE_VPARMS:
dea31012005-04-17 16:05:31 -05002004 case MBX_RUN_BIU_DIAG:
2005 case MBX_INIT_LINK:
2006 case MBX_DOWN_LINK:
2007 case MBX_CONFIG_LINK:
2008 case MBX_CONFIG_RING:
2009 case MBX_RESET_RING:
2010 case MBX_READ_CONFIG:
2011 case MBX_READ_RCONFIG:
2012 case MBX_READ_SPARM:
2013 case MBX_READ_STATUS:
2014 case MBX_READ_RPI:
2015 case MBX_READ_XRI:
2016 case MBX_READ_REV:
2017 case MBX_READ_LNK_STAT:
2018 case MBX_REG_LOGIN:
2019 case MBX_UNREG_LOGIN:
dea31012005-04-17 16:05:31 -05002020 case MBX_CLEAR_LA:
2021 case MBX_DUMP_MEMORY:
2022 case MBX_DUMP_CONTEXT:
2023 case MBX_RUN_DIAGS:
2024 case MBX_RESTART:
2025 case MBX_UPDATE_CFG:
2026 case MBX_DOWN_LOAD:
2027 case MBX_DEL_LD_ENTRY:
2028 case MBX_RUN_PROGRAM:
2029 case MBX_SET_MASK:
James Smart09372822008-01-11 01:52:54 -05002030 case MBX_SET_VARIABLE:
dea31012005-04-17 16:05:31 -05002031 case MBX_UNREG_D_ID:
Jamie Wellnitz41415862006-02-28 19:25:27 -05002032 case MBX_KILL_BOARD:
dea31012005-04-17 16:05:31 -05002033 case MBX_CONFIG_FARP:
Jamie Wellnitz41415862006-02-28 19:25:27 -05002034 case MBX_BEACON:
dea31012005-04-17 16:05:31 -05002035 case MBX_LOAD_AREA:
2036 case MBX_RUN_BIU_DIAG64:
2037 case MBX_CONFIG_PORT:
2038 case MBX_READ_SPARM64:
2039 case MBX_READ_RPI64:
2040 case MBX_REG_LOGIN64:
James Smart76a95d72010-11-20 23:11:48 -05002041 case MBX_READ_TOPOLOGY:
James Smart09372822008-01-11 01:52:54 -05002042 case MBX_WRITE_WWN:
dea31012005-04-17 16:05:31 -05002043 case MBX_SET_DEBUG:
2044 case MBX_LOAD_EXP_ROM:
James Smart57127f12007-10-27 13:37:05 -04002045 case MBX_ASYNCEVT_ENABLE:
James Smart92d7f7b2007-06-17 19:56:38 -05002046 case MBX_REG_VPI:
2047 case MBX_UNREG_VPI:
James Smart858c9f62007-06-17 19:56:39 -05002048 case MBX_HEARTBEAT:
James Smart84774a42008-08-24 21:50:06 -04002049 case MBX_PORT_CAPABILITIES:
2050 case MBX_PORT_IOV_CONTROL:
James Smart04c68492009-05-22 14:52:52 -04002051 case MBX_SLI4_CONFIG:
2052 case MBX_SLI4_REQ_FTRS:
2053 case MBX_REG_FCFI:
2054 case MBX_UNREG_FCFI:
2055 case MBX_REG_VFI:
2056 case MBX_UNREG_VFI:
2057 case MBX_INIT_VPI:
2058 case MBX_INIT_VFI:
2059 case MBX_RESUME_RPI:
James Smartc7495932010-04-06 15:05:28 -04002060 case MBX_READ_EVENT_LOG_STATUS:
2061 case MBX_READ_EVENT_LOG:
James Smartdcf2a4e2010-09-29 11:18:53 -04002062 case MBX_SECURITY_MGMT:
2063 case MBX_AUTH_PORT:
dea31012005-04-17 16:05:31 -05002064 ret = mbxCommand;
2065 break;
2066 default:
2067 ret = MBX_SHUTDOWN;
2068 break;
2069 }
James Smart2e0fef82007-06-17 19:56:36 -05002070 return ret;
dea31012005-04-17 16:05:31 -05002071}
James Smarte59058c2008-08-24 21:49:00 -04002072
2073/**
James Smart3621a712009-04-06 18:47:14 -04002074 * lpfc_sli_wake_mbox_wait - lpfc_sli_issue_mbox_wait mbox completion handler
James Smarte59058c2008-08-24 21:49:00 -04002075 * @phba: Pointer to HBA context object.
2076 * @pmboxq: Pointer to mailbox command.
2077 *
2078 * This is completion handler function for mailbox commands issued from
2079 * lpfc_sli_issue_mbox_wait function. This function is called by the
2080 * mailbox event handler function with no lock held. This function
2081 * will wake up thread waiting on the wait queue pointed by context1
2082 * of the mailbox.
2083 **/
James Smart04c68492009-05-22 14:52:52 -04002084void
James Smart2e0fef82007-06-17 19:56:36 -05002085lpfc_sli_wake_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
dea31012005-04-17 16:05:31 -05002086{
2087 wait_queue_head_t *pdone_q;
James Smart858c9f62007-06-17 19:56:39 -05002088 unsigned long drvr_flag;
dea31012005-04-17 16:05:31 -05002089
2090 /*
2091 * If pdone_q is empty, the driver thread gave up waiting and
2092 * continued running.
2093 */
James Smart7054a602007-04-25 09:52:34 -04002094 pmboxq->mbox_flag |= LPFC_MBX_WAKE;
James Smart858c9f62007-06-17 19:56:39 -05002095 spin_lock_irqsave(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05002096 pdone_q = (wait_queue_head_t *) pmboxq->context1;
2097 if (pdone_q)
2098 wake_up_interruptible(pdone_q);
James Smart858c9f62007-06-17 19:56:39 -05002099 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05002100 return;
2101}
2102
James Smarte59058c2008-08-24 21:49:00 -04002103
2104/**
James Smart3621a712009-04-06 18:47:14 -04002105 * lpfc_sli_def_mbox_cmpl - Default mailbox completion handler
James Smarte59058c2008-08-24 21:49:00 -04002106 * @phba: Pointer to HBA context object.
2107 * @pmb: Pointer to mailbox object.
2108 *
2109 * This function is the default mailbox completion handler. It
2110 * frees the memory resources associated with the completed mailbox
2111 * command. If the completed command is a REG_LOGIN mailbox command,
2112 * this function will issue a UREG_LOGIN to re-claim the RPI.
2113 **/
dea31012005-04-17 16:05:31 -05002114void
James Smart2e0fef82007-06-17 19:56:36 -05002115lpfc_sli_def_mbox_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05002116{
James Smartd439d282010-09-29 11:18:45 -04002117 struct lpfc_vport *vport = pmb->vport;
dea31012005-04-17 16:05:31 -05002118 struct lpfc_dmabuf *mp;
James Smartd439d282010-09-29 11:18:45 -04002119 struct lpfc_nodelist *ndlp;
James Smart5af5eee2010-10-22 11:06:38 -04002120 struct Scsi_Host *shost;
James Smart04c68492009-05-22 14:52:52 -04002121 uint16_t rpi, vpi;
James Smart7054a602007-04-25 09:52:34 -04002122 int rc;
2123
dea31012005-04-17 16:05:31 -05002124 mp = (struct lpfc_dmabuf *) (pmb->context1);
James Smart7054a602007-04-25 09:52:34 -04002125
dea31012005-04-17 16:05:31 -05002126 if (mp) {
2127 lpfc_mbuf_free(phba, mp->virt, mp->phys);
2128 kfree(mp);
2129 }
James Smart7054a602007-04-25 09:52:34 -04002130
2131 /*
2132 * If a REG_LOGIN succeeded after node is destroyed or node
2133 * is in re-discovery driver need to cleanup the RPI.
2134 */
James Smart2e0fef82007-06-17 19:56:36 -05002135 if (!(phba->pport->load_flag & FC_UNLOADING) &&
James Smart04c68492009-05-22 14:52:52 -04002136 pmb->u.mb.mbxCommand == MBX_REG_LOGIN64 &&
2137 !pmb->u.mb.mbxStatus) {
2138 rpi = pmb->u.mb.un.varWords[0];
James Smart6d368e52011-05-24 11:44:12 -04002139 vpi = pmb->u.mb.un.varRegLogin.vpi;
James Smart04c68492009-05-22 14:52:52 -04002140 lpfc_unreg_login(phba, vpi, rpi, pmb);
James Smart92d7f7b2007-06-17 19:56:38 -05002141 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart7054a602007-04-25 09:52:34 -04002142 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
2143 if (rc != MBX_NOT_FINISHED)
2144 return;
2145 }
2146
James Smart695a8142010-01-26 23:08:03 -05002147 if ((pmb->u.mb.mbxCommand == MBX_REG_VPI) &&
2148 !(phba->pport->load_flag & FC_UNLOADING) &&
2149 !pmb->u.mb.mbxStatus) {
James Smart5af5eee2010-10-22 11:06:38 -04002150 shost = lpfc_shost_from_vport(vport);
2151 spin_lock_irq(shost->host_lock);
2152 vport->vpi_state |= LPFC_VPI_REGISTERED;
2153 vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
2154 spin_unlock_irq(shost->host_lock);
James Smart695a8142010-01-26 23:08:03 -05002155 }
2156
James Smartd439d282010-09-29 11:18:45 -04002157 if (pmb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
2158 ndlp = (struct lpfc_nodelist *)pmb->context2;
2159 lpfc_nlp_put(ndlp);
2160 pmb->context2 = NULL;
2161 }
2162
James Smartdcf2a4e2010-09-29 11:18:53 -04002163 /* Check security permission status on INIT_LINK mailbox command */
2164 if ((pmb->u.mb.mbxCommand == MBX_INIT_LINK) &&
2165 (pmb->u.mb.mbxStatus == MBXERR_SEC_NO_PERMISSION))
2166 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
2167 "2860 SLI authentication is required "
2168 "for INIT_LINK but has not done yet\n");
2169
James Smart04c68492009-05-22 14:52:52 -04002170 if (bf_get(lpfc_mqe_command, &pmb->u.mqe) == MBX_SLI4_CONFIG)
2171 lpfc_sli4_mbox_cmd_free(phba, pmb);
2172 else
2173 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05002174}
2175
James Smarte59058c2008-08-24 21:49:00 -04002176/**
James Smart3621a712009-04-06 18:47:14 -04002177 * lpfc_sli_handle_mb_event - Handle mailbox completions from firmware
James Smarte59058c2008-08-24 21:49:00 -04002178 * @phba: Pointer to HBA context object.
2179 *
2180 * This function is called with no lock held. This function processes all
2181 * the completed mailbox commands and gives it to upper layers. The interrupt
2182 * service routine processes mailbox completion interrupt and adds completed
2183 * mailbox commands to the mboxq_cmpl queue and signals the worker thread.
2184 * Worker thread call lpfc_sli_handle_mb_event, which will return the
2185 * completed mailbox commands in mboxq_cmpl queue to the upper layers. This
2186 * function returns the mailbox commands to the upper layer by calling the
2187 * completion handler function of each mailbox.
2188 **/
dea31012005-04-17 16:05:31 -05002189int
James Smart2e0fef82007-06-17 19:56:36 -05002190lpfc_sli_handle_mb_event(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05002191{
James Smart92d7f7b2007-06-17 19:56:38 -05002192 MAILBOX_t *pmbox;
dea31012005-04-17 16:05:31 -05002193 LPFC_MBOXQ_t *pmb;
James Smart92d7f7b2007-06-17 19:56:38 -05002194 int rc;
2195 LIST_HEAD(cmplq);
dea31012005-04-17 16:05:31 -05002196
2197 phba->sli.slistat.mbox_event++;
2198
James Smart92d7f7b2007-06-17 19:56:38 -05002199 /* Get all completed mailboxe buffers into the cmplq */
2200 spin_lock_irq(&phba->hbalock);
2201 list_splice_init(&phba->sli.mboxq_cmpl, &cmplq);
2202 spin_unlock_irq(&phba->hbalock);
2203
dea31012005-04-17 16:05:31 -05002204 /* Get a Mailbox buffer to setup mailbox commands for callback */
James Smart92d7f7b2007-06-17 19:56:38 -05002205 do {
2206 list_remove_head(&cmplq, pmb, LPFC_MBOXQ_t, list);
2207 if (pmb == NULL)
2208 break;
2209
James Smart04c68492009-05-22 14:52:52 -04002210 pmbox = &pmb->u.mb;
dea31012005-04-17 16:05:31 -05002211
James Smart858c9f62007-06-17 19:56:39 -05002212 if (pmbox->mbxCommand != MBX_HEARTBEAT) {
2213 if (pmb->vport) {
2214 lpfc_debugfs_disc_trc(pmb->vport,
2215 LPFC_DISC_TRC_MBOX_VPORT,
2216 "MBOX cmpl vport: cmd:x%x mb:x%x x%x",
2217 (uint32_t)pmbox->mbxCommand,
2218 pmbox->un.varWords[0],
2219 pmbox->un.varWords[1]);
2220 }
2221 else {
2222 lpfc_debugfs_disc_trc(phba->pport,
2223 LPFC_DISC_TRC_MBOX,
2224 "MBOX cmpl: cmd:x%x mb:x%x x%x",
2225 (uint32_t)pmbox->mbxCommand,
2226 pmbox->un.varWords[0],
2227 pmbox->un.varWords[1]);
2228 }
2229 }
2230
dea31012005-04-17 16:05:31 -05002231 /*
2232 * It is a fatal error if unknown mbox command completion.
2233 */
2234 if (lpfc_sli_chk_mbx_command(pmbox->mbxCommand) ==
2235 MBX_SHUTDOWN) {
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02002236 /* Unknown mailbox command compl */
James Smart92d7f7b2007-06-17 19:56:38 -05002237 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002238 "(%d):0323 Unknown Mailbox command "
James Smarta183a152011-10-10 21:32:43 -04002239 "x%x (x%x/x%x) Cmpl\n",
James Smart92d7f7b2007-06-17 19:56:38 -05002240 pmb->vport ? pmb->vport->vpi : 0,
James Smart04c68492009-05-22 14:52:52 -04002241 pmbox->mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04002242 lpfc_sli_config_mbox_subsys_get(phba,
2243 pmb),
2244 lpfc_sli_config_mbox_opcode_get(phba,
2245 pmb));
James Smart2e0fef82007-06-17 19:56:36 -05002246 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05002247 phba->work_hs = HS_FFER3;
2248 lpfc_handle_eratt(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05002249 continue;
dea31012005-04-17 16:05:31 -05002250 }
2251
dea31012005-04-17 16:05:31 -05002252 if (pmbox->mbxStatus) {
2253 phba->sli.slistat.mbox_stat_err++;
2254 if (pmbox->mbxStatus == MBXERR_NO_RESOURCES) {
2255 /* Mbox cmd cmpl error - RETRYing */
James Smart92d7f7b2007-06-17 19:56:38 -05002256 lpfc_printf_log(phba, KERN_INFO,
James Smarta183a152011-10-10 21:32:43 -04002257 LOG_MBOX | LOG_SLI,
2258 "(%d):0305 Mbox cmd cmpl "
2259 "error - RETRYing Data: x%x "
2260 "(x%x/x%x) x%x x%x x%x\n",
2261 pmb->vport ? pmb->vport->vpi : 0,
2262 pmbox->mbxCommand,
2263 lpfc_sli_config_mbox_subsys_get(phba,
2264 pmb),
2265 lpfc_sli_config_mbox_opcode_get(phba,
2266 pmb),
2267 pmbox->mbxStatus,
2268 pmbox->un.varWords[0],
2269 pmb->vport->port_state);
dea31012005-04-17 16:05:31 -05002270 pmbox->mbxStatus = 0;
2271 pmbox->mbxOwner = OWN_HOST;
dea31012005-04-17 16:05:31 -05002272 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
James Smart04c68492009-05-22 14:52:52 -04002273 if (rc != MBX_NOT_FINISHED)
James Smart92d7f7b2007-06-17 19:56:38 -05002274 continue;
dea31012005-04-17 16:05:31 -05002275 }
2276 }
2277
2278 /* Mailbox cmd <cmd> Cmpl <cmpl> */
James Smart92d7f7b2007-06-17 19:56:38 -05002279 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04002280 "(%d):0307 Mailbox cmd x%x (x%x/x%x) Cmpl x%p "
dea31012005-04-17 16:05:31 -05002281 "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 -05002282 pmb->vport ? pmb->vport->vpi : 0,
dea31012005-04-17 16:05:31 -05002283 pmbox->mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04002284 lpfc_sli_config_mbox_subsys_get(phba, pmb),
2285 lpfc_sli_config_mbox_opcode_get(phba, pmb),
dea31012005-04-17 16:05:31 -05002286 pmb->mbox_cmpl,
2287 *((uint32_t *) pmbox),
2288 pmbox->un.varWords[0],
2289 pmbox->un.varWords[1],
2290 pmbox->un.varWords[2],
2291 pmbox->un.varWords[3],
2292 pmbox->un.varWords[4],
2293 pmbox->un.varWords[5],
2294 pmbox->un.varWords[6],
2295 pmbox->un.varWords[7]);
2296
James Smart92d7f7b2007-06-17 19:56:38 -05002297 if (pmb->mbox_cmpl)
dea31012005-04-17 16:05:31 -05002298 pmb->mbox_cmpl(phba,pmb);
James Smart92d7f7b2007-06-17 19:56:38 -05002299 } while (1);
James Smart2e0fef82007-06-17 19:56:36 -05002300 return 0;
dea31012005-04-17 16:05:31 -05002301}
James Smart92d7f7b2007-06-17 19:56:38 -05002302
James Smarte59058c2008-08-24 21:49:00 -04002303/**
James Smart3621a712009-04-06 18:47:14 -04002304 * lpfc_sli_get_buff - Get the buffer associated with the buffer tag
James Smarte59058c2008-08-24 21:49:00 -04002305 * @phba: Pointer to HBA context object.
2306 * @pring: Pointer to driver SLI ring object.
2307 * @tag: buffer tag.
2308 *
2309 * This function is called with no lock held. When QUE_BUFTAG_BIT bit
2310 * is set in the tag the buffer is posted for a particular exchange,
2311 * the function will return the buffer without replacing the buffer.
2312 * If the buffer is for unsolicited ELS or CT traffic, this function
2313 * returns the buffer and also posts another buffer to the firmware.
2314 **/
James Smart76bb24e2007-10-27 13:38:00 -04002315static struct lpfc_dmabuf *
2316lpfc_sli_get_buff(struct lpfc_hba *phba,
James Smart9f1e1b52008-12-04 22:39:40 -05002317 struct lpfc_sli_ring *pring,
2318 uint32_t tag)
James Smart76bb24e2007-10-27 13:38:00 -04002319{
James Smart9f1e1b52008-12-04 22:39:40 -05002320 struct hbq_dmabuf *hbq_entry;
2321
James Smart76bb24e2007-10-27 13:38:00 -04002322 if (tag & QUE_BUFTAG_BIT)
2323 return lpfc_sli_ring_taggedbuf_get(phba, pring, tag);
James Smart9f1e1b52008-12-04 22:39:40 -05002324 hbq_entry = lpfc_sli_hbqbuf_find(phba, tag);
2325 if (!hbq_entry)
2326 return NULL;
2327 return &hbq_entry->dbuf;
James Smart76bb24e2007-10-27 13:38:00 -04002328}
James Smart57127f12007-10-27 13:37:05 -04002329
James Smart3772a992009-05-22 14:50:54 -04002330/**
2331 * lpfc_complete_unsol_iocb - Complete an unsolicited sequence
2332 * @phba: Pointer to HBA context object.
2333 * @pring: Pointer to driver SLI ring object.
2334 * @saveq: Pointer to the iocbq struct representing the sequence starting frame.
2335 * @fch_r_ctl: the r_ctl for the first frame of the sequence.
2336 * @fch_type: the type for the first frame of the sequence.
2337 *
2338 * This function is called with no lock held. This function uses the r_ctl and
2339 * type of the received sequence to find the correct callback function to call
2340 * to process the sequence.
2341 **/
2342static int
2343lpfc_complete_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2344 struct lpfc_iocbq *saveq, uint32_t fch_r_ctl,
2345 uint32_t fch_type)
2346{
2347 int i;
2348
2349 /* unSolicited Responses */
2350 if (pring->prt[0].profile) {
2351 if (pring->prt[0].lpfc_sli_rcv_unsol_event)
2352 (pring->prt[0].lpfc_sli_rcv_unsol_event) (phba, pring,
2353 saveq);
2354 return 1;
2355 }
2356 /* We must search, based on rctl / type
2357 for the right routine */
2358 for (i = 0; i < pring->num_mask; i++) {
2359 if ((pring->prt[i].rctl == fch_r_ctl) &&
2360 (pring->prt[i].type == fch_type)) {
2361 if (pring->prt[i].lpfc_sli_rcv_unsol_event)
2362 (pring->prt[i].lpfc_sli_rcv_unsol_event)
2363 (phba, pring, saveq);
2364 return 1;
2365 }
2366 }
2367 return 0;
2368}
James Smarte59058c2008-08-24 21:49:00 -04002369
2370/**
James Smart3621a712009-04-06 18:47:14 -04002371 * lpfc_sli_process_unsol_iocb - Unsolicited iocb handler
James Smarte59058c2008-08-24 21:49:00 -04002372 * @phba: Pointer to HBA context object.
2373 * @pring: Pointer to driver SLI ring object.
2374 * @saveq: Pointer to the unsolicited iocb.
2375 *
2376 * This function is called with no lock held by the ring event handler
2377 * when there is an unsolicited iocb posted to the response ring by the
2378 * firmware. This function gets the buffer associated with the iocbs
2379 * and calls the event handler for the ring. This function handles both
2380 * qring buffers and hbq buffers.
2381 * When the function returns 1 the caller can free the iocb object otherwise
2382 * upper layer functions will free the iocb objects.
2383 **/
dea31012005-04-17 16:05:31 -05002384static int
2385lpfc_sli_process_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2386 struct lpfc_iocbq *saveq)
2387{
2388 IOCB_t * irsp;
2389 WORD5 * w5p;
2390 uint32_t Rctl, Type;
James Smart3772a992009-05-22 14:50:54 -04002391 uint32_t match;
James Smart76bb24e2007-10-27 13:38:00 -04002392 struct lpfc_iocbq *iocbq;
James Smart3163f722008-02-08 18:50:25 -05002393 struct lpfc_dmabuf *dmzbuf;
dea31012005-04-17 16:05:31 -05002394
2395 match = 0;
2396 irsp = &(saveq->iocb);
James Smart57127f12007-10-27 13:37:05 -04002397
2398 if (irsp->ulpCommand == CMD_ASYNC_STATUS) {
2399 if (pring->lpfc_sli_rcv_async_status)
2400 pring->lpfc_sli_rcv_async_status(phba, pring, saveq);
2401 else
2402 lpfc_printf_log(phba,
2403 KERN_WARNING,
2404 LOG_SLI,
2405 "0316 Ring %d handler: unexpected "
2406 "ASYNC_STATUS iocb received evt_code "
2407 "0x%x\n",
2408 pring->ringno,
2409 irsp->un.asyncstat.evt_code);
2410 return 1;
2411 }
2412
James Smart3163f722008-02-08 18:50:25 -05002413 if ((irsp->ulpCommand == CMD_IOCB_RET_XRI64_CX) &&
2414 (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)) {
2415 if (irsp->ulpBdeCount > 0) {
2416 dmzbuf = lpfc_sli_get_buff(phba, pring,
2417 irsp->un.ulpWord[3]);
2418 lpfc_in_buf_free(phba, dmzbuf);
2419 }
2420
2421 if (irsp->ulpBdeCount > 1) {
2422 dmzbuf = lpfc_sli_get_buff(phba, pring,
2423 irsp->unsli3.sli3Words[3]);
2424 lpfc_in_buf_free(phba, dmzbuf);
2425 }
2426
2427 if (irsp->ulpBdeCount > 2) {
2428 dmzbuf = lpfc_sli_get_buff(phba, pring,
2429 irsp->unsli3.sli3Words[7]);
2430 lpfc_in_buf_free(phba, dmzbuf);
2431 }
2432
2433 return 1;
2434 }
2435
James Smart92d7f7b2007-06-17 19:56:38 -05002436 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
James Smart76bb24e2007-10-27 13:38:00 -04002437 if (irsp->ulpBdeCount != 0) {
2438 saveq->context2 = lpfc_sli_get_buff(phba, pring,
James Smart92d7f7b2007-06-17 19:56:38 -05002439 irsp->un.ulpWord[3]);
James Smart76bb24e2007-10-27 13:38:00 -04002440 if (!saveq->context2)
2441 lpfc_printf_log(phba,
2442 KERN_ERR,
2443 LOG_SLI,
2444 "0341 Ring %d Cannot find buffer for "
2445 "an unsolicited iocb. tag 0x%x\n",
2446 pring->ringno,
2447 irsp->un.ulpWord[3]);
James Smart76bb24e2007-10-27 13:38:00 -04002448 }
2449 if (irsp->ulpBdeCount == 2) {
2450 saveq->context3 = lpfc_sli_get_buff(phba, pring,
James Smart51ef4c22007-08-02 11:10:31 -04002451 irsp->unsli3.sli3Words[7]);
James Smart76bb24e2007-10-27 13:38:00 -04002452 if (!saveq->context3)
2453 lpfc_printf_log(phba,
2454 KERN_ERR,
2455 LOG_SLI,
2456 "0342 Ring %d Cannot find buffer for an"
2457 " unsolicited iocb. tag 0x%x\n",
2458 pring->ringno,
2459 irsp->unsli3.sli3Words[7]);
2460 }
2461 list_for_each_entry(iocbq, &saveq->list, list) {
James Smart76bb24e2007-10-27 13:38:00 -04002462 irsp = &(iocbq->iocb);
James Smart76bb24e2007-10-27 13:38:00 -04002463 if (irsp->ulpBdeCount != 0) {
2464 iocbq->context2 = lpfc_sli_get_buff(phba, pring,
2465 irsp->un.ulpWord[3]);
James Smart9c2face2008-01-11 01:53:18 -05002466 if (!iocbq->context2)
James Smart76bb24e2007-10-27 13:38:00 -04002467 lpfc_printf_log(phba,
2468 KERN_ERR,
2469 LOG_SLI,
2470 "0343 Ring %d Cannot find "
2471 "buffer for an unsolicited iocb"
2472 ". tag 0x%x\n", pring->ringno,
2473 irsp->un.ulpWord[3]);
2474 }
2475 if (irsp->ulpBdeCount == 2) {
2476 iocbq->context3 = lpfc_sli_get_buff(phba, pring,
2477 irsp->unsli3.sli3Words[7]);
James Smart9c2face2008-01-11 01:53:18 -05002478 if (!iocbq->context3)
James Smart76bb24e2007-10-27 13:38:00 -04002479 lpfc_printf_log(phba,
2480 KERN_ERR,
2481 LOG_SLI,
2482 "0344 Ring %d Cannot find "
2483 "buffer for an unsolicited "
2484 "iocb. tag 0x%x\n",
2485 pring->ringno,
2486 irsp->unsli3.sli3Words[7]);
2487 }
2488 }
James Smart92d7f7b2007-06-17 19:56:38 -05002489 }
James Smart9c2face2008-01-11 01:53:18 -05002490 if (irsp->ulpBdeCount != 0 &&
2491 (irsp->ulpCommand == CMD_IOCB_RCV_CONT64_CX ||
2492 irsp->ulpStatus == IOSTAT_INTERMED_RSP)) {
2493 int found = 0;
2494
2495 /* search continue save q for same XRI */
2496 list_for_each_entry(iocbq, &pring->iocb_continue_saveq, clist) {
James Smart7851fe22011-07-22 18:36:52 -04002497 if (iocbq->iocb.unsli3.rcvsli3.ox_id ==
2498 saveq->iocb.unsli3.rcvsli3.ox_id) {
James Smart9c2face2008-01-11 01:53:18 -05002499 list_add_tail(&saveq->list, &iocbq->list);
2500 found = 1;
2501 break;
2502 }
2503 }
2504 if (!found)
2505 list_add_tail(&saveq->clist,
2506 &pring->iocb_continue_saveq);
2507 if (saveq->iocb.ulpStatus != IOSTAT_INTERMED_RSP) {
2508 list_del_init(&iocbq->clist);
2509 saveq = iocbq;
2510 irsp = &(saveq->iocb);
2511 } else
2512 return 0;
2513 }
2514 if ((irsp->ulpCommand == CMD_RCV_ELS_REQ64_CX) ||
2515 (irsp->ulpCommand == CMD_RCV_ELS_REQ_CX) ||
2516 (irsp->ulpCommand == CMD_IOCB_RCV_ELS64_CX)) {
James Smart6a9c52c2009-10-02 15:16:51 -04002517 Rctl = FC_RCTL_ELS_REQ;
2518 Type = FC_TYPE_ELS;
James Smart9c2face2008-01-11 01:53:18 -05002519 } else {
2520 w5p = (WORD5 *)&(saveq->iocb.un.ulpWord[5]);
2521 Rctl = w5p->hcsw.Rctl;
2522 Type = w5p->hcsw.Type;
2523
2524 /* Firmware Workaround */
2525 if ((Rctl == 0) && (pring->ringno == LPFC_ELS_RING) &&
2526 (irsp->ulpCommand == CMD_RCV_SEQUENCE64_CX ||
2527 irsp->ulpCommand == CMD_IOCB_RCV_SEQ64_CX)) {
James Smart6a9c52c2009-10-02 15:16:51 -04002528 Rctl = FC_RCTL_ELS_REQ;
2529 Type = FC_TYPE_ELS;
James Smart9c2face2008-01-11 01:53:18 -05002530 w5p->hcsw.Rctl = Rctl;
2531 w5p->hcsw.Type = Type;
2532 }
2533 }
James Smart92d7f7b2007-06-17 19:56:38 -05002534
James Smart3772a992009-05-22 14:50:54 -04002535 if (!lpfc_complete_unsol_iocb(phba, pring, saveq, Rctl, Type))
James Smart92d7f7b2007-06-17 19:56:38 -05002536 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002537 "0313 Ring %d handler: unexpected Rctl x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05002538 "Type x%x received\n",
James Smarte8b62012007-08-02 11:10:09 -04002539 pring->ringno, Rctl, Type);
James Smart3772a992009-05-22 14:50:54 -04002540
James Smart92d7f7b2007-06-17 19:56:38 -05002541 return 1;
dea31012005-04-17 16:05:31 -05002542}
2543
James Smarte59058c2008-08-24 21:49:00 -04002544/**
James Smart3621a712009-04-06 18:47:14 -04002545 * lpfc_sli_iocbq_lookup - Find command iocb for the given response iocb
James Smarte59058c2008-08-24 21:49:00 -04002546 * @phba: Pointer to HBA context object.
2547 * @pring: Pointer to driver SLI ring object.
2548 * @prspiocb: Pointer to response iocb object.
2549 *
2550 * This function looks up the iocb_lookup table to get the command iocb
2551 * corresponding to the given response iocb using the iotag of the
2552 * response iocb. This function is called with the hbalock held.
2553 * This function returns the command iocb object if it finds the command
2554 * iocb else returns NULL.
2555 **/
dea31012005-04-17 16:05:31 -05002556static struct lpfc_iocbq *
James Smart2e0fef82007-06-17 19:56:36 -05002557lpfc_sli_iocbq_lookup(struct lpfc_hba *phba,
2558 struct lpfc_sli_ring *pring,
2559 struct lpfc_iocbq *prspiocb)
dea31012005-04-17 16:05:31 -05002560{
dea31012005-04-17 16:05:31 -05002561 struct lpfc_iocbq *cmd_iocb = NULL;
2562 uint16_t iotag;
2563
James Bottomley604a3e32005-10-29 10:28:33 -05002564 iotag = prspiocb->iocb.ulpIoTag;
dea31012005-04-17 16:05:31 -05002565
James Bottomley604a3e32005-10-29 10:28:33 -05002566 if (iotag != 0 && iotag <= phba->sli.last_iotag) {
2567 cmd_iocb = phba->sli.iocbq_lookup[iotag];
James Smart92d7f7b2007-06-17 19:56:38 -05002568 list_del_init(&cmd_iocb->list);
James Smart4f2e66c2012-05-09 21:17:07 -04002569 if (cmd_iocb->iocb_flag & LPFC_IO_ON_TXCMPLQ) {
James Smart2a9bf3d2010-06-07 15:24:45 -04002570 pring->txcmplq_cnt--;
James Smart4f2e66c2012-05-09 21:17:07 -04002571 cmd_iocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
James Smart2a9bf3d2010-06-07 15:24:45 -04002572 }
James Bottomley604a3e32005-10-29 10:28:33 -05002573 return cmd_iocb;
dea31012005-04-17 16:05:31 -05002574 }
2575
dea31012005-04-17 16:05:31 -05002576 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002577 "0317 iotag x%x is out off "
James Bottomley604a3e32005-10-29 10:28:33 -05002578 "range: max iotag x%x wd0 x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04002579 iotag, phba->sli.last_iotag,
James Bottomley604a3e32005-10-29 10:28:33 -05002580 *(((uint32_t *) &prspiocb->iocb) + 7));
dea31012005-04-17 16:05:31 -05002581 return NULL;
2582}
2583
James Smarte59058c2008-08-24 21:49:00 -04002584/**
James Smart3772a992009-05-22 14:50:54 -04002585 * lpfc_sli_iocbq_lookup_by_tag - Find command iocb for the iotag
2586 * @phba: Pointer to HBA context object.
2587 * @pring: Pointer to driver SLI ring object.
2588 * @iotag: IOCB tag.
2589 *
2590 * This function looks up the iocb_lookup table to get the command iocb
2591 * corresponding to the given iotag. This function is called with the
2592 * hbalock held.
2593 * This function returns the command iocb object if it finds the command
2594 * iocb else returns NULL.
2595 **/
2596static struct lpfc_iocbq *
2597lpfc_sli_iocbq_lookup_by_tag(struct lpfc_hba *phba,
2598 struct lpfc_sli_ring *pring, uint16_t iotag)
2599{
2600 struct lpfc_iocbq *cmd_iocb;
2601
2602 if (iotag != 0 && iotag <= phba->sli.last_iotag) {
2603 cmd_iocb = phba->sli.iocbq_lookup[iotag];
James Smart4f2e66c2012-05-09 21:17:07 -04002604 if (cmd_iocb->iocb_flag & LPFC_IO_ON_TXCMPLQ) {
2605 /* remove from txcmpl queue list */
2606 list_del_init(&cmd_iocb->list);
2607 cmd_iocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
James Smart2a9bf3d2010-06-07 15:24:45 -04002608 pring->txcmplq_cnt--;
James Smart4f2e66c2012-05-09 21:17:07 -04002609 return cmd_iocb;
James Smart2a9bf3d2010-06-07 15:24:45 -04002610 }
James Smart3772a992009-05-22 14:50:54 -04002611 }
James Smart3772a992009-05-22 14:50:54 -04002612 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2613 "0372 iotag x%x is out off range: max iotag (x%x)\n",
2614 iotag, phba->sli.last_iotag);
2615 return NULL;
2616}
2617
2618/**
James Smart3621a712009-04-06 18:47:14 -04002619 * lpfc_sli_process_sol_iocb - process solicited iocb completion
James Smarte59058c2008-08-24 21:49:00 -04002620 * @phba: Pointer to HBA context object.
2621 * @pring: Pointer to driver SLI ring object.
2622 * @saveq: Pointer to the response iocb to be processed.
2623 *
2624 * This function is called by the ring event handler for non-fcp
2625 * rings when there is a new response iocb in the response ring.
2626 * The caller is not required to hold any locks. This function
2627 * gets the command iocb associated with the response iocb and
2628 * calls the completion handler for the command iocb. If there
2629 * is no completion handler, the function will free the resources
2630 * associated with command iocb. If the response iocb is for
2631 * an already aborted command iocb, the status of the completion
2632 * is changed to IOSTAT_LOCAL_REJECT/IOERR_SLI_ABORTED.
2633 * This function always returns 1.
2634 **/
dea31012005-04-17 16:05:31 -05002635static int
James Smart2e0fef82007-06-17 19:56:36 -05002636lpfc_sli_process_sol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
dea31012005-04-17 16:05:31 -05002637 struct lpfc_iocbq *saveq)
2638{
James Smart2e0fef82007-06-17 19:56:36 -05002639 struct lpfc_iocbq *cmdiocbp;
dea31012005-04-17 16:05:31 -05002640 int rc = 1;
2641 unsigned long iflag;
2642
2643 /* Based on the iotag field, get the cmd IOCB from the txcmplq */
James Smart2e0fef82007-06-17 19:56:36 -05002644 spin_lock_irqsave(&phba->hbalock, iflag);
James Bottomley604a3e32005-10-29 10:28:33 -05002645 cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring, saveq);
James Smart2e0fef82007-06-17 19:56:36 -05002646 spin_unlock_irqrestore(&phba->hbalock, iflag);
2647
dea31012005-04-17 16:05:31 -05002648 if (cmdiocbp) {
2649 if (cmdiocbp->iocb_cmpl) {
2650 /*
James Smartea2151b2008-09-07 11:52:10 -04002651 * If an ELS command failed send an event to mgmt
2652 * application.
2653 */
2654 if (saveq->iocb.ulpStatus &&
2655 (pring->ringno == LPFC_ELS_RING) &&
2656 (cmdiocbp->iocb.ulpCommand ==
2657 CMD_ELS_REQUEST64_CR))
2658 lpfc_send_els_failure_event(phba,
2659 cmdiocbp, saveq);
2660
2661 /*
dea31012005-04-17 16:05:31 -05002662 * Post all ELS completions to the worker thread.
2663 * All other are passed to the completion callback.
2664 */
2665 if (pring->ringno == LPFC_ELS_RING) {
James Smart341af102010-01-26 23:07:37 -05002666 if ((phba->sli_rev < LPFC_SLI_REV4) &&
2667 (cmdiocbp->iocb_flag &
2668 LPFC_DRIVER_ABORTED)) {
2669 spin_lock_irqsave(&phba->hbalock,
2670 iflag);
James Smart07951072007-04-25 09:51:38 -04002671 cmdiocbp->iocb_flag &=
2672 ~LPFC_DRIVER_ABORTED;
James Smart341af102010-01-26 23:07:37 -05002673 spin_unlock_irqrestore(&phba->hbalock,
2674 iflag);
James Smart07951072007-04-25 09:51:38 -04002675 saveq->iocb.ulpStatus =
2676 IOSTAT_LOCAL_REJECT;
2677 saveq->iocb.un.ulpWord[4] =
2678 IOERR_SLI_ABORTED;
James Smart0ff10d42008-01-11 01:52:36 -05002679
2680 /* Firmware could still be in progress
2681 * of DMAing payload, so don't free data
2682 * buffer till after a hbeat.
2683 */
James Smart341af102010-01-26 23:07:37 -05002684 spin_lock_irqsave(&phba->hbalock,
2685 iflag);
James Smart0ff10d42008-01-11 01:52:36 -05002686 saveq->iocb_flag |= LPFC_DELAY_MEM_FREE;
James Smart341af102010-01-26 23:07:37 -05002687 spin_unlock_irqrestore(&phba->hbalock,
2688 iflag);
2689 }
James Smart0f65ff62010-02-26 14:14:23 -05002690 if (phba->sli_rev == LPFC_SLI_REV4) {
2691 if (saveq->iocb_flag &
2692 LPFC_EXCHANGE_BUSY) {
2693 /* Set cmdiocb flag for the
2694 * exchange busy so sgl (xri)
2695 * will not be released until
2696 * the abort xri is received
2697 * from hba.
2698 */
2699 spin_lock_irqsave(
2700 &phba->hbalock, iflag);
2701 cmdiocbp->iocb_flag |=
2702 LPFC_EXCHANGE_BUSY;
2703 spin_unlock_irqrestore(
2704 &phba->hbalock, iflag);
2705 }
2706 if (cmdiocbp->iocb_flag &
2707 LPFC_DRIVER_ABORTED) {
2708 /*
2709 * Clear LPFC_DRIVER_ABORTED
2710 * bit in case it was driver
2711 * initiated abort.
2712 */
2713 spin_lock_irqsave(
2714 &phba->hbalock, iflag);
2715 cmdiocbp->iocb_flag &=
2716 ~LPFC_DRIVER_ABORTED;
2717 spin_unlock_irqrestore(
2718 &phba->hbalock, iflag);
2719 cmdiocbp->iocb.ulpStatus =
2720 IOSTAT_LOCAL_REJECT;
2721 cmdiocbp->iocb.un.ulpWord[4] =
2722 IOERR_ABORT_REQUESTED;
2723 /*
2724 * For SLI4, irsiocb contains
2725 * NO_XRI in sli_xritag, it
2726 * shall not affect releasing
2727 * sgl (xri) process.
2728 */
2729 saveq->iocb.ulpStatus =
2730 IOSTAT_LOCAL_REJECT;
2731 saveq->iocb.un.ulpWord[4] =
2732 IOERR_SLI_ABORTED;
2733 spin_lock_irqsave(
2734 &phba->hbalock, iflag);
2735 saveq->iocb_flag |=
2736 LPFC_DELAY_MEM_FREE;
2737 spin_unlock_irqrestore(
2738 &phba->hbalock, iflag);
2739 }
James Smart07951072007-04-25 09:51:38 -04002740 }
dea31012005-04-17 16:05:31 -05002741 }
James Smart2e0fef82007-06-17 19:56:36 -05002742 (cmdiocbp->iocb_cmpl) (phba, cmdiocbp, saveq);
James Bottomley604a3e32005-10-29 10:28:33 -05002743 } else
2744 lpfc_sli_release_iocbq(phba, cmdiocbp);
dea31012005-04-17 16:05:31 -05002745 } else {
2746 /*
2747 * Unknown initiating command based on the response iotag.
2748 * This could be the case on the ELS ring because of
2749 * lpfc_els_abort().
2750 */
2751 if (pring->ringno != LPFC_ELS_RING) {
2752 /*
2753 * Ring <ringno> handler: unexpected completion IoTag
2754 * <IoTag>
2755 */
James Smarta257bf92009-04-06 18:48:10 -04002756 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002757 "0322 Ring %d handler: "
2758 "unexpected completion IoTag x%x "
2759 "Data: x%x x%x x%x x%x\n",
2760 pring->ringno,
2761 saveq->iocb.ulpIoTag,
2762 saveq->iocb.ulpStatus,
2763 saveq->iocb.un.ulpWord[4],
2764 saveq->iocb.ulpCommand,
2765 saveq->iocb.ulpContext);
dea31012005-04-17 16:05:31 -05002766 }
2767 }
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04002768
dea31012005-04-17 16:05:31 -05002769 return rc;
2770}
2771
James Smarte59058c2008-08-24 21:49:00 -04002772/**
James Smart3621a712009-04-06 18:47:14 -04002773 * lpfc_sli_rsp_pointers_error - Response ring pointer error handler
James Smarte59058c2008-08-24 21:49:00 -04002774 * @phba: Pointer to HBA context object.
2775 * @pring: Pointer to driver SLI ring object.
2776 *
2777 * This function is called from the iocb ring event handlers when
2778 * put pointer is ahead of the get pointer for a ring. This function signal
2779 * an error attention condition to the worker thread and the worker
2780 * thread will transition the HBA to offline state.
2781 **/
James Smart2e0fef82007-06-17 19:56:36 -05002782static void
2783lpfc_sli_rsp_pointers_error(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002784{
James Smart34b02dc2008-08-24 21:49:55 -04002785 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002786 /*
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02002787 * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002788 * rsp ring <portRspMax>
2789 */
2790 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002791 "0312 Ring %d handler: portRspPut %d "
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02002792 "is bigger than rsp ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04002793 pring->ringno, le32_to_cpu(pgp->rspPutInx),
James Smart7e56aa22012-08-03 12:35:34 -04002794 pring->sli.sli3.numRiocb);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002795
James Smart2e0fef82007-06-17 19:56:36 -05002796 phba->link_state = LPFC_HBA_ERROR;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002797
2798 /*
2799 * All error attention handlers are posted to
2800 * worker thread
2801 */
2802 phba->work_ha |= HA_ERATT;
2803 phba->work_hs = HS_FFER3;
James Smart92d7f7b2007-06-17 19:56:38 -05002804
James Smart5e9d9b82008-06-14 22:52:53 -04002805 lpfc_worker_wake_up(phba);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002806
2807 return;
2808}
2809
James Smarte59058c2008-08-24 21:49:00 -04002810/**
James Smart3621a712009-04-06 18:47:14 -04002811 * lpfc_poll_eratt - Error attention polling timer timeout handler
James Smart93996272008-08-24 21:50:30 -04002812 * @ptr: Pointer to address of HBA context object.
2813 *
2814 * This function is invoked by the Error Attention polling timer when the
2815 * timer times out. It will check the SLI Error Attention register for
2816 * possible attention events. If so, it will post an Error Attention event
2817 * and wake up worker thread to process it. Otherwise, it will set up the
2818 * Error Attention polling timer for the next poll.
2819 **/
2820void lpfc_poll_eratt(unsigned long ptr)
2821{
2822 struct lpfc_hba *phba;
2823 uint32_t eratt = 0;
2824
2825 phba = (struct lpfc_hba *)ptr;
2826
2827 /* Check chip HA register for error event */
2828 eratt = lpfc_sli_check_eratt(phba);
2829
2830 if (eratt)
2831 /* Tell the worker thread there is work to do */
2832 lpfc_worker_wake_up(phba);
2833 else
2834 /* Restart the timer for next eratt poll */
2835 mod_timer(&phba->eratt_poll, jiffies +
2836 HZ * LPFC_ERATT_POLL_INTERVAL);
2837 return;
2838}
2839
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002840
James Smarte59058c2008-08-24 21:49:00 -04002841/**
James Smart3621a712009-04-06 18:47:14 -04002842 * lpfc_sli_handle_fast_ring_event - Handle ring events on FCP ring
James Smarte59058c2008-08-24 21:49:00 -04002843 * @phba: Pointer to HBA context object.
2844 * @pring: Pointer to driver SLI ring object.
2845 * @mask: Host attention register mask for this ring.
2846 *
2847 * This function is called from the interrupt context when there is a ring
2848 * event for the fcp ring. The caller does not hold any lock.
2849 * The function processes each response iocb in the response ring until it
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002850 * finds an iocb with LE bit set and chains all the iocbs up to the iocb with
James Smarte59058c2008-08-24 21:49:00 -04002851 * LE bit set. The function will call the completion handler of the command iocb
2852 * if the response iocb indicates a completion for a command iocb or it is
2853 * an abort completion. The function will call lpfc_sli_process_unsol_iocb
2854 * function if this is an unsolicited iocb.
dea31012005-04-17 16:05:31 -05002855 * This routine presumes LPFC_FCP_RING handling and doesn't bother
James Smart45ed1192009-10-02 15:17:02 -04002856 * to check it explicitly.
2857 */
2858int
James Smart2e0fef82007-06-17 19:56:36 -05002859lpfc_sli_handle_fast_ring_event(struct lpfc_hba *phba,
2860 struct lpfc_sli_ring *pring, uint32_t mask)
dea31012005-04-17 16:05:31 -05002861{
James Smart34b02dc2008-08-24 21:49:55 -04002862 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
dea31012005-04-17 16:05:31 -05002863 IOCB_t *irsp = NULL;
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04002864 IOCB_t *entry = NULL;
dea31012005-04-17 16:05:31 -05002865 struct lpfc_iocbq *cmdiocbq = NULL;
2866 struct lpfc_iocbq rspiocbq;
dea31012005-04-17 16:05:31 -05002867 uint32_t status;
2868 uint32_t portRspPut, portRspMax;
2869 int rc = 1;
2870 lpfc_iocb_type type;
2871 unsigned long iflag;
2872 uint32_t rsp_cmpl = 0;
dea31012005-04-17 16:05:31 -05002873
James Smart2e0fef82007-06-17 19:56:36 -05002874 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05002875 pring->stats.iocb_event++;
2876
dea31012005-04-17 16:05:31 -05002877 /*
2878 * The next available response entry should never exceed the maximum
2879 * entries. If it does, treat it as an adapter hardware error.
2880 */
James Smart7e56aa22012-08-03 12:35:34 -04002881 portRspMax = pring->sli.sli3.numRiocb;
dea31012005-04-17 16:05:31 -05002882 portRspPut = le32_to_cpu(pgp->rspPutInx);
2883 if (unlikely(portRspPut >= portRspMax)) {
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002884 lpfc_sli_rsp_pointers_error(phba, pring);
James Smart2e0fef82007-06-17 19:56:36 -05002885 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05002886 return 1;
2887 }
James Smart45ed1192009-10-02 15:17:02 -04002888 if (phba->fcp_ring_in_use) {
2889 spin_unlock_irqrestore(&phba->hbalock, iflag);
2890 return 1;
2891 } else
2892 phba->fcp_ring_in_use = 1;
dea31012005-04-17 16:05:31 -05002893
2894 rmb();
James Smart7e56aa22012-08-03 12:35:34 -04002895 while (pring->sli.sli3.rspidx != portRspPut) {
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04002896 /*
2897 * Fetch an entry off the ring and copy it into a local data
2898 * structure. The copy involves a byte-swap since the
2899 * network byte order and pci byte orders are different.
2900 */
James Smarted957682007-06-17 19:56:37 -05002901 entry = lpfc_resp_iocb(phba, pring);
James Smart858c9f62007-06-17 19:56:39 -05002902 phba->last_completion_time = jiffies;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002903
James Smart7e56aa22012-08-03 12:35:34 -04002904 if (++pring->sli.sli3.rspidx >= portRspMax)
2905 pring->sli.sli3.rspidx = 0;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002906
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04002907 lpfc_sli_pcimem_bcopy((uint32_t *) entry,
2908 (uint32_t *) &rspiocbq.iocb,
James Smarted957682007-06-17 19:56:37 -05002909 phba->iocb_rsp_size);
James Smarta4bc3372006-12-02 13:34:16 -05002910 INIT_LIST_HEAD(&(rspiocbq.list));
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04002911 irsp = &rspiocbq.iocb;
2912
dea31012005-04-17 16:05:31 -05002913 type = lpfc_sli_iocb_cmd_type(irsp->ulpCommand & CMD_IOCB_MASK);
2914 pring->stats.iocb_rsp++;
2915 rsp_cmpl++;
2916
2917 if (unlikely(irsp->ulpStatus)) {
James Smart92d7f7b2007-06-17 19:56:38 -05002918 /*
2919 * If resource errors reported from HBA, reduce
2920 * queuedepths of the SCSI device.
2921 */
2922 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
2923 (irsp->un.ulpWord[4] == IOERR_NO_RESOURCES)) {
2924 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart3772a992009-05-22 14:50:54 -04002925 phba->lpfc_rampdown_queue_depth(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05002926 spin_lock_irqsave(&phba->hbalock, iflag);
2927 }
2928
dea31012005-04-17 16:05:31 -05002929 /* Rsp ring <ringno> error: IOCB */
2930 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002931 "0336 Rsp Ring %d error: IOCB Data: "
James Smart92d7f7b2007-06-17 19:56:38 -05002932 "x%x x%x x%x x%x x%x x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04002933 pring->ringno,
James Smart92d7f7b2007-06-17 19:56:38 -05002934 irsp->un.ulpWord[0],
2935 irsp->un.ulpWord[1],
2936 irsp->un.ulpWord[2],
2937 irsp->un.ulpWord[3],
2938 irsp->un.ulpWord[4],
2939 irsp->un.ulpWord[5],
James Smartd7c255b2008-08-24 21:50:00 -04002940 *(uint32_t *)&irsp->un1,
2941 *((uint32_t *)&irsp->un1 + 1));
dea31012005-04-17 16:05:31 -05002942 }
2943
2944 switch (type) {
2945 case LPFC_ABORT_IOCB:
2946 case LPFC_SOL_IOCB:
2947 /*
2948 * Idle exchange closed via ABTS from port. No iocb
2949 * resources need to be recovered.
2950 */
2951 if (unlikely(irsp->ulpCommand == CMD_XRI_ABORTED_CX)) {
James Smartdca94792006-08-01 07:34:08 -04002952 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002953 "0333 IOCB cmd 0x%x"
James Smartdca94792006-08-01 07:34:08 -04002954 " processed. Skipping"
James Smart92d7f7b2007-06-17 19:56:38 -05002955 " completion\n",
James Smartdca94792006-08-01 07:34:08 -04002956 irsp->ulpCommand);
dea31012005-04-17 16:05:31 -05002957 break;
2958 }
2959
James Bottomley604a3e32005-10-29 10:28:33 -05002960 cmdiocbq = lpfc_sli_iocbq_lookup(phba, pring,
2961 &rspiocbq);
James Smart0f65ff62010-02-26 14:14:23 -05002962 if (unlikely(!cmdiocbq))
2963 break;
2964 if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED)
2965 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
2966 if (cmdiocbq->iocb_cmpl) {
2967 spin_unlock_irqrestore(&phba->hbalock, iflag);
2968 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
2969 &rspiocbq);
2970 spin_lock_irqsave(&phba->hbalock, iflag);
2971 }
dea31012005-04-17 16:05:31 -05002972 break;
James Smarta4bc3372006-12-02 13:34:16 -05002973 case LPFC_UNSOL_IOCB:
James Smart2e0fef82007-06-17 19:56:36 -05002974 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta4bc3372006-12-02 13:34:16 -05002975 lpfc_sli_process_unsol_iocb(phba, pring, &rspiocbq);
James Smart2e0fef82007-06-17 19:56:36 -05002976 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta4bc3372006-12-02 13:34:16 -05002977 break;
dea31012005-04-17 16:05:31 -05002978 default:
2979 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
2980 char adaptermsg[LPFC_MAX_ADPTMSG];
2981 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
2982 memcpy(&adaptermsg[0], (uint8_t *) irsp,
2983 MAX_MSG_DATA);
Joe Perches898eb712007-10-18 03:06:30 -07002984 dev_warn(&((phba->pcidev)->dev),
2985 "lpfc%d: %s\n",
dea31012005-04-17 16:05:31 -05002986 phba->brd_no, adaptermsg);
2987 } else {
2988 /* Unknown IOCB command */
2989 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002990 "0334 Unknown IOCB command "
James Smart92d7f7b2007-06-17 19:56:38 -05002991 "Data: x%x, x%x x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04002992 type, irsp->ulpCommand,
James Smart92d7f7b2007-06-17 19:56:38 -05002993 irsp->ulpStatus,
2994 irsp->ulpIoTag,
2995 irsp->ulpContext);
dea31012005-04-17 16:05:31 -05002996 }
2997 break;
2998 }
2999
3000 /*
3001 * The response IOCB has been processed. Update the ring
3002 * pointer in SLIM. If the port response put pointer has not
3003 * been updated, sync the pgp->rspPutInx and fetch the new port
3004 * response put pointer.
3005 */
James Smart7e56aa22012-08-03 12:35:34 -04003006 writel(pring->sli.sli3.rspidx,
3007 &phba->host_gp[pring->ringno].rspGetInx);
dea31012005-04-17 16:05:31 -05003008
James Smart7e56aa22012-08-03 12:35:34 -04003009 if (pring->sli.sli3.rspidx == portRspPut)
dea31012005-04-17 16:05:31 -05003010 portRspPut = le32_to_cpu(pgp->rspPutInx);
3011 }
3012
3013 if ((rsp_cmpl > 0) && (mask & HA_R0RE_REQ)) {
3014 pring->stats.iocb_rsp_full++;
3015 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
3016 writel(status, phba->CAregaddr);
3017 readl(phba->CAregaddr);
3018 }
3019 if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
3020 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
3021 pring->stats.iocb_cmd_empty++;
3022
3023 /* Force update of the local copy of cmdGetInx */
James Smart7e56aa22012-08-03 12:35:34 -04003024 pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
dea31012005-04-17 16:05:31 -05003025 lpfc_sli_resume_iocb(phba, pring);
3026
3027 if ((pring->lpfc_sli_cmd_available))
3028 (pring->lpfc_sli_cmd_available) (phba, pring);
3029
3030 }
3031
James Smart45ed1192009-10-02 15:17:02 -04003032 phba->fcp_ring_in_use = 0;
James Smart2e0fef82007-06-17 19:56:36 -05003033 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003034 return rc;
3035}
3036
James Smarte59058c2008-08-24 21:49:00 -04003037/**
James Smart3772a992009-05-22 14:50:54 -04003038 * lpfc_sli_sp_handle_rspiocb - Handle slow-path response iocb
3039 * @phba: Pointer to HBA context object.
3040 * @pring: Pointer to driver SLI ring object.
3041 * @rspiocbp: Pointer to driver response IOCB object.
3042 *
3043 * This function is called from the worker thread when there is a slow-path
3044 * response IOCB to process. This function chains all the response iocbs until
3045 * seeing the iocb with the LE bit set. The function will call
3046 * lpfc_sli_process_sol_iocb function if the response iocb indicates a
3047 * completion of a command iocb. The function will call the
3048 * lpfc_sli_process_unsol_iocb function if this is an unsolicited iocb.
3049 * The function frees the resources or calls the completion handler if this
3050 * iocb is an abort completion. The function returns NULL when the response
3051 * iocb has the LE bit set and all the chained iocbs are processed, otherwise
3052 * this function shall chain the iocb on to the iocb_continueq and return the
3053 * response iocb passed in.
3054 **/
3055static struct lpfc_iocbq *
3056lpfc_sli_sp_handle_rspiocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
3057 struct lpfc_iocbq *rspiocbp)
3058{
3059 struct lpfc_iocbq *saveq;
3060 struct lpfc_iocbq *cmdiocbp;
3061 struct lpfc_iocbq *next_iocb;
3062 IOCB_t *irsp = NULL;
3063 uint32_t free_saveq;
3064 uint8_t iocb_cmd_type;
3065 lpfc_iocb_type type;
3066 unsigned long iflag;
3067 int rc;
3068
3069 spin_lock_irqsave(&phba->hbalock, iflag);
3070 /* First add the response iocb to the countinueq list */
3071 list_add_tail(&rspiocbp->list, &(pring->iocb_continueq));
3072 pring->iocb_continueq_cnt++;
3073
Justin P. Mattock70f23fd2011-05-10 10:16:21 +02003074 /* Now, determine whether the list is completed for processing */
James Smart3772a992009-05-22 14:50:54 -04003075 irsp = &rspiocbp->iocb;
3076 if (irsp->ulpLe) {
3077 /*
3078 * By default, the driver expects to free all resources
3079 * associated with this iocb completion.
3080 */
3081 free_saveq = 1;
3082 saveq = list_get_first(&pring->iocb_continueq,
3083 struct lpfc_iocbq, list);
3084 irsp = &(saveq->iocb);
3085 list_del_init(&pring->iocb_continueq);
3086 pring->iocb_continueq_cnt = 0;
3087
3088 pring->stats.iocb_rsp++;
3089
3090 /*
3091 * If resource errors reported from HBA, reduce
3092 * queuedepths of the SCSI device.
3093 */
3094 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
3095 (irsp->un.ulpWord[4] == IOERR_NO_RESOURCES)) {
3096 spin_unlock_irqrestore(&phba->hbalock, iflag);
3097 phba->lpfc_rampdown_queue_depth(phba);
3098 spin_lock_irqsave(&phba->hbalock, iflag);
3099 }
3100
3101 if (irsp->ulpStatus) {
3102 /* Rsp ring <ringno> error: IOCB */
3103 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
3104 "0328 Rsp Ring %d error: "
3105 "IOCB Data: "
3106 "x%x x%x x%x x%x "
3107 "x%x x%x x%x x%x "
3108 "x%x x%x x%x x%x "
3109 "x%x x%x x%x x%x\n",
3110 pring->ringno,
3111 irsp->un.ulpWord[0],
3112 irsp->un.ulpWord[1],
3113 irsp->un.ulpWord[2],
3114 irsp->un.ulpWord[3],
3115 irsp->un.ulpWord[4],
3116 irsp->un.ulpWord[5],
3117 *(((uint32_t *) irsp) + 6),
3118 *(((uint32_t *) irsp) + 7),
3119 *(((uint32_t *) irsp) + 8),
3120 *(((uint32_t *) irsp) + 9),
3121 *(((uint32_t *) irsp) + 10),
3122 *(((uint32_t *) irsp) + 11),
3123 *(((uint32_t *) irsp) + 12),
3124 *(((uint32_t *) irsp) + 13),
3125 *(((uint32_t *) irsp) + 14),
3126 *(((uint32_t *) irsp) + 15));
3127 }
3128
3129 /*
3130 * Fetch the IOCB command type and call the correct completion
3131 * routine. Solicited and Unsolicited IOCBs on the ELS ring
3132 * get freed back to the lpfc_iocb_list by the discovery
3133 * kernel thread.
3134 */
3135 iocb_cmd_type = irsp->ulpCommand & CMD_IOCB_MASK;
3136 type = lpfc_sli_iocb_cmd_type(iocb_cmd_type);
3137 switch (type) {
3138 case LPFC_SOL_IOCB:
3139 spin_unlock_irqrestore(&phba->hbalock, iflag);
3140 rc = lpfc_sli_process_sol_iocb(phba, pring, saveq);
3141 spin_lock_irqsave(&phba->hbalock, iflag);
3142 break;
3143
3144 case LPFC_UNSOL_IOCB:
3145 spin_unlock_irqrestore(&phba->hbalock, iflag);
3146 rc = lpfc_sli_process_unsol_iocb(phba, pring, saveq);
3147 spin_lock_irqsave(&phba->hbalock, iflag);
3148 if (!rc)
3149 free_saveq = 0;
3150 break;
3151
3152 case LPFC_ABORT_IOCB:
3153 cmdiocbp = NULL;
3154 if (irsp->ulpCommand != CMD_XRI_ABORTED_CX)
3155 cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring,
3156 saveq);
3157 if (cmdiocbp) {
3158 /* Call the specified completion routine */
3159 if (cmdiocbp->iocb_cmpl) {
3160 spin_unlock_irqrestore(&phba->hbalock,
3161 iflag);
3162 (cmdiocbp->iocb_cmpl)(phba, cmdiocbp,
3163 saveq);
3164 spin_lock_irqsave(&phba->hbalock,
3165 iflag);
3166 } else
3167 __lpfc_sli_release_iocbq(phba,
3168 cmdiocbp);
3169 }
3170 break;
3171
3172 case LPFC_UNKNOWN_IOCB:
3173 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
3174 char adaptermsg[LPFC_MAX_ADPTMSG];
3175 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
3176 memcpy(&adaptermsg[0], (uint8_t *)irsp,
3177 MAX_MSG_DATA);
3178 dev_warn(&((phba->pcidev)->dev),
3179 "lpfc%d: %s\n",
3180 phba->brd_no, adaptermsg);
3181 } else {
3182 /* Unknown IOCB command */
3183 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3184 "0335 Unknown IOCB "
3185 "command Data: x%x "
3186 "x%x x%x x%x\n",
3187 irsp->ulpCommand,
3188 irsp->ulpStatus,
3189 irsp->ulpIoTag,
3190 irsp->ulpContext);
3191 }
3192 break;
3193 }
3194
3195 if (free_saveq) {
3196 list_for_each_entry_safe(rspiocbp, next_iocb,
3197 &saveq->list, list) {
3198 list_del(&rspiocbp->list);
3199 __lpfc_sli_release_iocbq(phba, rspiocbp);
3200 }
3201 __lpfc_sli_release_iocbq(phba, saveq);
3202 }
3203 rspiocbp = NULL;
3204 }
3205 spin_unlock_irqrestore(&phba->hbalock, iflag);
3206 return rspiocbp;
3207}
3208
3209/**
3210 * lpfc_sli_handle_slow_ring_event - Wrapper func for handling slow-path iocbs
James Smarte59058c2008-08-24 21:49:00 -04003211 * @phba: Pointer to HBA context object.
3212 * @pring: Pointer to driver SLI ring object.
3213 * @mask: Host attention register mask for this ring.
3214 *
James Smart3772a992009-05-22 14:50:54 -04003215 * This routine wraps the actual slow_ring event process routine from the
3216 * API jump table function pointer from the lpfc_hba struct.
James Smarte59058c2008-08-24 21:49:00 -04003217 **/
James Smart3772a992009-05-22 14:50:54 -04003218void
James Smart2e0fef82007-06-17 19:56:36 -05003219lpfc_sli_handle_slow_ring_event(struct lpfc_hba *phba,
3220 struct lpfc_sli_ring *pring, uint32_t mask)
dea31012005-04-17 16:05:31 -05003221{
James Smart3772a992009-05-22 14:50:54 -04003222 phba->lpfc_sli_handle_slow_ring_event(phba, pring, mask);
3223}
3224
3225/**
3226 * lpfc_sli_handle_slow_ring_event_s3 - Handle SLI3 ring event for non-FCP rings
3227 * @phba: Pointer to HBA context object.
3228 * @pring: Pointer to driver SLI ring object.
3229 * @mask: Host attention register mask for this ring.
3230 *
3231 * This function is called from the worker thread when there is a ring event
3232 * for non-fcp rings. The caller does not hold any lock. The function will
3233 * remove each response iocb in the response ring and calls the handle
3234 * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
3235 **/
3236static void
3237lpfc_sli_handle_slow_ring_event_s3(struct lpfc_hba *phba,
3238 struct lpfc_sli_ring *pring, uint32_t mask)
3239{
James Smart34b02dc2008-08-24 21:49:55 -04003240 struct lpfc_pgp *pgp;
dea31012005-04-17 16:05:31 -05003241 IOCB_t *entry;
3242 IOCB_t *irsp = NULL;
3243 struct lpfc_iocbq *rspiocbp = NULL;
dea31012005-04-17 16:05:31 -05003244 uint32_t portRspPut, portRspMax;
dea31012005-04-17 16:05:31 -05003245 unsigned long iflag;
James Smart3772a992009-05-22 14:50:54 -04003246 uint32_t status;
dea31012005-04-17 16:05:31 -05003247
James Smart34b02dc2008-08-24 21:49:55 -04003248 pgp = &phba->port_gp[pring->ringno];
James Smart2e0fef82007-06-17 19:56:36 -05003249 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003250 pring->stats.iocb_event++;
3251
dea31012005-04-17 16:05:31 -05003252 /*
3253 * The next available response entry should never exceed the maximum
3254 * entries. If it does, treat it as an adapter hardware error.
3255 */
James Smart7e56aa22012-08-03 12:35:34 -04003256 portRspMax = pring->sli.sli3.numRiocb;
dea31012005-04-17 16:05:31 -05003257 portRspPut = le32_to_cpu(pgp->rspPutInx);
3258 if (portRspPut >= portRspMax) {
3259 /*
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02003260 * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
dea31012005-04-17 16:05:31 -05003261 * rsp ring <portRspMax>
3262 */
James Smarted957682007-06-17 19:56:37 -05003263 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003264 "0303 Ring %d handler: portRspPut %d "
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02003265 "is bigger than rsp ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04003266 pring->ringno, portRspPut, portRspMax);
dea31012005-04-17 16:05:31 -05003267
James Smart2e0fef82007-06-17 19:56:36 -05003268 phba->link_state = LPFC_HBA_ERROR;
3269 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003270
3271 phba->work_hs = HS_FFER3;
3272 lpfc_handle_eratt(phba);
3273
James Smart3772a992009-05-22 14:50:54 -04003274 return;
dea31012005-04-17 16:05:31 -05003275 }
3276
3277 rmb();
James Smart7e56aa22012-08-03 12:35:34 -04003278 while (pring->sli.sli3.rspidx != portRspPut) {
dea31012005-04-17 16:05:31 -05003279 /*
3280 * Build a completion list and call the appropriate handler.
3281 * The process is to get the next available response iocb, get
3282 * a free iocb from the list, copy the response data into the
3283 * free iocb, insert to the continuation list, and update the
3284 * next response index to slim. This process makes response
3285 * iocb's in the ring available to DMA as fast as possible but
3286 * pays a penalty for a copy operation. Since the iocb is
3287 * only 32 bytes, this penalty is considered small relative to
3288 * the PCI reads for register values and a slim write. When
3289 * the ulpLe field is set, the entire Command has been
3290 * received.
3291 */
James Smarted957682007-06-17 19:56:37 -05003292 entry = lpfc_resp_iocb(phba, pring);
3293
James Smart858c9f62007-06-17 19:56:39 -05003294 phba->last_completion_time = jiffies;
James Smart2e0fef82007-06-17 19:56:36 -05003295 rspiocbp = __lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05003296 if (rspiocbp == NULL) {
3297 printk(KERN_ERR "%s: out of buffers! Failing "
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07003298 "completion.\n", __func__);
dea31012005-04-17 16:05:31 -05003299 break;
3300 }
3301
James Smarted957682007-06-17 19:56:37 -05003302 lpfc_sli_pcimem_bcopy(entry, &rspiocbp->iocb,
3303 phba->iocb_rsp_size);
dea31012005-04-17 16:05:31 -05003304 irsp = &rspiocbp->iocb;
3305
James Smart7e56aa22012-08-03 12:35:34 -04003306 if (++pring->sli.sli3.rspidx >= portRspMax)
3307 pring->sli.sli3.rspidx = 0;
dea31012005-04-17 16:05:31 -05003308
James Smarta58cbd52007-08-02 11:09:43 -04003309 if (pring->ringno == LPFC_ELS_RING) {
3310 lpfc_debugfs_slow_ring_trc(phba,
3311 "IOCB rsp ring: wd4:x%08x wd6:x%08x wd7:x%08x",
3312 *(((uint32_t *) irsp) + 4),
3313 *(((uint32_t *) irsp) + 6),
3314 *(((uint32_t *) irsp) + 7));
3315 }
3316
James Smart7e56aa22012-08-03 12:35:34 -04003317 writel(pring->sli.sli3.rspidx,
3318 &phba->host_gp[pring->ringno].rspGetInx);
dea31012005-04-17 16:05:31 -05003319
James Smart3772a992009-05-22 14:50:54 -04003320 spin_unlock_irqrestore(&phba->hbalock, iflag);
3321 /* Handle the response IOCB */
3322 rspiocbp = lpfc_sli_sp_handle_rspiocb(phba, pring, rspiocbp);
3323 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003324
3325 /*
3326 * If the port response put pointer has not been updated, sync
3327 * the pgp->rspPutInx in the MAILBOX_tand fetch the new port
3328 * response put pointer.
3329 */
James Smart7e56aa22012-08-03 12:35:34 -04003330 if (pring->sli.sli3.rspidx == portRspPut) {
dea31012005-04-17 16:05:31 -05003331 portRspPut = le32_to_cpu(pgp->rspPutInx);
3332 }
James Smart7e56aa22012-08-03 12:35:34 -04003333 } /* while (pring->sli.sli3.rspidx != portRspPut) */
dea31012005-04-17 16:05:31 -05003334
James Smart92d7f7b2007-06-17 19:56:38 -05003335 if ((rspiocbp != NULL) && (mask & HA_R0RE_REQ)) {
dea31012005-04-17 16:05:31 -05003336 /* At least one response entry has been freed */
3337 pring->stats.iocb_rsp_full++;
3338 /* SET RxRE_RSP in Chip Att register */
3339 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
3340 writel(status, phba->CAregaddr);
3341 readl(phba->CAregaddr); /* flush */
3342 }
3343 if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
3344 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
3345 pring->stats.iocb_cmd_empty++;
3346
3347 /* Force update of the local copy of cmdGetInx */
James Smart7e56aa22012-08-03 12:35:34 -04003348 pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
dea31012005-04-17 16:05:31 -05003349 lpfc_sli_resume_iocb(phba, pring);
3350
3351 if ((pring->lpfc_sli_cmd_available))
3352 (pring->lpfc_sli_cmd_available) (phba, pring);
3353
3354 }
3355
James Smart2e0fef82007-06-17 19:56:36 -05003356 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart3772a992009-05-22 14:50:54 -04003357 return;
dea31012005-04-17 16:05:31 -05003358}
3359
James Smarte59058c2008-08-24 21:49:00 -04003360/**
James Smart4f774512009-05-22 14:52:35 -04003361 * lpfc_sli_handle_slow_ring_event_s4 - Handle SLI4 slow-path els events
3362 * @phba: Pointer to HBA context object.
3363 * @pring: Pointer to driver SLI ring object.
3364 * @mask: Host attention register mask for this ring.
3365 *
3366 * This function is called from the worker thread when there is a pending
3367 * ELS response iocb on the driver internal slow-path response iocb worker
3368 * queue. The caller does not hold any lock. The function will remove each
3369 * response iocb from the response worker queue and calls the handle
3370 * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
3371 **/
3372static void
3373lpfc_sli_handle_slow_ring_event_s4(struct lpfc_hba *phba,
3374 struct lpfc_sli_ring *pring, uint32_t mask)
3375{
3376 struct lpfc_iocbq *irspiocbq;
James Smart4d9ab992009-10-02 15:16:39 -04003377 struct hbq_dmabuf *dmabuf;
3378 struct lpfc_cq_event *cq_event;
James Smart4f774512009-05-22 14:52:35 -04003379 unsigned long iflag;
3380
James Smart45ed1192009-10-02 15:17:02 -04003381 spin_lock_irqsave(&phba->hbalock, iflag);
3382 phba->hba_flag &= ~HBA_SP_QUEUE_EVT;
3383 spin_unlock_irqrestore(&phba->hbalock, iflag);
3384 while (!list_empty(&phba->sli4_hba.sp_queue_event)) {
James Smart4f774512009-05-22 14:52:35 -04003385 /* Get the response iocb from the head of work queue */
3386 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart45ed1192009-10-02 15:17:02 -04003387 list_remove_head(&phba->sli4_hba.sp_queue_event,
James Smart4d9ab992009-10-02 15:16:39 -04003388 cq_event, struct lpfc_cq_event, list);
James Smart4f774512009-05-22 14:52:35 -04003389 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart4d9ab992009-10-02 15:16:39 -04003390
3391 switch (bf_get(lpfc_wcqe_c_code, &cq_event->cqe.wcqe_cmpl)) {
3392 case CQE_CODE_COMPL_WQE:
3393 irspiocbq = container_of(cq_event, struct lpfc_iocbq,
3394 cq_event);
James Smart45ed1192009-10-02 15:17:02 -04003395 /* Translate ELS WCQE to response IOCBQ */
3396 irspiocbq = lpfc_sli4_els_wcqe_to_rspiocbq(phba,
3397 irspiocbq);
3398 if (irspiocbq)
3399 lpfc_sli_sp_handle_rspiocb(phba, pring,
3400 irspiocbq);
James Smart4d9ab992009-10-02 15:16:39 -04003401 break;
3402 case CQE_CODE_RECEIVE:
James Smart7851fe22011-07-22 18:36:52 -04003403 case CQE_CODE_RECEIVE_V1:
James Smart4d9ab992009-10-02 15:16:39 -04003404 dmabuf = container_of(cq_event, struct hbq_dmabuf,
3405 cq_event);
3406 lpfc_sli4_handle_received_buffer(phba, dmabuf);
3407 break;
3408 default:
3409 break;
3410 }
James Smart4f774512009-05-22 14:52:35 -04003411 }
3412}
3413
3414/**
James Smart3621a712009-04-06 18:47:14 -04003415 * lpfc_sli_abort_iocb_ring - Abort all iocbs in the ring
James Smarte59058c2008-08-24 21:49:00 -04003416 * @phba: Pointer to HBA context object.
3417 * @pring: Pointer to driver SLI ring object.
3418 *
3419 * This function aborts all iocbs in the given ring and frees all the iocb
3420 * objects in txq. This function issues an abort iocb for all the iocb commands
3421 * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
3422 * the return of this function. The caller is not required to hold any locks.
3423 **/
James Smart2e0fef82007-06-17 19:56:36 -05003424void
dea31012005-04-17 16:05:31 -05003425lpfc_sli_abort_iocb_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
3426{
James Smart2534ba72007-04-25 09:52:20 -04003427 LIST_HEAD(completions);
dea31012005-04-17 16:05:31 -05003428 struct lpfc_iocbq *iocb, *next_iocb;
dea31012005-04-17 16:05:31 -05003429
James Smart92d7f7b2007-06-17 19:56:38 -05003430 if (pring->ringno == LPFC_ELS_RING) {
3431 lpfc_fabric_abort_hba(phba);
3432 }
3433
dea31012005-04-17 16:05:31 -05003434 /* Error everything on txq and txcmplq
3435 * First do the txq.
3436 */
James Smart2e0fef82007-06-17 19:56:36 -05003437 spin_lock_irq(&phba->hbalock);
James Smart2534ba72007-04-25 09:52:20 -04003438 list_splice_init(&pring->txq, &completions);
dea31012005-04-17 16:05:31 -05003439 pring->txq_cnt = 0;
dea31012005-04-17 16:05:31 -05003440
3441 /* Next issue ABTS for everything on the txcmplq */
James Smart2534ba72007-04-25 09:52:20 -04003442 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
3443 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
3444
James Smart2e0fef82007-06-17 19:56:36 -05003445 spin_unlock_irq(&phba->hbalock);
James Smart2534ba72007-04-25 09:52:20 -04003446
James Smarta257bf92009-04-06 18:48:10 -04003447 /* Cancel all the IOCBs from the completions list */
3448 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
3449 IOERR_SLI_ABORTED);
dea31012005-04-17 16:05:31 -05003450}
3451
James Smarte59058c2008-08-24 21:49:00 -04003452/**
James Smart3621a712009-04-06 18:47:14 -04003453 * lpfc_sli_flush_fcp_rings - flush all iocbs in the fcp ring
James Smarta8e497d2008-08-24 21:50:11 -04003454 * @phba: Pointer to HBA context object.
3455 *
3456 * This function flushes all iocbs in the fcp ring and frees all the iocb
3457 * objects in txq and txcmplq. This function will not issue abort iocbs
3458 * for all the iocb commands in txcmplq, they will just be returned with
3459 * IOERR_SLI_DOWN. This function is invoked with EEH when device's PCI
3460 * slot has been permanently disabled.
3461 **/
3462void
3463lpfc_sli_flush_fcp_rings(struct lpfc_hba *phba)
3464{
3465 LIST_HEAD(txq);
3466 LIST_HEAD(txcmplq);
James Smarta8e497d2008-08-24 21:50:11 -04003467 struct lpfc_sli *psli = &phba->sli;
3468 struct lpfc_sli_ring *pring;
3469
3470 /* Currently, only one fcp ring */
3471 pring = &psli->ring[psli->fcp_ring];
3472
3473 spin_lock_irq(&phba->hbalock);
3474 /* Retrieve everything on txq */
3475 list_splice_init(&pring->txq, &txq);
3476 pring->txq_cnt = 0;
3477
3478 /* Retrieve everything on the txcmplq */
3479 list_splice_init(&pring->txcmplq, &txcmplq);
3480 pring->txcmplq_cnt = 0;
James Smart4f2e66c2012-05-09 21:17:07 -04003481
3482 /* Indicate the I/O queues are flushed */
3483 phba->hba_flag |= HBA_FCP_IOQ_FLUSH;
James Smarta8e497d2008-08-24 21:50:11 -04003484 spin_unlock_irq(&phba->hbalock);
3485
3486 /* Flush the txq */
James Smarta257bf92009-04-06 18:48:10 -04003487 lpfc_sli_cancel_iocbs(phba, &txq, IOSTAT_LOCAL_REJECT,
3488 IOERR_SLI_DOWN);
James Smarta8e497d2008-08-24 21:50:11 -04003489
3490 /* Flush the txcmpq */
James Smarta257bf92009-04-06 18:48:10 -04003491 lpfc_sli_cancel_iocbs(phba, &txcmplq, IOSTAT_LOCAL_REJECT,
3492 IOERR_SLI_DOWN);
James Smarta8e497d2008-08-24 21:50:11 -04003493}
3494
3495/**
James Smart3772a992009-05-22 14:50:54 -04003496 * lpfc_sli_brdready_s3 - Check for sli3 host ready status
James Smarte59058c2008-08-24 21:49:00 -04003497 * @phba: Pointer to HBA context object.
3498 * @mask: Bit mask to be checked.
3499 *
3500 * This function reads the host status register and compares
3501 * with the provided bit mask to check if HBA completed
3502 * the restart. This function will wait in a loop for the
3503 * HBA to complete restart. If the HBA does not restart within
3504 * 15 iterations, the function will reset the HBA again. The
3505 * function returns 1 when HBA fail to restart otherwise returns
3506 * zero.
3507 **/
James Smart3772a992009-05-22 14:50:54 -04003508static int
3509lpfc_sli_brdready_s3(struct lpfc_hba *phba, uint32_t mask)
dea31012005-04-17 16:05:31 -05003510{
Jamie Wellnitz41415862006-02-28 19:25:27 -05003511 uint32_t status;
3512 int i = 0;
3513 int retval = 0;
dea31012005-04-17 16:05:31 -05003514
Jamie Wellnitz41415862006-02-28 19:25:27 -05003515 /* Read the HBA Host Status Register */
James Smart9940b972011-03-11 16:06:12 -05003516 if (lpfc_readl(phba->HSregaddr, &status))
3517 return 1;
dea31012005-04-17 16:05:31 -05003518
Jamie Wellnitz41415862006-02-28 19:25:27 -05003519 /*
3520 * Check status register every 100ms for 5 retries, then every
3521 * 500ms for 5, then every 2.5 sec for 5, then reset board and
3522 * every 2.5 sec for 4.
3523 * Break our of the loop if errors occurred during init.
3524 */
3525 while (((status & mask) != mask) &&
3526 !(status & HS_FFERM) &&
3527 i++ < 20) {
dea31012005-04-17 16:05:31 -05003528
Jamie Wellnitz41415862006-02-28 19:25:27 -05003529 if (i <= 5)
3530 msleep(10);
3531 else if (i <= 10)
3532 msleep(500);
3533 else
3534 msleep(2500);
dea31012005-04-17 16:05:31 -05003535
Jamie Wellnitz41415862006-02-28 19:25:27 -05003536 if (i == 15) {
James Smart2e0fef82007-06-17 19:56:36 -05003537 /* Do post */
James Smart92d7f7b2007-06-17 19:56:38 -05003538 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003539 lpfc_sli_brdrestart(phba);
3540 }
3541 /* Read the HBA Host Status Register */
James Smart9940b972011-03-11 16:06:12 -05003542 if (lpfc_readl(phba->HSregaddr, &status)) {
3543 retval = 1;
3544 break;
3545 }
dea31012005-04-17 16:05:31 -05003546 }
dea31012005-04-17 16:05:31 -05003547
Jamie Wellnitz41415862006-02-28 19:25:27 -05003548 /* Check to see if any errors occurred during init */
3549 if ((status & HS_FFERM) || (i >= 20)) {
James Smarte40a02c2010-02-26 14:13:54 -05003550 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3551 "2751 Adapter failed to restart, "
3552 "status reg x%x, FW Data: A8 x%x AC x%x\n",
3553 status,
3554 readl(phba->MBslimaddr + 0xa8),
3555 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05003556 phba->link_state = LPFC_HBA_ERROR;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003557 retval = 1;
3558 }
dea31012005-04-17 16:05:31 -05003559
Jamie Wellnitz41415862006-02-28 19:25:27 -05003560 return retval;
dea31012005-04-17 16:05:31 -05003561}
3562
James Smartda0436e2009-05-22 14:51:39 -04003563/**
3564 * lpfc_sli_brdready_s4 - Check for sli4 host ready status
3565 * @phba: Pointer to HBA context object.
3566 * @mask: Bit mask to be checked.
3567 *
3568 * This function checks the host status register to check if HBA is
3569 * ready. This function will wait in a loop for the HBA to be ready
3570 * If the HBA is not ready , the function will will reset the HBA PCI
3571 * function again. The function returns 1 when HBA fail to be ready
3572 * otherwise returns zero.
3573 **/
3574static int
3575lpfc_sli_brdready_s4(struct lpfc_hba *phba, uint32_t mask)
3576{
3577 uint32_t status;
3578 int retval = 0;
3579
3580 /* Read the HBA Host Status Register */
3581 status = lpfc_sli4_post_status_check(phba);
3582
3583 if (status) {
3584 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
3585 lpfc_sli_brdrestart(phba);
3586 status = lpfc_sli4_post_status_check(phba);
3587 }
3588
3589 /* Check to see if any errors occurred during init */
3590 if (status) {
3591 phba->link_state = LPFC_HBA_ERROR;
3592 retval = 1;
3593 } else
3594 phba->sli4_hba.intr_enable = 0;
3595
3596 return retval;
3597}
3598
3599/**
3600 * lpfc_sli_brdready - Wrapper func for checking the hba readyness
3601 * @phba: Pointer to HBA context object.
3602 * @mask: Bit mask to be checked.
3603 *
3604 * This routine wraps the actual SLI3 or SLI4 hba readyness check routine
3605 * from the API jump table function pointer from the lpfc_hba struct.
3606 **/
3607int
3608lpfc_sli_brdready(struct lpfc_hba *phba, uint32_t mask)
3609{
3610 return phba->lpfc_sli_brdready(phba, mask);
3611}
3612
James Smart92908312006-03-07 15:04:13 -05003613#define BARRIER_TEST_PATTERN (0xdeadbeef)
3614
James Smarte59058c2008-08-24 21:49:00 -04003615/**
James Smart3621a712009-04-06 18:47:14 -04003616 * lpfc_reset_barrier - Make HBA ready for HBA reset
James Smarte59058c2008-08-24 21:49:00 -04003617 * @phba: Pointer to HBA context object.
3618 *
James Smart1b511972011-12-13 13:23:09 -05003619 * This function is called before resetting an HBA. This function is called
3620 * with hbalock held and requests HBA to quiesce DMAs before a reset.
James Smarte59058c2008-08-24 21:49:00 -04003621 **/
James Smart2e0fef82007-06-17 19:56:36 -05003622void lpfc_reset_barrier(struct lpfc_hba *phba)
James Smart92908312006-03-07 15:04:13 -05003623{
James Smart65a29c12006-07-06 15:50:50 -04003624 uint32_t __iomem *resp_buf;
3625 uint32_t __iomem *mbox_buf;
James Smart92908312006-03-07 15:04:13 -05003626 volatile uint32_t mbox;
James Smart9940b972011-03-11 16:06:12 -05003627 uint32_t hc_copy, ha_copy, resp_data;
James Smart92908312006-03-07 15:04:13 -05003628 int i;
3629 uint8_t hdrtype;
3630
3631 pci_read_config_byte(phba->pcidev, PCI_HEADER_TYPE, &hdrtype);
3632 if (hdrtype != 0x80 ||
3633 (FC_JEDEC_ID(phba->vpd.rev.biuRev) != HELIOS_JEDEC_ID &&
3634 FC_JEDEC_ID(phba->vpd.rev.biuRev) != THOR_JEDEC_ID))
3635 return;
3636
3637 /*
3638 * Tell the other part of the chip to suspend temporarily all
3639 * its DMA activity.
3640 */
James Smart65a29c12006-07-06 15:50:50 -04003641 resp_buf = phba->MBslimaddr;
James Smart92908312006-03-07 15:04:13 -05003642
3643 /* Disable the error attention */
James Smart9940b972011-03-11 16:06:12 -05003644 if (lpfc_readl(phba->HCregaddr, &hc_copy))
3645 return;
James Smart92908312006-03-07 15:04:13 -05003646 writel((hc_copy & ~HC_ERINT_ENA), phba->HCregaddr);
3647 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05003648 phba->link_flag |= LS_IGNORE_ERATT;
James Smart92908312006-03-07 15:04:13 -05003649
James Smart9940b972011-03-11 16:06:12 -05003650 if (lpfc_readl(phba->HAregaddr, &ha_copy))
3651 return;
3652 if (ha_copy & HA_ERATT) {
James Smart92908312006-03-07 15:04:13 -05003653 /* Clear Chip error bit */
3654 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05003655 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05003656 }
3657
3658 mbox = 0;
3659 ((MAILBOX_t *)&mbox)->mbxCommand = MBX_KILL_BOARD;
3660 ((MAILBOX_t *)&mbox)->mbxOwner = OWN_CHIP;
3661
3662 writel(BARRIER_TEST_PATTERN, (resp_buf + 1));
James Smart65a29c12006-07-06 15:50:50 -04003663 mbox_buf = phba->MBslimaddr;
James Smart92908312006-03-07 15:04:13 -05003664 writel(mbox, mbox_buf);
3665
James Smart9940b972011-03-11 16:06:12 -05003666 for (i = 0; i < 50; i++) {
3667 if (lpfc_readl((resp_buf + 1), &resp_data))
3668 return;
3669 if (resp_data != ~(BARRIER_TEST_PATTERN))
3670 mdelay(1);
3671 else
3672 break;
3673 }
3674 resp_data = 0;
3675 if (lpfc_readl((resp_buf + 1), &resp_data))
3676 return;
3677 if (resp_data != ~(BARRIER_TEST_PATTERN)) {
James Smartf4b4c682009-05-22 14:53:12 -04003678 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE ||
James Smart2e0fef82007-06-17 19:56:36 -05003679 phba->pport->stopped)
James Smart92908312006-03-07 15:04:13 -05003680 goto restore_hc;
3681 else
3682 goto clear_errat;
3683 }
3684
3685 ((MAILBOX_t *)&mbox)->mbxOwner = OWN_HOST;
James Smart9940b972011-03-11 16:06:12 -05003686 resp_data = 0;
3687 for (i = 0; i < 500; i++) {
3688 if (lpfc_readl(resp_buf, &resp_data))
3689 return;
3690 if (resp_data != mbox)
3691 mdelay(1);
3692 else
3693 break;
3694 }
James Smart92908312006-03-07 15:04:13 -05003695
3696clear_errat:
3697
James Smart9940b972011-03-11 16:06:12 -05003698 while (++i < 500) {
3699 if (lpfc_readl(phba->HAregaddr, &ha_copy))
3700 return;
3701 if (!(ha_copy & HA_ERATT))
3702 mdelay(1);
3703 else
3704 break;
3705 }
James Smart92908312006-03-07 15:04:13 -05003706
3707 if (readl(phba->HAregaddr) & HA_ERATT) {
3708 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05003709 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05003710 }
3711
3712restore_hc:
James Smart2e0fef82007-06-17 19:56:36 -05003713 phba->link_flag &= ~LS_IGNORE_ERATT;
James Smart92908312006-03-07 15:04:13 -05003714 writel(hc_copy, phba->HCregaddr);
3715 readl(phba->HCregaddr); /* flush */
3716}
3717
James Smarte59058c2008-08-24 21:49:00 -04003718/**
James Smart3621a712009-04-06 18:47:14 -04003719 * lpfc_sli_brdkill - Issue a kill_board mailbox command
James Smarte59058c2008-08-24 21:49:00 -04003720 * @phba: Pointer to HBA context object.
3721 *
3722 * This function issues a kill_board mailbox command and waits for
3723 * the error attention interrupt. This function is called for stopping
3724 * the firmware processing. The caller is not required to hold any
3725 * locks. This function calls lpfc_hba_down_post function to free
3726 * any pending commands after the kill. The function will return 1 when it
3727 * fails to kill the board else will return 0.
3728 **/
Jamie Wellnitz41415862006-02-28 19:25:27 -05003729int
James Smart2e0fef82007-06-17 19:56:36 -05003730lpfc_sli_brdkill(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05003731{
Jamie Wellnitz41415862006-02-28 19:25:27 -05003732 struct lpfc_sli *psli;
3733 LPFC_MBOXQ_t *pmb;
3734 uint32_t status;
3735 uint32_t ha_copy;
3736 int retval;
3737 int i = 0;
3738
3739 psli = &phba->sli;
3740
3741 /* Kill HBA */
James Smarted957682007-06-17 19:56:37 -05003742 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003743 "0329 Kill HBA Data: x%x x%x\n",
3744 phba->pport->port_state, psli->sli_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003745
James Smart98c9ea52007-10-27 13:37:33 -04003746 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3747 if (!pmb)
Jamie Wellnitz41415862006-02-28 19:25:27 -05003748 return 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003749
3750 /* Disable the error attention */
James Smart2e0fef82007-06-17 19:56:36 -05003751 spin_lock_irq(&phba->hbalock);
James Smart9940b972011-03-11 16:06:12 -05003752 if (lpfc_readl(phba->HCregaddr, &status)) {
3753 spin_unlock_irq(&phba->hbalock);
3754 mempool_free(pmb, phba->mbox_mem_pool);
3755 return 1;
3756 }
Jamie Wellnitz41415862006-02-28 19:25:27 -05003757 status &= ~HC_ERINT_ENA;
3758 writel(status, phba->HCregaddr);
3759 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05003760 phba->link_flag |= LS_IGNORE_ERATT;
3761 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003762
3763 lpfc_kill_board(phba, pmb);
3764 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
3765 retval = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
3766
3767 if (retval != MBX_SUCCESS) {
3768 if (retval != MBX_BUSY)
3769 mempool_free(pmb, phba->mbox_mem_pool);
James Smarte40a02c2010-02-26 14:13:54 -05003770 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3771 "2752 KILL_BOARD command failed retval %d\n",
3772 retval);
James Smart2e0fef82007-06-17 19:56:36 -05003773 spin_lock_irq(&phba->hbalock);
3774 phba->link_flag &= ~LS_IGNORE_ERATT;
3775 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003776 return 1;
3777 }
3778
James Smartf4b4c682009-05-22 14:53:12 -04003779 spin_lock_irq(&phba->hbalock);
3780 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
3781 spin_unlock_irq(&phba->hbalock);
James Smart92908312006-03-07 15:04:13 -05003782
Jamie Wellnitz41415862006-02-28 19:25:27 -05003783 mempool_free(pmb, phba->mbox_mem_pool);
3784
3785 /* There is no completion for a KILL_BOARD mbox cmd. Check for an error
3786 * attention every 100ms for 3 seconds. If we don't get ERATT after
3787 * 3 seconds we still set HBA_ERROR state because the status of the
3788 * board is now undefined.
3789 */
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 while ((i++ < 30) && !(ha_copy & HA_ERATT)) {
3793 mdelay(100);
James Smart9940b972011-03-11 16:06:12 -05003794 if (lpfc_readl(phba->HAregaddr, &ha_copy))
3795 return 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003796 }
3797
3798 del_timer_sync(&psli->mbox_tmo);
James Smart92908312006-03-07 15:04:13 -05003799 if (ha_copy & HA_ERATT) {
3800 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05003801 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05003802 }
James Smart2e0fef82007-06-17 19:56:36 -05003803 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003804 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart04c68492009-05-22 14:52:52 -04003805 psli->mbox_active = NULL;
James Smart2e0fef82007-06-17 19:56:36 -05003806 phba->link_flag &= ~LS_IGNORE_ERATT;
3807 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003808
Jamie Wellnitz41415862006-02-28 19:25:27 -05003809 lpfc_hba_down_post(phba);
James Smart2e0fef82007-06-17 19:56:36 -05003810 phba->link_state = LPFC_HBA_ERROR;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003811
James Smart2e0fef82007-06-17 19:56:36 -05003812 return ha_copy & HA_ERATT ? 0 : 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003813}
3814
James Smarte59058c2008-08-24 21:49:00 -04003815/**
James Smart3772a992009-05-22 14:50:54 -04003816 * lpfc_sli_brdreset - Reset a sli-2 or sli-3 HBA
James Smarte59058c2008-08-24 21:49:00 -04003817 * @phba: Pointer to HBA context object.
3818 *
3819 * This function resets the HBA by writing HC_INITFF to the control
3820 * register. After the HBA resets, this function resets all the iocb ring
3821 * indices. This function disables PCI layer parity checking during
3822 * the reset.
3823 * This function returns 0 always.
3824 * The caller is not required to hold any locks.
3825 **/
Jamie Wellnitz41415862006-02-28 19:25:27 -05003826int
James Smart2e0fef82007-06-17 19:56:36 -05003827lpfc_sli_brdreset(struct lpfc_hba *phba)
Jamie Wellnitz41415862006-02-28 19:25:27 -05003828{
3829 struct lpfc_sli *psli;
dea31012005-04-17 16:05:31 -05003830 struct lpfc_sli_ring *pring;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003831 uint16_t cfg_value;
dea31012005-04-17 16:05:31 -05003832 int i;
dea31012005-04-17 16:05:31 -05003833
Jamie Wellnitz41415862006-02-28 19:25:27 -05003834 psli = &phba->sli;
dea31012005-04-17 16:05:31 -05003835
Jamie Wellnitz41415862006-02-28 19:25:27 -05003836 /* Reset HBA */
3837 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003838 "0325 Reset HBA Data: x%x x%x\n",
James Smart2e0fef82007-06-17 19:56:36 -05003839 phba->pport->port_state, psli->sli_flag);
dea31012005-04-17 16:05:31 -05003840
3841 /* perform board reset */
3842 phba->fc_eventTag = 0;
James Smart4d9ab992009-10-02 15:16:39 -04003843 phba->link_events = 0;
James Smart2e0fef82007-06-17 19:56:36 -05003844 phba->pport->fc_myDID = 0;
3845 phba->pport->fc_prevDID = 0;
dea31012005-04-17 16:05:31 -05003846
Jamie Wellnitz41415862006-02-28 19:25:27 -05003847 /* Turn off parity checking and serr during the physical reset */
3848 pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
3849 pci_write_config_word(phba->pcidev, PCI_COMMAND,
3850 (cfg_value &
3851 ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
3852
James Smart3772a992009-05-22 14:50:54 -04003853 psli->sli_flag &= ~(LPFC_SLI_ACTIVE | LPFC_PROCESS_LA);
3854
Jamie Wellnitz41415862006-02-28 19:25:27 -05003855 /* Now toggle INITFF bit in the Host Control Register */
3856 writel(HC_INITFF, phba->HCregaddr);
3857 mdelay(1);
3858 readl(phba->HCregaddr); /* flush */
3859 writel(0, phba->HCregaddr);
3860 readl(phba->HCregaddr); /* flush */
3861
3862 /* Restore PCI cmd register */
3863 pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
dea31012005-04-17 16:05:31 -05003864
3865 /* Initialize relevant SLI info */
Jamie Wellnitz41415862006-02-28 19:25:27 -05003866 for (i = 0; i < psli->num_rings; i++) {
3867 pring = &psli->ring[i];
dea31012005-04-17 16:05:31 -05003868 pring->flag = 0;
James Smart7e56aa22012-08-03 12:35:34 -04003869 pring->sli.sli3.rspidx = 0;
3870 pring->sli.sli3.next_cmdidx = 0;
3871 pring->sli.sli3.local_getidx = 0;
3872 pring->sli.sli3.cmdidx = 0;
dea31012005-04-17 16:05:31 -05003873 pring->missbufcnt = 0;
3874 }
dea31012005-04-17 16:05:31 -05003875
James Smart2e0fef82007-06-17 19:56:36 -05003876 phba->link_state = LPFC_WARM_START;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003877 return 0;
3878}
3879
James Smarte59058c2008-08-24 21:49:00 -04003880/**
James Smartda0436e2009-05-22 14:51:39 -04003881 * lpfc_sli4_brdreset - Reset a sli-4 HBA
3882 * @phba: Pointer to HBA context object.
3883 *
3884 * This function resets a SLI4 HBA. This function disables PCI layer parity
3885 * checking during resets the device. The caller is not required to hold
3886 * any locks.
3887 *
3888 * This function returns 0 always.
3889 **/
3890int
3891lpfc_sli4_brdreset(struct lpfc_hba *phba)
3892{
3893 struct lpfc_sli *psli = &phba->sli;
3894 uint16_t cfg_value;
James Smart27b01b82012-05-09 21:19:44 -04003895 int rc;
James Smartda0436e2009-05-22 14:51:39 -04003896
3897 /* Reset HBA */
3898 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3899 "0295 Reset HBA Data: x%x x%x\n",
3900 phba->pport->port_state, psli->sli_flag);
3901
3902 /* perform board reset */
3903 phba->fc_eventTag = 0;
James Smart4d9ab992009-10-02 15:16:39 -04003904 phba->link_events = 0;
James Smartda0436e2009-05-22 14:51:39 -04003905 phba->pport->fc_myDID = 0;
3906 phba->pport->fc_prevDID = 0;
3907
James Smartda0436e2009-05-22 14:51:39 -04003908 spin_lock_irq(&phba->hbalock);
3909 psli->sli_flag &= ~(LPFC_PROCESS_LA);
3910 phba->fcf.fcf_flag = 0;
James Smartda0436e2009-05-22 14:51:39 -04003911 spin_unlock_irq(&phba->hbalock);
3912
3913 /* Now physically reset the device */
3914 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3915 "0389 Performing PCI function reset!\n");
James Smartbe858b62010-12-15 17:57:20 -05003916
3917 /* Turn off parity checking and serr during the physical reset */
3918 pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
3919 pci_write_config_word(phba->pcidev, PCI_COMMAND, (cfg_value &
3920 ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
3921
James Smartda0436e2009-05-22 14:51:39 -04003922 /* Perform FCoE PCI function reset */
James Smart2e90f4b2011-12-13 13:22:37 -05003923 lpfc_sli4_queue_destroy(phba);
James Smart27b01b82012-05-09 21:19:44 -04003924 rc = lpfc_pci_function_reset(phba);
James Smartda0436e2009-05-22 14:51:39 -04003925
James Smartbe858b62010-12-15 17:57:20 -05003926 /* Restore PCI cmd register */
3927 pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
3928
James Smart27b01b82012-05-09 21:19:44 -04003929 return rc;
James Smartda0436e2009-05-22 14:51:39 -04003930}
3931
3932/**
3933 * lpfc_sli_brdrestart_s3 - Restart a sli-3 hba
James Smarte59058c2008-08-24 21:49:00 -04003934 * @phba: Pointer to HBA context object.
3935 *
3936 * This function is called in the SLI initialization code path to
3937 * restart the HBA. The caller is not required to hold any lock.
3938 * This function writes MBX_RESTART mailbox command to the SLIM and
3939 * resets the HBA. At the end of the function, it calls lpfc_hba_down_post
3940 * function to free any pending commands. The function enables
3941 * POST only during the first initialization. The function returns zero.
3942 * The function does not guarantee completion of MBX_RESTART mailbox
3943 * command before the return of this function.
3944 **/
James Smartda0436e2009-05-22 14:51:39 -04003945static int
3946lpfc_sli_brdrestart_s3(struct lpfc_hba *phba)
Jamie Wellnitz41415862006-02-28 19:25:27 -05003947{
3948 MAILBOX_t *mb;
3949 struct lpfc_sli *psli;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003950 volatile uint32_t word0;
3951 void __iomem *to_slim;
James Smart0d878412009-10-02 15:16:56 -04003952 uint32_t hba_aer_enabled;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003953
James Smart2e0fef82007-06-17 19:56:36 -05003954 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003955
James Smart0d878412009-10-02 15:16:56 -04003956 /* Take PCIe device Advanced Error Reporting (AER) state */
3957 hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
3958
Jamie Wellnitz41415862006-02-28 19:25:27 -05003959 psli = &phba->sli;
3960
3961 /* Restart HBA */
3962 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003963 "0337 Restart HBA Data: x%x x%x\n",
James Smart2e0fef82007-06-17 19:56:36 -05003964 phba->pport->port_state, psli->sli_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003965
3966 word0 = 0;
3967 mb = (MAILBOX_t *) &word0;
3968 mb->mbxCommand = MBX_RESTART;
3969 mb->mbxHc = 1;
3970
James Smart92908312006-03-07 15:04:13 -05003971 lpfc_reset_barrier(phba);
3972
Jamie Wellnitz41415862006-02-28 19:25:27 -05003973 to_slim = phba->MBslimaddr;
3974 writel(*(uint32_t *) mb, to_slim);
3975 readl(to_slim); /* flush */
3976
3977 /* Only skip post after fc_ffinit is completed */
James Smarteaf15d52008-12-04 22:39:29 -05003978 if (phba->pport->port_state)
Jamie Wellnitz41415862006-02-28 19:25:27 -05003979 word0 = 1; /* This is really setting up word1 */
James Smarteaf15d52008-12-04 22:39:29 -05003980 else
Jamie Wellnitz41415862006-02-28 19:25:27 -05003981 word0 = 0; /* This is really setting up word1 */
James Smart65a29c12006-07-06 15:50:50 -04003982 to_slim = phba->MBslimaddr + sizeof (uint32_t);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003983 writel(*(uint32_t *) mb, to_slim);
3984 readl(to_slim); /* flush */
3985
3986 lpfc_sli_brdreset(phba);
James Smart2e0fef82007-06-17 19:56:36 -05003987 phba->pport->stopped = 0;
3988 phba->link_state = LPFC_INIT_START;
James Smartda0436e2009-05-22 14:51:39 -04003989 phba->hba_flag = 0;
James Smart2e0fef82007-06-17 19:56:36 -05003990 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003991
James Smart64ba8812006-08-02 15:24:34 -04003992 memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
3993 psli->stats_start = get_seconds();
3994
James Smarteaf15d52008-12-04 22:39:29 -05003995 /* Give the INITFF and Post time to settle. */
3996 mdelay(100);
dea31012005-04-17 16:05:31 -05003997
James Smart0d878412009-10-02 15:16:56 -04003998 /* Reset HBA AER if it was enabled, note hba_flag was reset above */
3999 if (hba_aer_enabled)
4000 pci_disable_pcie_error_reporting(phba->pcidev);
4001
Jamie Wellnitz41415862006-02-28 19:25:27 -05004002 lpfc_hba_down_post(phba);
dea31012005-04-17 16:05:31 -05004003
4004 return 0;
4005}
4006
James Smarte59058c2008-08-24 21:49:00 -04004007/**
James Smartda0436e2009-05-22 14:51:39 -04004008 * lpfc_sli_brdrestart_s4 - Restart the sli-4 hba
4009 * @phba: Pointer to HBA context object.
4010 *
4011 * This function is called in the SLI initialization code path to restart
4012 * a SLI4 HBA. The caller is not required to hold any lock.
4013 * At the end of the function, it calls lpfc_hba_down_post function to
4014 * free any pending commands.
4015 **/
4016static int
4017lpfc_sli_brdrestart_s4(struct lpfc_hba *phba)
4018{
4019 struct lpfc_sli *psli = &phba->sli;
James Smart75baf692010-06-08 18:31:21 -04004020 uint32_t hba_aer_enabled;
James Smart27b01b82012-05-09 21:19:44 -04004021 int rc;
James Smartda0436e2009-05-22 14:51:39 -04004022
4023 /* Restart HBA */
4024 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4025 "0296 Restart HBA Data: x%x x%x\n",
4026 phba->pport->port_state, psli->sli_flag);
4027
James Smart75baf692010-06-08 18:31:21 -04004028 /* Take PCIe device Advanced Error Reporting (AER) state */
4029 hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
4030
James Smart27b01b82012-05-09 21:19:44 -04004031 rc = lpfc_sli4_brdreset(phba);
James Smartda0436e2009-05-22 14:51:39 -04004032
4033 spin_lock_irq(&phba->hbalock);
4034 phba->pport->stopped = 0;
4035 phba->link_state = LPFC_INIT_START;
4036 phba->hba_flag = 0;
4037 spin_unlock_irq(&phba->hbalock);
4038
4039 memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
4040 psli->stats_start = get_seconds();
4041
James Smart75baf692010-06-08 18:31:21 -04004042 /* Reset HBA AER if it was enabled, note hba_flag was reset above */
4043 if (hba_aer_enabled)
4044 pci_disable_pcie_error_reporting(phba->pcidev);
4045
James Smartda0436e2009-05-22 14:51:39 -04004046 lpfc_hba_down_post(phba);
4047
James Smart27b01b82012-05-09 21:19:44 -04004048 return rc;
James Smartda0436e2009-05-22 14:51:39 -04004049}
4050
4051/**
4052 * lpfc_sli_brdrestart - Wrapper func for restarting hba
4053 * @phba: Pointer to HBA context object.
4054 *
4055 * This routine wraps the actual SLI3 or SLI4 hba restart routine from the
4056 * API jump table function pointer from the lpfc_hba struct.
4057**/
4058int
4059lpfc_sli_brdrestart(struct lpfc_hba *phba)
4060{
4061 return phba->lpfc_sli_brdrestart(phba);
4062}
4063
4064/**
James Smart3621a712009-04-06 18:47:14 -04004065 * lpfc_sli_chipset_init - Wait for the restart of the HBA after a restart
James Smarte59058c2008-08-24 21:49:00 -04004066 * @phba: Pointer to HBA context object.
4067 *
4068 * This function is called after a HBA restart to wait for successful
4069 * restart of the HBA. Successful restart of the HBA is indicated by
4070 * HS_FFRDY and HS_MBRDY bits. If the HBA fails to restart even after 15
4071 * iteration, the function will restart the HBA again. The function returns
4072 * zero if HBA successfully restarted else returns negative error code.
4073 **/
dea31012005-04-17 16:05:31 -05004074static int
4075lpfc_sli_chipset_init(struct lpfc_hba *phba)
4076{
4077 uint32_t status, i = 0;
4078
4079 /* Read the HBA Host Status Register */
James Smart9940b972011-03-11 16:06:12 -05004080 if (lpfc_readl(phba->HSregaddr, &status))
4081 return -EIO;
dea31012005-04-17 16:05:31 -05004082
4083 /* Check status register to see what current state is */
4084 i = 0;
4085 while ((status & (HS_FFRDY | HS_MBRDY)) != (HS_FFRDY | HS_MBRDY)) {
4086
James Smartdcf2a4e2010-09-29 11:18:53 -04004087 /* Check every 10ms for 10 retries, then every 100ms for 90
4088 * retries, then every 1 sec for 50 retires for a total of
4089 * ~60 seconds before reset the board again and check every
4090 * 1 sec for 50 retries. The up to 60 seconds before the
4091 * board ready is required by the Falcon FIPS zeroization
4092 * complete, and any reset the board in between shall cause
4093 * restart of zeroization, further delay the board ready.
dea31012005-04-17 16:05:31 -05004094 */
James Smartdcf2a4e2010-09-29 11:18:53 -04004095 if (i++ >= 200) {
dea31012005-04-17 16:05:31 -05004096 /* Adapter failed to init, timeout, status reg
4097 <status> */
James Smarted957682007-06-17 19:56:37 -05004098 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004099 "0436 Adapter failed to init, "
James Smart09372822008-01-11 01:52:54 -05004100 "timeout, status reg x%x, "
4101 "FW Data: A8 x%x AC x%x\n", status,
4102 readl(phba->MBslimaddr + 0xa8),
4103 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05004104 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05004105 return -ETIMEDOUT;
4106 }
4107
4108 /* Check to see if any errors occurred during init */
4109 if (status & HS_FFERM) {
4110 /* ERROR: During chipset initialization */
4111 /* Adapter failed to init, chipset, status reg
4112 <status> */
James Smarted957682007-06-17 19:56:37 -05004113 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004114 "0437 Adapter failed to init, "
James Smart09372822008-01-11 01:52:54 -05004115 "chipset, status reg x%x, "
4116 "FW Data: A8 x%x AC x%x\n", status,
4117 readl(phba->MBslimaddr + 0xa8),
4118 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05004119 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05004120 return -EIO;
4121 }
4122
James Smartdcf2a4e2010-09-29 11:18:53 -04004123 if (i <= 10)
dea31012005-04-17 16:05:31 -05004124 msleep(10);
James Smartdcf2a4e2010-09-29 11:18:53 -04004125 else if (i <= 100)
4126 msleep(100);
4127 else
4128 msleep(1000);
dea31012005-04-17 16:05:31 -05004129
James Smartdcf2a4e2010-09-29 11:18:53 -04004130 if (i == 150) {
4131 /* Do post */
James Smart92d7f7b2007-06-17 19:56:38 -05004132 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004133 lpfc_sli_brdrestart(phba);
dea31012005-04-17 16:05:31 -05004134 }
4135 /* Read the HBA Host Status Register */
James Smart9940b972011-03-11 16:06:12 -05004136 if (lpfc_readl(phba->HSregaddr, &status))
4137 return -EIO;
dea31012005-04-17 16:05:31 -05004138 }
4139
4140 /* Check to see if any errors occurred during init */
4141 if (status & HS_FFERM) {
4142 /* ERROR: During chipset initialization */
4143 /* Adapter failed to init, chipset, status reg <status> */
James Smarted957682007-06-17 19:56:37 -05004144 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004145 "0438 Adapter failed to init, chipset, "
James Smart09372822008-01-11 01:52:54 -05004146 "status reg x%x, "
4147 "FW Data: A8 x%x AC x%x\n", status,
4148 readl(phba->MBslimaddr + 0xa8),
4149 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05004150 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05004151 return -EIO;
4152 }
4153
4154 /* Clear all interrupt enable conditions */
4155 writel(0, phba->HCregaddr);
4156 readl(phba->HCregaddr); /* flush */
4157
4158 /* setup host attn register */
4159 writel(0xffffffff, phba->HAregaddr);
4160 readl(phba->HAregaddr); /* flush */
4161 return 0;
4162}
4163
James Smarte59058c2008-08-24 21:49:00 -04004164/**
James Smart3621a712009-04-06 18:47:14 -04004165 * lpfc_sli_hbq_count - Get the number of HBQs to be configured
James Smarte59058c2008-08-24 21:49:00 -04004166 *
4167 * This function calculates and returns the number of HBQs required to be
4168 * configured.
4169 **/
James Smart78b2d852007-08-02 11:10:21 -04004170int
James Smarted957682007-06-17 19:56:37 -05004171lpfc_sli_hbq_count(void)
4172{
James Smart92d7f7b2007-06-17 19:56:38 -05004173 return ARRAY_SIZE(lpfc_hbq_defs);
James Smarted957682007-06-17 19:56:37 -05004174}
4175
James Smarte59058c2008-08-24 21:49:00 -04004176/**
James Smart3621a712009-04-06 18:47:14 -04004177 * lpfc_sli_hbq_entry_count - Calculate total number of hbq entries
James Smarte59058c2008-08-24 21:49:00 -04004178 *
4179 * This function adds the number of hbq entries in every HBQ to get
4180 * the total number of hbq entries required for the HBA and returns
4181 * the total count.
4182 **/
James Smarted957682007-06-17 19:56:37 -05004183static int
4184lpfc_sli_hbq_entry_count(void)
4185{
4186 int hbq_count = lpfc_sli_hbq_count();
4187 int count = 0;
4188 int i;
4189
4190 for (i = 0; i < hbq_count; ++i)
James Smart92d7f7b2007-06-17 19:56:38 -05004191 count += lpfc_hbq_defs[i]->entry_count;
James Smarted957682007-06-17 19:56:37 -05004192 return count;
4193}
4194
James Smarte59058c2008-08-24 21:49:00 -04004195/**
James Smart3621a712009-04-06 18:47:14 -04004196 * lpfc_sli_hbq_size - Calculate memory required for all hbq entries
James Smarte59058c2008-08-24 21:49:00 -04004197 *
4198 * This function calculates amount of memory required for all hbq entries
4199 * to be configured and returns the total memory required.
4200 **/
dea31012005-04-17 16:05:31 -05004201int
James Smarted957682007-06-17 19:56:37 -05004202lpfc_sli_hbq_size(void)
4203{
4204 return lpfc_sli_hbq_entry_count() * sizeof(struct lpfc_hbq_entry);
4205}
4206
James Smarte59058c2008-08-24 21:49:00 -04004207/**
James Smart3621a712009-04-06 18:47:14 -04004208 * lpfc_sli_hbq_setup - configure and initialize HBQs
James Smarte59058c2008-08-24 21:49:00 -04004209 * @phba: Pointer to HBA context object.
4210 *
4211 * This function is called during the SLI initialization to configure
4212 * all the HBQs and post buffers to the HBQ. The caller is not
4213 * required to hold any locks. This function will return zero if successful
4214 * else it will return negative error code.
4215 **/
James Smarted957682007-06-17 19:56:37 -05004216static int
4217lpfc_sli_hbq_setup(struct lpfc_hba *phba)
4218{
4219 int hbq_count = lpfc_sli_hbq_count();
4220 LPFC_MBOXQ_t *pmb;
4221 MAILBOX_t *pmbox;
4222 uint32_t hbqno;
4223 uint32_t hbq_entry_index;
James Smarted957682007-06-17 19:56:37 -05004224
James Smart92d7f7b2007-06-17 19:56:38 -05004225 /* Get a Mailbox buffer to setup mailbox
4226 * commands for HBA initialization
4227 */
James Smarted957682007-06-17 19:56:37 -05004228 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4229
4230 if (!pmb)
4231 return -ENOMEM;
4232
James Smart04c68492009-05-22 14:52:52 -04004233 pmbox = &pmb->u.mb;
James Smarted957682007-06-17 19:56:37 -05004234
4235 /* Initialize the struct lpfc_sli_hbq structure for each hbq */
4236 phba->link_state = LPFC_INIT_MBX_CMDS;
James Smart3163f722008-02-08 18:50:25 -05004237 phba->hbq_in_use = 1;
James Smarted957682007-06-17 19:56:37 -05004238
4239 hbq_entry_index = 0;
4240 for (hbqno = 0; hbqno < hbq_count; ++hbqno) {
4241 phba->hbqs[hbqno].next_hbqPutIdx = 0;
4242 phba->hbqs[hbqno].hbqPutIdx = 0;
4243 phba->hbqs[hbqno].local_hbqGetIdx = 0;
4244 phba->hbqs[hbqno].entry_count =
James Smart92d7f7b2007-06-17 19:56:38 -05004245 lpfc_hbq_defs[hbqno]->entry_count;
James Smart51ef4c22007-08-02 11:10:31 -04004246 lpfc_config_hbq(phba, hbqno, lpfc_hbq_defs[hbqno],
4247 hbq_entry_index, pmb);
James Smarted957682007-06-17 19:56:37 -05004248 hbq_entry_index += phba->hbqs[hbqno].entry_count;
4249
4250 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
4251 /* Adapter failed to init, mbxCmd <cmd> CFG_RING,
4252 mbxStatus <status>, ring <num> */
4253
4254 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -05004255 LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04004256 "1805 Adapter failed to init. "
James Smarted957682007-06-17 19:56:37 -05004257 "Data: x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04004258 pmbox->mbxCommand,
James Smarted957682007-06-17 19:56:37 -05004259 pmbox->mbxStatus, hbqno);
4260
4261 phba->link_state = LPFC_HBA_ERROR;
4262 mempool_free(pmb, phba->mbox_mem_pool);
James Smart6e7288d2010-06-07 15:23:35 -04004263 return -ENXIO;
James Smarted957682007-06-17 19:56:37 -05004264 }
4265 }
4266 phba->hbq_count = hbq_count;
4267
James Smarted957682007-06-17 19:56:37 -05004268 mempool_free(pmb, phba->mbox_mem_pool);
4269
James Smart92d7f7b2007-06-17 19:56:38 -05004270 /* Initially populate or replenish the HBQs */
James Smartd7c255b2008-08-24 21:50:00 -04004271 for (hbqno = 0; hbqno < hbq_count; ++hbqno)
4272 lpfc_sli_hbqbuf_init_hbqs(phba, hbqno);
James Smarted957682007-06-17 19:56:37 -05004273 return 0;
4274}
4275
James Smarte59058c2008-08-24 21:49:00 -04004276/**
James Smart4f774512009-05-22 14:52:35 -04004277 * lpfc_sli4_rb_setup - Initialize and post RBs to HBA
4278 * @phba: Pointer to HBA context object.
4279 *
4280 * This function is called during the SLI initialization to configure
4281 * all the HBQs and post buffers to the HBQ. The caller is not
4282 * required to hold any locks. This function will return zero if successful
4283 * else it will return negative error code.
4284 **/
4285static int
4286lpfc_sli4_rb_setup(struct lpfc_hba *phba)
4287{
4288 phba->hbq_in_use = 1;
4289 phba->hbqs[0].entry_count = lpfc_hbq_defs[0]->entry_count;
4290 phba->hbq_count = 1;
4291 /* Initially populate or replenish the HBQs */
4292 lpfc_sli_hbqbuf_init_hbqs(phba, 0);
4293 return 0;
4294}
4295
4296/**
James Smart3621a712009-04-06 18:47:14 -04004297 * lpfc_sli_config_port - Issue config port mailbox command
James Smarte59058c2008-08-24 21:49:00 -04004298 * @phba: Pointer to HBA context object.
4299 * @sli_mode: sli mode - 2/3
4300 *
4301 * This function is called by the sli intialization code path
4302 * to issue config_port mailbox command. This function restarts the
4303 * HBA firmware and issues a config_port mailbox command to configure
4304 * the SLI interface in the sli mode specified by sli_mode
4305 * variable. The caller is not required to hold any locks.
4306 * The function returns 0 if successful, else returns negative error
4307 * code.
4308 **/
James Smart93996272008-08-24 21:50:30 -04004309int
4310lpfc_sli_config_port(struct lpfc_hba *phba, int sli_mode)
dea31012005-04-17 16:05:31 -05004311{
4312 LPFC_MBOXQ_t *pmb;
4313 uint32_t resetcount = 0, rc = 0, done = 0;
4314
4315 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4316 if (!pmb) {
James Smart2e0fef82007-06-17 19:56:36 -05004317 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05004318 return -ENOMEM;
4319 }
4320
James Smarted957682007-06-17 19:56:37 -05004321 phba->sli_rev = sli_mode;
dea31012005-04-17 16:05:31 -05004322 while (resetcount < 2 && !done) {
James Smart2e0fef82007-06-17 19:56:36 -05004323 spin_lock_irq(&phba->hbalock);
James Smart1c067a42006-08-01 07:33:52 -04004324 phba->sli.sli_flag |= LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05004325 spin_unlock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -05004326 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004327 lpfc_sli_brdrestart(phba);
dea31012005-04-17 16:05:31 -05004328 rc = lpfc_sli_chipset_init(phba);
4329 if (rc)
4330 break;
4331
James Smart2e0fef82007-06-17 19:56:36 -05004332 spin_lock_irq(&phba->hbalock);
James Smart1c067a42006-08-01 07:33:52 -04004333 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05004334 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004335 resetcount++;
4336
James Smarted957682007-06-17 19:56:37 -05004337 /* Call pre CONFIG_PORT mailbox command initialization. A
4338 * value of 0 means the call was successful. Any other
4339 * nonzero value is a failure, but if ERESTART is returned,
4340 * the driver may reset the HBA and try again.
4341 */
dea31012005-04-17 16:05:31 -05004342 rc = lpfc_config_port_prep(phba);
4343 if (rc == -ERESTART) {
James Smarted957682007-06-17 19:56:37 -05004344 phba->link_state = LPFC_LINK_UNKNOWN;
dea31012005-04-17 16:05:31 -05004345 continue;
James Smart34b02dc2008-08-24 21:49:55 -04004346 } else if (rc)
dea31012005-04-17 16:05:31 -05004347 break;
James Smart6d368e52011-05-24 11:44:12 -04004348
James Smart2e0fef82007-06-17 19:56:36 -05004349 phba->link_state = LPFC_INIT_MBX_CMDS;
dea31012005-04-17 16:05:31 -05004350 lpfc_config_port(phba, pmb);
4351 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
James Smart34b02dc2008-08-24 21:49:55 -04004352 phba->sli3_options &= ~(LPFC_SLI3_NPIV_ENABLED |
4353 LPFC_SLI3_HBQ_ENABLED |
4354 LPFC_SLI3_CRP_ENABLED |
James Smartbc739052010-08-04 16:11:18 -04004355 LPFC_SLI3_BG_ENABLED |
4356 LPFC_SLI3_DSS_ENABLED);
James Smarted957682007-06-17 19:56:37 -05004357 if (rc != MBX_SUCCESS) {
dea31012005-04-17 16:05:31 -05004358 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004359 "0442 Adapter failed to init, mbxCmd x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05004360 "CONFIG_PORT, mbxStatus x%x Data: x%x\n",
James Smart04c68492009-05-22 14:52:52 -04004361 pmb->u.mb.mbxCommand, pmb->u.mb.mbxStatus, 0);
James Smart2e0fef82007-06-17 19:56:36 -05004362 spin_lock_irq(&phba->hbalock);
James Smart04c68492009-05-22 14:52:52 -04004363 phba->sli.sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05004364 spin_unlock_irq(&phba->hbalock);
4365 rc = -ENXIO;
James Smart04c68492009-05-22 14:52:52 -04004366 } else {
4367 /* Allow asynchronous mailbox command to go through */
4368 spin_lock_irq(&phba->hbalock);
4369 phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
4370 spin_unlock_irq(&phba->hbalock);
James Smarted957682007-06-17 19:56:37 -05004371 done = 1;
James Smartcb69f7d2011-12-13 13:21:57 -05004372
4373 if ((pmb->u.mb.un.varCfgPort.casabt == 1) &&
4374 (pmb->u.mb.un.varCfgPort.gasabt == 0))
4375 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
4376 "3110 Port did not grant ASABT\n");
James Smart04c68492009-05-22 14:52:52 -04004377 }
dea31012005-04-17 16:05:31 -05004378 }
James Smarted957682007-06-17 19:56:37 -05004379 if (!done) {
4380 rc = -EINVAL;
4381 goto do_prep_failed;
4382 }
James Smart04c68492009-05-22 14:52:52 -04004383 if (pmb->u.mb.un.varCfgPort.sli_mode == 3) {
4384 if (!pmb->u.mb.un.varCfgPort.cMA) {
James Smart34b02dc2008-08-24 21:49:55 -04004385 rc = -ENXIO;
4386 goto do_prep_failed;
4387 }
James Smart04c68492009-05-22 14:52:52 -04004388 if (phba->max_vpi && pmb->u.mb.un.varCfgPort.gmv) {
James Smart34b02dc2008-08-24 21:49:55 -04004389 phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
James Smart04c68492009-05-22 14:52:52 -04004390 phba->max_vpi = pmb->u.mb.un.varCfgPort.max_vpi;
4391 phba->max_vports = (phba->max_vpi > phba->max_vports) ?
4392 phba->max_vpi : phba->max_vports;
4393
James Smart34b02dc2008-08-24 21:49:55 -04004394 } else
4395 phba->max_vpi = 0;
James Smartbc739052010-08-04 16:11:18 -04004396 phba->fips_level = 0;
4397 phba->fips_spec_rev = 0;
4398 if (pmb->u.mb.un.varCfgPort.gdss) {
James Smart04c68492009-05-22 14:52:52 -04004399 phba->sli3_options |= LPFC_SLI3_DSS_ENABLED;
James Smartbc739052010-08-04 16:11:18 -04004400 phba->fips_level = pmb->u.mb.un.varCfgPort.fips_level;
4401 phba->fips_spec_rev = pmb->u.mb.un.varCfgPort.fips_rev;
4402 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4403 "2850 Security Crypto Active. FIPS x%d "
4404 "(Spec Rev: x%d)",
4405 phba->fips_level, phba->fips_spec_rev);
4406 }
4407 if (pmb->u.mb.un.varCfgPort.sec_err) {
4408 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4409 "2856 Config Port Security Crypto "
4410 "Error: x%x ",
4411 pmb->u.mb.un.varCfgPort.sec_err);
4412 }
James Smart04c68492009-05-22 14:52:52 -04004413 if (pmb->u.mb.un.varCfgPort.gerbm)
James Smart34b02dc2008-08-24 21:49:55 -04004414 phba->sli3_options |= LPFC_SLI3_HBQ_ENABLED;
James Smart04c68492009-05-22 14:52:52 -04004415 if (pmb->u.mb.un.varCfgPort.gcrp)
James Smart34b02dc2008-08-24 21:49:55 -04004416 phba->sli3_options |= LPFC_SLI3_CRP_ENABLED;
James Smart6e7288d2010-06-07 15:23:35 -04004417
4418 phba->hbq_get = phba->mbox->us.s3_pgp.hbq_get;
4419 phba->port_gp = phba->mbox->us.s3_pgp.port;
James Smarte2a0a9d2008-12-04 22:40:02 -05004420
4421 if (phba->cfg_enable_bg) {
James Smart04c68492009-05-22 14:52:52 -04004422 if (pmb->u.mb.un.varCfgPort.gbg)
James Smarte2a0a9d2008-12-04 22:40:02 -05004423 phba->sli3_options |= LPFC_SLI3_BG_ENABLED;
4424 else
4425 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4426 "0443 Adapter did not grant "
4427 "BlockGuard\n");
4428 }
James Smart34b02dc2008-08-24 21:49:55 -04004429 } else {
James Smart8f34f4c2008-12-04 22:39:23 -05004430 phba->hbq_get = NULL;
James Smart34b02dc2008-08-24 21:49:55 -04004431 phba->port_gp = phba->mbox->us.s2.port;
James Smartd7c255b2008-08-24 21:50:00 -04004432 phba->max_vpi = 0;
James Smarted957682007-06-17 19:56:37 -05004433 }
James Smart92d7f7b2007-06-17 19:56:38 -05004434do_prep_failed:
James Smarted957682007-06-17 19:56:37 -05004435 mempool_free(pmb, phba->mbox_mem_pool);
4436 return rc;
4437}
4438
James Smarte59058c2008-08-24 21:49:00 -04004439
4440/**
James Smart3621a712009-04-06 18:47:14 -04004441 * lpfc_sli_hba_setup - SLI intialization function
James Smarte59058c2008-08-24 21:49:00 -04004442 * @phba: Pointer to HBA context object.
4443 *
4444 * This function is the main SLI intialization function. This function
4445 * is called by the HBA intialization code, HBA reset code and HBA
4446 * error attention handler code. Caller is not required to hold any
4447 * locks. This function issues config_port mailbox command to configure
4448 * the SLI, setup iocb rings and HBQ rings. In the end the function
4449 * calls the config_port_post function to issue init_link mailbox
4450 * command and to start the discovery. The function will return zero
4451 * if successful, else it will return negative error code.
4452 **/
James Smarted957682007-06-17 19:56:37 -05004453int
4454lpfc_sli_hba_setup(struct lpfc_hba *phba)
4455{
4456 uint32_t rc;
James Smart6d368e52011-05-24 11:44:12 -04004457 int mode = 3, i;
4458 int longs;
James Smarted957682007-06-17 19:56:37 -05004459
4460 switch (lpfc_sli_mode) {
4461 case 2:
James Smart78b2d852007-08-02 11:10:21 -04004462 if (phba->cfg_enable_npiv) {
James Smart92d7f7b2007-06-17 19:56:38 -05004463 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04004464 "1824 NPIV enabled: Override lpfc_sli_mode "
James Smart92d7f7b2007-06-17 19:56:38 -05004465 "parameter (%d) to auto (0).\n",
James Smarte8b62012007-08-02 11:10:09 -04004466 lpfc_sli_mode);
James Smart92d7f7b2007-06-17 19:56:38 -05004467 break;
4468 }
James Smarted957682007-06-17 19:56:37 -05004469 mode = 2;
4470 break;
4471 case 0:
4472 case 3:
4473 break;
4474 default:
James Smart92d7f7b2007-06-17 19:56:38 -05004475 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04004476 "1819 Unrecognized lpfc_sli_mode "
4477 "parameter: %d.\n", lpfc_sli_mode);
James Smarted957682007-06-17 19:56:37 -05004478
4479 break;
4480 }
4481
James Smart93996272008-08-24 21:50:30 -04004482 rc = lpfc_sli_config_port(phba, mode);
4483
James Smarted957682007-06-17 19:56:37 -05004484 if (rc && lpfc_sli_mode == 3)
James Smart92d7f7b2007-06-17 19:56:38 -05004485 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04004486 "1820 Unable to select SLI-3. "
4487 "Not supported by adapter.\n");
James Smarted957682007-06-17 19:56:37 -05004488 if (rc && mode != 2)
James Smart93996272008-08-24 21:50:30 -04004489 rc = lpfc_sli_config_port(phba, 2);
James Smarted957682007-06-17 19:56:37 -05004490 if (rc)
dea31012005-04-17 16:05:31 -05004491 goto lpfc_sli_hba_setup_error;
4492
James Smart0d878412009-10-02 15:16:56 -04004493 /* Enable PCIe device Advanced Error Reporting (AER) if configured */
4494 if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
4495 rc = pci_enable_pcie_error_reporting(phba->pcidev);
4496 if (!rc) {
4497 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4498 "2709 This device supports "
4499 "Advanced Error Reporting (AER)\n");
4500 spin_lock_irq(&phba->hbalock);
4501 phba->hba_flag |= HBA_AER_ENABLED;
4502 spin_unlock_irq(&phba->hbalock);
4503 } else {
4504 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4505 "2708 This device does not support "
4506 "Advanced Error Reporting (AER)\n");
4507 phba->cfg_aer_support = 0;
4508 }
4509 }
4510
James Smarted957682007-06-17 19:56:37 -05004511 if (phba->sli_rev == 3) {
4512 phba->iocb_cmd_size = SLI3_IOCB_CMD_SIZE;
4513 phba->iocb_rsp_size = SLI3_IOCB_RSP_SIZE;
James Smarted957682007-06-17 19:56:37 -05004514 } else {
4515 phba->iocb_cmd_size = SLI2_IOCB_CMD_SIZE;
4516 phba->iocb_rsp_size = SLI2_IOCB_RSP_SIZE;
James Smart92d7f7b2007-06-17 19:56:38 -05004517 phba->sli3_options = 0;
James Smarted957682007-06-17 19:56:37 -05004518 }
4519
4520 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004521 "0444 Firmware in SLI %x mode. Max_vpi %d\n",
4522 phba->sli_rev, phba->max_vpi);
James Smarted957682007-06-17 19:56:37 -05004523 rc = lpfc_sli_ring_map(phba);
dea31012005-04-17 16:05:31 -05004524
4525 if (rc)
4526 goto lpfc_sli_hba_setup_error;
4527
James Smart6d368e52011-05-24 11:44:12 -04004528 /* Initialize VPIs. */
4529 if (phba->sli_rev == LPFC_SLI_REV3) {
4530 /*
4531 * The VPI bitmask and physical ID array are allocated
4532 * and initialized once only - at driver load. A port
4533 * reset doesn't need to reinitialize this memory.
4534 */
4535 if ((phba->vpi_bmask == NULL) && (phba->vpi_ids == NULL)) {
4536 longs = (phba->max_vpi + BITS_PER_LONG) / BITS_PER_LONG;
4537 phba->vpi_bmask = kzalloc(longs * sizeof(unsigned long),
4538 GFP_KERNEL);
4539 if (!phba->vpi_bmask) {
4540 rc = -ENOMEM;
4541 goto lpfc_sli_hba_setup_error;
4542 }
4543
4544 phba->vpi_ids = kzalloc(
4545 (phba->max_vpi+1) * sizeof(uint16_t),
4546 GFP_KERNEL);
4547 if (!phba->vpi_ids) {
4548 kfree(phba->vpi_bmask);
4549 rc = -ENOMEM;
4550 goto lpfc_sli_hba_setup_error;
4551 }
4552 for (i = 0; i < phba->max_vpi; i++)
4553 phba->vpi_ids[i] = i;
4554 }
4555 }
4556
James Smart93996272008-08-24 21:50:30 -04004557 /* Init HBQs */
James Smarted957682007-06-17 19:56:37 -05004558 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
4559 rc = lpfc_sli_hbq_setup(phba);
4560 if (rc)
4561 goto lpfc_sli_hba_setup_error;
4562 }
James Smart04c68492009-05-22 14:52:52 -04004563 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004564 phba->sli.sli_flag |= LPFC_PROCESS_LA;
James Smart04c68492009-05-22 14:52:52 -04004565 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004566
4567 rc = lpfc_config_port_post(phba);
4568 if (rc)
4569 goto lpfc_sli_hba_setup_error;
4570
James Smarted957682007-06-17 19:56:37 -05004571 return rc;
4572
James Smart92d7f7b2007-06-17 19:56:38 -05004573lpfc_sli_hba_setup_error:
James Smart2e0fef82007-06-17 19:56:36 -05004574 phba->link_state = LPFC_HBA_ERROR;
James Smarte40a02c2010-02-26 14:13:54 -05004575 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004576 "0445 Firmware initialization failed\n");
dea31012005-04-17 16:05:31 -05004577 return rc;
4578}
4579
James Smartda0436e2009-05-22 14:51:39 -04004580/**
4581 * lpfc_sli4_read_fcoe_params - Read fcoe params from conf region
4582 * @phba: Pointer to HBA context object.
4583 * @mboxq: mailbox pointer.
4584 * This function issue a dump mailbox command to read config region
4585 * 23 and parse the records in the region and populate driver
4586 * data structure.
4587 **/
4588static int
James Smartff78d8f2011-12-13 13:21:35 -05004589lpfc_sli4_read_fcoe_params(struct lpfc_hba *phba)
James Smartda0436e2009-05-22 14:51:39 -04004590{
James Smartff78d8f2011-12-13 13:21:35 -05004591 LPFC_MBOXQ_t *mboxq;
James Smartda0436e2009-05-22 14:51:39 -04004592 struct lpfc_dmabuf *mp;
4593 struct lpfc_mqe *mqe;
4594 uint32_t data_length;
4595 int rc;
4596
4597 /* Program the default value of vlan_id and fc_map */
4598 phba->valid_vlan = 0;
4599 phba->fc_map[0] = LPFC_FCOE_FCF_MAP0;
4600 phba->fc_map[1] = LPFC_FCOE_FCF_MAP1;
4601 phba->fc_map[2] = LPFC_FCOE_FCF_MAP2;
4602
James Smartff78d8f2011-12-13 13:21:35 -05004603 mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4604 if (!mboxq)
James Smartda0436e2009-05-22 14:51:39 -04004605 return -ENOMEM;
4606
James Smartff78d8f2011-12-13 13:21:35 -05004607 mqe = &mboxq->u.mqe;
4608 if (lpfc_sli4_dump_cfg_rg23(phba, mboxq)) {
4609 rc = -ENOMEM;
4610 goto out_free_mboxq;
4611 }
4612
James Smartda0436e2009-05-22 14:51:39 -04004613 mp = (struct lpfc_dmabuf *) mboxq->context1;
4614 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4615
4616 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
4617 "(%d):2571 Mailbox cmd x%x Status x%x "
4618 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x "
4619 "x%x x%x x%x x%x x%x x%x x%x x%x x%x "
4620 "CQ: x%x x%x x%x x%x\n",
4621 mboxq->vport ? mboxq->vport->vpi : 0,
4622 bf_get(lpfc_mqe_command, mqe),
4623 bf_get(lpfc_mqe_status, mqe),
4624 mqe->un.mb_words[0], mqe->un.mb_words[1],
4625 mqe->un.mb_words[2], mqe->un.mb_words[3],
4626 mqe->un.mb_words[4], mqe->un.mb_words[5],
4627 mqe->un.mb_words[6], mqe->un.mb_words[7],
4628 mqe->un.mb_words[8], mqe->un.mb_words[9],
4629 mqe->un.mb_words[10], mqe->un.mb_words[11],
4630 mqe->un.mb_words[12], mqe->un.mb_words[13],
4631 mqe->un.mb_words[14], mqe->un.mb_words[15],
4632 mqe->un.mb_words[16], mqe->un.mb_words[50],
4633 mboxq->mcqe.word0,
4634 mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
4635 mboxq->mcqe.trailer);
4636
4637 if (rc) {
4638 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4639 kfree(mp);
James Smartff78d8f2011-12-13 13:21:35 -05004640 rc = -EIO;
4641 goto out_free_mboxq;
James Smartda0436e2009-05-22 14:51:39 -04004642 }
4643 data_length = mqe->un.mb_words[5];
James Smarta0c87cb2009-07-19 10:01:10 -04004644 if (data_length > DMP_RGN23_SIZE) {
James Smartd11e31d2009-06-10 17:23:06 -04004645 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4646 kfree(mp);
James Smartff78d8f2011-12-13 13:21:35 -05004647 rc = -EIO;
4648 goto out_free_mboxq;
James Smartd11e31d2009-06-10 17:23:06 -04004649 }
James Smartda0436e2009-05-22 14:51:39 -04004650
4651 lpfc_parse_fcoe_conf(phba, mp->virt, data_length);
4652 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4653 kfree(mp);
James Smartff78d8f2011-12-13 13:21:35 -05004654 rc = 0;
4655
4656out_free_mboxq:
4657 mempool_free(mboxq, phba->mbox_mem_pool);
4658 return rc;
James Smartda0436e2009-05-22 14:51:39 -04004659}
4660
4661/**
4662 * lpfc_sli4_read_rev - Issue READ_REV and collect vpd data
4663 * @phba: pointer to lpfc hba data structure.
4664 * @mboxq: pointer to the LPFC_MBOXQ_t structure.
4665 * @vpd: pointer to the memory to hold resulting port vpd data.
4666 * @vpd_size: On input, the number of bytes allocated to @vpd.
4667 * On output, the number of data bytes in @vpd.
4668 *
4669 * This routine executes a READ_REV SLI4 mailbox command. In
4670 * addition, this routine gets the port vpd data.
4671 *
4672 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02004673 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -04004674 * -ENOMEM - could not allocated memory.
James Smartda0436e2009-05-22 14:51:39 -04004675 **/
4676static int
4677lpfc_sli4_read_rev(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
4678 uint8_t *vpd, uint32_t *vpd_size)
4679{
4680 int rc = 0;
4681 uint32_t dma_size;
4682 struct lpfc_dmabuf *dmabuf;
4683 struct lpfc_mqe *mqe;
4684
4685 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
4686 if (!dmabuf)
4687 return -ENOMEM;
4688
4689 /*
4690 * Get a DMA buffer for the vpd data resulting from the READ_REV
4691 * mailbox command.
4692 */
4693 dma_size = *vpd_size;
4694 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
4695 dma_size,
4696 &dmabuf->phys,
4697 GFP_KERNEL);
4698 if (!dmabuf->virt) {
4699 kfree(dmabuf);
4700 return -ENOMEM;
4701 }
4702 memset(dmabuf->virt, 0, dma_size);
4703
4704 /*
4705 * The SLI4 implementation of READ_REV conflicts at word1,
4706 * bits 31:16 and SLI4 adds vpd functionality not present
4707 * in SLI3. This code corrects the conflicts.
4708 */
4709 lpfc_read_rev(phba, mboxq);
4710 mqe = &mboxq->u.mqe;
4711 mqe->un.read_rev.vpd_paddr_high = putPaddrHigh(dmabuf->phys);
4712 mqe->un.read_rev.vpd_paddr_low = putPaddrLow(dmabuf->phys);
4713 mqe->un.read_rev.word1 &= 0x0000FFFF;
4714 bf_set(lpfc_mbx_rd_rev_vpd, &mqe->un.read_rev, 1);
4715 bf_set(lpfc_mbx_rd_rev_avail_len, &mqe->un.read_rev, dma_size);
4716
4717 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4718 if (rc) {
4719 dma_free_coherent(&phba->pcidev->dev, dma_size,
4720 dmabuf->virt, dmabuf->phys);
James Smartdef9c7a2009-12-21 17:02:28 -05004721 kfree(dmabuf);
James Smartda0436e2009-05-22 14:51:39 -04004722 return -EIO;
4723 }
4724
James Smartda0436e2009-05-22 14:51:39 -04004725 /*
4726 * The available vpd length cannot be bigger than the
4727 * DMA buffer passed to the port. Catch the less than
4728 * case and update the caller's size.
4729 */
4730 if (mqe->un.read_rev.avail_vpd_len < *vpd_size)
4731 *vpd_size = mqe->un.read_rev.avail_vpd_len;
4732
James Smartd7c47992010-06-08 18:31:54 -04004733 memcpy(vpd, dmabuf->virt, *vpd_size);
4734
James Smartda0436e2009-05-22 14:51:39 -04004735 dma_free_coherent(&phba->pcidev->dev, dma_size,
4736 dmabuf->virt, dmabuf->phys);
4737 kfree(dmabuf);
4738 return 0;
4739}
4740
4741/**
James Smartcd1c8302011-10-10 21:33:25 -04004742 * lpfc_sli4_retrieve_pport_name - Retrieve SLI4 device physical port name
4743 * @phba: pointer to lpfc hba data structure.
4744 *
4745 * This routine retrieves SLI4 device physical port name this PCI function
4746 * is attached to.
4747 *
4748 * Return codes
4749 * 0 - sucessful
4750 * otherwise - failed to retrieve physical port name
4751 **/
4752static int
4753lpfc_sli4_retrieve_pport_name(struct lpfc_hba *phba)
4754{
4755 LPFC_MBOXQ_t *mboxq;
James Smartcd1c8302011-10-10 21:33:25 -04004756 struct lpfc_mbx_get_cntl_attributes *mbx_cntl_attr;
4757 struct lpfc_controller_attribute *cntl_attr;
4758 struct lpfc_mbx_get_port_name *get_port_name;
4759 void *virtaddr = NULL;
4760 uint32_t alloclen, reqlen;
4761 uint32_t shdr_status, shdr_add_status;
4762 union lpfc_sli4_cfg_shdr *shdr;
4763 char cport_name = 0;
4764 int rc;
4765
4766 /* We assume nothing at this point */
4767 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_INVAL;
4768 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_NON;
4769
4770 mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4771 if (!mboxq)
4772 return -ENOMEM;
James Smartcd1c8302011-10-10 21:33:25 -04004773 /* obtain link type and link number via READ_CONFIG */
James Smartff78d8f2011-12-13 13:21:35 -05004774 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_INVAL;
4775 lpfc_sli4_read_config(phba);
4776 if (phba->sli4_hba.lnk_info.lnk_dv == LPFC_LNK_DAT_VAL)
4777 goto retrieve_ppname;
James Smartcd1c8302011-10-10 21:33:25 -04004778
4779 /* obtain link type and link number via COMMON_GET_CNTL_ATTRIBUTES */
4780 reqlen = sizeof(struct lpfc_mbx_get_cntl_attributes);
4781 alloclen = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
4782 LPFC_MBOX_OPCODE_GET_CNTL_ATTRIBUTES, reqlen,
4783 LPFC_SLI4_MBX_NEMBED);
4784 if (alloclen < reqlen) {
4785 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4786 "3084 Allocated DMA memory size (%d) is "
4787 "less than the requested DMA memory size "
4788 "(%d)\n", alloclen, reqlen);
4789 rc = -ENOMEM;
4790 goto out_free_mboxq;
4791 }
4792 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4793 virtaddr = mboxq->sge_array->addr[0];
4794 mbx_cntl_attr = (struct lpfc_mbx_get_cntl_attributes *)virtaddr;
4795 shdr = &mbx_cntl_attr->cfg_shdr;
4796 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
4797 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
4798 if (shdr_status || shdr_add_status || rc) {
4799 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
4800 "3085 Mailbox x%x (x%x/x%x) failed, "
4801 "rc:x%x, status:x%x, add_status:x%x\n",
4802 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
4803 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
4804 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
4805 rc, shdr_status, shdr_add_status);
4806 rc = -ENXIO;
4807 goto out_free_mboxq;
4808 }
4809 cntl_attr = &mbx_cntl_attr->cntl_attr;
4810 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_VAL;
4811 phba->sli4_hba.lnk_info.lnk_tp =
4812 bf_get(lpfc_cntl_attr_lnk_type, cntl_attr);
4813 phba->sli4_hba.lnk_info.lnk_no =
4814 bf_get(lpfc_cntl_attr_lnk_numb, cntl_attr);
4815 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4816 "3086 lnk_type:%d, lnk_numb:%d\n",
4817 phba->sli4_hba.lnk_info.lnk_tp,
4818 phba->sli4_hba.lnk_info.lnk_no);
4819
4820retrieve_ppname:
4821 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
4822 LPFC_MBOX_OPCODE_GET_PORT_NAME,
4823 sizeof(struct lpfc_mbx_get_port_name) -
4824 sizeof(struct lpfc_sli4_cfg_mhdr),
4825 LPFC_SLI4_MBX_EMBED);
4826 get_port_name = &mboxq->u.mqe.un.get_port_name;
4827 shdr = (union lpfc_sli4_cfg_shdr *)&get_port_name->header.cfg_shdr;
4828 bf_set(lpfc_mbox_hdr_version, &shdr->request, LPFC_OPCODE_VERSION_1);
4829 bf_set(lpfc_mbx_get_port_name_lnk_type, &get_port_name->u.request,
4830 phba->sli4_hba.lnk_info.lnk_tp);
4831 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4832 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
4833 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
4834 if (shdr_status || shdr_add_status || rc) {
4835 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
4836 "3087 Mailbox x%x (x%x/x%x) failed: "
4837 "rc:x%x, status:x%x, add_status:x%x\n",
4838 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
4839 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
4840 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
4841 rc, shdr_status, shdr_add_status);
4842 rc = -ENXIO;
4843 goto out_free_mboxq;
4844 }
4845 switch (phba->sli4_hba.lnk_info.lnk_no) {
4846 case LPFC_LINK_NUMBER_0:
4847 cport_name = bf_get(lpfc_mbx_get_port_name_name0,
4848 &get_port_name->u.response);
4849 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
4850 break;
4851 case LPFC_LINK_NUMBER_1:
4852 cport_name = bf_get(lpfc_mbx_get_port_name_name1,
4853 &get_port_name->u.response);
4854 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
4855 break;
4856 case LPFC_LINK_NUMBER_2:
4857 cport_name = bf_get(lpfc_mbx_get_port_name_name2,
4858 &get_port_name->u.response);
4859 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
4860 break;
4861 case LPFC_LINK_NUMBER_3:
4862 cport_name = bf_get(lpfc_mbx_get_port_name_name3,
4863 &get_port_name->u.response);
4864 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
4865 break;
4866 default:
4867 break;
4868 }
4869
4870 if (phba->sli4_hba.pport_name_sta == LPFC_SLI4_PPNAME_GET) {
4871 phba->Port[0] = cport_name;
4872 phba->Port[1] = '\0';
4873 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4874 "3091 SLI get port name: %s\n", phba->Port);
4875 }
4876
4877out_free_mboxq:
4878 if (rc != MBX_TIMEOUT) {
4879 if (bf_get(lpfc_mqe_command, &mboxq->u.mqe) == MBX_SLI4_CONFIG)
4880 lpfc_sli4_mbox_cmd_free(phba, mboxq);
4881 else
4882 mempool_free(mboxq, phba->mbox_mem_pool);
4883 }
4884 return rc;
4885}
4886
4887/**
James Smartda0436e2009-05-22 14:51:39 -04004888 * lpfc_sli4_arm_cqeq_intr - Arm sli-4 device completion and event queues
4889 * @phba: pointer to lpfc hba data structure.
4890 *
4891 * This routine is called to explicitly arm the SLI4 device's completion and
4892 * event queues
4893 **/
4894static void
4895lpfc_sli4_arm_cqeq_intr(struct lpfc_hba *phba)
4896{
4897 uint8_t fcp_eqidx;
4898
4899 lpfc_sli4_cq_release(phba->sli4_hba.mbx_cq, LPFC_QUEUE_REARM);
4900 lpfc_sli4_cq_release(phba->sli4_hba.els_cq, LPFC_QUEUE_REARM);
James Smart05580562011-05-24 11:40:48 -04004901 fcp_eqidx = 0;
James Smart2e90f4b2011-12-13 13:22:37 -05004902 if (phba->sli4_hba.fcp_cq) {
4903 do
4904 lpfc_sli4_cq_release(phba->sli4_hba.fcp_cq[fcp_eqidx],
4905 LPFC_QUEUE_REARM);
4906 while (++fcp_eqidx < phba->cfg_fcp_eq_count);
4907 }
James Smartda0436e2009-05-22 14:51:39 -04004908 lpfc_sli4_eq_release(phba->sli4_hba.sp_eq, LPFC_QUEUE_REARM);
James Smart2e90f4b2011-12-13 13:22:37 -05004909 if (phba->sli4_hba.fp_eq) {
4910 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count;
4911 fcp_eqidx++)
4912 lpfc_sli4_eq_release(phba->sli4_hba.fp_eq[fcp_eqidx],
4913 LPFC_QUEUE_REARM);
4914 }
James Smartda0436e2009-05-22 14:51:39 -04004915}
4916
4917/**
James Smart6d368e52011-05-24 11:44:12 -04004918 * lpfc_sli4_get_avail_extnt_rsrc - Get available resource extent count.
4919 * @phba: Pointer to HBA context object.
4920 * @type: The resource extent type.
James Smartb76f2dc2011-07-22 18:37:42 -04004921 * @extnt_count: buffer to hold port available extent count.
4922 * @extnt_size: buffer to hold element count per extent.
James Smart6d368e52011-05-24 11:44:12 -04004923 *
James Smartb76f2dc2011-07-22 18:37:42 -04004924 * This function calls the port and retrievs the number of available
4925 * extents and their size for a particular extent type.
4926 *
4927 * Returns: 0 if successful. Nonzero otherwise.
James Smart6d368e52011-05-24 11:44:12 -04004928 **/
James Smartb76f2dc2011-07-22 18:37:42 -04004929int
James Smart6d368e52011-05-24 11:44:12 -04004930lpfc_sli4_get_avail_extnt_rsrc(struct lpfc_hba *phba, uint16_t type,
4931 uint16_t *extnt_count, uint16_t *extnt_size)
4932{
4933 int rc = 0;
4934 uint32_t length;
4935 uint32_t mbox_tmo;
4936 struct lpfc_mbx_get_rsrc_extent_info *rsrc_info;
4937 LPFC_MBOXQ_t *mbox;
4938
4939 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4940 if (!mbox)
4941 return -ENOMEM;
4942
4943 /* Find out how many extents are available for this resource type */
4944 length = (sizeof(struct lpfc_mbx_get_rsrc_extent_info) -
4945 sizeof(struct lpfc_sli4_cfg_mhdr));
4946 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
4947 LPFC_MBOX_OPCODE_GET_RSRC_EXTENT_INFO,
4948 length, LPFC_SLI4_MBX_EMBED);
4949
4950 /* Send an extents count of 0 - the GET doesn't use it. */
4951 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, 0, type,
4952 LPFC_SLI4_MBX_EMBED);
4953 if (unlikely(rc)) {
4954 rc = -EIO;
4955 goto err_exit;
4956 }
4957
4958 if (!phba->sli4_hba.intr_enable)
4959 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
4960 else {
James Smarta183a152011-10-10 21:32:43 -04004961 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart6d368e52011-05-24 11:44:12 -04004962 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
4963 }
4964 if (unlikely(rc)) {
4965 rc = -EIO;
4966 goto err_exit;
4967 }
4968
4969 rsrc_info = &mbox->u.mqe.un.rsrc_extent_info;
4970 if (bf_get(lpfc_mbox_hdr_status,
4971 &rsrc_info->header.cfg_shdr.response)) {
4972 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
4973 "2930 Failed to get resource extents "
4974 "Status 0x%x Add'l Status 0x%x\n",
4975 bf_get(lpfc_mbox_hdr_status,
4976 &rsrc_info->header.cfg_shdr.response),
4977 bf_get(lpfc_mbox_hdr_add_status,
4978 &rsrc_info->header.cfg_shdr.response));
4979 rc = -EIO;
4980 goto err_exit;
4981 }
4982
4983 *extnt_count = bf_get(lpfc_mbx_get_rsrc_extent_info_cnt,
4984 &rsrc_info->u.rsp);
4985 *extnt_size = bf_get(lpfc_mbx_get_rsrc_extent_info_size,
4986 &rsrc_info->u.rsp);
James Smart8a9d2e82012-05-09 21:16:12 -04004987
4988 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4989 "3162 Retrieved extents type-%d from port: count:%d, "
4990 "size:%d\n", type, *extnt_count, *extnt_size);
4991
4992err_exit:
James Smart6d368e52011-05-24 11:44:12 -04004993 mempool_free(mbox, phba->mbox_mem_pool);
4994 return rc;
4995}
4996
4997/**
4998 * lpfc_sli4_chk_avail_extnt_rsrc - Check for available SLI4 resource extents.
4999 * @phba: Pointer to HBA context object.
5000 * @type: The extent type to check.
5001 *
5002 * This function reads the current available extents from the port and checks
5003 * if the extent count or extent size has changed since the last access.
5004 * Callers use this routine post port reset to understand if there is a
5005 * extent reprovisioning requirement.
5006 *
5007 * Returns:
5008 * -Error: error indicates problem.
5009 * 1: Extent count or size has changed.
5010 * 0: No changes.
5011 **/
5012static int
5013lpfc_sli4_chk_avail_extnt_rsrc(struct lpfc_hba *phba, uint16_t type)
5014{
5015 uint16_t curr_ext_cnt, rsrc_ext_cnt;
5016 uint16_t size_diff, rsrc_ext_size;
5017 int rc = 0;
5018 struct lpfc_rsrc_blks *rsrc_entry;
5019 struct list_head *rsrc_blk_list = NULL;
5020
5021 size_diff = 0;
5022 curr_ext_cnt = 0;
5023 rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,
5024 &rsrc_ext_cnt,
5025 &rsrc_ext_size);
5026 if (unlikely(rc))
5027 return -EIO;
5028
5029 switch (type) {
5030 case LPFC_RSC_TYPE_FCOE_RPI:
5031 rsrc_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;
5032 break;
5033 case LPFC_RSC_TYPE_FCOE_VPI:
5034 rsrc_blk_list = &phba->lpfc_vpi_blk_list;
5035 break;
5036 case LPFC_RSC_TYPE_FCOE_XRI:
5037 rsrc_blk_list = &phba->sli4_hba.lpfc_xri_blk_list;
5038 break;
5039 case LPFC_RSC_TYPE_FCOE_VFI:
5040 rsrc_blk_list = &phba->sli4_hba.lpfc_vfi_blk_list;
5041 break;
5042 default:
5043 break;
5044 }
5045
5046 list_for_each_entry(rsrc_entry, rsrc_blk_list, list) {
5047 curr_ext_cnt++;
5048 if (rsrc_entry->rsrc_size != rsrc_ext_size)
5049 size_diff++;
5050 }
5051
5052 if (curr_ext_cnt != rsrc_ext_cnt || size_diff != 0)
5053 rc = 1;
5054
5055 return rc;
5056}
5057
5058/**
5059 * lpfc_sli4_cfg_post_extnts -
5060 * @phba: Pointer to HBA context object.
5061 * @extnt_cnt - number of available extents.
5062 * @type - the extent type (rpi, xri, vfi, vpi).
5063 * @emb - buffer to hold either MBX_EMBED or MBX_NEMBED operation.
5064 * @mbox - pointer to the caller's allocated mailbox structure.
5065 *
5066 * This function executes the extents allocation request. It also
5067 * takes care of the amount of memory needed to allocate or get the
5068 * allocated extents. It is the caller's responsibility to evaluate
5069 * the response.
5070 *
5071 * Returns:
5072 * -Error: Error value describes the condition found.
5073 * 0: if successful
5074 **/
5075static int
James Smart8a9d2e82012-05-09 21:16:12 -04005076lpfc_sli4_cfg_post_extnts(struct lpfc_hba *phba, uint16_t extnt_cnt,
James Smart6d368e52011-05-24 11:44:12 -04005077 uint16_t type, bool *emb, LPFC_MBOXQ_t *mbox)
5078{
5079 int rc = 0;
5080 uint32_t req_len;
5081 uint32_t emb_len;
5082 uint32_t alloc_len, mbox_tmo;
5083
5084 /* Calculate the total requested length of the dma memory */
James Smart8a9d2e82012-05-09 21:16:12 -04005085 req_len = extnt_cnt * sizeof(uint16_t);
James Smart6d368e52011-05-24 11:44:12 -04005086
5087 /*
5088 * Calculate the size of an embedded mailbox. The uint32_t
5089 * accounts for extents-specific word.
5090 */
5091 emb_len = sizeof(MAILBOX_t) - sizeof(struct mbox_header) -
5092 sizeof(uint32_t);
5093
5094 /*
5095 * Presume the allocation and response will fit into an embedded
5096 * mailbox. If not true, reconfigure to a non-embedded mailbox.
5097 */
5098 *emb = LPFC_SLI4_MBX_EMBED;
5099 if (req_len > emb_len) {
James Smart8a9d2e82012-05-09 21:16:12 -04005100 req_len = extnt_cnt * sizeof(uint16_t) +
James Smart6d368e52011-05-24 11:44:12 -04005101 sizeof(union lpfc_sli4_cfg_shdr) +
5102 sizeof(uint32_t);
5103 *emb = LPFC_SLI4_MBX_NEMBED;
5104 }
5105
5106 alloc_len = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5107 LPFC_MBOX_OPCODE_ALLOC_RSRC_EXTENT,
5108 req_len, *emb);
5109 if (alloc_len < req_len) {
5110 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartb76f2dc2011-07-22 18:37:42 -04005111 "2982 Allocated DMA memory size (x%x) is "
James Smart6d368e52011-05-24 11:44:12 -04005112 "less than the requested DMA memory "
5113 "size (x%x)\n", alloc_len, req_len);
5114 return -ENOMEM;
5115 }
James Smart8a9d2e82012-05-09 21:16:12 -04005116 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, extnt_cnt, type, *emb);
James Smart6d368e52011-05-24 11:44:12 -04005117 if (unlikely(rc))
5118 return -EIO;
5119
5120 if (!phba->sli4_hba.intr_enable)
5121 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5122 else {
James Smarta183a152011-10-10 21:32:43 -04005123 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart6d368e52011-05-24 11:44:12 -04005124 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5125 }
5126
5127 if (unlikely(rc))
5128 rc = -EIO;
5129 return rc;
5130}
5131
5132/**
5133 * lpfc_sli4_alloc_extent - Allocate an SLI4 resource extent.
5134 * @phba: Pointer to HBA context object.
5135 * @type: The resource extent type to allocate.
5136 *
5137 * This function allocates the number of elements for the specified
5138 * resource type.
5139 **/
5140static int
5141lpfc_sli4_alloc_extent(struct lpfc_hba *phba, uint16_t type)
5142{
5143 bool emb = false;
5144 uint16_t rsrc_id_cnt, rsrc_cnt, rsrc_size;
5145 uint16_t rsrc_id, rsrc_start, j, k;
5146 uint16_t *ids;
5147 int i, rc;
5148 unsigned long longs;
5149 unsigned long *bmask;
5150 struct lpfc_rsrc_blks *rsrc_blks;
5151 LPFC_MBOXQ_t *mbox;
5152 uint32_t length;
5153 struct lpfc_id_range *id_array = NULL;
5154 void *virtaddr = NULL;
5155 struct lpfc_mbx_nembed_rsrc_extent *n_rsrc;
5156 struct lpfc_mbx_alloc_rsrc_extents *rsrc_ext;
5157 struct list_head *ext_blk_list;
5158
5159 rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,
5160 &rsrc_cnt,
5161 &rsrc_size);
5162 if (unlikely(rc))
5163 return -EIO;
5164
5165 if ((rsrc_cnt == 0) || (rsrc_size == 0)) {
5166 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
5167 "3009 No available Resource Extents "
5168 "for resource type 0x%x: Count: 0x%x, "
5169 "Size 0x%x\n", type, rsrc_cnt,
5170 rsrc_size);
5171 return -ENOMEM;
5172 }
5173
James Smart8a9d2e82012-05-09 21:16:12 -04005174 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_INIT | LOG_SLI,
5175 "2903 Post resource extents type-0x%x: "
5176 "count:%d, size %d\n", type, rsrc_cnt, rsrc_size);
James Smart6d368e52011-05-24 11:44:12 -04005177
5178 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5179 if (!mbox)
5180 return -ENOMEM;
5181
James Smart8a9d2e82012-05-09 21:16:12 -04005182 rc = lpfc_sli4_cfg_post_extnts(phba, rsrc_cnt, type, &emb, mbox);
James Smart6d368e52011-05-24 11:44:12 -04005183 if (unlikely(rc)) {
5184 rc = -EIO;
5185 goto err_exit;
5186 }
5187
5188 /*
5189 * Figure out where the response is located. Then get local pointers
5190 * to the response data. The port does not guarantee to respond to
5191 * all extents counts request so update the local variable with the
5192 * allocated count from the port.
5193 */
5194 if (emb == LPFC_SLI4_MBX_EMBED) {
5195 rsrc_ext = &mbox->u.mqe.un.alloc_rsrc_extents;
5196 id_array = &rsrc_ext->u.rsp.id[0];
5197 rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, &rsrc_ext->u.rsp);
5198 } else {
5199 virtaddr = mbox->sge_array->addr[0];
5200 n_rsrc = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
5201 rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, n_rsrc);
5202 id_array = &n_rsrc->id;
5203 }
5204
5205 longs = ((rsrc_cnt * rsrc_size) + BITS_PER_LONG - 1) / BITS_PER_LONG;
5206 rsrc_id_cnt = rsrc_cnt * rsrc_size;
5207
5208 /*
5209 * Based on the resource size and count, correct the base and max
5210 * resource values.
5211 */
5212 length = sizeof(struct lpfc_rsrc_blks);
5213 switch (type) {
5214 case LPFC_RSC_TYPE_FCOE_RPI:
5215 phba->sli4_hba.rpi_bmask = kzalloc(longs *
5216 sizeof(unsigned long),
5217 GFP_KERNEL);
5218 if (unlikely(!phba->sli4_hba.rpi_bmask)) {
5219 rc = -ENOMEM;
5220 goto err_exit;
5221 }
5222 phba->sli4_hba.rpi_ids = kzalloc(rsrc_id_cnt *
5223 sizeof(uint16_t),
5224 GFP_KERNEL);
5225 if (unlikely(!phba->sli4_hba.rpi_ids)) {
5226 kfree(phba->sli4_hba.rpi_bmask);
5227 rc = -ENOMEM;
5228 goto err_exit;
5229 }
5230
5231 /*
5232 * The next_rpi was initialized with the maximum available
5233 * count but the port may allocate a smaller number. Catch
5234 * that case and update the next_rpi.
5235 */
5236 phba->sli4_hba.next_rpi = rsrc_id_cnt;
5237
5238 /* Initialize local ptrs for common extent processing later. */
5239 bmask = phba->sli4_hba.rpi_bmask;
5240 ids = phba->sli4_hba.rpi_ids;
5241 ext_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;
5242 break;
5243 case LPFC_RSC_TYPE_FCOE_VPI:
5244 phba->vpi_bmask = kzalloc(longs *
5245 sizeof(unsigned long),
5246 GFP_KERNEL);
5247 if (unlikely(!phba->vpi_bmask)) {
5248 rc = -ENOMEM;
5249 goto err_exit;
5250 }
5251 phba->vpi_ids = kzalloc(rsrc_id_cnt *
5252 sizeof(uint16_t),
5253 GFP_KERNEL);
5254 if (unlikely(!phba->vpi_ids)) {
5255 kfree(phba->vpi_bmask);
5256 rc = -ENOMEM;
5257 goto err_exit;
5258 }
5259
5260 /* Initialize local ptrs for common extent processing later. */
5261 bmask = phba->vpi_bmask;
5262 ids = phba->vpi_ids;
5263 ext_blk_list = &phba->lpfc_vpi_blk_list;
5264 break;
5265 case LPFC_RSC_TYPE_FCOE_XRI:
5266 phba->sli4_hba.xri_bmask = kzalloc(longs *
5267 sizeof(unsigned long),
5268 GFP_KERNEL);
5269 if (unlikely(!phba->sli4_hba.xri_bmask)) {
5270 rc = -ENOMEM;
5271 goto err_exit;
5272 }
James Smart8a9d2e82012-05-09 21:16:12 -04005273 phba->sli4_hba.max_cfg_param.xri_used = 0;
James Smart6d368e52011-05-24 11:44:12 -04005274 phba->sli4_hba.xri_ids = kzalloc(rsrc_id_cnt *
5275 sizeof(uint16_t),
5276 GFP_KERNEL);
5277 if (unlikely(!phba->sli4_hba.xri_ids)) {
5278 kfree(phba->sli4_hba.xri_bmask);
5279 rc = -ENOMEM;
5280 goto err_exit;
5281 }
5282
5283 /* Initialize local ptrs for common extent processing later. */
5284 bmask = phba->sli4_hba.xri_bmask;
5285 ids = phba->sli4_hba.xri_ids;
5286 ext_blk_list = &phba->sli4_hba.lpfc_xri_blk_list;
5287 break;
5288 case LPFC_RSC_TYPE_FCOE_VFI:
5289 phba->sli4_hba.vfi_bmask = kzalloc(longs *
5290 sizeof(unsigned long),
5291 GFP_KERNEL);
5292 if (unlikely(!phba->sli4_hba.vfi_bmask)) {
5293 rc = -ENOMEM;
5294 goto err_exit;
5295 }
5296 phba->sli4_hba.vfi_ids = kzalloc(rsrc_id_cnt *
5297 sizeof(uint16_t),
5298 GFP_KERNEL);
5299 if (unlikely(!phba->sli4_hba.vfi_ids)) {
5300 kfree(phba->sli4_hba.vfi_bmask);
5301 rc = -ENOMEM;
5302 goto err_exit;
5303 }
5304
5305 /* Initialize local ptrs for common extent processing later. */
5306 bmask = phba->sli4_hba.vfi_bmask;
5307 ids = phba->sli4_hba.vfi_ids;
5308 ext_blk_list = &phba->sli4_hba.lpfc_vfi_blk_list;
5309 break;
5310 default:
5311 /* Unsupported Opcode. Fail call. */
5312 id_array = NULL;
5313 bmask = NULL;
5314 ids = NULL;
5315 ext_blk_list = NULL;
5316 goto err_exit;
5317 }
5318
5319 /*
5320 * Complete initializing the extent configuration with the
5321 * allocated ids assigned to this function. The bitmask serves
5322 * as an index into the array and manages the available ids. The
5323 * array just stores the ids communicated to the port via the wqes.
5324 */
5325 for (i = 0, j = 0, k = 0; i < rsrc_cnt; i++) {
5326 if ((i % 2) == 0)
5327 rsrc_id = bf_get(lpfc_mbx_rsrc_id_word4_0,
5328 &id_array[k]);
5329 else
5330 rsrc_id = bf_get(lpfc_mbx_rsrc_id_word4_1,
5331 &id_array[k]);
5332
5333 rsrc_blks = kzalloc(length, GFP_KERNEL);
5334 if (unlikely(!rsrc_blks)) {
5335 rc = -ENOMEM;
5336 kfree(bmask);
5337 kfree(ids);
5338 goto err_exit;
5339 }
5340 rsrc_blks->rsrc_start = rsrc_id;
5341 rsrc_blks->rsrc_size = rsrc_size;
5342 list_add_tail(&rsrc_blks->list, ext_blk_list);
5343 rsrc_start = rsrc_id;
5344 if ((type == LPFC_RSC_TYPE_FCOE_XRI) && (j == 0))
5345 phba->sli4_hba.scsi_xri_start = rsrc_start +
5346 lpfc_sli4_get_els_iocb_cnt(phba);
5347
5348 while (rsrc_id < (rsrc_start + rsrc_size)) {
5349 ids[j] = rsrc_id;
5350 rsrc_id++;
5351 j++;
5352 }
5353 /* Entire word processed. Get next word.*/
5354 if ((i % 2) == 1)
5355 k++;
5356 }
5357 err_exit:
5358 lpfc_sli4_mbox_cmd_free(phba, mbox);
5359 return rc;
5360}
5361
5362/**
5363 * lpfc_sli4_dealloc_extent - Deallocate an SLI4 resource extent.
5364 * @phba: Pointer to HBA context object.
5365 * @type: the extent's type.
5366 *
5367 * This function deallocates all extents of a particular resource type.
5368 * SLI4 does not allow for deallocating a particular extent range. It
5369 * is the caller's responsibility to release all kernel memory resources.
5370 **/
5371static int
5372lpfc_sli4_dealloc_extent(struct lpfc_hba *phba, uint16_t type)
5373{
5374 int rc;
5375 uint32_t length, mbox_tmo = 0;
5376 LPFC_MBOXQ_t *mbox;
5377 struct lpfc_mbx_dealloc_rsrc_extents *dealloc_rsrc;
5378 struct lpfc_rsrc_blks *rsrc_blk, *rsrc_blk_next;
5379
5380 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5381 if (!mbox)
5382 return -ENOMEM;
5383
5384 /*
5385 * This function sends an embedded mailbox because it only sends the
5386 * the resource type. All extents of this type are released by the
5387 * port.
5388 */
5389 length = (sizeof(struct lpfc_mbx_dealloc_rsrc_extents) -
5390 sizeof(struct lpfc_sli4_cfg_mhdr));
5391 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5392 LPFC_MBOX_OPCODE_DEALLOC_RSRC_EXTENT,
5393 length, LPFC_SLI4_MBX_EMBED);
5394
5395 /* Send an extents count of 0 - the dealloc doesn't use it. */
5396 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, 0, type,
5397 LPFC_SLI4_MBX_EMBED);
5398 if (unlikely(rc)) {
5399 rc = -EIO;
5400 goto out_free_mbox;
5401 }
5402 if (!phba->sli4_hba.intr_enable)
5403 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5404 else {
James Smarta183a152011-10-10 21:32:43 -04005405 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart6d368e52011-05-24 11:44:12 -04005406 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5407 }
5408 if (unlikely(rc)) {
5409 rc = -EIO;
5410 goto out_free_mbox;
5411 }
5412
5413 dealloc_rsrc = &mbox->u.mqe.un.dealloc_rsrc_extents;
5414 if (bf_get(lpfc_mbox_hdr_status,
5415 &dealloc_rsrc->header.cfg_shdr.response)) {
5416 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
5417 "2919 Failed to release resource extents "
5418 "for type %d - Status 0x%x Add'l Status 0x%x. "
5419 "Resource memory not released.\n",
5420 type,
5421 bf_get(lpfc_mbox_hdr_status,
5422 &dealloc_rsrc->header.cfg_shdr.response),
5423 bf_get(lpfc_mbox_hdr_add_status,
5424 &dealloc_rsrc->header.cfg_shdr.response));
5425 rc = -EIO;
5426 goto out_free_mbox;
5427 }
5428
5429 /* Release kernel memory resources for the specific type. */
5430 switch (type) {
5431 case LPFC_RSC_TYPE_FCOE_VPI:
5432 kfree(phba->vpi_bmask);
5433 kfree(phba->vpi_ids);
5434 bf_set(lpfc_vpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5435 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5436 &phba->lpfc_vpi_blk_list, list) {
5437 list_del_init(&rsrc_blk->list);
5438 kfree(rsrc_blk);
5439 }
5440 break;
5441 case LPFC_RSC_TYPE_FCOE_XRI:
5442 kfree(phba->sli4_hba.xri_bmask);
5443 kfree(phba->sli4_hba.xri_ids);
James Smart6d368e52011-05-24 11:44:12 -04005444 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5445 &phba->sli4_hba.lpfc_xri_blk_list, list) {
5446 list_del_init(&rsrc_blk->list);
5447 kfree(rsrc_blk);
5448 }
5449 break;
5450 case LPFC_RSC_TYPE_FCOE_VFI:
5451 kfree(phba->sli4_hba.vfi_bmask);
5452 kfree(phba->sli4_hba.vfi_ids);
5453 bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5454 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5455 &phba->sli4_hba.lpfc_vfi_blk_list, list) {
5456 list_del_init(&rsrc_blk->list);
5457 kfree(rsrc_blk);
5458 }
5459 break;
5460 case LPFC_RSC_TYPE_FCOE_RPI:
5461 /* RPI bitmask and physical id array are cleaned up earlier. */
5462 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5463 &phba->sli4_hba.lpfc_rpi_blk_list, list) {
5464 list_del_init(&rsrc_blk->list);
5465 kfree(rsrc_blk);
5466 }
5467 break;
5468 default:
5469 break;
5470 }
5471
5472 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5473
5474 out_free_mbox:
5475 mempool_free(mbox, phba->mbox_mem_pool);
5476 return rc;
5477}
5478
5479/**
5480 * lpfc_sli4_alloc_resource_identifiers - Allocate all SLI4 resource extents.
5481 * @phba: Pointer to HBA context object.
5482 *
5483 * This function allocates all SLI4 resource identifiers.
5484 **/
5485int
5486lpfc_sli4_alloc_resource_identifiers(struct lpfc_hba *phba)
5487{
5488 int i, rc, error = 0;
5489 uint16_t count, base;
5490 unsigned long longs;
5491
James Smartff78d8f2011-12-13 13:21:35 -05005492 if (!phba->sli4_hba.rpi_hdrs_in_use)
5493 phba->sli4_hba.next_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
James Smart6d368e52011-05-24 11:44:12 -04005494 if (phba->sli4_hba.extents_in_use) {
5495 /*
5496 * The port supports resource extents. The XRI, VPI, VFI, RPI
5497 * resource extent count must be read and allocated before
5498 * provisioning the resource id arrays.
5499 */
5500 if (bf_get(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags) ==
5501 LPFC_IDX_RSRC_RDY) {
5502 /*
5503 * Extent-based resources are set - the driver could
5504 * be in a port reset. Figure out if any corrective
5505 * actions need to be taken.
5506 */
5507 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
5508 LPFC_RSC_TYPE_FCOE_VFI);
5509 if (rc != 0)
5510 error++;
5511 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
5512 LPFC_RSC_TYPE_FCOE_VPI);
5513 if (rc != 0)
5514 error++;
5515 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
5516 LPFC_RSC_TYPE_FCOE_XRI);
5517 if (rc != 0)
5518 error++;
5519 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
5520 LPFC_RSC_TYPE_FCOE_RPI);
5521 if (rc != 0)
5522 error++;
5523
5524 /*
5525 * It's possible that the number of resources
5526 * provided to this port instance changed between
5527 * resets. Detect this condition and reallocate
5528 * resources. Otherwise, there is no action.
5529 */
5530 if (error) {
5531 lpfc_printf_log(phba, KERN_INFO,
5532 LOG_MBOX | LOG_INIT,
5533 "2931 Detected extent resource "
5534 "change. Reallocating all "
5535 "extents.\n");
5536 rc = lpfc_sli4_dealloc_extent(phba,
5537 LPFC_RSC_TYPE_FCOE_VFI);
5538 rc = lpfc_sli4_dealloc_extent(phba,
5539 LPFC_RSC_TYPE_FCOE_VPI);
5540 rc = lpfc_sli4_dealloc_extent(phba,
5541 LPFC_RSC_TYPE_FCOE_XRI);
5542 rc = lpfc_sli4_dealloc_extent(phba,
5543 LPFC_RSC_TYPE_FCOE_RPI);
5544 } else
5545 return 0;
5546 }
5547
5548 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_VFI);
5549 if (unlikely(rc))
5550 goto err_exit;
5551
5552 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_VPI);
5553 if (unlikely(rc))
5554 goto err_exit;
5555
5556 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_RPI);
5557 if (unlikely(rc))
5558 goto err_exit;
5559
5560 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_XRI);
5561 if (unlikely(rc))
5562 goto err_exit;
5563 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags,
5564 LPFC_IDX_RSRC_RDY);
5565 return rc;
5566 } else {
5567 /*
5568 * The port does not support resource extents. The XRI, VPI,
5569 * VFI, RPI resource ids were determined from READ_CONFIG.
5570 * Just allocate the bitmasks and provision the resource id
5571 * arrays. If a port reset is active, the resources don't
5572 * need any action - just exit.
5573 */
5574 if (bf_get(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags) ==
James Smartff78d8f2011-12-13 13:21:35 -05005575 LPFC_IDX_RSRC_RDY) {
5576 lpfc_sli4_dealloc_resource_identifiers(phba);
5577 lpfc_sli4_remove_rpis(phba);
5578 }
James Smart6d368e52011-05-24 11:44:12 -04005579 /* RPIs. */
5580 count = phba->sli4_hba.max_cfg_param.max_rpi;
5581 base = phba->sli4_hba.max_cfg_param.rpi_base;
5582 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
5583 phba->sli4_hba.rpi_bmask = kzalloc(longs *
5584 sizeof(unsigned long),
5585 GFP_KERNEL);
5586 if (unlikely(!phba->sli4_hba.rpi_bmask)) {
5587 rc = -ENOMEM;
5588 goto err_exit;
5589 }
5590 phba->sli4_hba.rpi_ids = kzalloc(count *
5591 sizeof(uint16_t),
5592 GFP_KERNEL);
5593 if (unlikely(!phba->sli4_hba.rpi_ids)) {
5594 rc = -ENOMEM;
5595 goto free_rpi_bmask;
5596 }
5597
5598 for (i = 0; i < count; i++)
5599 phba->sli4_hba.rpi_ids[i] = base + i;
5600
5601 /* VPIs. */
5602 count = phba->sli4_hba.max_cfg_param.max_vpi;
5603 base = phba->sli4_hba.max_cfg_param.vpi_base;
5604 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
5605 phba->vpi_bmask = kzalloc(longs *
5606 sizeof(unsigned long),
5607 GFP_KERNEL);
5608 if (unlikely(!phba->vpi_bmask)) {
5609 rc = -ENOMEM;
5610 goto free_rpi_ids;
5611 }
5612 phba->vpi_ids = kzalloc(count *
5613 sizeof(uint16_t),
5614 GFP_KERNEL);
5615 if (unlikely(!phba->vpi_ids)) {
5616 rc = -ENOMEM;
5617 goto free_vpi_bmask;
5618 }
5619
5620 for (i = 0; i < count; i++)
5621 phba->vpi_ids[i] = base + i;
5622
5623 /* XRIs. */
5624 count = phba->sli4_hba.max_cfg_param.max_xri;
5625 base = phba->sli4_hba.max_cfg_param.xri_base;
5626 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
5627 phba->sli4_hba.xri_bmask = kzalloc(longs *
5628 sizeof(unsigned long),
5629 GFP_KERNEL);
5630 if (unlikely(!phba->sli4_hba.xri_bmask)) {
5631 rc = -ENOMEM;
5632 goto free_vpi_ids;
5633 }
James Smart41899be2012-03-01 22:34:19 -05005634 phba->sli4_hba.max_cfg_param.xri_used = 0;
James Smart6d368e52011-05-24 11:44:12 -04005635 phba->sli4_hba.xri_ids = kzalloc(count *
5636 sizeof(uint16_t),
5637 GFP_KERNEL);
5638 if (unlikely(!phba->sli4_hba.xri_ids)) {
5639 rc = -ENOMEM;
5640 goto free_xri_bmask;
5641 }
5642
5643 for (i = 0; i < count; i++)
5644 phba->sli4_hba.xri_ids[i] = base + i;
5645
5646 /* VFIs. */
5647 count = phba->sli4_hba.max_cfg_param.max_vfi;
5648 base = phba->sli4_hba.max_cfg_param.vfi_base;
5649 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
5650 phba->sli4_hba.vfi_bmask = kzalloc(longs *
5651 sizeof(unsigned long),
5652 GFP_KERNEL);
5653 if (unlikely(!phba->sli4_hba.vfi_bmask)) {
5654 rc = -ENOMEM;
5655 goto free_xri_ids;
5656 }
5657 phba->sli4_hba.vfi_ids = kzalloc(count *
5658 sizeof(uint16_t),
5659 GFP_KERNEL);
5660 if (unlikely(!phba->sli4_hba.vfi_ids)) {
5661 rc = -ENOMEM;
5662 goto free_vfi_bmask;
5663 }
5664
5665 for (i = 0; i < count; i++)
5666 phba->sli4_hba.vfi_ids[i] = base + i;
5667
5668 /*
5669 * Mark all resources ready. An HBA reset doesn't need
5670 * to reset the initialization.
5671 */
5672 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags,
5673 LPFC_IDX_RSRC_RDY);
5674 return 0;
5675 }
5676
5677 free_vfi_bmask:
5678 kfree(phba->sli4_hba.vfi_bmask);
5679 free_xri_ids:
5680 kfree(phba->sli4_hba.xri_ids);
5681 free_xri_bmask:
5682 kfree(phba->sli4_hba.xri_bmask);
5683 free_vpi_ids:
5684 kfree(phba->vpi_ids);
5685 free_vpi_bmask:
5686 kfree(phba->vpi_bmask);
5687 free_rpi_ids:
5688 kfree(phba->sli4_hba.rpi_ids);
5689 free_rpi_bmask:
5690 kfree(phba->sli4_hba.rpi_bmask);
5691 err_exit:
5692 return rc;
5693}
5694
5695/**
5696 * lpfc_sli4_dealloc_resource_identifiers - Deallocate all SLI4 resource extents.
5697 * @phba: Pointer to HBA context object.
5698 *
5699 * This function allocates the number of elements for the specified
5700 * resource type.
5701 **/
5702int
5703lpfc_sli4_dealloc_resource_identifiers(struct lpfc_hba *phba)
5704{
5705 if (phba->sli4_hba.extents_in_use) {
5706 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VPI);
5707 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_RPI);
5708 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_XRI);
5709 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VFI);
5710 } else {
5711 kfree(phba->vpi_bmask);
5712 kfree(phba->vpi_ids);
5713 bf_set(lpfc_vpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5714 kfree(phba->sli4_hba.xri_bmask);
5715 kfree(phba->sli4_hba.xri_ids);
James Smart6d368e52011-05-24 11:44:12 -04005716 kfree(phba->sli4_hba.vfi_bmask);
5717 kfree(phba->sli4_hba.vfi_ids);
5718 bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5719 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5720 }
5721
5722 return 0;
5723}
5724
5725/**
James Smartb76f2dc2011-07-22 18:37:42 -04005726 * lpfc_sli4_get_allocated_extnts - Get the port's allocated extents.
5727 * @phba: Pointer to HBA context object.
5728 * @type: The resource extent type.
5729 * @extnt_count: buffer to hold port extent count response
5730 * @extnt_size: buffer to hold port extent size response.
5731 *
5732 * This function calls the port to read the host allocated extents
5733 * for a particular type.
5734 **/
5735int
5736lpfc_sli4_get_allocated_extnts(struct lpfc_hba *phba, uint16_t type,
5737 uint16_t *extnt_cnt, uint16_t *extnt_size)
5738{
5739 bool emb;
5740 int rc = 0;
5741 uint16_t curr_blks = 0;
5742 uint32_t req_len, emb_len;
5743 uint32_t alloc_len, mbox_tmo;
5744 struct list_head *blk_list_head;
5745 struct lpfc_rsrc_blks *rsrc_blk;
5746 LPFC_MBOXQ_t *mbox;
5747 void *virtaddr = NULL;
5748 struct lpfc_mbx_nembed_rsrc_extent *n_rsrc;
5749 struct lpfc_mbx_alloc_rsrc_extents *rsrc_ext;
5750 union lpfc_sli4_cfg_shdr *shdr;
5751
5752 switch (type) {
5753 case LPFC_RSC_TYPE_FCOE_VPI:
5754 blk_list_head = &phba->lpfc_vpi_blk_list;
5755 break;
5756 case LPFC_RSC_TYPE_FCOE_XRI:
5757 blk_list_head = &phba->sli4_hba.lpfc_xri_blk_list;
5758 break;
5759 case LPFC_RSC_TYPE_FCOE_VFI:
5760 blk_list_head = &phba->sli4_hba.lpfc_vfi_blk_list;
5761 break;
5762 case LPFC_RSC_TYPE_FCOE_RPI:
5763 blk_list_head = &phba->sli4_hba.lpfc_rpi_blk_list;
5764 break;
5765 default:
5766 return -EIO;
5767 }
5768
5769 /* Count the number of extents currently allocatd for this type. */
5770 list_for_each_entry(rsrc_blk, blk_list_head, list) {
5771 if (curr_blks == 0) {
5772 /*
5773 * The GET_ALLOCATED mailbox does not return the size,
5774 * just the count. The size should be just the size
5775 * stored in the current allocated block and all sizes
5776 * for an extent type are the same so set the return
5777 * value now.
5778 */
5779 *extnt_size = rsrc_blk->rsrc_size;
5780 }
5781 curr_blks++;
5782 }
5783
5784 /* Calculate the total requested length of the dma memory. */
5785 req_len = curr_blks * sizeof(uint16_t);
5786
5787 /*
5788 * Calculate the size of an embedded mailbox. The uint32_t
5789 * accounts for extents-specific word.
5790 */
5791 emb_len = sizeof(MAILBOX_t) - sizeof(struct mbox_header) -
5792 sizeof(uint32_t);
5793
5794 /*
5795 * Presume the allocation and response will fit into an embedded
5796 * mailbox. If not true, reconfigure to a non-embedded mailbox.
5797 */
5798 emb = LPFC_SLI4_MBX_EMBED;
5799 req_len = emb_len;
5800 if (req_len > emb_len) {
5801 req_len = curr_blks * sizeof(uint16_t) +
5802 sizeof(union lpfc_sli4_cfg_shdr) +
5803 sizeof(uint32_t);
5804 emb = LPFC_SLI4_MBX_NEMBED;
5805 }
5806
5807 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5808 if (!mbox)
5809 return -ENOMEM;
5810 memset(mbox, 0, sizeof(LPFC_MBOXQ_t));
5811
5812 alloc_len = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5813 LPFC_MBOX_OPCODE_GET_ALLOC_RSRC_EXTENT,
5814 req_len, emb);
5815 if (alloc_len < req_len) {
5816 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5817 "2983 Allocated DMA memory size (x%x) is "
5818 "less than the requested DMA memory "
5819 "size (x%x)\n", alloc_len, req_len);
5820 rc = -ENOMEM;
5821 goto err_exit;
5822 }
5823 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, curr_blks, type, emb);
5824 if (unlikely(rc)) {
5825 rc = -EIO;
5826 goto err_exit;
5827 }
5828
5829 if (!phba->sli4_hba.intr_enable)
5830 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5831 else {
James Smarta183a152011-10-10 21:32:43 -04005832 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smartb76f2dc2011-07-22 18:37:42 -04005833 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5834 }
5835
5836 if (unlikely(rc)) {
5837 rc = -EIO;
5838 goto err_exit;
5839 }
5840
5841 /*
5842 * Figure out where the response is located. Then get local pointers
5843 * to the response data. The port does not guarantee to respond to
5844 * all extents counts request so update the local variable with the
5845 * allocated count from the port.
5846 */
5847 if (emb == LPFC_SLI4_MBX_EMBED) {
5848 rsrc_ext = &mbox->u.mqe.un.alloc_rsrc_extents;
5849 shdr = &rsrc_ext->header.cfg_shdr;
5850 *extnt_cnt = bf_get(lpfc_mbx_rsrc_cnt, &rsrc_ext->u.rsp);
5851 } else {
5852 virtaddr = mbox->sge_array->addr[0];
5853 n_rsrc = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
5854 shdr = &n_rsrc->cfg_shdr;
5855 *extnt_cnt = bf_get(lpfc_mbx_rsrc_cnt, n_rsrc);
5856 }
5857
5858 if (bf_get(lpfc_mbox_hdr_status, &shdr->response)) {
5859 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
5860 "2984 Failed to read allocated resources "
5861 "for type %d - Status 0x%x Add'l Status 0x%x.\n",
5862 type,
5863 bf_get(lpfc_mbox_hdr_status, &shdr->response),
5864 bf_get(lpfc_mbox_hdr_add_status, &shdr->response));
5865 rc = -EIO;
5866 goto err_exit;
5867 }
5868 err_exit:
5869 lpfc_sli4_mbox_cmd_free(phba, mbox);
5870 return rc;
5871}
5872
5873/**
James Smart8a9d2e82012-05-09 21:16:12 -04005874 * lpfc_sli4_repost_els_sgl_list - Repsot the els buffers sgl pages as block
5875 * @phba: pointer to lpfc hba data structure.
5876 *
5877 * This routine walks the list of els buffers that have been allocated and
5878 * repost them to the port by using SGL block post. This is needed after a
5879 * pci_function_reset/warm_start or start. It attempts to construct blocks
5880 * of els buffer sgls which contains contiguous xris and uses the non-embedded
5881 * SGL block post mailbox commands to post them to the port. For single els
5882 * buffer sgl with non-contiguous xri, if any, it shall use embedded SGL post
5883 * mailbox command for posting.
5884 *
5885 * Returns: 0 = success, non-zero failure.
5886 **/
5887static int
5888lpfc_sli4_repost_els_sgl_list(struct lpfc_hba *phba)
5889{
5890 struct lpfc_sglq *sglq_entry = NULL;
5891 struct lpfc_sglq *sglq_entry_next = NULL;
5892 struct lpfc_sglq *sglq_entry_first = NULL;
5893 int status, post_cnt = 0, num_posted = 0, block_cnt = 0;
5894 int last_xritag = NO_XRI;
5895 LIST_HEAD(prep_sgl_list);
5896 LIST_HEAD(blck_sgl_list);
5897 LIST_HEAD(allc_sgl_list);
5898 LIST_HEAD(post_sgl_list);
5899 LIST_HEAD(free_sgl_list);
5900
5901 spin_lock(&phba->hbalock);
5902 list_splice_init(&phba->sli4_hba.lpfc_sgl_list, &allc_sgl_list);
5903 spin_unlock(&phba->hbalock);
5904
5905 list_for_each_entry_safe(sglq_entry, sglq_entry_next,
5906 &allc_sgl_list, list) {
5907 list_del_init(&sglq_entry->list);
5908 block_cnt++;
5909 if ((last_xritag != NO_XRI) &&
5910 (sglq_entry->sli4_xritag != last_xritag + 1)) {
5911 /* a hole in xri block, form a sgl posting block */
5912 list_splice_init(&prep_sgl_list, &blck_sgl_list);
5913 post_cnt = block_cnt - 1;
5914 /* prepare list for next posting block */
5915 list_add_tail(&sglq_entry->list, &prep_sgl_list);
5916 block_cnt = 1;
5917 } else {
5918 /* prepare list for next posting block */
5919 list_add_tail(&sglq_entry->list, &prep_sgl_list);
5920 /* enough sgls for non-embed sgl mbox command */
5921 if (block_cnt == LPFC_NEMBED_MBOX_SGL_CNT) {
5922 list_splice_init(&prep_sgl_list,
5923 &blck_sgl_list);
5924 post_cnt = block_cnt;
5925 block_cnt = 0;
5926 }
5927 }
5928 num_posted++;
5929
5930 /* keep track of last sgl's xritag */
5931 last_xritag = sglq_entry->sli4_xritag;
5932
5933 /* end of repost sgl list condition for els buffers */
5934 if (num_posted == phba->sli4_hba.els_xri_cnt) {
5935 if (post_cnt == 0) {
5936 list_splice_init(&prep_sgl_list,
5937 &blck_sgl_list);
5938 post_cnt = block_cnt;
5939 } else if (block_cnt == 1) {
5940 status = lpfc_sli4_post_sgl(phba,
5941 sglq_entry->phys, 0,
5942 sglq_entry->sli4_xritag);
5943 if (!status) {
5944 /* successful, put sgl to posted list */
5945 list_add_tail(&sglq_entry->list,
5946 &post_sgl_list);
5947 } else {
5948 /* Failure, put sgl to free list */
5949 lpfc_printf_log(phba, KERN_WARNING,
5950 LOG_SLI,
5951 "3159 Failed to post els "
5952 "sgl, xritag:x%x\n",
5953 sglq_entry->sli4_xritag);
5954 list_add_tail(&sglq_entry->list,
5955 &free_sgl_list);
5956 spin_lock_irq(&phba->hbalock);
5957 phba->sli4_hba.els_xri_cnt--;
5958 spin_unlock_irq(&phba->hbalock);
5959 }
5960 }
5961 }
5962
5963 /* continue until a nembed page worth of sgls */
5964 if (post_cnt == 0)
5965 continue;
5966
5967 /* post the els buffer list sgls as a block */
5968 status = lpfc_sli4_post_els_sgl_list(phba, &blck_sgl_list,
5969 post_cnt);
5970
5971 if (!status) {
5972 /* success, put sgl list to posted sgl list */
5973 list_splice_init(&blck_sgl_list, &post_sgl_list);
5974 } else {
5975 /* Failure, put sgl list to free sgl list */
5976 sglq_entry_first = list_first_entry(&blck_sgl_list,
5977 struct lpfc_sglq,
5978 list);
5979 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
5980 "3160 Failed to post els sgl-list, "
5981 "xritag:x%x-x%x\n",
5982 sglq_entry_first->sli4_xritag,
5983 (sglq_entry_first->sli4_xritag +
5984 post_cnt - 1));
5985 list_splice_init(&blck_sgl_list, &free_sgl_list);
5986 spin_lock_irq(&phba->hbalock);
5987 phba->sli4_hba.els_xri_cnt -= post_cnt;
5988 spin_unlock_irq(&phba->hbalock);
5989 }
5990
5991 /* don't reset xirtag due to hole in xri block */
5992 if (block_cnt == 0)
5993 last_xritag = NO_XRI;
5994
5995 /* reset els sgl post count for next round of posting */
5996 post_cnt = 0;
5997 }
5998
5999 /* free the els sgls failed to post */
6000 lpfc_free_sgl_list(phba, &free_sgl_list);
6001
6002 /* push els sgls posted to the availble list */
6003 if (!list_empty(&post_sgl_list)) {
6004 spin_lock(&phba->hbalock);
6005 list_splice_init(&post_sgl_list,
6006 &phba->sli4_hba.lpfc_sgl_list);
6007 spin_unlock(&phba->hbalock);
6008 } else {
6009 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6010 "3161 Failure to post els sgl to port.\n");
6011 return -EIO;
6012 }
6013 return 0;
6014}
6015
6016/**
James Smartda0436e2009-05-22 14:51:39 -04006017 * lpfc_sli4_hba_setup - SLI4 device intialization PCI function
6018 * @phba: Pointer to HBA context object.
6019 *
6020 * This function is the main SLI4 device intialization PCI function. This
6021 * function is called by the HBA intialization code, HBA reset code and
6022 * HBA error attention handler code. Caller is not required to hold any
6023 * locks.
6024 **/
6025int
6026lpfc_sli4_hba_setup(struct lpfc_hba *phba)
6027{
6028 int rc;
6029 LPFC_MBOXQ_t *mboxq;
6030 struct lpfc_mqe *mqe;
6031 uint8_t *vpd;
6032 uint32_t vpd_size;
6033 uint32_t ftr_rsp = 0;
6034 struct Scsi_Host *shost = lpfc_shost_from_vport(phba->pport);
6035 struct lpfc_vport *vport = phba->pport;
6036 struct lpfc_dmabuf *mp;
6037
6038 /* Perform a PCI function reset to start from clean */
6039 rc = lpfc_pci_function_reset(phba);
6040 if (unlikely(rc))
6041 return -ENODEV;
6042
6043 /* Check the HBA Host Status Register for readyness */
6044 rc = lpfc_sli4_post_status_check(phba);
6045 if (unlikely(rc))
6046 return -ENODEV;
6047 else {
6048 spin_lock_irq(&phba->hbalock);
6049 phba->sli.sli_flag |= LPFC_SLI_ACTIVE;
6050 spin_unlock_irq(&phba->hbalock);
6051 }
6052
6053 /*
6054 * Allocate a single mailbox container for initializing the
6055 * port.
6056 */
6057 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6058 if (!mboxq)
6059 return -ENOMEM;
6060
James Smartda0436e2009-05-22 14:51:39 -04006061 /* Issue READ_REV to collect vpd and FW information. */
James Smart49198b32010-04-06 15:04:33 -04006062 vpd_size = SLI4_PAGE_SIZE;
James Smartda0436e2009-05-22 14:51:39 -04006063 vpd = kzalloc(vpd_size, GFP_KERNEL);
6064 if (!vpd) {
6065 rc = -ENOMEM;
6066 goto out_free_mbox;
6067 }
6068
6069 rc = lpfc_sli4_read_rev(phba, mboxq, vpd, &vpd_size);
James Smart76a95d72010-11-20 23:11:48 -05006070 if (unlikely(rc)) {
6071 kfree(vpd);
6072 goto out_free_mbox;
6073 }
James Smartda0436e2009-05-22 14:51:39 -04006074 mqe = &mboxq->u.mqe;
James Smartf1126682009-06-10 17:22:44 -04006075 phba->sli_rev = bf_get(lpfc_mbx_rd_rev_sli_lvl, &mqe->un.read_rev);
6076 if (bf_get(lpfc_mbx_rd_rev_fcoe, &mqe->un.read_rev))
James Smart76a95d72010-11-20 23:11:48 -05006077 phba->hba_flag |= HBA_FCOE_MODE;
6078 else
6079 phba->hba_flag &= ~HBA_FCOE_MODE;
James Smart45ed1192009-10-02 15:17:02 -04006080
6081 if (bf_get(lpfc_mbx_rd_rev_cee_ver, &mqe->un.read_rev) ==
6082 LPFC_DCBX_CEE_MODE)
6083 phba->hba_flag |= HBA_FIP_SUPPORT;
6084 else
6085 phba->hba_flag &= ~HBA_FIP_SUPPORT;
6086
James Smart4f2e66c2012-05-09 21:17:07 -04006087 phba->hba_flag &= ~HBA_FCP_IOQ_FLUSH;
6088
James Smartc31098c2011-04-16 11:03:33 -04006089 if (phba->sli_rev != LPFC_SLI_REV4) {
James Smartda0436e2009-05-22 14:51:39 -04006090 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6091 "0376 READ_REV Error. SLI Level %d "
6092 "FCoE enabled %d\n",
James Smart76a95d72010-11-20 23:11:48 -05006093 phba->sli_rev, phba->hba_flag & HBA_FCOE_MODE);
James Smartda0436e2009-05-22 14:51:39 -04006094 rc = -EIO;
James Smart76a95d72010-11-20 23:11:48 -05006095 kfree(vpd);
6096 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04006097 }
James Smartcd1c8302011-10-10 21:33:25 -04006098
6099 /*
James Smartff78d8f2011-12-13 13:21:35 -05006100 * Continue initialization with default values even if driver failed
6101 * to read FCoE param config regions, only read parameters if the
6102 * board is FCoE
6103 */
6104 if (phba->hba_flag & HBA_FCOE_MODE &&
6105 lpfc_sli4_read_fcoe_params(phba))
6106 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_INIT,
6107 "2570 Failed to read FCoE parameters\n");
6108
6109 /*
James Smartcd1c8302011-10-10 21:33:25 -04006110 * Retrieve sli4 device physical port name, failure of doing it
6111 * is considered as non-fatal.
6112 */
6113 rc = lpfc_sli4_retrieve_pport_name(phba);
6114 if (!rc)
6115 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
6116 "3080 Successful retrieving SLI4 device "
6117 "physical port name: %s.\n", phba->Port);
6118
James Smartda0436e2009-05-22 14:51:39 -04006119 /*
6120 * Evaluate the read rev and vpd data. Populate the driver
6121 * state with the results. If this routine fails, the failure
6122 * is not fatal as the driver will use generic values.
6123 */
6124 rc = lpfc_parse_vpd(phba, vpd, vpd_size);
6125 if (unlikely(!rc)) {
6126 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6127 "0377 Error %d parsing vpd. "
6128 "Using defaults.\n", rc);
6129 rc = 0;
6130 }
James Smart76a95d72010-11-20 23:11:48 -05006131 kfree(vpd);
James Smartda0436e2009-05-22 14:51:39 -04006132
James Smartf1126682009-06-10 17:22:44 -04006133 /* Save information as VPD data */
6134 phba->vpd.rev.biuRev = mqe->un.read_rev.first_hw_rev;
6135 phba->vpd.rev.smRev = mqe->un.read_rev.second_hw_rev;
6136 phba->vpd.rev.endecRev = mqe->un.read_rev.third_hw_rev;
6137 phba->vpd.rev.fcphHigh = bf_get(lpfc_mbx_rd_rev_fcph_high,
6138 &mqe->un.read_rev);
6139 phba->vpd.rev.fcphLow = bf_get(lpfc_mbx_rd_rev_fcph_low,
6140 &mqe->un.read_rev);
6141 phba->vpd.rev.feaLevelHigh = bf_get(lpfc_mbx_rd_rev_ftr_lvl_high,
6142 &mqe->un.read_rev);
6143 phba->vpd.rev.feaLevelLow = bf_get(lpfc_mbx_rd_rev_ftr_lvl_low,
6144 &mqe->un.read_rev);
6145 phba->vpd.rev.sli1FwRev = mqe->un.read_rev.fw_id_rev;
6146 memcpy(phba->vpd.rev.sli1FwName, mqe->un.read_rev.fw_name, 16);
6147 phba->vpd.rev.sli2FwRev = mqe->un.read_rev.ulp_fw_id_rev;
6148 memcpy(phba->vpd.rev.sli2FwName, mqe->un.read_rev.ulp_fw_name, 16);
6149 phba->vpd.rev.opFwRev = mqe->un.read_rev.fw_id_rev;
6150 memcpy(phba->vpd.rev.opFwName, mqe->un.read_rev.fw_name, 16);
6151 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
6152 "(%d):0380 READ_REV Status x%x "
6153 "fw_rev:%s fcphHi:%x fcphLo:%x flHi:%x flLo:%x\n",
6154 mboxq->vport ? mboxq->vport->vpi : 0,
6155 bf_get(lpfc_mqe_status, mqe),
6156 phba->vpd.rev.opFwName,
6157 phba->vpd.rev.fcphHigh, phba->vpd.rev.fcphLow,
6158 phba->vpd.rev.feaLevelHigh, phba->vpd.rev.feaLevelLow);
James Smartda0436e2009-05-22 14:51:39 -04006159
6160 /*
6161 * Discover the port's supported feature set and match it against the
6162 * hosts requests.
6163 */
6164 lpfc_request_features(phba, mboxq);
6165 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6166 if (unlikely(rc)) {
6167 rc = -EIO;
James Smart76a95d72010-11-20 23:11:48 -05006168 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04006169 }
6170
6171 /*
6172 * The port must support FCP initiator mode as this is the
6173 * only mode running in the host.
6174 */
6175 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_fcpi, &mqe->un.req_ftrs))) {
6176 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
6177 "0378 No support for fcpi mode.\n");
6178 ftr_rsp++;
6179 }
James Smartfedd3b72011-02-16 12:39:24 -05006180 if (bf_get(lpfc_mbx_rq_ftr_rsp_perfh, &mqe->un.req_ftrs))
6181 phba->sli3_options |= LPFC_SLI4_PERFH_ENABLED;
6182 else
6183 phba->sli3_options &= ~LPFC_SLI4_PERFH_ENABLED;
James Smartda0436e2009-05-22 14:51:39 -04006184 /*
6185 * If the port cannot support the host's requested features
6186 * then turn off the global config parameters to disable the
6187 * feature in the driver. This is not a fatal error.
6188 */
James Smartbf086112011-08-21 21:48:13 -04006189 phba->sli3_options &= ~LPFC_SLI3_BG_ENABLED;
6190 if (phba->cfg_enable_bg) {
6191 if (bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs))
6192 phba->sli3_options |= LPFC_SLI3_BG_ENABLED;
6193 else
6194 ftr_rsp++;
6195 }
James Smartda0436e2009-05-22 14:51:39 -04006196
6197 if (phba->max_vpi && phba->cfg_enable_npiv &&
6198 !(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
6199 ftr_rsp++;
6200
6201 if (ftr_rsp) {
6202 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
6203 "0379 Feature Mismatch Data: x%08x %08x "
6204 "x%x x%x x%x\n", mqe->un.req_ftrs.word2,
6205 mqe->un.req_ftrs.word3, phba->cfg_enable_bg,
6206 phba->cfg_enable_npiv, phba->max_vpi);
6207 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs)))
6208 phba->cfg_enable_bg = 0;
6209 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
6210 phba->cfg_enable_npiv = 0;
6211 }
6212
6213 /* These SLI3 features are assumed in SLI4 */
6214 spin_lock_irq(&phba->hbalock);
6215 phba->sli3_options |= (LPFC_SLI3_NPIV_ENABLED | LPFC_SLI3_HBQ_ENABLED);
6216 spin_unlock_irq(&phba->hbalock);
6217
James Smart6d368e52011-05-24 11:44:12 -04006218 /*
6219 * Allocate all resources (xri,rpi,vpi,vfi) now. Subsequent
6220 * calls depends on these resources to complete port setup.
6221 */
6222 rc = lpfc_sli4_alloc_resource_identifiers(phba);
6223 if (rc) {
6224 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6225 "2920 Failed to alloc Resource IDs "
6226 "rc = x%x\n", rc);
6227 goto out_free_mbox;
6228 }
6229
James Smartda0436e2009-05-22 14:51:39 -04006230 /* Read the port's service parameters. */
James Smart9f1177a2010-02-26 14:12:57 -05006231 rc = lpfc_read_sparam(phba, mboxq, vport->vpi);
6232 if (rc) {
6233 phba->link_state = LPFC_HBA_ERROR;
6234 rc = -ENOMEM;
James Smart76a95d72010-11-20 23:11:48 -05006235 goto out_free_mbox;
James Smart9f1177a2010-02-26 14:12:57 -05006236 }
6237
James Smartda0436e2009-05-22 14:51:39 -04006238 mboxq->vport = vport;
6239 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6240 mp = (struct lpfc_dmabuf *) mboxq->context1;
6241 if (rc == MBX_SUCCESS) {
6242 memcpy(&vport->fc_sparam, mp->virt, sizeof(struct serv_parm));
6243 rc = 0;
6244 }
6245
6246 /*
6247 * This memory was allocated by the lpfc_read_sparam routine. Release
6248 * it to the mbuf pool.
6249 */
6250 lpfc_mbuf_free(phba, mp->virt, mp->phys);
6251 kfree(mp);
6252 mboxq->context1 = NULL;
6253 if (unlikely(rc)) {
6254 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6255 "0382 READ_SPARAM command failed "
6256 "status %d, mbxStatus x%x\n",
6257 rc, bf_get(lpfc_mqe_status, mqe));
6258 phba->link_state = LPFC_HBA_ERROR;
6259 rc = -EIO;
James Smart76a95d72010-11-20 23:11:48 -05006260 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04006261 }
6262
James Smart05580562011-05-24 11:40:48 -04006263 lpfc_update_vport_wwn(vport);
James Smartda0436e2009-05-22 14:51:39 -04006264
6265 /* Update the fc_host data structures with new wwn. */
6266 fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
6267 fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
6268
James Smart8a9d2e82012-05-09 21:16:12 -04006269 /* update host els and scsi xri-sgl sizes and mappings */
6270 rc = lpfc_sli4_xri_sgl_update(phba);
6271 if (unlikely(rc)) {
6272 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6273 "1400 Failed to update xri-sgl size and "
6274 "mapping: %d\n", rc);
6275 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04006276 }
6277
James Smart8a9d2e82012-05-09 21:16:12 -04006278 /* register the els sgl pool to the port */
6279 rc = lpfc_sli4_repost_els_sgl_list(phba);
6280 if (unlikely(rc)) {
6281 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6282 "0582 Error %d during els sgl post "
6283 "operation\n", rc);
6284 rc = -ENODEV;
6285 goto out_free_mbox;
6286 }
6287
6288 /* register the allocated scsi sgl pool to the port */
James Smartda0436e2009-05-22 14:51:39 -04006289 rc = lpfc_sli4_repost_scsi_sgl_list(phba);
6290 if (unlikely(rc)) {
James Smart6d368e52011-05-24 11:44:12 -04006291 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smart6a9c52c2009-10-02 15:16:51 -04006292 "0383 Error %d during scsi sgl post "
6293 "operation\n", rc);
James Smartda0436e2009-05-22 14:51:39 -04006294 /* Some Scsi buffers were moved to the abort scsi list */
6295 /* A pci function reset will repost them */
6296 rc = -ENODEV;
James Smart76a95d72010-11-20 23:11:48 -05006297 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04006298 }
6299
6300 /* Post the rpi header region to the device. */
6301 rc = lpfc_sli4_post_all_rpi_hdrs(phba);
6302 if (unlikely(rc)) {
6303 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6304 "0393 Error %d during rpi post operation\n",
6305 rc);
6306 rc = -ENODEV;
James Smart76a95d72010-11-20 23:11:48 -05006307 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04006308 }
James Smart97f2ecf2012-03-01 22:35:23 -05006309 lpfc_sli4_node_prep(phba);
James Smartda0436e2009-05-22 14:51:39 -04006310
James Smart5350d872011-10-10 21:33:49 -04006311 /* Create all the SLI4 queues */
6312 rc = lpfc_sli4_queue_create(phba);
6313 if (rc) {
6314 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6315 "3089 Failed to allocate queues\n");
6316 rc = -ENODEV;
6317 goto out_stop_timers;
6318 }
James Smartda0436e2009-05-22 14:51:39 -04006319 /* Set up all the queues to the device */
6320 rc = lpfc_sli4_queue_setup(phba);
6321 if (unlikely(rc)) {
6322 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6323 "0381 Error %d during queue setup.\n ", rc);
James Smart5350d872011-10-10 21:33:49 -04006324 goto out_destroy_queue;
James Smartda0436e2009-05-22 14:51:39 -04006325 }
6326
6327 /* Arm the CQs and then EQs on device */
6328 lpfc_sli4_arm_cqeq_intr(phba);
6329
6330 /* Indicate device interrupt mode */
6331 phba->sli4_hba.intr_enable = 1;
6332
6333 /* Allow asynchronous mailbox command to go through */
6334 spin_lock_irq(&phba->hbalock);
6335 phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
6336 spin_unlock_irq(&phba->hbalock);
6337
6338 /* Post receive buffers to the device */
6339 lpfc_sli4_rb_setup(phba);
6340
James Smartfc2b9892010-02-26 14:15:29 -05006341 /* Reset HBA FCF states after HBA reset */
6342 phba->fcf.fcf_flag = 0;
6343 phba->fcf.current_rec.flag = 0;
6344
James Smartda0436e2009-05-22 14:51:39 -04006345 /* Start the ELS watchdog timer */
James Smart8fa38512009-07-19 10:01:03 -04006346 mod_timer(&vport->els_tmofunc,
6347 jiffies + HZ * (phba->fc_ratov * 2));
James Smartda0436e2009-05-22 14:51:39 -04006348
6349 /* Start heart beat timer */
6350 mod_timer(&phba->hb_tmofunc,
6351 jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
6352 phba->hb_outstanding = 0;
6353 phba->last_completion_time = jiffies;
6354
6355 /* Start error attention (ERATT) polling timer */
6356 mod_timer(&phba->eratt_poll, jiffies + HZ * LPFC_ERATT_POLL_INTERVAL);
6357
James Smart75baf692010-06-08 18:31:21 -04006358 /* Enable PCIe device Advanced Error Reporting (AER) if configured */
6359 if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
6360 rc = pci_enable_pcie_error_reporting(phba->pcidev);
6361 if (!rc) {
6362 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6363 "2829 This device supports "
6364 "Advanced Error Reporting (AER)\n");
6365 spin_lock_irq(&phba->hbalock);
6366 phba->hba_flag |= HBA_AER_ENABLED;
6367 spin_unlock_irq(&phba->hbalock);
6368 } else {
6369 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6370 "2830 This device does not support "
6371 "Advanced Error Reporting (AER)\n");
6372 phba->cfg_aer_support = 0;
6373 }
James Smart0a96e972011-07-22 18:37:28 -04006374 rc = 0;
James Smart75baf692010-06-08 18:31:21 -04006375 }
6376
James Smart76a95d72010-11-20 23:11:48 -05006377 if (!(phba->hba_flag & HBA_FCOE_MODE)) {
6378 /*
6379 * The FC Port needs to register FCFI (index 0)
6380 */
6381 lpfc_reg_fcfi(phba, mboxq);
6382 mboxq->vport = phba->pport;
6383 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
James Smart9589b0622011-04-16 11:03:17 -04006384 if (rc != MBX_SUCCESS)
James Smart76a95d72010-11-20 23:11:48 -05006385 goto out_unset_queue;
James Smart9589b0622011-04-16 11:03:17 -04006386 rc = 0;
6387 phba->fcf.fcfi = bf_get(lpfc_reg_fcfi_fcfi,
6388 &mboxq->u.mqe.un.reg_fcfi);
James Smart026abb82011-12-13 13:20:45 -05006389
6390 /* Check if the port is configured to be disabled */
6391 lpfc_sli_read_link_ste(phba);
James Smart76a95d72010-11-20 23:11:48 -05006392 }
James Smart026abb82011-12-13 13:20:45 -05006393
James Smartda0436e2009-05-22 14:51:39 -04006394 /*
6395 * The port is ready, set the host's link state to LINK_DOWN
6396 * in preparation for link interrupts.
6397 */
James Smartda0436e2009-05-22 14:51:39 -04006398 spin_lock_irq(&phba->hbalock);
6399 phba->link_state = LPFC_LINK_DOWN;
6400 spin_unlock_irq(&phba->hbalock);
James Smart026abb82011-12-13 13:20:45 -05006401 if (!(phba->hba_flag & HBA_FCOE_MODE) &&
6402 (phba->hba_flag & LINK_DISABLED)) {
6403 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_SLI,
6404 "3103 Adapter Link is disabled.\n");
6405 lpfc_down_link(phba, mboxq);
6406 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6407 if (rc != MBX_SUCCESS) {
6408 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_SLI,
6409 "3104 Adapter failed to issue "
6410 "DOWN_LINK mbox cmd, rc:x%x\n", rc);
6411 goto out_unset_queue;
6412 }
6413 } else if (phba->cfg_suppress_link_up == LPFC_INITIALIZE_LINK) {
James Smart1b511972011-12-13 13:23:09 -05006414 /* don't perform init_link on SLI4 FC port loopback test */
6415 if (!(phba->link_flag & LS_LOOPBACK_MODE)) {
6416 rc = phba->lpfc_hba_init_link(phba, MBX_NOWAIT);
6417 if (rc)
6418 goto out_unset_queue;
6419 }
James Smart5350d872011-10-10 21:33:49 -04006420 }
6421 mempool_free(mboxq, phba->mbox_mem_pool);
6422 return rc;
James Smart76a95d72010-11-20 23:11:48 -05006423out_unset_queue:
James Smartda0436e2009-05-22 14:51:39 -04006424 /* Unset all the queues set up in this routine when error out */
James Smart5350d872011-10-10 21:33:49 -04006425 lpfc_sli4_queue_unset(phba);
6426out_destroy_queue:
6427 lpfc_sli4_queue_destroy(phba);
James Smartda0436e2009-05-22 14:51:39 -04006428out_stop_timers:
James Smart5350d872011-10-10 21:33:49 -04006429 lpfc_stop_hba_timers(phba);
James Smartda0436e2009-05-22 14:51:39 -04006430out_free_mbox:
6431 mempool_free(mboxq, phba->mbox_mem_pool);
6432 return rc;
6433}
James Smarte59058c2008-08-24 21:49:00 -04006434
6435/**
James Smart3621a712009-04-06 18:47:14 -04006436 * lpfc_mbox_timeout - Timeout call back function for mbox timer
James Smarte59058c2008-08-24 21:49:00 -04006437 * @ptr: context object - pointer to hba structure.
dea31012005-04-17 16:05:31 -05006438 *
James Smarte59058c2008-08-24 21:49:00 -04006439 * This is the callback function for mailbox timer. The mailbox
6440 * timer is armed when a new mailbox command is issued and the timer
6441 * is deleted when the mailbox complete. The function is called by
6442 * the kernel timer code when a mailbox does not complete within
6443 * expected time. This function wakes up the worker thread to
6444 * process the mailbox timeout and returns. All the processing is
6445 * done by the worker thread function lpfc_mbox_timeout_handler.
6446 **/
dea31012005-04-17 16:05:31 -05006447void
6448lpfc_mbox_timeout(unsigned long ptr)
6449{
James Smart92d7f7b2007-06-17 19:56:38 -05006450 struct lpfc_hba *phba = (struct lpfc_hba *) ptr;
dea31012005-04-17 16:05:31 -05006451 unsigned long iflag;
James Smart2e0fef82007-06-17 19:56:36 -05006452 uint32_t tmo_posted;
dea31012005-04-17 16:05:31 -05006453
James Smart2e0fef82007-06-17 19:56:36 -05006454 spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
James Smart92d7f7b2007-06-17 19:56:38 -05006455 tmo_posted = phba->pport->work_port_events & WORKER_MBOX_TMO;
James Smart2e0fef82007-06-17 19:56:36 -05006456 if (!tmo_posted)
6457 phba->pport->work_port_events |= WORKER_MBOX_TMO;
6458 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
6459
James Smart5e9d9b82008-06-14 22:52:53 -04006460 if (!tmo_posted)
6461 lpfc_worker_wake_up(phba);
6462 return;
dea31012005-04-17 16:05:31 -05006463}
6464
James Smarte59058c2008-08-24 21:49:00 -04006465
6466/**
James Smart3621a712009-04-06 18:47:14 -04006467 * lpfc_mbox_timeout_handler - Worker thread function to handle mailbox timeout
James Smarte59058c2008-08-24 21:49:00 -04006468 * @phba: Pointer to HBA context object.
6469 *
6470 * This function is called from worker thread when a mailbox command times out.
6471 * The caller is not required to hold any locks. This function will reset the
6472 * HBA and recover all the pending commands.
6473 **/
dea31012005-04-17 16:05:31 -05006474void
6475lpfc_mbox_timeout_handler(struct lpfc_hba *phba)
6476{
James Smart2e0fef82007-06-17 19:56:36 -05006477 LPFC_MBOXQ_t *pmbox = phba->sli.mbox_active;
James Smart04c68492009-05-22 14:52:52 -04006478 MAILBOX_t *mb = &pmbox->u.mb;
James Smart1dcb58e2007-04-25 09:51:30 -04006479 struct lpfc_sli *psli = &phba->sli;
6480 struct lpfc_sli_ring *pring;
dea31012005-04-17 16:05:31 -05006481
James Smarta257bf92009-04-06 18:48:10 -04006482 /* Check the pmbox pointer first. There is a race condition
6483 * between the mbox timeout handler getting executed in the
6484 * worklist and the mailbox actually completing. When this
6485 * race condition occurs, the mbox_active will be NULL.
6486 */
6487 spin_lock_irq(&phba->hbalock);
6488 if (pmbox == NULL) {
6489 lpfc_printf_log(phba, KERN_WARNING,
6490 LOG_MBOX | LOG_SLI,
6491 "0353 Active Mailbox cleared - mailbox timeout "
6492 "exiting\n");
6493 spin_unlock_irq(&phba->hbalock);
6494 return;
6495 }
6496
dea31012005-04-17 16:05:31 -05006497 /* Mbox cmd <mbxCommand> timeout */
James Smarted957682007-06-17 19:56:37 -05006498 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04006499 "0310 Mailbox command x%x timeout Data: x%x x%x x%p\n",
James Smart92d7f7b2007-06-17 19:56:38 -05006500 mb->mbxCommand,
6501 phba->pport->port_state,
6502 phba->sli.sli_flag,
6503 phba->sli.mbox_active);
James Smarta257bf92009-04-06 18:48:10 -04006504 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05006505
James Smart1dcb58e2007-04-25 09:51:30 -04006506 /* Setting state unknown so lpfc_sli_abort_iocb_ring
6507 * would get IOCB_ERROR from lpfc_sli_issue_iocb, allowing
Lucas De Marchi25985ed2011-03-30 22:57:33 -03006508 * it to fail all outstanding SCSI IO.
James Smart1dcb58e2007-04-25 09:51:30 -04006509 */
James Smart2e0fef82007-06-17 19:56:36 -05006510 spin_lock_irq(&phba->pport->work_port_lock);
6511 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
6512 spin_unlock_irq(&phba->pport->work_port_lock);
6513 spin_lock_irq(&phba->hbalock);
6514 phba->link_state = LPFC_LINK_UNKNOWN;
James Smartf4b4c682009-05-22 14:53:12 -04006515 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05006516 spin_unlock_irq(&phba->hbalock);
James Smart1dcb58e2007-04-25 09:51:30 -04006517
6518 pring = &psli->ring[psli->fcp_ring];
6519 lpfc_sli_abort_iocb_ring(phba, pring);
6520
6521 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smart76bb24e2007-10-27 13:38:00 -04006522 "0345 Resetting board due to mailbox timeout\n");
James Smart3772a992009-05-22 14:50:54 -04006523
6524 /* Reset the HBA device */
6525 lpfc_reset_hba(phba);
dea31012005-04-17 16:05:31 -05006526}
6527
James Smarte59058c2008-08-24 21:49:00 -04006528/**
James Smart3772a992009-05-22 14:50:54 -04006529 * lpfc_sli_issue_mbox_s3 - Issue an SLI3 mailbox command to firmware
James Smarte59058c2008-08-24 21:49:00 -04006530 * @phba: Pointer to HBA context object.
6531 * @pmbox: Pointer to mailbox object.
6532 * @flag: Flag indicating how the mailbox need to be processed.
6533 *
6534 * This function is called by discovery code and HBA management code
James Smart3772a992009-05-22 14:50:54 -04006535 * to submit a mailbox command to firmware with SLI-3 interface spec. This
6536 * function gets the hbalock to protect the data structures.
James Smarte59058c2008-08-24 21:49:00 -04006537 * The mailbox command can be submitted in polling mode, in which case
6538 * this function will wait in a polling loop for the completion of the
6539 * mailbox.
6540 * If the mailbox is submitted in no_wait mode (not polling) the
6541 * function will submit the command and returns immediately without waiting
6542 * for the mailbox completion. The no_wait is supported only when HBA
6543 * is in SLI2/SLI3 mode - interrupts are enabled.
6544 * The SLI interface allows only one mailbox pending at a time. If the
6545 * mailbox is issued in polling mode and there is already a mailbox
6546 * pending, then the function will return an error. If the mailbox is issued
6547 * in NO_WAIT mode and there is a mailbox pending already, the function
6548 * will return MBX_BUSY after queuing the mailbox into mailbox queue.
6549 * The sli layer owns the mailbox object until the completion of mailbox
6550 * command if this function return MBX_BUSY or MBX_SUCCESS. For all other
6551 * return codes the caller owns the mailbox command after the return of
6552 * the function.
6553 **/
James Smart3772a992009-05-22 14:50:54 -04006554static int
6555lpfc_sli_issue_mbox_s3(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox,
6556 uint32_t flag)
dea31012005-04-17 16:05:31 -05006557{
dea31012005-04-17 16:05:31 -05006558 MAILBOX_t *mb;
James Smart2e0fef82007-06-17 19:56:36 -05006559 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05006560 uint32_t status, evtctr;
James Smart9940b972011-03-11 16:06:12 -05006561 uint32_t ha_copy, hc_copy;
dea31012005-04-17 16:05:31 -05006562 int i;
James Smart09372822008-01-11 01:52:54 -05006563 unsigned long timeout;
dea31012005-04-17 16:05:31 -05006564 unsigned long drvr_flag = 0;
James Smart34b02dc2008-08-24 21:49:55 -04006565 uint32_t word0, ldata;
dea31012005-04-17 16:05:31 -05006566 void __iomem *to_slim;
James Smart58da1ff2008-04-07 10:15:56 -04006567 int processing_queue = 0;
6568
6569 spin_lock_irqsave(&phba->hbalock, drvr_flag);
6570 if (!pmbox) {
James Smart8568a4d2009-07-19 10:01:16 -04006571 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart58da1ff2008-04-07 10:15:56 -04006572 /* processing mbox queue from intr_handler */
James Smart3772a992009-05-22 14:50:54 -04006573 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
6574 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
6575 return MBX_SUCCESS;
6576 }
James Smart58da1ff2008-04-07 10:15:56 -04006577 processing_queue = 1;
James Smart58da1ff2008-04-07 10:15:56 -04006578 pmbox = lpfc_mbox_get(phba);
6579 if (!pmbox) {
6580 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
6581 return MBX_SUCCESS;
6582 }
6583 }
dea31012005-04-17 16:05:31 -05006584
James Smarted957682007-06-17 19:56:37 -05006585 if (pmbox->mbox_cmpl && pmbox->mbox_cmpl != lpfc_sli_def_mbox_cmpl &&
James Smart92d7f7b2007-06-17 19:56:38 -05006586 pmbox->mbox_cmpl != lpfc_sli_wake_mbox_wait) {
James Smarted957682007-06-17 19:56:37 -05006587 if(!pmbox->vport) {
James Smart58da1ff2008-04-07 10:15:56 -04006588 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
James Smarted957682007-06-17 19:56:37 -05006589 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -05006590 LOG_MBOX | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04006591 "1806 Mbox x%x failed. No vport\n",
James Smart3772a992009-05-22 14:50:54 -04006592 pmbox->u.mb.mbxCommand);
James Smarted957682007-06-17 19:56:37 -05006593 dump_stack();
James Smart58da1ff2008-04-07 10:15:56 -04006594 goto out_not_finished;
James Smarted957682007-06-17 19:56:37 -05006595 }
6596 }
6597
Linas Vepstas8d63f372007-02-14 14:28:36 -06006598 /* If the PCI channel is in offline state, do not post mbox. */
James Smart58da1ff2008-04-07 10:15:56 -04006599 if (unlikely(pci_channel_offline(phba->pcidev))) {
6600 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
6601 goto out_not_finished;
6602 }
Linas Vepstas8d63f372007-02-14 14:28:36 -06006603
James Smarta257bf92009-04-06 18:48:10 -04006604 /* If HBA has a deferred error attention, fail the iocb. */
6605 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
6606 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
6607 goto out_not_finished;
6608 }
6609
dea31012005-04-17 16:05:31 -05006610 psli = &phba->sli;
James Smart92d7f7b2007-06-17 19:56:38 -05006611
James Smart3772a992009-05-22 14:50:54 -04006612 mb = &pmbox->u.mb;
dea31012005-04-17 16:05:31 -05006613 status = MBX_SUCCESS;
6614
James Smart2e0fef82007-06-17 19:56:36 -05006615 if (phba->link_state == LPFC_HBA_ERROR) {
6616 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05006617
6618 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04006619 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6620 "(%d):0311 Mailbox command x%x cannot "
6621 "issue Data: x%x x%x\n",
6622 pmbox->vport ? pmbox->vport->vpi : 0,
6623 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04006624 goto out_not_finished;
Jamie Wellnitz41415862006-02-28 19:25:27 -05006625 }
6626
James Smart9940b972011-03-11 16:06:12 -05006627 if (mb->mbxCommand != MBX_KILL_BOARD && flag & MBX_NOWAIT) {
6628 if (lpfc_readl(phba->HCregaddr, &hc_copy) ||
6629 !(hc_copy & HC_MBINT_ENA)) {
6630 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
6631 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smart3772a992009-05-22 14:50:54 -04006632 "(%d):2528 Mailbox command x%x cannot "
6633 "issue Data: x%x x%x\n",
6634 pmbox->vport ? pmbox->vport->vpi : 0,
6635 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
James Smart9940b972011-03-11 16:06:12 -05006636 goto out_not_finished;
6637 }
James Smart92908312006-03-07 15:04:13 -05006638 }
6639
dea31012005-04-17 16:05:31 -05006640 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
6641 /* Polling for a mbox command when another one is already active
6642 * is not allowed in SLI. Also, the driver must have established
6643 * SLI2 mode to queue and process multiple mbox commands.
6644 */
6645
6646 if (flag & MBX_POLL) {
James Smart2e0fef82007-06-17 19:56:36 -05006647 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05006648
6649 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04006650 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6651 "(%d):2529 Mailbox command x%x "
6652 "cannot issue Data: x%x x%x\n",
6653 pmbox->vport ? pmbox->vport->vpi : 0,
6654 pmbox->u.mb.mbxCommand,
6655 psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04006656 goto out_not_finished;
dea31012005-04-17 16:05:31 -05006657 }
6658
James Smart3772a992009-05-22 14:50:54 -04006659 if (!(psli->sli_flag & LPFC_SLI_ACTIVE)) {
James Smart2e0fef82007-06-17 19:56:36 -05006660 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05006661 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04006662 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6663 "(%d):2530 Mailbox command x%x "
6664 "cannot issue Data: x%x x%x\n",
6665 pmbox->vport ? pmbox->vport->vpi : 0,
6666 pmbox->u.mb.mbxCommand,
6667 psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04006668 goto out_not_finished;
dea31012005-04-17 16:05:31 -05006669 }
6670
dea31012005-04-17 16:05:31 -05006671 /* Another mailbox command is still being processed, queue this
6672 * command to be processed later.
6673 */
6674 lpfc_mbox_put(phba, pmbox);
6675
6676 /* Mbox cmd issue - BUSY */
James Smarted957682007-06-17 19:56:37 -05006677 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04006678 "(%d):0308 Mbox cmd issue - BUSY Data: "
James Smart92d7f7b2007-06-17 19:56:38 -05006679 "x%x x%x x%x x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -05006680 pmbox->vport ? pmbox->vport->vpi : 0xffffff,
6681 mb->mbxCommand, phba->pport->port_state,
6682 psli->sli_flag, flag);
dea31012005-04-17 16:05:31 -05006683
6684 psli->slistat.mbox_busy++;
James Smart2e0fef82007-06-17 19:56:36 -05006685 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05006686
James Smart858c9f62007-06-17 19:56:39 -05006687 if (pmbox->vport) {
6688 lpfc_debugfs_disc_trc(pmbox->vport,
6689 LPFC_DISC_TRC_MBOX_VPORT,
6690 "MBOX Bsy vport: cmd:x%x mb:x%x x%x",
6691 (uint32_t)mb->mbxCommand,
6692 mb->un.varWords[0], mb->un.varWords[1]);
6693 }
6694 else {
6695 lpfc_debugfs_disc_trc(phba->pport,
6696 LPFC_DISC_TRC_MBOX,
6697 "MBOX Bsy: cmd:x%x mb:x%x x%x",
6698 (uint32_t)mb->mbxCommand,
6699 mb->un.varWords[0], mb->un.varWords[1]);
6700 }
6701
James Smart2e0fef82007-06-17 19:56:36 -05006702 return MBX_BUSY;
dea31012005-04-17 16:05:31 -05006703 }
6704
dea31012005-04-17 16:05:31 -05006705 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
6706
6707 /* If we are not polling, we MUST be in SLI2 mode */
6708 if (flag != MBX_POLL) {
James Smart3772a992009-05-22 14:50:54 -04006709 if (!(psli->sli_flag & LPFC_SLI_ACTIVE) &&
Jamie Wellnitz41415862006-02-28 19:25:27 -05006710 (mb->mbxCommand != MBX_KILL_BOARD)) {
dea31012005-04-17 16:05:31 -05006711 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05006712 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05006713 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04006714 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6715 "(%d):2531 Mailbox command x%x "
6716 "cannot issue Data: x%x x%x\n",
6717 pmbox->vport ? pmbox->vport->vpi : 0,
6718 pmbox->u.mb.mbxCommand,
6719 psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04006720 goto out_not_finished;
dea31012005-04-17 16:05:31 -05006721 }
6722 /* timeout active mbox command */
James Smarta309a6b2006-08-01 07:33:43 -04006723 mod_timer(&psli->mbox_tmo, (jiffies +
James Smarta183a152011-10-10 21:32:43 -04006724 (HZ * lpfc_mbox_tmo_val(phba, pmbox))));
dea31012005-04-17 16:05:31 -05006725 }
6726
6727 /* Mailbox cmd <cmd> issue */
James Smarted957682007-06-17 19:56:37 -05006728 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04006729 "(%d):0309 Mailbox cmd x%x issue Data: x%x x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05006730 "x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04006731 pmbox->vport ? pmbox->vport->vpi : 0,
James Smart92d7f7b2007-06-17 19:56:38 -05006732 mb->mbxCommand, phba->pport->port_state,
6733 psli->sli_flag, flag);
dea31012005-04-17 16:05:31 -05006734
James Smart858c9f62007-06-17 19:56:39 -05006735 if (mb->mbxCommand != MBX_HEARTBEAT) {
6736 if (pmbox->vport) {
6737 lpfc_debugfs_disc_trc(pmbox->vport,
6738 LPFC_DISC_TRC_MBOX_VPORT,
6739 "MBOX Send vport: cmd:x%x mb:x%x x%x",
6740 (uint32_t)mb->mbxCommand,
6741 mb->un.varWords[0], mb->un.varWords[1]);
6742 }
6743 else {
6744 lpfc_debugfs_disc_trc(phba->pport,
6745 LPFC_DISC_TRC_MBOX,
6746 "MBOX Send: cmd:x%x mb:x%x x%x",
6747 (uint32_t)mb->mbxCommand,
6748 mb->un.varWords[0], mb->un.varWords[1]);
6749 }
6750 }
6751
dea31012005-04-17 16:05:31 -05006752 psli->slistat.mbox_cmd++;
6753 evtctr = psli->slistat.mbox_event;
6754
6755 /* next set own bit for the adapter and copy over command word */
6756 mb->mbxOwner = OWN_CHIP;
6757
James Smart3772a992009-05-22 14:50:54 -04006758 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
James Smart7a470272010-03-15 11:25:20 -04006759 /* Populate mbox extension offset word. */
6760 if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len) {
6761 *(((uint32_t *)mb) + pmbox->mbox_offset_word)
6762 = (uint8_t *)phba->mbox_ext
6763 - (uint8_t *)phba->mbox;
6764 }
6765
6766 /* Copy the mailbox extension data */
6767 if (pmbox->in_ext_byte_len && pmbox->context2) {
6768 lpfc_sli_pcimem_bcopy(pmbox->context2,
6769 (uint8_t *)phba->mbox_ext,
6770 pmbox->in_ext_byte_len);
6771 }
6772 /* Copy command data to host SLIM area */
James Smart34b02dc2008-08-24 21:49:55 -04006773 lpfc_sli_pcimem_bcopy(mb, phba->mbox, MAILBOX_CMD_SIZE);
dea31012005-04-17 16:05:31 -05006774 } else {
James Smart7a470272010-03-15 11:25:20 -04006775 /* Populate mbox extension offset word. */
6776 if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len)
6777 *(((uint32_t *)mb) + pmbox->mbox_offset_word)
6778 = MAILBOX_HBA_EXT_OFFSET;
6779
6780 /* Copy the mailbox extension data */
6781 if (pmbox->in_ext_byte_len && pmbox->context2) {
6782 lpfc_memcpy_to_slim(phba->MBslimaddr +
6783 MAILBOX_HBA_EXT_OFFSET,
6784 pmbox->context2, pmbox->in_ext_byte_len);
6785
6786 }
James Smart92908312006-03-07 15:04:13 -05006787 if (mb->mbxCommand == MBX_CONFIG_PORT) {
dea31012005-04-17 16:05:31 -05006788 /* copy command data into host mbox for cmpl */
James Smart34b02dc2008-08-24 21:49:55 -04006789 lpfc_sli_pcimem_bcopy(mb, phba->mbox, MAILBOX_CMD_SIZE);
dea31012005-04-17 16:05:31 -05006790 }
6791
6792 /* First copy mbox command data to HBA SLIM, skip past first
6793 word */
6794 to_slim = phba->MBslimaddr + sizeof (uint32_t);
6795 lpfc_memcpy_to_slim(to_slim, &mb->un.varWords[0],
6796 MAILBOX_CMD_SIZE - sizeof (uint32_t));
6797
6798 /* Next copy over first word, with mbxOwner set */
James Smart34b02dc2008-08-24 21:49:55 -04006799 ldata = *((uint32_t *)mb);
dea31012005-04-17 16:05:31 -05006800 to_slim = phba->MBslimaddr;
6801 writel(ldata, to_slim);
6802 readl(to_slim); /* flush */
6803
6804 if (mb->mbxCommand == MBX_CONFIG_PORT) {
6805 /* switch over to host mailbox */
James Smart3772a992009-05-22 14:50:54 -04006806 psli->sli_flag |= LPFC_SLI_ACTIVE;
dea31012005-04-17 16:05:31 -05006807 }
6808 }
6809
6810 wmb();
dea31012005-04-17 16:05:31 -05006811
6812 switch (flag) {
6813 case MBX_NOWAIT:
James Smart09372822008-01-11 01:52:54 -05006814 /* Set up reference to mailbox command */
dea31012005-04-17 16:05:31 -05006815 psli->mbox_active = pmbox;
James Smart09372822008-01-11 01:52:54 -05006816 /* Interrupt board to do it */
6817 writel(CA_MBATT, phba->CAregaddr);
6818 readl(phba->CAregaddr); /* flush */
6819 /* Don't wait for it to finish, just return */
dea31012005-04-17 16:05:31 -05006820 break;
6821
6822 case MBX_POLL:
James Smart09372822008-01-11 01:52:54 -05006823 /* Set up null reference to mailbox command */
dea31012005-04-17 16:05:31 -05006824 psli->mbox_active = NULL;
James Smart09372822008-01-11 01:52:54 -05006825 /* Interrupt board to do it */
6826 writel(CA_MBATT, phba->CAregaddr);
6827 readl(phba->CAregaddr); /* flush */
6828
James Smart3772a992009-05-22 14:50:54 -04006829 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05006830 /* First read mbox status word */
James Smart34b02dc2008-08-24 21:49:55 -04006831 word0 = *((uint32_t *)phba->mbox);
dea31012005-04-17 16:05:31 -05006832 word0 = le32_to_cpu(word0);
6833 } else {
6834 /* First read mbox status word */
James Smart9940b972011-03-11 16:06:12 -05006835 if (lpfc_readl(phba->MBslimaddr, &word0)) {
6836 spin_unlock_irqrestore(&phba->hbalock,
6837 drvr_flag);
6838 goto out_not_finished;
6839 }
dea31012005-04-17 16:05:31 -05006840 }
6841
6842 /* Read the HBA Host Attention Register */
James Smart9940b972011-03-11 16:06:12 -05006843 if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
6844 spin_unlock_irqrestore(&phba->hbalock,
6845 drvr_flag);
6846 goto out_not_finished;
6847 }
James Smarta183a152011-10-10 21:32:43 -04006848 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, pmbox) *
6849 1000) + jiffies;
James Smart09372822008-01-11 01:52:54 -05006850 i = 0;
dea31012005-04-17 16:05:31 -05006851 /* Wait for command to complete */
Jamie Wellnitz41415862006-02-28 19:25:27 -05006852 while (((word0 & OWN_CHIP) == OWN_CHIP) ||
6853 (!(ha_copy & HA_MBATT) &&
James Smart2e0fef82007-06-17 19:56:36 -05006854 (phba->link_state > LPFC_WARM_START))) {
James Smart09372822008-01-11 01:52:54 -05006855 if (time_after(jiffies, timeout)) {
dea31012005-04-17 16:05:31 -05006856 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05006857 spin_unlock_irqrestore(&phba->hbalock,
dea31012005-04-17 16:05:31 -05006858 drvr_flag);
James Smart58da1ff2008-04-07 10:15:56 -04006859 goto out_not_finished;
dea31012005-04-17 16:05:31 -05006860 }
6861
6862 /* Check if we took a mbox interrupt while we were
6863 polling */
6864 if (((word0 & OWN_CHIP) != OWN_CHIP)
6865 && (evtctr != psli->slistat.mbox_event))
6866 break;
6867
James Smart09372822008-01-11 01:52:54 -05006868 if (i++ > 10) {
6869 spin_unlock_irqrestore(&phba->hbalock,
6870 drvr_flag);
6871 msleep(1);
6872 spin_lock_irqsave(&phba->hbalock, drvr_flag);
6873 }
dea31012005-04-17 16:05:31 -05006874
James Smart3772a992009-05-22 14:50:54 -04006875 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05006876 /* First copy command data */
James Smart34b02dc2008-08-24 21:49:55 -04006877 word0 = *((uint32_t *)phba->mbox);
dea31012005-04-17 16:05:31 -05006878 word0 = le32_to_cpu(word0);
6879 if (mb->mbxCommand == MBX_CONFIG_PORT) {
6880 MAILBOX_t *slimmb;
James Smart34b02dc2008-08-24 21:49:55 -04006881 uint32_t slimword0;
dea31012005-04-17 16:05:31 -05006882 /* Check real SLIM for any errors */
6883 slimword0 = readl(phba->MBslimaddr);
6884 slimmb = (MAILBOX_t *) & slimword0;
6885 if (((slimword0 & OWN_CHIP) != OWN_CHIP)
6886 && slimmb->mbxStatus) {
6887 psli->sli_flag &=
James Smart3772a992009-05-22 14:50:54 -04006888 ~LPFC_SLI_ACTIVE;
dea31012005-04-17 16:05:31 -05006889 word0 = slimword0;
6890 }
6891 }
6892 } else {
6893 /* First copy command data */
6894 word0 = readl(phba->MBslimaddr);
6895 }
6896 /* Read the HBA Host Attention Register */
James Smart9940b972011-03-11 16:06:12 -05006897 if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
6898 spin_unlock_irqrestore(&phba->hbalock,
6899 drvr_flag);
6900 goto out_not_finished;
6901 }
dea31012005-04-17 16:05:31 -05006902 }
6903
James Smart3772a992009-05-22 14:50:54 -04006904 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05006905 /* copy results back to user */
James Smart34b02dc2008-08-24 21:49:55 -04006906 lpfc_sli_pcimem_bcopy(phba->mbox, mb, MAILBOX_CMD_SIZE);
James Smart7a470272010-03-15 11:25:20 -04006907 /* Copy the mailbox extension data */
6908 if (pmbox->out_ext_byte_len && pmbox->context2) {
6909 lpfc_sli_pcimem_bcopy(phba->mbox_ext,
6910 pmbox->context2,
6911 pmbox->out_ext_byte_len);
6912 }
dea31012005-04-17 16:05:31 -05006913 } else {
6914 /* First copy command data */
6915 lpfc_memcpy_from_slim(mb, phba->MBslimaddr,
6916 MAILBOX_CMD_SIZE);
James Smart7a470272010-03-15 11:25:20 -04006917 /* Copy the mailbox extension data */
6918 if (pmbox->out_ext_byte_len && pmbox->context2) {
6919 lpfc_memcpy_from_slim(pmbox->context2,
6920 phba->MBslimaddr +
6921 MAILBOX_HBA_EXT_OFFSET,
6922 pmbox->out_ext_byte_len);
dea31012005-04-17 16:05:31 -05006923 }
6924 }
6925
6926 writel(HA_MBATT, phba->HAregaddr);
6927 readl(phba->HAregaddr); /* flush */
6928
6929 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
6930 status = mb->mbxStatus;
6931 }
6932
James Smart2e0fef82007-06-17 19:56:36 -05006933 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
6934 return status;
James Smart58da1ff2008-04-07 10:15:56 -04006935
6936out_not_finished:
6937 if (processing_queue) {
James Smartda0436e2009-05-22 14:51:39 -04006938 pmbox->u.mb.mbxStatus = MBX_NOT_FINISHED;
James Smart58da1ff2008-04-07 10:15:56 -04006939 lpfc_mbox_cmpl_put(phba, pmbox);
6940 }
6941 return MBX_NOT_FINISHED;
dea31012005-04-17 16:05:31 -05006942}
6943
James Smarte59058c2008-08-24 21:49:00 -04006944/**
James Smartf1126682009-06-10 17:22:44 -04006945 * lpfc_sli4_async_mbox_block - Block posting SLI4 asynchronous mailbox command
6946 * @phba: Pointer to HBA context object.
6947 *
6948 * The function blocks the posting of SLI4 asynchronous mailbox commands from
6949 * the driver internal pending mailbox queue. It will then try to wait out the
6950 * possible outstanding mailbox command before return.
6951 *
6952 * Returns:
6953 * 0 - the outstanding mailbox command completed; otherwise, the wait for
6954 * the outstanding mailbox command timed out.
6955 **/
6956static int
6957lpfc_sli4_async_mbox_block(struct lpfc_hba *phba)
6958{
6959 struct lpfc_sli *psli = &phba->sli;
James Smartf1126682009-06-10 17:22:44 -04006960 int rc = 0;
James Smarta183a152011-10-10 21:32:43 -04006961 unsigned long timeout = 0;
James Smartf1126682009-06-10 17:22:44 -04006962
6963 /* Mark the asynchronous mailbox command posting as blocked */
6964 spin_lock_irq(&phba->hbalock);
6965 psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
James Smartf1126682009-06-10 17:22:44 -04006966 /* Determine how long we might wait for the active mailbox
6967 * command to be gracefully completed by firmware.
6968 */
James Smarta183a152011-10-10 21:32:43 -04006969 if (phba->sli.mbox_active)
6970 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
6971 phba->sli.mbox_active) *
6972 1000) + jiffies;
6973 spin_unlock_irq(&phba->hbalock);
6974
James Smartf1126682009-06-10 17:22:44 -04006975 /* Wait for the outstnading mailbox command to complete */
6976 while (phba->sli.mbox_active) {
6977 /* Check active mailbox complete status every 2ms */
6978 msleep(2);
6979 if (time_after(jiffies, timeout)) {
6980 /* Timeout, marked the outstanding cmd not complete */
6981 rc = 1;
6982 break;
6983 }
6984 }
6985
6986 /* Can not cleanly block async mailbox command, fails it */
6987 if (rc) {
6988 spin_lock_irq(&phba->hbalock);
6989 psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
6990 spin_unlock_irq(&phba->hbalock);
6991 }
6992 return rc;
6993}
6994
6995/**
6996 * lpfc_sli4_async_mbox_unblock - Block posting SLI4 async mailbox command
6997 * @phba: Pointer to HBA context object.
6998 *
6999 * The function unblocks and resume posting of SLI4 asynchronous mailbox
7000 * commands from the driver internal pending mailbox queue. It makes sure
7001 * that there is no outstanding mailbox command before resuming posting
7002 * asynchronous mailbox commands. If, for any reason, there is outstanding
7003 * mailbox command, it will try to wait it out before resuming asynchronous
7004 * mailbox command posting.
7005 **/
7006static void
7007lpfc_sli4_async_mbox_unblock(struct lpfc_hba *phba)
7008{
7009 struct lpfc_sli *psli = &phba->sli;
7010
7011 spin_lock_irq(&phba->hbalock);
7012 if (!(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
7013 /* Asynchronous mailbox posting is not blocked, do nothing */
7014 spin_unlock_irq(&phba->hbalock);
7015 return;
7016 }
7017
7018 /* Outstanding synchronous mailbox command is guaranteed to be done,
7019 * successful or timeout, after timing-out the outstanding mailbox
7020 * command shall always be removed, so just unblock posting async
7021 * mailbox command and resume
7022 */
7023 psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
7024 spin_unlock_irq(&phba->hbalock);
7025
7026 /* wake up worker thread to post asynchronlous mailbox command */
7027 lpfc_worker_wake_up(phba);
7028}
7029
7030/**
James Smartda0436e2009-05-22 14:51:39 -04007031 * lpfc_sli4_post_sync_mbox - Post an SLI4 mailbox to the bootstrap mailbox
7032 * @phba: Pointer to HBA context object.
7033 * @mboxq: Pointer to mailbox object.
7034 *
7035 * The function posts a mailbox to the port. The mailbox is expected
7036 * to be comletely filled in and ready for the port to operate on it.
7037 * This routine executes a synchronous completion operation on the
7038 * mailbox by polling for its completion.
7039 *
7040 * The caller must not be holding any locks when calling this routine.
7041 *
7042 * Returns:
7043 * MBX_SUCCESS - mailbox posted successfully
7044 * Any of the MBX error values.
7045 **/
7046static int
7047lpfc_sli4_post_sync_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
7048{
7049 int rc = MBX_SUCCESS;
7050 unsigned long iflag;
7051 uint32_t db_ready;
7052 uint32_t mcqe_status;
7053 uint32_t mbx_cmnd;
7054 unsigned long timeout;
7055 struct lpfc_sli *psli = &phba->sli;
7056 struct lpfc_mqe *mb = &mboxq->u.mqe;
7057 struct lpfc_bmbx_create *mbox_rgn;
7058 struct dma_address *dma_address;
7059 struct lpfc_register bmbx_reg;
7060
7061 /*
7062 * Only one mailbox can be active to the bootstrap mailbox region
7063 * at a time and there is no queueing provided.
7064 */
7065 spin_lock_irqsave(&phba->hbalock, iflag);
7066 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
7067 spin_unlock_irqrestore(&phba->hbalock, iflag);
7068 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04007069 "(%d):2532 Mailbox command x%x (x%x/x%x) "
James Smartda0436e2009-05-22 14:51:39 -04007070 "cannot issue Data: x%x x%x\n",
7071 mboxq->vport ? mboxq->vport->vpi : 0,
7072 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04007073 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7074 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04007075 psli->sli_flag, MBX_POLL);
7076 return MBXERR_ERROR;
7077 }
7078 /* The server grabs the token and owns it until release */
7079 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
7080 phba->sli.mbox_active = mboxq;
7081 spin_unlock_irqrestore(&phba->hbalock, iflag);
7082
7083 /*
7084 * Initialize the bootstrap memory region to avoid stale data areas
7085 * in the mailbox post. Then copy the caller's mailbox contents to
7086 * the bmbx mailbox region.
7087 */
7088 mbx_cmnd = bf_get(lpfc_mqe_command, mb);
7089 memset(phba->sli4_hba.bmbx.avirt, 0, sizeof(struct lpfc_bmbx_create));
7090 lpfc_sli_pcimem_bcopy(mb, phba->sli4_hba.bmbx.avirt,
7091 sizeof(struct lpfc_mqe));
7092
7093 /* Post the high mailbox dma address to the port and wait for ready. */
7094 dma_address = &phba->sli4_hba.bmbx.dma_address;
7095 writel(dma_address->addr_hi, phba->sli4_hba.BMBXregaddr);
7096
James Smarta183a152011-10-10 21:32:43 -04007097 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mboxq)
James Smartda0436e2009-05-22 14:51:39 -04007098 * 1000) + jiffies;
7099 do {
7100 bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
7101 db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
7102 if (!db_ready)
7103 msleep(2);
7104
7105 if (time_after(jiffies, timeout)) {
7106 rc = MBXERR_ERROR;
7107 goto exit;
7108 }
7109 } while (!db_ready);
7110
7111 /* Post the low mailbox dma address to the port. */
7112 writel(dma_address->addr_lo, phba->sli4_hba.BMBXregaddr);
James Smarta183a152011-10-10 21:32:43 -04007113 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mboxq)
James Smartda0436e2009-05-22 14:51:39 -04007114 * 1000) + jiffies;
7115 do {
7116 bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
7117 db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
7118 if (!db_ready)
7119 msleep(2);
7120
7121 if (time_after(jiffies, timeout)) {
7122 rc = MBXERR_ERROR;
7123 goto exit;
7124 }
7125 } while (!db_ready);
7126
7127 /*
7128 * Read the CQ to ensure the mailbox has completed.
7129 * If so, update the mailbox status so that the upper layers
7130 * can complete the request normally.
7131 */
7132 lpfc_sli_pcimem_bcopy(phba->sli4_hba.bmbx.avirt, mb,
7133 sizeof(struct lpfc_mqe));
7134 mbox_rgn = (struct lpfc_bmbx_create *) phba->sli4_hba.bmbx.avirt;
7135 lpfc_sli_pcimem_bcopy(&mbox_rgn->mcqe, &mboxq->mcqe,
7136 sizeof(struct lpfc_mcqe));
7137 mcqe_status = bf_get(lpfc_mcqe_status, &mbox_rgn->mcqe);
James Smart05580562011-05-24 11:40:48 -04007138 /*
7139 * When the CQE status indicates a failure and the mailbox status
7140 * indicates success then copy the CQE status into the mailbox status
7141 * (and prefix it with x4000).
7142 */
James Smartda0436e2009-05-22 14:51:39 -04007143 if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
James Smart05580562011-05-24 11:40:48 -04007144 if (bf_get(lpfc_mqe_status, mb) == MBX_SUCCESS)
7145 bf_set(lpfc_mqe_status, mb,
7146 (LPFC_MBX_ERROR_RANGE | mcqe_status));
James Smartda0436e2009-05-22 14:51:39 -04007147 rc = MBXERR_ERROR;
James Smartd7c47992010-06-08 18:31:54 -04007148 } else
7149 lpfc_sli4_swap_str(phba, mboxq);
James Smartda0436e2009-05-22 14:51:39 -04007150
7151 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04007152 "(%d):0356 Mailbox cmd x%x (x%x/x%x) Status x%x "
James Smartda0436e2009-05-22 14:51:39 -04007153 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x"
7154 " x%x x%x CQ: x%x x%x x%x x%x\n",
James Smarta183a152011-10-10 21:32:43 -04007155 mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
7156 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7157 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04007158 bf_get(lpfc_mqe_status, mb),
7159 mb->un.mb_words[0], mb->un.mb_words[1],
7160 mb->un.mb_words[2], mb->un.mb_words[3],
7161 mb->un.mb_words[4], mb->un.mb_words[5],
7162 mb->un.mb_words[6], mb->un.mb_words[7],
7163 mb->un.mb_words[8], mb->un.mb_words[9],
7164 mb->un.mb_words[10], mb->un.mb_words[11],
7165 mb->un.mb_words[12], mboxq->mcqe.word0,
7166 mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
7167 mboxq->mcqe.trailer);
7168exit:
7169 /* We are holding the token, no needed for lock when release */
7170 spin_lock_irqsave(&phba->hbalock, iflag);
7171 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
7172 phba->sli.mbox_active = NULL;
7173 spin_unlock_irqrestore(&phba->hbalock, iflag);
7174 return rc;
7175}
7176
7177/**
7178 * lpfc_sli_issue_mbox_s4 - Issue an SLI4 mailbox command to firmware
7179 * @phba: Pointer to HBA context object.
7180 * @pmbox: Pointer to mailbox object.
7181 * @flag: Flag indicating how the mailbox need to be processed.
7182 *
7183 * This function is called by discovery code and HBA management code to submit
7184 * a mailbox command to firmware with SLI-4 interface spec.
7185 *
7186 * Return codes the caller owns the mailbox command after the return of the
7187 * function.
7188 **/
7189static int
7190lpfc_sli_issue_mbox_s4(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
7191 uint32_t flag)
7192{
7193 struct lpfc_sli *psli = &phba->sli;
7194 unsigned long iflags;
7195 int rc;
7196
James Smartb76f2dc2011-07-22 18:37:42 -04007197 /* dump from issue mailbox command if setup */
7198 lpfc_idiag_mbxacc_dump_issue_mbox(phba, &mboxq->u.mb);
7199
James Smart8fa38512009-07-19 10:01:03 -04007200 rc = lpfc_mbox_dev_check(phba);
7201 if (unlikely(rc)) {
7202 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04007203 "(%d):2544 Mailbox command x%x (x%x/x%x) "
James Smart8fa38512009-07-19 10:01:03 -04007204 "cannot issue Data: x%x x%x\n",
7205 mboxq->vport ? mboxq->vport->vpi : 0,
7206 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04007207 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7208 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smart8fa38512009-07-19 10:01:03 -04007209 psli->sli_flag, flag);
7210 goto out_not_finished;
7211 }
7212
James Smartda0436e2009-05-22 14:51:39 -04007213 /* Detect polling mode and jump to a handler */
7214 if (!phba->sli4_hba.intr_enable) {
7215 if (flag == MBX_POLL)
7216 rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
7217 else
7218 rc = -EIO;
7219 if (rc != MBX_SUCCESS)
James Smart05580562011-05-24 11:40:48 -04007220 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
James Smartda0436e2009-05-22 14:51:39 -04007221 "(%d):2541 Mailbox command x%x "
James Smartcc459f12012-05-09 21:18:30 -04007222 "(x%x/x%x) failure: "
7223 "mqe_sta: x%x mcqe_sta: x%x/x%x "
7224 "Data: x%x x%x\n,",
James Smartda0436e2009-05-22 14:51:39 -04007225 mboxq->vport ? mboxq->vport->vpi : 0,
7226 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04007227 lpfc_sli_config_mbox_subsys_get(phba,
7228 mboxq),
7229 lpfc_sli_config_mbox_opcode_get(phba,
7230 mboxq),
James Smartcc459f12012-05-09 21:18:30 -04007231 bf_get(lpfc_mqe_status, &mboxq->u.mqe),
7232 bf_get(lpfc_mcqe_status, &mboxq->mcqe),
7233 bf_get(lpfc_mcqe_ext_status,
7234 &mboxq->mcqe),
James Smartda0436e2009-05-22 14:51:39 -04007235 psli->sli_flag, flag);
7236 return rc;
7237 } else if (flag == MBX_POLL) {
James Smartf1126682009-06-10 17:22:44 -04007238 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
7239 "(%d):2542 Try to issue mailbox command "
James Smarta183a152011-10-10 21:32:43 -04007240 "x%x (x%x/x%x) synchronously ahead of async"
James Smartf1126682009-06-10 17:22:44 -04007241 "mailbox command queue: x%x x%x\n",
James Smartda0436e2009-05-22 14:51:39 -04007242 mboxq->vport ? mboxq->vport->vpi : 0,
7243 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04007244 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7245 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04007246 psli->sli_flag, flag);
James Smartf1126682009-06-10 17:22:44 -04007247 /* Try to block the asynchronous mailbox posting */
7248 rc = lpfc_sli4_async_mbox_block(phba);
7249 if (!rc) {
7250 /* Successfully blocked, now issue sync mbox cmd */
7251 rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
7252 if (rc != MBX_SUCCESS)
James Smartcc459f12012-05-09 21:18:30 -04007253 lpfc_printf_log(phba, KERN_WARNING,
James Smarta183a152011-10-10 21:32:43 -04007254 LOG_MBOX | LOG_SLI,
James Smartcc459f12012-05-09 21:18:30 -04007255 "(%d):2597 Sync Mailbox command "
7256 "x%x (x%x/x%x) failure: "
7257 "mqe_sta: x%x mcqe_sta: x%x/x%x "
7258 "Data: x%x x%x\n,",
7259 mboxq->vport ? mboxq->vport->vpi : 0,
James Smarta183a152011-10-10 21:32:43 -04007260 mboxq->u.mb.mbxCommand,
7261 lpfc_sli_config_mbox_subsys_get(phba,
7262 mboxq),
7263 lpfc_sli_config_mbox_opcode_get(phba,
7264 mboxq),
James Smartcc459f12012-05-09 21:18:30 -04007265 bf_get(lpfc_mqe_status, &mboxq->u.mqe),
7266 bf_get(lpfc_mcqe_status, &mboxq->mcqe),
7267 bf_get(lpfc_mcqe_ext_status,
7268 &mboxq->mcqe),
James Smarta183a152011-10-10 21:32:43 -04007269 psli->sli_flag, flag);
James Smartf1126682009-06-10 17:22:44 -04007270 /* Unblock the async mailbox posting afterward */
7271 lpfc_sli4_async_mbox_unblock(phba);
7272 }
7273 return rc;
James Smartda0436e2009-05-22 14:51:39 -04007274 }
7275
7276 /* Now, interrupt mode asynchrous mailbox command */
7277 rc = lpfc_mbox_cmd_check(phba, mboxq);
7278 if (rc) {
7279 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04007280 "(%d):2543 Mailbox command x%x (x%x/x%x) "
James Smartda0436e2009-05-22 14:51:39 -04007281 "cannot issue Data: x%x x%x\n",
7282 mboxq->vport ? mboxq->vport->vpi : 0,
7283 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04007284 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7285 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04007286 psli->sli_flag, flag);
7287 goto out_not_finished;
7288 }
James Smartda0436e2009-05-22 14:51:39 -04007289
7290 /* Put the mailbox command to the driver internal FIFO */
7291 psli->slistat.mbox_busy++;
7292 spin_lock_irqsave(&phba->hbalock, iflags);
7293 lpfc_mbox_put(phba, mboxq);
7294 spin_unlock_irqrestore(&phba->hbalock, iflags);
7295 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
7296 "(%d):0354 Mbox cmd issue - Enqueue Data: "
James Smarta183a152011-10-10 21:32:43 -04007297 "x%x (x%x/x%x) x%x x%x x%x\n",
James Smartda0436e2009-05-22 14:51:39 -04007298 mboxq->vport ? mboxq->vport->vpi : 0xffffff,
7299 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
James Smarta183a152011-10-10 21:32:43 -04007300 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7301 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04007302 phba->pport->port_state,
7303 psli->sli_flag, MBX_NOWAIT);
7304 /* Wake up worker thread to transport mailbox command from head */
7305 lpfc_worker_wake_up(phba);
7306
7307 return MBX_BUSY;
7308
7309out_not_finished:
7310 return MBX_NOT_FINISHED;
7311}
7312
7313/**
7314 * lpfc_sli4_post_async_mbox - Post an SLI4 mailbox command to device
7315 * @phba: Pointer to HBA context object.
7316 *
7317 * This function is called by worker thread to send a mailbox command to
7318 * SLI4 HBA firmware.
7319 *
7320 **/
7321int
7322lpfc_sli4_post_async_mbox(struct lpfc_hba *phba)
7323{
7324 struct lpfc_sli *psli = &phba->sli;
7325 LPFC_MBOXQ_t *mboxq;
7326 int rc = MBX_SUCCESS;
7327 unsigned long iflags;
7328 struct lpfc_mqe *mqe;
7329 uint32_t mbx_cmnd;
7330
7331 /* Check interrupt mode before post async mailbox command */
7332 if (unlikely(!phba->sli4_hba.intr_enable))
7333 return MBX_NOT_FINISHED;
7334
7335 /* Check for mailbox command service token */
7336 spin_lock_irqsave(&phba->hbalock, iflags);
7337 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
7338 spin_unlock_irqrestore(&phba->hbalock, iflags);
7339 return MBX_NOT_FINISHED;
7340 }
7341 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
7342 spin_unlock_irqrestore(&phba->hbalock, iflags);
7343 return MBX_NOT_FINISHED;
7344 }
7345 if (unlikely(phba->sli.mbox_active)) {
7346 spin_unlock_irqrestore(&phba->hbalock, iflags);
7347 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7348 "0384 There is pending active mailbox cmd\n");
7349 return MBX_NOT_FINISHED;
7350 }
7351 /* Take the mailbox command service token */
7352 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
7353
7354 /* Get the next mailbox command from head of queue */
7355 mboxq = lpfc_mbox_get(phba);
7356
7357 /* If no more mailbox command waiting for post, we're done */
7358 if (!mboxq) {
7359 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
7360 spin_unlock_irqrestore(&phba->hbalock, iflags);
7361 return MBX_SUCCESS;
7362 }
7363 phba->sli.mbox_active = mboxq;
7364 spin_unlock_irqrestore(&phba->hbalock, iflags);
7365
7366 /* Check device readiness for posting mailbox command */
7367 rc = lpfc_mbox_dev_check(phba);
7368 if (unlikely(rc))
7369 /* Driver clean routine will clean up pending mailbox */
7370 goto out_not_finished;
7371
7372 /* Prepare the mbox command to be posted */
7373 mqe = &mboxq->u.mqe;
7374 mbx_cmnd = bf_get(lpfc_mqe_command, mqe);
7375
7376 /* Start timer for the mbox_tmo and log some mailbox post messages */
7377 mod_timer(&psli->mbox_tmo, (jiffies +
James Smarta183a152011-10-10 21:32:43 -04007378 (HZ * lpfc_mbox_tmo_val(phba, mboxq))));
James Smartda0436e2009-05-22 14:51:39 -04007379
7380 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04007381 "(%d):0355 Mailbox cmd x%x (x%x/x%x) issue Data: "
James Smartda0436e2009-05-22 14:51:39 -04007382 "x%x x%x\n",
7383 mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
James Smarta183a152011-10-10 21:32:43 -04007384 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7385 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04007386 phba->pport->port_state, psli->sli_flag);
7387
7388 if (mbx_cmnd != MBX_HEARTBEAT) {
7389 if (mboxq->vport) {
7390 lpfc_debugfs_disc_trc(mboxq->vport,
7391 LPFC_DISC_TRC_MBOX_VPORT,
7392 "MBOX Send vport: cmd:x%x mb:x%x x%x",
7393 mbx_cmnd, mqe->un.mb_words[0],
7394 mqe->un.mb_words[1]);
7395 } else {
7396 lpfc_debugfs_disc_trc(phba->pport,
7397 LPFC_DISC_TRC_MBOX,
7398 "MBOX Send: cmd:x%x mb:x%x x%x",
7399 mbx_cmnd, mqe->un.mb_words[0],
7400 mqe->un.mb_words[1]);
7401 }
7402 }
7403 psli->slistat.mbox_cmd++;
7404
7405 /* Post the mailbox command to the port */
7406 rc = lpfc_sli4_mq_put(phba->sli4_hba.mbx_wq, mqe);
7407 if (rc != MBX_SUCCESS) {
7408 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04007409 "(%d):2533 Mailbox command x%x (x%x/x%x) "
James Smartda0436e2009-05-22 14:51:39 -04007410 "cannot issue Data: x%x x%x\n",
7411 mboxq->vport ? mboxq->vport->vpi : 0,
7412 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04007413 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7414 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04007415 psli->sli_flag, MBX_NOWAIT);
7416 goto out_not_finished;
7417 }
7418
7419 return rc;
7420
7421out_not_finished:
7422 spin_lock_irqsave(&phba->hbalock, iflags);
James Smartd7069f02012-03-01 22:36:29 -05007423 if (phba->sli.mbox_active) {
7424 mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
7425 __lpfc_mbox_cmpl_put(phba, mboxq);
7426 /* Release the token */
7427 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
7428 phba->sli.mbox_active = NULL;
7429 }
James Smartda0436e2009-05-22 14:51:39 -04007430 spin_unlock_irqrestore(&phba->hbalock, iflags);
7431
7432 return MBX_NOT_FINISHED;
7433}
7434
7435/**
7436 * lpfc_sli_issue_mbox - Wrapper func for issuing mailbox command
7437 * @phba: Pointer to HBA context object.
7438 * @pmbox: Pointer to mailbox object.
7439 * @flag: Flag indicating how the mailbox need to be processed.
7440 *
7441 * This routine wraps the actual SLI3 or SLI4 mailbox issuing routine from
7442 * the API jump table function pointer from the lpfc_hba struct.
7443 *
7444 * Return codes the caller owns the mailbox command after the return of the
7445 * function.
7446 **/
7447int
7448lpfc_sli_issue_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox, uint32_t flag)
7449{
7450 return phba->lpfc_sli_issue_mbox(phba, pmbox, flag);
7451}
7452
7453/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03007454 * lpfc_mbox_api_table_setup - Set up mbox api function jump table
James Smartda0436e2009-05-22 14:51:39 -04007455 * @phba: The hba struct for which this call is being executed.
7456 * @dev_grp: The HBA PCI-Device group number.
7457 *
7458 * This routine sets up the mbox interface API function jump table in @phba
7459 * struct.
7460 * Returns: 0 - success, -ENODEV - failure.
7461 **/
7462int
7463lpfc_mbox_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
7464{
7465
7466 switch (dev_grp) {
7467 case LPFC_PCI_DEV_LP:
7468 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s3;
7469 phba->lpfc_sli_handle_slow_ring_event =
7470 lpfc_sli_handle_slow_ring_event_s3;
7471 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s3;
7472 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s3;
7473 phba->lpfc_sli_brdready = lpfc_sli_brdready_s3;
7474 break;
7475 case LPFC_PCI_DEV_OC:
7476 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s4;
7477 phba->lpfc_sli_handle_slow_ring_event =
7478 lpfc_sli_handle_slow_ring_event_s4;
7479 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s4;
7480 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s4;
7481 phba->lpfc_sli_brdready = lpfc_sli_brdready_s4;
7482 break;
7483 default:
7484 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7485 "1420 Invalid HBA PCI-device group: 0x%x\n",
7486 dev_grp);
7487 return -ENODEV;
7488 break;
7489 }
7490 return 0;
7491}
7492
7493/**
James Smart3621a712009-04-06 18:47:14 -04007494 * __lpfc_sli_ringtx_put - Add an iocb to the txq
James Smarte59058c2008-08-24 21:49:00 -04007495 * @phba: Pointer to HBA context object.
7496 * @pring: Pointer to driver SLI ring object.
7497 * @piocb: Pointer to address of newly added command iocb.
7498 *
7499 * This function is called with hbalock held to add a command
7500 * iocb to the txq when SLI layer cannot submit the command iocb
7501 * to the ring.
7502 **/
James Smart2a9bf3d2010-06-07 15:24:45 -04007503void
James Smart92d7f7b2007-06-17 19:56:38 -05007504__lpfc_sli_ringtx_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -05007505 struct lpfc_iocbq *piocb)
dea31012005-04-17 16:05:31 -05007506{
7507 /* Insert the caller's iocb in the txq tail for later processing. */
7508 list_add_tail(&piocb->list, &pring->txq);
7509 pring->txq_cnt++;
dea31012005-04-17 16:05:31 -05007510}
7511
James Smarte59058c2008-08-24 21:49:00 -04007512/**
James Smart3621a712009-04-06 18:47:14 -04007513 * lpfc_sli_next_iocb - Get the next iocb in the txq
James Smarte59058c2008-08-24 21:49:00 -04007514 * @phba: Pointer to HBA context object.
7515 * @pring: Pointer to driver SLI ring object.
7516 * @piocb: Pointer to address of newly added command iocb.
7517 *
7518 * This function is called with hbalock held before a new
7519 * iocb is submitted to the firmware. This function checks
7520 * txq to flush the iocbs in txq to Firmware before
7521 * submitting new iocbs to the Firmware.
7522 * If there are iocbs in the txq which need to be submitted
7523 * to firmware, lpfc_sli_next_iocb returns the first element
7524 * of the txq after dequeuing it from txq.
7525 * If there is no iocb in the txq then the function will return
7526 * *piocb and *piocb is set to NULL. Caller needs to check
7527 * *piocb to find if there are more commands in the txq.
7528 **/
dea31012005-04-17 16:05:31 -05007529static struct lpfc_iocbq *
7530lpfc_sli_next_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -05007531 struct lpfc_iocbq **piocb)
dea31012005-04-17 16:05:31 -05007532{
7533 struct lpfc_iocbq * nextiocb;
7534
7535 nextiocb = lpfc_sli_ringtx_get(phba, pring);
7536 if (!nextiocb) {
7537 nextiocb = *piocb;
7538 *piocb = NULL;
7539 }
7540
7541 return nextiocb;
7542}
7543
James Smarte59058c2008-08-24 21:49:00 -04007544/**
James Smart3772a992009-05-22 14:50:54 -04007545 * __lpfc_sli_issue_iocb_s3 - SLI3 device lockless ver of lpfc_sli_issue_iocb
James Smarte59058c2008-08-24 21:49:00 -04007546 * @phba: Pointer to HBA context object.
James Smart3772a992009-05-22 14:50:54 -04007547 * @ring_number: SLI ring number to issue iocb on.
James Smarte59058c2008-08-24 21:49:00 -04007548 * @piocb: Pointer to command iocb.
7549 * @flag: Flag indicating if this command can be put into txq.
7550 *
James Smart3772a992009-05-22 14:50:54 -04007551 * __lpfc_sli_issue_iocb_s3 is used by other functions in the driver to issue
7552 * an iocb command to an HBA with SLI-3 interface spec. If the PCI slot is
7553 * recovering from error state, if HBA is resetting or if LPFC_STOP_IOCB_EVENT
7554 * flag is turned on, the function returns IOCB_ERROR. When the link is down,
7555 * this function allows only iocbs for posting buffers. This function finds
7556 * next available slot in the command ring and posts the command to the
7557 * available slot and writes the port attention register to request HBA start
7558 * processing new iocb. If there is no slot available in the ring and
7559 * flag & SLI_IOCB_RET_IOCB is set, the new iocb is added to the txq, otherwise
7560 * the function returns IOCB_BUSY.
James Smarte59058c2008-08-24 21:49:00 -04007561 *
James Smart3772a992009-05-22 14:50:54 -04007562 * This function is called with hbalock held. The function will return success
7563 * after it successfully submit the iocb to firmware or after adding to the
7564 * txq.
James Smarte59058c2008-08-24 21:49:00 -04007565 **/
James Smart98c9ea52007-10-27 13:37:33 -04007566static int
James Smart3772a992009-05-22 14:50:54 -04007567__lpfc_sli_issue_iocb_s3(struct lpfc_hba *phba, uint32_t ring_number,
dea31012005-04-17 16:05:31 -05007568 struct lpfc_iocbq *piocb, uint32_t flag)
7569{
7570 struct lpfc_iocbq *nextiocb;
7571 IOCB_t *iocb;
James Smart3772a992009-05-22 14:50:54 -04007572 struct lpfc_sli_ring *pring = &phba->sli.ring[ring_number];
dea31012005-04-17 16:05:31 -05007573
James Smart92d7f7b2007-06-17 19:56:38 -05007574 if (piocb->iocb_cmpl && (!piocb->vport) &&
7575 (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
7576 (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
7577 lpfc_printf_log(phba, KERN_ERR,
7578 LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04007579 "1807 IOCB x%x failed. No vport\n",
James Smart92d7f7b2007-06-17 19:56:38 -05007580 piocb->iocb.ulpCommand);
7581 dump_stack();
7582 return IOCB_ERROR;
7583 }
7584
7585
Linas Vepstas8d63f372007-02-14 14:28:36 -06007586 /* If the PCI channel is in offline state, do not post iocbs. */
7587 if (unlikely(pci_channel_offline(phba->pcidev)))
7588 return IOCB_ERROR;
7589
James Smarta257bf92009-04-06 18:48:10 -04007590 /* If HBA has a deferred error attention, fail the iocb. */
7591 if (unlikely(phba->hba_flag & DEFER_ERATT))
7592 return IOCB_ERROR;
7593
dea31012005-04-17 16:05:31 -05007594 /*
7595 * We should never get an IOCB if we are in a < LINK_DOWN state
7596 */
James Smart2e0fef82007-06-17 19:56:36 -05007597 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
dea31012005-04-17 16:05:31 -05007598 return IOCB_ERROR;
7599
7600 /*
7601 * Check to see if we are blocking IOCB processing because of a
James Smart0b727fe2007-10-27 13:37:25 -04007602 * outstanding event.
dea31012005-04-17 16:05:31 -05007603 */
James Smart0b727fe2007-10-27 13:37:25 -04007604 if (unlikely(pring->flag & LPFC_STOP_IOCB_EVENT))
dea31012005-04-17 16:05:31 -05007605 goto iocb_busy;
7606
James Smart2e0fef82007-06-17 19:56:36 -05007607 if (unlikely(phba->link_state == LPFC_LINK_DOWN)) {
dea31012005-04-17 16:05:31 -05007608 /*
James Smart2680eea2007-04-25 09:52:55 -04007609 * Only CREATE_XRI, CLOSE_XRI, and QUE_RING_BUF
dea31012005-04-17 16:05:31 -05007610 * can be issued if the link is not up.
7611 */
7612 switch (piocb->iocb.ulpCommand) {
James Smart84774a42008-08-24 21:50:06 -04007613 case CMD_GEN_REQUEST64_CR:
7614 case CMD_GEN_REQUEST64_CX:
7615 if (!(phba->sli.sli_flag & LPFC_MENLO_MAINT) ||
7616 (piocb->iocb.un.genreq64.w5.hcsw.Rctl !=
James Smart6a9c52c2009-10-02 15:16:51 -04007617 FC_RCTL_DD_UNSOL_CMD) ||
James Smart84774a42008-08-24 21:50:06 -04007618 (piocb->iocb.un.genreq64.w5.hcsw.Type !=
7619 MENLO_TRANSPORT_TYPE))
7620
7621 goto iocb_busy;
7622 break;
dea31012005-04-17 16:05:31 -05007623 case CMD_QUE_RING_BUF_CN:
7624 case CMD_QUE_RING_BUF64_CN:
dea31012005-04-17 16:05:31 -05007625 /*
7626 * For IOCBs, like QUE_RING_BUF, that have no rsp ring
7627 * completion, iocb_cmpl MUST be 0.
7628 */
7629 if (piocb->iocb_cmpl)
7630 piocb->iocb_cmpl = NULL;
7631 /*FALLTHROUGH*/
7632 case CMD_CREATE_XRI_CR:
James Smart2680eea2007-04-25 09:52:55 -04007633 case CMD_CLOSE_XRI_CN:
7634 case CMD_CLOSE_XRI_CX:
dea31012005-04-17 16:05:31 -05007635 break;
7636 default:
7637 goto iocb_busy;
7638 }
7639
7640 /*
7641 * For FCP commands, we must be in a state where we can process link
7642 * attention events.
7643 */
7644 } else if (unlikely(pring->ringno == phba->sli.fcp_ring &&
James Smart92d7f7b2007-06-17 19:56:38 -05007645 !(phba->sli.sli_flag & LPFC_PROCESS_LA))) {
dea31012005-04-17 16:05:31 -05007646 goto iocb_busy;
James Smart92d7f7b2007-06-17 19:56:38 -05007647 }
dea31012005-04-17 16:05:31 -05007648
dea31012005-04-17 16:05:31 -05007649 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
7650 (nextiocb = lpfc_sli_next_iocb(phba, pring, &piocb)))
7651 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
7652
7653 if (iocb)
7654 lpfc_sli_update_ring(phba, pring);
7655 else
7656 lpfc_sli_update_full_ring(phba, pring);
7657
7658 if (!piocb)
7659 return IOCB_SUCCESS;
7660
7661 goto out_busy;
7662
7663 iocb_busy:
7664 pring->stats.iocb_cmd_delay++;
7665
7666 out_busy:
7667
7668 if (!(flag & SLI_IOCB_RET_IOCB)) {
James Smart92d7f7b2007-06-17 19:56:38 -05007669 __lpfc_sli_ringtx_put(phba, pring, piocb);
dea31012005-04-17 16:05:31 -05007670 return IOCB_SUCCESS;
7671 }
7672
7673 return IOCB_BUSY;
7674}
7675
James Smart3772a992009-05-22 14:50:54 -04007676/**
James Smart4f774512009-05-22 14:52:35 -04007677 * lpfc_sli4_bpl2sgl - Convert the bpl/bde to a sgl.
7678 * @phba: Pointer to HBA context object.
7679 * @piocb: Pointer to command iocb.
7680 * @sglq: Pointer to the scatter gather queue object.
7681 *
7682 * This routine converts the bpl or bde that is in the IOCB
7683 * to a sgl list for the sli4 hardware. The physical address
7684 * of the bpl/bde is converted back to a virtual address.
7685 * If the IOCB contains a BPL then the list of BDE's is
7686 * converted to sli4_sge's. If the IOCB contains a single
7687 * BDE then it is converted to a single sli_sge.
7688 * The IOCB is still in cpu endianess so the contents of
7689 * the bpl can be used without byte swapping.
7690 *
7691 * Returns valid XRI = Success, NO_XRI = Failure.
7692**/
7693static uint16_t
7694lpfc_sli4_bpl2sgl(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq,
7695 struct lpfc_sglq *sglq)
7696{
7697 uint16_t xritag = NO_XRI;
7698 struct ulp_bde64 *bpl = NULL;
7699 struct ulp_bde64 bde;
7700 struct sli4_sge *sgl = NULL;
James Smart1b511972011-12-13 13:23:09 -05007701 struct lpfc_dmabuf *dmabuf;
James Smart4f774512009-05-22 14:52:35 -04007702 IOCB_t *icmd;
7703 int numBdes = 0;
7704 int i = 0;
James Smart63e801c2010-11-20 23:14:19 -05007705 uint32_t offset = 0; /* accumulated offset in the sg request list */
7706 int inbound = 0; /* number of sg reply entries inbound from firmware */
James Smart4f774512009-05-22 14:52:35 -04007707
7708 if (!piocbq || !sglq)
7709 return xritag;
7710
7711 sgl = (struct sli4_sge *)sglq->sgl;
7712 icmd = &piocbq->iocb;
James Smart6b5151f2012-01-18 16:24:06 -05007713 if (icmd->ulpCommand == CMD_XMIT_BLS_RSP64_CX)
7714 return sglq->sli4_xritag;
James Smart4f774512009-05-22 14:52:35 -04007715 if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
7716 numBdes = icmd->un.genreq64.bdl.bdeSize /
7717 sizeof(struct ulp_bde64);
7718 /* The addrHigh and addrLow fields within the IOCB
7719 * have not been byteswapped yet so there is no
7720 * need to swap them back.
7721 */
James Smart1b511972011-12-13 13:23:09 -05007722 if (piocbq->context3)
7723 dmabuf = (struct lpfc_dmabuf *)piocbq->context3;
7724 else
7725 return xritag;
James Smart4f774512009-05-22 14:52:35 -04007726
James Smart1b511972011-12-13 13:23:09 -05007727 bpl = (struct ulp_bde64 *)dmabuf->virt;
James Smart4f774512009-05-22 14:52:35 -04007728 if (!bpl)
7729 return xritag;
7730
7731 for (i = 0; i < numBdes; i++) {
7732 /* Should already be byte swapped. */
James Smart28baac72010-02-12 14:42:03 -05007733 sgl->addr_hi = bpl->addrHigh;
7734 sgl->addr_lo = bpl->addrLow;
7735
James Smart05580562011-05-24 11:40:48 -04007736 sgl->word2 = le32_to_cpu(sgl->word2);
James Smart4f774512009-05-22 14:52:35 -04007737 if ((i+1) == numBdes)
7738 bf_set(lpfc_sli4_sge_last, sgl, 1);
7739 else
7740 bf_set(lpfc_sli4_sge_last, sgl, 0);
James Smart28baac72010-02-12 14:42:03 -05007741 /* swap the size field back to the cpu so we
7742 * can assign it to the sgl.
7743 */
7744 bde.tus.w = le32_to_cpu(bpl->tus.w);
7745 sgl->sge_len = cpu_to_le32(bde.tus.f.bdeSize);
James Smart63e801c2010-11-20 23:14:19 -05007746 /* The offsets in the sgl need to be accumulated
7747 * separately for the request and reply lists.
7748 * The request is always first, the reply follows.
7749 */
7750 if (piocbq->iocb.ulpCommand == CMD_GEN_REQUEST64_CR) {
7751 /* add up the reply sg entries */
7752 if (bpl->tus.f.bdeFlags == BUFF_TYPE_BDE_64I)
7753 inbound++;
7754 /* first inbound? reset the offset */
7755 if (inbound == 1)
7756 offset = 0;
7757 bf_set(lpfc_sli4_sge_offset, sgl, offset);
James Smartf9bb2da2011-10-10 21:34:11 -04007758 bf_set(lpfc_sli4_sge_type, sgl,
7759 LPFC_SGE_TYPE_DATA);
James Smart63e801c2010-11-20 23:14:19 -05007760 offset += bde.tus.f.bdeSize;
7761 }
James Smart546fc852011-03-11 16:06:29 -05007762 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart4f774512009-05-22 14:52:35 -04007763 bpl++;
7764 sgl++;
7765 }
7766 } else if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BDE_64) {
7767 /* The addrHigh and addrLow fields of the BDE have not
7768 * been byteswapped yet so they need to be swapped
7769 * before putting them in the sgl.
7770 */
7771 sgl->addr_hi =
7772 cpu_to_le32(icmd->un.genreq64.bdl.addrHigh);
7773 sgl->addr_lo =
7774 cpu_to_le32(icmd->un.genreq64.bdl.addrLow);
James Smart05580562011-05-24 11:40:48 -04007775 sgl->word2 = le32_to_cpu(sgl->word2);
James Smart4f774512009-05-22 14:52:35 -04007776 bf_set(lpfc_sli4_sge_last, sgl, 1);
7777 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart28baac72010-02-12 14:42:03 -05007778 sgl->sge_len =
7779 cpu_to_le32(icmd->un.genreq64.bdl.bdeSize);
James Smart4f774512009-05-22 14:52:35 -04007780 }
7781 return sglq->sli4_xritag;
7782}
7783
7784/**
7785 * lpfc_sli4_scmd_to_wqidx_distr - scsi command to SLI4 WQ index distribution
7786 * @phba: Pointer to HBA context object.
James Smart4f774512009-05-22 14:52:35 -04007787 *
James Smarta93ff372010-10-22 11:06:08 -04007788 * This routine performs a roundrobin SCSI command to SLI4 FCP WQ index
James Smart8fa38512009-07-19 10:01:03 -04007789 * distribution. This is called by __lpfc_sli_issue_iocb_s4() with the hbalock
7790 * held.
James Smart4f774512009-05-22 14:52:35 -04007791 *
7792 * Return: index into SLI4 fast-path FCP queue index.
7793 **/
7794static uint32_t
James Smart8fa38512009-07-19 10:01:03 -04007795lpfc_sli4_scmd_to_wqidx_distr(struct lpfc_hba *phba)
James Smart4f774512009-05-22 14:52:35 -04007796{
James Smart8fa38512009-07-19 10:01:03 -04007797 ++phba->fcp_qidx;
7798 if (phba->fcp_qidx >= phba->cfg_fcp_wq_count)
7799 phba->fcp_qidx = 0;
James Smart4f774512009-05-22 14:52:35 -04007800
James Smart8fa38512009-07-19 10:01:03 -04007801 return phba->fcp_qidx;
James Smart4f774512009-05-22 14:52:35 -04007802}
7803
7804/**
7805 * lpfc_sli_iocb2wqe - Convert the IOCB to a work queue entry.
7806 * @phba: Pointer to HBA context object.
7807 * @piocb: Pointer to command iocb.
7808 * @wqe: Pointer to the work queue entry.
7809 *
7810 * This routine converts the iocb command to its Work Queue Entry
7811 * equivalent. The wqe pointer should not have any fields set when
7812 * this routine is called because it will memcpy over them.
7813 * This routine does not set the CQ_ID or the WQEC bits in the
7814 * wqe.
7815 *
7816 * Returns: 0 = Success, IOCB_ERROR = Failure.
7817 **/
7818static int
7819lpfc_sli4_iocb2wqe(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq,
7820 union lpfc_wqe *wqe)
7821{
James Smart5ffc2662009-11-18 15:39:44 -05007822 uint32_t xmit_len = 0, total_len = 0;
James Smart4f774512009-05-22 14:52:35 -04007823 uint8_t ct = 0;
7824 uint32_t fip;
7825 uint32_t abort_tag;
7826 uint8_t command_type = ELS_COMMAND_NON_FIP;
7827 uint8_t cmnd;
7828 uint16_t xritag;
James Smartdcf2a4e2010-09-29 11:18:53 -04007829 uint16_t abrt_iotag;
7830 struct lpfc_iocbq *abrtiocbq;
James Smart4f774512009-05-22 14:52:35 -04007831 struct ulp_bde64 *bpl = NULL;
James Smartf0d9bcc2010-10-22 11:07:09 -04007832 uint32_t els_id = LPFC_ELS_ID_DEFAULT;
James Smart5ffc2662009-11-18 15:39:44 -05007833 int numBdes, i;
7834 struct ulp_bde64 bde;
James Smartc31098c2011-04-16 11:03:33 -04007835 struct lpfc_nodelist *ndlp;
James Smartff78d8f2011-12-13 13:21:35 -05007836 uint32_t *pcmd;
James Smart1b511972011-12-13 13:23:09 -05007837 uint32_t if_type;
James Smart4f774512009-05-22 14:52:35 -04007838
James Smart45ed1192009-10-02 15:17:02 -04007839 fip = phba->hba_flag & HBA_FIP_SUPPORT;
James Smart4f774512009-05-22 14:52:35 -04007840 /* The fcp commands will set command type */
James Smart0c287582009-06-10 17:22:56 -04007841 if (iocbq->iocb_flag & LPFC_IO_FCP)
James Smart4f774512009-05-22 14:52:35 -04007842 command_type = FCP_COMMAND;
James Smartc8685952009-11-18 15:39:16 -05007843 else if (fip && (iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK))
James Smart0c287582009-06-10 17:22:56 -04007844 command_type = ELS_COMMAND_FIP;
7845 else
7846 command_type = ELS_COMMAND_NON_FIP;
7847
James Smart4f774512009-05-22 14:52:35 -04007848 /* Some of the fields are in the right position already */
7849 memcpy(wqe, &iocbq->iocb, sizeof(union lpfc_wqe));
7850 abort_tag = (uint32_t) iocbq->iotag;
7851 xritag = iocbq->sli4_xritag;
James Smartf0d9bcc2010-10-22 11:07:09 -04007852 wqe->generic.wqe_com.word7 = 0; /* The ct field has moved so reset */
James Smart4f774512009-05-22 14:52:35 -04007853 /* words0-2 bpl convert bde */
7854 if (iocbq->iocb.un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
James Smart5ffc2662009-11-18 15:39:44 -05007855 numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
7856 sizeof(struct ulp_bde64);
James Smart4f774512009-05-22 14:52:35 -04007857 bpl = (struct ulp_bde64 *)
7858 ((struct lpfc_dmabuf *)iocbq->context3)->virt;
7859 if (!bpl)
7860 return IOCB_ERROR;
7861
7862 /* Should already be byte swapped. */
7863 wqe->generic.bde.addrHigh = le32_to_cpu(bpl->addrHigh);
7864 wqe->generic.bde.addrLow = le32_to_cpu(bpl->addrLow);
7865 /* swap the size field back to the cpu so we
7866 * can assign it to the sgl.
7867 */
7868 wqe->generic.bde.tus.w = le32_to_cpu(bpl->tus.w);
James Smart5ffc2662009-11-18 15:39:44 -05007869 xmit_len = wqe->generic.bde.tus.f.bdeSize;
7870 total_len = 0;
7871 for (i = 0; i < numBdes; i++) {
7872 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
7873 total_len += bde.tus.f.bdeSize;
7874 }
James Smart4f774512009-05-22 14:52:35 -04007875 } else
James Smart5ffc2662009-11-18 15:39:44 -05007876 xmit_len = iocbq->iocb.un.fcpi64.bdl.bdeSize;
James Smart4f774512009-05-22 14:52:35 -04007877
7878 iocbq->iocb.ulpIoTag = iocbq->iotag;
7879 cmnd = iocbq->iocb.ulpCommand;
7880
7881 switch (iocbq->iocb.ulpCommand) {
7882 case CMD_ELS_REQUEST64_CR:
James Smart93d1379e2012-05-09 21:19:34 -04007883 if (iocbq->iocb_flag & LPFC_IO_LIBDFC)
7884 ndlp = iocbq->context_un.ndlp;
7885 else
7886 ndlp = (struct lpfc_nodelist *)iocbq->context1;
James Smart4f774512009-05-22 14:52:35 -04007887 if (!iocbq->iocb.ulpLe) {
7888 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
7889 "2007 Only Limited Edition cmd Format"
7890 " supported 0x%x\n",
7891 iocbq->iocb.ulpCommand);
7892 return IOCB_ERROR;
7893 }
James Smartff78d8f2011-12-13 13:21:35 -05007894
James Smart5ffc2662009-11-18 15:39:44 -05007895 wqe->els_req.payload_len = xmit_len;
James Smart4f774512009-05-22 14:52:35 -04007896 /* Els_reguest64 has a TMO */
7897 bf_set(wqe_tmo, &wqe->els_req.wqe_com,
7898 iocbq->iocb.ulpTimeout);
7899 /* Need a VF for word 4 set the vf bit*/
7900 bf_set(els_req64_vf, &wqe->els_req, 0);
7901 /* And a VFID for word 12 */
7902 bf_set(els_req64_vfid, &wqe->els_req, 0);
James Smart4f774512009-05-22 14:52:35 -04007903 ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
James Smartf0d9bcc2010-10-22 11:07:09 -04007904 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
7905 iocbq->iocb.ulpContext);
7906 bf_set(wqe_ct, &wqe->els_req.wqe_com, ct);
7907 bf_set(wqe_pu, &wqe->els_req.wqe_com, 0);
James Smart4f774512009-05-22 14:52:35 -04007908 /* CCP CCPE PV PRI in word10 were set in the memcpy */
James Smartff78d8f2011-12-13 13:21:35 -05007909 if (command_type == ELS_COMMAND_FIP)
James Smartc8685952009-11-18 15:39:16 -05007910 els_id = ((iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK)
7911 >> LPFC_FIP_ELS_ID_SHIFT);
James Smartff78d8f2011-12-13 13:21:35 -05007912 pcmd = (uint32_t *) (((struct lpfc_dmabuf *)
7913 iocbq->context2)->virt);
James Smart1b511972011-12-13 13:23:09 -05007914 if_type = bf_get(lpfc_sli_intf_if_type,
7915 &phba->sli4_hba.sli_intf);
7916 if (if_type == LPFC_SLI_INTF_IF_TYPE_2) {
James Smartff78d8f2011-12-13 13:21:35 -05007917 if (pcmd && (*pcmd == ELS_CMD_FLOGI ||
James Smartcb69f7d2011-12-13 13:21:57 -05007918 *pcmd == ELS_CMD_SCR ||
James Smart6b5151f2012-01-18 16:24:06 -05007919 *pcmd == ELS_CMD_FDISC ||
James Smartbdcd2b92012-03-01 22:33:52 -05007920 *pcmd == ELS_CMD_LOGO ||
James Smartff78d8f2011-12-13 13:21:35 -05007921 *pcmd == ELS_CMD_PLOGI)) {
7922 bf_set(els_req64_sp, &wqe->els_req, 1);
7923 bf_set(els_req64_sid, &wqe->els_req,
7924 iocbq->vport->fc_myDID);
James Smart939723a2012-05-09 21:19:03 -04007925 if ((*pcmd == ELS_CMD_FLOGI) &&
7926 !(phba->fc_topology ==
7927 LPFC_TOPOLOGY_LOOP))
7928 bf_set(els_req64_sid, &wqe->els_req, 0);
James Smartff78d8f2011-12-13 13:21:35 -05007929 bf_set(wqe_ct, &wqe->els_req.wqe_com, 1);
7930 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
James Smarta7dd9c02012-05-09 21:16:50 -04007931 phba->vpi_ids[iocbq->vport->vpi]);
James Smart3ef6d242012-01-18 16:23:48 -05007932 } else if (pcmd && iocbq->context1) {
James Smartff78d8f2011-12-13 13:21:35 -05007933 bf_set(wqe_ct, &wqe->els_req.wqe_com, 0);
7934 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
7935 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
7936 }
James Smartc8685952009-11-18 15:39:16 -05007937 }
James Smart6d368e52011-05-24 11:44:12 -04007938 bf_set(wqe_temp_rpi, &wqe->els_req.wqe_com,
7939 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
James Smartf0d9bcc2010-10-22 11:07:09 -04007940 bf_set(wqe_els_id, &wqe->els_req.wqe_com, els_id);
7941 bf_set(wqe_dbde, &wqe->els_req.wqe_com, 1);
7942 bf_set(wqe_iod, &wqe->els_req.wqe_com, LPFC_WQE_IOD_READ);
7943 bf_set(wqe_qosd, &wqe->els_req.wqe_com, 1);
7944 bf_set(wqe_lenloc, &wqe->els_req.wqe_com, LPFC_WQE_LENLOC_NONE);
7945 bf_set(wqe_ebde_cnt, &wqe->els_req.wqe_com, 0);
James Smart7851fe22011-07-22 18:36:52 -04007946 break;
James Smart5ffc2662009-11-18 15:39:44 -05007947 case CMD_XMIT_SEQUENCE64_CX:
James Smartf0d9bcc2010-10-22 11:07:09 -04007948 bf_set(wqe_ctxt_tag, &wqe->xmit_sequence.wqe_com,
7949 iocbq->iocb.un.ulpWord[3]);
7950 bf_set(wqe_rcvoxid, &wqe->xmit_sequence.wqe_com,
James Smart7851fe22011-07-22 18:36:52 -04007951 iocbq->iocb.unsli3.rcvsli3.ox_id);
James Smart5ffc2662009-11-18 15:39:44 -05007952 /* The entire sequence is transmitted for this IOCB */
7953 xmit_len = total_len;
7954 cmnd = CMD_XMIT_SEQUENCE64_CR;
James Smart1b511972011-12-13 13:23:09 -05007955 if (phba->link_flag & LS_LOOPBACK_MODE)
7956 bf_set(wqe_xo, &wqe->xmit_sequence.wge_ctl, 1);
James Smart4f774512009-05-22 14:52:35 -04007957 case CMD_XMIT_SEQUENCE64_CR:
James Smartf0d9bcc2010-10-22 11:07:09 -04007958 /* word3 iocb=io_tag32 wqe=reserved */
7959 wqe->xmit_sequence.rsvd3 = 0;
James Smart4f774512009-05-22 14:52:35 -04007960 /* word4 relative_offset memcpy */
7961 /* word5 r_ctl/df_ctl memcpy */
James Smartf0d9bcc2010-10-22 11:07:09 -04007962 bf_set(wqe_pu, &wqe->xmit_sequence.wqe_com, 0);
7963 bf_set(wqe_dbde, &wqe->xmit_sequence.wqe_com, 1);
7964 bf_set(wqe_iod, &wqe->xmit_sequence.wqe_com,
7965 LPFC_WQE_IOD_WRITE);
7966 bf_set(wqe_lenloc, &wqe->xmit_sequence.wqe_com,
7967 LPFC_WQE_LENLOC_WORD12);
7968 bf_set(wqe_ebde_cnt, &wqe->xmit_sequence.wqe_com, 0);
James Smart5ffc2662009-11-18 15:39:44 -05007969 wqe->xmit_sequence.xmit_len = xmit_len;
7970 command_type = OTHER_COMMAND;
James Smart7851fe22011-07-22 18:36:52 -04007971 break;
James Smart4f774512009-05-22 14:52:35 -04007972 case CMD_XMIT_BCAST64_CN:
James Smartf0d9bcc2010-10-22 11:07:09 -04007973 /* word3 iocb=iotag32 wqe=seq_payload_len */
7974 wqe->xmit_bcast64.seq_payload_len = xmit_len;
James Smart4f774512009-05-22 14:52:35 -04007975 /* word4 iocb=rsvd wqe=rsvd */
7976 /* word5 iocb=rctl/type/df_ctl wqe=rctl/type/df_ctl memcpy */
7977 /* word6 iocb=ctxt_tag/io_tag wqe=ctxt_tag/xri */
James Smartf0d9bcc2010-10-22 11:07:09 -04007978 bf_set(wqe_ct, &wqe->xmit_bcast64.wqe_com,
James Smart4f774512009-05-22 14:52:35 -04007979 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
James Smartf0d9bcc2010-10-22 11:07:09 -04007980 bf_set(wqe_dbde, &wqe->xmit_bcast64.wqe_com, 1);
7981 bf_set(wqe_iod, &wqe->xmit_bcast64.wqe_com, LPFC_WQE_IOD_WRITE);
7982 bf_set(wqe_lenloc, &wqe->xmit_bcast64.wqe_com,
7983 LPFC_WQE_LENLOC_WORD3);
7984 bf_set(wqe_ebde_cnt, &wqe->xmit_bcast64.wqe_com, 0);
James Smart7851fe22011-07-22 18:36:52 -04007985 break;
James Smart4f774512009-05-22 14:52:35 -04007986 case CMD_FCP_IWRITE64_CR:
7987 command_type = FCP_COMMAND_DATA_OUT;
James Smartf0d9bcc2010-10-22 11:07:09 -04007988 /* word3 iocb=iotag wqe=payload_offset_len */
7989 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
7990 wqe->fcp_iwrite.payload_offset_len =
James Smart5ffc2662009-11-18 15:39:44 -05007991 xmit_len + sizeof(struct fcp_rsp);
James Smartf0d9bcc2010-10-22 11:07:09 -04007992 /* word4 iocb=parameter wqe=total_xfer_length memcpy */
7993 /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
7994 bf_set(wqe_erp, &wqe->fcp_iwrite.wqe_com,
7995 iocbq->iocb.ulpFCP2Rcvy);
7996 bf_set(wqe_lnk, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpXS);
7997 /* Always open the exchange */
7998 bf_set(wqe_xc, &wqe->fcp_iwrite.wqe_com, 0);
James Smartf0d9bcc2010-10-22 11:07:09 -04007999 bf_set(wqe_iod, &wqe->fcp_iwrite.wqe_com, LPFC_WQE_IOD_WRITE);
8000 bf_set(wqe_lenloc, &wqe->fcp_iwrite.wqe_com,
8001 LPFC_WQE_LENLOC_WORD4);
8002 bf_set(wqe_ebde_cnt, &wqe->fcp_iwrite.wqe_com, 0);
8003 bf_set(wqe_pu, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpPU);
James Smartacd68592012-01-18 16:25:09 -05008004 if (iocbq->iocb_flag & LPFC_IO_DIF) {
8005 iocbq->iocb_flag &= ~LPFC_IO_DIF;
8006 bf_set(wqe_dif, &wqe->generic.wqe_com, 1);
8007 }
8008 bf_set(wqe_dbde, &wqe->fcp_iwrite.wqe_com, 1);
James Smart7851fe22011-07-22 18:36:52 -04008009 break;
James Smartf0d9bcc2010-10-22 11:07:09 -04008010 case CMD_FCP_IREAD64_CR:
8011 /* word3 iocb=iotag wqe=payload_offset_len */
8012 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
8013 wqe->fcp_iread.payload_offset_len =
8014 xmit_len + sizeof(struct fcp_rsp);
8015 /* word4 iocb=parameter wqe=total_xfer_length memcpy */
8016 /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
8017 bf_set(wqe_erp, &wqe->fcp_iread.wqe_com,
8018 iocbq->iocb.ulpFCP2Rcvy);
8019 bf_set(wqe_lnk, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpXS);
James Smart4f774512009-05-22 14:52:35 -04008020 /* Always open the exchange */
8021 bf_set(wqe_xc, &wqe->fcp_iread.wqe_com, 0);
James Smartf0d9bcc2010-10-22 11:07:09 -04008022 bf_set(wqe_iod, &wqe->fcp_iread.wqe_com, LPFC_WQE_IOD_READ);
8023 bf_set(wqe_lenloc, &wqe->fcp_iread.wqe_com,
8024 LPFC_WQE_LENLOC_WORD4);
8025 bf_set(wqe_ebde_cnt, &wqe->fcp_iread.wqe_com, 0);
8026 bf_set(wqe_pu, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpPU);
James Smartacd68592012-01-18 16:25:09 -05008027 if (iocbq->iocb_flag & LPFC_IO_DIF) {
8028 iocbq->iocb_flag &= ~LPFC_IO_DIF;
8029 bf_set(wqe_dif, &wqe->generic.wqe_com, 1);
8030 }
8031 bf_set(wqe_dbde, &wqe->fcp_iread.wqe_com, 1);
James Smart7851fe22011-07-22 18:36:52 -04008032 break;
James Smartf1126682009-06-10 17:22:44 -04008033 case CMD_FCP_ICMND64_CR:
James Smartf0d9bcc2010-10-22 11:07:09 -04008034 /* word3 iocb=IO_TAG wqe=reserved */
8035 wqe->fcp_icmd.rsrvd3 = 0;
8036 bf_set(wqe_pu, &wqe->fcp_icmd.wqe_com, 0);
James Smartf1126682009-06-10 17:22:44 -04008037 /* Always open the exchange */
James Smartf0d9bcc2010-10-22 11:07:09 -04008038 bf_set(wqe_xc, &wqe->fcp_icmd.wqe_com, 0);
8039 bf_set(wqe_dbde, &wqe->fcp_icmd.wqe_com, 1);
8040 bf_set(wqe_iod, &wqe->fcp_icmd.wqe_com, LPFC_WQE_IOD_WRITE);
8041 bf_set(wqe_qosd, &wqe->fcp_icmd.wqe_com, 1);
8042 bf_set(wqe_lenloc, &wqe->fcp_icmd.wqe_com,
8043 LPFC_WQE_LENLOC_NONE);
8044 bf_set(wqe_ebde_cnt, &wqe->fcp_icmd.wqe_com, 0);
James Smart7851fe22011-07-22 18:36:52 -04008045 break;
James Smart4f774512009-05-22 14:52:35 -04008046 case CMD_GEN_REQUEST64_CR:
James Smart63e801c2010-11-20 23:14:19 -05008047 /* For this command calculate the xmit length of the
8048 * request bde.
8049 */
8050 xmit_len = 0;
8051 numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
8052 sizeof(struct ulp_bde64);
8053 for (i = 0; i < numBdes; i++) {
James Smart63e801c2010-11-20 23:14:19 -05008054 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
James Smart546fc852011-03-11 16:06:29 -05008055 if (bde.tus.f.bdeFlags != BUFF_TYPE_BDE_64)
8056 break;
James Smart63e801c2010-11-20 23:14:19 -05008057 xmit_len += bde.tus.f.bdeSize;
8058 }
James Smartf0d9bcc2010-10-22 11:07:09 -04008059 /* word3 iocb=IO_TAG wqe=request_payload_len */
8060 wqe->gen_req.request_payload_len = xmit_len;
8061 /* word4 iocb=parameter wqe=relative_offset memcpy */
8062 /* word5 [rctl, type, df_ctl, la] copied in memcpy */
James Smart4f774512009-05-22 14:52:35 -04008063 /* word6 context tag copied in memcpy */
8064 if (iocbq->iocb.ulpCt_h || iocbq->iocb.ulpCt_l) {
8065 ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
8066 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8067 "2015 Invalid CT %x command 0x%x\n",
8068 ct, iocbq->iocb.ulpCommand);
8069 return IOCB_ERROR;
8070 }
James Smartf0d9bcc2010-10-22 11:07:09 -04008071 bf_set(wqe_ct, &wqe->gen_req.wqe_com, 0);
8072 bf_set(wqe_tmo, &wqe->gen_req.wqe_com, iocbq->iocb.ulpTimeout);
8073 bf_set(wqe_pu, &wqe->gen_req.wqe_com, iocbq->iocb.ulpPU);
8074 bf_set(wqe_dbde, &wqe->gen_req.wqe_com, 1);
8075 bf_set(wqe_iod, &wqe->gen_req.wqe_com, LPFC_WQE_IOD_READ);
8076 bf_set(wqe_qosd, &wqe->gen_req.wqe_com, 1);
8077 bf_set(wqe_lenloc, &wqe->gen_req.wqe_com, LPFC_WQE_LENLOC_NONE);
8078 bf_set(wqe_ebde_cnt, &wqe->gen_req.wqe_com, 0);
James Smart4f774512009-05-22 14:52:35 -04008079 command_type = OTHER_COMMAND;
James Smart7851fe22011-07-22 18:36:52 -04008080 break;
James Smart4f774512009-05-22 14:52:35 -04008081 case CMD_XMIT_ELS_RSP64_CX:
James Smartc31098c2011-04-16 11:03:33 -04008082 ndlp = (struct lpfc_nodelist *)iocbq->context1;
James Smart4f774512009-05-22 14:52:35 -04008083 /* words0-2 BDE memcpy */
James Smartf0d9bcc2010-10-22 11:07:09 -04008084 /* word3 iocb=iotag32 wqe=response_payload_len */
8085 wqe->xmit_els_rsp.response_payload_len = xmit_len;
James Smart939723a2012-05-09 21:19:03 -04008086 /* word4 */
8087 wqe->xmit_els_rsp.word4 = 0;
James Smart4f774512009-05-22 14:52:35 -04008088 /* word5 iocb=rsvd wge=did */
8089 bf_set(wqe_els_did, &wqe->xmit_els_rsp.wqe_dest,
James Smart939723a2012-05-09 21:19:03 -04008090 iocbq->iocb.un.xseq64.xmit_els_remoteID);
8091
8092 if_type = bf_get(lpfc_sli_intf_if_type,
8093 &phba->sli4_hba.sli_intf);
8094 if (if_type == LPFC_SLI_INTF_IF_TYPE_2) {
8095 if (iocbq->vport->fc_flag & FC_PT2PT) {
8096 bf_set(els_rsp64_sp, &wqe->xmit_els_rsp, 1);
8097 bf_set(els_rsp64_sid, &wqe->xmit_els_rsp,
8098 iocbq->vport->fc_myDID);
8099 if (iocbq->vport->fc_myDID == Fabric_DID) {
8100 bf_set(wqe_els_did,
8101 &wqe->xmit_els_rsp.wqe_dest, 0);
8102 }
8103 }
8104 }
James Smartf0d9bcc2010-10-22 11:07:09 -04008105 bf_set(wqe_ct, &wqe->xmit_els_rsp.wqe_com,
8106 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
8107 bf_set(wqe_pu, &wqe->xmit_els_rsp.wqe_com, iocbq->iocb.ulpPU);
8108 bf_set(wqe_rcvoxid, &wqe->xmit_els_rsp.wqe_com,
James Smart7851fe22011-07-22 18:36:52 -04008109 iocbq->iocb.unsli3.rcvsli3.ox_id);
James Smart4f774512009-05-22 14:52:35 -04008110 if (!iocbq->iocb.ulpCt_h && iocbq->iocb.ulpCt_l)
James Smartf0d9bcc2010-10-22 11:07:09 -04008111 bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
James Smart6d368e52011-05-24 11:44:12 -04008112 phba->vpi_ids[iocbq->vport->vpi]);
James Smartf0d9bcc2010-10-22 11:07:09 -04008113 bf_set(wqe_dbde, &wqe->xmit_els_rsp.wqe_com, 1);
8114 bf_set(wqe_iod, &wqe->xmit_els_rsp.wqe_com, LPFC_WQE_IOD_WRITE);
8115 bf_set(wqe_qosd, &wqe->xmit_els_rsp.wqe_com, 1);
8116 bf_set(wqe_lenloc, &wqe->xmit_els_rsp.wqe_com,
8117 LPFC_WQE_LENLOC_WORD3);
8118 bf_set(wqe_ebde_cnt, &wqe->xmit_els_rsp.wqe_com, 0);
James Smart6d368e52011-05-24 11:44:12 -04008119 bf_set(wqe_rsp_temp_rpi, &wqe->xmit_els_rsp,
8120 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
James Smartff78d8f2011-12-13 13:21:35 -05008121 pcmd = (uint32_t *) (((struct lpfc_dmabuf *)
8122 iocbq->context2)->virt);
8123 if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
James Smart939723a2012-05-09 21:19:03 -04008124 bf_set(els_rsp64_sp, &wqe->xmit_els_rsp, 1);
8125 bf_set(els_rsp64_sid, &wqe->xmit_els_rsp,
James Smartff78d8f2011-12-13 13:21:35 -05008126 iocbq->vport->fc_myDID);
James Smart939723a2012-05-09 21:19:03 -04008127 bf_set(wqe_ct, &wqe->xmit_els_rsp.wqe_com, 1);
8128 bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
James Smartff78d8f2011-12-13 13:21:35 -05008129 phba->vpi_ids[phba->pport->vpi]);
8130 }
James Smart4f774512009-05-22 14:52:35 -04008131 command_type = OTHER_COMMAND;
James Smart7851fe22011-07-22 18:36:52 -04008132 break;
James Smart4f774512009-05-22 14:52:35 -04008133 case CMD_CLOSE_XRI_CN:
8134 case CMD_ABORT_XRI_CN:
8135 case CMD_ABORT_XRI_CX:
8136 /* words 0-2 memcpy should be 0 rserved */
8137 /* port will send abts */
James Smartdcf2a4e2010-09-29 11:18:53 -04008138 abrt_iotag = iocbq->iocb.un.acxri.abortContextTag;
8139 if (abrt_iotag != 0 && abrt_iotag <= phba->sli.last_iotag) {
8140 abrtiocbq = phba->sli.iocbq_lookup[abrt_iotag];
8141 fip = abrtiocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK;
8142 } else
8143 fip = 0;
8144
8145 if ((iocbq->iocb.ulpCommand == CMD_CLOSE_XRI_CN) || fip)
James Smart4f774512009-05-22 14:52:35 -04008146 /*
James Smartdcf2a4e2010-09-29 11:18:53 -04008147 * The link is down, or the command was ELS_FIP
8148 * so the fw does not need to send abts
James Smart4f774512009-05-22 14:52:35 -04008149 * on the wire.
8150 */
8151 bf_set(abort_cmd_ia, &wqe->abort_cmd, 1);
8152 else
8153 bf_set(abort_cmd_ia, &wqe->abort_cmd, 0);
8154 bf_set(abort_cmd_criteria, &wqe->abort_cmd, T_XRI_TAG);
James Smartf0d9bcc2010-10-22 11:07:09 -04008155 /* word5 iocb=CONTEXT_TAG|IO_TAG wqe=reserved */
8156 wqe->abort_cmd.rsrvd5 = 0;
8157 bf_set(wqe_ct, &wqe->abort_cmd.wqe_com,
James Smart4f774512009-05-22 14:52:35 -04008158 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
8159 abort_tag = iocbq->iocb.un.acxri.abortIoTag;
James Smart4f774512009-05-22 14:52:35 -04008160 /*
8161 * The abort handler will send us CMD_ABORT_XRI_CN or
8162 * CMD_CLOSE_XRI_CN and the fw only accepts CMD_ABORT_XRI_CX
8163 */
James Smartf0d9bcc2010-10-22 11:07:09 -04008164 bf_set(wqe_cmnd, &wqe->abort_cmd.wqe_com, CMD_ABORT_XRI_CX);
8165 bf_set(wqe_qosd, &wqe->abort_cmd.wqe_com, 1);
8166 bf_set(wqe_lenloc, &wqe->abort_cmd.wqe_com,
8167 LPFC_WQE_LENLOC_NONE);
James Smart4f774512009-05-22 14:52:35 -04008168 cmnd = CMD_ABORT_XRI_CX;
8169 command_type = OTHER_COMMAND;
8170 xritag = 0;
James Smart7851fe22011-07-22 18:36:52 -04008171 break;
James Smart6669f9b2009-10-02 15:16:45 -04008172 case CMD_XMIT_BLS_RSP64_CX:
James Smart6b5151f2012-01-18 16:24:06 -05008173 ndlp = (struct lpfc_nodelist *)iocbq->context1;
James Smart546fc852011-03-11 16:06:29 -05008174 /* As BLS ABTS RSP WQE is very different from other WQEs,
James Smart6669f9b2009-10-02 15:16:45 -04008175 * we re-construct this WQE here based on information in
8176 * iocbq from scratch.
8177 */
8178 memset(wqe, 0, sizeof(union lpfc_wqe));
James Smart5ffc2662009-11-18 15:39:44 -05008179 /* OX_ID is invariable to who sent ABTS to CT exchange */
James Smart6669f9b2009-10-02 15:16:45 -04008180 bf_set(xmit_bls_rsp64_oxid, &wqe->xmit_bls_rsp,
James Smart546fc852011-03-11 16:06:29 -05008181 bf_get(lpfc_abts_oxid, &iocbq->iocb.un.bls_rsp));
8182 if (bf_get(lpfc_abts_orig, &iocbq->iocb.un.bls_rsp) ==
James Smart5ffc2662009-11-18 15:39:44 -05008183 LPFC_ABTS_UNSOL_INT) {
8184 /* ABTS sent by initiator to CT exchange, the
8185 * RX_ID field will be filled with the newly
8186 * allocated responder XRI.
8187 */
8188 bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
8189 iocbq->sli4_xritag);
8190 } else {
8191 /* ABTS sent by responder to CT exchange, the
8192 * RX_ID field will be filled with the responder
8193 * RX_ID from ABTS.
8194 */
8195 bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
James Smart546fc852011-03-11 16:06:29 -05008196 bf_get(lpfc_abts_rxid, &iocbq->iocb.un.bls_rsp));
James Smart5ffc2662009-11-18 15:39:44 -05008197 }
James Smart6669f9b2009-10-02 15:16:45 -04008198 bf_set(xmit_bls_rsp64_seqcnthi, &wqe->xmit_bls_rsp, 0xffff);
8199 bf_set(wqe_xmit_bls_pt, &wqe->xmit_bls_rsp.wqe_dest, 0x1);
James Smart6b5151f2012-01-18 16:24:06 -05008200
8201 /* Use CT=VPI */
8202 bf_set(wqe_els_did, &wqe->xmit_bls_rsp.wqe_dest,
8203 ndlp->nlp_DID);
8204 bf_set(xmit_bls_rsp64_temprpi, &wqe->xmit_bls_rsp,
8205 iocbq->iocb.ulpContext);
8206 bf_set(wqe_ct, &wqe->xmit_bls_rsp.wqe_com, 1);
James Smart6669f9b2009-10-02 15:16:45 -04008207 bf_set(wqe_ctxt_tag, &wqe->xmit_bls_rsp.wqe_com,
James Smart6b5151f2012-01-18 16:24:06 -05008208 phba->vpi_ids[phba->pport->vpi]);
James Smartf0d9bcc2010-10-22 11:07:09 -04008209 bf_set(wqe_qosd, &wqe->xmit_bls_rsp.wqe_com, 1);
8210 bf_set(wqe_lenloc, &wqe->xmit_bls_rsp.wqe_com,
8211 LPFC_WQE_LENLOC_NONE);
James Smart6669f9b2009-10-02 15:16:45 -04008212 /* Overwrite the pre-set comnd type with OTHER_COMMAND */
8213 command_type = OTHER_COMMAND;
James Smart546fc852011-03-11 16:06:29 -05008214 if (iocbq->iocb.un.xseq64.w5.hcsw.Rctl == FC_RCTL_BA_RJT) {
8215 bf_set(xmit_bls_rsp64_rjt_vspec, &wqe->xmit_bls_rsp,
8216 bf_get(lpfc_vndr_code, &iocbq->iocb.un.bls_rsp));
8217 bf_set(xmit_bls_rsp64_rjt_expc, &wqe->xmit_bls_rsp,
8218 bf_get(lpfc_rsn_expln, &iocbq->iocb.un.bls_rsp));
8219 bf_set(xmit_bls_rsp64_rjt_rsnc, &wqe->xmit_bls_rsp,
8220 bf_get(lpfc_rsn_code, &iocbq->iocb.un.bls_rsp));
8221 }
8222
James Smart7851fe22011-07-22 18:36:52 -04008223 break;
James Smart4f774512009-05-22 14:52:35 -04008224 case CMD_XRI_ABORTED_CX:
8225 case CMD_CREATE_XRI_CR: /* Do we expect to use this? */
James Smart4f774512009-05-22 14:52:35 -04008226 case CMD_IOCB_FCP_IBIDIR64_CR: /* bidirectional xfer */
8227 case CMD_FCP_TSEND64_CX: /* Target mode send xfer-ready */
8228 case CMD_FCP_TRSP64_CX: /* Target mode rcv */
8229 case CMD_FCP_AUTO_TRSP_CX: /* Auto target rsp */
8230 default:
8231 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8232 "2014 Invalid command 0x%x\n",
8233 iocbq->iocb.ulpCommand);
8234 return IOCB_ERROR;
James Smart7851fe22011-07-22 18:36:52 -04008235 break;
James Smart4f774512009-05-22 14:52:35 -04008236 }
James Smart6d368e52011-05-24 11:44:12 -04008237
James Smartf0d9bcc2010-10-22 11:07:09 -04008238 bf_set(wqe_xri_tag, &wqe->generic.wqe_com, xritag);
8239 bf_set(wqe_reqtag, &wqe->generic.wqe_com, iocbq->iotag);
8240 wqe->generic.wqe_com.abort_tag = abort_tag;
8241 bf_set(wqe_cmd_type, &wqe->generic.wqe_com, command_type);
8242 bf_set(wqe_cmnd, &wqe->generic.wqe_com, cmnd);
8243 bf_set(wqe_class, &wqe->generic.wqe_com, iocbq->iocb.ulpClass);
8244 bf_set(wqe_cqid, &wqe->generic.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
James Smart4f774512009-05-22 14:52:35 -04008245 return 0;
8246}
8247
8248/**
8249 * __lpfc_sli_issue_iocb_s4 - SLI4 device lockless ver of lpfc_sli_issue_iocb
8250 * @phba: Pointer to HBA context object.
8251 * @ring_number: SLI ring number to issue iocb on.
8252 * @piocb: Pointer to command iocb.
8253 * @flag: Flag indicating if this command can be put into txq.
8254 *
8255 * __lpfc_sli_issue_iocb_s4 is used by other functions in the driver to issue
8256 * an iocb command to an HBA with SLI-4 interface spec.
8257 *
8258 * This function is called with hbalock held. The function will return success
8259 * after it successfully submit the iocb to firmware or after adding to the
8260 * txq.
8261 **/
8262static int
8263__lpfc_sli_issue_iocb_s4(struct lpfc_hba *phba, uint32_t ring_number,
8264 struct lpfc_iocbq *piocb, uint32_t flag)
8265{
8266 struct lpfc_sglq *sglq;
James Smart4f774512009-05-22 14:52:35 -04008267 union lpfc_wqe wqe;
8268 struct lpfc_sli_ring *pring = &phba->sli.ring[ring_number];
James Smart4f774512009-05-22 14:52:35 -04008269
8270 if (piocb->sli4_xritag == NO_XRI) {
8271 if (piocb->iocb.ulpCommand == CMD_ABORT_XRI_CN ||
James Smart6b5151f2012-01-18 16:24:06 -05008272 piocb->iocb.ulpCommand == CMD_CLOSE_XRI_CN)
James Smart4f774512009-05-22 14:52:35 -04008273 sglq = NULL;
8274 else {
James Smart2a9bf3d2010-06-07 15:24:45 -04008275 if (pring->txq_cnt) {
8276 if (!(flag & SLI_IOCB_RET_IOCB)) {
8277 __lpfc_sli_ringtx_put(phba,
8278 pring, piocb);
8279 return IOCB_SUCCESS;
8280 } else {
8281 return IOCB_BUSY;
8282 }
8283 } else {
James Smart6d368e52011-05-24 11:44:12 -04008284 sglq = __lpfc_sli_get_sglq(phba, piocb);
James Smart2a9bf3d2010-06-07 15:24:45 -04008285 if (!sglq) {
8286 if (!(flag & SLI_IOCB_RET_IOCB)) {
8287 __lpfc_sli_ringtx_put(phba,
8288 pring,
8289 piocb);
8290 return IOCB_SUCCESS;
8291 } else
8292 return IOCB_BUSY;
8293 }
8294 }
James Smart4f774512009-05-22 14:52:35 -04008295 }
8296 } else if (piocb->iocb_flag & LPFC_IO_FCP) {
James Smart6d368e52011-05-24 11:44:12 -04008297 /* These IO's already have an XRI and a mapped sgl. */
8298 sglq = NULL;
James Smart4f774512009-05-22 14:52:35 -04008299 } else {
James Smart6d368e52011-05-24 11:44:12 -04008300 /*
8301 * This is a continuation of a commandi,(CX) so this
James Smart4f774512009-05-22 14:52:35 -04008302 * sglq is on the active list
8303 */
8304 sglq = __lpfc_get_active_sglq(phba, piocb->sli4_xritag);
8305 if (!sglq)
8306 return IOCB_ERROR;
8307 }
8308
8309 if (sglq) {
James Smart6d368e52011-05-24 11:44:12 -04008310 piocb->sli4_lxritag = sglq->sli4_lxritag;
James Smart2a9bf3d2010-06-07 15:24:45 -04008311 piocb->sli4_xritag = sglq->sli4_xritag;
James Smart2a9bf3d2010-06-07 15:24:45 -04008312 if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocb, sglq))
James Smart4f774512009-05-22 14:52:35 -04008313 return IOCB_ERROR;
8314 }
8315
8316 if (lpfc_sli4_iocb2wqe(phba, piocb, &wqe))
8317 return IOCB_ERROR;
8318
James Smart341af102010-01-26 23:07:37 -05008319 if ((piocb->iocb_flag & LPFC_IO_FCP) ||
8320 (piocb->iocb_flag & LPFC_USE_FCPWQIDX)) {
James Smart5ffc2662009-11-18 15:39:44 -05008321 /*
8322 * For FCP command IOCB, get a new WQ index to distribute
8323 * WQE across the WQsr. On the other hand, for abort IOCB,
8324 * it carries the same WQ index to the original command
8325 * IOCB.
8326 */
James Smart341af102010-01-26 23:07:37 -05008327 if (piocb->iocb_flag & LPFC_IO_FCP)
James Smart5ffc2662009-11-18 15:39:44 -05008328 piocb->fcp_wqidx = lpfc_sli4_scmd_to_wqidx_distr(phba);
James Smart2e90f4b2011-12-13 13:22:37 -05008329 if (unlikely(!phba->sli4_hba.fcp_wq))
8330 return IOCB_ERROR;
James Smart5ffc2662009-11-18 15:39:44 -05008331 if (lpfc_sli4_wq_put(phba->sli4_hba.fcp_wq[piocb->fcp_wqidx],
8332 &wqe))
James Smart4f774512009-05-22 14:52:35 -04008333 return IOCB_ERROR;
8334 } else {
8335 if (lpfc_sli4_wq_put(phba->sli4_hba.els_wq, &wqe))
8336 return IOCB_ERROR;
8337 }
8338 lpfc_sli_ringtxcmpl_put(phba, pring, piocb);
8339
8340 return 0;
8341}
8342
8343/**
James Smart3772a992009-05-22 14:50:54 -04008344 * __lpfc_sli_issue_iocb - Wrapper func of lockless version for issuing iocb
8345 *
8346 * This routine wraps the actual lockless version for issusing IOCB function
8347 * pointer from the lpfc_hba struct.
8348 *
8349 * Return codes:
8350 * IOCB_ERROR - Error
8351 * IOCB_SUCCESS - Success
8352 * IOCB_BUSY - Busy
8353 **/
James Smart2a9bf3d2010-06-07 15:24:45 -04008354int
James Smart3772a992009-05-22 14:50:54 -04008355__lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
8356 struct lpfc_iocbq *piocb, uint32_t flag)
8357{
8358 return phba->__lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
8359}
8360
8361/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03008362 * lpfc_sli_api_table_setup - Set up sli api function jump table
James Smart3772a992009-05-22 14:50:54 -04008363 * @phba: The hba struct for which this call is being executed.
8364 * @dev_grp: The HBA PCI-Device group number.
8365 *
8366 * This routine sets up the SLI interface API function jump table in @phba
8367 * struct.
8368 * Returns: 0 - success, -ENODEV - failure.
8369 **/
8370int
8371lpfc_sli_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
8372{
8373
8374 switch (dev_grp) {
8375 case LPFC_PCI_DEV_LP:
8376 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s3;
8377 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s3;
8378 break;
James Smart4f774512009-05-22 14:52:35 -04008379 case LPFC_PCI_DEV_OC:
8380 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s4;
8381 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s4;
8382 break;
James Smart3772a992009-05-22 14:50:54 -04008383 default:
8384 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8385 "1419 Invalid HBA PCI-device group: 0x%x\n",
8386 dev_grp);
8387 return -ENODEV;
8388 break;
8389 }
8390 phba->lpfc_get_iocb_from_iocbq = lpfc_get_iocb_from_iocbq;
8391 return 0;
8392}
James Smart92d7f7b2007-06-17 19:56:38 -05008393
James Smarte59058c2008-08-24 21:49:00 -04008394/**
James Smart3621a712009-04-06 18:47:14 -04008395 * lpfc_sli_issue_iocb - Wrapper function for __lpfc_sli_issue_iocb
James Smarte59058c2008-08-24 21:49:00 -04008396 * @phba: Pointer to HBA context object.
8397 * @pring: Pointer to driver SLI ring object.
8398 * @piocb: Pointer to command iocb.
8399 * @flag: Flag indicating if this command can be put into txq.
8400 *
8401 * lpfc_sli_issue_iocb is a wrapper around __lpfc_sli_issue_iocb
8402 * function. This function gets the hbalock and calls
8403 * __lpfc_sli_issue_iocb function and will return the error returned
8404 * by __lpfc_sli_issue_iocb function. This wrapper is used by
8405 * functions which do not hold hbalock.
8406 **/
James Smart92d7f7b2007-06-17 19:56:38 -05008407int
James Smart3772a992009-05-22 14:50:54 -04008408lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
James Smart92d7f7b2007-06-17 19:56:38 -05008409 struct lpfc_iocbq *piocb, uint32_t flag)
8410{
James Smart7e56aa22012-08-03 12:35:34 -04008411 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
James Smart92d7f7b2007-06-17 19:56:38 -05008412 unsigned long iflags;
8413 int rc;
8414
James Smart7e56aa22012-08-03 12:35:34 -04008415 if (phba->sli_rev == LPFC_SLI_REV4) {
8416 pring = &phba->sli.ring[ring_number];
8417 spin_lock_irqsave(&pring->ring_lock, iflags);
8418 rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
8419 spin_unlock_irqrestore(&pring->ring_lock, iflags);
8420 } else {
8421 /* For now, SLI2/3 will still use hbalock */
8422 spin_lock_irqsave(&phba->hbalock, iflags);
8423 rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
8424 spin_unlock_irqrestore(&phba->hbalock, iflags);
8425 }
James Smart92d7f7b2007-06-17 19:56:38 -05008426 return rc;
8427}
8428
James Smarte59058c2008-08-24 21:49:00 -04008429/**
James Smart3621a712009-04-06 18:47:14 -04008430 * lpfc_extra_ring_setup - Extra ring setup function
James Smarte59058c2008-08-24 21:49:00 -04008431 * @phba: Pointer to HBA context object.
8432 *
8433 * This function is called while driver attaches with the
8434 * HBA to setup the extra ring. The extra ring is used
8435 * only when driver needs to support target mode functionality
8436 * or IP over FC functionalities.
8437 *
8438 * This function is called with no lock held.
8439 **/
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05008440static int
8441lpfc_extra_ring_setup( struct lpfc_hba *phba)
8442{
8443 struct lpfc_sli *psli;
8444 struct lpfc_sli_ring *pring;
8445
8446 psli = &phba->sli;
8447
8448 /* Adjust cmd/rsp ring iocb entries more evenly */
James Smarta4bc3372006-12-02 13:34:16 -05008449
8450 /* Take some away from the FCP ring */
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05008451 pring = &psli->ring[psli->fcp_ring];
James Smart7e56aa22012-08-03 12:35:34 -04008452 pring->sli.sli3.numCiocb -= SLI2_IOCB_CMD_R1XTRA_ENTRIES;
8453 pring->sli.sli3.numRiocb -= SLI2_IOCB_RSP_R1XTRA_ENTRIES;
8454 pring->sli.sli3.numCiocb -= SLI2_IOCB_CMD_R3XTRA_ENTRIES;
8455 pring->sli.sli3.numRiocb -= SLI2_IOCB_RSP_R3XTRA_ENTRIES;
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05008456
James Smarta4bc3372006-12-02 13:34:16 -05008457 /* and give them to the extra ring */
8458 pring = &psli->ring[psli->extra_ring];
8459
James Smart7e56aa22012-08-03 12:35:34 -04008460 pring->sli.sli3.numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
8461 pring->sli.sli3.numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
8462 pring->sli.sli3.numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
8463 pring->sli.sli3.numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05008464
8465 /* Setup default profile for this ring */
8466 pring->iotag_max = 4096;
8467 pring->num_mask = 1;
8468 pring->prt[0].profile = 0; /* Mask 0 */
James Smarta4bc3372006-12-02 13:34:16 -05008469 pring->prt[0].rctl = phba->cfg_multi_ring_rctl;
8470 pring->prt[0].type = phba->cfg_multi_ring_type;
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05008471 pring->prt[0].lpfc_sli_rcv_unsol_event = NULL;
8472 return 0;
8473}
8474
James Smartcb69f7d2011-12-13 13:21:57 -05008475/* lpfc_sli_abts_recover_port - Recover a port that failed an ABTS.
8476 * @vport: pointer to virtual port object.
8477 * @ndlp: nodelist pointer for the impacted rport.
8478 *
8479 * The driver calls this routine in response to a XRI ABORT CQE
8480 * event from the port. In this event, the driver is required to
8481 * recover its login to the rport even though its login may be valid
8482 * from the driver's perspective. The failed ABTS notice from the
8483 * port indicates the rport is not responding.
8484 */
8485static void
8486lpfc_sli_abts_recover_port(struct lpfc_vport *vport,
8487 struct lpfc_nodelist *ndlp)
8488{
8489 struct Scsi_Host *shost;
8490 struct lpfc_hba *phba;
8491 unsigned long flags = 0;
8492
8493 shost = lpfc_shost_from_vport(vport);
8494 phba = vport->phba;
8495 if (ndlp->nlp_state != NLP_STE_MAPPED_NODE) {
8496 lpfc_printf_log(phba, KERN_INFO,
8497 LOG_SLI, "3093 No rport recovery needed. "
8498 "rport in state 0x%x\n",
8499 ndlp->nlp_state);
8500 return;
8501 }
8502 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8503 "3094 Start rport recovery on shost id 0x%x "
8504 "fc_id 0x%06x vpi 0x%x rpi 0x%x state 0x%x "
8505 "flags 0x%x\n",
8506 shost->host_no, ndlp->nlp_DID,
8507 vport->vpi, ndlp->nlp_rpi, ndlp->nlp_state,
8508 ndlp->nlp_flag);
8509 /*
8510 * The rport is not responding. Don't attempt ADISC recovery.
8511 * Remove the FCP-2 flag to force a PLOGI.
8512 */
8513 spin_lock_irqsave(shost->host_lock, flags);
8514 ndlp->nlp_fcp_info &= ~NLP_FCP_2_DEVICE;
8515 spin_unlock_irqrestore(shost->host_lock, flags);
8516 lpfc_disc_state_machine(vport, ndlp, NULL,
8517 NLP_EVT_DEVICE_RECOVERY);
8518 lpfc_cancel_retry_delay_tmo(vport, ndlp);
8519 spin_lock_irqsave(shost->host_lock, flags);
8520 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
8521 spin_unlock_irqrestore(shost->host_lock, flags);
8522 lpfc_disc_start(vport);
8523}
8524
8525/* lpfc_sli_abts_err_handler - handle a failed ABTS request from an SLI3 port.
8526 * @phba: Pointer to HBA context object.
8527 * @iocbq: Pointer to iocb object.
8528 *
8529 * The async_event handler calls this routine when it receives
8530 * an ASYNC_STATUS_CN event from the port. The port generates
8531 * this event when an Abort Sequence request to an rport fails
8532 * twice in succession. The abort could be originated by the
8533 * driver or by the port. The ABTS could have been for an ELS
8534 * or FCP IO. The port only generates this event when an ABTS
8535 * fails to complete after one retry.
8536 */
8537static void
8538lpfc_sli_abts_err_handler(struct lpfc_hba *phba,
8539 struct lpfc_iocbq *iocbq)
8540{
8541 struct lpfc_nodelist *ndlp = NULL;
8542 uint16_t rpi = 0, vpi = 0;
8543 struct lpfc_vport *vport = NULL;
8544
8545 /* The rpi in the ulpContext is vport-sensitive. */
8546 vpi = iocbq->iocb.un.asyncstat.sub_ctxt_tag;
8547 rpi = iocbq->iocb.ulpContext;
8548
8549 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
8550 "3092 Port generated ABTS async event "
8551 "on vpi %d rpi %d status 0x%x\n",
8552 vpi, rpi, iocbq->iocb.ulpStatus);
8553
8554 vport = lpfc_find_vport_by_vpid(phba, vpi);
8555 if (!vport)
8556 goto err_exit;
8557 ndlp = lpfc_findnode_rpi(vport, rpi);
8558 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp))
8559 goto err_exit;
8560
8561 if (iocbq->iocb.ulpStatus == IOSTAT_LOCAL_REJECT)
8562 lpfc_sli_abts_recover_port(vport, ndlp);
8563 return;
8564
8565 err_exit:
8566 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
8567 "3095 Event Context not found, no "
8568 "action on vpi %d rpi %d status 0x%x, reason 0x%x\n",
8569 iocbq->iocb.ulpContext, iocbq->iocb.ulpStatus,
8570 vpi, rpi);
8571}
8572
8573/* lpfc_sli4_abts_err_handler - handle a failed ABTS request from an SLI4 port.
8574 * @phba: pointer to HBA context object.
8575 * @ndlp: nodelist pointer for the impacted rport.
8576 * @axri: pointer to the wcqe containing the failed exchange.
8577 *
8578 * The driver calls this routine when it receives an ABORT_XRI_FCP CQE from the
8579 * port. The port generates this event when an abort exchange request to an
8580 * rport fails twice in succession with no reply. The abort could be originated
8581 * by the driver or by the port. The ABTS could have been for an ELS or FCP IO.
8582 */
8583void
8584lpfc_sli4_abts_err_handler(struct lpfc_hba *phba,
8585 struct lpfc_nodelist *ndlp,
8586 struct sli4_wcqe_xri_aborted *axri)
8587{
8588 struct lpfc_vport *vport;
James Smart5c1db2a2012-03-01 22:34:36 -05008589 uint32_t ext_status = 0;
James Smartcb69f7d2011-12-13 13:21:57 -05008590
James Smart6b5151f2012-01-18 16:24:06 -05008591 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
James Smartcb69f7d2011-12-13 13:21:57 -05008592 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
8593 "3115 Node Context not found, driver "
8594 "ignoring abts err event\n");
James Smart6b5151f2012-01-18 16:24:06 -05008595 return;
8596 }
8597
James Smartcb69f7d2011-12-13 13:21:57 -05008598 vport = ndlp->vport;
8599 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
8600 "3116 Port generated FCP XRI ABORT event on "
James Smart5c1db2a2012-03-01 22:34:36 -05008601 "vpi %d rpi %d xri x%x status 0x%x parameter x%x\n",
James Smartcb69f7d2011-12-13 13:21:57 -05008602 ndlp->vport->vpi, ndlp->nlp_rpi,
8603 bf_get(lpfc_wcqe_xa_xri, axri),
James Smart5c1db2a2012-03-01 22:34:36 -05008604 bf_get(lpfc_wcqe_xa_status, axri),
8605 axri->parameter);
James Smartcb69f7d2011-12-13 13:21:57 -05008606
James Smart5c1db2a2012-03-01 22:34:36 -05008607 /*
8608 * Catch the ABTS protocol failure case. Older OCe FW releases returned
8609 * LOCAL_REJECT and 0 for a failed ABTS exchange and later OCe and
8610 * LPe FW releases returned LOCAL_REJECT and SEQUENCE_TIMEOUT.
8611 */
8612 ext_status = axri->parameter & WCQE_PARAM_MASK;
8613 if ((bf_get(lpfc_wcqe_xa_status, axri) == IOSTAT_LOCAL_REJECT) &&
8614 ((ext_status == IOERR_SEQUENCE_TIMEOUT) || (ext_status == 0)))
James Smartcb69f7d2011-12-13 13:21:57 -05008615 lpfc_sli_abts_recover_port(vport, ndlp);
8616}
8617
James Smarte59058c2008-08-24 21:49:00 -04008618/**
James Smart3621a712009-04-06 18:47:14 -04008619 * lpfc_sli_async_event_handler - ASYNC iocb handler function
James Smarte59058c2008-08-24 21:49:00 -04008620 * @phba: Pointer to HBA context object.
8621 * @pring: Pointer to driver SLI ring object.
8622 * @iocbq: Pointer to iocb object.
8623 *
8624 * This function is called by the slow ring event handler
8625 * function when there is an ASYNC event iocb in the ring.
8626 * This function is called with no lock held.
8627 * Currently this function handles only temperature related
8628 * ASYNC events. The function decodes the temperature sensor
8629 * event message and posts events for the management applications.
8630 **/
James Smart98c9ea52007-10-27 13:37:33 -04008631static void
James Smart57127f12007-10-27 13:37:05 -04008632lpfc_sli_async_event_handler(struct lpfc_hba * phba,
8633 struct lpfc_sli_ring * pring, struct lpfc_iocbq * iocbq)
8634{
8635 IOCB_t *icmd;
8636 uint16_t evt_code;
James Smart57127f12007-10-27 13:37:05 -04008637 struct temp_event temp_event_data;
8638 struct Scsi_Host *shost;
James Smarta257bf92009-04-06 18:48:10 -04008639 uint32_t *iocb_w;
James Smart57127f12007-10-27 13:37:05 -04008640
8641 icmd = &iocbq->iocb;
8642 evt_code = icmd->un.asyncstat.evt_code;
James Smart57127f12007-10-27 13:37:05 -04008643
James Smartcb69f7d2011-12-13 13:21:57 -05008644 switch (evt_code) {
8645 case ASYNC_TEMP_WARN:
8646 case ASYNC_TEMP_SAFE:
8647 temp_event_data.data = (uint32_t) icmd->ulpContext;
8648 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
8649 if (evt_code == ASYNC_TEMP_WARN) {
8650 temp_event_data.event_code = LPFC_THRESHOLD_TEMP;
8651 lpfc_printf_log(phba, KERN_ERR, LOG_TEMP,
8652 "0347 Adapter is very hot, please take "
8653 "corrective action. temperature : %d Celsius\n",
8654 (uint32_t) icmd->ulpContext);
8655 } else {
8656 temp_event_data.event_code = LPFC_NORMAL_TEMP;
8657 lpfc_printf_log(phba, KERN_ERR, LOG_TEMP,
8658 "0340 Adapter temperature is OK now. "
8659 "temperature : %d Celsius\n",
8660 (uint32_t) icmd->ulpContext);
8661 }
8662
8663 /* Send temperature change event to applications */
8664 shost = lpfc_shost_from_vport(phba->pport);
8665 fc_host_post_vendor_event(shost, fc_get_event_number(),
8666 sizeof(temp_event_data), (char *) &temp_event_data,
8667 LPFC_NL_VENDOR_ID);
8668 break;
8669 case ASYNC_STATUS_CN:
8670 lpfc_sli_abts_err_handler(phba, iocbq);
8671 break;
8672 default:
James Smarta257bf92009-04-06 18:48:10 -04008673 iocb_w = (uint32_t *) icmd;
James Smartcb69f7d2011-12-13 13:21:57 -05008674 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart76bb24e2007-10-27 13:38:00 -04008675 "0346 Ring %d handler: unexpected ASYNC_STATUS"
James Smarte4e74272009-07-19 10:01:38 -04008676 " evt_code 0x%x\n"
James Smarta257bf92009-04-06 18:48:10 -04008677 "W0 0x%08x W1 0x%08x W2 0x%08x W3 0x%08x\n"
8678 "W4 0x%08x W5 0x%08x W6 0x%08x W7 0x%08x\n"
8679 "W8 0x%08x W9 0x%08x W10 0x%08x W11 0x%08x\n"
8680 "W12 0x%08x W13 0x%08x W14 0x%08x W15 0x%08x\n",
James Smartcb69f7d2011-12-13 13:21:57 -05008681 pring->ringno, icmd->un.asyncstat.evt_code,
James Smarta257bf92009-04-06 18:48:10 -04008682 iocb_w[0], iocb_w[1], iocb_w[2], iocb_w[3],
8683 iocb_w[4], iocb_w[5], iocb_w[6], iocb_w[7],
8684 iocb_w[8], iocb_w[9], iocb_w[10], iocb_w[11],
8685 iocb_w[12], iocb_w[13], iocb_w[14], iocb_w[15]);
8686
James Smartcb69f7d2011-12-13 13:21:57 -05008687 break;
James Smart57127f12007-10-27 13:37:05 -04008688 }
James Smart57127f12007-10-27 13:37:05 -04008689}
8690
8691
James Smarte59058c2008-08-24 21:49:00 -04008692/**
James Smart3621a712009-04-06 18:47:14 -04008693 * lpfc_sli_setup - SLI ring setup function
James Smarte59058c2008-08-24 21:49:00 -04008694 * @phba: Pointer to HBA context object.
8695 *
8696 * lpfc_sli_setup sets up rings of the SLI interface with
8697 * number of iocbs per ring and iotags. This function is
8698 * called while driver attach to the HBA and before the
8699 * interrupts are enabled. So there is no need for locking.
8700 *
8701 * This function always returns 0.
8702 **/
dea31012005-04-17 16:05:31 -05008703int
8704lpfc_sli_setup(struct lpfc_hba *phba)
8705{
James Smarted957682007-06-17 19:56:37 -05008706 int i, totiocbsize = 0;
dea31012005-04-17 16:05:31 -05008707 struct lpfc_sli *psli = &phba->sli;
8708 struct lpfc_sli_ring *pring;
8709
8710 psli->num_rings = MAX_CONFIGURED_RINGS;
8711 psli->sli_flag = 0;
8712 psli->fcp_ring = LPFC_FCP_RING;
8713 psli->next_ring = LPFC_FCP_NEXT_RING;
James Smarta4bc3372006-12-02 13:34:16 -05008714 psli->extra_ring = LPFC_EXTRA_RING;
dea31012005-04-17 16:05:31 -05008715
James Bottomley604a3e32005-10-29 10:28:33 -05008716 psli->iocbq_lookup = NULL;
8717 psli->iocbq_lookup_len = 0;
8718 psli->last_iotag = 0;
8719
dea31012005-04-17 16:05:31 -05008720 for (i = 0; i < psli->num_rings; i++) {
8721 pring = &psli->ring[i];
8722 switch (i) {
8723 case LPFC_FCP_RING: /* ring 0 - FCP */
8724 /* numCiocb and numRiocb are used in config_port */
James Smart7e56aa22012-08-03 12:35:34 -04008725 pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R0_ENTRIES;
8726 pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R0_ENTRIES;
8727 pring->sli.sli3.numCiocb +=
8728 SLI2_IOCB_CMD_R1XTRA_ENTRIES;
8729 pring->sli.sli3.numRiocb +=
8730 SLI2_IOCB_RSP_R1XTRA_ENTRIES;
8731 pring->sli.sli3.numCiocb +=
8732 SLI2_IOCB_CMD_R3XTRA_ENTRIES;
8733 pring->sli.sli3.numRiocb +=
8734 SLI2_IOCB_RSP_R3XTRA_ENTRIES;
8735 pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05008736 SLI3_IOCB_CMD_SIZE :
8737 SLI2_IOCB_CMD_SIZE;
James Smart7e56aa22012-08-03 12:35:34 -04008738 pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05008739 SLI3_IOCB_RSP_SIZE :
8740 SLI2_IOCB_RSP_SIZE;
dea31012005-04-17 16:05:31 -05008741 pring->iotag_ctr = 0;
8742 pring->iotag_max =
James Smart92d7f7b2007-06-17 19:56:38 -05008743 (phba->cfg_hba_queue_depth * 2);
dea31012005-04-17 16:05:31 -05008744 pring->fast_iotag = pring->iotag_max;
8745 pring->num_mask = 0;
8746 break;
James Smarta4bc3372006-12-02 13:34:16 -05008747 case LPFC_EXTRA_RING: /* ring 1 - EXTRA */
dea31012005-04-17 16:05:31 -05008748 /* numCiocb and numRiocb are used in config_port */
James Smart7e56aa22012-08-03 12:35:34 -04008749 pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R1_ENTRIES;
8750 pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R1_ENTRIES;
8751 pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05008752 SLI3_IOCB_CMD_SIZE :
8753 SLI2_IOCB_CMD_SIZE;
James Smart7e56aa22012-08-03 12:35:34 -04008754 pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05008755 SLI3_IOCB_RSP_SIZE :
8756 SLI2_IOCB_RSP_SIZE;
James Smart2e0fef82007-06-17 19:56:36 -05008757 pring->iotag_max = phba->cfg_hba_queue_depth;
dea31012005-04-17 16:05:31 -05008758 pring->num_mask = 0;
8759 break;
8760 case LPFC_ELS_RING: /* ring 2 - ELS / CT */
8761 /* numCiocb and numRiocb are used in config_port */
James Smart7e56aa22012-08-03 12:35:34 -04008762 pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R2_ENTRIES;
8763 pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R2_ENTRIES;
8764 pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05008765 SLI3_IOCB_CMD_SIZE :
8766 SLI2_IOCB_CMD_SIZE;
James Smart7e56aa22012-08-03 12:35:34 -04008767 pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05008768 SLI3_IOCB_RSP_SIZE :
8769 SLI2_IOCB_RSP_SIZE;
dea31012005-04-17 16:05:31 -05008770 pring->fast_iotag = 0;
8771 pring->iotag_ctr = 0;
8772 pring->iotag_max = 4096;
James Smart57127f12007-10-27 13:37:05 -04008773 pring->lpfc_sli_rcv_async_status =
8774 lpfc_sli_async_event_handler;
James Smart6669f9b2009-10-02 15:16:45 -04008775 pring->num_mask = LPFC_MAX_RING_MASK;
dea31012005-04-17 16:05:31 -05008776 pring->prt[0].profile = 0; /* Mask 0 */
James Smart6a9c52c2009-10-02 15:16:51 -04008777 pring->prt[0].rctl = FC_RCTL_ELS_REQ;
8778 pring->prt[0].type = FC_TYPE_ELS;
dea31012005-04-17 16:05:31 -05008779 pring->prt[0].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05008780 lpfc_els_unsol_event;
dea31012005-04-17 16:05:31 -05008781 pring->prt[1].profile = 0; /* Mask 1 */
James Smart6a9c52c2009-10-02 15:16:51 -04008782 pring->prt[1].rctl = FC_RCTL_ELS_REP;
8783 pring->prt[1].type = FC_TYPE_ELS;
dea31012005-04-17 16:05:31 -05008784 pring->prt[1].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05008785 lpfc_els_unsol_event;
dea31012005-04-17 16:05:31 -05008786 pring->prt[2].profile = 0; /* Mask 2 */
8787 /* NameServer Inquiry */
James Smart6a9c52c2009-10-02 15:16:51 -04008788 pring->prt[2].rctl = FC_RCTL_DD_UNSOL_CTL;
dea31012005-04-17 16:05:31 -05008789 /* NameServer */
James Smart6a9c52c2009-10-02 15:16:51 -04008790 pring->prt[2].type = FC_TYPE_CT;
dea31012005-04-17 16:05:31 -05008791 pring->prt[2].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05008792 lpfc_ct_unsol_event;
dea31012005-04-17 16:05:31 -05008793 pring->prt[3].profile = 0; /* Mask 3 */
8794 /* NameServer response */
James Smart6a9c52c2009-10-02 15:16:51 -04008795 pring->prt[3].rctl = FC_RCTL_DD_SOL_CTL;
dea31012005-04-17 16:05:31 -05008796 /* NameServer */
James Smart6a9c52c2009-10-02 15:16:51 -04008797 pring->prt[3].type = FC_TYPE_CT;
dea31012005-04-17 16:05:31 -05008798 pring->prt[3].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05008799 lpfc_ct_unsol_event;
James Smart6669f9b2009-10-02 15:16:45 -04008800 /* abort unsolicited sequence */
8801 pring->prt[4].profile = 0; /* Mask 4 */
8802 pring->prt[4].rctl = FC_RCTL_BA_ABTS;
8803 pring->prt[4].type = FC_TYPE_BLS;
8804 pring->prt[4].lpfc_sli_rcv_unsol_event =
8805 lpfc_sli4_ct_abort_unsol_event;
dea31012005-04-17 16:05:31 -05008806 break;
8807 }
James Smart7e56aa22012-08-03 12:35:34 -04008808 totiocbsize += (pring->sli.sli3.numCiocb *
8809 pring->sli.sli3.sizeCiocb) +
8810 (pring->sli.sli3.numRiocb * pring->sli.sli3.sizeRiocb);
dea31012005-04-17 16:05:31 -05008811 }
James Smarted957682007-06-17 19:56:37 -05008812 if (totiocbsize > MAX_SLIM_IOCB_SIZE) {
dea31012005-04-17 16:05:31 -05008813 /* Too many cmd / rsp ring entries in SLI2 SLIM */
James Smarte8b62012007-08-02 11:10:09 -04008814 printk(KERN_ERR "%d:0462 Too many cmd / rsp ring entries in "
8815 "SLI2 SLIM Data: x%x x%lx\n",
8816 phba->brd_no, totiocbsize,
8817 (unsigned long) MAX_SLIM_IOCB_SIZE);
dea31012005-04-17 16:05:31 -05008818 }
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05008819 if (phba->cfg_multi_ring_support == 2)
8820 lpfc_extra_ring_setup(phba);
dea31012005-04-17 16:05:31 -05008821
8822 return 0;
8823}
8824
James Smarte59058c2008-08-24 21:49:00 -04008825/**
James Smart3621a712009-04-06 18:47:14 -04008826 * lpfc_sli_queue_setup - Queue initialization function
James Smarte59058c2008-08-24 21:49:00 -04008827 * @phba: Pointer to HBA context object.
8828 *
8829 * lpfc_sli_queue_setup sets up mailbox queues and iocb queues for each
8830 * ring. This function also initializes ring indices of each ring.
8831 * This function is called during the initialization of the SLI
8832 * interface of an HBA.
8833 * This function is called with no lock held and always returns
8834 * 1.
8835 **/
dea31012005-04-17 16:05:31 -05008836int
James Smart2e0fef82007-06-17 19:56:36 -05008837lpfc_sli_queue_setup(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05008838{
8839 struct lpfc_sli *psli;
8840 struct lpfc_sli_ring *pring;
James Bottomley604a3e32005-10-29 10:28:33 -05008841 int i;
dea31012005-04-17 16:05:31 -05008842
8843 psli = &phba->sli;
James Smart2e0fef82007-06-17 19:56:36 -05008844 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05008845 INIT_LIST_HEAD(&psli->mboxq);
James Smart92d7f7b2007-06-17 19:56:38 -05008846 INIT_LIST_HEAD(&psli->mboxq_cmpl);
dea31012005-04-17 16:05:31 -05008847 /* Initialize list headers for txq and txcmplq as double linked lists */
8848 for (i = 0; i < psli->num_rings; i++) {
8849 pring = &psli->ring[i];
8850 pring->ringno = i;
James Smart7e56aa22012-08-03 12:35:34 -04008851 pring->sli.sli3.next_cmdidx = 0;
8852 pring->sli.sli3.local_getidx = 0;
8853 pring->sli.sli3.cmdidx = 0;
dea31012005-04-17 16:05:31 -05008854 INIT_LIST_HEAD(&pring->txq);
8855 INIT_LIST_HEAD(&pring->txcmplq);
8856 INIT_LIST_HEAD(&pring->iocb_continueq);
James Smart9c2face2008-01-11 01:53:18 -05008857 INIT_LIST_HEAD(&pring->iocb_continue_saveq);
dea31012005-04-17 16:05:31 -05008858 INIT_LIST_HEAD(&pring->postbufq);
James Smart7e56aa22012-08-03 12:35:34 -04008859 spin_lock_init(&pring->ring_lock);
dea31012005-04-17 16:05:31 -05008860 }
James Smart2e0fef82007-06-17 19:56:36 -05008861 spin_unlock_irq(&phba->hbalock);
8862 return 1;
dea31012005-04-17 16:05:31 -05008863}
8864
James Smarte59058c2008-08-24 21:49:00 -04008865/**
James Smart04c68492009-05-22 14:52:52 -04008866 * lpfc_sli_mbox_sys_flush - Flush mailbox command sub-system
8867 * @phba: Pointer to HBA context object.
8868 *
8869 * This routine flushes the mailbox command subsystem. It will unconditionally
8870 * flush all the mailbox commands in the three possible stages in the mailbox
8871 * command sub-system: pending mailbox command queue; the outstanding mailbox
8872 * command; and completed mailbox command queue. It is caller's responsibility
8873 * to make sure that the driver is in the proper state to flush the mailbox
8874 * command sub-system. Namely, the posting of mailbox commands into the
8875 * pending mailbox command queue from the various clients must be stopped;
8876 * either the HBA is in a state that it will never works on the outstanding
8877 * mailbox command (such as in EEH or ERATT conditions) or the outstanding
8878 * mailbox command has been completed.
8879 **/
8880static void
8881lpfc_sli_mbox_sys_flush(struct lpfc_hba *phba)
8882{
8883 LIST_HEAD(completions);
8884 struct lpfc_sli *psli = &phba->sli;
8885 LPFC_MBOXQ_t *pmb;
8886 unsigned long iflag;
8887
8888 /* Flush all the mailbox commands in the mbox system */
8889 spin_lock_irqsave(&phba->hbalock, iflag);
8890 /* The pending mailbox command queue */
8891 list_splice_init(&phba->sli.mboxq, &completions);
8892 /* The outstanding active mailbox command */
8893 if (psli->mbox_active) {
8894 list_add_tail(&psli->mbox_active->list, &completions);
8895 psli->mbox_active = NULL;
8896 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
8897 }
8898 /* The completed mailbox command queue */
8899 list_splice_init(&phba->sli.mboxq_cmpl, &completions);
8900 spin_unlock_irqrestore(&phba->hbalock, iflag);
8901
8902 /* Return all flushed mailbox commands with MBX_NOT_FINISHED status */
8903 while (!list_empty(&completions)) {
8904 list_remove_head(&completions, pmb, LPFC_MBOXQ_t, list);
8905 pmb->u.mb.mbxStatus = MBX_NOT_FINISHED;
8906 if (pmb->mbox_cmpl)
8907 pmb->mbox_cmpl(phba, pmb);
8908 }
8909}
8910
8911/**
James Smart3621a712009-04-06 18:47:14 -04008912 * lpfc_sli_host_down - Vport cleanup function
James Smarte59058c2008-08-24 21:49:00 -04008913 * @vport: Pointer to virtual port object.
8914 *
8915 * lpfc_sli_host_down is called to clean up the resources
8916 * associated with a vport before destroying virtual
8917 * port data structures.
8918 * This function does following operations:
8919 * - Free discovery resources associated with this virtual
8920 * port.
8921 * - Free iocbs associated with this virtual port in
8922 * the txq.
8923 * - Send abort for all iocb commands associated with this
8924 * vport in txcmplq.
8925 *
8926 * This function is called with no lock held and always returns 1.
8927 **/
dea31012005-04-17 16:05:31 -05008928int
James Smart92d7f7b2007-06-17 19:56:38 -05008929lpfc_sli_host_down(struct lpfc_vport *vport)
8930{
James Smart858c9f62007-06-17 19:56:39 -05008931 LIST_HEAD(completions);
James Smart92d7f7b2007-06-17 19:56:38 -05008932 struct lpfc_hba *phba = vport->phba;
8933 struct lpfc_sli *psli = &phba->sli;
8934 struct lpfc_sli_ring *pring;
8935 struct lpfc_iocbq *iocb, *next_iocb;
James Smart92d7f7b2007-06-17 19:56:38 -05008936 int i;
8937 unsigned long flags = 0;
8938 uint16_t prev_pring_flag;
8939
8940 lpfc_cleanup_discovery_resources(vport);
8941
8942 spin_lock_irqsave(&phba->hbalock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -05008943 for (i = 0; i < psli->num_rings; i++) {
8944 pring = &psli->ring[i];
8945 prev_pring_flag = pring->flag;
James Smart5e9d9b82008-06-14 22:52:53 -04008946 /* Only slow rings */
8947 if (pring->ringno == LPFC_ELS_RING) {
James Smart858c9f62007-06-17 19:56:39 -05008948 pring->flag |= LPFC_DEFERRED_RING_EVENT;
James Smart5e9d9b82008-06-14 22:52:53 -04008949 /* Set the lpfc data pending flag */
8950 set_bit(LPFC_DATA_READY, &phba->data_flags);
8951 }
James Smart92d7f7b2007-06-17 19:56:38 -05008952 /*
8953 * Error everything on the txq since these iocbs have not been
8954 * given to the FW yet.
8955 */
James Smart92d7f7b2007-06-17 19:56:38 -05008956 list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
8957 if (iocb->vport != vport)
8958 continue;
James Smart858c9f62007-06-17 19:56:39 -05008959 list_move_tail(&iocb->list, &completions);
James Smart92d7f7b2007-06-17 19:56:38 -05008960 pring->txq_cnt--;
James Smart92d7f7b2007-06-17 19:56:38 -05008961 }
8962
8963 /* Next issue ABTS for everything on the txcmplq */
8964 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq,
8965 list) {
8966 if (iocb->vport != vport)
8967 continue;
8968 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
8969 }
8970
8971 pring->flag = prev_pring_flag;
8972 }
8973
8974 spin_unlock_irqrestore(&phba->hbalock, flags);
8975
James Smarta257bf92009-04-06 18:48:10 -04008976 /* Cancel all the IOCBs from the completions list */
8977 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
8978 IOERR_SLI_DOWN);
James Smart92d7f7b2007-06-17 19:56:38 -05008979 return 1;
8980}
8981
James Smarte59058c2008-08-24 21:49:00 -04008982/**
James Smart3621a712009-04-06 18:47:14 -04008983 * lpfc_sli_hba_down - Resource cleanup function for the HBA
James Smarte59058c2008-08-24 21:49:00 -04008984 * @phba: Pointer to HBA context object.
8985 *
8986 * This function cleans up all iocb, buffers, mailbox commands
8987 * while shutting down the HBA. This function is called with no
8988 * lock held and always returns 1.
8989 * This function does the following to cleanup driver resources:
8990 * - Free discovery resources for each virtual port
8991 * - Cleanup any pending fabric iocbs
8992 * - Iterate through the iocb txq and free each entry
8993 * in the list.
8994 * - Free up any buffer posted to the HBA
8995 * - Free mailbox commands in the mailbox queue.
8996 **/
James Smart92d7f7b2007-06-17 19:56:38 -05008997int
James Smart2e0fef82007-06-17 19:56:36 -05008998lpfc_sli_hba_down(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05008999{
James Smart2534ba72007-04-25 09:52:20 -04009000 LIST_HEAD(completions);
James Smart2e0fef82007-06-17 19:56:36 -05009001 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05009002 struct lpfc_sli_ring *pring;
James Smart0ff10d42008-01-11 01:52:36 -05009003 struct lpfc_dmabuf *buf_ptr;
dea31012005-04-17 16:05:31 -05009004 unsigned long flags = 0;
James Smart04c68492009-05-22 14:52:52 -04009005 int i;
9006
9007 /* Shutdown the mailbox command sub-system */
James Smart618a5232012-06-12 13:54:36 -04009008 lpfc_sli_mbox_sys_shutdown(phba, LPFC_MBX_WAIT);
dea31012005-04-17 16:05:31 -05009009
dea31012005-04-17 16:05:31 -05009010 lpfc_hba_down_prep(phba);
9011
James Smart92d7f7b2007-06-17 19:56:38 -05009012 lpfc_fabric_abort_hba(phba);
9013
James Smart2e0fef82007-06-17 19:56:36 -05009014 spin_lock_irqsave(&phba->hbalock, flags);
dea31012005-04-17 16:05:31 -05009015 for (i = 0; i < psli->num_rings; i++) {
9016 pring = &psli->ring[i];
James Smart5e9d9b82008-06-14 22:52:53 -04009017 /* Only slow rings */
9018 if (pring->ringno == LPFC_ELS_RING) {
James Smart858c9f62007-06-17 19:56:39 -05009019 pring->flag |= LPFC_DEFERRED_RING_EVENT;
James Smart5e9d9b82008-06-14 22:52:53 -04009020 /* Set the lpfc data pending flag */
9021 set_bit(LPFC_DATA_READY, &phba->data_flags);
9022 }
dea31012005-04-17 16:05:31 -05009023
9024 /*
9025 * Error everything on the txq since these iocbs have not been
9026 * given to the FW yet.
9027 */
James Smart2534ba72007-04-25 09:52:20 -04009028 list_splice_init(&pring->txq, &completions);
dea31012005-04-17 16:05:31 -05009029 pring->txq_cnt = 0;
9030
dea31012005-04-17 16:05:31 -05009031 }
James Smart2e0fef82007-06-17 19:56:36 -05009032 spin_unlock_irqrestore(&phba->hbalock, flags);
dea31012005-04-17 16:05:31 -05009033
James Smarta257bf92009-04-06 18:48:10 -04009034 /* Cancel all the IOCBs from the completions list */
9035 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
9036 IOERR_SLI_DOWN);
James Smart2534ba72007-04-25 09:52:20 -04009037
James Smart0ff10d42008-01-11 01:52:36 -05009038 spin_lock_irqsave(&phba->hbalock, flags);
9039 list_splice_init(&phba->elsbuf, &completions);
9040 phba->elsbuf_cnt = 0;
9041 phba->elsbuf_prev_cnt = 0;
9042 spin_unlock_irqrestore(&phba->hbalock, flags);
9043
9044 while (!list_empty(&completions)) {
9045 list_remove_head(&completions, buf_ptr,
9046 struct lpfc_dmabuf, list);
9047 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
9048 kfree(buf_ptr);
9049 }
9050
dea31012005-04-17 16:05:31 -05009051 /* Return any active mbox cmds */
9052 del_timer_sync(&psli->mbox_tmo);
James Smart92d7f7b2007-06-17 19:56:38 -05009053
James Smartda0436e2009-05-22 14:51:39 -04009054 spin_lock_irqsave(&phba->pport->work_port_lock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -05009055 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
James Smartda0436e2009-05-22 14:51:39 -04009056 spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -05009057
James Smartda0436e2009-05-22 14:51:39 -04009058 return 1;
9059}
James Smart92d7f7b2007-06-17 19:56:38 -05009060
James Smartda0436e2009-05-22 14:51:39 -04009061/**
James Smart3621a712009-04-06 18:47:14 -04009062 * lpfc_sli_pcimem_bcopy - SLI memory copy function
James Smarte59058c2008-08-24 21:49:00 -04009063 * @srcp: Source memory pointer.
9064 * @destp: Destination memory pointer.
9065 * @cnt: Number of words required to be copied.
9066 *
9067 * This function is used for copying data between driver memory
9068 * and the SLI memory. This function also changes the endianness
9069 * of each word if native endianness is different from SLI
9070 * endianness. This function can be called with or without
9071 * lock.
9072 **/
dea31012005-04-17 16:05:31 -05009073void
9074lpfc_sli_pcimem_bcopy(void *srcp, void *destp, uint32_t cnt)
9075{
9076 uint32_t *src = srcp;
9077 uint32_t *dest = destp;
9078 uint32_t ldata;
9079 int i;
9080
9081 for (i = 0; i < (int)cnt; i += sizeof (uint32_t)) {
9082 ldata = *src;
9083 ldata = le32_to_cpu(ldata);
9084 *dest = ldata;
9085 src++;
9086 dest++;
9087 }
9088}
9089
James Smarte59058c2008-08-24 21:49:00 -04009090
9091/**
James Smarta0c87cb2009-07-19 10:01:10 -04009092 * lpfc_sli_bemem_bcopy - SLI memory copy function
9093 * @srcp: Source memory pointer.
9094 * @destp: Destination memory pointer.
9095 * @cnt: Number of words required to be copied.
9096 *
9097 * This function is used for copying data between a data structure
9098 * with big endian representation to local endianness.
9099 * This function can be called with or without lock.
9100 **/
9101void
9102lpfc_sli_bemem_bcopy(void *srcp, void *destp, uint32_t cnt)
9103{
9104 uint32_t *src = srcp;
9105 uint32_t *dest = destp;
9106 uint32_t ldata;
9107 int i;
9108
9109 for (i = 0; i < (int)cnt; i += sizeof(uint32_t)) {
9110 ldata = *src;
9111 ldata = be32_to_cpu(ldata);
9112 *dest = ldata;
9113 src++;
9114 dest++;
9115 }
9116}
9117
9118/**
James Smart3621a712009-04-06 18:47:14 -04009119 * lpfc_sli_ringpostbuf_put - Function to add a buffer to postbufq
James Smarte59058c2008-08-24 21:49:00 -04009120 * @phba: Pointer to HBA context object.
9121 * @pring: Pointer to driver SLI ring object.
9122 * @mp: Pointer to driver buffer object.
9123 *
9124 * This function is called with no lock held.
9125 * It always return zero after adding the buffer to the postbufq
9126 * buffer list.
9127 **/
dea31012005-04-17 16:05:31 -05009128int
James Smart2e0fef82007-06-17 19:56:36 -05009129lpfc_sli_ringpostbuf_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
9130 struct lpfc_dmabuf *mp)
dea31012005-04-17 16:05:31 -05009131{
9132 /* Stick struct lpfc_dmabuf at end of postbufq so driver can look it up
9133 later */
James Smart2e0fef82007-06-17 19:56:36 -05009134 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05009135 list_add_tail(&mp->list, &pring->postbufq);
dea31012005-04-17 16:05:31 -05009136 pring->postbufq_cnt++;
James Smart2e0fef82007-06-17 19:56:36 -05009137 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05009138 return 0;
9139}
9140
James Smarte59058c2008-08-24 21:49:00 -04009141/**
James Smart3621a712009-04-06 18:47:14 -04009142 * lpfc_sli_get_buffer_tag - allocates a tag for a CMD_QUE_XRI64_CX buffer
James Smarte59058c2008-08-24 21:49:00 -04009143 * @phba: Pointer to HBA context object.
9144 *
9145 * When HBQ is enabled, buffers are searched based on tags. This function
9146 * allocates a tag for buffer posted using CMD_QUE_XRI64_CX iocb. The
9147 * tag is bit wise or-ed with QUE_BUFTAG_BIT to make sure that the tag
9148 * does not conflict with tags of buffer posted for unsolicited events.
9149 * The function returns the allocated tag. The function is called with
9150 * no locks held.
9151 **/
James Smart76bb24e2007-10-27 13:38:00 -04009152uint32_t
9153lpfc_sli_get_buffer_tag(struct lpfc_hba *phba)
9154{
9155 spin_lock_irq(&phba->hbalock);
9156 phba->buffer_tag_count++;
9157 /*
9158 * Always set the QUE_BUFTAG_BIT to distiguish between
9159 * a tag assigned by HBQ.
9160 */
9161 phba->buffer_tag_count |= QUE_BUFTAG_BIT;
9162 spin_unlock_irq(&phba->hbalock);
9163 return phba->buffer_tag_count;
9164}
9165
James Smarte59058c2008-08-24 21:49:00 -04009166/**
James Smart3621a712009-04-06 18:47:14 -04009167 * lpfc_sli_ring_taggedbuf_get - find HBQ buffer associated with given tag
James Smarte59058c2008-08-24 21:49:00 -04009168 * @phba: Pointer to HBA context object.
9169 * @pring: Pointer to driver SLI ring object.
9170 * @tag: Buffer tag.
9171 *
9172 * Buffers posted using CMD_QUE_XRI64_CX iocb are in pring->postbufq
9173 * list. After HBA DMA data to these buffers, CMD_IOCB_RET_XRI64_CX
9174 * iocb is posted to the response ring with the tag of the buffer.
9175 * This function searches the pring->postbufq list using the tag
9176 * to find buffer associated with CMD_IOCB_RET_XRI64_CX
9177 * iocb. If the buffer is found then lpfc_dmabuf object of the
9178 * buffer is returned to the caller else NULL is returned.
9179 * This function is called with no lock held.
9180 **/
James Smart76bb24e2007-10-27 13:38:00 -04009181struct lpfc_dmabuf *
9182lpfc_sli_ring_taggedbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
9183 uint32_t tag)
9184{
9185 struct lpfc_dmabuf *mp, *next_mp;
9186 struct list_head *slp = &pring->postbufq;
9187
Lucas De Marchi25985ed2011-03-30 22:57:33 -03009188 /* Search postbufq, from the beginning, looking for a match on tag */
James Smart76bb24e2007-10-27 13:38:00 -04009189 spin_lock_irq(&phba->hbalock);
9190 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
9191 if (mp->buffer_tag == tag) {
9192 list_del_init(&mp->list);
9193 pring->postbufq_cnt--;
9194 spin_unlock_irq(&phba->hbalock);
9195 return mp;
9196 }
9197 }
9198
9199 spin_unlock_irq(&phba->hbalock);
9200 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartd7c255b2008-08-24 21:50:00 -04009201 "0402 Cannot find virtual addr for buffer tag on "
James Smart76bb24e2007-10-27 13:38:00 -04009202 "ring %d Data x%lx x%p x%p x%x\n",
9203 pring->ringno, (unsigned long) tag,
9204 slp->next, slp->prev, pring->postbufq_cnt);
9205
9206 return NULL;
9207}
dea31012005-04-17 16:05:31 -05009208
James Smarte59058c2008-08-24 21:49:00 -04009209/**
James Smart3621a712009-04-06 18:47:14 -04009210 * lpfc_sli_ringpostbuf_get - search buffers for unsolicited CT and ELS events
James Smarte59058c2008-08-24 21:49:00 -04009211 * @phba: Pointer to HBA context object.
9212 * @pring: Pointer to driver SLI ring object.
9213 * @phys: DMA address of the buffer.
9214 *
9215 * This function searches the buffer list using the dma_address
9216 * of unsolicited event to find the driver's lpfc_dmabuf object
9217 * corresponding to the dma_address. The function returns the
9218 * lpfc_dmabuf object if a buffer is found else it returns NULL.
9219 * This function is called by the ct and els unsolicited event
9220 * handlers to get the buffer associated with the unsolicited
9221 * event.
9222 *
9223 * This function is called with no lock held.
9224 **/
dea31012005-04-17 16:05:31 -05009225struct lpfc_dmabuf *
9226lpfc_sli_ringpostbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
9227 dma_addr_t phys)
9228{
9229 struct lpfc_dmabuf *mp, *next_mp;
9230 struct list_head *slp = &pring->postbufq;
9231
Lucas De Marchi25985ed2011-03-30 22:57:33 -03009232 /* Search postbufq, from the beginning, looking for a match on phys */
James Smart2e0fef82007-06-17 19:56:36 -05009233 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05009234 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
9235 if (mp->phys == phys) {
9236 list_del_init(&mp->list);
9237 pring->postbufq_cnt--;
James Smart2e0fef82007-06-17 19:56:36 -05009238 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05009239 return mp;
9240 }
9241 }
9242
James Smart2e0fef82007-06-17 19:56:36 -05009243 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05009244 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04009245 "0410 Cannot find virtual addr for mapped buf on "
dea31012005-04-17 16:05:31 -05009246 "ring %d Data x%llx x%p x%p x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04009247 pring->ringno, (unsigned long long)phys,
dea31012005-04-17 16:05:31 -05009248 slp->next, slp->prev, pring->postbufq_cnt);
9249 return NULL;
9250}
9251
James Smarte59058c2008-08-24 21:49:00 -04009252/**
James Smart3621a712009-04-06 18:47:14 -04009253 * lpfc_sli_abort_els_cmpl - Completion handler for the els abort iocbs
James Smarte59058c2008-08-24 21:49:00 -04009254 * @phba: Pointer to HBA context object.
9255 * @cmdiocb: Pointer to driver command iocb object.
9256 * @rspiocb: Pointer to driver response iocb object.
9257 *
9258 * This function is the completion handler for the abort iocbs for
9259 * ELS commands. This function is called from the ELS ring event
9260 * handler with no lock held. This function frees memory resources
9261 * associated with the abort iocb.
9262 **/
dea31012005-04-17 16:05:31 -05009263static void
James Smart2e0fef82007-06-17 19:56:36 -05009264lpfc_sli_abort_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
9265 struct lpfc_iocbq *rspiocb)
dea31012005-04-17 16:05:31 -05009266{
James Smart2e0fef82007-06-17 19:56:36 -05009267 IOCB_t *irsp = &rspiocb->iocb;
James Smart2680eea2007-04-25 09:52:55 -04009268 uint16_t abort_iotag, abort_context;
James Smartff78d8f2011-12-13 13:21:35 -05009269 struct lpfc_iocbq *abort_iocb = NULL;
James Smart2680eea2007-04-25 09:52:55 -04009270
9271 if (irsp->ulpStatus) {
James Smartff78d8f2011-12-13 13:21:35 -05009272
9273 /*
9274 * Assume that the port already completed and returned, or
9275 * will return the iocb. Just Log the message.
9276 */
James Smart2680eea2007-04-25 09:52:55 -04009277 abort_context = cmdiocb->iocb.un.acxri.abortContextTag;
9278 abort_iotag = cmdiocb->iocb.un.acxri.abortIoTag;
9279
James Smart2e0fef82007-06-17 19:56:36 -05009280 spin_lock_irq(&phba->hbalock);
James Smart45ed1192009-10-02 15:17:02 -04009281 if (phba->sli_rev < LPFC_SLI_REV4) {
9282 if (abort_iotag != 0 &&
9283 abort_iotag <= phba->sli.last_iotag)
9284 abort_iocb =
9285 phba->sli.iocbq_lookup[abort_iotag];
9286 } else
9287 /* For sli4 the abort_tag is the XRI,
9288 * so the abort routine puts the iotag of the iocb
9289 * being aborted in the context field of the abort
9290 * IOCB.
9291 */
9292 abort_iocb = phba->sli.iocbq_lookup[abort_context];
James Smart2680eea2007-04-25 09:52:55 -04009293
James Smart2a9bf3d2010-06-07 15:24:45 -04009294 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS | LOG_SLI,
9295 "0327 Cannot abort els iocb %p "
9296 "with tag %x context %x, abort status %x, "
9297 "abort code %x\n",
9298 abort_iocb, abort_iotag, abort_context,
9299 irsp->ulpStatus, irsp->un.ulpWord[4]);
James Smart2680eea2007-04-25 09:52:55 -04009300
James Smartff78d8f2011-12-13 13:21:35 -05009301 spin_unlock_irq(&phba->hbalock);
James Smart2680eea2007-04-25 09:52:55 -04009302 }
James Bottomley604a3e32005-10-29 10:28:33 -05009303 lpfc_sli_release_iocbq(phba, cmdiocb);
dea31012005-04-17 16:05:31 -05009304 return;
9305}
9306
James Smarte59058c2008-08-24 21:49:00 -04009307/**
James Smart3621a712009-04-06 18:47:14 -04009308 * lpfc_ignore_els_cmpl - Completion handler for aborted ELS command
James Smarte59058c2008-08-24 21:49:00 -04009309 * @phba: Pointer to HBA context object.
9310 * @cmdiocb: Pointer to driver command iocb object.
9311 * @rspiocb: Pointer to driver response iocb object.
9312 *
9313 * The function is called from SLI ring event handler with no
9314 * lock held. This function is the completion handler for ELS commands
9315 * which are aborted. The function frees memory resources used for
9316 * the aborted ELS commands.
9317 **/
James Smart92d7f7b2007-06-17 19:56:38 -05009318static void
9319lpfc_ignore_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
9320 struct lpfc_iocbq *rspiocb)
9321{
9322 IOCB_t *irsp = &rspiocb->iocb;
9323
9324 /* ELS cmd tag <ulpIoTag> completes */
9325 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
James Smartd7c255b2008-08-24 21:50:00 -04009326 "0139 Ignoring ELS cmd tag x%x completion Data: "
James Smart92d7f7b2007-06-17 19:56:38 -05009327 "x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04009328 irsp->ulpIoTag, irsp->ulpStatus,
James Smart92d7f7b2007-06-17 19:56:38 -05009329 irsp->un.ulpWord[4], irsp->ulpTimeout);
James Smart858c9f62007-06-17 19:56:39 -05009330 if (cmdiocb->iocb.ulpCommand == CMD_GEN_REQUEST64_CR)
9331 lpfc_ct_free_iocb(phba, cmdiocb);
9332 else
9333 lpfc_els_free_iocb(phba, cmdiocb);
James Smart92d7f7b2007-06-17 19:56:38 -05009334 return;
9335}
9336
James Smarte59058c2008-08-24 21:49:00 -04009337/**
James Smart5af5eee2010-10-22 11:06:38 -04009338 * lpfc_sli_abort_iotag_issue - Issue abort for a command iocb
James Smarte59058c2008-08-24 21:49:00 -04009339 * @phba: Pointer to HBA context object.
9340 * @pring: Pointer to driver SLI ring object.
9341 * @cmdiocb: Pointer to driver command iocb object.
9342 *
James Smart5af5eee2010-10-22 11:06:38 -04009343 * This function issues an abort iocb for the provided command iocb down to
9344 * the port. Other than the case the outstanding command iocb is an abort
9345 * request, this function issues abort out unconditionally. This function is
9346 * called with hbalock held. The function returns 0 when it fails due to
9347 * memory allocation failure or when the command iocb is an abort request.
James Smarte59058c2008-08-24 21:49:00 -04009348 **/
James Smart5af5eee2010-10-22 11:06:38 -04009349static int
9350lpfc_sli_abort_iotag_issue(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -05009351 struct lpfc_iocbq *cmdiocb)
dea31012005-04-17 16:05:31 -05009352{
James Smart2e0fef82007-06-17 19:56:36 -05009353 struct lpfc_vport *vport = cmdiocb->vport;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009354 struct lpfc_iocbq *abtsiocbp;
dea31012005-04-17 16:05:31 -05009355 IOCB_t *icmd = NULL;
9356 IOCB_t *iabt = NULL;
James Smart5af5eee2010-10-22 11:06:38 -04009357 int retval;
James Smart7e56aa22012-08-03 12:35:34 -04009358 unsigned long iflags;
James Smart07951072007-04-25 09:51:38 -04009359
James Smart92d7f7b2007-06-17 19:56:38 -05009360 /*
9361 * There are certain command types we don't want to abort. And we
9362 * don't want to abort commands that are already in the process of
9363 * being aborted.
James Smart07951072007-04-25 09:51:38 -04009364 */
9365 icmd = &cmdiocb->iocb;
James Smart2e0fef82007-06-17 19:56:36 -05009366 if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
James Smart92d7f7b2007-06-17 19:56:38 -05009367 icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
9368 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
James Smart07951072007-04-25 09:51:38 -04009369 return 0;
9370
dea31012005-04-17 16:05:31 -05009371 /* issue ABTS for this IOCB based on iotag */
James Smart92d7f7b2007-06-17 19:56:38 -05009372 abtsiocbp = __lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05009373 if (abtsiocbp == NULL)
9374 return 0;
dea31012005-04-17 16:05:31 -05009375
James Smart07951072007-04-25 09:51:38 -04009376 /* This signals the response to set the correct status
James Smart341af102010-01-26 23:07:37 -05009377 * before calling the completion handler
James Smart07951072007-04-25 09:51:38 -04009378 */
9379 cmdiocb->iocb_flag |= LPFC_DRIVER_ABORTED;
9380
dea31012005-04-17 16:05:31 -05009381 iabt = &abtsiocbp->iocb;
James Smart07951072007-04-25 09:51:38 -04009382 iabt->un.acxri.abortType = ABORT_TYPE_ABTS;
9383 iabt->un.acxri.abortContextTag = icmd->ulpContext;
James Smart45ed1192009-10-02 15:17:02 -04009384 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smartda0436e2009-05-22 14:51:39 -04009385 iabt->un.acxri.abortIoTag = cmdiocb->sli4_xritag;
James Smart45ed1192009-10-02 15:17:02 -04009386 iabt->un.acxri.abortContextTag = cmdiocb->iotag;
9387 }
James Smartda0436e2009-05-22 14:51:39 -04009388 else
9389 iabt->un.acxri.abortIoTag = icmd->ulpIoTag;
dea31012005-04-17 16:05:31 -05009390 iabt->ulpLe = 1;
James Smart07951072007-04-25 09:51:38 -04009391 iabt->ulpClass = icmd->ulpClass;
dea31012005-04-17 16:05:31 -05009392
James Smart5ffc2662009-11-18 15:39:44 -05009393 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
9394 abtsiocbp->fcp_wqidx = cmdiocb->fcp_wqidx;
James Smart341af102010-01-26 23:07:37 -05009395 if (cmdiocb->iocb_flag & LPFC_IO_FCP)
9396 abtsiocbp->iocb_flag |= LPFC_USE_FCPWQIDX;
James Smart5ffc2662009-11-18 15:39:44 -05009397
James Smart2e0fef82007-06-17 19:56:36 -05009398 if (phba->link_state >= LPFC_LINK_UP)
James Smart07951072007-04-25 09:51:38 -04009399 iabt->ulpCommand = CMD_ABORT_XRI_CN;
9400 else
9401 iabt->ulpCommand = CMD_CLOSE_XRI_CN;
9402
9403 abtsiocbp->iocb_cmpl = lpfc_sli_abort_els_cmpl;
James Smart5b8bd0c2007-04-25 09:52:49 -04009404
James Smarte8b62012007-08-02 11:10:09 -04009405 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
9406 "0339 Abort xri x%x, original iotag x%x, "
9407 "abort cmd iotag x%x\n",
James Smart2a9bf3d2010-06-07 15:24:45 -04009408 iabt->un.acxri.abortIoTag,
James Smarte8b62012007-08-02 11:10:09 -04009409 iabt->un.acxri.abortContextTag,
James Smart2a9bf3d2010-06-07 15:24:45 -04009410 abtsiocbp->iotag);
James Smart7e56aa22012-08-03 12:35:34 -04009411
9412 if (phba->sli_rev == LPFC_SLI_REV4) {
9413 /* Note: both hbalock and ring_lock need to be set here */
9414 spin_lock_irqsave(&pring->ring_lock, iflags);
9415 retval = __lpfc_sli_issue_iocb(phba, pring->ringno,
9416 abtsiocbp, 0);
9417 spin_unlock_irqrestore(&pring->ring_lock, iflags);
9418 } else {
9419 retval = __lpfc_sli_issue_iocb(phba, pring->ringno,
9420 abtsiocbp, 0);
9421 }
James Smart07951072007-04-25 09:51:38 -04009422
James Smartd7c255b2008-08-24 21:50:00 -04009423 if (retval)
9424 __lpfc_sli_release_iocbq(phba, abtsiocbp);
James Smart5af5eee2010-10-22 11:06:38 -04009425
9426 /*
9427 * Caller to this routine should check for IOCB_ERROR
9428 * and handle it properly. This routine no longer removes
9429 * iocb off txcmplq and call compl in case of IOCB_ERROR.
9430 */
9431 return retval;
9432}
9433
9434/**
9435 * lpfc_sli_issue_abort_iotag - Abort function for a command iocb
9436 * @phba: Pointer to HBA context object.
9437 * @pring: Pointer to driver SLI ring object.
9438 * @cmdiocb: Pointer to driver command iocb object.
9439 *
9440 * This function issues an abort iocb for the provided command iocb. In case
9441 * of unloading, the abort iocb will not be issued to commands on the ELS
9442 * ring. Instead, the callback function shall be changed to those commands
9443 * so that nothing happens when them finishes. This function is called with
9444 * hbalock held. The function returns 0 when the command iocb is an abort
9445 * request.
9446 **/
9447int
9448lpfc_sli_issue_abort_iotag(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
9449 struct lpfc_iocbq *cmdiocb)
9450{
9451 struct lpfc_vport *vport = cmdiocb->vport;
9452 int retval = IOCB_ERROR;
9453 IOCB_t *icmd = NULL;
9454
9455 /*
9456 * There are certain command types we don't want to abort. And we
9457 * don't want to abort commands that are already in the process of
9458 * being aborted.
9459 */
9460 icmd = &cmdiocb->iocb;
9461 if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
9462 icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
9463 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
9464 return 0;
9465
9466 /*
9467 * If we're unloading, don't abort iocb on the ELS ring, but change
9468 * the callback so that nothing happens when it finishes.
9469 */
9470 if ((vport->load_flag & FC_UNLOADING) &&
9471 (pring->ringno == LPFC_ELS_RING)) {
9472 if (cmdiocb->iocb_flag & LPFC_IO_FABRIC)
9473 cmdiocb->fabric_iocb_cmpl = lpfc_ignore_els_cmpl;
9474 else
9475 cmdiocb->iocb_cmpl = lpfc_ignore_els_cmpl;
9476 goto abort_iotag_exit;
9477 }
9478
9479 /* Now, we try to issue the abort to the cmdiocb out */
9480 retval = lpfc_sli_abort_iotag_issue(phba, pring, cmdiocb);
9481
James Smart07951072007-04-25 09:51:38 -04009482abort_iotag_exit:
James Smart2e0fef82007-06-17 19:56:36 -05009483 /*
9484 * Caller to this routine should check for IOCB_ERROR
9485 * and handle it properly. This routine no longer removes
9486 * iocb off txcmplq and call compl in case of IOCB_ERROR.
James Smart07951072007-04-25 09:51:38 -04009487 */
James Smart2e0fef82007-06-17 19:56:36 -05009488 return retval;
dea31012005-04-17 16:05:31 -05009489}
9490
James Smarte59058c2008-08-24 21:49:00 -04009491/**
James Smart5af5eee2010-10-22 11:06:38 -04009492 * lpfc_sli_iocb_ring_abort - Unconditionally abort all iocbs on an iocb ring
9493 * @phba: Pointer to HBA context object.
9494 * @pring: Pointer to driver SLI ring object.
9495 *
9496 * This function aborts all iocbs in the given ring and frees all the iocb
9497 * objects in txq. This function issues abort iocbs unconditionally for all
9498 * the iocb commands in txcmplq. The iocbs in the txcmplq is not guaranteed
9499 * to complete before the return of this function. The caller is not required
9500 * to hold any locks.
9501 **/
9502static void
9503lpfc_sli_iocb_ring_abort(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
9504{
9505 LIST_HEAD(completions);
9506 struct lpfc_iocbq *iocb, *next_iocb;
9507
9508 if (pring->ringno == LPFC_ELS_RING)
9509 lpfc_fabric_abort_hba(phba);
9510
9511 spin_lock_irq(&phba->hbalock);
9512
9513 /* Take off all the iocbs on txq for cancelling */
9514 list_splice_init(&pring->txq, &completions);
9515 pring->txq_cnt = 0;
9516
9517 /* Next issue ABTS for everything on the txcmplq */
9518 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
9519 lpfc_sli_abort_iotag_issue(phba, pring, iocb);
9520
9521 spin_unlock_irq(&phba->hbalock);
9522
9523 /* Cancel all the IOCBs from the completions list */
9524 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
9525 IOERR_SLI_ABORTED);
9526}
9527
9528/**
9529 * lpfc_sli_hba_iocb_abort - Abort all iocbs to an hba.
9530 * @phba: pointer to lpfc HBA data structure.
9531 *
9532 * This routine will abort all pending and outstanding iocbs to an HBA.
9533 **/
9534void
9535lpfc_sli_hba_iocb_abort(struct lpfc_hba *phba)
9536{
9537 struct lpfc_sli *psli = &phba->sli;
9538 struct lpfc_sli_ring *pring;
9539 int i;
9540
9541 for (i = 0; i < psli->num_rings; i++) {
9542 pring = &psli->ring[i];
9543 lpfc_sli_iocb_ring_abort(phba, pring);
9544 }
9545}
9546
9547/**
James Smart3621a712009-04-06 18:47:14 -04009548 * lpfc_sli_validate_fcp_iocb - find commands associated with a vport or LUN
James Smarte59058c2008-08-24 21:49:00 -04009549 * @iocbq: Pointer to driver iocb object.
9550 * @vport: Pointer to driver virtual port object.
9551 * @tgt_id: SCSI ID of the target.
9552 * @lun_id: LUN ID of the scsi device.
9553 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST
9554 *
James Smart3621a712009-04-06 18:47:14 -04009555 * This function acts as an iocb filter for functions which abort or count
James Smarte59058c2008-08-24 21:49:00 -04009556 * all FCP iocbs pending on a lun/SCSI target/SCSI host. It will return
9557 * 0 if the filtering criteria is met for the given iocb and will return
9558 * 1 if the filtering criteria is not met.
9559 * If ctx_cmd == LPFC_CTX_LUN, the function returns 0 only if the
9560 * given iocb is for the SCSI device specified by vport, tgt_id and
9561 * lun_id parameter.
9562 * If ctx_cmd == LPFC_CTX_TGT, the function returns 0 only if the
9563 * given iocb is for the SCSI target specified by vport and tgt_id
9564 * parameters.
9565 * If ctx_cmd == LPFC_CTX_HOST, the function returns 0 only if the
9566 * given iocb is for the SCSI host associated with the given vport.
9567 * This function is called with no locks held.
9568 **/
dea31012005-04-17 16:05:31 -05009569static int
James Smart51ef4c22007-08-02 11:10:31 -04009570lpfc_sli_validate_fcp_iocb(struct lpfc_iocbq *iocbq, struct lpfc_vport *vport,
9571 uint16_t tgt_id, uint64_t lun_id,
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009572 lpfc_ctx_cmd ctx_cmd)
dea31012005-04-17 16:05:31 -05009573{
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009574 struct lpfc_scsi_buf *lpfc_cmd;
dea31012005-04-17 16:05:31 -05009575 int rc = 1;
9576
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009577 if (!(iocbq->iocb_flag & LPFC_IO_FCP))
9578 return rc;
9579
James Smart51ef4c22007-08-02 11:10:31 -04009580 if (iocbq->vport != vport)
9581 return rc;
9582
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009583 lpfc_cmd = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009584
James Smart495a7142008-06-14 22:52:59 -04009585 if (lpfc_cmd->pCmd == NULL)
dea31012005-04-17 16:05:31 -05009586 return rc;
9587
9588 switch (ctx_cmd) {
9589 case LPFC_CTX_LUN:
James Smart495a7142008-06-14 22:52:59 -04009590 if ((lpfc_cmd->rdata->pnode) &&
9591 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id) &&
9592 (scsilun_to_int(&lpfc_cmd->fcp_cmnd->fcp_lun) == lun_id))
dea31012005-04-17 16:05:31 -05009593 rc = 0;
9594 break;
9595 case LPFC_CTX_TGT:
James Smart495a7142008-06-14 22:52:59 -04009596 if ((lpfc_cmd->rdata->pnode) &&
9597 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id))
dea31012005-04-17 16:05:31 -05009598 rc = 0;
9599 break;
dea31012005-04-17 16:05:31 -05009600 case LPFC_CTX_HOST:
9601 rc = 0;
9602 break;
9603 default:
9604 printk(KERN_ERR "%s: Unknown context cmd type, value %d\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07009605 __func__, ctx_cmd);
dea31012005-04-17 16:05:31 -05009606 break;
9607 }
9608
9609 return rc;
9610}
9611
James Smarte59058c2008-08-24 21:49:00 -04009612/**
James Smart3621a712009-04-06 18:47:14 -04009613 * lpfc_sli_sum_iocb - Function to count the number of FCP iocbs pending
James Smarte59058c2008-08-24 21:49:00 -04009614 * @vport: Pointer to virtual port.
9615 * @tgt_id: SCSI ID of the target.
9616 * @lun_id: LUN ID of the scsi device.
9617 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
9618 *
9619 * This function returns number of FCP commands pending for the vport.
9620 * When ctx_cmd == LPFC_CTX_LUN, the function returns number of FCP
9621 * commands pending on the vport associated with SCSI device specified
9622 * by tgt_id and lun_id parameters.
9623 * When ctx_cmd == LPFC_CTX_TGT, the function returns number of FCP
9624 * commands pending on the vport associated with SCSI target specified
9625 * by tgt_id parameter.
9626 * When ctx_cmd == LPFC_CTX_HOST, the function returns number of FCP
9627 * commands pending on the vport.
9628 * This function returns the number of iocbs which satisfy the filter.
9629 * This function is called without any lock held.
9630 **/
dea31012005-04-17 16:05:31 -05009631int
James Smart51ef4c22007-08-02 11:10:31 -04009632lpfc_sli_sum_iocb(struct lpfc_vport *vport, uint16_t tgt_id, uint64_t lun_id,
9633 lpfc_ctx_cmd ctx_cmd)
dea31012005-04-17 16:05:31 -05009634{
James Smart51ef4c22007-08-02 11:10:31 -04009635 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009636 struct lpfc_iocbq *iocbq;
9637 int sum, i;
dea31012005-04-17 16:05:31 -05009638
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009639 for (i = 1, sum = 0; i <= phba->sli.last_iotag; i++) {
9640 iocbq = phba->sli.iocbq_lookup[i];
dea31012005-04-17 16:05:31 -05009641
James Smart51ef4c22007-08-02 11:10:31 -04009642 if (lpfc_sli_validate_fcp_iocb (iocbq, vport, tgt_id, lun_id,
9643 ctx_cmd) == 0)
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009644 sum++;
dea31012005-04-17 16:05:31 -05009645 }
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009646
dea31012005-04-17 16:05:31 -05009647 return sum;
9648}
9649
James Smarte59058c2008-08-24 21:49:00 -04009650/**
James Smart3621a712009-04-06 18:47:14 -04009651 * lpfc_sli_abort_fcp_cmpl - Completion handler function for aborted FCP IOCBs
James Smarte59058c2008-08-24 21:49:00 -04009652 * @phba: Pointer to HBA context object
9653 * @cmdiocb: Pointer to command iocb object.
9654 * @rspiocb: Pointer to response iocb object.
9655 *
9656 * This function is called when an aborted FCP iocb completes. This
9657 * function is called by the ring event handler with no lock held.
9658 * This function frees the iocb.
9659 **/
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -04009660void
James Smart2e0fef82007-06-17 19:56:36 -05009661lpfc_sli_abort_fcp_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
9662 struct lpfc_iocbq *rspiocb)
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -04009663{
James Smartcb69f7d2011-12-13 13:21:57 -05009664 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
9665 "3096 ABORT_XRI_CN completing on xri x%x "
9666 "original iotag x%x, abort cmd iotag x%x "
9667 "status 0x%x, reason 0x%x\n",
9668 cmdiocb->iocb.un.acxri.abortContextTag,
9669 cmdiocb->iocb.un.acxri.abortIoTag,
9670 cmdiocb->iotag, rspiocb->iocb.ulpStatus,
9671 rspiocb->iocb.un.ulpWord[4]);
James Bottomley604a3e32005-10-29 10:28:33 -05009672 lpfc_sli_release_iocbq(phba, cmdiocb);
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -04009673 return;
9674}
9675
James Smarte59058c2008-08-24 21:49:00 -04009676/**
James Smart3621a712009-04-06 18:47:14 -04009677 * lpfc_sli_abort_iocb - issue abort for all commands on a host/target/LUN
James Smarte59058c2008-08-24 21:49:00 -04009678 * @vport: Pointer to virtual port.
9679 * @pring: Pointer to driver SLI ring object.
9680 * @tgt_id: SCSI ID of the target.
9681 * @lun_id: LUN ID of the scsi device.
9682 * @abort_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
9683 *
9684 * This function sends an abort command for every SCSI command
9685 * associated with the given virtual port pending on the ring
9686 * filtered by lpfc_sli_validate_fcp_iocb function.
9687 * When abort_cmd == LPFC_CTX_LUN, the function sends abort only to the
9688 * FCP iocbs associated with lun specified by tgt_id and lun_id
9689 * parameters
9690 * When abort_cmd == LPFC_CTX_TGT, the function sends abort only to the
9691 * FCP iocbs associated with SCSI target specified by tgt_id parameter.
9692 * When abort_cmd == LPFC_CTX_HOST, the function sends abort to all
9693 * FCP iocbs associated with virtual port.
9694 * This function returns number of iocbs it failed to abort.
9695 * This function is called with no locks held.
9696 **/
dea31012005-04-17 16:05:31 -05009697int
James Smart51ef4c22007-08-02 11:10:31 -04009698lpfc_sli_abort_iocb(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
9699 uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd abort_cmd)
dea31012005-04-17 16:05:31 -05009700{
James Smart51ef4c22007-08-02 11:10:31 -04009701 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009702 struct lpfc_iocbq *iocbq;
9703 struct lpfc_iocbq *abtsiocb;
dea31012005-04-17 16:05:31 -05009704 IOCB_t *cmd = NULL;
dea31012005-04-17 16:05:31 -05009705 int errcnt = 0, ret_val = 0;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009706 int i;
dea31012005-04-17 16:05:31 -05009707
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009708 for (i = 1; i <= phba->sli.last_iotag; i++) {
9709 iocbq = phba->sli.iocbq_lookup[i];
dea31012005-04-17 16:05:31 -05009710
James Smart51ef4c22007-08-02 11:10:31 -04009711 if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
James Smart2e0fef82007-06-17 19:56:36 -05009712 abort_cmd) != 0)
dea31012005-04-17 16:05:31 -05009713 continue;
9714
9715 /* issue ABTS for this IOCB based on iotag */
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009716 abtsiocb = lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05009717 if (abtsiocb == NULL) {
9718 errcnt++;
9719 continue;
9720 }
dea31012005-04-17 16:05:31 -05009721
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009722 cmd = &iocbq->iocb;
dea31012005-04-17 16:05:31 -05009723 abtsiocb->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
9724 abtsiocb->iocb.un.acxri.abortContextTag = cmd->ulpContext;
James Smartda0436e2009-05-22 14:51:39 -04009725 if (phba->sli_rev == LPFC_SLI_REV4)
9726 abtsiocb->iocb.un.acxri.abortIoTag = iocbq->sli4_xritag;
9727 else
9728 abtsiocb->iocb.un.acxri.abortIoTag = cmd->ulpIoTag;
dea31012005-04-17 16:05:31 -05009729 abtsiocb->iocb.ulpLe = 1;
9730 abtsiocb->iocb.ulpClass = cmd->ulpClass;
James Smart2e0fef82007-06-17 19:56:36 -05009731 abtsiocb->vport = phba->pport;
dea31012005-04-17 16:05:31 -05009732
James Smart5ffc2662009-11-18 15:39:44 -05009733 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
9734 abtsiocb->fcp_wqidx = iocbq->fcp_wqidx;
James Smart341af102010-01-26 23:07:37 -05009735 if (iocbq->iocb_flag & LPFC_IO_FCP)
9736 abtsiocb->iocb_flag |= LPFC_USE_FCPWQIDX;
James Smart5ffc2662009-11-18 15:39:44 -05009737
James Smart2e0fef82007-06-17 19:56:36 -05009738 if (lpfc_is_link_up(phba))
dea31012005-04-17 16:05:31 -05009739 abtsiocb->iocb.ulpCommand = CMD_ABORT_XRI_CN;
9740 else
9741 abtsiocb->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
9742
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -04009743 /* Setup callback routine and issue the command. */
9744 abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
James Smartda0436e2009-05-22 14:51:39 -04009745 ret_val = lpfc_sli_issue_iocb(phba, pring->ringno,
9746 abtsiocb, 0);
dea31012005-04-17 16:05:31 -05009747 if (ret_val == IOCB_ERROR) {
James Bottomley604a3e32005-10-29 10:28:33 -05009748 lpfc_sli_release_iocbq(phba, abtsiocb);
dea31012005-04-17 16:05:31 -05009749 errcnt++;
9750 continue;
9751 }
9752 }
9753
9754 return errcnt;
9755}
9756
James Smarte59058c2008-08-24 21:49:00 -04009757/**
James Smart3621a712009-04-06 18:47:14 -04009758 * lpfc_sli_wake_iocb_wait - lpfc_sli_issue_iocb_wait's completion handler
James Smarte59058c2008-08-24 21:49:00 -04009759 * @phba: Pointer to HBA context object.
9760 * @cmdiocbq: Pointer to command iocb.
9761 * @rspiocbq: Pointer to response iocb.
9762 *
9763 * This function is the completion handler for iocbs issued using
9764 * lpfc_sli_issue_iocb_wait function. This function is called by the
9765 * ring event handler function without any lock held. This function
9766 * can be called from both worker thread context and interrupt
9767 * context. This function also can be called from other thread which
9768 * cleans up the SLI layer objects.
9769 * This function copy the contents of the response iocb to the
9770 * response iocb memory object provided by the caller of
9771 * lpfc_sli_issue_iocb_wait and then wakes up the thread which
9772 * sleeps for the iocb completion.
9773 **/
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009774static void
9775lpfc_sli_wake_iocb_wait(struct lpfc_hba *phba,
9776 struct lpfc_iocbq *cmdiocbq,
9777 struct lpfc_iocbq *rspiocbq)
dea31012005-04-17 16:05:31 -05009778{
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009779 wait_queue_head_t *pdone_q;
9780 unsigned long iflags;
James Smart0f65ff62010-02-26 14:14:23 -05009781 struct lpfc_scsi_buf *lpfc_cmd;
dea31012005-04-17 16:05:31 -05009782
James Smart2e0fef82007-06-17 19:56:36 -05009783 spin_lock_irqsave(&phba->hbalock, iflags);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009784 cmdiocbq->iocb_flag |= LPFC_IO_WAKE;
9785 if (cmdiocbq->context2 && rspiocbq)
9786 memcpy(&((struct lpfc_iocbq *)cmdiocbq->context2)->iocb,
9787 &rspiocbq->iocb, sizeof(IOCB_t));
9788
James Smart0f65ff62010-02-26 14:14:23 -05009789 /* Set the exchange busy flag for task management commands */
9790 if ((cmdiocbq->iocb_flag & LPFC_IO_FCP) &&
9791 !(cmdiocbq->iocb_flag & LPFC_IO_LIBDFC)) {
9792 lpfc_cmd = container_of(cmdiocbq, struct lpfc_scsi_buf,
9793 cur_iocbq);
9794 lpfc_cmd->exch_busy = rspiocbq->iocb_flag & LPFC_EXCHANGE_BUSY;
9795 }
9796
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009797 pdone_q = cmdiocbq->context_un.wait_queue;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009798 if (pdone_q)
9799 wake_up(pdone_q);
James Smart858c9f62007-06-17 19:56:39 -05009800 spin_unlock_irqrestore(&phba->hbalock, iflags);
dea31012005-04-17 16:05:31 -05009801 return;
9802}
9803
James Smarte59058c2008-08-24 21:49:00 -04009804/**
James Smartd11e31d2009-06-10 17:23:06 -04009805 * lpfc_chk_iocb_flg - Test IOCB flag with lock held.
9806 * @phba: Pointer to HBA context object..
9807 * @piocbq: Pointer to command iocb.
9808 * @flag: Flag to test.
9809 *
9810 * This routine grabs the hbalock and then test the iocb_flag to
9811 * see if the passed in flag is set.
9812 * Returns:
9813 * 1 if flag is set.
9814 * 0 if flag is not set.
9815 **/
9816static int
9817lpfc_chk_iocb_flg(struct lpfc_hba *phba,
9818 struct lpfc_iocbq *piocbq, uint32_t flag)
9819{
9820 unsigned long iflags;
9821 int ret;
9822
9823 spin_lock_irqsave(&phba->hbalock, iflags);
9824 ret = piocbq->iocb_flag & flag;
9825 spin_unlock_irqrestore(&phba->hbalock, iflags);
9826 return ret;
9827
9828}
9829
9830/**
James Smart3621a712009-04-06 18:47:14 -04009831 * lpfc_sli_issue_iocb_wait - Synchronous function to issue iocb commands
James Smarte59058c2008-08-24 21:49:00 -04009832 * @phba: Pointer to HBA context object..
9833 * @pring: Pointer to sli ring.
9834 * @piocb: Pointer to command iocb.
9835 * @prspiocbq: Pointer to response iocb.
9836 * @timeout: Timeout in number of seconds.
9837 *
9838 * This function issues the iocb to firmware and waits for the
9839 * iocb to complete. If the iocb command is not
9840 * completed within timeout seconds, it returns IOCB_TIMEDOUT.
9841 * Caller should not free the iocb resources if this function
9842 * returns IOCB_TIMEDOUT.
9843 * The function waits for the iocb completion using an
9844 * non-interruptible wait.
9845 * This function will sleep while waiting for iocb completion.
9846 * So, this function should not be called from any context which
9847 * does not allow sleeping. Due to the same reason, this function
9848 * cannot be called with interrupt disabled.
9849 * This function assumes that the iocb completions occur while
9850 * this function sleep. So, this function cannot be called from
9851 * the thread which process iocb completion for this ring.
9852 * This function clears the iocb_flag of the iocb object before
9853 * issuing the iocb and the iocb completion handler sets this
9854 * flag and wakes this thread when the iocb completes.
9855 * The contents of the response iocb will be copied to prspiocbq
9856 * by the completion handler when the command completes.
9857 * This function returns IOCB_SUCCESS when success.
9858 * This function is called with no lock held.
9859 **/
dea31012005-04-17 16:05:31 -05009860int
James Smart2e0fef82007-06-17 19:56:36 -05009861lpfc_sli_issue_iocb_wait(struct lpfc_hba *phba,
James Smartda0436e2009-05-22 14:51:39 -04009862 uint32_t ring_number,
James Smart2e0fef82007-06-17 19:56:36 -05009863 struct lpfc_iocbq *piocb,
9864 struct lpfc_iocbq *prspiocbq,
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009865 uint32_t timeout)
dea31012005-04-17 16:05:31 -05009866{
Peter Zijlstra7259f0d2006-10-29 22:46:36 -08009867 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009868 long timeleft, timeout_req = 0;
9869 int retval = IOCB_SUCCESS;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05009870 uint32_t creg_val;
James Smart2a9bf3d2010-06-07 15:24:45 -04009871 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
dea31012005-04-17 16:05:31 -05009872 /*
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009873 * If the caller has provided a response iocbq buffer, then context2
9874 * is NULL or its an error.
dea31012005-04-17 16:05:31 -05009875 */
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009876 if (prspiocbq) {
9877 if (piocb->context2)
9878 return IOCB_ERROR;
9879 piocb->context2 = prspiocbq;
dea31012005-04-17 16:05:31 -05009880 }
9881
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009882 piocb->iocb_cmpl = lpfc_sli_wake_iocb_wait;
9883 piocb->context_un.wait_queue = &done_q;
9884 piocb->iocb_flag &= ~LPFC_IO_WAKE;
dea31012005-04-17 16:05:31 -05009885
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05009886 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
James Smart9940b972011-03-11 16:06:12 -05009887 if (lpfc_readl(phba->HCregaddr, &creg_val))
9888 return IOCB_ERROR;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05009889 creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
9890 writel(creg_val, phba->HCregaddr);
9891 readl(phba->HCregaddr); /* flush */
9892 }
9893
James Smart2a9bf3d2010-06-07 15:24:45 -04009894 retval = lpfc_sli_issue_iocb(phba, ring_number, piocb,
9895 SLI_IOCB_RET_IOCB);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009896 if (retval == IOCB_SUCCESS) {
9897 timeout_req = timeout * HZ;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009898 timeleft = wait_event_timeout(done_q,
James Smartd11e31d2009-06-10 17:23:06 -04009899 lpfc_chk_iocb_flg(phba, piocb, LPFC_IO_WAKE),
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009900 timeout_req);
dea31012005-04-17 16:05:31 -05009901
James Smart7054a602007-04-25 09:52:34 -04009902 if (piocb->iocb_flag & LPFC_IO_WAKE) {
9903 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04009904 "0331 IOCB wake signaled\n");
James Smart7054a602007-04-25 09:52:34 -04009905 } else if (timeleft == 0) {
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009906 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04009907 "0338 IOCB wait timeout error - no "
9908 "wake response Data x%x\n", timeout);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009909 retval = IOCB_TIMEDOUT;
James Smart7054a602007-04-25 09:52:34 -04009910 } else {
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009911 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04009912 "0330 IOCB wake NOT set, "
9913 "Data x%x x%lx\n",
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009914 timeout, (timeleft / jiffies));
9915 retval = IOCB_TIMEDOUT;
dea31012005-04-17 16:05:31 -05009916 }
James Smart2a9bf3d2010-06-07 15:24:45 -04009917 } else if (retval == IOCB_BUSY) {
9918 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
9919 "2818 Max IOCBs %d txq cnt %d txcmplq cnt %d\n",
9920 phba->iocb_cnt, pring->txq_cnt, pring->txcmplq_cnt);
9921 return retval;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009922 } else {
9923 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smartd7c255b2008-08-24 21:50:00 -04009924 "0332 IOCB wait issue failed, Data x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04009925 retval);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009926 retval = IOCB_ERROR;
dea31012005-04-17 16:05:31 -05009927 }
9928
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05009929 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
James Smart9940b972011-03-11 16:06:12 -05009930 if (lpfc_readl(phba->HCregaddr, &creg_val))
9931 return IOCB_ERROR;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05009932 creg_val &= ~(HC_R0INT_ENA << LPFC_FCP_RING);
9933 writel(creg_val, phba->HCregaddr);
9934 readl(phba->HCregaddr); /* flush */
9935 }
9936
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009937 if (prspiocbq)
9938 piocb->context2 = NULL;
9939
9940 piocb->context_un.wait_queue = NULL;
9941 piocb->iocb_cmpl = NULL;
dea31012005-04-17 16:05:31 -05009942 return retval;
9943}
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009944
James Smarte59058c2008-08-24 21:49:00 -04009945/**
James Smart3621a712009-04-06 18:47:14 -04009946 * lpfc_sli_issue_mbox_wait - Synchronous function to issue mailbox
James Smarte59058c2008-08-24 21:49:00 -04009947 * @phba: Pointer to HBA context object.
9948 * @pmboxq: Pointer to driver mailbox object.
9949 * @timeout: Timeout in number of seconds.
9950 *
9951 * This function issues the mailbox to firmware and waits for the
9952 * mailbox command to complete. If the mailbox command is not
9953 * completed within timeout seconds, it returns MBX_TIMEOUT.
9954 * The function waits for the mailbox completion using an
9955 * interruptible wait. If the thread is woken up due to a
9956 * signal, MBX_TIMEOUT error is returned to the caller. Caller
9957 * should not free the mailbox resources, if this function returns
9958 * MBX_TIMEOUT.
9959 * This function will sleep while waiting for mailbox completion.
9960 * So, this function should not be called from any context which
9961 * does not allow sleeping. Due to the same reason, this function
9962 * cannot be called with interrupt disabled.
9963 * This function assumes that the mailbox completion occurs while
9964 * this function sleep. So, this function cannot be called from
9965 * the worker thread which processes mailbox completion.
9966 * This function is called in the context of HBA management
9967 * applications.
9968 * This function returns MBX_SUCCESS when successful.
9969 * This function is called with no lock held.
9970 **/
dea31012005-04-17 16:05:31 -05009971int
James Smart2e0fef82007-06-17 19:56:36 -05009972lpfc_sli_issue_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq,
dea31012005-04-17 16:05:31 -05009973 uint32_t timeout)
9974{
Peter Zijlstra7259f0d2006-10-29 22:46:36 -08009975 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
dea31012005-04-17 16:05:31 -05009976 int retval;
James Smart858c9f62007-06-17 19:56:39 -05009977 unsigned long flag;
dea31012005-04-17 16:05:31 -05009978
9979 /* The caller must leave context1 empty. */
James Smart98c9ea52007-10-27 13:37:33 -04009980 if (pmboxq->context1)
James Smart2e0fef82007-06-17 19:56:36 -05009981 return MBX_NOT_FINISHED;
dea31012005-04-17 16:05:31 -05009982
James Smart495a7142008-06-14 22:52:59 -04009983 pmboxq->mbox_flag &= ~LPFC_MBX_WAKE;
dea31012005-04-17 16:05:31 -05009984 /* setup wake call as IOCB callback */
9985 pmboxq->mbox_cmpl = lpfc_sli_wake_mbox_wait;
9986 /* setup context field to pass wait_queue pointer to wake function */
9987 pmboxq->context1 = &done_q;
9988
dea31012005-04-17 16:05:31 -05009989 /* now issue the command */
9990 retval = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
dea31012005-04-17 16:05:31 -05009991 if (retval == MBX_BUSY || retval == MBX_SUCCESS) {
James Smart7054a602007-04-25 09:52:34 -04009992 wait_event_interruptible_timeout(done_q,
9993 pmboxq->mbox_flag & LPFC_MBX_WAKE,
9994 timeout * HZ);
9995
James Smart858c9f62007-06-17 19:56:39 -05009996 spin_lock_irqsave(&phba->hbalock, flag);
dea31012005-04-17 16:05:31 -05009997 pmboxq->context1 = NULL;
James Smart7054a602007-04-25 09:52:34 -04009998 /*
9999 * if LPFC_MBX_WAKE flag is set the mailbox is completed
10000 * else do not free the resources.
10001 */
James Smartd7c47992010-06-08 18:31:54 -040010002 if (pmboxq->mbox_flag & LPFC_MBX_WAKE) {
dea31012005-04-17 16:05:31 -050010003 retval = MBX_SUCCESS;
James Smartd7c47992010-06-08 18:31:54 -040010004 lpfc_sli4_swap_str(phba, pmboxq);
10005 } else {
James Smart7054a602007-04-25 09:52:34 -040010006 retval = MBX_TIMEOUT;
James Smart858c9f62007-06-17 19:56:39 -050010007 pmboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
10008 }
10009 spin_unlock_irqrestore(&phba->hbalock, flag);
dea31012005-04-17 16:05:31 -050010010 }
10011
dea31012005-04-17 16:05:31 -050010012 return retval;
10013}
10014
James Smarte59058c2008-08-24 21:49:00 -040010015/**
James Smart3772a992009-05-22 14:50:54 -040010016 * lpfc_sli_mbox_sys_shutdown - shutdown mailbox command sub-system
James Smarte59058c2008-08-24 21:49:00 -040010017 * @phba: Pointer to HBA context.
10018 *
James Smart3772a992009-05-22 14:50:54 -040010019 * This function is called to shutdown the driver's mailbox sub-system.
10020 * It first marks the mailbox sub-system is in a block state to prevent
10021 * the asynchronous mailbox command from issued off the pending mailbox
10022 * command queue. If the mailbox command sub-system shutdown is due to
10023 * HBA error conditions such as EEH or ERATT, this routine shall invoke
10024 * the mailbox sub-system flush routine to forcefully bring down the
10025 * mailbox sub-system. Otherwise, if it is due to normal condition (such
10026 * as with offline or HBA function reset), this routine will wait for the
10027 * outstanding mailbox command to complete before invoking the mailbox
10028 * sub-system flush routine to gracefully bring down mailbox sub-system.
James Smarte59058c2008-08-24 21:49:00 -040010029 **/
James Smart3772a992009-05-22 14:50:54 -040010030void
James Smart618a5232012-06-12 13:54:36 -040010031lpfc_sli_mbox_sys_shutdown(struct lpfc_hba *phba, int mbx_action)
James Smartb4c02652006-07-06 15:50:43 -040010032{
James Smart3772a992009-05-22 14:50:54 -040010033 struct lpfc_sli *psli = &phba->sli;
James Smart3772a992009-05-22 14:50:54 -040010034 unsigned long timeout;
10035
James Smart618a5232012-06-12 13:54:36 -040010036 if (mbx_action == LPFC_MBX_NO_WAIT) {
10037 /* delay 100ms for port state */
10038 msleep(100);
10039 lpfc_sli_mbox_sys_flush(phba);
10040 return;
10041 }
James Smarta183a152011-10-10 21:32:43 -040010042 timeout = msecs_to_jiffies(LPFC_MBOX_TMO * 1000) + jiffies;
James Smartd7069f02012-03-01 22:36:29 -050010043
James Smart3772a992009-05-22 14:50:54 -040010044 spin_lock_irq(&phba->hbalock);
10045 psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
James Smart3772a992009-05-22 14:50:54 -040010046
10047 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
James Smart3772a992009-05-22 14:50:54 -040010048 /* Determine how long we might wait for the active mailbox
10049 * command to be gracefully completed by firmware.
10050 */
James Smarta183a152011-10-10 21:32:43 -040010051 if (phba->sli.mbox_active)
10052 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
10053 phba->sli.mbox_active) *
10054 1000) + jiffies;
10055 spin_unlock_irq(&phba->hbalock);
10056
James Smart3772a992009-05-22 14:50:54 -040010057 while (phba->sli.mbox_active) {
10058 /* Check active mailbox complete status every 2ms */
10059 msleep(2);
10060 if (time_after(jiffies, timeout))
10061 /* Timeout, let the mailbox flush routine to
10062 * forcefully release active mailbox command
10063 */
10064 break;
10065 }
James Smartd7069f02012-03-01 22:36:29 -050010066 } else
10067 spin_unlock_irq(&phba->hbalock);
10068
James Smart3772a992009-05-22 14:50:54 -040010069 lpfc_sli_mbox_sys_flush(phba);
10070}
10071
10072/**
10073 * lpfc_sli_eratt_read - read sli-3 error attention events
10074 * @phba: Pointer to HBA context.
10075 *
10076 * This function is called to read the SLI3 device error attention registers
10077 * for possible error attention events. The caller must hold the hostlock
10078 * with spin_lock_irq().
10079 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030010080 * This function returns 1 when there is Error Attention in the Host Attention
James Smart3772a992009-05-22 14:50:54 -040010081 * Register and returns 0 otherwise.
10082 **/
10083static int
10084lpfc_sli_eratt_read(struct lpfc_hba *phba)
10085{
James Smarted957682007-06-17 19:56:37 -050010086 uint32_t ha_copy;
James Smartb4c02652006-07-06 15:50:43 -040010087
James Smart3772a992009-05-22 14:50:54 -040010088 /* Read chip Host Attention (HA) register */
James Smart9940b972011-03-11 16:06:12 -050010089 if (lpfc_readl(phba->HAregaddr, &ha_copy))
10090 goto unplug_err;
10091
James Smart3772a992009-05-22 14:50:54 -040010092 if (ha_copy & HA_ERATT) {
10093 /* Read host status register to retrieve error event */
James Smart9940b972011-03-11 16:06:12 -050010094 if (lpfc_sli_read_hs(phba))
10095 goto unplug_err;
James Smartb4c02652006-07-06 15:50:43 -040010096
James Smart3772a992009-05-22 14:50:54 -040010097 /* Check if there is a deferred error condition is active */
10098 if ((HS_FFER1 & phba->work_hs) &&
10099 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
James Smartdcf2a4e2010-09-29 11:18:53 -040010100 HS_FFER6 | HS_FFER7 | HS_FFER8) & phba->work_hs)) {
James Smart3772a992009-05-22 14:50:54 -040010101 phba->hba_flag |= DEFER_ERATT;
James Smart3772a992009-05-22 14:50:54 -040010102 /* Clear all interrupt enable conditions */
10103 writel(0, phba->HCregaddr);
10104 readl(phba->HCregaddr);
10105 }
10106
10107 /* Set the driver HA work bitmap */
James Smart3772a992009-05-22 14:50:54 -040010108 phba->work_ha |= HA_ERATT;
10109 /* Indicate polling handles this ERATT */
10110 phba->hba_flag |= HBA_ERATT_HANDLED;
James Smart3772a992009-05-22 14:50:54 -040010111 return 1;
James Smartb4c02652006-07-06 15:50:43 -040010112 }
James Smart3772a992009-05-22 14:50:54 -040010113 return 0;
James Smart9940b972011-03-11 16:06:12 -050010114
10115unplug_err:
10116 /* Set the driver HS work bitmap */
10117 phba->work_hs |= UNPLUG_ERR;
10118 /* Set the driver HA work bitmap */
10119 phba->work_ha |= HA_ERATT;
10120 /* Indicate polling handles this ERATT */
10121 phba->hba_flag |= HBA_ERATT_HANDLED;
10122 return 1;
James Smartb4c02652006-07-06 15:50:43 -040010123}
10124
James Smarte59058c2008-08-24 21:49:00 -040010125/**
James Smartda0436e2009-05-22 14:51:39 -040010126 * lpfc_sli4_eratt_read - read sli-4 error attention events
10127 * @phba: Pointer to HBA context.
10128 *
10129 * This function is called to read the SLI4 device error attention registers
10130 * for possible error attention events. The caller must hold the hostlock
10131 * with spin_lock_irq().
10132 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030010133 * This function returns 1 when there is Error Attention in the Host Attention
James Smartda0436e2009-05-22 14:51:39 -040010134 * Register and returns 0 otherwise.
10135 **/
10136static int
10137lpfc_sli4_eratt_read(struct lpfc_hba *phba)
10138{
10139 uint32_t uerr_sta_hi, uerr_sta_lo;
James Smart2fcee4b2010-12-15 17:57:46 -050010140 uint32_t if_type, portsmphr;
10141 struct lpfc_register portstat_reg;
James Smartda0436e2009-05-22 14:51:39 -040010142
James Smart2fcee4b2010-12-15 17:57:46 -050010143 /*
10144 * For now, use the SLI4 device internal unrecoverable error
James Smartda0436e2009-05-22 14:51:39 -040010145 * registers for error attention. This can be changed later.
10146 */
James Smart2fcee4b2010-12-15 17:57:46 -050010147 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
10148 switch (if_type) {
10149 case LPFC_SLI_INTF_IF_TYPE_0:
James Smart9940b972011-03-11 16:06:12 -050010150 if (lpfc_readl(phba->sli4_hba.u.if_type0.UERRLOregaddr,
10151 &uerr_sta_lo) ||
10152 lpfc_readl(phba->sli4_hba.u.if_type0.UERRHIregaddr,
10153 &uerr_sta_hi)) {
10154 phba->work_hs |= UNPLUG_ERR;
10155 phba->work_ha |= HA_ERATT;
10156 phba->hba_flag |= HBA_ERATT_HANDLED;
10157 return 1;
10158 }
James Smart2fcee4b2010-12-15 17:57:46 -050010159 if ((~phba->sli4_hba.ue_mask_lo & uerr_sta_lo) ||
10160 (~phba->sli4_hba.ue_mask_hi & uerr_sta_hi)) {
10161 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10162 "1423 HBA Unrecoverable error: "
10163 "uerr_lo_reg=0x%x, uerr_hi_reg=0x%x, "
10164 "ue_mask_lo_reg=0x%x, "
10165 "ue_mask_hi_reg=0x%x\n",
10166 uerr_sta_lo, uerr_sta_hi,
10167 phba->sli4_hba.ue_mask_lo,
10168 phba->sli4_hba.ue_mask_hi);
10169 phba->work_status[0] = uerr_sta_lo;
10170 phba->work_status[1] = uerr_sta_hi;
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_2:
James Smart9940b972011-03-11 16:06:12 -050010177 if (lpfc_readl(phba->sli4_hba.u.if_type2.STATUSregaddr,
10178 &portstat_reg.word0) ||
10179 lpfc_readl(phba->sli4_hba.PSMPHRregaddr,
10180 &portsmphr)){
10181 phba->work_hs |= UNPLUG_ERR;
10182 phba->work_ha |= HA_ERATT;
10183 phba->hba_flag |= HBA_ERATT_HANDLED;
10184 return 1;
10185 }
James Smart2fcee4b2010-12-15 17:57:46 -050010186 if (bf_get(lpfc_sliport_status_err, &portstat_reg)) {
10187 phba->work_status[0] =
10188 readl(phba->sli4_hba.u.if_type2.ERR1regaddr);
10189 phba->work_status[1] =
10190 readl(phba->sli4_hba.u.if_type2.ERR2regaddr);
10191 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart2e90f4b2011-12-13 13:22:37 -050010192 "2885 Port Status Event: "
James Smart2fcee4b2010-12-15 17:57:46 -050010193 "port status reg 0x%x, "
10194 "port smphr reg 0x%x, "
10195 "error 1=0x%x, error 2=0x%x\n",
10196 portstat_reg.word0,
10197 portsmphr,
10198 phba->work_status[0],
10199 phba->work_status[1]);
10200 phba->work_ha |= HA_ERATT;
10201 phba->hba_flag |= HBA_ERATT_HANDLED;
10202 return 1;
10203 }
10204 break;
10205 case LPFC_SLI_INTF_IF_TYPE_1:
10206 default:
James Smarta747c9c2009-11-18 15:41:10 -050010207 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart2fcee4b2010-12-15 17:57:46 -050010208 "2886 HBA Error Attention on unsupported "
10209 "if type %d.", if_type);
James Smarta747c9c2009-11-18 15:41:10 -050010210 return 1;
James Smartda0436e2009-05-22 14:51:39 -040010211 }
James Smart2fcee4b2010-12-15 17:57:46 -050010212
James Smartda0436e2009-05-22 14:51:39 -040010213 return 0;
10214}
10215
10216/**
James Smart3621a712009-04-06 18:47:14 -040010217 * lpfc_sli_check_eratt - check error attention events
James Smart93996272008-08-24 21:50:30 -040010218 * @phba: Pointer to HBA context.
10219 *
James Smart3772a992009-05-22 14:50:54 -040010220 * This function is called from timer soft interrupt context to check HBA's
James Smart93996272008-08-24 21:50:30 -040010221 * error attention register bit for error attention events.
10222 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030010223 * This function returns 1 when there is Error Attention in the Host Attention
James Smart93996272008-08-24 21:50:30 -040010224 * Register and returns 0 otherwise.
10225 **/
10226int
10227lpfc_sli_check_eratt(struct lpfc_hba *phba)
10228{
10229 uint32_t ha_copy;
10230
10231 /* If somebody is waiting to handle an eratt, don't process it
10232 * here. The brdkill function will do this.
10233 */
10234 if (phba->link_flag & LS_IGNORE_ERATT)
10235 return 0;
10236
10237 /* Check if interrupt handler handles this ERATT */
10238 spin_lock_irq(&phba->hbalock);
10239 if (phba->hba_flag & HBA_ERATT_HANDLED) {
10240 /* Interrupt handler has handled ERATT */
10241 spin_unlock_irq(&phba->hbalock);
10242 return 0;
10243 }
10244
James Smarta257bf92009-04-06 18:48:10 -040010245 /*
10246 * If there is deferred error attention, do not check for error
10247 * attention
10248 */
10249 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
10250 spin_unlock_irq(&phba->hbalock);
10251 return 0;
10252 }
10253
James Smart3772a992009-05-22 14:50:54 -040010254 /* If PCI channel is offline, don't process it */
10255 if (unlikely(pci_channel_offline(phba->pcidev))) {
James Smart93996272008-08-24 21:50:30 -040010256 spin_unlock_irq(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -040010257 return 0;
10258 }
10259
10260 switch (phba->sli_rev) {
10261 case LPFC_SLI_REV2:
10262 case LPFC_SLI_REV3:
10263 /* Read chip Host Attention (HA) register */
10264 ha_copy = lpfc_sli_eratt_read(phba);
10265 break;
James Smartda0436e2009-05-22 14:51:39 -040010266 case LPFC_SLI_REV4:
James Smart2fcee4b2010-12-15 17:57:46 -050010267 /* Read device Uncoverable Error (UERR) registers */
James Smartda0436e2009-05-22 14:51:39 -040010268 ha_copy = lpfc_sli4_eratt_read(phba);
10269 break;
James Smart3772a992009-05-22 14:50:54 -040010270 default:
10271 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10272 "0299 Invalid SLI revision (%d)\n",
10273 phba->sli_rev);
10274 ha_copy = 0;
10275 break;
James Smart93996272008-08-24 21:50:30 -040010276 }
10277 spin_unlock_irq(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -040010278
10279 return ha_copy;
10280}
10281
10282/**
10283 * lpfc_intr_state_check - Check device state for interrupt handling
10284 * @phba: Pointer to HBA context.
10285 *
10286 * This inline routine checks whether a device or its PCI slot is in a state
10287 * that the interrupt should be handled.
10288 *
10289 * This function returns 0 if the device or the PCI slot is in a state that
10290 * interrupt should be handled, otherwise -EIO.
10291 */
10292static inline int
10293lpfc_intr_state_check(struct lpfc_hba *phba)
10294{
10295 /* If the pci channel is offline, ignore all the interrupts */
10296 if (unlikely(pci_channel_offline(phba->pcidev)))
10297 return -EIO;
10298
10299 /* Update device level interrupt statistics */
10300 phba->sli.slistat.sli_intr++;
10301
10302 /* Ignore all interrupts during initialization. */
10303 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
10304 return -EIO;
10305
James Smart93996272008-08-24 21:50:30 -040010306 return 0;
10307}
10308
10309/**
James Smart3772a992009-05-22 14:50:54 -040010310 * lpfc_sli_sp_intr_handler - Slow-path interrupt handler to SLI-3 device
James Smarte59058c2008-08-24 21:49:00 -040010311 * @irq: Interrupt number.
10312 * @dev_id: The device context pointer.
10313 *
James Smart93996272008-08-24 21:50:30 -040010314 * This function is directly called from the PCI layer as an interrupt
James Smart3772a992009-05-22 14:50:54 -040010315 * service routine when device with SLI-3 interface spec is enabled with
10316 * MSI-X multi-message interrupt mode and there are slow-path events in
10317 * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
10318 * interrupt mode, this function is called as part of the device-level
10319 * interrupt handler. When the PCI slot is in error recovery or the HBA
10320 * is undergoing initialization, the interrupt handler will not process
10321 * the interrupt. The link attention and ELS ring attention events are
10322 * handled by the worker thread. The interrupt handler signals the worker
10323 * thread and returns for these events. This function is called without
10324 * any lock held. It gets the hbalock to access and update SLI data
James Smart93996272008-08-24 21:50:30 -040010325 * structures.
10326 *
10327 * This function returns IRQ_HANDLED when interrupt is handled else it
10328 * returns IRQ_NONE.
James Smarte59058c2008-08-24 21:49:00 -040010329 **/
dea31012005-04-17 16:05:31 -050010330irqreturn_t
James Smart3772a992009-05-22 14:50:54 -040010331lpfc_sli_sp_intr_handler(int irq, void *dev_id)
dea31012005-04-17 16:05:31 -050010332{
James Smart2e0fef82007-06-17 19:56:36 -050010333 struct lpfc_hba *phba;
James Smarta747c9c2009-11-18 15:41:10 -050010334 uint32_t ha_copy, hc_copy;
dea31012005-04-17 16:05:31 -050010335 uint32_t work_ha_copy;
10336 unsigned long status;
James Smart5b75da22008-12-04 22:39:35 -050010337 unsigned long iflag;
dea31012005-04-17 16:05:31 -050010338 uint32_t control;
10339
James Smart92d7f7b2007-06-17 19:56:38 -050010340 MAILBOX_t *mbox, *pmbox;
James Smart858c9f62007-06-17 19:56:39 -050010341 struct lpfc_vport *vport;
10342 struct lpfc_nodelist *ndlp;
10343 struct lpfc_dmabuf *mp;
James Smart92d7f7b2007-06-17 19:56:38 -050010344 LPFC_MBOXQ_t *pmb;
10345 int rc;
10346
dea31012005-04-17 16:05:31 -050010347 /*
10348 * Get the driver's phba structure from the dev_id and
10349 * assume the HBA is not interrupting.
10350 */
James Smart93996272008-08-24 21:50:30 -040010351 phba = (struct lpfc_hba *)dev_id;
dea31012005-04-17 16:05:31 -050010352
10353 if (unlikely(!phba))
10354 return IRQ_NONE;
10355
dea31012005-04-17 16:05:31 -050010356 /*
James Smart93996272008-08-24 21:50:30 -040010357 * Stuff needs to be attented to when this function is invoked as an
10358 * individual interrupt handler in MSI-X multi-message interrupt mode
dea31012005-04-17 16:05:31 -050010359 */
James Smart93996272008-08-24 21:50:30 -040010360 if (phba->intr_type == MSIX) {
James Smart3772a992009-05-22 14:50:54 -040010361 /* Check device state for handling interrupt */
10362 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -040010363 return IRQ_NONE;
10364 /* Need to read HA REG for slow-path events */
James Smart5b75da22008-12-04 22:39:35 -050010365 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart9940b972011-03-11 16:06:12 -050010366 if (lpfc_readl(phba->HAregaddr, &ha_copy))
10367 goto unplug_error;
James Smart93996272008-08-24 21:50:30 -040010368 /* If somebody is waiting to handle an eratt don't process it
10369 * here. The brdkill function will do this.
10370 */
10371 if (phba->link_flag & LS_IGNORE_ERATT)
10372 ha_copy &= ~HA_ERATT;
10373 /* Check the need for handling ERATT in interrupt handler */
10374 if (ha_copy & HA_ERATT) {
10375 if (phba->hba_flag & HBA_ERATT_HANDLED)
10376 /* ERATT polling has handled ERATT */
10377 ha_copy &= ~HA_ERATT;
10378 else
10379 /* Indicate interrupt handler handles ERATT */
10380 phba->hba_flag |= HBA_ERATT_HANDLED;
10381 }
James Smarta257bf92009-04-06 18:48:10 -040010382
10383 /*
10384 * If there is deferred error attention, do not check for any
10385 * interrupt.
10386 */
10387 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
James Smart3772a992009-05-22 14:50:54 -040010388 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -040010389 return IRQ_NONE;
10390 }
10391
James Smart93996272008-08-24 21:50:30 -040010392 /* Clear up only attention source related to slow-path */
James Smart9940b972011-03-11 16:06:12 -050010393 if (lpfc_readl(phba->HCregaddr, &hc_copy))
10394 goto unplug_error;
10395
James Smarta747c9c2009-11-18 15:41:10 -050010396 writel(hc_copy & ~(HC_MBINT_ENA | HC_R2INT_ENA |
10397 HC_LAINT_ENA | HC_ERINT_ENA),
10398 phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -040010399 writel((ha_copy & (HA_MBATT | HA_R2_CLR_MSK)),
10400 phba->HAregaddr);
James Smarta747c9c2009-11-18 15:41:10 -050010401 writel(hc_copy, phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -040010402 readl(phba->HAregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -050010403 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -040010404 } else
10405 ha_copy = phba->ha_copy;
dea31012005-04-17 16:05:31 -050010406
dea31012005-04-17 16:05:31 -050010407 work_ha_copy = ha_copy & phba->work_ha_mask;
10408
James Smart93996272008-08-24 21:50:30 -040010409 if (work_ha_copy) {
dea31012005-04-17 16:05:31 -050010410 if (work_ha_copy & HA_LATT) {
10411 if (phba->sli.sli_flag & LPFC_PROCESS_LA) {
10412 /*
10413 * Turn off Link Attention interrupts
10414 * until CLEAR_LA done
10415 */
James Smart5b75da22008-12-04 22:39:35 -050010416 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -050010417 phba->sli.sli_flag &= ~LPFC_PROCESS_LA;
James Smart9940b972011-03-11 16:06:12 -050010418 if (lpfc_readl(phba->HCregaddr, &control))
10419 goto unplug_error;
dea31012005-04-17 16:05:31 -050010420 control &= ~HC_LAINT_ENA;
10421 writel(control, phba->HCregaddr);
10422 readl(phba->HCregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -050010423 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -050010424 }
10425 else
10426 work_ha_copy &= ~HA_LATT;
10427 }
10428
James Smart93996272008-08-24 21:50:30 -040010429 if (work_ha_copy & ~(HA_ERATT | HA_MBATT | HA_LATT)) {
James Smart858c9f62007-06-17 19:56:39 -050010430 /*
10431 * Turn off Slow Rings interrupts, LPFC_ELS_RING is
10432 * the only slow ring.
10433 */
10434 status = (work_ha_copy &
10435 (HA_RXMASK << (4*LPFC_ELS_RING)));
10436 status >>= (4*LPFC_ELS_RING);
10437 if (status & HA_RXMASK) {
James Smart5b75da22008-12-04 22:39:35 -050010438 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart9940b972011-03-11 16:06:12 -050010439 if (lpfc_readl(phba->HCregaddr, &control))
10440 goto unplug_error;
James Smarta58cbd52007-08-02 11:09:43 -040010441
10442 lpfc_debugfs_slow_ring_trc(phba,
10443 "ISR slow ring: ctl:x%x stat:x%x isrcnt:x%x",
10444 control, status,
10445 (uint32_t)phba->sli.slistat.sli_intr);
10446
James Smart858c9f62007-06-17 19:56:39 -050010447 if (control & (HC_R0INT_ENA << LPFC_ELS_RING)) {
James Smarta58cbd52007-08-02 11:09:43 -040010448 lpfc_debugfs_slow_ring_trc(phba,
10449 "ISR Disable ring:"
10450 "pwork:x%x hawork:x%x wait:x%x",
10451 phba->work_ha, work_ha_copy,
10452 (uint32_t)((unsigned long)
James Smart5e9d9b82008-06-14 22:52:53 -040010453 &phba->work_waitq));
James Smarta58cbd52007-08-02 11:09:43 -040010454
James Smart858c9f62007-06-17 19:56:39 -050010455 control &=
10456 ~(HC_R0INT_ENA << LPFC_ELS_RING);
dea31012005-04-17 16:05:31 -050010457 writel(control, phba->HCregaddr);
10458 readl(phba->HCregaddr); /* flush */
dea31012005-04-17 16:05:31 -050010459 }
James Smarta58cbd52007-08-02 11:09:43 -040010460 else {
10461 lpfc_debugfs_slow_ring_trc(phba,
10462 "ISR slow ring: pwork:"
10463 "x%x hawork:x%x wait:x%x",
10464 phba->work_ha, work_ha_copy,
10465 (uint32_t)((unsigned long)
James Smart5e9d9b82008-06-14 22:52:53 -040010466 &phba->work_waitq));
James Smarta58cbd52007-08-02 11:09:43 -040010467 }
James Smart5b75da22008-12-04 22:39:35 -050010468 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -050010469 }
10470 }
James Smart5b75da22008-12-04 22:39:35 -050010471 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -040010472 if (work_ha_copy & HA_ERATT) {
James Smart9940b972011-03-11 16:06:12 -050010473 if (lpfc_sli_read_hs(phba))
10474 goto unplug_error;
James Smarta257bf92009-04-06 18:48:10 -040010475 /*
10476 * Check if there is a deferred error condition
10477 * is active
10478 */
10479 if ((HS_FFER1 & phba->work_hs) &&
10480 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
James Smartdcf2a4e2010-09-29 11:18:53 -040010481 HS_FFER6 | HS_FFER7 | HS_FFER8) &
10482 phba->work_hs)) {
James Smarta257bf92009-04-06 18:48:10 -040010483 phba->hba_flag |= DEFER_ERATT;
10484 /* Clear all interrupt enable conditions */
10485 writel(0, phba->HCregaddr);
10486 readl(phba->HCregaddr);
10487 }
10488 }
10489
James Smart93996272008-08-24 21:50:30 -040010490 if ((work_ha_copy & HA_MBATT) && (phba->sli.mbox_active)) {
James Smart92d7f7b2007-06-17 19:56:38 -050010491 pmb = phba->sli.mbox_active;
James Smart04c68492009-05-22 14:52:52 -040010492 pmbox = &pmb->u.mb;
James Smart34b02dc2008-08-24 21:49:55 -040010493 mbox = phba->mbox;
James Smart858c9f62007-06-17 19:56:39 -050010494 vport = pmb->vport;
James Smart92d7f7b2007-06-17 19:56:38 -050010495
10496 /* First check out the status word */
10497 lpfc_sli_pcimem_bcopy(mbox, pmbox, sizeof(uint32_t));
10498 if (pmbox->mbxOwner != OWN_HOST) {
James Smart5b75da22008-12-04 22:39:35 -050010499 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart92d7f7b2007-06-17 19:56:38 -050010500 /*
10501 * Stray Mailbox Interrupt, mbxCommand <cmd>
10502 * mbxStatus <status>
10503 */
James Smart09372822008-01-11 01:52:54 -050010504 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
James Smart92d7f7b2007-06-17 19:56:38 -050010505 LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -040010506 "(%d):0304 Stray Mailbox "
James Smart92d7f7b2007-06-17 19:56:38 -050010507 "Interrupt mbxCommand x%x "
10508 "mbxStatus x%x\n",
James Smarte8b62012007-08-02 11:10:09 -040010509 (vport ? vport->vpi : 0),
James Smart92d7f7b2007-06-17 19:56:38 -050010510 pmbox->mbxCommand,
10511 pmbox->mbxStatus);
James Smart09372822008-01-11 01:52:54 -050010512 /* clear mailbox attention bit */
10513 work_ha_copy &= ~HA_MBATT;
10514 } else {
James Smart97eab632008-04-07 10:16:05 -040010515 phba->sli.mbox_active = NULL;
James Smart5b75da22008-12-04 22:39:35 -050010516 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart09372822008-01-11 01:52:54 -050010517 phba->last_completion_time = jiffies;
10518 del_timer(&phba->sli.mbox_tmo);
James Smart09372822008-01-11 01:52:54 -050010519 if (pmb->mbox_cmpl) {
10520 lpfc_sli_pcimem_bcopy(mbox, pmbox,
10521 MAILBOX_CMD_SIZE);
James Smart7a470272010-03-15 11:25:20 -040010522 if (pmb->out_ext_byte_len &&
10523 pmb->context2)
10524 lpfc_sli_pcimem_bcopy(
10525 phba->mbox_ext,
10526 pmb->context2,
10527 pmb->out_ext_byte_len);
James Smart858c9f62007-06-17 19:56:39 -050010528 }
James Smart09372822008-01-11 01:52:54 -050010529 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
10530 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
10531
10532 lpfc_debugfs_disc_trc(vport,
10533 LPFC_DISC_TRC_MBOX_VPORT,
10534 "MBOX dflt rpi: : "
10535 "status:x%x rpi:x%x",
10536 (uint32_t)pmbox->mbxStatus,
10537 pmbox->un.varWords[0], 0);
10538
10539 if (!pmbox->mbxStatus) {
10540 mp = (struct lpfc_dmabuf *)
10541 (pmb->context1);
10542 ndlp = (struct lpfc_nodelist *)
10543 pmb->context2;
10544
10545 /* Reg_LOGIN of dflt RPI was
10546 * successful. new lets get
10547 * rid of the RPI using the
10548 * same mbox buffer.
10549 */
10550 lpfc_unreg_login(phba,
10551 vport->vpi,
10552 pmbox->un.varWords[0],
10553 pmb);
10554 pmb->mbox_cmpl =
10555 lpfc_mbx_cmpl_dflt_rpi;
10556 pmb->context1 = mp;
10557 pmb->context2 = ndlp;
10558 pmb->vport = vport;
James Smart58da1ff2008-04-07 10:15:56 -040010559 rc = lpfc_sli_issue_mbox(phba,
10560 pmb,
10561 MBX_NOWAIT);
10562 if (rc != MBX_BUSY)
10563 lpfc_printf_log(phba,
10564 KERN_ERR,
10565 LOG_MBOX | LOG_SLI,
James Smartd7c255b2008-08-24 21:50:00 -040010566 "0350 rc should have"
James Smart6a9c52c2009-10-02 15:16:51 -040010567 "been MBX_BUSY\n");
James Smart3772a992009-05-22 14:50:54 -040010568 if (rc != MBX_NOT_FINISHED)
10569 goto send_current_mbox;
James Smart09372822008-01-11 01:52:54 -050010570 }
10571 }
James Smart5b75da22008-12-04 22:39:35 -050010572 spin_lock_irqsave(
10573 &phba->pport->work_port_lock,
10574 iflag);
James Smart09372822008-01-11 01:52:54 -050010575 phba->pport->work_port_events &=
10576 ~WORKER_MBOX_TMO;
James Smart5b75da22008-12-04 22:39:35 -050010577 spin_unlock_irqrestore(
10578 &phba->pport->work_port_lock,
10579 iflag);
James Smart09372822008-01-11 01:52:54 -050010580 lpfc_mbox_cmpl_put(phba, pmb);
James Smart858c9f62007-06-17 19:56:39 -050010581 }
James Smart97eab632008-04-07 10:16:05 -040010582 } else
James Smart5b75da22008-12-04 22:39:35 -050010583 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -040010584
James Smart92d7f7b2007-06-17 19:56:38 -050010585 if ((work_ha_copy & HA_MBATT) &&
10586 (phba->sli.mbox_active == NULL)) {
James Smart858c9f62007-06-17 19:56:39 -050010587send_current_mbox:
James Smart92d7f7b2007-06-17 19:56:38 -050010588 /* Process next mailbox command if there is one */
James Smart58da1ff2008-04-07 10:15:56 -040010589 do {
10590 rc = lpfc_sli_issue_mbox(phba, NULL,
10591 MBX_NOWAIT);
10592 } while (rc == MBX_NOT_FINISHED);
10593 if (rc != MBX_SUCCESS)
10594 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
10595 LOG_SLI, "0349 rc should be "
James Smart6a9c52c2009-10-02 15:16:51 -040010596 "MBX_SUCCESS\n");
James Smart92d7f7b2007-06-17 19:56:38 -050010597 }
10598
James Smart5b75da22008-12-04 22:39:35 -050010599 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -050010600 phba->work_ha |= work_ha_copy;
James Smart5b75da22008-12-04 22:39:35 -050010601 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart5e9d9b82008-06-14 22:52:53 -040010602 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -050010603 }
James Smart93996272008-08-24 21:50:30 -040010604 return IRQ_HANDLED;
James Smart9940b972011-03-11 16:06:12 -050010605unplug_error:
10606 spin_unlock_irqrestore(&phba->hbalock, iflag);
10607 return IRQ_HANDLED;
dea31012005-04-17 16:05:31 -050010608
James Smart3772a992009-05-22 14:50:54 -040010609} /* lpfc_sli_sp_intr_handler */
James Smart93996272008-08-24 21:50:30 -040010610
10611/**
James Smart3772a992009-05-22 14:50:54 -040010612 * lpfc_sli_fp_intr_handler - Fast-path interrupt handler to SLI-3 device.
James Smart93996272008-08-24 21:50:30 -040010613 * @irq: Interrupt number.
10614 * @dev_id: The device context pointer.
10615 *
10616 * This function is directly called from the PCI layer as an interrupt
James Smart3772a992009-05-22 14:50:54 -040010617 * service routine when device with SLI-3 interface spec is enabled with
10618 * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
10619 * ring event in the HBA. However, when the device is enabled with either
10620 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
10621 * device-level interrupt handler. When the PCI slot is in error recovery
10622 * or the HBA is undergoing initialization, the interrupt handler will not
10623 * process the interrupt. The SCSI FCP fast-path ring event are handled in
10624 * the intrrupt context. This function is called without any lock held.
10625 * It gets the hbalock to access and update SLI data structures.
James Smart93996272008-08-24 21:50:30 -040010626 *
10627 * This function returns IRQ_HANDLED when interrupt is handled else it
10628 * returns IRQ_NONE.
10629 **/
10630irqreturn_t
James Smart3772a992009-05-22 14:50:54 -040010631lpfc_sli_fp_intr_handler(int irq, void *dev_id)
James Smart93996272008-08-24 21:50:30 -040010632{
10633 struct lpfc_hba *phba;
10634 uint32_t ha_copy;
10635 unsigned long status;
James Smart5b75da22008-12-04 22:39:35 -050010636 unsigned long iflag;
James Smart93996272008-08-24 21:50:30 -040010637
10638 /* Get the driver's phba structure from the dev_id and
10639 * assume the HBA is not interrupting.
10640 */
10641 phba = (struct lpfc_hba *) dev_id;
10642
10643 if (unlikely(!phba))
10644 return IRQ_NONE;
dea31012005-04-17 16:05:31 -050010645
10646 /*
James Smart93996272008-08-24 21:50:30 -040010647 * Stuff needs to be attented to when this function is invoked as an
10648 * individual interrupt handler in MSI-X multi-message interrupt mode
dea31012005-04-17 16:05:31 -050010649 */
James Smart93996272008-08-24 21:50:30 -040010650 if (phba->intr_type == MSIX) {
James Smart3772a992009-05-22 14:50:54 -040010651 /* Check device state for handling interrupt */
10652 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -040010653 return IRQ_NONE;
10654 /* Need to read HA REG for FCP ring and other ring events */
James Smart9940b972011-03-11 16:06:12 -050010655 if (lpfc_readl(phba->HAregaddr, &ha_copy))
10656 return IRQ_HANDLED;
James Smart93996272008-08-24 21:50:30 -040010657 /* Clear up only attention source related to fast-path */
James Smart5b75da22008-12-04 22:39:35 -050010658 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -040010659 /*
10660 * If there is deferred error attention, do not check for
10661 * any interrupt.
10662 */
10663 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
James Smart3772a992009-05-22 14:50:54 -040010664 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -040010665 return IRQ_NONE;
10666 }
James Smart93996272008-08-24 21:50:30 -040010667 writel((ha_copy & (HA_R0_CLR_MSK | HA_R1_CLR_MSK)),
10668 phba->HAregaddr);
10669 readl(phba->HAregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -050010670 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -040010671 } else
10672 ha_copy = phba->ha_copy;
10673
10674 /*
10675 * Process all events on FCP ring. Take the optimized path for FCP IO.
10676 */
10677 ha_copy &= ~(phba->work_ha_mask);
10678
10679 status = (ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
dea31012005-04-17 16:05:31 -050010680 status >>= (4*LPFC_FCP_RING);
James Smart858c9f62007-06-17 19:56:39 -050010681 if (status & HA_RXMASK)
dea31012005-04-17 16:05:31 -050010682 lpfc_sli_handle_fast_ring_event(phba,
10683 &phba->sli.ring[LPFC_FCP_RING],
10684 status);
James Smarta4bc3372006-12-02 13:34:16 -050010685
10686 if (phba->cfg_multi_ring_support == 2) {
10687 /*
James Smart93996272008-08-24 21:50:30 -040010688 * Process all events on extra ring. Take the optimized path
10689 * for extra ring IO.
James Smarta4bc3372006-12-02 13:34:16 -050010690 */
James Smart93996272008-08-24 21:50:30 -040010691 status = (ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
James Smarta4bc3372006-12-02 13:34:16 -050010692 status >>= (4*LPFC_EXTRA_RING);
James Smart858c9f62007-06-17 19:56:39 -050010693 if (status & HA_RXMASK) {
James Smarta4bc3372006-12-02 13:34:16 -050010694 lpfc_sli_handle_fast_ring_event(phba,
10695 &phba->sli.ring[LPFC_EXTRA_RING],
10696 status);
10697 }
10698 }
dea31012005-04-17 16:05:31 -050010699 return IRQ_HANDLED;
James Smart3772a992009-05-22 14:50:54 -040010700} /* lpfc_sli_fp_intr_handler */
dea31012005-04-17 16:05:31 -050010701
James Smart93996272008-08-24 21:50:30 -040010702/**
James Smart3772a992009-05-22 14:50:54 -040010703 * lpfc_sli_intr_handler - Device-level interrupt handler to SLI-3 device
James Smart93996272008-08-24 21:50:30 -040010704 * @irq: Interrupt number.
10705 * @dev_id: The device context pointer.
10706 *
James Smart3772a992009-05-22 14:50:54 -040010707 * This function is the HBA device-level interrupt handler to device with
10708 * SLI-3 interface spec, called from the PCI layer when either MSI or
10709 * Pin-IRQ interrupt mode is enabled and there is an event in the HBA which
10710 * requires driver attention. This function invokes the slow-path interrupt
10711 * attention handling function and fast-path interrupt attention handling
10712 * function in turn to process the relevant HBA attention events. This
10713 * function is called without any lock held. It gets the hbalock to access
10714 * and update SLI data structures.
James Smart93996272008-08-24 21:50:30 -040010715 *
10716 * This function returns IRQ_HANDLED when interrupt is handled, else it
10717 * returns IRQ_NONE.
10718 **/
10719irqreturn_t
James Smart3772a992009-05-22 14:50:54 -040010720lpfc_sli_intr_handler(int irq, void *dev_id)
James Smart93996272008-08-24 21:50:30 -040010721{
10722 struct lpfc_hba *phba;
10723 irqreturn_t sp_irq_rc, fp_irq_rc;
10724 unsigned long status1, status2;
James Smarta747c9c2009-11-18 15:41:10 -050010725 uint32_t hc_copy;
James Smart93996272008-08-24 21:50:30 -040010726
10727 /*
10728 * Get the driver's phba structure from the dev_id and
10729 * assume the HBA is not interrupting.
10730 */
10731 phba = (struct lpfc_hba *) dev_id;
10732
10733 if (unlikely(!phba))
10734 return IRQ_NONE;
10735
James Smart3772a992009-05-22 14:50:54 -040010736 /* Check device state for handling interrupt */
10737 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -040010738 return IRQ_NONE;
10739
10740 spin_lock(&phba->hbalock);
James Smart9940b972011-03-11 16:06:12 -050010741 if (lpfc_readl(phba->HAregaddr, &phba->ha_copy)) {
10742 spin_unlock(&phba->hbalock);
10743 return IRQ_HANDLED;
10744 }
10745
James Smart93996272008-08-24 21:50:30 -040010746 if (unlikely(!phba->ha_copy)) {
10747 spin_unlock(&phba->hbalock);
10748 return IRQ_NONE;
10749 } else if (phba->ha_copy & HA_ERATT) {
10750 if (phba->hba_flag & HBA_ERATT_HANDLED)
10751 /* ERATT polling has handled ERATT */
10752 phba->ha_copy &= ~HA_ERATT;
10753 else
10754 /* Indicate interrupt handler handles ERATT */
10755 phba->hba_flag |= HBA_ERATT_HANDLED;
10756 }
10757
James Smarta257bf92009-04-06 18:48:10 -040010758 /*
10759 * If there is deferred error attention, do not check for any interrupt.
10760 */
10761 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
Dan Carpenterec21b3b2010-08-08 00:15:17 +020010762 spin_unlock(&phba->hbalock);
James Smarta257bf92009-04-06 18:48:10 -040010763 return IRQ_NONE;
10764 }
10765
James Smart93996272008-08-24 21:50:30 -040010766 /* Clear attention sources except link and error attentions */
James Smart9940b972011-03-11 16:06:12 -050010767 if (lpfc_readl(phba->HCregaddr, &hc_copy)) {
10768 spin_unlock(&phba->hbalock);
10769 return IRQ_HANDLED;
10770 }
James Smarta747c9c2009-11-18 15:41:10 -050010771 writel(hc_copy & ~(HC_MBINT_ENA | HC_R0INT_ENA | HC_R1INT_ENA
10772 | HC_R2INT_ENA | HC_LAINT_ENA | HC_ERINT_ENA),
10773 phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -040010774 writel((phba->ha_copy & ~(HA_LATT | HA_ERATT)), phba->HAregaddr);
James Smarta747c9c2009-11-18 15:41:10 -050010775 writel(hc_copy, phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -040010776 readl(phba->HAregaddr); /* flush */
10777 spin_unlock(&phba->hbalock);
10778
10779 /*
10780 * Invokes slow-path host attention interrupt handling as appropriate.
10781 */
10782
10783 /* status of events with mailbox and link attention */
10784 status1 = phba->ha_copy & (HA_MBATT | HA_LATT | HA_ERATT);
10785
10786 /* status of events with ELS ring */
10787 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_ELS_RING)));
10788 status2 >>= (4*LPFC_ELS_RING);
10789
10790 if (status1 || (status2 & HA_RXMASK))
James Smart3772a992009-05-22 14:50:54 -040010791 sp_irq_rc = lpfc_sli_sp_intr_handler(irq, dev_id);
James Smart93996272008-08-24 21:50:30 -040010792 else
10793 sp_irq_rc = IRQ_NONE;
10794
10795 /*
10796 * Invoke fast-path host attention interrupt handling as appropriate.
10797 */
10798
10799 /* status of events with FCP ring */
10800 status1 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
10801 status1 >>= (4*LPFC_FCP_RING);
10802
10803 /* status of events with extra ring */
10804 if (phba->cfg_multi_ring_support == 2) {
10805 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
10806 status2 >>= (4*LPFC_EXTRA_RING);
10807 } else
10808 status2 = 0;
10809
10810 if ((status1 & HA_RXMASK) || (status2 & HA_RXMASK))
James Smart3772a992009-05-22 14:50:54 -040010811 fp_irq_rc = lpfc_sli_fp_intr_handler(irq, dev_id);
James Smart93996272008-08-24 21:50:30 -040010812 else
10813 fp_irq_rc = IRQ_NONE;
10814
10815 /* Return device-level interrupt handling status */
10816 return (sp_irq_rc == IRQ_HANDLED) ? sp_irq_rc : fp_irq_rc;
James Smart3772a992009-05-22 14:50:54 -040010817} /* lpfc_sli_intr_handler */
James Smart4f774512009-05-22 14:52:35 -040010818
10819/**
10820 * lpfc_sli4_fcp_xri_abort_event_proc - Process fcp 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 FCP abort XRI events.
10825 **/
10826void lpfc_sli4_fcp_xri_abort_event_proc(struct lpfc_hba *phba)
10827{
10828 struct lpfc_cq_event *cq_event;
10829
10830 /* First, declare the fcp xri abort event has been handled */
10831 spin_lock_irq(&phba->hbalock);
10832 phba->hba_flag &= ~FCP_XRI_ABORT_EVENT;
10833 spin_unlock_irq(&phba->hbalock);
10834 /* Now, handle all the fcp xri abort events */
10835 while (!list_empty(&phba->sli4_hba.sp_fcp_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_fcp_xri_aborted_work_queue,
10839 cq_event, struct lpfc_cq_event, list);
10840 spin_unlock_irq(&phba->hbalock);
10841 /* Notify aborted XRI for FCP work queue */
10842 lpfc_sli4_fcp_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
10848/**
10849 * lpfc_sli4_els_xri_abort_event_proc - Process els xri abort event
10850 * @phba: pointer to lpfc hba data structure.
10851 *
10852 * This routine is invoked by the worker thread to process all the pending
10853 * SLI4 els abort xri events.
10854 **/
10855void lpfc_sli4_els_xri_abort_event_proc(struct lpfc_hba *phba)
10856{
10857 struct lpfc_cq_event *cq_event;
10858
10859 /* First, declare the els xri abort event has been handled */
10860 spin_lock_irq(&phba->hbalock);
10861 phba->hba_flag &= ~ELS_XRI_ABORT_EVENT;
10862 spin_unlock_irq(&phba->hbalock);
10863 /* Now, handle all the els xri abort events */
10864 while (!list_empty(&phba->sli4_hba.sp_els_xri_aborted_work_queue)) {
10865 /* Get the first event from the head of the event queue */
10866 spin_lock_irq(&phba->hbalock);
10867 list_remove_head(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
10868 cq_event, struct lpfc_cq_event, list);
10869 spin_unlock_irq(&phba->hbalock);
10870 /* Notify aborted XRI for ELS work queue */
10871 lpfc_sli4_els_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
10872 /* Free the event processed back to the free pool */
10873 lpfc_sli4_cq_event_release(phba, cq_event);
10874 }
10875}
10876
James Smart341af102010-01-26 23:07:37 -050010877/**
10878 * lpfc_sli4_iocb_param_transfer - Transfer pIocbOut and cmpl status to pIocbIn
10879 * @phba: pointer to lpfc hba data structure
10880 * @pIocbIn: pointer to the rspiocbq
10881 * @pIocbOut: pointer to the cmdiocbq
10882 * @wcqe: pointer to the complete wcqe
10883 *
10884 * This routine transfers the fields of a command iocbq to a response iocbq
10885 * by copying all the IOCB fields from command iocbq and transferring the
10886 * completion status information from the complete wcqe.
10887 **/
James Smart4f774512009-05-22 14:52:35 -040010888static void
James Smart341af102010-01-26 23:07:37 -050010889lpfc_sli4_iocb_param_transfer(struct lpfc_hba *phba,
10890 struct lpfc_iocbq *pIocbIn,
James Smart4f774512009-05-22 14:52:35 -040010891 struct lpfc_iocbq *pIocbOut,
10892 struct lpfc_wcqe_complete *wcqe)
10893{
James Smart341af102010-01-26 23:07:37 -050010894 unsigned long iflags;
James Smartacd68592012-01-18 16:25:09 -050010895 uint32_t status;
James Smart4f774512009-05-22 14:52:35 -040010896 size_t offset = offsetof(struct lpfc_iocbq, iocb);
10897
10898 memcpy((char *)pIocbIn + offset, (char *)pIocbOut + offset,
10899 sizeof(struct lpfc_iocbq) - offset);
James Smart4f774512009-05-22 14:52:35 -040010900 /* Map WCQE parameters into irspiocb parameters */
James Smartacd68592012-01-18 16:25:09 -050010901 status = bf_get(lpfc_wcqe_c_status, wcqe);
10902 pIocbIn->iocb.ulpStatus = (status & LPFC_IOCB_STATUS_MASK);
James Smart4f774512009-05-22 14:52:35 -040010903 if (pIocbOut->iocb_flag & LPFC_IO_FCP)
10904 if (pIocbIn->iocb.ulpStatus == IOSTAT_FCP_RSP_ERROR)
10905 pIocbIn->iocb.un.fcpi.fcpi_parm =
10906 pIocbOut->iocb.un.fcpi.fcpi_parm -
10907 wcqe->total_data_placed;
10908 else
10909 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
James Smart695a8142010-01-26 23:08:03 -050010910 else {
James Smart4f774512009-05-22 14:52:35 -040010911 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
James Smart695a8142010-01-26 23:08:03 -050010912 pIocbIn->iocb.un.genreq64.bdl.bdeSize = wcqe->total_data_placed;
10913 }
James Smart341af102010-01-26 23:07:37 -050010914
James Smartacd68592012-01-18 16:25:09 -050010915 /* Convert BG errors for completion status */
10916 if (status == CQE_STATUS_DI_ERROR) {
10917 pIocbIn->iocb.ulpStatus = IOSTAT_LOCAL_REJECT;
10918
10919 if (bf_get(lpfc_wcqe_c_bg_edir, wcqe))
10920 pIocbIn->iocb.un.ulpWord[4] = IOERR_RX_DMA_FAILED;
10921 else
10922 pIocbIn->iocb.un.ulpWord[4] = IOERR_TX_DMA_FAILED;
10923
10924 pIocbIn->iocb.unsli3.sli3_bg.bgstat = 0;
10925 if (bf_get(lpfc_wcqe_c_bg_ge, wcqe)) /* Guard Check failed */
10926 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
10927 BGS_GUARD_ERR_MASK;
10928 if (bf_get(lpfc_wcqe_c_bg_ae, wcqe)) /* App Tag Check failed */
10929 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
10930 BGS_APPTAG_ERR_MASK;
10931 if (bf_get(lpfc_wcqe_c_bg_re, wcqe)) /* Ref Tag Check failed */
10932 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
10933 BGS_REFTAG_ERR_MASK;
10934
10935 /* Check to see if there was any good data before the error */
10936 if (bf_get(lpfc_wcqe_c_bg_tdpv, wcqe)) {
10937 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
10938 BGS_HI_WATER_MARK_PRESENT_MASK;
10939 pIocbIn->iocb.unsli3.sli3_bg.bghm =
10940 wcqe->total_data_placed;
10941 }
10942
10943 /*
10944 * Set ALL the error bits to indicate we don't know what
10945 * type of error it is.
10946 */
10947 if (!pIocbIn->iocb.unsli3.sli3_bg.bgstat)
10948 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
10949 (BGS_REFTAG_ERR_MASK | BGS_APPTAG_ERR_MASK |
10950 BGS_GUARD_ERR_MASK);
10951 }
10952
James Smart341af102010-01-26 23:07:37 -050010953 /* Pick up HBA exchange busy condition */
10954 if (bf_get(lpfc_wcqe_c_xb, wcqe)) {
10955 spin_lock_irqsave(&phba->hbalock, iflags);
10956 pIocbIn->iocb_flag |= LPFC_EXCHANGE_BUSY;
10957 spin_unlock_irqrestore(&phba->hbalock, iflags);
10958 }
James Smart4f774512009-05-22 14:52:35 -040010959}
10960
10961/**
James Smart45ed1192009-10-02 15:17:02 -040010962 * lpfc_sli4_els_wcqe_to_rspiocbq - Get response iocbq from els wcqe
10963 * @phba: Pointer to HBA context object.
10964 * @wcqe: Pointer to work-queue completion queue entry.
10965 *
10966 * This routine handles an ELS work-queue completion event and construct
10967 * a pseudo response ELS IODBQ from the SLI4 ELS WCQE for the common
10968 * discovery engine to handle.
10969 *
10970 * Return: Pointer to the receive IOCBQ, NULL otherwise.
10971 **/
10972static struct lpfc_iocbq *
10973lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *phba,
10974 struct lpfc_iocbq *irspiocbq)
10975{
10976 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
10977 struct lpfc_iocbq *cmdiocbq;
10978 struct lpfc_wcqe_complete *wcqe;
10979 unsigned long iflags;
10980
10981 wcqe = &irspiocbq->cq_event.cqe.wcqe_cmpl;
James Smart7e56aa22012-08-03 12:35:34 -040010982 spin_lock_irqsave(&pring->ring_lock, iflags);
James Smart45ed1192009-10-02 15:17:02 -040010983 pring->stats.iocb_event++;
10984 /* Look up the ELS command IOCB and create pseudo response IOCB */
10985 cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
10986 bf_get(lpfc_wcqe_c_request_tag, wcqe));
James Smart7e56aa22012-08-03 12:35:34 -040010987 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart45ed1192009-10-02 15:17:02 -040010988
10989 if (unlikely(!cmdiocbq)) {
10990 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
10991 "0386 ELS complete with no corresponding "
10992 "cmdiocb: iotag (%d)\n",
10993 bf_get(lpfc_wcqe_c_request_tag, wcqe));
10994 lpfc_sli_release_iocbq(phba, irspiocbq);
10995 return NULL;
10996 }
10997
10998 /* Fake the irspiocbq and copy necessary response information */
James Smart341af102010-01-26 23:07:37 -050010999 lpfc_sli4_iocb_param_transfer(phba, irspiocbq, cmdiocbq, wcqe);
James Smart45ed1192009-10-02 15:17:02 -040011000
11001 return irspiocbq;
11002}
11003
11004/**
James Smart04c68492009-05-22 14:52:52 -040011005 * lpfc_sli4_sp_handle_async_event - Handle an asynchroous event
11006 * @phba: Pointer to HBA context object.
11007 * @cqe: Pointer to mailbox completion queue entry.
11008 *
11009 * This routine process a mailbox completion queue entry with asynchrous
11010 * event.
11011 *
11012 * Return: true if work posted to worker thread, otherwise false.
11013 **/
11014static bool
11015lpfc_sli4_sp_handle_async_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
11016{
11017 struct lpfc_cq_event *cq_event;
11018 unsigned long iflags;
11019
11020 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
11021 "0392 Async Event: word0:x%x, word1:x%x, "
11022 "word2:x%x, word3:x%x\n", mcqe->word0,
11023 mcqe->mcqe_tag0, mcqe->mcqe_tag1, mcqe->trailer);
11024
11025 /* Allocate a new internal CQ_EVENT entry */
11026 cq_event = lpfc_sli4_cq_event_alloc(phba);
11027 if (!cq_event) {
11028 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11029 "0394 Failed to allocate CQ_EVENT entry\n");
11030 return false;
11031 }
11032
11033 /* Move the CQE into an asynchronous event entry */
11034 memcpy(&cq_event->cqe, mcqe, sizeof(struct lpfc_mcqe));
11035 spin_lock_irqsave(&phba->hbalock, iflags);
11036 list_add_tail(&cq_event->list, &phba->sli4_hba.sp_asynce_work_queue);
11037 /* Set the async event flag */
11038 phba->hba_flag |= ASYNC_EVENT;
11039 spin_unlock_irqrestore(&phba->hbalock, iflags);
11040
11041 return true;
11042}
11043
11044/**
11045 * lpfc_sli4_sp_handle_mbox_event - Handle a mailbox completion event
11046 * @phba: Pointer to HBA context object.
11047 * @cqe: Pointer to mailbox completion queue entry.
11048 *
11049 * This routine process a mailbox completion queue entry with mailbox
11050 * completion event.
11051 *
11052 * Return: true if work posted to worker thread, otherwise false.
11053 **/
11054static bool
11055lpfc_sli4_sp_handle_mbox_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
11056{
11057 uint32_t mcqe_status;
11058 MAILBOX_t *mbox, *pmbox;
11059 struct lpfc_mqe *mqe;
11060 struct lpfc_vport *vport;
11061 struct lpfc_nodelist *ndlp;
11062 struct lpfc_dmabuf *mp;
11063 unsigned long iflags;
11064 LPFC_MBOXQ_t *pmb;
11065 bool workposted = false;
11066 int rc;
11067
11068 /* If not a mailbox complete MCQE, out by checking mailbox consume */
11069 if (!bf_get(lpfc_trailer_completed, mcqe))
11070 goto out_no_mqe_complete;
11071
11072 /* Get the reference to the active mbox command */
11073 spin_lock_irqsave(&phba->hbalock, iflags);
11074 pmb = phba->sli.mbox_active;
11075 if (unlikely(!pmb)) {
11076 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
11077 "1832 No pending MBOX command to handle\n");
11078 spin_unlock_irqrestore(&phba->hbalock, iflags);
11079 goto out_no_mqe_complete;
11080 }
11081 spin_unlock_irqrestore(&phba->hbalock, iflags);
11082 mqe = &pmb->u.mqe;
11083 pmbox = (MAILBOX_t *)&pmb->u.mqe;
11084 mbox = phba->mbox;
11085 vport = pmb->vport;
11086
11087 /* Reset heartbeat timer */
11088 phba->last_completion_time = jiffies;
11089 del_timer(&phba->sli.mbox_tmo);
11090
11091 /* Move mbox data to caller's mailbox region, do endian swapping */
11092 if (pmb->mbox_cmpl && mbox)
11093 lpfc_sli_pcimem_bcopy(mbox, mqe, sizeof(struct lpfc_mqe));
James Smart04c68492009-05-22 14:52:52 -040011094
James Smart73d91e52011-10-10 21:32:10 -040011095 /*
11096 * For mcqe errors, conditionally move a modified error code to
11097 * the mbox so that the error will not be missed.
11098 */
11099 mcqe_status = bf_get(lpfc_mcqe_status, mcqe);
11100 if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
11101 if (bf_get(lpfc_mqe_status, mqe) == MBX_SUCCESS)
11102 bf_set(lpfc_mqe_status, mqe,
11103 (LPFC_MBX_ERROR_RANGE | mcqe_status));
11104 }
James Smart04c68492009-05-22 14:52:52 -040011105 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
11106 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
11107 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_MBOX_VPORT,
11108 "MBOX dflt rpi: status:x%x rpi:x%x",
11109 mcqe_status,
11110 pmbox->un.varWords[0], 0);
11111 if (mcqe_status == MB_CQE_STATUS_SUCCESS) {
11112 mp = (struct lpfc_dmabuf *)(pmb->context1);
11113 ndlp = (struct lpfc_nodelist *)pmb->context2;
11114 /* Reg_LOGIN of dflt RPI was successful. Now lets get
11115 * RID of the PPI using the same mbox buffer.
11116 */
11117 lpfc_unreg_login(phba, vport->vpi,
11118 pmbox->un.varWords[0], pmb);
11119 pmb->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
11120 pmb->context1 = mp;
11121 pmb->context2 = ndlp;
11122 pmb->vport = vport;
11123 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
11124 if (rc != MBX_BUSY)
11125 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
11126 LOG_SLI, "0385 rc should "
11127 "have been MBX_BUSY\n");
11128 if (rc != MBX_NOT_FINISHED)
11129 goto send_current_mbox;
11130 }
11131 }
11132 spin_lock_irqsave(&phba->pport->work_port_lock, iflags);
11133 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
11134 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflags);
11135
11136 /* There is mailbox completion work to do */
11137 spin_lock_irqsave(&phba->hbalock, iflags);
11138 __lpfc_mbox_cmpl_put(phba, pmb);
11139 phba->work_ha |= HA_MBATT;
11140 spin_unlock_irqrestore(&phba->hbalock, iflags);
11141 workposted = true;
11142
11143send_current_mbox:
11144 spin_lock_irqsave(&phba->hbalock, iflags);
11145 /* Release the mailbox command posting token */
11146 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
11147 /* Setting active mailbox pointer need to be in sync to flag clear */
11148 phba->sli.mbox_active = NULL;
11149 spin_unlock_irqrestore(&phba->hbalock, iflags);
11150 /* Wake up worker thread to post the next pending mailbox command */
11151 lpfc_worker_wake_up(phba);
11152out_no_mqe_complete:
11153 if (bf_get(lpfc_trailer_consumed, mcqe))
11154 lpfc_sli4_mq_release(phba->sli4_hba.mbx_wq);
11155 return workposted;
11156}
11157
11158/**
11159 * lpfc_sli4_sp_handle_mcqe - Process a mailbox completion queue entry
11160 * @phba: Pointer to HBA context object.
11161 * @cqe: Pointer to mailbox completion queue entry.
11162 *
11163 * This routine process a mailbox completion queue entry, it invokes the
11164 * proper mailbox complete handling or asynchrous event handling routine
11165 * according to the MCQE's async bit.
11166 *
11167 * Return: true if work posted to worker thread, otherwise false.
11168 **/
11169static bool
11170lpfc_sli4_sp_handle_mcqe(struct lpfc_hba *phba, struct lpfc_cqe *cqe)
11171{
11172 struct lpfc_mcqe mcqe;
11173 bool workposted;
11174
11175 /* Copy the mailbox MCQE and convert endian order as needed */
11176 lpfc_sli_pcimem_bcopy(cqe, &mcqe, sizeof(struct lpfc_mcqe));
11177
11178 /* Invoke the proper event handling routine */
11179 if (!bf_get(lpfc_trailer_async, &mcqe))
11180 workposted = lpfc_sli4_sp_handle_mbox_event(phba, &mcqe);
11181 else
11182 workposted = lpfc_sli4_sp_handle_async_event(phba, &mcqe);
11183 return workposted;
11184}
11185
11186/**
James Smart4f774512009-05-22 14:52:35 -040011187 * lpfc_sli4_sp_handle_els_wcqe - Handle els work-queue completion event
11188 * @phba: Pointer to HBA context object.
11189 * @wcqe: Pointer to work-queue completion queue entry.
11190 *
11191 * This routine handles an ELS work-queue completion event.
11192 *
11193 * Return: true if work posted to worker thread, otherwise false.
11194 **/
11195static bool
11196lpfc_sli4_sp_handle_els_wcqe(struct lpfc_hba *phba,
11197 struct lpfc_wcqe_complete *wcqe)
11198{
James Smart4f774512009-05-22 14:52:35 -040011199 struct lpfc_iocbq *irspiocbq;
11200 unsigned long iflags;
James Smart7e56aa22012-08-03 12:35:34 -040011201 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
James Smart4f774512009-05-22 14:52:35 -040011202
James Smart45ed1192009-10-02 15:17:02 -040011203 /* Get an irspiocbq for later ELS response processing use */
James Smart4f774512009-05-22 14:52:35 -040011204 irspiocbq = lpfc_sli_get_iocbq(phba);
11205 if (!irspiocbq) {
11206 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart2a9bf3d2010-06-07 15:24:45 -040011207 "0387 NO IOCBQ data: txq_cnt=%d iocb_cnt=%d "
11208 "fcp_txcmplq_cnt=%d, els_txcmplq_cnt=%d\n",
11209 pring->txq_cnt, phba->iocb_cnt,
11210 phba->sli.ring[LPFC_FCP_RING].txcmplq_cnt,
11211 phba->sli.ring[LPFC_ELS_RING].txcmplq_cnt);
James Smart45ed1192009-10-02 15:17:02 -040011212 return false;
James Smart4f774512009-05-22 14:52:35 -040011213 }
James Smart4f774512009-05-22 14:52:35 -040011214
James Smart45ed1192009-10-02 15:17:02 -040011215 /* Save off the slow-path queue event for work thread to process */
11216 memcpy(&irspiocbq->cq_event.cqe.wcqe_cmpl, wcqe, sizeof(*wcqe));
James Smart4f774512009-05-22 14:52:35 -040011217 spin_lock_irqsave(&phba->hbalock, iflags);
James Smart4d9ab992009-10-02 15:16:39 -040011218 list_add_tail(&irspiocbq->cq_event.list,
James Smart45ed1192009-10-02 15:17:02 -040011219 &phba->sli4_hba.sp_queue_event);
11220 phba->hba_flag |= HBA_SP_QUEUE_EVT;
James Smart4f774512009-05-22 14:52:35 -040011221 spin_unlock_irqrestore(&phba->hbalock, iflags);
James Smart4f774512009-05-22 14:52:35 -040011222
James Smart45ed1192009-10-02 15:17:02 -040011223 return true;
James Smart4f774512009-05-22 14:52:35 -040011224}
11225
11226/**
11227 * lpfc_sli4_sp_handle_rel_wcqe - Handle slow-path WQ entry consumed event
11228 * @phba: Pointer to HBA context object.
11229 * @wcqe: Pointer to work-queue completion queue entry.
11230 *
11231 * This routine handles slow-path WQ entry comsumed event by invoking the
11232 * proper WQ release routine to the slow-path WQ.
11233 **/
11234static void
11235lpfc_sli4_sp_handle_rel_wcqe(struct lpfc_hba *phba,
11236 struct lpfc_wcqe_release *wcqe)
11237{
James Smart2e90f4b2011-12-13 13:22:37 -050011238 /* sanity check on queue memory */
11239 if (unlikely(!phba->sli4_hba.els_wq))
11240 return;
James Smart4f774512009-05-22 14:52:35 -040011241 /* Check for the slow-path ELS work queue */
11242 if (bf_get(lpfc_wcqe_r_wq_id, wcqe) == phba->sli4_hba.els_wq->queue_id)
11243 lpfc_sli4_wq_release(phba->sli4_hba.els_wq,
11244 bf_get(lpfc_wcqe_r_wqe_index, wcqe));
11245 else
11246 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11247 "2579 Slow-path wqe consume event carries "
11248 "miss-matched qid: wcqe-qid=x%x, sp-qid=x%x\n",
11249 bf_get(lpfc_wcqe_r_wqe_index, wcqe),
11250 phba->sli4_hba.els_wq->queue_id);
11251}
11252
11253/**
11254 * lpfc_sli4_sp_handle_abort_xri_wcqe - Handle a xri abort event
11255 * @phba: Pointer to HBA context object.
11256 * @cq: Pointer to a WQ completion queue.
11257 * @wcqe: Pointer to work-queue completion queue entry.
11258 *
11259 * This routine handles an XRI abort event.
11260 *
11261 * Return: true if work posted to worker thread, otherwise false.
11262 **/
11263static bool
11264lpfc_sli4_sp_handle_abort_xri_wcqe(struct lpfc_hba *phba,
11265 struct lpfc_queue *cq,
11266 struct sli4_wcqe_xri_aborted *wcqe)
11267{
11268 bool workposted = false;
11269 struct lpfc_cq_event *cq_event;
11270 unsigned long iflags;
11271
11272 /* Allocate a new internal CQ_EVENT entry */
11273 cq_event = lpfc_sli4_cq_event_alloc(phba);
11274 if (!cq_event) {
11275 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11276 "0602 Failed to allocate CQ_EVENT entry\n");
11277 return false;
11278 }
11279
11280 /* Move the CQE into the proper xri abort event list */
11281 memcpy(&cq_event->cqe, wcqe, sizeof(struct sli4_wcqe_xri_aborted));
11282 switch (cq->subtype) {
11283 case LPFC_FCP:
11284 spin_lock_irqsave(&phba->hbalock, iflags);
11285 list_add_tail(&cq_event->list,
11286 &phba->sli4_hba.sp_fcp_xri_aborted_work_queue);
11287 /* Set the fcp xri abort event flag */
11288 phba->hba_flag |= FCP_XRI_ABORT_EVENT;
11289 spin_unlock_irqrestore(&phba->hbalock, iflags);
11290 workposted = true;
11291 break;
11292 case LPFC_ELS:
11293 spin_lock_irqsave(&phba->hbalock, iflags);
11294 list_add_tail(&cq_event->list,
11295 &phba->sli4_hba.sp_els_xri_aborted_work_queue);
11296 /* Set the els xri abort event flag */
11297 phba->hba_flag |= ELS_XRI_ABORT_EVENT;
11298 spin_unlock_irqrestore(&phba->hbalock, iflags);
11299 workposted = true;
11300 break;
11301 default:
11302 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11303 "0603 Invalid work queue CQE subtype (x%x)\n",
11304 cq->subtype);
11305 workposted = false;
11306 break;
11307 }
11308 return workposted;
11309}
11310
11311/**
James Smart4d9ab992009-10-02 15:16:39 -040011312 * lpfc_sli4_sp_handle_rcqe - Process a receive-queue completion queue entry
James Smart4f774512009-05-22 14:52:35 -040011313 * @phba: Pointer to HBA context object.
James Smart4d9ab992009-10-02 15:16:39 -040011314 * @rcqe: Pointer to receive-queue completion queue entry.
James Smart4f774512009-05-22 14:52:35 -040011315 *
James Smart4d9ab992009-10-02 15:16:39 -040011316 * This routine process a receive-queue completion queue entry.
James Smart4f774512009-05-22 14:52:35 -040011317 *
11318 * Return: true if work posted to worker thread, otherwise false.
11319 **/
11320static bool
James Smart4d9ab992009-10-02 15:16:39 -040011321lpfc_sli4_sp_handle_rcqe(struct lpfc_hba *phba, struct lpfc_rcqe *rcqe)
11322{
11323 bool workposted = false;
11324 struct lpfc_queue *hrq = phba->sli4_hba.hdr_rq;
11325 struct lpfc_queue *drq = phba->sli4_hba.dat_rq;
11326 struct hbq_dmabuf *dma_buf;
James Smart7851fe22011-07-22 18:36:52 -040011327 uint32_t status, rq_id;
James Smart4d9ab992009-10-02 15:16:39 -040011328 unsigned long iflags;
11329
James Smart2e90f4b2011-12-13 13:22:37 -050011330 /* sanity check on queue memory */
11331 if (unlikely(!hrq) || unlikely(!drq))
11332 return workposted;
11333
James Smart7851fe22011-07-22 18:36:52 -040011334 if (bf_get(lpfc_cqe_code, rcqe) == CQE_CODE_RECEIVE_V1)
11335 rq_id = bf_get(lpfc_rcqe_rq_id_v1, rcqe);
11336 else
11337 rq_id = bf_get(lpfc_rcqe_rq_id, rcqe);
11338 if (rq_id != hrq->queue_id)
James Smart4d9ab992009-10-02 15:16:39 -040011339 goto out;
11340
11341 status = bf_get(lpfc_rcqe_status, rcqe);
11342 switch (status) {
11343 case FC_STATUS_RQ_BUF_LEN_EXCEEDED:
11344 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11345 "2537 Receive Frame Truncated!!\n");
James Smartb84daac2012-08-03 12:35:13 -040011346 hrq->RQ_buf_trunc++;
James Smart4d9ab992009-10-02 15:16:39 -040011347 case FC_STATUS_RQ_SUCCESS:
James Smart5ffc2662009-11-18 15:39:44 -050011348 lpfc_sli4_rq_release(hrq, drq);
James Smart4d9ab992009-10-02 15:16:39 -040011349 spin_lock_irqsave(&phba->hbalock, iflags);
11350 dma_buf = lpfc_sli_hbqbuf_get(&phba->hbqs[0].hbq_buffer_list);
11351 if (!dma_buf) {
James Smartb84daac2012-08-03 12:35:13 -040011352 hrq->RQ_no_buf_found++;
James Smart4d9ab992009-10-02 15:16:39 -040011353 spin_unlock_irqrestore(&phba->hbalock, iflags);
11354 goto out;
11355 }
James Smartb84daac2012-08-03 12:35:13 -040011356 hrq->RQ_rcv_buf++;
James Smart4d9ab992009-10-02 15:16:39 -040011357 memcpy(&dma_buf->cq_event.cqe.rcqe_cmpl, rcqe, sizeof(*rcqe));
11358 /* save off the frame for the word thread to process */
11359 list_add_tail(&dma_buf->cq_event.list,
James Smart45ed1192009-10-02 15:17:02 -040011360 &phba->sli4_hba.sp_queue_event);
James Smart4d9ab992009-10-02 15:16:39 -040011361 /* Frame received */
James Smart45ed1192009-10-02 15:17:02 -040011362 phba->hba_flag |= HBA_SP_QUEUE_EVT;
James Smart4d9ab992009-10-02 15:16:39 -040011363 spin_unlock_irqrestore(&phba->hbalock, iflags);
11364 workposted = true;
11365 break;
11366 case FC_STATUS_INSUFF_BUF_NEED_BUF:
11367 case FC_STATUS_INSUFF_BUF_FRM_DISC:
James Smartb84daac2012-08-03 12:35:13 -040011368 hrq->RQ_no_posted_buf++;
James Smart4d9ab992009-10-02 15:16:39 -040011369 /* Post more buffers if possible */
11370 spin_lock_irqsave(&phba->hbalock, iflags);
11371 phba->hba_flag |= HBA_POST_RECEIVE_BUFFER;
11372 spin_unlock_irqrestore(&phba->hbalock, iflags);
11373 workposted = true;
11374 break;
11375 }
11376out:
11377 return workposted;
James Smart4d9ab992009-10-02 15:16:39 -040011378}
11379
11380/**
11381 * lpfc_sli4_sp_handle_cqe - Process a slow path completion queue entry
11382 * @phba: Pointer to HBA context object.
11383 * @cq: Pointer to the completion queue.
11384 * @wcqe: Pointer to a completion queue entry.
11385 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030011386 * This routine process a slow-path work-queue or receive queue completion queue
James Smart4d9ab992009-10-02 15:16:39 -040011387 * entry.
11388 *
11389 * Return: true if work posted to worker thread, otherwise false.
11390 **/
11391static bool
11392lpfc_sli4_sp_handle_cqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
James Smart4f774512009-05-22 14:52:35 -040011393 struct lpfc_cqe *cqe)
11394{
James Smart45ed1192009-10-02 15:17:02 -040011395 struct lpfc_cqe cqevt;
James Smart4f774512009-05-22 14:52:35 -040011396 bool workposted = false;
11397
11398 /* Copy the work queue CQE and convert endian order if needed */
James Smart45ed1192009-10-02 15:17:02 -040011399 lpfc_sli_pcimem_bcopy(cqe, &cqevt, sizeof(struct lpfc_cqe));
James Smart4f774512009-05-22 14:52:35 -040011400
11401 /* Check and process for different type of WCQE and dispatch */
James Smart45ed1192009-10-02 15:17:02 -040011402 switch (bf_get(lpfc_cqe_code, &cqevt)) {
James Smart4f774512009-05-22 14:52:35 -040011403 case CQE_CODE_COMPL_WQE:
James Smart45ed1192009-10-02 15:17:02 -040011404 /* Process the WQ/RQ complete event */
James Smartbc739052010-08-04 16:11:18 -040011405 phba->last_completion_time = jiffies;
James Smart4f774512009-05-22 14:52:35 -040011406 workposted = lpfc_sli4_sp_handle_els_wcqe(phba,
James Smart45ed1192009-10-02 15:17:02 -040011407 (struct lpfc_wcqe_complete *)&cqevt);
James Smart4f774512009-05-22 14:52:35 -040011408 break;
11409 case CQE_CODE_RELEASE_WQE:
11410 /* Process the WQ release event */
11411 lpfc_sli4_sp_handle_rel_wcqe(phba,
James Smart45ed1192009-10-02 15:17:02 -040011412 (struct lpfc_wcqe_release *)&cqevt);
James Smart4f774512009-05-22 14:52:35 -040011413 break;
11414 case CQE_CODE_XRI_ABORTED:
11415 /* Process the WQ XRI abort event */
James Smartbc739052010-08-04 16:11:18 -040011416 phba->last_completion_time = jiffies;
James Smart4f774512009-05-22 14:52:35 -040011417 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
James Smart45ed1192009-10-02 15:17:02 -040011418 (struct sli4_wcqe_xri_aborted *)&cqevt);
James Smart4f774512009-05-22 14:52:35 -040011419 break;
James Smart4d9ab992009-10-02 15:16:39 -040011420 case CQE_CODE_RECEIVE:
James Smart7851fe22011-07-22 18:36:52 -040011421 case CQE_CODE_RECEIVE_V1:
James Smart4d9ab992009-10-02 15:16:39 -040011422 /* Process the RQ event */
James Smartbc739052010-08-04 16:11:18 -040011423 phba->last_completion_time = jiffies;
James Smart4d9ab992009-10-02 15:16:39 -040011424 workposted = lpfc_sli4_sp_handle_rcqe(phba,
James Smart45ed1192009-10-02 15:17:02 -040011425 (struct lpfc_rcqe *)&cqevt);
James Smart4d9ab992009-10-02 15:16:39 -040011426 break;
James Smart4f774512009-05-22 14:52:35 -040011427 default:
11428 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11429 "0388 Not a valid WCQE code: x%x\n",
James Smart45ed1192009-10-02 15:17:02 -040011430 bf_get(lpfc_cqe_code, &cqevt));
James Smart4f774512009-05-22 14:52:35 -040011431 break;
11432 }
11433 return workposted;
11434}
11435
11436/**
James Smart4f774512009-05-22 14:52:35 -040011437 * lpfc_sli4_sp_handle_eqe - Process a slow-path event queue entry
11438 * @phba: Pointer to HBA context object.
11439 * @eqe: Pointer to fast-path event queue entry.
11440 *
11441 * This routine process a event queue entry from the slow-path event queue.
11442 * It will check the MajorCode and MinorCode to determine this is for a
11443 * completion event on a completion queue, if not, an error shall be logged
11444 * and just return. Otherwise, it will get to the corresponding completion
11445 * queue and process all the entries on that completion queue, rearm the
11446 * completion queue, and then return.
11447 *
11448 **/
11449static void
11450lpfc_sli4_sp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe)
11451{
11452 struct lpfc_queue *cq = NULL, *childq, *speq;
11453 struct lpfc_cqe *cqe;
11454 bool workposted = false;
11455 int ecount = 0;
11456 uint16_t cqid;
11457
James Smartcb5172e2010-03-15 11:25:07 -040011458 if (bf_get_le32(lpfc_eqe_major_code, eqe) != 0) {
James Smart4f774512009-05-22 14:52:35 -040011459 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11460 "0359 Not a valid slow-path completion "
11461 "event: majorcode=x%x, minorcode=x%x\n",
James Smartcb5172e2010-03-15 11:25:07 -040011462 bf_get_le32(lpfc_eqe_major_code, eqe),
11463 bf_get_le32(lpfc_eqe_minor_code, eqe));
James Smart4f774512009-05-22 14:52:35 -040011464 return;
11465 }
11466
11467 /* Get the reference to the corresponding CQ */
James Smartcb5172e2010-03-15 11:25:07 -040011468 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
James Smart4f774512009-05-22 14:52:35 -040011469
11470 /* Search for completion queue pointer matching this cqid */
11471 speq = phba->sli4_hba.sp_eq;
James Smart2e90f4b2011-12-13 13:22:37 -050011472 /* sanity check on queue memory */
11473 if (unlikely(!speq))
11474 return;
James Smart4f774512009-05-22 14:52:35 -040011475 list_for_each_entry(childq, &speq->child_list, list) {
11476 if (childq->queue_id == cqid) {
11477 cq = childq;
11478 break;
11479 }
11480 }
11481 if (unlikely(!cq)) {
James Smart75baf692010-06-08 18:31:21 -040011482 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
11483 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11484 "0365 Slow-path CQ identifier "
11485 "(%d) does not exist\n", cqid);
James Smart4f774512009-05-22 14:52:35 -040011486 return;
11487 }
11488
11489 /* Process all the entries to the CQ */
11490 switch (cq->type) {
11491 case LPFC_MCQ:
11492 while ((cqe = lpfc_sli4_cq_get(cq))) {
11493 workposted |= lpfc_sli4_sp_handle_mcqe(phba, cqe);
James Smart73d91e52011-10-10 21:32:10 -040011494 if (!(++ecount % cq->entry_repost))
James Smart4f774512009-05-22 14:52:35 -040011495 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
James Smartb84daac2012-08-03 12:35:13 -040011496 cq->CQ_mbox++;
James Smart4f774512009-05-22 14:52:35 -040011497 }
11498 break;
11499 case LPFC_WCQ:
11500 while ((cqe = lpfc_sli4_cq_get(cq))) {
James Smart05580562011-05-24 11:40:48 -040011501 if (cq->subtype == LPFC_FCP)
11502 workposted |= lpfc_sli4_fp_handle_wcqe(phba, cq,
11503 cqe);
11504 else
11505 workposted |= lpfc_sli4_sp_handle_cqe(phba, cq,
11506 cqe);
James Smart73d91e52011-10-10 21:32:10 -040011507 if (!(++ecount % cq->entry_repost))
James Smart4f774512009-05-22 14:52:35 -040011508 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
11509 }
James Smartb84daac2012-08-03 12:35:13 -040011510
11511 /* Track the max number of CQEs processed in 1 EQ */
11512 if (ecount > cq->CQ_max_cqe)
11513 cq->CQ_max_cqe = ecount;
James Smart4f774512009-05-22 14:52:35 -040011514 break;
11515 default:
11516 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11517 "0370 Invalid completion queue type (%d)\n",
11518 cq->type);
11519 return;
11520 }
11521
11522 /* Catch the no cq entry condition, log an error */
11523 if (unlikely(ecount == 0))
11524 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11525 "0371 No entry from the CQ: identifier "
11526 "(x%x), type (%d)\n", cq->queue_id, cq->type);
11527
11528 /* In any case, flash and re-arm the RCQ */
11529 lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
11530
11531 /* wake up worker thread if there are works to be done */
11532 if (workposted)
11533 lpfc_worker_wake_up(phba);
11534}
11535
11536/**
11537 * lpfc_sli4_fp_handle_fcp_wcqe - Process fast-path work queue completion entry
11538 * @eqe: Pointer to fast-path completion queue entry.
11539 *
11540 * This routine process a fast-path work queue completion entry from fast-path
11541 * event queue for FCP command response completion.
11542 **/
11543static void
11544lpfc_sli4_fp_handle_fcp_wcqe(struct lpfc_hba *phba,
11545 struct lpfc_wcqe_complete *wcqe)
11546{
11547 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_FCP_RING];
11548 struct lpfc_iocbq *cmdiocbq;
11549 struct lpfc_iocbq irspiocbq;
11550 unsigned long iflags;
11551
James Smart4f774512009-05-22 14:52:35 -040011552 /* Check for response status */
11553 if (unlikely(bf_get(lpfc_wcqe_c_status, wcqe))) {
11554 /* If resource errors reported from HBA, reduce queue
11555 * depth of the SCSI device.
11556 */
11557 if ((bf_get(lpfc_wcqe_c_status, wcqe) ==
11558 IOSTAT_LOCAL_REJECT) &&
11559 (wcqe->parameter == IOERR_NO_RESOURCES)) {
11560 phba->lpfc_rampdown_queue_depth(phba);
11561 }
11562 /* Log the error status */
11563 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11564 "0373 FCP complete error: status=x%x, "
11565 "hw_status=x%x, total_data_specified=%d, "
11566 "parameter=x%x, word3=x%x\n",
11567 bf_get(lpfc_wcqe_c_status, wcqe),
11568 bf_get(lpfc_wcqe_c_hw_status, wcqe),
11569 wcqe->total_data_placed, wcqe->parameter,
11570 wcqe->word3);
11571 }
11572
11573 /* Look up the FCP command IOCB and create pseudo response IOCB */
James Smart7e56aa22012-08-03 12:35:34 -040011574 spin_lock_irqsave(&pring->ring_lock, iflags);
11575 pring->stats.iocb_event++;
James Smart4f774512009-05-22 14:52:35 -040011576 cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
11577 bf_get(lpfc_wcqe_c_request_tag, wcqe));
James Smart7e56aa22012-08-03 12:35:34 -040011578 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart4f774512009-05-22 14:52:35 -040011579 if (unlikely(!cmdiocbq)) {
11580 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11581 "0374 FCP complete with no corresponding "
11582 "cmdiocb: iotag (%d)\n",
11583 bf_get(lpfc_wcqe_c_request_tag, wcqe));
11584 return;
11585 }
11586 if (unlikely(!cmdiocbq->iocb_cmpl)) {
11587 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11588 "0375 FCP cmdiocb not callback function "
11589 "iotag: (%d)\n",
11590 bf_get(lpfc_wcqe_c_request_tag, wcqe));
11591 return;
11592 }
11593
11594 /* Fake the irspiocb and copy necessary response information */
James Smart341af102010-01-26 23:07:37 -050011595 lpfc_sli4_iocb_param_transfer(phba, &irspiocbq, cmdiocbq, wcqe);
James Smart4f774512009-05-22 14:52:35 -040011596
James Smart0f65ff62010-02-26 14:14:23 -050011597 if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED) {
11598 spin_lock_irqsave(&phba->hbalock, iflags);
11599 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
11600 spin_unlock_irqrestore(&phba->hbalock, iflags);
11601 }
11602
James Smart4f774512009-05-22 14:52:35 -040011603 /* Pass the cmd_iocb and the rsp state to the upper layer */
11604 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq, &irspiocbq);
11605}
11606
11607/**
11608 * lpfc_sli4_fp_handle_rel_wcqe - Handle fast-path WQ entry consumed event
11609 * @phba: Pointer to HBA context object.
11610 * @cq: Pointer to completion queue.
11611 * @wcqe: Pointer to work-queue completion queue entry.
11612 *
11613 * This routine handles an fast-path WQ entry comsumed event by invoking the
11614 * proper WQ release routine to the slow-path WQ.
11615 **/
11616static void
11617lpfc_sli4_fp_handle_rel_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
11618 struct lpfc_wcqe_release *wcqe)
11619{
11620 struct lpfc_queue *childwq;
11621 bool wqid_matched = false;
11622 uint16_t fcp_wqid;
11623
11624 /* Check for fast-path FCP work queue release */
11625 fcp_wqid = bf_get(lpfc_wcqe_r_wq_id, wcqe);
11626 list_for_each_entry(childwq, &cq->child_list, list) {
11627 if (childwq->queue_id == fcp_wqid) {
11628 lpfc_sli4_wq_release(childwq,
11629 bf_get(lpfc_wcqe_r_wqe_index, wcqe));
11630 wqid_matched = true;
11631 break;
11632 }
11633 }
11634 /* Report warning log message if no match found */
11635 if (wqid_matched != true)
11636 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11637 "2580 Fast-path wqe consume event carries "
11638 "miss-matched qid: wcqe-qid=x%x\n", fcp_wqid);
11639}
11640
11641/**
11642 * lpfc_sli4_fp_handle_wcqe - Process fast-path work queue completion entry
11643 * @cq: Pointer to the completion queue.
11644 * @eqe: Pointer to fast-path completion queue entry.
11645 *
11646 * This routine process a fast-path work queue completion entry from fast-path
11647 * event queue for FCP command response completion.
11648 **/
11649static int
11650lpfc_sli4_fp_handle_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
11651 struct lpfc_cqe *cqe)
11652{
11653 struct lpfc_wcqe_release wcqe;
11654 bool workposted = false;
11655
11656 /* Copy the work queue CQE and convert endian order if needed */
11657 lpfc_sli_pcimem_bcopy(cqe, &wcqe, sizeof(struct lpfc_cqe));
11658
11659 /* Check and process for different type of WCQE and dispatch */
11660 switch (bf_get(lpfc_wcqe_c_code, &wcqe)) {
11661 case CQE_CODE_COMPL_WQE:
James Smartb84daac2012-08-03 12:35:13 -040011662 cq->CQ_wq++;
James Smart4f774512009-05-22 14:52:35 -040011663 /* Process the WQ complete event */
James Smart98fc5dd2010-06-07 15:24:29 -040011664 phba->last_completion_time = jiffies;
James Smart4f774512009-05-22 14:52:35 -040011665 lpfc_sli4_fp_handle_fcp_wcqe(phba,
11666 (struct lpfc_wcqe_complete *)&wcqe);
11667 break;
11668 case CQE_CODE_RELEASE_WQE:
James Smartb84daac2012-08-03 12:35:13 -040011669 cq->CQ_release_wqe++;
James Smart4f774512009-05-22 14:52:35 -040011670 /* Process the WQ release event */
11671 lpfc_sli4_fp_handle_rel_wcqe(phba, cq,
11672 (struct lpfc_wcqe_release *)&wcqe);
11673 break;
11674 case CQE_CODE_XRI_ABORTED:
James Smartb84daac2012-08-03 12:35:13 -040011675 cq->CQ_xri_aborted++;
James Smart4f774512009-05-22 14:52:35 -040011676 /* Process the WQ XRI abort event */
James Smartbc739052010-08-04 16:11:18 -040011677 phba->last_completion_time = jiffies;
James Smart4f774512009-05-22 14:52:35 -040011678 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
11679 (struct sli4_wcqe_xri_aborted *)&wcqe);
11680 break;
11681 default:
11682 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11683 "0144 Not a valid WCQE code: x%x\n",
11684 bf_get(lpfc_wcqe_c_code, &wcqe));
11685 break;
11686 }
11687 return workposted;
11688}
11689
11690/**
11691 * lpfc_sli4_fp_handle_eqe - Process a fast-path event queue entry
11692 * @phba: Pointer to HBA context object.
11693 * @eqe: Pointer to fast-path event queue entry.
11694 *
11695 * This routine process a event queue entry from the fast-path event queue.
11696 * It will check the MajorCode and MinorCode to determine this is for a
11697 * completion event on a completion queue, if not, an error shall be logged
11698 * and just return. Otherwise, it will get to the corresponding completion
11699 * queue and process all the entries on the completion queue, rearm the
11700 * completion queue, and then return.
11701 **/
11702static void
11703lpfc_sli4_fp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
11704 uint32_t fcp_cqidx)
11705{
11706 struct lpfc_queue *cq;
11707 struct lpfc_cqe *cqe;
11708 bool workposted = false;
11709 uint16_t cqid;
11710 int ecount = 0;
11711
James Smartcb5172e2010-03-15 11:25:07 -040011712 if (unlikely(bf_get_le32(lpfc_eqe_major_code, eqe) != 0)) {
James Smart4f774512009-05-22 14:52:35 -040011713 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11714 "0366 Not a valid fast-path completion "
11715 "event: majorcode=x%x, minorcode=x%x\n",
James Smartcb5172e2010-03-15 11:25:07 -040011716 bf_get_le32(lpfc_eqe_major_code, eqe),
11717 bf_get_le32(lpfc_eqe_minor_code, eqe));
James Smart4f774512009-05-22 14:52:35 -040011718 return;
11719 }
11720
James Smart2e90f4b2011-12-13 13:22:37 -050011721 if (unlikely(!phba->sli4_hba.fcp_cq)) {
11722 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11723 "3146 Fast-path completion queues "
11724 "does not exist\n");
11725 return;
11726 }
James Smart4f774512009-05-22 14:52:35 -040011727 cq = phba->sli4_hba.fcp_cq[fcp_cqidx];
11728 if (unlikely(!cq)) {
James Smart75baf692010-06-08 18:31:21 -040011729 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
11730 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11731 "0367 Fast-path completion queue "
James Smart2e90f4b2011-12-13 13:22:37 -050011732 "(%d) does not exist\n", fcp_cqidx);
James Smart4f774512009-05-22 14:52:35 -040011733 return;
11734 }
11735
11736 /* Get the reference to the corresponding CQ */
James Smartcb5172e2010-03-15 11:25:07 -040011737 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
James Smart4f774512009-05-22 14:52:35 -040011738 if (unlikely(cqid != cq->queue_id)) {
11739 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11740 "0368 Miss-matched fast-path completion "
11741 "queue identifier: eqcqid=%d, fcpcqid=%d\n",
11742 cqid, cq->queue_id);
11743 return;
11744 }
11745
11746 /* Process all the entries to the CQ */
11747 while ((cqe = lpfc_sli4_cq_get(cq))) {
11748 workposted |= lpfc_sli4_fp_handle_wcqe(phba, cq, cqe);
James Smart73d91e52011-10-10 21:32:10 -040011749 if (!(++ecount % cq->entry_repost))
James Smart4f774512009-05-22 14:52:35 -040011750 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
11751 }
11752
James Smartb84daac2012-08-03 12:35:13 -040011753 /* Track the max number of CQEs processed in 1 EQ */
11754 if (ecount > cq->CQ_max_cqe)
11755 cq->CQ_max_cqe = ecount;
11756
James Smart4f774512009-05-22 14:52:35 -040011757 /* Catch the no cq entry condition */
11758 if (unlikely(ecount == 0))
11759 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11760 "0369 No entry from fast-path completion "
11761 "queue fcpcqid=%d\n", cq->queue_id);
11762
11763 /* In any case, flash and re-arm the CQ */
11764 lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
11765
11766 /* wake up worker thread if there are works to be done */
11767 if (workposted)
11768 lpfc_worker_wake_up(phba);
11769}
11770
11771static void
11772lpfc_sli4_eq_flush(struct lpfc_hba *phba, struct lpfc_queue *eq)
11773{
11774 struct lpfc_eqe *eqe;
11775
11776 /* walk all the EQ entries and drop on the floor */
11777 while ((eqe = lpfc_sli4_eq_get(eq)))
11778 ;
11779
11780 /* Clear and re-arm the EQ */
11781 lpfc_sli4_eq_release(eq, LPFC_QUEUE_REARM);
11782}
11783
11784/**
11785 * lpfc_sli4_sp_intr_handler - Slow-path interrupt handler to SLI-4 device
11786 * @irq: Interrupt number.
11787 * @dev_id: The device context pointer.
11788 *
11789 * This function is directly called from the PCI layer as an interrupt
11790 * service routine when device with SLI-4 interface spec is enabled with
11791 * MSI-X multi-message interrupt mode and there are slow-path events in
11792 * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
11793 * interrupt mode, this function is called as part of the device-level
11794 * interrupt handler. When the PCI slot is in error recovery or the HBA is
11795 * undergoing initialization, the interrupt handler will not process the
11796 * interrupt. The link attention and ELS ring attention events are handled
11797 * by the worker thread. The interrupt handler signals the worker thread
11798 * and returns for these events. This function is called without any lock
11799 * held. It gets the hbalock to access and update SLI data structures.
11800 *
11801 * This function returns IRQ_HANDLED when interrupt is handled else it
11802 * returns IRQ_NONE.
11803 **/
11804irqreturn_t
11805lpfc_sli4_sp_intr_handler(int irq, void *dev_id)
11806{
11807 struct lpfc_hba *phba;
11808 struct lpfc_queue *speq;
11809 struct lpfc_eqe *eqe;
11810 unsigned long iflag;
11811 int ecount = 0;
11812
11813 /*
11814 * Get the driver's phba structure from the dev_id
11815 */
11816 phba = (struct lpfc_hba *)dev_id;
11817
11818 if (unlikely(!phba))
11819 return IRQ_NONE;
11820
11821 /* Get to the EQ struct associated with this vector */
11822 speq = phba->sli4_hba.sp_eq;
James Smart5350d872011-10-10 21:33:49 -040011823 if (unlikely(!speq))
11824 return IRQ_NONE;
James Smart4f774512009-05-22 14:52:35 -040011825
11826 /* Check device state for handling interrupt */
11827 if (unlikely(lpfc_intr_state_check(phba))) {
James Smartb84daac2012-08-03 12:35:13 -040011828 speq->EQ_badstate++;
James Smart4f774512009-05-22 14:52:35 -040011829 /* Check again for link_state with lock held */
11830 spin_lock_irqsave(&phba->hbalock, iflag);
11831 if (phba->link_state < LPFC_LINK_DOWN)
11832 /* Flush, clear interrupt, and rearm the EQ */
11833 lpfc_sli4_eq_flush(phba, speq);
11834 spin_unlock_irqrestore(&phba->hbalock, iflag);
11835 return IRQ_NONE;
11836 }
11837
11838 /*
11839 * Process all the event on FCP slow-path EQ
11840 */
11841 while ((eqe = lpfc_sli4_eq_get(speq))) {
11842 lpfc_sli4_sp_handle_eqe(phba, eqe);
James Smart73d91e52011-10-10 21:32:10 -040011843 if (!(++ecount % speq->entry_repost))
James Smart4f774512009-05-22 14:52:35 -040011844 lpfc_sli4_eq_release(speq, LPFC_QUEUE_NOARM);
James Smartb84daac2012-08-03 12:35:13 -040011845 speq->EQ_processed++;
James Smart4f774512009-05-22 14:52:35 -040011846 }
11847
James Smartb84daac2012-08-03 12:35:13 -040011848 /* Track the max number of EQEs processed in 1 intr */
11849 if (ecount > speq->EQ_max_eqe)
11850 speq->EQ_max_eqe = ecount;
11851
James Smart4f774512009-05-22 14:52:35 -040011852 /* Always clear and re-arm the slow-path EQ */
11853 lpfc_sli4_eq_release(speq, LPFC_QUEUE_REARM);
11854
11855 /* Catch the no cq entry condition */
11856 if (unlikely(ecount == 0)) {
James Smartb84daac2012-08-03 12:35:13 -040011857 speq->EQ_no_entry++;
James Smart4f774512009-05-22 14:52:35 -040011858 if (phba->intr_type == MSIX)
11859 /* MSI-X treated interrupt served as no EQ share INT */
11860 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11861 "0357 MSI-X interrupt with no EQE\n");
11862 else
11863 /* Non MSI-X treated on interrupt as EQ share INT */
11864 return IRQ_NONE;
11865 }
11866
11867 return IRQ_HANDLED;
11868} /* lpfc_sli4_sp_intr_handler */
11869
11870/**
11871 * lpfc_sli4_fp_intr_handler - Fast-path interrupt handler to SLI-4 device
11872 * @irq: Interrupt number.
11873 * @dev_id: The device context pointer.
11874 *
11875 * This function is directly called from the PCI layer as an interrupt
11876 * service routine when device with SLI-4 interface spec is enabled with
11877 * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
11878 * ring event in the HBA. However, when the device is enabled with either
11879 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
11880 * device-level interrupt handler. When the PCI slot is in error recovery
11881 * or the HBA is undergoing initialization, the interrupt handler will not
11882 * process the interrupt. The SCSI FCP fast-path ring event are handled in
11883 * the intrrupt context. This function is called without any lock held.
11884 * It gets the hbalock to access and update SLI data structures. Note that,
11885 * the FCP EQ to FCP CQ are one-to-one map such that the FCP EQ index is
11886 * equal to that of FCP CQ index.
11887 *
11888 * This function returns IRQ_HANDLED when interrupt is handled else it
11889 * returns IRQ_NONE.
11890 **/
11891irqreturn_t
11892lpfc_sli4_fp_intr_handler(int irq, void *dev_id)
11893{
11894 struct lpfc_hba *phba;
11895 struct lpfc_fcp_eq_hdl *fcp_eq_hdl;
11896 struct lpfc_queue *fpeq;
11897 struct lpfc_eqe *eqe;
11898 unsigned long iflag;
11899 int ecount = 0;
11900 uint32_t fcp_eqidx;
11901
11902 /* Get the driver's phba structure from the dev_id */
11903 fcp_eq_hdl = (struct lpfc_fcp_eq_hdl *)dev_id;
11904 phba = fcp_eq_hdl->phba;
11905 fcp_eqidx = fcp_eq_hdl->idx;
11906
11907 if (unlikely(!phba))
11908 return IRQ_NONE;
James Smart5350d872011-10-10 21:33:49 -040011909 if (unlikely(!phba->sli4_hba.fp_eq))
11910 return IRQ_NONE;
James Smart4f774512009-05-22 14:52:35 -040011911
11912 /* Get to the EQ struct associated with this vector */
11913 fpeq = phba->sli4_hba.fp_eq[fcp_eqidx];
James Smart2e90f4b2011-12-13 13:22:37 -050011914 if (unlikely(!fpeq))
11915 return IRQ_NONE;
James Smart4f774512009-05-22 14:52:35 -040011916
11917 /* Check device state for handling interrupt */
11918 if (unlikely(lpfc_intr_state_check(phba))) {
James Smartb84daac2012-08-03 12:35:13 -040011919 fpeq->EQ_badstate++;
James Smart4f774512009-05-22 14:52:35 -040011920 /* Check again for link_state with lock held */
11921 spin_lock_irqsave(&phba->hbalock, iflag);
11922 if (phba->link_state < LPFC_LINK_DOWN)
11923 /* Flush, clear interrupt, and rearm the EQ */
11924 lpfc_sli4_eq_flush(phba, fpeq);
11925 spin_unlock_irqrestore(&phba->hbalock, iflag);
11926 return IRQ_NONE;
11927 }
11928
11929 /*
11930 * Process all the event on FCP fast-path EQ
11931 */
11932 while ((eqe = lpfc_sli4_eq_get(fpeq))) {
11933 lpfc_sli4_fp_handle_eqe(phba, eqe, fcp_eqidx);
James Smart73d91e52011-10-10 21:32:10 -040011934 if (!(++ecount % fpeq->entry_repost))
James Smart4f774512009-05-22 14:52:35 -040011935 lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_NOARM);
James Smartb84daac2012-08-03 12:35:13 -040011936 fpeq->EQ_processed++;
James Smart4f774512009-05-22 14:52:35 -040011937 }
11938
James Smartb84daac2012-08-03 12:35:13 -040011939 /* Track the max number of EQEs processed in 1 intr */
11940 if (ecount > fpeq->EQ_max_eqe)
11941 fpeq->EQ_max_eqe = ecount;
11942
James Smart4f774512009-05-22 14:52:35 -040011943 /* Always clear and re-arm the fast-path EQ */
11944 lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_REARM);
11945
11946 if (unlikely(ecount == 0)) {
James Smartb84daac2012-08-03 12:35:13 -040011947 fpeq->EQ_no_entry++;
James Smart4f774512009-05-22 14:52:35 -040011948 if (phba->intr_type == MSIX)
11949 /* MSI-X treated interrupt served as no EQ share INT */
11950 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11951 "0358 MSI-X interrupt with no EQE\n");
11952 else
11953 /* Non MSI-X treated on interrupt as EQ share INT */
11954 return IRQ_NONE;
11955 }
11956
11957 return IRQ_HANDLED;
11958} /* lpfc_sli4_fp_intr_handler */
11959
11960/**
11961 * lpfc_sli4_intr_handler - Device-level interrupt handler for SLI-4 device
11962 * @irq: Interrupt number.
11963 * @dev_id: The device context pointer.
11964 *
11965 * This function is the device-level interrupt handler to device with SLI-4
11966 * interface spec, called from the PCI layer when either MSI or Pin-IRQ
11967 * interrupt mode is enabled and there is an event in the HBA which requires
11968 * driver attention. This function invokes the slow-path interrupt attention
11969 * handling function and fast-path interrupt attention handling function in
11970 * turn to process the relevant HBA attention events. This function is called
11971 * without any lock held. It gets the hbalock to access and update SLI data
11972 * structures.
11973 *
11974 * This function returns IRQ_HANDLED when interrupt is handled, else it
11975 * returns IRQ_NONE.
11976 **/
11977irqreturn_t
11978lpfc_sli4_intr_handler(int irq, void *dev_id)
11979{
11980 struct lpfc_hba *phba;
11981 irqreturn_t sp_irq_rc, fp_irq_rc;
11982 bool fp_handled = false;
11983 uint32_t fcp_eqidx;
11984
11985 /* Get the driver's phba structure from the dev_id */
11986 phba = (struct lpfc_hba *)dev_id;
11987
11988 if (unlikely(!phba))
11989 return IRQ_NONE;
11990
11991 /*
11992 * Invokes slow-path host attention interrupt handling as appropriate.
11993 */
11994 sp_irq_rc = lpfc_sli4_sp_intr_handler(irq, dev_id);
11995
11996 /*
11997 * Invoke fast-path host attention interrupt handling as appropriate.
11998 */
11999 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count; fcp_eqidx++) {
12000 fp_irq_rc = lpfc_sli4_fp_intr_handler(irq,
12001 &phba->sli4_hba.fcp_eq_hdl[fcp_eqidx]);
12002 if (fp_irq_rc == IRQ_HANDLED)
12003 fp_handled |= true;
12004 }
12005
12006 return (fp_handled == true) ? IRQ_HANDLED : sp_irq_rc;
12007} /* lpfc_sli4_intr_handler */
12008
12009/**
12010 * lpfc_sli4_queue_free - free a queue structure and associated memory
12011 * @queue: The queue structure to free.
12012 *
Uwe Kleine-Königb5950762010-11-01 15:38:34 -040012013 * This function frees a queue structure and the DMAable memory used for
James Smart4f774512009-05-22 14:52:35 -040012014 * the host resident queue. This function must be called after destroying the
12015 * queue on the HBA.
12016 **/
12017void
12018lpfc_sli4_queue_free(struct lpfc_queue *queue)
12019{
12020 struct lpfc_dmabuf *dmabuf;
12021
12022 if (!queue)
12023 return;
12024
12025 while (!list_empty(&queue->page_list)) {
12026 list_remove_head(&queue->page_list, dmabuf, struct lpfc_dmabuf,
12027 list);
James Smart49198b32010-04-06 15:04:33 -040012028 dma_free_coherent(&queue->phba->pcidev->dev, SLI4_PAGE_SIZE,
James Smart4f774512009-05-22 14:52:35 -040012029 dmabuf->virt, dmabuf->phys);
12030 kfree(dmabuf);
12031 }
12032 kfree(queue);
12033 return;
12034}
12035
12036/**
12037 * lpfc_sli4_queue_alloc - Allocate and initialize a queue structure
12038 * @phba: The HBA that this queue is being created on.
12039 * @entry_size: The size of each queue entry for this queue.
12040 * @entry count: The number of entries that this queue will handle.
12041 *
12042 * This function allocates a queue structure and the DMAable memory used for
12043 * the host resident queue. This function must be called before creating the
12044 * queue on the HBA.
12045 **/
12046struct lpfc_queue *
12047lpfc_sli4_queue_alloc(struct lpfc_hba *phba, uint32_t entry_size,
12048 uint32_t entry_count)
12049{
12050 struct lpfc_queue *queue;
12051 struct lpfc_dmabuf *dmabuf;
12052 int x, total_qe_count;
12053 void *dma_pointer;
James Smartcb5172e2010-03-15 11:25:07 -040012054 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smart4f774512009-05-22 14:52:35 -040012055
James Smartcb5172e2010-03-15 11:25:07 -040012056 if (!phba->sli4_hba.pc_sli4_params.supported)
12057 hw_page_size = SLI4_PAGE_SIZE;
12058
James Smart4f774512009-05-22 14:52:35 -040012059 queue = kzalloc(sizeof(struct lpfc_queue) +
12060 (sizeof(union sli4_qe) * entry_count), GFP_KERNEL);
12061 if (!queue)
12062 return NULL;
James Smartcb5172e2010-03-15 11:25:07 -040012063 queue->page_count = (ALIGN(entry_size * entry_count,
12064 hw_page_size))/hw_page_size;
James Smart4f774512009-05-22 14:52:35 -040012065 INIT_LIST_HEAD(&queue->list);
12066 INIT_LIST_HEAD(&queue->page_list);
12067 INIT_LIST_HEAD(&queue->child_list);
12068 for (x = 0, total_qe_count = 0; x < queue->page_count; x++) {
12069 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
12070 if (!dmabuf)
12071 goto out_fail;
12072 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
James Smartcb5172e2010-03-15 11:25:07 -040012073 hw_page_size, &dmabuf->phys,
James Smart4f774512009-05-22 14:52:35 -040012074 GFP_KERNEL);
12075 if (!dmabuf->virt) {
12076 kfree(dmabuf);
12077 goto out_fail;
12078 }
James Smartcb5172e2010-03-15 11:25:07 -040012079 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -040012080 dmabuf->buffer_tag = x;
12081 list_add_tail(&dmabuf->list, &queue->page_list);
12082 /* initialize queue's entry array */
12083 dma_pointer = dmabuf->virt;
12084 for (; total_qe_count < entry_count &&
James Smartcb5172e2010-03-15 11:25:07 -040012085 dma_pointer < (hw_page_size + dmabuf->virt);
James Smart4f774512009-05-22 14:52:35 -040012086 total_qe_count++, dma_pointer += entry_size) {
12087 queue->qe[total_qe_count].address = dma_pointer;
12088 }
12089 }
12090 queue->entry_size = entry_size;
12091 queue->entry_count = entry_count;
James Smart73d91e52011-10-10 21:32:10 -040012092
12093 /*
12094 * entry_repost is calculated based on the number of entries in the
12095 * queue. This works out except for RQs. If buffers are NOT initially
12096 * posted for every RQE, entry_repost should be adjusted accordingly.
12097 */
12098 queue->entry_repost = (entry_count >> 3);
12099 if (queue->entry_repost < LPFC_QUEUE_MIN_REPOST)
12100 queue->entry_repost = LPFC_QUEUE_MIN_REPOST;
James Smart4f774512009-05-22 14:52:35 -040012101 queue->phba = phba;
12102
12103 return queue;
12104out_fail:
12105 lpfc_sli4_queue_free(queue);
12106 return NULL;
12107}
12108
12109/**
James Smart173edbb2012-06-12 13:54:50 -040012110 * lpfc_modify_fcp_eq_delay - Modify Delay Multiplier on FCP EQs
12111 * @phba: HBA structure that indicates port to create a queue on.
12112 * @startq: The starting FCP EQ to modify
12113 *
12114 * This function sends an MODIFY_EQ_DELAY mailbox command to the HBA.
12115 *
12116 * The @phba struct is used to send mailbox command to HBA. The @startq
12117 * is used to get the starting FCP EQ to change.
12118 * This function is asynchronous and will wait for the mailbox
12119 * command to finish before continuing.
12120 *
12121 * On success this function will return a zero. If unable to allocate enough
12122 * memory this function will return -ENOMEM. If the queue create mailbox command
12123 * fails this function will return -ENXIO.
12124 **/
12125uint32_t
12126lpfc_modify_fcp_eq_delay(struct lpfc_hba *phba, uint16_t startq)
12127{
12128 struct lpfc_mbx_modify_eq_delay *eq_delay;
12129 LPFC_MBOXQ_t *mbox;
12130 struct lpfc_queue *eq;
12131 int cnt, rc, length, status = 0;
12132 uint32_t shdr_status, shdr_add_status;
12133 int fcp_eqidx;
12134 union lpfc_sli4_cfg_shdr *shdr;
12135 uint16_t dmult;
12136
12137 if (startq >= phba->cfg_fcp_eq_count)
12138 return 0;
12139
12140 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12141 if (!mbox)
12142 return -ENOMEM;
12143 length = (sizeof(struct lpfc_mbx_modify_eq_delay) -
12144 sizeof(struct lpfc_sli4_cfg_mhdr));
12145 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
12146 LPFC_MBOX_OPCODE_MODIFY_EQ_DELAY,
12147 length, LPFC_SLI4_MBX_EMBED);
12148 eq_delay = &mbox->u.mqe.un.eq_delay;
12149
12150 /* Calculate delay multiper from maximum interrupt per second */
12151 dmult = LPFC_DMULT_CONST/phba->cfg_fcp_imax - 1;
12152
12153 cnt = 0;
12154 for (fcp_eqidx = startq; fcp_eqidx < phba->cfg_fcp_eq_count;
12155 fcp_eqidx++) {
12156 eq = phba->sli4_hba.fp_eq[fcp_eqidx];
12157 if (!eq)
12158 continue;
12159 eq_delay->u.request.eq[cnt].eq_id = eq->queue_id;
12160 eq_delay->u.request.eq[cnt].phase = 0;
12161 eq_delay->u.request.eq[cnt].delay_multi = dmult;
12162 cnt++;
12163 if (cnt >= LPFC_MAX_EQ_DELAY)
12164 break;
12165 }
12166 eq_delay->u.request.num_eq = cnt;
12167
12168 mbox->vport = phba->pport;
12169 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
12170 mbox->context1 = NULL;
12171 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
12172 shdr = (union lpfc_sli4_cfg_shdr *) &eq_delay->header.cfg_shdr;
12173 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12174 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12175 if (shdr_status || shdr_add_status || rc) {
12176 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12177 "2512 MODIFY_EQ_DELAY mailbox failed with "
12178 "status x%x add_status x%x, mbx status x%x\n",
12179 shdr_status, shdr_add_status, rc);
12180 status = -ENXIO;
12181 }
12182 mempool_free(mbox, phba->mbox_mem_pool);
12183 return status;
12184}
12185
12186/**
James Smart4f774512009-05-22 14:52:35 -040012187 * lpfc_eq_create - Create an Event Queue on the HBA
12188 * @phba: HBA structure that indicates port to create a queue on.
12189 * @eq: The queue structure to use to create the event queue.
12190 * @imax: The maximum interrupt per second limit.
12191 *
12192 * This function creates an event queue, as detailed in @eq, on a port,
12193 * described by @phba by sending an EQ_CREATE mailbox command to the HBA.
12194 *
12195 * The @phba struct is used to send mailbox command to HBA. The @eq struct
12196 * is used to get the entry count and entry size that are necessary to
12197 * determine the number of pages to allocate and use for this queue. This
12198 * function will send the EQ_CREATE mailbox command to the HBA to setup the
12199 * event queue. This function is asynchronous and will wait for the mailbox
12200 * command to finish before continuing.
12201 *
12202 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040012203 * memory this function will return -ENOMEM. If the queue create mailbox command
12204 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040012205 **/
12206uint32_t
12207lpfc_eq_create(struct lpfc_hba *phba, struct lpfc_queue *eq, uint16_t imax)
12208{
12209 struct lpfc_mbx_eq_create *eq_create;
12210 LPFC_MBOXQ_t *mbox;
12211 int rc, length, status = 0;
12212 struct lpfc_dmabuf *dmabuf;
12213 uint32_t shdr_status, shdr_add_status;
12214 union lpfc_sli4_cfg_shdr *shdr;
12215 uint16_t dmult;
James Smart49198b32010-04-06 15:04:33 -040012216 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
12217
James Smart2e90f4b2011-12-13 13:22:37 -050012218 /* sanity check on queue memory */
12219 if (!eq)
12220 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040012221 if (!phba->sli4_hba.pc_sli4_params.supported)
12222 hw_page_size = SLI4_PAGE_SIZE;
James Smart4f774512009-05-22 14:52:35 -040012223
12224 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12225 if (!mbox)
12226 return -ENOMEM;
12227 length = (sizeof(struct lpfc_mbx_eq_create) -
12228 sizeof(struct lpfc_sli4_cfg_mhdr));
12229 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
12230 LPFC_MBOX_OPCODE_EQ_CREATE,
12231 length, LPFC_SLI4_MBX_EMBED);
12232 eq_create = &mbox->u.mqe.un.eq_create;
12233 bf_set(lpfc_mbx_eq_create_num_pages, &eq_create->u.request,
12234 eq->page_count);
12235 bf_set(lpfc_eq_context_size, &eq_create->u.request.context,
12236 LPFC_EQE_SIZE);
12237 bf_set(lpfc_eq_context_valid, &eq_create->u.request.context, 1);
12238 /* Calculate delay multiper from maximum interrupt per second */
12239 dmult = LPFC_DMULT_CONST/imax - 1;
12240 bf_set(lpfc_eq_context_delay_multi, &eq_create->u.request.context,
12241 dmult);
12242 switch (eq->entry_count) {
12243 default:
12244 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12245 "0360 Unsupported EQ count. (%d)\n",
12246 eq->entry_count);
12247 if (eq->entry_count < 256)
12248 return -EINVAL;
12249 /* otherwise default to smallest count (drop through) */
12250 case 256:
12251 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
12252 LPFC_EQ_CNT_256);
12253 break;
12254 case 512:
12255 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
12256 LPFC_EQ_CNT_512);
12257 break;
12258 case 1024:
12259 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
12260 LPFC_EQ_CNT_1024);
12261 break;
12262 case 2048:
12263 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
12264 LPFC_EQ_CNT_2048);
12265 break;
12266 case 4096:
12267 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
12268 LPFC_EQ_CNT_4096);
12269 break;
12270 }
12271 list_for_each_entry(dmabuf, &eq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040012272 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -040012273 eq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
12274 putPaddrLow(dmabuf->phys);
12275 eq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
12276 putPaddrHigh(dmabuf->phys);
12277 }
12278 mbox->vport = phba->pport;
12279 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
12280 mbox->context1 = NULL;
12281 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
12282 shdr = (union lpfc_sli4_cfg_shdr *) &eq_create->header.cfg_shdr;
12283 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12284 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12285 if (shdr_status || shdr_add_status || rc) {
12286 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12287 "2500 EQ_CREATE mailbox failed with "
12288 "status x%x add_status x%x, mbx status x%x\n",
12289 shdr_status, shdr_add_status, rc);
12290 status = -ENXIO;
12291 }
12292 eq->type = LPFC_EQ;
12293 eq->subtype = LPFC_NONE;
12294 eq->queue_id = bf_get(lpfc_mbx_eq_create_q_id, &eq_create->u.response);
12295 if (eq->queue_id == 0xFFFF)
12296 status = -ENXIO;
12297 eq->host_index = 0;
12298 eq->hba_index = 0;
12299
James Smart8fa38512009-07-19 10:01:03 -040012300 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040012301 return status;
12302}
12303
12304/**
12305 * lpfc_cq_create - Create a Completion Queue on the HBA
12306 * @phba: HBA structure that indicates port to create a queue on.
12307 * @cq: The queue structure to use to create the completion queue.
12308 * @eq: The event queue to bind this completion queue to.
12309 *
12310 * This function creates a completion queue, as detailed in @wq, on a port,
12311 * described by @phba by sending a CQ_CREATE mailbox command to the HBA.
12312 *
12313 * The @phba struct is used to send mailbox command to HBA. The @cq struct
12314 * is used to get the entry count and entry size that are necessary to
12315 * determine the number of pages to allocate and use for this queue. The @eq
12316 * is used to indicate which event queue to bind this completion queue to. This
12317 * function will send the CQ_CREATE mailbox command to the HBA to setup the
12318 * completion queue. This function is asynchronous and will wait for the mailbox
12319 * command to finish before continuing.
12320 *
12321 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040012322 * memory this function will return -ENOMEM. If the queue create mailbox command
12323 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040012324 **/
12325uint32_t
12326lpfc_cq_create(struct lpfc_hba *phba, struct lpfc_queue *cq,
12327 struct lpfc_queue *eq, uint32_t type, uint32_t subtype)
12328{
12329 struct lpfc_mbx_cq_create *cq_create;
12330 struct lpfc_dmabuf *dmabuf;
12331 LPFC_MBOXQ_t *mbox;
12332 int rc, length, status = 0;
12333 uint32_t shdr_status, shdr_add_status;
12334 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040012335 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
12336
James Smart2e90f4b2011-12-13 13:22:37 -050012337 /* sanity check on queue memory */
12338 if (!cq || !eq)
12339 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040012340 if (!phba->sli4_hba.pc_sli4_params.supported)
12341 hw_page_size = SLI4_PAGE_SIZE;
12342
James Smart4f774512009-05-22 14:52:35 -040012343 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12344 if (!mbox)
12345 return -ENOMEM;
12346 length = (sizeof(struct lpfc_mbx_cq_create) -
12347 sizeof(struct lpfc_sli4_cfg_mhdr));
12348 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
12349 LPFC_MBOX_OPCODE_CQ_CREATE,
12350 length, LPFC_SLI4_MBX_EMBED);
12351 cq_create = &mbox->u.mqe.un.cq_create;
James Smart5a6f1332011-03-11 16:05:35 -050012352 shdr = (union lpfc_sli4_cfg_shdr *) &cq_create->header.cfg_shdr;
James Smart4f774512009-05-22 14:52:35 -040012353 bf_set(lpfc_mbx_cq_create_num_pages, &cq_create->u.request,
12354 cq->page_count);
12355 bf_set(lpfc_cq_context_event, &cq_create->u.request.context, 1);
12356 bf_set(lpfc_cq_context_valid, &cq_create->u.request.context, 1);
James Smart5a6f1332011-03-11 16:05:35 -050012357 bf_set(lpfc_mbox_hdr_version, &shdr->request,
12358 phba->sli4_hba.pc_sli4_params.cqv);
12359 if (phba->sli4_hba.pc_sli4_params.cqv == LPFC_Q_CREATE_VERSION_2) {
James Smartc31098c2011-04-16 11:03:33 -040012360 /* FW only supports 1. Should be PAGE_SIZE/SLI4_PAGE_SIZE */
12361 bf_set(lpfc_mbx_cq_create_page_size, &cq_create->u.request, 1);
James Smart5a6f1332011-03-11 16:05:35 -050012362 bf_set(lpfc_cq_eq_id_2, &cq_create->u.request.context,
12363 eq->queue_id);
12364 } else {
12365 bf_set(lpfc_cq_eq_id, &cq_create->u.request.context,
12366 eq->queue_id);
12367 }
James Smart4f774512009-05-22 14:52:35 -040012368 switch (cq->entry_count) {
12369 default:
12370 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12371 "0361 Unsupported CQ count. (%d)\n",
12372 cq->entry_count);
James Smart4f4c1862012-06-12 13:54:02 -040012373 if (cq->entry_count < 256) {
12374 status = -EINVAL;
12375 goto out;
12376 }
James Smart4f774512009-05-22 14:52:35 -040012377 /* otherwise default to smallest count (drop through) */
12378 case 256:
12379 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
12380 LPFC_CQ_CNT_256);
12381 break;
12382 case 512:
12383 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
12384 LPFC_CQ_CNT_512);
12385 break;
12386 case 1024:
12387 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
12388 LPFC_CQ_CNT_1024);
12389 break;
12390 }
12391 list_for_each_entry(dmabuf, &cq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040012392 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -040012393 cq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
12394 putPaddrLow(dmabuf->phys);
12395 cq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
12396 putPaddrHigh(dmabuf->phys);
12397 }
12398 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
12399
12400 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart4f774512009-05-22 14:52:35 -040012401 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12402 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12403 if (shdr_status || shdr_add_status || rc) {
12404 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12405 "2501 CQ_CREATE mailbox failed with "
12406 "status x%x add_status x%x, mbx status x%x\n",
12407 shdr_status, shdr_add_status, rc);
12408 status = -ENXIO;
12409 goto out;
12410 }
12411 cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
12412 if (cq->queue_id == 0xFFFF) {
12413 status = -ENXIO;
12414 goto out;
12415 }
12416 /* link the cq onto the parent eq child list */
12417 list_add_tail(&cq->list, &eq->child_list);
12418 /* Set up completion queue's type and subtype */
12419 cq->type = type;
12420 cq->subtype = subtype;
12421 cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
James Smart2a622bf2011-02-16 12:40:06 -050012422 cq->assoc_qid = eq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040012423 cq->host_index = 0;
12424 cq->hba_index = 0;
James Smart4f774512009-05-22 14:52:35 -040012425
James Smart8fa38512009-07-19 10:01:03 -040012426out:
12427 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040012428 return status;
12429}
12430
12431/**
James Smartb19a0612010-04-06 14:48:51 -040012432 * lpfc_mq_create_fb_init - Send MCC_CREATE without async events registration
James Smart04c68492009-05-22 14:52:52 -040012433 * @phba: HBA structure that indicates port to create a queue on.
12434 * @mq: The queue structure to use to create the mailbox queue.
James Smartb19a0612010-04-06 14:48:51 -040012435 * @mbox: An allocated pointer to type LPFC_MBOXQ_t
12436 * @cq: The completion queue to associate with this cq.
James Smart04c68492009-05-22 14:52:52 -040012437 *
James Smartb19a0612010-04-06 14:48:51 -040012438 * This function provides failback (fb) functionality when the
12439 * mq_create_ext fails on older FW generations. It's purpose is identical
12440 * to mq_create_ext otherwise.
James Smart04c68492009-05-22 14:52:52 -040012441 *
James Smartb19a0612010-04-06 14:48:51 -040012442 * This routine cannot fail as all attributes were previously accessed and
12443 * initialized in mq_create_ext.
James Smart04c68492009-05-22 14:52:52 -040012444 **/
James Smartb19a0612010-04-06 14:48:51 -040012445static void
12446lpfc_mq_create_fb_init(struct lpfc_hba *phba, struct lpfc_queue *mq,
12447 LPFC_MBOXQ_t *mbox, struct lpfc_queue *cq)
James Smart04c68492009-05-22 14:52:52 -040012448{
12449 struct lpfc_mbx_mq_create *mq_create;
12450 struct lpfc_dmabuf *dmabuf;
James Smartb19a0612010-04-06 14:48:51 -040012451 int length;
James Smart04c68492009-05-22 14:52:52 -040012452
James Smart04c68492009-05-22 14:52:52 -040012453 length = (sizeof(struct lpfc_mbx_mq_create) -
12454 sizeof(struct lpfc_sli4_cfg_mhdr));
12455 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
12456 LPFC_MBOX_OPCODE_MQ_CREATE,
12457 length, LPFC_SLI4_MBX_EMBED);
12458 mq_create = &mbox->u.mqe.un.mq_create;
12459 bf_set(lpfc_mbx_mq_create_num_pages, &mq_create->u.request,
James Smartb19a0612010-04-06 14:48:51 -040012460 mq->page_count);
James Smart04c68492009-05-22 14:52:52 -040012461 bf_set(lpfc_mq_context_cq_id, &mq_create->u.request.context,
James Smartb19a0612010-04-06 14:48:51 -040012462 cq->queue_id);
James Smart04c68492009-05-22 14:52:52 -040012463 bf_set(lpfc_mq_context_valid, &mq_create->u.request.context, 1);
12464 switch (mq->entry_count) {
James Smart04c68492009-05-22 14:52:52 -040012465 case 16:
James Smart5a6f1332011-03-11 16:05:35 -050012466 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
12467 LPFC_MQ_RING_SIZE_16);
James Smart04c68492009-05-22 14:52:52 -040012468 break;
12469 case 32:
James Smart5a6f1332011-03-11 16:05:35 -050012470 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
12471 LPFC_MQ_RING_SIZE_32);
James Smart04c68492009-05-22 14:52:52 -040012472 break;
12473 case 64:
James Smart5a6f1332011-03-11 16:05:35 -050012474 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
12475 LPFC_MQ_RING_SIZE_64);
James Smart04c68492009-05-22 14:52:52 -040012476 break;
12477 case 128:
James Smart5a6f1332011-03-11 16:05:35 -050012478 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
12479 LPFC_MQ_RING_SIZE_128);
James Smart04c68492009-05-22 14:52:52 -040012480 break;
12481 }
12482 list_for_each_entry(dmabuf, &mq->page_list, list) {
12483 mq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
James Smartb19a0612010-04-06 14:48:51 -040012484 putPaddrLow(dmabuf->phys);
James Smart04c68492009-05-22 14:52:52 -040012485 mq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
James Smartb19a0612010-04-06 14:48:51 -040012486 putPaddrHigh(dmabuf->phys);
12487 }
12488}
12489
12490/**
12491 * lpfc_mq_create - Create a mailbox Queue on the HBA
12492 * @phba: HBA structure that indicates port to create a queue on.
12493 * @mq: The queue structure to use to create the mailbox queue.
12494 * @cq: The completion queue to associate with this cq.
12495 * @subtype: The queue's subtype.
12496 *
12497 * This function creates a mailbox queue, as detailed in @mq, on a port,
12498 * described by @phba by sending a MQ_CREATE mailbox command to the HBA.
12499 *
12500 * The @phba struct is used to send mailbox command to HBA. The @cq struct
12501 * is used to get the entry count and entry size that are necessary to
12502 * determine the number of pages to allocate and use for this queue. This
12503 * function will send the MQ_CREATE mailbox command to the HBA to setup the
12504 * mailbox queue. This function is asynchronous and will wait for the mailbox
12505 * command to finish before continuing.
12506 *
12507 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040012508 * memory this function will return -ENOMEM. If the queue create mailbox command
12509 * fails this function will return -ENXIO.
James Smartb19a0612010-04-06 14:48:51 -040012510 **/
12511int32_t
12512lpfc_mq_create(struct lpfc_hba *phba, struct lpfc_queue *mq,
12513 struct lpfc_queue *cq, uint32_t subtype)
12514{
12515 struct lpfc_mbx_mq_create *mq_create;
12516 struct lpfc_mbx_mq_create_ext *mq_create_ext;
12517 struct lpfc_dmabuf *dmabuf;
12518 LPFC_MBOXQ_t *mbox;
12519 int rc, length, status = 0;
12520 uint32_t shdr_status, shdr_add_status;
12521 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040012522 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smartb19a0612010-04-06 14:48:51 -040012523
James Smart2e90f4b2011-12-13 13:22:37 -050012524 /* sanity check on queue memory */
12525 if (!mq || !cq)
12526 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040012527 if (!phba->sli4_hba.pc_sli4_params.supported)
12528 hw_page_size = SLI4_PAGE_SIZE;
James Smartb19a0612010-04-06 14:48:51 -040012529
12530 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12531 if (!mbox)
12532 return -ENOMEM;
12533 length = (sizeof(struct lpfc_mbx_mq_create_ext) -
12534 sizeof(struct lpfc_sli4_cfg_mhdr));
12535 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
12536 LPFC_MBOX_OPCODE_MQ_CREATE_EXT,
12537 length, LPFC_SLI4_MBX_EMBED);
12538
12539 mq_create_ext = &mbox->u.mqe.un.mq_create_ext;
James Smart5a6f1332011-03-11 16:05:35 -050012540 shdr = (union lpfc_sli4_cfg_shdr *) &mq_create_ext->header.cfg_shdr;
James Smart70f3c072010-12-15 17:57:33 -050012541 bf_set(lpfc_mbx_mq_create_ext_num_pages,
12542 &mq_create_ext->u.request, mq->page_count);
12543 bf_set(lpfc_mbx_mq_create_ext_async_evt_link,
12544 &mq_create_ext->u.request, 1);
12545 bf_set(lpfc_mbx_mq_create_ext_async_evt_fip,
James Smartb19a0612010-04-06 14:48:51 -040012546 &mq_create_ext->u.request, 1);
12547 bf_set(lpfc_mbx_mq_create_ext_async_evt_group5,
12548 &mq_create_ext->u.request, 1);
James Smart70f3c072010-12-15 17:57:33 -050012549 bf_set(lpfc_mbx_mq_create_ext_async_evt_fc,
12550 &mq_create_ext->u.request, 1);
12551 bf_set(lpfc_mbx_mq_create_ext_async_evt_sli,
12552 &mq_create_ext->u.request, 1);
James Smartb19a0612010-04-06 14:48:51 -040012553 bf_set(lpfc_mq_context_valid, &mq_create_ext->u.request.context, 1);
James Smart5a6f1332011-03-11 16:05:35 -050012554 bf_set(lpfc_mbox_hdr_version, &shdr->request,
12555 phba->sli4_hba.pc_sli4_params.mqv);
12556 if (phba->sli4_hba.pc_sli4_params.mqv == LPFC_Q_CREATE_VERSION_1)
12557 bf_set(lpfc_mbx_mq_create_ext_cq_id, &mq_create_ext->u.request,
12558 cq->queue_id);
12559 else
12560 bf_set(lpfc_mq_context_cq_id, &mq_create_ext->u.request.context,
12561 cq->queue_id);
James Smartb19a0612010-04-06 14:48:51 -040012562 switch (mq->entry_count) {
12563 default:
12564 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12565 "0362 Unsupported MQ count. (%d)\n",
12566 mq->entry_count);
James Smart4f4c1862012-06-12 13:54:02 -040012567 if (mq->entry_count < 16) {
12568 status = -EINVAL;
12569 goto out;
12570 }
James Smartb19a0612010-04-06 14:48:51 -040012571 /* otherwise default to smallest count (drop through) */
12572 case 16:
James Smart5a6f1332011-03-11 16:05:35 -050012573 bf_set(lpfc_mq_context_ring_size,
12574 &mq_create_ext->u.request.context,
12575 LPFC_MQ_RING_SIZE_16);
James Smartb19a0612010-04-06 14:48:51 -040012576 break;
12577 case 32:
James Smart5a6f1332011-03-11 16:05:35 -050012578 bf_set(lpfc_mq_context_ring_size,
12579 &mq_create_ext->u.request.context,
12580 LPFC_MQ_RING_SIZE_32);
James Smartb19a0612010-04-06 14:48:51 -040012581 break;
12582 case 64:
James Smart5a6f1332011-03-11 16:05:35 -050012583 bf_set(lpfc_mq_context_ring_size,
12584 &mq_create_ext->u.request.context,
12585 LPFC_MQ_RING_SIZE_64);
James Smartb19a0612010-04-06 14:48:51 -040012586 break;
12587 case 128:
James Smart5a6f1332011-03-11 16:05:35 -050012588 bf_set(lpfc_mq_context_ring_size,
12589 &mq_create_ext->u.request.context,
12590 LPFC_MQ_RING_SIZE_128);
James Smartb19a0612010-04-06 14:48:51 -040012591 break;
12592 }
12593 list_for_each_entry(dmabuf, &mq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040012594 memset(dmabuf->virt, 0, hw_page_size);
James Smartb19a0612010-04-06 14:48:51 -040012595 mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_lo =
12596 putPaddrLow(dmabuf->phys);
12597 mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_hi =
James Smart04c68492009-05-22 14:52:52 -040012598 putPaddrHigh(dmabuf->phys);
12599 }
12600 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
James Smartb19a0612010-04-06 14:48:51 -040012601 mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
12602 &mq_create_ext->u.response);
12603 if (rc != MBX_SUCCESS) {
12604 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
12605 "2795 MQ_CREATE_EXT failed with "
12606 "status x%x. Failback to MQ_CREATE.\n",
12607 rc);
12608 lpfc_mq_create_fb_init(phba, mq, mbox, cq);
12609 mq_create = &mbox->u.mqe.un.mq_create;
12610 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
12611 shdr = (union lpfc_sli4_cfg_shdr *) &mq_create->header.cfg_shdr;
12612 mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
12613 &mq_create->u.response);
12614 }
12615
James Smart04c68492009-05-22 14:52:52 -040012616 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart04c68492009-05-22 14:52:52 -040012617 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12618 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12619 if (shdr_status || shdr_add_status || rc) {
12620 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12621 "2502 MQ_CREATE mailbox failed with "
12622 "status x%x add_status x%x, mbx status x%x\n",
12623 shdr_status, shdr_add_status, rc);
12624 status = -ENXIO;
12625 goto out;
12626 }
James Smart04c68492009-05-22 14:52:52 -040012627 if (mq->queue_id == 0xFFFF) {
12628 status = -ENXIO;
12629 goto out;
12630 }
12631 mq->type = LPFC_MQ;
James Smart2a622bf2011-02-16 12:40:06 -050012632 mq->assoc_qid = cq->queue_id;
James Smart04c68492009-05-22 14:52:52 -040012633 mq->subtype = subtype;
12634 mq->host_index = 0;
12635 mq->hba_index = 0;
12636
12637 /* link the mq onto the parent cq child list */
12638 list_add_tail(&mq->list, &cq->child_list);
12639out:
James Smart8fa38512009-07-19 10:01:03 -040012640 mempool_free(mbox, phba->mbox_mem_pool);
James Smart04c68492009-05-22 14:52:52 -040012641 return status;
12642}
12643
12644/**
James Smart4f774512009-05-22 14:52:35 -040012645 * lpfc_wq_create - Create a Work Queue on the HBA
12646 * @phba: HBA structure that indicates port to create a queue on.
12647 * @wq: The queue structure to use to create the work queue.
12648 * @cq: The completion queue to bind this work queue to.
12649 * @subtype: The subtype of the work queue indicating its functionality.
12650 *
12651 * This function creates a work queue, as detailed in @wq, on a port, described
12652 * by @phba by sending a WQ_CREATE mailbox command to the HBA.
12653 *
12654 * The @phba struct is used to send mailbox command to HBA. The @wq struct
12655 * is used to get the entry count and entry size that are necessary to
12656 * determine the number of pages to allocate and use for this queue. The @cq
12657 * is used to indicate which completion queue to bind this work queue to. This
12658 * function will send the WQ_CREATE mailbox command to the HBA to setup the
12659 * work queue. This function is asynchronous and will wait for the mailbox
12660 * command to finish before continuing.
12661 *
12662 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040012663 * memory this function will return -ENOMEM. If the queue create mailbox command
12664 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040012665 **/
12666uint32_t
12667lpfc_wq_create(struct lpfc_hba *phba, struct lpfc_queue *wq,
12668 struct lpfc_queue *cq, uint32_t subtype)
12669{
12670 struct lpfc_mbx_wq_create *wq_create;
12671 struct lpfc_dmabuf *dmabuf;
12672 LPFC_MBOXQ_t *mbox;
12673 int rc, length, status = 0;
12674 uint32_t shdr_status, shdr_add_status;
12675 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040012676 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smart5a6f1332011-03-11 16:05:35 -050012677 struct dma_address *page;
James Smart49198b32010-04-06 15:04:33 -040012678
James Smart2e90f4b2011-12-13 13:22:37 -050012679 /* sanity check on queue memory */
12680 if (!wq || !cq)
12681 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040012682 if (!phba->sli4_hba.pc_sli4_params.supported)
12683 hw_page_size = SLI4_PAGE_SIZE;
James Smart4f774512009-05-22 14:52:35 -040012684
12685 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12686 if (!mbox)
12687 return -ENOMEM;
12688 length = (sizeof(struct lpfc_mbx_wq_create) -
12689 sizeof(struct lpfc_sli4_cfg_mhdr));
12690 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
12691 LPFC_MBOX_OPCODE_FCOE_WQ_CREATE,
12692 length, LPFC_SLI4_MBX_EMBED);
12693 wq_create = &mbox->u.mqe.un.wq_create;
James Smart5a6f1332011-03-11 16:05:35 -050012694 shdr = (union lpfc_sli4_cfg_shdr *) &wq_create->header.cfg_shdr;
James Smart4f774512009-05-22 14:52:35 -040012695 bf_set(lpfc_mbx_wq_create_num_pages, &wq_create->u.request,
12696 wq->page_count);
12697 bf_set(lpfc_mbx_wq_create_cq_id, &wq_create->u.request,
12698 cq->queue_id);
James Smart5a6f1332011-03-11 16:05:35 -050012699 bf_set(lpfc_mbox_hdr_version, &shdr->request,
12700 phba->sli4_hba.pc_sli4_params.wqv);
12701 if (phba->sli4_hba.pc_sli4_params.wqv == LPFC_Q_CREATE_VERSION_1) {
12702 bf_set(lpfc_mbx_wq_create_wqe_count, &wq_create->u.request_1,
12703 wq->entry_count);
12704 switch (wq->entry_size) {
12705 default:
12706 case 64:
12707 bf_set(lpfc_mbx_wq_create_wqe_size,
12708 &wq_create->u.request_1,
12709 LPFC_WQ_WQE_SIZE_64);
12710 break;
12711 case 128:
12712 bf_set(lpfc_mbx_wq_create_wqe_size,
12713 &wq_create->u.request_1,
12714 LPFC_WQ_WQE_SIZE_128);
12715 break;
12716 }
12717 bf_set(lpfc_mbx_wq_create_page_size, &wq_create->u.request_1,
12718 (PAGE_SIZE/SLI4_PAGE_SIZE));
12719 page = wq_create->u.request_1.page;
12720 } else {
12721 page = wq_create->u.request.page;
12722 }
James Smart4f774512009-05-22 14:52:35 -040012723 list_for_each_entry(dmabuf, &wq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040012724 memset(dmabuf->virt, 0, hw_page_size);
James Smart5a6f1332011-03-11 16:05:35 -050012725 page[dmabuf->buffer_tag].addr_lo = putPaddrLow(dmabuf->phys);
12726 page[dmabuf->buffer_tag].addr_hi = putPaddrHigh(dmabuf->phys);
James Smart4f774512009-05-22 14:52:35 -040012727 }
12728 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
12729 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart4f774512009-05-22 14:52:35 -040012730 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12731 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12732 if (shdr_status || shdr_add_status || rc) {
12733 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12734 "2503 WQ_CREATE mailbox failed with "
12735 "status x%x add_status x%x, mbx status x%x\n",
12736 shdr_status, shdr_add_status, rc);
12737 status = -ENXIO;
12738 goto out;
12739 }
12740 wq->queue_id = bf_get(lpfc_mbx_wq_create_q_id, &wq_create->u.response);
12741 if (wq->queue_id == 0xFFFF) {
12742 status = -ENXIO;
12743 goto out;
12744 }
12745 wq->type = LPFC_WQ;
James Smart2a622bf2011-02-16 12:40:06 -050012746 wq->assoc_qid = cq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040012747 wq->subtype = subtype;
12748 wq->host_index = 0;
12749 wq->hba_index = 0;
James Smartff78d8f2011-12-13 13:21:35 -050012750 wq->entry_repost = LPFC_RELEASE_NOTIFICATION_INTERVAL;
James Smart4f774512009-05-22 14:52:35 -040012751
12752 /* link the wq onto the parent cq child list */
12753 list_add_tail(&wq->list, &cq->child_list);
12754out:
James Smart8fa38512009-07-19 10:01:03 -040012755 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040012756 return status;
12757}
12758
12759/**
James Smart73d91e52011-10-10 21:32:10 -040012760 * lpfc_rq_adjust_repost - Adjust entry_repost for an RQ
12761 * @phba: HBA structure that indicates port to create a queue on.
12762 * @rq: The queue structure to use for the receive queue.
12763 * @qno: The associated HBQ number
12764 *
12765 *
12766 * For SLI4 we need to adjust the RQ repost value based on
12767 * the number of buffers that are initially posted to the RQ.
12768 */
12769void
12770lpfc_rq_adjust_repost(struct lpfc_hba *phba, struct lpfc_queue *rq, int qno)
12771{
12772 uint32_t cnt;
12773
James Smart2e90f4b2011-12-13 13:22:37 -050012774 /* sanity check on queue memory */
12775 if (!rq)
12776 return;
James Smart73d91e52011-10-10 21:32:10 -040012777 cnt = lpfc_hbq_defs[qno]->entry_count;
12778
12779 /* Recalc repost for RQs based on buffers initially posted */
12780 cnt = (cnt >> 3);
12781 if (cnt < LPFC_QUEUE_MIN_REPOST)
12782 cnt = LPFC_QUEUE_MIN_REPOST;
12783
12784 rq->entry_repost = cnt;
12785}
12786
12787/**
James Smart4f774512009-05-22 14:52:35 -040012788 * lpfc_rq_create - Create a Receive Queue on the HBA
12789 * @phba: HBA structure that indicates port to create a queue on.
12790 * @hrq: The queue structure to use to create the header receive queue.
12791 * @drq: The queue structure to use to create the data receive queue.
12792 * @cq: The completion queue to bind this work queue to.
12793 *
12794 * This function creates a receive buffer queue pair , as detailed in @hrq and
12795 * @drq, on a port, described by @phba by sending a RQ_CREATE mailbox command
12796 * to the HBA.
12797 *
12798 * The @phba struct is used to send mailbox command to HBA. The @drq and @hrq
12799 * struct is used to get the entry count that is necessary to determine the
12800 * number of pages to use for this queue. The @cq is used to indicate which
12801 * completion queue to bind received buffers that are posted to these queues to.
12802 * This function will send the RQ_CREATE mailbox command to the HBA to setup the
12803 * receive queue pair. This function is asynchronous and will wait for the
12804 * mailbox command to finish before continuing.
12805 *
12806 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040012807 * memory this function will return -ENOMEM. If the queue create mailbox command
12808 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040012809 **/
12810uint32_t
12811lpfc_rq_create(struct lpfc_hba *phba, struct lpfc_queue *hrq,
12812 struct lpfc_queue *drq, struct lpfc_queue *cq, uint32_t subtype)
12813{
12814 struct lpfc_mbx_rq_create *rq_create;
12815 struct lpfc_dmabuf *dmabuf;
12816 LPFC_MBOXQ_t *mbox;
12817 int rc, length, status = 0;
12818 uint32_t shdr_status, shdr_add_status;
12819 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040012820 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
12821
James Smart2e90f4b2011-12-13 13:22:37 -050012822 /* sanity check on queue memory */
12823 if (!hrq || !drq || !cq)
12824 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040012825 if (!phba->sli4_hba.pc_sli4_params.supported)
12826 hw_page_size = SLI4_PAGE_SIZE;
James Smart4f774512009-05-22 14:52:35 -040012827
12828 if (hrq->entry_count != drq->entry_count)
12829 return -EINVAL;
12830 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12831 if (!mbox)
12832 return -ENOMEM;
12833 length = (sizeof(struct lpfc_mbx_rq_create) -
12834 sizeof(struct lpfc_sli4_cfg_mhdr));
12835 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
12836 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
12837 length, LPFC_SLI4_MBX_EMBED);
12838 rq_create = &mbox->u.mqe.un.rq_create;
James Smart5a6f1332011-03-11 16:05:35 -050012839 shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
12840 bf_set(lpfc_mbox_hdr_version, &shdr->request,
12841 phba->sli4_hba.pc_sli4_params.rqv);
12842 if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
12843 bf_set(lpfc_rq_context_rqe_count_1,
12844 &rq_create->u.request.context,
12845 hrq->entry_count);
12846 rq_create->u.request.context.buffer_size = LPFC_HDR_BUF_SIZE;
James Smartc31098c2011-04-16 11:03:33 -040012847 bf_set(lpfc_rq_context_rqe_size,
12848 &rq_create->u.request.context,
12849 LPFC_RQE_SIZE_8);
12850 bf_set(lpfc_rq_context_page_size,
12851 &rq_create->u.request.context,
12852 (PAGE_SIZE/SLI4_PAGE_SIZE));
James Smart5a6f1332011-03-11 16:05:35 -050012853 } else {
12854 switch (hrq->entry_count) {
12855 default:
12856 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12857 "2535 Unsupported RQ count. (%d)\n",
12858 hrq->entry_count);
James Smart4f4c1862012-06-12 13:54:02 -040012859 if (hrq->entry_count < 512) {
12860 status = -EINVAL;
12861 goto out;
12862 }
James Smart5a6f1332011-03-11 16:05:35 -050012863 /* otherwise default to smallest count (drop through) */
12864 case 512:
12865 bf_set(lpfc_rq_context_rqe_count,
12866 &rq_create->u.request.context,
12867 LPFC_RQ_RING_SIZE_512);
12868 break;
12869 case 1024:
12870 bf_set(lpfc_rq_context_rqe_count,
12871 &rq_create->u.request.context,
12872 LPFC_RQ_RING_SIZE_1024);
12873 break;
12874 case 2048:
12875 bf_set(lpfc_rq_context_rqe_count,
12876 &rq_create->u.request.context,
12877 LPFC_RQ_RING_SIZE_2048);
12878 break;
12879 case 4096:
12880 bf_set(lpfc_rq_context_rqe_count,
12881 &rq_create->u.request.context,
12882 LPFC_RQ_RING_SIZE_4096);
12883 break;
12884 }
12885 bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
12886 LPFC_HDR_BUF_SIZE);
James Smart4f774512009-05-22 14:52:35 -040012887 }
12888 bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
12889 cq->queue_id);
12890 bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
12891 hrq->page_count);
James Smart4f774512009-05-22 14:52:35 -040012892 list_for_each_entry(dmabuf, &hrq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040012893 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -040012894 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
12895 putPaddrLow(dmabuf->phys);
12896 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
12897 putPaddrHigh(dmabuf->phys);
12898 }
12899 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
12900 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart4f774512009-05-22 14:52:35 -040012901 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12902 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12903 if (shdr_status || shdr_add_status || rc) {
12904 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12905 "2504 RQ_CREATE mailbox failed with "
12906 "status x%x add_status x%x, mbx status x%x\n",
12907 shdr_status, shdr_add_status, rc);
12908 status = -ENXIO;
12909 goto out;
12910 }
12911 hrq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
12912 if (hrq->queue_id == 0xFFFF) {
12913 status = -ENXIO;
12914 goto out;
12915 }
12916 hrq->type = LPFC_HRQ;
James Smart2a622bf2011-02-16 12:40:06 -050012917 hrq->assoc_qid = cq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040012918 hrq->subtype = subtype;
12919 hrq->host_index = 0;
12920 hrq->hba_index = 0;
12921
12922 /* now create the data queue */
12923 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
12924 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
12925 length, LPFC_SLI4_MBX_EMBED);
James Smart5a6f1332011-03-11 16:05:35 -050012926 bf_set(lpfc_mbox_hdr_version, &shdr->request,
12927 phba->sli4_hba.pc_sli4_params.rqv);
12928 if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
12929 bf_set(lpfc_rq_context_rqe_count_1,
James Smartc31098c2011-04-16 11:03:33 -040012930 &rq_create->u.request.context, hrq->entry_count);
James Smart5a6f1332011-03-11 16:05:35 -050012931 rq_create->u.request.context.buffer_size = LPFC_DATA_BUF_SIZE;
James Smartc31098c2011-04-16 11:03:33 -040012932 bf_set(lpfc_rq_context_rqe_size, &rq_create->u.request.context,
12933 LPFC_RQE_SIZE_8);
12934 bf_set(lpfc_rq_context_page_size, &rq_create->u.request.context,
12935 (PAGE_SIZE/SLI4_PAGE_SIZE));
James Smart5a6f1332011-03-11 16:05:35 -050012936 } else {
12937 switch (drq->entry_count) {
12938 default:
12939 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12940 "2536 Unsupported RQ count. (%d)\n",
12941 drq->entry_count);
James Smart4f4c1862012-06-12 13:54:02 -040012942 if (drq->entry_count < 512) {
12943 status = -EINVAL;
12944 goto out;
12945 }
James Smart5a6f1332011-03-11 16:05:35 -050012946 /* otherwise default to smallest count (drop through) */
12947 case 512:
12948 bf_set(lpfc_rq_context_rqe_count,
12949 &rq_create->u.request.context,
12950 LPFC_RQ_RING_SIZE_512);
12951 break;
12952 case 1024:
12953 bf_set(lpfc_rq_context_rqe_count,
12954 &rq_create->u.request.context,
12955 LPFC_RQ_RING_SIZE_1024);
12956 break;
12957 case 2048:
12958 bf_set(lpfc_rq_context_rqe_count,
12959 &rq_create->u.request.context,
12960 LPFC_RQ_RING_SIZE_2048);
12961 break;
12962 case 4096:
12963 bf_set(lpfc_rq_context_rqe_count,
12964 &rq_create->u.request.context,
12965 LPFC_RQ_RING_SIZE_4096);
12966 break;
12967 }
12968 bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
12969 LPFC_DATA_BUF_SIZE);
James Smart4f774512009-05-22 14:52:35 -040012970 }
12971 bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
12972 cq->queue_id);
12973 bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
12974 drq->page_count);
James Smart4f774512009-05-22 14:52:35 -040012975 list_for_each_entry(dmabuf, &drq->page_list, list) {
12976 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
12977 putPaddrLow(dmabuf->phys);
12978 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
12979 putPaddrHigh(dmabuf->phys);
12980 }
12981 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
12982 /* The IOCTL status is embedded in the mailbox subheader. */
12983 shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
12984 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12985 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12986 if (shdr_status || shdr_add_status || rc) {
12987 status = -ENXIO;
12988 goto out;
12989 }
12990 drq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
12991 if (drq->queue_id == 0xFFFF) {
12992 status = -ENXIO;
12993 goto out;
12994 }
12995 drq->type = LPFC_DRQ;
James Smart2a622bf2011-02-16 12:40:06 -050012996 drq->assoc_qid = cq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040012997 drq->subtype = subtype;
12998 drq->host_index = 0;
12999 drq->hba_index = 0;
13000
13001 /* link the header and data RQs onto the parent cq child list */
13002 list_add_tail(&hrq->list, &cq->child_list);
13003 list_add_tail(&drq->list, &cq->child_list);
13004
13005out:
James Smart8fa38512009-07-19 10:01:03 -040013006 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040013007 return status;
13008}
13009
13010/**
13011 * lpfc_eq_destroy - Destroy an event Queue on the HBA
13012 * @eq: The queue structure associated with the queue to destroy.
13013 *
13014 * This function destroys a queue, as detailed in @eq by sending an mailbox
13015 * command, specific to the type of queue, to the HBA.
13016 *
13017 * The @eq struct is used to get the queue ID of the queue to destroy.
13018 *
13019 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040013020 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040013021 **/
13022uint32_t
13023lpfc_eq_destroy(struct lpfc_hba *phba, struct lpfc_queue *eq)
13024{
13025 LPFC_MBOXQ_t *mbox;
13026 int rc, length, status = 0;
13027 uint32_t shdr_status, shdr_add_status;
13028 union lpfc_sli4_cfg_shdr *shdr;
13029
James Smart2e90f4b2011-12-13 13:22:37 -050013030 /* sanity check on queue memory */
James Smart4f774512009-05-22 14:52:35 -040013031 if (!eq)
13032 return -ENODEV;
13033 mbox = mempool_alloc(eq->phba->mbox_mem_pool, GFP_KERNEL);
13034 if (!mbox)
13035 return -ENOMEM;
13036 length = (sizeof(struct lpfc_mbx_eq_destroy) -
13037 sizeof(struct lpfc_sli4_cfg_mhdr));
13038 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
13039 LPFC_MBOX_OPCODE_EQ_DESTROY,
13040 length, LPFC_SLI4_MBX_EMBED);
13041 bf_set(lpfc_mbx_eq_destroy_q_id, &mbox->u.mqe.un.eq_destroy.u.request,
13042 eq->queue_id);
13043 mbox->vport = eq->phba->pport;
13044 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
13045
13046 rc = lpfc_sli_issue_mbox(eq->phba, mbox, MBX_POLL);
13047 /* The IOCTL status is embedded in the mailbox subheader. */
13048 shdr = (union lpfc_sli4_cfg_shdr *)
13049 &mbox->u.mqe.un.eq_destroy.header.cfg_shdr;
13050 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13051 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13052 if (shdr_status || shdr_add_status || rc) {
13053 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13054 "2505 EQ_DESTROY mailbox failed with "
13055 "status x%x add_status x%x, mbx status x%x\n",
13056 shdr_status, shdr_add_status, rc);
13057 status = -ENXIO;
13058 }
13059
13060 /* Remove eq from any list */
13061 list_del_init(&eq->list);
James Smart8fa38512009-07-19 10:01:03 -040013062 mempool_free(mbox, eq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040013063 return status;
13064}
13065
13066/**
13067 * lpfc_cq_destroy - Destroy a Completion Queue on the HBA
13068 * @cq: The queue structure associated with the queue to destroy.
13069 *
13070 * This function destroys a queue, as detailed in @cq by sending an mailbox
13071 * command, specific to the type of queue, to the HBA.
13072 *
13073 * The @cq struct is used to get the queue ID of the queue to destroy.
13074 *
13075 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040013076 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040013077 **/
13078uint32_t
13079lpfc_cq_destroy(struct lpfc_hba *phba, struct lpfc_queue *cq)
13080{
13081 LPFC_MBOXQ_t *mbox;
13082 int rc, length, status = 0;
13083 uint32_t shdr_status, shdr_add_status;
13084 union lpfc_sli4_cfg_shdr *shdr;
13085
James Smart2e90f4b2011-12-13 13:22:37 -050013086 /* sanity check on queue memory */
James Smart4f774512009-05-22 14:52:35 -040013087 if (!cq)
13088 return -ENODEV;
13089 mbox = mempool_alloc(cq->phba->mbox_mem_pool, GFP_KERNEL);
13090 if (!mbox)
13091 return -ENOMEM;
13092 length = (sizeof(struct lpfc_mbx_cq_destroy) -
13093 sizeof(struct lpfc_sli4_cfg_mhdr));
13094 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
13095 LPFC_MBOX_OPCODE_CQ_DESTROY,
13096 length, LPFC_SLI4_MBX_EMBED);
13097 bf_set(lpfc_mbx_cq_destroy_q_id, &mbox->u.mqe.un.cq_destroy.u.request,
13098 cq->queue_id);
13099 mbox->vport = cq->phba->pport;
13100 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
13101 rc = lpfc_sli_issue_mbox(cq->phba, mbox, MBX_POLL);
13102 /* The IOCTL status is embedded in the mailbox subheader. */
13103 shdr = (union lpfc_sli4_cfg_shdr *)
13104 &mbox->u.mqe.un.wq_create.header.cfg_shdr;
13105 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13106 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13107 if (shdr_status || shdr_add_status || rc) {
13108 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13109 "2506 CQ_DESTROY mailbox failed with "
13110 "status x%x add_status x%x, mbx status x%x\n",
13111 shdr_status, shdr_add_status, rc);
13112 status = -ENXIO;
13113 }
13114 /* Remove cq from any list */
13115 list_del_init(&cq->list);
James Smart8fa38512009-07-19 10:01:03 -040013116 mempool_free(mbox, cq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040013117 return status;
13118}
13119
13120/**
James Smart04c68492009-05-22 14:52:52 -040013121 * lpfc_mq_destroy - Destroy a Mailbox Queue on the HBA
13122 * @qm: The queue structure associated with the queue to destroy.
13123 *
13124 * This function destroys a queue, as detailed in @mq by sending an mailbox
13125 * command, specific to the type of queue, to the HBA.
13126 *
13127 * The @mq struct is used to get the queue ID of the queue to destroy.
13128 *
13129 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040013130 * command fails this function will return -ENXIO.
James Smart04c68492009-05-22 14:52:52 -040013131 **/
13132uint32_t
13133lpfc_mq_destroy(struct lpfc_hba *phba, struct lpfc_queue *mq)
13134{
13135 LPFC_MBOXQ_t *mbox;
13136 int rc, length, status = 0;
13137 uint32_t shdr_status, shdr_add_status;
13138 union lpfc_sli4_cfg_shdr *shdr;
13139
James Smart2e90f4b2011-12-13 13:22:37 -050013140 /* sanity check on queue memory */
James Smart04c68492009-05-22 14:52:52 -040013141 if (!mq)
13142 return -ENODEV;
13143 mbox = mempool_alloc(mq->phba->mbox_mem_pool, GFP_KERNEL);
13144 if (!mbox)
13145 return -ENOMEM;
13146 length = (sizeof(struct lpfc_mbx_mq_destroy) -
13147 sizeof(struct lpfc_sli4_cfg_mhdr));
13148 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
13149 LPFC_MBOX_OPCODE_MQ_DESTROY,
13150 length, LPFC_SLI4_MBX_EMBED);
13151 bf_set(lpfc_mbx_mq_destroy_q_id, &mbox->u.mqe.un.mq_destroy.u.request,
13152 mq->queue_id);
13153 mbox->vport = mq->phba->pport;
13154 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
13155 rc = lpfc_sli_issue_mbox(mq->phba, mbox, MBX_POLL);
13156 /* The IOCTL status is embedded in the mailbox subheader. */
13157 shdr = (union lpfc_sli4_cfg_shdr *)
13158 &mbox->u.mqe.un.mq_destroy.header.cfg_shdr;
13159 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13160 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13161 if (shdr_status || shdr_add_status || rc) {
13162 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13163 "2507 MQ_DESTROY mailbox failed with "
13164 "status x%x add_status x%x, mbx status x%x\n",
13165 shdr_status, shdr_add_status, rc);
13166 status = -ENXIO;
13167 }
13168 /* Remove mq from any list */
13169 list_del_init(&mq->list);
James Smart8fa38512009-07-19 10:01:03 -040013170 mempool_free(mbox, mq->phba->mbox_mem_pool);
James Smart04c68492009-05-22 14:52:52 -040013171 return status;
13172}
13173
13174/**
James Smart4f774512009-05-22 14:52:35 -040013175 * lpfc_wq_destroy - Destroy a Work Queue on the HBA
13176 * @wq: The queue structure associated with the queue to destroy.
13177 *
13178 * This function destroys a queue, as detailed in @wq by sending an mailbox
13179 * command, specific to the type of queue, to the HBA.
13180 *
13181 * The @wq struct is used to get the queue ID of the queue to destroy.
13182 *
13183 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040013184 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040013185 **/
13186uint32_t
13187lpfc_wq_destroy(struct lpfc_hba *phba, struct lpfc_queue *wq)
13188{
13189 LPFC_MBOXQ_t *mbox;
13190 int rc, length, status = 0;
13191 uint32_t shdr_status, shdr_add_status;
13192 union lpfc_sli4_cfg_shdr *shdr;
13193
James Smart2e90f4b2011-12-13 13:22:37 -050013194 /* sanity check on queue memory */
James Smart4f774512009-05-22 14:52:35 -040013195 if (!wq)
13196 return -ENODEV;
13197 mbox = mempool_alloc(wq->phba->mbox_mem_pool, GFP_KERNEL);
13198 if (!mbox)
13199 return -ENOMEM;
13200 length = (sizeof(struct lpfc_mbx_wq_destroy) -
13201 sizeof(struct lpfc_sli4_cfg_mhdr));
13202 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
13203 LPFC_MBOX_OPCODE_FCOE_WQ_DESTROY,
13204 length, LPFC_SLI4_MBX_EMBED);
13205 bf_set(lpfc_mbx_wq_destroy_q_id, &mbox->u.mqe.un.wq_destroy.u.request,
13206 wq->queue_id);
13207 mbox->vport = wq->phba->pport;
13208 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
13209 rc = lpfc_sli_issue_mbox(wq->phba, mbox, MBX_POLL);
13210 shdr = (union lpfc_sli4_cfg_shdr *)
13211 &mbox->u.mqe.un.wq_destroy.header.cfg_shdr;
13212 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13213 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13214 if (shdr_status || shdr_add_status || rc) {
13215 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13216 "2508 WQ_DESTROY mailbox failed with "
13217 "status x%x add_status x%x, mbx status x%x\n",
13218 shdr_status, shdr_add_status, rc);
13219 status = -ENXIO;
13220 }
13221 /* Remove wq from any list */
13222 list_del_init(&wq->list);
James Smart8fa38512009-07-19 10:01:03 -040013223 mempool_free(mbox, wq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040013224 return status;
13225}
13226
13227/**
13228 * lpfc_rq_destroy - Destroy a Receive Queue on the HBA
13229 * @rq: The queue structure associated with the queue to destroy.
13230 *
13231 * This function destroys a queue, as detailed in @rq by sending an mailbox
13232 * command, specific to the type of queue, to the HBA.
13233 *
13234 * The @rq struct is used to get the queue ID of the queue to destroy.
13235 *
13236 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040013237 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040013238 **/
13239uint32_t
13240lpfc_rq_destroy(struct lpfc_hba *phba, struct lpfc_queue *hrq,
13241 struct lpfc_queue *drq)
13242{
13243 LPFC_MBOXQ_t *mbox;
13244 int rc, length, status = 0;
13245 uint32_t shdr_status, shdr_add_status;
13246 union lpfc_sli4_cfg_shdr *shdr;
13247
James Smart2e90f4b2011-12-13 13:22:37 -050013248 /* sanity check on queue memory */
James Smart4f774512009-05-22 14:52:35 -040013249 if (!hrq || !drq)
13250 return -ENODEV;
13251 mbox = mempool_alloc(hrq->phba->mbox_mem_pool, GFP_KERNEL);
13252 if (!mbox)
13253 return -ENOMEM;
13254 length = (sizeof(struct lpfc_mbx_rq_destroy) -
James Smartfedd3b72011-02-16 12:39:24 -050013255 sizeof(struct lpfc_sli4_cfg_mhdr));
James Smart4f774512009-05-22 14:52:35 -040013256 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
13257 LPFC_MBOX_OPCODE_FCOE_RQ_DESTROY,
13258 length, LPFC_SLI4_MBX_EMBED);
13259 bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
13260 hrq->queue_id);
13261 mbox->vport = hrq->phba->pport;
13262 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
13263 rc = lpfc_sli_issue_mbox(hrq->phba, mbox, MBX_POLL);
13264 /* The IOCTL status is embedded in the mailbox subheader. */
13265 shdr = (union lpfc_sli4_cfg_shdr *)
13266 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
13267 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13268 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13269 if (shdr_status || shdr_add_status || rc) {
13270 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13271 "2509 RQ_DESTROY mailbox failed with "
13272 "status x%x add_status x%x, mbx status x%x\n",
13273 shdr_status, shdr_add_status, rc);
13274 if (rc != MBX_TIMEOUT)
13275 mempool_free(mbox, hrq->phba->mbox_mem_pool);
13276 return -ENXIO;
13277 }
13278 bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
13279 drq->queue_id);
13280 rc = lpfc_sli_issue_mbox(drq->phba, mbox, MBX_POLL);
13281 shdr = (union lpfc_sli4_cfg_shdr *)
13282 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
13283 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13284 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13285 if (shdr_status || shdr_add_status || rc) {
13286 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13287 "2510 RQ_DESTROY mailbox failed with "
13288 "status x%x add_status x%x, mbx status x%x\n",
13289 shdr_status, shdr_add_status, rc);
13290 status = -ENXIO;
13291 }
13292 list_del_init(&hrq->list);
13293 list_del_init(&drq->list);
James Smart8fa38512009-07-19 10:01:03 -040013294 mempool_free(mbox, hrq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040013295 return status;
13296}
13297
13298/**
13299 * lpfc_sli4_post_sgl - Post scatter gather list for an XRI to HBA
13300 * @phba: The virtual port for which this call being executed.
13301 * @pdma_phys_addr0: Physical address of the 1st SGL page.
13302 * @pdma_phys_addr1: Physical address of the 2nd SGL page.
13303 * @xritag: the xritag that ties this io to the SGL pages.
13304 *
13305 * This routine will post the sgl pages for the IO that has the xritag
13306 * that is in the iocbq structure. The xritag is assigned during iocbq
13307 * creation and persists for as long as the driver is loaded.
13308 * if the caller has fewer than 256 scatter gather segments to map then
13309 * pdma_phys_addr1 should be 0.
13310 * If the caller needs to map more than 256 scatter gather segment then
13311 * pdma_phys_addr1 should be a valid physical address.
13312 * physical address for SGLs must be 64 byte aligned.
13313 * If you are going to map 2 SGL's then the first one must have 256 entries
13314 * the second sgl can have between 1 and 256 entries.
13315 *
13316 * Return codes:
13317 * 0 - Success
13318 * -ENXIO, -ENOMEM - Failure
13319 **/
13320int
13321lpfc_sli4_post_sgl(struct lpfc_hba *phba,
13322 dma_addr_t pdma_phys_addr0,
13323 dma_addr_t pdma_phys_addr1,
13324 uint16_t xritag)
13325{
13326 struct lpfc_mbx_post_sgl_pages *post_sgl_pages;
13327 LPFC_MBOXQ_t *mbox;
13328 int rc;
13329 uint32_t shdr_status, shdr_add_status;
James Smart6d368e52011-05-24 11:44:12 -040013330 uint32_t mbox_tmo;
James Smart4f774512009-05-22 14:52:35 -040013331 union lpfc_sli4_cfg_shdr *shdr;
13332
13333 if (xritag == NO_XRI) {
13334 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13335 "0364 Invalid param:\n");
13336 return -EINVAL;
13337 }
13338
13339 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
13340 if (!mbox)
13341 return -ENOMEM;
13342
13343 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
13344 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES,
13345 sizeof(struct lpfc_mbx_post_sgl_pages) -
James Smartfedd3b72011-02-16 12:39:24 -050013346 sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
James Smart4f774512009-05-22 14:52:35 -040013347
13348 post_sgl_pages = (struct lpfc_mbx_post_sgl_pages *)
13349 &mbox->u.mqe.un.post_sgl_pages;
13350 bf_set(lpfc_post_sgl_pages_xri, post_sgl_pages, xritag);
13351 bf_set(lpfc_post_sgl_pages_xricnt, post_sgl_pages, 1);
13352
13353 post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_lo =
13354 cpu_to_le32(putPaddrLow(pdma_phys_addr0));
13355 post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_hi =
13356 cpu_to_le32(putPaddrHigh(pdma_phys_addr0));
13357
13358 post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_lo =
13359 cpu_to_le32(putPaddrLow(pdma_phys_addr1));
13360 post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_hi =
13361 cpu_to_le32(putPaddrHigh(pdma_phys_addr1));
13362 if (!phba->sli4_hba.intr_enable)
13363 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
James Smart6d368e52011-05-24 11:44:12 -040013364 else {
James Smarta183a152011-10-10 21:32:43 -040013365 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart6d368e52011-05-24 11:44:12 -040013366 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
13367 }
James Smart4f774512009-05-22 14:52:35 -040013368 /* The IOCTL status is embedded in the mailbox subheader. */
13369 shdr = (union lpfc_sli4_cfg_shdr *) &post_sgl_pages->header.cfg_shdr;
13370 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13371 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13372 if (rc != MBX_TIMEOUT)
13373 mempool_free(mbox, phba->mbox_mem_pool);
13374 if (shdr_status || shdr_add_status || rc) {
13375 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13376 "2511 POST_SGL mailbox failed with "
13377 "status x%x add_status x%x, mbx status x%x\n",
13378 shdr_status, shdr_add_status, rc);
13379 rc = -ENXIO;
13380 }
13381 return 0;
13382}
James Smart4f774512009-05-22 14:52:35 -040013383
13384/**
James Smart88a2cfb2011-07-22 18:36:33 -040013385 * lpfc_sli4_alloc_xri - Get an available rpi in the device's range
James Smart6d368e52011-05-24 11:44:12 -040013386 * @phba: pointer to lpfc hba data structure.
13387 *
13388 * This routine is invoked to post rpi header templates to the
James Smart88a2cfb2011-07-22 18:36:33 -040013389 * HBA consistent with the SLI-4 interface spec. This routine
13390 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
13391 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
James Smart6d368e52011-05-24 11:44:12 -040013392 *
James Smart88a2cfb2011-07-22 18:36:33 -040013393 * Returns
13394 * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
13395 * LPFC_RPI_ALLOC_ERROR if no rpis are available.
13396 **/
James Smart6d368e52011-05-24 11:44:12 -040013397uint16_t
13398lpfc_sli4_alloc_xri(struct lpfc_hba *phba)
13399{
13400 unsigned long xri;
13401
13402 /*
13403 * Fetch the next logical xri. Because this index is logical,
13404 * the driver starts at 0 each time.
13405 */
13406 spin_lock_irq(&phba->hbalock);
13407 xri = find_next_zero_bit(phba->sli4_hba.xri_bmask,
13408 phba->sli4_hba.max_cfg_param.max_xri, 0);
13409 if (xri >= phba->sli4_hba.max_cfg_param.max_xri) {
13410 spin_unlock_irq(&phba->hbalock);
13411 return NO_XRI;
13412 } else {
13413 set_bit(xri, phba->sli4_hba.xri_bmask);
13414 phba->sli4_hba.max_cfg_param.xri_used++;
James Smart6d368e52011-05-24 11:44:12 -040013415 }
James Smart6d368e52011-05-24 11:44:12 -040013416 spin_unlock_irq(&phba->hbalock);
13417 return xri;
13418}
13419
13420/**
13421 * lpfc_sli4_free_xri - Release an xri for reuse.
13422 * @phba: pointer to lpfc hba data structure.
13423 *
13424 * This routine is invoked to release an xri to the pool of
13425 * available rpis maintained by the driver.
13426 **/
13427void
13428__lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
13429{
13430 if (test_and_clear_bit(xri, phba->sli4_hba.xri_bmask)) {
James Smart6d368e52011-05-24 11:44:12 -040013431 phba->sli4_hba.max_cfg_param.xri_used--;
13432 }
13433}
13434
13435/**
13436 * lpfc_sli4_free_xri - Release an xri for reuse.
13437 * @phba: pointer to lpfc hba data structure.
13438 *
13439 * This routine is invoked to release an xri to the pool of
13440 * available rpis maintained by the driver.
13441 **/
13442void
13443lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
13444{
13445 spin_lock_irq(&phba->hbalock);
13446 __lpfc_sli4_free_xri(phba, xri);
13447 spin_unlock_irq(&phba->hbalock);
13448}
13449
13450/**
James Smart4f774512009-05-22 14:52:35 -040013451 * lpfc_sli4_next_xritag - Get an xritag for the io
13452 * @phba: Pointer to HBA context object.
13453 *
13454 * This function gets an xritag for the iocb. If there is no unused xritag
13455 * it will return 0xffff.
13456 * The function returns the allocated xritag if successful, else returns zero.
13457 * Zero is not a valid xritag.
13458 * The caller is not required to hold any lock.
13459 **/
13460uint16_t
13461lpfc_sli4_next_xritag(struct lpfc_hba *phba)
13462{
James Smart6d368e52011-05-24 11:44:12 -040013463 uint16_t xri_index;
James Smart4f774512009-05-22 14:52:35 -040013464
James Smart6d368e52011-05-24 11:44:12 -040013465 xri_index = lpfc_sli4_alloc_xri(phba);
James Smart81378052012-05-09 21:17:37 -040013466 if (xri_index == NO_XRI)
13467 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13468 "2004 Failed to allocate XRI.last XRITAG is %d"
13469 " Max XRI is %d, Used XRI is %d\n",
13470 xri_index,
13471 phba->sli4_hba.max_cfg_param.max_xri,
13472 phba->sli4_hba.max_cfg_param.xri_used);
13473 return xri_index;
James Smart4f774512009-05-22 14:52:35 -040013474}
13475
13476/**
James Smart6d368e52011-05-24 11:44:12 -040013477 * lpfc_sli4_post_els_sgl_list - post a block of ELS sgls to the port.
James Smart4f774512009-05-22 14:52:35 -040013478 * @phba: pointer to lpfc hba data structure.
James Smart8a9d2e82012-05-09 21:16:12 -040013479 * @post_sgl_list: pointer to els sgl entry list.
13480 * @count: number of els sgl entries on the list.
James Smart4f774512009-05-22 14:52:35 -040013481 *
13482 * This routine is invoked to post a block of driver's sgl pages to the
13483 * HBA using non-embedded mailbox command. No Lock is held. This routine
13484 * is only called when the driver is loading and after all IO has been
13485 * stopped.
13486 **/
James Smart8a9d2e82012-05-09 21:16:12 -040013487static int
13488lpfc_sli4_post_els_sgl_list(struct lpfc_hba *phba,
13489 struct list_head *post_sgl_list,
13490 int post_cnt)
James Smart4f774512009-05-22 14:52:35 -040013491{
James Smart8a9d2e82012-05-09 21:16:12 -040013492 struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
James Smart4f774512009-05-22 14:52:35 -040013493 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
13494 struct sgl_page_pairs *sgl_pg_pairs;
13495 void *viraddr;
13496 LPFC_MBOXQ_t *mbox;
13497 uint32_t reqlen, alloclen, pg_pairs;
13498 uint32_t mbox_tmo;
James Smart8a9d2e82012-05-09 21:16:12 -040013499 uint16_t xritag_start = 0;
13500 int rc = 0;
James Smart4f774512009-05-22 14:52:35 -040013501 uint32_t shdr_status, shdr_add_status;
13502 union lpfc_sli4_cfg_shdr *shdr;
13503
James Smart8a9d2e82012-05-09 21:16:12 -040013504 reqlen = phba->sli4_hba.els_xri_cnt * sizeof(struct sgl_page_pairs) +
James Smart4f774512009-05-22 14:52:35 -040013505 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
James Smart49198b32010-04-06 15:04:33 -040013506 if (reqlen > SLI4_PAGE_SIZE) {
James Smart4f774512009-05-22 14:52:35 -040013507 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
13508 "2559 Block sgl registration required DMA "
13509 "size (%d) great than a page\n", reqlen);
13510 return -ENOMEM;
13511 }
13512 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
James Smart6d368e52011-05-24 11:44:12 -040013513 if (!mbox)
James Smart4f774512009-05-22 14:52:35 -040013514 return -ENOMEM;
James Smart4f774512009-05-22 14:52:35 -040013515
13516 /* Allocate DMA memory and set up the non-embedded mailbox command */
13517 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
13518 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
13519 LPFC_SLI4_MBX_NEMBED);
13520
13521 if (alloclen < reqlen) {
13522 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13523 "0285 Allocated DMA memory size (%d) is "
13524 "less than the requested DMA memory "
13525 "size (%d)\n", alloclen, reqlen);
13526 lpfc_sli4_mbox_cmd_free(phba, mbox);
13527 return -ENOMEM;
13528 }
James Smart4f774512009-05-22 14:52:35 -040013529 /* Set up the SGL pages in the non-embedded DMA pages */
James Smart6d368e52011-05-24 11:44:12 -040013530 viraddr = mbox->sge_array->addr[0];
James Smart4f774512009-05-22 14:52:35 -040013531 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
13532 sgl_pg_pairs = &sgl->sgl_pg_pairs;
13533
James Smart8a9d2e82012-05-09 21:16:12 -040013534 pg_pairs = 0;
13535 list_for_each_entry_safe(sglq_entry, sglq_next, post_sgl_list, list) {
James Smart4f774512009-05-22 14:52:35 -040013536 /* Set up the sge entry */
13537 sgl_pg_pairs->sgl_pg0_addr_lo =
13538 cpu_to_le32(putPaddrLow(sglq_entry->phys));
13539 sgl_pg_pairs->sgl_pg0_addr_hi =
13540 cpu_to_le32(putPaddrHigh(sglq_entry->phys));
13541 sgl_pg_pairs->sgl_pg1_addr_lo =
13542 cpu_to_le32(putPaddrLow(0));
13543 sgl_pg_pairs->sgl_pg1_addr_hi =
13544 cpu_to_le32(putPaddrHigh(0));
James Smart6d368e52011-05-24 11:44:12 -040013545
James Smart4f774512009-05-22 14:52:35 -040013546 /* Keep the first xritag on the list */
13547 if (pg_pairs == 0)
13548 xritag_start = sglq_entry->sli4_xritag;
13549 sgl_pg_pairs++;
James Smart8a9d2e82012-05-09 21:16:12 -040013550 pg_pairs++;
James Smart4f774512009-05-22 14:52:35 -040013551 }
James Smart6d368e52011-05-24 11:44:12 -040013552
13553 /* Complete initialization and perform endian conversion. */
James Smart4f774512009-05-22 14:52:35 -040013554 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
James Smart8a9d2e82012-05-09 21:16:12 -040013555 bf_set(lpfc_post_sgl_pages_xricnt, sgl, phba->sli4_hba.els_xri_cnt);
James Smart4f774512009-05-22 14:52:35 -040013556 sgl->word0 = cpu_to_le32(sgl->word0);
James Smart4f774512009-05-22 14:52:35 -040013557 if (!phba->sli4_hba.intr_enable)
13558 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
13559 else {
James Smarta183a152011-10-10 21:32:43 -040013560 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart4f774512009-05-22 14:52:35 -040013561 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
13562 }
13563 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
13564 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13565 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13566 if (rc != MBX_TIMEOUT)
13567 lpfc_sli4_mbox_cmd_free(phba, mbox);
13568 if (shdr_status || shdr_add_status || rc) {
13569 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13570 "2513 POST_SGL_BLOCK mailbox command failed "
13571 "status x%x add_status x%x mbx status x%x\n",
13572 shdr_status, shdr_add_status, rc);
13573 rc = -ENXIO;
13574 }
13575 return rc;
13576}
13577
13578/**
13579 * lpfc_sli4_post_scsi_sgl_block - post a block of scsi sgl list to firmware
13580 * @phba: pointer to lpfc hba data structure.
13581 * @sblist: pointer to scsi buffer list.
13582 * @count: number of scsi buffers on the list.
13583 *
13584 * This routine is invoked to post a block of @count scsi sgl pages from a
13585 * SCSI buffer list @sblist to the HBA using non-embedded mailbox command.
13586 * No Lock is held.
13587 *
13588 **/
13589int
James Smart8a9d2e82012-05-09 21:16:12 -040013590lpfc_sli4_post_scsi_sgl_block(struct lpfc_hba *phba,
13591 struct list_head *sblist,
13592 int count)
James Smart4f774512009-05-22 14:52:35 -040013593{
13594 struct lpfc_scsi_buf *psb;
13595 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
13596 struct sgl_page_pairs *sgl_pg_pairs;
13597 void *viraddr;
13598 LPFC_MBOXQ_t *mbox;
13599 uint32_t reqlen, alloclen, pg_pairs;
13600 uint32_t mbox_tmo;
13601 uint16_t xritag_start = 0;
13602 int rc = 0;
13603 uint32_t shdr_status, shdr_add_status;
13604 dma_addr_t pdma_phys_bpl1;
13605 union lpfc_sli4_cfg_shdr *shdr;
13606
13607 /* Calculate the requested length of the dma memory */
James Smart8a9d2e82012-05-09 21:16:12 -040013608 reqlen = count * sizeof(struct sgl_page_pairs) +
James Smart4f774512009-05-22 14:52:35 -040013609 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
James Smart49198b32010-04-06 15:04:33 -040013610 if (reqlen > SLI4_PAGE_SIZE) {
James Smart4f774512009-05-22 14:52:35 -040013611 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
13612 "0217 Block sgl registration required DMA "
13613 "size (%d) great than a page\n", reqlen);
13614 return -ENOMEM;
13615 }
13616 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
13617 if (!mbox) {
13618 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13619 "0283 Failed to allocate mbox cmd memory\n");
13620 return -ENOMEM;
13621 }
13622
13623 /* Allocate DMA memory and set up the non-embedded mailbox command */
13624 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
13625 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
13626 LPFC_SLI4_MBX_NEMBED);
13627
13628 if (alloclen < reqlen) {
13629 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13630 "2561 Allocated DMA memory size (%d) is "
13631 "less than the requested DMA memory "
13632 "size (%d)\n", alloclen, reqlen);
13633 lpfc_sli4_mbox_cmd_free(phba, mbox);
13634 return -ENOMEM;
13635 }
James Smart6d368e52011-05-24 11:44:12 -040013636
James Smart4f774512009-05-22 14:52:35 -040013637 /* Get the first SGE entry from the non-embedded DMA memory */
James Smart4f774512009-05-22 14:52:35 -040013638 viraddr = mbox->sge_array->addr[0];
13639
13640 /* Set up the SGL pages in the non-embedded DMA pages */
13641 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
13642 sgl_pg_pairs = &sgl->sgl_pg_pairs;
13643
13644 pg_pairs = 0;
13645 list_for_each_entry(psb, sblist, list) {
13646 /* Set up the sge entry */
13647 sgl_pg_pairs->sgl_pg0_addr_lo =
13648 cpu_to_le32(putPaddrLow(psb->dma_phys_bpl));
13649 sgl_pg_pairs->sgl_pg0_addr_hi =
13650 cpu_to_le32(putPaddrHigh(psb->dma_phys_bpl));
13651 if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
13652 pdma_phys_bpl1 = psb->dma_phys_bpl + SGL_PAGE_SIZE;
13653 else
13654 pdma_phys_bpl1 = 0;
13655 sgl_pg_pairs->sgl_pg1_addr_lo =
13656 cpu_to_le32(putPaddrLow(pdma_phys_bpl1));
13657 sgl_pg_pairs->sgl_pg1_addr_hi =
13658 cpu_to_le32(putPaddrHigh(pdma_phys_bpl1));
13659 /* Keep the first xritag on the list */
13660 if (pg_pairs == 0)
13661 xritag_start = psb->cur_iocbq.sli4_xritag;
13662 sgl_pg_pairs++;
13663 pg_pairs++;
13664 }
13665 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
13666 bf_set(lpfc_post_sgl_pages_xricnt, sgl, pg_pairs);
13667 /* Perform endian conversion if necessary */
13668 sgl->word0 = cpu_to_le32(sgl->word0);
13669
13670 if (!phba->sli4_hba.intr_enable)
13671 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
13672 else {
James Smarta183a152011-10-10 21:32:43 -040013673 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart4f774512009-05-22 14:52:35 -040013674 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
13675 }
13676 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
13677 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13678 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13679 if (rc != MBX_TIMEOUT)
13680 lpfc_sli4_mbox_cmd_free(phba, mbox);
13681 if (shdr_status || shdr_add_status || rc) {
13682 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13683 "2564 POST_SGL_BLOCK mailbox command failed "
13684 "status x%x add_status x%x mbx status x%x\n",
13685 shdr_status, shdr_add_status, rc);
13686 rc = -ENXIO;
13687 }
13688 return rc;
13689}
13690
13691/**
13692 * lpfc_fc_frame_check - Check that this frame is a valid frame to handle
13693 * @phba: pointer to lpfc_hba struct that the frame was received on
13694 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
13695 *
13696 * This function checks the fields in the @fc_hdr to see if the FC frame is a
13697 * valid type of frame that the LPFC driver will handle. This function will
13698 * return a zero if the frame is a valid frame or a non zero value when the
13699 * frame does not pass the check.
13700 **/
13701static int
13702lpfc_fc_frame_check(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr)
13703{
Tomas Henzl474ffb72010-12-22 16:52:40 +010013704 /* make rctl_names static to save stack space */
13705 static char *rctl_names[] = FC_RCTL_NAMES_INIT;
James Smart4f774512009-05-22 14:52:35 -040013706 char *type_names[] = FC_TYPE_NAMES_INIT;
13707 struct fc_vft_header *fc_vft_hdr;
James Smart546fc852011-03-11 16:06:29 -050013708 uint32_t *header = (uint32_t *) fc_hdr;
James Smart4f774512009-05-22 14:52:35 -040013709
13710 switch (fc_hdr->fh_r_ctl) {
13711 case FC_RCTL_DD_UNCAT: /* uncategorized information */
13712 case FC_RCTL_DD_SOL_DATA: /* solicited data */
13713 case FC_RCTL_DD_UNSOL_CTL: /* unsolicited control */
13714 case FC_RCTL_DD_SOL_CTL: /* solicited control or reply */
13715 case FC_RCTL_DD_UNSOL_DATA: /* unsolicited data */
13716 case FC_RCTL_DD_DATA_DESC: /* data descriptor */
13717 case FC_RCTL_DD_UNSOL_CMD: /* unsolicited command */
13718 case FC_RCTL_DD_CMD_STATUS: /* command status */
13719 case FC_RCTL_ELS_REQ: /* extended link services request */
13720 case FC_RCTL_ELS_REP: /* extended link services reply */
13721 case FC_RCTL_ELS4_REQ: /* FC-4 ELS request */
13722 case FC_RCTL_ELS4_REP: /* FC-4 ELS reply */
13723 case FC_RCTL_BA_NOP: /* basic link service NOP */
13724 case FC_RCTL_BA_ABTS: /* basic link service abort */
13725 case FC_RCTL_BA_RMC: /* remove connection */
13726 case FC_RCTL_BA_ACC: /* basic accept */
13727 case FC_RCTL_BA_RJT: /* basic reject */
13728 case FC_RCTL_BA_PRMT:
13729 case FC_RCTL_ACK_1: /* acknowledge_1 */
13730 case FC_RCTL_ACK_0: /* acknowledge_0 */
13731 case FC_RCTL_P_RJT: /* port reject */
13732 case FC_RCTL_F_RJT: /* fabric reject */
13733 case FC_RCTL_P_BSY: /* port busy */
13734 case FC_RCTL_F_BSY: /* fabric busy to data frame */
13735 case FC_RCTL_F_BSYL: /* fabric busy to link control frame */
13736 case FC_RCTL_LCR: /* link credit reset */
13737 case FC_RCTL_END: /* end */
13738 break;
13739 case FC_RCTL_VFTH: /* Virtual Fabric tagging Header */
13740 fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
13741 fc_hdr = &((struct fc_frame_header *)fc_vft_hdr)[1];
13742 return lpfc_fc_frame_check(phba, fc_hdr);
13743 default:
13744 goto drop;
13745 }
13746 switch (fc_hdr->fh_type) {
13747 case FC_TYPE_BLS:
13748 case FC_TYPE_ELS:
13749 case FC_TYPE_FCP:
13750 case FC_TYPE_CT:
13751 break;
13752 case FC_TYPE_IP:
13753 case FC_TYPE_ILS:
13754 default:
13755 goto drop;
13756 }
James Smart546fc852011-03-11 16:06:29 -050013757
James Smart4f774512009-05-22 14:52:35 -040013758 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
James Smart546fc852011-03-11 16:06:29 -050013759 "2538 Received frame rctl:%s type:%s "
13760 "Frame Data:%08x %08x %08x %08x %08x %08x\n",
James Smart4f774512009-05-22 14:52:35 -040013761 rctl_names[fc_hdr->fh_r_ctl],
James Smart546fc852011-03-11 16:06:29 -050013762 type_names[fc_hdr->fh_type],
13763 be32_to_cpu(header[0]), be32_to_cpu(header[1]),
13764 be32_to_cpu(header[2]), be32_to_cpu(header[3]),
13765 be32_to_cpu(header[4]), be32_to_cpu(header[5]));
James Smart4f774512009-05-22 14:52:35 -040013766 return 0;
13767drop:
13768 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
13769 "2539 Dropped frame rctl:%s type:%s\n",
13770 rctl_names[fc_hdr->fh_r_ctl],
13771 type_names[fc_hdr->fh_type]);
13772 return 1;
13773}
13774
13775/**
13776 * lpfc_fc_hdr_get_vfi - Get the VFI from an FC frame
13777 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
13778 *
13779 * This function processes the FC header to retrieve the VFI from the VF
13780 * header, if one exists. This function will return the VFI if one exists
13781 * or 0 if no VSAN Header exists.
13782 **/
13783static uint32_t
13784lpfc_fc_hdr_get_vfi(struct fc_frame_header *fc_hdr)
13785{
13786 struct fc_vft_header *fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
13787
13788 if (fc_hdr->fh_r_ctl != FC_RCTL_VFTH)
13789 return 0;
13790 return bf_get(fc_vft_hdr_vf_id, fc_vft_hdr);
13791}
13792
13793/**
13794 * lpfc_fc_frame_to_vport - Finds the vport that a frame is destined to
13795 * @phba: Pointer to the HBA structure to search for the vport on
13796 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
13797 * @fcfi: The FC Fabric ID that the frame came from
13798 *
13799 * This function searches the @phba for a vport that matches the content of the
13800 * @fc_hdr passed in and the @fcfi. This function uses the @fc_hdr to fetch the
13801 * VFI, if the Virtual Fabric Tagging Header exists, and the DID. This function
13802 * returns the matching vport pointer or NULL if unable to match frame to a
13803 * vport.
13804 **/
13805static struct lpfc_vport *
13806lpfc_fc_frame_to_vport(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr,
13807 uint16_t fcfi)
13808{
13809 struct lpfc_vport **vports;
13810 struct lpfc_vport *vport = NULL;
13811 int i;
13812 uint32_t did = (fc_hdr->fh_d_id[0] << 16 |
13813 fc_hdr->fh_d_id[1] << 8 |
13814 fc_hdr->fh_d_id[2]);
James Smart939723a2012-05-09 21:19:03 -040013815
James Smartbf086112011-08-21 21:48:13 -040013816 if (did == Fabric_DID)
13817 return phba->pport;
James Smart939723a2012-05-09 21:19:03 -040013818 if ((phba->pport->fc_flag & FC_PT2PT) &&
13819 !(phba->link_state == LPFC_HBA_READY))
13820 return phba->pport;
13821
James Smart4f774512009-05-22 14:52:35 -040013822 vports = lpfc_create_vport_work_array(phba);
13823 if (vports != NULL)
13824 for (i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
13825 if (phba->fcf.fcfi == fcfi &&
13826 vports[i]->vfi == lpfc_fc_hdr_get_vfi(fc_hdr) &&
13827 vports[i]->fc_myDID == did) {
13828 vport = vports[i];
13829 break;
13830 }
13831 }
13832 lpfc_destroy_vport_work_array(phba, vports);
13833 return vport;
13834}
13835
13836/**
James Smart45ed1192009-10-02 15:17:02 -040013837 * lpfc_update_rcv_time_stamp - Update vport's rcv seq time stamp
13838 * @vport: The vport to work on.
13839 *
13840 * This function updates the receive sequence time stamp for this vport. The
13841 * receive sequence time stamp indicates the time that the last frame of the
13842 * the sequence that has been idle for the longest amount of time was received.
13843 * the driver uses this time stamp to indicate if any received sequences have
13844 * timed out.
13845 **/
13846void
13847lpfc_update_rcv_time_stamp(struct lpfc_vport *vport)
13848{
13849 struct lpfc_dmabuf *h_buf;
13850 struct hbq_dmabuf *dmabuf = NULL;
13851
13852 /* get the oldest sequence on the rcv list */
13853 h_buf = list_get_first(&vport->rcv_buffer_list,
13854 struct lpfc_dmabuf, list);
13855 if (!h_buf)
13856 return;
13857 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
13858 vport->rcv_buffer_time_stamp = dmabuf->time_stamp;
13859}
13860
13861/**
13862 * lpfc_cleanup_rcv_buffers - Cleans up all outstanding receive sequences.
13863 * @vport: The vport that the received sequences were sent to.
13864 *
13865 * This function cleans up all outstanding received sequences. This is called
13866 * by the driver when a link event or user action invalidates all the received
13867 * sequences.
13868 **/
13869void
13870lpfc_cleanup_rcv_buffers(struct lpfc_vport *vport)
13871{
13872 struct lpfc_dmabuf *h_buf, *hnext;
13873 struct lpfc_dmabuf *d_buf, *dnext;
13874 struct hbq_dmabuf *dmabuf = NULL;
13875
13876 /* start with the oldest sequence on the rcv list */
13877 list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
13878 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
13879 list_del_init(&dmabuf->hbuf.list);
13880 list_for_each_entry_safe(d_buf, dnext,
13881 &dmabuf->dbuf.list, list) {
13882 list_del_init(&d_buf->list);
13883 lpfc_in_buf_free(vport->phba, d_buf);
13884 }
13885 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
13886 }
13887}
13888
13889/**
13890 * lpfc_rcv_seq_check_edtov - Cleans up timed out receive sequences.
13891 * @vport: The vport that the received sequences were sent to.
13892 *
13893 * This function determines whether any received sequences have timed out by
13894 * first checking the vport's rcv_buffer_time_stamp. If this time_stamp
13895 * indicates that there is at least one timed out sequence this routine will
13896 * go through the received sequences one at a time from most inactive to most
13897 * active to determine which ones need to be cleaned up. Once it has determined
13898 * that a sequence needs to be cleaned up it will simply free up the resources
13899 * without sending an abort.
13900 **/
13901void
13902lpfc_rcv_seq_check_edtov(struct lpfc_vport *vport)
13903{
13904 struct lpfc_dmabuf *h_buf, *hnext;
13905 struct lpfc_dmabuf *d_buf, *dnext;
13906 struct hbq_dmabuf *dmabuf = NULL;
13907 unsigned long timeout;
13908 int abort_count = 0;
13909
13910 timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
13911 vport->rcv_buffer_time_stamp);
13912 if (list_empty(&vport->rcv_buffer_list) ||
13913 time_before(jiffies, timeout))
13914 return;
13915 /* start with the oldest sequence on the rcv list */
13916 list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
13917 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
13918 timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
13919 dmabuf->time_stamp);
13920 if (time_before(jiffies, timeout))
13921 break;
13922 abort_count++;
13923 list_del_init(&dmabuf->hbuf.list);
13924 list_for_each_entry_safe(d_buf, dnext,
13925 &dmabuf->dbuf.list, list) {
13926 list_del_init(&d_buf->list);
13927 lpfc_in_buf_free(vport->phba, d_buf);
13928 }
13929 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
13930 }
13931 if (abort_count)
13932 lpfc_update_rcv_time_stamp(vport);
13933}
13934
13935/**
James Smart4f774512009-05-22 14:52:35 -040013936 * lpfc_fc_frame_add - Adds a frame to the vport's list of received sequences
13937 * @dmabuf: pointer to a dmabuf that describes the hdr and data of the FC frame
13938 *
13939 * This function searches through the existing incomplete sequences that have
13940 * been sent to this @vport. If the frame matches one of the incomplete
13941 * sequences then the dbuf in the @dmabuf is added to the list of frames that
13942 * make up that sequence. If no sequence is found that matches this frame then
13943 * the function will add the hbuf in the @dmabuf to the @vport's rcv_buffer_list
13944 * This function returns a pointer to the first dmabuf in the sequence list that
13945 * the frame was linked to.
13946 **/
13947static struct hbq_dmabuf *
13948lpfc_fc_frame_add(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
13949{
13950 struct fc_frame_header *new_hdr;
13951 struct fc_frame_header *temp_hdr;
13952 struct lpfc_dmabuf *d_buf;
13953 struct lpfc_dmabuf *h_buf;
13954 struct hbq_dmabuf *seq_dmabuf = NULL;
13955 struct hbq_dmabuf *temp_dmabuf = NULL;
13956
James Smart4d9ab992009-10-02 15:16:39 -040013957 INIT_LIST_HEAD(&dmabuf->dbuf.list);
James Smart45ed1192009-10-02 15:17:02 -040013958 dmabuf->time_stamp = jiffies;
James Smart4f774512009-05-22 14:52:35 -040013959 new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
13960 /* Use the hdr_buf to find the sequence that this frame belongs to */
13961 list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
13962 temp_hdr = (struct fc_frame_header *)h_buf->virt;
13963 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
13964 (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
13965 (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
13966 continue;
13967 /* found a pending sequence that matches this frame */
13968 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
13969 break;
13970 }
13971 if (!seq_dmabuf) {
13972 /*
13973 * This indicates first frame received for this sequence.
13974 * Queue the buffer on the vport's rcv_buffer_list.
13975 */
13976 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
James Smart45ed1192009-10-02 15:17:02 -040013977 lpfc_update_rcv_time_stamp(vport);
James Smart4f774512009-05-22 14:52:35 -040013978 return dmabuf;
13979 }
13980 temp_hdr = seq_dmabuf->hbuf.virt;
James Smarteeead812009-12-21 17:01:23 -050013981 if (be16_to_cpu(new_hdr->fh_seq_cnt) <
13982 be16_to_cpu(temp_hdr->fh_seq_cnt)) {
James Smart4d9ab992009-10-02 15:16:39 -040013983 list_del_init(&seq_dmabuf->hbuf.list);
13984 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
13985 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
James Smart45ed1192009-10-02 15:17:02 -040013986 lpfc_update_rcv_time_stamp(vport);
James Smart4f774512009-05-22 14:52:35 -040013987 return dmabuf;
13988 }
James Smart45ed1192009-10-02 15:17:02 -040013989 /* move this sequence to the tail to indicate a young sequence */
13990 list_move_tail(&seq_dmabuf->hbuf.list, &vport->rcv_buffer_list);
13991 seq_dmabuf->time_stamp = jiffies;
13992 lpfc_update_rcv_time_stamp(vport);
James Smarteeead812009-12-21 17:01:23 -050013993 if (list_empty(&seq_dmabuf->dbuf.list)) {
13994 temp_hdr = dmabuf->hbuf.virt;
13995 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
13996 return seq_dmabuf;
13997 }
James Smart4f774512009-05-22 14:52:35 -040013998 /* find the correct place in the sequence to insert this frame */
13999 list_for_each_entry_reverse(d_buf, &seq_dmabuf->dbuf.list, list) {
14000 temp_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
14001 temp_hdr = (struct fc_frame_header *)temp_dmabuf->hbuf.virt;
14002 /*
14003 * If the frame's sequence count is greater than the frame on
14004 * the list then insert the frame right after this frame
14005 */
James Smarteeead812009-12-21 17:01:23 -050014006 if (be16_to_cpu(new_hdr->fh_seq_cnt) >
14007 be16_to_cpu(temp_hdr->fh_seq_cnt)) {
James Smart4f774512009-05-22 14:52:35 -040014008 list_add(&dmabuf->dbuf.list, &temp_dmabuf->dbuf.list);
14009 return seq_dmabuf;
14010 }
14011 }
14012 return NULL;
14013}
14014
14015/**
James Smart6669f9b2009-10-02 15:16:45 -040014016 * lpfc_sli4_abort_partial_seq - Abort partially assembled unsol sequence
14017 * @vport: pointer to a vitural port
14018 * @dmabuf: pointer to a dmabuf that describes the FC sequence
14019 *
14020 * This function tries to abort from the partially assembed sequence, described
14021 * by the information from basic abbort @dmabuf. It checks to see whether such
14022 * partially assembled sequence held by the driver. If so, it shall free up all
14023 * the frames from the partially assembled sequence.
14024 *
14025 * Return
14026 * true -- if there is matching partially assembled sequence present and all
14027 * the frames freed with the sequence;
14028 * false -- if there is no matching partially assembled sequence present so
14029 * nothing got aborted in the lower layer driver
14030 **/
14031static bool
14032lpfc_sli4_abort_partial_seq(struct lpfc_vport *vport,
14033 struct hbq_dmabuf *dmabuf)
14034{
14035 struct fc_frame_header *new_hdr;
14036 struct fc_frame_header *temp_hdr;
14037 struct lpfc_dmabuf *d_buf, *n_buf, *h_buf;
14038 struct hbq_dmabuf *seq_dmabuf = NULL;
14039
14040 /* Use the hdr_buf to find the sequence that matches this frame */
14041 INIT_LIST_HEAD(&dmabuf->dbuf.list);
14042 INIT_LIST_HEAD(&dmabuf->hbuf.list);
14043 new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
14044 list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
14045 temp_hdr = (struct fc_frame_header *)h_buf->virt;
14046 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
14047 (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
14048 (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
14049 continue;
14050 /* found a pending sequence that matches this frame */
14051 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
14052 break;
14053 }
14054
14055 /* Free up all the frames from the partially assembled sequence */
14056 if (seq_dmabuf) {
14057 list_for_each_entry_safe(d_buf, n_buf,
14058 &seq_dmabuf->dbuf.list, list) {
14059 list_del_init(&d_buf->list);
14060 lpfc_in_buf_free(vport->phba, d_buf);
14061 }
14062 return true;
14063 }
14064 return false;
14065}
14066
14067/**
James Smart546fc852011-03-11 16:06:29 -050014068 * lpfc_sli4_seq_abort_rsp_cmpl - BLS ABORT RSP seq abort iocb complete handler
James Smart6669f9b2009-10-02 15:16:45 -040014069 * @phba: Pointer to HBA context object.
14070 * @cmd_iocbq: pointer to the command iocbq structure.
14071 * @rsp_iocbq: pointer to the response iocbq structure.
14072 *
James Smart546fc852011-03-11 16:06:29 -050014073 * This function handles the sequence abort response iocb command complete
James Smart6669f9b2009-10-02 15:16:45 -040014074 * event. It properly releases the memory allocated to the sequence abort
14075 * accept iocb.
14076 **/
14077static void
James Smart546fc852011-03-11 16:06:29 -050014078lpfc_sli4_seq_abort_rsp_cmpl(struct lpfc_hba *phba,
James Smart6669f9b2009-10-02 15:16:45 -040014079 struct lpfc_iocbq *cmd_iocbq,
14080 struct lpfc_iocbq *rsp_iocbq)
14081{
14082 if (cmd_iocbq)
14083 lpfc_sli_release_iocbq(phba, cmd_iocbq);
James Smart6b5151f2012-01-18 16:24:06 -050014084
14085 /* Failure means BLS ABORT RSP did not get delivered to remote node*/
14086 if (rsp_iocbq && rsp_iocbq->iocb.ulpStatus)
14087 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14088 "3154 BLS ABORT RSP failed, data: x%x/x%x\n",
14089 rsp_iocbq->iocb.ulpStatus,
14090 rsp_iocbq->iocb.un.ulpWord[4]);
James Smart6669f9b2009-10-02 15:16:45 -040014091}
14092
14093/**
James Smart6d368e52011-05-24 11:44:12 -040014094 * lpfc_sli4_xri_inrange - check xri is in range of xris owned by driver.
14095 * @phba: Pointer to HBA context object.
14096 * @xri: xri id in transaction.
14097 *
14098 * This function validates the xri maps to the known range of XRIs allocated an
14099 * used by the driver.
14100 **/
James Smart7851fe22011-07-22 18:36:52 -040014101uint16_t
James Smart6d368e52011-05-24 11:44:12 -040014102lpfc_sli4_xri_inrange(struct lpfc_hba *phba,
14103 uint16_t xri)
14104{
14105 int i;
14106
14107 for (i = 0; i < phba->sli4_hba.max_cfg_param.max_xri; i++) {
14108 if (xri == phba->sli4_hba.xri_ids[i])
14109 return i;
14110 }
14111 return NO_XRI;
14112}
14113
James Smart6d368e52011-05-24 11:44:12 -040014114/**
James Smart546fc852011-03-11 16:06:29 -050014115 * lpfc_sli4_seq_abort_rsp - bls rsp to sequence abort
James Smart6669f9b2009-10-02 15:16:45 -040014116 * @phba: Pointer to HBA context object.
14117 * @fc_hdr: pointer to a FC frame header.
14118 *
James Smart546fc852011-03-11 16:06:29 -050014119 * This function sends a basic response to a previous unsol sequence abort
James Smart6669f9b2009-10-02 15:16:45 -040014120 * event after aborting the sequence handling.
14121 **/
14122static void
James Smart546fc852011-03-11 16:06:29 -050014123lpfc_sli4_seq_abort_rsp(struct lpfc_hba *phba,
James Smart6669f9b2009-10-02 15:16:45 -040014124 struct fc_frame_header *fc_hdr)
14125{
14126 struct lpfc_iocbq *ctiocb = NULL;
14127 struct lpfc_nodelist *ndlp;
James Smartee0f4fe2012-05-09 21:19:14 -040014128 uint16_t oxid, rxid, xri, lxri;
James Smart5ffc2662009-11-18 15:39:44 -050014129 uint32_t sid, fctl;
James Smart6669f9b2009-10-02 15:16:45 -040014130 IOCB_t *icmd;
James Smart546fc852011-03-11 16:06:29 -050014131 int rc;
James Smart6669f9b2009-10-02 15:16:45 -040014132
14133 if (!lpfc_is_link_up(phba))
14134 return;
14135
14136 sid = sli4_sid_from_fc_hdr(fc_hdr);
14137 oxid = be16_to_cpu(fc_hdr->fh_ox_id);
James Smart5ffc2662009-11-18 15:39:44 -050014138 rxid = be16_to_cpu(fc_hdr->fh_rx_id);
James Smart6669f9b2009-10-02 15:16:45 -040014139
14140 ndlp = lpfc_findnode_did(phba->pport, sid);
14141 if (!ndlp) {
14142 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
14143 "1268 Find ndlp returned NULL for oxid:x%x "
14144 "SID:x%x\n", oxid, sid);
14145 return;
14146 }
14147
James Smart546fc852011-03-11 16:06:29 -050014148 /* Allocate buffer for rsp iocb */
James Smart6669f9b2009-10-02 15:16:45 -040014149 ctiocb = lpfc_sli_get_iocbq(phba);
14150 if (!ctiocb)
14151 return;
14152
James Smart5ffc2662009-11-18 15:39:44 -050014153 /* Extract the F_CTL field from FC_HDR */
14154 fctl = sli4_fctl_from_fc_hdr(fc_hdr);
14155
James Smart6669f9b2009-10-02 15:16:45 -040014156 icmd = &ctiocb->iocb;
James Smart6669f9b2009-10-02 15:16:45 -040014157 icmd->un.xseq64.bdl.bdeSize = 0;
James Smart5ffc2662009-11-18 15:39:44 -050014158 icmd->un.xseq64.bdl.ulpIoTag32 = 0;
James Smart6669f9b2009-10-02 15:16:45 -040014159 icmd->un.xseq64.w5.hcsw.Dfctl = 0;
14160 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_ACC;
14161 icmd->un.xseq64.w5.hcsw.Type = FC_TYPE_BLS;
14162
14163 /* Fill in the rest of iocb fields */
14164 icmd->ulpCommand = CMD_XMIT_BLS_RSP64_CX;
14165 icmd->ulpBdeCount = 0;
14166 icmd->ulpLe = 1;
14167 icmd->ulpClass = CLASS3;
James Smart6d368e52011-05-24 11:44:12 -040014168 icmd->ulpContext = phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
James Smartbe858b62010-12-15 17:57:20 -050014169 ctiocb->context1 = ndlp;
James Smart6669f9b2009-10-02 15:16:45 -040014170
James Smart6669f9b2009-10-02 15:16:45 -040014171 ctiocb->iocb_cmpl = NULL;
14172 ctiocb->vport = phba->pport;
James Smart546fc852011-03-11 16:06:29 -050014173 ctiocb->iocb_cmpl = lpfc_sli4_seq_abort_rsp_cmpl;
James Smart6d368e52011-05-24 11:44:12 -040014174 ctiocb->sli4_lxritag = NO_XRI;
James Smart546fc852011-03-11 16:06:29 -050014175 ctiocb->sli4_xritag = NO_XRI;
14176
James Smartee0f4fe2012-05-09 21:19:14 -040014177 if (fctl & FC_FC_EX_CTX)
14178 /* Exchange responder sent the abort so we
14179 * own the oxid.
14180 */
14181 xri = oxid;
14182 else
14183 xri = rxid;
14184 lxri = lpfc_sli4_xri_inrange(phba, xri);
14185 if (lxri != NO_XRI)
14186 lpfc_set_rrq_active(phba, ndlp, lxri,
14187 (xri == oxid) ? rxid : oxid, 0);
James Smart546fc852011-03-11 16:06:29 -050014188 /* If the oxid maps to the FCP XRI range or if it is out of range,
14189 * send a BLS_RJT. The driver no longer has that exchange.
14190 * Override the IOCB for a BA_RJT.
14191 */
James Smartee0f4fe2012-05-09 21:19:14 -040014192 if (xri > (phba->sli4_hba.max_cfg_param.max_xri +
James Smart546fc852011-03-11 16:06:29 -050014193 phba->sli4_hba.max_cfg_param.xri_base) ||
James Smartee0f4fe2012-05-09 21:19:14 -040014194 xri > (lpfc_sli4_get_els_iocb_cnt(phba) +
James Smart546fc852011-03-11 16:06:29 -050014195 phba->sli4_hba.max_cfg_param.xri_base)) {
14196 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_RJT;
14197 bf_set(lpfc_vndr_code, &icmd->un.bls_rsp, 0);
14198 bf_set(lpfc_rsn_expln, &icmd->un.bls_rsp, FC_BA_RJT_INV_XID);
14199 bf_set(lpfc_rsn_code, &icmd->un.bls_rsp, FC_BA_RJT_UNABLE);
14200 }
James Smart6669f9b2009-10-02 15:16:45 -040014201
James Smart5ffc2662009-11-18 15:39:44 -050014202 if (fctl & FC_FC_EX_CTX) {
14203 /* ABTS sent by responder to CT exchange, construction
14204 * of BA_ACC will use OX_ID from ABTS for the XRI_TAG
14205 * field and RX_ID from ABTS for RX_ID field.
14206 */
James Smart546fc852011-03-11 16:06:29 -050014207 bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_RSP);
James Smart5ffc2662009-11-18 15:39:44 -050014208 } else {
14209 /* ABTS sent by initiator to CT exchange, construction
14210 * of BA_ACC will need to allocate a new XRI as for the
James Smartf09c3ac2012-03-01 22:33:29 -050014211 * XRI_TAG field.
James Smart5ffc2662009-11-18 15:39:44 -050014212 */
James Smart546fc852011-03-11 16:06:29 -050014213 bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_INT);
James Smart5ffc2662009-11-18 15:39:44 -050014214 }
James Smartf09c3ac2012-03-01 22:33:29 -050014215 bf_set(lpfc_abts_rxid, &icmd->un.bls_rsp, rxid);
James Smart546fc852011-03-11 16:06:29 -050014216 bf_set(lpfc_abts_oxid, &icmd->un.bls_rsp, oxid);
James Smart5ffc2662009-11-18 15:39:44 -050014217
James Smart546fc852011-03-11 16:06:29 -050014218 /* Xmit CT abts response on exchange <xid> */
James Smart6669f9b2009-10-02 15:16:45 -040014219 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
James Smart546fc852011-03-11 16:06:29 -050014220 "1200 Send BLS cmd x%x on oxid x%x Data: x%x\n",
14221 icmd->un.xseq64.w5.hcsw.Rctl, oxid, phba->link_state);
14222
14223 rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, ctiocb, 0);
14224 if (rc == IOCB_ERROR) {
14225 lpfc_printf_log(phba, KERN_ERR, LOG_ELS,
14226 "2925 Failed to issue CT ABTS RSP x%x on "
14227 "xri x%x, Data x%x\n",
14228 icmd->un.xseq64.w5.hcsw.Rctl, oxid,
14229 phba->link_state);
14230 lpfc_sli_release_iocbq(phba, ctiocb);
14231 }
James Smart6669f9b2009-10-02 15:16:45 -040014232}
14233
14234/**
14235 * lpfc_sli4_handle_unsol_abort - Handle sli-4 unsolicited abort event
14236 * @vport: Pointer to the vport on which this sequence was received
14237 * @dmabuf: pointer to a dmabuf that describes the FC sequence
14238 *
14239 * This function handles an SLI-4 unsolicited abort event. If the unsolicited
14240 * receive sequence is only partially assembed by the driver, it shall abort
14241 * the partially assembled frames for the sequence. Otherwise, if the
14242 * unsolicited receive sequence has been completely assembled and passed to
14243 * the Upper Layer Protocol (UPL), it then mark the per oxid status for the
14244 * unsolicited sequence has been aborted. After that, it will issue a basic
14245 * accept to accept the abort.
14246 **/
14247void
14248lpfc_sli4_handle_unsol_abort(struct lpfc_vport *vport,
14249 struct hbq_dmabuf *dmabuf)
14250{
14251 struct lpfc_hba *phba = vport->phba;
14252 struct fc_frame_header fc_hdr;
James Smart5ffc2662009-11-18 15:39:44 -050014253 uint32_t fctl;
James Smart6669f9b2009-10-02 15:16:45 -040014254 bool abts_par;
14255
James Smart6669f9b2009-10-02 15:16:45 -040014256 /* Make a copy of fc_hdr before the dmabuf being released */
14257 memcpy(&fc_hdr, dmabuf->hbuf.virt, sizeof(struct fc_frame_header));
James Smart5ffc2662009-11-18 15:39:44 -050014258 fctl = sli4_fctl_from_fc_hdr(&fc_hdr);
James Smart6669f9b2009-10-02 15:16:45 -040014259
James Smart5ffc2662009-11-18 15:39:44 -050014260 if (fctl & FC_FC_EX_CTX) {
14261 /*
14262 * ABTS sent by responder to exchange, just free the buffer
14263 */
James Smart6669f9b2009-10-02 15:16:45 -040014264 lpfc_in_buf_free(phba, &dmabuf->dbuf);
James Smart5ffc2662009-11-18 15:39:44 -050014265 } else {
14266 /*
14267 * ABTS sent by initiator to exchange, need to do cleanup
14268 */
14269 /* Try to abort partially assembled seq */
14270 abts_par = lpfc_sli4_abort_partial_seq(vport, dmabuf);
14271
14272 /* Send abort to ULP if partially seq abort failed */
14273 if (abts_par == false)
14274 lpfc_sli4_send_seq_to_ulp(vport, dmabuf);
14275 else
14276 lpfc_in_buf_free(phba, &dmabuf->dbuf);
14277 }
James Smart6669f9b2009-10-02 15:16:45 -040014278 /* Send basic accept (BA_ACC) to the abort requester */
James Smart546fc852011-03-11 16:06:29 -050014279 lpfc_sli4_seq_abort_rsp(phba, &fc_hdr);
James Smart6669f9b2009-10-02 15:16:45 -040014280}
14281
14282/**
James Smart4f774512009-05-22 14:52:35 -040014283 * lpfc_seq_complete - Indicates if a sequence is complete
14284 * @dmabuf: pointer to a dmabuf that describes the FC sequence
14285 *
14286 * This function checks the sequence, starting with the frame described by
14287 * @dmabuf, to see if all the frames associated with this sequence are present.
14288 * the frames associated with this sequence are linked to the @dmabuf using the
14289 * dbuf list. This function looks for two major things. 1) That the first frame
14290 * has a sequence count of zero. 2) There is a frame with last frame of sequence
14291 * set. 3) That there are no holes in the sequence count. The function will
14292 * return 1 when the sequence is complete, otherwise it will return 0.
14293 **/
14294static int
14295lpfc_seq_complete(struct hbq_dmabuf *dmabuf)
14296{
14297 struct fc_frame_header *hdr;
14298 struct lpfc_dmabuf *d_buf;
14299 struct hbq_dmabuf *seq_dmabuf;
14300 uint32_t fctl;
14301 int seq_count = 0;
14302
14303 hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
14304 /* make sure first fame of sequence has a sequence count of zero */
14305 if (hdr->fh_seq_cnt != seq_count)
14306 return 0;
14307 fctl = (hdr->fh_f_ctl[0] << 16 |
14308 hdr->fh_f_ctl[1] << 8 |
14309 hdr->fh_f_ctl[2]);
14310 /* If last frame of sequence we can return success. */
14311 if (fctl & FC_FC_END_SEQ)
14312 return 1;
14313 list_for_each_entry(d_buf, &dmabuf->dbuf.list, list) {
14314 seq_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
14315 hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
14316 /* If there is a hole in the sequence count then fail. */
James Smarteeead812009-12-21 17:01:23 -050014317 if (++seq_count != be16_to_cpu(hdr->fh_seq_cnt))
James Smart4f774512009-05-22 14:52:35 -040014318 return 0;
14319 fctl = (hdr->fh_f_ctl[0] << 16 |
14320 hdr->fh_f_ctl[1] << 8 |
14321 hdr->fh_f_ctl[2]);
14322 /* If last frame of sequence we can return success. */
14323 if (fctl & FC_FC_END_SEQ)
14324 return 1;
14325 }
14326 return 0;
14327}
14328
14329/**
14330 * lpfc_prep_seq - Prep sequence for ULP processing
14331 * @vport: Pointer to the vport on which this sequence was received
14332 * @dmabuf: pointer to a dmabuf that describes the FC sequence
14333 *
14334 * This function takes a sequence, described by a list of frames, and creates
14335 * a list of iocbq structures to describe the sequence. This iocbq list will be
14336 * used to issue to the generic unsolicited sequence handler. This routine
14337 * returns a pointer to the first iocbq in the list. If the function is unable
14338 * to allocate an iocbq then it throw out the received frames that were not
14339 * able to be described and return a pointer to the first iocbq. If unable to
14340 * allocate any iocbqs (including the first) this function will return NULL.
14341 **/
14342static struct lpfc_iocbq *
14343lpfc_prep_seq(struct lpfc_vport *vport, struct hbq_dmabuf *seq_dmabuf)
14344{
James Smart7851fe22011-07-22 18:36:52 -040014345 struct hbq_dmabuf *hbq_buf;
James Smart4f774512009-05-22 14:52:35 -040014346 struct lpfc_dmabuf *d_buf, *n_buf;
14347 struct lpfc_iocbq *first_iocbq, *iocbq;
14348 struct fc_frame_header *fc_hdr;
14349 uint32_t sid;
James Smart7851fe22011-07-22 18:36:52 -040014350 uint32_t len, tot_len;
James Smarteeead812009-12-21 17:01:23 -050014351 struct ulp_bde64 *pbde;
James Smart4f774512009-05-22 14:52:35 -040014352
14353 fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
14354 /* remove from receive buffer list */
14355 list_del_init(&seq_dmabuf->hbuf.list);
James Smart45ed1192009-10-02 15:17:02 -040014356 lpfc_update_rcv_time_stamp(vport);
James Smart4f774512009-05-22 14:52:35 -040014357 /* get the Remote Port's SID */
James Smart6669f9b2009-10-02 15:16:45 -040014358 sid = sli4_sid_from_fc_hdr(fc_hdr);
James Smart7851fe22011-07-22 18:36:52 -040014359 tot_len = 0;
James Smart4f774512009-05-22 14:52:35 -040014360 /* Get an iocbq struct to fill in. */
14361 first_iocbq = lpfc_sli_get_iocbq(vport->phba);
14362 if (first_iocbq) {
14363 /* Initialize the first IOCB. */
James Smart8fa38512009-07-19 10:01:03 -040014364 first_iocbq->iocb.unsli3.rcvsli3.acc_len = 0;
James Smart4f774512009-05-22 14:52:35 -040014365 first_iocbq->iocb.ulpStatus = IOSTAT_SUCCESS;
James Smart939723a2012-05-09 21:19:03 -040014366
14367 /* Check FC Header to see what TYPE of frame we are rcv'ing */
14368 if (sli4_type_from_fc_hdr(fc_hdr) == FC_TYPE_ELS) {
14369 first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_ELS64_CX;
14370 first_iocbq->iocb.un.rcvels.parmRo =
14371 sli4_did_from_fc_hdr(fc_hdr);
14372 first_iocbq->iocb.ulpPU = PARM_NPIV_DID;
14373 } else
14374 first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_SEQ64_CX;
James Smart7851fe22011-07-22 18:36:52 -040014375 first_iocbq->iocb.ulpContext = NO_XRI;
14376 first_iocbq->iocb.unsli3.rcvsli3.ox_id =
14377 be16_to_cpu(fc_hdr->fh_ox_id);
14378 /* iocbq is prepped for internal consumption. Physical vpi. */
14379 first_iocbq->iocb.unsli3.rcvsli3.vpi =
14380 vport->phba->vpi_ids[vport->vpi];
James Smart4f774512009-05-22 14:52:35 -040014381 /* put the first buffer into the first IOCBq */
14382 first_iocbq->context2 = &seq_dmabuf->dbuf;
14383 first_iocbq->context3 = NULL;
14384 first_iocbq->iocb.ulpBdeCount = 1;
14385 first_iocbq->iocb.un.cont64[0].tus.f.bdeSize =
14386 LPFC_DATA_BUF_SIZE;
14387 first_iocbq->iocb.un.rcvels.remoteID = sid;
James Smart7851fe22011-07-22 18:36:52 -040014388 tot_len = bf_get(lpfc_rcqe_length,
James Smart4d9ab992009-10-02 15:16:39 -040014389 &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
James Smart7851fe22011-07-22 18:36:52 -040014390 first_iocbq->iocb.unsli3.rcvsli3.acc_len = tot_len;
James Smart4f774512009-05-22 14:52:35 -040014391 }
14392 iocbq = first_iocbq;
14393 /*
14394 * Each IOCBq can have two Buffers assigned, so go through the list
14395 * of buffers for this sequence and save two buffers in each IOCBq
14396 */
14397 list_for_each_entry_safe(d_buf, n_buf, &seq_dmabuf->dbuf.list, list) {
14398 if (!iocbq) {
14399 lpfc_in_buf_free(vport->phba, d_buf);
14400 continue;
14401 }
14402 if (!iocbq->context3) {
14403 iocbq->context3 = d_buf;
14404 iocbq->iocb.ulpBdeCount++;
James Smarteeead812009-12-21 17:01:23 -050014405 pbde = (struct ulp_bde64 *)
14406 &iocbq->iocb.unsli3.sli3Words[4];
14407 pbde->tus.f.bdeSize = LPFC_DATA_BUF_SIZE;
James Smart7851fe22011-07-22 18:36:52 -040014408
14409 /* We need to get the size out of the right CQE */
14410 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
14411 len = bf_get(lpfc_rcqe_length,
14412 &hbq_buf->cq_event.cqe.rcqe_cmpl);
14413 iocbq->iocb.unsli3.rcvsli3.acc_len += len;
14414 tot_len += len;
James Smart4f774512009-05-22 14:52:35 -040014415 } else {
14416 iocbq = lpfc_sli_get_iocbq(vport->phba);
14417 if (!iocbq) {
14418 if (first_iocbq) {
14419 first_iocbq->iocb.ulpStatus =
14420 IOSTAT_FCP_RSP_ERROR;
14421 first_iocbq->iocb.un.ulpWord[4] =
14422 IOERR_NO_RESOURCES;
14423 }
14424 lpfc_in_buf_free(vport->phba, d_buf);
14425 continue;
14426 }
14427 iocbq->context2 = d_buf;
14428 iocbq->context3 = NULL;
14429 iocbq->iocb.ulpBdeCount = 1;
14430 iocbq->iocb.un.cont64[0].tus.f.bdeSize =
14431 LPFC_DATA_BUF_SIZE;
James Smart7851fe22011-07-22 18:36:52 -040014432
14433 /* We need to get the size out of the right CQE */
14434 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
14435 len = bf_get(lpfc_rcqe_length,
14436 &hbq_buf->cq_event.cqe.rcqe_cmpl);
14437 tot_len += len;
14438 iocbq->iocb.unsli3.rcvsli3.acc_len = tot_len;
14439
James Smart4f774512009-05-22 14:52:35 -040014440 iocbq->iocb.un.rcvels.remoteID = sid;
14441 list_add_tail(&iocbq->list, &first_iocbq->list);
14442 }
14443 }
14444 return first_iocbq;
14445}
14446
James Smart6669f9b2009-10-02 15:16:45 -040014447static void
14448lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *vport,
14449 struct hbq_dmabuf *seq_dmabuf)
14450{
14451 struct fc_frame_header *fc_hdr;
14452 struct lpfc_iocbq *iocbq, *curr_iocb, *next_iocb;
14453 struct lpfc_hba *phba = vport->phba;
14454
14455 fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
14456 iocbq = lpfc_prep_seq(vport, seq_dmabuf);
14457 if (!iocbq) {
14458 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14459 "2707 Ring %d handler: Failed to allocate "
14460 "iocb Rctl x%x Type x%x received\n",
14461 LPFC_ELS_RING,
14462 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
14463 return;
14464 }
14465 if (!lpfc_complete_unsol_iocb(phba,
14466 &phba->sli.ring[LPFC_ELS_RING],
14467 iocbq, fc_hdr->fh_r_ctl,
14468 fc_hdr->fh_type))
James Smart6d368e52011-05-24 11:44:12 -040014469 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart6669f9b2009-10-02 15:16:45 -040014470 "2540 Ring %d handler: unexpected Rctl "
14471 "x%x Type x%x received\n",
14472 LPFC_ELS_RING,
14473 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
14474
14475 /* Free iocb created in lpfc_prep_seq */
14476 list_for_each_entry_safe(curr_iocb, next_iocb,
14477 &iocbq->list, list) {
14478 list_del_init(&curr_iocb->list);
14479 lpfc_sli_release_iocbq(phba, curr_iocb);
14480 }
14481 lpfc_sli_release_iocbq(phba, iocbq);
14482}
14483
James Smart4f774512009-05-22 14:52:35 -040014484/**
14485 * lpfc_sli4_handle_received_buffer - Handle received buffers from firmware
14486 * @phba: Pointer to HBA context object.
14487 *
14488 * This function is called with no lock held. This function processes all
14489 * the received buffers and gives it to upper layers when a received buffer
14490 * indicates that it is the final frame in the sequence. The interrupt
14491 * service routine processes received buffers at interrupt contexts and adds
14492 * received dma buffers to the rb_pend_list queue and signals the worker thread.
14493 * Worker thread calls lpfc_sli4_handle_received_buffer, which will call the
14494 * appropriate receive function when the final frame in a sequence is received.
14495 **/
James Smart4d9ab992009-10-02 15:16:39 -040014496void
14497lpfc_sli4_handle_received_buffer(struct lpfc_hba *phba,
14498 struct hbq_dmabuf *dmabuf)
James Smart4f774512009-05-22 14:52:35 -040014499{
James Smart4d9ab992009-10-02 15:16:39 -040014500 struct hbq_dmabuf *seq_dmabuf;
James Smart4f774512009-05-22 14:52:35 -040014501 struct fc_frame_header *fc_hdr;
14502 struct lpfc_vport *vport;
14503 uint32_t fcfi;
James Smart939723a2012-05-09 21:19:03 -040014504 uint32_t did;
James Smart4f774512009-05-22 14:52:35 -040014505
James Smart4f774512009-05-22 14:52:35 -040014506 /* Process each received buffer */
James Smart4d9ab992009-10-02 15:16:39 -040014507 fc_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
14508 /* check to see if this a valid type of frame */
14509 if (lpfc_fc_frame_check(phba, fc_hdr)) {
14510 lpfc_in_buf_free(phba, &dmabuf->dbuf);
14511 return;
14512 }
James Smart7851fe22011-07-22 18:36:52 -040014513 if ((bf_get(lpfc_cqe_code,
14514 &dmabuf->cq_event.cqe.rcqe_cmpl) == CQE_CODE_RECEIVE_V1))
14515 fcfi = bf_get(lpfc_rcqe_fcf_id_v1,
14516 &dmabuf->cq_event.cqe.rcqe_cmpl);
14517 else
14518 fcfi = bf_get(lpfc_rcqe_fcf_id,
14519 &dmabuf->cq_event.cqe.rcqe_cmpl);
James Smart939723a2012-05-09 21:19:03 -040014520
James Smart4d9ab992009-10-02 15:16:39 -040014521 vport = lpfc_fc_frame_to_vport(phba, fc_hdr, fcfi);
James Smart939723a2012-05-09 21:19:03 -040014522 if (!vport) {
James Smart4d9ab992009-10-02 15:16:39 -040014523 /* throw out the frame */
14524 lpfc_in_buf_free(phba, &dmabuf->dbuf);
14525 return;
14526 }
James Smart939723a2012-05-09 21:19:03 -040014527
14528 /* d_id this frame is directed to */
14529 did = sli4_did_from_fc_hdr(fc_hdr);
14530
14531 /* vport is registered unless we rcv a FLOGI directed to Fabric_DID */
14532 if (!(vport->vpi_state & LPFC_VPI_REGISTERED) &&
14533 (did != Fabric_DID)) {
14534 /*
14535 * Throw out the frame if we are not pt2pt.
14536 * The pt2pt protocol allows for discovery frames
14537 * to be received without a registered VPI.
14538 */
14539 if (!(vport->fc_flag & FC_PT2PT) ||
14540 (phba->link_state == LPFC_HBA_READY)) {
14541 lpfc_in_buf_free(phba, &dmabuf->dbuf);
14542 return;
14543 }
14544 }
14545
James Smart6669f9b2009-10-02 15:16:45 -040014546 /* Handle the basic abort sequence (BA_ABTS) event */
14547 if (fc_hdr->fh_r_ctl == FC_RCTL_BA_ABTS) {
14548 lpfc_sli4_handle_unsol_abort(vport, dmabuf);
14549 return;
14550 }
14551
James Smart4d9ab992009-10-02 15:16:39 -040014552 /* Link this frame */
14553 seq_dmabuf = lpfc_fc_frame_add(vport, dmabuf);
14554 if (!seq_dmabuf) {
14555 /* unable to add frame to vport - throw it out */
14556 lpfc_in_buf_free(phba, &dmabuf->dbuf);
14557 return;
14558 }
14559 /* If not last frame in sequence continue processing frames. */
James Smartdef9c7a2009-12-21 17:02:28 -050014560 if (!lpfc_seq_complete(seq_dmabuf))
James Smart4d9ab992009-10-02 15:16:39 -040014561 return;
James Smartdef9c7a2009-12-21 17:02:28 -050014562
James Smart6669f9b2009-10-02 15:16:45 -040014563 /* Send the complete sequence to the upper layer protocol */
14564 lpfc_sli4_send_seq_to_ulp(vport, seq_dmabuf);
James Smart4f774512009-05-22 14:52:35 -040014565}
James Smart6fb120a2009-05-22 14:52:59 -040014566
14567/**
14568 * lpfc_sli4_post_all_rpi_hdrs - Post the rpi header memory region to the port
14569 * @phba: pointer to lpfc hba data structure.
14570 *
14571 * This routine is invoked to post rpi header templates to the
14572 * HBA consistent with the SLI-4 interface spec. This routine
James Smart49198b32010-04-06 15:04:33 -040014573 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
14574 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
James Smart6fb120a2009-05-22 14:52:59 -040014575 *
14576 * This routine does not require any locks. It's usage is expected
14577 * to be driver load or reset recovery when the driver is
14578 * sequential.
14579 *
14580 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020014581 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -040014582 * -EIO - The mailbox failed to complete successfully.
James Smart6fb120a2009-05-22 14:52:59 -040014583 * When this error occurs, the driver is not guaranteed
14584 * to have any rpi regions posted to the device and
14585 * must either attempt to repost the regions or take a
14586 * fatal error.
14587 **/
14588int
14589lpfc_sli4_post_all_rpi_hdrs(struct lpfc_hba *phba)
14590{
14591 struct lpfc_rpi_hdr *rpi_page;
14592 uint32_t rc = 0;
James Smart6d368e52011-05-24 11:44:12 -040014593 uint16_t lrpi = 0;
James Smart6fb120a2009-05-22 14:52:59 -040014594
James Smart6d368e52011-05-24 11:44:12 -040014595 /* SLI4 ports that support extents do not require RPI headers. */
14596 if (!phba->sli4_hba.rpi_hdrs_in_use)
14597 goto exit;
14598 if (phba->sli4_hba.extents_in_use)
14599 return -EIO;
14600
James Smart6fb120a2009-05-22 14:52:59 -040014601 list_for_each_entry(rpi_page, &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
James Smart6d368e52011-05-24 11:44:12 -040014602 /*
14603 * Assign the rpi headers a physical rpi only if the driver
14604 * has not initialized those resources. A port reset only
14605 * needs the headers posted.
14606 */
14607 if (bf_get(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags) !=
14608 LPFC_RPI_RSRC_RDY)
14609 rpi_page->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
14610
James Smart6fb120a2009-05-22 14:52:59 -040014611 rc = lpfc_sli4_post_rpi_hdr(phba, rpi_page);
14612 if (rc != MBX_SUCCESS) {
14613 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14614 "2008 Error %d posting all rpi "
14615 "headers\n", rc);
14616 rc = -EIO;
14617 break;
14618 }
14619 }
14620
James Smart6d368e52011-05-24 11:44:12 -040014621 exit:
14622 bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags,
14623 LPFC_RPI_RSRC_RDY);
James Smart6fb120a2009-05-22 14:52:59 -040014624 return rc;
14625}
14626
14627/**
14628 * lpfc_sli4_post_rpi_hdr - Post an rpi header memory region to the port
14629 * @phba: pointer to lpfc hba data structure.
14630 * @rpi_page: pointer to the rpi memory region.
14631 *
14632 * This routine is invoked to post a single rpi header to the
14633 * HBA consistent with the SLI-4 interface spec. This memory region
14634 * maps up to 64 rpi context regions.
14635 *
14636 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020014637 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -040014638 * -ENOMEM - No available memory
14639 * -EIO - The mailbox failed to complete successfully.
James Smart6fb120a2009-05-22 14:52:59 -040014640 **/
14641int
14642lpfc_sli4_post_rpi_hdr(struct lpfc_hba *phba, struct lpfc_rpi_hdr *rpi_page)
14643{
14644 LPFC_MBOXQ_t *mboxq;
14645 struct lpfc_mbx_post_hdr_tmpl *hdr_tmpl;
14646 uint32_t rc = 0;
James Smart6fb120a2009-05-22 14:52:59 -040014647 uint32_t shdr_status, shdr_add_status;
14648 union lpfc_sli4_cfg_shdr *shdr;
14649
James Smart6d368e52011-05-24 11:44:12 -040014650 /* SLI4 ports that support extents do not require RPI headers. */
14651 if (!phba->sli4_hba.rpi_hdrs_in_use)
14652 return rc;
14653 if (phba->sli4_hba.extents_in_use)
14654 return -EIO;
14655
James Smart6fb120a2009-05-22 14:52:59 -040014656 /* The port is notified of the header region via a mailbox command. */
14657 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14658 if (!mboxq) {
14659 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14660 "2001 Unable to allocate memory for issuing "
14661 "SLI_CONFIG_SPECIAL mailbox command\n");
14662 return -ENOMEM;
14663 }
14664
14665 /* Post all rpi memory regions to the port. */
14666 hdr_tmpl = &mboxq->u.mqe.un.hdr_tmpl;
James Smart6fb120a2009-05-22 14:52:59 -040014667 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
14668 LPFC_MBOX_OPCODE_FCOE_POST_HDR_TEMPLATE,
14669 sizeof(struct lpfc_mbx_post_hdr_tmpl) -
James Smartfedd3b72011-02-16 12:39:24 -050014670 sizeof(struct lpfc_sli4_cfg_mhdr),
14671 LPFC_SLI4_MBX_EMBED);
James Smart6d368e52011-05-24 11:44:12 -040014672
14673
14674 /* Post the physical rpi to the port for this rpi header. */
James Smart6fb120a2009-05-22 14:52:59 -040014675 bf_set(lpfc_mbx_post_hdr_tmpl_rpi_offset, hdr_tmpl,
14676 rpi_page->start_rpi);
James Smart6d368e52011-05-24 11:44:12 -040014677 bf_set(lpfc_mbx_post_hdr_tmpl_page_cnt,
14678 hdr_tmpl, rpi_page->page_count);
14679
James Smart6fb120a2009-05-22 14:52:59 -040014680 hdr_tmpl->rpi_paddr_lo = putPaddrLow(rpi_page->dmabuf->phys);
14681 hdr_tmpl->rpi_paddr_hi = putPaddrHigh(rpi_page->dmabuf->phys);
James Smartf1126682009-06-10 17:22:44 -040014682 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
James Smart6fb120a2009-05-22 14:52:59 -040014683 shdr = (union lpfc_sli4_cfg_shdr *) &hdr_tmpl->header.cfg_shdr;
14684 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14685 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14686 if (rc != MBX_TIMEOUT)
14687 mempool_free(mboxq, phba->mbox_mem_pool);
14688 if (shdr_status || shdr_add_status || rc) {
14689 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14690 "2514 POST_RPI_HDR mailbox failed with "
14691 "status x%x add_status x%x, mbx status x%x\n",
14692 shdr_status, shdr_add_status, rc);
14693 rc = -ENXIO;
14694 }
14695 return rc;
14696}
14697
14698/**
14699 * lpfc_sli4_alloc_rpi - Get an available rpi in the device's range
14700 * @phba: pointer to lpfc hba data structure.
14701 *
14702 * This routine is invoked to post rpi header templates to the
14703 * HBA consistent with the SLI-4 interface spec. This routine
James Smart49198b32010-04-06 15:04:33 -040014704 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
14705 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
James Smart6fb120a2009-05-22 14:52:59 -040014706 *
14707 * Returns
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020014708 * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
James Smart6fb120a2009-05-22 14:52:59 -040014709 * LPFC_RPI_ALLOC_ERROR if no rpis are available.
14710 **/
14711int
14712lpfc_sli4_alloc_rpi(struct lpfc_hba *phba)
14713{
James Smart6d368e52011-05-24 11:44:12 -040014714 unsigned long rpi;
14715 uint16_t max_rpi, rpi_limit;
14716 uint16_t rpi_remaining, lrpi = 0;
James Smart6fb120a2009-05-22 14:52:59 -040014717 struct lpfc_rpi_hdr *rpi_hdr;
14718
14719 max_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
James Smart6fb120a2009-05-22 14:52:59 -040014720 rpi_limit = phba->sli4_hba.next_rpi;
14721
14722 /*
James Smart6d368e52011-05-24 11:44:12 -040014723 * Fetch the next logical rpi. Because this index is logical,
14724 * the driver starts at 0 each time.
James Smart6fb120a2009-05-22 14:52:59 -040014725 */
14726 spin_lock_irq(&phba->hbalock);
James Smart6d368e52011-05-24 11:44:12 -040014727 rpi = find_next_zero_bit(phba->sli4_hba.rpi_bmask, rpi_limit, 0);
14728 if (rpi >= rpi_limit)
James Smart6fb120a2009-05-22 14:52:59 -040014729 rpi = LPFC_RPI_ALLOC_ERROR;
14730 else {
14731 set_bit(rpi, phba->sli4_hba.rpi_bmask);
14732 phba->sli4_hba.max_cfg_param.rpi_used++;
14733 phba->sli4_hba.rpi_count++;
14734 }
14735
14736 /*
14737 * Don't try to allocate more rpi header regions if the device limit
James Smart6d368e52011-05-24 11:44:12 -040014738 * has been exhausted.
James Smart6fb120a2009-05-22 14:52:59 -040014739 */
14740 if ((rpi == LPFC_RPI_ALLOC_ERROR) &&
14741 (phba->sli4_hba.rpi_count >= max_rpi)) {
14742 spin_unlock_irq(&phba->hbalock);
14743 return rpi;
14744 }
14745
14746 /*
James Smart6d368e52011-05-24 11:44:12 -040014747 * RPI header postings are not required for SLI4 ports capable of
14748 * extents.
14749 */
14750 if (!phba->sli4_hba.rpi_hdrs_in_use) {
14751 spin_unlock_irq(&phba->hbalock);
14752 return rpi;
14753 }
14754
14755 /*
James Smart6fb120a2009-05-22 14:52:59 -040014756 * If the driver is running low on rpi resources, allocate another
14757 * page now. Note that the next_rpi value is used because
14758 * it represents how many are actually in use whereas max_rpi notes
14759 * how many are supported max by the device.
14760 */
James Smart6d368e52011-05-24 11:44:12 -040014761 rpi_remaining = phba->sli4_hba.next_rpi - phba->sli4_hba.rpi_count;
James Smart6fb120a2009-05-22 14:52:59 -040014762 spin_unlock_irq(&phba->hbalock);
14763 if (rpi_remaining < LPFC_RPI_LOW_WATER_MARK) {
14764 rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
14765 if (!rpi_hdr) {
14766 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14767 "2002 Error Could not grow rpi "
14768 "count\n");
14769 } else {
James Smart6d368e52011-05-24 11:44:12 -040014770 lrpi = rpi_hdr->start_rpi;
14771 rpi_hdr->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
James Smart6fb120a2009-05-22 14:52:59 -040014772 lpfc_sli4_post_rpi_hdr(phba, rpi_hdr);
14773 }
14774 }
14775
14776 return rpi;
14777}
14778
14779/**
14780 * lpfc_sli4_free_rpi - Release an rpi for reuse.
14781 * @phba: pointer to lpfc hba data structure.
14782 *
14783 * This routine is invoked to release an rpi to the pool of
14784 * available rpis maintained by the driver.
14785 **/
14786void
James Smartd7c47992010-06-08 18:31:54 -040014787__lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
14788{
14789 if (test_and_clear_bit(rpi, phba->sli4_hba.rpi_bmask)) {
14790 phba->sli4_hba.rpi_count--;
14791 phba->sli4_hba.max_cfg_param.rpi_used--;
14792 }
14793}
14794
14795/**
14796 * lpfc_sli4_free_rpi - Release an rpi for reuse.
14797 * @phba: pointer to lpfc hba data structure.
14798 *
14799 * This routine is invoked to release an rpi to the pool of
14800 * available rpis maintained by the driver.
14801 **/
14802void
James Smart6fb120a2009-05-22 14:52:59 -040014803lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
14804{
14805 spin_lock_irq(&phba->hbalock);
James Smartd7c47992010-06-08 18:31:54 -040014806 __lpfc_sli4_free_rpi(phba, rpi);
James Smart6fb120a2009-05-22 14:52:59 -040014807 spin_unlock_irq(&phba->hbalock);
14808}
14809
14810/**
14811 * lpfc_sli4_remove_rpis - Remove the rpi bitmask region
14812 * @phba: pointer to lpfc hba data structure.
14813 *
14814 * This routine is invoked to remove the memory region that
14815 * provided rpi via a bitmask.
14816 **/
14817void
14818lpfc_sli4_remove_rpis(struct lpfc_hba *phba)
14819{
14820 kfree(phba->sli4_hba.rpi_bmask);
James Smart6d368e52011-05-24 11:44:12 -040014821 kfree(phba->sli4_hba.rpi_ids);
14822 bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
James Smart6fb120a2009-05-22 14:52:59 -040014823}
14824
14825/**
14826 * lpfc_sli4_resume_rpi - Remove the rpi bitmask region
14827 * @phba: pointer to lpfc hba data structure.
14828 *
14829 * This routine is invoked to remove the memory region that
14830 * provided rpi via a bitmask.
14831 **/
14832int
James Smart6b5151f2012-01-18 16:24:06 -050014833lpfc_sli4_resume_rpi(struct lpfc_nodelist *ndlp,
14834 void (*cmpl)(struct lpfc_hba *, LPFC_MBOXQ_t *), void *arg)
James Smart6fb120a2009-05-22 14:52:59 -040014835{
14836 LPFC_MBOXQ_t *mboxq;
14837 struct lpfc_hba *phba = ndlp->phba;
14838 int rc;
14839
14840 /* The port is notified of the header region via a mailbox command. */
14841 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14842 if (!mboxq)
14843 return -ENOMEM;
14844
14845 /* Post all rpi memory regions to the port. */
14846 lpfc_resume_rpi(mboxq, ndlp);
James Smart6b5151f2012-01-18 16:24:06 -050014847 if (cmpl) {
14848 mboxq->mbox_cmpl = cmpl;
14849 mboxq->context1 = arg;
14850 mboxq->context2 = ndlp;
James Smart72859902012-01-18 16:25:38 -050014851 } else
14852 mboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart6b5151f2012-01-18 16:24:06 -050014853 mboxq->vport = ndlp->vport;
James Smart6fb120a2009-05-22 14:52:59 -040014854 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
14855 if (rc == MBX_NOT_FINISHED) {
14856 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14857 "2010 Resume RPI Mailbox failed "
14858 "status %d, mbxStatus x%x\n", rc,
14859 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
14860 mempool_free(mboxq, phba->mbox_mem_pool);
14861 return -EIO;
14862 }
14863 return 0;
14864}
14865
14866/**
14867 * lpfc_sli4_init_vpi - Initialize a vpi with the port
James Smart76a95d72010-11-20 23:11:48 -050014868 * @vport: Pointer to the vport for which the vpi is being initialized
James Smart6fb120a2009-05-22 14:52:59 -040014869 *
James Smart76a95d72010-11-20 23:11:48 -050014870 * This routine is invoked to activate a vpi with the port.
James Smart6fb120a2009-05-22 14:52:59 -040014871 *
14872 * Returns:
14873 * 0 success
14874 * -Evalue otherwise
14875 **/
14876int
James Smart76a95d72010-11-20 23:11:48 -050014877lpfc_sli4_init_vpi(struct lpfc_vport *vport)
James Smart6fb120a2009-05-22 14:52:59 -040014878{
14879 LPFC_MBOXQ_t *mboxq;
14880 int rc = 0;
James Smart6a9c52c2009-10-02 15:16:51 -040014881 int retval = MBX_SUCCESS;
James Smart6fb120a2009-05-22 14:52:59 -040014882 uint32_t mbox_tmo;
James Smart76a95d72010-11-20 23:11:48 -050014883 struct lpfc_hba *phba = vport->phba;
James Smart6fb120a2009-05-22 14:52:59 -040014884 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14885 if (!mboxq)
14886 return -ENOMEM;
James Smart76a95d72010-11-20 23:11:48 -050014887 lpfc_init_vpi(phba, mboxq, vport->vpi);
James Smarta183a152011-10-10 21:32:43 -040014888 mbox_tmo = lpfc_mbox_tmo_val(phba, mboxq);
James Smart6fb120a2009-05-22 14:52:59 -040014889 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
James Smart6fb120a2009-05-22 14:52:59 -040014890 if (rc != MBX_SUCCESS) {
James Smart76a95d72010-11-20 23:11:48 -050014891 lpfc_printf_vlog(vport, KERN_ERR, LOG_SLI,
James Smart6fb120a2009-05-22 14:52:59 -040014892 "2022 INIT VPI Mailbox failed "
14893 "status %d, mbxStatus x%x\n", rc,
14894 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
James Smart6a9c52c2009-10-02 15:16:51 -040014895 retval = -EIO;
James Smart6fb120a2009-05-22 14:52:59 -040014896 }
James Smart6a9c52c2009-10-02 15:16:51 -040014897 if (rc != MBX_TIMEOUT)
James Smart76a95d72010-11-20 23:11:48 -050014898 mempool_free(mboxq, vport->phba->mbox_mem_pool);
James Smart6a9c52c2009-10-02 15:16:51 -040014899
14900 return retval;
James Smart6fb120a2009-05-22 14:52:59 -040014901}
14902
14903/**
14904 * lpfc_mbx_cmpl_add_fcf_record - add fcf mbox completion handler.
14905 * @phba: pointer to lpfc hba data structure.
14906 * @mboxq: Pointer to mailbox object.
14907 *
14908 * This routine is invoked to manually add a single FCF record. The caller
14909 * must pass a completely initialized FCF_Record. This routine takes
14910 * care of the nonembedded mailbox operations.
14911 **/
14912static void
14913lpfc_mbx_cmpl_add_fcf_record(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
14914{
14915 void *virt_addr;
14916 union lpfc_sli4_cfg_shdr *shdr;
14917 uint32_t shdr_status, shdr_add_status;
14918
14919 virt_addr = mboxq->sge_array->addr[0];
14920 /* The IOCTL status is embedded in the mailbox subheader. */
14921 shdr = (union lpfc_sli4_cfg_shdr *) virt_addr;
14922 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14923 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14924
14925 if ((shdr_status || shdr_add_status) &&
14926 (shdr_status != STATUS_FCF_IN_USE))
14927 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14928 "2558 ADD_FCF_RECORD mailbox failed with "
14929 "status x%x add_status x%x\n",
14930 shdr_status, shdr_add_status);
14931
14932 lpfc_sli4_mbox_cmd_free(phba, mboxq);
14933}
14934
14935/**
14936 * lpfc_sli4_add_fcf_record - Manually add an FCF Record.
14937 * @phba: pointer to lpfc hba data structure.
14938 * @fcf_record: pointer to the initialized fcf record to add.
14939 *
14940 * This routine is invoked to manually add a single FCF record. The caller
14941 * must pass a completely initialized FCF_Record. This routine takes
14942 * care of the nonembedded mailbox operations.
14943 **/
14944int
14945lpfc_sli4_add_fcf_record(struct lpfc_hba *phba, struct fcf_record *fcf_record)
14946{
14947 int rc = 0;
14948 LPFC_MBOXQ_t *mboxq;
14949 uint8_t *bytep;
14950 void *virt_addr;
14951 dma_addr_t phys_addr;
14952 struct lpfc_mbx_sge sge;
14953 uint32_t alloc_len, req_len;
14954 uint32_t fcfindex;
14955
14956 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14957 if (!mboxq) {
14958 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14959 "2009 Failed to allocate mbox for ADD_FCF cmd\n");
14960 return -ENOMEM;
14961 }
14962
14963 req_len = sizeof(struct fcf_record) + sizeof(union lpfc_sli4_cfg_shdr) +
14964 sizeof(uint32_t);
14965
14966 /* Allocate DMA memory and set up the non-embedded mailbox command */
14967 alloc_len = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
14968 LPFC_MBOX_OPCODE_FCOE_ADD_FCF,
14969 req_len, LPFC_SLI4_MBX_NEMBED);
14970 if (alloc_len < req_len) {
14971 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14972 "2523 Allocated DMA memory size (x%x) is "
14973 "less than the requested DMA memory "
14974 "size (x%x)\n", alloc_len, req_len);
14975 lpfc_sli4_mbox_cmd_free(phba, mboxq);
14976 return -ENOMEM;
14977 }
14978
14979 /*
14980 * Get the first SGE entry from the non-embedded DMA memory. This
14981 * routine only uses a single SGE.
14982 */
14983 lpfc_sli4_mbx_sge_get(mboxq, 0, &sge);
14984 phys_addr = getPaddr(sge.pa_hi, sge.pa_lo);
James Smart6fb120a2009-05-22 14:52:59 -040014985 virt_addr = mboxq->sge_array->addr[0];
14986 /*
14987 * Configure the FCF record for FCFI 0. This is the driver's
14988 * hardcoded default and gets used in nonFIP mode.
14989 */
14990 fcfindex = bf_get(lpfc_fcf_record_fcf_index, fcf_record);
14991 bytep = virt_addr + sizeof(union lpfc_sli4_cfg_shdr);
14992 lpfc_sli_pcimem_bcopy(&fcfindex, bytep, sizeof(uint32_t));
14993
14994 /*
14995 * Copy the fcf_index and the FCF Record Data. The data starts after
14996 * the FCoE header plus word10. The data copy needs to be endian
14997 * correct.
14998 */
14999 bytep += sizeof(uint32_t);
15000 lpfc_sli_pcimem_bcopy(fcf_record, bytep, sizeof(struct fcf_record));
15001 mboxq->vport = phba->pport;
15002 mboxq->mbox_cmpl = lpfc_mbx_cmpl_add_fcf_record;
15003 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
15004 if (rc == MBX_NOT_FINISHED) {
15005 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15006 "2515 ADD_FCF_RECORD mailbox failed with "
15007 "status 0x%x\n", rc);
15008 lpfc_sli4_mbox_cmd_free(phba, mboxq);
15009 rc = -EIO;
15010 } else
15011 rc = 0;
15012
15013 return rc;
15014}
15015
15016/**
15017 * lpfc_sli4_build_dflt_fcf_record - Build the driver's default FCF Record.
15018 * @phba: pointer to lpfc hba data structure.
15019 * @fcf_record: pointer to the fcf record to write the default data.
15020 * @fcf_index: FCF table entry index.
15021 *
15022 * This routine is invoked to build the driver's default FCF record. The
15023 * values used are hardcoded. This routine handles memory initialization.
15024 *
15025 **/
15026void
15027lpfc_sli4_build_dflt_fcf_record(struct lpfc_hba *phba,
15028 struct fcf_record *fcf_record,
15029 uint16_t fcf_index)
15030{
15031 memset(fcf_record, 0, sizeof(struct fcf_record));
15032 fcf_record->max_rcv_size = LPFC_FCOE_MAX_RCV_SIZE;
15033 fcf_record->fka_adv_period = LPFC_FCOE_FKA_ADV_PER;
15034 fcf_record->fip_priority = LPFC_FCOE_FIP_PRIORITY;
15035 bf_set(lpfc_fcf_record_mac_0, fcf_record, phba->fc_map[0]);
15036 bf_set(lpfc_fcf_record_mac_1, fcf_record, phba->fc_map[1]);
15037 bf_set(lpfc_fcf_record_mac_2, fcf_record, phba->fc_map[2]);
15038 bf_set(lpfc_fcf_record_mac_3, fcf_record, LPFC_FCOE_FCF_MAC3);
15039 bf_set(lpfc_fcf_record_mac_4, fcf_record, LPFC_FCOE_FCF_MAC4);
15040 bf_set(lpfc_fcf_record_mac_5, fcf_record, LPFC_FCOE_FCF_MAC5);
15041 bf_set(lpfc_fcf_record_fc_map_0, fcf_record, phba->fc_map[0]);
15042 bf_set(lpfc_fcf_record_fc_map_1, fcf_record, phba->fc_map[1]);
15043 bf_set(lpfc_fcf_record_fc_map_2, fcf_record, phba->fc_map[2]);
15044 bf_set(lpfc_fcf_record_fcf_valid, fcf_record, 1);
James Smart0c287582009-06-10 17:22:56 -040015045 bf_set(lpfc_fcf_record_fcf_avail, fcf_record, 1);
James Smart6fb120a2009-05-22 14:52:59 -040015046 bf_set(lpfc_fcf_record_fcf_index, fcf_record, fcf_index);
15047 bf_set(lpfc_fcf_record_mac_addr_prov, fcf_record,
15048 LPFC_FCF_FPMA | LPFC_FCF_SPMA);
15049 /* Set the VLAN bit map */
15050 if (phba->valid_vlan) {
15051 fcf_record->vlan_bitmap[phba->vlan_id / 8]
15052 = 1 << (phba->vlan_id % 8);
15053 }
15054}
15055
15056/**
James Smart0c9ab6f2010-02-26 14:15:57 -050015057 * lpfc_sli4_fcf_scan_read_fcf_rec - Read hba fcf record for fcf scan.
James Smart6fb120a2009-05-22 14:52:59 -040015058 * @phba: pointer to lpfc hba data structure.
15059 * @fcf_index: FCF table entry offset.
15060 *
James Smart0c9ab6f2010-02-26 14:15:57 -050015061 * This routine is invoked to scan the entire FCF table by reading FCF
15062 * record and processing it one at a time starting from the @fcf_index
15063 * for initial FCF discovery or fast FCF failover rediscovery.
15064 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030015065 * Return 0 if the mailbox command is submitted successfully, none 0
James Smart0c9ab6f2010-02-26 14:15:57 -050015066 * otherwise.
James Smart6fb120a2009-05-22 14:52:59 -040015067 **/
15068int
James Smart0c9ab6f2010-02-26 14:15:57 -050015069lpfc_sli4_fcf_scan_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
James Smart6fb120a2009-05-22 14:52:59 -040015070{
15071 int rc = 0, error;
15072 LPFC_MBOXQ_t *mboxq;
James Smart6fb120a2009-05-22 14:52:59 -040015073
James Smart32b97932009-07-19 10:01:21 -040015074 phba->fcoe_eventtag_at_fcf_scan = phba->fcoe_eventtag;
James Smart80c17842012-03-01 22:35:45 -050015075 phba->fcoe_cvl_eventtag_attn = phba->fcoe_cvl_eventtag;
James Smart6fb120a2009-05-22 14:52:59 -040015076 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15077 if (!mboxq) {
15078 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15079 "2000 Failed to allocate mbox for "
15080 "READ_FCF cmd\n");
James Smart4d9ab992009-10-02 15:16:39 -040015081 error = -ENOMEM;
James Smart0c9ab6f2010-02-26 14:15:57 -050015082 goto fail_fcf_scan;
James Smart6fb120a2009-05-22 14:52:59 -040015083 }
James Smartecfd03c2010-02-12 14:41:27 -050015084 /* Construct the read FCF record mailbox command */
James Smart0c9ab6f2010-02-26 14:15:57 -050015085 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
James Smartecfd03c2010-02-12 14:41:27 -050015086 if (rc) {
15087 error = -EINVAL;
James Smart0c9ab6f2010-02-26 14:15:57 -050015088 goto fail_fcf_scan;
James Smart6fb120a2009-05-22 14:52:59 -040015089 }
James Smartecfd03c2010-02-12 14:41:27 -050015090 /* Issue the mailbox command asynchronously */
James Smart6fb120a2009-05-22 14:52:59 -040015091 mboxq->vport = phba->pport;
James Smart0c9ab6f2010-02-26 14:15:57 -050015092 mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_scan_read_fcf_rec;
James Smarta93ff372010-10-22 11:06:08 -040015093
15094 spin_lock_irq(&phba->hbalock);
15095 phba->hba_flag |= FCF_TS_INPROG;
15096 spin_unlock_irq(&phba->hbalock);
15097
James Smart6fb120a2009-05-22 14:52:59 -040015098 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
James Smartecfd03c2010-02-12 14:41:27 -050015099 if (rc == MBX_NOT_FINISHED)
James Smart6fb120a2009-05-22 14:52:59 -040015100 error = -EIO;
James Smartecfd03c2010-02-12 14:41:27 -050015101 else {
James Smart38b92ef2010-08-04 16:11:39 -040015102 /* Reset eligible FCF count for new scan */
15103 if (fcf_index == LPFC_FCOE_FCF_GET_FIRST)
James Smart999d8132010-03-15 11:24:56 -040015104 phba->fcf.eligible_fcf_cnt = 0;
James Smart6fb120a2009-05-22 14:52:59 -040015105 error = 0;
James Smart32b97932009-07-19 10:01:21 -040015106 }
James Smart0c9ab6f2010-02-26 14:15:57 -050015107fail_fcf_scan:
James Smart4d9ab992009-10-02 15:16:39 -040015108 if (error) {
15109 if (mboxq)
15110 lpfc_sli4_mbox_cmd_free(phba, mboxq);
James Smarta93ff372010-10-22 11:06:08 -040015111 /* FCF scan failed, clear FCF_TS_INPROG flag */
James Smart4d9ab992009-10-02 15:16:39 -040015112 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -040015113 phba->hba_flag &= ~FCF_TS_INPROG;
James Smart4d9ab992009-10-02 15:16:39 -040015114 spin_unlock_irq(&phba->hbalock);
15115 }
James Smart6fb120a2009-05-22 14:52:59 -040015116 return error;
15117}
James Smarta0c87cb2009-07-19 10:01:10 -040015118
15119/**
James Smarta93ff372010-10-22 11:06:08 -040015120 * lpfc_sli4_fcf_rr_read_fcf_rec - Read hba fcf record for roundrobin fcf.
James Smart0c9ab6f2010-02-26 14:15:57 -050015121 * @phba: pointer to lpfc hba data structure.
15122 * @fcf_index: FCF table entry offset.
15123 *
15124 * This routine is invoked to read an FCF record indicated by @fcf_index
James Smarta93ff372010-10-22 11:06:08 -040015125 * and to use it for FLOGI roundrobin FCF failover.
James Smart0c9ab6f2010-02-26 14:15:57 -050015126 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030015127 * Return 0 if the mailbox command is submitted successfully, none 0
James Smart0c9ab6f2010-02-26 14:15:57 -050015128 * otherwise.
15129 **/
15130int
15131lpfc_sli4_fcf_rr_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
15132{
15133 int rc = 0, error;
15134 LPFC_MBOXQ_t *mboxq;
15135
15136 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15137 if (!mboxq) {
15138 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
15139 "2763 Failed to allocate mbox for "
15140 "READ_FCF cmd\n");
15141 error = -ENOMEM;
15142 goto fail_fcf_read;
15143 }
15144 /* Construct the read FCF record mailbox command */
15145 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
15146 if (rc) {
15147 error = -EINVAL;
15148 goto fail_fcf_read;
15149 }
15150 /* Issue the mailbox command asynchronously */
15151 mboxq->vport = phba->pport;
15152 mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_rr_read_fcf_rec;
15153 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
15154 if (rc == MBX_NOT_FINISHED)
15155 error = -EIO;
15156 else
15157 error = 0;
15158
15159fail_fcf_read:
15160 if (error && mboxq)
15161 lpfc_sli4_mbox_cmd_free(phba, mboxq);
15162 return error;
15163}
15164
15165/**
15166 * lpfc_sli4_read_fcf_rec - Read hba fcf record for update eligible fcf bmask.
15167 * @phba: pointer to lpfc hba data structure.
15168 * @fcf_index: FCF table entry offset.
15169 *
15170 * This routine is invoked to read an FCF record indicated by @fcf_index to
James Smarta93ff372010-10-22 11:06:08 -040015171 * determine whether it's eligible for FLOGI roundrobin failover list.
James Smart0c9ab6f2010-02-26 14:15:57 -050015172 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030015173 * Return 0 if the mailbox command is submitted successfully, none 0
James Smart0c9ab6f2010-02-26 14:15:57 -050015174 * otherwise.
15175 **/
15176int
15177lpfc_sli4_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
15178{
15179 int rc = 0, error;
15180 LPFC_MBOXQ_t *mboxq;
15181
15182 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15183 if (!mboxq) {
15184 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
15185 "2758 Failed to allocate mbox for "
15186 "READ_FCF cmd\n");
15187 error = -ENOMEM;
15188 goto fail_fcf_read;
15189 }
15190 /* Construct the read FCF record mailbox command */
15191 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
15192 if (rc) {
15193 error = -EINVAL;
15194 goto fail_fcf_read;
15195 }
15196 /* Issue the mailbox command asynchronously */
15197 mboxq->vport = phba->pport;
15198 mboxq->mbox_cmpl = lpfc_mbx_cmpl_read_fcf_rec;
15199 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
15200 if (rc == MBX_NOT_FINISHED)
15201 error = -EIO;
15202 else
15203 error = 0;
15204
15205fail_fcf_read:
15206 if (error && mboxq)
15207 lpfc_sli4_mbox_cmd_free(phba, mboxq);
15208 return error;
15209}
15210
15211/**
James Smart7d791df2011-07-22 18:37:52 -040015212 * lpfc_check_next_fcf_pri
15213 * phba pointer to the lpfc_hba struct for this port.
15214 * This routine is called from the lpfc_sli4_fcf_rr_next_index_get
15215 * routine when the rr_bmask is empty. The FCF indecies are put into the
15216 * rr_bmask based on their priority level. Starting from the highest priority
15217 * to the lowest. The most likely FCF candidate will be in the highest
15218 * priority group. When this routine is called it searches the fcf_pri list for
15219 * next lowest priority group and repopulates the rr_bmask with only those
15220 * fcf_indexes.
15221 * returns:
15222 * 1=success 0=failure
15223 **/
15224int
15225lpfc_check_next_fcf_pri_level(struct lpfc_hba *phba)
15226{
15227 uint16_t next_fcf_pri;
15228 uint16_t last_index;
15229 struct lpfc_fcf_pri *fcf_pri;
15230 int rc;
15231 int ret = 0;
15232
15233 last_index = find_first_bit(phba->fcf.fcf_rr_bmask,
15234 LPFC_SLI4_FCF_TBL_INDX_MAX);
15235 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
15236 "3060 Last IDX %d\n", last_index);
15237 if (list_empty(&phba->fcf.fcf_pri_list)) {
15238 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
15239 "3061 Last IDX %d\n", last_index);
15240 return 0; /* Empty rr list */
15241 }
15242 next_fcf_pri = 0;
15243 /*
15244 * Clear the rr_bmask and set all of the bits that are at this
15245 * priority.
15246 */
15247 memset(phba->fcf.fcf_rr_bmask, 0,
15248 sizeof(*phba->fcf.fcf_rr_bmask));
15249 spin_lock_irq(&phba->hbalock);
15250 list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
15251 if (fcf_pri->fcf_rec.flag & LPFC_FCF_FLOGI_FAILED)
15252 continue;
15253 /*
15254 * the 1st priority that has not FLOGI failed
15255 * will be the highest.
15256 */
15257 if (!next_fcf_pri)
15258 next_fcf_pri = fcf_pri->fcf_rec.priority;
15259 spin_unlock_irq(&phba->hbalock);
15260 if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
15261 rc = lpfc_sli4_fcf_rr_index_set(phba,
15262 fcf_pri->fcf_rec.fcf_index);
15263 if (rc)
15264 return 0;
15265 }
15266 spin_lock_irq(&phba->hbalock);
15267 }
15268 /*
15269 * if next_fcf_pri was not set above and the list is not empty then
15270 * we have failed flogis on all of them. So reset flogi failed
15271 * and start at the begining.
15272 */
15273 if (!next_fcf_pri && !list_empty(&phba->fcf.fcf_pri_list)) {
15274 list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
15275 fcf_pri->fcf_rec.flag &= ~LPFC_FCF_FLOGI_FAILED;
15276 /*
15277 * the 1st priority that has not FLOGI failed
15278 * will be the highest.
15279 */
15280 if (!next_fcf_pri)
15281 next_fcf_pri = fcf_pri->fcf_rec.priority;
15282 spin_unlock_irq(&phba->hbalock);
15283 if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
15284 rc = lpfc_sli4_fcf_rr_index_set(phba,
15285 fcf_pri->fcf_rec.fcf_index);
15286 if (rc)
15287 return 0;
15288 }
15289 spin_lock_irq(&phba->hbalock);
15290 }
15291 } else
15292 ret = 1;
15293 spin_unlock_irq(&phba->hbalock);
15294
15295 return ret;
15296}
15297/**
James Smart0c9ab6f2010-02-26 14:15:57 -050015298 * lpfc_sli4_fcf_rr_next_index_get - Get next eligible fcf record index
15299 * @phba: pointer to lpfc hba data structure.
15300 *
15301 * This routine is to get the next eligible FCF record index in a round
15302 * robin fashion. If the next eligible FCF record index equals to the
James Smarta93ff372010-10-22 11:06:08 -040015303 * initial roundrobin FCF record index, LPFC_FCOE_FCF_NEXT_NONE (0xFFFF)
James Smart0c9ab6f2010-02-26 14:15:57 -050015304 * shall be returned, otherwise, the next eligible FCF record's index
15305 * shall be returned.
15306 **/
15307uint16_t
15308lpfc_sli4_fcf_rr_next_index_get(struct lpfc_hba *phba)
15309{
15310 uint16_t next_fcf_index;
15311
James Smart3804dc82010-07-14 15:31:37 -040015312 /* Search start from next bit of currently registered FCF index */
James Smart7d791df2011-07-22 18:37:52 -040015313next_priority:
James Smart3804dc82010-07-14 15:31:37 -040015314 next_fcf_index = (phba->fcf.current_rec.fcf_indx + 1) %
15315 LPFC_SLI4_FCF_TBL_INDX_MAX;
James Smart0c9ab6f2010-02-26 14:15:57 -050015316 next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
15317 LPFC_SLI4_FCF_TBL_INDX_MAX,
James Smart3804dc82010-07-14 15:31:37 -040015318 next_fcf_index);
15319
James Smart0c9ab6f2010-02-26 14:15:57 -050015320 /* Wrap around condition on phba->fcf.fcf_rr_bmask */
James Smart7d791df2011-07-22 18:37:52 -040015321 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
15322 /*
15323 * If we have wrapped then we need to clear the bits that
15324 * have been tested so that we can detect when we should
15325 * change the priority level.
15326 */
James Smart0c9ab6f2010-02-26 14:15:57 -050015327 next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
15328 LPFC_SLI4_FCF_TBL_INDX_MAX, 0);
James Smart7d791df2011-07-22 18:37:52 -040015329 }
15330
James Smart0c9ab6f2010-02-26 14:15:57 -050015331
James Smart3804dc82010-07-14 15:31:37 -040015332 /* Check roundrobin failover list empty condition */
James Smart7d791df2011-07-22 18:37:52 -040015333 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX ||
15334 next_fcf_index == phba->fcf.current_rec.fcf_indx) {
15335 /*
15336 * If next fcf index is not found check if there are lower
15337 * Priority level fcf's in the fcf_priority list.
15338 * Set up the rr_bmask with all of the avaiable fcf bits
15339 * at that level and continue the selection process.
15340 */
15341 if (lpfc_check_next_fcf_pri_level(phba))
15342 goto next_priority;
James Smart3804dc82010-07-14 15:31:37 -040015343 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
15344 "2844 No roundrobin failover FCF available\n");
James Smart7d791df2011-07-22 18:37:52 -040015345 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX)
15346 return LPFC_FCOE_FCF_NEXT_NONE;
15347 else {
15348 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
15349 "3063 Only FCF available idx %d, flag %x\n",
15350 next_fcf_index,
15351 phba->fcf.fcf_pri[next_fcf_index].fcf_rec.flag);
15352 return next_fcf_index;
15353 }
James Smart3804dc82010-07-14 15:31:37 -040015354 }
15355
James Smart7d791df2011-07-22 18:37:52 -040015356 if (next_fcf_index < LPFC_SLI4_FCF_TBL_INDX_MAX &&
15357 phba->fcf.fcf_pri[next_fcf_index].fcf_rec.flag &
15358 LPFC_FCF_FLOGI_FAILED)
15359 goto next_priority;
15360
James Smart3804dc82010-07-14 15:31:37 -040015361 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040015362 "2845 Get next roundrobin failover FCF (x%x)\n",
15363 next_fcf_index);
15364
James Smart0c9ab6f2010-02-26 14:15:57 -050015365 return next_fcf_index;
15366}
15367
15368/**
15369 * lpfc_sli4_fcf_rr_index_set - Set bmask with eligible fcf record index
15370 * @phba: pointer to lpfc hba data structure.
15371 *
15372 * This routine sets the FCF record index in to the eligible bmask for
James Smarta93ff372010-10-22 11:06:08 -040015373 * roundrobin failover search. It checks to make sure that the index
James Smart0c9ab6f2010-02-26 14:15:57 -050015374 * does not go beyond the range of the driver allocated bmask dimension
15375 * before setting the bit.
15376 *
15377 * Returns 0 if the index bit successfully set, otherwise, it returns
15378 * -EINVAL.
15379 **/
15380int
15381lpfc_sli4_fcf_rr_index_set(struct lpfc_hba *phba, uint16_t fcf_index)
15382{
15383 if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
15384 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040015385 "2610 FCF (x%x) reached driver's book "
15386 "keeping dimension:x%x\n",
James Smart0c9ab6f2010-02-26 14:15:57 -050015387 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
15388 return -EINVAL;
15389 }
15390 /* Set the eligible FCF record index bmask */
15391 set_bit(fcf_index, phba->fcf.fcf_rr_bmask);
15392
James Smart3804dc82010-07-14 15:31:37 -040015393 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040015394 "2790 Set FCF (x%x) to roundrobin FCF failover "
James Smart3804dc82010-07-14 15:31:37 -040015395 "bmask\n", fcf_index);
15396
James Smart0c9ab6f2010-02-26 14:15:57 -050015397 return 0;
15398}
15399
15400/**
James Smart3804dc82010-07-14 15:31:37 -040015401 * lpfc_sli4_fcf_rr_index_clear - Clear bmask from eligible fcf record index
James Smart0c9ab6f2010-02-26 14:15:57 -050015402 * @phba: pointer to lpfc hba data structure.
15403 *
15404 * This routine clears the FCF record index from the eligible bmask for
James Smarta93ff372010-10-22 11:06:08 -040015405 * roundrobin failover search. It checks to make sure that the index
James Smart0c9ab6f2010-02-26 14:15:57 -050015406 * does not go beyond the range of the driver allocated bmask dimension
15407 * before clearing the bit.
15408 **/
15409void
15410lpfc_sli4_fcf_rr_index_clear(struct lpfc_hba *phba, uint16_t fcf_index)
15411{
James Smart7d791df2011-07-22 18:37:52 -040015412 struct lpfc_fcf_pri *fcf_pri;
James Smart0c9ab6f2010-02-26 14:15:57 -050015413 if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
15414 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040015415 "2762 FCF (x%x) reached driver's book "
15416 "keeping dimension:x%x\n",
James Smart0c9ab6f2010-02-26 14:15:57 -050015417 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
15418 return;
15419 }
15420 /* Clear the eligible FCF record index bmask */
James Smart7d791df2011-07-22 18:37:52 -040015421 spin_lock_irq(&phba->hbalock);
15422 list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
15423 if (fcf_pri->fcf_rec.fcf_index == fcf_index) {
15424 list_del_init(&fcf_pri->list);
15425 break;
15426 }
15427 }
15428 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -050015429 clear_bit(fcf_index, phba->fcf.fcf_rr_bmask);
James Smart3804dc82010-07-14 15:31:37 -040015430
15431 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040015432 "2791 Clear FCF (x%x) from roundrobin failover "
James Smart3804dc82010-07-14 15:31:37 -040015433 "bmask\n", fcf_index);
James Smart0c9ab6f2010-02-26 14:15:57 -050015434}
15435
15436/**
James Smartecfd03c2010-02-12 14:41:27 -050015437 * lpfc_mbx_cmpl_redisc_fcf_table - completion routine for rediscover FCF table
15438 * @phba: pointer to lpfc hba data structure.
15439 *
15440 * This routine is the completion routine for the rediscover FCF table mailbox
15441 * command. If the mailbox command returned failure, it will try to stop the
15442 * FCF rediscover wait timer.
15443 **/
15444void
15445lpfc_mbx_cmpl_redisc_fcf_table(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
15446{
15447 struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
15448 uint32_t shdr_status, shdr_add_status;
15449
15450 redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
15451
15452 shdr_status = bf_get(lpfc_mbox_hdr_status,
15453 &redisc_fcf->header.cfg_shdr.response);
15454 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
15455 &redisc_fcf->header.cfg_shdr.response);
15456 if (shdr_status || shdr_add_status) {
James Smart0c9ab6f2010-02-26 14:15:57 -050015457 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smartecfd03c2010-02-12 14:41:27 -050015458 "2746 Requesting for FCF rediscovery failed "
15459 "status x%x add_status x%x\n",
15460 shdr_status, shdr_add_status);
James Smart0c9ab6f2010-02-26 14:15:57 -050015461 if (phba->fcf.fcf_flag & FCF_ACVL_DISC) {
James Smartfc2b9892010-02-26 14:15:29 -050015462 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -050015463 phba->fcf.fcf_flag &= ~FCF_ACVL_DISC;
James Smartfc2b9892010-02-26 14:15:29 -050015464 spin_unlock_irq(&phba->hbalock);
15465 /*
15466 * CVL event triggered FCF rediscover request failed,
15467 * last resort to re-try current registered FCF entry.
15468 */
15469 lpfc_retry_pport_discovery(phba);
15470 } else {
15471 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -050015472 phba->fcf.fcf_flag &= ~FCF_DEAD_DISC;
James Smartfc2b9892010-02-26 14:15:29 -050015473 spin_unlock_irq(&phba->hbalock);
15474 /*
15475 * DEAD FCF event triggered FCF rediscover request
15476 * failed, last resort to fail over as a link down
15477 * to FCF registration.
15478 */
15479 lpfc_sli4_fcf_dead_failthrough(phba);
15480 }
James Smart0c9ab6f2010-02-26 14:15:57 -050015481 } else {
15482 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040015483 "2775 Start FCF rediscover quiescent timer\n");
James Smartecfd03c2010-02-12 14:41:27 -050015484 /*
15485 * Start FCF rediscovery wait timer for pending FCF
15486 * before rescan FCF record table.
15487 */
15488 lpfc_fcf_redisc_wait_start_timer(phba);
James Smart0c9ab6f2010-02-26 14:15:57 -050015489 }
James Smartecfd03c2010-02-12 14:41:27 -050015490
15491 mempool_free(mbox, phba->mbox_mem_pool);
15492}
15493
15494/**
James Smart3804dc82010-07-14 15:31:37 -040015495 * lpfc_sli4_redisc_fcf_table - Request to rediscover entire FCF table by port.
James Smartecfd03c2010-02-12 14:41:27 -050015496 * @phba: pointer to lpfc hba data structure.
15497 *
15498 * This routine is invoked to request for rediscovery of the entire FCF table
15499 * by the port.
15500 **/
15501int
15502lpfc_sli4_redisc_fcf_table(struct lpfc_hba *phba)
15503{
15504 LPFC_MBOXQ_t *mbox;
15505 struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
15506 int rc, length;
15507
James Smart0c9ab6f2010-02-26 14:15:57 -050015508 /* Cancel retry delay timers to all vports before FCF rediscover */
15509 lpfc_cancel_all_vport_retry_delay_timer(phba);
15510
James Smartecfd03c2010-02-12 14:41:27 -050015511 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15512 if (!mbox) {
15513 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15514 "2745 Failed to allocate mbox for "
15515 "requesting FCF rediscover.\n");
15516 return -ENOMEM;
15517 }
15518
15519 length = (sizeof(struct lpfc_mbx_redisc_fcf_tbl) -
15520 sizeof(struct lpfc_sli4_cfg_mhdr));
15521 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15522 LPFC_MBOX_OPCODE_FCOE_REDISCOVER_FCF,
15523 length, LPFC_SLI4_MBX_EMBED);
15524
15525 redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
15526 /* Set count to 0 for invalidating the entire FCF database */
15527 bf_set(lpfc_mbx_redisc_fcf_count, redisc_fcf, 0);
15528
15529 /* Issue the mailbox command asynchronously */
15530 mbox->vport = phba->pport;
15531 mbox->mbox_cmpl = lpfc_mbx_cmpl_redisc_fcf_table;
15532 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
15533
15534 if (rc == MBX_NOT_FINISHED) {
15535 mempool_free(mbox, phba->mbox_mem_pool);
15536 return -EIO;
15537 }
15538 return 0;
15539}
15540
15541/**
James Smartfc2b9892010-02-26 14:15:29 -050015542 * lpfc_sli4_fcf_dead_failthrough - Failthrough routine to fcf dead event
15543 * @phba: pointer to lpfc hba data structure.
15544 *
15545 * This function is the failover routine as a last resort to the FCF DEAD
15546 * event when driver failed to perform fast FCF failover.
15547 **/
15548void
15549lpfc_sli4_fcf_dead_failthrough(struct lpfc_hba *phba)
15550{
15551 uint32_t link_state;
15552
15553 /*
15554 * Last resort as FCF DEAD event failover will treat this as
15555 * a link down, but save the link state because we don't want
15556 * it to be changed to Link Down unless it is already down.
15557 */
15558 link_state = phba->link_state;
15559 lpfc_linkdown(phba);
15560 phba->link_state = link_state;
15561
15562 /* Unregister FCF if no devices connected to it */
15563 lpfc_unregister_unused_fcf(phba);
15564}
15565
15566/**
James Smart026abb82011-12-13 13:20:45 -050015567 * lpfc_sli_get_config_region23 - Get sli3 port region 23 data.
James Smarta0c87cb2009-07-19 10:01:10 -040015568 * @phba: pointer to lpfc hba data structure.
James Smart026abb82011-12-13 13:20:45 -050015569 * @rgn23_data: pointer to configure region 23 data.
James Smarta0c87cb2009-07-19 10:01:10 -040015570 *
James Smart026abb82011-12-13 13:20:45 -050015571 * This function gets SLI3 port configure region 23 data through memory dump
15572 * mailbox command. When it successfully retrieves data, the size of the data
15573 * will be returned, otherwise, 0 will be returned.
James Smarta0c87cb2009-07-19 10:01:10 -040015574 **/
James Smart026abb82011-12-13 13:20:45 -050015575static uint32_t
15576lpfc_sli_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)
James Smarta0c87cb2009-07-19 10:01:10 -040015577{
15578 LPFC_MBOXQ_t *pmb = NULL;
15579 MAILBOX_t *mb;
James Smart026abb82011-12-13 13:20:45 -050015580 uint32_t offset = 0;
James Smarta0c87cb2009-07-19 10:01:10 -040015581 int rc;
15582
James Smart026abb82011-12-13 13:20:45 -050015583 if (!rgn23_data)
15584 return 0;
15585
James Smarta0c87cb2009-07-19 10:01:10 -040015586 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15587 if (!pmb) {
15588 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart026abb82011-12-13 13:20:45 -050015589 "2600 failed to allocate mailbox memory\n");
15590 return 0;
James Smarta0c87cb2009-07-19 10:01:10 -040015591 }
15592 mb = &pmb->u.mb;
15593
James Smarta0c87cb2009-07-19 10:01:10 -040015594 do {
15595 lpfc_dump_mem(phba, pmb, offset, DMP_REGION_23);
15596 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
15597
15598 if (rc != MBX_SUCCESS) {
15599 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smart026abb82011-12-13 13:20:45 -050015600 "2601 failed to read config "
15601 "region 23, rc 0x%x Status 0x%x\n",
15602 rc, mb->mbxStatus);
James Smarta0c87cb2009-07-19 10:01:10 -040015603 mb->un.varDmp.word_cnt = 0;
15604 }
15605 /*
15606 * dump mem may return a zero when finished or we got a
15607 * mailbox error, either way we are done.
15608 */
15609 if (mb->un.varDmp.word_cnt == 0)
15610 break;
15611 if (mb->un.varDmp.word_cnt > DMP_RGN23_SIZE - offset)
15612 mb->un.varDmp.word_cnt = DMP_RGN23_SIZE - offset;
15613
15614 lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
James Smart026abb82011-12-13 13:20:45 -050015615 rgn23_data + offset,
15616 mb->un.varDmp.word_cnt);
James Smarta0c87cb2009-07-19 10:01:10 -040015617 offset += mb->un.varDmp.word_cnt;
15618 } while (mb->un.varDmp.word_cnt && offset < DMP_RGN23_SIZE);
15619
James Smart026abb82011-12-13 13:20:45 -050015620 mempool_free(pmb, phba->mbox_mem_pool);
15621 return offset;
15622}
15623
15624/**
15625 * lpfc_sli4_get_config_region23 - Get sli4 port region 23 data.
15626 * @phba: pointer to lpfc hba data structure.
15627 * @rgn23_data: pointer to configure region 23 data.
15628 *
15629 * This function gets SLI4 port configure region 23 data through memory dump
15630 * mailbox command. When it successfully retrieves data, the size of the data
15631 * will be returned, otherwise, 0 will be returned.
15632 **/
15633static uint32_t
15634lpfc_sli4_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)
15635{
15636 LPFC_MBOXQ_t *mboxq = NULL;
15637 struct lpfc_dmabuf *mp = NULL;
15638 struct lpfc_mqe *mqe;
15639 uint32_t data_length = 0;
15640 int rc;
15641
15642 if (!rgn23_data)
15643 return 0;
15644
15645 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15646 if (!mboxq) {
15647 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15648 "3105 failed to allocate mailbox memory\n");
15649 return 0;
15650 }
15651
15652 if (lpfc_sli4_dump_cfg_rg23(phba, mboxq))
15653 goto out;
15654 mqe = &mboxq->u.mqe;
15655 mp = (struct lpfc_dmabuf *) mboxq->context1;
15656 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
15657 if (rc)
15658 goto out;
15659 data_length = mqe->un.mb_words[5];
15660 if (data_length == 0)
15661 goto out;
15662 if (data_length > DMP_RGN23_SIZE) {
15663 data_length = 0;
15664 goto out;
15665 }
15666 lpfc_sli_pcimem_bcopy((char *)mp->virt, rgn23_data, data_length);
15667out:
15668 mempool_free(mboxq, phba->mbox_mem_pool);
15669 if (mp) {
15670 lpfc_mbuf_free(phba, mp->virt, mp->phys);
15671 kfree(mp);
15672 }
15673 return data_length;
15674}
15675
15676/**
15677 * lpfc_sli_read_link_ste - Read region 23 to decide if link is disabled.
15678 * @phba: pointer to lpfc hba data structure.
15679 *
15680 * This function read region 23 and parse TLV for port status to
15681 * decide if the user disaled the port. If the TLV indicates the
15682 * port is disabled, the hba_flag is set accordingly.
15683 **/
15684void
15685lpfc_sli_read_link_ste(struct lpfc_hba *phba)
15686{
15687 uint8_t *rgn23_data = NULL;
15688 uint32_t if_type, data_size, sub_tlv_len, tlv_offset;
15689 uint32_t offset = 0;
15690
15691 /* Get adapter Region 23 data */
15692 rgn23_data = kzalloc(DMP_RGN23_SIZE, GFP_KERNEL);
15693 if (!rgn23_data)
15694 goto out;
15695
15696 if (phba->sli_rev < LPFC_SLI_REV4)
15697 data_size = lpfc_sli_get_config_region23(phba, rgn23_data);
15698 else {
15699 if_type = bf_get(lpfc_sli_intf_if_type,
15700 &phba->sli4_hba.sli_intf);
15701 if (if_type == LPFC_SLI_INTF_IF_TYPE_0)
15702 goto out;
15703 data_size = lpfc_sli4_get_config_region23(phba, rgn23_data);
15704 }
James Smarta0c87cb2009-07-19 10:01:10 -040015705
15706 if (!data_size)
15707 goto out;
15708
15709 /* Check the region signature first */
15710 if (memcmp(&rgn23_data[offset], LPFC_REGION23_SIGNATURE, 4)) {
15711 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15712 "2619 Config region 23 has bad signature\n");
15713 goto out;
15714 }
15715 offset += 4;
15716
15717 /* Check the data structure version */
15718 if (rgn23_data[offset] != LPFC_REGION23_VERSION) {
15719 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15720 "2620 Config region 23 has bad version\n");
15721 goto out;
15722 }
15723 offset += 4;
15724
15725 /* Parse TLV entries in the region */
15726 while (offset < data_size) {
15727 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC)
15728 break;
15729 /*
15730 * If the TLV is not driver specific TLV or driver id is
15731 * not linux driver id, skip the record.
15732 */
15733 if ((rgn23_data[offset] != DRIVER_SPECIFIC_TYPE) ||
15734 (rgn23_data[offset + 2] != LINUX_DRIVER_ID) ||
15735 (rgn23_data[offset + 3] != 0)) {
15736 offset += rgn23_data[offset + 1] * 4 + 4;
15737 continue;
15738 }
15739
15740 /* Driver found a driver specific TLV in the config region */
15741 sub_tlv_len = rgn23_data[offset + 1] * 4;
15742 offset += 4;
15743 tlv_offset = 0;
15744
15745 /*
15746 * Search for configured port state sub-TLV.
15747 */
15748 while ((offset < data_size) &&
15749 (tlv_offset < sub_tlv_len)) {
15750 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC) {
15751 offset += 4;
15752 tlv_offset += 4;
15753 break;
15754 }
15755 if (rgn23_data[offset] != PORT_STE_TYPE) {
15756 offset += rgn23_data[offset + 1] * 4 + 4;
15757 tlv_offset += rgn23_data[offset + 1] * 4 + 4;
15758 continue;
15759 }
15760
15761 /* This HBA contains PORT_STE configured */
15762 if (!rgn23_data[offset + 2])
15763 phba->hba_flag |= LINK_DISABLED;
15764
15765 goto out;
15766 }
15767 }
James Smart026abb82011-12-13 13:20:45 -050015768
James Smarta0c87cb2009-07-19 10:01:10 -040015769out:
James Smarta0c87cb2009-07-19 10:01:10 -040015770 kfree(rgn23_data);
15771 return;
15772}
James Smart695a8142010-01-26 23:08:03 -050015773
15774/**
James Smart52d52442011-05-24 11:42:45 -040015775 * lpfc_wr_object - write an object to the firmware
15776 * @phba: HBA structure that indicates port to create a queue on.
15777 * @dmabuf_list: list of dmabufs to write to the port.
15778 * @size: the total byte value of the objects to write to the port.
15779 * @offset: the current offset to be used to start the transfer.
15780 *
15781 * This routine will create a wr_object mailbox command to send to the port.
15782 * the mailbox command will be constructed using the dma buffers described in
15783 * @dmabuf_list to create a list of BDEs. This routine will fill in as many
15784 * BDEs that the imbedded mailbox can support. The @offset variable will be
15785 * used to indicate the starting offset of the transfer and will also return
15786 * the offset after the write object mailbox has completed. @size is used to
15787 * determine the end of the object and whether the eof bit should be set.
15788 *
15789 * Return 0 is successful and offset will contain the the new offset to use
15790 * for the next write.
15791 * Return negative value for error cases.
15792 **/
15793int
15794lpfc_wr_object(struct lpfc_hba *phba, struct list_head *dmabuf_list,
15795 uint32_t size, uint32_t *offset)
15796{
15797 struct lpfc_mbx_wr_object *wr_object;
15798 LPFC_MBOXQ_t *mbox;
15799 int rc = 0, i = 0;
15800 uint32_t shdr_status, shdr_add_status;
15801 uint32_t mbox_tmo;
15802 union lpfc_sli4_cfg_shdr *shdr;
15803 struct lpfc_dmabuf *dmabuf;
15804 uint32_t written = 0;
15805
15806 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15807 if (!mbox)
15808 return -ENOMEM;
15809
15810 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
15811 LPFC_MBOX_OPCODE_WRITE_OBJECT,
15812 sizeof(struct lpfc_mbx_wr_object) -
15813 sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
15814
15815 wr_object = (struct lpfc_mbx_wr_object *)&mbox->u.mqe.un.wr_object;
15816 wr_object->u.request.write_offset = *offset;
15817 sprintf((uint8_t *)wr_object->u.request.object_name, "/");
15818 wr_object->u.request.object_name[0] =
15819 cpu_to_le32(wr_object->u.request.object_name[0]);
15820 bf_set(lpfc_wr_object_eof, &wr_object->u.request, 0);
15821 list_for_each_entry(dmabuf, dmabuf_list, list) {
15822 if (i >= LPFC_MBX_WR_CONFIG_MAX_BDE || written >= size)
15823 break;
15824 wr_object->u.request.bde[i].addrLow = putPaddrLow(dmabuf->phys);
15825 wr_object->u.request.bde[i].addrHigh =
15826 putPaddrHigh(dmabuf->phys);
15827 if (written + SLI4_PAGE_SIZE >= size) {
15828 wr_object->u.request.bde[i].tus.f.bdeSize =
15829 (size - written);
15830 written += (size - written);
15831 bf_set(lpfc_wr_object_eof, &wr_object->u.request, 1);
15832 } else {
15833 wr_object->u.request.bde[i].tus.f.bdeSize =
15834 SLI4_PAGE_SIZE;
15835 written += SLI4_PAGE_SIZE;
15836 }
15837 i++;
15838 }
15839 wr_object->u.request.bde_count = i;
15840 bf_set(lpfc_wr_object_write_length, &wr_object->u.request, written);
15841 if (!phba->sli4_hba.intr_enable)
15842 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15843 else {
James Smarta183a152011-10-10 21:32:43 -040015844 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart52d52442011-05-24 11:42:45 -040015845 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
15846 }
15847 /* The IOCTL status is embedded in the mailbox subheader. */
15848 shdr = (union lpfc_sli4_cfg_shdr *) &wr_object->header.cfg_shdr;
15849 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15850 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15851 if (rc != MBX_TIMEOUT)
15852 mempool_free(mbox, phba->mbox_mem_pool);
15853 if (shdr_status || shdr_add_status || rc) {
15854 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15855 "3025 Write Object mailbox failed with "
15856 "status x%x add_status x%x, mbx status x%x\n",
15857 shdr_status, shdr_add_status, rc);
15858 rc = -ENXIO;
15859 } else
15860 *offset += wr_object->u.response.actual_write_length;
15861 return rc;
15862}
15863
15864/**
James Smart695a8142010-01-26 23:08:03 -050015865 * lpfc_cleanup_pending_mbox - Free up vport discovery mailbox commands.
15866 * @vport: pointer to vport data structure.
15867 *
15868 * This function iterate through the mailboxq and clean up all REG_LOGIN
15869 * and REG_VPI mailbox commands associated with the vport. This function
15870 * is called when driver want to restart discovery of the vport due to
15871 * a Clear Virtual Link event.
15872 **/
15873void
15874lpfc_cleanup_pending_mbox(struct lpfc_vport *vport)
15875{
15876 struct lpfc_hba *phba = vport->phba;
15877 LPFC_MBOXQ_t *mb, *nextmb;
15878 struct lpfc_dmabuf *mp;
James Smart78730cf2010-04-06 15:06:30 -040015879 struct lpfc_nodelist *ndlp;
James Smartd439d282010-09-29 11:18:45 -040015880 struct lpfc_nodelist *act_mbx_ndlp = NULL;
James Smart589a52d2010-07-14 15:30:54 -040015881 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smartd439d282010-09-29 11:18:45 -040015882 LIST_HEAD(mbox_cmd_list);
James Smart63e801c2010-11-20 23:14:19 -050015883 uint8_t restart_loop;
James Smart695a8142010-01-26 23:08:03 -050015884
James Smartd439d282010-09-29 11:18:45 -040015885 /* Clean up internally queued mailbox commands with the vport */
James Smart695a8142010-01-26 23:08:03 -050015886 spin_lock_irq(&phba->hbalock);
15887 list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
15888 if (mb->vport != vport)
15889 continue;
15890
15891 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
15892 (mb->u.mb.mbxCommand != MBX_REG_VPI))
15893 continue;
15894
James Smartd439d282010-09-29 11:18:45 -040015895 list_del(&mb->list);
15896 list_add_tail(&mb->list, &mbox_cmd_list);
15897 }
15898 /* Clean up active mailbox command with the vport */
15899 mb = phba->sli.mbox_active;
15900 if (mb && (mb->vport == vport)) {
15901 if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) ||
15902 (mb->u.mb.mbxCommand == MBX_REG_VPI))
15903 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
15904 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
15905 act_mbx_ndlp = (struct lpfc_nodelist *)mb->context2;
15906 /* Put reference count for delayed processing */
15907 act_mbx_ndlp = lpfc_nlp_get(act_mbx_ndlp);
15908 /* Unregister the RPI when mailbox complete */
15909 mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
15910 }
15911 }
James Smart63e801c2010-11-20 23:14:19 -050015912 /* Cleanup any mailbox completions which are not yet processed */
15913 do {
15914 restart_loop = 0;
15915 list_for_each_entry(mb, &phba->sli.mboxq_cmpl, list) {
15916 /*
15917 * If this mailox is already processed or it is
15918 * for another vport ignore it.
15919 */
15920 if ((mb->vport != vport) ||
15921 (mb->mbox_flag & LPFC_MBX_IMED_UNREG))
15922 continue;
15923
15924 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
15925 (mb->u.mb.mbxCommand != MBX_REG_VPI))
15926 continue;
15927
15928 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
15929 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
15930 ndlp = (struct lpfc_nodelist *)mb->context2;
15931 /* Unregister the RPI when mailbox complete */
15932 mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
15933 restart_loop = 1;
15934 spin_unlock_irq(&phba->hbalock);
15935 spin_lock(shost->host_lock);
15936 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
15937 spin_unlock(shost->host_lock);
15938 spin_lock_irq(&phba->hbalock);
15939 break;
15940 }
15941 }
15942 } while (restart_loop);
15943
James Smartd439d282010-09-29 11:18:45 -040015944 spin_unlock_irq(&phba->hbalock);
15945
15946 /* Release the cleaned-up mailbox commands */
15947 while (!list_empty(&mbox_cmd_list)) {
15948 list_remove_head(&mbox_cmd_list, mb, LPFC_MBOXQ_t, list);
James Smart695a8142010-01-26 23:08:03 -050015949 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
15950 mp = (struct lpfc_dmabuf *) (mb->context1);
15951 if (mp) {
15952 __lpfc_mbuf_free(phba, mp->virt, mp->phys);
15953 kfree(mp);
15954 }
James Smart78730cf2010-04-06 15:06:30 -040015955 ndlp = (struct lpfc_nodelist *) mb->context2;
James Smartd439d282010-09-29 11:18:45 -040015956 mb->context2 = NULL;
James Smart78730cf2010-04-06 15:06:30 -040015957 if (ndlp) {
Dan Carpenterec21b3b2010-08-08 00:15:17 +020015958 spin_lock(shost->host_lock);
James Smart589a52d2010-07-14 15:30:54 -040015959 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
Dan Carpenterec21b3b2010-08-08 00:15:17 +020015960 spin_unlock(shost->host_lock);
James Smart78730cf2010-04-06 15:06:30 -040015961 lpfc_nlp_put(ndlp);
James Smart78730cf2010-04-06 15:06:30 -040015962 }
James Smart695a8142010-01-26 23:08:03 -050015963 }
James Smart695a8142010-01-26 23:08:03 -050015964 mempool_free(mb, phba->mbox_mem_pool);
15965 }
James Smartd439d282010-09-29 11:18:45 -040015966
15967 /* Release the ndlp with the cleaned-up active mailbox command */
15968 if (act_mbx_ndlp) {
15969 spin_lock(shost->host_lock);
15970 act_mbx_ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
15971 spin_unlock(shost->host_lock);
15972 lpfc_nlp_put(act_mbx_ndlp);
James Smart695a8142010-01-26 23:08:03 -050015973 }
James Smart695a8142010-01-26 23:08:03 -050015974}
15975
James Smart2a9bf3d2010-06-07 15:24:45 -040015976/**
15977 * lpfc_drain_txq - Drain the txq
15978 * @phba: Pointer to HBA context object.
15979 *
15980 * This function attempt to submit IOCBs on the txq
15981 * to the adapter. For SLI4 adapters, the txq contains
15982 * ELS IOCBs that have been deferred because the there
15983 * are no SGLs. This congestion can occur with large
15984 * vport counts during node discovery.
15985 **/
15986
15987uint32_t
15988lpfc_drain_txq(struct lpfc_hba *phba)
15989{
15990 LIST_HEAD(completions);
15991 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
15992 struct lpfc_iocbq *piocbq = 0;
15993 unsigned long iflags = 0;
15994 char *fail_msg = NULL;
15995 struct lpfc_sglq *sglq;
15996 union lpfc_wqe wqe;
15997
15998 spin_lock_irqsave(&phba->hbalock, iflags);
15999 if (pring->txq_cnt > pring->txq_max)
16000 pring->txq_max = pring->txq_cnt;
16001
16002 spin_unlock_irqrestore(&phba->hbalock, iflags);
16003
16004 while (pring->txq_cnt) {
16005 spin_lock_irqsave(&phba->hbalock, iflags);
16006
James Smart19ca7602010-11-20 23:11:55 -050016007 piocbq = lpfc_sli_ringtx_get(phba, pring);
James Smarta6298522012-06-12 13:54:11 -040016008 if (!piocbq) {
16009 spin_unlock_irqrestore(&phba->hbalock, iflags);
16010 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
16011 "2823 txq empty and txq_cnt is %d\n ",
16012 pring->txq_cnt);
16013 break;
16014 }
James Smart19ca7602010-11-20 23:11:55 -050016015 sglq = __lpfc_sli_get_sglq(phba, piocbq);
James Smart2a9bf3d2010-06-07 15:24:45 -040016016 if (!sglq) {
James Smart19ca7602010-11-20 23:11:55 -050016017 __lpfc_sli_ringtx_put(phba, pring, piocbq);
James Smart2a9bf3d2010-06-07 15:24:45 -040016018 spin_unlock_irqrestore(&phba->hbalock, iflags);
16019 break;
James Smart2a9bf3d2010-06-07 15:24:45 -040016020 }
16021
16022 /* The xri and iocb resources secured,
16023 * attempt to issue request
16024 */
James Smart6d368e52011-05-24 11:44:12 -040016025 piocbq->sli4_lxritag = sglq->sli4_lxritag;
James Smart2a9bf3d2010-06-07 15:24:45 -040016026 piocbq->sli4_xritag = sglq->sli4_xritag;
16027 if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocbq, sglq))
16028 fail_msg = "to convert bpl to sgl";
16029 else if (lpfc_sli4_iocb2wqe(phba, piocbq, &wqe))
16030 fail_msg = "to convert iocb to wqe";
16031 else if (lpfc_sli4_wq_put(phba->sli4_hba.els_wq, &wqe))
16032 fail_msg = " - Wq is full";
16033 else
16034 lpfc_sli_ringtxcmpl_put(phba, pring, piocbq);
16035
16036 if (fail_msg) {
16037 /* Failed means we can't issue and need to cancel */
16038 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
16039 "2822 IOCB failed %s iotag 0x%x "
16040 "xri 0x%x\n",
16041 fail_msg,
16042 piocbq->iotag, piocbq->sli4_xritag);
16043 list_add_tail(&piocbq->list, &completions);
16044 }
16045 spin_unlock_irqrestore(&phba->hbalock, iflags);
16046 }
16047
James Smart2a9bf3d2010-06-07 15:24:45 -040016048 /* Cancel all the IOCBs that cannot be issued */
16049 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
16050 IOERR_SLI_ABORTED);
16051
16052 return pring->txq_cnt;
16053}