blob: e27cb44b3ec2c812d8a39705db18af8f23401808 [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 Smart792581d2011-03-11 16:06:44 -05004 * Copyright (C) 2004-2011 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 *);
70
James Smart4f774512009-05-22 14:52:35 -040071static IOCB_t *
72lpfc_get_iocb_from_iocbq(struct lpfc_iocbq *iocbq)
73{
74 return &iocbq->iocb;
75}
76
77/**
78 * lpfc_sli4_wq_put - Put a Work Queue Entry on an Work Queue
79 * @q: The Work Queue to operate on.
80 * @wqe: The work Queue Entry to put on the Work queue.
81 *
82 * This routine will copy the contents of @wqe to the next available entry on
83 * the @q. This function will then ring the Work Queue Doorbell to signal the
84 * HBA to start processing the Work Queue Entry. This function returns 0 if
85 * successful. If no entries are available on @q then this function will return
86 * -ENOMEM.
87 * The caller is expected to hold the hbalock when calling this routine.
88 **/
89static uint32_t
90lpfc_sli4_wq_put(struct lpfc_queue *q, union lpfc_wqe *wqe)
91{
James Smart2e90f4b2011-12-13 13:22:37 -050092 union lpfc_wqe *temp_wqe;
James Smart4f774512009-05-22 14:52:35 -040093 struct lpfc_register doorbell;
94 uint32_t host_index;
95
James Smart2e90f4b2011-12-13 13:22:37 -050096 /* sanity check on queue memory */
97 if (unlikely(!q))
98 return -ENOMEM;
99 temp_wqe = q->qe[q->host_index].wqe;
100
James Smart4f774512009-05-22 14:52:35 -0400101 /* If the host has not yet processed the next entry then we are done */
102 if (((q->host_index + 1) % q->entry_count) == q->hba_index)
103 return -ENOMEM;
104 /* set consumption flag every once in a while */
James Smartff78d8f2011-12-13 13:21:35 -0500105 if (!((q->host_index + 1) % q->entry_repost))
James Smartf0d9bcc2010-10-22 11:07:09 -0400106 bf_set(wqe_wqec, &wqe->generic.wqe_com, 1);
James Smartfedd3b72011-02-16 12:39:24 -0500107 if (q->phba->sli3_options & LPFC_SLI4_PHWQ_ENABLED)
108 bf_set(wqe_wqid, &wqe->generic.wqe_com, q->queue_id);
James Smart4f774512009-05-22 14:52:35 -0400109 lpfc_sli_pcimem_bcopy(wqe, temp_wqe, q->entry_size);
110
111 /* Update the host index before invoking device */
112 host_index = q->host_index;
113 q->host_index = ((q->host_index + 1) % q->entry_count);
114
115 /* Ring Doorbell */
116 doorbell.word0 = 0;
117 bf_set(lpfc_wq_doorbell_num_posted, &doorbell, 1);
118 bf_set(lpfc_wq_doorbell_index, &doorbell, host_index);
119 bf_set(lpfc_wq_doorbell_id, &doorbell, q->queue_id);
120 writel(doorbell.word0, q->phba->sli4_hba.WQDBregaddr);
121 readl(q->phba->sli4_hba.WQDBregaddr); /* Flush */
122
123 return 0;
124}
125
126/**
127 * lpfc_sli4_wq_release - Updates internal hba index for WQ
128 * @q: The Work Queue to operate on.
129 * @index: The index to advance the hba index to.
130 *
131 * This routine will update the HBA index of a queue to reflect consumption of
132 * Work Queue Entries by the HBA. When the HBA indicates that it has consumed
133 * an entry the host calls this function to update the queue's internal
134 * pointers. This routine returns the number of entries that were consumed by
135 * the HBA.
136 **/
137static uint32_t
138lpfc_sli4_wq_release(struct lpfc_queue *q, uint32_t index)
139{
140 uint32_t released = 0;
141
James Smart2e90f4b2011-12-13 13:22:37 -0500142 /* sanity check on queue memory */
143 if (unlikely(!q))
144 return 0;
145
James Smart4f774512009-05-22 14:52:35 -0400146 if (q->hba_index == index)
147 return 0;
148 do {
149 q->hba_index = ((q->hba_index + 1) % q->entry_count);
150 released++;
151 } while (q->hba_index != index);
152 return released;
153}
154
155/**
156 * lpfc_sli4_mq_put - Put a Mailbox Queue Entry on an Mailbox Queue
157 * @q: The Mailbox Queue to operate on.
158 * @wqe: The Mailbox Queue Entry to put on the Work queue.
159 *
160 * This routine will copy the contents of @mqe to the next available entry on
161 * the @q. This function will then ring the Work Queue Doorbell to signal the
162 * HBA to start processing the Work Queue Entry. This function returns 0 if
163 * successful. If no entries are available on @q then this function will return
164 * -ENOMEM.
165 * The caller is expected to hold the hbalock when calling this routine.
166 **/
167static uint32_t
168lpfc_sli4_mq_put(struct lpfc_queue *q, struct lpfc_mqe *mqe)
169{
James Smart2e90f4b2011-12-13 13:22:37 -0500170 struct lpfc_mqe *temp_mqe;
James Smart4f774512009-05-22 14:52:35 -0400171 struct lpfc_register doorbell;
172 uint32_t host_index;
173
James Smart2e90f4b2011-12-13 13:22:37 -0500174 /* sanity check on queue memory */
175 if (unlikely(!q))
176 return -ENOMEM;
177 temp_mqe = q->qe[q->host_index].mqe;
178
James Smart4f774512009-05-22 14:52:35 -0400179 /* If the host has not yet processed the next entry then we are done */
180 if (((q->host_index + 1) % q->entry_count) == q->hba_index)
181 return -ENOMEM;
182 lpfc_sli_pcimem_bcopy(mqe, temp_mqe, q->entry_size);
183 /* Save off the mailbox pointer for completion */
184 q->phba->mbox = (MAILBOX_t *)temp_mqe;
185
186 /* Update the host index before invoking device */
187 host_index = q->host_index;
188 q->host_index = ((q->host_index + 1) % q->entry_count);
189
190 /* Ring Doorbell */
191 doorbell.word0 = 0;
192 bf_set(lpfc_mq_doorbell_num_posted, &doorbell, 1);
193 bf_set(lpfc_mq_doorbell_id, &doorbell, q->queue_id);
194 writel(doorbell.word0, q->phba->sli4_hba.MQDBregaddr);
195 readl(q->phba->sli4_hba.MQDBregaddr); /* Flush */
196 return 0;
197}
198
199/**
200 * lpfc_sli4_mq_release - Updates internal hba index for MQ
201 * @q: The Mailbox Queue to operate on.
202 *
203 * This routine will update the HBA index of a queue to reflect consumption of
204 * a Mailbox Queue Entry by the HBA. When the HBA indicates that it has consumed
205 * an entry the host calls this function to update the queue's internal
206 * pointers. This routine returns the number of entries that were consumed by
207 * the HBA.
208 **/
209static uint32_t
210lpfc_sli4_mq_release(struct lpfc_queue *q)
211{
James Smart2e90f4b2011-12-13 13:22:37 -0500212 /* sanity check on queue memory */
213 if (unlikely(!q))
214 return 0;
215
James Smart4f774512009-05-22 14:52:35 -0400216 /* Clear the mailbox pointer for completion */
217 q->phba->mbox = NULL;
218 q->hba_index = ((q->hba_index + 1) % q->entry_count);
219 return 1;
220}
221
222/**
223 * lpfc_sli4_eq_get - Gets the next valid EQE from a EQ
224 * @q: The Event Queue to get the first valid EQE from
225 *
226 * This routine will get the first valid Event Queue Entry from @q, update
227 * the queue's internal hba index, and return the EQE. If no valid EQEs are in
228 * the Queue (no more work to do), or the Queue is full of EQEs that have been
229 * processed, but not popped back to the HBA then this routine will return NULL.
230 **/
231static struct lpfc_eqe *
232lpfc_sli4_eq_get(struct lpfc_queue *q)
233{
James Smart2e90f4b2011-12-13 13:22:37 -0500234 struct lpfc_eqe *eqe;
235
236 /* sanity check on queue memory */
237 if (unlikely(!q))
238 return NULL;
239 eqe = q->qe[q->hba_index].eqe;
James Smart4f774512009-05-22 14:52:35 -0400240
241 /* If the next EQE is not valid then we are done */
James Smartcb5172e2010-03-15 11:25:07 -0400242 if (!bf_get_le32(lpfc_eqe_valid, eqe))
James Smart4f774512009-05-22 14:52:35 -0400243 return NULL;
244 /* If the host has not yet processed the next entry then we are done */
245 if (((q->hba_index + 1) % q->entry_count) == q->host_index)
246 return NULL;
247
248 q->hba_index = ((q->hba_index + 1) % q->entry_count);
249 return eqe;
250}
251
252/**
253 * lpfc_sli4_eq_release - Indicates the host has finished processing an EQ
254 * @q: The Event Queue that the host has completed processing for.
255 * @arm: Indicates whether the host wants to arms this CQ.
256 *
257 * This routine will mark all Event Queue Entries on @q, from the last
258 * known completed entry to the last entry that was processed, as completed
259 * by clearing the valid bit for each completion queue entry. Then it will
260 * notify the HBA, by ringing the doorbell, that the EQEs have been processed.
261 * The internal host index in the @q will be updated by this routine to indicate
262 * that the host has finished processing the entries. The @arm parameter
263 * indicates that the queue should be rearmed when ringing the doorbell.
264 *
265 * This function will return the number of EQEs that were popped.
266 **/
267uint32_t
268lpfc_sli4_eq_release(struct lpfc_queue *q, bool arm)
269{
270 uint32_t released = 0;
271 struct lpfc_eqe *temp_eqe;
272 struct lpfc_register doorbell;
273
James Smart2e90f4b2011-12-13 13:22:37 -0500274 /* sanity check on queue memory */
275 if (unlikely(!q))
276 return 0;
277
James Smart4f774512009-05-22 14:52:35 -0400278 /* while there are valid entries */
279 while (q->hba_index != q->host_index) {
280 temp_eqe = q->qe[q->host_index].eqe;
James Smartcb5172e2010-03-15 11:25:07 -0400281 bf_set_le32(lpfc_eqe_valid, temp_eqe, 0);
James Smart4f774512009-05-22 14:52:35 -0400282 released++;
283 q->host_index = ((q->host_index + 1) % q->entry_count);
284 }
285 if (unlikely(released == 0 && !arm))
286 return 0;
287
288 /* ring doorbell for number popped */
289 doorbell.word0 = 0;
290 if (arm) {
291 bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);
292 bf_set(lpfc_eqcq_doorbell_eqci, &doorbell, 1);
293 }
294 bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, released);
295 bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_EVENT);
296 bf_set(lpfc_eqcq_doorbell_eqid, &doorbell, q->queue_id);
297 writel(doorbell.word0, q->phba->sli4_hba.EQCQDBregaddr);
James Smarta747c9c2009-11-18 15:41:10 -0500298 /* PCI read to flush PCI pipeline on re-arming for INTx mode */
299 if ((q->phba->intr_type == INTx) && (arm == LPFC_QUEUE_REARM))
300 readl(q->phba->sli4_hba.EQCQDBregaddr);
James Smart4f774512009-05-22 14:52:35 -0400301 return released;
302}
303
304/**
305 * lpfc_sli4_cq_get - Gets the next valid CQE from a CQ
306 * @q: The Completion Queue to get the first valid CQE from
307 *
308 * This routine will get the first valid Completion Queue Entry from @q, update
309 * the queue's internal hba index, and return the CQE. If no valid CQEs are in
310 * the Queue (no more work to do), or the Queue is full of CQEs that have been
311 * processed, but not popped back to the HBA then this routine will return NULL.
312 **/
313static struct lpfc_cqe *
314lpfc_sli4_cq_get(struct lpfc_queue *q)
315{
316 struct lpfc_cqe *cqe;
317
James Smart2e90f4b2011-12-13 13:22:37 -0500318 /* sanity check on queue memory */
319 if (unlikely(!q))
320 return NULL;
321
James Smart4f774512009-05-22 14:52:35 -0400322 /* If the next CQE is not valid then we are done */
James Smartcb5172e2010-03-15 11:25:07 -0400323 if (!bf_get_le32(lpfc_cqe_valid, q->qe[q->hba_index].cqe))
James Smart4f774512009-05-22 14:52:35 -0400324 return NULL;
325 /* If the host has not yet processed the next entry then we are done */
326 if (((q->hba_index + 1) % q->entry_count) == q->host_index)
327 return NULL;
328
329 cqe = q->qe[q->hba_index].cqe;
330 q->hba_index = ((q->hba_index + 1) % q->entry_count);
331 return cqe;
332}
333
334/**
335 * lpfc_sli4_cq_release - Indicates the host has finished processing a CQ
336 * @q: The Completion Queue that the host has completed processing for.
337 * @arm: Indicates whether the host wants to arms this CQ.
338 *
339 * This routine will mark all Completion queue entries on @q, from the last
340 * known completed entry to the last entry that was processed, as completed
341 * by clearing the valid bit for each completion queue entry. Then it will
342 * notify the HBA, by ringing the doorbell, that the CQEs have been processed.
343 * The internal host index in the @q will be updated by this routine to indicate
344 * that the host has finished processing the entries. The @arm parameter
345 * indicates that the queue should be rearmed when ringing the doorbell.
346 *
347 * This function will return the number of CQEs that were released.
348 **/
349uint32_t
350lpfc_sli4_cq_release(struct lpfc_queue *q, bool arm)
351{
352 uint32_t released = 0;
353 struct lpfc_cqe *temp_qe;
354 struct lpfc_register doorbell;
355
James Smart2e90f4b2011-12-13 13:22:37 -0500356 /* sanity check on queue memory */
357 if (unlikely(!q))
358 return 0;
James Smart4f774512009-05-22 14:52:35 -0400359 /* while there are valid entries */
360 while (q->hba_index != q->host_index) {
361 temp_qe = q->qe[q->host_index].cqe;
James Smartcb5172e2010-03-15 11:25:07 -0400362 bf_set_le32(lpfc_cqe_valid, temp_qe, 0);
James Smart4f774512009-05-22 14:52:35 -0400363 released++;
364 q->host_index = ((q->host_index + 1) % q->entry_count);
365 }
366 if (unlikely(released == 0 && !arm))
367 return 0;
368
369 /* ring doorbell for number popped */
370 doorbell.word0 = 0;
371 if (arm)
372 bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);
373 bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, released);
374 bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_COMPLETION);
375 bf_set(lpfc_eqcq_doorbell_cqid, &doorbell, q->queue_id);
376 writel(doorbell.word0, q->phba->sli4_hba.EQCQDBregaddr);
377 return released;
378}
379
380/**
381 * lpfc_sli4_rq_put - Put a Receive Buffer Queue Entry on a Receive Queue
382 * @q: The Header Receive Queue to operate on.
383 * @wqe: The Receive Queue Entry to put on the Receive queue.
384 *
385 * This routine will copy the contents of @wqe to the next available entry on
386 * the @q. This function will then ring the Receive Queue Doorbell to signal the
387 * HBA to start processing the Receive Queue Entry. This function returns the
388 * index that the rqe was copied to if successful. If no entries are available
389 * on @q then this function will return -ENOMEM.
390 * The caller is expected to hold the hbalock when calling this routine.
391 **/
392static int
393lpfc_sli4_rq_put(struct lpfc_queue *hq, struct lpfc_queue *dq,
394 struct lpfc_rqe *hrqe, struct lpfc_rqe *drqe)
395{
James Smart2e90f4b2011-12-13 13:22:37 -0500396 struct lpfc_rqe *temp_hrqe;
397 struct lpfc_rqe *temp_drqe;
James Smart4f774512009-05-22 14:52:35 -0400398 struct lpfc_register doorbell;
399 int put_index = hq->host_index;
400
James Smart2e90f4b2011-12-13 13:22:37 -0500401 /* sanity check on queue memory */
402 if (unlikely(!hq) || unlikely(!dq))
403 return -ENOMEM;
404 temp_hrqe = hq->qe[hq->host_index].rqe;
405 temp_drqe = dq->qe[dq->host_index].rqe;
406
James Smart4f774512009-05-22 14:52:35 -0400407 if (hq->type != LPFC_HRQ || dq->type != LPFC_DRQ)
408 return -EINVAL;
409 if (hq->host_index != dq->host_index)
410 return -EINVAL;
411 /* If the host has not yet processed the next entry then we are done */
412 if (((hq->host_index + 1) % hq->entry_count) == hq->hba_index)
413 return -EBUSY;
414 lpfc_sli_pcimem_bcopy(hrqe, temp_hrqe, hq->entry_size);
415 lpfc_sli_pcimem_bcopy(drqe, temp_drqe, dq->entry_size);
416
417 /* Update the host index to point to the next slot */
418 hq->host_index = ((hq->host_index + 1) % hq->entry_count);
419 dq->host_index = ((dq->host_index + 1) % dq->entry_count);
420
421 /* Ring The Header Receive Queue Doorbell */
James Smart73d91e52011-10-10 21:32:10 -0400422 if (!(hq->host_index % hq->entry_repost)) {
James Smart4f774512009-05-22 14:52:35 -0400423 doorbell.word0 = 0;
424 bf_set(lpfc_rq_doorbell_num_posted, &doorbell,
James Smart73d91e52011-10-10 21:32:10 -0400425 hq->entry_repost);
James Smart4f774512009-05-22 14:52:35 -0400426 bf_set(lpfc_rq_doorbell_id, &doorbell, hq->queue_id);
427 writel(doorbell.word0, hq->phba->sli4_hba.RQDBregaddr);
428 }
429 return put_index;
430}
431
432/**
433 * lpfc_sli4_rq_release - Updates internal hba index for RQ
434 * @q: The Header Receive Queue to operate on.
435 *
436 * This routine will update the HBA index of a queue to reflect consumption of
437 * one Receive Queue Entry by the HBA. When the HBA indicates that it has
438 * consumed an entry the host calls this function to update the queue's
439 * internal pointers. This routine returns the number of entries that were
440 * consumed by the HBA.
441 **/
442static uint32_t
443lpfc_sli4_rq_release(struct lpfc_queue *hq, struct lpfc_queue *dq)
444{
James Smart2e90f4b2011-12-13 13:22:37 -0500445 /* sanity check on queue memory */
446 if (unlikely(!hq) || unlikely(!dq))
447 return 0;
448
James Smart4f774512009-05-22 14:52:35 -0400449 if ((hq->type != LPFC_HRQ) || (dq->type != LPFC_DRQ))
450 return 0;
451 hq->hba_index = ((hq->hba_index + 1) % hq->entry_count);
452 dq->hba_index = ((dq->hba_index + 1) % dq->entry_count);
453 return 1;
454}
455
James Smarte59058c2008-08-24 21:49:00 -0400456/**
James Smart3621a712009-04-06 18:47:14 -0400457 * lpfc_cmd_iocb - Get next command iocb entry in the ring
James Smarte59058c2008-08-24 21:49:00 -0400458 * @phba: Pointer to HBA context object.
459 * @pring: Pointer to driver SLI ring object.
460 *
461 * This function returns pointer to next command iocb entry
462 * in the command ring. The caller must hold hbalock to prevent
463 * other threads consume the next command iocb.
464 * SLI-2/SLI-3 provide different sized iocbs.
465 **/
James Smarted957682007-06-17 19:56:37 -0500466static inline IOCB_t *
467lpfc_cmd_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
468{
469 return (IOCB_t *) (((char *) pring->cmdringaddr) +
470 pring->cmdidx * phba->iocb_cmd_size);
471}
472
James Smarte59058c2008-08-24 21:49:00 -0400473/**
James Smart3621a712009-04-06 18:47:14 -0400474 * lpfc_resp_iocb - Get next response iocb entry in the ring
James Smarte59058c2008-08-24 21:49:00 -0400475 * @phba: Pointer to HBA context object.
476 * @pring: Pointer to driver SLI ring object.
477 *
478 * This function returns pointer to next response iocb entry
479 * in the response ring. The caller must hold hbalock to make sure
480 * that no other thread consume the next response iocb.
481 * SLI-2/SLI-3 provide different sized iocbs.
482 **/
James Smarted957682007-06-17 19:56:37 -0500483static inline IOCB_t *
484lpfc_resp_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
485{
486 return (IOCB_t *) (((char *) pring->rspringaddr) +
487 pring->rspidx * phba->iocb_rsp_size);
488}
489
James Smarte59058c2008-08-24 21:49:00 -0400490/**
James Smart3621a712009-04-06 18:47:14 -0400491 * __lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
James Smarte59058c2008-08-24 21:49:00 -0400492 * @phba: Pointer to HBA context object.
493 *
494 * This function is called with hbalock held. This function
495 * allocates a new driver iocb object from the iocb pool. If the
496 * allocation is successful, it returns pointer to the newly
497 * allocated iocb object else it returns NULL.
498 **/
James Smart2e0fef82007-06-17 19:56:36 -0500499static struct lpfc_iocbq *
500__lpfc_sli_get_iocbq(struct lpfc_hba *phba)
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400501{
502 struct list_head *lpfc_iocb_list = &phba->lpfc_iocb_list;
503 struct lpfc_iocbq * iocbq = NULL;
504
505 list_remove_head(lpfc_iocb_list, iocbq, struct lpfc_iocbq, list);
James Smart2a9bf3d2010-06-07 15:24:45 -0400506 if (iocbq)
507 phba->iocb_cnt++;
508 if (phba->iocb_cnt > phba->iocb_max)
509 phba->iocb_max = phba->iocb_cnt;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400510 return iocbq;
511}
512
James Smarte59058c2008-08-24 21:49:00 -0400513/**
James Smartda0436e2009-05-22 14:51:39 -0400514 * __lpfc_clear_active_sglq - Remove the active sglq for this XRI.
515 * @phba: Pointer to HBA context object.
516 * @xritag: XRI value.
517 *
518 * This function clears the sglq pointer from the array of acive
519 * sglq's. The xritag that is passed in is used to index into the
520 * array. Before the xritag can be used it needs to be adjusted
521 * by subtracting the xribase.
522 *
523 * Returns sglq ponter = success, NULL = Failure.
524 **/
525static struct lpfc_sglq *
526__lpfc_clear_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
527{
James Smartda0436e2009-05-22 14:51:39 -0400528 struct lpfc_sglq *sglq;
James Smart6d368e52011-05-24 11:44:12 -0400529
530 sglq = phba->sli4_hba.lpfc_sglq_active_list[xritag];
531 phba->sli4_hba.lpfc_sglq_active_list[xritag] = NULL;
James Smartda0436e2009-05-22 14:51:39 -0400532 return sglq;
533}
534
535/**
536 * __lpfc_get_active_sglq - Get the active sglq for this XRI.
537 * @phba: Pointer to HBA context object.
538 * @xritag: XRI value.
539 *
540 * This function returns the sglq pointer from the array of acive
541 * sglq's. The xritag that is passed in is used to index into the
542 * array. Before the xritag can be used it needs to be adjusted
543 * by subtracting the xribase.
544 *
545 * Returns sglq ponter = success, NULL = Failure.
546 **/
James Smart0f65ff62010-02-26 14:14:23 -0500547struct lpfc_sglq *
James Smartda0436e2009-05-22 14:51:39 -0400548__lpfc_get_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
549{
James Smartda0436e2009-05-22 14:51:39 -0400550 struct lpfc_sglq *sglq;
James Smart6d368e52011-05-24 11:44:12 -0400551
552 sglq = phba->sli4_hba.lpfc_sglq_active_list[xritag];
James Smartda0436e2009-05-22 14:51:39 -0400553 return sglq;
554}
555
556/**
James Smart19ca7602010-11-20 23:11:55 -0500557 * __lpfc_set_rrq_active - set RRQ active bit in the ndlp's xri_bitmap.
558 * @phba: Pointer to HBA context object.
559 * @ndlp: nodelist pointer for this target.
560 * @xritag: xri used in this exchange.
561 * @rxid: Remote Exchange ID.
562 * @send_rrq: Flag used to determine if we should send rrq els cmd.
563 *
564 * This function is called with hbalock held.
565 * The active bit is set in the ndlp's active rrq xri_bitmap. Allocates an
566 * rrq struct and adds it to the active_rrq_list.
567 *
568 * returns 0 for rrq slot for this xri
569 * < 0 Were not able to get rrq mem or invalid parameter.
570 **/
571static int
572__lpfc_set_rrq_active(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
573 uint16_t xritag, uint16_t rxid, uint16_t send_rrq)
574{
James Smart19ca7602010-11-20 23:11:55 -0500575 struct lpfc_node_rrq *rrq;
576 int empty;
James Smart1151e3e2011-02-16 12:39:35 -0500577 uint32_t did = 0;
578
579
580 if (!ndlp)
581 return -EINVAL;
582
583 if (!phba->cfg_enable_rrq)
584 return -EINVAL;
585
586 if (phba->pport->load_flag & FC_UNLOADING) {
587 phba->hba_flag &= ~HBA_RRQ_ACTIVE;
588 goto out;
589 }
590 did = ndlp->nlp_DID;
James Smart19ca7602010-11-20 23:11:55 -0500591
592 /*
593 * set the active bit even if there is no mem available.
594 */
James Smart1151e3e2011-02-16 12:39:35 -0500595 if (NLP_CHK_FREE_REQ(ndlp))
596 goto out;
597
598 if (ndlp->vport && (ndlp->vport->load_flag & FC_UNLOADING))
599 goto out;
600
James Smart6d368e52011-05-24 11:44:12 -0400601 if (test_and_set_bit(xritag, ndlp->active_rrqs.xri_bitmap))
James Smart1151e3e2011-02-16 12:39:35 -0500602 goto out;
603
James Smart19ca7602010-11-20 23:11:55 -0500604 rrq = mempool_alloc(phba->rrq_pool, GFP_KERNEL);
605 if (rrq) {
606 rrq->send_rrq = send_rrq;
James Smart7851fe22011-07-22 18:36:52 -0400607 rrq->xritag = xritag;
James Smart19ca7602010-11-20 23:11:55 -0500608 rrq->rrq_stop_time = jiffies + HZ * (phba->fc_ratov + 1);
609 rrq->ndlp = ndlp;
610 rrq->nlp_DID = ndlp->nlp_DID;
611 rrq->vport = ndlp->vport;
612 rrq->rxid = rxid;
613 empty = list_empty(&phba->active_rrq_list);
James Smart1151e3e2011-02-16 12:39:35 -0500614 rrq->send_rrq = send_rrq;
James Smart19ca7602010-11-20 23:11:55 -0500615 list_add_tail(&rrq->list, &phba->active_rrq_list);
616 if (!(phba->hba_flag & HBA_RRQ_ACTIVE)) {
617 phba->hba_flag |= HBA_RRQ_ACTIVE;
618 if (empty)
619 lpfc_worker_wake_up(phba);
620 }
621 return 0;
622 }
James Smart1151e3e2011-02-16 12:39:35 -0500623out:
624 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
625 "2921 Can't set rrq active xri:0x%x rxid:0x%x"
626 " DID:0x%x Send:%d\n",
627 xritag, rxid, did, send_rrq);
628 return -EINVAL;
James Smart19ca7602010-11-20 23:11:55 -0500629}
630
631/**
James Smart1151e3e2011-02-16 12:39:35 -0500632 * lpfc_clr_rrq_active - Clears RRQ active bit in xri_bitmap.
James Smart19ca7602010-11-20 23:11:55 -0500633 * @phba: Pointer to HBA context object.
634 * @xritag: xri used in this exchange.
635 * @rrq: The RRQ to be cleared.
636 *
James Smart19ca7602010-11-20 23:11:55 -0500637 **/
James Smart1151e3e2011-02-16 12:39:35 -0500638void
639lpfc_clr_rrq_active(struct lpfc_hba *phba,
640 uint16_t xritag,
641 struct lpfc_node_rrq *rrq)
James Smart19ca7602010-11-20 23:11:55 -0500642{
James Smart1151e3e2011-02-16 12:39:35 -0500643 struct lpfc_nodelist *ndlp = NULL;
James Smart19ca7602010-11-20 23:11:55 -0500644
James Smart1151e3e2011-02-16 12:39:35 -0500645 if ((rrq->vport) && NLP_CHK_NODE_ACT(rrq->ndlp))
646 ndlp = lpfc_findnode_did(rrq->vport, rrq->nlp_DID);
James Smart19ca7602010-11-20 23:11:55 -0500647
648 /* The target DID could have been swapped (cable swap)
649 * we should use the ndlp from the findnode if it is
650 * available.
651 */
James Smart1151e3e2011-02-16 12:39:35 -0500652 if ((!ndlp) && rrq->ndlp)
James Smart19ca7602010-11-20 23:11:55 -0500653 ndlp = rrq->ndlp;
654
James Smart1151e3e2011-02-16 12:39:35 -0500655 if (!ndlp)
656 goto out;
657
James Smart6d368e52011-05-24 11:44:12 -0400658 if (test_and_clear_bit(xritag, ndlp->active_rrqs.xri_bitmap)) {
James Smart19ca7602010-11-20 23:11:55 -0500659 rrq->send_rrq = 0;
660 rrq->xritag = 0;
661 rrq->rrq_stop_time = 0;
662 }
James Smart1151e3e2011-02-16 12:39:35 -0500663out:
James Smart19ca7602010-11-20 23:11:55 -0500664 mempool_free(rrq, phba->rrq_pool);
665}
666
667/**
668 * lpfc_handle_rrq_active - Checks if RRQ has waithed RATOV.
669 * @phba: Pointer to HBA context object.
670 *
671 * This function is called with hbalock held. This function
672 * Checks if stop_time (ratov from setting rrq active) has
673 * been reached, if it has and the send_rrq flag is set then
674 * it will call lpfc_send_rrq. If the send_rrq flag is not set
675 * then it will just call the routine to clear the rrq and
676 * free the rrq resource.
677 * The timer is set to the next rrq that is going to expire before
678 * leaving the routine.
679 *
680 **/
681void
682lpfc_handle_rrq_active(struct lpfc_hba *phba)
683{
684 struct lpfc_node_rrq *rrq;
685 struct lpfc_node_rrq *nextrrq;
686 unsigned long next_time;
687 unsigned long iflags;
James Smart1151e3e2011-02-16 12:39:35 -0500688 LIST_HEAD(send_rrq);
James Smart19ca7602010-11-20 23:11:55 -0500689
690 spin_lock_irqsave(&phba->hbalock, iflags);
691 phba->hba_flag &= ~HBA_RRQ_ACTIVE;
692 next_time = jiffies + HZ * (phba->fc_ratov + 1);
693 list_for_each_entry_safe(rrq, nextrrq,
James Smart1151e3e2011-02-16 12:39:35 -0500694 &phba->active_rrq_list, list) {
695 if (time_after(jiffies, rrq->rrq_stop_time))
696 list_move(&rrq->list, &send_rrq);
697 else if (time_before(rrq->rrq_stop_time, next_time))
James Smart19ca7602010-11-20 23:11:55 -0500698 next_time = rrq->rrq_stop_time;
699 }
700 spin_unlock_irqrestore(&phba->hbalock, iflags);
701 if (!list_empty(&phba->active_rrq_list))
702 mod_timer(&phba->rrq_tmr, next_time);
James Smart1151e3e2011-02-16 12:39:35 -0500703 list_for_each_entry_safe(rrq, nextrrq, &send_rrq, list) {
704 list_del(&rrq->list);
705 if (!rrq->send_rrq)
706 /* this call will free the rrq */
707 lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
708 else if (lpfc_send_rrq(phba, rrq)) {
709 /* if we send the rrq then the completion handler
710 * will clear the bit in the xribitmap.
711 */
712 lpfc_clr_rrq_active(phba, rrq->xritag,
713 rrq);
714 }
715 }
James Smart19ca7602010-11-20 23:11:55 -0500716}
717
718/**
719 * lpfc_get_active_rrq - Get the active RRQ for this exchange.
720 * @vport: Pointer to vport context object.
721 * @xri: The xri used in the exchange.
722 * @did: The targets DID for this exchange.
723 *
724 * returns NULL = rrq not found in the phba->active_rrq_list.
725 * rrq = rrq for this xri and target.
726 **/
727struct lpfc_node_rrq *
728lpfc_get_active_rrq(struct lpfc_vport *vport, uint16_t xri, uint32_t did)
729{
730 struct lpfc_hba *phba = vport->phba;
731 struct lpfc_node_rrq *rrq;
732 struct lpfc_node_rrq *nextrrq;
733 unsigned long iflags;
734
735 if (phba->sli_rev != LPFC_SLI_REV4)
736 return NULL;
737 spin_lock_irqsave(&phba->hbalock, iflags);
738 list_for_each_entry_safe(rrq, nextrrq, &phba->active_rrq_list, list) {
739 if (rrq->vport == vport && rrq->xritag == xri &&
740 rrq->nlp_DID == did){
741 list_del(&rrq->list);
742 spin_unlock_irqrestore(&phba->hbalock, iflags);
743 return rrq;
744 }
745 }
746 spin_unlock_irqrestore(&phba->hbalock, iflags);
747 return NULL;
748}
749
750/**
751 * lpfc_cleanup_vports_rrqs - Remove and clear the active RRQ for this vport.
752 * @vport: Pointer to vport context object.
James Smart1151e3e2011-02-16 12:39:35 -0500753 * @ndlp: Pointer to the lpfc_node_list structure.
754 * If ndlp is NULL Remove all active RRQs for this vport from the
755 * phba->active_rrq_list and clear the rrq.
756 * If ndlp is not NULL then only remove rrqs for this vport & this ndlp.
James Smart19ca7602010-11-20 23:11:55 -0500757 **/
758void
James Smart1151e3e2011-02-16 12:39:35 -0500759lpfc_cleanup_vports_rrqs(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
James Smart19ca7602010-11-20 23:11:55 -0500760
761{
762 struct lpfc_hba *phba = vport->phba;
763 struct lpfc_node_rrq *rrq;
764 struct lpfc_node_rrq *nextrrq;
765 unsigned long iflags;
James Smart1151e3e2011-02-16 12:39:35 -0500766 LIST_HEAD(rrq_list);
James Smart19ca7602010-11-20 23:11:55 -0500767
768 if (phba->sli_rev != LPFC_SLI_REV4)
769 return;
James Smart1151e3e2011-02-16 12:39:35 -0500770 if (!ndlp) {
771 lpfc_sli4_vport_delete_els_xri_aborted(vport);
772 lpfc_sli4_vport_delete_fcp_xri_aborted(vport);
James Smart19ca7602010-11-20 23:11:55 -0500773 }
James Smart1151e3e2011-02-16 12:39:35 -0500774 spin_lock_irqsave(&phba->hbalock, iflags);
775 list_for_each_entry_safe(rrq, nextrrq, &phba->active_rrq_list, list)
776 if ((rrq->vport == vport) && (!ndlp || rrq->ndlp == ndlp))
777 list_move(&rrq->list, &rrq_list);
James Smart19ca7602010-11-20 23:11:55 -0500778 spin_unlock_irqrestore(&phba->hbalock, iflags);
James Smart1151e3e2011-02-16 12:39:35 -0500779
780 list_for_each_entry_safe(rrq, nextrrq, &rrq_list, list) {
781 list_del(&rrq->list);
782 lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
783 }
James Smart19ca7602010-11-20 23:11:55 -0500784}
785
786/**
787 * lpfc_cleanup_wt_rrqs - Remove all rrq's from the active list.
788 * @phba: Pointer to HBA context object.
789 *
790 * Remove all rrqs from the phba->active_rrq_list and free them by
791 * calling __lpfc_clr_active_rrq
792 *
793 **/
794void
795lpfc_cleanup_wt_rrqs(struct lpfc_hba *phba)
796{
797 struct lpfc_node_rrq *rrq;
798 struct lpfc_node_rrq *nextrrq;
799 unsigned long next_time;
800 unsigned long iflags;
James Smart1151e3e2011-02-16 12:39:35 -0500801 LIST_HEAD(rrq_list);
James Smart19ca7602010-11-20 23:11:55 -0500802
803 if (phba->sli_rev != LPFC_SLI_REV4)
804 return;
805 spin_lock_irqsave(&phba->hbalock, iflags);
806 phba->hba_flag &= ~HBA_RRQ_ACTIVE;
807 next_time = jiffies + HZ * (phba->fc_ratov * 2);
James Smart1151e3e2011-02-16 12:39:35 -0500808 list_splice_init(&phba->active_rrq_list, &rrq_list);
James Smart19ca7602010-11-20 23:11:55 -0500809 spin_unlock_irqrestore(&phba->hbalock, iflags);
James Smart1151e3e2011-02-16 12:39:35 -0500810
811 list_for_each_entry_safe(rrq, nextrrq, &rrq_list, list) {
812 list_del(&rrq->list);
813 lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
814 }
James Smart19ca7602010-11-20 23:11:55 -0500815 if (!list_empty(&phba->active_rrq_list))
816 mod_timer(&phba->rrq_tmr, next_time);
817}
818
819
820/**
James Smart1151e3e2011-02-16 12:39:35 -0500821 * lpfc_test_rrq_active - Test RRQ bit in xri_bitmap.
James Smart19ca7602010-11-20 23:11:55 -0500822 * @phba: Pointer to HBA context object.
823 * @ndlp: Targets nodelist pointer for this exchange.
824 * @xritag the xri in the bitmap to test.
825 *
826 * This function is called with hbalock held. This function
827 * returns 0 = rrq not active for this xri
828 * 1 = rrq is valid for this xri.
829 **/
James Smart1151e3e2011-02-16 12:39:35 -0500830int
831lpfc_test_rrq_active(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
James Smart19ca7602010-11-20 23:11:55 -0500832 uint16_t xritag)
833{
James Smart19ca7602010-11-20 23:11:55 -0500834 if (!ndlp)
835 return 0;
James Smart6d368e52011-05-24 11:44:12 -0400836 if (test_bit(xritag, ndlp->active_rrqs.xri_bitmap))
James Smart19ca7602010-11-20 23:11:55 -0500837 return 1;
838 else
839 return 0;
840}
841
842/**
843 * lpfc_set_rrq_active - set RRQ active bit in xri_bitmap.
844 * @phba: Pointer to HBA context object.
845 * @ndlp: nodelist pointer for this target.
846 * @xritag: xri used in this exchange.
847 * @rxid: Remote Exchange ID.
848 * @send_rrq: Flag used to determine if we should send rrq els cmd.
849 *
850 * This function takes the hbalock.
851 * The active bit is always set in the active rrq xri_bitmap even
852 * if there is no slot avaiable for the other rrq information.
853 *
854 * returns 0 rrq actived for this xri
855 * < 0 No memory or invalid ndlp.
856 **/
857int
858lpfc_set_rrq_active(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
859 uint16_t xritag, uint16_t rxid, uint16_t send_rrq)
860{
861 int ret;
862 unsigned long iflags;
863
864 spin_lock_irqsave(&phba->hbalock, iflags);
865 ret = __lpfc_set_rrq_active(phba, ndlp, xritag, rxid, send_rrq);
866 spin_unlock_irqrestore(&phba->hbalock, iflags);
867 return ret;
868}
869
870/**
James Smartda0436e2009-05-22 14:51:39 -0400871 * __lpfc_sli_get_sglq - Allocates an iocb object from sgl pool
872 * @phba: Pointer to HBA context object.
James Smart19ca7602010-11-20 23:11:55 -0500873 * @piocb: Pointer to the iocbq.
James Smartda0436e2009-05-22 14:51:39 -0400874 *
875 * This function is called with hbalock held. This function
James Smart6d368e52011-05-24 11:44:12 -0400876 * gets a new driver sglq object from the sglq list. If the
James Smartda0436e2009-05-22 14:51:39 -0400877 * list is not empty then it is successful, it returns pointer to the newly
878 * allocated sglq object else it returns NULL.
879 **/
880static struct lpfc_sglq *
James Smart19ca7602010-11-20 23:11:55 -0500881__lpfc_sli_get_sglq(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq)
James Smartda0436e2009-05-22 14:51:39 -0400882{
883 struct list_head *lpfc_sgl_list = &phba->sli4_hba.lpfc_sgl_list;
884 struct lpfc_sglq *sglq = NULL;
James Smart19ca7602010-11-20 23:11:55 -0500885 struct lpfc_sglq *start_sglq = NULL;
James Smart19ca7602010-11-20 23:11:55 -0500886 struct lpfc_scsi_buf *lpfc_cmd;
887 struct lpfc_nodelist *ndlp;
888 int found = 0;
889
890 if (piocbq->iocb_flag & LPFC_IO_FCP) {
891 lpfc_cmd = (struct lpfc_scsi_buf *) piocbq->context1;
892 ndlp = lpfc_cmd->rdata->pnode;
James Smartbe858b62010-12-15 17:57:20 -0500893 } else if ((piocbq->iocb.ulpCommand == CMD_GEN_REQUEST64_CR) &&
894 !(piocbq->iocb_flag & LPFC_IO_LIBDFC))
James Smart19ca7602010-11-20 23:11:55 -0500895 ndlp = piocbq->context_un.ndlp;
James Smart19ca7602010-11-20 23:11:55 -0500896 else
897 ndlp = piocbq->context1;
898
James Smartda0436e2009-05-22 14:51:39 -0400899 list_remove_head(lpfc_sgl_list, sglq, struct lpfc_sglq, list);
James Smart19ca7602010-11-20 23:11:55 -0500900 start_sglq = sglq;
901 while (!found) {
902 if (!sglq)
903 return NULL;
James Smart1151e3e2011-02-16 12:39:35 -0500904 if (lpfc_test_rrq_active(phba, ndlp, sglq->sli4_xritag)) {
James Smart19ca7602010-11-20 23:11:55 -0500905 /* This xri has an rrq outstanding for this DID.
906 * put it back in the list and get another xri.
907 */
908 list_add_tail(&sglq->list, lpfc_sgl_list);
909 sglq = NULL;
910 list_remove_head(lpfc_sgl_list, sglq,
911 struct lpfc_sglq, list);
912 if (sglq == start_sglq) {
913 sglq = NULL;
914 break;
915 } else
916 continue;
917 }
918 sglq->ndlp = ndlp;
919 found = 1;
James Smart6d368e52011-05-24 11:44:12 -0400920 phba->sli4_hba.lpfc_sglq_active_list[sglq->sli4_lxritag] = sglq;
James Smart19ca7602010-11-20 23:11:55 -0500921 sglq->state = SGL_ALLOCATED;
922 }
James Smartda0436e2009-05-22 14:51:39 -0400923 return sglq;
924}
925
926/**
James Smart3621a712009-04-06 18:47:14 -0400927 * lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
James Smarte59058c2008-08-24 21:49:00 -0400928 * @phba: Pointer to HBA context object.
929 *
930 * This function is called with no lock held. This function
931 * allocates a new driver iocb object from the iocb pool. If the
932 * allocation is successful, it returns pointer to the newly
933 * allocated iocb object else it returns NULL.
934 **/
James Smart2e0fef82007-06-17 19:56:36 -0500935struct lpfc_iocbq *
936lpfc_sli_get_iocbq(struct lpfc_hba *phba)
James Bottomley604a3e32005-10-29 10:28:33 -0500937{
James Smart2e0fef82007-06-17 19:56:36 -0500938 struct lpfc_iocbq * iocbq = NULL;
939 unsigned long iflags;
940
941 spin_lock_irqsave(&phba->hbalock, iflags);
942 iocbq = __lpfc_sli_get_iocbq(phba);
943 spin_unlock_irqrestore(&phba->hbalock, iflags);
944 return iocbq;
945}
946
James Smarte59058c2008-08-24 21:49:00 -0400947/**
James Smart4f774512009-05-22 14:52:35 -0400948 * __lpfc_sli_release_iocbq_s4 - Release iocb to the iocb pool
949 * @phba: Pointer to HBA context object.
950 * @iocbq: Pointer to driver iocb object.
951 *
952 * This function is called with hbalock held to release driver
953 * iocb object to the iocb pool. The iotag in the iocb object
954 * does not change for each use of the iocb object. This function
955 * clears all other fields of the iocb object when it is freed.
956 * The sqlq structure that holds the xritag and phys and virtual
957 * mappings for the scatter gather list is retrieved from the
958 * active array of sglq. The get of the sglq pointer also clears
959 * the entry in the array. If the status of the IO indiactes that
960 * this IO was aborted then the sglq entry it put on the
961 * lpfc_abts_els_sgl_list until the CQ_ABORTED_XRI is received. If the
962 * IO has good status or fails for any other reason then the sglq
963 * entry is added to the free list (lpfc_sgl_list).
964 **/
965static void
966__lpfc_sli_release_iocbq_s4(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
967{
968 struct lpfc_sglq *sglq;
969 size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
James Smart2a9bf3d2010-06-07 15:24:45 -0400970 unsigned long iflag = 0;
971 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
James Smart4f774512009-05-22 14:52:35 -0400972
973 if (iocbq->sli4_xritag == NO_XRI)
974 sglq = NULL;
975 else
James Smart6d368e52011-05-24 11:44:12 -0400976 sglq = __lpfc_clear_active_sglq(phba, iocbq->sli4_lxritag);
977
James Smart4f774512009-05-22 14:52:35 -0400978 if (sglq) {
James Smart0f65ff62010-02-26 14:14:23 -0500979 if ((iocbq->iocb_flag & LPFC_EXCHANGE_BUSY) &&
980 (sglq->state != SGL_XRI_ABORTED)) {
James Smart4f774512009-05-22 14:52:35 -0400981 spin_lock_irqsave(&phba->sli4_hba.abts_sgl_list_lock,
982 iflag);
983 list_add(&sglq->list,
984 &phba->sli4_hba.lpfc_abts_els_sgl_list);
985 spin_unlock_irqrestore(
986 &phba->sli4_hba.abts_sgl_list_lock, iflag);
James Smart0f65ff62010-02-26 14:14:23 -0500987 } else {
988 sglq->state = SGL_FREED;
James Smart19ca7602010-11-20 23:11:55 -0500989 sglq->ndlp = NULL;
James Smartfedd3b72011-02-16 12:39:24 -0500990 list_add_tail(&sglq->list,
991 &phba->sli4_hba.lpfc_sgl_list);
James Smart2a9bf3d2010-06-07 15:24:45 -0400992
993 /* Check if TXQ queue needs to be serviced */
James Smart589a52d2010-07-14 15:30:54 -0400994 if (pring->txq_cnt)
James Smart2a9bf3d2010-06-07 15:24:45 -0400995 lpfc_worker_wake_up(phba);
James Smart0f65ff62010-02-26 14:14:23 -0500996 }
James Smart4f774512009-05-22 14:52:35 -0400997 }
998
999
1000 /*
1001 * Clean all volatile data fields, preserve iotag and node struct.
1002 */
1003 memset((char *)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
James Smart6d368e52011-05-24 11:44:12 -04001004 iocbq->sli4_lxritag = NO_XRI;
James Smart4f774512009-05-22 14:52:35 -04001005 iocbq->sli4_xritag = NO_XRI;
1006 list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
1007}
1008
James Smart2a9bf3d2010-06-07 15:24:45 -04001009
James Smart4f774512009-05-22 14:52:35 -04001010/**
James Smart3772a992009-05-22 14:50:54 -04001011 * __lpfc_sli_release_iocbq_s3 - Release iocb to the iocb pool
1012 * @phba: Pointer to HBA context object.
1013 * @iocbq: Pointer to driver iocb object.
1014 *
1015 * This function is called with hbalock held to release driver
1016 * iocb object to the iocb pool. The iotag in the iocb object
1017 * does not change for each use of the iocb object. This function
1018 * clears all other fields of the iocb object when it is freed.
1019 **/
1020static void
1021__lpfc_sli_release_iocbq_s3(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1022{
1023 size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
1024
1025 /*
1026 * Clean all volatile data fields, preserve iotag and node struct.
1027 */
1028 memset((char*)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
1029 iocbq->sli4_xritag = NO_XRI;
1030 list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
1031}
1032
1033/**
James Smart3621a712009-04-06 18:47:14 -04001034 * __lpfc_sli_release_iocbq - Release iocb to the iocb pool
James Smarte59058c2008-08-24 21:49:00 -04001035 * @phba: Pointer to HBA context object.
1036 * @iocbq: Pointer to driver iocb object.
1037 *
1038 * This function is called with hbalock held to release driver
1039 * iocb object to the iocb pool. The iotag in the iocb object
1040 * does not change for each use of the iocb object. This function
1041 * clears all other fields of the iocb object when it is freed.
1042 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01001043static void
James Smart2e0fef82007-06-17 19:56:36 -05001044__lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1045{
James Smart3772a992009-05-22 14:50:54 -04001046 phba->__lpfc_sli_release_iocbq(phba, iocbq);
James Smart2a9bf3d2010-06-07 15:24:45 -04001047 phba->iocb_cnt--;
James Bottomley604a3e32005-10-29 10:28:33 -05001048}
1049
James Smarte59058c2008-08-24 21:49:00 -04001050/**
James Smart3621a712009-04-06 18:47:14 -04001051 * lpfc_sli_release_iocbq - Release iocb to the iocb pool
James Smarte59058c2008-08-24 21:49:00 -04001052 * @phba: Pointer to HBA context object.
1053 * @iocbq: Pointer to driver iocb object.
1054 *
1055 * This function is called with no lock held to release the iocb to
1056 * iocb pool.
1057 **/
James Smart2e0fef82007-06-17 19:56:36 -05001058void
1059lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1060{
1061 unsigned long iflags;
1062
1063 /*
1064 * Clean all volatile data fields, preserve iotag and node struct.
1065 */
1066 spin_lock_irqsave(&phba->hbalock, iflags);
1067 __lpfc_sli_release_iocbq(phba, iocbq);
1068 spin_unlock_irqrestore(&phba->hbalock, iflags);
1069}
1070
James Smarte59058c2008-08-24 21:49:00 -04001071/**
James Smarta257bf92009-04-06 18:48:10 -04001072 * lpfc_sli_cancel_iocbs - Cancel all iocbs from a list.
1073 * @phba: Pointer to HBA context object.
1074 * @iocblist: List of IOCBs.
1075 * @ulpstatus: ULP status in IOCB command field.
1076 * @ulpWord4: ULP word-4 in IOCB command field.
1077 *
1078 * This function is called with a list of IOCBs to cancel. It cancels the IOCB
1079 * on the list by invoking the complete callback function associated with the
1080 * IOCB with the provided @ulpstatus and @ulpword4 set to the IOCB commond
1081 * fields.
1082 **/
1083void
1084lpfc_sli_cancel_iocbs(struct lpfc_hba *phba, struct list_head *iocblist,
1085 uint32_t ulpstatus, uint32_t ulpWord4)
1086{
1087 struct lpfc_iocbq *piocb;
1088
1089 while (!list_empty(iocblist)) {
1090 list_remove_head(iocblist, piocb, struct lpfc_iocbq, list);
1091
1092 if (!piocb->iocb_cmpl)
1093 lpfc_sli_release_iocbq(phba, piocb);
1094 else {
1095 piocb->iocb.ulpStatus = ulpstatus;
1096 piocb->iocb.un.ulpWord[4] = ulpWord4;
1097 (piocb->iocb_cmpl) (phba, piocb, piocb);
1098 }
1099 }
1100 return;
1101}
1102
1103/**
James Smart3621a712009-04-06 18:47:14 -04001104 * lpfc_sli_iocb_cmd_type - Get the iocb type
1105 * @iocb_cmnd: iocb command code.
James Smarte59058c2008-08-24 21:49:00 -04001106 *
1107 * This function is called by ring event handler function to get the iocb type.
1108 * This function translates the iocb command to an iocb command type used to
1109 * decide the final disposition of each completed IOCB.
1110 * The function returns
1111 * LPFC_UNKNOWN_IOCB if it is an unsupported iocb
1112 * LPFC_SOL_IOCB if it is a solicited iocb completion
1113 * LPFC_ABORT_IOCB if it is an abort iocb
1114 * LPFC_UNSOL_IOCB if it is an unsolicited iocb
1115 *
1116 * The caller is not required to hold any lock.
1117 **/
dea31012005-04-17 16:05:31 -05001118static lpfc_iocb_type
1119lpfc_sli_iocb_cmd_type(uint8_t iocb_cmnd)
1120{
1121 lpfc_iocb_type type = LPFC_UNKNOWN_IOCB;
1122
1123 if (iocb_cmnd > CMD_MAX_IOCB_CMD)
1124 return 0;
1125
1126 switch (iocb_cmnd) {
1127 case CMD_XMIT_SEQUENCE_CR:
1128 case CMD_XMIT_SEQUENCE_CX:
1129 case CMD_XMIT_BCAST_CN:
1130 case CMD_XMIT_BCAST_CX:
1131 case CMD_ELS_REQUEST_CR:
1132 case CMD_ELS_REQUEST_CX:
1133 case CMD_CREATE_XRI_CR:
1134 case CMD_CREATE_XRI_CX:
1135 case CMD_GET_RPI_CN:
1136 case CMD_XMIT_ELS_RSP_CX:
1137 case CMD_GET_RPI_CR:
1138 case CMD_FCP_IWRITE_CR:
1139 case CMD_FCP_IWRITE_CX:
1140 case CMD_FCP_IREAD_CR:
1141 case CMD_FCP_IREAD_CX:
1142 case CMD_FCP_ICMND_CR:
1143 case CMD_FCP_ICMND_CX:
James Smartf5603512006-12-02 13:35:43 -05001144 case CMD_FCP_TSEND_CX:
1145 case CMD_FCP_TRSP_CX:
1146 case CMD_FCP_TRECEIVE_CX:
1147 case CMD_FCP_AUTO_TRSP_CX:
dea31012005-04-17 16:05:31 -05001148 case CMD_ADAPTER_MSG:
1149 case CMD_ADAPTER_DUMP:
1150 case CMD_XMIT_SEQUENCE64_CR:
1151 case CMD_XMIT_SEQUENCE64_CX:
1152 case CMD_XMIT_BCAST64_CN:
1153 case CMD_XMIT_BCAST64_CX:
1154 case CMD_ELS_REQUEST64_CR:
1155 case CMD_ELS_REQUEST64_CX:
1156 case CMD_FCP_IWRITE64_CR:
1157 case CMD_FCP_IWRITE64_CX:
1158 case CMD_FCP_IREAD64_CR:
1159 case CMD_FCP_IREAD64_CX:
1160 case CMD_FCP_ICMND64_CR:
1161 case CMD_FCP_ICMND64_CX:
James Smartf5603512006-12-02 13:35:43 -05001162 case CMD_FCP_TSEND64_CX:
1163 case CMD_FCP_TRSP64_CX:
1164 case CMD_FCP_TRECEIVE64_CX:
dea31012005-04-17 16:05:31 -05001165 case CMD_GEN_REQUEST64_CR:
1166 case CMD_GEN_REQUEST64_CX:
1167 case CMD_XMIT_ELS_RSP64_CX:
James Smartda0436e2009-05-22 14:51:39 -04001168 case DSSCMD_IWRITE64_CR:
1169 case DSSCMD_IWRITE64_CX:
1170 case DSSCMD_IREAD64_CR:
1171 case DSSCMD_IREAD64_CX:
dea31012005-04-17 16:05:31 -05001172 type = LPFC_SOL_IOCB;
1173 break;
1174 case CMD_ABORT_XRI_CN:
1175 case CMD_ABORT_XRI_CX:
1176 case CMD_CLOSE_XRI_CN:
1177 case CMD_CLOSE_XRI_CX:
1178 case CMD_XRI_ABORTED_CX:
1179 case CMD_ABORT_MXRI64_CN:
James Smart6669f9b2009-10-02 15:16:45 -04001180 case CMD_XMIT_BLS_RSP64_CX:
dea31012005-04-17 16:05:31 -05001181 type = LPFC_ABORT_IOCB;
1182 break;
1183 case CMD_RCV_SEQUENCE_CX:
1184 case CMD_RCV_ELS_REQ_CX:
1185 case CMD_RCV_SEQUENCE64_CX:
1186 case CMD_RCV_ELS_REQ64_CX:
James Smart57127f12007-10-27 13:37:05 -04001187 case CMD_ASYNC_STATUS:
James Smarted957682007-06-17 19:56:37 -05001188 case CMD_IOCB_RCV_SEQ64_CX:
1189 case CMD_IOCB_RCV_ELS64_CX:
1190 case CMD_IOCB_RCV_CONT64_CX:
James Smart3163f722008-02-08 18:50:25 -05001191 case CMD_IOCB_RET_XRI64_CX:
dea31012005-04-17 16:05:31 -05001192 type = LPFC_UNSOL_IOCB;
1193 break;
James Smart3163f722008-02-08 18:50:25 -05001194 case CMD_IOCB_XMIT_MSEQ64_CR:
1195 case CMD_IOCB_XMIT_MSEQ64_CX:
1196 case CMD_IOCB_RCV_SEQ_LIST64_CX:
1197 case CMD_IOCB_RCV_ELS_LIST64_CX:
1198 case CMD_IOCB_CLOSE_EXTENDED_CN:
1199 case CMD_IOCB_ABORT_EXTENDED_CN:
1200 case CMD_IOCB_RET_HBQE64_CN:
1201 case CMD_IOCB_FCP_IBIDIR64_CR:
1202 case CMD_IOCB_FCP_IBIDIR64_CX:
1203 case CMD_IOCB_FCP_ITASKMGT64_CX:
1204 case CMD_IOCB_LOGENTRY_CN:
1205 case CMD_IOCB_LOGENTRY_ASYNC_CN:
1206 printk("%s - Unhandled SLI-3 Command x%x\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07001207 __func__, iocb_cmnd);
James Smart3163f722008-02-08 18:50:25 -05001208 type = LPFC_UNKNOWN_IOCB;
1209 break;
dea31012005-04-17 16:05:31 -05001210 default:
1211 type = LPFC_UNKNOWN_IOCB;
1212 break;
1213 }
1214
1215 return type;
1216}
1217
James Smarte59058c2008-08-24 21:49:00 -04001218/**
James Smart3621a712009-04-06 18:47:14 -04001219 * lpfc_sli_ring_map - Issue config_ring mbox for all rings
James Smarte59058c2008-08-24 21:49:00 -04001220 * @phba: Pointer to HBA context object.
1221 *
1222 * This function is called from SLI initialization code
1223 * to configure every ring of the HBA's SLI interface. The
1224 * caller is not required to hold any lock. This function issues
1225 * a config_ring mailbox command for each ring.
1226 * This function returns zero if successful else returns a negative
1227 * error code.
1228 **/
dea31012005-04-17 16:05:31 -05001229static int
James Smarted957682007-06-17 19:56:37 -05001230lpfc_sli_ring_map(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05001231{
1232 struct lpfc_sli *psli = &phba->sli;
James Smarted957682007-06-17 19:56:37 -05001233 LPFC_MBOXQ_t *pmb;
1234 MAILBOX_t *pmbox;
1235 int i, rc, ret = 0;
dea31012005-04-17 16:05:31 -05001236
James Smarted957682007-06-17 19:56:37 -05001237 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1238 if (!pmb)
1239 return -ENOMEM;
James Smart04c68492009-05-22 14:52:52 -04001240 pmbox = &pmb->u.mb;
James Smarted957682007-06-17 19:56:37 -05001241 phba->link_state = LPFC_INIT_MBX_CMDS;
dea31012005-04-17 16:05:31 -05001242 for (i = 0; i < psli->num_rings; i++) {
dea31012005-04-17 16:05:31 -05001243 lpfc_config_ring(phba, i, pmb);
1244 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
1245 if (rc != MBX_SUCCESS) {
James Smart92d7f7b2007-06-17 19:56:38 -05001246 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04001247 "0446 Adapter failed to init (%d), "
dea31012005-04-17 16:05:31 -05001248 "mbxCmd x%x CFG_RING, mbxStatus x%x, "
1249 "ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04001250 rc, pmbox->mbxCommand,
1251 pmbox->mbxStatus, i);
James Smart2e0fef82007-06-17 19:56:36 -05001252 phba->link_state = LPFC_HBA_ERROR;
James Smarted957682007-06-17 19:56:37 -05001253 ret = -ENXIO;
1254 break;
dea31012005-04-17 16:05:31 -05001255 }
1256 }
James Smarted957682007-06-17 19:56:37 -05001257 mempool_free(pmb, phba->mbox_mem_pool);
1258 return ret;
dea31012005-04-17 16:05:31 -05001259}
1260
James Smarte59058c2008-08-24 21:49:00 -04001261/**
James Smart3621a712009-04-06 18:47:14 -04001262 * lpfc_sli_ringtxcmpl_put - Adds new iocb to the txcmplq
James Smarte59058c2008-08-24 21:49:00 -04001263 * @phba: Pointer to HBA context object.
1264 * @pring: Pointer to driver SLI ring object.
1265 * @piocb: Pointer to the driver iocb object.
1266 *
1267 * This function is called with hbalock held. The function adds the
1268 * new iocb to txcmplq of the given ring. This function always returns
1269 * 0. If this function is called for ELS ring, this function checks if
1270 * there is a vport associated with the ELS command. This function also
1271 * starts els_tmofunc timer if this is an ELS command.
1272 **/
dea31012005-04-17 16:05:31 -05001273static int
James Smart2e0fef82007-06-17 19:56:36 -05001274lpfc_sli_ringtxcmpl_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1275 struct lpfc_iocbq *piocb)
dea31012005-04-17 16:05:31 -05001276{
dea31012005-04-17 16:05:31 -05001277 list_add_tail(&piocb->list, &pring->txcmplq);
James Smart2a9bf3d2010-06-07 15:24:45 -04001278 piocb->iocb_flag |= LPFC_IO_ON_Q;
dea31012005-04-17 16:05:31 -05001279 pring->txcmplq_cnt++;
James Smart2a9bf3d2010-06-07 15:24:45 -04001280 if (pring->txcmplq_cnt > pring->txcmplq_max)
1281 pring->txcmplq_max = pring->txcmplq_cnt;
1282
James Smart92d7f7b2007-06-17 19:56:38 -05001283 if ((unlikely(pring->ringno == LPFC_ELS_RING)) &&
1284 (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
1285 (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
1286 if (!piocb->vport)
1287 BUG();
1288 else
1289 mod_timer(&piocb->vport->els_tmofunc,
1290 jiffies + HZ * (phba->fc_ratov << 1));
1291 }
1292
dea31012005-04-17 16:05:31 -05001293
James Smart2e0fef82007-06-17 19:56:36 -05001294 return 0;
dea31012005-04-17 16:05:31 -05001295}
1296
James Smarte59058c2008-08-24 21:49:00 -04001297/**
James Smart3621a712009-04-06 18:47:14 -04001298 * lpfc_sli_ringtx_get - Get first element of the txq
James Smarte59058c2008-08-24 21:49:00 -04001299 * @phba: Pointer to HBA context object.
1300 * @pring: Pointer to driver SLI ring object.
1301 *
1302 * This function is called with hbalock held to get next
1303 * iocb in txq of the given ring. If there is any iocb in
1304 * the txq, the function returns first iocb in the list after
1305 * removing the iocb from the list, else it returns NULL.
1306 **/
James Smart2a9bf3d2010-06-07 15:24:45 -04001307struct lpfc_iocbq *
James Smart2e0fef82007-06-17 19:56:36 -05001308lpfc_sli_ringtx_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -05001309{
dea31012005-04-17 16:05:31 -05001310 struct lpfc_iocbq *cmd_iocb;
1311
James Smart858c9f62007-06-17 19:56:39 -05001312 list_remove_head((&pring->txq), cmd_iocb, struct lpfc_iocbq, list);
1313 if (cmd_iocb != NULL)
dea31012005-04-17 16:05:31 -05001314 pring->txq_cnt--;
James Smart2e0fef82007-06-17 19:56:36 -05001315 return cmd_iocb;
dea31012005-04-17 16:05:31 -05001316}
1317
James Smarte59058c2008-08-24 21:49:00 -04001318/**
James Smart3621a712009-04-06 18:47:14 -04001319 * lpfc_sli_next_iocb_slot - Get next iocb slot in the ring
James Smarte59058c2008-08-24 21:49:00 -04001320 * @phba: Pointer to HBA context object.
1321 * @pring: Pointer to driver SLI ring object.
1322 *
1323 * This function is called with hbalock held and the caller must post the
1324 * iocb without releasing the lock. If the caller releases the lock,
1325 * iocb slot returned by the function is not guaranteed to be available.
1326 * The function returns pointer to the next available iocb slot if there
1327 * is available slot in the ring, else it returns NULL.
1328 * If the get index of the ring is ahead of the put index, the function
1329 * will post an error attention event to the worker thread to take the
1330 * HBA to offline state.
1331 **/
dea31012005-04-17 16:05:31 -05001332static IOCB_t *
1333lpfc_sli_next_iocb_slot (struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
1334{
James Smart34b02dc2008-08-24 21:49:55 -04001335 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
dea31012005-04-17 16:05:31 -05001336 uint32_t max_cmd_idx = pring->numCiocb;
dea31012005-04-17 16:05:31 -05001337 if ((pring->next_cmdidx == pring->cmdidx) &&
1338 (++pring->next_cmdidx >= max_cmd_idx))
1339 pring->next_cmdidx = 0;
1340
1341 if (unlikely(pring->local_getidx == pring->next_cmdidx)) {
1342
1343 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
1344
1345 if (unlikely(pring->local_getidx >= max_cmd_idx)) {
1346 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001347 "0315 Ring %d issue: portCmdGet %d "
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02001348 "is bigger than cmd ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04001349 pring->ringno,
dea31012005-04-17 16:05:31 -05001350 pring->local_getidx, max_cmd_idx);
1351
James Smart2e0fef82007-06-17 19:56:36 -05001352 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05001353 /*
1354 * All error attention handlers are posted to
1355 * worker thread
1356 */
1357 phba->work_ha |= HA_ERATT;
1358 phba->work_hs = HS_FFER3;
James Smart92d7f7b2007-06-17 19:56:38 -05001359
James Smart5e9d9b82008-06-14 22:52:53 -04001360 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -05001361
1362 return NULL;
1363 }
1364
1365 if (pring->local_getidx == pring->next_cmdidx)
1366 return NULL;
1367 }
1368
James Smarted957682007-06-17 19:56:37 -05001369 return lpfc_cmd_iocb(phba, pring);
dea31012005-04-17 16:05:31 -05001370}
1371
James Smarte59058c2008-08-24 21:49:00 -04001372/**
James Smart3621a712009-04-06 18:47:14 -04001373 * lpfc_sli_next_iotag - Get an iotag for the iocb
James Smarte59058c2008-08-24 21:49:00 -04001374 * @phba: Pointer to HBA context object.
1375 * @iocbq: Pointer to driver iocb object.
1376 *
1377 * This function gets an iotag for the iocb. If there is no unused iotag and
1378 * the iocbq_lookup_len < 0xffff, this function allocates a bigger iotag_lookup
1379 * array and assigns a new iotag.
1380 * The function returns the allocated iotag if successful, else returns zero.
1381 * Zero is not a valid iotag.
1382 * The caller is not required to hold any lock.
1383 **/
James Bottomley604a3e32005-10-29 10:28:33 -05001384uint16_t
James Smart2e0fef82007-06-17 19:56:36 -05001385lpfc_sli_next_iotag(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
dea31012005-04-17 16:05:31 -05001386{
James Smart2e0fef82007-06-17 19:56:36 -05001387 struct lpfc_iocbq **new_arr;
1388 struct lpfc_iocbq **old_arr;
James Bottomley604a3e32005-10-29 10:28:33 -05001389 size_t new_len;
1390 struct lpfc_sli *psli = &phba->sli;
1391 uint16_t iotag;
dea31012005-04-17 16:05:31 -05001392
James Smart2e0fef82007-06-17 19:56:36 -05001393 spin_lock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001394 iotag = psli->last_iotag;
1395 if(++iotag < psli->iocbq_lookup_len) {
1396 psli->last_iotag = iotag;
1397 psli->iocbq_lookup[iotag] = iocbq;
James Smart2e0fef82007-06-17 19:56:36 -05001398 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001399 iocbq->iotag = iotag;
1400 return iotag;
James Smart2e0fef82007-06-17 19:56:36 -05001401 } else if (psli->iocbq_lookup_len < (0xffff
James Bottomley604a3e32005-10-29 10:28:33 -05001402 - LPFC_IOCBQ_LOOKUP_INCREMENT)) {
1403 new_len = psli->iocbq_lookup_len + LPFC_IOCBQ_LOOKUP_INCREMENT;
James Smart2e0fef82007-06-17 19:56:36 -05001404 spin_unlock_irq(&phba->hbalock);
1405 new_arr = kzalloc(new_len * sizeof (struct lpfc_iocbq *),
James Bottomley604a3e32005-10-29 10:28:33 -05001406 GFP_KERNEL);
1407 if (new_arr) {
James Smart2e0fef82007-06-17 19:56:36 -05001408 spin_lock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001409 old_arr = psli->iocbq_lookup;
1410 if (new_len <= psli->iocbq_lookup_len) {
1411 /* highly unprobable case */
1412 kfree(new_arr);
1413 iotag = psli->last_iotag;
1414 if(++iotag < psli->iocbq_lookup_len) {
1415 psli->last_iotag = iotag;
1416 psli->iocbq_lookup[iotag] = iocbq;
James Smart2e0fef82007-06-17 19:56:36 -05001417 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001418 iocbq->iotag = iotag;
1419 return iotag;
1420 }
James Smart2e0fef82007-06-17 19:56:36 -05001421 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001422 return 0;
1423 }
1424 if (psli->iocbq_lookup)
1425 memcpy(new_arr, old_arr,
1426 ((psli->last_iotag + 1) *
James Smart311464e2007-08-02 11:10:37 -04001427 sizeof (struct lpfc_iocbq *)));
James Bottomley604a3e32005-10-29 10:28:33 -05001428 psli->iocbq_lookup = new_arr;
1429 psli->iocbq_lookup_len = new_len;
1430 psli->last_iotag = iotag;
1431 psli->iocbq_lookup[iotag] = iocbq;
James Smart2e0fef82007-06-17 19:56:36 -05001432 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001433 iocbq->iotag = iotag;
1434 kfree(old_arr);
1435 return iotag;
1436 }
James Smart8f6d98d2006-08-01 07:34:00 -04001437 } else
James Smart2e0fef82007-06-17 19:56:36 -05001438 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001439
James Smartbc739052010-08-04 16:11:18 -04001440 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001441 "0318 Failed to allocate IOTAG.last IOTAG is %d\n",
1442 psli->last_iotag);
dea31012005-04-17 16:05:31 -05001443
James Bottomley604a3e32005-10-29 10:28:33 -05001444 return 0;
dea31012005-04-17 16:05:31 -05001445}
1446
James Smarte59058c2008-08-24 21:49:00 -04001447/**
James Smart3621a712009-04-06 18:47:14 -04001448 * lpfc_sli_submit_iocb - Submit an iocb to the firmware
James Smarte59058c2008-08-24 21:49:00 -04001449 * @phba: Pointer to HBA context object.
1450 * @pring: Pointer to driver SLI ring object.
1451 * @iocb: Pointer to iocb slot in the ring.
1452 * @nextiocb: Pointer to driver iocb object which need to be
1453 * posted to firmware.
1454 *
1455 * This function is called with hbalock held to post a new iocb to
1456 * the firmware. This function copies the new iocb to ring iocb slot and
1457 * updates the ring pointers. It adds the new iocb to txcmplq if there is
1458 * a completion call back for this iocb else the function will free the
1459 * iocb object.
1460 **/
dea31012005-04-17 16:05:31 -05001461static void
1462lpfc_sli_submit_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1463 IOCB_t *iocb, struct lpfc_iocbq *nextiocb)
1464{
1465 /*
James Bottomley604a3e32005-10-29 10:28:33 -05001466 * Set up an iotag
dea31012005-04-17 16:05:31 -05001467 */
James Bottomley604a3e32005-10-29 10:28:33 -05001468 nextiocb->iocb.ulpIoTag = (nextiocb->iocb_cmpl) ? nextiocb->iotag : 0;
dea31012005-04-17 16:05:31 -05001469
James Smarte2a0a9d2008-12-04 22:40:02 -05001470
James Smarta58cbd52007-08-02 11:09:43 -04001471 if (pring->ringno == LPFC_ELS_RING) {
1472 lpfc_debugfs_slow_ring_trc(phba,
1473 "IOCB cmd ring: wd4:x%08x wd6:x%08x wd7:x%08x",
1474 *(((uint32_t *) &nextiocb->iocb) + 4),
1475 *(((uint32_t *) &nextiocb->iocb) + 6),
1476 *(((uint32_t *) &nextiocb->iocb) + 7));
1477 }
1478
dea31012005-04-17 16:05:31 -05001479 /*
1480 * Issue iocb command to adapter
1481 */
James Smart92d7f7b2007-06-17 19:56:38 -05001482 lpfc_sli_pcimem_bcopy(&nextiocb->iocb, iocb, phba->iocb_cmd_size);
dea31012005-04-17 16:05:31 -05001483 wmb();
1484 pring->stats.iocb_cmd++;
1485
1486 /*
1487 * If there is no completion routine to call, we can release the
1488 * IOCB buffer back right now. For IOCBs, like QUE_RING_BUF,
1489 * that have no rsp ring completion, iocb_cmpl MUST be NULL.
1490 */
1491 if (nextiocb->iocb_cmpl)
1492 lpfc_sli_ringtxcmpl_put(phba, pring, nextiocb);
James Bottomley604a3e32005-10-29 10:28:33 -05001493 else
James Smart2e0fef82007-06-17 19:56:36 -05001494 __lpfc_sli_release_iocbq(phba, nextiocb);
dea31012005-04-17 16:05:31 -05001495
1496 /*
1497 * Let the HBA know what IOCB slot will be the next one the
1498 * driver will put a command into.
1499 */
1500 pring->cmdidx = pring->next_cmdidx;
James Smarted957682007-06-17 19:56:37 -05001501 writel(pring->cmdidx, &phba->host_gp[pring->ringno].cmdPutInx);
dea31012005-04-17 16:05:31 -05001502}
1503
James Smarte59058c2008-08-24 21:49:00 -04001504/**
James Smart3621a712009-04-06 18:47:14 -04001505 * lpfc_sli_update_full_ring - Update the chip attention register
James Smarte59058c2008-08-24 21:49:00 -04001506 * @phba: Pointer to HBA context object.
1507 * @pring: Pointer to driver SLI ring object.
1508 *
1509 * The caller is not required to hold any lock for calling this function.
1510 * This function updates the chip attention bits for the ring to inform firmware
1511 * that there are pending work to be done for this ring and requests an
1512 * interrupt when there is space available in the ring. This function is
1513 * called when the driver is unable to post more iocbs to the ring due
1514 * to unavailability of space in the ring.
1515 **/
dea31012005-04-17 16:05:31 -05001516static void
James Smart2e0fef82007-06-17 19:56:36 -05001517lpfc_sli_update_full_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -05001518{
1519 int ringno = pring->ringno;
1520
1521 pring->flag |= LPFC_CALL_RING_AVAILABLE;
1522
1523 wmb();
1524
1525 /*
1526 * Set ring 'ringno' to SET R0CE_REQ in Chip Att register.
1527 * The HBA will tell us when an IOCB entry is available.
1528 */
1529 writel((CA_R0ATT|CA_R0CE_REQ) << (ringno*4), phba->CAregaddr);
1530 readl(phba->CAregaddr); /* flush */
1531
1532 pring->stats.iocb_cmd_full++;
1533}
1534
James Smarte59058c2008-08-24 21:49:00 -04001535/**
James Smart3621a712009-04-06 18:47:14 -04001536 * lpfc_sli_update_ring - Update chip attention register
James Smarte59058c2008-08-24 21:49:00 -04001537 * @phba: Pointer to HBA context object.
1538 * @pring: Pointer to driver SLI ring object.
1539 *
1540 * This function updates the chip attention register bit for the
1541 * given ring to inform HBA that there is more work to be done
1542 * in this ring. The caller is not required to hold any lock.
1543 **/
dea31012005-04-17 16:05:31 -05001544static void
James Smart2e0fef82007-06-17 19:56:36 -05001545lpfc_sli_update_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -05001546{
1547 int ringno = pring->ringno;
1548
1549 /*
1550 * Tell the HBA that there is work to do in this ring.
1551 */
James Smart34b02dc2008-08-24 21:49:55 -04001552 if (!(phba->sli3_options & LPFC_SLI3_CRP_ENABLED)) {
1553 wmb();
1554 writel(CA_R0ATT << (ringno * 4), phba->CAregaddr);
1555 readl(phba->CAregaddr); /* flush */
1556 }
dea31012005-04-17 16:05:31 -05001557}
1558
James Smarte59058c2008-08-24 21:49:00 -04001559/**
James Smart3621a712009-04-06 18:47:14 -04001560 * lpfc_sli_resume_iocb - Process iocbs in the txq
James Smarte59058c2008-08-24 21:49:00 -04001561 * @phba: Pointer to HBA context object.
1562 * @pring: Pointer to driver SLI ring object.
1563 *
1564 * This function is called with hbalock held to post pending iocbs
1565 * in the txq to the firmware. This function is called when driver
1566 * detects space available in the ring.
1567 **/
dea31012005-04-17 16:05:31 -05001568static void
James Smart2e0fef82007-06-17 19:56:36 -05001569lpfc_sli_resume_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -05001570{
1571 IOCB_t *iocb;
1572 struct lpfc_iocbq *nextiocb;
1573
1574 /*
1575 * Check to see if:
1576 * (a) there is anything on the txq to send
1577 * (b) link is up
1578 * (c) link attention events can be processed (fcp ring only)
1579 * (d) IOCB processing is not blocked by the outstanding mbox command.
1580 */
1581 if (pring->txq_cnt &&
James Smart2e0fef82007-06-17 19:56:36 -05001582 lpfc_is_link_up(phba) &&
dea31012005-04-17 16:05:31 -05001583 (pring->ringno != phba->sli.fcp_ring ||
James Smart0b727fe2007-10-27 13:37:25 -04001584 phba->sli.sli_flag & LPFC_PROCESS_LA)) {
dea31012005-04-17 16:05:31 -05001585
1586 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
1587 (nextiocb = lpfc_sli_ringtx_get(phba, pring)))
1588 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
1589
1590 if (iocb)
1591 lpfc_sli_update_ring(phba, pring);
1592 else
1593 lpfc_sli_update_full_ring(phba, pring);
1594 }
1595
1596 return;
1597}
1598
James Smarte59058c2008-08-24 21:49:00 -04001599/**
James Smart3621a712009-04-06 18:47:14 -04001600 * lpfc_sli_next_hbq_slot - Get next hbq entry for the HBQ
James Smarte59058c2008-08-24 21:49:00 -04001601 * @phba: Pointer to HBA context object.
1602 * @hbqno: HBQ number.
1603 *
1604 * This function is called with hbalock held to get the next
1605 * available slot for the given HBQ. If there is free slot
1606 * available for the HBQ it will return pointer to the next available
1607 * HBQ entry else it will return NULL.
1608 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01001609static struct lpfc_hbq_entry *
James Smarted957682007-06-17 19:56:37 -05001610lpfc_sli_next_hbq_slot(struct lpfc_hba *phba, uint32_t hbqno)
1611{
1612 struct hbq_s *hbqp = &phba->hbqs[hbqno];
1613
1614 if (hbqp->next_hbqPutIdx == hbqp->hbqPutIdx &&
1615 ++hbqp->next_hbqPutIdx >= hbqp->entry_count)
1616 hbqp->next_hbqPutIdx = 0;
1617
1618 if (unlikely(hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)) {
James Smart92d7f7b2007-06-17 19:56:38 -05001619 uint32_t raw_index = phba->hbq_get[hbqno];
James Smarted957682007-06-17 19:56:37 -05001620 uint32_t getidx = le32_to_cpu(raw_index);
1621
1622 hbqp->local_hbqGetIdx = getidx;
1623
1624 if (unlikely(hbqp->local_hbqGetIdx >= hbqp->entry_count)) {
1625 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -05001626 LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04001627 "1802 HBQ %d: local_hbqGetIdx "
James Smarted957682007-06-17 19:56:37 -05001628 "%u is > than hbqp->entry_count %u\n",
James Smarte8b62012007-08-02 11:10:09 -04001629 hbqno, hbqp->local_hbqGetIdx,
James Smarted957682007-06-17 19:56:37 -05001630 hbqp->entry_count);
1631
1632 phba->link_state = LPFC_HBA_ERROR;
1633 return NULL;
1634 }
1635
1636 if (hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)
1637 return NULL;
1638 }
1639
James Smart51ef4c22007-08-02 11:10:31 -04001640 return (struct lpfc_hbq_entry *) phba->hbqs[hbqno].hbq_virt +
1641 hbqp->hbqPutIdx;
James Smarted957682007-06-17 19:56:37 -05001642}
1643
James Smarte59058c2008-08-24 21:49:00 -04001644/**
James Smart3621a712009-04-06 18:47:14 -04001645 * lpfc_sli_hbqbuf_free_all - Free all the hbq buffers
James Smarte59058c2008-08-24 21:49:00 -04001646 * @phba: Pointer to HBA context object.
1647 *
1648 * This function is called with no lock held to free all the
1649 * hbq buffers while uninitializing the SLI interface. It also
1650 * frees the HBQ buffers returned by the firmware but not yet
1651 * processed by the upper layers.
1652 **/
James Smarted957682007-06-17 19:56:37 -05001653void
1654lpfc_sli_hbqbuf_free_all(struct lpfc_hba *phba)
1655{
James Smart92d7f7b2007-06-17 19:56:38 -05001656 struct lpfc_dmabuf *dmabuf, *next_dmabuf;
1657 struct hbq_dmabuf *hbq_buf;
James Smart3163f722008-02-08 18:50:25 -05001658 unsigned long flags;
James Smart51ef4c22007-08-02 11:10:31 -04001659 int i, hbq_count;
James Smart3163f722008-02-08 18:50:25 -05001660 uint32_t hbqno;
James Smarted957682007-06-17 19:56:37 -05001661
James Smart51ef4c22007-08-02 11:10:31 -04001662 hbq_count = lpfc_sli_hbq_count();
James Smarted957682007-06-17 19:56:37 -05001663 /* Return all memory used by all HBQs */
James Smart3163f722008-02-08 18:50:25 -05001664 spin_lock_irqsave(&phba->hbalock, flags);
James Smart51ef4c22007-08-02 11:10:31 -04001665 for (i = 0; i < hbq_count; ++i) {
1666 list_for_each_entry_safe(dmabuf, next_dmabuf,
1667 &phba->hbqs[i].hbq_buffer_list, list) {
1668 hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
1669 list_del(&hbq_buf->dbuf.list);
1670 (phba->hbqs[i].hbq_free_buffer)(phba, hbq_buf);
1671 }
James Smarta8adb832007-10-27 13:37:53 -04001672 phba->hbqs[i].buffer_count = 0;
James Smarted957682007-06-17 19:56:37 -05001673 }
James Smart3163f722008-02-08 18:50:25 -05001674 /* Return all HBQ buffer that are in-fly */
James Smart3772a992009-05-22 14:50:54 -04001675 list_for_each_entry_safe(dmabuf, next_dmabuf, &phba->rb_pend_list,
1676 list) {
James Smart3163f722008-02-08 18:50:25 -05001677 hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
1678 list_del(&hbq_buf->dbuf.list);
1679 if (hbq_buf->tag == -1) {
1680 (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
1681 (phba, hbq_buf);
1682 } else {
1683 hbqno = hbq_buf->tag >> 16;
1684 if (hbqno >= LPFC_MAX_HBQS)
1685 (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
1686 (phba, hbq_buf);
1687 else
1688 (phba->hbqs[hbqno].hbq_free_buffer)(phba,
1689 hbq_buf);
1690 }
1691 }
1692
1693 /* Mark the HBQs not in use */
1694 phba->hbq_in_use = 0;
1695 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smarted957682007-06-17 19:56:37 -05001696}
1697
James Smarte59058c2008-08-24 21:49:00 -04001698/**
James Smart3621a712009-04-06 18:47:14 -04001699 * lpfc_sli_hbq_to_firmware - Post the hbq buffer to firmware
James Smarte59058c2008-08-24 21:49:00 -04001700 * @phba: Pointer to HBA context object.
1701 * @hbqno: HBQ number.
1702 * @hbq_buf: Pointer to HBQ buffer.
1703 *
1704 * This function is called with the hbalock held to post a
1705 * hbq buffer to the firmware. If the function finds an empty
1706 * slot in the HBQ, it will post the buffer. The function will return
1707 * pointer to the hbq entry if it successfully post the buffer
1708 * else it will return NULL.
1709 **/
James Smart3772a992009-05-22 14:50:54 -04001710static int
James Smarted957682007-06-17 19:56:37 -05001711lpfc_sli_hbq_to_firmware(struct lpfc_hba *phba, uint32_t hbqno,
James Smart92d7f7b2007-06-17 19:56:38 -05001712 struct hbq_dmabuf *hbq_buf)
James Smarted957682007-06-17 19:56:37 -05001713{
James Smart3772a992009-05-22 14:50:54 -04001714 return phba->lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buf);
1715}
1716
1717/**
1718 * lpfc_sli_hbq_to_firmware_s3 - Post the hbq buffer to SLI3 firmware
1719 * @phba: Pointer to HBA context object.
1720 * @hbqno: HBQ number.
1721 * @hbq_buf: Pointer to HBQ buffer.
1722 *
1723 * This function is called with the hbalock held to post a hbq buffer to the
1724 * firmware. If the function finds an empty slot in the HBQ, it will post the
1725 * buffer and place it on the hbq_buffer_list. The function will return zero if
1726 * it successfully post the buffer else it will return an error.
1727 **/
1728static int
1729lpfc_sli_hbq_to_firmware_s3(struct lpfc_hba *phba, uint32_t hbqno,
1730 struct hbq_dmabuf *hbq_buf)
1731{
James Smarted957682007-06-17 19:56:37 -05001732 struct lpfc_hbq_entry *hbqe;
James Smart92d7f7b2007-06-17 19:56:38 -05001733 dma_addr_t physaddr = hbq_buf->dbuf.phys;
James Smarted957682007-06-17 19:56:37 -05001734
1735 /* Get next HBQ entry slot to use */
1736 hbqe = lpfc_sli_next_hbq_slot(phba, hbqno);
1737 if (hbqe) {
1738 struct hbq_s *hbqp = &phba->hbqs[hbqno];
1739
James Smart92d7f7b2007-06-17 19:56:38 -05001740 hbqe->bde.addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
1741 hbqe->bde.addrLow = le32_to_cpu(putPaddrLow(physaddr));
James Smart51ef4c22007-08-02 11:10:31 -04001742 hbqe->bde.tus.f.bdeSize = hbq_buf->size;
James Smarted957682007-06-17 19:56:37 -05001743 hbqe->bde.tus.f.bdeFlags = 0;
James Smart92d7f7b2007-06-17 19:56:38 -05001744 hbqe->bde.tus.w = le32_to_cpu(hbqe->bde.tus.w);
1745 hbqe->buffer_tag = le32_to_cpu(hbq_buf->tag);
1746 /* Sync SLIM */
James Smarted957682007-06-17 19:56:37 -05001747 hbqp->hbqPutIdx = hbqp->next_hbqPutIdx;
1748 writel(hbqp->hbqPutIdx, phba->hbq_put + hbqno);
James Smart92d7f7b2007-06-17 19:56:38 -05001749 /* flush */
James Smarted957682007-06-17 19:56:37 -05001750 readl(phba->hbq_put + hbqno);
James Smart51ef4c22007-08-02 11:10:31 -04001751 list_add_tail(&hbq_buf->dbuf.list, &hbqp->hbq_buffer_list);
James Smart3772a992009-05-22 14:50:54 -04001752 return 0;
1753 } else
1754 return -ENOMEM;
James Smarted957682007-06-17 19:56:37 -05001755}
1756
James Smart4f774512009-05-22 14:52:35 -04001757/**
1758 * lpfc_sli_hbq_to_firmware_s4 - Post the hbq buffer to SLI4 firmware
1759 * @phba: Pointer to HBA context object.
1760 * @hbqno: HBQ number.
1761 * @hbq_buf: Pointer to HBQ buffer.
1762 *
1763 * This function is called with the hbalock held to post an RQE to the SLI4
1764 * firmware. If able to post the RQE to the RQ it will queue the hbq entry to
1765 * the hbq_buffer_list and return zero, otherwise it will return an error.
1766 **/
1767static int
1768lpfc_sli_hbq_to_firmware_s4(struct lpfc_hba *phba, uint32_t hbqno,
1769 struct hbq_dmabuf *hbq_buf)
1770{
1771 int rc;
1772 struct lpfc_rqe hrqe;
1773 struct lpfc_rqe drqe;
1774
1775 hrqe.address_lo = putPaddrLow(hbq_buf->hbuf.phys);
1776 hrqe.address_hi = putPaddrHigh(hbq_buf->hbuf.phys);
1777 drqe.address_lo = putPaddrLow(hbq_buf->dbuf.phys);
1778 drqe.address_hi = putPaddrHigh(hbq_buf->dbuf.phys);
1779 rc = lpfc_sli4_rq_put(phba->sli4_hba.hdr_rq, phba->sli4_hba.dat_rq,
1780 &hrqe, &drqe);
1781 if (rc < 0)
1782 return rc;
1783 hbq_buf->tag = rc;
1784 list_add_tail(&hbq_buf->dbuf.list, &phba->hbqs[hbqno].hbq_buffer_list);
1785 return 0;
1786}
1787
James Smarte59058c2008-08-24 21:49:00 -04001788/* HBQ for ELS and CT traffic. */
James Smart92d7f7b2007-06-17 19:56:38 -05001789static struct lpfc_hbq_init lpfc_els_hbq = {
1790 .rn = 1,
James Smartdef9c7a2009-12-21 17:02:28 -05001791 .entry_count = 256,
James Smart92d7f7b2007-06-17 19:56:38 -05001792 .mask_count = 0,
1793 .profile = 0,
James Smart51ef4c22007-08-02 11:10:31 -04001794 .ring_mask = (1 << LPFC_ELS_RING),
James Smart92d7f7b2007-06-17 19:56:38 -05001795 .buffer_count = 0,
James Smarta257bf92009-04-06 18:48:10 -04001796 .init_count = 40,
1797 .add_count = 40,
James Smart92d7f7b2007-06-17 19:56:38 -05001798};
James Smarted957682007-06-17 19:56:37 -05001799
James Smarte59058c2008-08-24 21:49:00 -04001800/* HBQ for the extra ring if needed */
James Smart51ef4c22007-08-02 11:10:31 -04001801static struct lpfc_hbq_init lpfc_extra_hbq = {
1802 .rn = 1,
1803 .entry_count = 200,
1804 .mask_count = 0,
1805 .profile = 0,
1806 .ring_mask = (1 << LPFC_EXTRA_RING),
1807 .buffer_count = 0,
1808 .init_count = 0,
1809 .add_count = 5,
1810};
1811
James Smarte59058c2008-08-24 21:49:00 -04001812/* Array of HBQs */
James Smart78b2d852007-08-02 11:10:21 -04001813struct lpfc_hbq_init *lpfc_hbq_defs[] = {
James Smart92d7f7b2007-06-17 19:56:38 -05001814 &lpfc_els_hbq,
James Smart51ef4c22007-08-02 11:10:31 -04001815 &lpfc_extra_hbq,
James Smart92d7f7b2007-06-17 19:56:38 -05001816};
1817
James Smarte59058c2008-08-24 21:49:00 -04001818/**
James Smart3621a712009-04-06 18:47:14 -04001819 * lpfc_sli_hbqbuf_fill_hbqs - Post more hbq buffers to HBQ
James Smarte59058c2008-08-24 21:49:00 -04001820 * @phba: Pointer to HBA context object.
1821 * @hbqno: HBQ number.
1822 * @count: Number of HBQ buffers to be posted.
1823 *
James Smartd7c255b2008-08-24 21:50:00 -04001824 * This function is called with no lock held to post more hbq buffers to the
1825 * given HBQ. The function returns the number of HBQ buffers successfully
1826 * posted.
James Smarte59058c2008-08-24 21:49:00 -04001827 **/
James Smart311464e2007-08-02 11:10:37 -04001828static int
James Smart92d7f7b2007-06-17 19:56:38 -05001829lpfc_sli_hbqbuf_fill_hbqs(struct lpfc_hba *phba, uint32_t hbqno, uint32_t count)
1830{
James Smartd7c255b2008-08-24 21:50:00 -04001831 uint32_t i, posted = 0;
James Smart3163f722008-02-08 18:50:25 -05001832 unsigned long flags;
James Smart92d7f7b2007-06-17 19:56:38 -05001833 struct hbq_dmabuf *hbq_buffer;
James Smartd7c255b2008-08-24 21:50:00 -04001834 LIST_HEAD(hbq_buf_list);
Matthew Wilcoxeafe1df2008-02-21 05:44:33 -07001835 if (!phba->hbqs[hbqno].hbq_alloc_buffer)
James Smart51ef4c22007-08-02 11:10:31 -04001836 return 0;
James Smart51ef4c22007-08-02 11:10:31 -04001837
James Smartd7c255b2008-08-24 21:50:00 -04001838 if ((phba->hbqs[hbqno].buffer_count + count) >
1839 lpfc_hbq_defs[hbqno]->entry_count)
1840 count = lpfc_hbq_defs[hbqno]->entry_count -
1841 phba->hbqs[hbqno].buffer_count;
1842 if (!count)
1843 return 0;
1844 /* Allocate HBQ entries */
1845 for (i = 0; i < count; i++) {
1846 hbq_buffer = (phba->hbqs[hbqno].hbq_alloc_buffer)(phba);
1847 if (!hbq_buffer)
1848 break;
1849 list_add_tail(&hbq_buffer->dbuf.list, &hbq_buf_list);
1850 }
James Smart3163f722008-02-08 18:50:25 -05001851 /* Check whether HBQ is still in use */
1852 spin_lock_irqsave(&phba->hbalock, flags);
Matthew Wilcoxeafe1df2008-02-21 05:44:33 -07001853 if (!phba->hbq_in_use)
James Smartd7c255b2008-08-24 21:50:00 -04001854 goto err;
1855 while (!list_empty(&hbq_buf_list)) {
1856 list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
1857 dbuf.list);
1858 hbq_buffer->tag = (phba->hbqs[hbqno].buffer_count |
1859 (hbqno << 16));
James Smart3772a992009-05-22 14:50:54 -04001860 if (!lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer)) {
James Smarta8adb832007-10-27 13:37:53 -04001861 phba->hbqs[hbqno].buffer_count++;
James Smartd7c255b2008-08-24 21:50:00 -04001862 posted++;
1863 } else
James Smart51ef4c22007-08-02 11:10:31 -04001864 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
James Smart92d7f7b2007-06-17 19:56:38 -05001865 }
James Smart3163f722008-02-08 18:50:25 -05001866 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smartd7c255b2008-08-24 21:50:00 -04001867 return posted;
1868err:
1869 spin_unlock_irqrestore(&phba->hbalock, flags);
1870 while (!list_empty(&hbq_buf_list)) {
1871 list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
1872 dbuf.list);
1873 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
1874 }
James Smart92d7f7b2007-06-17 19:56:38 -05001875 return 0;
James Smarted957682007-06-17 19:56:37 -05001876}
1877
James Smarte59058c2008-08-24 21:49:00 -04001878/**
James Smart3621a712009-04-06 18:47:14 -04001879 * lpfc_sli_hbqbuf_add_hbqs - Post more HBQ buffers to firmware
James Smarte59058c2008-08-24 21:49:00 -04001880 * @phba: Pointer to HBA context object.
1881 * @qno: HBQ number.
1882 *
1883 * This function posts more buffers to the HBQ. This function
James Smartd7c255b2008-08-24 21:50:00 -04001884 * is called with no lock held. The function returns the number of HBQ entries
1885 * successfully allocated.
James Smarte59058c2008-08-24 21:49:00 -04001886 **/
James Smarted957682007-06-17 19:56:37 -05001887int
James Smart92d7f7b2007-06-17 19:56:38 -05001888lpfc_sli_hbqbuf_add_hbqs(struct lpfc_hba *phba, uint32_t qno)
James Smarted957682007-06-17 19:56:37 -05001889{
James Smartdef9c7a2009-12-21 17:02:28 -05001890 if (phba->sli_rev == LPFC_SLI_REV4)
1891 return 0;
1892 else
1893 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
1894 lpfc_hbq_defs[qno]->add_count);
James Smarted957682007-06-17 19:56:37 -05001895}
1896
James Smarte59058c2008-08-24 21:49:00 -04001897/**
James Smart3621a712009-04-06 18:47:14 -04001898 * lpfc_sli_hbqbuf_init_hbqs - Post initial buffers to the HBQ
James Smarte59058c2008-08-24 21:49:00 -04001899 * @phba: Pointer to HBA context object.
1900 * @qno: HBQ queue number.
1901 *
1902 * This function is called from SLI initialization code path with
1903 * no lock held to post initial HBQ buffers to firmware. The
James Smartd7c255b2008-08-24 21:50:00 -04001904 * function returns the number of HBQ entries successfully allocated.
James Smarte59058c2008-08-24 21:49:00 -04001905 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01001906static int
James Smart92d7f7b2007-06-17 19:56:38 -05001907lpfc_sli_hbqbuf_init_hbqs(struct lpfc_hba *phba, uint32_t qno)
James Smarted957682007-06-17 19:56:37 -05001908{
James Smartdef9c7a2009-12-21 17:02:28 -05001909 if (phba->sli_rev == LPFC_SLI_REV4)
1910 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
James Smart73d91e52011-10-10 21:32:10 -04001911 lpfc_hbq_defs[qno]->entry_count);
James Smartdef9c7a2009-12-21 17:02:28 -05001912 else
1913 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
1914 lpfc_hbq_defs[qno]->init_count);
James Smarted957682007-06-17 19:56:37 -05001915}
1916
James Smarte59058c2008-08-24 21:49:00 -04001917/**
James Smart3772a992009-05-22 14:50:54 -04001918 * lpfc_sli_hbqbuf_get - Remove the first hbq off of an hbq list
1919 * @phba: Pointer to HBA context object.
1920 * @hbqno: HBQ number.
1921 *
1922 * This function removes the first hbq buffer on an hbq list and returns a
1923 * pointer to that buffer. If it finds no buffers on the list it returns NULL.
1924 **/
1925static struct hbq_dmabuf *
1926lpfc_sli_hbqbuf_get(struct list_head *rb_list)
1927{
1928 struct lpfc_dmabuf *d_buf;
1929
1930 list_remove_head(rb_list, d_buf, struct lpfc_dmabuf, list);
1931 if (!d_buf)
1932 return NULL;
1933 return container_of(d_buf, struct hbq_dmabuf, dbuf);
1934}
1935
1936/**
James Smart3621a712009-04-06 18:47:14 -04001937 * lpfc_sli_hbqbuf_find - Find the hbq buffer associated with a tag
James Smarte59058c2008-08-24 21:49:00 -04001938 * @phba: Pointer to HBA context object.
1939 * @tag: Tag of the hbq buffer.
1940 *
1941 * This function is called with hbalock held. This function searches
1942 * for the hbq buffer associated with the given tag in the hbq buffer
1943 * list. If it finds the hbq buffer, it returns the hbq_buffer other wise
1944 * it returns NULL.
1945 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01001946static struct hbq_dmabuf *
James Smarted957682007-06-17 19:56:37 -05001947lpfc_sli_hbqbuf_find(struct lpfc_hba *phba, uint32_t tag)
1948{
James Smart92d7f7b2007-06-17 19:56:38 -05001949 struct lpfc_dmabuf *d_buf;
1950 struct hbq_dmabuf *hbq_buf;
James Smart51ef4c22007-08-02 11:10:31 -04001951 uint32_t hbqno;
James Smarted957682007-06-17 19:56:37 -05001952
James Smart51ef4c22007-08-02 11:10:31 -04001953 hbqno = tag >> 16;
Jesper Juhla0a74e452007-08-09 20:47:15 +02001954 if (hbqno >= LPFC_MAX_HBQS)
James Smart51ef4c22007-08-02 11:10:31 -04001955 return NULL;
1956
James Smart3772a992009-05-22 14:50:54 -04001957 spin_lock_irq(&phba->hbalock);
James Smart51ef4c22007-08-02 11:10:31 -04001958 list_for_each_entry(d_buf, &phba->hbqs[hbqno].hbq_buffer_list, list) {
James Smart92d7f7b2007-06-17 19:56:38 -05001959 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
James Smart51ef4c22007-08-02 11:10:31 -04001960 if (hbq_buf->tag == tag) {
James Smart3772a992009-05-22 14:50:54 -04001961 spin_unlock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -05001962 return hbq_buf;
James Smarted957682007-06-17 19:56:37 -05001963 }
1964 }
James Smart3772a992009-05-22 14:50:54 -04001965 spin_unlock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -05001966 lpfc_printf_log(phba, KERN_ERR, LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04001967 "1803 Bad hbq tag. Data: x%x x%x\n",
James Smarta8adb832007-10-27 13:37:53 -04001968 tag, phba->hbqs[tag >> 16].buffer_count);
James Smart92d7f7b2007-06-17 19:56:38 -05001969 return NULL;
James Smarted957682007-06-17 19:56:37 -05001970}
1971
James Smarte59058c2008-08-24 21:49:00 -04001972/**
James Smart3621a712009-04-06 18:47:14 -04001973 * lpfc_sli_free_hbq - Give back the hbq buffer to firmware
James Smarte59058c2008-08-24 21:49:00 -04001974 * @phba: Pointer to HBA context object.
1975 * @hbq_buffer: Pointer to HBQ buffer.
1976 *
1977 * This function is called with hbalock. This function gives back
1978 * the hbq buffer to firmware. If the HBQ does not have space to
1979 * post the buffer, it will free the buffer.
1980 **/
James Smarted957682007-06-17 19:56:37 -05001981void
James Smart51ef4c22007-08-02 11:10:31 -04001982lpfc_sli_free_hbq(struct lpfc_hba *phba, struct hbq_dmabuf *hbq_buffer)
James Smarted957682007-06-17 19:56:37 -05001983{
1984 uint32_t hbqno;
1985
James Smart51ef4c22007-08-02 11:10:31 -04001986 if (hbq_buffer) {
1987 hbqno = hbq_buffer->tag >> 16;
James Smart3772a992009-05-22 14:50:54 -04001988 if (lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer))
James Smart51ef4c22007-08-02 11:10:31 -04001989 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
James Smarted957682007-06-17 19:56:37 -05001990 }
1991}
1992
James Smarte59058c2008-08-24 21:49:00 -04001993/**
James Smart3621a712009-04-06 18:47:14 -04001994 * lpfc_sli_chk_mbx_command - Check if the mailbox is a legitimate mailbox
James Smarte59058c2008-08-24 21:49:00 -04001995 * @mbxCommand: mailbox command code.
1996 *
1997 * This function is called by the mailbox event handler function to verify
1998 * that the completed mailbox command is a legitimate mailbox command. If the
1999 * completed mailbox is not known to the function, it will return MBX_SHUTDOWN
2000 * and the mailbox event handler will take the HBA offline.
2001 **/
dea31012005-04-17 16:05:31 -05002002static int
2003lpfc_sli_chk_mbx_command(uint8_t mbxCommand)
2004{
2005 uint8_t ret;
2006
2007 switch (mbxCommand) {
2008 case MBX_LOAD_SM:
2009 case MBX_READ_NV:
2010 case MBX_WRITE_NV:
James Smarta8adb832007-10-27 13:37:53 -04002011 case MBX_WRITE_VPARMS:
dea31012005-04-17 16:05:31 -05002012 case MBX_RUN_BIU_DIAG:
2013 case MBX_INIT_LINK:
2014 case MBX_DOWN_LINK:
2015 case MBX_CONFIG_LINK:
2016 case MBX_CONFIG_RING:
2017 case MBX_RESET_RING:
2018 case MBX_READ_CONFIG:
2019 case MBX_READ_RCONFIG:
2020 case MBX_READ_SPARM:
2021 case MBX_READ_STATUS:
2022 case MBX_READ_RPI:
2023 case MBX_READ_XRI:
2024 case MBX_READ_REV:
2025 case MBX_READ_LNK_STAT:
2026 case MBX_REG_LOGIN:
2027 case MBX_UNREG_LOGIN:
dea31012005-04-17 16:05:31 -05002028 case MBX_CLEAR_LA:
2029 case MBX_DUMP_MEMORY:
2030 case MBX_DUMP_CONTEXT:
2031 case MBX_RUN_DIAGS:
2032 case MBX_RESTART:
2033 case MBX_UPDATE_CFG:
2034 case MBX_DOWN_LOAD:
2035 case MBX_DEL_LD_ENTRY:
2036 case MBX_RUN_PROGRAM:
2037 case MBX_SET_MASK:
James Smart09372822008-01-11 01:52:54 -05002038 case MBX_SET_VARIABLE:
dea31012005-04-17 16:05:31 -05002039 case MBX_UNREG_D_ID:
Jamie Wellnitz41415862006-02-28 19:25:27 -05002040 case MBX_KILL_BOARD:
dea31012005-04-17 16:05:31 -05002041 case MBX_CONFIG_FARP:
Jamie Wellnitz41415862006-02-28 19:25:27 -05002042 case MBX_BEACON:
dea31012005-04-17 16:05:31 -05002043 case MBX_LOAD_AREA:
2044 case MBX_RUN_BIU_DIAG64:
2045 case MBX_CONFIG_PORT:
2046 case MBX_READ_SPARM64:
2047 case MBX_READ_RPI64:
2048 case MBX_REG_LOGIN64:
James Smart76a95d72010-11-20 23:11:48 -05002049 case MBX_READ_TOPOLOGY:
James Smart09372822008-01-11 01:52:54 -05002050 case MBX_WRITE_WWN:
dea31012005-04-17 16:05:31 -05002051 case MBX_SET_DEBUG:
2052 case MBX_LOAD_EXP_ROM:
James Smart57127f12007-10-27 13:37:05 -04002053 case MBX_ASYNCEVT_ENABLE:
James Smart92d7f7b2007-06-17 19:56:38 -05002054 case MBX_REG_VPI:
2055 case MBX_UNREG_VPI:
James Smart858c9f62007-06-17 19:56:39 -05002056 case MBX_HEARTBEAT:
James Smart84774a42008-08-24 21:50:06 -04002057 case MBX_PORT_CAPABILITIES:
2058 case MBX_PORT_IOV_CONTROL:
James Smart04c68492009-05-22 14:52:52 -04002059 case MBX_SLI4_CONFIG:
2060 case MBX_SLI4_REQ_FTRS:
2061 case MBX_REG_FCFI:
2062 case MBX_UNREG_FCFI:
2063 case MBX_REG_VFI:
2064 case MBX_UNREG_VFI:
2065 case MBX_INIT_VPI:
2066 case MBX_INIT_VFI:
2067 case MBX_RESUME_RPI:
James Smartc7495932010-04-06 15:05:28 -04002068 case MBX_READ_EVENT_LOG_STATUS:
2069 case MBX_READ_EVENT_LOG:
James Smartdcf2a4e2010-09-29 11:18:53 -04002070 case MBX_SECURITY_MGMT:
2071 case MBX_AUTH_PORT:
dea31012005-04-17 16:05:31 -05002072 ret = mbxCommand;
2073 break;
2074 default:
2075 ret = MBX_SHUTDOWN;
2076 break;
2077 }
James Smart2e0fef82007-06-17 19:56:36 -05002078 return ret;
dea31012005-04-17 16:05:31 -05002079}
James Smarte59058c2008-08-24 21:49:00 -04002080
2081/**
James Smart3621a712009-04-06 18:47:14 -04002082 * lpfc_sli_wake_mbox_wait - lpfc_sli_issue_mbox_wait mbox completion handler
James Smarte59058c2008-08-24 21:49:00 -04002083 * @phba: Pointer to HBA context object.
2084 * @pmboxq: Pointer to mailbox command.
2085 *
2086 * This is completion handler function for mailbox commands issued from
2087 * lpfc_sli_issue_mbox_wait function. This function is called by the
2088 * mailbox event handler function with no lock held. This function
2089 * will wake up thread waiting on the wait queue pointed by context1
2090 * of the mailbox.
2091 **/
James Smart04c68492009-05-22 14:52:52 -04002092void
James Smart2e0fef82007-06-17 19:56:36 -05002093lpfc_sli_wake_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
dea31012005-04-17 16:05:31 -05002094{
2095 wait_queue_head_t *pdone_q;
James Smart858c9f62007-06-17 19:56:39 -05002096 unsigned long drvr_flag;
dea31012005-04-17 16:05:31 -05002097
2098 /*
2099 * If pdone_q is empty, the driver thread gave up waiting and
2100 * continued running.
2101 */
James Smart7054a602007-04-25 09:52:34 -04002102 pmboxq->mbox_flag |= LPFC_MBX_WAKE;
James Smart858c9f62007-06-17 19:56:39 -05002103 spin_lock_irqsave(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05002104 pdone_q = (wait_queue_head_t *) pmboxq->context1;
2105 if (pdone_q)
2106 wake_up_interruptible(pdone_q);
James Smart858c9f62007-06-17 19:56:39 -05002107 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05002108 return;
2109}
2110
James Smarte59058c2008-08-24 21:49:00 -04002111
2112/**
James Smart3621a712009-04-06 18:47:14 -04002113 * lpfc_sli_def_mbox_cmpl - Default mailbox completion handler
James Smarte59058c2008-08-24 21:49:00 -04002114 * @phba: Pointer to HBA context object.
2115 * @pmb: Pointer to mailbox object.
2116 *
2117 * This function is the default mailbox completion handler. It
2118 * frees the memory resources associated with the completed mailbox
2119 * command. If the completed command is a REG_LOGIN mailbox command,
2120 * this function will issue a UREG_LOGIN to re-claim the RPI.
2121 **/
dea31012005-04-17 16:05:31 -05002122void
James Smart2e0fef82007-06-17 19:56:36 -05002123lpfc_sli_def_mbox_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05002124{
James Smartd439d282010-09-29 11:18:45 -04002125 struct lpfc_vport *vport = pmb->vport;
dea31012005-04-17 16:05:31 -05002126 struct lpfc_dmabuf *mp;
James Smartd439d282010-09-29 11:18:45 -04002127 struct lpfc_nodelist *ndlp;
James Smart5af5eee2010-10-22 11:06:38 -04002128 struct Scsi_Host *shost;
James Smart04c68492009-05-22 14:52:52 -04002129 uint16_t rpi, vpi;
James Smart7054a602007-04-25 09:52:34 -04002130 int rc;
2131
dea31012005-04-17 16:05:31 -05002132 mp = (struct lpfc_dmabuf *) (pmb->context1);
James Smart7054a602007-04-25 09:52:34 -04002133
dea31012005-04-17 16:05:31 -05002134 if (mp) {
2135 lpfc_mbuf_free(phba, mp->virt, mp->phys);
2136 kfree(mp);
2137 }
James Smart7054a602007-04-25 09:52:34 -04002138
2139 /*
2140 * If a REG_LOGIN succeeded after node is destroyed or node
2141 * is in re-discovery driver need to cleanup the RPI.
2142 */
James Smart2e0fef82007-06-17 19:56:36 -05002143 if (!(phba->pport->load_flag & FC_UNLOADING) &&
James Smart04c68492009-05-22 14:52:52 -04002144 pmb->u.mb.mbxCommand == MBX_REG_LOGIN64 &&
2145 !pmb->u.mb.mbxStatus) {
2146 rpi = pmb->u.mb.un.varWords[0];
James Smart6d368e52011-05-24 11:44:12 -04002147 vpi = pmb->u.mb.un.varRegLogin.vpi;
James Smart04c68492009-05-22 14:52:52 -04002148 lpfc_unreg_login(phba, vpi, rpi, pmb);
James Smart92d7f7b2007-06-17 19:56:38 -05002149 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart7054a602007-04-25 09:52:34 -04002150 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
2151 if (rc != MBX_NOT_FINISHED)
2152 return;
2153 }
2154
James Smart695a8142010-01-26 23:08:03 -05002155 if ((pmb->u.mb.mbxCommand == MBX_REG_VPI) &&
2156 !(phba->pport->load_flag & FC_UNLOADING) &&
2157 !pmb->u.mb.mbxStatus) {
James Smart5af5eee2010-10-22 11:06:38 -04002158 shost = lpfc_shost_from_vport(vport);
2159 spin_lock_irq(shost->host_lock);
2160 vport->vpi_state |= LPFC_VPI_REGISTERED;
2161 vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
2162 spin_unlock_irq(shost->host_lock);
James Smart695a8142010-01-26 23:08:03 -05002163 }
2164
James Smartd439d282010-09-29 11:18:45 -04002165 if (pmb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
2166 ndlp = (struct lpfc_nodelist *)pmb->context2;
2167 lpfc_nlp_put(ndlp);
2168 pmb->context2 = NULL;
2169 }
2170
James Smartdcf2a4e2010-09-29 11:18:53 -04002171 /* Check security permission status on INIT_LINK mailbox command */
2172 if ((pmb->u.mb.mbxCommand == MBX_INIT_LINK) &&
2173 (pmb->u.mb.mbxStatus == MBXERR_SEC_NO_PERMISSION))
2174 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
2175 "2860 SLI authentication is required "
2176 "for INIT_LINK but has not done yet\n");
2177
James Smart04c68492009-05-22 14:52:52 -04002178 if (bf_get(lpfc_mqe_command, &pmb->u.mqe) == MBX_SLI4_CONFIG)
2179 lpfc_sli4_mbox_cmd_free(phba, pmb);
2180 else
2181 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05002182}
2183
James Smarte59058c2008-08-24 21:49:00 -04002184/**
James Smart3621a712009-04-06 18:47:14 -04002185 * lpfc_sli_handle_mb_event - Handle mailbox completions from firmware
James Smarte59058c2008-08-24 21:49:00 -04002186 * @phba: Pointer to HBA context object.
2187 *
2188 * This function is called with no lock held. This function processes all
2189 * the completed mailbox commands and gives it to upper layers. The interrupt
2190 * service routine processes mailbox completion interrupt and adds completed
2191 * mailbox commands to the mboxq_cmpl queue and signals the worker thread.
2192 * Worker thread call lpfc_sli_handle_mb_event, which will return the
2193 * completed mailbox commands in mboxq_cmpl queue to the upper layers. This
2194 * function returns the mailbox commands to the upper layer by calling the
2195 * completion handler function of each mailbox.
2196 **/
dea31012005-04-17 16:05:31 -05002197int
James Smart2e0fef82007-06-17 19:56:36 -05002198lpfc_sli_handle_mb_event(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05002199{
James Smart92d7f7b2007-06-17 19:56:38 -05002200 MAILBOX_t *pmbox;
dea31012005-04-17 16:05:31 -05002201 LPFC_MBOXQ_t *pmb;
James Smart92d7f7b2007-06-17 19:56:38 -05002202 int rc;
2203 LIST_HEAD(cmplq);
dea31012005-04-17 16:05:31 -05002204
2205 phba->sli.slistat.mbox_event++;
2206
James Smart92d7f7b2007-06-17 19:56:38 -05002207 /* Get all completed mailboxe buffers into the cmplq */
2208 spin_lock_irq(&phba->hbalock);
2209 list_splice_init(&phba->sli.mboxq_cmpl, &cmplq);
2210 spin_unlock_irq(&phba->hbalock);
2211
dea31012005-04-17 16:05:31 -05002212 /* Get a Mailbox buffer to setup mailbox commands for callback */
James Smart92d7f7b2007-06-17 19:56:38 -05002213 do {
2214 list_remove_head(&cmplq, pmb, LPFC_MBOXQ_t, list);
2215 if (pmb == NULL)
2216 break;
2217
James Smart04c68492009-05-22 14:52:52 -04002218 pmbox = &pmb->u.mb;
dea31012005-04-17 16:05:31 -05002219
James Smart858c9f62007-06-17 19:56:39 -05002220 if (pmbox->mbxCommand != MBX_HEARTBEAT) {
2221 if (pmb->vport) {
2222 lpfc_debugfs_disc_trc(pmb->vport,
2223 LPFC_DISC_TRC_MBOX_VPORT,
2224 "MBOX cmpl vport: 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 else {
2230 lpfc_debugfs_disc_trc(phba->pport,
2231 LPFC_DISC_TRC_MBOX,
2232 "MBOX cmpl: cmd:x%x mb:x%x x%x",
2233 (uint32_t)pmbox->mbxCommand,
2234 pmbox->un.varWords[0],
2235 pmbox->un.varWords[1]);
2236 }
2237 }
2238
dea31012005-04-17 16:05:31 -05002239 /*
2240 * It is a fatal error if unknown mbox command completion.
2241 */
2242 if (lpfc_sli_chk_mbx_command(pmbox->mbxCommand) ==
2243 MBX_SHUTDOWN) {
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02002244 /* Unknown mailbox command compl */
James Smart92d7f7b2007-06-17 19:56:38 -05002245 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002246 "(%d):0323 Unknown Mailbox command "
James Smarta183a152011-10-10 21:32:43 -04002247 "x%x (x%x/x%x) Cmpl\n",
James Smart92d7f7b2007-06-17 19:56:38 -05002248 pmb->vport ? pmb->vport->vpi : 0,
James Smart04c68492009-05-22 14:52:52 -04002249 pmbox->mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04002250 lpfc_sli_config_mbox_subsys_get(phba,
2251 pmb),
2252 lpfc_sli_config_mbox_opcode_get(phba,
2253 pmb));
James Smart2e0fef82007-06-17 19:56:36 -05002254 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05002255 phba->work_hs = HS_FFER3;
2256 lpfc_handle_eratt(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05002257 continue;
dea31012005-04-17 16:05:31 -05002258 }
2259
dea31012005-04-17 16:05:31 -05002260 if (pmbox->mbxStatus) {
2261 phba->sli.slistat.mbox_stat_err++;
2262 if (pmbox->mbxStatus == MBXERR_NO_RESOURCES) {
2263 /* Mbox cmd cmpl error - RETRYing */
James Smart92d7f7b2007-06-17 19:56:38 -05002264 lpfc_printf_log(phba, KERN_INFO,
James Smarta183a152011-10-10 21:32:43 -04002265 LOG_MBOX | LOG_SLI,
2266 "(%d):0305 Mbox cmd cmpl "
2267 "error - RETRYing Data: x%x "
2268 "(x%x/x%x) x%x x%x x%x\n",
2269 pmb->vport ? pmb->vport->vpi : 0,
2270 pmbox->mbxCommand,
2271 lpfc_sli_config_mbox_subsys_get(phba,
2272 pmb),
2273 lpfc_sli_config_mbox_opcode_get(phba,
2274 pmb),
2275 pmbox->mbxStatus,
2276 pmbox->un.varWords[0],
2277 pmb->vport->port_state);
dea31012005-04-17 16:05:31 -05002278 pmbox->mbxStatus = 0;
2279 pmbox->mbxOwner = OWN_HOST;
dea31012005-04-17 16:05:31 -05002280 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
James Smart04c68492009-05-22 14:52:52 -04002281 if (rc != MBX_NOT_FINISHED)
James Smart92d7f7b2007-06-17 19:56:38 -05002282 continue;
dea31012005-04-17 16:05:31 -05002283 }
2284 }
2285
2286 /* Mailbox cmd <cmd> Cmpl <cmpl> */
James Smart92d7f7b2007-06-17 19:56:38 -05002287 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04002288 "(%d):0307 Mailbox cmd x%x (x%x/x%x) Cmpl x%p "
dea31012005-04-17 16:05:31 -05002289 "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 -05002290 pmb->vport ? pmb->vport->vpi : 0,
dea31012005-04-17 16:05:31 -05002291 pmbox->mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04002292 lpfc_sli_config_mbox_subsys_get(phba, pmb),
2293 lpfc_sli_config_mbox_opcode_get(phba, pmb),
dea31012005-04-17 16:05:31 -05002294 pmb->mbox_cmpl,
2295 *((uint32_t *) pmbox),
2296 pmbox->un.varWords[0],
2297 pmbox->un.varWords[1],
2298 pmbox->un.varWords[2],
2299 pmbox->un.varWords[3],
2300 pmbox->un.varWords[4],
2301 pmbox->un.varWords[5],
2302 pmbox->un.varWords[6],
2303 pmbox->un.varWords[7]);
2304
James Smart92d7f7b2007-06-17 19:56:38 -05002305 if (pmb->mbox_cmpl)
dea31012005-04-17 16:05:31 -05002306 pmb->mbox_cmpl(phba,pmb);
James Smart92d7f7b2007-06-17 19:56:38 -05002307 } while (1);
James Smart2e0fef82007-06-17 19:56:36 -05002308 return 0;
dea31012005-04-17 16:05:31 -05002309}
James Smart92d7f7b2007-06-17 19:56:38 -05002310
James Smarte59058c2008-08-24 21:49:00 -04002311/**
James Smart3621a712009-04-06 18:47:14 -04002312 * lpfc_sli_get_buff - Get the buffer associated with the buffer tag
James Smarte59058c2008-08-24 21:49:00 -04002313 * @phba: Pointer to HBA context object.
2314 * @pring: Pointer to driver SLI ring object.
2315 * @tag: buffer tag.
2316 *
2317 * This function is called with no lock held. When QUE_BUFTAG_BIT bit
2318 * is set in the tag the buffer is posted for a particular exchange,
2319 * the function will return the buffer without replacing the buffer.
2320 * If the buffer is for unsolicited ELS or CT traffic, this function
2321 * returns the buffer and also posts another buffer to the firmware.
2322 **/
James Smart76bb24e2007-10-27 13:38:00 -04002323static struct lpfc_dmabuf *
2324lpfc_sli_get_buff(struct lpfc_hba *phba,
James Smart9f1e1b52008-12-04 22:39:40 -05002325 struct lpfc_sli_ring *pring,
2326 uint32_t tag)
James Smart76bb24e2007-10-27 13:38:00 -04002327{
James Smart9f1e1b52008-12-04 22:39:40 -05002328 struct hbq_dmabuf *hbq_entry;
2329
James Smart76bb24e2007-10-27 13:38:00 -04002330 if (tag & QUE_BUFTAG_BIT)
2331 return lpfc_sli_ring_taggedbuf_get(phba, pring, tag);
James Smart9f1e1b52008-12-04 22:39:40 -05002332 hbq_entry = lpfc_sli_hbqbuf_find(phba, tag);
2333 if (!hbq_entry)
2334 return NULL;
2335 return &hbq_entry->dbuf;
James Smart76bb24e2007-10-27 13:38:00 -04002336}
James Smart57127f12007-10-27 13:37:05 -04002337
James Smart3772a992009-05-22 14:50:54 -04002338/**
2339 * lpfc_complete_unsol_iocb - Complete an unsolicited sequence
2340 * @phba: Pointer to HBA context object.
2341 * @pring: Pointer to driver SLI ring object.
2342 * @saveq: Pointer to the iocbq struct representing the sequence starting frame.
2343 * @fch_r_ctl: the r_ctl for the first frame of the sequence.
2344 * @fch_type: the type for the first frame of the sequence.
2345 *
2346 * This function is called with no lock held. This function uses the r_ctl and
2347 * type of the received sequence to find the correct callback function to call
2348 * to process the sequence.
2349 **/
2350static int
2351lpfc_complete_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2352 struct lpfc_iocbq *saveq, uint32_t fch_r_ctl,
2353 uint32_t fch_type)
2354{
2355 int i;
2356
2357 /* unSolicited Responses */
2358 if (pring->prt[0].profile) {
2359 if (pring->prt[0].lpfc_sli_rcv_unsol_event)
2360 (pring->prt[0].lpfc_sli_rcv_unsol_event) (phba, pring,
2361 saveq);
2362 return 1;
2363 }
2364 /* We must search, based on rctl / type
2365 for the right routine */
2366 for (i = 0; i < pring->num_mask; i++) {
2367 if ((pring->prt[i].rctl == fch_r_ctl) &&
2368 (pring->prt[i].type == fch_type)) {
2369 if (pring->prt[i].lpfc_sli_rcv_unsol_event)
2370 (pring->prt[i].lpfc_sli_rcv_unsol_event)
2371 (phba, pring, saveq);
2372 return 1;
2373 }
2374 }
2375 return 0;
2376}
James Smarte59058c2008-08-24 21:49:00 -04002377
2378/**
James Smart3621a712009-04-06 18:47:14 -04002379 * lpfc_sli_process_unsol_iocb - Unsolicited iocb handler
James Smarte59058c2008-08-24 21:49:00 -04002380 * @phba: Pointer to HBA context object.
2381 * @pring: Pointer to driver SLI ring object.
2382 * @saveq: Pointer to the unsolicited iocb.
2383 *
2384 * This function is called with no lock held by the ring event handler
2385 * when there is an unsolicited iocb posted to the response ring by the
2386 * firmware. This function gets the buffer associated with the iocbs
2387 * and calls the event handler for the ring. This function handles both
2388 * qring buffers and hbq buffers.
2389 * When the function returns 1 the caller can free the iocb object otherwise
2390 * upper layer functions will free the iocb objects.
2391 **/
dea31012005-04-17 16:05:31 -05002392static int
2393lpfc_sli_process_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2394 struct lpfc_iocbq *saveq)
2395{
2396 IOCB_t * irsp;
2397 WORD5 * w5p;
2398 uint32_t Rctl, Type;
James Smart3772a992009-05-22 14:50:54 -04002399 uint32_t match;
James Smart76bb24e2007-10-27 13:38:00 -04002400 struct lpfc_iocbq *iocbq;
James Smart3163f722008-02-08 18:50:25 -05002401 struct lpfc_dmabuf *dmzbuf;
dea31012005-04-17 16:05:31 -05002402
2403 match = 0;
2404 irsp = &(saveq->iocb);
James Smart57127f12007-10-27 13:37:05 -04002405
2406 if (irsp->ulpCommand == CMD_ASYNC_STATUS) {
2407 if (pring->lpfc_sli_rcv_async_status)
2408 pring->lpfc_sli_rcv_async_status(phba, pring, saveq);
2409 else
2410 lpfc_printf_log(phba,
2411 KERN_WARNING,
2412 LOG_SLI,
2413 "0316 Ring %d handler: unexpected "
2414 "ASYNC_STATUS iocb received evt_code "
2415 "0x%x\n",
2416 pring->ringno,
2417 irsp->un.asyncstat.evt_code);
2418 return 1;
2419 }
2420
James Smart3163f722008-02-08 18:50:25 -05002421 if ((irsp->ulpCommand == CMD_IOCB_RET_XRI64_CX) &&
2422 (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)) {
2423 if (irsp->ulpBdeCount > 0) {
2424 dmzbuf = lpfc_sli_get_buff(phba, pring,
2425 irsp->un.ulpWord[3]);
2426 lpfc_in_buf_free(phba, dmzbuf);
2427 }
2428
2429 if (irsp->ulpBdeCount > 1) {
2430 dmzbuf = lpfc_sli_get_buff(phba, pring,
2431 irsp->unsli3.sli3Words[3]);
2432 lpfc_in_buf_free(phba, dmzbuf);
2433 }
2434
2435 if (irsp->ulpBdeCount > 2) {
2436 dmzbuf = lpfc_sli_get_buff(phba, pring,
2437 irsp->unsli3.sli3Words[7]);
2438 lpfc_in_buf_free(phba, dmzbuf);
2439 }
2440
2441 return 1;
2442 }
2443
James Smart92d7f7b2007-06-17 19:56:38 -05002444 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
James Smart76bb24e2007-10-27 13:38:00 -04002445 if (irsp->ulpBdeCount != 0) {
2446 saveq->context2 = lpfc_sli_get_buff(phba, pring,
James Smart92d7f7b2007-06-17 19:56:38 -05002447 irsp->un.ulpWord[3]);
James Smart76bb24e2007-10-27 13:38:00 -04002448 if (!saveq->context2)
2449 lpfc_printf_log(phba,
2450 KERN_ERR,
2451 LOG_SLI,
2452 "0341 Ring %d Cannot find buffer for "
2453 "an unsolicited iocb. tag 0x%x\n",
2454 pring->ringno,
2455 irsp->un.ulpWord[3]);
James Smart76bb24e2007-10-27 13:38:00 -04002456 }
2457 if (irsp->ulpBdeCount == 2) {
2458 saveq->context3 = lpfc_sli_get_buff(phba, pring,
James Smart51ef4c22007-08-02 11:10:31 -04002459 irsp->unsli3.sli3Words[7]);
James Smart76bb24e2007-10-27 13:38:00 -04002460 if (!saveq->context3)
2461 lpfc_printf_log(phba,
2462 KERN_ERR,
2463 LOG_SLI,
2464 "0342 Ring %d Cannot find buffer for an"
2465 " unsolicited iocb. tag 0x%x\n",
2466 pring->ringno,
2467 irsp->unsli3.sli3Words[7]);
2468 }
2469 list_for_each_entry(iocbq, &saveq->list, list) {
James Smart76bb24e2007-10-27 13:38:00 -04002470 irsp = &(iocbq->iocb);
James Smart76bb24e2007-10-27 13:38:00 -04002471 if (irsp->ulpBdeCount != 0) {
2472 iocbq->context2 = lpfc_sli_get_buff(phba, pring,
2473 irsp->un.ulpWord[3]);
James Smart9c2face2008-01-11 01:53:18 -05002474 if (!iocbq->context2)
James Smart76bb24e2007-10-27 13:38:00 -04002475 lpfc_printf_log(phba,
2476 KERN_ERR,
2477 LOG_SLI,
2478 "0343 Ring %d Cannot find "
2479 "buffer for an unsolicited iocb"
2480 ". tag 0x%x\n", pring->ringno,
2481 irsp->un.ulpWord[3]);
2482 }
2483 if (irsp->ulpBdeCount == 2) {
2484 iocbq->context3 = lpfc_sli_get_buff(phba, pring,
2485 irsp->unsli3.sli3Words[7]);
James Smart9c2face2008-01-11 01:53:18 -05002486 if (!iocbq->context3)
James Smart76bb24e2007-10-27 13:38:00 -04002487 lpfc_printf_log(phba,
2488 KERN_ERR,
2489 LOG_SLI,
2490 "0344 Ring %d Cannot find "
2491 "buffer for an unsolicited "
2492 "iocb. tag 0x%x\n",
2493 pring->ringno,
2494 irsp->unsli3.sli3Words[7]);
2495 }
2496 }
James Smart92d7f7b2007-06-17 19:56:38 -05002497 }
James Smart9c2face2008-01-11 01:53:18 -05002498 if (irsp->ulpBdeCount != 0 &&
2499 (irsp->ulpCommand == CMD_IOCB_RCV_CONT64_CX ||
2500 irsp->ulpStatus == IOSTAT_INTERMED_RSP)) {
2501 int found = 0;
2502
2503 /* search continue save q for same XRI */
2504 list_for_each_entry(iocbq, &pring->iocb_continue_saveq, clist) {
James Smart7851fe22011-07-22 18:36:52 -04002505 if (iocbq->iocb.unsli3.rcvsli3.ox_id ==
2506 saveq->iocb.unsli3.rcvsli3.ox_id) {
James Smart9c2face2008-01-11 01:53:18 -05002507 list_add_tail(&saveq->list, &iocbq->list);
2508 found = 1;
2509 break;
2510 }
2511 }
2512 if (!found)
2513 list_add_tail(&saveq->clist,
2514 &pring->iocb_continue_saveq);
2515 if (saveq->iocb.ulpStatus != IOSTAT_INTERMED_RSP) {
2516 list_del_init(&iocbq->clist);
2517 saveq = iocbq;
2518 irsp = &(saveq->iocb);
2519 } else
2520 return 0;
2521 }
2522 if ((irsp->ulpCommand == CMD_RCV_ELS_REQ64_CX) ||
2523 (irsp->ulpCommand == CMD_RCV_ELS_REQ_CX) ||
2524 (irsp->ulpCommand == CMD_IOCB_RCV_ELS64_CX)) {
James Smart6a9c52c2009-10-02 15:16:51 -04002525 Rctl = FC_RCTL_ELS_REQ;
2526 Type = FC_TYPE_ELS;
James Smart9c2face2008-01-11 01:53:18 -05002527 } else {
2528 w5p = (WORD5 *)&(saveq->iocb.un.ulpWord[5]);
2529 Rctl = w5p->hcsw.Rctl;
2530 Type = w5p->hcsw.Type;
2531
2532 /* Firmware Workaround */
2533 if ((Rctl == 0) && (pring->ringno == LPFC_ELS_RING) &&
2534 (irsp->ulpCommand == CMD_RCV_SEQUENCE64_CX ||
2535 irsp->ulpCommand == CMD_IOCB_RCV_SEQ64_CX)) {
James Smart6a9c52c2009-10-02 15:16:51 -04002536 Rctl = FC_RCTL_ELS_REQ;
2537 Type = FC_TYPE_ELS;
James Smart9c2face2008-01-11 01:53:18 -05002538 w5p->hcsw.Rctl = Rctl;
2539 w5p->hcsw.Type = Type;
2540 }
2541 }
James Smart92d7f7b2007-06-17 19:56:38 -05002542
James Smart3772a992009-05-22 14:50:54 -04002543 if (!lpfc_complete_unsol_iocb(phba, pring, saveq, Rctl, Type))
James Smart92d7f7b2007-06-17 19:56:38 -05002544 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002545 "0313 Ring %d handler: unexpected Rctl x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05002546 "Type x%x received\n",
James Smarte8b62012007-08-02 11:10:09 -04002547 pring->ringno, Rctl, Type);
James Smart3772a992009-05-22 14:50:54 -04002548
James Smart92d7f7b2007-06-17 19:56:38 -05002549 return 1;
dea31012005-04-17 16:05:31 -05002550}
2551
James Smarte59058c2008-08-24 21:49:00 -04002552/**
James Smart3621a712009-04-06 18:47:14 -04002553 * lpfc_sli_iocbq_lookup - Find command iocb for the given response iocb
James Smarte59058c2008-08-24 21:49:00 -04002554 * @phba: Pointer to HBA context object.
2555 * @pring: Pointer to driver SLI ring object.
2556 * @prspiocb: Pointer to response iocb object.
2557 *
2558 * This function looks up the iocb_lookup table to get the command iocb
2559 * corresponding to the given response iocb using the iotag of the
2560 * response iocb. This function is called with the hbalock held.
2561 * This function returns the command iocb object if it finds the command
2562 * iocb else returns NULL.
2563 **/
dea31012005-04-17 16:05:31 -05002564static struct lpfc_iocbq *
James Smart2e0fef82007-06-17 19:56:36 -05002565lpfc_sli_iocbq_lookup(struct lpfc_hba *phba,
2566 struct lpfc_sli_ring *pring,
2567 struct lpfc_iocbq *prspiocb)
dea31012005-04-17 16:05:31 -05002568{
dea31012005-04-17 16:05:31 -05002569 struct lpfc_iocbq *cmd_iocb = NULL;
2570 uint16_t iotag;
2571
James Bottomley604a3e32005-10-29 10:28:33 -05002572 iotag = prspiocb->iocb.ulpIoTag;
dea31012005-04-17 16:05:31 -05002573
James Bottomley604a3e32005-10-29 10:28:33 -05002574 if (iotag != 0 && iotag <= phba->sli.last_iotag) {
2575 cmd_iocb = phba->sli.iocbq_lookup[iotag];
James Smart92d7f7b2007-06-17 19:56:38 -05002576 list_del_init(&cmd_iocb->list);
James Smart2a9bf3d2010-06-07 15:24:45 -04002577 if (cmd_iocb->iocb_flag & LPFC_IO_ON_Q) {
2578 pring->txcmplq_cnt--;
2579 cmd_iocb->iocb_flag &= ~LPFC_IO_ON_Q;
2580 }
James Bottomley604a3e32005-10-29 10:28:33 -05002581 return cmd_iocb;
dea31012005-04-17 16:05:31 -05002582 }
2583
dea31012005-04-17 16:05:31 -05002584 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002585 "0317 iotag x%x is out off "
James Bottomley604a3e32005-10-29 10:28:33 -05002586 "range: max iotag x%x wd0 x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04002587 iotag, phba->sli.last_iotag,
James Bottomley604a3e32005-10-29 10:28:33 -05002588 *(((uint32_t *) &prspiocb->iocb) + 7));
dea31012005-04-17 16:05:31 -05002589 return NULL;
2590}
2591
James Smarte59058c2008-08-24 21:49:00 -04002592/**
James Smart3772a992009-05-22 14:50:54 -04002593 * lpfc_sli_iocbq_lookup_by_tag - Find command iocb for the iotag
2594 * @phba: Pointer to HBA context object.
2595 * @pring: Pointer to driver SLI ring object.
2596 * @iotag: IOCB tag.
2597 *
2598 * This function looks up the iocb_lookup table to get the command iocb
2599 * corresponding to the given iotag. This function is called with the
2600 * hbalock held.
2601 * This function returns the command iocb object if it finds the command
2602 * iocb else returns NULL.
2603 **/
2604static struct lpfc_iocbq *
2605lpfc_sli_iocbq_lookup_by_tag(struct lpfc_hba *phba,
2606 struct lpfc_sli_ring *pring, uint16_t iotag)
2607{
2608 struct lpfc_iocbq *cmd_iocb;
2609
2610 if (iotag != 0 && iotag <= phba->sli.last_iotag) {
2611 cmd_iocb = phba->sli.iocbq_lookup[iotag];
2612 list_del_init(&cmd_iocb->list);
James Smart2a9bf3d2010-06-07 15:24:45 -04002613 if (cmd_iocb->iocb_flag & LPFC_IO_ON_Q) {
2614 cmd_iocb->iocb_flag &= ~LPFC_IO_ON_Q;
2615 pring->txcmplq_cnt--;
2616 }
James Smart3772a992009-05-22 14:50:54 -04002617 return cmd_iocb;
2618 }
2619
2620 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2621 "0372 iotag x%x is out off range: max iotag (x%x)\n",
2622 iotag, phba->sli.last_iotag);
2623 return NULL;
2624}
2625
2626/**
James Smart3621a712009-04-06 18:47:14 -04002627 * lpfc_sli_process_sol_iocb - process solicited iocb completion
James Smarte59058c2008-08-24 21:49:00 -04002628 * @phba: Pointer to HBA context object.
2629 * @pring: Pointer to driver SLI ring object.
2630 * @saveq: Pointer to the response iocb to be processed.
2631 *
2632 * This function is called by the ring event handler for non-fcp
2633 * rings when there is a new response iocb in the response ring.
2634 * The caller is not required to hold any locks. This function
2635 * gets the command iocb associated with the response iocb and
2636 * calls the completion handler for the command iocb. If there
2637 * is no completion handler, the function will free the resources
2638 * associated with command iocb. If the response iocb is for
2639 * an already aborted command iocb, the status of the completion
2640 * is changed to IOSTAT_LOCAL_REJECT/IOERR_SLI_ABORTED.
2641 * This function always returns 1.
2642 **/
dea31012005-04-17 16:05:31 -05002643static int
James Smart2e0fef82007-06-17 19:56:36 -05002644lpfc_sli_process_sol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
dea31012005-04-17 16:05:31 -05002645 struct lpfc_iocbq *saveq)
2646{
James Smart2e0fef82007-06-17 19:56:36 -05002647 struct lpfc_iocbq *cmdiocbp;
dea31012005-04-17 16:05:31 -05002648 int rc = 1;
2649 unsigned long iflag;
2650
2651 /* Based on the iotag field, get the cmd IOCB from the txcmplq */
James Smart2e0fef82007-06-17 19:56:36 -05002652 spin_lock_irqsave(&phba->hbalock, iflag);
James Bottomley604a3e32005-10-29 10:28:33 -05002653 cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring, saveq);
James Smart2e0fef82007-06-17 19:56:36 -05002654 spin_unlock_irqrestore(&phba->hbalock, iflag);
2655
dea31012005-04-17 16:05:31 -05002656 if (cmdiocbp) {
2657 if (cmdiocbp->iocb_cmpl) {
2658 /*
James Smartea2151b2008-09-07 11:52:10 -04002659 * If an ELS command failed send an event to mgmt
2660 * application.
2661 */
2662 if (saveq->iocb.ulpStatus &&
2663 (pring->ringno == LPFC_ELS_RING) &&
2664 (cmdiocbp->iocb.ulpCommand ==
2665 CMD_ELS_REQUEST64_CR))
2666 lpfc_send_els_failure_event(phba,
2667 cmdiocbp, saveq);
2668
2669 /*
dea31012005-04-17 16:05:31 -05002670 * Post all ELS completions to the worker thread.
2671 * All other are passed to the completion callback.
2672 */
2673 if (pring->ringno == LPFC_ELS_RING) {
James Smart341af102010-01-26 23:07:37 -05002674 if ((phba->sli_rev < LPFC_SLI_REV4) &&
2675 (cmdiocbp->iocb_flag &
2676 LPFC_DRIVER_ABORTED)) {
2677 spin_lock_irqsave(&phba->hbalock,
2678 iflag);
James Smart07951072007-04-25 09:51:38 -04002679 cmdiocbp->iocb_flag &=
2680 ~LPFC_DRIVER_ABORTED;
James Smart341af102010-01-26 23:07:37 -05002681 spin_unlock_irqrestore(&phba->hbalock,
2682 iflag);
James Smart07951072007-04-25 09:51:38 -04002683 saveq->iocb.ulpStatus =
2684 IOSTAT_LOCAL_REJECT;
2685 saveq->iocb.un.ulpWord[4] =
2686 IOERR_SLI_ABORTED;
James Smart0ff10d42008-01-11 01:52:36 -05002687
2688 /* Firmware could still be in progress
2689 * of DMAing payload, so don't free data
2690 * buffer till after a hbeat.
2691 */
James Smart341af102010-01-26 23:07:37 -05002692 spin_lock_irqsave(&phba->hbalock,
2693 iflag);
James Smart0ff10d42008-01-11 01:52:36 -05002694 saveq->iocb_flag |= LPFC_DELAY_MEM_FREE;
James Smart341af102010-01-26 23:07:37 -05002695 spin_unlock_irqrestore(&phba->hbalock,
2696 iflag);
2697 }
James Smart0f65ff62010-02-26 14:14:23 -05002698 if (phba->sli_rev == LPFC_SLI_REV4) {
2699 if (saveq->iocb_flag &
2700 LPFC_EXCHANGE_BUSY) {
2701 /* Set cmdiocb flag for the
2702 * exchange busy so sgl (xri)
2703 * will not be released until
2704 * the abort xri is received
2705 * from hba.
2706 */
2707 spin_lock_irqsave(
2708 &phba->hbalock, iflag);
2709 cmdiocbp->iocb_flag |=
2710 LPFC_EXCHANGE_BUSY;
2711 spin_unlock_irqrestore(
2712 &phba->hbalock, iflag);
2713 }
2714 if (cmdiocbp->iocb_flag &
2715 LPFC_DRIVER_ABORTED) {
2716 /*
2717 * Clear LPFC_DRIVER_ABORTED
2718 * bit in case it was driver
2719 * initiated abort.
2720 */
2721 spin_lock_irqsave(
2722 &phba->hbalock, iflag);
2723 cmdiocbp->iocb_flag &=
2724 ~LPFC_DRIVER_ABORTED;
2725 spin_unlock_irqrestore(
2726 &phba->hbalock, iflag);
2727 cmdiocbp->iocb.ulpStatus =
2728 IOSTAT_LOCAL_REJECT;
2729 cmdiocbp->iocb.un.ulpWord[4] =
2730 IOERR_ABORT_REQUESTED;
2731 /*
2732 * For SLI4, irsiocb contains
2733 * NO_XRI in sli_xritag, it
2734 * shall not affect releasing
2735 * sgl (xri) process.
2736 */
2737 saveq->iocb.ulpStatus =
2738 IOSTAT_LOCAL_REJECT;
2739 saveq->iocb.un.ulpWord[4] =
2740 IOERR_SLI_ABORTED;
2741 spin_lock_irqsave(
2742 &phba->hbalock, iflag);
2743 saveq->iocb_flag |=
2744 LPFC_DELAY_MEM_FREE;
2745 spin_unlock_irqrestore(
2746 &phba->hbalock, iflag);
2747 }
James Smart07951072007-04-25 09:51:38 -04002748 }
dea31012005-04-17 16:05:31 -05002749 }
James Smart2e0fef82007-06-17 19:56:36 -05002750 (cmdiocbp->iocb_cmpl) (phba, cmdiocbp, saveq);
James Bottomley604a3e32005-10-29 10:28:33 -05002751 } else
2752 lpfc_sli_release_iocbq(phba, cmdiocbp);
dea31012005-04-17 16:05:31 -05002753 } else {
2754 /*
2755 * Unknown initiating command based on the response iotag.
2756 * This could be the case on the ELS ring because of
2757 * lpfc_els_abort().
2758 */
2759 if (pring->ringno != LPFC_ELS_RING) {
2760 /*
2761 * Ring <ringno> handler: unexpected completion IoTag
2762 * <IoTag>
2763 */
James Smarta257bf92009-04-06 18:48:10 -04002764 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002765 "0322 Ring %d handler: "
2766 "unexpected completion IoTag x%x "
2767 "Data: x%x x%x x%x x%x\n",
2768 pring->ringno,
2769 saveq->iocb.ulpIoTag,
2770 saveq->iocb.ulpStatus,
2771 saveq->iocb.un.ulpWord[4],
2772 saveq->iocb.ulpCommand,
2773 saveq->iocb.ulpContext);
dea31012005-04-17 16:05:31 -05002774 }
2775 }
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04002776
dea31012005-04-17 16:05:31 -05002777 return rc;
2778}
2779
James Smarte59058c2008-08-24 21:49:00 -04002780/**
James Smart3621a712009-04-06 18:47:14 -04002781 * lpfc_sli_rsp_pointers_error - Response ring pointer error handler
James Smarte59058c2008-08-24 21:49:00 -04002782 * @phba: Pointer to HBA context object.
2783 * @pring: Pointer to driver SLI ring object.
2784 *
2785 * This function is called from the iocb ring event handlers when
2786 * put pointer is ahead of the get pointer for a ring. This function signal
2787 * an error attention condition to the worker thread and the worker
2788 * thread will transition the HBA to offline state.
2789 **/
James Smart2e0fef82007-06-17 19:56:36 -05002790static void
2791lpfc_sli_rsp_pointers_error(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002792{
James Smart34b02dc2008-08-24 21:49:55 -04002793 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002794 /*
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02002795 * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002796 * rsp ring <portRspMax>
2797 */
2798 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002799 "0312 Ring %d handler: portRspPut %d "
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02002800 "is bigger than rsp ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04002801 pring->ringno, le32_to_cpu(pgp->rspPutInx),
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002802 pring->numRiocb);
2803
James Smart2e0fef82007-06-17 19:56:36 -05002804 phba->link_state = LPFC_HBA_ERROR;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002805
2806 /*
2807 * All error attention handlers are posted to
2808 * worker thread
2809 */
2810 phba->work_ha |= HA_ERATT;
2811 phba->work_hs = HS_FFER3;
James Smart92d7f7b2007-06-17 19:56:38 -05002812
James Smart5e9d9b82008-06-14 22:52:53 -04002813 lpfc_worker_wake_up(phba);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002814
2815 return;
2816}
2817
James Smarte59058c2008-08-24 21:49:00 -04002818/**
James Smart3621a712009-04-06 18:47:14 -04002819 * lpfc_poll_eratt - Error attention polling timer timeout handler
James Smart93996272008-08-24 21:50:30 -04002820 * @ptr: Pointer to address of HBA context object.
2821 *
2822 * This function is invoked by the Error Attention polling timer when the
2823 * timer times out. It will check the SLI Error Attention register for
2824 * possible attention events. If so, it will post an Error Attention event
2825 * and wake up worker thread to process it. Otherwise, it will set up the
2826 * Error Attention polling timer for the next poll.
2827 **/
2828void lpfc_poll_eratt(unsigned long ptr)
2829{
2830 struct lpfc_hba *phba;
2831 uint32_t eratt = 0;
2832
2833 phba = (struct lpfc_hba *)ptr;
2834
2835 /* Check chip HA register for error event */
2836 eratt = lpfc_sli_check_eratt(phba);
2837
2838 if (eratt)
2839 /* Tell the worker thread there is work to do */
2840 lpfc_worker_wake_up(phba);
2841 else
2842 /* Restart the timer for next eratt poll */
2843 mod_timer(&phba->eratt_poll, jiffies +
2844 HZ * LPFC_ERATT_POLL_INTERVAL);
2845 return;
2846}
2847
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002848
James Smarte59058c2008-08-24 21:49:00 -04002849/**
James Smart3621a712009-04-06 18:47:14 -04002850 * lpfc_sli_handle_fast_ring_event - Handle ring events on FCP ring
James Smarte59058c2008-08-24 21:49:00 -04002851 * @phba: Pointer to HBA context object.
2852 * @pring: Pointer to driver SLI ring object.
2853 * @mask: Host attention register mask for this ring.
2854 *
2855 * This function is called from the interrupt context when there is a ring
2856 * event for the fcp ring. The caller does not hold any lock.
2857 * The function processes each response iocb in the response ring until it
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002858 * finds an iocb with LE bit set and chains all the iocbs up to the iocb with
James Smarte59058c2008-08-24 21:49:00 -04002859 * LE bit set. The function will call the completion handler of the command iocb
2860 * if the response iocb indicates a completion for a command iocb or it is
2861 * an abort completion. The function will call lpfc_sli_process_unsol_iocb
2862 * function if this is an unsolicited iocb.
dea31012005-04-17 16:05:31 -05002863 * This routine presumes LPFC_FCP_RING handling and doesn't bother
James Smart45ed1192009-10-02 15:17:02 -04002864 * to check it explicitly.
2865 */
2866int
James Smart2e0fef82007-06-17 19:56:36 -05002867lpfc_sli_handle_fast_ring_event(struct lpfc_hba *phba,
2868 struct lpfc_sli_ring *pring, uint32_t mask)
dea31012005-04-17 16:05:31 -05002869{
James Smart34b02dc2008-08-24 21:49:55 -04002870 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
dea31012005-04-17 16:05:31 -05002871 IOCB_t *irsp = NULL;
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04002872 IOCB_t *entry = NULL;
dea31012005-04-17 16:05:31 -05002873 struct lpfc_iocbq *cmdiocbq = NULL;
2874 struct lpfc_iocbq rspiocbq;
dea31012005-04-17 16:05:31 -05002875 uint32_t status;
2876 uint32_t portRspPut, portRspMax;
2877 int rc = 1;
2878 lpfc_iocb_type type;
2879 unsigned long iflag;
2880 uint32_t rsp_cmpl = 0;
dea31012005-04-17 16:05:31 -05002881
James Smart2e0fef82007-06-17 19:56:36 -05002882 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05002883 pring->stats.iocb_event++;
2884
dea31012005-04-17 16:05:31 -05002885 /*
2886 * The next available response entry should never exceed the maximum
2887 * entries. If it does, treat it as an adapter hardware error.
2888 */
2889 portRspMax = pring->numRiocb;
2890 portRspPut = le32_to_cpu(pgp->rspPutInx);
2891 if (unlikely(portRspPut >= portRspMax)) {
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002892 lpfc_sli_rsp_pointers_error(phba, pring);
James Smart2e0fef82007-06-17 19:56:36 -05002893 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05002894 return 1;
2895 }
James Smart45ed1192009-10-02 15:17:02 -04002896 if (phba->fcp_ring_in_use) {
2897 spin_unlock_irqrestore(&phba->hbalock, iflag);
2898 return 1;
2899 } else
2900 phba->fcp_ring_in_use = 1;
dea31012005-04-17 16:05:31 -05002901
2902 rmb();
2903 while (pring->rspidx != portRspPut) {
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04002904 /*
2905 * Fetch an entry off the ring and copy it into a local data
2906 * structure. The copy involves a byte-swap since the
2907 * network byte order and pci byte orders are different.
2908 */
James Smarted957682007-06-17 19:56:37 -05002909 entry = lpfc_resp_iocb(phba, pring);
James Smart858c9f62007-06-17 19:56:39 -05002910 phba->last_completion_time = jiffies;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002911
2912 if (++pring->rspidx >= portRspMax)
2913 pring->rspidx = 0;
2914
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04002915 lpfc_sli_pcimem_bcopy((uint32_t *) entry,
2916 (uint32_t *) &rspiocbq.iocb,
James Smarted957682007-06-17 19:56:37 -05002917 phba->iocb_rsp_size);
James Smarta4bc3372006-12-02 13:34:16 -05002918 INIT_LIST_HEAD(&(rspiocbq.list));
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04002919 irsp = &rspiocbq.iocb;
2920
dea31012005-04-17 16:05:31 -05002921 type = lpfc_sli_iocb_cmd_type(irsp->ulpCommand & CMD_IOCB_MASK);
2922 pring->stats.iocb_rsp++;
2923 rsp_cmpl++;
2924
2925 if (unlikely(irsp->ulpStatus)) {
James Smart92d7f7b2007-06-17 19:56:38 -05002926 /*
2927 * If resource errors reported from HBA, reduce
2928 * queuedepths of the SCSI device.
2929 */
2930 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
2931 (irsp->un.ulpWord[4] == IOERR_NO_RESOURCES)) {
2932 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart3772a992009-05-22 14:50:54 -04002933 phba->lpfc_rampdown_queue_depth(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05002934 spin_lock_irqsave(&phba->hbalock, iflag);
2935 }
2936
dea31012005-04-17 16:05:31 -05002937 /* Rsp ring <ringno> error: IOCB */
2938 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002939 "0336 Rsp Ring %d error: IOCB Data: "
James Smart92d7f7b2007-06-17 19:56:38 -05002940 "x%x x%x x%x x%x x%x x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04002941 pring->ringno,
James Smart92d7f7b2007-06-17 19:56:38 -05002942 irsp->un.ulpWord[0],
2943 irsp->un.ulpWord[1],
2944 irsp->un.ulpWord[2],
2945 irsp->un.ulpWord[3],
2946 irsp->un.ulpWord[4],
2947 irsp->un.ulpWord[5],
James Smartd7c255b2008-08-24 21:50:00 -04002948 *(uint32_t *)&irsp->un1,
2949 *((uint32_t *)&irsp->un1 + 1));
dea31012005-04-17 16:05:31 -05002950 }
2951
2952 switch (type) {
2953 case LPFC_ABORT_IOCB:
2954 case LPFC_SOL_IOCB:
2955 /*
2956 * Idle exchange closed via ABTS from port. No iocb
2957 * resources need to be recovered.
2958 */
2959 if (unlikely(irsp->ulpCommand == CMD_XRI_ABORTED_CX)) {
James Smartdca94792006-08-01 07:34:08 -04002960 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002961 "0333 IOCB cmd 0x%x"
James Smartdca94792006-08-01 07:34:08 -04002962 " processed. Skipping"
James Smart92d7f7b2007-06-17 19:56:38 -05002963 " completion\n",
James Smartdca94792006-08-01 07:34:08 -04002964 irsp->ulpCommand);
dea31012005-04-17 16:05:31 -05002965 break;
2966 }
2967
James Bottomley604a3e32005-10-29 10:28:33 -05002968 cmdiocbq = lpfc_sli_iocbq_lookup(phba, pring,
2969 &rspiocbq);
James Smart0f65ff62010-02-26 14:14:23 -05002970 if (unlikely(!cmdiocbq))
2971 break;
2972 if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED)
2973 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
2974 if (cmdiocbq->iocb_cmpl) {
2975 spin_unlock_irqrestore(&phba->hbalock, iflag);
2976 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
2977 &rspiocbq);
2978 spin_lock_irqsave(&phba->hbalock, iflag);
2979 }
dea31012005-04-17 16:05:31 -05002980 break;
James Smarta4bc3372006-12-02 13:34:16 -05002981 case LPFC_UNSOL_IOCB:
James Smart2e0fef82007-06-17 19:56:36 -05002982 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta4bc3372006-12-02 13:34:16 -05002983 lpfc_sli_process_unsol_iocb(phba, pring, &rspiocbq);
James Smart2e0fef82007-06-17 19:56:36 -05002984 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta4bc3372006-12-02 13:34:16 -05002985 break;
dea31012005-04-17 16:05:31 -05002986 default:
2987 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
2988 char adaptermsg[LPFC_MAX_ADPTMSG];
2989 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
2990 memcpy(&adaptermsg[0], (uint8_t *) irsp,
2991 MAX_MSG_DATA);
Joe Perches898eb712007-10-18 03:06:30 -07002992 dev_warn(&((phba->pcidev)->dev),
2993 "lpfc%d: %s\n",
dea31012005-04-17 16:05:31 -05002994 phba->brd_no, adaptermsg);
2995 } else {
2996 /* Unknown IOCB command */
2997 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002998 "0334 Unknown IOCB command "
James Smart92d7f7b2007-06-17 19:56:38 -05002999 "Data: x%x, x%x x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04003000 type, irsp->ulpCommand,
James Smart92d7f7b2007-06-17 19:56:38 -05003001 irsp->ulpStatus,
3002 irsp->ulpIoTag,
3003 irsp->ulpContext);
dea31012005-04-17 16:05:31 -05003004 }
3005 break;
3006 }
3007
3008 /*
3009 * The response IOCB has been processed. Update the ring
3010 * pointer in SLIM. If the port response put pointer has not
3011 * been updated, sync the pgp->rspPutInx and fetch the new port
3012 * response put pointer.
3013 */
James Smarted957682007-06-17 19:56:37 -05003014 writel(pring->rspidx, &phba->host_gp[pring->ringno].rspGetInx);
dea31012005-04-17 16:05:31 -05003015
3016 if (pring->rspidx == portRspPut)
3017 portRspPut = le32_to_cpu(pgp->rspPutInx);
3018 }
3019
3020 if ((rsp_cmpl > 0) && (mask & HA_R0RE_REQ)) {
3021 pring->stats.iocb_rsp_full++;
3022 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
3023 writel(status, phba->CAregaddr);
3024 readl(phba->CAregaddr);
3025 }
3026 if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
3027 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
3028 pring->stats.iocb_cmd_empty++;
3029
3030 /* Force update of the local copy of cmdGetInx */
3031 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
3032 lpfc_sli_resume_iocb(phba, pring);
3033
3034 if ((pring->lpfc_sli_cmd_available))
3035 (pring->lpfc_sli_cmd_available) (phba, pring);
3036
3037 }
3038
James Smart45ed1192009-10-02 15:17:02 -04003039 phba->fcp_ring_in_use = 0;
James Smart2e0fef82007-06-17 19:56:36 -05003040 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003041 return rc;
3042}
3043
James Smarte59058c2008-08-24 21:49:00 -04003044/**
James Smart3772a992009-05-22 14:50:54 -04003045 * lpfc_sli_sp_handle_rspiocb - Handle slow-path response iocb
3046 * @phba: Pointer to HBA context object.
3047 * @pring: Pointer to driver SLI ring object.
3048 * @rspiocbp: Pointer to driver response IOCB object.
3049 *
3050 * This function is called from the worker thread when there is a slow-path
3051 * response IOCB to process. This function chains all the response iocbs until
3052 * seeing the iocb with the LE bit set. The function will call
3053 * lpfc_sli_process_sol_iocb function if the response iocb indicates a
3054 * completion of a command iocb. The function will call the
3055 * lpfc_sli_process_unsol_iocb function if this is an unsolicited iocb.
3056 * The function frees the resources or calls the completion handler if this
3057 * iocb is an abort completion. The function returns NULL when the response
3058 * iocb has the LE bit set and all the chained iocbs are processed, otherwise
3059 * this function shall chain the iocb on to the iocb_continueq and return the
3060 * response iocb passed in.
3061 **/
3062static struct lpfc_iocbq *
3063lpfc_sli_sp_handle_rspiocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
3064 struct lpfc_iocbq *rspiocbp)
3065{
3066 struct lpfc_iocbq *saveq;
3067 struct lpfc_iocbq *cmdiocbp;
3068 struct lpfc_iocbq *next_iocb;
3069 IOCB_t *irsp = NULL;
3070 uint32_t free_saveq;
3071 uint8_t iocb_cmd_type;
3072 lpfc_iocb_type type;
3073 unsigned long iflag;
3074 int rc;
3075
3076 spin_lock_irqsave(&phba->hbalock, iflag);
3077 /* First add the response iocb to the countinueq list */
3078 list_add_tail(&rspiocbp->list, &(pring->iocb_continueq));
3079 pring->iocb_continueq_cnt++;
3080
Justin P. Mattock70f23fd2011-05-10 10:16:21 +02003081 /* Now, determine whether the list is completed for processing */
James Smart3772a992009-05-22 14:50:54 -04003082 irsp = &rspiocbp->iocb;
3083 if (irsp->ulpLe) {
3084 /*
3085 * By default, the driver expects to free all resources
3086 * associated with this iocb completion.
3087 */
3088 free_saveq = 1;
3089 saveq = list_get_first(&pring->iocb_continueq,
3090 struct lpfc_iocbq, list);
3091 irsp = &(saveq->iocb);
3092 list_del_init(&pring->iocb_continueq);
3093 pring->iocb_continueq_cnt = 0;
3094
3095 pring->stats.iocb_rsp++;
3096
3097 /*
3098 * If resource errors reported from HBA, reduce
3099 * queuedepths of the SCSI device.
3100 */
3101 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
3102 (irsp->un.ulpWord[4] == IOERR_NO_RESOURCES)) {
3103 spin_unlock_irqrestore(&phba->hbalock, iflag);
3104 phba->lpfc_rampdown_queue_depth(phba);
3105 spin_lock_irqsave(&phba->hbalock, iflag);
3106 }
3107
3108 if (irsp->ulpStatus) {
3109 /* Rsp ring <ringno> error: IOCB */
3110 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
3111 "0328 Rsp Ring %d error: "
3112 "IOCB Data: "
3113 "x%x x%x x%x x%x "
3114 "x%x x%x x%x x%x "
3115 "x%x x%x x%x x%x "
3116 "x%x x%x x%x x%x\n",
3117 pring->ringno,
3118 irsp->un.ulpWord[0],
3119 irsp->un.ulpWord[1],
3120 irsp->un.ulpWord[2],
3121 irsp->un.ulpWord[3],
3122 irsp->un.ulpWord[4],
3123 irsp->un.ulpWord[5],
3124 *(((uint32_t *) irsp) + 6),
3125 *(((uint32_t *) irsp) + 7),
3126 *(((uint32_t *) irsp) + 8),
3127 *(((uint32_t *) irsp) + 9),
3128 *(((uint32_t *) irsp) + 10),
3129 *(((uint32_t *) irsp) + 11),
3130 *(((uint32_t *) irsp) + 12),
3131 *(((uint32_t *) irsp) + 13),
3132 *(((uint32_t *) irsp) + 14),
3133 *(((uint32_t *) irsp) + 15));
3134 }
3135
3136 /*
3137 * Fetch the IOCB command type and call the correct completion
3138 * routine. Solicited and Unsolicited IOCBs on the ELS ring
3139 * get freed back to the lpfc_iocb_list by the discovery
3140 * kernel thread.
3141 */
3142 iocb_cmd_type = irsp->ulpCommand & CMD_IOCB_MASK;
3143 type = lpfc_sli_iocb_cmd_type(iocb_cmd_type);
3144 switch (type) {
3145 case LPFC_SOL_IOCB:
3146 spin_unlock_irqrestore(&phba->hbalock, iflag);
3147 rc = lpfc_sli_process_sol_iocb(phba, pring, saveq);
3148 spin_lock_irqsave(&phba->hbalock, iflag);
3149 break;
3150
3151 case LPFC_UNSOL_IOCB:
3152 spin_unlock_irqrestore(&phba->hbalock, iflag);
3153 rc = lpfc_sli_process_unsol_iocb(phba, pring, saveq);
3154 spin_lock_irqsave(&phba->hbalock, iflag);
3155 if (!rc)
3156 free_saveq = 0;
3157 break;
3158
3159 case LPFC_ABORT_IOCB:
3160 cmdiocbp = NULL;
3161 if (irsp->ulpCommand != CMD_XRI_ABORTED_CX)
3162 cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring,
3163 saveq);
3164 if (cmdiocbp) {
3165 /* Call the specified completion routine */
3166 if (cmdiocbp->iocb_cmpl) {
3167 spin_unlock_irqrestore(&phba->hbalock,
3168 iflag);
3169 (cmdiocbp->iocb_cmpl)(phba, cmdiocbp,
3170 saveq);
3171 spin_lock_irqsave(&phba->hbalock,
3172 iflag);
3173 } else
3174 __lpfc_sli_release_iocbq(phba,
3175 cmdiocbp);
3176 }
3177 break;
3178
3179 case LPFC_UNKNOWN_IOCB:
3180 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
3181 char adaptermsg[LPFC_MAX_ADPTMSG];
3182 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
3183 memcpy(&adaptermsg[0], (uint8_t *)irsp,
3184 MAX_MSG_DATA);
3185 dev_warn(&((phba->pcidev)->dev),
3186 "lpfc%d: %s\n",
3187 phba->brd_no, adaptermsg);
3188 } else {
3189 /* Unknown IOCB command */
3190 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3191 "0335 Unknown IOCB "
3192 "command Data: x%x "
3193 "x%x x%x x%x\n",
3194 irsp->ulpCommand,
3195 irsp->ulpStatus,
3196 irsp->ulpIoTag,
3197 irsp->ulpContext);
3198 }
3199 break;
3200 }
3201
3202 if (free_saveq) {
3203 list_for_each_entry_safe(rspiocbp, next_iocb,
3204 &saveq->list, list) {
3205 list_del(&rspiocbp->list);
3206 __lpfc_sli_release_iocbq(phba, rspiocbp);
3207 }
3208 __lpfc_sli_release_iocbq(phba, saveq);
3209 }
3210 rspiocbp = NULL;
3211 }
3212 spin_unlock_irqrestore(&phba->hbalock, iflag);
3213 return rspiocbp;
3214}
3215
3216/**
3217 * lpfc_sli_handle_slow_ring_event - Wrapper func for handling slow-path iocbs
James Smarte59058c2008-08-24 21:49:00 -04003218 * @phba: Pointer to HBA context object.
3219 * @pring: Pointer to driver SLI ring object.
3220 * @mask: Host attention register mask for this ring.
3221 *
James Smart3772a992009-05-22 14:50:54 -04003222 * This routine wraps the actual slow_ring event process routine from the
3223 * API jump table function pointer from the lpfc_hba struct.
James Smarte59058c2008-08-24 21:49:00 -04003224 **/
James Smart3772a992009-05-22 14:50:54 -04003225void
James Smart2e0fef82007-06-17 19:56:36 -05003226lpfc_sli_handle_slow_ring_event(struct lpfc_hba *phba,
3227 struct lpfc_sli_ring *pring, uint32_t mask)
dea31012005-04-17 16:05:31 -05003228{
James Smart3772a992009-05-22 14:50:54 -04003229 phba->lpfc_sli_handle_slow_ring_event(phba, pring, mask);
3230}
3231
3232/**
3233 * lpfc_sli_handle_slow_ring_event_s3 - Handle SLI3 ring event for non-FCP rings
3234 * @phba: Pointer to HBA context object.
3235 * @pring: Pointer to driver SLI ring object.
3236 * @mask: Host attention register mask for this ring.
3237 *
3238 * This function is called from the worker thread when there is a ring event
3239 * for non-fcp rings. The caller does not hold any lock. The function will
3240 * remove each response iocb in the response ring and calls the handle
3241 * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
3242 **/
3243static void
3244lpfc_sli_handle_slow_ring_event_s3(struct lpfc_hba *phba,
3245 struct lpfc_sli_ring *pring, uint32_t mask)
3246{
James Smart34b02dc2008-08-24 21:49:55 -04003247 struct lpfc_pgp *pgp;
dea31012005-04-17 16:05:31 -05003248 IOCB_t *entry;
3249 IOCB_t *irsp = NULL;
3250 struct lpfc_iocbq *rspiocbp = NULL;
dea31012005-04-17 16:05:31 -05003251 uint32_t portRspPut, portRspMax;
dea31012005-04-17 16:05:31 -05003252 unsigned long iflag;
James Smart3772a992009-05-22 14:50:54 -04003253 uint32_t status;
dea31012005-04-17 16:05:31 -05003254
James Smart34b02dc2008-08-24 21:49:55 -04003255 pgp = &phba->port_gp[pring->ringno];
James Smart2e0fef82007-06-17 19:56:36 -05003256 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003257 pring->stats.iocb_event++;
3258
dea31012005-04-17 16:05:31 -05003259 /*
3260 * The next available response entry should never exceed the maximum
3261 * entries. If it does, treat it as an adapter hardware error.
3262 */
3263 portRspMax = pring->numRiocb;
3264 portRspPut = le32_to_cpu(pgp->rspPutInx);
3265 if (portRspPut >= portRspMax) {
3266 /*
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02003267 * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
dea31012005-04-17 16:05:31 -05003268 * rsp ring <portRspMax>
3269 */
James Smarted957682007-06-17 19:56:37 -05003270 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003271 "0303 Ring %d handler: portRspPut %d "
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02003272 "is bigger than rsp ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04003273 pring->ringno, portRspPut, portRspMax);
dea31012005-04-17 16:05:31 -05003274
James Smart2e0fef82007-06-17 19:56:36 -05003275 phba->link_state = LPFC_HBA_ERROR;
3276 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003277
3278 phba->work_hs = HS_FFER3;
3279 lpfc_handle_eratt(phba);
3280
James Smart3772a992009-05-22 14:50:54 -04003281 return;
dea31012005-04-17 16:05:31 -05003282 }
3283
3284 rmb();
dea31012005-04-17 16:05:31 -05003285 while (pring->rspidx != portRspPut) {
3286 /*
3287 * Build a completion list and call the appropriate handler.
3288 * The process is to get the next available response iocb, get
3289 * a free iocb from the list, copy the response data into the
3290 * free iocb, insert to the continuation list, and update the
3291 * next response index to slim. This process makes response
3292 * iocb's in the ring available to DMA as fast as possible but
3293 * pays a penalty for a copy operation. Since the iocb is
3294 * only 32 bytes, this penalty is considered small relative to
3295 * the PCI reads for register values and a slim write. When
3296 * the ulpLe field is set, the entire Command has been
3297 * received.
3298 */
James Smarted957682007-06-17 19:56:37 -05003299 entry = lpfc_resp_iocb(phba, pring);
3300
James Smart858c9f62007-06-17 19:56:39 -05003301 phba->last_completion_time = jiffies;
James Smart2e0fef82007-06-17 19:56:36 -05003302 rspiocbp = __lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05003303 if (rspiocbp == NULL) {
3304 printk(KERN_ERR "%s: out of buffers! Failing "
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07003305 "completion.\n", __func__);
dea31012005-04-17 16:05:31 -05003306 break;
3307 }
3308
James Smarted957682007-06-17 19:56:37 -05003309 lpfc_sli_pcimem_bcopy(entry, &rspiocbp->iocb,
3310 phba->iocb_rsp_size);
dea31012005-04-17 16:05:31 -05003311 irsp = &rspiocbp->iocb;
3312
3313 if (++pring->rspidx >= portRspMax)
3314 pring->rspidx = 0;
3315
James Smarta58cbd52007-08-02 11:09:43 -04003316 if (pring->ringno == LPFC_ELS_RING) {
3317 lpfc_debugfs_slow_ring_trc(phba,
3318 "IOCB rsp ring: wd4:x%08x wd6:x%08x wd7:x%08x",
3319 *(((uint32_t *) irsp) + 4),
3320 *(((uint32_t *) irsp) + 6),
3321 *(((uint32_t *) irsp) + 7));
3322 }
3323
James Smarted957682007-06-17 19:56:37 -05003324 writel(pring->rspidx, &phba->host_gp[pring->ringno].rspGetInx);
dea31012005-04-17 16:05:31 -05003325
James Smart3772a992009-05-22 14:50:54 -04003326 spin_unlock_irqrestore(&phba->hbalock, iflag);
3327 /* Handle the response IOCB */
3328 rspiocbp = lpfc_sli_sp_handle_rspiocb(phba, pring, rspiocbp);
3329 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003330
3331 /*
3332 * If the port response put pointer has not been updated, sync
3333 * the pgp->rspPutInx in the MAILBOX_tand fetch the new port
3334 * response put pointer.
3335 */
3336 if (pring->rspidx == portRspPut) {
3337 portRspPut = le32_to_cpu(pgp->rspPutInx);
3338 }
3339 } /* while (pring->rspidx != portRspPut) */
3340
James Smart92d7f7b2007-06-17 19:56:38 -05003341 if ((rspiocbp != NULL) && (mask & HA_R0RE_REQ)) {
dea31012005-04-17 16:05:31 -05003342 /* At least one response entry has been freed */
3343 pring->stats.iocb_rsp_full++;
3344 /* SET RxRE_RSP in Chip Att register */
3345 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
3346 writel(status, phba->CAregaddr);
3347 readl(phba->CAregaddr); /* flush */
3348 }
3349 if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
3350 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
3351 pring->stats.iocb_cmd_empty++;
3352
3353 /* Force update of the local copy of cmdGetInx */
3354 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
3355 lpfc_sli_resume_iocb(phba, pring);
3356
3357 if ((pring->lpfc_sli_cmd_available))
3358 (pring->lpfc_sli_cmd_available) (phba, pring);
3359
3360 }
3361
James Smart2e0fef82007-06-17 19:56:36 -05003362 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart3772a992009-05-22 14:50:54 -04003363 return;
dea31012005-04-17 16:05:31 -05003364}
3365
James Smarte59058c2008-08-24 21:49:00 -04003366/**
James Smart4f774512009-05-22 14:52:35 -04003367 * lpfc_sli_handle_slow_ring_event_s4 - Handle SLI4 slow-path els events
3368 * @phba: Pointer to HBA context object.
3369 * @pring: Pointer to driver SLI ring object.
3370 * @mask: Host attention register mask for this ring.
3371 *
3372 * This function is called from the worker thread when there is a pending
3373 * ELS response iocb on the driver internal slow-path response iocb worker
3374 * queue. The caller does not hold any lock. The function will remove each
3375 * response iocb from the response worker queue and calls the handle
3376 * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
3377 **/
3378static void
3379lpfc_sli_handle_slow_ring_event_s4(struct lpfc_hba *phba,
3380 struct lpfc_sli_ring *pring, uint32_t mask)
3381{
3382 struct lpfc_iocbq *irspiocbq;
James Smart4d9ab992009-10-02 15:16:39 -04003383 struct hbq_dmabuf *dmabuf;
3384 struct lpfc_cq_event *cq_event;
James Smart4f774512009-05-22 14:52:35 -04003385 unsigned long iflag;
3386
James Smart45ed1192009-10-02 15:17:02 -04003387 spin_lock_irqsave(&phba->hbalock, iflag);
3388 phba->hba_flag &= ~HBA_SP_QUEUE_EVT;
3389 spin_unlock_irqrestore(&phba->hbalock, iflag);
3390 while (!list_empty(&phba->sli4_hba.sp_queue_event)) {
James Smart4f774512009-05-22 14:52:35 -04003391 /* Get the response iocb from the head of work queue */
3392 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart45ed1192009-10-02 15:17:02 -04003393 list_remove_head(&phba->sli4_hba.sp_queue_event,
James Smart4d9ab992009-10-02 15:16:39 -04003394 cq_event, struct lpfc_cq_event, list);
James Smart4f774512009-05-22 14:52:35 -04003395 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart4d9ab992009-10-02 15:16:39 -04003396
3397 switch (bf_get(lpfc_wcqe_c_code, &cq_event->cqe.wcqe_cmpl)) {
3398 case CQE_CODE_COMPL_WQE:
3399 irspiocbq = container_of(cq_event, struct lpfc_iocbq,
3400 cq_event);
James Smart45ed1192009-10-02 15:17:02 -04003401 /* Translate ELS WCQE to response IOCBQ */
3402 irspiocbq = lpfc_sli4_els_wcqe_to_rspiocbq(phba,
3403 irspiocbq);
3404 if (irspiocbq)
3405 lpfc_sli_sp_handle_rspiocb(phba, pring,
3406 irspiocbq);
James Smart4d9ab992009-10-02 15:16:39 -04003407 break;
3408 case CQE_CODE_RECEIVE:
James Smart7851fe22011-07-22 18:36:52 -04003409 case CQE_CODE_RECEIVE_V1:
James Smart4d9ab992009-10-02 15:16:39 -04003410 dmabuf = container_of(cq_event, struct hbq_dmabuf,
3411 cq_event);
3412 lpfc_sli4_handle_received_buffer(phba, dmabuf);
3413 break;
3414 default:
3415 break;
3416 }
James Smart4f774512009-05-22 14:52:35 -04003417 }
3418}
3419
3420/**
James Smart3621a712009-04-06 18:47:14 -04003421 * lpfc_sli_abort_iocb_ring - Abort all iocbs in the ring
James Smarte59058c2008-08-24 21:49:00 -04003422 * @phba: Pointer to HBA context object.
3423 * @pring: Pointer to driver SLI ring object.
3424 *
3425 * This function aborts all iocbs in the given ring and frees all the iocb
3426 * objects in txq. This function issues an abort iocb for all the iocb commands
3427 * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
3428 * the return of this function. The caller is not required to hold any locks.
3429 **/
James Smart2e0fef82007-06-17 19:56:36 -05003430void
dea31012005-04-17 16:05:31 -05003431lpfc_sli_abort_iocb_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
3432{
James Smart2534ba72007-04-25 09:52:20 -04003433 LIST_HEAD(completions);
dea31012005-04-17 16:05:31 -05003434 struct lpfc_iocbq *iocb, *next_iocb;
dea31012005-04-17 16:05:31 -05003435
James Smart92d7f7b2007-06-17 19:56:38 -05003436 if (pring->ringno == LPFC_ELS_RING) {
3437 lpfc_fabric_abort_hba(phba);
3438 }
3439
dea31012005-04-17 16:05:31 -05003440 /* Error everything on txq and txcmplq
3441 * First do the txq.
3442 */
James Smart2e0fef82007-06-17 19:56:36 -05003443 spin_lock_irq(&phba->hbalock);
James Smart2534ba72007-04-25 09:52:20 -04003444 list_splice_init(&pring->txq, &completions);
dea31012005-04-17 16:05:31 -05003445 pring->txq_cnt = 0;
dea31012005-04-17 16:05:31 -05003446
3447 /* Next issue ABTS for everything on the txcmplq */
James Smart2534ba72007-04-25 09:52:20 -04003448 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
3449 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
3450
James Smart2e0fef82007-06-17 19:56:36 -05003451 spin_unlock_irq(&phba->hbalock);
James Smart2534ba72007-04-25 09:52:20 -04003452
James Smarta257bf92009-04-06 18:48:10 -04003453 /* Cancel all the IOCBs from the completions list */
3454 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
3455 IOERR_SLI_ABORTED);
dea31012005-04-17 16:05:31 -05003456}
3457
James Smarte59058c2008-08-24 21:49:00 -04003458/**
James Smart3621a712009-04-06 18:47:14 -04003459 * lpfc_sli_flush_fcp_rings - flush all iocbs in the fcp ring
James Smarta8e497d2008-08-24 21:50:11 -04003460 * @phba: Pointer to HBA context object.
3461 *
3462 * This function flushes all iocbs in the fcp ring and frees all the iocb
3463 * objects in txq and txcmplq. This function will not issue abort iocbs
3464 * for all the iocb commands in txcmplq, they will just be returned with
3465 * IOERR_SLI_DOWN. This function is invoked with EEH when device's PCI
3466 * slot has been permanently disabled.
3467 **/
3468void
3469lpfc_sli_flush_fcp_rings(struct lpfc_hba *phba)
3470{
3471 LIST_HEAD(txq);
3472 LIST_HEAD(txcmplq);
James Smarta8e497d2008-08-24 21:50:11 -04003473 struct lpfc_sli *psli = &phba->sli;
3474 struct lpfc_sli_ring *pring;
3475
3476 /* Currently, only one fcp ring */
3477 pring = &psli->ring[psli->fcp_ring];
3478
3479 spin_lock_irq(&phba->hbalock);
3480 /* Retrieve everything on txq */
3481 list_splice_init(&pring->txq, &txq);
3482 pring->txq_cnt = 0;
3483
3484 /* Retrieve everything on the txcmplq */
3485 list_splice_init(&pring->txcmplq, &txcmplq);
3486 pring->txcmplq_cnt = 0;
3487 spin_unlock_irq(&phba->hbalock);
3488
3489 /* Flush the txq */
James Smarta257bf92009-04-06 18:48:10 -04003490 lpfc_sli_cancel_iocbs(phba, &txq, IOSTAT_LOCAL_REJECT,
3491 IOERR_SLI_DOWN);
James Smarta8e497d2008-08-24 21:50:11 -04003492
3493 /* Flush the txcmpq */
James Smarta257bf92009-04-06 18:48:10 -04003494 lpfc_sli_cancel_iocbs(phba, &txcmplq, IOSTAT_LOCAL_REJECT,
3495 IOERR_SLI_DOWN);
James Smarta8e497d2008-08-24 21:50:11 -04003496}
3497
3498/**
James Smart3772a992009-05-22 14:50:54 -04003499 * lpfc_sli_brdready_s3 - Check for sli3 host ready status
James Smarte59058c2008-08-24 21:49:00 -04003500 * @phba: Pointer to HBA context object.
3501 * @mask: Bit mask to be checked.
3502 *
3503 * This function reads the host status register and compares
3504 * with the provided bit mask to check if HBA completed
3505 * the restart. This function will wait in a loop for the
3506 * HBA to complete restart. If the HBA does not restart within
3507 * 15 iterations, the function will reset the HBA again. The
3508 * function returns 1 when HBA fail to restart otherwise returns
3509 * zero.
3510 **/
James Smart3772a992009-05-22 14:50:54 -04003511static int
3512lpfc_sli_brdready_s3(struct lpfc_hba *phba, uint32_t mask)
dea31012005-04-17 16:05:31 -05003513{
Jamie Wellnitz41415862006-02-28 19:25:27 -05003514 uint32_t status;
3515 int i = 0;
3516 int retval = 0;
dea31012005-04-17 16:05:31 -05003517
Jamie Wellnitz41415862006-02-28 19:25:27 -05003518 /* Read the HBA Host Status Register */
James Smart9940b972011-03-11 16:06:12 -05003519 if (lpfc_readl(phba->HSregaddr, &status))
3520 return 1;
dea31012005-04-17 16:05:31 -05003521
Jamie Wellnitz41415862006-02-28 19:25:27 -05003522 /*
3523 * Check status register every 100ms for 5 retries, then every
3524 * 500ms for 5, then every 2.5 sec for 5, then reset board and
3525 * every 2.5 sec for 4.
3526 * Break our of the loop if errors occurred during init.
3527 */
3528 while (((status & mask) != mask) &&
3529 !(status & HS_FFERM) &&
3530 i++ < 20) {
dea31012005-04-17 16:05:31 -05003531
Jamie Wellnitz41415862006-02-28 19:25:27 -05003532 if (i <= 5)
3533 msleep(10);
3534 else if (i <= 10)
3535 msleep(500);
3536 else
3537 msleep(2500);
dea31012005-04-17 16:05:31 -05003538
Jamie Wellnitz41415862006-02-28 19:25:27 -05003539 if (i == 15) {
James Smart2e0fef82007-06-17 19:56:36 -05003540 /* Do post */
James Smart92d7f7b2007-06-17 19:56:38 -05003541 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003542 lpfc_sli_brdrestart(phba);
3543 }
3544 /* Read the HBA Host Status Register */
James Smart9940b972011-03-11 16:06:12 -05003545 if (lpfc_readl(phba->HSregaddr, &status)) {
3546 retval = 1;
3547 break;
3548 }
dea31012005-04-17 16:05:31 -05003549 }
dea31012005-04-17 16:05:31 -05003550
Jamie Wellnitz41415862006-02-28 19:25:27 -05003551 /* Check to see if any errors occurred during init */
3552 if ((status & HS_FFERM) || (i >= 20)) {
James Smarte40a02c2010-02-26 14:13:54 -05003553 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3554 "2751 Adapter failed to restart, "
3555 "status reg x%x, FW Data: A8 x%x AC x%x\n",
3556 status,
3557 readl(phba->MBslimaddr + 0xa8),
3558 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05003559 phba->link_state = LPFC_HBA_ERROR;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003560 retval = 1;
3561 }
dea31012005-04-17 16:05:31 -05003562
Jamie Wellnitz41415862006-02-28 19:25:27 -05003563 return retval;
dea31012005-04-17 16:05:31 -05003564}
3565
James Smartda0436e2009-05-22 14:51:39 -04003566/**
3567 * lpfc_sli_brdready_s4 - Check for sli4 host ready status
3568 * @phba: Pointer to HBA context object.
3569 * @mask: Bit mask to be checked.
3570 *
3571 * This function checks the host status register to check if HBA is
3572 * ready. This function will wait in a loop for the HBA to be ready
3573 * If the HBA is not ready , the function will will reset the HBA PCI
3574 * function again. The function returns 1 when HBA fail to be ready
3575 * otherwise returns zero.
3576 **/
3577static int
3578lpfc_sli_brdready_s4(struct lpfc_hba *phba, uint32_t mask)
3579{
3580 uint32_t status;
3581 int retval = 0;
3582
3583 /* Read the HBA Host Status Register */
3584 status = lpfc_sli4_post_status_check(phba);
3585
3586 if (status) {
3587 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
3588 lpfc_sli_brdrestart(phba);
3589 status = lpfc_sli4_post_status_check(phba);
3590 }
3591
3592 /* Check to see if any errors occurred during init */
3593 if (status) {
3594 phba->link_state = LPFC_HBA_ERROR;
3595 retval = 1;
3596 } else
3597 phba->sli4_hba.intr_enable = 0;
3598
3599 return retval;
3600}
3601
3602/**
3603 * lpfc_sli_brdready - Wrapper func for checking the hba readyness
3604 * @phba: Pointer to HBA context object.
3605 * @mask: Bit mask to be checked.
3606 *
3607 * This routine wraps the actual SLI3 or SLI4 hba readyness check routine
3608 * from the API jump table function pointer from the lpfc_hba struct.
3609 **/
3610int
3611lpfc_sli_brdready(struct lpfc_hba *phba, uint32_t mask)
3612{
3613 return phba->lpfc_sli_brdready(phba, mask);
3614}
3615
James Smart92908312006-03-07 15:04:13 -05003616#define BARRIER_TEST_PATTERN (0xdeadbeef)
3617
James Smarte59058c2008-08-24 21:49:00 -04003618/**
James Smart3621a712009-04-06 18:47:14 -04003619 * lpfc_reset_barrier - Make HBA ready for HBA reset
James Smarte59058c2008-08-24 21:49:00 -04003620 * @phba: Pointer to HBA context object.
3621 *
3622 * This function is called before resetting an HBA. This
3623 * function requests HBA to quiesce DMAs before a reset.
3624 **/
James Smart2e0fef82007-06-17 19:56:36 -05003625void lpfc_reset_barrier(struct lpfc_hba *phba)
James Smart92908312006-03-07 15:04:13 -05003626{
James Smart65a29c12006-07-06 15:50:50 -04003627 uint32_t __iomem *resp_buf;
3628 uint32_t __iomem *mbox_buf;
James Smart92908312006-03-07 15:04:13 -05003629 volatile uint32_t mbox;
James Smart9940b972011-03-11 16:06:12 -05003630 uint32_t hc_copy, ha_copy, resp_data;
James Smart92908312006-03-07 15:04:13 -05003631 int i;
3632 uint8_t hdrtype;
3633
3634 pci_read_config_byte(phba->pcidev, PCI_HEADER_TYPE, &hdrtype);
3635 if (hdrtype != 0x80 ||
3636 (FC_JEDEC_ID(phba->vpd.rev.biuRev) != HELIOS_JEDEC_ID &&
3637 FC_JEDEC_ID(phba->vpd.rev.biuRev) != THOR_JEDEC_ID))
3638 return;
3639
3640 /*
3641 * Tell the other part of the chip to suspend temporarily all
3642 * its DMA activity.
3643 */
James Smart65a29c12006-07-06 15:50:50 -04003644 resp_buf = phba->MBslimaddr;
James Smart92908312006-03-07 15:04:13 -05003645
3646 /* Disable the error attention */
James Smart9940b972011-03-11 16:06:12 -05003647 if (lpfc_readl(phba->HCregaddr, &hc_copy))
3648 return;
James Smart92908312006-03-07 15:04:13 -05003649 writel((hc_copy & ~HC_ERINT_ENA), phba->HCregaddr);
3650 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05003651 phba->link_flag |= LS_IGNORE_ERATT;
James Smart92908312006-03-07 15:04:13 -05003652
James Smart9940b972011-03-11 16:06:12 -05003653 if (lpfc_readl(phba->HAregaddr, &ha_copy))
3654 return;
3655 if (ha_copy & HA_ERATT) {
James Smart92908312006-03-07 15:04:13 -05003656 /* Clear Chip error bit */
3657 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05003658 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05003659 }
3660
3661 mbox = 0;
3662 ((MAILBOX_t *)&mbox)->mbxCommand = MBX_KILL_BOARD;
3663 ((MAILBOX_t *)&mbox)->mbxOwner = OWN_CHIP;
3664
3665 writel(BARRIER_TEST_PATTERN, (resp_buf + 1));
James Smart65a29c12006-07-06 15:50:50 -04003666 mbox_buf = phba->MBslimaddr;
James Smart92908312006-03-07 15:04:13 -05003667 writel(mbox, mbox_buf);
3668
James Smart9940b972011-03-11 16:06:12 -05003669 for (i = 0; i < 50; i++) {
3670 if (lpfc_readl((resp_buf + 1), &resp_data))
3671 return;
3672 if (resp_data != ~(BARRIER_TEST_PATTERN))
3673 mdelay(1);
3674 else
3675 break;
3676 }
3677 resp_data = 0;
3678 if (lpfc_readl((resp_buf + 1), &resp_data))
3679 return;
3680 if (resp_data != ~(BARRIER_TEST_PATTERN)) {
James Smartf4b4c682009-05-22 14:53:12 -04003681 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE ||
James Smart2e0fef82007-06-17 19:56:36 -05003682 phba->pport->stopped)
James Smart92908312006-03-07 15:04:13 -05003683 goto restore_hc;
3684 else
3685 goto clear_errat;
3686 }
3687
3688 ((MAILBOX_t *)&mbox)->mbxOwner = OWN_HOST;
James Smart9940b972011-03-11 16:06:12 -05003689 resp_data = 0;
3690 for (i = 0; i < 500; i++) {
3691 if (lpfc_readl(resp_buf, &resp_data))
3692 return;
3693 if (resp_data != mbox)
3694 mdelay(1);
3695 else
3696 break;
3697 }
James Smart92908312006-03-07 15:04:13 -05003698
3699clear_errat:
3700
James Smart9940b972011-03-11 16:06:12 -05003701 while (++i < 500) {
3702 if (lpfc_readl(phba->HAregaddr, &ha_copy))
3703 return;
3704 if (!(ha_copy & HA_ERATT))
3705 mdelay(1);
3706 else
3707 break;
3708 }
James Smart92908312006-03-07 15:04:13 -05003709
3710 if (readl(phba->HAregaddr) & HA_ERATT) {
3711 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05003712 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05003713 }
3714
3715restore_hc:
James Smart2e0fef82007-06-17 19:56:36 -05003716 phba->link_flag &= ~LS_IGNORE_ERATT;
James Smart92908312006-03-07 15:04:13 -05003717 writel(hc_copy, phba->HCregaddr);
3718 readl(phba->HCregaddr); /* flush */
3719}
3720
James Smarte59058c2008-08-24 21:49:00 -04003721/**
James Smart3621a712009-04-06 18:47:14 -04003722 * lpfc_sli_brdkill - Issue a kill_board mailbox command
James Smarte59058c2008-08-24 21:49:00 -04003723 * @phba: Pointer to HBA context object.
3724 *
3725 * This function issues a kill_board mailbox command and waits for
3726 * the error attention interrupt. This function is called for stopping
3727 * the firmware processing. The caller is not required to hold any
3728 * locks. This function calls lpfc_hba_down_post function to free
3729 * any pending commands after the kill. The function will return 1 when it
3730 * fails to kill the board else will return 0.
3731 **/
Jamie Wellnitz41415862006-02-28 19:25:27 -05003732int
James Smart2e0fef82007-06-17 19:56:36 -05003733lpfc_sli_brdkill(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05003734{
Jamie Wellnitz41415862006-02-28 19:25:27 -05003735 struct lpfc_sli *psli;
3736 LPFC_MBOXQ_t *pmb;
3737 uint32_t status;
3738 uint32_t ha_copy;
3739 int retval;
3740 int i = 0;
3741
3742 psli = &phba->sli;
3743
3744 /* Kill HBA */
James Smarted957682007-06-17 19:56:37 -05003745 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003746 "0329 Kill HBA Data: x%x x%x\n",
3747 phba->pport->port_state, psli->sli_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003748
James Smart98c9ea52007-10-27 13:37:33 -04003749 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3750 if (!pmb)
Jamie Wellnitz41415862006-02-28 19:25:27 -05003751 return 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003752
3753 /* Disable the error attention */
James Smart2e0fef82007-06-17 19:56:36 -05003754 spin_lock_irq(&phba->hbalock);
James Smart9940b972011-03-11 16:06:12 -05003755 if (lpfc_readl(phba->HCregaddr, &status)) {
3756 spin_unlock_irq(&phba->hbalock);
3757 mempool_free(pmb, phba->mbox_mem_pool);
3758 return 1;
3759 }
Jamie Wellnitz41415862006-02-28 19:25:27 -05003760 status &= ~HC_ERINT_ENA;
3761 writel(status, phba->HCregaddr);
3762 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05003763 phba->link_flag |= LS_IGNORE_ERATT;
3764 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003765
3766 lpfc_kill_board(phba, pmb);
3767 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
3768 retval = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
3769
3770 if (retval != MBX_SUCCESS) {
3771 if (retval != MBX_BUSY)
3772 mempool_free(pmb, phba->mbox_mem_pool);
James Smarte40a02c2010-02-26 14:13:54 -05003773 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3774 "2752 KILL_BOARD command failed retval %d\n",
3775 retval);
James Smart2e0fef82007-06-17 19:56:36 -05003776 spin_lock_irq(&phba->hbalock);
3777 phba->link_flag &= ~LS_IGNORE_ERATT;
3778 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003779 return 1;
3780 }
3781
James Smartf4b4c682009-05-22 14:53:12 -04003782 spin_lock_irq(&phba->hbalock);
3783 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
3784 spin_unlock_irq(&phba->hbalock);
James Smart92908312006-03-07 15:04:13 -05003785
Jamie Wellnitz41415862006-02-28 19:25:27 -05003786 mempool_free(pmb, phba->mbox_mem_pool);
3787
3788 /* There is no completion for a KILL_BOARD mbox cmd. Check for an error
3789 * attention every 100ms for 3 seconds. If we don't get ERATT after
3790 * 3 seconds we still set HBA_ERROR state because the status of the
3791 * board is now undefined.
3792 */
James Smart9940b972011-03-11 16:06:12 -05003793 if (lpfc_readl(phba->HAregaddr, &ha_copy))
3794 return 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003795 while ((i++ < 30) && !(ha_copy & HA_ERATT)) {
3796 mdelay(100);
James Smart9940b972011-03-11 16:06:12 -05003797 if (lpfc_readl(phba->HAregaddr, &ha_copy))
3798 return 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003799 }
3800
3801 del_timer_sync(&psli->mbox_tmo);
James Smart92908312006-03-07 15:04:13 -05003802 if (ha_copy & HA_ERATT) {
3803 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05003804 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05003805 }
James Smart2e0fef82007-06-17 19:56:36 -05003806 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003807 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart04c68492009-05-22 14:52:52 -04003808 psli->mbox_active = NULL;
James Smart2e0fef82007-06-17 19:56:36 -05003809 phba->link_flag &= ~LS_IGNORE_ERATT;
3810 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003811
Jamie Wellnitz41415862006-02-28 19:25:27 -05003812 lpfc_hba_down_post(phba);
James Smart2e0fef82007-06-17 19:56:36 -05003813 phba->link_state = LPFC_HBA_ERROR;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003814
James Smart2e0fef82007-06-17 19:56:36 -05003815 return ha_copy & HA_ERATT ? 0 : 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003816}
3817
James Smarte59058c2008-08-24 21:49:00 -04003818/**
James Smart3772a992009-05-22 14:50:54 -04003819 * lpfc_sli_brdreset - Reset a sli-2 or sli-3 HBA
James Smarte59058c2008-08-24 21:49:00 -04003820 * @phba: Pointer to HBA context object.
3821 *
3822 * This function resets the HBA by writing HC_INITFF to the control
3823 * register. After the HBA resets, this function resets all the iocb ring
3824 * indices. This function disables PCI layer parity checking during
3825 * the reset.
3826 * This function returns 0 always.
3827 * The caller is not required to hold any locks.
3828 **/
Jamie Wellnitz41415862006-02-28 19:25:27 -05003829int
James Smart2e0fef82007-06-17 19:56:36 -05003830lpfc_sli_brdreset(struct lpfc_hba *phba)
Jamie Wellnitz41415862006-02-28 19:25:27 -05003831{
3832 struct lpfc_sli *psli;
dea31012005-04-17 16:05:31 -05003833 struct lpfc_sli_ring *pring;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003834 uint16_t cfg_value;
dea31012005-04-17 16:05:31 -05003835 int i;
dea31012005-04-17 16:05:31 -05003836
Jamie Wellnitz41415862006-02-28 19:25:27 -05003837 psli = &phba->sli;
dea31012005-04-17 16:05:31 -05003838
Jamie Wellnitz41415862006-02-28 19:25:27 -05003839 /* Reset HBA */
3840 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003841 "0325 Reset HBA Data: x%x x%x\n",
James Smart2e0fef82007-06-17 19:56:36 -05003842 phba->pport->port_state, psli->sli_flag);
dea31012005-04-17 16:05:31 -05003843
3844 /* perform board reset */
3845 phba->fc_eventTag = 0;
James Smart4d9ab992009-10-02 15:16:39 -04003846 phba->link_events = 0;
James Smart2e0fef82007-06-17 19:56:36 -05003847 phba->pport->fc_myDID = 0;
3848 phba->pport->fc_prevDID = 0;
dea31012005-04-17 16:05:31 -05003849
Jamie Wellnitz41415862006-02-28 19:25:27 -05003850 /* Turn off parity checking and serr during the physical reset */
3851 pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
3852 pci_write_config_word(phba->pcidev, PCI_COMMAND,
3853 (cfg_value &
3854 ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
3855
James Smart3772a992009-05-22 14:50:54 -04003856 psli->sli_flag &= ~(LPFC_SLI_ACTIVE | LPFC_PROCESS_LA);
3857
Jamie Wellnitz41415862006-02-28 19:25:27 -05003858 /* Now toggle INITFF bit in the Host Control Register */
3859 writel(HC_INITFF, phba->HCregaddr);
3860 mdelay(1);
3861 readl(phba->HCregaddr); /* flush */
3862 writel(0, phba->HCregaddr);
3863 readl(phba->HCregaddr); /* flush */
3864
3865 /* Restore PCI cmd register */
3866 pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
dea31012005-04-17 16:05:31 -05003867
3868 /* Initialize relevant SLI info */
Jamie Wellnitz41415862006-02-28 19:25:27 -05003869 for (i = 0; i < psli->num_rings; i++) {
3870 pring = &psli->ring[i];
dea31012005-04-17 16:05:31 -05003871 pring->flag = 0;
3872 pring->rspidx = 0;
3873 pring->next_cmdidx = 0;
3874 pring->local_getidx = 0;
3875 pring->cmdidx = 0;
3876 pring->missbufcnt = 0;
3877 }
dea31012005-04-17 16:05:31 -05003878
James Smart2e0fef82007-06-17 19:56:36 -05003879 phba->link_state = LPFC_WARM_START;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003880 return 0;
3881}
3882
James Smarte59058c2008-08-24 21:49:00 -04003883/**
James Smartda0436e2009-05-22 14:51:39 -04003884 * lpfc_sli4_brdreset - Reset a sli-4 HBA
3885 * @phba: Pointer to HBA context object.
3886 *
3887 * This function resets a SLI4 HBA. This function disables PCI layer parity
3888 * checking during resets the device. The caller is not required to hold
3889 * any locks.
3890 *
3891 * This function returns 0 always.
3892 **/
3893int
3894lpfc_sli4_brdreset(struct lpfc_hba *phba)
3895{
3896 struct lpfc_sli *psli = &phba->sli;
3897 uint16_t cfg_value;
James Smartda0436e2009-05-22 14:51:39 -04003898
3899 /* Reset HBA */
3900 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3901 "0295 Reset HBA Data: x%x x%x\n",
3902 phba->pport->port_state, psli->sli_flag);
3903
3904 /* perform board reset */
3905 phba->fc_eventTag = 0;
James Smart4d9ab992009-10-02 15:16:39 -04003906 phba->link_events = 0;
James Smartda0436e2009-05-22 14:51:39 -04003907 phba->pport->fc_myDID = 0;
3908 phba->pport->fc_prevDID = 0;
3909
James Smartda0436e2009-05-22 14:51:39 -04003910 spin_lock_irq(&phba->hbalock);
3911 psli->sli_flag &= ~(LPFC_PROCESS_LA);
3912 phba->fcf.fcf_flag = 0;
James Smartda0436e2009-05-22 14:51:39 -04003913 spin_unlock_irq(&phba->hbalock);
3914
3915 /* Now physically reset the device */
3916 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3917 "0389 Performing PCI function reset!\n");
James Smartbe858b62010-12-15 17:57:20 -05003918
3919 /* Turn off parity checking and serr during the physical reset */
3920 pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
3921 pci_write_config_word(phba->pcidev, PCI_COMMAND, (cfg_value &
3922 ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
3923
James Smartda0436e2009-05-22 14:51:39 -04003924 /* Perform FCoE PCI function reset */
James Smart2e90f4b2011-12-13 13:22:37 -05003925 lpfc_sli4_queue_destroy(phba);
James Smartda0436e2009-05-22 14:51:39 -04003926 lpfc_pci_function_reset(phba);
3927
James Smartbe858b62010-12-15 17:57:20 -05003928 /* Restore PCI cmd register */
3929 pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
3930
James Smartda0436e2009-05-22 14:51:39 -04003931 return 0;
3932}
3933
3934/**
3935 * lpfc_sli_brdrestart_s3 - Restart a sli-3 hba
James Smarte59058c2008-08-24 21:49:00 -04003936 * @phba: Pointer to HBA context object.
3937 *
3938 * This function is called in the SLI initialization code path to
3939 * restart the HBA. The caller is not required to hold any lock.
3940 * This function writes MBX_RESTART mailbox command to the SLIM and
3941 * resets the HBA. At the end of the function, it calls lpfc_hba_down_post
3942 * function to free any pending commands. The function enables
3943 * POST only during the first initialization. The function returns zero.
3944 * The function does not guarantee completion of MBX_RESTART mailbox
3945 * command before the return of this function.
3946 **/
James Smartda0436e2009-05-22 14:51:39 -04003947static int
3948lpfc_sli_brdrestart_s3(struct lpfc_hba *phba)
Jamie Wellnitz41415862006-02-28 19:25:27 -05003949{
3950 MAILBOX_t *mb;
3951 struct lpfc_sli *psli;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003952 volatile uint32_t word0;
3953 void __iomem *to_slim;
James Smart0d878412009-10-02 15:16:56 -04003954 uint32_t hba_aer_enabled;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003955
James Smart2e0fef82007-06-17 19:56:36 -05003956 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003957
James Smart0d878412009-10-02 15:16:56 -04003958 /* Take PCIe device Advanced Error Reporting (AER) state */
3959 hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
3960
Jamie Wellnitz41415862006-02-28 19:25:27 -05003961 psli = &phba->sli;
3962
3963 /* Restart HBA */
3964 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003965 "0337 Restart HBA Data: x%x x%x\n",
James Smart2e0fef82007-06-17 19:56:36 -05003966 phba->pport->port_state, psli->sli_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003967
3968 word0 = 0;
3969 mb = (MAILBOX_t *) &word0;
3970 mb->mbxCommand = MBX_RESTART;
3971 mb->mbxHc = 1;
3972
James Smart92908312006-03-07 15:04:13 -05003973 lpfc_reset_barrier(phba);
3974
Jamie Wellnitz41415862006-02-28 19:25:27 -05003975 to_slim = phba->MBslimaddr;
3976 writel(*(uint32_t *) mb, to_slim);
3977 readl(to_slim); /* flush */
3978
3979 /* Only skip post after fc_ffinit is completed */
James Smarteaf15d52008-12-04 22:39:29 -05003980 if (phba->pport->port_state)
Jamie Wellnitz41415862006-02-28 19:25:27 -05003981 word0 = 1; /* This is really setting up word1 */
James Smarteaf15d52008-12-04 22:39:29 -05003982 else
Jamie Wellnitz41415862006-02-28 19:25:27 -05003983 word0 = 0; /* This is really setting up word1 */
James Smart65a29c12006-07-06 15:50:50 -04003984 to_slim = phba->MBslimaddr + sizeof (uint32_t);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003985 writel(*(uint32_t *) mb, to_slim);
3986 readl(to_slim); /* flush */
3987
3988 lpfc_sli_brdreset(phba);
James Smart2e0fef82007-06-17 19:56:36 -05003989 phba->pport->stopped = 0;
3990 phba->link_state = LPFC_INIT_START;
James Smartda0436e2009-05-22 14:51:39 -04003991 phba->hba_flag = 0;
James Smart2e0fef82007-06-17 19:56:36 -05003992 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003993
James Smart64ba8812006-08-02 15:24:34 -04003994 memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
3995 psli->stats_start = get_seconds();
3996
James Smarteaf15d52008-12-04 22:39:29 -05003997 /* Give the INITFF and Post time to settle. */
3998 mdelay(100);
dea31012005-04-17 16:05:31 -05003999
James Smart0d878412009-10-02 15:16:56 -04004000 /* Reset HBA AER if it was enabled, note hba_flag was reset above */
4001 if (hba_aer_enabled)
4002 pci_disable_pcie_error_reporting(phba->pcidev);
4003
Jamie Wellnitz41415862006-02-28 19:25:27 -05004004 lpfc_hba_down_post(phba);
dea31012005-04-17 16:05:31 -05004005
4006 return 0;
4007}
4008
James Smarte59058c2008-08-24 21:49:00 -04004009/**
James Smartda0436e2009-05-22 14:51:39 -04004010 * lpfc_sli_brdrestart_s4 - Restart the sli-4 hba
4011 * @phba: Pointer to HBA context object.
4012 *
4013 * This function is called in the SLI initialization code path to restart
4014 * a SLI4 HBA. The caller is not required to hold any lock.
4015 * At the end of the function, it calls lpfc_hba_down_post function to
4016 * free any pending commands.
4017 **/
4018static int
4019lpfc_sli_brdrestart_s4(struct lpfc_hba *phba)
4020{
4021 struct lpfc_sli *psli = &phba->sli;
James Smart75baf692010-06-08 18:31:21 -04004022 uint32_t hba_aer_enabled;
James Smartda0436e2009-05-22 14:51:39 -04004023
4024 /* Restart HBA */
4025 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4026 "0296 Restart HBA Data: x%x x%x\n",
4027 phba->pport->port_state, psli->sli_flag);
4028
James Smart75baf692010-06-08 18:31:21 -04004029 /* Take PCIe device Advanced Error Reporting (AER) state */
4030 hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
4031
James Smartda0436e2009-05-22 14:51:39 -04004032 lpfc_sli4_brdreset(phba);
4033
4034 spin_lock_irq(&phba->hbalock);
4035 phba->pport->stopped = 0;
4036 phba->link_state = LPFC_INIT_START;
4037 phba->hba_flag = 0;
4038 spin_unlock_irq(&phba->hbalock);
4039
4040 memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
4041 psli->stats_start = get_seconds();
4042
James Smart75baf692010-06-08 18:31:21 -04004043 /* Reset HBA AER if it was enabled, note hba_flag was reset above */
4044 if (hba_aer_enabled)
4045 pci_disable_pcie_error_reporting(phba->pcidev);
4046
James Smartda0436e2009-05-22 14:51:39 -04004047 lpfc_hba_down_post(phba);
4048
4049 return 0;
4050}
4051
4052/**
4053 * lpfc_sli_brdrestart - Wrapper func for restarting hba
4054 * @phba: Pointer to HBA context object.
4055 *
4056 * This routine wraps the actual SLI3 or SLI4 hba restart routine from the
4057 * API jump table function pointer from the lpfc_hba struct.
4058**/
4059int
4060lpfc_sli_brdrestart(struct lpfc_hba *phba)
4061{
4062 return phba->lpfc_sli_brdrestart(phba);
4063}
4064
4065/**
James Smart3621a712009-04-06 18:47:14 -04004066 * lpfc_sli_chipset_init - Wait for the restart of the HBA after a restart
James Smarte59058c2008-08-24 21:49:00 -04004067 * @phba: Pointer to HBA context object.
4068 *
4069 * This function is called after a HBA restart to wait for successful
4070 * restart of the HBA. Successful restart of the HBA is indicated by
4071 * HS_FFRDY and HS_MBRDY bits. If the HBA fails to restart even after 15
4072 * iteration, the function will restart the HBA again. The function returns
4073 * zero if HBA successfully restarted else returns negative error code.
4074 **/
dea31012005-04-17 16:05:31 -05004075static int
4076lpfc_sli_chipset_init(struct lpfc_hba *phba)
4077{
4078 uint32_t status, i = 0;
4079
4080 /* Read the HBA Host Status Register */
James Smart9940b972011-03-11 16:06:12 -05004081 if (lpfc_readl(phba->HSregaddr, &status))
4082 return -EIO;
dea31012005-04-17 16:05:31 -05004083
4084 /* Check status register to see what current state is */
4085 i = 0;
4086 while ((status & (HS_FFRDY | HS_MBRDY)) != (HS_FFRDY | HS_MBRDY)) {
4087
James Smartdcf2a4e2010-09-29 11:18:53 -04004088 /* Check every 10ms for 10 retries, then every 100ms for 90
4089 * retries, then every 1 sec for 50 retires for a total of
4090 * ~60 seconds before reset the board again and check every
4091 * 1 sec for 50 retries. The up to 60 seconds before the
4092 * board ready is required by the Falcon FIPS zeroization
4093 * complete, and any reset the board in between shall cause
4094 * restart of zeroization, further delay the board ready.
dea31012005-04-17 16:05:31 -05004095 */
James Smartdcf2a4e2010-09-29 11:18:53 -04004096 if (i++ >= 200) {
dea31012005-04-17 16:05:31 -05004097 /* Adapter failed to init, timeout, status reg
4098 <status> */
James Smarted957682007-06-17 19:56:37 -05004099 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004100 "0436 Adapter failed to init, "
James Smart09372822008-01-11 01:52:54 -05004101 "timeout, status reg x%x, "
4102 "FW Data: A8 x%x AC x%x\n", status,
4103 readl(phba->MBslimaddr + 0xa8),
4104 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05004105 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05004106 return -ETIMEDOUT;
4107 }
4108
4109 /* Check to see if any errors occurred during init */
4110 if (status & HS_FFERM) {
4111 /* ERROR: During chipset initialization */
4112 /* Adapter failed to init, chipset, status reg
4113 <status> */
James Smarted957682007-06-17 19:56:37 -05004114 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004115 "0437 Adapter failed to init, "
James Smart09372822008-01-11 01:52:54 -05004116 "chipset, status reg x%x, "
4117 "FW Data: A8 x%x AC x%x\n", status,
4118 readl(phba->MBslimaddr + 0xa8),
4119 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05004120 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05004121 return -EIO;
4122 }
4123
James Smartdcf2a4e2010-09-29 11:18:53 -04004124 if (i <= 10)
dea31012005-04-17 16:05:31 -05004125 msleep(10);
James Smartdcf2a4e2010-09-29 11:18:53 -04004126 else if (i <= 100)
4127 msleep(100);
4128 else
4129 msleep(1000);
dea31012005-04-17 16:05:31 -05004130
James Smartdcf2a4e2010-09-29 11:18:53 -04004131 if (i == 150) {
4132 /* Do post */
James Smart92d7f7b2007-06-17 19:56:38 -05004133 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004134 lpfc_sli_brdrestart(phba);
dea31012005-04-17 16:05:31 -05004135 }
4136 /* Read the HBA Host Status Register */
James Smart9940b972011-03-11 16:06:12 -05004137 if (lpfc_readl(phba->HSregaddr, &status))
4138 return -EIO;
dea31012005-04-17 16:05:31 -05004139 }
4140
4141 /* Check to see if any errors occurred during init */
4142 if (status & HS_FFERM) {
4143 /* ERROR: During chipset initialization */
4144 /* Adapter failed to init, chipset, status reg <status> */
James Smarted957682007-06-17 19:56:37 -05004145 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004146 "0438 Adapter failed to init, chipset, "
James Smart09372822008-01-11 01:52:54 -05004147 "status reg x%x, "
4148 "FW Data: A8 x%x AC x%x\n", status,
4149 readl(phba->MBslimaddr + 0xa8),
4150 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05004151 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05004152 return -EIO;
4153 }
4154
4155 /* Clear all interrupt enable conditions */
4156 writel(0, phba->HCregaddr);
4157 readl(phba->HCregaddr); /* flush */
4158
4159 /* setup host attn register */
4160 writel(0xffffffff, phba->HAregaddr);
4161 readl(phba->HAregaddr); /* flush */
4162 return 0;
4163}
4164
James Smarte59058c2008-08-24 21:49:00 -04004165/**
James Smart3621a712009-04-06 18:47:14 -04004166 * lpfc_sli_hbq_count - Get the number of HBQs to be configured
James Smarte59058c2008-08-24 21:49:00 -04004167 *
4168 * This function calculates and returns the number of HBQs required to be
4169 * configured.
4170 **/
James Smart78b2d852007-08-02 11:10:21 -04004171int
James Smarted957682007-06-17 19:56:37 -05004172lpfc_sli_hbq_count(void)
4173{
James Smart92d7f7b2007-06-17 19:56:38 -05004174 return ARRAY_SIZE(lpfc_hbq_defs);
James Smarted957682007-06-17 19:56:37 -05004175}
4176
James Smarte59058c2008-08-24 21:49:00 -04004177/**
James Smart3621a712009-04-06 18:47:14 -04004178 * lpfc_sli_hbq_entry_count - Calculate total number of hbq entries
James Smarte59058c2008-08-24 21:49:00 -04004179 *
4180 * This function adds the number of hbq entries in every HBQ to get
4181 * the total number of hbq entries required for the HBA and returns
4182 * the total count.
4183 **/
James Smarted957682007-06-17 19:56:37 -05004184static int
4185lpfc_sli_hbq_entry_count(void)
4186{
4187 int hbq_count = lpfc_sli_hbq_count();
4188 int count = 0;
4189 int i;
4190
4191 for (i = 0; i < hbq_count; ++i)
James Smart92d7f7b2007-06-17 19:56:38 -05004192 count += lpfc_hbq_defs[i]->entry_count;
James Smarted957682007-06-17 19:56:37 -05004193 return count;
4194}
4195
James Smarte59058c2008-08-24 21:49:00 -04004196/**
James Smart3621a712009-04-06 18:47:14 -04004197 * lpfc_sli_hbq_size - Calculate memory required for all hbq entries
James Smarte59058c2008-08-24 21:49:00 -04004198 *
4199 * This function calculates amount of memory required for all hbq entries
4200 * to be configured and returns the total memory required.
4201 **/
dea31012005-04-17 16:05:31 -05004202int
James Smarted957682007-06-17 19:56:37 -05004203lpfc_sli_hbq_size(void)
4204{
4205 return lpfc_sli_hbq_entry_count() * sizeof(struct lpfc_hbq_entry);
4206}
4207
James Smarte59058c2008-08-24 21:49:00 -04004208/**
James Smart3621a712009-04-06 18:47:14 -04004209 * lpfc_sli_hbq_setup - configure and initialize HBQs
James Smarte59058c2008-08-24 21:49:00 -04004210 * @phba: Pointer to HBA context object.
4211 *
4212 * This function is called during the SLI initialization to configure
4213 * all the HBQs and post buffers to the HBQ. The caller is not
4214 * required to hold any locks. This function will return zero if successful
4215 * else it will return negative error code.
4216 **/
James Smarted957682007-06-17 19:56:37 -05004217static int
4218lpfc_sli_hbq_setup(struct lpfc_hba *phba)
4219{
4220 int hbq_count = lpfc_sli_hbq_count();
4221 LPFC_MBOXQ_t *pmb;
4222 MAILBOX_t *pmbox;
4223 uint32_t hbqno;
4224 uint32_t hbq_entry_index;
James Smarted957682007-06-17 19:56:37 -05004225
James Smart92d7f7b2007-06-17 19:56:38 -05004226 /* Get a Mailbox buffer to setup mailbox
4227 * commands for HBA initialization
4228 */
James Smarted957682007-06-17 19:56:37 -05004229 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4230
4231 if (!pmb)
4232 return -ENOMEM;
4233
James Smart04c68492009-05-22 14:52:52 -04004234 pmbox = &pmb->u.mb;
James Smarted957682007-06-17 19:56:37 -05004235
4236 /* Initialize the struct lpfc_sli_hbq structure for each hbq */
4237 phba->link_state = LPFC_INIT_MBX_CMDS;
James Smart3163f722008-02-08 18:50:25 -05004238 phba->hbq_in_use = 1;
James Smarted957682007-06-17 19:56:37 -05004239
4240 hbq_entry_index = 0;
4241 for (hbqno = 0; hbqno < hbq_count; ++hbqno) {
4242 phba->hbqs[hbqno].next_hbqPutIdx = 0;
4243 phba->hbqs[hbqno].hbqPutIdx = 0;
4244 phba->hbqs[hbqno].local_hbqGetIdx = 0;
4245 phba->hbqs[hbqno].entry_count =
James Smart92d7f7b2007-06-17 19:56:38 -05004246 lpfc_hbq_defs[hbqno]->entry_count;
James Smart51ef4c22007-08-02 11:10:31 -04004247 lpfc_config_hbq(phba, hbqno, lpfc_hbq_defs[hbqno],
4248 hbq_entry_index, pmb);
James Smarted957682007-06-17 19:56:37 -05004249 hbq_entry_index += phba->hbqs[hbqno].entry_count;
4250
4251 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
4252 /* Adapter failed to init, mbxCmd <cmd> CFG_RING,
4253 mbxStatus <status>, ring <num> */
4254
4255 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -05004256 LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04004257 "1805 Adapter failed to init. "
James Smarted957682007-06-17 19:56:37 -05004258 "Data: x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04004259 pmbox->mbxCommand,
James Smarted957682007-06-17 19:56:37 -05004260 pmbox->mbxStatus, hbqno);
4261
4262 phba->link_state = LPFC_HBA_ERROR;
4263 mempool_free(pmb, phba->mbox_mem_pool);
James Smart6e7288d2010-06-07 15:23:35 -04004264 return -ENXIO;
James Smarted957682007-06-17 19:56:37 -05004265 }
4266 }
4267 phba->hbq_count = hbq_count;
4268
James Smarted957682007-06-17 19:56:37 -05004269 mempool_free(pmb, phba->mbox_mem_pool);
4270
James Smart92d7f7b2007-06-17 19:56:38 -05004271 /* Initially populate or replenish the HBQs */
James Smartd7c255b2008-08-24 21:50:00 -04004272 for (hbqno = 0; hbqno < hbq_count; ++hbqno)
4273 lpfc_sli_hbqbuf_init_hbqs(phba, hbqno);
James Smarted957682007-06-17 19:56:37 -05004274 return 0;
4275}
4276
James Smarte59058c2008-08-24 21:49:00 -04004277/**
James Smart4f774512009-05-22 14:52:35 -04004278 * lpfc_sli4_rb_setup - Initialize and post RBs to HBA
4279 * @phba: Pointer to HBA context object.
4280 *
4281 * This function is called during the SLI initialization to configure
4282 * all the HBQs and post buffers to the HBQ. The caller is not
4283 * required to hold any locks. This function will return zero if successful
4284 * else it will return negative error code.
4285 **/
4286static int
4287lpfc_sli4_rb_setup(struct lpfc_hba *phba)
4288{
4289 phba->hbq_in_use = 1;
4290 phba->hbqs[0].entry_count = lpfc_hbq_defs[0]->entry_count;
4291 phba->hbq_count = 1;
4292 /* Initially populate or replenish the HBQs */
4293 lpfc_sli_hbqbuf_init_hbqs(phba, 0);
4294 return 0;
4295}
4296
4297/**
James Smart3621a712009-04-06 18:47:14 -04004298 * lpfc_sli_config_port - Issue config port mailbox command
James Smarte59058c2008-08-24 21:49:00 -04004299 * @phba: Pointer to HBA context object.
4300 * @sli_mode: sli mode - 2/3
4301 *
4302 * This function is called by the sli intialization code path
4303 * to issue config_port mailbox command. This function restarts the
4304 * HBA firmware and issues a config_port mailbox command to configure
4305 * the SLI interface in the sli mode specified by sli_mode
4306 * variable. The caller is not required to hold any locks.
4307 * The function returns 0 if successful, else returns negative error
4308 * code.
4309 **/
James Smart93996272008-08-24 21:50:30 -04004310int
4311lpfc_sli_config_port(struct lpfc_hba *phba, int sli_mode)
dea31012005-04-17 16:05:31 -05004312{
4313 LPFC_MBOXQ_t *pmb;
4314 uint32_t resetcount = 0, rc = 0, done = 0;
4315
4316 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4317 if (!pmb) {
James Smart2e0fef82007-06-17 19:56:36 -05004318 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05004319 return -ENOMEM;
4320 }
4321
James Smarted957682007-06-17 19:56:37 -05004322 phba->sli_rev = sli_mode;
dea31012005-04-17 16:05:31 -05004323 while (resetcount < 2 && !done) {
James Smart2e0fef82007-06-17 19:56:36 -05004324 spin_lock_irq(&phba->hbalock);
James Smart1c067a42006-08-01 07:33:52 -04004325 phba->sli.sli_flag |= LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05004326 spin_unlock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -05004327 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004328 lpfc_sli_brdrestart(phba);
dea31012005-04-17 16:05:31 -05004329 rc = lpfc_sli_chipset_init(phba);
4330 if (rc)
4331 break;
4332
James Smart2e0fef82007-06-17 19:56:36 -05004333 spin_lock_irq(&phba->hbalock);
James Smart1c067a42006-08-01 07:33:52 -04004334 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05004335 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004336 resetcount++;
4337
James Smarted957682007-06-17 19:56:37 -05004338 /* Call pre CONFIG_PORT mailbox command initialization. A
4339 * value of 0 means the call was successful. Any other
4340 * nonzero value is a failure, but if ERESTART is returned,
4341 * the driver may reset the HBA and try again.
4342 */
dea31012005-04-17 16:05:31 -05004343 rc = lpfc_config_port_prep(phba);
4344 if (rc == -ERESTART) {
James Smarted957682007-06-17 19:56:37 -05004345 phba->link_state = LPFC_LINK_UNKNOWN;
dea31012005-04-17 16:05:31 -05004346 continue;
James Smart34b02dc2008-08-24 21:49:55 -04004347 } else if (rc)
dea31012005-04-17 16:05:31 -05004348 break;
James Smart6d368e52011-05-24 11:44:12 -04004349
James Smart2e0fef82007-06-17 19:56:36 -05004350 phba->link_state = LPFC_INIT_MBX_CMDS;
dea31012005-04-17 16:05:31 -05004351 lpfc_config_port(phba, pmb);
4352 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
James Smart34b02dc2008-08-24 21:49:55 -04004353 phba->sli3_options &= ~(LPFC_SLI3_NPIV_ENABLED |
4354 LPFC_SLI3_HBQ_ENABLED |
4355 LPFC_SLI3_CRP_ENABLED |
James Smartbc739052010-08-04 16:11:18 -04004356 LPFC_SLI3_BG_ENABLED |
4357 LPFC_SLI3_DSS_ENABLED);
James Smarted957682007-06-17 19:56:37 -05004358 if (rc != MBX_SUCCESS) {
dea31012005-04-17 16:05:31 -05004359 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004360 "0442 Adapter failed to init, mbxCmd x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05004361 "CONFIG_PORT, mbxStatus x%x Data: x%x\n",
James Smart04c68492009-05-22 14:52:52 -04004362 pmb->u.mb.mbxCommand, pmb->u.mb.mbxStatus, 0);
James Smart2e0fef82007-06-17 19:56:36 -05004363 spin_lock_irq(&phba->hbalock);
James Smart04c68492009-05-22 14:52:52 -04004364 phba->sli.sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05004365 spin_unlock_irq(&phba->hbalock);
4366 rc = -ENXIO;
James Smart04c68492009-05-22 14:52:52 -04004367 } else {
4368 /* Allow asynchronous mailbox command to go through */
4369 spin_lock_irq(&phba->hbalock);
4370 phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
4371 spin_unlock_irq(&phba->hbalock);
James Smarted957682007-06-17 19:56:37 -05004372 done = 1;
James Smartcb69f7d2011-12-13 13:21:57 -05004373
4374 if ((pmb->u.mb.un.varCfgPort.casabt == 1) &&
4375 (pmb->u.mb.un.varCfgPort.gasabt == 0))
4376 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
4377 "3110 Port did not grant ASABT\n");
James Smart04c68492009-05-22 14:52:52 -04004378 }
dea31012005-04-17 16:05:31 -05004379 }
James Smarted957682007-06-17 19:56:37 -05004380 if (!done) {
4381 rc = -EINVAL;
4382 goto do_prep_failed;
4383 }
James Smart04c68492009-05-22 14:52:52 -04004384 if (pmb->u.mb.un.varCfgPort.sli_mode == 3) {
4385 if (!pmb->u.mb.un.varCfgPort.cMA) {
James Smart34b02dc2008-08-24 21:49:55 -04004386 rc = -ENXIO;
4387 goto do_prep_failed;
4388 }
James Smart04c68492009-05-22 14:52:52 -04004389 if (phba->max_vpi && pmb->u.mb.un.varCfgPort.gmv) {
James Smart34b02dc2008-08-24 21:49:55 -04004390 phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
James Smart04c68492009-05-22 14:52:52 -04004391 phba->max_vpi = pmb->u.mb.un.varCfgPort.max_vpi;
4392 phba->max_vports = (phba->max_vpi > phba->max_vports) ?
4393 phba->max_vpi : phba->max_vports;
4394
James Smart34b02dc2008-08-24 21:49:55 -04004395 } else
4396 phba->max_vpi = 0;
James Smartbc739052010-08-04 16:11:18 -04004397 phba->fips_level = 0;
4398 phba->fips_spec_rev = 0;
4399 if (pmb->u.mb.un.varCfgPort.gdss) {
James Smart04c68492009-05-22 14:52:52 -04004400 phba->sli3_options |= LPFC_SLI3_DSS_ENABLED;
James Smartbc739052010-08-04 16:11:18 -04004401 phba->fips_level = pmb->u.mb.un.varCfgPort.fips_level;
4402 phba->fips_spec_rev = pmb->u.mb.un.varCfgPort.fips_rev;
4403 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4404 "2850 Security Crypto Active. FIPS x%d "
4405 "(Spec Rev: x%d)",
4406 phba->fips_level, phba->fips_spec_rev);
4407 }
4408 if (pmb->u.mb.un.varCfgPort.sec_err) {
4409 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4410 "2856 Config Port Security Crypto "
4411 "Error: x%x ",
4412 pmb->u.mb.un.varCfgPort.sec_err);
4413 }
James Smart04c68492009-05-22 14:52:52 -04004414 if (pmb->u.mb.un.varCfgPort.gerbm)
James Smart34b02dc2008-08-24 21:49:55 -04004415 phba->sli3_options |= LPFC_SLI3_HBQ_ENABLED;
James Smart04c68492009-05-22 14:52:52 -04004416 if (pmb->u.mb.un.varCfgPort.gcrp)
James Smart34b02dc2008-08-24 21:49:55 -04004417 phba->sli3_options |= LPFC_SLI3_CRP_ENABLED;
James Smart6e7288d2010-06-07 15:23:35 -04004418
4419 phba->hbq_get = phba->mbox->us.s3_pgp.hbq_get;
4420 phba->port_gp = phba->mbox->us.s3_pgp.port;
James Smarte2a0a9d2008-12-04 22:40:02 -05004421
4422 if (phba->cfg_enable_bg) {
James Smart04c68492009-05-22 14:52:52 -04004423 if (pmb->u.mb.un.varCfgPort.gbg)
James Smarte2a0a9d2008-12-04 22:40:02 -05004424 phba->sli3_options |= LPFC_SLI3_BG_ENABLED;
4425 else
4426 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4427 "0443 Adapter did not grant "
4428 "BlockGuard\n");
4429 }
James Smart34b02dc2008-08-24 21:49:55 -04004430 } else {
James Smart8f34f4c2008-12-04 22:39:23 -05004431 phba->hbq_get = NULL;
James Smart34b02dc2008-08-24 21:49:55 -04004432 phba->port_gp = phba->mbox->us.s2.port;
James Smartd7c255b2008-08-24 21:50:00 -04004433 phba->max_vpi = 0;
James Smarted957682007-06-17 19:56:37 -05004434 }
James Smart92d7f7b2007-06-17 19:56:38 -05004435do_prep_failed:
James Smarted957682007-06-17 19:56:37 -05004436 mempool_free(pmb, phba->mbox_mem_pool);
4437 return rc;
4438}
4439
James Smarte59058c2008-08-24 21:49:00 -04004440
4441/**
James Smart3621a712009-04-06 18:47:14 -04004442 * lpfc_sli_hba_setup - SLI intialization function
James Smarte59058c2008-08-24 21:49:00 -04004443 * @phba: Pointer to HBA context object.
4444 *
4445 * This function is the main SLI intialization function. This function
4446 * is called by the HBA intialization code, HBA reset code and HBA
4447 * error attention handler code. Caller is not required to hold any
4448 * locks. This function issues config_port mailbox command to configure
4449 * the SLI, setup iocb rings and HBQ rings. In the end the function
4450 * calls the config_port_post function to issue init_link mailbox
4451 * command and to start the discovery. The function will return zero
4452 * if successful, else it will return negative error code.
4453 **/
James Smarted957682007-06-17 19:56:37 -05004454int
4455lpfc_sli_hba_setup(struct lpfc_hba *phba)
4456{
4457 uint32_t rc;
James Smart6d368e52011-05-24 11:44:12 -04004458 int mode = 3, i;
4459 int longs;
James Smarted957682007-06-17 19:56:37 -05004460
4461 switch (lpfc_sli_mode) {
4462 case 2:
James Smart78b2d852007-08-02 11:10:21 -04004463 if (phba->cfg_enable_npiv) {
James Smart92d7f7b2007-06-17 19:56:38 -05004464 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04004465 "1824 NPIV enabled: Override lpfc_sli_mode "
James Smart92d7f7b2007-06-17 19:56:38 -05004466 "parameter (%d) to auto (0).\n",
James Smarte8b62012007-08-02 11:10:09 -04004467 lpfc_sli_mode);
James Smart92d7f7b2007-06-17 19:56:38 -05004468 break;
4469 }
James Smarted957682007-06-17 19:56:37 -05004470 mode = 2;
4471 break;
4472 case 0:
4473 case 3:
4474 break;
4475 default:
James Smart92d7f7b2007-06-17 19:56:38 -05004476 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04004477 "1819 Unrecognized lpfc_sli_mode "
4478 "parameter: %d.\n", lpfc_sli_mode);
James Smarted957682007-06-17 19:56:37 -05004479
4480 break;
4481 }
4482
James Smart93996272008-08-24 21:50:30 -04004483 rc = lpfc_sli_config_port(phba, mode);
4484
James Smarted957682007-06-17 19:56:37 -05004485 if (rc && lpfc_sli_mode == 3)
James Smart92d7f7b2007-06-17 19:56:38 -05004486 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04004487 "1820 Unable to select SLI-3. "
4488 "Not supported by adapter.\n");
James Smarted957682007-06-17 19:56:37 -05004489 if (rc && mode != 2)
James Smart93996272008-08-24 21:50:30 -04004490 rc = lpfc_sli_config_port(phba, 2);
James Smarted957682007-06-17 19:56:37 -05004491 if (rc)
dea31012005-04-17 16:05:31 -05004492 goto lpfc_sli_hba_setup_error;
4493
James Smart0d878412009-10-02 15:16:56 -04004494 /* Enable PCIe device Advanced Error Reporting (AER) if configured */
4495 if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
4496 rc = pci_enable_pcie_error_reporting(phba->pcidev);
4497 if (!rc) {
4498 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4499 "2709 This device supports "
4500 "Advanced Error Reporting (AER)\n");
4501 spin_lock_irq(&phba->hbalock);
4502 phba->hba_flag |= HBA_AER_ENABLED;
4503 spin_unlock_irq(&phba->hbalock);
4504 } else {
4505 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4506 "2708 This device does not support "
4507 "Advanced Error Reporting (AER)\n");
4508 phba->cfg_aer_support = 0;
4509 }
4510 }
4511
James Smarted957682007-06-17 19:56:37 -05004512 if (phba->sli_rev == 3) {
4513 phba->iocb_cmd_size = SLI3_IOCB_CMD_SIZE;
4514 phba->iocb_rsp_size = SLI3_IOCB_RSP_SIZE;
James Smarted957682007-06-17 19:56:37 -05004515 } else {
4516 phba->iocb_cmd_size = SLI2_IOCB_CMD_SIZE;
4517 phba->iocb_rsp_size = SLI2_IOCB_RSP_SIZE;
James Smart92d7f7b2007-06-17 19:56:38 -05004518 phba->sli3_options = 0;
James Smarted957682007-06-17 19:56:37 -05004519 }
4520
4521 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004522 "0444 Firmware in SLI %x mode. Max_vpi %d\n",
4523 phba->sli_rev, phba->max_vpi);
James Smarted957682007-06-17 19:56:37 -05004524 rc = lpfc_sli_ring_map(phba);
dea31012005-04-17 16:05:31 -05004525
4526 if (rc)
4527 goto lpfc_sli_hba_setup_error;
4528
James Smart6d368e52011-05-24 11:44:12 -04004529 /* Initialize VPIs. */
4530 if (phba->sli_rev == LPFC_SLI_REV3) {
4531 /*
4532 * The VPI bitmask and physical ID array are allocated
4533 * and initialized once only - at driver load. A port
4534 * reset doesn't need to reinitialize this memory.
4535 */
4536 if ((phba->vpi_bmask == NULL) && (phba->vpi_ids == NULL)) {
4537 longs = (phba->max_vpi + BITS_PER_LONG) / BITS_PER_LONG;
4538 phba->vpi_bmask = kzalloc(longs * sizeof(unsigned long),
4539 GFP_KERNEL);
4540 if (!phba->vpi_bmask) {
4541 rc = -ENOMEM;
4542 goto lpfc_sli_hba_setup_error;
4543 }
4544
4545 phba->vpi_ids = kzalloc(
4546 (phba->max_vpi+1) * sizeof(uint16_t),
4547 GFP_KERNEL);
4548 if (!phba->vpi_ids) {
4549 kfree(phba->vpi_bmask);
4550 rc = -ENOMEM;
4551 goto lpfc_sli_hba_setup_error;
4552 }
4553 for (i = 0; i < phba->max_vpi; i++)
4554 phba->vpi_ids[i] = i;
4555 }
4556 }
4557
James Smart93996272008-08-24 21:50:30 -04004558 /* Init HBQs */
James Smarted957682007-06-17 19:56:37 -05004559 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
4560 rc = lpfc_sli_hbq_setup(phba);
4561 if (rc)
4562 goto lpfc_sli_hba_setup_error;
4563 }
James Smart04c68492009-05-22 14:52:52 -04004564 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004565 phba->sli.sli_flag |= LPFC_PROCESS_LA;
James Smart04c68492009-05-22 14:52:52 -04004566 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004567
4568 rc = lpfc_config_port_post(phba);
4569 if (rc)
4570 goto lpfc_sli_hba_setup_error;
4571
James Smarted957682007-06-17 19:56:37 -05004572 return rc;
4573
James Smart92d7f7b2007-06-17 19:56:38 -05004574lpfc_sli_hba_setup_error:
James Smart2e0fef82007-06-17 19:56:36 -05004575 phba->link_state = LPFC_HBA_ERROR;
James Smarte40a02c2010-02-26 14:13:54 -05004576 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004577 "0445 Firmware initialization failed\n");
dea31012005-04-17 16:05:31 -05004578 return rc;
4579}
4580
James Smartda0436e2009-05-22 14:51:39 -04004581/**
4582 * lpfc_sli4_read_fcoe_params - Read fcoe params from conf region
4583 * @phba: Pointer to HBA context object.
4584 * @mboxq: mailbox pointer.
4585 * This function issue a dump mailbox command to read config region
4586 * 23 and parse the records in the region and populate driver
4587 * data structure.
4588 **/
4589static int
James Smartff78d8f2011-12-13 13:21:35 -05004590lpfc_sli4_read_fcoe_params(struct lpfc_hba *phba)
James Smartda0436e2009-05-22 14:51:39 -04004591{
James Smartff78d8f2011-12-13 13:21:35 -05004592 LPFC_MBOXQ_t *mboxq;
James Smartda0436e2009-05-22 14:51:39 -04004593 struct lpfc_dmabuf *mp;
4594 struct lpfc_mqe *mqe;
4595 uint32_t data_length;
4596 int rc;
4597
4598 /* Program the default value of vlan_id and fc_map */
4599 phba->valid_vlan = 0;
4600 phba->fc_map[0] = LPFC_FCOE_FCF_MAP0;
4601 phba->fc_map[1] = LPFC_FCOE_FCF_MAP1;
4602 phba->fc_map[2] = LPFC_FCOE_FCF_MAP2;
4603
James Smartff78d8f2011-12-13 13:21:35 -05004604 mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4605 if (!mboxq)
James Smartda0436e2009-05-22 14:51:39 -04004606 return -ENOMEM;
4607
James Smartff78d8f2011-12-13 13:21:35 -05004608 mqe = &mboxq->u.mqe;
4609 if (lpfc_sli4_dump_cfg_rg23(phba, mboxq)) {
4610 rc = -ENOMEM;
4611 goto out_free_mboxq;
4612 }
4613
James Smartda0436e2009-05-22 14:51:39 -04004614 mp = (struct lpfc_dmabuf *) mboxq->context1;
4615 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4616
4617 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
4618 "(%d):2571 Mailbox cmd x%x Status x%x "
4619 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x "
4620 "x%x x%x x%x x%x x%x x%x x%x x%x x%x "
4621 "CQ: x%x x%x x%x x%x\n",
4622 mboxq->vport ? mboxq->vport->vpi : 0,
4623 bf_get(lpfc_mqe_command, mqe),
4624 bf_get(lpfc_mqe_status, mqe),
4625 mqe->un.mb_words[0], mqe->un.mb_words[1],
4626 mqe->un.mb_words[2], mqe->un.mb_words[3],
4627 mqe->un.mb_words[4], mqe->un.mb_words[5],
4628 mqe->un.mb_words[6], mqe->un.mb_words[7],
4629 mqe->un.mb_words[8], mqe->un.mb_words[9],
4630 mqe->un.mb_words[10], mqe->un.mb_words[11],
4631 mqe->un.mb_words[12], mqe->un.mb_words[13],
4632 mqe->un.mb_words[14], mqe->un.mb_words[15],
4633 mqe->un.mb_words[16], mqe->un.mb_words[50],
4634 mboxq->mcqe.word0,
4635 mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
4636 mboxq->mcqe.trailer);
4637
4638 if (rc) {
4639 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4640 kfree(mp);
James Smartff78d8f2011-12-13 13:21:35 -05004641 rc = -EIO;
4642 goto out_free_mboxq;
James Smartda0436e2009-05-22 14:51:39 -04004643 }
4644 data_length = mqe->un.mb_words[5];
James Smarta0c87cb2009-07-19 10:01:10 -04004645 if (data_length > DMP_RGN23_SIZE) {
James Smartd11e31d2009-06-10 17:23:06 -04004646 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4647 kfree(mp);
James Smartff78d8f2011-12-13 13:21:35 -05004648 rc = -EIO;
4649 goto out_free_mboxq;
James Smartd11e31d2009-06-10 17:23:06 -04004650 }
James Smartda0436e2009-05-22 14:51:39 -04004651
4652 lpfc_parse_fcoe_conf(phba, mp->virt, data_length);
4653 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4654 kfree(mp);
James Smartff78d8f2011-12-13 13:21:35 -05004655 rc = 0;
4656
4657out_free_mboxq:
4658 mempool_free(mboxq, phba->mbox_mem_pool);
4659 return rc;
James Smartda0436e2009-05-22 14:51:39 -04004660}
4661
4662/**
4663 * lpfc_sli4_read_rev - Issue READ_REV and collect vpd data
4664 * @phba: pointer to lpfc hba data structure.
4665 * @mboxq: pointer to the LPFC_MBOXQ_t structure.
4666 * @vpd: pointer to the memory to hold resulting port vpd data.
4667 * @vpd_size: On input, the number of bytes allocated to @vpd.
4668 * On output, the number of data bytes in @vpd.
4669 *
4670 * This routine executes a READ_REV SLI4 mailbox command. In
4671 * addition, this routine gets the port vpd data.
4672 *
4673 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02004674 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -04004675 * -ENOMEM - could not allocated memory.
James Smartda0436e2009-05-22 14:51:39 -04004676 **/
4677static int
4678lpfc_sli4_read_rev(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
4679 uint8_t *vpd, uint32_t *vpd_size)
4680{
4681 int rc = 0;
4682 uint32_t dma_size;
4683 struct lpfc_dmabuf *dmabuf;
4684 struct lpfc_mqe *mqe;
4685
4686 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
4687 if (!dmabuf)
4688 return -ENOMEM;
4689
4690 /*
4691 * Get a DMA buffer for the vpd data resulting from the READ_REV
4692 * mailbox command.
4693 */
4694 dma_size = *vpd_size;
4695 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
4696 dma_size,
4697 &dmabuf->phys,
4698 GFP_KERNEL);
4699 if (!dmabuf->virt) {
4700 kfree(dmabuf);
4701 return -ENOMEM;
4702 }
4703 memset(dmabuf->virt, 0, dma_size);
4704
4705 /*
4706 * The SLI4 implementation of READ_REV conflicts at word1,
4707 * bits 31:16 and SLI4 adds vpd functionality not present
4708 * in SLI3. This code corrects the conflicts.
4709 */
4710 lpfc_read_rev(phba, mboxq);
4711 mqe = &mboxq->u.mqe;
4712 mqe->un.read_rev.vpd_paddr_high = putPaddrHigh(dmabuf->phys);
4713 mqe->un.read_rev.vpd_paddr_low = putPaddrLow(dmabuf->phys);
4714 mqe->un.read_rev.word1 &= 0x0000FFFF;
4715 bf_set(lpfc_mbx_rd_rev_vpd, &mqe->un.read_rev, 1);
4716 bf_set(lpfc_mbx_rd_rev_avail_len, &mqe->un.read_rev, dma_size);
4717
4718 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4719 if (rc) {
4720 dma_free_coherent(&phba->pcidev->dev, dma_size,
4721 dmabuf->virt, dmabuf->phys);
James Smartdef9c7a2009-12-21 17:02:28 -05004722 kfree(dmabuf);
James Smartda0436e2009-05-22 14:51:39 -04004723 return -EIO;
4724 }
4725
James Smartda0436e2009-05-22 14:51:39 -04004726 /*
4727 * The available vpd length cannot be bigger than the
4728 * DMA buffer passed to the port. Catch the less than
4729 * case and update the caller's size.
4730 */
4731 if (mqe->un.read_rev.avail_vpd_len < *vpd_size)
4732 *vpd_size = mqe->un.read_rev.avail_vpd_len;
4733
James Smartd7c47992010-06-08 18:31:54 -04004734 memcpy(vpd, dmabuf->virt, *vpd_size);
4735
James Smartda0436e2009-05-22 14:51:39 -04004736 dma_free_coherent(&phba->pcidev->dev, dma_size,
4737 dmabuf->virt, dmabuf->phys);
4738 kfree(dmabuf);
4739 return 0;
4740}
4741
4742/**
James Smartcd1c8302011-10-10 21:33:25 -04004743 * lpfc_sli4_retrieve_pport_name - Retrieve SLI4 device physical port name
4744 * @phba: pointer to lpfc hba data structure.
4745 *
4746 * This routine retrieves SLI4 device physical port name this PCI function
4747 * is attached to.
4748 *
4749 * Return codes
4750 * 0 - sucessful
4751 * otherwise - failed to retrieve physical port name
4752 **/
4753static int
4754lpfc_sli4_retrieve_pport_name(struct lpfc_hba *phba)
4755{
4756 LPFC_MBOXQ_t *mboxq;
James Smartcd1c8302011-10-10 21:33:25 -04004757 struct lpfc_mbx_get_cntl_attributes *mbx_cntl_attr;
4758 struct lpfc_controller_attribute *cntl_attr;
4759 struct lpfc_mbx_get_port_name *get_port_name;
4760 void *virtaddr = NULL;
4761 uint32_t alloclen, reqlen;
4762 uint32_t shdr_status, shdr_add_status;
4763 union lpfc_sli4_cfg_shdr *shdr;
4764 char cport_name = 0;
4765 int rc;
4766
4767 /* We assume nothing at this point */
4768 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_INVAL;
4769 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_NON;
4770
4771 mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4772 if (!mboxq)
4773 return -ENOMEM;
James Smartcd1c8302011-10-10 21:33:25 -04004774 /* obtain link type and link number via READ_CONFIG */
James Smartff78d8f2011-12-13 13:21:35 -05004775 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_INVAL;
4776 lpfc_sli4_read_config(phba);
4777 if (phba->sli4_hba.lnk_info.lnk_dv == LPFC_LNK_DAT_VAL)
4778 goto retrieve_ppname;
James Smartcd1c8302011-10-10 21:33:25 -04004779
4780 /* obtain link type and link number via COMMON_GET_CNTL_ATTRIBUTES */
4781 reqlen = sizeof(struct lpfc_mbx_get_cntl_attributes);
4782 alloclen = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
4783 LPFC_MBOX_OPCODE_GET_CNTL_ATTRIBUTES, reqlen,
4784 LPFC_SLI4_MBX_NEMBED);
4785 if (alloclen < reqlen) {
4786 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4787 "3084 Allocated DMA memory size (%d) is "
4788 "less than the requested DMA memory size "
4789 "(%d)\n", alloclen, reqlen);
4790 rc = -ENOMEM;
4791 goto out_free_mboxq;
4792 }
4793 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4794 virtaddr = mboxq->sge_array->addr[0];
4795 mbx_cntl_attr = (struct lpfc_mbx_get_cntl_attributes *)virtaddr;
4796 shdr = &mbx_cntl_attr->cfg_shdr;
4797 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
4798 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
4799 if (shdr_status || shdr_add_status || rc) {
4800 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
4801 "3085 Mailbox x%x (x%x/x%x) failed, "
4802 "rc:x%x, status:x%x, add_status:x%x\n",
4803 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
4804 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
4805 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
4806 rc, shdr_status, shdr_add_status);
4807 rc = -ENXIO;
4808 goto out_free_mboxq;
4809 }
4810 cntl_attr = &mbx_cntl_attr->cntl_attr;
4811 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_VAL;
4812 phba->sli4_hba.lnk_info.lnk_tp =
4813 bf_get(lpfc_cntl_attr_lnk_type, cntl_attr);
4814 phba->sli4_hba.lnk_info.lnk_no =
4815 bf_get(lpfc_cntl_attr_lnk_numb, cntl_attr);
4816 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4817 "3086 lnk_type:%d, lnk_numb:%d\n",
4818 phba->sli4_hba.lnk_info.lnk_tp,
4819 phba->sli4_hba.lnk_info.lnk_no);
4820
4821retrieve_ppname:
4822 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
4823 LPFC_MBOX_OPCODE_GET_PORT_NAME,
4824 sizeof(struct lpfc_mbx_get_port_name) -
4825 sizeof(struct lpfc_sli4_cfg_mhdr),
4826 LPFC_SLI4_MBX_EMBED);
4827 get_port_name = &mboxq->u.mqe.un.get_port_name;
4828 shdr = (union lpfc_sli4_cfg_shdr *)&get_port_name->header.cfg_shdr;
4829 bf_set(lpfc_mbox_hdr_version, &shdr->request, LPFC_OPCODE_VERSION_1);
4830 bf_set(lpfc_mbx_get_port_name_lnk_type, &get_port_name->u.request,
4831 phba->sli4_hba.lnk_info.lnk_tp);
4832 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4833 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
4834 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
4835 if (shdr_status || shdr_add_status || rc) {
4836 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
4837 "3087 Mailbox x%x (x%x/x%x) failed: "
4838 "rc:x%x, status:x%x, add_status:x%x\n",
4839 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
4840 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
4841 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
4842 rc, shdr_status, shdr_add_status);
4843 rc = -ENXIO;
4844 goto out_free_mboxq;
4845 }
4846 switch (phba->sli4_hba.lnk_info.lnk_no) {
4847 case LPFC_LINK_NUMBER_0:
4848 cport_name = bf_get(lpfc_mbx_get_port_name_name0,
4849 &get_port_name->u.response);
4850 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
4851 break;
4852 case LPFC_LINK_NUMBER_1:
4853 cport_name = bf_get(lpfc_mbx_get_port_name_name1,
4854 &get_port_name->u.response);
4855 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
4856 break;
4857 case LPFC_LINK_NUMBER_2:
4858 cport_name = bf_get(lpfc_mbx_get_port_name_name2,
4859 &get_port_name->u.response);
4860 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
4861 break;
4862 case LPFC_LINK_NUMBER_3:
4863 cport_name = bf_get(lpfc_mbx_get_port_name_name3,
4864 &get_port_name->u.response);
4865 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
4866 break;
4867 default:
4868 break;
4869 }
4870
4871 if (phba->sli4_hba.pport_name_sta == LPFC_SLI4_PPNAME_GET) {
4872 phba->Port[0] = cport_name;
4873 phba->Port[1] = '\0';
4874 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4875 "3091 SLI get port name: %s\n", phba->Port);
4876 }
4877
4878out_free_mboxq:
4879 if (rc != MBX_TIMEOUT) {
4880 if (bf_get(lpfc_mqe_command, &mboxq->u.mqe) == MBX_SLI4_CONFIG)
4881 lpfc_sli4_mbox_cmd_free(phba, mboxq);
4882 else
4883 mempool_free(mboxq, phba->mbox_mem_pool);
4884 }
4885 return rc;
4886}
4887
4888/**
James Smartda0436e2009-05-22 14:51:39 -04004889 * lpfc_sli4_arm_cqeq_intr - Arm sli-4 device completion and event queues
4890 * @phba: pointer to lpfc hba data structure.
4891 *
4892 * This routine is called to explicitly arm the SLI4 device's completion and
4893 * event queues
4894 **/
4895static void
4896lpfc_sli4_arm_cqeq_intr(struct lpfc_hba *phba)
4897{
4898 uint8_t fcp_eqidx;
4899
4900 lpfc_sli4_cq_release(phba->sli4_hba.mbx_cq, LPFC_QUEUE_REARM);
4901 lpfc_sli4_cq_release(phba->sli4_hba.els_cq, LPFC_QUEUE_REARM);
James Smart05580562011-05-24 11:40:48 -04004902 fcp_eqidx = 0;
James Smart2e90f4b2011-12-13 13:22:37 -05004903 if (phba->sli4_hba.fcp_cq) {
4904 do
4905 lpfc_sli4_cq_release(phba->sli4_hba.fcp_cq[fcp_eqidx],
4906 LPFC_QUEUE_REARM);
4907 while (++fcp_eqidx < phba->cfg_fcp_eq_count);
4908 }
James Smartda0436e2009-05-22 14:51:39 -04004909 lpfc_sli4_eq_release(phba->sli4_hba.sp_eq, LPFC_QUEUE_REARM);
James Smart2e90f4b2011-12-13 13:22:37 -05004910 if (phba->sli4_hba.fp_eq) {
4911 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count;
4912 fcp_eqidx++)
4913 lpfc_sli4_eq_release(phba->sli4_hba.fp_eq[fcp_eqidx],
4914 LPFC_QUEUE_REARM);
4915 }
James Smartda0436e2009-05-22 14:51:39 -04004916}
4917
4918/**
James Smart6d368e52011-05-24 11:44:12 -04004919 * lpfc_sli4_get_avail_extnt_rsrc - Get available resource extent count.
4920 * @phba: Pointer to HBA context object.
4921 * @type: The resource extent type.
James Smartb76f2dc2011-07-22 18:37:42 -04004922 * @extnt_count: buffer to hold port available extent count.
4923 * @extnt_size: buffer to hold element count per extent.
James Smart6d368e52011-05-24 11:44:12 -04004924 *
James Smartb76f2dc2011-07-22 18:37:42 -04004925 * This function calls the port and retrievs the number of available
4926 * extents and their size for a particular extent type.
4927 *
4928 * Returns: 0 if successful. Nonzero otherwise.
James Smart6d368e52011-05-24 11:44:12 -04004929 **/
James Smartb76f2dc2011-07-22 18:37:42 -04004930int
James Smart6d368e52011-05-24 11:44:12 -04004931lpfc_sli4_get_avail_extnt_rsrc(struct lpfc_hba *phba, uint16_t type,
4932 uint16_t *extnt_count, uint16_t *extnt_size)
4933{
4934 int rc = 0;
4935 uint32_t length;
4936 uint32_t mbox_tmo;
4937 struct lpfc_mbx_get_rsrc_extent_info *rsrc_info;
4938 LPFC_MBOXQ_t *mbox;
4939
4940 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4941 if (!mbox)
4942 return -ENOMEM;
4943
4944 /* Find out how many extents are available for this resource type */
4945 length = (sizeof(struct lpfc_mbx_get_rsrc_extent_info) -
4946 sizeof(struct lpfc_sli4_cfg_mhdr));
4947 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
4948 LPFC_MBOX_OPCODE_GET_RSRC_EXTENT_INFO,
4949 length, LPFC_SLI4_MBX_EMBED);
4950
4951 /* Send an extents count of 0 - the GET doesn't use it. */
4952 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, 0, type,
4953 LPFC_SLI4_MBX_EMBED);
4954 if (unlikely(rc)) {
4955 rc = -EIO;
4956 goto err_exit;
4957 }
4958
4959 if (!phba->sli4_hba.intr_enable)
4960 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
4961 else {
James Smarta183a152011-10-10 21:32:43 -04004962 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart6d368e52011-05-24 11:44:12 -04004963 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
4964 }
4965 if (unlikely(rc)) {
4966 rc = -EIO;
4967 goto err_exit;
4968 }
4969
4970 rsrc_info = &mbox->u.mqe.un.rsrc_extent_info;
4971 if (bf_get(lpfc_mbox_hdr_status,
4972 &rsrc_info->header.cfg_shdr.response)) {
4973 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
4974 "2930 Failed to get resource extents "
4975 "Status 0x%x Add'l Status 0x%x\n",
4976 bf_get(lpfc_mbox_hdr_status,
4977 &rsrc_info->header.cfg_shdr.response),
4978 bf_get(lpfc_mbox_hdr_add_status,
4979 &rsrc_info->header.cfg_shdr.response));
4980 rc = -EIO;
4981 goto err_exit;
4982 }
4983
4984 *extnt_count = bf_get(lpfc_mbx_get_rsrc_extent_info_cnt,
4985 &rsrc_info->u.rsp);
4986 *extnt_size = bf_get(lpfc_mbx_get_rsrc_extent_info_size,
4987 &rsrc_info->u.rsp);
4988 err_exit:
4989 mempool_free(mbox, phba->mbox_mem_pool);
4990 return rc;
4991}
4992
4993/**
4994 * lpfc_sli4_chk_avail_extnt_rsrc - Check for available SLI4 resource extents.
4995 * @phba: Pointer to HBA context object.
4996 * @type: The extent type to check.
4997 *
4998 * This function reads the current available extents from the port and checks
4999 * if the extent count or extent size has changed since the last access.
5000 * Callers use this routine post port reset to understand if there is a
5001 * extent reprovisioning requirement.
5002 *
5003 * Returns:
5004 * -Error: error indicates problem.
5005 * 1: Extent count or size has changed.
5006 * 0: No changes.
5007 **/
5008static int
5009lpfc_sli4_chk_avail_extnt_rsrc(struct lpfc_hba *phba, uint16_t type)
5010{
5011 uint16_t curr_ext_cnt, rsrc_ext_cnt;
5012 uint16_t size_diff, rsrc_ext_size;
5013 int rc = 0;
5014 struct lpfc_rsrc_blks *rsrc_entry;
5015 struct list_head *rsrc_blk_list = NULL;
5016
5017 size_diff = 0;
5018 curr_ext_cnt = 0;
5019 rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,
5020 &rsrc_ext_cnt,
5021 &rsrc_ext_size);
5022 if (unlikely(rc))
5023 return -EIO;
5024
5025 switch (type) {
5026 case LPFC_RSC_TYPE_FCOE_RPI:
5027 rsrc_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;
5028 break;
5029 case LPFC_RSC_TYPE_FCOE_VPI:
5030 rsrc_blk_list = &phba->lpfc_vpi_blk_list;
5031 break;
5032 case LPFC_RSC_TYPE_FCOE_XRI:
5033 rsrc_blk_list = &phba->sli4_hba.lpfc_xri_blk_list;
5034 break;
5035 case LPFC_RSC_TYPE_FCOE_VFI:
5036 rsrc_blk_list = &phba->sli4_hba.lpfc_vfi_blk_list;
5037 break;
5038 default:
5039 break;
5040 }
5041
5042 list_for_each_entry(rsrc_entry, rsrc_blk_list, list) {
5043 curr_ext_cnt++;
5044 if (rsrc_entry->rsrc_size != rsrc_ext_size)
5045 size_diff++;
5046 }
5047
5048 if (curr_ext_cnt != rsrc_ext_cnt || size_diff != 0)
5049 rc = 1;
5050
5051 return rc;
5052}
5053
5054/**
5055 * lpfc_sli4_cfg_post_extnts -
5056 * @phba: Pointer to HBA context object.
5057 * @extnt_cnt - number of available extents.
5058 * @type - the extent type (rpi, xri, vfi, vpi).
5059 * @emb - buffer to hold either MBX_EMBED or MBX_NEMBED operation.
5060 * @mbox - pointer to the caller's allocated mailbox structure.
5061 *
5062 * This function executes the extents allocation request. It also
5063 * takes care of the amount of memory needed to allocate or get the
5064 * allocated extents. It is the caller's responsibility to evaluate
5065 * the response.
5066 *
5067 * Returns:
5068 * -Error: Error value describes the condition found.
5069 * 0: if successful
5070 **/
5071static int
5072lpfc_sli4_cfg_post_extnts(struct lpfc_hba *phba, uint16_t *extnt_cnt,
5073 uint16_t type, bool *emb, LPFC_MBOXQ_t *mbox)
5074{
5075 int rc = 0;
5076 uint32_t req_len;
5077 uint32_t emb_len;
5078 uint32_t alloc_len, mbox_tmo;
5079
5080 /* Calculate the total requested length of the dma memory */
5081 req_len = *extnt_cnt * sizeof(uint16_t);
5082
5083 /*
5084 * Calculate the size of an embedded mailbox. The uint32_t
5085 * accounts for extents-specific word.
5086 */
5087 emb_len = sizeof(MAILBOX_t) - sizeof(struct mbox_header) -
5088 sizeof(uint32_t);
5089
5090 /*
5091 * Presume the allocation and response will fit into an embedded
5092 * mailbox. If not true, reconfigure to a non-embedded mailbox.
5093 */
5094 *emb = LPFC_SLI4_MBX_EMBED;
5095 if (req_len > emb_len) {
5096 req_len = *extnt_cnt * sizeof(uint16_t) +
5097 sizeof(union lpfc_sli4_cfg_shdr) +
5098 sizeof(uint32_t);
5099 *emb = LPFC_SLI4_MBX_NEMBED;
5100 }
5101
5102 alloc_len = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5103 LPFC_MBOX_OPCODE_ALLOC_RSRC_EXTENT,
5104 req_len, *emb);
5105 if (alloc_len < req_len) {
5106 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartb76f2dc2011-07-22 18:37:42 -04005107 "2982 Allocated DMA memory size (x%x) is "
James Smart6d368e52011-05-24 11:44:12 -04005108 "less than the requested DMA memory "
5109 "size (x%x)\n", alloc_len, req_len);
5110 return -ENOMEM;
5111 }
5112 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, *extnt_cnt, type, *emb);
5113 if (unlikely(rc))
5114 return -EIO;
5115
5116 if (!phba->sli4_hba.intr_enable)
5117 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5118 else {
James Smarta183a152011-10-10 21:32:43 -04005119 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart6d368e52011-05-24 11:44:12 -04005120 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5121 }
5122
5123 if (unlikely(rc))
5124 rc = -EIO;
5125 return rc;
5126}
5127
5128/**
5129 * lpfc_sli4_alloc_extent - Allocate an SLI4 resource extent.
5130 * @phba: Pointer to HBA context object.
5131 * @type: The resource extent type to allocate.
5132 *
5133 * This function allocates the number of elements for the specified
5134 * resource type.
5135 **/
5136static int
5137lpfc_sli4_alloc_extent(struct lpfc_hba *phba, uint16_t type)
5138{
5139 bool emb = false;
5140 uint16_t rsrc_id_cnt, rsrc_cnt, rsrc_size;
5141 uint16_t rsrc_id, rsrc_start, j, k;
5142 uint16_t *ids;
5143 int i, rc;
5144 unsigned long longs;
5145 unsigned long *bmask;
5146 struct lpfc_rsrc_blks *rsrc_blks;
5147 LPFC_MBOXQ_t *mbox;
5148 uint32_t length;
5149 struct lpfc_id_range *id_array = NULL;
5150 void *virtaddr = NULL;
5151 struct lpfc_mbx_nembed_rsrc_extent *n_rsrc;
5152 struct lpfc_mbx_alloc_rsrc_extents *rsrc_ext;
5153 struct list_head *ext_blk_list;
5154
5155 rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,
5156 &rsrc_cnt,
5157 &rsrc_size);
5158 if (unlikely(rc))
5159 return -EIO;
5160
5161 if ((rsrc_cnt == 0) || (rsrc_size == 0)) {
5162 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
5163 "3009 No available Resource Extents "
5164 "for resource type 0x%x: Count: 0x%x, "
5165 "Size 0x%x\n", type, rsrc_cnt,
5166 rsrc_size);
5167 return -ENOMEM;
5168 }
5169
5170 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_INIT,
5171 "2903 Available Resource Extents "
5172 "for resource type 0x%x: Count: 0x%x, "
5173 "Size 0x%x\n", type, rsrc_cnt,
5174 rsrc_size);
5175
5176 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5177 if (!mbox)
5178 return -ENOMEM;
5179
5180 rc = lpfc_sli4_cfg_post_extnts(phba, &rsrc_cnt, type, &emb, mbox);
5181 if (unlikely(rc)) {
5182 rc = -EIO;
5183 goto err_exit;
5184 }
5185
5186 /*
5187 * Figure out where the response is located. Then get local pointers
5188 * to the response data. The port does not guarantee to respond to
5189 * all extents counts request so update the local variable with the
5190 * allocated count from the port.
5191 */
5192 if (emb == LPFC_SLI4_MBX_EMBED) {
5193 rsrc_ext = &mbox->u.mqe.un.alloc_rsrc_extents;
5194 id_array = &rsrc_ext->u.rsp.id[0];
5195 rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, &rsrc_ext->u.rsp);
5196 } else {
5197 virtaddr = mbox->sge_array->addr[0];
5198 n_rsrc = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
5199 rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, n_rsrc);
5200 id_array = &n_rsrc->id;
5201 }
5202
5203 longs = ((rsrc_cnt * rsrc_size) + BITS_PER_LONG - 1) / BITS_PER_LONG;
5204 rsrc_id_cnt = rsrc_cnt * rsrc_size;
5205
5206 /*
5207 * Based on the resource size and count, correct the base and max
5208 * resource values.
5209 */
5210 length = sizeof(struct lpfc_rsrc_blks);
5211 switch (type) {
5212 case LPFC_RSC_TYPE_FCOE_RPI:
5213 phba->sli4_hba.rpi_bmask = kzalloc(longs *
5214 sizeof(unsigned long),
5215 GFP_KERNEL);
5216 if (unlikely(!phba->sli4_hba.rpi_bmask)) {
5217 rc = -ENOMEM;
5218 goto err_exit;
5219 }
5220 phba->sli4_hba.rpi_ids = kzalloc(rsrc_id_cnt *
5221 sizeof(uint16_t),
5222 GFP_KERNEL);
5223 if (unlikely(!phba->sli4_hba.rpi_ids)) {
5224 kfree(phba->sli4_hba.rpi_bmask);
5225 rc = -ENOMEM;
5226 goto err_exit;
5227 }
5228
5229 /*
5230 * The next_rpi was initialized with the maximum available
5231 * count but the port may allocate a smaller number. Catch
5232 * that case and update the next_rpi.
5233 */
5234 phba->sli4_hba.next_rpi = rsrc_id_cnt;
5235
5236 /* Initialize local ptrs for common extent processing later. */
5237 bmask = phba->sli4_hba.rpi_bmask;
5238 ids = phba->sli4_hba.rpi_ids;
5239 ext_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;
5240 break;
5241 case LPFC_RSC_TYPE_FCOE_VPI:
5242 phba->vpi_bmask = kzalloc(longs *
5243 sizeof(unsigned long),
5244 GFP_KERNEL);
5245 if (unlikely(!phba->vpi_bmask)) {
5246 rc = -ENOMEM;
5247 goto err_exit;
5248 }
5249 phba->vpi_ids = kzalloc(rsrc_id_cnt *
5250 sizeof(uint16_t),
5251 GFP_KERNEL);
5252 if (unlikely(!phba->vpi_ids)) {
5253 kfree(phba->vpi_bmask);
5254 rc = -ENOMEM;
5255 goto err_exit;
5256 }
5257
5258 /* Initialize local ptrs for common extent processing later. */
5259 bmask = phba->vpi_bmask;
5260 ids = phba->vpi_ids;
5261 ext_blk_list = &phba->lpfc_vpi_blk_list;
5262 break;
5263 case LPFC_RSC_TYPE_FCOE_XRI:
5264 phba->sli4_hba.xri_bmask = kzalloc(longs *
5265 sizeof(unsigned long),
5266 GFP_KERNEL);
5267 if (unlikely(!phba->sli4_hba.xri_bmask)) {
5268 rc = -ENOMEM;
5269 goto err_exit;
5270 }
5271 phba->sli4_hba.xri_ids = kzalloc(rsrc_id_cnt *
5272 sizeof(uint16_t),
5273 GFP_KERNEL);
5274 if (unlikely(!phba->sli4_hba.xri_ids)) {
5275 kfree(phba->sli4_hba.xri_bmask);
5276 rc = -ENOMEM;
5277 goto err_exit;
5278 }
5279
5280 /* Initialize local ptrs for common extent processing later. */
5281 bmask = phba->sli4_hba.xri_bmask;
5282 ids = phba->sli4_hba.xri_ids;
5283 ext_blk_list = &phba->sli4_hba.lpfc_xri_blk_list;
5284 break;
5285 case LPFC_RSC_TYPE_FCOE_VFI:
5286 phba->sli4_hba.vfi_bmask = kzalloc(longs *
5287 sizeof(unsigned long),
5288 GFP_KERNEL);
5289 if (unlikely(!phba->sli4_hba.vfi_bmask)) {
5290 rc = -ENOMEM;
5291 goto err_exit;
5292 }
5293 phba->sli4_hba.vfi_ids = kzalloc(rsrc_id_cnt *
5294 sizeof(uint16_t),
5295 GFP_KERNEL);
5296 if (unlikely(!phba->sli4_hba.vfi_ids)) {
5297 kfree(phba->sli4_hba.vfi_bmask);
5298 rc = -ENOMEM;
5299 goto err_exit;
5300 }
5301
5302 /* Initialize local ptrs for common extent processing later. */
5303 bmask = phba->sli4_hba.vfi_bmask;
5304 ids = phba->sli4_hba.vfi_ids;
5305 ext_blk_list = &phba->sli4_hba.lpfc_vfi_blk_list;
5306 break;
5307 default:
5308 /* Unsupported Opcode. Fail call. */
5309 id_array = NULL;
5310 bmask = NULL;
5311 ids = NULL;
5312 ext_blk_list = NULL;
5313 goto err_exit;
5314 }
5315
5316 /*
5317 * Complete initializing the extent configuration with the
5318 * allocated ids assigned to this function. The bitmask serves
5319 * as an index into the array and manages the available ids. The
5320 * array just stores the ids communicated to the port via the wqes.
5321 */
5322 for (i = 0, j = 0, k = 0; i < rsrc_cnt; i++) {
5323 if ((i % 2) == 0)
5324 rsrc_id = bf_get(lpfc_mbx_rsrc_id_word4_0,
5325 &id_array[k]);
5326 else
5327 rsrc_id = bf_get(lpfc_mbx_rsrc_id_word4_1,
5328 &id_array[k]);
5329
5330 rsrc_blks = kzalloc(length, GFP_KERNEL);
5331 if (unlikely(!rsrc_blks)) {
5332 rc = -ENOMEM;
5333 kfree(bmask);
5334 kfree(ids);
5335 goto err_exit;
5336 }
5337 rsrc_blks->rsrc_start = rsrc_id;
5338 rsrc_blks->rsrc_size = rsrc_size;
5339 list_add_tail(&rsrc_blks->list, ext_blk_list);
5340 rsrc_start = rsrc_id;
5341 if ((type == LPFC_RSC_TYPE_FCOE_XRI) && (j == 0))
5342 phba->sli4_hba.scsi_xri_start = rsrc_start +
5343 lpfc_sli4_get_els_iocb_cnt(phba);
5344
5345 while (rsrc_id < (rsrc_start + rsrc_size)) {
5346 ids[j] = rsrc_id;
5347 rsrc_id++;
5348 j++;
5349 }
5350 /* Entire word processed. Get next word.*/
5351 if ((i % 2) == 1)
5352 k++;
5353 }
5354 err_exit:
5355 lpfc_sli4_mbox_cmd_free(phba, mbox);
5356 return rc;
5357}
5358
5359/**
5360 * lpfc_sli4_dealloc_extent - Deallocate an SLI4 resource extent.
5361 * @phba: Pointer to HBA context object.
5362 * @type: the extent's type.
5363 *
5364 * This function deallocates all extents of a particular resource type.
5365 * SLI4 does not allow for deallocating a particular extent range. It
5366 * is the caller's responsibility to release all kernel memory resources.
5367 **/
5368static int
5369lpfc_sli4_dealloc_extent(struct lpfc_hba *phba, uint16_t type)
5370{
5371 int rc;
5372 uint32_t length, mbox_tmo = 0;
5373 LPFC_MBOXQ_t *mbox;
5374 struct lpfc_mbx_dealloc_rsrc_extents *dealloc_rsrc;
5375 struct lpfc_rsrc_blks *rsrc_blk, *rsrc_blk_next;
5376
5377 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5378 if (!mbox)
5379 return -ENOMEM;
5380
5381 /*
5382 * This function sends an embedded mailbox because it only sends the
5383 * the resource type. All extents of this type are released by the
5384 * port.
5385 */
5386 length = (sizeof(struct lpfc_mbx_dealloc_rsrc_extents) -
5387 sizeof(struct lpfc_sli4_cfg_mhdr));
5388 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5389 LPFC_MBOX_OPCODE_DEALLOC_RSRC_EXTENT,
5390 length, LPFC_SLI4_MBX_EMBED);
5391
5392 /* Send an extents count of 0 - the dealloc doesn't use it. */
5393 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, 0, type,
5394 LPFC_SLI4_MBX_EMBED);
5395 if (unlikely(rc)) {
5396 rc = -EIO;
5397 goto out_free_mbox;
5398 }
5399 if (!phba->sli4_hba.intr_enable)
5400 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5401 else {
James Smarta183a152011-10-10 21:32:43 -04005402 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart6d368e52011-05-24 11:44:12 -04005403 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5404 }
5405 if (unlikely(rc)) {
5406 rc = -EIO;
5407 goto out_free_mbox;
5408 }
5409
5410 dealloc_rsrc = &mbox->u.mqe.un.dealloc_rsrc_extents;
5411 if (bf_get(lpfc_mbox_hdr_status,
5412 &dealloc_rsrc->header.cfg_shdr.response)) {
5413 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
5414 "2919 Failed to release resource extents "
5415 "for type %d - Status 0x%x Add'l Status 0x%x. "
5416 "Resource memory not released.\n",
5417 type,
5418 bf_get(lpfc_mbox_hdr_status,
5419 &dealloc_rsrc->header.cfg_shdr.response),
5420 bf_get(lpfc_mbox_hdr_add_status,
5421 &dealloc_rsrc->header.cfg_shdr.response));
5422 rc = -EIO;
5423 goto out_free_mbox;
5424 }
5425
5426 /* Release kernel memory resources for the specific type. */
5427 switch (type) {
5428 case LPFC_RSC_TYPE_FCOE_VPI:
5429 kfree(phba->vpi_bmask);
5430 kfree(phba->vpi_ids);
5431 bf_set(lpfc_vpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5432 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5433 &phba->lpfc_vpi_blk_list, list) {
5434 list_del_init(&rsrc_blk->list);
5435 kfree(rsrc_blk);
5436 }
5437 break;
5438 case LPFC_RSC_TYPE_FCOE_XRI:
5439 kfree(phba->sli4_hba.xri_bmask);
5440 kfree(phba->sli4_hba.xri_ids);
5441 bf_set(lpfc_xri_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5442 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5443 &phba->sli4_hba.lpfc_xri_blk_list, list) {
5444 list_del_init(&rsrc_blk->list);
5445 kfree(rsrc_blk);
5446 }
5447 break;
5448 case LPFC_RSC_TYPE_FCOE_VFI:
5449 kfree(phba->sli4_hba.vfi_bmask);
5450 kfree(phba->sli4_hba.vfi_ids);
5451 bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5452 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5453 &phba->sli4_hba.lpfc_vfi_blk_list, list) {
5454 list_del_init(&rsrc_blk->list);
5455 kfree(rsrc_blk);
5456 }
5457 break;
5458 case LPFC_RSC_TYPE_FCOE_RPI:
5459 /* RPI bitmask and physical id array are cleaned up earlier. */
5460 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5461 &phba->sli4_hba.lpfc_rpi_blk_list, list) {
5462 list_del_init(&rsrc_blk->list);
5463 kfree(rsrc_blk);
5464 }
5465 break;
5466 default:
5467 break;
5468 }
5469
5470 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5471
5472 out_free_mbox:
5473 mempool_free(mbox, phba->mbox_mem_pool);
5474 return rc;
5475}
5476
5477/**
5478 * lpfc_sli4_alloc_resource_identifiers - Allocate all SLI4 resource extents.
5479 * @phba: Pointer to HBA context object.
5480 *
5481 * This function allocates all SLI4 resource identifiers.
5482 **/
5483int
5484lpfc_sli4_alloc_resource_identifiers(struct lpfc_hba *phba)
5485{
5486 int i, rc, error = 0;
5487 uint16_t count, base;
5488 unsigned long longs;
5489
James Smartff78d8f2011-12-13 13:21:35 -05005490 if (!phba->sli4_hba.rpi_hdrs_in_use)
5491 phba->sli4_hba.next_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
James Smart6d368e52011-05-24 11:44:12 -04005492 if (phba->sli4_hba.extents_in_use) {
5493 /*
5494 * The port supports resource extents. The XRI, VPI, VFI, RPI
5495 * resource extent count must be read and allocated before
5496 * provisioning the resource id arrays.
5497 */
5498 if (bf_get(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags) ==
5499 LPFC_IDX_RSRC_RDY) {
5500 /*
5501 * Extent-based resources are set - the driver could
5502 * be in a port reset. Figure out if any corrective
5503 * actions need to be taken.
5504 */
5505 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
5506 LPFC_RSC_TYPE_FCOE_VFI);
5507 if (rc != 0)
5508 error++;
5509 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
5510 LPFC_RSC_TYPE_FCOE_VPI);
5511 if (rc != 0)
5512 error++;
5513 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
5514 LPFC_RSC_TYPE_FCOE_XRI);
5515 if (rc != 0)
5516 error++;
5517 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
5518 LPFC_RSC_TYPE_FCOE_RPI);
5519 if (rc != 0)
5520 error++;
5521
5522 /*
5523 * It's possible that the number of resources
5524 * provided to this port instance changed between
5525 * resets. Detect this condition and reallocate
5526 * resources. Otherwise, there is no action.
5527 */
5528 if (error) {
5529 lpfc_printf_log(phba, KERN_INFO,
5530 LOG_MBOX | LOG_INIT,
5531 "2931 Detected extent resource "
5532 "change. Reallocating all "
5533 "extents.\n");
5534 rc = lpfc_sli4_dealloc_extent(phba,
5535 LPFC_RSC_TYPE_FCOE_VFI);
5536 rc = lpfc_sli4_dealloc_extent(phba,
5537 LPFC_RSC_TYPE_FCOE_VPI);
5538 rc = lpfc_sli4_dealloc_extent(phba,
5539 LPFC_RSC_TYPE_FCOE_XRI);
5540 rc = lpfc_sli4_dealloc_extent(phba,
5541 LPFC_RSC_TYPE_FCOE_RPI);
5542 } else
5543 return 0;
5544 }
5545
5546 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_VFI);
5547 if (unlikely(rc))
5548 goto err_exit;
5549
5550 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_VPI);
5551 if (unlikely(rc))
5552 goto err_exit;
5553
5554 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_RPI);
5555 if (unlikely(rc))
5556 goto err_exit;
5557
5558 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_XRI);
5559 if (unlikely(rc))
5560 goto err_exit;
5561 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags,
5562 LPFC_IDX_RSRC_RDY);
5563 return rc;
5564 } else {
5565 /*
5566 * The port does not support resource extents. The XRI, VPI,
5567 * VFI, RPI resource ids were determined from READ_CONFIG.
5568 * Just allocate the bitmasks and provision the resource id
5569 * arrays. If a port reset is active, the resources don't
5570 * need any action - just exit.
5571 */
5572 if (bf_get(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags) ==
James Smartff78d8f2011-12-13 13:21:35 -05005573 LPFC_IDX_RSRC_RDY) {
5574 lpfc_sli4_dealloc_resource_identifiers(phba);
5575 lpfc_sli4_remove_rpis(phba);
5576 }
James Smart6d368e52011-05-24 11:44:12 -04005577 /* RPIs. */
5578 count = phba->sli4_hba.max_cfg_param.max_rpi;
5579 base = phba->sli4_hba.max_cfg_param.rpi_base;
5580 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
5581 phba->sli4_hba.rpi_bmask = kzalloc(longs *
5582 sizeof(unsigned long),
5583 GFP_KERNEL);
5584 if (unlikely(!phba->sli4_hba.rpi_bmask)) {
5585 rc = -ENOMEM;
5586 goto err_exit;
5587 }
5588 phba->sli4_hba.rpi_ids = kzalloc(count *
5589 sizeof(uint16_t),
5590 GFP_KERNEL);
5591 if (unlikely(!phba->sli4_hba.rpi_ids)) {
5592 rc = -ENOMEM;
5593 goto free_rpi_bmask;
5594 }
5595
5596 for (i = 0; i < count; i++)
5597 phba->sli4_hba.rpi_ids[i] = base + i;
5598
5599 /* VPIs. */
5600 count = phba->sli4_hba.max_cfg_param.max_vpi;
5601 base = phba->sli4_hba.max_cfg_param.vpi_base;
5602 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
5603 phba->vpi_bmask = kzalloc(longs *
5604 sizeof(unsigned long),
5605 GFP_KERNEL);
5606 if (unlikely(!phba->vpi_bmask)) {
5607 rc = -ENOMEM;
5608 goto free_rpi_ids;
5609 }
5610 phba->vpi_ids = kzalloc(count *
5611 sizeof(uint16_t),
5612 GFP_KERNEL);
5613 if (unlikely(!phba->vpi_ids)) {
5614 rc = -ENOMEM;
5615 goto free_vpi_bmask;
5616 }
5617
5618 for (i = 0; i < count; i++)
5619 phba->vpi_ids[i] = base + i;
5620
5621 /* XRIs. */
5622 count = phba->sli4_hba.max_cfg_param.max_xri;
5623 base = phba->sli4_hba.max_cfg_param.xri_base;
5624 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
5625 phba->sli4_hba.xri_bmask = kzalloc(longs *
5626 sizeof(unsigned long),
5627 GFP_KERNEL);
5628 if (unlikely(!phba->sli4_hba.xri_bmask)) {
5629 rc = -ENOMEM;
5630 goto free_vpi_ids;
5631 }
5632 phba->sli4_hba.xri_ids = kzalloc(count *
5633 sizeof(uint16_t),
5634 GFP_KERNEL);
5635 if (unlikely(!phba->sli4_hba.xri_ids)) {
5636 rc = -ENOMEM;
5637 goto free_xri_bmask;
5638 }
5639
5640 for (i = 0; i < count; i++)
5641 phba->sli4_hba.xri_ids[i] = base + i;
5642
5643 /* VFIs. */
5644 count = phba->sli4_hba.max_cfg_param.max_vfi;
5645 base = phba->sli4_hba.max_cfg_param.vfi_base;
5646 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
5647 phba->sli4_hba.vfi_bmask = kzalloc(longs *
5648 sizeof(unsigned long),
5649 GFP_KERNEL);
5650 if (unlikely(!phba->sli4_hba.vfi_bmask)) {
5651 rc = -ENOMEM;
5652 goto free_xri_ids;
5653 }
5654 phba->sli4_hba.vfi_ids = kzalloc(count *
5655 sizeof(uint16_t),
5656 GFP_KERNEL);
5657 if (unlikely(!phba->sli4_hba.vfi_ids)) {
5658 rc = -ENOMEM;
5659 goto free_vfi_bmask;
5660 }
5661
5662 for (i = 0; i < count; i++)
5663 phba->sli4_hba.vfi_ids[i] = base + i;
5664
5665 /*
5666 * Mark all resources ready. An HBA reset doesn't need
5667 * to reset the initialization.
5668 */
5669 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags,
5670 LPFC_IDX_RSRC_RDY);
5671 return 0;
5672 }
5673
5674 free_vfi_bmask:
5675 kfree(phba->sli4_hba.vfi_bmask);
5676 free_xri_ids:
5677 kfree(phba->sli4_hba.xri_ids);
5678 free_xri_bmask:
5679 kfree(phba->sli4_hba.xri_bmask);
5680 free_vpi_ids:
5681 kfree(phba->vpi_ids);
5682 free_vpi_bmask:
5683 kfree(phba->vpi_bmask);
5684 free_rpi_ids:
5685 kfree(phba->sli4_hba.rpi_ids);
5686 free_rpi_bmask:
5687 kfree(phba->sli4_hba.rpi_bmask);
5688 err_exit:
5689 return rc;
5690}
5691
5692/**
5693 * lpfc_sli4_dealloc_resource_identifiers - Deallocate all SLI4 resource extents.
5694 * @phba: Pointer to HBA context object.
5695 *
5696 * This function allocates the number of elements for the specified
5697 * resource type.
5698 **/
5699int
5700lpfc_sli4_dealloc_resource_identifiers(struct lpfc_hba *phba)
5701{
5702 if (phba->sli4_hba.extents_in_use) {
5703 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VPI);
5704 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_RPI);
5705 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_XRI);
5706 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VFI);
5707 } else {
5708 kfree(phba->vpi_bmask);
5709 kfree(phba->vpi_ids);
5710 bf_set(lpfc_vpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5711 kfree(phba->sli4_hba.xri_bmask);
5712 kfree(phba->sli4_hba.xri_ids);
5713 bf_set(lpfc_xri_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5714 kfree(phba->sli4_hba.vfi_bmask);
5715 kfree(phba->sli4_hba.vfi_ids);
5716 bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5717 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5718 }
5719
5720 return 0;
5721}
5722
5723/**
James Smartb76f2dc2011-07-22 18:37:42 -04005724 * lpfc_sli4_get_allocated_extnts - Get the port's allocated extents.
5725 * @phba: Pointer to HBA context object.
5726 * @type: The resource extent type.
5727 * @extnt_count: buffer to hold port extent count response
5728 * @extnt_size: buffer to hold port extent size response.
5729 *
5730 * This function calls the port to read the host allocated extents
5731 * for a particular type.
5732 **/
5733int
5734lpfc_sli4_get_allocated_extnts(struct lpfc_hba *phba, uint16_t type,
5735 uint16_t *extnt_cnt, uint16_t *extnt_size)
5736{
5737 bool emb;
5738 int rc = 0;
5739 uint16_t curr_blks = 0;
5740 uint32_t req_len, emb_len;
5741 uint32_t alloc_len, mbox_tmo;
5742 struct list_head *blk_list_head;
5743 struct lpfc_rsrc_blks *rsrc_blk;
5744 LPFC_MBOXQ_t *mbox;
5745 void *virtaddr = NULL;
5746 struct lpfc_mbx_nembed_rsrc_extent *n_rsrc;
5747 struct lpfc_mbx_alloc_rsrc_extents *rsrc_ext;
5748 union lpfc_sli4_cfg_shdr *shdr;
5749
5750 switch (type) {
5751 case LPFC_RSC_TYPE_FCOE_VPI:
5752 blk_list_head = &phba->lpfc_vpi_blk_list;
5753 break;
5754 case LPFC_RSC_TYPE_FCOE_XRI:
5755 blk_list_head = &phba->sli4_hba.lpfc_xri_blk_list;
5756 break;
5757 case LPFC_RSC_TYPE_FCOE_VFI:
5758 blk_list_head = &phba->sli4_hba.lpfc_vfi_blk_list;
5759 break;
5760 case LPFC_RSC_TYPE_FCOE_RPI:
5761 blk_list_head = &phba->sli4_hba.lpfc_rpi_blk_list;
5762 break;
5763 default:
5764 return -EIO;
5765 }
5766
5767 /* Count the number of extents currently allocatd for this type. */
5768 list_for_each_entry(rsrc_blk, blk_list_head, list) {
5769 if (curr_blks == 0) {
5770 /*
5771 * The GET_ALLOCATED mailbox does not return the size,
5772 * just the count. The size should be just the size
5773 * stored in the current allocated block and all sizes
5774 * for an extent type are the same so set the return
5775 * value now.
5776 */
5777 *extnt_size = rsrc_blk->rsrc_size;
5778 }
5779 curr_blks++;
5780 }
5781
5782 /* Calculate the total requested length of the dma memory. */
5783 req_len = curr_blks * sizeof(uint16_t);
5784
5785 /*
5786 * Calculate the size of an embedded mailbox. The uint32_t
5787 * accounts for extents-specific word.
5788 */
5789 emb_len = sizeof(MAILBOX_t) - sizeof(struct mbox_header) -
5790 sizeof(uint32_t);
5791
5792 /*
5793 * Presume the allocation and response will fit into an embedded
5794 * mailbox. If not true, reconfigure to a non-embedded mailbox.
5795 */
5796 emb = LPFC_SLI4_MBX_EMBED;
5797 req_len = emb_len;
5798 if (req_len > emb_len) {
5799 req_len = curr_blks * sizeof(uint16_t) +
5800 sizeof(union lpfc_sli4_cfg_shdr) +
5801 sizeof(uint32_t);
5802 emb = LPFC_SLI4_MBX_NEMBED;
5803 }
5804
5805 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5806 if (!mbox)
5807 return -ENOMEM;
5808 memset(mbox, 0, sizeof(LPFC_MBOXQ_t));
5809
5810 alloc_len = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5811 LPFC_MBOX_OPCODE_GET_ALLOC_RSRC_EXTENT,
5812 req_len, emb);
5813 if (alloc_len < req_len) {
5814 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5815 "2983 Allocated DMA memory size (x%x) is "
5816 "less than the requested DMA memory "
5817 "size (x%x)\n", alloc_len, req_len);
5818 rc = -ENOMEM;
5819 goto err_exit;
5820 }
5821 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, curr_blks, type, emb);
5822 if (unlikely(rc)) {
5823 rc = -EIO;
5824 goto err_exit;
5825 }
5826
5827 if (!phba->sli4_hba.intr_enable)
5828 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5829 else {
James Smarta183a152011-10-10 21:32:43 -04005830 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smartb76f2dc2011-07-22 18:37:42 -04005831 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5832 }
5833
5834 if (unlikely(rc)) {
5835 rc = -EIO;
5836 goto err_exit;
5837 }
5838
5839 /*
5840 * Figure out where the response is located. Then get local pointers
5841 * to the response data. The port does not guarantee to respond to
5842 * all extents counts request so update the local variable with the
5843 * allocated count from the port.
5844 */
5845 if (emb == LPFC_SLI4_MBX_EMBED) {
5846 rsrc_ext = &mbox->u.mqe.un.alloc_rsrc_extents;
5847 shdr = &rsrc_ext->header.cfg_shdr;
5848 *extnt_cnt = bf_get(lpfc_mbx_rsrc_cnt, &rsrc_ext->u.rsp);
5849 } else {
5850 virtaddr = mbox->sge_array->addr[0];
5851 n_rsrc = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
5852 shdr = &n_rsrc->cfg_shdr;
5853 *extnt_cnt = bf_get(lpfc_mbx_rsrc_cnt, n_rsrc);
5854 }
5855
5856 if (bf_get(lpfc_mbox_hdr_status, &shdr->response)) {
5857 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
5858 "2984 Failed to read allocated resources "
5859 "for type %d - Status 0x%x Add'l Status 0x%x.\n",
5860 type,
5861 bf_get(lpfc_mbox_hdr_status, &shdr->response),
5862 bf_get(lpfc_mbox_hdr_add_status, &shdr->response));
5863 rc = -EIO;
5864 goto err_exit;
5865 }
5866 err_exit:
5867 lpfc_sli4_mbox_cmd_free(phba, mbox);
5868 return rc;
5869}
5870
5871/**
James Smartda0436e2009-05-22 14:51:39 -04005872 * lpfc_sli4_hba_setup - SLI4 device intialization PCI function
5873 * @phba: Pointer to HBA context object.
5874 *
5875 * This function is the main SLI4 device intialization PCI function. This
5876 * function is called by the HBA intialization code, HBA reset code and
5877 * HBA error attention handler code. Caller is not required to hold any
5878 * locks.
5879 **/
5880int
5881lpfc_sli4_hba_setup(struct lpfc_hba *phba)
5882{
5883 int rc;
5884 LPFC_MBOXQ_t *mboxq;
5885 struct lpfc_mqe *mqe;
5886 uint8_t *vpd;
5887 uint32_t vpd_size;
5888 uint32_t ftr_rsp = 0;
5889 struct Scsi_Host *shost = lpfc_shost_from_vport(phba->pport);
5890 struct lpfc_vport *vport = phba->pport;
5891 struct lpfc_dmabuf *mp;
5892
5893 /* Perform a PCI function reset to start from clean */
5894 rc = lpfc_pci_function_reset(phba);
5895 if (unlikely(rc))
5896 return -ENODEV;
5897
5898 /* Check the HBA Host Status Register for readyness */
5899 rc = lpfc_sli4_post_status_check(phba);
5900 if (unlikely(rc))
5901 return -ENODEV;
5902 else {
5903 spin_lock_irq(&phba->hbalock);
5904 phba->sli.sli_flag |= LPFC_SLI_ACTIVE;
5905 spin_unlock_irq(&phba->hbalock);
5906 }
5907
5908 /*
5909 * Allocate a single mailbox container for initializing the
5910 * port.
5911 */
5912 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5913 if (!mboxq)
5914 return -ENOMEM;
5915
James Smartda0436e2009-05-22 14:51:39 -04005916 /* Issue READ_REV to collect vpd and FW information. */
James Smart49198b32010-04-06 15:04:33 -04005917 vpd_size = SLI4_PAGE_SIZE;
James Smartda0436e2009-05-22 14:51:39 -04005918 vpd = kzalloc(vpd_size, GFP_KERNEL);
5919 if (!vpd) {
5920 rc = -ENOMEM;
5921 goto out_free_mbox;
5922 }
5923
5924 rc = lpfc_sli4_read_rev(phba, mboxq, vpd, &vpd_size);
James Smart76a95d72010-11-20 23:11:48 -05005925 if (unlikely(rc)) {
5926 kfree(vpd);
5927 goto out_free_mbox;
5928 }
James Smartda0436e2009-05-22 14:51:39 -04005929 mqe = &mboxq->u.mqe;
James Smartf1126682009-06-10 17:22:44 -04005930 phba->sli_rev = bf_get(lpfc_mbx_rd_rev_sli_lvl, &mqe->un.read_rev);
5931 if (bf_get(lpfc_mbx_rd_rev_fcoe, &mqe->un.read_rev))
James Smart76a95d72010-11-20 23:11:48 -05005932 phba->hba_flag |= HBA_FCOE_MODE;
5933 else
5934 phba->hba_flag &= ~HBA_FCOE_MODE;
James Smart45ed1192009-10-02 15:17:02 -04005935
5936 if (bf_get(lpfc_mbx_rd_rev_cee_ver, &mqe->un.read_rev) ==
5937 LPFC_DCBX_CEE_MODE)
5938 phba->hba_flag |= HBA_FIP_SUPPORT;
5939 else
5940 phba->hba_flag &= ~HBA_FIP_SUPPORT;
5941
James Smartc31098c2011-04-16 11:03:33 -04005942 if (phba->sli_rev != LPFC_SLI_REV4) {
James Smartda0436e2009-05-22 14:51:39 -04005943 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5944 "0376 READ_REV Error. SLI Level %d "
5945 "FCoE enabled %d\n",
James Smart76a95d72010-11-20 23:11:48 -05005946 phba->sli_rev, phba->hba_flag & HBA_FCOE_MODE);
James Smartda0436e2009-05-22 14:51:39 -04005947 rc = -EIO;
James Smart76a95d72010-11-20 23:11:48 -05005948 kfree(vpd);
5949 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04005950 }
James Smartcd1c8302011-10-10 21:33:25 -04005951
5952 /*
James Smartff78d8f2011-12-13 13:21:35 -05005953 * Continue initialization with default values even if driver failed
5954 * to read FCoE param config regions, only read parameters if the
5955 * board is FCoE
5956 */
5957 if (phba->hba_flag & HBA_FCOE_MODE &&
5958 lpfc_sli4_read_fcoe_params(phba))
5959 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_INIT,
5960 "2570 Failed to read FCoE parameters\n");
5961
5962 /*
James Smartcd1c8302011-10-10 21:33:25 -04005963 * Retrieve sli4 device physical port name, failure of doing it
5964 * is considered as non-fatal.
5965 */
5966 rc = lpfc_sli4_retrieve_pport_name(phba);
5967 if (!rc)
5968 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
5969 "3080 Successful retrieving SLI4 device "
5970 "physical port name: %s.\n", phba->Port);
5971
James Smartda0436e2009-05-22 14:51:39 -04005972 /*
5973 * Evaluate the read rev and vpd data. Populate the driver
5974 * state with the results. If this routine fails, the failure
5975 * is not fatal as the driver will use generic values.
5976 */
5977 rc = lpfc_parse_vpd(phba, vpd, vpd_size);
5978 if (unlikely(!rc)) {
5979 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5980 "0377 Error %d parsing vpd. "
5981 "Using defaults.\n", rc);
5982 rc = 0;
5983 }
James Smart76a95d72010-11-20 23:11:48 -05005984 kfree(vpd);
James Smartda0436e2009-05-22 14:51:39 -04005985
James Smartf1126682009-06-10 17:22:44 -04005986 /* Save information as VPD data */
5987 phba->vpd.rev.biuRev = mqe->un.read_rev.first_hw_rev;
5988 phba->vpd.rev.smRev = mqe->un.read_rev.second_hw_rev;
5989 phba->vpd.rev.endecRev = mqe->un.read_rev.third_hw_rev;
5990 phba->vpd.rev.fcphHigh = bf_get(lpfc_mbx_rd_rev_fcph_high,
5991 &mqe->un.read_rev);
5992 phba->vpd.rev.fcphLow = bf_get(lpfc_mbx_rd_rev_fcph_low,
5993 &mqe->un.read_rev);
5994 phba->vpd.rev.feaLevelHigh = bf_get(lpfc_mbx_rd_rev_ftr_lvl_high,
5995 &mqe->un.read_rev);
5996 phba->vpd.rev.feaLevelLow = bf_get(lpfc_mbx_rd_rev_ftr_lvl_low,
5997 &mqe->un.read_rev);
5998 phba->vpd.rev.sli1FwRev = mqe->un.read_rev.fw_id_rev;
5999 memcpy(phba->vpd.rev.sli1FwName, mqe->un.read_rev.fw_name, 16);
6000 phba->vpd.rev.sli2FwRev = mqe->un.read_rev.ulp_fw_id_rev;
6001 memcpy(phba->vpd.rev.sli2FwName, mqe->un.read_rev.ulp_fw_name, 16);
6002 phba->vpd.rev.opFwRev = mqe->un.read_rev.fw_id_rev;
6003 memcpy(phba->vpd.rev.opFwName, mqe->un.read_rev.fw_name, 16);
6004 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
6005 "(%d):0380 READ_REV Status x%x "
6006 "fw_rev:%s fcphHi:%x fcphLo:%x flHi:%x flLo:%x\n",
6007 mboxq->vport ? mboxq->vport->vpi : 0,
6008 bf_get(lpfc_mqe_status, mqe),
6009 phba->vpd.rev.opFwName,
6010 phba->vpd.rev.fcphHigh, phba->vpd.rev.fcphLow,
6011 phba->vpd.rev.feaLevelHigh, phba->vpd.rev.feaLevelLow);
James Smartda0436e2009-05-22 14:51:39 -04006012
6013 /*
6014 * Discover the port's supported feature set and match it against the
6015 * hosts requests.
6016 */
6017 lpfc_request_features(phba, mboxq);
6018 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6019 if (unlikely(rc)) {
6020 rc = -EIO;
James Smart76a95d72010-11-20 23:11:48 -05006021 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04006022 }
6023
6024 /*
6025 * The port must support FCP initiator mode as this is the
6026 * only mode running in the host.
6027 */
6028 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_fcpi, &mqe->un.req_ftrs))) {
6029 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
6030 "0378 No support for fcpi mode.\n");
6031 ftr_rsp++;
6032 }
James Smartfedd3b72011-02-16 12:39:24 -05006033 if (bf_get(lpfc_mbx_rq_ftr_rsp_perfh, &mqe->un.req_ftrs))
6034 phba->sli3_options |= LPFC_SLI4_PERFH_ENABLED;
6035 else
6036 phba->sli3_options &= ~LPFC_SLI4_PERFH_ENABLED;
James Smartda0436e2009-05-22 14:51:39 -04006037 /*
6038 * If the port cannot support the host's requested features
6039 * then turn off the global config parameters to disable the
6040 * feature in the driver. This is not a fatal error.
6041 */
James Smartbf086112011-08-21 21:48:13 -04006042 phba->sli3_options &= ~LPFC_SLI3_BG_ENABLED;
6043 if (phba->cfg_enable_bg) {
6044 if (bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs))
6045 phba->sli3_options |= LPFC_SLI3_BG_ENABLED;
6046 else
6047 ftr_rsp++;
6048 }
James Smartda0436e2009-05-22 14:51:39 -04006049
6050 if (phba->max_vpi && phba->cfg_enable_npiv &&
6051 !(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
6052 ftr_rsp++;
6053
6054 if (ftr_rsp) {
6055 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
6056 "0379 Feature Mismatch Data: x%08x %08x "
6057 "x%x x%x x%x\n", mqe->un.req_ftrs.word2,
6058 mqe->un.req_ftrs.word3, phba->cfg_enable_bg,
6059 phba->cfg_enable_npiv, phba->max_vpi);
6060 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs)))
6061 phba->cfg_enable_bg = 0;
6062 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
6063 phba->cfg_enable_npiv = 0;
6064 }
6065
6066 /* These SLI3 features are assumed in SLI4 */
6067 spin_lock_irq(&phba->hbalock);
6068 phba->sli3_options |= (LPFC_SLI3_NPIV_ENABLED | LPFC_SLI3_HBQ_ENABLED);
6069 spin_unlock_irq(&phba->hbalock);
6070
James Smart6d368e52011-05-24 11:44:12 -04006071 /*
6072 * Allocate all resources (xri,rpi,vpi,vfi) now. Subsequent
6073 * calls depends on these resources to complete port setup.
6074 */
6075 rc = lpfc_sli4_alloc_resource_identifiers(phba);
6076 if (rc) {
6077 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6078 "2920 Failed to alloc Resource IDs "
6079 "rc = x%x\n", rc);
6080 goto out_free_mbox;
6081 }
James Smartff78d8f2011-12-13 13:21:35 -05006082 /* update physical xri mappings in the scsi buffers */
6083 lpfc_scsi_buf_update(phba);
James Smart6d368e52011-05-24 11:44:12 -04006084
James Smartda0436e2009-05-22 14:51:39 -04006085 /* Read the port's service parameters. */
James Smart9f1177a2010-02-26 14:12:57 -05006086 rc = lpfc_read_sparam(phba, mboxq, vport->vpi);
6087 if (rc) {
6088 phba->link_state = LPFC_HBA_ERROR;
6089 rc = -ENOMEM;
James Smart76a95d72010-11-20 23:11:48 -05006090 goto out_free_mbox;
James Smart9f1177a2010-02-26 14:12:57 -05006091 }
6092
James Smartda0436e2009-05-22 14:51:39 -04006093 mboxq->vport = vport;
6094 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6095 mp = (struct lpfc_dmabuf *) mboxq->context1;
6096 if (rc == MBX_SUCCESS) {
6097 memcpy(&vport->fc_sparam, mp->virt, sizeof(struct serv_parm));
6098 rc = 0;
6099 }
6100
6101 /*
6102 * This memory was allocated by the lpfc_read_sparam routine. Release
6103 * it to the mbuf pool.
6104 */
6105 lpfc_mbuf_free(phba, mp->virt, mp->phys);
6106 kfree(mp);
6107 mboxq->context1 = NULL;
6108 if (unlikely(rc)) {
6109 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6110 "0382 READ_SPARAM command failed "
6111 "status %d, mbxStatus x%x\n",
6112 rc, bf_get(lpfc_mqe_status, mqe));
6113 phba->link_state = LPFC_HBA_ERROR;
6114 rc = -EIO;
James Smart76a95d72010-11-20 23:11:48 -05006115 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04006116 }
6117
James Smart05580562011-05-24 11:40:48 -04006118 lpfc_update_vport_wwn(vport);
James Smartda0436e2009-05-22 14:51:39 -04006119
6120 /* Update the fc_host data structures with new wwn. */
6121 fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
6122 fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
6123
6124 /* Register SGL pool to the device using non-embedded mailbox command */
James Smart6d368e52011-05-24 11:44:12 -04006125 if (!phba->sli4_hba.extents_in_use) {
6126 rc = lpfc_sli4_post_els_sgl_list(phba);
6127 if (unlikely(rc)) {
6128 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6129 "0582 Error %d during els sgl post "
6130 "operation\n", rc);
6131 rc = -ENODEV;
6132 goto out_free_mbox;
6133 }
6134 } else {
6135 rc = lpfc_sli4_post_els_sgl_list_ext(phba);
6136 if (unlikely(rc)) {
6137 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6138 "2560 Error %d during els sgl post "
6139 "operation\n", rc);
6140 rc = -ENODEV;
6141 goto out_free_mbox;
6142 }
James Smartda0436e2009-05-22 14:51:39 -04006143 }
6144
6145 /* Register SCSI SGL pool to the device */
6146 rc = lpfc_sli4_repost_scsi_sgl_list(phba);
6147 if (unlikely(rc)) {
James Smart6d368e52011-05-24 11:44:12 -04006148 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smart6a9c52c2009-10-02 15:16:51 -04006149 "0383 Error %d during scsi sgl post "
6150 "operation\n", rc);
James Smartda0436e2009-05-22 14:51:39 -04006151 /* Some Scsi buffers were moved to the abort scsi list */
6152 /* A pci function reset will repost them */
6153 rc = -ENODEV;
James Smart76a95d72010-11-20 23:11:48 -05006154 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04006155 }
6156
6157 /* Post the rpi header region to the device. */
6158 rc = lpfc_sli4_post_all_rpi_hdrs(phba);
6159 if (unlikely(rc)) {
6160 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6161 "0393 Error %d during rpi post operation\n",
6162 rc);
6163 rc = -ENODEV;
James Smart76a95d72010-11-20 23:11:48 -05006164 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04006165 }
James Smartda0436e2009-05-22 14:51:39 -04006166
James Smart5350d872011-10-10 21:33:49 -04006167 /* Create all the SLI4 queues */
6168 rc = lpfc_sli4_queue_create(phba);
6169 if (rc) {
6170 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6171 "3089 Failed to allocate queues\n");
6172 rc = -ENODEV;
6173 goto out_stop_timers;
6174 }
James Smartda0436e2009-05-22 14:51:39 -04006175 /* Set up all the queues to the device */
6176 rc = lpfc_sli4_queue_setup(phba);
6177 if (unlikely(rc)) {
6178 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6179 "0381 Error %d during queue setup.\n ", rc);
James Smart5350d872011-10-10 21:33:49 -04006180 goto out_destroy_queue;
James Smartda0436e2009-05-22 14:51:39 -04006181 }
6182
6183 /* Arm the CQs and then EQs on device */
6184 lpfc_sli4_arm_cqeq_intr(phba);
6185
6186 /* Indicate device interrupt mode */
6187 phba->sli4_hba.intr_enable = 1;
6188
6189 /* Allow asynchronous mailbox command to go through */
6190 spin_lock_irq(&phba->hbalock);
6191 phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
6192 spin_unlock_irq(&phba->hbalock);
6193
6194 /* Post receive buffers to the device */
6195 lpfc_sli4_rb_setup(phba);
6196
James Smartfc2b9892010-02-26 14:15:29 -05006197 /* Reset HBA FCF states after HBA reset */
6198 phba->fcf.fcf_flag = 0;
6199 phba->fcf.current_rec.flag = 0;
6200
James Smartda0436e2009-05-22 14:51:39 -04006201 /* Start the ELS watchdog timer */
James Smart8fa38512009-07-19 10:01:03 -04006202 mod_timer(&vport->els_tmofunc,
6203 jiffies + HZ * (phba->fc_ratov * 2));
James Smartda0436e2009-05-22 14:51:39 -04006204
6205 /* Start heart beat timer */
6206 mod_timer(&phba->hb_tmofunc,
6207 jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
6208 phba->hb_outstanding = 0;
6209 phba->last_completion_time = jiffies;
6210
6211 /* Start error attention (ERATT) polling timer */
6212 mod_timer(&phba->eratt_poll, jiffies + HZ * LPFC_ERATT_POLL_INTERVAL);
6213
James Smart75baf692010-06-08 18:31:21 -04006214 /* Enable PCIe device Advanced Error Reporting (AER) if configured */
6215 if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
6216 rc = pci_enable_pcie_error_reporting(phba->pcidev);
6217 if (!rc) {
6218 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6219 "2829 This device supports "
6220 "Advanced Error Reporting (AER)\n");
6221 spin_lock_irq(&phba->hbalock);
6222 phba->hba_flag |= HBA_AER_ENABLED;
6223 spin_unlock_irq(&phba->hbalock);
6224 } else {
6225 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6226 "2830 This device does not support "
6227 "Advanced Error Reporting (AER)\n");
6228 phba->cfg_aer_support = 0;
6229 }
James Smart0a96e972011-07-22 18:37:28 -04006230 rc = 0;
James Smart75baf692010-06-08 18:31:21 -04006231 }
6232
James Smart76a95d72010-11-20 23:11:48 -05006233 if (!(phba->hba_flag & HBA_FCOE_MODE)) {
6234 /*
6235 * The FC Port needs to register FCFI (index 0)
6236 */
6237 lpfc_reg_fcfi(phba, mboxq);
6238 mboxq->vport = phba->pport;
6239 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
James Smart9589b062011-04-16 11:03:17 -04006240 if (rc != MBX_SUCCESS)
James Smart76a95d72010-11-20 23:11:48 -05006241 goto out_unset_queue;
James Smart9589b062011-04-16 11:03:17 -04006242 rc = 0;
6243 phba->fcf.fcfi = bf_get(lpfc_reg_fcfi_fcfi,
6244 &mboxq->u.mqe.un.reg_fcfi);
James Smart026abb82011-12-13 13:20:45 -05006245
6246 /* Check if the port is configured to be disabled */
6247 lpfc_sli_read_link_ste(phba);
James Smart76a95d72010-11-20 23:11:48 -05006248 }
James Smart026abb82011-12-13 13:20:45 -05006249
James Smartda0436e2009-05-22 14:51:39 -04006250 /*
6251 * The port is ready, set the host's link state to LINK_DOWN
6252 * in preparation for link interrupts.
6253 */
James Smartda0436e2009-05-22 14:51:39 -04006254 spin_lock_irq(&phba->hbalock);
6255 phba->link_state = LPFC_LINK_DOWN;
6256 spin_unlock_irq(&phba->hbalock);
James Smart026abb82011-12-13 13:20:45 -05006257 if (!(phba->hba_flag & HBA_FCOE_MODE) &&
6258 (phba->hba_flag & LINK_DISABLED)) {
6259 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_SLI,
6260 "3103 Adapter Link is disabled.\n");
6261 lpfc_down_link(phba, mboxq);
6262 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6263 if (rc != MBX_SUCCESS) {
6264 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_SLI,
6265 "3104 Adapter failed to issue "
6266 "DOWN_LINK mbox cmd, rc:x%x\n", rc);
6267 goto out_unset_queue;
6268 }
6269 } else if (phba->cfg_suppress_link_up == LPFC_INITIALIZE_LINK) {
James Smartfedd3b72011-02-16 12:39:24 -05006270 rc = phba->lpfc_hba_init_link(phba, MBX_NOWAIT);
James Smart5350d872011-10-10 21:33:49 -04006271 if (rc)
6272 goto out_unset_queue;
6273 }
6274 mempool_free(mboxq, phba->mbox_mem_pool);
6275 return rc;
James Smart76a95d72010-11-20 23:11:48 -05006276out_unset_queue:
James Smartda0436e2009-05-22 14:51:39 -04006277 /* Unset all the queues set up in this routine when error out */
James Smart5350d872011-10-10 21:33:49 -04006278 lpfc_sli4_queue_unset(phba);
6279out_destroy_queue:
6280 lpfc_sli4_queue_destroy(phba);
James Smartda0436e2009-05-22 14:51:39 -04006281out_stop_timers:
James Smart5350d872011-10-10 21:33:49 -04006282 lpfc_stop_hba_timers(phba);
James Smartda0436e2009-05-22 14:51:39 -04006283out_free_mbox:
6284 mempool_free(mboxq, phba->mbox_mem_pool);
6285 return rc;
6286}
James Smarte59058c2008-08-24 21:49:00 -04006287
6288/**
James Smart3621a712009-04-06 18:47:14 -04006289 * lpfc_mbox_timeout - Timeout call back function for mbox timer
James Smarte59058c2008-08-24 21:49:00 -04006290 * @ptr: context object - pointer to hba structure.
dea31012005-04-17 16:05:31 -05006291 *
James Smarte59058c2008-08-24 21:49:00 -04006292 * This is the callback function for mailbox timer. The mailbox
6293 * timer is armed when a new mailbox command is issued and the timer
6294 * is deleted when the mailbox complete. The function is called by
6295 * the kernel timer code when a mailbox does not complete within
6296 * expected time. This function wakes up the worker thread to
6297 * process the mailbox timeout and returns. All the processing is
6298 * done by the worker thread function lpfc_mbox_timeout_handler.
6299 **/
dea31012005-04-17 16:05:31 -05006300void
6301lpfc_mbox_timeout(unsigned long ptr)
6302{
James Smart92d7f7b2007-06-17 19:56:38 -05006303 struct lpfc_hba *phba = (struct lpfc_hba *) ptr;
dea31012005-04-17 16:05:31 -05006304 unsigned long iflag;
James Smart2e0fef82007-06-17 19:56:36 -05006305 uint32_t tmo_posted;
dea31012005-04-17 16:05:31 -05006306
James Smart2e0fef82007-06-17 19:56:36 -05006307 spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
James Smart92d7f7b2007-06-17 19:56:38 -05006308 tmo_posted = phba->pport->work_port_events & WORKER_MBOX_TMO;
James Smart2e0fef82007-06-17 19:56:36 -05006309 if (!tmo_posted)
6310 phba->pport->work_port_events |= WORKER_MBOX_TMO;
6311 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
6312
James Smart5e9d9b82008-06-14 22:52:53 -04006313 if (!tmo_posted)
6314 lpfc_worker_wake_up(phba);
6315 return;
dea31012005-04-17 16:05:31 -05006316}
6317
James Smarte59058c2008-08-24 21:49:00 -04006318
6319/**
James Smart3621a712009-04-06 18:47:14 -04006320 * lpfc_mbox_timeout_handler - Worker thread function to handle mailbox timeout
James Smarte59058c2008-08-24 21:49:00 -04006321 * @phba: Pointer to HBA context object.
6322 *
6323 * This function is called from worker thread when a mailbox command times out.
6324 * The caller is not required to hold any locks. This function will reset the
6325 * HBA and recover all the pending commands.
6326 **/
dea31012005-04-17 16:05:31 -05006327void
6328lpfc_mbox_timeout_handler(struct lpfc_hba *phba)
6329{
James Smart2e0fef82007-06-17 19:56:36 -05006330 LPFC_MBOXQ_t *pmbox = phba->sli.mbox_active;
James Smart04c68492009-05-22 14:52:52 -04006331 MAILBOX_t *mb = &pmbox->u.mb;
James Smart1dcb58e2007-04-25 09:51:30 -04006332 struct lpfc_sli *psli = &phba->sli;
6333 struct lpfc_sli_ring *pring;
dea31012005-04-17 16:05:31 -05006334
James Smarta257bf92009-04-06 18:48:10 -04006335 /* Check the pmbox pointer first. There is a race condition
6336 * between the mbox timeout handler getting executed in the
6337 * worklist and the mailbox actually completing. When this
6338 * race condition occurs, the mbox_active will be NULL.
6339 */
6340 spin_lock_irq(&phba->hbalock);
6341 if (pmbox == NULL) {
6342 lpfc_printf_log(phba, KERN_WARNING,
6343 LOG_MBOX | LOG_SLI,
6344 "0353 Active Mailbox cleared - mailbox timeout "
6345 "exiting\n");
6346 spin_unlock_irq(&phba->hbalock);
6347 return;
6348 }
6349
dea31012005-04-17 16:05:31 -05006350 /* Mbox cmd <mbxCommand> timeout */
James Smarted957682007-06-17 19:56:37 -05006351 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04006352 "0310 Mailbox command x%x timeout Data: x%x x%x x%p\n",
James Smart92d7f7b2007-06-17 19:56:38 -05006353 mb->mbxCommand,
6354 phba->pport->port_state,
6355 phba->sli.sli_flag,
6356 phba->sli.mbox_active);
James Smarta257bf92009-04-06 18:48:10 -04006357 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05006358
James Smart1dcb58e2007-04-25 09:51:30 -04006359 /* Setting state unknown so lpfc_sli_abort_iocb_ring
6360 * would get IOCB_ERROR from lpfc_sli_issue_iocb, allowing
Lucas De Marchi25985ed2011-03-30 22:57:33 -03006361 * it to fail all outstanding SCSI IO.
James Smart1dcb58e2007-04-25 09:51:30 -04006362 */
James Smart2e0fef82007-06-17 19:56:36 -05006363 spin_lock_irq(&phba->pport->work_port_lock);
6364 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
6365 spin_unlock_irq(&phba->pport->work_port_lock);
6366 spin_lock_irq(&phba->hbalock);
6367 phba->link_state = LPFC_LINK_UNKNOWN;
James Smartf4b4c682009-05-22 14:53:12 -04006368 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05006369 spin_unlock_irq(&phba->hbalock);
James Smart1dcb58e2007-04-25 09:51:30 -04006370
6371 pring = &psli->ring[psli->fcp_ring];
6372 lpfc_sli_abort_iocb_ring(phba, pring);
6373
6374 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smart76bb24e2007-10-27 13:38:00 -04006375 "0345 Resetting board due to mailbox timeout\n");
James Smart3772a992009-05-22 14:50:54 -04006376
6377 /* Reset the HBA device */
6378 lpfc_reset_hba(phba);
dea31012005-04-17 16:05:31 -05006379}
6380
James Smarte59058c2008-08-24 21:49:00 -04006381/**
James Smart3772a992009-05-22 14:50:54 -04006382 * lpfc_sli_issue_mbox_s3 - Issue an SLI3 mailbox command to firmware
James Smarte59058c2008-08-24 21:49:00 -04006383 * @phba: Pointer to HBA context object.
6384 * @pmbox: Pointer to mailbox object.
6385 * @flag: Flag indicating how the mailbox need to be processed.
6386 *
6387 * This function is called by discovery code and HBA management code
James Smart3772a992009-05-22 14:50:54 -04006388 * to submit a mailbox command to firmware with SLI-3 interface spec. This
6389 * function gets the hbalock to protect the data structures.
James Smarte59058c2008-08-24 21:49:00 -04006390 * The mailbox command can be submitted in polling mode, in which case
6391 * this function will wait in a polling loop for the completion of the
6392 * mailbox.
6393 * If the mailbox is submitted in no_wait mode (not polling) the
6394 * function will submit the command and returns immediately without waiting
6395 * for the mailbox completion. The no_wait is supported only when HBA
6396 * is in SLI2/SLI3 mode - interrupts are enabled.
6397 * The SLI interface allows only one mailbox pending at a time. If the
6398 * mailbox is issued in polling mode and there is already a mailbox
6399 * pending, then the function will return an error. If the mailbox is issued
6400 * in NO_WAIT mode and there is a mailbox pending already, the function
6401 * will return MBX_BUSY after queuing the mailbox into mailbox queue.
6402 * The sli layer owns the mailbox object until the completion of mailbox
6403 * command if this function return MBX_BUSY or MBX_SUCCESS. For all other
6404 * return codes the caller owns the mailbox command after the return of
6405 * the function.
6406 **/
James Smart3772a992009-05-22 14:50:54 -04006407static int
6408lpfc_sli_issue_mbox_s3(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox,
6409 uint32_t flag)
dea31012005-04-17 16:05:31 -05006410{
dea31012005-04-17 16:05:31 -05006411 MAILBOX_t *mb;
James Smart2e0fef82007-06-17 19:56:36 -05006412 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05006413 uint32_t status, evtctr;
James Smart9940b972011-03-11 16:06:12 -05006414 uint32_t ha_copy, hc_copy;
dea31012005-04-17 16:05:31 -05006415 int i;
James Smart09372822008-01-11 01:52:54 -05006416 unsigned long timeout;
dea31012005-04-17 16:05:31 -05006417 unsigned long drvr_flag = 0;
James Smart34b02dc2008-08-24 21:49:55 -04006418 uint32_t word0, ldata;
dea31012005-04-17 16:05:31 -05006419 void __iomem *to_slim;
James Smart58da1ff2008-04-07 10:15:56 -04006420 int processing_queue = 0;
6421
6422 spin_lock_irqsave(&phba->hbalock, drvr_flag);
6423 if (!pmbox) {
James Smart8568a4d2009-07-19 10:01:16 -04006424 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart58da1ff2008-04-07 10:15:56 -04006425 /* processing mbox queue from intr_handler */
James Smart3772a992009-05-22 14:50:54 -04006426 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
6427 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
6428 return MBX_SUCCESS;
6429 }
James Smart58da1ff2008-04-07 10:15:56 -04006430 processing_queue = 1;
James Smart58da1ff2008-04-07 10:15:56 -04006431 pmbox = lpfc_mbox_get(phba);
6432 if (!pmbox) {
6433 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
6434 return MBX_SUCCESS;
6435 }
6436 }
dea31012005-04-17 16:05:31 -05006437
James Smarted957682007-06-17 19:56:37 -05006438 if (pmbox->mbox_cmpl && pmbox->mbox_cmpl != lpfc_sli_def_mbox_cmpl &&
James Smart92d7f7b2007-06-17 19:56:38 -05006439 pmbox->mbox_cmpl != lpfc_sli_wake_mbox_wait) {
James Smarted957682007-06-17 19:56:37 -05006440 if(!pmbox->vport) {
James Smart58da1ff2008-04-07 10:15:56 -04006441 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
James Smarted957682007-06-17 19:56:37 -05006442 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -05006443 LOG_MBOX | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04006444 "1806 Mbox x%x failed. No vport\n",
James Smart3772a992009-05-22 14:50:54 -04006445 pmbox->u.mb.mbxCommand);
James Smarted957682007-06-17 19:56:37 -05006446 dump_stack();
James Smart58da1ff2008-04-07 10:15:56 -04006447 goto out_not_finished;
James Smarted957682007-06-17 19:56:37 -05006448 }
6449 }
6450
Linas Vepstas8d63f372007-02-14 14:28:36 -06006451 /* If the PCI channel is in offline state, do not post mbox. */
James Smart58da1ff2008-04-07 10:15:56 -04006452 if (unlikely(pci_channel_offline(phba->pcidev))) {
6453 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
6454 goto out_not_finished;
6455 }
Linas Vepstas8d63f372007-02-14 14:28:36 -06006456
James Smarta257bf92009-04-06 18:48:10 -04006457 /* If HBA has a deferred error attention, fail the iocb. */
6458 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
6459 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
6460 goto out_not_finished;
6461 }
6462
dea31012005-04-17 16:05:31 -05006463 psli = &phba->sli;
James Smart92d7f7b2007-06-17 19:56:38 -05006464
James Smart3772a992009-05-22 14:50:54 -04006465 mb = &pmbox->u.mb;
dea31012005-04-17 16:05:31 -05006466 status = MBX_SUCCESS;
6467
James Smart2e0fef82007-06-17 19:56:36 -05006468 if (phba->link_state == LPFC_HBA_ERROR) {
6469 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05006470
6471 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04006472 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6473 "(%d):0311 Mailbox command x%x cannot "
6474 "issue Data: x%x x%x\n",
6475 pmbox->vport ? pmbox->vport->vpi : 0,
6476 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04006477 goto out_not_finished;
Jamie Wellnitz41415862006-02-28 19:25:27 -05006478 }
6479
James Smart9940b972011-03-11 16:06:12 -05006480 if (mb->mbxCommand != MBX_KILL_BOARD && flag & MBX_NOWAIT) {
6481 if (lpfc_readl(phba->HCregaddr, &hc_copy) ||
6482 !(hc_copy & HC_MBINT_ENA)) {
6483 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
6484 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smart3772a992009-05-22 14:50:54 -04006485 "(%d):2528 Mailbox command x%x cannot "
6486 "issue Data: x%x x%x\n",
6487 pmbox->vport ? pmbox->vport->vpi : 0,
6488 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
James Smart9940b972011-03-11 16:06:12 -05006489 goto out_not_finished;
6490 }
James Smart92908312006-03-07 15:04:13 -05006491 }
6492
dea31012005-04-17 16:05:31 -05006493 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
6494 /* Polling for a mbox command when another one is already active
6495 * is not allowed in SLI. Also, the driver must have established
6496 * SLI2 mode to queue and process multiple mbox commands.
6497 */
6498
6499 if (flag & MBX_POLL) {
James Smart2e0fef82007-06-17 19:56:36 -05006500 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05006501
6502 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04006503 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6504 "(%d):2529 Mailbox command x%x "
6505 "cannot issue Data: x%x x%x\n",
6506 pmbox->vport ? pmbox->vport->vpi : 0,
6507 pmbox->u.mb.mbxCommand,
6508 psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04006509 goto out_not_finished;
dea31012005-04-17 16:05:31 -05006510 }
6511
James Smart3772a992009-05-22 14:50:54 -04006512 if (!(psli->sli_flag & LPFC_SLI_ACTIVE)) {
James Smart2e0fef82007-06-17 19:56:36 -05006513 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05006514 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04006515 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6516 "(%d):2530 Mailbox command x%x "
6517 "cannot issue Data: x%x x%x\n",
6518 pmbox->vport ? pmbox->vport->vpi : 0,
6519 pmbox->u.mb.mbxCommand,
6520 psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04006521 goto out_not_finished;
dea31012005-04-17 16:05:31 -05006522 }
6523
dea31012005-04-17 16:05:31 -05006524 /* Another mailbox command is still being processed, queue this
6525 * command to be processed later.
6526 */
6527 lpfc_mbox_put(phba, pmbox);
6528
6529 /* Mbox cmd issue - BUSY */
James Smarted957682007-06-17 19:56:37 -05006530 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04006531 "(%d):0308 Mbox cmd issue - BUSY Data: "
James Smart92d7f7b2007-06-17 19:56:38 -05006532 "x%x x%x x%x x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -05006533 pmbox->vport ? pmbox->vport->vpi : 0xffffff,
6534 mb->mbxCommand, phba->pport->port_state,
6535 psli->sli_flag, flag);
dea31012005-04-17 16:05:31 -05006536
6537 psli->slistat.mbox_busy++;
James Smart2e0fef82007-06-17 19:56:36 -05006538 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05006539
James Smart858c9f62007-06-17 19:56:39 -05006540 if (pmbox->vport) {
6541 lpfc_debugfs_disc_trc(pmbox->vport,
6542 LPFC_DISC_TRC_MBOX_VPORT,
6543 "MBOX Bsy vport: cmd:x%x mb:x%x x%x",
6544 (uint32_t)mb->mbxCommand,
6545 mb->un.varWords[0], mb->un.varWords[1]);
6546 }
6547 else {
6548 lpfc_debugfs_disc_trc(phba->pport,
6549 LPFC_DISC_TRC_MBOX,
6550 "MBOX Bsy: cmd:x%x mb:x%x x%x",
6551 (uint32_t)mb->mbxCommand,
6552 mb->un.varWords[0], mb->un.varWords[1]);
6553 }
6554
James Smart2e0fef82007-06-17 19:56:36 -05006555 return MBX_BUSY;
dea31012005-04-17 16:05:31 -05006556 }
6557
dea31012005-04-17 16:05:31 -05006558 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
6559
6560 /* If we are not polling, we MUST be in SLI2 mode */
6561 if (flag != MBX_POLL) {
James Smart3772a992009-05-22 14:50:54 -04006562 if (!(psli->sli_flag & LPFC_SLI_ACTIVE) &&
Jamie Wellnitz41415862006-02-28 19:25:27 -05006563 (mb->mbxCommand != MBX_KILL_BOARD)) {
dea31012005-04-17 16:05:31 -05006564 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05006565 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05006566 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04006567 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6568 "(%d):2531 Mailbox command x%x "
6569 "cannot issue Data: x%x x%x\n",
6570 pmbox->vport ? pmbox->vport->vpi : 0,
6571 pmbox->u.mb.mbxCommand,
6572 psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04006573 goto out_not_finished;
dea31012005-04-17 16:05:31 -05006574 }
6575 /* timeout active mbox command */
James Smarta309a6b2006-08-01 07:33:43 -04006576 mod_timer(&psli->mbox_tmo, (jiffies +
James Smarta183a152011-10-10 21:32:43 -04006577 (HZ * lpfc_mbox_tmo_val(phba, pmbox))));
dea31012005-04-17 16:05:31 -05006578 }
6579
6580 /* Mailbox cmd <cmd> issue */
James Smarted957682007-06-17 19:56:37 -05006581 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04006582 "(%d):0309 Mailbox cmd x%x issue Data: x%x x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05006583 "x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04006584 pmbox->vport ? pmbox->vport->vpi : 0,
James Smart92d7f7b2007-06-17 19:56:38 -05006585 mb->mbxCommand, phba->pport->port_state,
6586 psli->sli_flag, flag);
dea31012005-04-17 16:05:31 -05006587
James Smart858c9f62007-06-17 19:56:39 -05006588 if (mb->mbxCommand != MBX_HEARTBEAT) {
6589 if (pmbox->vport) {
6590 lpfc_debugfs_disc_trc(pmbox->vport,
6591 LPFC_DISC_TRC_MBOX_VPORT,
6592 "MBOX Send vport: cmd:x%x mb:x%x x%x",
6593 (uint32_t)mb->mbxCommand,
6594 mb->un.varWords[0], mb->un.varWords[1]);
6595 }
6596 else {
6597 lpfc_debugfs_disc_trc(phba->pport,
6598 LPFC_DISC_TRC_MBOX,
6599 "MBOX Send: cmd:x%x mb:x%x x%x",
6600 (uint32_t)mb->mbxCommand,
6601 mb->un.varWords[0], mb->un.varWords[1]);
6602 }
6603 }
6604
dea31012005-04-17 16:05:31 -05006605 psli->slistat.mbox_cmd++;
6606 evtctr = psli->slistat.mbox_event;
6607
6608 /* next set own bit for the adapter and copy over command word */
6609 mb->mbxOwner = OWN_CHIP;
6610
James Smart3772a992009-05-22 14:50:54 -04006611 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
James Smart7a470272010-03-15 11:25:20 -04006612 /* Populate mbox extension offset word. */
6613 if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len) {
6614 *(((uint32_t *)mb) + pmbox->mbox_offset_word)
6615 = (uint8_t *)phba->mbox_ext
6616 - (uint8_t *)phba->mbox;
6617 }
6618
6619 /* Copy the mailbox extension data */
6620 if (pmbox->in_ext_byte_len && pmbox->context2) {
6621 lpfc_sli_pcimem_bcopy(pmbox->context2,
6622 (uint8_t *)phba->mbox_ext,
6623 pmbox->in_ext_byte_len);
6624 }
6625 /* Copy command data to host SLIM area */
James Smart34b02dc2008-08-24 21:49:55 -04006626 lpfc_sli_pcimem_bcopy(mb, phba->mbox, MAILBOX_CMD_SIZE);
dea31012005-04-17 16:05:31 -05006627 } else {
James Smart7a470272010-03-15 11:25:20 -04006628 /* Populate mbox extension offset word. */
6629 if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len)
6630 *(((uint32_t *)mb) + pmbox->mbox_offset_word)
6631 = MAILBOX_HBA_EXT_OFFSET;
6632
6633 /* Copy the mailbox extension data */
6634 if (pmbox->in_ext_byte_len && pmbox->context2) {
6635 lpfc_memcpy_to_slim(phba->MBslimaddr +
6636 MAILBOX_HBA_EXT_OFFSET,
6637 pmbox->context2, pmbox->in_ext_byte_len);
6638
6639 }
James Smart92908312006-03-07 15:04:13 -05006640 if (mb->mbxCommand == MBX_CONFIG_PORT) {
dea31012005-04-17 16:05:31 -05006641 /* copy command data into host mbox for cmpl */
James Smart34b02dc2008-08-24 21:49:55 -04006642 lpfc_sli_pcimem_bcopy(mb, phba->mbox, MAILBOX_CMD_SIZE);
dea31012005-04-17 16:05:31 -05006643 }
6644
6645 /* First copy mbox command data to HBA SLIM, skip past first
6646 word */
6647 to_slim = phba->MBslimaddr + sizeof (uint32_t);
6648 lpfc_memcpy_to_slim(to_slim, &mb->un.varWords[0],
6649 MAILBOX_CMD_SIZE - sizeof (uint32_t));
6650
6651 /* Next copy over first word, with mbxOwner set */
James Smart34b02dc2008-08-24 21:49:55 -04006652 ldata = *((uint32_t *)mb);
dea31012005-04-17 16:05:31 -05006653 to_slim = phba->MBslimaddr;
6654 writel(ldata, to_slim);
6655 readl(to_slim); /* flush */
6656
6657 if (mb->mbxCommand == MBX_CONFIG_PORT) {
6658 /* switch over to host mailbox */
James Smart3772a992009-05-22 14:50:54 -04006659 psli->sli_flag |= LPFC_SLI_ACTIVE;
dea31012005-04-17 16:05:31 -05006660 }
6661 }
6662
6663 wmb();
dea31012005-04-17 16:05:31 -05006664
6665 switch (flag) {
6666 case MBX_NOWAIT:
James Smart09372822008-01-11 01:52:54 -05006667 /* Set up reference to mailbox command */
dea31012005-04-17 16:05:31 -05006668 psli->mbox_active = pmbox;
James Smart09372822008-01-11 01:52:54 -05006669 /* Interrupt board to do it */
6670 writel(CA_MBATT, phba->CAregaddr);
6671 readl(phba->CAregaddr); /* flush */
6672 /* Don't wait for it to finish, just return */
dea31012005-04-17 16:05:31 -05006673 break;
6674
6675 case MBX_POLL:
James Smart09372822008-01-11 01:52:54 -05006676 /* Set up null reference to mailbox command */
dea31012005-04-17 16:05:31 -05006677 psli->mbox_active = NULL;
James Smart09372822008-01-11 01:52:54 -05006678 /* Interrupt board to do it */
6679 writel(CA_MBATT, phba->CAregaddr);
6680 readl(phba->CAregaddr); /* flush */
6681
James Smart3772a992009-05-22 14:50:54 -04006682 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05006683 /* First read mbox status word */
James Smart34b02dc2008-08-24 21:49:55 -04006684 word0 = *((uint32_t *)phba->mbox);
dea31012005-04-17 16:05:31 -05006685 word0 = le32_to_cpu(word0);
6686 } else {
6687 /* First read mbox status word */
James Smart9940b972011-03-11 16:06:12 -05006688 if (lpfc_readl(phba->MBslimaddr, &word0)) {
6689 spin_unlock_irqrestore(&phba->hbalock,
6690 drvr_flag);
6691 goto out_not_finished;
6692 }
dea31012005-04-17 16:05:31 -05006693 }
6694
6695 /* Read the HBA Host Attention Register */
James Smart9940b972011-03-11 16:06:12 -05006696 if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
6697 spin_unlock_irqrestore(&phba->hbalock,
6698 drvr_flag);
6699 goto out_not_finished;
6700 }
James Smarta183a152011-10-10 21:32:43 -04006701 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, pmbox) *
6702 1000) + jiffies;
James Smart09372822008-01-11 01:52:54 -05006703 i = 0;
dea31012005-04-17 16:05:31 -05006704 /* Wait for command to complete */
Jamie Wellnitz41415862006-02-28 19:25:27 -05006705 while (((word0 & OWN_CHIP) == OWN_CHIP) ||
6706 (!(ha_copy & HA_MBATT) &&
James Smart2e0fef82007-06-17 19:56:36 -05006707 (phba->link_state > LPFC_WARM_START))) {
James Smart09372822008-01-11 01:52:54 -05006708 if (time_after(jiffies, timeout)) {
dea31012005-04-17 16:05:31 -05006709 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05006710 spin_unlock_irqrestore(&phba->hbalock,
dea31012005-04-17 16:05:31 -05006711 drvr_flag);
James Smart58da1ff2008-04-07 10:15:56 -04006712 goto out_not_finished;
dea31012005-04-17 16:05:31 -05006713 }
6714
6715 /* Check if we took a mbox interrupt while we were
6716 polling */
6717 if (((word0 & OWN_CHIP) != OWN_CHIP)
6718 && (evtctr != psli->slistat.mbox_event))
6719 break;
6720
James Smart09372822008-01-11 01:52:54 -05006721 if (i++ > 10) {
6722 spin_unlock_irqrestore(&phba->hbalock,
6723 drvr_flag);
6724 msleep(1);
6725 spin_lock_irqsave(&phba->hbalock, drvr_flag);
6726 }
dea31012005-04-17 16:05:31 -05006727
James Smart3772a992009-05-22 14:50:54 -04006728 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05006729 /* First copy command data */
James Smart34b02dc2008-08-24 21:49:55 -04006730 word0 = *((uint32_t *)phba->mbox);
dea31012005-04-17 16:05:31 -05006731 word0 = le32_to_cpu(word0);
6732 if (mb->mbxCommand == MBX_CONFIG_PORT) {
6733 MAILBOX_t *slimmb;
James Smart34b02dc2008-08-24 21:49:55 -04006734 uint32_t slimword0;
dea31012005-04-17 16:05:31 -05006735 /* Check real SLIM for any errors */
6736 slimword0 = readl(phba->MBslimaddr);
6737 slimmb = (MAILBOX_t *) & slimword0;
6738 if (((slimword0 & OWN_CHIP) != OWN_CHIP)
6739 && slimmb->mbxStatus) {
6740 psli->sli_flag &=
James Smart3772a992009-05-22 14:50:54 -04006741 ~LPFC_SLI_ACTIVE;
dea31012005-04-17 16:05:31 -05006742 word0 = slimword0;
6743 }
6744 }
6745 } else {
6746 /* First copy command data */
6747 word0 = readl(phba->MBslimaddr);
6748 }
6749 /* Read the HBA Host Attention Register */
James Smart9940b972011-03-11 16:06:12 -05006750 if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
6751 spin_unlock_irqrestore(&phba->hbalock,
6752 drvr_flag);
6753 goto out_not_finished;
6754 }
dea31012005-04-17 16:05:31 -05006755 }
6756
James Smart3772a992009-05-22 14:50:54 -04006757 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05006758 /* copy results back to user */
James Smart34b02dc2008-08-24 21:49:55 -04006759 lpfc_sli_pcimem_bcopy(phba->mbox, mb, MAILBOX_CMD_SIZE);
James Smart7a470272010-03-15 11:25:20 -04006760 /* Copy the mailbox extension data */
6761 if (pmbox->out_ext_byte_len && pmbox->context2) {
6762 lpfc_sli_pcimem_bcopy(phba->mbox_ext,
6763 pmbox->context2,
6764 pmbox->out_ext_byte_len);
6765 }
dea31012005-04-17 16:05:31 -05006766 } else {
6767 /* First copy command data */
6768 lpfc_memcpy_from_slim(mb, phba->MBslimaddr,
6769 MAILBOX_CMD_SIZE);
James Smart7a470272010-03-15 11:25:20 -04006770 /* Copy the mailbox extension data */
6771 if (pmbox->out_ext_byte_len && pmbox->context2) {
6772 lpfc_memcpy_from_slim(pmbox->context2,
6773 phba->MBslimaddr +
6774 MAILBOX_HBA_EXT_OFFSET,
6775 pmbox->out_ext_byte_len);
dea31012005-04-17 16:05:31 -05006776 }
6777 }
6778
6779 writel(HA_MBATT, phba->HAregaddr);
6780 readl(phba->HAregaddr); /* flush */
6781
6782 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
6783 status = mb->mbxStatus;
6784 }
6785
James Smart2e0fef82007-06-17 19:56:36 -05006786 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
6787 return status;
James Smart58da1ff2008-04-07 10:15:56 -04006788
6789out_not_finished:
6790 if (processing_queue) {
James Smartda0436e2009-05-22 14:51:39 -04006791 pmbox->u.mb.mbxStatus = MBX_NOT_FINISHED;
James Smart58da1ff2008-04-07 10:15:56 -04006792 lpfc_mbox_cmpl_put(phba, pmbox);
6793 }
6794 return MBX_NOT_FINISHED;
dea31012005-04-17 16:05:31 -05006795}
6796
James Smarte59058c2008-08-24 21:49:00 -04006797/**
James Smartf1126682009-06-10 17:22:44 -04006798 * lpfc_sli4_async_mbox_block - Block posting SLI4 asynchronous mailbox command
6799 * @phba: Pointer to HBA context object.
6800 *
6801 * The function blocks the posting of SLI4 asynchronous mailbox commands from
6802 * the driver internal pending mailbox queue. It will then try to wait out the
6803 * possible outstanding mailbox command before return.
6804 *
6805 * Returns:
6806 * 0 - the outstanding mailbox command completed; otherwise, the wait for
6807 * the outstanding mailbox command timed out.
6808 **/
6809static int
6810lpfc_sli4_async_mbox_block(struct lpfc_hba *phba)
6811{
6812 struct lpfc_sli *psli = &phba->sli;
James Smartf1126682009-06-10 17:22:44 -04006813 int rc = 0;
James Smarta183a152011-10-10 21:32:43 -04006814 unsigned long timeout = 0;
James Smartf1126682009-06-10 17:22:44 -04006815
6816 /* Mark the asynchronous mailbox command posting as blocked */
6817 spin_lock_irq(&phba->hbalock);
6818 psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
James Smartf1126682009-06-10 17:22:44 -04006819 /* Determine how long we might wait for the active mailbox
6820 * command to be gracefully completed by firmware.
6821 */
James Smarta183a152011-10-10 21:32:43 -04006822 if (phba->sli.mbox_active)
6823 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
6824 phba->sli.mbox_active) *
6825 1000) + jiffies;
6826 spin_unlock_irq(&phba->hbalock);
6827
James Smartf1126682009-06-10 17:22:44 -04006828 /* Wait for the outstnading mailbox command to complete */
6829 while (phba->sli.mbox_active) {
6830 /* Check active mailbox complete status every 2ms */
6831 msleep(2);
6832 if (time_after(jiffies, timeout)) {
6833 /* Timeout, marked the outstanding cmd not complete */
6834 rc = 1;
6835 break;
6836 }
6837 }
6838
6839 /* Can not cleanly block async mailbox command, fails it */
6840 if (rc) {
6841 spin_lock_irq(&phba->hbalock);
6842 psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
6843 spin_unlock_irq(&phba->hbalock);
6844 }
6845 return rc;
6846}
6847
6848/**
6849 * lpfc_sli4_async_mbox_unblock - Block posting SLI4 async mailbox command
6850 * @phba: Pointer to HBA context object.
6851 *
6852 * The function unblocks and resume posting of SLI4 asynchronous mailbox
6853 * commands from the driver internal pending mailbox queue. It makes sure
6854 * that there is no outstanding mailbox command before resuming posting
6855 * asynchronous mailbox commands. If, for any reason, there is outstanding
6856 * mailbox command, it will try to wait it out before resuming asynchronous
6857 * mailbox command posting.
6858 **/
6859static void
6860lpfc_sli4_async_mbox_unblock(struct lpfc_hba *phba)
6861{
6862 struct lpfc_sli *psli = &phba->sli;
6863
6864 spin_lock_irq(&phba->hbalock);
6865 if (!(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
6866 /* Asynchronous mailbox posting is not blocked, do nothing */
6867 spin_unlock_irq(&phba->hbalock);
6868 return;
6869 }
6870
6871 /* Outstanding synchronous mailbox command is guaranteed to be done,
6872 * successful or timeout, after timing-out the outstanding mailbox
6873 * command shall always be removed, so just unblock posting async
6874 * mailbox command and resume
6875 */
6876 psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
6877 spin_unlock_irq(&phba->hbalock);
6878
6879 /* wake up worker thread to post asynchronlous mailbox command */
6880 lpfc_worker_wake_up(phba);
6881}
6882
6883/**
James Smartda0436e2009-05-22 14:51:39 -04006884 * lpfc_sli4_post_sync_mbox - Post an SLI4 mailbox to the bootstrap mailbox
6885 * @phba: Pointer to HBA context object.
6886 * @mboxq: Pointer to mailbox object.
6887 *
6888 * The function posts a mailbox to the port. The mailbox is expected
6889 * to be comletely filled in and ready for the port to operate on it.
6890 * This routine executes a synchronous completion operation on the
6891 * mailbox by polling for its completion.
6892 *
6893 * The caller must not be holding any locks when calling this routine.
6894 *
6895 * Returns:
6896 * MBX_SUCCESS - mailbox posted successfully
6897 * Any of the MBX error values.
6898 **/
6899static int
6900lpfc_sli4_post_sync_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
6901{
6902 int rc = MBX_SUCCESS;
6903 unsigned long iflag;
6904 uint32_t db_ready;
6905 uint32_t mcqe_status;
6906 uint32_t mbx_cmnd;
6907 unsigned long timeout;
6908 struct lpfc_sli *psli = &phba->sli;
6909 struct lpfc_mqe *mb = &mboxq->u.mqe;
6910 struct lpfc_bmbx_create *mbox_rgn;
6911 struct dma_address *dma_address;
6912 struct lpfc_register bmbx_reg;
6913
6914 /*
6915 * Only one mailbox can be active to the bootstrap mailbox region
6916 * at a time and there is no queueing provided.
6917 */
6918 spin_lock_irqsave(&phba->hbalock, iflag);
6919 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
6920 spin_unlock_irqrestore(&phba->hbalock, iflag);
6921 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04006922 "(%d):2532 Mailbox command x%x (x%x/x%x) "
James Smartda0436e2009-05-22 14:51:39 -04006923 "cannot issue Data: x%x x%x\n",
6924 mboxq->vport ? mboxq->vport->vpi : 0,
6925 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04006926 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
6927 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04006928 psli->sli_flag, MBX_POLL);
6929 return MBXERR_ERROR;
6930 }
6931 /* The server grabs the token and owns it until release */
6932 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
6933 phba->sli.mbox_active = mboxq;
6934 spin_unlock_irqrestore(&phba->hbalock, iflag);
6935
6936 /*
6937 * Initialize the bootstrap memory region to avoid stale data areas
6938 * in the mailbox post. Then copy the caller's mailbox contents to
6939 * the bmbx mailbox region.
6940 */
6941 mbx_cmnd = bf_get(lpfc_mqe_command, mb);
6942 memset(phba->sli4_hba.bmbx.avirt, 0, sizeof(struct lpfc_bmbx_create));
6943 lpfc_sli_pcimem_bcopy(mb, phba->sli4_hba.bmbx.avirt,
6944 sizeof(struct lpfc_mqe));
6945
6946 /* Post the high mailbox dma address to the port and wait for ready. */
6947 dma_address = &phba->sli4_hba.bmbx.dma_address;
6948 writel(dma_address->addr_hi, phba->sli4_hba.BMBXregaddr);
6949
James Smarta183a152011-10-10 21:32:43 -04006950 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mboxq)
James Smartda0436e2009-05-22 14:51:39 -04006951 * 1000) + jiffies;
6952 do {
6953 bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
6954 db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
6955 if (!db_ready)
6956 msleep(2);
6957
6958 if (time_after(jiffies, timeout)) {
6959 rc = MBXERR_ERROR;
6960 goto exit;
6961 }
6962 } while (!db_ready);
6963
6964 /* Post the low mailbox dma address to the port. */
6965 writel(dma_address->addr_lo, phba->sli4_hba.BMBXregaddr);
James Smarta183a152011-10-10 21:32:43 -04006966 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mboxq)
James Smartda0436e2009-05-22 14:51:39 -04006967 * 1000) + jiffies;
6968 do {
6969 bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
6970 db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
6971 if (!db_ready)
6972 msleep(2);
6973
6974 if (time_after(jiffies, timeout)) {
6975 rc = MBXERR_ERROR;
6976 goto exit;
6977 }
6978 } while (!db_ready);
6979
6980 /*
6981 * Read the CQ to ensure the mailbox has completed.
6982 * If so, update the mailbox status so that the upper layers
6983 * can complete the request normally.
6984 */
6985 lpfc_sli_pcimem_bcopy(phba->sli4_hba.bmbx.avirt, mb,
6986 sizeof(struct lpfc_mqe));
6987 mbox_rgn = (struct lpfc_bmbx_create *) phba->sli4_hba.bmbx.avirt;
6988 lpfc_sli_pcimem_bcopy(&mbox_rgn->mcqe, &mboxq->mcqe,
6989 sizeof(struct lpfc_mcqe));
6990 mcqe_status = bf_get(lpfc_mcqe_status, &mbox_rgn->mcqe);
James Smart05580562011-05-24 11:40:48 -04006991 /*
6992 * When the CQE status indicates a failure and the mailbox status
6993 * indicates success then copy the CQE status into the mailbox status
6994 * (and prefix it with x4000).
6995 */
James Smartda0436e2009-05-22 14:51:39 -04006996 if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
James Smart05580562011-05-24 11:40:48 -04006997 if (bf_get(lpfc_mqe_status, mb) == MBX_SUCCESS)
6998 bf_set(lpfc_mqe_status, mb,
6999 (LPFC_MBX_ERROR_RANGE | mcqe_status));
James Smartda0436e2009-05-22 14:51:39 -04007000 rc = MBXERR_ERROR;
James Smartd7c47992010-06-08 18:31:54 -04007001 } else
7002 lpfc_sli4_swap_str(phba, mboxq);
James Smartda0436e2009-05-22 14:51:39 -04007003
7004 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04007005 "(%d):0356 Mailbox cmd x%x (x%x/x%x) Status x%x "
James Smartda0436e2009-05-22 14:51:39 -04007006 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x"
7007 " x%x x%x CQ: x%x x%x x%x x%x\n",
James Smarta183a152011-10-10 21:32:43 -04007008 mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
7009 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7010 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04007011 bf_get(lpfc_mqe_status, mb),
7012 mb->un.mb_words[0], mb->un.mb_words[1],
7013 mb->un.mb_words[2], mb->un.mb_words[3],
7014 mb->un.mb_words[4], mb->un.mb_words[5],
7015 mb->un.mb_words[6], mb->un.mb_words[7],
7016 mb->un.mb_words[8], mb->un.mb_words[9],
7017 mb->un.mb_words[10], mb->un.mb_words[11],
7018 mb->un.mb_words[12], mboxq->mcqe.word0,
7019 mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
7020 mboxq->mcqe.trailer);
7021exit:
7022 /* We are holding the token, no needed for lock when release */
7023 spin_lock_irqsave(&phba->hbalock, iflag);
7024 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
7025 phba->sli.mbox_active = NULL;
7026 spin_unlock_irqrestore(&phba->hbalock, iflag);
7027 return rc;
7028}
7029
7030/**
7031 * lpfc_sli_issue_mbox_s4 - Issue an SLI4 mailbox command to firmware
7032 * @phba: Pointer to HBA context object.
7033 * @pmbox: Pointer to mailbox object.
7034 * @flag: Flag indicating how the mailbox need to be processed.
7035 *
7036 * This function is called by discovery code and HBA management code to submit
7037 * a mailbox command to firmware with SLI-4 interface spec.
7038 *
7039 * Return codes the caller owns the mailbox command after the return of the
7040 * function.
7041 **/
7042static int
7043lpfc_sli_issue_mbox_s4(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
7044 uint32_t flag)
7045{
7046 struct lpfc_sli *psli = &phba->sli;
7047 unsigned long iflags;
7048 int rc;
7049
James Smartb76f2dc2011-07-22 18:37:42 -04007050 /* dump from issue mailbox command if setup */
7051 lpfc_idiag_mbxacc_dump_issue_mbox(phba, &mboxq->u.mb);
7052
James Smart8fa38512009-07-19 10:01:03 -04007053 rc = lpfc_mbox_dev_check(phba);
7054 if (unlikely(rc)) {
7055 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04007056 "(%d):2544 Mailbox command x%x (x%x/x%x) "
James Smart8fa38512009-07-19 10:01:03 -04007057 "cannot issue Data: x%x x%x\n",
7058 mboxq->vport ? mboxq->vport->vpi : 0,
7059 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04007060 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7061 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smart8fa38512009-07-19 10:01:03 -04007062 psli->sli_flag, flag);
7063 goto out_not_finished;
7064 }
7065
James Smartda0436e2009-05-22 14:51:39 -04007066 /* Detect polling mode and jump to a handler */
7067 if (!phba->sli4_hba.intr_enable) {
7068 if (flag == MBX_POLL)
7069 rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
7070 else
7071 rc = -EIO;
7072 if (rc != MBX_SUCCESS)
James Smart05580562011-05-24 11:40:48 -04007073 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
James Smartda0436e2009-05-22 14:51:39 -04007074 "(%d):2541 Mailbox command x%x "
James Smarta183a152011-10-10 21:32:43 -04007075 "(x%x/x%x) cannot issue Data: "
7076 "x%x x%x\n",
James Smartda0436e2009-05-22 14:51:39 -04007077 mboxq->vport ? mboxq->vport->vpi : 0,
7078 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04007079 lpfc_sli_config_mbox_subsys_get(phba,
7080 mboxq),
7081 lpfc_sli_config_mbox_opcode_get(phba,
7082 mboxq),
James Smartda0436e2009-05-22 14:51:39 -04007083 psli->sli_flag, flag);
7084 return rc;
7085 } else if (flag == MBX_POLL) {
James Smartf1126682009-06-10 17:22:44 -04007086 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
7087 "(%d):2542 Try to issue mailbox command "
James Smarta183a152011-10-10 21:32:43 -04007088 "x%x (x%x/x%x) synchronously ahead of async"
James Smartf1126682009-06-10 17:22:44 -04007089 "mailbox command queue: x%x x%x\n",
James Smartda0436e2009-05-22 14:51:39 -04007090 mboxq->vport ? mboxq->vport->vpi : 0,
7091 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04007092 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7093 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04007094 psli->sli_flag, flag);
James Smartf1126682009-06-10 17:22:44 -04007095 /* Try to block the asynchronous mailbox posting */
7096 rc = lpfc_sli4_async_mbox_block(phba);
7097 if (!rc) {
7098 /* Successfully blocked, now issue sync mbox cmd */
7099 rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
7100 if (rc != MBX_SUCCESS)
7101 lpfc_printf_log(phba, KERN_ERR,
James Smarta183a152011-10-10 21:32:43 -04007102 LOG_MBOX | LOG_SLI,
7103 "(%d):2597 Mailbox command "
7104 "x%x (x%x/x%x) cannot issue "
7105 "Data: x%x x%x\n",
7106 mboxq->vport ?
7107 mboxq->vport->vpi : 0,
7108 mboxq->u.mb.mbxCommand,
7109 lpfc_sli_config_mbox_subsys_get(phba,
7110 mboxq),
7111 lpfc_sli_config_mbox_opcode_get(phba,
7112 mboxq),
7113 psli->sli_flag, flag);
James Smartf1126682009-06-10 17:22:44 -04007114 /* Unblock the async mailbox posting afterward */
7115 lpfc_sli4_async_mbox_unblock(phba);
7116 }
7117 return rc;
James Smartda0436e2009-05-22 14:51:39 -04007118 }
7119
7120 /* Now, interrupt mode asynchrous mailbox command */
7121 rc = lpfc_mbox_cmd_check(phba, mboxq);
7122 if (rc) {
7123 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04007124 "(%d):2543 Mailbox command x%x (x%x/x%x) "
James Smartda0436e2009-05-22 14:51:39 -04007125 "cannot issue Data: x%x x%x\n",
7126 mboxq->vport ? mboxq->vport->vpi : 0,
7127 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04007128 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7129 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04007130 psli->sli_flag, flag);
7131 goto out_not_finished;
7132 }
James Smartda0436e2009-05-22 14:51:39 -04007133
7134 /* Put the mailbox command to the driver internal FIFO */
7135 psli->slistat.mbox_busy++;
7136 spin_lock_irqsave(&phba->hbalock, iflags);
7137 lpfc_mbox_put(phba, mboxq);
7138 spin_unlock_irqrestore(&phba->hbalock, iflags);
7139 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
7140 "(%d):0354 Mbox cmd issue - Enqueue Data: "
James Smarta183a152011-10-10 21:32:43 -04007141 "x%x (x%x/x%x) x%x x%x x%x\n",
James Smartda0436e2009-05-22 14:51:39 -04007142 mboxq->vport ? mboxq->vport->vpi : 0xffffff,
7143 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
James Smarta183a152011-10-10 21:32:43 -04007144 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7145 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04007146 phba->pport->port_state,
7147 psli->sli_flag, MBX_NOWAIT);
7148 /* Wake up worker thread to transport mailbox command from head */
7149 lpfc_worker_wake_up(phba);
7150
7151 return MBX_BUSY;
7152
7153out_not_finished:
7154 return MBX_NOT_FINISHED;
7155}
7156
7157/**
7158 * lpfc_sli4_post_async_mbox - Post an SLI4 mailbox command to device
7159 * @phba: Pointer to HBA context object.
7160 *
7161 * This function is called by worker thread to send a mailbox command to
7162 * SLI4 HBA firmware.
7163 *
7164 **/
7165int
7166lpfc_sli4_post_async_mbox(struct lpfc_hba *phba)
7167{
7168 struct lpfc_sli *psli = &phba->sli;
7169 LPFC_MBOXQ_t *mboxq;
7170 int rc = MBX_SUCCESS;
7171 unsigned long iflags;
7172 struct lpfc_mqe *mqe;
7173 uint32_t mbx_cmnd;
7174
7175 /* Check interrupt mode before post async mailbox command */
7176 if (unlikely(!phba->sli4_hba.intr_enable))
7177 return MBX_NOT_FINISHED;
7178
7179 /* Check for mailbox command service token */
7180 spin_lock_irqsave(&phba->hbalock, iflags);
7181 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
7182 spin_unlock_irqrestore(&phba->hbalock, iflags);
7183 return MBX_NOT_FINISHED;
7184 }
7185 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
7186 spin_unlock_irqrestore(&phba->hbalock, iflags);
7187 return MBX_NOT_FINISHED;
7188 }
7189 if (unlikely(phba->sli.mbox_active)) {
7190 spin_unlock_irqrestore(&phba->hbalock, iflags);
7191 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7192 "0384 There is pending active mailbox cmd\n");
7193 return MBX_NOT_FINISHED;
7194 }
7195 /* Take the mailbox command service token */
7196 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
7197
7198 /* Get the next mailbox command from head of queue */
7199 mboxq = lpfc_mbox_get(phba);
7200
7201 /* If no more mailbox command waiting for post, we're done */
7202 if (!mboxq) {
7203 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
7204 spin_unlock_irqrestore(&phba->hbalock, iflags);
7205 return MBX_SUCCESS;
7206 }
7207 phba->sli.mbox_active = mboxq;
7208 spin_unlock_irqrestore(&phba->hbalock, iflags);
7209
7210 /* Check device readiness for posting mailbox command */
7211 rc = lpfc_mbox_dev_check(phba);
7212 if (unlikely(rc))
7213 /* Driver clean routine will clean up pending mailbox */
7214 goto out_not_finished;
7215
7216 /* Prepare the mbox command to be posted */
7217 mqe = &mboxq->u.mqe;
7218 mbx_cmnd = bf_get(lpfc_mqe_command, mqe);
7219
7220 /* Start timer for the mbox_tmo and log some mailbox post messages */
7221 mod_timer(&psli->mbox_tmo, (jiffies +
James Smarta183a152011-10-10 21:32:43 -04007222 (HZ * lpfc_mbox_tmo_val(phba, mboxq))));
James Smartda0436e2009-05-22 14:51:39 -04007223
7224 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04007225 "(%d):0355 Mailbox cmd x%x (x%x/x%x) issue Data: "
James Smartda0436e2009-05-22 14:51:39 -04007226 "x%x x%x\n",
7227 mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
James Smarta183a152011-10-10 21:32:43 -04007228 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7229 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04007230 phba->pport->port_state, psli->sli_flag);
7231
7232 if (mbx_cmnd != MBX_HEARTBEAT) {
7233 if (mboxq->vport) {
7234 lpfc_debugfs_disc_trc(mboxq->vport,
7235 LPFC_DISC_TRC_MBOX_VPORT,
7236 "MBOX Send vport: cmd:x%x mb:x%x x%x",
7237 mbx_cmnd, mqe->un.mb_words[0],
7238 mqe->un.mb_words[1]);
7239 } else {
7240 lpfc_debugfs_disc_trc(phba->pport,
7241 LPFC_DISC_TRC_MBOX,
7242 "MBOX Send: cmd:x%x mb:x%x x%x",
7243 mbx_cmnd, mqe->un.mb_words[0],
7244 mqe->un.mb_words[1]);
7245 }
7246 }
7247 psli->slistat.mbox_cmd++;
7248
7249 /* Post the mailbox command to the port */
7250 rc = lpfc_sli4_mq_put(phba->sli4_hba.mbx_wq, mqe);
7251 if (rc != MBX_SUCCESS) {
7252 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04007253 "(%d):2533 Mailbox command x%x (x%x/x%x) "
James Smartda0436e2009-05-22 14:51:39 -04007254 "cannot issue Data: x%x x%x\n",
7255 mboxq->vport ? mboxq->vport->vpi : 0,
7256 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04007257 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7258 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04007259 psli->sli_flag, MBX_NOWAIT);
7260 goto out_not_finished;
7261 }
7262
7263 return rc;
7264
7265out_not_finished:
7266 spin_lock_irqsave(&phba->hbalock, iflags);
7267 mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
7268 __lpfc_mbox_cmpl_put(phba, mboxq);
7269 /* Release the token */
7270 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
7271 phba->sli.mbox_active = NULL;
7272 spin_unlock_irqrestore(&phba->hbalock, iflags);
7273
7274 return MBX_NOT_FINISHED;
7275}
7276
7277/**
7278 * lpfc_sli_issue_mbox - Wrapper func for issuing mailbox command
7279 * @phba: Pointer to HBA context object.
7280 * @pmbox: Pointer to mailbox object.
7281 * @flag: Flag indicating how the mailbox need to be processed.
7282 *
7283 * This routine wraps the actual SLI3 or SLI4 mailbox issuing routine from
7284 * the API jump table function pointer from the lpfc_hba struct.
7285 *
7286 * Return codes the caller owns the mailbox command after the return of the
7287 * function.
7288 **/
7289int
7290lpfc_sli_issue_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox, uint32_t flag)
7291{
7292 return phba->lpfc_sli_issue_mbox(phba, pmbox, flag);
7293}
7294
7295/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03007296 * lpfc_mbox_api_table_setup - Set up mbox api function jump table
James Smartda0436e2009-05-22 14:51:39 -04007297 * @phba: The hba struct for which this call is being executed.
7298 * @dev_grp: The HBA PCI-Device group number.
7299 *
7300 * This routine sets up the mbox interface API function jump table in @phba
7301 * struct.
7302 * Returns: 0 - success, -ENODEV - failure.
7303 **/
7304int
7305lpfc_mbox_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
7306{
7307
7308 switch (dev_grp) {
7309 case LPFC_PCI_DEV_LP:
7310 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s3;
7311 phba->lpfc_sli_handle_slow_ring_event =
7312 lpfc_sli_handle_slow_ring_event_s3;
7313 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s3;
7314 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s3;
7315 phba->lpfc_sli_brdready = lpfc_sli_brdready_s3;
7316 break;
7317 case LPFC_PCI_DEV_OC:
7318 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s4;
7319 phba->lpfc_sli_handle_slow_ring_event =
7320 lpfc_sli_handle_slow_ring_event_s4;
7321 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s4;
7322 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s4;
7323 phba->lpfc_sli_brdready = lpfc_sli_brdready_s4;
7324 break;
7325 default:
7326 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7327 "1420 Invalid HBA PCI-device group: 0x%x\n",
7328 dev_grp);
7329 return -ENODEV;
7330 break;
7331 }
7332 return 0;
7333}
7334
7335/**
James Smart3621a712009-04-06 18:47:14 -04007336 * __lpfc_sli_ringtx_put - Add an iocb to the txq
James Smarte59058c2008-08-24 21:49:00 -04007337 * @phba: Pointer to HBA context object.
7338 * @pring: Pointer to driver SLI ring object.
7339 * @piocb: Pointer to address of newly added command iocb.
7340 *
7341 * This function is called with hbalock held to add a command
7342 * iocb to the txq when SLI layer cannot submit the command iocb
7343 * to the ring.
7344 **/
James Smart2a9bf3d2010-06-07 15:24:45 -04007345void
James Smart92d7f7b2007-06-17 19:56:38 -05007346__lpfc_sli_ringtx_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -05007347 struct lpfc_iocbq *piocb)
dea31012005-04-17 16:05:31 -05007348{
7349 /* Insert the caller's iocb in the txq tail for later processing. */
7350 list_add_tail(&piocb->list, &pring->txq);
7351 pring->txq_cnt++;
dea31012005-04-17 16:05:31 -05007352}
7353
James Smarte59058c2008-08-24 21:49:00 -04007354/**
James Smart3621a712009-04-06 18:47:14 -04007355 * lpfc_sli_next_iocb - Get the next iocb in the txq
James Smarte59058c2008-08-24 21:49:00 -04007356 * @phba: Pointer to HBA context object.
7357 * @pring: Pointer to driver SLI ring object.
7358 * @piocb: Pointer to address of newly added command iocb.
7359 *
7360 * This function is called with hbalock held before a new
7361 * iocb is submitted to the firmware. This function checks
7362 * txq to flush the iocbs in txq to Firmware before
7363 * submitting new iocbs to the Firmware.
7364 * If there are iocbs in the txq which need to be submitted
7365 * to firmware, lpfc_sli_next_iocb returns the first element
7366 * of the txq after dequeuing it from txq.
7367 * If there is no iocb in the txq then the function will return
7368 * *piocb and *piocb is set to NULL. Caller needs to check
7369 * *piocb to find if there are more commands in the txq.
7370 **/
dea31012005-04-17 16:05:31 -05007371static struct lpfc_iocbq *
7372lpfc_sli_next_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -05007373 struct lpfc_iocbq **piocb)
dea31012005-04-17 16:05:31 -05007374{
7375 struct lpfc_iocbq * nextiocb;
7376
7377 nextiocb = lpfc_sli_ringtx_get(phba, pring);
7378 if (!nextiocb) {
7379 nextiocb = *piocb;
7380 *piocb = NULL;
7381 }
7382
7383 return nextiocb;
7384}
7385
James Smarte59058c2008-08-24 21:49:00 -04007386/**
James Smart3772a992009-05-22 14:50:54 -04007387 * __lpfc_sli_issue_iocb_s3 - SLI3 device lockless ver of lpfc_sli_issue_iocb
James Smarte59058c2008-08-24 21:49:00 -04007388 * @phba: Pointer to HBA context object.
James Smart3772a992009-05-22 14:50:54 -04007389 * @ring_number: SLI ring number to issue iocb on.
James Smarte59058c2008-08-24 21:49:00 -04007390 * @piocb: Pointer to command iocb.
7391 * @flag: Flag indicating if this command can be put into txq.
7392 *
James Smart3772a992009-05-22 14:50:54 -04007393 * __lpfc_sli_issue_iocb_s3 is used by other functions in the driver to issue
7394 * an iocb command to an HBA with SLI-3 interface spec. If the PCI slot is
7395 * recovering from error state, if HBA is resetting or if LPFC_STOP_IOCB_EVENT
7396 * flag is turned on, the function returns IOCB_ERROR. When the link is down,
7397 * this function allows only iocbs for posting buffers. This function finds
7398 * next available slot in the command ring and posts the command to the
7399 * available slot and writes the port attention register to request HBA start
7400 * processing new iocb. If there is no slot available in the ring and
7401 * flag & SLI_IOCB_RET_IOCB is set, the new iocb is added to the txq, otherwise
7402 * the function returns IOCB_BUSY.
James Smarte59058c2008-08-24 21:49:00 -04007403 *
James Smart3772a992009-05-22 14:50:54 -04007404 * This function is called with hbalock held. The function will return success
7405 * after it successfully submit the iocb to firmware or after adding to the
7406 * txq.
James Smarte59058c2008-08-24 21:49:00 -04007407 **/
James Smart98c9ea52007-10-27 13:37:33 -04007408static int
James Smart3772a992009-05-22 14:50:54 -04007409__lpfc_sli_issue_iocb_s3(struct lpfc_hba *phba, uint32_t ring_number,
dea31012005-04-17 16:05:31 -05007410 struct lpfc_iocbq *piocb, uint32_t flag)
7411{
7412 struct lpfc_iocbq *nextiocb;
7413 IOCB_t *iocb;
James Smart3772a992009-05-22 14:50:54 -04007414 struct lpfc_sli_ring *pring = &phba->sli.ring[ring_number];
dea31012005-04-17 16:05:31 -05007415
James Smart92d7f7b2007-06-17 19:56:38 -05007416 if (piocb->iocb_cmpl && (!piocb->vport) &&
7417 (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
7418 (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
7419 lpfc_printf_log(phba, KERN_ERR,
7420 LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04007421 "1807 IOCB x%x failed. No vport\n",
James Smart92d7f7b2007-06-17 19:56:38 -05007422 piocb->iocb.ulpCommand);
7423 dump_stack();
7424 return IOCB_ERROR;
7425 }
7426
7427
Linas Vepstas8d63f372007-02-14 14:28:36 -06007428 /* If the PCI channel is in offline state, do not post iocbs. */
7429 if (unlikely(pci_channel_offline(phba->pcidev)))
7430 return IOCB_ERROR;
7431
James Smarta257bf92009-04-06 18:48:10 -04007432 /* If HBA has a deferred error attention, fail the iocb. */
7433 if (unlikely(phba->hba_flag & DEFER_ERATT))
7434 return IOCB_ERROR;
7435
dea31012005-04-17 16:05:31 -05007436 /*
7437 * We should never get an IOCB if we are in a < LINK_DOWN state
7438 */
James Smart2e0fef82007-06-17 19:56:36 -05007439 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
dea31012005-04-17 16:05:31 -05007440 return IOCB_ERROR;
7441
7442 /*
7443 * Check to see if we are blocking IOCB processing because of a
James Smart0b727fe2007-10-27 13:37:25 -04007444 * outstanding event.
dea31012005-04-17 16:05:31 -05007445 */
James Smart0b727fe2007-10-27 13:37:25 -04007446 if (unlikely(pring->flag & LPFC_STOP_IOCB_EVENT))
dea31012005-04-17 16:05:31 -05007447 goto iocb_busy;
7448
James Smart2e0fef82007-06-17 19:56:36 -05007449 if (unlikely(phba->link_state == LPFC_LINK_DOWN)) {
dea31012005-04-17 16:05:31 -05007450 /*
James Smart2680eea2007-04-25 09:52:55 -04007451 * Only CREATE_XRI, CLOSE_XRI, and QUE_RING_BUF
dea31012005-04-17 16:05:31 -05007452 * can be issued if the link is not up.
7453 */
7454 switch (piocb->iocb.ulpCommand) {
James Smart84774a42008-08-24 21:50:06 -04007455 case CMD_GEN_REQUEST64_CR:
7456 case CMD_GEN_REQUEST64_CX:
7457 if (!(phba->sli.sli_flag & LPFC_MENLO_MAINT) ||
7458 (piocb->iocb.un.genreq64.w5.hcsw.Rctl !=
James Smart6a9c52c2009-10-02 15:16:51 -04007459 FC_RCTL_DD_UNSOL_CMD) ||
James Smart84774a42008-08-24 21:50:06 -04007460 (piocb->iocb.un.genreq64.w5.hcsw.Type !=
7461 MENLO_TRANSPORT_TYPE))
7462
7463 goto iocb_busy;
7464 break;
dea31012005-04-17 16:05:31 -05007465 case CMD_QUE_RING_BUF_CN:
7466 case CMD_QUE_RING_BUF64_CN:
dea31012005-04-17 16:05:31 -05007467 /*
7468 * For IOCBs, like QUE_RING_BUF, that have no rsp ring
7469 * completion, iocb_cmpl MUST be 0.
7470 */
7471 if (piocb->iocb_cmpl)
7472 piocb->iocb_cmpl = NULL;
7473 /*FALLTHROUGH*/
7474 case CMD_CREATE_XRI_CR:
James Smart2680eea2007-04-25 09:52:55 -04007475 case CMD_CLOSE_XRI_CN:
7476 case CMD_CLOSE_XRI_CX:
dea31012005-04-17 16:05:31 -05007477 break;
7478 default:
7479 goto iocb_busy;
7480 }
7481
7482 /*
7483 * For FCP commands, we must be in a state where we can process link
7484 * attention events.
7485 */
7486 } else if (unlikely(pring->ringno == phba->sli.fcp_ring &&
James Smart92d7f7b2007-06-17 19:56:38 -05007487 !(phba->sli.sli_flag & LPFC_PROCESS_LA))) {
dea31012005-04-17 16:05:31 -05007488 goto iocb_busy;
James Smart92d7f7b2007-06-17 19:56:38 -05007489 }
dea31012005-04-17 16:05:31 -05007490
dea31012005-04-17 16:05:31 -05007491 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
7492 (nextiocb = lpfc_sli_next_iocb(phba, pring, &piocb)))
7493 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
7494
7495 if (iocb)
7496 lpfc_sli_update_ring(phba, pring);
7497 else
7498 lpfc_sli_update_full_ring(phba, pring);
7499
7500 if (!piocb)
7501 return IOCB_SUCCESS;
7502
7503 goto out_busy;
7504
7505 iocb_busy:
7506 pring->stats.iocb_cmd_delay++;
7507
7508 out_busy:
7509
7510 if (!(flag & SLI_IOCB_RET_IOCB)) {
James Smart92d7f7b2007-06-17 19:56:38 -05007511 __lpfc_sli_ringtx_put(phba, pring, piocb);
dea31012005-04-17 16:05:31 -05007512 return IOCB_SUCCESS;
7513 }
7514
7515 return IOCB_BUSY;
7516}
7517
James Smart3772a992009-05-22 14:50:54 -04007518/**
James Smart4f774512009-05-22 14:52:35 -04007519 * lpfc_sli4_bpl2sgl - Convert the bpl/bde to a sgl.
7520 * @phba: Pointer to HBA context object.
7521 * @piocb: Pointer to command iocb.
7522 * @sglq: Pointer to the scatter gather queue object.
7523 *
7524 * This routine converts the bpl or bde that is in the IOCB
7525 * to a sgl list for the sli4 hardware. The physical address
7526 * of the bpl/bde is converted back to a virtual address.
7527 * If the IOCB contains a BPL then the list of BDE's is
7528 * converted to sli4_sge's. If the IOCB contains a single
7529 * BDE then it is converted to a single sli_sge.
7530 * The IOCB is still in cpu endianess so the contents of
7531 * the bpl can be used without byte swapping.
7532 *
7533 * Returns valid XRI = Success, NO_XRI = Failure.
7534**/
7535static uint16_t
7536lpfc_sli4_bpl2sgl(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq,
7537 struct lpfc_sglq *sglq)
7538{
7539 uint16_t xritag = NO_XRI;
7540 struct ulp_bde64 *bpl = NULL;
7541 struct ulp_bde64 bde;
7542 struct sli4_sge *sgl = NULL;
7543 IOCB_t *icmd;
7544 int numBdes = 0;
7545 int i = 0;
James Smart63e801c2010-11-20 23:14:19 -05007546 uint32_t offset = 0; /* accumulated offset in the sg request list */
7547 int inbound = 0; /* number of sg reply entries inbound from firmware */
James Smart4f774512009-05-22 14:52:35 -04007548
7549 if (!piocbq || !sglq)
7550 return xritag;
7551
7552 sgl = (struct sli4_sge *)sglq->sgl;
7553 icmd = &piocbq->iocb;
7554 if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
7555 numBdes = icmd->un.genreq64.bdl.bdeSize /
7556 sizeof(struct ulp_bde64);
7557 /* The addrHigh and addrLow fields within the IOCB
7558 * have not been byteswapped yet so there is no
7559 * need to swap them back.
7560 */
7561 bpl = (struct ulp_bde64 *)
7562 ((struct lpfc_dmabuf *)piocbq->context3)->virt;
7563
7564 if (!bpl)
7565 return xritag;
7566
7567 for (i = 0; i < numBdes; i++) {
7568 /* Should already be byte swapped. */
James Smart28baac72010-02-12 14:42:03 -05007569 sgl->addr_hi = bpl->addrHigh;
7570 sgl->addr_lo = bpl->addrLow;
7571
James Smart05580562011-05-24 11:40:48 -04007572 sgl->word2 = le32_to_cpu(sgl->word2);
James Smart4f774512009-05-22 14:52:35 -04007573 if ((i+1) == numBdes)
7574 bf_set(lpfc_sli4_sge_last, sgl, 1);
7575 else
7576 bf_set(lpfc_sli4_sge_last, sgl, 0);
James Smart28baac72010-02-12 14:42:03 -05007577 /* swap the size field back to the cpu so we
7578 * can assign it to the sgl.
7579 */
7580 bde.tus.w = le32_to_cpu(bpl->tus.w);
7581 sgl->sge_len = cpu_to_le32(bde.tus.f.bdeSize);
James Smart63e801c2010-11-20 23:14:19 -05007582 /* The offsets in the sgl need to be accumulated
7583 * separately for the request and reply lists.
7584 * The request is always first, the reply follows.
7585 */
7586 if (piocbq->iocb.ulpCommand == CMD_GEN_REQUEST64_CR) {
7587 /* add up the reply sg entries */
7588 if (bpl->tus.f.bdeFlags == BUFF_TYPE_BDE_64I)
7589 inbound++;
7590 /* first inbound? reset the offset */
7591 if (inbound == 1)
7592 offset = 0;
7593 bf_set(lpfc_sli4_sge_offset, sgl, offset);
James Smartf9bb2da2011-10-10 21:34:11 -04007594 bf_set(lpfc_sli4_sge_type, sgl,
7595 LPFC_SGE_TYPE_DATA);
James Smart63e801c2010-11-20 23:14:19 -05007596 offset += bde.tus.f.bdeSize;
7597 }
James Smart546fc852011-03-11 16:06:29 -05007598 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart4f774512009-05-22 14:52:35 -04007599 bpl++;
7600 sgl++;
7601 }
7602 } else if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BDE_64) {
7603 /* The addrHigh and addrLow fields of the BDE have not
7604 * been byteswapped yet so they need to be swapped
7605 * before putting them in the sgl.
7606 */
7607 sgl->addr_hi =
7608 cpu_to_le32(icmd->un.genreq64.bdl.addrHigh);
7609 sgl->addr_lo =
7610 cpu_to_le32(icmd->un.genreq64.bdl.addrLow);
James Smart05580562011-05-24 11:40:48 -04007611 sgl->word2 = le32_to_cpu(sgl->word2);
James Smart4f774512009-05-22 14:52:35 -04007612 bf_set(lpfc_sli4_sge_last, sgl, 1);
7613 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart28baac72010-02-12 14:42:03 -05007614 sgl->sge_len =
7615 cpu_to_le32(icmd->un.genreq64.bdl.bdeSize);
James Smart4f774512009-05-22 14:52:35 -04007616 }
7617 return sglq->sli4_xritag;
7618}
7619
7620/**
7621 * lpfc_sli4_scmd_to_wqidx_distr - scsi command to SLI4 WQ index distribution
7622 * @phba: Pointer to HBA context object.
James Smart4f774512009-05-22 14:52:35 -04007623 *
James Smarta93ff372010-10-22 11:06:08 -04007624 * This routine performs a roundrobin SCSI command to SLI4 FCP WQ index
James Smart8fa38512009-07-19 10:01:03 -04007625 * distribution. This is called by __lpfc_sli_issue_iocb_s4() with the hbalock
7626 * held.
James Smart4f774512009-05-22 14:52:35 -04007627 *
7628 * Return: index into SLI4 fast-path FCP queue index.
7629 **/
7630static uint32_t
James Smart8fa38512009-07-19 10:01:03 -04007631lpfc_sli4_scmd_to_wqidx_distr(struct lpfc_hba *phba)
James Smart4f774512009-05-22 14:52:35 -04007632{
James Smart8fa38512009-07-19 10:01:03 -04007633 ++phba->fcp_qidx;
7634 if (phba->fcp_qidx >= phba->cfg_fcp_wq_count)
7635 phba->fcp_qidx = 0;
James Smart4f774512009-05-22 14:52:35 -04007636
James Smart8fa38512009-07-19 10:01:03 -04007637 return phba->fcp_qidx;
James Smart4f774512009-05-22 14:52:35 -04007638}
7639
7640/**
7641 * lpfc_sli_iocb2wqe - Convert the IOCB to a work queue entry.
7642 * @phba: Pointer to HBA context object.
7643 * @piocb: Pointer to command iocb.
7644 * @wqe: Pointer to the work queue entry.
7645 *
7646 * This routine converts the iocb command to its Work Queue Entry
7647 * equivalent. The wqe pointer should not have any fields set when
7648 * this routine is called because it will memcpy over them.
7649 * This routine does not set the CQ_ID or the WQEC bits in the
7650 * wqe.
7651 *
7652 * Returns: 0 = Success, IOCB_ERROR = Failure.
7653 **/
7654static int
7655lpfc_sli4_iocb2wqe(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq,
7656 union lpfc_wqe *wqe)
7657{
James Smart5ffc2662009-11-18 15:39:44 -05007658 uint32_t xmit_len = 0, total_len = 0;
James Smart4f774512009-05-22 14:52:35 -04007659 uint8_t ct = 0;
7660 uint32_t fip;
7661 uint32_t abort_tag;
7662 uint8_t command_type = ELS_COMMAND_NON_FIP;
7663 uint8_t cmnd;
7664 uint16_t xritag;
James Smartdcf2a4e2010-09-29 11:18:53 -04007665 uint16_t abrt_iotag;
7666 struct lpfc_iocbq *abrtiocbq;
James Smart4f774512009-05-22 14:52:35 -04007667 struct ulp_bde64 *bpl = NULL;
James Smartf0d9bcc2010-10-22 11:07:09 -04007668 uint32_t els_id = LPFC_ELS_ID_DEFAULT;
James Smart5ffc2662009-11-18 15:39:44 -05007669 int numBdes, i;
7670 struct ulp_bde64 bde;
James Smartc31098c2011-04-16 11:03:33 -04007671 struct lpfc_nodelist *ndlp;
James Smartff78d8f2011-12-13 13:21:35 -05007672 uint32_t *pcmd;
James Smart4f774512009-05-22 14:52:35 -04007673
James Smart45ed1192009-10-02 15:17:02 -04007674 fip = phba->hba_flag & HBA_FIP_SUPPORT;
James Smart4f774512009-05-22 14:52:35 -04007675 /* The fcp commands will set command type */
James Smart0c287582009-06-10 17:22:56 -04007676 if (iocbq->iocb_flag & LPFC_IO_FCP)
James Smart4f774512009-05-22 14:52:35 -04007677 command_type = FCP_COMMAND;
James Smartc8685952009-11-18 15:39:16 -05007678 else if (fip && (iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK))
James Smart0c287582009-06-10 17:22:56 -04007679 command_type = ELS_COMMAND_FIP;
7680 else
7681 command_type = ELS_COMMAND_NON_FIP;
7682
James Smart4f774512009-05-22 14:52:35 -04007683 /* Some of the fields are in the right position already */
7684 memcpy(wqe, &iocbq->iocb, sizeof(union lpfc_wqe));
7685 abort_tag = (uint32_t) iocbq->iotag;
7686 xritag = iocbq->sli4_xritag;
James Smartf0d9bcc2010-10-22 11:07:09 -04007687 wqe->generic.wqe_com.word7 = 0; /* The ct field has moved so reset */
James Smart4f774512009-05-22 14:52:35 -04007688 /* words0-2 bpl convert bde */
7689 if (iocbq->iocb.un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
James Smart5ffc2662009-11-18 15:39:44 -05007690 numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
7691 sizeof(struct ulp_bde64);
James Smart4f774512009-05-22 14:52:35 -04007692 bpl = (struct ulp_bde64 *)
7693 ((struct lpfc_dmabuf *)iocbq->context3)->virt;
7694 if (!bpl)
7695 return IOCB_ERROR;
7696
7697 /* Should already be byte swapped. */
7698 wqe->generic.bde.addrHigh = le32_to_cpu(bpl->addrHigh);
7699 wqe->generic.bde.addrLow = le32_to_cpu(bpl->addrLow);
7700 /* swap the size field back to the cpu so we
7701 * can assign it to the sgl.
7702 */
7703 wqe->generic.bde.tus.w = le32_to_cpu(bpl->tus.w);
James Smart5ffc2662009-11-18 15:39:44 -05007704 xmit_len = wqe->generic.bde.tus.f.bdeSize;
7705 total_len = 0;
7706 for (i = 0; i < numBdes; i++) {
7707 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
7708 total_len += bde.tus.f.bdeSize;
7709 }
James Smart4f774512009-05-22 14:52:35 -04007710 } else
James Smart5ffc2662009-11-18 15:39:44 -05007711 xmit_len = iocbq->iocb.un.fcpi64.bdl.bdeSize;
James Smart4f774512009-05-22 14:52:35 -04007712
7713 iocbq->iocb.ulpIoTag = iocbq->iotag;
7714 cmnd = iocbq->iocb.ulpCommand;
7715
7716 switch (iocbq->iocb.ulpCommand) {
7717 case CMD_ELS_REQUEST64_CR:
James Smartc31098c2011-04-16 11:03:33 -04007718 ndlp = (struct lpfc_nodelist *)iocbq->context1;
James Smart4f774512009-05-22 14:52:35 -04007719 if (!iocbq->iocb.ulpLe) {
7720 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
7721 "2007 Only Limited Edition cmd Format"
7722 " supported 0x%x\n",
7723 iocbq->iocb.ulpCommand);
7724 return IOCB_ERROR;
7725 }
James Smartff78d8f2011-12-13 13:21:35 -05007726
James Smart5ffc2662009-11-18 15:39:44 -05007727 wqe->els_req.payload_len = xmit_len;
James Smart4f774512009-05-22 14:52:35 -04007728 /* Els_reguest64 has a TMO */
7729 bf_set(wqe_tmo, &wqe->els_req.wqe_com,
7730 iocbq->iocb.ulpTimeout);
7731 /* Need a VF for word 4 set the vf bit*/
7732 bf_set(els_req64_vf, &wqe->els_req, 0);
7733 /* And a VFID for word 12 */
7734 bf_set(els_req64_vfid, &wqe->els_req, 0);
James Smart4f774512009-05-22 14:52:35 -04007735 ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
James Smartf0d9bcc2010-10-22 11:07:09 -04007736 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
7737 iocbq->iocb.ulpContext);
7738 bf_set(wqe_ct, &wqe->els_req.wqe_com, ct);
7739 bf_set(wqe_pu, &wqe->els_req.wqe_com, 0);
James Smart4f774512009-05-22 14:52:35 -04007740 /* CCP CCPE PV PRI in word10 were set in the memcpy */
James Smartff78d8f2011-12-13 13:21:35 -05007741 if (command_type == ELS_COMMAND_FIP)
James Smartc8685952009-11-18 15:39:16 -05007742 els_id = ((iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK)
7743 >> LPFC_FIP_ELS_ID_SHIFT);
James Smartff78d8f2011-12-13 13:21:35 -05007744 pcmd = (uint32_t *) (((struct lpfc_dmabuf *)
7745 iocbq->context2)->virt);
7746 if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
7747 if (pcmd && (*pcmd == ELS_CMD_FLOGI ||
James Smartcb69f7d2011-12-13 13:21:57 -05007748 *pcmd == ELS_CMD_SCR ||
James Smartff78d8f2011-12-13 13:21:35 -05007749 *pcmd == ELS_CMD_PLOGI)) {
7750 bf_set(els_req64_sp, &wqe->els_req, 1);
7751 bf_set(els_req64_sid, &wqe->els_req,
7752 iocbq->vport->fc_myDID);
7753 bf_set(wqe_ct, &wqe->els_req.wqe_com, 1);
7754 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
7755 phba->vpi_ids[phba->pport->vpi]);
7756 } else if (iocbq->context1) {
7757 bf_set(wqe_ct, &wqe->els_req.wqe_com, 0);
7758 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
7759 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
7760 }
James Smartc8685952009-11-18 15:39:16 -05007761 }
James Smart6d368e52011-05-24 11:44:12 -04007762 bf_set(wqe_temp_rpi, &wqe->els_req.wqe_com,
7763 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
James Smartf0d9bcc2010-10-22 11:07:09 -04007764 bf_set(wqe_els_id, &wqe->els_req.wqe_com, els_id);
7765 bf_set(wqe_dbde, &wqe->els_req.wqe_com, 1);
7766 bf_set(wqe_iod, &wqe->els_req.wqe_com, LPFC_WQE_IOD_READ);
7767 bf_set(wqe_qosd, &wqe->els_req.wqe_com, 1);
7768 bf_set(wqe_lenloc, &wqe->els_req.wqe_com, LPFC_WQE_LENLOC_NONE);
7769 bf_set(wqe_ebde_cnt, &wqe->els_req.wqe_com, 0);
James Smart7851fe22011-07-22 18:36:52 -04007770 break;
James Smart5ffc2662009-11-18 15:39:44 -05007771 case CMD_XMIT_SEQUENCE64_CX:
James Smartf0d9bcc2010-10-22 11:07:09 -04007772 bf_set(wqe_ctxt_tag, &wqe->xmit_sequence.wqe_com,
7773 iocbq->iocb.un.ulpWord[3]);
7774 bf_set(wqe_rcvoxid, &wqe->xmit_sequence.wqe_com,
James Smart7851fe22011-07-22 18:36:52 -04007775 iocbq->iocb.unsli3.rcvsli3.ox_id);
James Smart5ffc2662009-11-18 15:39:44 -05007776 /* The entire sequence is transmitted for this IOCB */
7777 xmit_len = total_len;
7778 cmnd = CMD_XMIT_SEQUENCE64_CR;
James Smart4f774512009-05-22 14:52:35 -04007779 case CMD_XMIT_SEQUENCE64_CR:
James Smartf0d9bcc2010-10-22 11:07:09 -04007780 /* word3 iocb=io_tag32 wqe=reserved */
7781 wqe->xmit_sequence.rsvd3 = 0;
James Smart4f774512009-05-22 14:52:35 -04007782 /* word4 relative_offset memcpy */
7783 /* word5 r_ctl/df_ctl memcpy */
James Smartf0d9bcc2010-10-22 11:07:09 -04007784 bf_set(wqe_pu, &wqe->xmit_sequence.wqe_com, 0);
7785 bf_set(wqe_dbde, &wqe->xmit_sequence.wqe_com, 1);
7786 bf_set(wqe_iod, &wqe->xmit_sequence.wqe_com,
7787 LPFC_WQE_IOD_WRITE);
7788 bf_set(wqe_lenloc, &wqe->xmit_sequence.wqe_com,
7789 LPFC_WQE_LENLOC_WORD12);
7790 bf_set(wqe_ebde_cnt, &wqe->xmit_sequence.wqe_com, 0);
James Smart5ffc2662009-11-18 15:39:44 -05007791 wqe->xmit_sequence.xmit_len = xmit_len;
7792 command_type = OTHER_COMMAND;
James Smart7851fe22011-07-22 18:36:52 -04007793 break;
James Smart4f774512009-05-22 14:52:35 -04007794 case CMD_XMIT_BCAST64_CN:
James Smartf0d9bcc2010-10-22 11:07:09 -04007795 /* word3 iocb=iotag32 wqe=seq_payload_len */
7796 wqe->xmit_bcast64.seq_payload_len = xmit_len;
James Smart4f774512009-05-22 14:52:35 -04007797 /* word4 iocb=rsvd wqe=rsvd */
7798 /* word5 iocb=rctl/type/df_ctl wqe=rctl/type/df_ctl memcpy */
7799 /* word6 iocb=ctxt_tag/io_tag wqe=ctxt_tag/xri */
James Smartf0d9bcc2010-10-22 11:07:09 -04007800 bf_set(wqe_ct, &wqe->xmit_bcast64.wqe_com,
James Smart4f774512009-05-22 14:52:35 -04007801 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
James Smartf0d9bcc2010-10-22 11:07:09 -04007802 bf_set(wqe_dbde, &wqe->xmit_bcast64.wqe_com, 1);
7803 bf_set(wqe_iod, &wqe->xmit_bcast64.wqe_com, LPFC_WQE_IOD_WRITE);
7804 bf_set(wqe_lenloc, &wqe->xmit_bcast64.wqe_com,
7805 LPFC_WQE_LENLOC_WORD3);
7806 bf_set(wqe_ebde_cnt, &wqe->xmit_bcast64.wqe_com, 0);
James Smart7851fe22011-07-22 18:36:52 -04007807 break;
James Smart4f774512009-05-22 14:52:35 -04007808 case CMD_FCP_IWRITE64_CR:
7809 command_type = FCP_COMMAND_DATA_OUT;
James Smartf0d9bcc2010-10-22 11:07:09 -04007810 /* word3 iocb=iotag wqe=payload_offset_len */
7811 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
7812 wqe->fcp_iwrite.payload_offset_len =
James Smart5ffc2662009-11-18 15:39:44 -05007813 xmit_len + sizeof(struct fcp_rsp);
James Smartf0d9bcc2010-10-22 11:07:09 -04007814 /* word4 iocb=parameter wqe=total_xfer_length memcpy */
7815 /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
7816 bf_set(wqe_erp, &wqe->fcp_iwrite.wqe_com,
7817 iocbq->iocb.ulpFCP2Rcvy);
7818 bf_set(wqe_lnk, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpXS);
7819 /* Always open the exchange */
7820 bf_set(wqe_xc, &wqe->fcp_iwrite.wqe_com, 0);
7821 bf_set(wqe_dbde, &wqe->fcp_iwrite.wqe_com, 1);
7822 bf_set(wqe_iod, &wqe->fcp_iwrite.wqe_com, LPFC_WQE_IOD_WRITE);
7823 bf_set(wqe_lenloc, &wqe->fcp_iwrite.wqe_com,
7824 LPFC_WQE_LENLOC_WORD4);
7825 bf_set(wqe_ebde_cnt, &wqe->fcp_iwrite.wqe_com, 0);
7826 bf_set(wqe_pu, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpPU);
James Smart7851fe22011-07-22 18:36:52 -04007827 break;
James Smartf0d9bcc2010-10-22 11:07:09 -04007828 case CMD_FCP_IREAD64_CR:
7829 /* word3 iocb=iotag wqe=payload_offset_len */
7830 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
7831 wqe->fcp_iread.payload_offset_len =
7832 xmit_len + sizeof(struct fcp_rsp);
7833 /* word4 iocb=parameter wqe=total_xfer_length memcpy */
7834 /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
7835 bf_set(wqe_erp, &wqe->fcp_iread.wqe_com,
7836 iocbq->iocb.ulpFCP2Rcvy);
7837 bf_set(wqe_lnk, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpXS);
James Smart4f774512009-05-22 14:52:35 -04007838 /* Always open the exchange */
7839 bf_set(wqe_xc, &wqe->fcp_iread.wqe_com, 0);
James Smartf0d9bcc2010-10-22 11:07:09 -04007840 bf_set(wqe_dbde, &wqe->fcp_iread.wqe_com, 1);
7841 bf_set(wqe_iod, &wqe->fcp_iread.wqe_com, LPFC_WQE_IOD_READ);
7842 bf_set(wqe_lenloc, &wqe->fcp_iread.wqe_com,
7843 LPFC_WQE_LENLOC_WORD4);
7844 bf_set(wqe_ebde_cnt, &wqe->fcp_iread.wqe_com, 0);
7845 bf_set(wqe_pu, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpPU);
James Smart7851fe22011-07-22 18:36:52 -04007846 break;
James Smartf1126682009-06-10 17:22:44 -04007847 case CMD_FCP_ICMND64_CR:
James Smartf0d9bcc2010-10-22 11:07:09 -04007848 /* word3 iocb=IO_TAG wqe=reserved */
7849 wqe->fcp_icmd.rsrvd3 = 0;
7850 bf_set(wqe_pu, &wqe->fcp_icmd.wqe_com, 0);
James Smartf1126682009-06-10 17:22:44 -04007851 /* Always open the exchange */
James Smartf0d9bcc2010-10-22 11:07:09 -04007852 bf_set(wqe_xc, &wqe->fcp_icmd.wqe_com, 0);
7853 bf_set(wqe_dbde, &wqe->fcp_icmd.wqe_com, 1);
7854 bf_set(wqe_iod, &wqe->fcp_icmd.wqe_com, LPFC_WQE_IOD_WRITE);
7855 bf_set(wqe_qosd, &wqe->fcp_icmd.wqe_com, 1);
7856 bf_set(wqe_lenloc, &wqe->fcp_icmd.wqe_com,
7857 LPFC_WQE_LENLOC_NONE);
7858 bf_set(wqe_ebde_cnt, &wqe->fcp_icmd.wqe_com, 0);
James Smart7851fe22011-07-22 18:36:52 -04007859 break;
James Smart4f774512009-05-22 14:52:35 -04007860 case CMD_GEN_REQUEST64_CR:
James Smart63e801c2010-11-20 23:14:19 -05007861 /* For this command calculate the xmit length of the
7862 * request bde.
7863 */
7864 xmit_len = 0;
7865 numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
7866 sizeof(struct ulp_bde64);
7867 for (i = 0; i < numBdes; i++) {
James Smart63e801c2010-11-20 23:14:19 -05007868 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
James Smart546fc852011-03-11 16:06:29 -05007869 if (bde.tus.f.bdeFlags != BUFF_TYPE_BDE_64)
7870 break;
James Smart63e801c2010-11-20 23:14:19 -05007871 xmit_len += bde.tus.f.bdeSize;
7872 }
James Smartf0d9bcc2010-10-22 11:07:09 -04007873 /* word3 iocb=IO_TAG wqe=request_payload_len */
7874 wqe->gen_req.request_payload_len = xmit_len;
7875 /* word4 iocb=parameter wqe=relative_offset memcpy */
7876 /* word5 [rctl, type, df_ctl, la] copied in memcpy */
James Smart4f774512009-05-22 14:52:35 -04007877 /* word6 context tag copied in memcpy */
7878 if (iocbq->iocb.ulpCt_h || iocbq->iocb.ulpCt_l) {
7879 ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
7880 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
7881 "2015 Invalid CT %x command 0x%x\n",
7882 ct, iocbq->iocb.ulpCommand);
7883 return IOCB_ERROR;
7884 }
James Smartf0d9bcc2010-10-22 11:07:09 -04007885 bf_set(wqe_ct, &wqe->gen_req.wqe_com, 0);
7886 bf_set(wqe_tmo, &wqe->gen_req.wqe_com, iocbq->iocb.ulpTimeout);
7887 bf_set(wqe_pu, &wqe->gen_req.wqe_com, iocbq->iocb.ulpPU);
7888 bf_set(wqe_dbde, &wqe->gen_req.wqe_com, 1);
7889 bf_set(wqe_iod, &wqe->gen_req.wqe_com, LPFC_WQE_IOD_READ);
7890 bf_set(wqe_qosd, &wqe->gen_req.wqe_com, 1);
7891 bf_set(wqe_lenloc, &wqe->gen_req.wqe_com, LPFC_WQE_LENLOC_NONE);
7892 bf_set(wqe_ebde_cnt, &wqe->gen_req.wqe_com, 0);
James Smart4f774512009-05-22 14:52:35 -04007893 command_type = OTHER_COMMAND;
James Smart7851fe22011-07-22 18:36:52 -04007894 break;
James Smart4f774512009-05-22 14:52:35 -04007895 case CMD_XMIT_ELS_RSP64_CX:
James Smartc31098c2011-04-16 11:03:33 -04007896 ndlp = (struct lpfc_nodelist *)iocbq->context1;
James Smart4f774512009-05-22 14:52:35 -04007897 /* words0-2 BDE memcpy */
James Smartf0d9bcc2010-10-22 11:07:09 -04007898 /* word3 iocb=iotag32 wqe=response_payload_len */
7899 wqe->xmit_els_rsp.response_payload_len = xmit_len;
James Smart4f774512009-05-22 14:52:35 -04007900 /* word4 iocb=did wge=rsvd. */
James Smartf0d9bcc2010-10-22 11:07:09 -04007901 wqe->xmit_els_rsp.rsvd4 = 0;
James Smart4f774512009-05-22 14:52:35 -04007902 /* word5 iocb=rsvd wge=did */
7903 bf_set(wqe_els_did, &wqe->xmit_els_rsp.wqe_dest,
7904 iocbq->iocb.un.elsreq64.remoteID);
James Smartf0d9bcc2010-10-22 11:07:09 -04007905 bf_set(wqe_ct, &wqe->xmit_els_rsp.wqe_com,
7906 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
7907 bf_set(wqe_pu, &wqe->xmit_els_rsp.wqe_com, iocbq->iocb.ulpPU);
7908 bf_set(wqe_rcvoxid, &wqe->xmit_els_rsp.wqe_com,
James Smart7851fe22011-07-22 18:36:52 -04007909 iocbq->iocb.unsli3.rcvsli3.ox_id);
James Smart4f774512009-05-22 14:52:35 -04007910 if (!iocbq->iocb.ulpCt_h && iocbq->iocb.ulpCt_l)
James Smartf0d9bcc2010-10-22 11:07:09 -04007911 bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
James Smart6d368e52011-05-24 11:44:12 -04007912 phba->vpi_ids[iocbq->vport->vpi]);
James Smartf0d9bcc2010-10-22 11:07:09 -04007913 bf_set(wqe_dbde, &wqe->xmit_els_rsp.wqe_com, 1);
7914 bf_set(wqe_iod, &wqe->xmit_els_rsp.wqe_com, LPFC_WQE_IOD_WRITE);
7915 bf_set(wqe_qosd, &wqe->xmit_els_rsp.wqe_com, 1);
7916 bf_set(wqe_lenloc, &wqe->xmit_els_rsp.wqe_com,
7917 LPFC_WQE_LENLOC_WORD3);
7918 bf_set(wqe_ebde_cnt, &wqe->xmit_els_rsp.wqe_com, 0);
James Smart6d368e52011-05-24 11:44:12 -04007919 bf_set(wqe_rsp_temp_rpi, &wqe->xmit_els_rsp,
7920 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
James Smartff78d8f2011-12-13 13:21:35 -05007921 pcmd = (uint32_t *) (((struct lpfc_dmabuf *)
7922 iocbq->context2)->virt);
7923 if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
7924 bf_set(els_req64_sp, &wqe->els_req, 1);
7925 bf_set(els_req64_sid, &wqe->els_req,
7926 iocbq->vport->fc_myDID);
7927 bf_set(wqe_ct, &wqe->els_req.wqe_com, 1);
7928 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
7929 phba->vpi_ids[phba->pport->vpi]);
7930 }
James Smart4f774512009-05-22 14:52:35 -04007931 command_type = OTHER_COMMAND;
James Smart7851fe22011-07-22 18:36:52 -04007932 break;
James Smart4f774512009-05-22 14:52:35 -04007933 case CMD_CLOSE_XRI_CN:
7934 case CMD_ABORT_XRI_CN:
7935 case CMD_ABORT_XRI_CX:
7936 /* words 0-2 memcpy should be 0 rserved */
7937 /* port will send abts */
James Smartdcf2a4e2010-09-29 11:18:53 -04007938 abrt_iotag = iocbq->iocb.un.acxri.abortContextTag;
7939 if (abrt_iotag != 0 && abrt_iotag <= phba->sli.last_iotag) {
7940 abrtiocbq = phba->sli.iocbq_lookup[abrt_iotag];
7941 fip = abrtiocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK;
7942 } else
7943 fip = 0;
7944
7945 if ((iocbq->iocb.ulpCommand == CMD_CLOSE_XRI_CN) || fip)
James Smart4f774512009-05-22 14:52:35 -04007946 /*
James Smartdcf2a4e2010-09-29 11:18:53 -04007947 * The link is down, or the command was ELS_FIP
7948 * so the fw does not need to send abts
James Smart4f774512009-05-22 14:52:35 -04007949 * on the wire.
7950 */
7951 bf_set(abort_cmd_ia, &wqe->abort_cmd, 1);
7952 else
7953 bf_set(abort_cmd_ia, &wqe->abort_cmd, 0);
7954 bf_set(abort_cmd_criteria, &wqe->abort_cmd, T_XRI_TAG);
James Smartf0d9bcc2010-10-22 11:07:09 -04007955 /* word5 iocb=CONTEXT_TAG|IO_TAG wqe=reserved */
7956 wqe->abort_cmd.rsrvd5 = 0;
7957 bf_set(wqe_ct, &wqe->abort_cmd.wqe_com,
James Smart4f774512009-05-22 14:52:35 -04007958 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
7959 abort_tag = iocbq->iocb.un.acxri.abortIoTag;
James Smart4f774512009-05-22 14:52:35 -04007960 /*
7961 * The abort handler will send us CMD_ABORT_XRI_CN or
7962 * CMD_CLOSE_XRI_CN and the fw only accepts CMD_ABORT_XRI_CX
7963 */
James Smartf0d9bcc2010-10-22 11:07:09 -04007964 bf_set(wqe_cmnd, &wqe->abort_cmd.wqe_com, CMD_ABORT_XRI_CX);
7965 bf_set(wqe_qosd, &wqe->abort_cmd.wqe_com, 1);
7966 bf_set(wqe_lenloc, &wqe->abort_cmd.wqe_com,
7967 LPFC_WQE_LENLOC_NONE);
James Smart4f774512009-05-22 14:52:35 -04007968 cmnd = CMD_ABORT_XRI_CX;
7969 command_type = OTHER_COMMAND;
7970 xritag = 0;
James Smart7851fe22011-07-22 18:36:52 -04007971 break;
James Smart6669f9b2009-10-02 15:16:45 -04007972 case CMD_XMIT_BLS_RSP64_CX:
James Smart546fc852011-03-11 16:06:29 -05007973 /* As BLS ABTS RSP WQE is very different from other WQEs,
James Smart6669f9b2009-10-02 15:16:45 -04007974 * we re-construct this WQE here based on information in
7975 * iocbq from scratch.
7976 */
7977 memset(wqe, 0, sizeof(union lpfc_wqe));
James Smart5ffc2662009-11-18 15:39:44 -05007978 /* OX_ID is invariable to who sent ABTS to CT exchange */
James Smart6669f9b2009-10-02 15:16:45 -04007979 bf_set(xmit_bls_rsp64_oxid, &wqe->xmit_bls_rsp,
James Smart546fc852011-03-11 16:06:29 -05007980 bf_get(lpfc_abts_oxid, &iocbq->iocb.un.bls_rsp));
7981 if (bf_get(lpfc_abts_orig, &iocbq->iocb.un.bls_rsp) ==
James Smart5ffc2662009-11-18 15:39:44 -05007982 LPFC_ABTS_UNSOL_INT) {
7983 /* ABTS sent by initiator to CT exchange, the
7984 * RX_ID field will be filled with the newly
7985 * allocated responder XRI.
7986 */
7987 bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
7988 iocbq->sli4_xritag);
7989 } else {
7990 /* ABTS sent by responder to CT exchange, the
7991 * RX_ID field will be filled with the responder
7992 * RX_ID from ABTS.
7993 */
7994 bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
James Smart546fc852011-03-11 16:06:29 -05007995 bf_get(lpfc_abts_rxid, &iocbq->iocb.un.bls_rsp));
James Smart5ffc2662009-11-18 15:39:44 -05007996 }
James Smart6669f9b2009-10-02 15:16:45 -04007997 bf_set(xmit_bls_rsp64_seqcnthi, &wqe->xmit_bls_rsp, 0xffff);
7998 bf_set(wqe_xmit_bls_pt, &wqe->xmit_bls_rsp.wqe_dest, 0x1);
7999 bf_set(wqe_ctxt_tag, &wqe->xmit_bls_rsp.wqe_com,
8000 iocbq->iocb.ulpContext);
James Smartf0d9bcc2010-10-22 11:07:09 -04008001 bf_set(wqe_qosd, &wqe->xmit_bls_rsp.wqe_com, 1);
8002 bf_set(wqe_lenloc, &wqe->xmit_bls_rsp.wqe_com,
8003 LPFC_WQE_LENLOC_NONE);
James Smart6669f9b2009-10-02 15:16:45 -04008004 /* Overwrite the pre-set comnd type with OTHER_COMMAND */
8005 command_type = OTHER_COMMAND;
James Smart546fc852011-03-11 16:06:29 -05008006 if (iocbq->iocb.un.xseq64.w5.hcsw.Rctl == FC_RCTL_BA_RJT) {
8007 bf_set(xmit_bls_rsp64_rjt_vspec, &wqe->xmit_bls_rsp,
8008 bf_get(lpfc_vndr_code, &iocbq->iocb.un.bls_rsp));
8009 bf_set(xmit_bls_rsp64_rjt_expc, &wqe->xmit_bls_rsp,
8010 bf_get(lpfc_rsn_expln, &iocbq->iocb.un.bls_rsp));
8011 bf_set(xmit_bls_rsp64_rjt_rsnc, &wqe->xmit_bls_rsp,
8012 bf_get(lpfc_rsn_code, &iocbq->iocb.un.bls_rsp));
8013 }
8014
James Smart7851fe22011-07-22 18:36:52 -04008015 break;
James Smart4f774512009-05-22 14:52:35 -04008016 case CMD_XRI_ABORTED_CX:
8017 case CMD_CREATE_XRI_CR: /* Do we expect to use this? */
James Smart4f774512009-05-22 14:52:35 -04008018 case CMD_IOCB_FCP_IBIDIR64_CR: /* bidirectional xfer */
8019 case CMD_FCP_TSEND64_CX: /* Target mode send xfer-ready */
8020 case CMD_FCP_TRSP64_CX: /* Target mode rcv */
8021 case CMD_FCP_AUTO_TRSP_CX: /* Auto target rsp */
8022 default:
8023 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8024 "2014 Invalid command 0x%x\n",
8025 iocbq->iocb.ulpCommand);
8026 return IOCB_ERROR;
James Smart7851fe22011-07-22 18:36:52 -04008027 break;
James Smart4f774512009-05-22 14:52:35 -04008028 }
James Smart6d368e52011-05-24 11:44:12 -04008029
James Smartf0d9bcc2010-10-22 11:07:09 -04008030 bf_set(wqe_xri_tag, &wqe->generic.wqe_com, xritag);
8031 bf_set(wqe_reqtag, &wqe->generic.wqe_com, iocbq->iotag);
8032 wqe->generic.wqe_com.abort_tag = abort_tag;
8033 bf_set(wqe_cmd_type, &wqe->generic.wqe_com, command_type);
8034 bf_set(wqe_cmnd, &wqe->generic.wqe_com, cmnd);
8035 bf_set(wqe_class, &wqe->generic.wqe_com, iocbq->iocb.ulpClass);
8036 bf_set(wqe_cqid, &wqe->generic.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
James Smart4f774512009-05-22 14:52:35 -04008037 return 0;
8038}
8039
8040/**
8041 * __lpfc_sli_issue_iocb_s4 - SLI4 device lockless ver of lpfc_sli_issue_iocb
8042 * @phba: Pointer to HBA context object.
8043 * @ring_number: SLI ring number to issue iocb on.
8044 * @piocb: Pointer to command iocb.
8045 * @flag: Flag indicating if this command can be put into txq.
8046 *
8047 * __lpfc_sli_issue_iocb_s4 is used by other functions in the driver to issue
8048 * an iocb command to an HBA with SLI-4 interface spec.
8049 *
8050 * This function is called with hbalock held. The function will return success
8051 * after it successfully submit the iocb to firmware or after adding to the
8052 * txq.
8053 **/
8054static int
8055__lpfc_sli_issue_iocb_s4(struct lpfc_hba *phba, uint32_t ring_number,
8056 struct lpfc_iocbq *piocb, uint32_t flag)
8057{
8058 struct lpfc_sglq *sglq;
James Smart4f774512009-05-22 14:52:35 -04008059 union lpfc_wqe wqe;
8060 struct lpfc_sli_ring *pring = &phba->sli.ring[ring_number];
James Smart4f774512009-05-22 14:52:35 -04008061
8062 if (piocb->sli4_xritag == NO_XRI) {
8063 if (piocb->iocb.ulpCommand == CMD_ABORT_XRI_CN ||
James Smart546fc852011-03-11 16:06:29 -05008064 piocb->iocb.ulpCommand == CMD_CLOSE_XRI_CN ||
8065 piocb->iocb.ulpCommand == CMD_XMIT_BLS_RSP64_CX)
James Smart4f774512009-05-22 14:52:35 -04008066 sglq = NULL;
8067 else {
James Smart2a9bf3d2010-06-07 15:24:45 -04008068 if (pring->txq_cnt) {
8069 if (!(flag & SLI_IOCB_RET_IOCB)) {
8070 __lpfc_sli_ringtx_put(phba,
8071 pring, piocb);
8072 return IOCB_SUCCESS;
8073 } else {
8074 return IOCB_BUSY;
8075 }
8076 } else {
James Smart6d368e52011-05-24 11:44:12 -04008077 sglq = __lpfc_sli_get_sglq(phba, piocb);
James Smart2a9bf3d2010-06-07 15:24:45 -04008078 if (!sglq) {
8079 if (!(flag & SLI_IOCB_RET_IOCB)) {
8080 __lpfc_sli_ringtx_put(phba,
8081 pring,
8082 piocb);
8083 return IOCB_SUCCESS;
8084 } else
8085 return IOCB_BUSY;
8086 }
8087 }
James Smart4f774512009-05-22 14:52:35 -04008088 }
8089 } else if (piocb->iocb_flag & LPFC_IO_FCP) {
James Smart6d368e52011-05-24 11:44:12 -04008090 /* These IO's already have an XRI and a mapped sgl. */
8091 sglq = NULL;
James Smart4f774512009-05-22 14:52:35 -04008092 } else {
James Smart6d368e52011-05-24 11:44:12 -04008093 /*
8094 * This is a continuation of a commandi,(CX) so this
James Smart4f774512009-05-22 14:52:35 -04008095 * sglq is on the active list
8096 */
8097 sglq = __lpfc_get_active_sglq(phba, piocb->sli4_xritag);
8098 if (!sglq)
8099 return IOCB_ERROR;
8100 }
8101
8102 if (sglq) {
James Smart6d368e52011-05-24 11:44:12 -04008103 piocb->sli4_lxritag = sglq->sli4_lxritag;
James Smart2a9bf3d2010-06-07 15:24:45 -04008104 piocb->sli4_xritag = sglq->sli4_xritag;
James Smart2a9bf3d2010-06-07 15:24:45 -04008105 if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocb, sglq))
James Smart4f774512009-05-22 14:52:35 -04008106 return IOCB_ERROR;
8107 }
8108
8109 if (lpfc_sli4_iocb2wqe(phba, piocb, &wqe))
8110 return IOCB_ERROR;
8111
James Smart341af102010-01-26 23:07:37 -05008112 if ((piocb->iocb_flag & LPFC_IO_FCP) ||
8113 (piocb->iocb_flag & LPFC_USE_FCPWQIDX)) {
James Smart5ffc2662009-11-18 15:39:44 -05008114 /*
8115 * For FCP command IOCB, get a new WQ index to distribute
8116 * WQE across the WQsr. On the other hand, for abort IOCB,
8117 * it carries the same WQ index to the original command
8118 * IOCB.
8119 */
James Smart341af102010-01-26 23:07:37 -05008120 if (piocb->iocb_flag & LPFC_IO_FCP)
James Smart5ffc2662009-11-18 15:39:44 -05008121 piocb->fcp_wqidx = lpfc_sli4_scmd_to_wqidx_distr(phba);
James Smart2e90f4b2011-12-13 13:22:37 -05008122 if (unlikely(!phba->sli4_hba.fcp_wq))
8123 return IOCB_ERROR;
James Smart5ffc2662009-11-18 15:39:44 -05008124 if (lpfc_sli4_wq_put(phba->sli4_hba.fcp_wq[piocb->fcp_wqidx],
8125 &wqe))
James Smart4f774512009-05-22 14:52:35 -04008126 return IOCB_ERROR;
8127 } else {
8128 if (lpfc_sli4_wq_put(phba->sli4_hba.els_wq, &wqe))
8129 return IOCB_ERROR;
8130 }
8131 lpfc_sli_ringtxcmpl_put(phba, pring, piocb);
8132
8133 return 0;
8134}
8135
8136/**
James Smart3772a992009-05-22 14:50:54 -04008137 * __lpfc_sli_issue_iocb - Wrapper func of lockless version for issuing iocb
8138 *
8139 * This routine wraps the actual lockless version for issusing IOCB function
8140 * pointer from the lpfc_hba struct.
8141 *
8142 * Return codes:
8143 * IOCB_ERROR - Error
8144 * IOCB_SUCCESS - Success
8145 * IOCB_BUSY - Busy
8146 **/
James Smart2a9bf3d2010-06-07 15:24:45 -04008147int
James Smart3772a992009-05-22 14:50:54 -04008148__lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
8149 struct lpfc_iocbq *piocb, uint32_t flag)
8150{
8151 return phba->__lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
8152}
8153
8154/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03008155 * lpfc_sli_api_table_setup - Set up sli api function jump table
James Smart3772a992009-05-22 14:50:54 -04008156 * @phba: The hba struct for which this call is being executed.
8157 * @dev_grp: The HBA PCI-Device group number.
8158 *
8159 * This routine sets up the SLI interface API function jump table in @phba
8160 * struct.
8161 * Returns: 0 - success, -ENODEV - failure.
8162 **/
8163int
8164lpfc_sli_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
8165{
8166
8167 switch (dev_grp) {
8168 case LPFC_PCI_DEV_LP:
8169 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s3;
8170 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s3;
8171 break;
James Smart4f774512009-05-22 14:52:35 -04008172 case LPFC_PCI_DEV_OC:
8173 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s4;
8174 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s4;
8175 break;
James Smart3772a992009-05-22 14:50:54 -04008176 default:
8177 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8178 "1419 Invalid HBA PCI-device group: 0x%x\n",
8179 dev_grp);
8180 return -ENODEV;
8181 break;
8182 }
8183 phba->lpfc_get_iocb_from_iocbq = lpfc_get_iocb_from_iocbq;
8184 return 0;
8185}
James Smart92d7f7b2007-06-17 19:56:38 -05008186
James Smarte59058c2008-08-24 21:49:00 -04008187/**
James Smart3621a712009-04-06 18:47:14 -04008188 * lpfc_sli_issue_iocb - Wrapper function for __lpfc_sli_issue_iocb
James Smarte59058c2008-08-24 21:49:00 -04008189 * @phba: Pointer to HBA context object.
8190 * @pring: Pointer to driver SLI ring object.
8191 * @piocb: Pointer to command iocb.
8192 * @flag: Flag indicating if this command can be put into txq.
8193 *
8194 * lpfc_sli_issue_iocb is a wrapper around __lpfc_sli_issue_iocb
8195 * function. This function gets the hbalock and calls
8196 * __lpfc_sli_issue_iocb function and will return the error returned
8197 * by __lpfc_sli_issue_iocb function. This wrapper is used by
8198 * functions which do not hold hbalock.
8199 **/
James Smart92d7f7b2007-06-17 19:56:38 -05008200int
James Smart3772a992009-05-22 14:50:54 -04008201lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
James Smart92d7f7b2007-06-17 19:56:38 -05008202 struct lpfc_iocbq *piocb, uint32_t flag)
8203{
8204 unsigned long iflags;
8205 int rc;
8206
8207 spin_lock_irqsave(&phba->hbalock, iflags);
James Smart3772a992009-05-22 14:50:54 -04008208 rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
James Smart92d7f7b2007-06-17 19:56:38 -05008209 spin_unlock_irqrestore(&phba->hbalock, iflags);
8210
8211 return rc;
8212}
8213
James Smarte59058c2008-08-24 21:49:00 -04008214/**
James Smart3621a712009-04-06 18:47:14 -04008215 * lpfc_extra_ring_setup - Extra ring setup function
James Smarte59058c2008-08-24 21:49:00 -04008216 * @phba: Pointer to HBA context object.
8217 *
8218 * This function is called while driver attaches with the
8219 * HBA to setup the extra ring. The extra ring is used
8220 * only when driver needs to support target mode functionality
8221 * or IP over FC functionalities.
8222 *
8223 * This function is called with no lock held.
8224 **/
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05008225static int
8226lpfc_extra_ring_setup( struct lpfc_hba *phba)
8227{
8228 struct lpfc_sli *psli;
8229 struct lpfc_sli_ring *pring;
8230
8231 psli = &phba->sli;
8232
8233 /* Adjust cmd/rsp ring iocb entries more evenly */
James Smarta4bc3372006-12-02 13:34:16 -05008234
8235 /* Take some away from the FCP ring */
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05008236 pring = &psli->ring[psli->fcp_ring];
8237 pring->numCiocb -= SLI2_IOCB_CMD_R1XTRA_ENTRIES;
8238 pring->numRiocb -= SLI2_IOCB_RSP_R1XTRA_ENTRIES;
8239 pring->numCiocb -= SLI2_IOCB_CMD_R3XTRA_ENTRIES;
8240 pring->numRiocb -= SLI2_IOCB_RSP_R3XTRA_ENTRIES;
8241
James Smarta4bc3372006-12-02 13:34:16 -05008242 /* and give them to the extra ring */
8243 pring = &psli->ring[psli->extra_ring];
8244
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05008245 pring->numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
8246 pring->numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
8247 pring->numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
8248 pring->numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
8249
8250 /* Setup default profile for this ring */
8251 pring->iotag_max = 4096;
8252 pring->num_mask = 1;
8253 pring->prt[0].profile = 0; /* Mask 0 */
James Smarta4bc3372006-12-02 13:34:16 -05008254 pring->prt[0].rctl = phba->cfg_multi_ring_rctl;
8255 pring->prt[0].type = phba->cfg_multi_ring_type;
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05008256 pring->prt[0].lpfc_sli_rcv_unsol_event = NULL;
8257 return 0;
8258}
8259
James Smartcb69f7d2011-12-13 13:21:57 -05008260/* lpfc_sli_abts_recover_port - Recover a port that failed an ABTS.
8261 * @vport: pointer to virtual port object.
8262 * @ndlp: nodelist pointer for the impacted rport.
8263 *
8264 * The driver calls this routine in response to a XRI ABORT CQE
8265 * event from the port. In this event, the driver is required to
8266 * recover its login to the rport even though its login may be valid
8267 * from the driver's perspective. The failed ABTS notice from the
8268 * port indicates the rport is not responding.
8269 */
8270static void
8271lpfc_sli_abts_recover_port(struct lpfc_vport *vport,
8272 struct lpfc_nodelist *ndlp)
8273{
8274 struct Scsi_Host *shost;
8275 struct lpfc_hba *phba;
8276 unsigned long flags = 0;
8277
8278 shost = lpfc_shost_from_vport(vport);
8279 phba = vport->phba;
8280 if (ndlp->nlp_state != NLP_STE_MAPPED_NODE) {
8281 lpfc_printf_log(phba, KERN_INFO,
8282 LOG_SLI, "3093 No rport recovery needed. "
8283 "rport in state 0x%x\n",
8284 ndlp->nlp_state);
8285 return;
8286 }
8287 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8288 "3094 Start rport recovery on shost id 0x%x "
8289 "fc_id 0x%06x vpi 0x%x rpi 0x%x state 0x%x "
8290 "flags 0x%x\n",
8291 shost->host_no, ndlp->nlp_DID,
8292 vport->vpi, ndlp->nlp_rpi, ndlp->nlp_state,
8293 ndlp->nlp_flag);
8294 /*
8295 * The rport is not responding. Don't attempt ADISC recovery.
8296 * Remove the FCP-2 flag to force a PLOGI.
8297 */
8298 spin_lock_irqsave(shost->host_lock, flags);
8299 ndlp->nlp_fcp_info &= ~NLP_FCP_2_DEVICE;
8300 spin_unlock_irqrestore(shost->host_lock, flags);
8301 lpfc_disc_state_machine(vport, ndlp, NULL,
8302 NLP_EVT_DEVICE_RECOVERY);
8303 lpfc_cancel_retry_delay_tmo(vport, ndlp);
8304 spin_lock_irqsave(shost->host_lock, flags);
8305 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
8306 spin_unlock_irqrestore(shost->host_lock, flags);
8307 lpfc_disc_start(vport);
8308}
8309
8310/* lpfc_sli_abts_err_handler - handle a failed ABTS request from an SLI3 port.
8311 * @phba: Pointer to HBA context object.
8312 * @iocbq: Pointer to iocb object.
8313 *
8314 * The async_event handler calls this routine when it receives
8315 * an ASYNC_STATUS_CN event from the port. The port generates
8316 * this event when an Abort Sequence request to an rport fails
8317 * twice in succession. The abort could be originated by the
8318 * driver or by the port. The ABTS could have been for an ELS
8319 * or FCP IO. The port only generates this event when an ABTS
8320 * fails to complete after one retry.
8321 */
8322static void
8323lpfc_sli_abts_err_handler(struct lpfc_hba *phba,
8324 struct lpfc_iocbq *iocbq)
8325{
8326 struct lpfc_nodelist *ndlp = NULL;
8327 uint16_t rpi = 0, vpi = 0;
8328 struct lpfc_vport *vport = NULL;
8329
8330 /* The rpi in the ulpContext is vport-sensitive. */
8331 vpi = iocbq->iocb.un.asyncstat.sub_ctxt_tag;
8332 rpi = iocbq->iocb.ulpContext;
8333
8334 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
8335 "3092 Port generated ABTS async event "
8336 "on vpi %d rpi %d status 0x%x\n",
8337 vpi, rpi, iocbq->iocb.ulpStatus);
8338
8339 vport = lpfc_find_vport_by_vpid(phba, vpi);
8340 if (!vport)
8341 goto err_exit;
8342 ndlp = lpfc_findnode_rpi(vport, rpi);
8343 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp))
8344 goto err_exit;
8345
8346 if (iocbq->iocb.ulpStatus == IOSTAT_LOCAL_REJECT)
8347 lpfc_sli_abts_recover_port(vport, ndlp);
8348 return;
8349
8350 err_exit:
8351 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
8352 "3095 Event Context not found, no "
8353 "action on vpi %d rpi %d status 0x%x, reason 0x%x\n",
8354 iocbq->iocb.ulpContext, iocbq->iocb.ulpStatus,
8355 vpi, rpi);
8356}
8357
8358/* lpfc_sli4_abts_err_handler - handle a failed ABTS request from an SLI4 port.
8359 * @phba: pointer to HBA context object.
8360 * @ndlp: nodelist pointer for the impacted rport.
8361 * @axri: pointer to the wcqe containing the failed exchange.
8362 *
8363 * The driver calls this routine when it receives an ABORT_XRI_FCP CQE from the
8364 * port. The port generates this event when an abort exchange request to an
8365 * rport fails twice in succession with no reply. The abort could be originated
8366 * by the driver or by the port. The ABTS could have been for an ELS or FCP IO.
8367 */
8368void
8369lpfc_sli4_abts_err_handler(struct lpfc_hba *phba,
8370 struct lpfc_nodelist *ndlp,
8371 struct sli4_wcqe_xri_aborted *axri)
8372{
8373 struct lpfc_vport *vport;
8374
8375 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp))
8376 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
8377 "3115 Node Context not found, driver "
8378 "ignoring abts err event\n");
8379 vport = ndlp->vport;
8380 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
8381 "3116 Port generated FCP XRI ABORT event on "
8382 "vpi %d rpi %d xri x%x status 0x%x\n",
8383 ndlp->vport->vpi, ndlp->nlp_rpi,
8384 bf_get(lpfc_wcqe_xa_xri, axri),
8385 bf_get(lpfc_wcqe_xa_status, axri));
8386
8387 if (bf_get(lpfc_wcqe_xa_status, axri) == IOSTAT_LOCAL_REJECT)
8388 lpfc_sli_abts_recover_port(vport, ndlp);
8389}
8390
James Smarte59058c2008-08-24 21:49:00 -04008391/**
James Smart3621a712009-04-06 18:47:14 -04008392 * lpfc_sli_async_event_handler - ASYNC iocb handler function
James Smarte59058c2008-08-24 21:49:00 -04008393 * @phba: Pointer to HBA context object.
8394 * @pring: Pointer to driver SLI ring object.
8395 * @iocbq: Pointer to iocb object.
8396 *
8397 * This function is called by the slow ring event handler
8398 * function when there is an ASYNC event iocb in the ring.
8399 * This function is called with no lock held.
8400 * Currently this function handles only temperature related
8401 * ASYNC events. The function decodes the temperature sensor
8402 * event message and posts events for the management applications.
8403 **/
James Smart98c9ea52007-10-27 13:37:33 -04008404static void
James Smart57127f12007-10-27 13:37:05 -04008405lpfc_sli_async_event_handler(struct lpfc_hba * phba,
8406 struct lpfc_sli_ring * pring, struct lpfc_iocbq * iocbq)
8407{
8408 IOCB_t *icmd;
8409 uint16_t evt_code;
James Smart57127f12007-10-27 13:37:05 -04008410 struct temp_event temp_event_data;
8411 struct Scsi_Host *shost;
James Smarta257bf92009-04-06 18:48:10 -04008412 uint32_t *iocb_w;
James Smart57127f12007-10-27 13:37:05 -04008413
8414 icmd = &iocbq->iocb;
8415 evt_code = icmd->un.asyncstat.evt_code;
James Smart57127f12007-10-27 13:37:05 -04008416
James Smartcb69f7d2011-12-13 13:21:57 -05008417 switch (evt_code) {
8418 case ASYNC_TEMP_WARN:
8419 case ASYNC_TEMP_SAFE:
8420 temp_event_data.data = (uint32_t) icmd->ulpContext;
8421 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
8422 if (evt_code == ASYNC_TEMP_WARN) {
8423 temp_event_data.event_code = LPFC_THRESHOLD_TEMP;
8424 lpfc_printf_log(phba, KERN_ERR, LOG_TEMP,
8425 "0347 Adapter is very hot, please take "
8426 "corrective action. temperature : %d Celsius\n",
8427 (uint32_t) icmd->ulpContext);
8428 } else {
8429 temp_event_data.event_code = LPFC_NORMAL_TEMP;
8430 lpfc_printf_log(phba, KERN_ERR, LOG_TEMP,
8431 "0340 Adapter temperature is OK now. "
8432 "temperature : %d Celsius\n",
8433 (uint32_t) icmd->ulpContext);
8434 }
8435
8436 /* Send temperature change event to applications */
8437 shost = lpfc_shost_from_vport(phba->pport);
8438 fc_host_post_vendor_event(shost, fc_get_event_number(),
8439 sizeof(temp_event_data), (char *) &temp_event_data,
8440 LPFC_NL_VENDOR_ID);
8441 break;
8442 case ASYNC_STATUS_CN:
8443 lpfc_sli_abts_err_handler(phba, iocbq);
8444 break;
8445 default:
James Smarta257bf92009-04-06 18:48:10 -04008446 iocb_w = (uint32_t *) icmd;
James Smartcb69f7d2011-12-13 13:21:57 -05008447 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart76bb24e2007-10-27 13:38:00 -04008448 "0346 Ring %d handler: unexpected ASYNC_STATUS"
James Smarte4e74272009-07-19 10:01:38 -04008449 " evt_code 0x%x\n"
James Smarta257bf92009-04-06 18:48:10 -04008450 "W0 0x%08x W1 0x%08x W2 0x%08x W3 0x%08x\n"
8451 "W4 0x%08x W5 0x%08x W6 0x%08x W7 0x%08x\n"
8452 "W8 0x%08x W9 0x%08x W10 0x%08x W11 0x%08x\n"
8453 "W12 0x%08x W13 0x%08x W14 0x%08x W15 0x%08x\n",
James Smartcb69f7d2011-12-13 13:21:57 -05008454 pring->ringno, icmd->un.asyncstat.evt_code,
James Smarta257bf92009-04-06 18:48:10 -04008455 iocb_w[0], iocb_w[1], iocb_w[2], iocb_w[3],
8456 iocb_w[4], iocb_w[5], iocb_w[6], iocb_w[7],
8457 iocb_w[8], iocb_w[9], iocb_w[10], iocb_w[11],
8458 iocb_w[12], iocb_w[13], iocb_w[14], iocb_w[15]);
8459
James Smartcb69f7d2011-12-13 13:21:57 -05008460 break;
James Smart57127f12007-10-27 13:37:05 -04008461 }
James Smart57127f12007-10-27 13:37:05 -04008462}
8463
8464
James Smarte59058c2008-08-24 21:49:00 -04008465/**
James Smart3621a712009-04-06 18:47:14 -04008466 * lpfc_sli_setup - SLI ring setup function
James Smarte59058c2008-08-24 21:49:00 -04008467 * @phba: Pointer to HBA context object.
8468 *
8469 * lpfc_sli_setup sets up rings of the SLI interface with
8470 * number of iocbs per ring and iotags. This function is
8471 * called while driver attach to the HBA and before the
8472 * interrupts are enabled. So there is no need for locking.
8473 *
8474 * This function always returns 0.
8475 **/
dea31012005-04-17 16:05:31 -05008476int
8477lpfc_sli_setup(struct lpfc_hba *phba)
8478{
James Smarted957682007-06-17 19:56:37 -05008479 int i, totiocbsize = 0;
dea31012005-04-17 16:05:31 -05008480 struct lpfc_sli *psli = &phba->sli;
8481 struct lpfc_sli_ring *pring;
8482
8483 psli->num_rings = MAX_CONFIGURED_RINGS;
8484 psli->sli_flag = 0;
8485 psli->fcp_ring = LPFC_FCP_RING;
8486 psli->next_ring = LPFC_FCP_NEXT_RING;
James Smarta4bc3372006-12-02 13:34:16 -05008487 psli->extra_ring = LPFC_EXTRA_RING;
dea31012005-04-17 16:05:31 -05008488
James Bottomley604a3e32005-10-29 10:28:33 -05008489 psli->iocbq_lookup = NULL;
8490 psli->iocbq_lookup_len = 0;
8491 psli->last_iotag = 0;
8492
dea31012005-04-17 16:05:31 -05008493 for (i = 0; i < psli->num_rings; i++) {
8494 pring = &psli->ring[i];
8495 switch (i) {
8496 case LPFC_FCP_RING: /* ring 0 - FCP */
8497 /* numCiocb and numRiocb are used in config_port */
8498 pring->numCiocb = SLI2_IOCB_CMD_R0_ENTRIES;
8499 pring->numRiocb = SLI2_IOCB_RSP_R0_ENTRIES;
8500 pring->numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
8501 pring->numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
8502 pring->numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
8503 pring->numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
James Smarted957682007-06-17 19:56:37 -05008504 pring->sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05008505 SLI3_IOCB_CMD_SIZE :
8506 SLI2_IOCB_CMD_SIZE;
James Smarted957682007-06-17 19:56:37 -05008507 pring->sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05008508 SLI3_IOCB_RSP_SIZE :
8509 SLI2_IOCB_RSP_SIZE;
dea31012005-04-17 16:05:31 -05008510 pring->iotag_ctr = 0;
8511 pring->iotag_max =
James Smart92d7f7b2007-06-17 19:56:38 -05008512 (phba->cfg_hba_queue_depth * 2);
dea31012005-04-17 16:05:31 -05008513 pring->fast_iotag = pring->iotag_max;
8514 pring->num_mask = 0;
8515 break;
James Smarta4bc3372006-12-02 13:34:16 -05008516 case LPFC_EXTRA_RING: /* ring 1 - EXTRA */
dea31012005-04-17 16:05:31 -05008517 /* numCiocb and numRiocb are used in config_port */
8518 pring->numCiocb = SLI2_IOCB_CMD_R1_ENTRIES;
8519 pring->numRiocb = SLI2_IOCB_RSP_R1_ENTRIES;
James Smarted957682007-06-17 19:56:37 -05008520 pring->sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05008521 SLI3_IOCB_CMD_SIZE :
8522 SLI2_IOCB_CMD_SIZE;
James Smarted957682007-06-17 19:56:37 -05008523 pring->sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05008524 SLI3_IOCB_RSP_SIZE :
8525 SLI2_IOCB_RSP_SIZE;
James Smart2e0fef82007-06-17 19:56:36 -05008526 pring->iotag_max = phba->cfg_hba_queue_depth;
dea31012005-04-17 16:05:31 -05008527 pring->num_mask = 0;
8528 break;
8529 case LPFC_ELS_RING: /* ring 2 - ELS / CT */
8530 /* numCiocb and numRiocb are used in config_port */
8531 pring->numCiocb = SLI2_IOCB_CMD_R2_ENTRIES;
8532 pring->numRiocb = SLI2_IOCB_RSP_R2_ENTRIES;
James Smarted957682007-06-17 19:56:37 -05008533 pring->sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05008534 SLI3_IOCB_CMD_SIZE :
8535 SLI2_IOCB_CMD_SIZE;
James Smarted957682007-06-17 19:56:37 -05008536 pring->sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05008537 SLI3_IOCB_RSP_SIZE :
8538 SLI2_IOCB_RSP_SIZE;
dea31012005-04-17 16:05:31 -05008539 pring->fast_iotag = 0;
8540 pring->iotag_ctr = 0;
8541 pring->iotag_max = 4096;
James Smart57127f12007-10-27 13:37:05 -04008542 pring->lpfc_sli_rcv_async_status =
8543 lpfc_sli_async_event_handler;
James Smart6669f9b2009-10-02 15:16:45 -04008544 pring->num_mask = LPFC_MAX_RING_MASK;
dea31012005-04-17 16:05:31 -05008545 pring->prt[0].profile = 0; /* Mask 0 */
James Smart6a9c52c2009-10-02 15:16:51 -04008546 pring->prt[0].rctl = FC_RCTL_ELS_REQ;
8547 pring->prt[0].type = FC_TYPE_ELS;
dea31012005-04-17 16:05:31 -05008548 pring->prt[0].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05008549 lpfc_els_unsol_event;
dea31012005-04-17 16:05:31 -05008550 pring->prt[1].profile = 0; /* Mask 1 */
James Smart6a9c52c2009-10-02 15:16:51 -04008551 pring->prt[1].rctl = FC_RCTL_ELS_REP;
8552 pring->prt[1].type = FC_TYPE_ELS;
dea31012005-04-17 16:05:31 -05008553 pring->prt[1].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05008554 lpfc_els_unsol_event;
dea31012005-04-17 16:05:31 -05008555 pring->prt[2].profile = 0; /* Mask 2 */
8556 /* NameServer Inquiry */
James Smart6a9c52c2009-10-02 15:16:51 -04008557 pring->prt[2].rctl = FC_RCTL_DD_UNSOL_CTL;
dea31012005-04-17 16:05:31 -05008558 /* NameServer */
James Smart6a9c52c2009-10-02 15:16:51 -04008559 pring->prt[2].type = FC_TYPE_CT;
dea31012005-04-17 16:05:31 -05008560 pring->prt[2].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05008561 lpfc_ct_unsol_event;
dea31012005-04-17 16:05:31 -05008562 pring->prt[3].profile = 0; /* Mask 3 */
8563 /* NameServer response */
James Smart6a9c52c2009-10-02 15:16:51 -04008564 pring->prt[3].rctl = FC_RCTL_DD_SOL_CTL;
dea31012005-04-17 16:05:31 -05008565 /* NameServer */
James Smart6a9c52c2009-10-02 15:16:51 -04008566 pring->prt[3].type = FC_TYPE_CT;
dea31012005-04-17 16:05:31 -05008567 pring->prt[3].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05008568 lpfc_ct_unsol_event;
James Smart6669f9b2009-10-02 15:16:45 -04008569 /* abort unsolicited sequence */
8570 pring->prt[4].profile = 0; /* Mask 4 */
8571 pring->prt[4].rctl = FC_RCTL_BA_ABTS;
8572 pring->prt[4].type = FC_TYPE_BLS;
8573 pring->prt[4].lpfc_sli_rcv_unsol_event =
8574 lpfc_sli4_ct_abort_unsol_event;
dea31012005-04-17 16:05:31 -05008575 break;
8576 }
James Smarted957682007-06-17 19:56:37 -05008577 totiocbsize += (pring->numCiocb * pring->sizeCiocb) +
James Smart92d7f7b2007-06-17 19:56:38 -05008578 (pring->numRiocb * pring->sizeRiocb);
dea31012005-04-17 16:05:31 -05008579 }
James Smarted957682007-06-17 19:56:37 -05008580 if (totiocbsize > MAX_SLIM_IOCB_SIZE) {
dea31012005-04-17 16:05:31 -05008581 /* Too many cmd / rsp ring entries in SLI2 SLIM */
James Smarte8b62012007-08-02 11:10:09 -04008582 printk(KERN_ERR "%d:0462 Too many cmd / rsp ring entries in "
8583 "SLI2 SLIM Data: x%x x%lx\n",
8584 phba->brd_no, totiocbsize,
8585 (unsigned long) MAX_SLIM_IOCB_SIZE);
dea31012005-04-17 16:05:31 -05008586 }
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05008587 if (phba->cfg_multi_ring_support == 2)
8588 lpfc_extra_ring_setup(phba);
dea31012005-04-17 16:05:31 -05008589
8590 return 0;
8591}
8592
James Smarte59058c2008-08-24 21:49:00 -04008593/**
James Smart3621a712009-04-06 18:47:14 -04008594 * lpfc_sli_queue_setup - Queue initialization function
James Smarte59058c2008-08-24 21:49:00 -04008595 * @phba: Pointer to HBA context object.
8596 *
8597 * lpfc_sli_queue_setup sets up mailbox queues and iocb queues for each
8598 * ring. This function also initializes ring indices of each ring.
8599 * This function is called during the initialization of the SLI
8600 * interface of an HBA.
8601 * This function is called with no lock held and always returns
8602 * 1.
8603 **/
dea31012005-04-17 16:05:31 -05008604int
James Smart2e0fef82007-06-17 19:56:36 -05008605lpfc_sli_queue_setup(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05008606{
8607 struct lpfc_sli *psli;
8608 struct lpfc_sli_ring *pring;
James Bottomley604a3e32005-10-29 10:28:33 -05008609 int i;
dea31012005-04-17 16:05:31 -05008610
8611 psli = &phba->sli;
James Smart2e0fef82007-06-17 19:56:36 -05008612 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05008613 INIT_LIST_HEAD(&psli->mboxq);
James Smart92d7f7b2007-06-17 19:56:38 -05008614 INIT_LIST_HEAD(&psli->mboxq_cmpl);
dea31012005-04-17 16:05:31 -05008615 /* Initialize list headers for txq and txcmplq as double linked lists */
8616 for (i = 0; i < psli->num_rings; i++) {
8617 pring = &psli->ring[i];
8618 pring->ringno = i;
8619 pring->next_cmdidx = 0;
8620 pring->local_getidx = 0;
8621 pring->cmdidx = 0;
8622 INIT_LIST_HEAD(&pring->txq);
8623 INIT_LIST_HEAD(&pring->txcmplq);
8624 INIT_LIST_HEAD(&pring->iocb_continueq);
James Smart9c2face2008-01-11 01:53:18 -05008625 INIT_LIST_HEAD(&pring->iocb_continue_saveq);
dea31012005-04-17 16:05:31 -05008626 INIT_LIST_HEAD(&pring->postbufq);
dea31012005-04-17 16:05:31 -05008627 }
James Smart2e0fef82007-06-17 19:56:36 -05008628 spin_unlock_irq(&phba->hbalock);
8629 return 1;
dea31012005-04-17 16:05:31 -05008630}
8631
James Smarte59058c2008-08-24 21:49:00 -04008632/**
James Smart04c68492009-05-22 14:52:52 -04008633 * lpfc_sli_mbox_sys_flush - Flush mailbox command sub-system
8634 * @phba: Pointer to HBA context object.
8635 *
8636 * This routine flushes the mailbox command subsystem. It will unconditionally
8637 * flush all the mailbox commands in the three possible stages in the mailbox
8638 * command sub-system: pending mailbox command queue; the outstanding mailbox
8639 * command; and completed mailbox command queue. It is caller's responsibility
8640 * to make sure that the driver is in the proper state to flush the mailbox
8641 * command sub-system. Namely, the posting of mailbox commands into the
8642 * pending mailbox command queue from the various clients must be stopped;
8643 * either the HBA is in a state that it will never works on the outstanding
8644 * mailbox command (such as in EEH or ERATT conditions) or the outstanding
8645 * mailbox command has been completed.
8646 **/
8647static void
8648lpfc_sli_mbox_sys_flush(struct lpfc_hba *phba)
8649{
8650 LIST_HEAD(completions);
8651 struct lpfc_sli *psli = &phba->sli;
8652 LPFC_MBOXQ_t *pmb;
8653 unsigned long iflag;
8654
8655 /* Flush all the mailbox commands in the mbox system */
8656 spin_lock_irqsave(&phba->hbalock, iflag);
8657 /* The pending mailbox command queue */
8658 list_splice_init(&phba->sli.mboxq, &completions);
8659 /* The outstanding active mailbox command */
8660 if (psli->mbox_active) {
8661 list_add_tail(&psli->mbox_active->list, &completions);
8662 psli->mbox_active = NULL;
8663 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
8664 }
8665 /* The completed mailbox command queue */
8666 list_splice_init(&phba->sli.mboxq_cmpl, &completions);
8667 spin_unlock_irqrestore(&phba->hbalock, iflag);
8668
8669 /* Return all flushed mailbox commands with MBX_NOT_FINISHED status */
8670 while (!list_empty(&completions)) {
8671 list_remove_head(&completions, pmb, LPFC_MBOXQ_t, list);
8672 pmb->u.mb.mbxStatus = MBX_NOT_FINISHED;
8673 if (pmb->mbox_cmpl)
8674 pmb->mbox_cmpl(phba, pmb);
8675 }
8676}
8677
8678/**
James Smart3621a712009-04-06 18:47:14 -04008679 * lpfc_sli_host_down - Vport cleanup function
James Smarte59058c2008-08-24 21:49:00 -04008680 * @vport: Pointer to virtual port object.
8681 *
8682 * lpfc_sli_host_down is called to clean up the resources
8683 * associated with a vport before destroying virtual
8684 * port data structures.
8685 * This function does following operations:
8686 * - Free discovery resources associated with this virtual
8687 * port.
8688 * - Free iocbs associated with this virtual port in
8689 * the txq.
8690 * - Send abort for all iocb commands associated with this
8691 * vport in txcmplq.
8692 *
8693 * This function is called with no lock held and always returns 1.
8694 **/
dea31012005-04-17 16:05:31 -05008695int
James Smart92d7f7b2007-06-17 19:56:38 -05008696lpfc_sli_host_down(struct lpfc_vport *vport)
8697{
James Smart858c9f62007-06-17 19:56:39 -05008698 LIST_HEAD(completions);
James Smart92d7f7b2007-06-17 19:56:38 -05008699 struct lpfc_hba *phba = vport->phba;
8700 struct lpfc_sli *psli = &phba->sli;
8701 struct lpfc_sli_ring *pring;
8702 struct lpfc_iocbq *iocb, *next_iocb;
James Smart92d7f7b2007-06-17 19:56:38 -05008703 int i;
8704 unsigned long flags = 0;
8705 uint16_t prev_pring_flag;
8706
8707 lpfc_cleanup_discovery_resources(vport);
8708
8709 spin_lock_irqsave(&phba->hbalock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -05008710 for (i = 0; i < psli->num_rings; i++) {
8711 pring = &psli->ring[i];
8712 prev_pring_flag = pring->flag;
James Smart5e9d9b82008-06-14 22:52:53 -04008713 /* Only slow rings */
8714 if (pring->ringno == LPFC_ELS_RING) {
James Smart858c9f62007-06-17 19:56:39 -05008715 pring->flag |= LPFC_DEFERRED_RING_EVENT;
James Smart5e9d9b82008-06-14 22:52:53 -04008716 /* Set the lpfc data pending flag */
8717 set_bit(LPFC_DATA_READY, &phba->data_flags);
8718 }
James Smart92d7f7b2007-06-17 19:56:38 -05008719 /*
8720 * Error everything on the txq since these iocbs have not been
8721 * given to the FW yet.
8722 */
James Smart92d7f7b2007-06-17 19:56:38 -05008723 list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
8724 if (iocb->vport != vport)
8725 continue;
James Smart858c9f62007-06-17 19:56:39 -05008726 list_move_tail(&iocb->list, &completions);
James Smart92d7f7b2007-06-17 19:56:38 -05008727 pring->txq_cnt--;
James Smart92d7f7b2007-06-17 19:56:38 -05008728 }
8729
8730 /* Next issue ABTS for everything on the txcmplq */
8731 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq,
8732 list) {
8733 if (iocb->vport != vport)
8734 continue;
8735 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
8736 }
8737
8738 pring->flag = prev_pring_flag;
8739 }
8740
8741 spin_unlock_irqrestore(&phba->hbalock, flags);
8742
James Smarta257bf92009-04-06 18:48:10 -04008743 /* Cancel all the IOCBs from the completions list */
8744 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
8745 IOERR_SLI_DOWN);
James Smart92d7f7b2007-06-17 19:56:38 -05008746 return 1;
8747}
8748
James Smarte59058c2008-08-24 21:49:00 -04008749/**
James Smart3621a712009-04-06 18:47:14 -04008750 * lpfc_sli_hba_down - Resource cleanup function for the HBA
James Smarte59058c2008-08-24 21:49:00 -04008751 * @phba: Pointer to HBA context object.
8752 *
8753 * This function cleans up all iocb, buffers, mailbox commands
8754 * while shutting down the HBA. This function is called with no
8755 * lock held and always returns 1.
8756 * This function does the following to cleanup driver resources:
8757 * - Free discovery resources for each virtual port
8758 * - Cleanup any pending fabric iocbs
8759 * - Iterate through the iocb txq and free each entry
8760 * in the list.
8761 * - Free up any buffer posted to the HBA
8762 * - Free mailbox commands in the mailbox queue.
8763 **/
James Smart92d7f7b2007-06-17 19:56:38 -05008764int
James Smart2e0fef82007-06-17 19:56:36 -05008765lpfc_sli_hba_down(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05008766{
James Smart2534ba72007-04-25 09:52:20 -04008767 LIST_HEAD(completions);
James Smart2e0fef82007-06-17 19:56:36 -05008768 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05008769 struct lpfc_sli_ring *pring;
James Smart0ff10d42008-01-11 01:52:36 -05008770 struct lpfc_dmabuf *buf_ptr;
dea31012005-04-17 16:05:31 -05008771 unsigned long flags = 0;
James Smart04c68492009-05-22 14:52:52 -04008772 int i;
8773
8774 /* Shutdown the mailbox command sub-system */
8775 lpfc_sli_mbox_sys_shutdown(phba);
dea31012005-04-17 16:05:31 -05008776
dea31012005-04-17 16:05:31 -05008777 lpfc_hba_down_prep(phba);
8778
James Smart92d7f7b2007-06-17 19:56:38 -05008779 lpfc_fabric_abort_hba(phba);
8780
James Smart2e0fef82007-06-17 19:56:36 -05008781 spin_lock_irqsave(&phba->hbalock, flags);
dea31012005-04-17 16:05:31 -05008782 for (i = 0; i < psli->num_rings; i++) {
8783 pring = &psli->ring[i];
James Smart5e9d9b82008-06-14 22:52:53 -04008784 /* Only slow rings */
8785 if (pring->ringno == LPFC_ELS_RING) {
James Smart858c9f62007-06-17 19:56:39 -05008786 pring->flag |= LPFC_DEFERRED_RING_EVENT;
James Smart5e9d9b82008-06-14 22:52:53 -04008787 /* Set the lpfc data pending flag */
8788 set_bit(LPFC_DATA_READY, &phba->data_flags);
8789 }
dea31012005-04-17 16:05:31 -05008790
8791 /*
8792 * Error everything on the txq since these iocbs have not been
8793 * given to the FW yet.
8794 */
James Smart2534ba72007-04-25 09:52:20 -04008795 list_splice_init(&pring->txq, &completions);
dea31012005-04-17 16:05:31 -05008796 pring->txq_cnt = 0;
8797
dea31012005-04-17 16:05:31 -05008798 }
James Smart2e0fef82007-06-17 19:56:36 -05008799 spin_unlock_irqrestore(&phba->hbalock, flags);
dea31012005-04-17 16:05:31 -05008800
James Smarta257bf92009-04-06 18:48:10 -04008801 /* Cancel all the IOCBs from the completions list */
8802 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
8803 IOERR_SLI_DOWN);
James Smart2534ba72007-04-25 09:52:20 -04008804
James Smart0ff10d42008-01-11 01:52:36 -05008805 spin_lock_irqsave(&phba->hbalock, flags);
8806 list_splice_init(&phba->elsbuf, &completions);
8807 phba->elsbuf_cnt = 0;
8808 phba->elsbuf_prev_cnt = 0;
8809 spin_unlock_irqrestore(&phba->hbalock, flags);
8810
8811 while (!list_empty(&completions)) {
8812 list_remove_head(&completions, buf_ptr,
8813 struct lpfc_dmabuf, list);
8814 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
8815 kfree(buf_ptr);
8816 }
8817
dea31012005-04-17 16:05:31 -05008818 /* Return any active mbox cmds */
8819 del_timer_sync(&psli->mbox_tmo);
James Smart92d7f7b2007-06-17 19:56:38 -05008820
James Smartda0436e2009-05-22 14:51:39 -04008821 spin_lock_irqsave(&phba->pport->work_port_lock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -05008822 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
James Smartda0436e2009-05-22 14:51:39 -04008823 spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -05008824
James Smartda0436e2009-05-22 14:51:39 -04008825 return 1;
8826}
James Smart92d7f7b2007-06-17 19:56:38 -05008827
James Smartda0436e2009-05-22 14:51:39 -04008828/**
James Smart3621a712009-04-06 18:47:14 -04008829 * lpfc_sli_pcimem_bcopy - SLI memory copy function
James Smarte59058c2008-08-24 21:49:00 -04008830 * @srcp: Source memory pointer.
8831 * @destp: Destination memory pointer.
8832 * @cnt: Number of words required to be copied.
8833 *
8834 * This function is used for copying data between driver memory
8835 * and the SLI memory. This function also changes the endianness
8836 * of each word if native endianness is different from SLI
8837 * endianness. This function can be called with or without
8838 * lock.
8839 **/
dea31012005-04-17 16:05:31 -05008840void
8841lpfc_sli_pcimem_bcopy(void *srcp, void *destp, uint32_t cnt)
8842{
8843 uint32_t *src = srcp;
8844 uint32_t *dest = destp;
8845 uint32_t ldata;
8846 int i;
8847
8848 for (i = 0; i < (int)cnt; i += sizeof (uint32_t)) {
8849 ldata = *src;
8850 ldata = le32_to_cpu(ldata);
8851 *dest = ldata;
8852 src++;
8853 dest++;
8854 }
8855}
8856
James Smarte59058c2008-08-24 21:49:00 -04008857
8858/**
James Smarta0c87cb2009-07-19 10:01:10 -04008859 * lpfc_sli_bemem_bcopy - SLI memory copy function
8860 * @srcp: Source memory pointer.
8861 * @destp: Destination memory pointer.
8862 * @cnt: Number of words required to be copied.
8863 *
8864 * This function is used for copying data between a data structure
8865 * with big endian representation to local endianness.
8866 * This function can be called with or without lock.
8867 **/
8868void
8869lpfc_sli_bemem_bcopy(void *srcp, void *destp, uint32_t cnt)
8870{
8871 uint32_t *src = srcp;
8872 uint32_t *dest = destp;
8873 uint32_t ldata;
8874 int i;
8875
8876 for (i = 0; i < (int)cnt; i += sizeof(uint32_t)) {
8877 ldata = *src;
8878 ldata = be32_to_cpu(ldata);
8879 *dest = ldata;
8880 src++;
8881 dest++;
8882 }
8883}
8884
8885/**
James Smart3621a712009-04-06 18:47:14 -04008886 * lpfc_sli_ringpostbuf_put - Function to add a buffer to postbufq
James Smarte59058c2008-08-24 21:49:00 -04008887 * @phba: Pointer to HBA context object.
8888 * @pring: Pointer to driver SLI ring object.
8889 * @mp: Pointer to driver buffer object.
8890 *
8891 * This function is called with no lock held.
8892 * It always return zero after adding the buffer to the postbufq
8893 * buffer list.
8894 **/
dea31012005-04-17 16:05:31 -05008895int
James Smart2e0fef82007-06-17 19:56:36 -05008896lpfc_sli_ringpostbuf_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
8897 struct lpfc_dmabuf *mp)
dea31012005-04-17 16:05:31 -05008898{
8899 /* Stick struct lpfc_dmabuf at end of postbufq so driver can look it up
8900 later */
James Smart2e0fef82007-06-17 19:56:36 -05008901 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05008902 list_add_tail(&mp->list, &pring->postbufq);
dea31012005-04-17 16:05:31 -05008903 pring->postbufq_cnt++;
James Smart2e0fef82007-06-17 19:56:36 -05008904 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05008905 return 0;
8906}
8907
James Smarte59058c2008-08-24 21:49:00 -04008908/**
James Smart3621a712009-04-06 18:47:14 -04008909 * lpfc_sli_get_buffer_tag - allocates a tag for a CMD_QUE_XRI64_CX buffer
James Smarte59058c2008-08-24 21:49:00 -04008910 * @phba: Pointer to HBA context object.
8911 *
8912 * When HBQ is enabled, buffers are searched based on tags. This function
8913 * allocates a tag for buffer posted using CMD_QUE_XRI64_CX iocb. The
8914 * tag is bit wise or-ed with QUE_BUFTAG_BIT to make sure that the tag
8915 * does not conflict with tags of buffer posted for unsolicited events.
8916 * The function returns the allocated tag. The function is called with
8917 * no locks held.
8918 **/
James Smart76bb24e2007-10-27 13:38:00 -04008919uint32_t
8920lpfc_sli_get_buffer_tag(struct lpfc_hba *phba)
8921{
8922 spin_lock_irq(&phba->hbalock);
8923 phba->buffer_tag_count++;
8924 /*
8925 * Always set the QUE_BUFTAG_BIT to distiguish between
8926 * a tag assigned by HBQ.
8927 */
8928 phba->buffer_tag_count |= QUE_BUFTAG_BIT;
8929 spin_unlock_irq(&phba->hbalock);
8930 return phba->buffer_tag_count;
8931}
8932
James Smarte59058c2008-08-24 21:49:00 -04008933/**
James Smart3621a712009-04-06 18:47:14 -04008934 * lpfc_sli_ring_taggedbuf_get - find HBQ buffer associated with given tag
James Smarte59058c2008-08-24 21:49:00 -04008935 * @phba: Pointer to HBA context object.
8936 * @pring: Pointer to driver SLI ring object.
8937 * @tag: Buffer tag.
8938 *
8939 * Buffers posted using CMD_QUE_XRI64_CX iocb are in pring->postbufq
8940 * list. After HBA DMA data to these buffers, CMD_IOCB_RET_XRI64_CX
8941 * iocb is posted to the response ring with the tag of the buffer.
8942 * This function searches the pring->postbufq list using the tag
8943 * to find buffer associated with CMD_IOCB_RET_XRI64_CX
8944 * iocb. If the buffer is found then lpfc_dmabuf object of the
8945 * buffer is returned to the caller else NULL is returned.
8946 * This function is called with no lock held.
8947 **/
James Smart76bb24e2007-10-27 13:38:00 -04008948struct lpfc_dmabuf *
8949lpfc_sli_ring_taggedbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
8950 uint32_t tag)
8951{
8952 struct lpfc_dmabuf *mp, *next_mp;
8953 struct list_head *slp = &pring->postbufq;
8954
Lucas De Marchi25985ed2011-03-30 22:57:33 -03008955 /* Search postbufq, from the beginning, looking for a match on tag */
James Smart76bb24e2007-10-27 13:38:00 -04008956 spin_lock_irq(&phba->hbalock);
8957 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
8958 if (mp->buffer_tag == tag) {
8959 list_del_init(&mp->list);
8960 pring->postbufq_cnt--;
8961 spin_unlock_irq(&phba->hbalock);
8962 return mp;
8963 }
8964 }
8965
8966 spin_unlock_irq(&phba->hbalock);
8967 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartd7c255b2008-08-24 21:50:00 -04008968 "0402 Cannot find virtual addr for buffer tag on "
James Smart76bb24e2007-10-27 13:38:00 -04008969 "ring %d Data x%lx x%p x%p x%x\n",
8970 pring->ringno, (unsigned long) tag,
8971 slp->next, slp->prev, pring->postbufq_cnt);
8972
8973 return NULL;
8974}
dea31012005-04-17 16:05:31 -05008975
James Smarte59058c2008-08-24 21:49:00 -04008976/**
James Smart3621a712009-04-06 18:47:14 -04008977 * lpfc_sli_ringpostbuf_get - search buffers for unsolicited CT and ELS events
James Smarte59058c2008-08-24 21:49:00 -04008978 * @phba: Pointer to HBA context object.
8979 * @pring: Pointer to driver SLI ring object.
8980 * @phys: DMA address of the buffer.
8981 *
8982 * This function searches the buffer list using the dma_address
8983 * of unsolicited event to find the driver's lpfc_dmabuf object
8984 * corresponding to the dma_address. The function returns the
8985 * lpfc_dmabuf object if a buffer is found else it returns NULL.
8986 * This function is called by the ct and els unsolicited event
8987 * handlers to get the buffer associated with the unsolicited
8988 * event.
8989 *
8990 * This function is called with no lock held.
8991 **/
dea31012005-04-17 16:05:31 -05008992struct lpfc_dmabuf *
8993lpfc_sli_ringpostbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
8994 dma_addr_t phys)
8995{
8996 struct lpfc_dmabuf *mp, *next_mp;
8997 struct list_head *slp = &pring->postbufq;
8998
Lucas De Marchi25985ed2011-03-30 22:57:33 -03008999 /* Search postbufq, from the beginning, looking for a match on phys */
James Smart2e0fef82007-06-17 19:56:36 -05009000 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05009001 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
9002 if (mp->phys == phys) {
9003 list_del_init(&mp->list);
9004 pring->postbufq_cnt--;
James Smart2e0fef82007-06-17 19:56:36 -05009005 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05009006 return mp;
9007 }
9008 }
9009
James Smart2e0fef82007-06-17 19:56:36 -05009010 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05009011 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04009012 "0410 Cannot find virtual addr for mapped buf on "
dea31012005-04-17 16:05:31 -05009013 "ring %d Data x%llx x%p x%p x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04009014 pring->ringno, (unsigned long long)phys,
dea31012005-04-17 16:05:31 -05009015 slp->next, slp->prev, pring->postbufq_cnt);
9016 return NULL;
9017}
9018
James Smarte59058c2008-08-24 21:49:00 -04009019/**
James Smart3621a712009-04-06 18:47:14 -04009020 * lpfc_sli_abort_els_cmpl - Completion handler for the els abort iocbs
James Smarte59058c2008-08-24 21:49:00 -04009021 * @phba: Pointer to HBA context object.
9022 * @cmdiocb: Pointer to driver command iocb object.
9023 * @rspiocb: Pointer to driver response iocb object.
9024 *
9025 * This function is the completion handler for the abort iocbs for
9026 * ELS commands. This function is called from the ELS ring event
9027 * handler with no lock held. This function frees memory resources
9028 * associated with the abort iocb.
9029 **/
dea31012005-04-17 16:05:31 -05009030static void
James Smart2e0fef82007-06-17 19:56:36 -05009031lpfc_sli_abort_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
9032 struct lpfc_iocbq *rspiocb)
dea31012005-04-17 16:05:31 -05009033{
James Smart2e0fef82007-06-17 19:56:36 -05009034 IOCB_t *irsp = &rspiocb->iocb;
James Smart2680eea2007-04-25 09:52:55 -04009035 uint16_t abort_iotag, abort_context;
James Smartff78d8f2011-12-13 13:21:35 -05009036 struct lpfc_iocbq *abort_iocb = NULL;
James Smart2680eea2007-04-25 09:52:55 -04009037
9038 if (irsp->ulpStatus) {
James Smartff78d8f2011-12-13 13:21:35 -05009039
9040 /*
9041 * Assume that the port already completed and returned, or
9042 * will return the iocb. Just Log the message.
9043 */
James Smart2680eea2007-04-25 09:52:55 -04009044 abort_context = cmdiocb->iocb.un.acxri.abortContextTag;
9045 abort_iotag = cmdiocb->iocb.un.acxri.abortIoTag;
9046
James Smart2e0fef82007-06-17 19:56:36 -05009047 spin_lock_irq(&phba->hbalock);
James Smart45ed1192009-10-02 15:17:02 -04009048 if (phba->sli_rev < LPFC_SLI_REV4) {
9049 if (abort_iotag != 0 &&
9050 abort_iotag <= phba->sli.last_iotag)
9051 abort_iocb =
9052 phba->sli.iocbq_lookup[abort_iotag];
9053 } else
9054 /* For sli4 the abort_tag is the XRI,
9055 * so the abort routine puts the iotag of the iocb
9056 * being aborted in the context field of the abort
9057 * IOCB.
9058 */
9059 abort_iocb = phba->sli.iocbq_lookup[abort_context];
James Smart2680eea2007-04-25 09:52:55 -04009060
James Smart2a9bf3d2010-06-07 15:24:45 -04009061 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS | LOG_SLI,
9062 "0327 Cannot abort els iocb %p "
9063 "with tag %x context %x, abort status %x, "
9064 "abort code %x\n",
9065 abort_iocb, abort_iotag, abort_context,
9066 irsp->ulpStatus, irsp->un.ulpWord[4]);
James Smart2680eea2007-04-25 09:52:55 -04009067
James Smartff78d8f2011-12-13 13:21:35 -05009068 spin_unlock_irq(&phba->hbalock);
James Smart2680eea2007-04-25 09:52:55 -04009069 }
James Bottomley604a3e32005-10-29 10:28:33 -05009070 lpfc_sli_release_iocbq(phba, cmdiocb);
dea31012005-04-17 16:05:31 -05009071 return;
9072}
9073
James Smarte59058c2008-08-24 21:49:00 -04009074/**
James Smart3621a712009-04-06 18:47:14 -04009075 * lpfc_ignore_els_cmpl - Completion handler for aborted ELS command
James Smarte59058c2008-08-24 21:49:00 -04009076 * @phba: Pointer to HBA context object.
9077 * @cmdiocb: Pointer to driver command iocb object.
9078 * @rspiocb: Pointer to driver response iocb object.
9079 *
9080 * The function is called from SLI ring event handler with no
9081 * lock held. This function is the completion handler for ELS commands
9082 * which are aborted. The function frees memory resources used for
9083 * the aborted ELS commands.
9084 **/
James Smart92d7f7b2007-06-17 19:56:38 -05009085static void
9086lpfc_ignore_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
9087 struct lpfc_iocbq *rspiocb)
9088{
9089 IOCB_t *irsp = &rspiocb->iocb;
9090
9091 /* ELS cmd tag <ulpIoTag> completes */
9092 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
James Smartd7c255b2008-08-24 21:50:00 -04009093 "0139 Ignoring ELS cmd tag x%x completion Data: "
James Smart92d7f7b2007-06-17 19:56:38 -05009094 "x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04009095 irsp->ulpIoTag, irsp->ulpStatus,
James Smart92d7f7b2007-06-17 19:56:38 -05009096 irsp->un.ulpWord[4], irsp->ulpTimeout);
James Smart858c9f62007-06-17 19:56:39 -05009097 if (cmdiocb->iocb.ulpCommand == CMD_GEN_REQUEST64_CR)
9098 lpfc_ct_free_iocb(phba, cmdiocb);
9099 else
9100 lpfc_els_free_iocb(phba, cmdiocb);
James Smart92d7f7b2007-06-17 19:56:38 -05009101 return;
9102}
9103
James Smarte59058c2008-08-24 21:49:00 -04009104/**
James Smart5af5eee2010-10-22 11:06:38 -04009105 * lpfc_sli_abort_iotag_issue - Issue abort for a command iocb
James Smarte59058c2008-08-24 21:49:00 -04009106 * @phba: Pointer to HBA context object.
9107 * @pring: Pointer to driver SLI ring object.
9108 * @cmdiocb: Pointer to driver command iocb object.
9109 *
James Smart5af5eee2010-10-22 11:06:38 -04009110 * This function issues an abort iocb for the provided command iocb down to
9111 * the port. Other than the case the outstanding command iocb is an abort
9112 * request, this function issues abort out unconditionally. This function is
9113 * called with hbalock held. The function returns 0 when it fails due to
9114 * memory allocation failure or when the command iocb is an abort request.
James Smarte59058c2008-08-24 21:49:00 -04009115 **/
James Smart5af5eee2010-10-22 11:06:38 -04009116static int
9117lpfc_sli_abort_iotag_issue(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -05009118 struct lpfc_iocbq *cmdiocb)
dea31012005-04-17 16:05:31 -05009119{
James Smart2e0fef82007-06-17 19:56:36 -05009120 struct lpfc_vport *vport = cmdiocb->vport;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009121 struct lpfc_iocbq *abtsiocbp;
dea31012005-04-17 16:05:31 -05009122 IOCB_t *icmd = NULL;
9123 IOCB_t *iabt = NULL;
James Smart5af5eee2010-10-22 11:06:38 -04009124 int retval;
James Smart07951072007-04-25 09:51:38 -04009125
James Smart92d7f7b2007-06-17 19:56:38 -05009126 /*
9127 * There are certain command types we don't want to abort. And we
9128 * don't want to abort commands that are already in the process of
9129 * being aborted.
James Smart07951072007-04-25 09:51:38 -04009130 */
9131 icmd = &cmdiocb->iocb;
James Smart2e0fef82007-06-17 19:56:36 -05009132 if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
James Smart92d7f7b2007-06-17 19:56:38 -05009133 icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
9134 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
James Smart07951072007-04-25 09:51:38 -04009135 return 0;
9136
dea31012005-04-17 16:05:31 -05009137 /* issue ABTS for this IOCB based on iotag */
James Smart92d7f7b2007-06-17 19:56:38 -05009138 abtsiocbp = __lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05009139 if (abtsiocbp == NULL)
9140 return 0;
dea31012005-04-17 16:05:31 -05009141
James Smart07951072007-04-25 09:51:38 -04009142 /* This signals the response to set the correct status
James Smart341af102010-01-26 23:07:37 -05009143 * before calling the completion handler
James Smart07951072007-04-25 09:51:38 -04009144 */
9145 cmdiocb->iocb_flag |= LPFC_DRIVER_ABORTED;
9146
dea31012005-04-17 16:05:31 -05009147 iabt = &abtsiocbp->iocb;
James Smart07951072007-04-25 09:51:38 -04009148 iabt->un.acxri.abortType = ABORT_TYPE_ABTS;
9149 iabt->un.acxri.abortContextTag = icmd->ulpContext;
James Smart45ed1192009-10-02 15:17:02 -04009150 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smartda0436e2009-05-22 14:51:39 -04009151 iabt->un.acxri.abortIoTag = cmdiocb->sli4_xritag;
James Smart45ed1192009-10-02 15:17:02 -04009152 iabt->un.acxri.abortContextTag = cmdiocb->iotag;
9153 }
James Smartda0436e2009-05-22 14:51:39 -04009154 else
9155 iabt->un.acxri.abortIoTag = icmd->ulpIoTag;
dea31012005-04-17 16:05:31 -05009156 iabt->ulpLe = 1;
James Smart07951072007-04-25 09:51:38 -04009157 iabt->ulpClass = icmd->ulpClass;
dea31012005-04-17 16:05:31 -05009158
James Smart5ffc2662009-11-18 15:39:44 -05009159 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
9160 abtsiocbp->fcp_wqidx = cmdiocb->fcp_wqidx;
James Smart341af102010-01-26 23:07:37 -05009161 if (cmdiocb->iocb_flag & LPFC_IO_FCP)
9162 abtsiocbp->iocb_flag |= LPFC_USE_FCPWQIDX;
James Smart5ffc2662009-11-18 15:39:44 -05009163
James Smart2e0fef82007-06-17 19:56:36 -05009164 if (phba->link_state >= LPFC_LINK_UP)
James Smart07951072007-04-25 09:51:38 -04009165 iabt->ulpCommand = CMD_ABORT_XRI_CN;
9166 else
9167 iabt->ulpCommand = CMD_CLOSE_XRI_CN;
9168
9169 abtsiocbp->iocb_cmpl = lpfc_sli_abort_els_cmpl;
James Smart5b8bd0c2007-04-25 09:52:49 -04009170
James Smarte8b62012007-08-02 11:10:09 -04009171 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
9172 "0339 Abort xri x%x, original iotag x%x, "
9173 "abort cmd iotag x%x\n",
James Smart2a9bf3d2010-06-07 15:24:45 -04009174 iabt->un.acxri.abortIoTag,
James Smarte8b62012007-08-02 11:10:09 -04009175 iabt->un.acxri.abortContextTag,
James Smart2a9bf3d2010-06-07 15:24:45 -04009176 abtsiocbp->iotag);
James Smartda0436e2009-05-22 14:51:39 -04009177 retval = __lpfc_sli_issue_iocb(phba, pring->ringno, abtsiocbp, 0);
James Smart07951072007-04-25 09:51:38 -04009178
James Smartd7c255b2008-08-24 21:50:00 -04009179 if (retval)
9180 __lpfc_sli_release_iocbq(phba, abtsiocbp);
James Smart5af5eee2010-10-22 11:06:38 -04009181
9182 /*
9183 * Caller to this routine should check for IOCB_ERROR
9184 * and handle it properly. This routine no longer removes
9185 * iocb off txcmplq and call compl in case of IOCB_ERROR.
9186 */
9187 return retval;
9188}
9189
9190/**
9191 * lpfc_sli_issue_abort_iotag - Abort function for a command iocb
9192 * @phba: Pointer to HBA context object.
9193 * @pring: Pointer to driver SLI ring object.
9194 * @cmdiocb: Pointer to driver command iocb object.
9195 *
9196 * This function issues an abort iocb for the provided command iocb. In case
9197 * of unloading, the abort iocb will not be issued to commands on the ELS
9198 * ring. Instead, the callback function shall be changed to those commands
9199 * so that nothing happens when them finishes. This function is called with
9200 * hbalock held. The function returns 0 when the command iocb is an abort
9201 * request.
9202 **/
9203int
9204lpfc_sli_issue_abort_iotag(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
9205 struct lpfc_iocbq *cmdiocb)
9206{
9207 struct lpfc_vport *vport = cmdiocb->vport;
9208 int retval = IOCB_ERROR;
9209 IOCB_t *icmd = NULL;
9210
9211 /*
9212 * There are certain command types we don't want to abort. And we
9213 * don't want to abort commands that are already in the process of
9214 * being aborted.
9215 */
9216 icmd = &cmdiocb->iocb;
9217 if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
9218 icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
9219 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
9220 return 0;
9221
9222 /*
9223 * If we're unloading, don't abort iocb on the ELS ring, but change
9224 * the callback so that nothing happens when it finishes.
9225 */
9226 if ((vport->load_flag & FC_UNLOADING) &&
9227 (pring->ringno == LPFC_ELS_RING)) {
9228 if (cmdiocb->iocb_flag & LPFC_IO_FABRIC)
9229 cmdiocb->fabric_iocb_cmpl = lpfc_ignore_els_cmpl;
9230 else
9231 cmdiocb->iocb_cmpl = lpfc_ignore_els_cmpl;
9232 goto abort_iotag_exit;
9233 }
9234
9235 /* Now, we try to issue the abort to the cmdiocb out */
9236 retval = lpfc_sli_abort_iotag_issue(phba, pring, cmdiocb);
9237
James Smart07951072007-04-25 09:51:38 -04009238abort_iotag_exit:
James Smart2e0fef82007-06-17 19:56:36 -05009239 /*
9240 * Caller to this routine should check for IOCB_ERROR
9241 * and handle it properly. This routine no longer removes
9242 * iocb off txcmplq and call compl in case of IOCB_ERROR.
James Smart07951072007-04-25 09:51:38 -04009243 */
James Smart2e0fef82007-06-17 19:56:36 -05009244 return retval;
dea31012005-04-17 16:05:31 -05009245}
9246
James Smarte59058c2008-08-24 21:49:00 -04009247/**
James Smart5af5eee2010-10-22 11:06:38 -04009248 * lpfc_sli_iocb_ring_abort - Unconditionally abort all iocbs on an iocb ring
9249 * @phba: Pointer to HBA context object.
9250 * @pring: Pointer to driver SLI ring object.
9251 *
9252 * This function aborts all iocbs in the given ring and frees all the iocb
9253 * objects in txq. This function issues abort iocbs unconditionally for all
9254 * the iocb commands in txcmplq. The iocbs in the txcmplq is not guaranteed
9255 * to complete before the return of this function. The caller is not required
9256 * to hold any locks.
9257 **/
9258static void
9259lpfc_sli_iocb_ring_abort(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
9260{
9261 LIST_HEAD(completions);
9262 struct lpfc_iocbq *iocb, *next_iocb;
9263
9264 if (pring->ringno == LPFC_ELS_RING)
9265 lpfc_fabric_abort_hba(phba);
9266
9267 spin_lock_irq(&phba->hbalock);
9268
9269 /* Take off all the iocbs on txq for cancelling */
9270 list_splice_init(&pring->txq, &completions);
9271 pring->txq_cnt = 0;
9272
9273 /* Next issue ABTS for everything on the txcmplq */
9274 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
9275 lpfc_sli_abort_iotag_issue(phba, pring, iocb);
9276
9277 spin_unlock_irq(&phba->hbalock);
9278
9279 /* Cancel all the IOCBs from the completions list */
9280 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
9281 IOERR_SLI_ABORTED);
9282}
9283
9284/**
9285 * lpfc_sli_hba_iocb_abort - Abort all iocbs to an hba.
9286 * @phba: pointer to lpfc HBA data structure.
9287 *
9288 * This routine will abort all pending and outstanding iocbs to an HBA.
9289 **/
9290void
9291lpfc_sli_hba_iocb_abort(struct lpfc_hba *phba)
9292{
9293 struct lpfc_sli *psli = &phba->sli;
9294 struct lpfc_sli_ring *pring;
9295 int i;
9296
9297 for (i = 0; i < psli->num_rings; i++) {
9298 pring = &psli->ring[i];
9299 lpfc_sli_iocb_ring_abort(phba, pring);
9300 }
9301}
9302
9303/**
James Smart3621a712009-04-06 18:47:14 -04009304 * lpfc_sli_validate_fcp_iocb - find commands associated with a vport or LUN
James Smarte59058c2008-08-24 21:49:00 -04009305 * @iocbq: Pointer to driver iocb object.
9306 * @vport: Pointer to driver virtual port object.
9307 * @tgt_id: SCSI ID of the target.
9308 * @lun_id: LUN ID of the scsi device.
9309 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST
9310 *
James Smart3621a712009-04-06 18:47:14 -04009311 * This function acts as an iocb filter for functions which abort or count
James Smarte59058c2008-08-24 21:49:00 -04009312 * all FCP iocbs pending on a lun/SCSI target/SCSI host. It will return
9313 * 0 if the filtering criteria is met for the given iocb and will return
9314 * 1 if the filtering criteria is not met.
9315 * If ctx_cmd == LPFC_CTX_LUN, the function returns 0 only if the
9316 * given iocb is for the SCSI device specified by vport, tgt_id and
9317 * lun_id parameter.
9318 * If ctx_cmd == LPFC_CTX_TGT, the function returns 0 only if the
9319 * given iocb is for the SCSI target specified by vport and tgt_id
9320 * parameters.
9321 * If ctx_cmd == LPFC_CTX_HOST, the function returns 0 only if the
9322 * given iocb is for the SCSI host associated with the given vport.
9323 * This function is called with no locks held.
9324 **/
dea31012005-04-17 16:05:31 -05009325static int
James Smart51ef4c22007-08-02 11:10:31 -04009326lpfc_sli_validate_fcp_iocb(struct lpfc_iocbq *iocbq, struct lpfc_vport *vport,
9327 uint16_t tgt_id, uint64_t lun_id,
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009328 lpfc_ctx_cmd ctx_cmd)
dea31012005-04-17 16:05:31 -05009329{
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009330 struct lpfc_scsi_buf *lpfc_cmd;
dea31012005-04-17 16:05:31 -05009331 int rc = 1;
9332
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009333 if (!(iocbq->iocb_flag & LPFC_IO_FCP))
9334 return rc;
9335
James Smart51ef4c22007-08-02 11:10:31 -04009336 if (iocbq->vport != vport)
9337 return rc;
9338
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009339 lpfc_cmd = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009340
James Smart495a7142008-06-14 22:52:59 -04009341 if (lpfc_cmd->pCmd == NULL)
dea31012005-04-17 16:05:31 -05009342 return rc;
9343
9344 switch (ctx_cmd) {
9345 case LPFC_CTX_LUN:
James Smart495a7142008-06-14 22:52:59 -04009346 if ((lpfc_cmd->rdata->pnode) &&
9347 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id) &&
9348 (scsilun_to_int(&lpfc_cmd->fcp_cmnd->fcp_lun) == lun_id))
dea31012005-04-17 16:05:31 -05009349 rc = 0;
9350 break;
9351 case LPFC_CTX_TGT:
James Smart495a7142008-06-14 22:52:59 -04009352 if ((lpfc_cmd->rdata->pnode) &&
9353 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id))
dea31012005-04-17 16:05:31 -05009354 rc = 0;
9355 break;
dea31012005-04-17 16:05:31 -05009356 case LPFC_CTX_HOST:
9357 rc = 0;
9358 break;
9359 default:
9360 printk(KERN_ERR "%s: Unknown context cmd type, value %d\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07009361 __func__, ctx_cmd);
dea31012005-04-17 16:05:31 -05009362 break;
9363 }
9364
9365 return rc;
9366}
9367
James Smarte59058c2008-08-24 21:49:00 -04009368/**
James Smart3621a712009-04-06 18:47:14 -04009369 * lpfc_sli_sum_iocb - Function to count the number of FCP iocbs pending
James Smarte59058c2008-08-24 21:49:00 -04009370 * @vport: Pointer to virtual port.
9371 * @tgt_id: SCSI ID of the target.
9372 * @lun_id: LUN ID of the scsi device.
9373 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
9374 *
9375 * This function returns number of FCP commands pending for the vport.
9376 * When ctx_cmd == LPFC_CTX_LUN, the function returns number of FCP
9377 * commands pending on the vport associated with SCSI device specified
9378 * by tgt_id and lun_id parameters.
9379 * When ctx_cmd == LPFC_CTX_TGT, the function returns number of FCP
9380 * commands pending on the vport associated with SCSI target specified
9381 * by tgt_id parameter.
9382 * When ctx_cmd == LPFC_CTX_HOST, the function returns number of FCP
9383 * commands pending on the vport.
9384 * This function returns the number of iocbs which satisfy the filter.
9385 * This function is called without any lock held.
9386 **/
dea31012005-04-17 16:05:31 -05009387int
James Smart51ef4c22007-08-02 11:10:31 -04009388lpfc_sli_sum_iocb(struct lpfc_vport *vport, uint16_t tgt_id, uint64_t lun_id,
9389 lpfc_ctx_cmd ctx_cmd)
dea31012005-04-17 16:05:31 -05009390{
James Smart51ef4c22007-08-02 11:10:31 -04009391 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009392 struct lpfc_iocbq *iocbq;
9393 int sum, i;
dea31012005-04-17 16:05:31 -05009394
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009395 for (i = 1, sum = 0; i <= phba->sli.last_iotag; i++) {
9396 iocbq = phba->sli.iocbq_lookup[i];
dea31012005-04-17 16:05:31 -05009397
James Smart51ef4c22007-08-02 11:10:31 -04009398 if (lpfc_sli_validate_fcp_iocb (iocbq, vport, tgt_id, lun_id,
9399 ctx_cmd) == 0)
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009400 sum++;
dea31012005-04-17 16:05:31 -05009401 }
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009402
dea31012005-04-17 16:05:31 -05009403 return sum;
9404}
9405
James Smarte59058c2008-08-24 21:49:00 -04009406/**
James Smart3621a712009-04-06 18:47:14 -04009407 * lpfc_sli_abort_fcp_cmpl - Completion handler function for aborted FCP IOCBs
James Smarte59058c2008-08-24 21:49:00 -04009408 * @phba: Pointer to HBA context object
9409 * @cmdiocb: Pointer to command iocb object.
9410 * @rspiocb: Pointer to response iocb object.
9411 *
9412 * This function is called when an aborted FCP iocb completes. This
9413 * function is called by the ring event handler with no lock held.
9414 * This function frees the iocb.
9415 **/
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -04009416void
James Smart2e0fef82007-06-17 19:56:36 -05009417lpfc_sli_abort_fcp_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
9418 struct lpfc_iocbq *rspiocb)
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -04009419{
James Smartcb69f7d2011-12-13 13:21:57 -05009420 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
9421 "3096 ABORT_XRI_CN completing on xri x%x "
9422 "original iotag x%x, abort cmd iotag x%x "
9423 "status 0x%x, reason 0x%x\n",
9424 cmdiocb->iocb.un.acxri.abortContextTag,
9425 cmdiocb->iocb.un.acxri.abortIoTag,
9426 cmdiocb->iotag, rspiocb->iocb.ulpStatus,
9427 rspiocb->iocb.un.ulpWord[4]);
James Bottomley604a3e32005-10-29 10:28:33 -05009428 lpfc_sli_release_iocbq(phba, cmdiocb);
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -04009429 return;
9430}
9431
James Smarte59058c2008-08-24 21:49:00 -04009432/**
James Smart3621a712009-04-06 18:47:14 -04009433 * lpfc_sli_abort_iocb - issue abort for all commands on a host/target/LUN
James Smarte59058c2008-08-24 21:49:00 -04009434 * @vport: Pointer to virtual port.
9435 * @pring: Pointer to driver SLI ring object.
9436 * @tgt_id: SCSI ID of the target.
9437 * @lun_id: LUN ID of the scsi device.
9438 * @abort_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
9439 *
9440 * This function sends an abort command for every SCSI command
9441 * associated with the given virtual port pending on the ring
9442 * filtered by lpfc_sli_validate_fcp_iocb function.
9443 * When abort_cmd == LPFC_CTX_LUN, the function sends abort only to the
9444 * FCP iocbs associated with lun specified by tgt_id and lun_id
9445 * parameters
9446 * When abort_cmd == LPFC_CTX_TGT, the function sends abort only to the
9447 * FCP iocbs associated with SCSI target specified by tgt_id parameter.
9448 * When abort_cmd == LPFC_CTX_HOST, the function sends abort to all
9449 * FCP iocbs associated with virtual port.
9450 * This function returns number of iocbs it failed to abort.
9451 * This function is called with no locks held.
9452 **/
dea31012005-04-17 16:05:31 -05009453int
James Smart51ef4c22007-08-02 11:10:31 -04009454lpfc_sli_abort_iocb(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
9455 uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd abort_cmd)
dea31012005-04-17 16:05:31 -05009456{
James Smart51ef4c22007-08-02 11:10:31 -04009457 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009458 struct lpfc_iocbq *iocbq;
9459 struct lpfc_iocbq *abtsiocb;
dea31012005-04-17 16:05:31 -05009460 IOCB_t *cmd = NULL;
dea31012005-04-17 16:05:31 -05009461 int errcnt = 0, ret_val = 0;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009462 int i;
dea31012005-04-17 16:05:31 -05009463
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009464 for (i = 1; i <= phba->sli.last_iotag; i++) {
9465 iocbq = phba->sli.iocbq_lookup[i];
dea31012005-04-17 16:05:31 -05009466
James Smart51ef4c22007-08-02 11:10:31 -04009467 if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
James Smart2e0fef82007-06-17 19:56:36 -05009468 abort_cmd) != 0)
dea31012005-04-17 16:05:31 -05009469 continue;
9470
9471 /* issue ABTS for this IOCB based on iotag */
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009472 abtsiocb = lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05009473 if (abtsiocb == NULL) {
9474 errcnt++;
9475 continue;
9476 }
dea31012005-04-17 16:05:31 -05009477
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009478 cmd = &iocbq->iocb;
dea31012005-04-17 16:05:31 -05009479 abtsiocb->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
9480 abtsiocb->iocb.un.acxri.abortContextTag = cmd->ulpContext;
James Smartda0436e2009-05-22 14:51:39 -04009481 if (phba->sli_rev == LPFC_SLI_REV4)
9482 abtsiocb->iocb.un.acxri.abortIoTag = iocbq->sli4_xritag;
9483 else
9484 abtsiocb->iocb.un.acxri.abortIoTag = cmd->ulpIoTag;
dea31012005-04-17 16:05:31 -05009485 abtsiocb->iocb.ulpLe = 1;
9486 abtsiocb->iocb.ulpClass = cmd->ulpClass;
James Smart2e0fef82007-06-17 19:56:36 -05009487 abtsiocb->vport = phba->pport;
dea31012005-04-17 16:05:31 -05009488
James Smart5ffc2662009-11-18 15:39:44 -05009489 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
9490 abtsiocb->fcp_wqidx = iocbq->fcp_wqidx;
James Smart341af102010-01-26 23:07:37 -05009491 if (iocbq->iocb_flag & LPFC_IO_FCP)
9492 abtsiocb->iocb_flag |= LPFC_USE_FCPWQIDX;
James Smart5ffc2662009-11-18 15:39:44 -05009493
James Smart2e0fef82007-06-17 19:56:36 -05009494 if (lpfc_is_link_up(phba))
dea31012005-04-17 16:05:31 -05009495 abtsiocb->iocb.ulpCommand = CMD_ABORT_XRI_CN;
9496 else
9497 abtsiocb->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
9498
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -04009499 /* Setup callback routine and issue the command. */
9500 abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
James Smartda0436e2009-05-22 14:51:39 -04009501 ret_val = lpfc_sli_issue_iocb(phba, pring->ringno,
9502 abtsiocb, 0);
dea31012005-04-17 16:05:31 -05009503 if (ret_val == IOCB_ERROR) {
James Bottomley604a3e32005-10-29 10:28:33 -05009504 lpfc_sli_release_iocbq(phba, abtsiocb);
dea31012005-04-17 16:05:31 -05009505 errcnt++;
9506 continue;
9507 }
9508 }
9509
9510 return errcnt;
9511}
9512
James Smarte59058c2008-08-24 21:49:00 -04009513/**
James Smart3621a712009-04-06 18:47:14 -04009514 * lpfc_sli_wake_iocb_wait - lpfc_sli_issue_iocb_wait's completion handler
James Smarte59058c2008-08-24 21:49:00 -04009515 * @phba: Pointer to HBA context object.
9516 * @cmdiocbq: Pointer to command iocb.
9517 * @rspiocbq: Pointer to response iocb.
9518 *
9519 * This function is the completion handler for iocbs issued using
9520 * lpfc_sli_issue_iocb_wait function. This function is called by the
9521 * ring event handler function without any lock held. This function
9522 * can be called from both worker thread context and interrupt
9523 * context. This function also can be called from other thread which
9524 * cleans up the SLI layer objects.
9525 * This function copy the contents of the response iocb to the
9526 * response iocb memory object provided by the caller of
9527 * lpfc_sli_issue_iocb_wait and then wakes up the thread which
9528 * sleeps for the iocb completion.
9529 **/
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009530static void
9531lpfc_sli_wake_iocb_wait(struct lpfc_hba *phba,
9532 struct lpfc_iocbq *cmdiocbq,
9533 struct lpfc_iocbq *rspiocbq)
dea31012005-04-17 16:05:31 -05009534{
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009535 wait_queue_head_t *pdone_q;
9536 unsigned long iflags;
James Smart0f65ff62010-02-26 14:14:23 -05009537 struct lpfc_scsi_buf *lpfc_cmd;
dea31012005-04-17 16:05:31 -05009538
James Smart2e0fef82007-06-17 19:56:36 -05009539 spin_lock_irqsave(&phba->hbalock, iflags);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009540 cmdiocbq->iocb_flag |= LPFC_IO_WAKE;
9541 if (cmdiocbq->context2 && rspiocbq)
9542 memcpy(&((struct lpfc_iocbq *)cmdiocbq->context2)->iocb,
9543 &rspiocbq->iocb, sizeof(IOCB_t));
9544
James Smart0f65ff62010-02-26 14:14:23 -05009545 /* Set the exchange busy flag for task management commands */
9546 if ((cmdiocbq->iocb_flag & LPFC_IO_FCP) &&
9547 !(cmdiocbq->iocb_flag & LPFC_IO_LIBDFC)) {
9548 lpfc_cmd = container_of(cmdiocbq, struct lpfc_scsi_buf,
9549 cur_iocbq);
9550 lpfc_cmd->exch_busy = rspiocbq->iocb_flag & LPFC_EXCHANGE_BUSY;
9551 }
9552
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009553 pdone_q = cmdiocbq->context_un.wait_queue;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009554 if (pdone_q)
9555 wake_up(pdone_q);
James Smart858c9f62007-06-17 19:56:39 -05009556 spin_unlock_irqrestore(&phba->hbalock, iflags);
dea31012005-04-17 16:05:31 -05009557 return;
9558}
9559
James Smarte59058c2008-08-24 21:49:00 -04009560/**
James Smartd11e31d2009-06-10 17:23:06 -04009561 * lpfc_chk_iocb_flg - Test IOCB flag with lock held.
9562 * @phba: Pointer to HBA context object..
9563 * @piocbq: Pointer to command iocb.
9564 * @flag: Flag to test.
9565 *
9566 * This routine grabs the hbalock and then test the iocb_flag to
9567 * see if the passed in flag is set.
9568 * Returns:
9569 * 1 if flag is set.
9570 * 0 if flag is not set.
9571 **/
9572static int
9573lpfc_chk_iocb_flg(struct lpfc_hba *phba,
9574 struct lpfc_iocbq *piocbq, uint32_t flag)
9575{
9576 unsigned long iflags;
9577 int ret;
9578
9579 spin_lock_irqsave(&phba->hbalock, iflags);
9580 ret = piocbq->iocb_flag & flag;
9581 spin_unlock_irqrestore(&phba->hbalock, iflags);
9582 return ret;
9583
9584}
9585
9586/**
James Smart3621a712009-04-06 18:47:14 -04009587 * lpfc_sli_issue_iocb_wait - Synchronous function to issue iocb commands
James Smarte59058c2008-08-24 21:49:00 -04009588 * @phba: Pointer to HBA context object..
9589 * @pring: Pointer to sli ring.
9590 * @piocb: Pointer to command iocb.
9591 * @prspiocbq: Pointer to response iocb.
9592 * @timeout: Timeout in number of seconds.
9593 *
9594 * This function issues the iocb to firmware and waits for the
9595 * iocb to complete. If the iocb command is not
9596 * completed within timeout seconds, it returns IOCB_TIMEDOUT.
9597 * Caller should not free the iocb resources if this function
9598 * returns IOCB_TIMEDOUT.
9599 * The function waits for the iocb completion using an
9600 * non-interruptible wait.
9601 * This function will sleep while waiting for iocb completion.
9602 * So, this function should not be called from any context which
9603 * does not allow sleeping. Due to the same reason, this function
9604 * cannot be called with interrupt disabled.
9605 * This function assumes that the iocb completions occur while
9606 * this function sleep. So, this function cannot be called from
9607 * the thread which process iocb completion for this ring.
9608 * This function clears the iocb_flag of the iocb object before
9609 * issuing the iocb and the iocb completion handler sets this
9610 * flag and wakes this thread when the iocb completes.
9611 * The contents of the response iocb will be copied to prspiocbq
9612 * by the completion handler when the command completes.
9613 * This function returns IOCB_SUCCESS when success.
9614 * This function is called with no lock held.
9615 **/
dea31012005-04-17 16:05:31 -05009616int
James Smart2e0fef82007-06-17 19:56:36 -05009617lpfc_sli_issue_iocb_wait(struct lpfc_hba *phba,
James Smartda0436e2009-05-22 14:51:39 -04009618 uint32_t ring_number,
James Smart2e0fef82007-06-17 19:56:36 -05009619 struct lpfc_iocbq *piocb,
9620 struct lpfc_iocbq *prspiocbq,
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009621 uint32_t timeout)
dea31012005-04-17 16:05:31 -05009622{
Peter Zijlstra7259f0d2006-10-29 22:46:36 -08009623 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009624 long timeleft, timeout_req = 0;
9625 int retval = IOCB_SUCCESS;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05009626 uint32_t creg_val;
James Smart2a9bf3d2010-06-07 15:24:45 -04009627 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
dea31012005-04-17 16:05:31 -05009628 /*
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009629 * If the caller has provided a response iocbq buffer, then context2
9630 * is NULL or its an error.
dea31012005-04-17 16:05:31 -05009631 */
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009632 if (prspiocbq) {
9633 if (piocb->context2)
9634 return IOCB_ERROR;
9635 piocb->context2 = prspiocbq;
dea31012005-04-17 16:05:31 -05009636 }
9637
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009638 piocb->iocb_cmpl = lpfc_sli_wake_iocb_wait;
9639 piocb->context_un.wait_queue = &done_q;
9640 piocb->iocb_flag &= ~LPFC_IO_WAKE;
dea31012005-04-17 16:05:31 -05009641
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05009642 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
James Smart9940b972011-03-11 16:06:12 -05009643 if (lpfc_readl(phba->HCregaddr, &creg_val))
9644 return IOCB_ERROR;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05009645 creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
9646 writel(creg_val, phba->HCregaddr);
9647 readl(phba->HCregaddr); /* flush */
9648 }
9649
James Smart2a9bf3d2010-06-07 15:24:45 -04009650 retval = lpfc_sli_issue_iocb(phba, ring_number, piocb,
9651 SLI_IOCB_RET_IOCB);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009652 if (retval == IOCB_SUCCESS) {
9653 timeout_req = timeout * HZ;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009654 timeleft = wait_event_timeout(done_q,
James Smartd11e31d2009-06-10 17:23:06 -04009655 lpfc_chk_iocb_flg(phba, piocb, LPFC_IO_WAKE),
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009656 timeout_req);
dea31012005-04-17 16:05:31 -05009657
James Smart7054a602007-04-25 09:52:34 -04009658 if (piocb->iocb_flag & LPFC_IO_WAKE) {
9659 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04009660 "0331 IOCB wake signaled\n");
James Smart7054a602007-04-25 09:52:34 -04009661 } else if (timeleft == 0) {
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009662 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04009663 "0338 IOCB wait timeout error - no "
9664 "wake response Data x%x\n", timeout);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009665 retval = IOCB_TIMEDOUT;
James Smart7054a602007-04-25 09:52:34 -04009666 } else {
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009667 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04009668 "0330 IOCB wake NOT set, "
9669 "Data x%x x%lx\n",
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009670 timeout, (timeleft / jiffies));
9671 retval = IOCB_TIMEDOUT;
dea31012005-04-17 16:05:31 -05009672 }
James Smart2a9bf3d2010-06-07 15:24:45 -04009673 } else if (retval == IOCB_BUSY) {
9674 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
9675 "2818 Max IOCBs %d txq cnt %d txcmplq cnt %d\n",
9676 phba->iocb_cnt, pring->txq_cnt, pring->txcmplq_cnt);
9677 return retval;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009678 } else {
9679 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smartd7c255b2008-08-24 21:50:00 -04009680 "0332 IOCB wait issue failed, Data x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04009681 retval);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009682 retval = IOCB_ERROR;
dea31012005-04-17 16:05:31 -05009683 }
9684
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05009685 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
James Smart9940b972011-03-11 16:06:12 -05009686 if (lpfc_readl(phba->HCregaddr, &creg_val))
9687 return IOCB_ERROR;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05009688 creg_val &= ~(HC_R0INT_ENA << LPFC_FCP_RING);
9689 writel(creg_val, phba->HCregaddr);
9690 readl(phba->HCregaddr); /* flush */
9691 }
9692
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009693 if (prspiocbq)
9694 piocb->context2 = NULL;
9695
9696 piocb->context_un.wait_queue = NULL;
9697 piocb->iocb_cmpl = NULL;
dea31012005-04-17 16:05:31 -05009698 return retval;
9699}
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009700
James Smarte59058c2008-08-24 21:49:00 -04009701/**
James Smart3621a712009-04-06 18:47:14 -04009702 * lpfc_sli_issue_mbox_wait - Synchronous function to issue mailbox
James Smarte59058c2008-08-24 21:49:00 -04009703 * @phba: Pointer to HBA context object.
9704 * @pmboxq: Pointer to driver mailbox object.
9705 * @timeout: Timeout in number of seconds.
9706 *
9707 * This function issues the mailbox to firmware and waits for the
9708 * mailbox command to complete. If the mailbox command is not
9709 * completed within timeout seconds, it returns MBX_TIMEOUT.
9710 * The function waits for the mailbox completion using an
9711 * interruptible wait. If the thread is woken up due to a
9712 * signal, MBX_TIMEOUT error is returned to the caller. Caller
9713 * should not free the mailbox resources, if this function returns
9714 * MBX_TIMEOUT.
9715 * This function will sleep while waiting for mailbox completion.
9716 * So, this function should not be called from any context which
9717 * does not allow sleeping. Due to the same reason, this function
9718 * cannot be called with interrupt disabled.
9719 * This function assumes that the mailbox completion occurs while
9720 * this function sleep. So, this function cannot be called from
9721 * the worker thread which processes mailbox completion.
9722 * This function is called in the context of HBA management
9723 * applications.
9724 * This function returns MBX_SUCCESS when successful.
9725 * This function is called with no lock held.
9726 **/
dea31012005-04-17 16:05:31 -05009727int
James Smart2e0fef82007-06-17 19:56:36 -05009728lpfc_sli_issue_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq,
dea31012005-04-17 16:05:31 -05009729 uint32_t timeout)
9730{
Peter Zijlstra7259f0d2006-10-29 22:46:36 -08009731 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
dea31012005-04-17 16:05:31 -05009732 int retval;
James Smart858c9f62007-06-17 19:56:39 -05009733 unsigned long flag;
dea31012005-04-17 16:05:31 -05009734
9735 /* The caller must leave context1 empty. */
James Smart98c9ea52007-10-27 13:37:33 -04009736 if (pmboxq->context1)
James Smart2e0fef82007-06-17 19:56:36 -05009737 return MBX_NOT_FINISHED;
dea31012005-04-17 16:05:31 -05009738
James Smart495a7142008-06-14 22:52:59 -04009739 pmboxq->mbox_flag &= ~LPFC_MBX_WAKE;
dea31012005-04-17 16:05:31 -05009740 /* setup wake call as IOCB callback */
9741 pmboxq->mbox_cmpl = lpfc_sli_wake_mbox_wait;
9742 /* setup context field to pass wait_queue pointer to wake function */
9743 pmboxq->context1 = &done_q;
9744
dea31012005-04-17 16:05:31 -05009745 /* now issue the command */
9746 retval = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
dea31012005-04-17 16:05:31 -05009747 if (retval == MBX_BUSY || retval == MBX_SUCCESS) {
James Smart7054a602007-04-25 09:52:34 -04009748 wait_event_interruptible_timeout(done_q,
9749 pmboxq->mbox_flag & LPFC_MBX_WAKE,
9750 timeout * HZ);
9751
James Smart858c9f62007-06-17 19:56:39 -05009752 spin_lock_irqsave(&phba->hbalock, flag);
dea31012005-04-17 16:05:31 -05009753 pmboxq->context1 = NULL;
James Smart7054a602007-04-25 09:52:34 -04009754 /*
9755 * if LPFC_MBX_WAKE flag is set the mailbox is completed
9756 * else do not free the resources.
9757 */
James Smartd7c47992010-06-08 18:31:54 -04009758 if (pmboxq->mbox_flag & LPFC_MBX_WAKE) {
dea31012005-04-17 16:05:31 -05009759 retval = MBX_SUCCESS;
James Smartd7c47992010-06-08 18:31:54 -04009760 lpfc_sli4_swap_str(phba, pmboxq);
9761 } else {
James Smart7054a602007-04-25 09:52:34 -04009762 retval = MBX_TIMEOUT;
James Smart858c9f62007-06-17 19:56:39 -05009763 pmboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
9764 }
9765 spin_unlock_irqrestore(&phba->hbalock, flag);
dea31012005-04-17 16:05:31 -05009766 }
9767
dea31012005-04-17 16:05:31 -05009768 return retval;
9769}
9770
James Smarte59058c2008-08-24 21:49:00 -04009771/**
James Smart3772a992009-05-22 14:50:54 -04009772 * lpfc_sli_mbox_sys_shutdown - shutdown mailbox command sub-system
James Smarte59058c2008-08-24 21:49:00 -04009773 * @phba: Pointer to HBA context.
9774 *
James Smart3772a992009-05-22 14:50:54 -04009775 * This function is called to shutdown the driver's mailbox sub-system.
9776 * It first marks the mailbox sub-system is in a block state to prevent
9777 * the asynchronous mailbox command from issued off the pending mailbox
9778 * command queue. If the mailbox command sub-system shutdown is due to
9779 * HBA error conditions such as EEH or ERATT, this routine shall invoke
9780 * the mailbox sub-system flush routine to forcefully bring down the
9781 * mailbox sub-system. Otherwise, if it is due to normal condition (such
9782 * as with offline or HBA function reset), this routine will wait for the
9783 * outstanding mailbox command to complete before invoking the mailbox
9784 * sub-system flush routine to gracefully bring down mailbox sub-system.
James Smarte59058c2008-08-24 21:49:00 -04009785 **/
James Smart3772a992009-05-22 14:50:54 -04009786void
9787lpfc_sli_mbox_sys_shutdown(struct lpfc_hba *phba)
James Smartb4c02652006-07-06 15:50:43 -04009788{
James Smart3772a992009-05-22 14:50:54 -04009789 struct lpfc_sli *psli = &phba->sli;
James Smart3772a992009-05-22 14:50:54 -04009790 unsigned long timeout;
9791
James Smarta183a152011-10-10 21:32:43 -04009792 timeout = msecs_to_jiffies(LPFC_MBOX_TMO * 1000) + jiffies;
James Smart3772a992009-05-22 14:50:54 -04009793 spin_lock_irq(&phba->hbalock);
9794 psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
9795 spin_unlock_irq(&phba->hbalock);
9796
9797 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
9798 spin_lock_irq(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -04009799 /* Determine how long we might wait for the active mailbox
9800 * command to be gracefully completed by firmware.
9801 */
James Smarta183a152011-10-10 21:32:43 -04009802 if (phba->sli.mbox_active)
9803 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
9804 phba->sli.mbox_active) *
9805 1000) + jiffies;
9806 spin_unlock_irq(&phba->hbalock);
9807
James Smart3772a992009-05-22 14:50:54 -04009808 while (phba->sli.mbox_active) {
9809 /* Check active mailbox complete status every 2ms */
9810 msleep(2);
9811 if (time_after(jiffies, timeout))
9812 /* Timeout, let the mailbox flush routine to
9813 * forcefully release active mailbox command
9814 */
9815 break;
9816 }
9817 }
9818 lpfc_sli_mbox_sys_flush(phba);
9819}
9820
9821/**
9822 * lpfc_sli_eratt_read - read sli-3 error attention events
9823 * @phba: Pointer to HBA context.
9824 *
9825 * This function is called to read the SLI3 device error attention registers
9826 * for possible error attention events. The caller must hold the hostlock
9827 * with spin_lock_irq().
9828 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03009829 * This function returns 1 when there is Error Attention in the Host Attention
James Smart3772a992009-05-22 14:50:54 -04009830 * Register and returns 0 otherwise.
9831 **/
9832static int
9833lpfc_sli_eratt_read(struct lpfc_hba *phba)
9834{
James Smarted957682007-06-17 19:56:37 -05009835 uint32_t ha_copy;
James Smartb4c02652006-07-06 15:50:43 -04009836
James Smart3772a992009-05-22 14:50:54 -04009837 /* Read chip Host Attention (HA) register */
James Smart9940b972011-03-11 16:06:12 -05009838 if (lpfc_readl(phba->HAregaddr, &ha_copy))
9839 goto unplug_err;
9840
James Smart3772a992009-05-22 14:50:54 -04009841 if (ha_copy & HA_ERATT) {
9842 /* Read host status register to retrieve error event */
James Smart9940b972011-03-11 16:06:12 -05009843 if (lpfc_sli_read_hs(phba))
9844 goto unplug_err;
James Smartb4c02652006-07-06 15:50:43 -04009845
James Smart3772a992009-05-22 14:50:54 -04009846 /* Check if there is a deferred error condition is active */
9847 if ((HS_FFER1 & phba->work_hs) &&
9848 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
James Smartdcf2a4e2010-09-29 11:18:53 -04009849 HS_FFER6 | HS_FFER7 | HS_FFER8) & phba->work_hs)) {
James Smart3772a992009-05-22 14:50:54 -04009850 phba->hba_flag |= DEFER_ERATT;
James Smart3772a992009-05-22 14:50:54 -04009851 /* Clear all interrupt enable conditions */
9852 writel(0, phba->HCregaddr);
9853 readl(phba->HCregaddr);
9854 }
9855
9856 /* Set the driver HA work bitmap */
James Smart3772a992009-05-22 14:50:54 -04009857 phba->work_ha |= HA_ERATT;
9858 /* Indicate polling handles this ERATT */
9859 phba->hba_flag |= HBA_ERATT_HANDLED;
James Smart3772a992009-05-22 14:50:54 -04009860 return 1;
James Smartb4c02652006-07-06 15:50:43 -04009861 }
James Smart3772a992009-05-22 14:50:54 -04009862 return 0;
James Smart9940b972011-03-11 16:06:12 -05009863
9864unplug_err:
9865 /* Set the driver HS work bitmap */
9866 phba->work_hs |= UNPLUG_ERR;
9867 /* Set the driver HA work bitmap */
9868 phba->work_ha |= HA_ERATT;
9869 /* Indicate polling handles this ERATT */
9870 phba->hba_flag |= HBA_ERATT_HANDLED;
9871 return 1;
James Smartb4c02652006-07-06 15:50:43 -04009872}
9873
James Smarte59058c2008-08-24 21:49:00 -04009874/**
James Smartda0436e2009-05-22 14:51:39 -04009875 * lpfc_sli4_eratt_read - read sli-4 error attention events
9876 * @phba: Pointer to HBA context.
9877 *
9878 * This function is called to read the SLI4 device error attention registers
9879 * for possible error attention events. The caller must hold the hostlock
9880 * with spin_lock_irq().
9881 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03009882 * This function returns 1 when there is Error Attention in the Host Attention
James Smartda0436e2009-05-22 14:51:39 -04009883 * Register and returns 0 otherwise.
9884 **/
9885static int
9886lpfc_sli4_eratt_read(struct lpfc_hba *phba)
9887{
9888 uint32_t uerr_sta_hi, uerr_sta_lo;
James Smart2fcee4b2010-12-15 17:57:46 -05009889 uint32_t if_type, portsmphr;
9890 struct lpfc_register portstat_reg;
James Smartda0436e2009-05-22 14:51:39 -04009891
James Smart2fcee4b2010-12-15 17:57:46 -05009892 /*
9893 * For now, use the SLI4 device internal unrecoverable error
James Smartda0436e2009-05-22 14:51:39 -04009894 * registers for error attention. This can be changed later.
9895 */
James Smart2fcee4b2010-12-15 17:57:46 -05009896 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
9897 switch (if_type) {
9898 case LPFC_SLI_INTF_IF_TYPE_0:
James Smart9940b972011-03-11 16:06:12 -05009899 if (lpfc_readl(phba->sli4_hba.u.if_type0.UERRLOregaddr,
9900 &uerr_sta_lo) ||
9901 lpfc_readl(phba->sli4_hba.u.if_type0.UERRHIregaddr,
9902 &uerr_sta_hi)) {
9903 phba->work_hs |= UNPLUG_ERR;
9904 phba->work_ha |= HA_ERATT;
9905 phba->hba_flag |= HBA_ERATT_HANDLED;
9906 return 1;
9907 }
James Smart2fcee4b2010-12-15 17:57:46 -05009908 if ((~phba->sli4_hba.ue_mask_lo & uerr_sta_lo) ||
9909 (~phba->sli4_hba.ue_mask_hi & uerr_sta_hi)) {
9910 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9911 "1423 HBA Unrecoverable error: "
9912 "uerr_lo_reg=0x%x, uerr_hi_reg=0x%x, "
9913 "ue_mask_lo_reg=0x%x, "
9914 "ue_mask_hi_reg=0x%x\n",
9915 uerr_sta_lo, uerr_sta_hi,
9916 phba->sli4_hba.ue_mask_lo,
9917 phba->sli4_hba.ue_mask_hi);
9918 phba->work_status[0] = uerr_sta_lo;
9919 phba->work_status[1] = uerr_sta_hi;
9920 phba->work_ha |= HA_ERATT;
9921 phba->hba_flag |= HBA_ERATT_HANDLED;
9922 return 1;
9923 }
9924 break;
9925 case LPFC_SLI_INTF_IF_TYPE_2:
James Smart9940b972011-03-11 16:06:12 -05009926 if (lpfc_readl(phba->sli4_hba.u.if_type2.STATUSregaddr,
9927 &portstat_reg.word0) ||
9928 lpfc_readl(phba->sli4_hba.PSMPHRregaddr,
9929 &portsmphr)){
9930 phba->work_hs |= UNPLUG_ERR;
9931 phba->work_ha |= HA_ERATT;
9932 phba->hba_flag |= HBA_ERATT_HANDLED;
9933 return 1;
9934 }
James Smart2fcee4b2010-12-15 17:57:46 -05009935 if (bf_get(lpfc_sliport_status_err, &portstat_reg)) {
9936 phba->work_status[0] =
9937 readl(phba->sli4_hba.u.if_type2.ERR1regaddr);
9938 phba->work_status[1] =
9939 readl(phba->sli4_hba.u.if_type2.ERR2regaddr);
9940 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart2e90f4b2011-12-13 13:22:37 -05009941 "2885 Port Status Event: "
James Smart2fcee4b2010-12-15 17:57:46 -05009942 "port status reg 0x%x, "
9943 "port smphr reg 0x%x, "
9944 "error 1=0x%x, error 2=0x%x\n",
9945 portstat_reg.word0,
9946 portsmphr,
9947 phba->work_status[0],
9948 phba->work_status[1]);
9949 phba->work_ha |= HA_ERATT;
9950 phba->hba_flag |= HBA_ERATT_HANDLED;
9951 return 1;
9952 }
9953 break;
9954 case LPFC_SLI_INTF_IF_TYPE_1:
9955 default:
James Smarta747c9c2009-11-18 15:41:10 -05009956 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart2fcee4b2010-12-15 17:57:46 -05009957 "2886 HBA Error Attention on unsupported "
9958 "if type %d.", if_type);
James Smarta747c9c2009-11-18 15:41:10 -05009959 return 1;
James Smartda0436e2009-05-22 14:51:39 -04009960 }
James Smart2fcee4b2010-12-15 17:57:46 -05009961
James Smartda0436e2009-05-22 14:51:39 -04009962 return 0;
9963}
9964
9965/**
James Smart3621a712009-04-06 18:47:14 -04009966 * lpfc_sli_check_eratt - check error attention events
James Smart93996272008-08-24 21:50:30 -04009967 * @phba: Pointer to HBA context.
9968 *
James Smart3772a992009-05-22 14:50:54 -04009969 * This function is called from timer soft interrupt context to check HBA's
James Smart93996272008-08-24 21:50:30 -04009970 * error attention register bit for error attention events.
9971 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03009972 * This function returns 1 when there is Error Attention in the Host Attention
James Smart93996272008-08-24 21:50:30 -04009973 * Register and returns 0 otherwise.
9974 **/
9975int
9976lpfc_sli_check_eratt(struct lpfc_hba *phba)
9977{
9978 uint32_t ha_copy;
9979
9980 /* If somebody is waiting to handle an eratt, don't process it
9981 * here. The brdkill function will do this.
9982 */
9983 if (phba->link_flag & LS_IGNORE_ERATT)
9984 return 0;
9985
9986 /* Check if interrupt handler handles this ERATT */
9987 spin_lock_irq(&phba->hbalock);
9988 if (phba->hba_flag & HBA_ERATT_HANDLED) {
9989 /* Interrupt handler has handled ERATT */
9990 spin_unlock_irq(&phba->hbalock);
9991 return 0;
9992 }
9993
James Smarta257bf92009-04-06 18:48:10 -04009994 /*
9995 * If there is deferred error attention, do not check for error
9996 * attention
9997 */
9998 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
9999 spin_unlock_irq(&phba->hbalock);
10000 return 0;
10001 }
10002
James Smart3772a992009-05-22 14:50:54 -040010003 /* If PCI channel is offline, don't process it */
10004 if (unlikely(pci_channel_offline(phba->pcidev))) {
James Smart93996272008-08-24 21:50:30 -040010005 spin_unlock_irq(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -040010006 return 0;
10007 }
10008
10009 switch (phba->sli_rev) {
10010 case LPFC_SLI_REV2:
10011 case LPFC_SLI_REV3:
10012 /* Read chip Host Attention (HA) register */
10013 ha_copy = lpfc_sli_eratt_read(phba);
10014 break;
James Smartda0436e2009-05-22 14:51:39 -040010015 case LPFC_SLI_REV4:
James Smart2fcee4b2010-12-15 17:57:46 -050010016 /* Read device Uncoverable Error (UERR) registers */
James Smartda0436e2009-05-22 14:51:39 -040010017 ha_copy = lpfc_sli4_eratt_read(phba);
10018 break;
James Smart3772a992009-05-22 14:50:54 -040010019 default:
10020 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10021 "0299 Invalid SLI revision (%d)\n",
10022 phba->sli_rev);
10023 ha_copy = 0;
10024 break;
James Smart93996272008-08-24 21:50:30 -040010025 }
10026 spin_unlock_irq(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -040010027
10028 return ha_copy;
10029}
10030
10031/**
10032 * lpfc_intr_state_check - Check device state for interrupt handling
10033 * @phba: Pointer to HBA context.
10034 *
10035 * This inline routine checks whether a device or its PCI slot is in a state
10036 * that the interrupt should be handled.
10037 *
10038 * This function returns 0 if the device or the PCI slot is in a state that
10039 * interrupt should be handled, otherwise -EIO.
10040 */
10041static inline int
10042lpfc_intr_state_check(struct lpfc_hba *phba)
10043{
10044 /* If the pci channel is offline, ignore all the interrupts */
10045 if (unlikely(pci_channel_offline(phba->pcidev)))
10046 return -EIO;
10047
10048 /* Update device level interrupt statistics */
10049 phba->sli.slistat.sli_intr++;
10050
10051 /* Ignore all interrupts during initialization. */
10052 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
10053 return -EIO;
10054
James Smart93996272008-08-24 21:50:30 -040010055 return 0;
10056}
10057
10058/**
James Smart3772a992009-05-22 14:50:54 -040010059 * lpfc_sli_sp_intr_handler - Slow-path interrupt handler to SLI-3 device
James Smarte59058c2008-08-24 21:49:00 -040010060 * @irq: Interrupt number.
10061 * @dev_id: The device context pointer.
10062 *
James Smart93996272008-08-24 21:50:30 -040010063 * This function is directly called from the PCI layer as an interrupt
James Smart3772a992009-05-22 14:50:54 -040010064 * service routine when device with SLI-3 interface spec is enabled with
10065 * MSI-X multi-message interrupt mode and there are slow-path events in
10066 * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
10067 * interrupt mode, this function is called as part of the device-level
10068 * interrupt handler. When the PCI slot is in error recovery or the HBA
10069 * is undergoing initialization, the interrupt handler will not process
10070 * the interrupt. The link attention and ELS ring attention events are
10071 * handled by the worker thread. The interrupt handler signals the worker
10072 * thread and returns for these events. This function is called without
10073 * any lock held. It gets the hbalock to access and update SLI data
James Smart93996272008-08-24 21:50:30 -040010074 * structures.
10075 *
10076 * This function returns IRQ_HANDLED when interrupt is handled else it
10077 * returns IRQ_NONE.
James Smarte59058c2008-08-24 21:49:00 -040010078 **/
dea31012005-04-17 16:05:31 -050010079irqreturn_t
James Smart3772a992009-05-22 14:50:54 -040010080lpfc_sli_sp_intr_handler(int irq, void *dev_id)
dea31012005-04-17 16:05:31 -050010081{
James Smart2e0fef82007-06-17 19:56:36 -050010082 struct lpfc_hba *phba;
James Smarta747c9c2009-11-18 15:41:10 -050010083 uint32_t ha_copy, hc_copy;
dea31012005-04-17 16:05:31 -050010084 uint32_t work_ha_copy;
10085 unsigned long status;
James Smart5b75da22008-12-04 22:39:35 -050010086 unsigned long iflag;
dea31012005-04-17 16:05:31 -050010087 uint32_t control;
10088
James Smart92d7f7b2007-06-17 19:56:38 -050010089 MAILBOX_t *mbox, *pmbox;
James Smart858c9f62007-06-17 19:56:39 -050010090 struct lpfc_vport *vport;
10091 struct lpfc_nodelist *ndlp;
10092 struct lpfc_dmabuf *mp;
James Smart92d7f7b2007-06-17 19:56:38 -050010093 LPFC_MBOXQ_t *pmb;
10094 int rc;
10095
dea31012005-04-17 16:05:31 -050010096 /*
10097 * Get the driver's phba structure from the dev_id and
10098 * assume the HBA is not interrupting.
10099 */
James Smart93996272008-08-24 21:50:30 -040010100 phba = (struct lpfc_hba *)dev_id;
dea31012005-04-17 16:05:31 -050010101
10102 if (unlikely(!phba))
10103 return IRQ_NONE;
10104
dea31012005-04-17 16:05:31 -050010105 /*
James Smart93996272008-08-24 21:50:30 -040010106 * Stuff needs to be attented to when this function is invoked as an
10107 * individual interrupt handler in MSI-X multi-message interrupt mode
dea31012005-04-17 16:05:31 -050010108 */
James Smart93996272008-08-24 21:50:30 -040010109 if (phba->intr_type == MSIX) {
James Smart3772a992009-05-22 14:50:54 -040010110 /* Check device state for handling interrupt */
10111 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -040010112 return IRQ_NONE;
10113 /* Need to read HA REG for slow-path events */
James Smart5b75da22008-12-04 22:39:35 -050010114 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart9940b972011-03-11 16:06:12 -050010115 if (lpfc_readl(phba->HAregaddr, &ha_copy))
10116 goto unplug_error;
James Smart93996272008-08-24 21:50:30 -040010117 /* If somebody is waiting to handle an eratt don't process it
10118 * here. The brdkill function will do this.
10119 */
10120 if (phba->link_flag & LS_IGNORE_ERATT)
10121 ha_copy &= ~HA_ERATT;
10122 /* Check the need for handling ERATT in interrupt handler */
10123 if (ha_copy & HA_ERATT) {
10124 if (phba->hba_flag & HBA_ERATT_HANDLED)
10125 /* ERATT polling has handled ERATT */
10126 ha_copy &= ~HA_ERATT;
10127 else
10128 /* Indicate interrupt handler handles ERATT */
10129 phba->hba_flag |= HBA_ERATT_HANDLED;
10130 }
James Smarta257bf92009-04-06 18:48:10 -040010131
10132 /*
10133 * If there is deferred error attention, do not check for any
10134 * interrupt.
10135 */
10136 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
James Smart3772a992009-05-22 14:50:54 -040010137 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -040010138 return IRQ_NONE;
10139 }
10140
James Smart93996272008-08-24 21:50:30 -040010141 /* Clear up only attention source related to slow-path */
James Smart9940b972011-03-11 16:06:12 -050010142 if (lpfc_readl(phba->HCregaddr, &hc_copy))
10143 goto unplug_error;
10144
James Smarta747c9c2009-11-18 15:41:10 -050010145 writel(hc_copy & ~(HC_MBINT_ENA | HC_R2INT_ENA |
10146 HC_LAINT_ENA | HC_ERINT_ENA),
10147 phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -040010148 writel((ha_copy & (HA_MBATT | HA_R2_CLR_MSK)),
10149 phba->HAregaddr);
James Smarta747c9c2009-11-18 15:41:10 -050010150 writel(hc_copy, phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -040010151 readl(phba->HAregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -050010152 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -040010153 } else
10154 ha_copy = phba->ha_copy;
dea31012005-04-17 16:05:31 -050010155
dea31012005-04-17 16:05:31 -050010156 work_ha_copy = ha_copy & phba->work_ha_mask;
10157
James Smart93996272008-08-24 21:50:30 -040010158 if (work_ha_copy) {
dea31012005-04-17 16:05:31 -050010159 if (work_ha_copy & HA_LATT) {
10160 if (phba->sli.sli_flag & LPFC_PROCESS_LA) {
10161 /*
10162 * Turn off Link Attention interrupts
10163 * until CLEAR_LA done
10164 */
James Smart5b75da22008-12-04 22:39:35 -050010165 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -050010166 phba->sli.sli_flag &= ~LPFC_PROCESS_LA;
James Smart9940b972011-03-11 16:06:12 -050010167 if (lpfc_readl(phba->HCregaddr, &control))
10168 goto unplug_error;
dea31012005-04-17 16:05:31 -050010169 control &= ~HC_LAINT_ENA;
10170 writel(control, phba->HCregaddr);
10171 readl(phba->HCregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -050010172 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -050010173 }
10174 else
10175 work_ha_copy &= ~HA_LATT;
10176 }
10177
James Smart93996272008-08-24 21:50:30 -040010178 if (work_ha_copy & ~(HA_ERATT | HA_MBATT | HA_LATT)) {
James Smart858c9f62007-06-17 19:56:39 -050010179 /*
10180 * Turn off Slow Rings interrupts, LPFC_ELS_RING is
10181 * the only slow ring.
10182 */
10183 status = (work_ha_copy &
10184 (HA_RXMASK << (4*LPFC_ELS_RING)));
10185 status >>= (4*LPFC_ELS_RING);
10186 if (status & HA_RXMASK) {
James Smart5b75da22008-12-04 22:39:35 -050010187 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart9940b972011-03-11 16:06:12 -050010188 if (lpfc_readl(phba->HCregaddr, &control))
10189 goto unplug_error;
James Smarta58cbd52007-08-02 11:09:43 -040010190
10191 lpfc_debugfs_slow_ring_trc(phba,
10192 "ISR slow ring: ctl:x%x stat:x%x isrcnt:x%x",
10193 control, status,
10194 (uint32_t)phba->sli.slistat.sli_intr);
10195
James Smart858c9f62007-06-17 19:56:39 -050010196 if (control & (HC_R0INT_ENA << LPFC_ELS_RING)) {
James Smarta58cbd52007-08-02 11:09:43 -040010197 lpfc_debugfs_slow_ring_trc(phba,
10198 "ISR Disable ring:"
10199 "pwork:x%x hawork:x%x wait:x%x",
10200 phba->work_ha, work_ha_copy,
10201 (uint32_t)((unsigned long)
James Smart5e9d9b82008-06-14 22:52:53 -040010202 &phba->work_waitq));
James Smarta58cbd52007-08-02 11:09:43 -040010203
James Smart858c9f62007-06-17 19:56:39 -050010204 control &=
10205 ~(HC_R0INT_ENA << LPFC_ELS_RING);
dea31012005-04-17 16:05:31 -050010206 writel(control, phba->HCregaddr);
10207 readl(phba->HCregaddr); /* flush */
dea31012005-04-17 16:05:31 -050010208 }
James Smarta58cbd52007-08-02 11:09:43 -040010209 else {
10210 lpfc_debugfs_slow_ring_trc(phba,
10211 "ISR slow ring: pwork:"
10212 "x%x hawork:x%x wait:x%x",
10213 phba->work_ha, work_ha_copy,
10214 (uint32_t)((unsigned long)
James Smart5e9d9b82008-06-14 22:52:53 -040010215 &phba->work_waitq));
James Smarta58cbd52007-08-02 11:09:43 -040010216 }
James Smart5b75da22008-12-04 22:39:35 -050010217 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -050010218 }
10219 }
James Smart5b75da22008-12-04 22:39:35 -050010220 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -040010221 if (work_ha_copy & HA_ERATT) {
James Smart9940b972011-03-11 16:06:12 -050010222 if (lpfc_sli_read_hs(phba))
10223 goto unplug_error;
James Smarta257bf92009-04-06 18:48:10 -040010224 /*
10225 * Check if there is a deferred error condition
10226 * is active
10227 */
10228 if ((HS_FFER1 & phba->work_hs) &&
10229 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
James Smartdcf2a4e2010-09-29 11:18:53 -040010230 HS_FFER6 | HS_FFER7 | HS_FFER8) &
10231 phba->work_hs)) {
James Smarta257bf92009-04-06 18:48:10 -040010232 phba->hba_flag |= DEFER_ERATT;
10233 /* Clear all interrupt enable conditions */
10234 writel(0, phba->HCregaddr);
10235 readl(phba->HCregaddr);
10236 }
10237 }
10238
James Smart93996272008-08-24 21:50:30 -040010239 if ((work_ha_copy & HA_MBATT) && (phba->sli.mbox_active)) {
James Smart92d7f7b2007-06-17 19:56:38 -050010240 pmb = phba->sli.mbox_active;
James Smart04c68492009-05-22 14:52:52 -040010241 pmbox = &pmb->u.mb;
James Smart34b02dc2008-08-24 21:49:55 -040010242 mbox = phba->mbox;
James Smart858c9f62007-06-17 19:56:39 -050010243 vport = pmb->vport;
James Smart92d7f7b2007-06-17 19:56:38 -050010244
10245 /* First check out the status word */
10246 lpfc_sli_pcimem_bcopy(mbox, pmbox, sizeof(uint32_t));
10247 if (pmbox->mbxOwner != OWN_HOST) {
James Smart5b75da22008-12-04 22:39:35 -050010248 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart92d7f7b2007-06-17 19:56:38 -050010249 /*
10250 * Stray Mailbox Interrupt, mbxCommand <cmd>
10251 * mbxStatus <status>
10252 */
James Smart09372822008-01-11 01:52:54 -050010253 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
James Smart92d7f7b2007-06-17 19:56:38 -050010254 LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -040010255 "(%d):0304 Stray Mailbox "
James Smart92d7f7b2007-06-17 19:56:38 -050010256 "Interrupt mbxCommand x%x "
10257 "mbxStatus x%x\n",
James Smarte8b62012007-08-02 11:10:09 -040010258 (vport ? vport->vpi : 0),
James Smart92d7f7b2007-06-17 19:56:38 -050010259 pmbox->mbxCommand,
10260 pmbox->mbxStatus);
James Smart09372822008-01-11 01:52:54 -050010261 /* clear mailbox attention bit */
10262 work_ha_copy &= ~HA_MBATT;
10263 } else {
James Smart97eab632008-04-07 10:16:05 -040010264 phba->sli.mbox_active = NULL;
James Smart5b75da22008-12-04 22:39:35 -050010265 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart09372822008-01-11 01:52:54 -050010266 phba->last_completion_time = jiffies;
10267 del_timer(&phba->sli.mbox_tmo);
James Smart09372822008-01-11 01:52:54 -050010268 if (pmb->mbox_cmpl) {
10269 lpfc_sli_pcimem_bcopy(mbox, pmbox,
10270 MAILBOX_CMD_SIZE);
James Smart7a470272010-03-15 11:25:20 -040010271 if (pmb->out_ext_byte_len &&
10272 pmb->context2)
10273 lpfc_sli_pcimem_bcopy(
10274 phba->mbox_ext,
10275 pmb->context2,
10276 pmb->out_ext_byte_len);
James Smart858c9f62007-06-17 19:56:39 -050010277 }
James Smart09372822008-01-11 01:52:54 -050010278 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
10279 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
10280
10281 lpfc_debugfs_disc_trc(vport,
10282 LPFC_DISC_TRC_MBOX_VPORT,
10283 "MBOX dflt rpi: : "
10284 "status:x%x rpi:x%x",
10285 (uint32_t)pmbox->mbxStatus,
10286 pmbox->un.varWords[0], 0);
10287
10288 if (!pmbox->mbxStatus) {
10289 mp = (struct lpfc_dmabuf *)
10290 (pmb->context1);
10291 ndlp = (struct lpfc_nodelist *)
10292 pmb->context2;
10293
10294 /* Reg_LOGIN of dflt RPI was
10295 * successful. new lets get
10296 * rid of the RPI using the
10297 * same mbox buffer.
10298 */
10299 lpfc_unreg_login(phba,
10300 vport->vpi,
10301 pmbox->un.varWords[0],
10302 pmb);
10303 pmb->mbox_cmpl =
10304 lpfc_mbx_cmpl_dflt_rpi;
10305 pmb->context1 = mp;
10306 pmb->context2 = ndlp;
10307 pmb->vport = vport;
James Smart58da1ff2008-04-07 10:15:56 -040010308 rc = lpfc_sli_issue_mbox(phba,
10309 pmb,
10310 MBX_NOWAIT);
10311 if (rc != MBX_BUSY)
10312 lpfc_printf_log(phba,
10313 KERN_ERR,
10314 LOG_MBOX | LOG_SLI,
James Smartd7c255b2008-08-24 21:50:00 -040010315 "0350 rc should have"
James Smart6a9c52c2009-10-02 15:16:51 -040010316 "been MBX_BUSY\n");
James Smart3772a992009-05-22 14:50:54 -040010317 if (rc != MBX_NOT_FINISHED)
10318 goto send_current_mbox;
James Smart09372822008-01-11 01:52:54 -050010319 }
10320 }
James Smart5b75da22008-12-04 22:39:35 -050010321 spin_lock_irqsave(
10322 &phba->pport->work_port_lock,
10323 iflag);
James Smart09372822008-01-11 01:52:54 -050010324 phba->pport->work_port_events &=
10325 ~WORKER_MBOX_TMO;
James Smart5b75da22008-12-04 22:39:35 -050010326 spin_unlock_irqrestore(
10327 &phba->pport->work_port_lock,
10328 iflag);
James Smart09372822008-01-11 01:52:54 -050010329 lpfc_mbox_cmpl_put(phba, pmb);
James Smart858c9f62007-06-17 19:56:39 -050010330 }
James Smart97eab632008-04-07 10:16:05 -040010331 } else
James Smart5b75da22008-12-04 22:39:35 -050010332 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -040010333
James Smart92d7f7b2007-06-17 19:56:38 -050010334 if ((work_ha_copy & HA_MBATT) &&
10335 (phba->sli.mbox_active == NULL)) {
James Smart858c9f62007-06-17 19:56:39 -050010336send_current_mbox:
James Smart92d7f7b2007-06-17 19:56:38 -050010337 /* Process next mailbox command if there is one */
James Smart58da1ff2008-04-07 10:15:56 -040010338 do {
10339 rc = lpfc_sli_issue_mbox(phba, NULL,
10340 MBX_NOWAIT);
10341 } while (rc == MBX_NOT_FINISHED);
10342 if (rc != MBX_SUCCESS)
10343 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
10344 LOG_SLI, "0349 rc should be "
James Smart6a9c52c2009-10-02 15:16:51 -040010345 "MBX_SUCCESS\n");
James Smart92d7f7b2007-06-17 19:56:38 -050010346 }
10347
James Smart5b75da22008-12-04 22:39:35 -050010348 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -050010349 phba->work_ha |= work_ha_copy;
James Smart5b75da22008-12-04 22:39:35 -050010350 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart5e9d9b82008-06-14 22:52:53 -040010351 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -050010352 }
James Smart93996272008-08-24 21:50:30 -040010353 return IRQ_HANDLED;
James Smart9940b972011-03-11 16:06:12 -050010354unplug_error:
10355 spin_unlock_irqrestore(&phba->hbalock, iflag);
10356 return IRQ_HANDLED;
dea31012005-04-17 16:05:31 -050010357
James Smart3772a992009-05-22 14:50:54 -040010358} /* lpfc_sli_sp_intr_handler */
James Smart93996272008-08-24 21:50:30 -040010359
10360/**
James Smart3772a992009-05-22 14:50:54 -040010361 * lpfc_sli_fp_intr_handler - Fast-path interrupt handler to SLI-3 device.
James Smart93996272008-08-24 21:50:30 -040010362 * @irq: Interrupt number.
10363 * @dev_id: The device context pointer.
10364 *
10365 * This function is directly called from the PCI layer as an interrupt
James Smart3772a992009-05-22 14:50:54 -040010366 * service routine when device with SLI-3 interface spec is enabled with
10367 * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
10368 * ring event in the HBA. However, when the device is enabled with either
10369 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
10370 * device-level interrupt handler. When the PCI slot is in error recovery
10371 * or the HBA is undergoing initialization, the interrupt handler will not
10372 * process the interrupt. The SCSI FCP fast-path ring event are handled in
10373 * the intrrupt context. This function is called without any lock held.
10374 * It gets the hbalock to access and update SLI data structures.
James Smart93996272008-08-24 21:50:30 -040010375 *
10376 * This function returns IRQ_HANDLED when interrupt is handled else it
10377 * returns IRQ_NONE.
10378 **/
10379irqreturn_t
James Smart3772a992009-05-22 14:50:54 -040010380lpfc_sli_fp_intr_handler(int irq, void *dev_id)
James Smart93996272008-08-24 21:50:30 -040010381{
10382 struct lpfc_hba *phba;
10383 uint32_t ha_copy;
10384 unsigned long status;
James Smart5b75da22008-12-04 22:39:35 -050010385 unsigned long iflag;
James Smart93996272008-08-24 21:50:30 -040010386
10387 /* Get the driver's phba structure from the dev_id and
10388 * assume the HBA is not interrupting.
10389 */
10390 phba = (struct lpfc_hba *) dev_id;
10391
10392 if (unlikely(!phba))
10393 return IRQ_NONE;
dea31012005-04-17 16:05:31 -050010394
10395 /*
James Smart93996272008-08-24 21:50:30 -040010396 * Stuff needs to be attented to when this function is invoked as an
10397 * individual interrupt handler in MSI-X multi-message interrupt mode
dea31012005-04-17 16:05:31 -050010398 */
James Smart93996272008-08-24 21:50:30 -040010399 if (phba->intr_type == MSIX) {
James Smart3772a992009-05-22 14:50:54 -040010400 /* Check device state for handling interrupt */
10401 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -040010402 return IRQ_NONE;
10403 /* Need to read HA REG for FCP ring and other ring events */
James Smart9940b972011-03-11 16:06:12 -050010404 if (lpfc_readl(phba->HAregaddr, &ha_copy))
10405 return IRQ_HANDLED;
James Smart93996272008-08-24 21:50:30 -040010406 /* Clear up only attention source related to fast-path */
James Smart5b75da22008-12-04 22:39:35 -050010407 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -040010408 /*
10409 * If there is deferred error attention, do not check for
10410 * any interrupt.
10411 */
10412 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
James Smart3772a992009-05-22 14:50:54 -040010413 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -040010414 return IRQ_NONE;
10415 }
James Smart93996272008-08-24 21:50:30 -040010416 writel((ha_copy & (HA_R0_CLR_MSK | HA_R1_CLR_MSK)),
10417 phba->HAregaddr);
10418 readl(phba->HAregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -050010419 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -040010420 } else
10421 ha_copy = phba->ha_copy;
10422
10423 /*
10424 * Process all events on FCP ring. Take the optimized path for FCP IO.
10425 */
10426 ha_copy &= ~(phba->work_ha_mask);
10427
10428 status = (ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
dea31012005-04-17 16:05:31 -050010429 status >>= (4*LPFC_FCP_RING);
James Smart858c9f62007-06-17 19:56:39 -050010430 if (status & HA_RXMASK)
dea31012005-04-17 16:05:31 -050010431 lpfc_sli_handle_fast_ring_event(phba,
10432 &phba->sli.ring[LPFC_FCP_RING],
10433 status);
James Smarta4bc3372006-12-02 13:34:16 -050010434
10435 if (phba->cfg_multi_ring_support == 2) {
10436 /*
James Smart93996272008-08-24 21:50:30 -040010437 * Process all events on extra ring. Take the optimized path
10438 * for extra ring IO.
James Smarta4bc3372006-12-02 13:34:16 -050010439 */
James Smart93996272008-08-24 21:50:30 -040010440 status = (ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
James Smarta4bc3372006-12-02 13:34:16 -050010441 status >>= (4*LPFC_EXTRA_RING);
James Smart858c9f62007-06-17 19:56:39 -050010442 if (status & HA_RXMASK) {
James Smarta4bc3372006-12-02 13:34:16 -050010443 lpfc_sli_handle_fast_ring_event(phba,
10444 &phba->sli.ring[LPFC_EXTRA_RING],
10445 status);
10446 }
10447 }
dea31012005-04-17 16:05:31 -050010448 return IRQ_HANDLED;
James Smart3772a992009-05-22 14:50:54 -040010449} /* lpfc_sli_fp_intr_handler */
dea31012005-04-17 16:05:31 -050010450
James Smart93996272008-08-24 21:50:30 -040010451/**
James Smart3772a992009-05-22 14:50:54 -040010452 * lpfc_sli_intr_handler - Device-level interrupt handler to SLI-3 device
James Smart93996272008-08-24 21:50:30 -040010453 * @irq: Interrupt number.
10454 * @dev_id: The device context pointer.
10455 *
James Smart3772a992009-05-22 14:50:54 -040010456 * This function is the HBA device-level interrupt handler to device with
10457 * SLI-3 interface spec, called from the PCI layer when either MSI or
10458 * Pin-IRQ interrupt mode is enabled and there is an event in the HBA which
10459 * requires driver attention. This function invokes the slow-path interrupt
10460 * attention handling function and fast-path interrupt attention handling
10461 * function in turn to process the relevant HBA attention events. This
10462 * function is called without any lock held. It gets the hbalock to access
10463 * and update SLI data structures.
James Smart93996272008-08-24 21:50:30 -040010464 *
10465 * This function returns IRQ_HANDLED when interrupt is handled, else it
10466 * returns IRQ_NONE.
10467 **/
10468irqreturn_t
James Smart3772a992009-05-22 14:50:54 -040010469lpfc_sli_intr_handler(int irq, void *dev_id)
James Smart93996272008-08-24 21:50:30 -040010470{
10471 struct lpfc_hba *phba;
10472 irqreturn_t sp_irq_rc, fp_irq_rc;
10473 unsigned long status1, status2;
James Smarta747c9c2009-11-18 15:41:10 -050010474 uint32_t hc_copy;
James Smart93996272008-08-24 21:50:30 -040010475
10476 /*
10477 * Get the driver's phba structure from the dev_id and
10478 * assume the HBA is not interrupting.
10479 */
10480 phba = (struct lpfc_hba *) dev_id;
10481
10482 if (unlikely(!phba))
10483 return IRQ_NONE;
10484
James Smart3772a992009-05-22 14:50:54 -040010485 /* Check device state for handling interrupt */
10486 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -040010487 return IRQ_NONE;
10488
10489 spin_lock(&phba->hbalock);
James Smart9940b972011-03-11 16:06:12 -050010490 if (lpfc_readl(phba->HAregaddr, &phba->ha_copy)) {
10491 spin_unlock(&phba->hbalock);
10492 return IRQ_HANDLED;
10493 }
10494
James Smart93996272008-08-24 21:50:30 -040010495 if (unlikely(!phba->ha_copy)) {
10496 spin_unlock(&phba->hbalock);
10497 return IRQ_NONE;
10498 } else if (phba->ha_copy & HA_ERATT) {
10499 if (phba->hba_flag & HBA_ERATT_HANDLED)
10500 /* ERATT polling has handled ERATT */
10501 phba->ha_copy &= ~HA_ERATT;
10502 else
10503 /* Indicate interrupt handler handles ERATT */
10504 phba->hba_flag |= HBA_ERATT_HANDLED;
10505 }
10506
James Smarta257bf92009-04-06 18:48:10 -040010507 /*
10508 * If there is deferred error attention, do not check for any interrupt.
10509 */
10510 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
Dan Carpenterec21b3b2010-08-08 00:15:17 +020010511 spin_unlock(&phba->hbalock);
James Smarta257bf92009-04-06 18:48:10 -040010512 return IRQ_NONE;
10513 }
10514
James Smart93996272008-08-24 21:50:30 -040010515 /* Clear attention sources except link and error attentions */
James Smart9940b972011-03-11 16:06:12 -050010516 if (lpfc_readl(phba->HCregaddr, &hc_copy)) {
10517 spin_unlock(&phba->hbalock);
10518 return IRQ_HANDLED;
10519 }
James Smarta747c9c2009-11-18 15:41:10 -050010520 writel(hc_copy & ~(HC_MBINT_ENA | HC_R0INT_ENA | HC_R1INT_ENA
10521 | HC_R2INT_ENA | HC_LAINT_ENA | HC_ERINT_ENA),
10522 phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -040010523 writel((phba->ha_copy & ~(HA_LATT | HA_ERATT)), phba->HAregaddr);
James Smarta747c9c2009-11-18 15:41:10 -050010524 writel(hc_copy, phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -040010525 readl(phba->HAregaddr); /* flush */
10526 spin_unlock(&phba->hbalock);
10527
10528 /*
10529 * Invokes slow-path host attention interrupt handling as appropriate.
10530 */
10531
10532 /* status of events with mailbox and link attention */
10533 status1 = phba->ha_copy & (HA_MBATT | HA_LATT | HA_ERATT);
10534
10535 /* status of events with ELS ring */
10536 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_ELS_RING)));
10537 status2 >>= (4*LPFC_ELS_RING);
10538
10539 if (status1 || (status2 & HA_RXMASK))
James Smart3772a992009-05-22 14:50:54 -040010540 sp_irq_rc = lpfc_sli_sp_intr_handler(irq, dev_id);
James Smart93996272008-08-24 21:50:30 -040010541 else
10542 sp_irq_rc = IRQ_NONE;
10543
10544 /*
10545 * Invoke fast-path host attention interrupt handling as appropriate.
10546 */
10547
10548 /* status of events with FCP ring */
10549 status1 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
10550 status1 >>= (4*LPFC_FCP_RING);
10551
10552 /* status of events with extra ring */
10553 if (phba->cfg_multi_ring_support == 2) {
10554 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
10555 status2 >>= (4*LPFC_EXTRA_RING);
10556 } else
10557 status2 = 0;
10558
10559 if ((status1 & HA_RXMASK) || (status2 & HA_RXMASK))
James Smart3772a992009-05-22 14:50:54 -040010560 fp_irq_rc = lpfc_sli_fp_intr_handler(irq, dev_id);
James Smart93996272008-08-24 21:50:30 -040010561 else
10562 fp_irq_rc = IRQ_NONE;
10563
10564 /* Return device-level interrupt handling status */
10565 return (sp_irq_rc == IRQ_HANDLED) ? sp_irq_rc : fp_irq_rc;
James Smart3772a992009-05-22 14:50:54 -040010566} /* lpfc_sli_intr_handler */
James Smart4f774512009-05-22 14:52:35 -040010567
10568/**
10569 * lpfc_sli4_fcp_xri_abort_event_proc - Process fcp xri abort event
10570 * @phba: pointer to lpfc hba data structure.
10571 *
10572 * This routine is invoked by the worker thread to process all the pending
10573 * SLI4 FCP abort XRI events.
10574 **/
10575void lpfc_sli4_fcp_xri_abort_event_proc(struct lpfc_hba *phba)
10576{
10577 struct lpfc_cq_event *cq_event;
10578
10579 /* First, declare the fcp xri abort event has been handled */
10580 spin_lock_irq(&phba->hbalock);
10581 phba->hba_flag &= ~FCP_XRI_ABORT_EVENT;
10582 spin_unlock_irq(&phba->hbalock);
10583 /* Now, handle all the fcp xri abort events */
10584 while (!list_empty(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue)) {
10585 /* Get the first event from the head of the event queue */
10586 spin_lock_irq(&phba->hbalock);
10587 list_remove_head(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue,
10588 cq_event, struct lpfc_cq_event, list);
10589 spin_unlock_irq(&phba->hbalock);
10590 /* Notify aborted XRI for FCP work queue */
10591 lpfc_sli4_fcp_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
10592 /* Free the event processed back to the free pool */
10593 lpfc_sli4_cq_event_release(phba, cq_event);
10594 }
10595}
10596
10597/**
10598 * lpfc_sli4_els_xri_abort_event_proc - Process els xri abort event
10599 * @phba: pointer to lpfc hba data structure.
10600 *
10601 * This routine is invoked by the worker thread to process all the pending
10602 * SLI4 els abort xri events.
10603 **/
10604void lpfc_sli4_els_xri_abort_event_proc(struct lpfc_hba *phba)
10605{
10606 struct lpfc_cq_event *cq_event;
10607
10608 /* First, declare the els xri abort event has been handled */
10609 spin_lock_irq(&phba->hbalock);
10610 phba->hba_flag &= ~ELS_XRI_ABORT_EVENT;
10611 spin_unlock_irq(&phba->hbalock);
10612 /* Now, handle all the els xri abort events */
10613 while (!list_empty(&phba->sli4_hba.sp_els_xri_aborted_work_queue)) {
10614 /* Get the first event from the head of the event queue */
10615 spin_lock_irq(&phba->hbalock);
10616 list_remove_head(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
10617 cq_event, struct lpfc_cq_event, list);
10618 spin_unlock_irq(&phba->hbalock);
10619 /* Notify aborted XRI for ELS work queue */
10620 lpfc_sli4_els_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
10621 /* Free the event processed back to the free pool */
10622 lpfc_sli4_cq_event_release(phba, cq_event);
10623 }
10624}
10625
James Smart341af102010-01-26 23:07:37 -050010626/**
10627 * lpfc_sli4_iocb_param_transfer - Transfer pIocbOut and cmpl status to pIocbIn
10628 * @phba: pointer to lpfc hba data structure
10629 * @pIocbIn: pointer to the rspiocbq
10630 * @pIocbOut: pointer to the cmdiocbq
10631 * @wcqe: pointer to the complete wcqe
10632 *
10633 * This routine transfers the fields of a command iocbq to a response iocbq
10634 * by copying all the IOCB fields from command iocbq and transferring the
10635 * completion status information from the complete wcqe.
10636 **/
James Smart4f774512009-05-22 14:52:35 -040010637static void
James Smart341af102010-01-26 23:07:37 -050010638lpfc_sli4_iocb_param_transfer(struct lpfc_hba *phba,
10639 struct lpfc_iocbq *pIocbIn,
James Smart4f774512009-05-22 14:52:35 -040010640 struct lpfc_iocbq *pIocbOut,
10641 struct lpfc_wcqe_complete *wcqe)
10642{
James Smart341af102010-01-26 23:07:37 -050010643 unsigned long iflags;
James Smart4f774512009-05-22 14:52:35 -040010644 size_t offset = offsetof(struct lpfc_iocbq, iocb);
10645
10646 memcpy((char *)pIocbIn + offset, (char *)pIocbOut + offset,
10647 sizeof(struct lpfc_iocbq) - offset);
James Smart4f774512009-05-22 14:52:35 -040010648 /* Map WCQE parameters into irspiocb parameters */
10649 pIocbIn->iocb.ulpStatus = bf_get(lpfc_wcqe_c_status, wcqe);
10650 if (pIocbOut->iocb_flag & LPFC_IO_FCP)
10651 if (pIocbIn->iocb.ulpStatus == IOSTAT_FCP_RSP_ERROR)
10652 pIocbIn->iocb.un.fcpi.fcpi_parm =
10653 pIocbOut->iocb.un.fcpi.fcpi_parm -
10654 wcqe->total_data_placed;
10655 else
10656 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
James Smart695a8142010-01-26 23:08:03 -050010657 else {
James Smart4f774512009-05-22 14:52:35 -040010658 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
James Smart695a8142010-01-26 23:08:03 -050010659 pIocbIn->iocb.un.genreq64.bdl.bdeSize = wcqe->total_data_placed;
10660 }
James Smart341af102010-01-26 23:07:37 -050010661
10662 /* Pick up HBA exchange busy condition */
10663 if (bf_get(lpfc_wcqe_c_xb, wcqe)) {
10664 spin_lock_irqsave(&phba->hbalock, iflags);
10665 pIocbIn->iocb_flag |= LPFC_EXCHANGE_BUSY;
10666 spin_unlock_irqrestore(&phba->hbalock, iflags);
10667 }
James Smart4f774512009-05-22 14:52:35 -040010668}
10669
10670/**
James Smart45ed1192009-10-02 15:17:02 -040010671 * lpfc_sli4_els_wcqe_to_rspiocbq - Get response iocbq from els wcqe
10672 * @phba: Pointer to HBA context object.
10673 * @wcqe: Pointer to work-queue completion queue entry.
10674 *
10675 * This routine handles an ELS work-queue completion event and construct
10676 * a pseudo response ELS IODBQ from the SLI4 ELS WCQE for the common
10677 * discovery engine to handle.
10678 *
10679 * Return: Pointer to the receive IOCBQ, NULL otherwise.
10680 **/
10681static struct lpfc_iocbq *
10682lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *phba,
10683 struct lpfc_iocbq *irspiocbq)
10684{
10685 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
10686 struct lpfc_iocbq *cmdiocbq;
10687 struct lpfc_wcqe_complete *wcqe;
10688 unsigned long iflags;
10689
10690 wcqe = &irspiocbq->cq_event.cqe.wcqe_cmpl;
10691 spin_lock_irqsave(&phba->hbalock, iflags);
10692 pring->stats.iocb_event++;
10693 /* Look up the ELS command IOCB and create pseudo response IOCB */
10694 cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
10695 bf_get(lpfc_wcqe_c_request_tag, wcqe));
10696 spin_unlock_irqrestore(&phba->hbalock, iflags);
10697
10698 if (unlikely(!cmdiocbq)) {
10699 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
10700 "0386 ELS complete with no corresponding "
10701 "cmdiocb: iotag (%d)\n",
10702 bf_get(lpfc_wcqe_c_request_tag, wcqe));
10703 lpfc_sli_release_iocbq(phba, irspiocbq);
10704 return NULL;
10705 }
10706
10707 /* Fake the irspiocbq and copy necessary response information */
James Smart341af102010-01-26 23:07:37 -050010708 lpfc_sli4_iocb_param_transfer(phba, irspiocbq, cmdiocbq, wcqe);
James Smart45ed1192009-10-02 15:17:02 -040010709
10710 return irspiocbq;
10711}
10712
10713/**
James Smart04c68492009-05-22 14:52:52 -040010714 * lpfc_sli4_sp_handle_async_event - Handle an asynchroous event
10715 * @phba: Pointer to HBA context object.
10716 * @cqe: Pointer to mailbox completion queue entry.
10717 *
10718 * This routine process a mailbox completion queue entry with asynchrous
10719 * event.
10720 *
10721 * Return: true if work posted to worker thread, otherwise false.
10722 **/
10723static bool
10724lpfc_sli4_sp_handle_async_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
10725{
10726 struct lpfc_cq_event *cq_event;
10727 unsigned long iflags;
10728
10729 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
10730 "0392 Async Event: word0:x%x, word1:x%x, "
10731 "word2:x%x, word3:x%x\n", mcqe->word0,
10732 mcqe->mcqe_tag0, mcqe->mcqe_tag1, mcqe->trailer);
10733
10734 /* Allocate a new internal CQ_EVENT entry */
10735 cq_event = lpfc_sli4_cq_event_alloc(phba);
10736 if (!cq_event) {
10737 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10738 "0394 Failed to allocate CQ_EVENT entry\n");
10739 return false;
10740 }
10741
10742 /* Move the CQE into an asynchronous event entry */
10743 memcpy(&cq_event->cqe, mcqe, sizeof(struct lpfc_mcqe));
10744 spin_lock_irqsave(&phba->hbalock, iflags);
10745 list_add_tail(&cq_event->list, &phba->sli4_hba.sp_asynce_work_queue);
10746 /* Set the async event flag */
10747 phba->hba_flag |= ASYNC_EVENT;
10748 spin_unlock_irqrestore(&phba->hbalock, iflags);
10749
10750 return true;
10751}
10752
10753/**
10754 * lpfc_sli4_sp_handle_mbox_event - Handle a mailbox completion event
10755 * @phba: Pointer to HBA context object.
10756 * @cqe: Pointer to mailbox completion queue entry.
10757 *
10758 * This routine process a mailbox completion queue entry with mailbox
10759 * completion event.
10760 *
10761 * Return: true if work posted to worker thread, otherwise false.
10762 **/
10763static bool
10764lpfc_sli4_sp_handle_mbox_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
10765{
10766 uint32_t mcqe_status;
10767 MAILBOX_t *mbox, *pmbox;
10768 struct lpfc_mqe *mqe;
10769 struct lpfc_vport *vport;
10770 struct lpfc_nodelist *ndlp;
10771 struct lpfc_dmabuf *mp;
10772 unsigned long iflags;
10773 LPFC_MBOXQ_t *pmb;
10774 bool workposted = false;
10775 int rc;
10776
10777 /* If not a mailbox complete MCQE, out by checking mailbox consume */
10778 if (!bf_get(lpfc_trailer_completed, mcqe))
10779 goto out_no_mqe_complete;
10780
10781 /* Get the reference to the active mbox command */
10782 spin_lock_irqsave(&phba->hbalock, iflags);
10783 pmb = phba->sli.mbox_active;
10784 if (unlikely(!pmb)) {
10785 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
10786 "1832 No pending MBOX command to handle\n");
10787 spin_unlock_irqrestore(&phba->hbalock, iflags);
10788 goto out_no_mqe_complete;
10789 }
10790 spin_unlock_irqrestore(&phba->hbalock, iflags);
10791 mqe = &pmb->u.mqe;
10792 pmbox = (MAILBOX_t *)&pmb->u.mqe;
10793 mbox = phba->mbox;
10794 vport = pmb->vport;
10795
10796 /* Reset heartbeat timer */
10797 phba->last_completion_time = jiffies;
10798 del_timer(&phba->sli.mbox_tmo);
10799
10800 /* Move mbox data to caller's mailbox region, do endian swapping */
10801 if (pmb->mbox_cmpl && mbox)
10802 lpfc_sli_pcimem_bcopy(mbox, mqe, sizeof(struct lpfc_mqe));
James Smart04c68492009-05-22 14:52:52 -040010803
James Smart73d91e52011-10-10 21:32:10 -040010804 /*
10805 * For mcqe errors, conditionally move a modified error code to
10806 * the mbox so that the error will not be missed.
10807 */
10808 mcqe_status = bf_get(lpfc_mcqe_status, mcqe);
10809 if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
10810 if (bf_get(lpfc_mqe_status, mqe) == MBX_SUCCESS)
10811 bf_set(lpfc_mqe_status, mqe,
10812 (LPFC_MBX_ERROR_RANGE | mcqe_status));
10813 }
James Smart04c68492009-05-22 14:52:52 -040010814 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
10815 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
10816 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_MBOX_VPORT,
10817 "MBOX dflt rpi: status:x%x rpi:x%x",
10818 mcqe_status,
10819 pmbox->un.varWords[0], 0);
10820 if (mcqe_status == MB_CQE_STATUS_SUCCESS) {
10821 mp = (struct lpfc_dmabuf *)(pmb->context1);
10822 ndlp = (struct lpfc_nodelist *)pmb->context2;
10823 /* Reg_LOGIN of dflt RPI was successful. Now lets get
10824 * RID of the PPI using the same mbox buffer.
10825 */
10826 lpfc_unreg_login(phba, vport->vpi,
10827 pmbox->un.varWords[0], pmb);
10828 pmb->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
10829 pmb->context1 = mp;
10830 pmb->context2 = ndlp;
10831 pmb->vport = vport;
10832 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
10833 if (rc != MBX_BUSY)
10834 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
10835 LOG_SLI, "0385 rc should "
10836 "have been MBX_BUSY\n");
10837 if (rc != MBX_NOT_FINISHED)
10838 goto send_current_mbox;
10839 }
10840 }
10841 spin_lock_irqsave(&phba->pport->work_port_lock, iflags);
10842 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
10843 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflags);
10844
10845 /* There is mailbox completion work to do */
10846 spin_lock_irqsave(&phba->hbalock, iflags);
10847 __lpfc_mbox_cmpl_put(phba, pmb);
10848 phba->work_ha |= HA_MBATT;
10849 spin_unlock_irqrestore(&phba->hbalock, iflags);
10850 workposted = true;
10851
10852send_current_mbox:
10853 spin_lock_irqsave(&phba->hbalock, iflags);
10854 /* Release the mailbox command posting token */
10855 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
10856 /* Setting active mailbox pointer need to be in sync to flag clear */
10857 phba->sli.mbox_active = NULL;
10858 spin_unlock_irqrestore(&phba->hbalock, iflags);
10859 /* Wake up worker thread to post the next pending mailbox command */
10860 lpfc_worker_wake_up(phba);
10861out_no_mqe_complete:
10862 if (bf_get(lpfc_trailer_consumed, mcqe))
10863 lpfc_sli4_mq_release(phba->sli4_hba.mbx_wq);
10864 return workposted;
10865}
10866
10867/**
10868 * lpfc_sli4_sp_handle_mcqe - Process a mailbox completion queue entry
10869 * @phba: Pointer to HBA context object.
10870 * @cqe: Pointer to mailbox completion queue entry.
10871 *
10872 * This routine process a mailbox completion queue entry, it invokes the
10873 * proper mailbox complete handling or asynchrous event handling routine
10874 * according to the MCQE's async bit.
10875 *
10876 * Return: true if work posted to worker thread, otherwise false.
10877 **/
10878static bool
10879lpfc_sli4_sp_handle_mcqe(struct lpfc_hba *phba, struct lpfc_cqe *cqe)
10880{
10881 struct lpfc_mcqe mcqe;
10882 bool workposted;
10883
10884 /* Copy the mailbox MCQE and convert endian order as needed */
10885 lpfc_sli_pcimem_bcopy(cqe, &mcqe, sizeof(struct lpfc_mcqe));
10886
10887 /* Invoke the proper event handling routine */
10888 if (!bf_get(lpfc_trailer_async, &mcqe))
10889 workposted = lpfc_sli4_sp_handle_mbox_event(phba, &mcqe);
10890 else
10891 workposted = lpfc_sli4_sp_handle_async_event(phba, &mcqe);
10892 return workposted;
10893}
10894
10895/**
James Smart4f774512009-05-22 14:52:35 -040010896 * lpfc_sli4_sp_handle_els_wcqe - Handle els work-queue completion event
10897 * @phba: Pointer to HBA context object.
10898 * @wcqe: Pointer to work-queue completion queue entry.
10899 *
10900 * This routine handles an ELS work-queue completion event.
10901 *
10902 * Return: true if work posted to worker thread, otherwise false.
10903 **/
10904static bool
10905lpfc_sli4_sp_handle_els_wcqe(struct lpfc_hba *phba,
10906 struct lpfc_wcqe_complete *wcqe)
10907{
James Smart4f774512009-05-22 14:52:35 -040010908 struct lpfc_iocbq *irspiocbq;
10909 unsigned long iflags;
James Smart2a9bf3d2010-06-07 15:24:45 -040010910 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_FCP_RING];
James Smart4f774512009-05-22 14:52:35 -040010911
James Smart45ed1192009-10-02 15:17:02 -040010912 /* Get an irspiocbq for later ELS response processing use */
James Smart4f774512009-05-22 14:52:35 -040010913 irspiocbq = lpfc_sli_get_iocbq(phba);
10914 if (!irspiocbq) {
10915 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart2a9bf3d2010-06-07 15:24:45 -040010916 "0387 NO IOCBQ data: txq_cnt=%d iocb_cnt=%d "
10917 "fcp_txcmplq_cnt=%d, els_txcmplq_cnt=%d\n",
10918 pring->txq_cnt, phba->iocb_cnt,
10919 phba->sli.ring[LPFC_FCP_RING].txcmplq_cnt,
10920 phba->sli.ring[LPFC_ELS_RING].txcmplq_cnt);
James Smart45ed1192009-10-02 15:17:02 -040010921 return false;
James Smart4f774512009-05-22 14:52:35 -040010922 }
James Smart4f774512009-05-22 14:52:35 -040010923
James Smart45ed1192009-10-02 15:17:02 -040010924 /* Save off the slow-path queue event for work thread to process */
10925 memcpy(&irspiocbq->cq_event.cqe.wcqe_cmpl, wcqe, sizeof(*wcqe));
James Smart4f774512009-05-22 14:52:35 -040010926 spin_lock_irqsave(&phba->hbalock, iflags);
James Smart4d9ab992009-10-02 15:16:39 -040010927 list_add_tail(&irspiocbq->cq_event.list,
James Smart45ed1192009-10-02 15:17:02 -040010928 &phba->sli4_hba.sp_queue_event);
10929 phba->hba_flag |= HBA_SP_QUEUE_EVT;
James Smart4f774512009-05-22 14:52:35 -040010930 spin_unlock_irqrestore(&phba->hbalock, iflags);
James Smart4f774512009-05-22 14:52:35 -040010931
James Smart45ed1192009-10-02 15:17:02 -040010932 return true;
James Smart4f774512009-05-22 14:52:35 -040010933}
10934
10935/**
10936 * lpfc_sli4_sp_handle_rel_wcqe - Handle slow-path WQ entry consumed event
10937 * @phba: Pointer to HBA context object.
10938 * @wcqe: Pointer to work-queue completion queue entry.
10939 *
10940 * This routine handles slow-path WQ entry comsumed event by invoking the
10941 * proper WQ release routine to the slow-path WQ.
10942 **/
10943static void
10944lpfc_sli4_sp_handle_rel_wcqe(struct lpfc_hba *phba,
10945 struct lpfc_wcqe_release *wcqe)
10946{
James Smart2e90f4b2011-12-13 13:22:37 -050010947 /* sanity check on queue memory */
10948 if (unlikely(!phba->sli4_hba.els_wq))
10949 return;
James Smart4f774512009-05-22 14:52:35 -040010950 /* Check for the slow-path ELS work queue */
10951 if (bf_get(lpfc_wcqe_r_wq_id, wcqe) == phba->sli4_hba.els_wq->queue_id)
10952 lpfc_sli4_wq_release(phba->sli4_hba.els_wq,
10953 bf_get(lpfc_wcqe_r_wqe_index, wcqe));
10954 else
10955 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
10956 "2579 Slow-path wqe consume event carries "
10957 "miss-matched qid: wcqe-qid=x%x, sp-qid=x%x\n",
10958 bf_get(lpfc_wcqe_r_wqe_index, wcqe),
10959 phba->sli4_hba.els_wq->queue_id);
10960}
10961
10962/**
10963 * lpfc_sli4_sp_handle_abort_xri_wcqe - Handle a xri abort event
10964 * @phba: Pointer to HBA context object.
10965 * @cq: Pointer to a WQ completion queue.
10966 * @wcqe: Pointer to work-queue completion queue entry.
10967 *
10968 * This routine handles an XRI abort event.
10969 *
10970 * Return: true if work posted to worker thread, otherwise false.
10971 **/
10972static bool
10973lpfc_sli4_sp_handle_abort_xri_wcqe(struct lpfc_hba *phba,
10974 struct lpfc_queue *cq,
10975 struct sli4_wcqe_xri_aborted *wcqe)
10976{
10977 bool workposted = false;
10978 struct lpfc_cq_event *cq_event;
10979 unsigned long iflags;
10980
10981 /* Allocate a new internal CQ_EVENT entry */
10982 cq_event = lpfc_sli4_cq_event_alloc(phba);
10983 if (!cq_event) {
10984 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10985 "0602 Failed to allocate CQ_EVENT entry\n");
10986 return false;
10987 }
10988
10989 /* Move the CQE into the proper xri abort event list */
10990 memcpy(&cq_event->cqe, wcqe, sizeof(struct sli4_wcqe_xri_aborted));
10991 switch (cq->subtype) {
10992 case LPFC_FCP:
10993 spin_lock_irqsave(&phba->hbalock, iflags);
10994 list_add_tail(&cq_event->list,
10995 &phba->sli4_hba.sp_fcp_xri_aborted_work_queue);
10996 /* Set the fcp xri abort event flag */
10997 phba->hba_flag |= FCP_XRI_ABORT_EVENT;
10998 spin_unlock_irqrestore(&phba->hbalock, iflags);
10999 workposted = true;
11000 break;
11001 case LPFC_ELS:
11002 spin_lock_irqsave(&phba->hbalock, iflags);
11003 list_add_tail(&cq_event->list,
11004 &phba->sli4_hba.sp_els_xri_aborted_work_queue);
11005 /* Set the els xri abort event flag */
11006 phba->hba_flag |= ELS_XRI_ABORT_EVENT;
11007 spin_unlock_irqrestore(&phba->hbalock, iflags);
11008 workposted = true;
11009 break;
11010 default:
11011 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11012 "0603 Invalid work queue CQE subtype (x%x)\n",
11013 cq->subtype);
11014 workposted = false;
11015 break;
11016 }
11017 return workposted;
11018}
11019
11020/**
James Smart4d9ab992009-10-02 15:16:39 -040011021 * lpfc_sli4_sp_handle_rcqe - Process a receive-queue completion queue entry
James Smart4f774512009-05-22 14:52:35 -040011022 * @phba: Pointer to HBA context object.
James Smart4d9ab992009-10-02 15:16:39 -040011023 * @rcqe: Pointer to receive-queue completion queue entry.
James Smart4f774512009-05-22 14:52:35 -040011024 *
James Smart4d9ab992009-10-02 15:16:39 -040011025 * This routine process a receive-queue completion queue entry.
James Smart4f774512009-05-22 14:52:35 -040011026 *
11027 * Return: true if work posted to worker thread, otherwise false.
11028 **/
11029static bool
James Smart4d9ab992009-10-02 15:16:39 -040011030lpfc_sli4_sp_handle_rcqe(struct lpfc_hba *phba, struct lpfc_rcqe *rcqe)
11031{
11032 bool workposted = false;
11033 struct lpfc_queue *hrq = phba->sli4_hba.hdr_rq;
11034 struct lpfc_queue *drq = phba->sli4_hba.dat_rq;
11035 struct hbq_dmabuf *dma_buf;
James Smart7851fe22011-07-22 18:36:52 -040011036 uint32_t status, rq_id;
James Smart4d9ab992009-10-02 15:16:39 -040011037 unsigned long iflags;
11038
James Smart2e90f4b2011-12-13 13:22:37 -050011039 /* sanity check on queue memory */
11040 if (unlikely(!hrq) || unlikely(!drq))
11041 return workposted;
11042
James Smart7851fe22011-07-22 18:36:52 -040011043 if (bf_get(lpfc_cqe_code, rcqe) == CQE_CODE_RECEIVE_V1)
11044 rq_id = bf_get(lpfc_rcqe_rq_id_v1, rcqe);
11045 else
11046 rq_id = bf_get(lpfc_rcqe_rq_id, rcqe);
11047 if (rq_id != hrq->queue_id)
James Smart4d9ab992009-10-02 15:16:39 -040011048 goto out;
11049
11050 status = bf_get(lpfc_rcqe_status, rcqe);
11051 switch (status) {
11052 case FC_STATUS_RQ_BUF_LEN_EXCEEDED:
11053 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11054 "2537 Receive Frame Truncated!!\n");
11055 case FC_STATUS_RQ_SUCCESS:
James Smart5ffc2662009-11-18 15:39:44 -050011056 lpfc_sli4_rq_release(hrq, drq);
James Smart4d9ab992009-10-02 15:16:39 -040011057 spin_lock_irqsave(&phba->hbalock, iflags);
11058 dma_buf = lpfc_sli_hbqbuf_get(&phba->hbqs[0].hbq_buffer_list);
11059 if (!dma_buf) {
11060 spin_unlock_irqrestore(&phba->hbalock, iflags);
11061 goto out;
11062 }
11063 memcpy(&dma_buf->cq_event.cqe.rcqe_cmpl, rcqe, sizeof(*rcqe));
11064 /* save off the frame for the word thread to process */
11065 list_add_tail(&dma_buf->cq_event.list,
James Smart45ed1192009-10-02 15:17:02 -040011066 &phba->sli4_hba.sp_queue_event);
James Smart4d9ab992009-10-02 15:16:39 -040011067 /* Frame received */
James Smart45ed1192009-10-02 15:17:02 -040011068 phba->hba_flag |= HBA_SP_QUEUE_EVT;
James Smart4d9ab992009-10-02 15:16:39 -040011069 spin_unlock_irqrestore(&phba->hbalock, iflags);
11070 workposted = true;
11071 break;
11072 case FC_STATUS_INSUFF_BUF_NEED_BUF:
11073 case FC_STATUS_INSUFF_BUF_FRM_DISC:
11074 /* Post more buffers if possible */
11075 spin_lock_irqsave(&phba->hbalock, iflags);
11076 phba->hba_flag |= HBA_POST_RECEIVE_BUFFER;
11077 spin_unlock_irqrestore(&phba->hbalock, iflags);
11078 workposted = true;
11079 break;
11080 }
11081out:
11082 return workposted;
James Smart4d9ab992009-10-02 15:16:39 -040011083}
11084
11085/**
11086 * lpfc_sli4_sp_handle_cqe - Process a slow path completion queue entry
11087 * @phba: Pointer to HBA context object.
11088 * @cq: Pointer to the completion queue.
11089 * @wcqe: Pointer to a completion queue entry.
11090 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030011091 * This routine process a slow-path work-queue or receive queue completion queue
James Smart4d9ab992009-10-02 15:16:39 -040011092 * entry.
11093 *
11094 * Return: true if work posted to worker thread, otherwise false.
11095 **/
11096static bool
11097lpfc_sli4_sp_handle_cqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
James Smart4f774512009-05-22 14:52:35 -040011098 struct lpfc_cqe *cqe)
11099{
James Smart45ed1192009-10-02 15:17:02 -040011100 struct lpfc_cqe cqevt;
James Smart4f774512009-05-22 14:52:35 -040011101 bool workposted = false;
11102
11103 /* Copy the work queue CQE and convert endian order if needed */
James Smart45ed1192009-10-02 15:17:02 -040011104 lpfc_sli_pcimem_bcopy(cqe, &cqevt, sizeof(struct lpfc_cqe));
James Smart4f774512009-05-22 14:52:35 -040011105
11106 /* Check and process for different type of WCQE and dispatch */
James Smart45ed1192009-10-02 15:17:02 -040011107 switch (bf_get(lpfc_cqe_code, &cqevt)) {
James Smart4f774512009-05-22 14:52:35 -040011108 case CQE_CODE_COMPL_WQE:
James Smart45ed1192009-10-02 15:17:02 -040011109 /* Process the WQ/RQ complete event */
James Smartbc739052010-08-04 16:11:18 -040011110 phba->last_completion_time = jiffies;
James Smart4f774512009-05-22 14:52:35 -040011111 workposted = lpfc_sli4_sp_handle_els_wcqe(phba,
James Smart45ed1192009-10-02 15:17:02 -040011112 (struct lpfc_wcqe_complete *)&cqevt);
James Smart4f774512009-05-22 14:52:35 -040011113 break;
11114 case CQE_CODE_RELEASE_WQE:
11115 /* Process the WQ release event */
11116 lpfc_sli4_sp_handle_rel_wcqe(phba,
James Smart45ed1192009-10-02 15:17:02 -040011117 (struct lpfc_wcqe_release *)&cqevt);
James Smart4f774512009-05-22 14:52:35 -040011118 break;
11119 case CQE_CODE_XRI_ABORTED:
11120 /* Process the WQ XRI abort event */
James Smartbc739052010-08-04 16:11:18 -040011121 phba->last_completion_time = jiffies;
James Smart4f774512009-05-22 14:52:35 -040011122 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
James Smart45ed1192009-10-02 15:17:02 -040011123 (struct sli4_wcqe_xri_aborted *)&cqevt);
James Smart4f774512009-05-22 14:52:35 -040011124 break;
James Smart4d9ab992009-10-02 15:16:39 -040011125 case CQE_CODE_RECEIVE:
James Smart7851fe22011-07-22 18:36:52 -040011126 case CQE_CODE_RECEIVE_V1:
James Smart4d9ab992009-10-02 15:16:39 -040011127 /* Process the RQ event */
James Smartbc739052010-08-04 16:11:18 -040011128 phba->last_completion_time = jiffies;
James Smart4d9ab992009-10-02 15:16:39 -040011129 workposted = lpfc_sli4_sp_handle_rcqe(phba,
James Smart45ed1192009-10-02 15:17:02 -040011130 (struct lpfc_rcqe *)&cqevt);
James Smart4d9ab992009-10-02 15:16:39 -040011131 break;
James Smart4f774512009-05-22 14:52:35 -040011132 default:
11133 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11134 "0388 Not a valid WCQE code: x%x\n",
James Smart45ed1192009-10-02 15:17:02 -040011135 bf_get(lpfc_cqe_code, &cqevt));
James Smart4f774512009-05-22 14:52:35 -040011136 break;
11137 }
11138 return workposted;
11139}
11140
11141/**
James Smart4f774512009-05-22 14:52:35 -040011142 * lpfc_sli4_sp_handle_eqe - Process a slow-path event queue entry
11143 * @phba: Pointer to HBA context object.
11144 * @eqe: Pointer to fast-path event queue entry.
11145 *
11146 * This routine process a event queue entry from the slow-path event queue.
11147 * It will check the MajorCode and MinorCode to determine this is for a
11148 * completion event on a completion queue, if not, an error shall be logged
11149 * and just return. Otherwise, it will get to the corresponding completion
11150 * queue and process all the entries on that completion queue, rearm the
11151 * completion queue, and then return.
11152 *
11153 **/
11154static void
11155lpfc_sli4_sp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe)
11156{
11157 struct lpfc_queue *cq = NULL, *childq, *speq;
11158 struct lpfc_cqe *cqe;
11159 bool workposted = false;
11160 int ecount = 0;
11161 uint16_t cqid;
11162
James Smartcb5172e2010-03-15 11:25:07 -040011163 if (bf_get_le32(lpfc_eqe_major_code, eqe) != 0) {
James Smart4f774512009-05-22 14:52:35 -040011164 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11165 "0359 Not a valid slow-path completion "
11166 "event: majorcode=x%x, minorcode=x%x\n",
James Smartcb5172e2010-03-15 11:25:07 -040011167 bf_get_le32(lpfc_eqe_major_code, eqe),
11168 bf_get_le32(lpfc_eqe_minor_code, eqe));
James Smart4f774512009-05-22 14:52:35 -040011169 return;
11170 }
11171
11172 /* Get the reference to the corresponding CQ */
James Smartcb5172e2010-03-15 11:25:07 -040011173 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
James Smart4f774512009-05-22 14:52:35 -040011174
11175 /* Search for completion queue pointer matching this cqid */
11176 speq = phba->sli4_hba.sp_eq;
James Smart2e90f4b2011-12-13 13:22:37 -050011177 /* sanity check on queue memory */
11178 if (unlikely(!speq))
11179 return;
James Smart4f774512009-05-22 14:52:35 -040011180 list_for_each_entry(childq, &speq->child_list, list) {
11181 if (childq->queue_id == cqid) {
11182 cq = childq;
11183 break;
11184 }
11185 }
11186 if (unlikely(!cq)) {
James Smart75baf692010-06-08 18:31:21 -040011187 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
11188 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11189 "0365 Slow-path CQ identifier "
11190 "(%d) does not exist\n", cqid);
James Smart4f774512009-05-22 14:52:35 -040011191 return;
11192 }
11193
11194 /* Process all the entries to the CQ */
11195 switch (cq->type) {
11196 case LPFC_MCQ:
11197 while ((cqe = lpfc_sli4_cq_get(cq))) {
11198 workposted |= lpfc_sli4_sp_handle_mcqe(phba, cqe);
James Smart73d91e52011-10-10 21:32:10 -040011199 if (!(++ecount % cq->entry_repost))
James Smart4f774512009-05-22 14:52:35 -040011200 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
11201 }
11202 break;
11203 case LPFC_WCQ:
11204 while ((cqe = lpfc_sli4_cq_get(cq))) {
James Smart05580562011-05-24 11:40:48 -040011205 if (cq->subtype == LPFC_FCP)
11206 workposted |= lpfc_sli4_fp_handle_wcqe(phba, cq,
11207 cqe);
11208 else
11209 workposted |= lpfc_sli4_sp_handle_cqe(phba, cq,
11210 cqe);
James Smart73d91e52011-10-10 21:32:10 -040011211 if (!(++ecount % cq->entry_repost))
James Smart4f774512009-05-22 14:52:35 -040011212 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
11213 }
11214 break;
11215 default:
11216 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11217 "0370 Invalid completion queue type (%d)\n",
11218 cq->type);
11219 return;
11220 }
11221
11222 /* Catch the no cq entry condition, log an error */
11223 if (unlikely(ecount == 0))
11224 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11225 "0371 No entry from the CQ: identifier "
11226 "(x%x), type (%d)\n", cq->queue_id, cq->type);
11227
11228 /* In any case, flash and re-arm the RCQ */
11229 lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
11230
11231 /* wake up worker thread if there are works to be done */
11232 if (workposted)
11233 lpfc_worker_wake_up(phba);
11234}
11235
11236/**
11237 * lpfc_sli4_fp_handle_fcp_wcqe - Process fast-path work queue completion entry
11238 * @eqe: Pointer to fast-path completion queue entry.
11239 *
11240 * This routine process a fast-path work queue completion entry from fast-path
11241 * event queue for FCP command response completion.
11242 **/
11243static void
11244lpfc_sli4_fp_handle_fcp_wcqe(struct lpfc_hba *phba,
11245 struct lpfc_wcqe_complete *wcqe)
11246{
11247 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_FCP_RING];
11248 struct lpfc_iocbq *cmdiocbq;
11249 struct lpfc_iocbq irspiocbq;
11250 unsigned long iflags;
11251
11252 spin_lock_irqsave(&phba->hbalock, iflags);
11253 pring->stats.iocb_event++;
11254 spin_unlock_irqrestore(&phba->hbalock, iflags);
11255
11256 /* Check for response status */
11257 if (unlikely(bf_get(lpfc_wcqe_c_status, wcqe))) {
11258 /* If resource errors reported from HBA, reduce queue
11259 * depth of the SCSI device.
11260 */
11261 if ((bf_get(lpfc_wcqe_c_status, wcqe) ==
11262 IOSTAT_LOCAL_REJECT) &&
11263 (wcqe->parameter == IOERR_NO_RESOURCES)) {
11264 phba->lpfc_rampdown_queue_depth(phba);
11265 }
11266 /* Log the error status */
11267 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11268 "0373 FCP complete error: status=x%x, "
11269 "hw_status=x%x, total_data_specified=%d, "
11270 "parameter=x%x, word3=x%x\n",
11271 bf_get(lpfc_wcqe_c_status, wcqe),
11272 bf_get(lpfc_wcqe_c_hw_status, wcqe),
11273 wcqe->total_data_placed, wcqe->parameter,
11274 wcqe->word3);
11275 }
11276
11277 /* Look up the FCP command IOCB and create pseudo response IOCB */
11278 spin_lock_irqsave(&phba->hbalock, iflags);
11279 cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
11280 bf_get(lpfc_wcqe_c_request_tag, wcqe));
11281 spin_unlock_irqrestore(&phba->hbalock, iflags);
11282 if (unlikely(!cmdiocbq)) {
11283 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11284 "0374 FCP complete with no corresponding "
11285 "cmdiocb: iotag (%d)\n",
11286 bf_get(lpfc_wcqe_c_request_tag, wcqe));
11287 return;
11288 }
11289 if (unlikely(!cmdiocbq->iocb_cmpl)) {
11290 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11291 "0375 FCP cmdiocb not callback function "
11292 "iotag: (%d)\n",
11293 bf_get(lpfc_wcqe_c_request_tag, wcqe));
11294 return;
11295 }
11296
11297 /* Fake the irspiocb and copy necessary response information */
James Smart341af102010-01-26 23:07:37 -050011298 lpfc_sli4_iocb_param_transfer(phba, &irspiocbq, cmdiocbq, wcqe);
James Smart4f774512009-05-22 14:52:35 -040011299
James Smart0f65ff62010-02-26 14:14:23 -050011300 if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED) {
11301 spin_lock_irqsave(&phba->hbalock, iflags);
11302 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
11303 spin_unlock_irqrestore(&phba->hbalock, iflags);
11304 }
11305
James Smart4f774512009-05-22 14:52:35 -040011306 /* Pass the cmd_iocb and the rsp state to the upper layer */
11307 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq, &irspiocbq);
11308}
11309
11310/**
11311 * lpfc_sli4_fp_handle_rel_wcqe - Handle fast-path WQ entry consumed event
11312 * @phba: Pointer to HBA context object.
11313 * @cq: Pointer to completion queue.
11314 * @wcqe: Pointer to work-queue completion queue entry.
11315 *
11316 * This routine handles an fast-path WQ entry comsumed event by invoking the
11317 * proper WQ release routine to the slow-path WQ.
11318 **/
11319static void
11320lpfc_sli4_fp_handle_rel_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
11321 struct lpfc_wcqe_release *wcqe)
11322{
11323 struct lpfc_queue *childwq;
11324 bool wqid_matched = false;
11325 uint16_t fcp_wqid;
11326
11327 /* Check for fast-path FCP work queue release */
11328 fcp_wqid = bf_get(lpfc_wcqe_r_wq_id, wcqe);
11329 list_for_each_entry(childwq, &cq->child_list, list) {
11330 if (childwq->queue_id == fcp_wqid) {
11331 lpfc_sli4_wq_release(childwq,
11332 bf_get(lpfc_wcqe_r_wqe_index, wcqe));
11333 wqid_matched = true;
11334 break;
11335 }
11336 }
11337 /* Report warning log message if no match found */
11338 if (wqid_matched != true)
11339 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11340 "2580 Fast-path wqe consume event carries "
11341 "miss-matched qid: wcqe-qid=x%x\n", fcp_wqid);
11342}
11343
11344/**
11345 * lpfc_sli4_fp_handle_wcqe - Process fast-path work queue completion entry
11346 * @cq: Pointer to the completion queue.
11347 * @eqe: Pointer to fast-path completion queue entry.
11348 *
11349 * This routine process a fast-path work queue completion entry from fast-path
11350 * event queue for FCP command response completion.
11351 **/
11352static int
11353lpfc_sli4_fp_handle_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
11354 struct lpfc_cqe *cqe)
11355{
11356 struct lpfc_wcqe_release wcqe;
11357 bool workposted = false;
11358
11359 /* Copy the work queue CQE and convert endian order if needed */
11360 lpfc_sli_pcimem_bcopy(cqe, &wcqe, sizeof(struct lpfc_cqe));
11361
11362 /* Check and process for different type of WCQE and dispatch */
11363 switch (bf_get(lpfc_wcqe_c_code, &wcqe)) {
11364 case CQE_CODE_COMPL_WQE:
11365 /* Process the WQ complete event */
James Smart98fc5dd2010-06-07 15:24:29 -040011366 phba->last_completion_time = jiffies;
James Smart4f774512009-05-22 14:52:35 -040011367 lpfc_sli4_fp_handle_fcp_wcqe(phba,
11368 (struct lpfc_wcqe_complete *)&wcqe);
11369 break;
11370 case CQE_CODE_RELEASE_WQE:
11371 /* Process the WQ release event */
11372 lpfc_sli4_fp_handle_rel_wcqe(phba, cq,
11373 (struct lpfc_wcqe_release *)&wcqe);
11374 break;
11375 case CQE_CODE_XRI_ABORTED:
11376 /* Process the WQ XRI abort event */
James Smartbc739052010-08-04 16:11:18 -040011377 phba->last_completion_time = jiffies;
James Smart4f774512009-05-22 14:52:35 -040011378 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
11379 (struct sli4_wcqe_xri_aborted *)&wcqe);
11380 break;
11381 default:
11382 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11383 "0144 Not a valid WCQE code: x%x\n",
11384 bf_get(lpfc_wcqe_c_code, &wcqe));
11385 break;
11386 }
11387 return workposted;
11388}
11389
11390/**
11391 * lpfc_sli4_fp_handle_eqe - Process a fast-path event queue entry
11392 * @phba: Pointer to HBA context object.
11393 * @eqe: Pointer to fast-path event queue entry.
11394 *
11395 * This routine process a event queue entry from the fast-path event queue.
11396 * It will check the MajorCode and MinorCode to determine this is for a
11397 * completion event on a completion queue, if not, an error shall be logged
11398 * and just return. Otherwise, it will get to the corresponding completion
11399 * queue and process all the entries on the completion queue, rearm the
11400 * completion queue, and then return.
11401 **/
11402static void
11403lpfc_sli4_fp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
11404 uint32_t fcp_cqidx)
11405{
11406 struct lpfc_queue *cq;
11407 struct lpfc_cqe *cqe;
11408 bool workposted = false;
11409 uint16_t cqid;
11410 int ecount = 0;
11411
James Smartcb5172e2010-03-15 11:25:07 -040011412 if (unlikely(bf_get_le32(lpfc_eqe_major_code, eqe) != 0)) {
James Smart4f774512009-05-22 14:52:35 -040011413 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11414 "0366 Not a valid fast-path completion "
11415 "event: majorcode=x%x, minorcode=x%x\n",
James Smartcb5172e2010-03-15 11:25:07 -040011416 bf_get_le32(lpfc_eqe_major_code, eqe),
11417 bf_get_le32(lpfc_eqe_minor_code, eqe));
James Smart4f774512009-05-22 14:52:35 -040011418 return;
11419 }
11420
James Smart2e90f4b2011-12-13 13:22:37 -050011421 if (unlikely(!phba->sli4_hba.fcp_cq)) {
11422 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11423 "3146 Fast-path completion queues "
11424 "does not exist\n");
11425 return;
11426 }
James Smart4f774512009-05-22 14:52:35 -040011427 cq = phba->sli4_hba.fcp_cq[fcp_cqidx];
11428 if (unlikely(!cq)) {
James Smart75baf692010-06-08 18:31:21 -040011429 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
11430 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11431 "0367 Fast-path completion queue "
James Smart2e90f4b2011-12-13 13:22:37 -050011432 "(%d) does not exist\n", fcp_cqidx);
James Smart4f774512009-05-22 14:52:35 -040011433 return;
11434 }
11435
11436 /* Get the reference to the corresponding CQ */
James Smartcb5172e2010-03-15 11:25:07 -040011437 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
James Smart4f774512009-05-22 14:52:35 -040011438 if (unlikely(cqid != cq->queue_id)) {
11439 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11440 "0368 Miss-matched fast-path completion "
11441 "queue identifier: eqcqid=%d, fcpcqid=%d\n",
11442 cqid, cq->queue_id);
11443 return;
11444 }
11445
11446 /* Process all the entries to the CQ */
11447 while ((cqe = lpfc_sli4_cq_get(cq))) {
11448 workposted |= lpfc_sli4_fp_handle_wcqe(phba, cq, cqe);
James Smart73d91e52011-10-10 21:32:10 -040011449 if (!(++ecount % cq->entry_repost))
James Smart4f774512009-05-22 14:52:35 -040011450 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
11451 }
11452
11453 /* Catch the no cq entry condition */
11454 if (unlikely(ecount == 0))
11455 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11456 "0369 No entry from fast-path completion "
11457 "queue fcpcqid=%d\n", cq->queue_id);
11458
11459 /* In any case, flash and re-arm the CQ */
11460 lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
11461
11462 /* wake up worker thread if there are works to be done */
11463 if (workposted)
11464 lpfc_worker_wake_up(phba);
11465}
11466
11467static void
11468lpfc_sli4_eq_flush(struct lpfc_hba *phba, struct lpfc_queue *eq)
11469{
11470 struct lpfc_eqe *eqe;
11471
11472 /* walk all the EQ entries and drop on the floor */
11473 while ((eqe = lpfc_sli4_eq_get(eq)))
11474 ;
11475
11476 /* Clear and re-arm the EQ */
11477 lpfc_sli4_eq_release(eq, LPFC_QUEUE_REARM);
11478}
11479
11480/**
11481 * lpfc_sli4_sp_intr_handler - Slow-path interrupt handler to SLI-4 device
11482 * @irq: Interrupt number.
11483 * @dev_id: The device context pointer.
11484 *
11485 * This function is directly called from the PCI layer as an interrupt
11486 * service routine when device with SLI-4 interface spec is enabled with
11487 * MSI-X multi-message interrupt mode and there are slow-path events in
11488 * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
11489 * interrupt mode, this function is called as part of the device-level
11490 * interrupt handler. When the PCI slot is in error recovery or the HBA is
11491 * undergoing initialization, the interrupt handler will not process the
11492 * interrupt. The link attention and ELS ring attention events are handled
11493 * by the worker thread. The interrupt handler signals the worker thread
11494 * and returns for these events. This function is called without any lock
11495 * held. It gets the hbalock to access and update SLI data structures.
11496 *
11497 * This function returns IRQ_HANDLED when interrupt is handled else it
11498 * returns IRQ_NONE.
11499 **/
11500irqreturn_t
11501lpfc_sli4_sp_intr_handler(int irq, void *dev_id)
11502{
11503 struct lpfc_hba *phba;
11504 struct lpfc_queue *speq;
11505 struct lpfc_eqe *eqe;
11506 unsigned long iflag;
11507 int ecount = 0;
11508
11509 /*
11510 * Get the driver's phba structure from the dev_id
11511 */
11512 phba = (struct lpfc_hba *)dev_id;
11513
11514 if (unlikely(!phba))
11515 return IRQ_NONE;
11516
11517 /* Get to the EQ struct associated with this vector */
11518 speq = phba->sli4_hba.sp_eq;
James Smart5350d872011-10-10 21:33:49 -040011519 if (unlikely(!speq))
11520 return IRQ_NONE;
James Smart4f774512009-05-22 14:52:35 -040011521
11522 /* Check device state for handling interrupt */
11523 if (unlikely(lpfc_intr_state_check(phba))) {
11524 /* Check again for link_state with lock held */
11525 spin_lock_irqsave(&phba->hbalock, iflag);
11526 if (phba->link_state < LPFC_LINK_DOWN)
11527 /* Flush, clear interrupt, and rearm the EQ */
11528 lpfc_sli4_eq_flush(phba, speq);
11529 spin_unlock_irqrestore(&phba->hbalock, iflag);
11530 return IRQ_NONE;
11531 }
11532
11533 /*
11534 * Process all the event on FCP slow-path EQ
11535 */
11536 while ((eqe = lpfc_sli4_eq_get(speq))) {
11537 lpfc_sli4_sp_handle_eqe(phba, eqe);
James Smart73d91e52011-10-10 21:32:10 -040011538 if (!(++ecount % speq->entry_repost))
James Smart4f774512009-05-22 14:52:35 -040011539 lpfc_sli4_eq_release(speq, LPFC_QUEUE_NOARM);
11540 }
11541
11542 /* Always clear and re-arm the slow-path EQ */
11543 lpfc_sli4_eq_release(speq, LPFC_QUEUE_REARM);
11544
11545 /* Catch the no cq entry condition */
11546 if (unlikely(ecount == 0)) {
11547 if (phba->intr_type == MSIX)
11548 /* MSI-X treated interrupt served as no EQ share INT */
11549 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11550 "0357 MSI-X interrupt with no EQE\n");
11551 else
11552 /* Non MSI-X treated on interrupt as EQ share INT */
11553 return IRQ_NONE;
11554 }
11555
11556 return IRQ_HANDLED;
11557} /* lpfc_sli4_sp_intr_handler */
11558
11559/**
11560 * lpfc_sli4_fp_intr_handler - Fast-path interrupt handler to SLI-4 device
11561 * @irq: Interrupt number.
11562 * @dev_id: The device context pointer.
11563 *
11564 * This function is directly called from the PCI layer as an interrupt
11565 * service routine when device with SLI-4 interface spec is enabled with
11566 * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
11567 * ring event in the HBA. However, when the device is enabled with either
11568 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
11569 * device-level interrupt handler. When the PCI slot is in error recovery
11570 * or the HBA is undergoing initialization, the interrupt handler will not
11571 * process the interrupt. The SCSI FCP fast-path ring event are handled in
11572 * the intrrupt context. This function is called without any lock held.
11573 * It gets the hbalock to access and update SLI data structures. Note that,
11574 * the FCP EQ to FCP CQ are one-to-one map such that the FCP EQ index is
11575 * equal to that of FCP CQ index.
11576 *
11577 * This function returns IRQ_HANDLED when interrupt is handled else it
11578 * returns IRQ_NONE.
11579 **/
11580irqreturn_t
11581lpfc_sli4_fp_intr_handler(int irq, void *dev_id)
11582{
11583 struct lpfc_hba *phba;
11584 struct lpfc_fcp_eq_hdl *fcp_eq_hdl;
11585 struct lpfc_queue *fpeq;
11586 struct lpfc_eqe *eqe;
11587 unsigned long iflag;
11588 int ecount = 0;
11589 uint32_t fcp_eqidx;
11590
11591 /* Get the driver's phba structure from the dev_id */
11592 fcp_eq_hdl = (struct lpfc_fcp_eq_hdl *)dev_id;
11593 phba = fcp_eq_hdl->phba;
11594 fcp_eqidx = fcp_eq_hdl->idx;
11595
11596 if (unlikely(!phba))
11597 return IRQ_NONE;
James Smart5350d872011-10-10 21:33:49 -040011598 if (unlikely(!phba->sli4_hba.fp_eq))
11599 return IRQ_NONE;
James Smart4f774512009-05-22 14:52:35 -040011600
11601 /* Get to the EQ struct associated with this vector */
11602 fpeq = phba->sli4_hba.fp_eq[fcp_eqidx];
James Smart2e90f4b2011-12-13 13:22:37 -050011603 if (unlikely(!fpeq))
11604 return IRQ_NONE;
James Smart4f774512009-05-22 14:52:35 -040011605
11606 /* Check device state for handling interrupt */
11607 if (unlikely(lpfc_intr_state_check(phba))) {
11608 /* Check again for link_state with lock held */
11609 spin_lock_irqsave(&phba->hbalock, iflag);
11610 if (phba->link_state < LPFC_LINK_DOWN)
11611 /* Flush, clear interrupt, and rearm the EQ */
11612 lpfc_sli4_eq_flush(phba, fpeq);
11613 spin_unlock_irqrestore(&phba->hbalock, iflag);
11614 return IRQ_NONE;
11615 }
11616
11617 /*
11618 * Process all the event on FCP fast-path EQ
11619 */
11620 while ((eqe = lpfc_sli4_eq_get(fpeq))) {
11621 lpfc_sli4_fp_handle_eqe(phba, eqe, fcp_eqidx);
James Smart73d91e52011-10-10 21:32:10 -040011622 if (!(++ecount % fpeq->entry_repost))
James Smart4f774512009-05-22 14:52:35 -040011623 lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_NOARM);
11624 }
11625
11626 /* Always clear and re-arm the fast-path EQ */
11627 lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_REARM);
11628
11629 if (unlikely(ecount == 0)) {
11630 if (phba->intr_type == MSIX)
11631 /* MSI-X treated interrupt served as no EQ share INT */
11632 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11633 "0358 MSI-X interrupt with no EQE\n");
11634 else
11635 /* Non MSI-X treated on interrupt as EQ share INT */
11636 return IRQ_NONE;
11637 }
11638
11639 return IRQ_HANDLED;
11640} /* lpfc_sli4_fp_intr_handler */
11641
11642/**
11643 * lpfc_sli4_intr_handler - Device-level interrupt handler for SLI-4 device
11644 * @irq: Interrupt number.
11645 * @dev_id: The device context pointer.
11646 *
11647 * This function is the device-level interrupt handler to device with SLI-4
11648 * interface spec, called from the PCI layer when either MSI or Pin-IRQ
11649 * interrupt mode is enabled and there is an event in the HBA which requires
11650 * driver attention. This function invokes the slow-path interrupt attention
11651 * handling function and fast-path interrupt attention handling function in
11652 * turn to process the relevant HBA attention events. This function is called
11653 * without any lock held. It gets the hbalock to access and update SLI data
11654 * structures.
11655 *
11656 * This function returns IRQ_HANDLED when interrupt is handled, else it
11657 * returns IRQ_NONE.
11658 **/
11659irqreturn_t
11660lpfc_sli4_intr_handler(int irq, void *dev_id)
11661{
11662 struct lpfc_hba *phba;
11663 irqreturn_t sp_irq_rc, fp_irq_rc;
11664 bool fp_handled = false;
11665 uint32_t fcp_eqidx;
11666
11667 /* Get the driver's phba structure from the dev_id */
11668 phba = (struct lpfc_hba *)dev_id;
11669
11670 if (unlikely(!phba))
11671 return IRQ_NONE;
11672
11673 /*
11674 * Invokes slow-path host attention interrupt handling as appropriate.
11675 */
11676 sp_irq_rc = lpfc_sli4_sp_intr_handler(irq, dev_id);
11677
11678 /*
11679 * Invoke fast-path host attention interrupt handling as appropriate.
11680 */
11681 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count; fcp_eqidx++) {
11682 fp_irq_rc = lpfc_sli4_fp_intr_handler(irq,
11683 &phba->sli4_hba.fcp_eq_hdl[fcp_eqidx]);
11684 if (fp_irq_rc == IRQ_HANDLED)
11685 fp_handled |= true;
11686 }
11687
11688 return (fp_handled == true) ? IRQ_HANDLED : sp_irq_rc;
11689} /* lpfc_sli4_intr_handler */
11690
11691/**
11692 * lpfc_sli4_queue_free - free a queue structure and associated memory
11693 * @queue: The queue structure to free.
11694 *
Uwe Kleine-Königb5950762010-11-01 15:38:34 -040011695 * This function frees a queue structure and the DMAable memory used for
James Smart4f774512009-05-22 14:52:35 -040011696 * the host resident queue. This function must be called after destroying the
11697 * queue on the HBA.
11698 **/
11699void
11700lpfc_sli4_queue_free(struct lpfc_queue *queue)
11701{
11702 struct lpfc_dmabuf *dmabuf;
11703
11704 if (!queue)
11705 return;
11706
11707 while (!list_empty(&queue->page_list)) {
11708 list_remove_head(&queue->page_list, dmabuf, struct lpfc_dmabuf,
11709 list);
James Smart49198b32010-04-06 15:04:33 -040011710 dma_free_coherent(&queue->phba->pcidev->dev, SLI4_PAGE_SIZE,
James Smart4f774512009-05-22 14:52:35 -040011711 dmabuf->virt, dmabuf->phys);
11712 kfree(dmabuf);
11713 }
11714 kfree(queue);
11715 return;
11716}
11717
11718/**
11719 * lpfc_sli4_queue_alloc - Allocate and initialize a queue structure
11720 * @phba: The HBA that this queue is being created on.
11721 * @entry_size: The size of each queue entry for this queue.
11722 * @entry count: The number of entries that this queue will handle.
11723 *
11724 * This function allocates a queue structure and the DMAable memory used for
11725 * the host resident queue. This function must be called before creating the
11726 * queue on the HBA.
11727 **/
11728struct lpfc_queue *
11729lpfc_sli4_queue_alloc(struct lpfc_hba *phba, uint32_t entry_size,
11730 uint32_t entry_count)
11731{
11732 struct lpfc_queue *queue;
11733 struct lpfc_dmabuf *dmabuf;
11734 int x, total_qe_count;
11735 void *dma_pointer;
James Smartcb5172e2010-03-15 11:25:07 -040011736 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smart4f774512009-05-22 14:52:35 -040011737
James Smartcb5172e2010-03-15 11:25:07 -040011738 if (!phba->sli4_hba.pc_sli4_params.supported)
11739 hw_page_size = SLI4_PAGE_SIZE;
11740
James Smart4f774512009-05-22 14:52:35 -040011741 queue = kzalloc(sizeof(struct lpfc_queue) +
11742 (sizeof(union sli4_qe) * entry_count), GFP_KERNEL);
11743 if (!queue)
11744 return NULL;
James Smartcb5172e2010-03-15 11:25:07 -040011745 queue->page_count = (ALIGN(entry_size * entry_count,
11746 hw_page_size))/hw_page_size;
James Smart4f774512009-05-22 14:52:35 -040011747 INIT_LIST_HEAD(&queue->list);
11748 INIT_LIST_HEAD(&queue->page_list);
11749 INIT_LIST_HEAD(&queue->child_list);
11750 for (x = 0, total_qe_count = 0; x < queue->page_count; x++) {
11751 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
11752 if (!dmabuf)
11753 goto out_fail;
11754 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
James Smartcb5172e2010-03-15 11:25:07 -040011755 hw_page_size, &dmabuf->phys,
James Smart4f774512009-05-22 14:52:35 -040011756 GFP_KERNEL);
11757 if (!dmabuf->virt) {
11758 kfree(dmabuf);
11759 goto out_fail;
11760 }
James Smartcb5172e2010-03-15 11:25:07 -040011761 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -040011762 dmabuf->buffer_tag = x;
11763 list_add_tail(&dmabuf->list, &queue->page_list);
11764 /* initialize queue's entry array */
11765 dma_pointer = dmabuf->virt;
11766 for (; total_qe_count < entry_count &&
James Smartcb5172e2010-03-15 11:25:07 -040011767 dma_pointer < (hw_page_size + dmabuf->virt);
James Smart4f774512009-05-22 14:52:35 -040011768 total_qe_count++, dma_pointer += entry_size) {
11769 queue->qe[total_qe_count].address = dma_pointer;
11770 }
11771 }
11772 queue->entry_size = entry_size;
11773 queue->entry_count = entry_count;
James Smart73d91e52011-10-10 21:32:10 -040011774
11775 /*
11776 * entry_repost is calculated based on the number of entries in the
11777 * queue. This works out except for RQs. If buffers are NOT initially
11778 * posted for every RQE, entry_repost should be adjusted accordingly.
11779 */
11780 queue->entry_repost = (entry_count >> 3);
11781 if (queue->entry_repost < LPFC_QUEUE_MIN_REPOST)
11782 queue->entry_repost = LPFC_QUEUE_MIN_REPOST;
James Smart4f774512009-05-22 14:52:35 -040011783 queue->phba = phba;
11784
11785 return queue;
11786out_fail:
11787 lpfc_sli4_queue_free(queue);
11788 return NULL;
11789}
11790
11791/**
11792 * lpfc_eq_create - Create an Event Queue on the HBA
11793 * @phba: HBA structure that indicates port to create a queue on.
11794 * @eq: The queue structure to use to create the event queue.
11795 * @imax: The maximum interrupt per second limit.
11796 *
11797 * This function creates an event queue, as detailed in @eq, on a port,
11798 * described by @phba by sending an EQ_CREATE mailbox command to the HBA.
11799 *
11800 * The @phba struct is used to send mailbox command to HBA. The @eq struct
11801 * is used to get the entry count and entry size that are necessary to
11802 * determine the number of pages to allocate and use for this queue. This
11803 * function will send the EQ_CREATE mailbox command to the HBA to setup the
11804 * event queue. This function is asynchronous and will wait for the mailbox
11805 * command to finish before continuing.
11806 *
11807 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040011808 * memory this function will return -ENOMEM. If the queue create mailbox command
11809 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040011810 **/
11811uint32_t
11812lpfc_eq_create(struct lpfc_hba *phba, struct lpfc_queue *eq, uint16_t imax)
11813{
11814 struct lpfc_mbx_eq_create *eq_create;
11815 LPFC_MBOXQ_t *mbox;
11816 int rc, length, status = 0;
11817 struct lpfc_dmabuf *dmabuf;
11818 uint32_t shdr_status, shdr_add_status;
11819 union lpfc_sli4_cfg_shdr *shdr;
11820 uint16_t dmult;
James Smart49198b32010-04-06 15:04:33 -040011821 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
11822
James Smart2e90f4b2011-12-13 13:22:37 -050011823 /* sanity check on queue memory */
11824 if (!eq)
11825 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040011826 if (!phba->sli4_hba.pc_sli4_params.supported)
11827 hw_page_size = SLI4_PAGE_SIZE;
James Smart4f774512009-05-22 14:52:35 -040011828
11829 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
11830 if (!mbox)
11831 return -ENOMEM;
11832 length = (sizeof(struct lpfc_mbx_eq_create) -
11833 sizeof(struct lpfc_sli4_cfg_mhdr));
11834 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
11835 LPFC_MBOX_OPCODE_EQ_CREATE,
11836 length, LPFC_SLI4_MBX_EMBED);
11837 eq_create = &mbox->u.mqe.un.eq_create;
11838 bf_set(lpfc_mbx_eq_create_num_pages, &eq_create->u.request,
11839 eq->page_count);
11840 bf_set(lpfc_eq_context_size, &eq_create->u.request.context,
11841 LPFC_EQE_SIZE);
11842 bf_set(lpfc_eq_context_valid, &eq_create->u.request.context, 1);
11843 /* Calculate delay multiper from maximum interrupt per second */
11844 dmult = LPFC_DMULT_CONST/imax - 1;
11845 bf_set(lpfc_eq_context_delay_multi, &eq_create->u.request.context,
11846 dmult);
11847 switch (eq->entry_count) {
11848 default:
11849 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11850 "0360 Unsupported EQ count. (%d)\n",
11851 eq->entry_count);
11852 if (eq->entry_count < 256)
11853 return -EINVAL;
11854 /* otherwise default to smallest count (drop through) */
11855 case 256:
11856 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
11857 LPFC_EQ_CNT_256);
11858 break;
11859 case 512:
11860 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
11861 LPFC_EQ_CNT_512);
11862 break;
11863 case 1024:
11864 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
11865 LPFC_EQ_CNT_1024);
11866 break;
11867 case 2048:
11868 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
11869 LPFC_EQ_CNT_2048);
11870 break;
11871 case 4096:
11872 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
11873 LPFC_EQ_CNT_4096);
11874 break;
11875 }
11876 list_for_each_entry(dmabuf, &eq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040011877 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -040011878 eq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
11879 putPaddrLow(dmabuf->phys);
11880 eq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
11881 putPaddrHigh(dmabuf->phys);
11882 }
11883 mbox->vport = phba->pport;
11884 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
11885 mbox->context1 = NULL;
11886 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
11887 shdr = (union lpfc_sli4_cfg_shdr *) &eq_create->header.cfg_shdr;
11888 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
11889 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
11890 if (shdr_status || shdr_add_status || rc) {
11891 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11892 "2500 EQ_CREATE mailbox failed with "
11893 "status x%x add_status x%x, mbx status x%x\n",
11894 shdr_status, shdr_add_status, rc);
11895 status = -ENXIO;
11896 }
11897 eq->type = LPFC_EQ;
11898 eq->subtype = LPFC_NONE;
11899 eq->queue_id = bf_get(lpfc_mbx_eq_create_q_id, &eq_create->u.response);
11900 if (eq->queue_id == 0xFFFF)
11901 status = -ENXIO;
11902 eq->host_index = 0;
11903 eq->hba_index = 0;
11904
James Smart8fa38512009-07-19 10:01:03 -040011905 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040011906 return status;
11907}
11908
11909/**
11910 * lpfc_cq_create - Create a Completion Queue on the HBA
11911 * @phba: HBA structure that indicates port to create a queue on.
11912 * @cq: The queue structure to use to create the completion queue.
11913 * @eq: The event queue to bind this completion queue to.
11914 *
11915 * This function creates a completion queue, as detailed in @wq, on a port,
11916 * described by @phba by sending a CQ_CREATE mailbox command to the HBA.
11917 *
11918 * The @phba struct is used to send mailbox command to HBA. The @cq struct
11919 * is used to get the entry count and entry size that are necessary to
11920 * determine the number of pages to allocate and use for this queue. The @eq
11921 * is used to indicate which event queue to bind this completion queue to. This
11922 * function will send the CQ_CREATE mailbox command to the HBA to setup the
11923 * completion queue. This function is asynchronous and will wait for the mailbox
11924 * command to finish before continuing.
11925 *
11926 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040011927 * memory this function will return -ENOMEM. If the queue create mailbox command
11928 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040011929 **/
11930uint32_t
11931lpfc_cq_create(struct lpfc_hba *phba, struct lpfc_queue *cq,
11932 struct lpfc_queue *eq, uint32_t type, uint32_t subtype)
11933{
11934 struct lpfc_mbx_cq_create *cq_create;
11935 struct lpfc_dmabuf *dmabuf;
11936 LPFC_MBOXQ_t *mbox;
11937 int rc, length, status = 0;
11938 uint32_t shdr_status, shdr_add_status;
11939 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040011940 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
11941
James Smart2e90f4b2011-12-13 13:22:37 -050011942 /* sanity check on queue memory */
11943 if (!cq || !eq)
11944 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040011945 if (!phba->sli4_hba.pc_sli4_params.supported)
11946 hw_page_size = SLI4_PAGE_SIZE;
11947
James Smart4f774512009-05-22 14:52:35 -040011948 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
11949 if (!mbox)
11950 return -ENOMEM;
11951 length = (sizeof(struct lpfc_mbx_cq_create) -
11952 sizeof(struct lpfc_sli4_cfg_mhdr));
11953 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
11954 LPFC_MBOX_OPCODE_CQ_CREATE,
11955 length, LPFC_SLI4_MBX_EMBED);
11956 cq_create = &mbox->u.mqe.un.cq_create;
James Smart5a6f1332011-03-11 16:05:35 -050011957 shdr = (union lpfc_sli4_cfg_shdr *) &cq_create->header.cfg_shdr;
James Smart4f774512009-05-22 14:52:35 -040011958 bf_set(lpfc_mbx_cq_create_num_pages, &cq_create->u.request,
11959 cq->page_count);
11960 bf_set(lpfc_cq_context_event, &cq_create->u.request.context, 1);
11961 bf_set(lpfc_cq_context_valid, &cq_create->u.request.context, 1);
James Smart5a6f1332011-03-11 16:05:35 -050011962 bf_set(lpfc_mbox_hdr_version, &shdr->request,
11963 phba->sli4_hba.pc_sli4_params.cqv);
11964 if (phba->sli4_hba.pc_sli4_params.cqv == LPFC_Q_CREATE_VERSION_2) {
James Smartc31098c2011-04-16 11:03:33 -040011965 /* FW only supports 1. Should be PAGE_SIZE/SLI4_PAGE_SIZE */
11966 bf_set(lpfc_mbx_cq_create_page_size, &cq_create->u.request, 1);
James Smart5a6f1332011-03-11 16:05:35 -050011967 bf_set(lpfc_cq_eq_id_2, &cq_create->u.request.context,
11968 eq->queue_id);
11969 } else {
11970 bf_set(lpfc_cq_eq_id, &cq_create->u.request.context,
11971 eq->queue_id);
11972 }
James Smart4f774512009-05-22 14:52:35 -040011973 switch (cq->entry_count) {
11974 default:
11975 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11976 "0361 Unsupported CQ count. (%d)\n",
11977 cq->entry_count);
11978 if (cq->entry_count < 256)
11979 return -EINVAL;
11980 /* otherwise default to smallest count (drop through) */
11981 case 256:
11982 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
11983 LPFC_CQ_CNT_256);
11984 break;
11985 case 512:
11986 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
11987 LPFC_CQ_CNT_512);
11988 break;
11989 case 1024:
11990 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
11991 LPFC_CQ_CNT_1024);
11992 break;
11993 }
11994 list_for_each_entry(dmabuf, &cq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040011995 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -040011996 cq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
11997 putPaddrLow(dmabuf->phys);
11998 cq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
11999 putPaddrHigh(dmabuf->phys);
12000 }
12001 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
12002
12003 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart4f774512009-05-22 14:52:35 -040012004 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12005 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12006 if (shdr_status || shdr_add_status || rc) {
12007 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12008 "2501 CQ_CREATE mailbox failed with "
12009 "status x%x add_status x%x, mbx status x%x\n",
12010 shdr_status, shdr_add_status, rc);
12011 status = -ENXIO;
12012 goto out;
12013 }
12014 cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
12015 if (cq->queue_id == 0xFFFF) {
12016 status = -ENXIO;
12017 goto out;
12018 }
12019 /* link the cq onto the parent eq child list */
12020 list_add_tail(&cq->list, &eq->child_list);
12021 /* Set up completion queue's type and subtype */
12022 cq->type = type;
12023 cq->subtype = subtype;
12024 cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
James Smart2a622bf2011-02-16 12:40:06 -050012025 cq->assoc_qid = eq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040012026 cq->host_index = 0;
12027 cq->hba_index = 0;
James Smart4f774512009-05-22 14:52:35 -040012028
James Smart8fa38512009-07-19 10:01:03 -040012029out:
12030 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040012031 return status;
12032}
12033
12034/**
James Smartb19a0612010-04-06 14:48:51 -040012035 * lpfc_mq_create_fb_init - Send MCC_CREATE without async events registration
James Smart04c68492009-05-22 14:52:52 -040012036 * @phba: HBA structure that indicates port to create a queue on.
12037 * @mq: The queue structure to use to create the mailbox queue.
James Smartb19a0612010-04-06 14:48:51 -040012038 * @mbox: An allocated pointer to type LPFC_MBOXQ_t
12039 * @cq: The completion queue to associate with this cq.
James Smart04c68492009-05-22 14:52:52 -040012040 *
James Smartb19a0612010-04-06 14:48:51 -040012041 * This function provides failback (fb) functionality when the
12042 * mq_create_ext fails on older FW generations. It's purpose is identical
12043 * to mq_create_ext otherwise.
James Smart04c68492009-05-22 14:52:52 -040012044 *
James Smartb19a0612010-04-06 14:48:51 -040012045 * This routine cannot fail as all attributes were previously accessed and
12046 * initialized in mq_create_ext.
James Smart04c68492009-05-22 14:52:52 -040012047 **/
James Smartb19a0612010-04-06 14:48:51 -040012048static void
12049lpfc_mq_create_fb_init(struct lpfc_hba *phba, struct lpfc_queue *mq,
12050 LPFC_MBOXQ_t *mbox, struct lpfc_queue *cq)
James Smart04c68492009-05-22 14:52:52 -040012051{
12052 struct lpfc_mbx_mq_create *mq_create;
12053 struct lpfc_dmabuf *dmabuf;
James Smartb19a0612010-04-06 14:48:51 -040012054 int length;
James Smart04c68492009-05-22 14:52:52 -040012055
James Smart04c68492009-05-22 14:52:52 -040012056 length = (sizeof(struct lpfc_mbx_mq_create) -
12057 sizeof(struct lpfc_sli4_cfg_mhdr));
12058 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
12059 LPFC_MBOX_OPCODE_MQ_CREATE,
12060 length, LPFC_SLI4_MBX_EMBED);
12061 mq_create = &mbox->u.mqe.un.mq_create;
12062 bf_set(lpfc_mbx_mq_create_num_pages, &mq_create->u.request,
James Smartb19a0612010-04-06 14:48:51 -040012063 mq->page_count);
James Smart04c68492009-05-22 14:52:52 -040012064 bf_set(lpfc_mq_context_cq_id, &mq_create->u.request.context,
James Smartb19a0612010-04-06 14:48:51 -040012065 cq->queue_id);
James Smart04c68492009-05-22 14:52:52 -040012066 bf_set(lpfc_mq_context_valid, &mq_create->u.request.context, 1);
12067 switch (mq->entry_count) {
James Smart04c68492009-05-22 14:52:52 -040012068 case 16:
James Smart5a6f1332011-03-11 16:05:35 -050012069 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
12070 LPFC_MQ_RING_SIZE_16);
James Smart04c68492009-05-22 14:52:52 -040012071 break;
12072 case 32:
James Smart5a6f1332011-03-11 16:05:35 -050012073 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
12074 LPFC_MQ_RING_SIZE_32);
James Smart04c68492009-05-22 14:52:52 -040012075 break;
12076 case 64:
James Smart5a6f1332011-03-11 16:05:35 -050012077 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
12078 LPFC_MQ_RING_SIZE_64);
James Smart04c68492009-05-22 14:52:52 -040012079 break;
12080 case 128:
James Smart5a6f1332011-03-11 16:05:35 -050012081 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
12082 LPFC_MQ_RING_SIZE_128);
James Smart04c68492009-05-22 14:52:52 -040012083 break;
12084 }
12085 list_for_each_entry(dmabuf, &mq->page_list, list) {
12086 mq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
James Smartb19a0612010-04-06 14:48:51 -040012087 putPaddrLow(dmabuf->phys);
James Smart04c68492009-05-22 14:52:52 -040012088 mq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
James Smartb19a0612010-04-06 14:48:51 -040012089 putPaddrHigh(dmabuf->phys);
12090 }
12091}
12092
12093/**
12094 * lpfc_mq_create - Create a mailbox Queue on the HBA
12095 * @phba: HBA structure that indicates port to create a queue on.
12096 * @mq: The queue structure to use to create the mailbox queue.
12097 * @cq: The completion queue to associate with this cq.
12098 * @subtype: The queue's subtype.
12099 *
12100 * This function creates a mailbox queue, as detailed in @mq, on a port,
12101 * described by @phba by sending a MQ_CREATE mailbox command to the HBA.
12102 *
12103 * The @phba struct is used to send mailbox command to HBA. The @cq struct
12104 * is used to get the entry count and entry size that are necessary to
12105 * determine the number of pages to allocate and use for this queue. This
12106 * function will send the MQ_CREATE mailbox command to the HBA to setup the
12107 * mailbox queue. This function is asynchronous and will wait for the mailbox
12108 * command to finish before continuing.
12109 *
12110 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040012111 * memory this function will return -ENOMEM. If the queue create mailbox command
12112 * fails this function will return -ENXIO.
James Smartb19a0612010-04-06 14:48:51 -040012113 **/
12114int32_t
12115lpfc_mq_create(struct lpfc_hba *phba, struct lpfc_queue *mq,
12116 struct lpfc_queue *cq, uint32_t subtype)
12117{
12118 struct lpfc_mbx_mq_create *mq_create;
12119 struct lpfc_mbx_mq_create_ext *mq_create_ext;
12120 struct lpfc_dmabuf *dmabuf;
12121 LPFC_MBOXQ_t *mbox;
12122 int rc, length, status = 0;
12123 uint32_t shdr_status, shdr_add_status;
12124 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040012125 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smartb19a0612010-04-06 14:48:51 -040012126
James Smart2e90f4b2011-12-13 13:22:37 -050012127 /* sanity check on queue memory */
12128 if (!mq || !cq)
12129 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040012130 if (!phba->sli4_hba.pc_sli4_params.supported)
12131 hw_page_size = SLI4_PAGE_SIZE;
James Smartb19a0612010-04-06 14:48:51 -040012132
12133 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12134 if (!mbox)
12135 return -ENOMEM;
12136 length = (sizeof(struct lpfc_mbx_mq_create_ext) -
12137 sizeof(struct lpfc_sli4_cfg_mhdr));
12138 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
12139 LPFC_MBOX_OPCODE_MQ_CREATE_EXT,
12140 length, LPFC_SLI4_MBX_EMBED);
12141
12142 mq_create_ext = &mbox->u.mqe.un.mq_create_ext;
James Smart5a6f1332011-03-11 16:05:35 -050012143 shdr = (union lpfc_sli4_cfg_shdr *) &mq_create_ext->header.cfg_shdr;
James Smart70f3c072010-12-15 17:57:33 -050012144 bf_set(lpfc_mbx_mq_create_ext_num_pages,
12145 &mq_create_ext->u.request, mq->page_count);
12146 bf_set(lpfc_mbx_mq_create_ext_async_evt_link,
12147 &mq_create_ext->u.request, 1);
12148 bf_set(lpfc_mbx_mq_create_ext_async_evt_fip,
James Smartb19a0612010-04-06 14:48:51 -040012149 &mq_create_ext->u.request, 1);
12150 bf_set(lpfc_mbx_mq_create_ext_async_evt_group5,
12151 &mq_create_ext->u.request, 1);
James Smart70f3c072010-12-15 17:57:33 -050012152 bf_set(lpfc_mbx_mq_create_ext_async_evt_fc,
12153 &mq_create_ext->u.request, 1);
12154 bf_set(lpfc_mbx_mq_create_ext_async_evt_sli,
12155 &mq_create_ext->u.request, 1);
James Smartb19a0612010-04-06 14:48:51 -040012156 bf_set(lpfc_mq_context_valid, &mq_create_ext->u.request.context, 1);
James Smart5a6f1332011-03-11 16:05:35 -050012157 bf_set(lpfc_mbox_hdr_version, &shdr->request,
12158 phba->sli4_hba.pc_sli4_params.mqv);
12159 if (phba->sli4_hba.pc_sli4_params.mqv == LPFC_Q_CREATE_VERSION_1)
12160 bf_set(lpfc_mbx_mq_create_ext_cq_id, &mq_create_ext->u.request,
12161 cq->queue_id);
12162 else
12163 bf_set(lpfc_mq_context_cq_id, &mq_create_ext->u.request.context,
12164 cq->queue_id);
James Smartb19a0612010-04-06 14:48:51 -040012165 switch (mq->entry_count) {
12166 default:
12167 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12168 "0362 Unsupported MQ count. (%d)\n",
12169 mq->entry_count);
12170 if (mq->entry_count < 16)
12171 return -EINVAL;
12172 /* otherwise default to smallest count (drop through) */
12173 case 16:
James Smart5a6f1332011-03-11 16:05:35 -050012174 bf_set(lpfc_mq_context_ring_size,
12175 &mq_create_ext->u.request.context,
12176 LPFC_MQ_RING_SIZE_16);
James Smartb19a0612010-04-06 14:48:51 -040012177 break;
12178 case 32:
James Smart5a6f1332011-03-11 16:05:35 -050012179 bf_set(lpfc_mq_context_ring_size,
12180 &mq_create_ext->u.request.context,
12181 LPFC_MQ_RING_SIZE_32);
James Smartb19a0612010-04-06 14:48:51 -040012182 break;
12183 case 64:
James Smart5a6f1332011-03-11 16:05:35 -050012184 bf_set(lpfc_mq_context_ring_size,
12185 &mq_create_ext->u.request.context,
12186 LPFC_MQ_RING_SIZE_64);
James Smartb19a0612010-04-06 14:48:51 -040012187 break;
12188 case 128:
James Smart5a6f1332011-03-11 16:05:35 -050012189 bf_set(lpfc_mq_context_ring_size,
12190 &mq_create_ext->u.request.context,
12191 LPFC_MQ_RING_SIZE_128);
James Smartb19a0612010-04-06 14:48:51 -040012192 break;
12193 }
12194 list_for_each_entry(dmabuf, &mq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040012195 memset(dmabuf->virt, 0, hw_page_size);
James Smartb19a0612010-04-06 14:48:51 -040012196 mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_lo =
12197 putPaddrLow(dmabuf->phys);
12198 mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_hi =
James Smart04c68492009-05-22 14:52:52 -040012199 putPaddrHigh(dmabuf->phys);
12200 }
12201 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
James Smartb19a0612010-04-06 14:48:51 -040012202 mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
12203 &mq_create_ext->u.response);
12204 if (rc != MBX_SUCCESS) {
12205 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
12206 "2795 MQ_CREATE_EXT failed with "
12207 "status x%x. Failback to MQ_CREATE.\n",
12208 rc);
12209 lpfc_mq_create_fb_init(phba, mq, mbox, cq);
12210 mq_create = &mbox->u.mqe.un.mq_create;
12211 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
12212 shdr = (union lpfc_sli4_cfg_shdr *) &mq_create->header.cfg_shdr;
12213 mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
12214 &mq_create->u.response);
12215 }
12216
James Smart04c68492009-05-22 14:52:52 -040012217 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart04c68492009-05-22 14:52:52 -040012218 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12219 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12220 if (shdr_status || shdr_add_status || rc) {
12221 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12222 "2502 MQ_CREATE mailbox failed with "
12223 "status x%x add_status x%x, mbx status x%x\n",
12224 shdr_status, shdr_add_status, rc);
12225 status = -ENXIO;
12226 goto out;
12227 }
James Smart04c68492009-05-22 14:52:52 -040012228 if (mq->queue_id == 0xFFFF) {
12229 status = -ENXIO;
12230 goto out;
12231 }
12232 mq->type = LPFC_MQ;
James Smart2a622bf2011-02-16 12:40:06 -050012233 mq->assoc_qid = cq->queue_id;
James Smart04c68492009-05-22 14:52:52 -040012234 mq->subtype = subtype;
12235 mq->host_index = 0;
12236 mq->hba_index = 0;
12237
12238 /* link the mq onto the parent cq child list */
12239 list_add_tail(&mq->list, &cq->child_list);
12240out:
James Smart8fa38512009-07-19 10:01:03 -040012241 mempool_free(mbox, phba->mbox_mem_pool);
James Smart04c68492009-05-22 14:52:52 -040012242 return status;
12243}
12244
12245/**
James Smart4f774512009-05-22 14:52:35 -040012246 * lpfc_wq_create - Create a Work Queue on the HBA
12247 * @phba: HBA structure that indicates port to create a queue on.
12248 * @wq: The queue structure to use to create the work queue.
12249 * @cq: The completion queue to bind this work queue to.
12250 * @subtype: The subtype of the work queue indicating its functionality.
12251 *
12252 * This function creates a work queue, as detailed in @wq, on a port, described
12253 * by @phba by sending a WQ_CREATE mailbox command to the HBA.
12254 *
12255 * The @phba struct is used to send mailbox command to HBA. The @wq struct
12256 * is used to get the entry count and entry size that are necessary to
12257 * determine the number of pages to allocate and use for this queue. The @cq
12258 * is used to indicate which completion queue to bind this work queue to. This
12259 * function will send the WQ_CREATE mailbox command to the HBA to setup the
12260 * work queue. This function is asynchronous and will wait for the mailbox
12261 * command to finish before continuing.
12262 *
12263 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040012264 * memory this function will return -ENOMEM. If the queue create mailbox command
12265 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040012266 **/
12267uint32_t
12268lpfc_wq_create(struct lpfc_hba *phba, struct lpfc_queue *wq,
12269 struct lpfc_queue *cq, uint32_t subtype)
12270{
12271 struct lpfc_mbx_wq_create *wq_create;
12272 struct lpfc_dmabuf *dmabuf;
12273 LPFC_MBOXQ_t *mbox;
12274 int rc, length, status = 0;
12275 uint32_t shdr_status, shdr_add_status;
12276 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040012277 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smart5a6f1332011-03-11 16:05:35 -050012278 struct dma_address *page;
James Smart49198b32010-04-06 15:04:33 -040012279
James Smart2e90f4b2011-12-13 13:22:37 -050012280 /* sanity check on queue memory */
12281 if (!wq || !cq)
12282 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040012283 if (!phba->sli4_hba.pc_sli4_params.supported)
12284 hw_page_size = SLI4_PAGE_SIZE;
James Smart4f774512009-05-22 14:52:35 -040012285
12286 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12287 if (!mbox)
12288 return -ENOMEM;
12289 length = (sizeof(struct lpfc_mbx_wq_create) -
12290 sizeof(struct lpfc_sli4_cfg_mhdr));
12291 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
12292 LPFC_MBOX_OPCODE_FCOE_WQ_CREATE,
12293 length, LPFC_SLI4_MBX_EMBED);
12294 wq_create = &mbox->u.mqe.un.wq_create;
James Smart5a6f1332011-03-11 16:05:35 -050012295 shdr = (union lpfc_sli4_cfg_shdr *) &wq_create->header.cfg_shdr;
James Smart4f774512009-05-22 14:52:35 -040012296 bf_set(lpfc_mbx_wq_create_num_pages, &wq_create->u.request,
12297 wq->page_count);
12298 bf_set(lpfc_mbx_wq_create_cq_id, &wq_create->u.request,
12299 cq->queue_id);
James Smart5a6f1332011-03-11 16:05:35 -050012300 bf_set(lpfc_mbox_hdr_version, &shdr->request,
12301 phba->sli4_hba.pc_sli4_params.wqv);
12302 if (phba->sli4_hba.pc_sli4_params.wqv == LPFC_Q_CREATE_VERSION_1) {
12303 bf_set(lpfc_mbx_wq_create_wqe_count, &wq_create->u.request_1,
12304 wq->entry_count);
12305 switch (wq->entry_size) {
12306 default:
12307 case 64:
12308 bf_set(lpfc_mbx_wq_create_wqe_size,
12309 &wq_create->u.request_1,
12310 LPFC_WQ_WQE_SIZE_64);
12311 break;
12312 case 128:
12313 bf_set(lpfc_mbx_wq_create_wqe_size,
12314 &wq_create->u.request_1,
12315 LPFC_WQ_WQE_SIZE_128);
12316 break;
12317 }
12318 bf_set(lpfc_mbx_wq_create_page_size, &wq_create->u.request_1,
12319 (PAGE_SIZE/SLI4_PAGE_SIZE));
12320 page = wq_create->u.request_1.page;
12321 } else {
12322 page = wq_create->u.request.page;
12323 }
James Smart4f774512009-05-22 14:52:35 -040012324 list_for_each_entry(dmabuf, &wq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040012325 memset(dmabuf->virt, 0, hw_page_size);
James Smart5a6f1332011-03-11 16:05:35 -050012326 page[dmabuf->buffer_tag].addr_lo = putPaddrLow(dmabuf->phys);
12327 page[dmabuf->buffer_tag].addr_hi = putPaddrHigh(dmabuf->phys);
James Smart4f774512009-05-22 14:52:35 -040012328 }
12329 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
12330 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart4f774512009-05-22 14:52:35 -040012331 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12332 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12333 if (shdr_status || shdr_add_status || rc) {
12334 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12335 "2503 WQ_CREATE mailbox failed with "
12336 "status x%x add_status x%x, mbx status x%x\n",
12337 shdr_status, shdr_add_status, rc);
12338 status = -ENXIO;
12339 goto out;
12340 }
12341 wq->queue_id = bf_get(lpfc_mbx_wq_create_q_id, &wq_create->u.response);
12342 if (wq->queue_id == 0xFFFF) {
12343 status = -ENXIO;
12344 goto out;
12345 }
12346 wq->type = LPFC_WQ;
James Smart2a622bf2011-02-16 12:40:06 -050012347 wq->assoc_qid = cq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040012348 wq->subtype = subtype;
12349 wq->host_index = 0;
12350 wq->hba_index = 0;
James Smartff78d8f2011-12-13 13:21:35 -050012351 wq->entry_repost = LPFC_RELEASE_NOTIFICATION_INTERVAL;
James Smart4f774512009-05-22 14:52:35 -040012352
12353 /* link the wq onto the parent cq child list */
12354 list_add_tail(&wq->list, &cq->child_list);
12355out:
James Smart8fa38512009-07-19 10:01:03 -040012356 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040012357 return status;
12358}
12359
12360/**
James Smart73d91e52011-10-10 21:32:10 -040012361 * lpfc_rq_adjust_repost - Adjust entry_repost for an RQ
12362 * @phba: HBA structure that indicates port to create a queue on.
12363 * @rq: The queue structure to use for the receive queue.
12364 * @qno: The associated HBQ number
12365 *
12366 *
12367 * For SLI4 we need to adjust the RQ repost value based on
12368 * the number of buffers that are initially posted to the RQ.
12369 */
12370void
12371lpfc_rq_adjust_repost(struct lpfc_hba *phba, struct lpfc_queue *rq, int qno)
12372{
12373 uint32_t cnt;
12374
James Smart2e90f4b2011-12-13 13:22:37 -050012375 /* sanity check on queue memory */
12376 if (!rq)
12377 return;
James Smart73d91e52011-10-10 21:32:10 -040012378 cnt = lpfc_hbq_defs[qno]->entry_count;
12379
12380 /* Recalc repost for RQs based on buffers initially posted */
12381 cnt = (cnt >> 3);
12382 if (cnt < LPFC_QUEUE_MIN_REPOST)
12383 cnt = LPFC_QUEUE_MIN_REPOST;
12384
12385 rq->entry_repost = cnt;
12386}
12387
12388/**
James Smart4f774512009-05-22 14:52:35 -040012389 * lpfc_rq_create - Create a Receive Queue on the HBA
12390 * @phba: HBA structure that indicates port to create a queue on.
12391 * @hrq: The queue structure to use to create the header receive queue.
12392 * @drq: The queue structure to use to create the data receive queue.
12393 * @cq: The completion queue to bind this work queue to.
12394 *
12395 * This function creates a receive buffer queue pair , as detailed in @hrq and
12396 * @drq, on a port, described by @phba by sending a RQ_CREATE mailbox command
12397 * to the HBA.
12398 *
12399 * The @phba struct is used to send mailbox command to HBA. The @drq and @hrq
12400 * struct is used to get the entry count that is necessary to determine the
12401 * number of pages to use for this queue. The @cq is used to indicate which
12402 * completion queue to bind received buffers that are posted to these queues to.
12403 * This function will send the RQ_CREATE mailbox command to the HBA to setup the
12404 * receive queue pair. This function is asynchronous and will wait for the
12405 * mailbox command to finish before continuing.
12406 *
12407 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040012408 * memory this function will return -ENOMEM. If the queue create mailbox command
12409 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040012410 **/
12411uint32_t
12412lpfc_rq_create(struct lpfc_hba *phba, struct lpfc_queue *hrq,
12413 struct lpfc_queue *drq, struct lpfc_queue *cq, uint32_t subtype)
12414{
12415 struct lpfc_mbx_rq_create *rq_create;
12416 struct lpfc_dmabuf *dmabuf;
12417 LPFC_MBOXQ_t *mbox;
12418 int rc, length, status = 0;
12419 uint32_t shdr_status, shdr_add_status;
12420 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040012421 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
12422
James Smart2e90f4b2011-12-13 13:22:37 -050012423 /* sanity check on queue memory */
12424 if (!hrq || !drq || !cq)
12425 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040012426 if (!phba->sli4_hba.pc_sli4_params.supported)
12427 hw_page_size = SLI4_PAGE_SIZE;
James Smart4f774512009-05-22 14:52:35 -040012428
12429 if (hrq->entry_count != drq->entry_count)
12430 return -EINVAL;
12431 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12432 if (!mbox)
12433 return -ENOMEM;
12434 length = (sizeof(struct lpfc_mbx_rq_create) -
12435 sizeof(struct lpfc_sli4_cfg_mhdr));
12436 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
12437 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
12438 length, LPFC_SLI4_MBX_EMBED);
12439 rq_create = &mbox->u.mqe.un.rq_create;
James Smart5a6f1332011-03-11 16:05:35 -050012440 shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
12441 bf_set(lpfc_mbox_hdr_version, &shdr->request,
12442 phba->sli4_hba.pc_sli4_params.rqv);
12443 if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
12444 bf_set(lpfc_rq_context_rqe_count_1,
12445 &rq_create->u.request.context,
12446 hrq->entry_count);
12447 rq_create->u.request.context.buffer_size = LPFC_HDR_BUF_SIZE;
James Smartc31098c2011-04-16 11:03:33 -040012448 bf_set(lpfc_rq_context_rqe_size,
12449 &rq_create->u.request.context,
12450 LPFC_RQE_SIZE_8);
12451 bf_set(lpfc_rq_context_page_size,
12452 &rq_create->u.request.context,
12453 (PAGE_SIZE/SLI4_PAGE_SIZE));
James Smart5a6f1332011-03-11 16:05:35 -050012454 } else {
12455 switch (hrq->entry_count) {
12456 default:
12457 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12458 "2535 Unsupported RQ count. (%d)\n",
12459 hrq->entry_count);
12460 if (hrq->entry_count < 512)
12461 return -EINVAL;
12462 /* otherwise default to smallest count (drop through) */
12463 case 512:
12464 bf_set(lpfc_rq_context_rqe_count,
12465 &rq_create->u.request.context,
12466 LPFC_RQ_RING_SIZE_512);
12467 break;
12468 case 1024:
12469 bf_set(lpfc_rq_context_rqe_count,
12470 &rq_create->u.request.context,
12471 LPFC_RQ_RING_SIZE_1024);
12472 break;
12473 case 2048:
12474 bf_set(lpfc_rq_context_rqe_count,
12475 &rq_create->u.request.context,
12476 LPFC_RQ_RING_SIZE_2048);
12477 break;
12478 case 4096:
12479 bf_set(lpfc_rq_context_rqe_count,
12480 &rq_create->u.request.context,
12481 LPFC_RQ_RING_SIZE_4096);
12482 break;
12483 }
12484 bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
12485 LPFC_HDR_BUF_SIZE);
James Smart4f774512009-05-22 14:52:35 -040012486 }
12487 bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
12488 cq->queue_id);
12489 bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
12490 hrq->page_count);
James Smart4f774512009-05-22 14:52:35 -040012491 list_for_each_entry(dmabuf, &hrq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040012492 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -040012493 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
12494 putPaddrLow(dmabuf->phys);
12495 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
12496 putPaddrHigh(dmabuf->phys);
12497 }
12498 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
12499 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart4f774512009-05-22 14:52:35 -040012500 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12501 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12502 if (shdr_status || shdr_add_status || rc) {
12503 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12504 "2504 RQ_CREATE mailbox failed with "
12505 "status x%x add_status x%x, mbx status x%x\n",
12506 shdr_status, shdr_add_status, rc);
12507 status = -ENXIO;
12508 goto out;
12509 }
12510 hrq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
12511 if (hrq->queue_id == 0xFFFF) {
12512 status = -ENXIO;
12513 goto out;
12514 }
12515 hrq->type = LPFC_HRQ;
James Smart2a622bf2011-02-16 12:40:06 -050012516 hrq->assoc_qid = cq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040012517 hrq->subtype = subtype;
12518 hrq->host_index = 0;
12519 hrq->hba_index = 0;
12520
12521 /* now create the data queue */
12522 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
12523 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
12524 length, LPFC_SLI4_MBX_EMBED);
James Smart5a6f1332011-03-11 16:05:35 -050012525 bf_set(lpfc_mbox_hdr_version, &shdr->request,
12526 phba->sli4_hba.pc_sli4_params.rqv);
12527 if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
12528 bf_set(lpfc_rq_context_rqe_count_1,
James Smartc31098c2011-04-16 11:03:33 -040012529 &rq_create->u.request.context, hrq->entry_count);
James Smart5a6f1332011-03-11 16:05:35 -050012530 rq_create->u.request.context.buffer_size = LPFC_DATA_BUF_SIZE;
James Smartc31098c2011-04-16 11:03:33 -040012531 bf_set(lpfc_rq_context_rqe_size, &rq_create->u.request.context,
12532 LPFC_RQE_SIZE_8);
12533 bf_set(lpfc_rq_context_page_size, &rq_create->u.request.context,
12534 (PAGE_SIZE/SLI4_PAGE_SIZE));
James Smart5a6f1332011-03-11 16:05:35 -050012535 } else {
12536 switch (drq->entry_count) {
12537 default:
12538 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12539 "2536 Unsupported RQ count. (%d)\n",
12540 drq->entry_count);
12541 if (drq->entry_count < 512)
12542 return -EINVAL;
12543 /* otherwise default to smallest count (drop through) */
12544 case 512:
12545 bf_set(lpfc_rq_context_rqe_count,
12546 &rq_create->u.request.context,
12547 LPFC_RQ_RING_SIZE_512);
12548 break;
12549 case 1024:
12550 bf_set(lpfc_rq_context_rqe_count,
12551 &rq_create->u.request.context,
12552 LPFC_RQ_RING_SIZE_1024);
12553 break;
12554 case 2048:
12555 bf_set(lpfc_rq_context_rqe_count,
12556 &rq_create->u.request.context,
12557 LPFC_RQ_RING_SIZE_2048);
12558 break;
12559 case 4096:
12560 bf_set(lpfc_rq_context_rqe_count,
12561 &rq_create->u.request.context,
12562 LPFC_RQ_RING_SIZE_4096);
12563 break;
12564 }
12565 bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
12566 LPFC_DATA_BUF_SIZE);
James Smart4f774512009-05-22 14:52:35 -040012567 }
12568 bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
12569 cq->queue_id);
12570 bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
12571 drq->page_count);
James Smart4f774512009-05-22 14:52:35 -040012572 list_for_each_entry(dmabuf, &drq->page_list, list) {
12573 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
12574 putPaddrLow(dmabuf->phys);
12575 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
12576 putPaddrHigh(dmabuf->phys);
12577 }
12578 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
12579 /* The IOCTL status is embedded in the mailbox subheader. */
12580 shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
12581 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12582 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12583 if (shdr_status || shdr_add_status || rc) {
12584 status = -ENXIO;
12585 goto out;
12586 }
12587 drq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
12588 if (drq->queue_id == 0xFFFF) {
12589 status = -ENXIO;
12590 goto out;
12591 }
12592 drq->type = LPFC_DRQ;
James Smart2a622bf2011-02-16 12:40:06 -050012593 drq->assoc_qid = cq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040012594 drq->subtype = subtype;
12595 drq->host_index = 0;
12596 drq->hba_index = 0;
12597
12598 /* link the header and data RQs onto the parent cq child list */
12599 list_add_tail(&hrq->list, &cq->child_list);
12600 list_add_tail(&drq->list, &cq->child_list);
12601
12602out:
James Smart8fa38512009-07-19 10:01:03 -040012603 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040012604 return status;
12605}
12606
12607/**
12608 * lpfc_eq_destroy - Destroy an event Queue on the HBA
12609 * @eq: The queue structure associated with the queue to destroy.
12610 *
12611 * This function destroys a queue, as detailed in @eq by sending an mailbox
12612 * command, specific to the type of queue, to the HBA.
12613 *
12614 * The @eq struct is used to get the queue ID of the queue to destroy.
12615 *
12616 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040012617 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040012618 **/
12619uint32_t
12620lpfc_eq_destroy(struct lpfc_hba *phba, struct lpfc_queue *eq)
12621{
12622 LPFC_MBOXQ_t *mbox;
12623 int rc, length, status = 0;
12624 uint32_t shdr_status, shdr_add_status;
12625 union lpfc_sli4_cfg_shdr *shdr;
12626
James Smart2e90f4b2011-12-13 13:22:37 -050012627 /* sanity check on queue memory */
James Smart4f774512009-05-22 14:52:35 -040012628 if (!eq)
12629 return -ENODEV;
12630 mbox = mempool_alloc(eq->phba->mbox_mem_pool, GFP_KERNEL);
12631 if (!mbox)
12632 return -ENOMEM;
12633 length = (sizeof(struct lpfc_mbx_eq_destroy) -
12634 sizeof(struct lpfc_sli4_cfg_mhdr));
12635 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
12636 LPFC_MBOX_OPCODE_EQ_DESTROY,
12637 length, LPFC_SLI4_MBX_EMBED);
12638 bf_set(lpfc_mbx_eq_destroy_q_id, &mbox->u.mqe.un.eq_destroy.u.request,
12639 eq->queue_id);
12640 mbox->vport = eq->phba->pport;
12641 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
12642
12643 rc = lpfc_sli_issue_mbox(eq->phba, mbox, MBX_POLL);
12644 /* The IOCTL status is embedded in the mailbox subheader. */
12645 shdr = (union lpfc_sli4_cfg_shdr *)
12646 &mbox->u.mqe.un.eq_destroy.header.cfg_shdr;
12647 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12648 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12649 if (shdr_status || shdr_add_status || rc) {
12650 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12651 "2505 EQ_DESTROY mailbox failed with "
12652 "status x%x add_status x%x, mbx status x%x\n",
12653 shdr_status, shdr_add_status, rc);
12654 status = -ENXIO;
12655 }
12656
12657 /* Remove eq from any list */
12658 list_del_init(&eq->list);
James Smart8fa38512009-07-19 10:01:03 -040012659 mempool_free(mbox, eq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040012660 return status;
12661}
12662
12663/**
12664 * lpfc_cq_destroy - Destroy a Completion Queue on the HBA
12665 * @cq: The queue structure associated with the queue to destroy.
12666 *
12667 * This function destroys a queue, as detailed in @cq by sending an mailbox
12668 * command, specific to the type of queue, to the HBA.
12669 *
12670 * The @cq struct is used to get the queue ID of the queue to destroy.
12671 *
12672 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040012673 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040012674 **/
12675uint32_t
12676lpfc_cq_destroy(struct lpfc_hba *phba, struct lpfc_queue *cq)
12677{
12678 LPFC_MBOXQ_t *mbox;
12679 int rc, length, status = 0;
12680 uint32_t shdr_status, shdr_add_status;
12681 union lpfc_sli4_cfg_shdr *shdr;
12682
James Smart2e90f4b2011-12-13 13:22:37 -050012683 /* sanity check on queue memory */
James Smart4f774512009-05-22 14:52:35 -040012684 if (!cq)
12685 return -ENODEV;
12686 mbox = mempool_alloc(cq->phba->mbox_mem_pool, GFP_KERNEL);
12687 if (!mbox)
12688 return -ENOMEM;
12689 length = (sizeof(struct lpfc_mbx_cq_destroy) -
12690 sizeof(struct lpfc_sli4_cfg_mhdr));
12691 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
12692 LPFC_MBOX_OPCODE_CQ_DESTROY,
12693 length, LPFC_SLI4_MBX_EMBED);
12694 bf_set(lpfc_mbx_cq_destroy_q_id, &mbox->u.mqe.un.cq_destroy.u.request,
12695 cq->queue_id);
12696 mbox->vport = cq->phba->pport;
12697 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
12698 rc = lpfc_sli_issue_mbox(cq->phba, mbox, MBX_POLL);
12699 /* The IOCTL status is embedded in the mailbox subheader. */
12700 shdr = (union lpfc_sli4_cfg_shdr *)
12701 &mbox->u.mqe.un.wq_create.header.cfg_shdr;
12702 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12703 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12704 if (shdr_status || shdr_add_status || rc) {
12705 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12706 "2506 CQ_DESTROY mailbox failed with "
12707 "status x%x add_status x%x, mbx status x%x\n",
12708 shdr_status, shdr_add_status, rc);
12709 status = -ENXIO;
12710 }
12711 /* Remove cq from any list */
12712 list_del_init(&cq->list);
James Smart8fa38512009-07-19 10:01:03 -040012713 mempool_free(mbox, cq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040012714 return status;
12715}
12716
12717/**
James Smart04c68492009-05-22 14:52:52 -040012718 * lpfc_mq_destroy - Destroy a Mailbox Queue on the HBA
12719 * @qm: The queue structure associated with the queue to destroy.
12720 *
12721 * This function destroys a queue, as detailed in @mq by sending an mailbox
12722 * command, specific to the type of queue, to the HBA.
12723 *
12724 * The @mq struct is used to get the queue ID of the queue to destroy.
12725 *
12726 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040012727 * command fails this function will return -ENXIO.
James Smart04c68492009-05-22 14:52:52 -040012728 **/
12729uint32_t
12730lpfc_mq_destroy(struct lpfc_hba *phba, struct lpfc_queue *mq)
12731{
12732 LPFC_MBOXQ_t *mbox;
12733 int rc, length, status = 0;
12734 uint32_t shdr_status, shdr_add_status;
12735 union lpfc_sli4_cfg_shdr *shdr;
12736
James Smart2e90f4b2011-12-13 13:22:37 -050012737 /* sanity check on queue memory */
James Smart04c68492009-05-22 14:52:52 -040012738 if (!mq)
12739 return -ENODEV;
12740 mbox = mempool_alloc(mq->phba->mbox_mem_pool, GFP_KERNEL);
12741 if (!mbox)
12742 return -ENOMEM;
12743 length = (sizeof(struct lpfc_mbx_mq_destroy) -
12744 sizeof(struct lpfc_sli4_cfg_mhdr));
12745 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
12746 LPFC_MBOX_OPCODE_MQ_DESTROY,
12747 length, LPFC_SLI4_MBX_EMBED);
12748 bf_set(lpfc_mbx_mq_destroy_q_id, &mbox->u.mqe.un.mq_destroy.u.request,
12749 mq->queue_id);
12750 mbox->vport = mq->phba->pport;
12751 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
12752 rc = lpfc_sli_issue_mbox(mq->phba, mbox, MBX_POLL);
12753 /* The IOCTL status is embedded in the mailbox subheader. */
12754 shdr = (union lpfc_sli4_cfg_shdr *)
12755 &mbox->u.mqe.un.mq_destroy.header.cfg_shdr;
12756 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12757 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12758 if (shdr_status || shdr_add_status || rc) {
12759 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12760 "2507 MQ_DESTROY mailbox failed with "
12761 "status x%x add_status x%x, mbx status x%x\n",
12762 shdr_status, shdr_add_status, rc);
12763 status = -ENXIO;
12764 }
12765 /* Remove mq from any list */
12766 list_del_init(&mq->list);
James Smart8fa38512009-07-19 10:01:03 -040012767 mempool_free(mbox, mq->phba->mbox_mem_pool);
James Smart04c68492009-05-22 14:52:52 -040012768 return status;
12769}
12770
12771/**
James Smart4f774512009-05-22 14:52:35 -040012772 * lpfc_wq_destroy - Destroy a Work Queue on the HBA
12773 * @wq: The queue structure associated with the queue to destroy.
12774 *
12775 * This function destroys a queue, as detailed in @wq by sending an mailbox
12776 * command, specific to the type of queue, to the HBA.
12777 *
12778 * The @wq struct is used to get the queue ID of the queue to destroy.
12779 *
12780 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040012781 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040012782 **/
12783uint32_t
12784lpfc_wq_destroy(struct lpfc_hba *phba, struct lpfc_queue *wq)
12785{
12786 LPFC_MBOXQ_t *mbox;
12787 int rc, length, status = 0;
12788 uint32_t shdr_status, shdr_add_status;
12789 union lpfc_sli4_cfg_shdr *shdr;
12790
James Smart2e90f4b2011-12-13 13:22:37 -050012791 /* sanity check on queue memory */
James Smart4f774512009-05-22 14:52:35 -040012792 if (!wq)
12793 return -ENODEV;
12794 mbox = mempool_alloc(wq->phba->mbox_mem_pool, GFP_KERNEL);
12795 if (!mbox)
12796 return -ENOMEM;
12797 length = (sizeof(struct lpfc_mbx_wq_destroy) -
12798 sizeof(struct lpfc_sli4_cfg_mhdr));
12799 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
12800 LPFC_MBOX_OPCODE_FCOE_WQ_DESTROY,
12801 length, LPFC_SLI4_MBX_EMBED);
12802 bf_set(lpfc_mbx_wq_destroy_q_id, &mbox->u.mqe.un.wq_destroy.u.request,
12803 wq->queue_id);
12804 mbox->vport = wq->phba->pport;
12805 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
12806 rc = lpfc_sli_issue_mbox(wq->phba, mbox, MBX_POLL);
12807 shdr = (union lpfc_sli4_cfg_shdr *)
12808 &mbox->u.mqe.un.wq_destroy.header.cfg_shdr;
12809 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12810 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12811 if (shdr_status || shdr_add_status || rc) {
12812 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12813 "2508 WQ_DESTROY mailbox failed with "
12814 "status x%x add_status x%x, mbx status x%x\n",
12815 shdr_status, shdr_add_status, rc);
12816 status = -ENXIO;
12817 }
12818 /* Remove wq from any list */
12819 list_del_init(&wq->list);
James Smart8fa38512009-07-19 10:01:03 -040012820 mempool_free(mbox, wq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040012821 return status;
12822}
12823
12824/**
12825 * lpfc_rq_destroy - Destroy a Receive Queue on the HBA
12826 * @rq: The queue structure associated with the queue to destroy.
12827 *
12828 * This function destroys a queue, as detailed in @rq by sending an mailbox
12829 * command, specific to the type of queue, to the HBA.
12830 *
12831 * The @rq struct is used to get the queue ID of the queue to destroy.
12832 *
12833 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040012834 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040012835 **/
12836uint32_t
12837lpfc_rq_destroy(struct lpfc_hba *phba, struct lpfc_queue *hrq,
12838 struct lpfc_queue *drq)
12839{
12840 LPFC_MBOXQ_t *mbox;
12841 int rc, length, status = 0;
12842 uint32_t shdr_status, shdr_add_status;
12843 union lpfc_sli4_cfg_shdr *shdr;
12844
James Smart2e90f4b2011-12-13 13:22:37 -050012845 /* sanity check on queue memory */
James Smart4f774512009-05-22 14:52:35 -040012846 if (!hrq || !drq)
12847 return -ENODEV;
12848 mbox = mempool_alloc(hrq->phba->mbox_mem_pool, GFP_KERNEL);
12849 if (!mbox)
12850 return -ENOMEM;
12851 length = (sizeof(struct lpfc_mbx_rq_destroy) -
James Smartfedd3b72011-02-16 12:39:24 -050012852 sizeof(struct lpfc_sli4_cfg_mhdr));
James Smart4f774512009-05-22 14:52:35 -040012853 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
12854 LPFC_MBOX_OPCODE_FCOE_RQ_DESTROY,
12855 length, LPFC_SLI4_MBX_EMBED);
12856 bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
12857 hrq->queue_id);
12858 mbox->vport = hrq->phba->pport;
12859 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
12860 rc = lpfc_sli_issue_mbox(hrq->phba, mbox, MBX_POLL);
12861 /* The IOCTL status is embedded in the mailbox subheader. */
12862 shdr = (union lpfc_sli4_cfg_shdr *)
12863 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
12864 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12865 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12866 if (shdr_status || shdr_add_status || rc) {
12867 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12868 "2509 RQ_DESTROY mailbox failed with "
12869 "status x%x add_status x%x, mbx status x%x\n",
12870 shdr_status, shdr_add_status, rc);
12871 if (rc != MBX_TIMEOUT)
12872 mempool_free(mbox, hrq->phba->mbox_mem_pool);
12873 return -ENXIO;
12874 }
12875 bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
12876 drq->queue_id);
12877 rc = lpfc_sli_issue_mbox(drq->phba, mbox, MBX_POLL);
12878 shdr = (union lpfc_sli4_cfg_shdr *)
12879 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
12880 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12881 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12882 if (shdr_status || shdr_add_status || rc) {
12883 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12884 "2510 RQ_DESTROY mailbox failed with "
12885 "status x%x add_status x%x, mbx status x%x\n",
12886 shdr_status, shdr_add_status, rc);
12887 status = -ENXIO;
12888 }
12889 list_del_init(&hrq->list);
12890 list_del_init(&drq->list);
James Smart8fa38512009-07-19 10:01:03 -040012891 mempool_free(mbox, hrq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040012892 return status;
12893}
12894
12895/**
12896 * lpfc_sli4_post_sgl - Post scatter gather list for an XRI to HBA
12897 * @phba: The virtual port for which this call being executed.
12898 * @pdma_phys_addr0: Physical address of the 1st SGL page.
12899 * @pdma_phys_addr1: Physical address of the 2nd SGL page.
12900 * @xritag: the xritag that ties this io to the SGL pages.
12901 *
12902 * This routine will post the sgl pages for the IO that has the xritag
12903 * that is in the iocbq structure. The xritag is assigned during iocbq
12904 * creation and persists for as long as the driver is loaded.
12905 * if the caller has fewer than 256 scatter gather segments to map then
12906 * pdma_phys_addr1 should be 0.
12907 * If the caller needs to map more than 256 scatter gather segment then
12908 * pdma_phys_addr1 should be a valid physical address.
12909 * physical address for SGLs must be 64 byte aligned.
12910 * If you are going to map 2 SGL's then the first one must have 256 entries
12911 * the second sgl can have between 1 and 256 entries.
12912 *
12913 * Return codes:
12914 * 0 - Success
12915 * -ENXIO, -ENOMEM - Failure
12916 **/
12917int
12918lpfc_sli4_post_sgl(struct lpfc_hba *phba,
12919 dma_addr_t pdma_phys_addr0,
12920 dma_addr_t pdma_phys_addr1,
12921 uint16_t xritag)
12922{
12923 struct lpfc_mbx_post_sgl_pages *post_sgl_pages;
12924 LPFC_MBOXQ_t *mbox;
12925 int rc;
12926 uint32_t shdr_status, shdr_add_status;
James Smart6d368e52011-05-24 11:44:12 -040012927 uint32_t mbox_tmo;
James Smart4f774512009-05-22 14:52:35 -040012928 union lpfc_sli4_cfg_shdr *shdr;
12929
12930 if (xritag == NO_XRI) {
12931 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12932 "0364 Invalid param:\n");
12933 return -EINVAL;
12934 }
12935
12936 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12937 if (!mbox)
12938 return -ENOMEM;
12939
12940 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
12941 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES,
12942 sizeof(struct lpfc_mbx_post_sgl_pages) -
James Smartfedd3b72011-02-16 12:39:24 -050012943 sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
James Smart4f774512009-05-22 14:52:35 -040012944
12945 post_sgl_pages = (struct lpfc_mbx_post_sgl_pages *)
12946 &mbox->u.mqe.un.post_sgl_pages;
12947 bf_set(lpfc_post_sgl_pages_xri, post_sgl_pages, xritag);
12948 bf_set(lpfc_post_sgl_pages_xricnt, post_sgl_pages, 1);
12949
12950 post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_lo =
12951 cpu_to_le32(putPaddrLow(pdma_phys_addr0));
12952 post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_hi =
12953 cpu_to_le32(putPaddrHigh(pdma_phys_addr0));
12954
12955 post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_lo =
12956 cpu_to_le32(putPaddrLow(pdma_phys_addr1));
12957 post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_hi =
12958 cpu_to_le32(putPaddrHigh(pdma_phys_addr1));
12959 if (!phba->sli4_hba.intr_enable)
12960 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
James Smart6d368e52011-05-24 11:44:12 -040012961 else {
James Smarta183a152011-10-10 21:32:43 -040012962 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart6d368e52011-05-24 11:44:12 -040012963 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
12964 }
James Smart4f774512009-05-22 14:52:35 -040012965 /* The IOCTL status is embedded in the mailbox subheader. */
12966 shdr = (union lpfc_sli4_cfg_shdr *) &post_sgl_pages->header.cfg_shdr;
12967 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12968 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12969 if (rc != MBX_TIMEOUT)
12970 mempool_free(mbox, phba->mbox_mem_pool);
12971 if (shdr_status || shdr_add_status || rc) {
12972 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12973 "2511 POST_SGL mailbox failed with "
12974 "status x%x add_status x%x, mbx status x%x\n",
12975 shdr_status, shdr_add_status, rc);
12976 rc = -ENXIO;
12977 }
12978 return 0;
12979}
James Smart4f774512009-05-22 14:52:35 -040012980
12981/**
James Smart88a2cfb2011-07-22 18:36:33 -040012982 * lpfc_sli4_alloc_xri - Get an available rpi in the device's range
James Smart6d368e52011-05-24 11:44:12 -040012983 * @phba: pointer to lpfc hba data structure.
12984 *
12985 * This routine is invoked to post rpi header templates to the
James Smart88a2cfb2011-07-22 18:36:33 -040012986 * HBA consistent with the SLI-4 interface spec. This routine
12987 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
12988 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
James Smart6d368e52011-05-24 11:44:12 -040012989 *
James Smart88a2cfb2011-07-22 18:36:33 -040012990 * Returns
12991 * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
12992 * LPFC_RPI_ALLOC_ERROR if no rpis are available.
12993 **/
James Smart6d368e52011-05-24 11:44:12 -040012994uint16_t
12995lpfc_sli4_alloc_xri(struct lpfc_hba *phba)
12996{
12997 unsigned long xri;
12998
12999 /*
13000 * Fetch the next logical xri. Because this index is logical,
13001 * the driver starts at 0 each time.
13002 */
13003 spin_lock_irq(&phba->hbalock);
13004 xri = find_next_zero_bit(phba->sli4_hba.xri_bmask,
13005 phba->sli4_hba.max_cfg_param.max_xri, 0);
13006 if (xri >= phba->sli4_hba.max_cfg_param.max_xri) {
13007 spin_unlock_irq(&phba->hbalock);
13008 return NO_XRI;
13009 } else {
13010 set_bit(xri, phba->sli4_hba.xri_bmask);
13011 phba->sli4_hba.max_cfg_param.xri_used++;
13012 phba->sli4_hba.xri_count++;
13013 }
13014
13015 spin_unlock_irq(&phba->hbalock);
13016 return xri;
13017}
13018
13019/**
13020 * lpfc_sli4_free_xri - Release an xri for reuse.
13021 * @phba: pointer to lpfc hba data structure.
13022 *
13023 * This routine is invoked to release an xri to the pool of
13024 * available rpis maintained by the driver.
13025 **/
13026void
13027__lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
13028{
13029 if (test_and_clear_bit(xri, phba->sli4_hba.xri_bmask)) {
13030 phba->sli4_hba.xri_count--;
13031 phba->sli4_hba.max_cfg_param.xri_used--;
13032 }
13033}
13034
13035/**
13036 * lpfc_sli4_free_xri - Release an xri for reuse.
13037 * @phba: pointer to lpfc hba data structure.
13038 *
13039 * This routine is invoked to release an xri to the pool of
13040 * available rpis maintained by the driver.
13041 **/
13042void
13043lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
13044{
13045 spin_lock_irq(&phba->hbalock);
13046 __lpfc_sli4_free_xri(phba, xri);
13047 spin_unlock_irq(&phba->hbalock);
13048}
13049
13050/**
James Smart4f774512009-05-22 14:52:35 -040013051 * lpfc_sli4_next_xritag - Get an xritag for the io
13052 * @phba: Pointer to HBA context object.
13053 *
13054 * This function gets an xritag for the iocb. If there is no unused xritag
13055 * it will return 0xffff.
13056 * The function returns the allocated xritag if successful, else returns zero.
13057 * Zero is not a valid xritag.
13058 * The caller is not required to hold any lock.
13059 **/
13060uint16_t
13061lpfc_sli4_next_xritag(struct lpfc_hba *phba)
13062{
James Smart6d368e52011-05-24 11:44:12 -040013063 uint16_t xri_index;
James Smart4f774512009-05-22 14:52:35 -040013064
James Smart6d368e52011-05-24 11:44:12 -040013065 xri_index = lpfc_sli4_alloc_xri(phba);
13066 if (xri_index != NO_XRI)
13067 return xri_index;
13068
13069 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart4f774512009-05-22 14:52:35 -040013070 "2004 Failed to allocate XRI.last XRITAG is %d"
13071 " Max XRI is %d, Used XRI is %d\n",
James Smart6d368e52011-05-24 11:44:12 -040013072 xri_index,
James Smart4f774512009-05-22 14:52:35 -040013073 phba->sli4_hba.max_cfg_param.max_xri,
13074 phba->sli4_hba.max_cfg_param.xri_used);
James Smart6d368e52011-05-24 11:44:12 -040013075 return NO_XRI;
James Smart4f774512009-05-22 14:52:35 -040013076}
13077
13078/**
James Smart6d368e52011-05-24 11:44:12 -040013079 * lpfc_sli4_post_els_sgl_list - post a block of ELS sgls to the port.
James Smart4f774512009-05-22 14:52:35 -040013080 * @phba: pointer to lpfc hba data structure.
13081 *
13082 * This routine is invoked to post a block of driver's sgl pages to the
13083 * HBA using non-embedded mailbox command. No Lock is held. This routine
13084 * is only called when the driver is loading and after all IO has been
13085 * stopped.
13086 **/
13087int
James Smart6d368e52011-05-24 11:44:12 -040013088lpfc_sli4_post_els_sgl_list(struct lpfc_hba *phba)
James Smart4f774512009-05-22 14:52:35 -040013089{
13090 struct lpfc_sglq *sglq_entry;
13091 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
13092 struct sgl_page_pairs *sgl_pg_pairs;
13093 void *viraddr;
13094 LPFC_MBOXQ_t *mbox;
13095 uint32_t reqlen, alloclen, pg_pairs;
13096 uint32_t mbox_tmo;
James Smart6d368e52011-05-24 11:44:12 -040013097 uint16_t xritag_start = 0, lxri = 0;
James Smart4f774512009-05-22 14:52:35 -040013098 int els_xri_cnt, rc = 0;
13099 uint32_t shdr_status, shdr_add_status;
13100 union lpfc_sli4_cfg_shdr *shdr;
13101
13102 /* The number of sgls to be posted */
13103 els_xri_cnt = lpfc_sli4_get_els_iocb_cnt(phba);
13104
13105 reqlen = els_xri_cnt * sizeof(struct sgl_page_pairs) +
13106 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
James Smart49198b32010-04-06 15:04:33 -040013107 if (reqlen > SLI4_PAGE_SIZE) {
James Smart4f774512009-05-22 14:52:35 -040013108 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
13109 "2559 Block sgl registration required DMA "
13110 "size (%d) great than a page\n", reqlen);
13111 return -ENOMEM;
13112 }
13113 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
James Smart6d368e52011-05-24 11:44:12 -040013114 if (!mbox)
James Smart4f774512009-05-22 14:52:35 -040013115 return -ENOMEM;
James Smart4f774512009-05-22 14:52:35 -040013116
13117 /* Allocate DMA memory and set up the non-embedded mailbox command */
13118 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
13119 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
13120 LPFC_SLI4_MBX_NEMBED);
13121
13122 if (alloclen < reqlen) {
13123 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13124 "0285 Allocated DMA memory size (%d) is "
13125 "less than the requested DMA memory "
13126 "size (%d)\n", alloclen, reqlen);
13127 lpfc_sli4_mbox_cmd_free(phba, mbox);
13128 return -ENOMEM;
13129 }
James Smart4f774512009-05-22 14:52:35 -040013130 /* Set up the SGL pages in the non-embedded DMA pages */
James Smart6d368e52011-05-24 11:44:12 -040013131 viraddr = mbox->sge_array->addr[0];
James Smart4f774512009-05-22 14:52:35 -040013132 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
13133 sgl_pg_pairs = &sgl->sgl_pg_pairs;
13134
13135 for (pg_pairs = 0; pg_pairs < els_xri_cnt; pg_pairs++) {
13136 sglq_entry = phba->sli4_hba.lpfc_els_sgl_array[pg_pairs];
James Smart6d368e52011-05-24 11:44:12 -040013137
13138 /*
13139 * Assign the sglq a physical xri only if the driver has not
13140 * initialized those resources. A port reset only needs
13141 * the sglq's posted.
13142 */
13143 if (bf_get(lpfc_xri_rsrc_rdy, &phba->sli4_hba.sli4_flags) !=
13144 LPFC_XRI_RSRC_RDY) {
13145 lxri = lpfc_sli4_next_xritag(phba);
13146 if (lxri == NO_XRI) {
13147 lpfc_sli4_mbox_cmd_free(phba, mbox);
13148 return -ENOMEM;
13149 }
13150 sglq_entry->sli4_lxritag = lxri;
13151 sglq_entry->sli4_xritag = phba->sli4_hba.xri_ids[lxri];
13152 }
13153
James Smart4f774512009-05-22 14:52:35 -040013154 /* Set up the sge entry */
13155 sgl_pg_pairs->sgl_pg0_addr_lo =
13156 cpu_to_le32(putPaddrLow(sglq_entry->phys));
13157 sgl_pg_pairs->sgl_pg0_addr_hi =
13158 cpu_to_le32(putPaddrHigh(sglq_entry->phys));
13159 sgl_pg_pairs->sgl_pg1_addr_lo =
13160 cpu_to_le32(putPaddrLow(0));
13161 sgl_pg_pairs->sgl_pg1_addr_hi =
13162 cpu_to_le32(putPaddrHigh(0));
James Smart6d368e52011-05-24 11:44:12 -040013163
James Smart4f774512009-05-22 14:52:35 -040013164 /* Keep the first xritag on the list */
13165 if (pg_pairs == 0)
13166 xritag_start = sglq_entry->sli4_xritag;
13167 sgl_pg_pairs++;
13168 }
James Smart6d368e52011-05-24 11:44:12 -040013169
13170 /* Complete initialization and perform endian conversion. */
James Smart4f774512009-05-22 14:52:35 -040013171 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
James Smart6a9c52c2009-10-02 15:16:51 -040013172 bf_set(lpfc_post_sgl_pages_xricnt, sgl, els_xri_cnt);
James Smart4f774512009-05-22 14:52:35 -040013173 sgl->word0 = cpu_to_le32(sgl->word0);
James Smart4f774512009-05-22 14:52:35 -040013174 if (!phba->sli4_hba.intr_enable)
13175 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
13176 else {
James Smarta183a152011-10-10 21:32:43 -040013177 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart4f774512009-05-22 14:52:35 -040013178 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
13179 }
13180 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
13181 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13182 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13183 if (rc != MBX_TIMEOUT)
13184 lpfc_sli4_mbox_cmd_free(phba, mbox);
13185 if (shdr_status || shdr_add_status || rc) {
13186 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13187 "2513 POST_SGL_BLOCK mailbox command failed "
13188 "status x%x add_status x%x mbx status x%x\n",
13189 shdr_status, shdr_add_status, rc);
13190 rc = -ENXIO;
13191 }
James Smart6d368e52011-05-24 11:44:12 -040013192
13193 if (rc == 0)
13194 bf_set(lpfc_xri_rsrc_rdy, &phba->sli4_hba.sli4_flags,
13195 LPFC_XRI_RSRC_RDY);
13196 return rc;
13197}
13198
13199/**
13200 * lpfc_sli4_post_els_sgl_list_ext - post a block of ELS sgls to the port.
13201 * @phba: pointer to lpfc hba data structure.
13202 *
13203 * This routine is invoked to post a block of driver's sgl pages to the
13204 * HBA using non-embedded mailbox command. No Lock is held. This routine
13205 * is only called when the driver is loading and after all IO has been
13206 * stopped.
13207 **/
13208int
13209lpfc_sli4_post_els_sgl_list_ext(struct lpfc_hba *phba)
13210{
13211 struct lpfc_sglq *sglq_entry;
13212 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
13213 struct sgl_page_pairs *sgl_pg_pairs;
13214 void *viraddr;
13215 LPFC_MBOXQ_t *mbox;
13216 uint32_t reqlen, alloclen, index;
13217 uint32_t mbox_tmo;
13218 uint16_t rsrc_start, rsrc_size, els_xri_cnt;
13219 uint16_t xritag_start = 0, lxri = 0;
13220 struct lpfc_rsrc_blks *rsrc_blk;
13221 int cnt, ttl_cnt, rc = 0;
13222 int loop_cnt;
13223 uint32_t shdr_status, shdr_add_status;
13224 union lpfc_sli4_cfg_shdr *shdr;
13225
13226 /* The number of sgls to be posted */
13227 els_xri_cnt = lpfc_sli4_get_els_iocb_cnt(phba);
13228
13229 reqlen = els_xri_cnt * sizeof(struct sgl_page_pairs) +
13230 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
13231 if (reqlen > SLI4_PAGE_SIZE) {
13232 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
13233 "2989 Block sgl registration required DMA "
13234 "size (%d) great than a page\n", reqlen);
13235 return -ENOMEM;
13236 }
13237
13238 cnt = 0;
13239 ttl_cnt = 0;
13240 list_for_each_entry(rsrc_blk, &phba->sli4_hba.lpfc_xri_blk_list,
13241 list) {
13242 rsrc_start = rsrc_blk->rsrc_start;
13243 rsrc_size = rsrc_blk->rsrc_size;
13244
13245 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
13246 "3014 Working ELS Extent start %d, cnt %d\n",
13247 rsrc_start, rsrc_size);
13248
13249 loop_cnt = min(els_xri_cnt, rsrc_size);
13250 if (ttl_cnt + loop_cnt >= els_xri_cnt) {
13251 loop_cnt = els_xri_cnt - ttl_cnt;
13252 ttl_cnt = els_xri_cnt;
13253 }
13254
13255 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
13256 if (!mbox)
13257 return -ENOMEM;
13258 /*
13259 * Allocate DMA memory and set up the non-embedded mailbox
13260 * command.
13261 */
13262 alloclen = lpfc_sli4_config(phba, mbox,
13263 LPFC_MBOX_SUBSYSTEM_FCOE,
13264 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES,
13265 reqlen, LPFC_SLI4_MBX_NEMBED);
13266 if (alloclen < reqlen) {
13267 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13268 "2987 Allocated DMA memory size (%d) "
13269 "is less than the requested DMA memory "
13270 "size (%d)\n", alloclen, reqlen);
13271 lpfc_sli4_mbox_cmd_free(phba, mbox);
13272 return -ENOMEM;
13273 }
13274
13275 /* Set up the SGL pages in the non-embedded DMA pages */
13276 viraddr = mbox->sge_array->addr[0];
13277 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
13278 sgl_pg_pairs = &sgl->sgl_pg_pairs;
13279
13280 /*
13281 * The starting resource may not begin at zero. Control
13282 * the loop variants via the block resource parameters,
13283 * but handle the sge pointers with a zero-based index
13284 * that doesn't get reset per loop pass.
13285 */
13286 for (index = rsrc_start;
13287 index < rsrc_start + loop_cnt;
13288 index++) {
13289 sglq_entry = phba->sli4_hba.lpfc_els_sgl_array[cnt];
13290
13291 /*
13292 * Assign the sglq a physical xri only if the driver
13293 * has not initialized those resources. A port reset
13294 * only needs the sglq's posted.
13295 */
13296 if (bf_get(lpfc_xri_rsrc_rdy,
13297 &phba->sli4_hba.sli4_flags) !=
13298 LPFC_XRI_RSRC_RDY) {
13299 lxri = lpfc_sli4_next_xritag(phba);
13300 if (lxri == NO_XRI) {
13301 lpfc_sli4_mbox_cmd_free(phba, mbox);
13302 rc = -ENOMEM;
13303 goto err_exit;
13304 }
13305 sglq_entry->sli4_lxritag = lxri;
13306 sglq_entry->sli4_xritag =
13307 phba->sli4_hba.xri_ids[lxri];
13308 }
13309
13310 /* Set up the sge entry */
13311 sgl_pg_pairs->sgl_pg0_addr_lo =
13312 cpu_to_le32(putPaddrLow(sglq_entry->phys));
13313 sgl_pg_pairs->sgl_pg0_addr_hi =
13314 cpu_to_le32(putPaddrHigh(sglq_entry->phys));
13315 sgl_pg_pairs->sgl_pg1_addr_lo =
13316 cpu_to_le32(putPaddrLow(0));
13317 sgl_pg_pairs->sgl_pg1_addr_hi =
13318 cpu_to_le32(putPaddrHigh(0));
13319
13320 /* Track the starting physical XRI for the mailbox. */
13321 if (index == rsrc_start)
13322 xritag_start = sglq_entry->sli4_xritag;
13323 sgl_pg_pairs++;
13324 cnt++;
13325 }
13326
13327 /* Complete initialization and perform endian conversion. */
13328 rsrc_blk->rsrc_used += loop_cnt;
13329 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
13330 bf_set(lpfc_post_sgl_pages_xricnt, sgl, loop_cnt);
13331 sgl->word0 = cpu_to_le32(sgl->word0);
13332
13333 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
13334 "3015 Post ELS Extent SGL, start %d, "
13335 "cnt %d, used %d\n",
13336 xritag_start, loop_cnt, rsrc_blk->rsrc_used);
13337 if (!phba->sli4_hba.intr_enable)
13338 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
13339 else {
James Smarta183a152011-10-10 21:32:43 -040013340 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart6d368e52011-05-24 11:44:12 -040013341 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
13342 }
13343 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
13344 shdr_status = bf_get(lpfc_mbox_hdr_status,
13345 &shdr->response);
13346 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
13347 &shdr->response);
13348 if (rc != MBX_TIMEOUT)
13349 lpfc_sli4_mbox_cmd_free(phba, mbox);
13350 if (shdr_status || shdr_add_status || rc) {
13351 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13352 "2988 POST_SGL_BLOCK mailbox "
13353 "command failed status x%x "
13354 "add_status x%x mbx status x%x\n",
13355 shdr_status, shdr_add_status, rc);
13356 rc = -ENXIO;
13357 goto err_exit;
13358 }
13359 if (ttl_cnt >= els_xri_cnt)
13360 break;
13361 }
13362
13363 err_exit:
13364 if (rc == 0)
13365 bf_set(lpfc_xri_rsrc_rdy, &phba->sli4_hba.sli4_flags,
13366 LPFC_XRI_RSRC_RDY);
James Smart4f774512009-05-22 14:52:35 -040013367 return rc;
13368}
13369
13370/**
13371 * lpfc_sli4_post_scsi_sgl_block - post a block of scsi sgl list to firmware
13372 * @phba: pointer to lpfc hba data structure.
13373 * @sblist: pointer to scsi buffer list.
13374 * @count: number of scsi buffers on the list.
13375 *
13376 * This routine is invoked to post a block of @count scsi sgl pages from a
13377 * SCSI buffer list @sblist to the HBA using non-embedded mailbox command.
13378 * No Lock is held.
13379 *
13380 **/
13381int
13382lpfc_sli4_post_scsi_sgl_block(struct lpfc_hba *phba, struct list_head *sblist,
13383 int cnt)
13384{
13385 struct lpfc_scsi_buf *psb;
13386 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
13387 struct sgl_page_pairs *sgl_pg_pairs;
13388 void *viraddr;
13389 LPFC_MBOXQ_t *mbox;
13390 uint32_t reqlen, alloclen, pg_pairs;
13391 uint32_t mbox_tmo;
13392 uint16_t xritag_start = 0;
13393 int rc = 0;
13394 uint32_t shdr_status, shdr_add_status;
13395 dma_addr_t pdma_phys_bpl1;
13396 union lpfc_sli4_cfg_shdr *shdr;
13397
13398 /* Calculate the requested length of the dma memory */
13399 reqlen = cnt * sizeof(struct sgl_page_pairs) +
13400 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
James Smart49198b32010-04-06 15:04:33 -040013401 if (reqlen > SLI4_PAGE_SIZE) {
James Smart4f774512009-05-22 14:52:35 -040013402 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
13403 "0217 Block sgl registration required DMA "
13404 "size (%d) great than a page\n", reqlen);
13405 return -ENOMEM;
13406 }
13407 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
13408 if (!mbox) {
13409 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13410 "0283 Failed to allocate mbox cmd memory\n");
13411 return -ENOMEM;
13412 }
13413
13414 /* Allocate DMA memory and set up the non-embedded mailbox command */
13415 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
13416 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
13417 LPFC_SLI4_MBX_NEMBED);
13418
13419 if (alloclen < reqlen) {
13420 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13421 "2561 Allocated DMA memory size (%d) is "
13422 "less than the requested DMA memory "
13423 "size (%d)\n", alloclen, reqlen);
13424 lpfc_sli4_mbox_cmd_free(phba, mbox);
13425 return -ENOMEM;
13426 }
James Smart6d368e52011-05-24 11:44:12 -040013427
James Smart4f774512009-05-22 14:52:35 -040013428 /* Get the first SGE entry from the non-embedded DMA memory */
James Smart4f774512009-05-22 14:52:35 -040013429 viraddr = mbox->sge_array->addr[0];
13430
13431 /* Set up the SGL pages in the non-embedded DMA pages */
13432 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
13433 sgl_pg_pairs = &sgl->sgl_pg_pairs;
13434
13435 pg_pairs = 0;
13436 list_for_each_entry(psb, sblist, list) {
13437 /* Set up the sge entry */
13438 sgl_pg_pairs->sgl_pg0_addr_lo =
13439 cpu_to_le32(putPaddrLow(psb->dma_phys_bpl));
13440 sgl_pg_pairs->sgl_pg0_addr_hi =
13441 cpu_to_le32(putPaddrHigh(psb->dma_phys_bpl));
13442 if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
13443 pdma_phys_bpl1 = psb->dma_phys_bpl + SGL_PAGE_SIZE;
13444 else
13445 pdma_phys_bpl1 = 0;
13446 sgl_pg_pairs->sgl_pg1_addr_lo =
13447 cpu_to_le32(putPaddrLow(pdma_phys_bpl1));
13448 sgl_pg_pairs->sgl_pg1_addr_hi =
13449 cpu_to_le32(putPaddrHigh(pdma_phys_bpl1));
13450 /* Keep the first xritag on the list */
13451 if (pg_pairs == 0)
13452 xritag_start = psb->cur_iocbq.sli4_xritag;
13453 sgl_pg_pairs++;
13454 pg_pairs++;
13455 }
13456 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
13457 bf_set(lpfc_post_sgl_pages_xricnt, sgl, pg_pairs);
13458 /* Perform endian conversion if necessary */
13459 sgl->word0 = cpu_to_le32(sgl->word0);
13460
13461 if (!phba->sli4_hba.intr_enable)
13462 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
13463 else {
James Smarta183a152011-10-10 21:32:43 -040013464 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart4f774512009-05-22 14:52:35 -040013465 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
13466 }
13467 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
13468 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13469 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13470 if (rc != MBX_TIMEOUT)
13471 lpfc_sli4_mbox_cmd_free(phba, mbox);
13472 if (shdr_status || shdr_add_status || rc) {
13473 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13474 "2564 POST_SGL_BLOCK mailbox command failed "
13475 "status x%x add_status x%x mbx status x%x\n",
13476 shdr_status, shdr_add_status, rc);
13477 rc = -ENXIO;
13478 }
13479 return rc;
13480}
13481
13482/**
James Smart6d368e52011-05-24 11:44:12 -040013483 * lpfc_sli4_post_scsi_sgl_blk_ext - post a block of scsi sgls to the port.
13484 * @phba: pointer to lpfc hba data structure.
13485 * @sblist: pointer to scsi buffer list.
13486 * @count: number of scsi buffers on the list.
13487 *
13488 * This routine is invoked to post a block of @count scsi sgl pages from a
13489 * SCSI buffer list @sblist to the HBA using non-embedded mailbox command.
13490 * No Lock is held.
13491 *
13492 **/
13493int
13494lpfc_sli4_post_scsi_sgl_blk_ext(struct lpfc_hba *phba, struct list_head *sblist,
13495 int cnt)
13496{
13497 struct lpfc_scsi_buf *psb = NULL;
13498 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
13499 struct sgl_page_pairs *sgl_pg_pairs;
13500 void *viraddr;
13501 LPFC_MBOXQ_t *mbox;
13502 uint32_t reqlen, alloclen, pg_pairs;
13503 uint32_t mbox_tmo;
13504 uint16_t xri_start = 0, scsi_xri_start;
13505 uint16_t rsrc_range;
13506 int rc = 0, avail_cnt;
13507 uint32_t shdr_status, shdr_add_status;
13508 dma_addr_t pdma_phys_bpl1;
13509 union lpfc_sli4_cfg_shdr *shdr;
13510 struct lpfc_rsrc_blks *rsrc_blk;
13511 uint32_t xri_cnt = 0;
13512
13513 /* Calculate the total requested length of the dma memory */
13514 reqlen = cnt * sizeof(struct sgl_page_pairs) +
13515 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
13516 if (reqlen > SLI4_PAGE_SIZE) {
13517 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
13518 "2932 Block sgl registration required DMA "
13519 "size (%d) great than a page\n", reqlen);
13520 return -ENOMEM;
13521 }
13522
13523 /*
13524 * The use of extents requires the driver to post the sgl headers
13525 * in multiple postings to meet the contiguous resource assignment.
13526 */
13527 psb = list_prepare_entry(psb, sblist, list);
13528 scsi_xri_start = phba->sli4_hba.scsi_xri_start;
13529 list_for_each_entry(rsrc_blk, &phba->sli4_hba.lpfc_xri_blk_list,
13530 list) {
13531 rsrc_range = rsrc_blk->rsrc_start + rsrc_blk->rsrc_size;
13532 if (rsrc_range < scsi_xri_start)
13533 continue;
13534 else if (rsrc_blk->rsrc_used >= rsrc_blk->rsrc_size)
13535 continue;
13536 else
13537 avail_cnt = rsrc_blk->rsrc_size - rsrc_blk->rsrc_used;
13538
13539 reqlen = (avail_cnt * sizeof(struct sgl_page_pairs)) +
13540 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
13541 /*
13542 * Allocate DMA memory and set up the non-embedded mailbox
13543 * command. The mbox is used to post an SGL page per loop
13544 * but the DMA memory has a use-once semantic so the mailbox
13545 * is used and freed per loop pass.
13546 */
13547 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
13548 if (!mbox) {
13549 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13550 "2933 Failed to allocate mbox cmd "
13551 "memory\n");
13552 return -ENOMEM;
13553 }
13554 alloclen = lpfc_sli4_config(phba, mbox,
13555 LPFC_MBOX_SUBSYSTEM_FCOE,
13556 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES,
13557 reqlen,
13558 LPFC_SLI4_MBX_NEMBED);
13559 if (alloclen < reqlen) {
13560 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13561 "2934 Allocated DMA memory size (%d) "
13562 "is less than the requested DMA memory "
13563 "size (%d)\n", alloclen, reqlen);
13564 lpfc_sli4_mbox_cmd_free(phba, mbox);
13565 return -ENOMEM;
13566 }
13567
13568 /* Get the first SGE entry from the non-embedded DMA memory */
13569 viraddr = mbox->sge_array->addr[0];
13570
13571 /* Set up the SGL pages in the non-embedded DMA pages */
13572 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
13573 sgl_pg_pairs = &sgl->sgl_pg_pairs;
13574
13575 /* pg_pairs tracks posted SGEs per loop iteration. */
13576 pg_pairs = 0;
13577 list_for_each_entry_continue(psb, sblist, list) {
13578 /* Set up the sge entry */
13579 sgl_pg_pairs->sgl_pg0_addr_lo =
13580 cpu_to_le32(putPaddrLow(psb->dma_phys_bpl));
13581 sgl_pg_pairs->sgl_pg0_addr_hi =
13582 cpu_to_le32(putPaddrHigh(psb->dma_phys_bpl));
13583 if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
13584 pdma_phys_bpl1 = psb->dma_phys_bpl +
13585 SGL_PAGE_SIZE;
13586 else
13587 pdma_phys_bpl1 = 0;
13588 sgl_pg_pairs->sgl_pg1_addr_lo =
13589 cpu_to_le32(putPaddrLow(pdma_phys_bpl1));
13590 sgl_pg_pairs->sgl_pg1_addr_hi =
13591 cpu_to_le32(putPaddrHigh(pdma_phys_bpl1));
13592 /* Keep the first xri for this extent. */
13593 if (pg_pairs == 0)
13594 xri_start = psb->cur_iocbq.sli4_xritag;
13595 sgl_pg_pairs++;
13596 pg_pairs++;
13597 xri_cnt++;
13598
13599 /*
13600 * Track two exit conditions - the loop has constructed
13601 * all of the caller's SGE pairs or all available
13602 * resource IDs in this extent are consumed.
13603 */
13604 if ((xri_cnt == cnt) || (pg_pairs >= avail_cnt))
13605 break;
13606 }
13607 rsrc_blk->rsrc_used += pg_pairs;
13608 bf_set(lpfc_post_sgl_pages_xri, sgl, xri_start);
13609 bf_set(lpfc_post_sgl_pages_xricnt, sgl, pg_pairs);
13610
13611 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
13612 "3016 Post SCSI Extent SGL, start %d, cnt %d "
13613 "blk use %d\n",
13614 xri_start, pg_pairs, rsrc_blk->rsrc_used);
13615 /* Perform endian conversion if necessary */
13616 sgl->word0 = cpu_to_le32(sgl->word0);
13617 if (!phba->sli4_hba.intr_enable)
13618 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
13619 else {
James Smarta183a152011-10-10 21:32:43 -040013620 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart6d368e52011-05-24 11:44:12 -040013621 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
13622 }
13623 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
13624 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13625 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
13626 &shdr->response);
13627 if (rc != MBX_TIMEOUT)
13628 lpfc_sli4_mbox_cmd_free(phba, mbox);
13629 if (shdr_status || shdr_add_status || rc) {
13630 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13631 "2935 POST_SGL_BLOCK mailbox command "
13632 "failed status x%x add_status x%x "
13633 "mbx status x%x\n",
13634 shdr_status, shdr_add_status, rc);
13635 return -ENXIO;
13636 }
13637
13638 /* Post only what is requested. */
13639 if (xri_cnt >= cnt)
13640 break;
13641 }
13642 return rc;
13643}
13644
13645/**
James Smart4f774512009-05-22 14:52:35 -040013646 * lpfc_fc_frame_check - Check that this frame is a valid frame to handle
13647 * @phba: pointer to lpfc_hba struct that the frame was received on
13648 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
13649 *
13650 * This function checks the fields in the @fc_hdr to see if the FC frame is a
13651 * valid type of frame that the LPFC driver will handle. This function will
13652 * return a zero if the frame is a valid frame or a non zero value when the
13653 * frame does not pass the check.
13654 **/
13655static int
13656lpfc_fc_frame_check(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr)
13657{
Tomas Henzl474ffb72010-12-22 16:52:40 +010013658 /* make rctl_names static to save stack space */
13659 static char *rctl_names[] = FC_RCTL_NAMES_INIT;
James Smart4f774512009-05-22 14:52:35 -040013660 char *type_names[] = FC_TYPE_NAMES_INIT;
13661 struct fc_vft_header *fc_vft_hdr;
James Smart546fc852011-03-11 16:06:29 -050013662 uint32_t *header = (uint32_t *) fc_hdr;
James Smart4f774512009-05-22 14:52:35 -040013663
13664 switch (fc_hdr->fh_r_ctl) {
13665 case FC_RCTL_DD_UNCAT: /* uncategorized information */
13666 case FC_RCTL_DD_SOL_DATA: /* solicited data */
13667 case FC_RCTL_DD_UNSOL_CTL: /* unsolicited control */
13668 case FC_RCTL_DD_SOL_CTL: /* solicited control or reply */
13669 case FC_RCTL_DD_UNSOL_DATA: /* unsolicited data */
13670 case FC_RCTL_DD_DATA_DESC: /* data descriptor */
13671 case FC_RCTL_DD_UNSOL_CMD: /* unsolicited command */
13672 case FC_RCTL_DD_CMD_STATUS: /* command status */
13673 case FC_RCTL_ELS_REQ: /* extended link services request */
13674 case FC_RCTL_ELS_REP: /* extended link services reply */
13675 case FC_RCTL_ELS4_REQ: /* FC-4 ELS request */
13676 case FC_RCTL_ELS4_REP: /* FC-4 ELS reply */
13677 case FC_RCTL_BA_NOP: /* basic link service NOP */
13678 case FC_RCTL_BA_ABTS: /* basic link service abort */
13679 case FC_RCTL_BA_RMC: /* remove connection */
13680 case FC_RCTL_BA_ACC: /* basic accept */
13681 case FC_RCTL_BA_RJT: /* basic reject */
13682 case FC_RCTL_BA_PRMT:
13683 case FC_RCTL_ACK_1: /* acknowledge_1 */
13684 case FC_RCTL_ACK_0: /* acknowledge_0 */
13685 case FC_RCTL_P_RJT: /* port reject */
13686 case FC_RCTL_F_RJT: /* fabric reject */
13687 case FC_RCTL_P_BSY: /* port busy */
13688 case FC_RCTL_F_BSY: /* fabric busy to data frame */
13689 case FC_RCTL_F_BSYL: /* fabric busy to link control frame */
13690 case FC_RCTL_LCR: /* link credit reset */
13691 case FC_RCTL_END: /* end */
13692 break;
13693 case FC_RCTL_VFTH: /* Virtual Fabric tagging Header */
13694 fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
13695 fc_hdr = &((struct fc_frame_header *)fc_vft_hdr)[1];
13696 return lpfc_fc_frame_check(phba, fc_hdr);
13697 default:
13698 goto drop;
13699 }
13700 switch (fc_hdr->fh_type) {
13701 case FC_TYPE_BLS:
13702 case FC_TYPE_ELS:
13703 case FC_TYPE_FCP:
13704 case FC_TYPE_CT:
13705 break;
13706 case FC_TYPE_IP:
13707 case FC_TYPE_ILS:
13708 default:
13709 goto drop;
13710 }
James Smart546fc852011-03-11 16:06:29 -050013711
James Smart4f774512009-05-22 14:52:35 -040013712 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
James Smart546fc852011-03-11 16:06:29 -050013713 "2538 Received frame rctl:%s type:%s "
13714 "Frame Data:%08x %08x %08x %08x %08x %08x\n",
James Smart4f774512009-05-22 14:52:35 -040013715 rctl_names[fc_hdr->fh_r_ctl],
James Smart546fc852011-03-11 16:06:29 -050013716 type_names[fc_hdr->fh_type],
13717 be32_to_cpu(header[0]), be32_to_cpu(header[1]),
13718 be32_to_cpu(header[2]), be32_to_cpu(header[3]),
13719 be32_to_cpu(header[4]), be32_to_cpu(header[5]));
James Smart4f774512009-05-22 14:52:35 -040013720 return 0;
13721drop:
13722 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
13723 "2539 Dropped frame rctl:%s type:%s\n",
13724 rctl_names[fc_hdr->fh_r_ctl],
13725 type_names[fc_hdr->fh_type]);
13726 return 1;
13727}
13728
13729/**
13730 * lpfc_fc_hdr_get_vfi - Get the VFI from an FC frame
13731 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
13732 *
13733 * This function processes the FC header to retrieve the VFI from the VF
13734 * header, if one exists. This function will return the VFI if one exists
13735 * or 0 if no VSAN Header exists.
13736 **/
13737static uint32_t
13738lpfc_fc_hdr_get_vfi(struct fc_frame_header *fc_hdr)
13739{
13740 struct fc_vft_header *fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
13741
13742 if (fc_hdr->fh_r_ctl != FC_RCTL_VFTH)
13743 return 0;
13744 return bf_get(fc_vft_hdr_vf_id, fc_vft_hdr);
13745}
13746
13747/**
13748 * lpfc_fc_frame_to_vport - Finds the vport that a frame is destined to
13749 * @phba: Pointer to the HBA structure to search for the vport on
13750 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
13751 * @fcfi: The FC Fabric ID that the frame came from
13752 *
13753 * This function searches the @phba for a vport that matches the content of the
13754 * @fc_hdr passed in and the @fcfi. This function uses the @fc_hdr to fetch the
13755 * VFI, if the Virtual Fabric Tagging Header exists, and the DID. This function
13756 * returns the matching vport pointer or NULL if unable to match frame to a
13757 * vport.
13758 **/
13759static struct lpfc_vport *
13760lpfc_fc_frame_to_vport(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr,
13761 uint16_t fcfi)
13762{
13763 struct lpfc_vport **vports;
13764 struct lpfc_vport *vport = NULL;
13765 int i;
13766 uint32_t did = (fc_hdr->fh_d_id[0] << 16 |
13767 fc_hdr->fh_d_id[1] << 8 |
13768 fc_hdr->fh_d_id[2]);
James Smartbf086112011-08-21 21:48:13 -040013769 if (did == Fabric_DID)
13770 return phba->pport;
James Smart4f774512009-05-22 14:52:35 -040013771 vports = lpfc_create_vport_work_array(phba);
13772 if (vports != NULL)
13773 for (i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
13774 if (phba->fcf.fcfi == fcfi &&
13775 vports[i]->vfi == lpfc_fc_hdr_get_vfi(fc_hdr) &&
13776 vports[i]->fc_myDID == did) {
13777 vport = vports[i];
13778 break;
13779 }
13780 }
13781 lpfc_destroy_vport_work_array(phba, vports);
13782 return vport;
13783}
13784
13785/**
James Smart45ed1192009-10-02 15:17:02 -040013786 * lpfc_update_rcv_time_stamp - Update vport's rcv seq time stamp
13787 * @vport: The vport to work on.
13788 *
13789 * This function updates the receive sequence time stamp for this vport. The
13790 * receive sequence time stamp indicates the time that the last frame of the
13791 * the sequence that has been idle for the longest amount of time was received.
13792 * the driver uses this time stamp to indicate if any received sequences have
13793 * timed out.
13794 **/
13795void
13796lpfc_update_rcv_time_stamp(struct lpfc_vport *vport)
13797{
13798 struct lpfc_dmabuf *h_buf;
13799 struct hbq_dmabuf *dmabuf = NULL;
13800
13801 /* get the oldest sequence on the rcv list */
13802 h_buf = list_get_first(&vport->rcv_buffer_list,
13803 struct lpfc_dmabuf, list);
13804 if (!h_buf)
13805 return;
13806 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
13807 vport->rcv_buffer_time_stamp = dmabuf->time_stamp;
13808}
13809
13810/**
13811 * lpfc_cleanup_rcv_buffers - Cleans up all outstanding receive sequences.
13812 * @vport: The vport that the received sequences were sent to.
13813 *
13814 * This function cleans up all outstanding received sequences. This is called
13815 * by the driver when a link event or user action invalidates all the received
13816 * sequences.
13817 **/
13818void
13819lpfc_cleanup_rcv_buffers(struct lpfc_vport *vport)
13820{
13821 struct lpfc_dmabuf *h_buf, *hnext;
13822 struct lpfc_dmabuf *d_buf, *dnext;
13823 struct hbq_dmabuf *dmabuf = NULL;
13824
13825 /* start with the oldest sequence on the rcv list */
13826 list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
13827 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
13828 list_del_init(&dmabuf->hbuf.list);
13829 list_for_each_entry_safe(d_buf, dnext,
13830 &dmabuf->dbuf.list, list) {
13831 list_del_init(&d_buf->list);
13832 lpfc_in_buf_free(vport->phba, d_buf);
13833 }
13834 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
13835 }
13836}
13837
13838/**
13839 * lpfc_rcv_seq_check_edtov - Cleans up timed out receive sequences.
13840 * @vport: The vport that the received sequences were sent to.
13841 *
13842 * This function determines whether any received sequences have timed out by
13843 * first checking the vport's rcv_buffer_time_stamp. If this time_stamp
13844 * indicates that there is at least one timed out sequence this routine will
13845 * go through the received sequences one at a time from most inactive to most
13846 * active to determine which ones need to be cleaned up. Once it has determined
13847 * that a sequence needs to be cleaned up it will simply free up the resources
13848 * without sending an abort.
13849 **/
13850void
13851lpfc_rcv_seq_check_edtov(struct lpfc_vport *vport)
13852{
13853 struct lpfc_dmabuf *h_buf, *hnext;
13854 struct lpfc_dmabuf *d_buf, *dnext;
13855 struct hbq_dmabuf *dmabuf = NULL;
13856 unsigned long timeout;
13857 int abort_count = 0;
13858
13859 timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
13860 vport->rcv_buffer_time_stamp);
13861 if (list_empty(&vport->rcv_buffer_list) ||
13862 time_before(jiffies, timeout))
13863 return;
13864 /* start with the oldest sequence on the rcv list */
13865 list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
13866 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
13867 timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
13868 dmabuf->time_stamp);
13869 if (time_before(jiffies, timeout))
13870 break;
13871 abort_count++;
13872 list_del_init(&dmabuf->hbuf.list);
13873 list_for_each_entry_safe(d_buf, dnext,
13874 &dmabuf->dbuf.list, list) {
13875 list_del_init(&d_buf->list);
13876 lpfc_in_buf_free(vport->phba, d_buf);
13877 }
13878 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
13879 }
13880 if (abort_count)
13881 lpfc_update_rcv_time_stamp(vport);
13882}
13883
13884/**
James Smart4f774512009-05-22 14:52:35 -040013885 * lpfc_fc_frame_add - Adds a frame to the vport's list of received sequences
13886 * @dmabuf: pointer to a dmabuf that describes the hdr and data of the FC frame
13887 *
13888 * This function searches through the existing incomplete sequences that have
13889 * been sent to this @vport. If the frame matches one of the incomplete
13890 * sequences then the dbuf in the @dmabuf is added to the list of frames that
13891 * make up that sequence. If no sequence is found that matches this frame then
13892 * the function will add the hbuf in the @dmabuf to the @vport's rcv_buffer_list
13893 * This function returns a pointer to the first dmabuf in the sequence list that
13894 * the frame was linked to.
13895 **/
13896static struct hbq_dmabuf *
13897lpfc_fc_frame_add(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
13898{
13899 struct fc_frame_header *new_hdr;
13900 struct fc_frame_header *temp_hdr;
13901 struct lpfc_dmabuf *d_buf;
13902 struct lpfc_dmabuf *h_buf;
13903 struct hbq_dmabuf *seq_dmabuf = NULL;
13904 struct hbq_dmabuf *temp_dmabuf = NULL;
13905
James Smart4d9ab992009-10-02 15:16:39 -040013906 INIT_LIST_HEAD(&dmabuf->dbuf.list);
James Smart45ed1192009-10-02 15:17:02 -040013907 dmabuf->time_stamp = jiffies;
James Smart4f774512009-05-22 14:52:35 -040013908 new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
13909 /* Use the hdr_buf to find the sequence that this frame belongs to */
13910 list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
13911 temp_hdr = (struct fc_frame_header *)h_buf->virt;
13912 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
13913 (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
13914 (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
13915 continue;
13916 /* found a pending sequence that matches this frame */
13917 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
13918 break;
13919 }
13920 if (!seq_dmabuf) {
13921 /*
13922 * This indicates first frame received for this sequence.
13923 * Queue the buffer on the vport's rcv_buffer_list.
13924 */
13925 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
James Smart45ed1192009-10-02 15:17:02 -040013926 lpfc_update_rcv_time_stamp(vport);
James Smart4f774512009-05-22 14:52:35 -040013927 return dmabuf;
13928 }
13929 temp_hdr = seq_dmabuf->hbuf.virt;
James Smarteeead812009-12-21 17:01:23 -050013930 if (be16_to_cpu(new_hdr->fh_seq_cnt) <
13931 be16_to_cpu(temp_hdr->fh_seq_cnt)) {
James Smart4d9ab992009-10-02 15:16:39 -040013932 list_del_init(&seq_dmabuf->hbuf.list);
13933 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
13934 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
James Smart45ed1192009-10-02 15:17:02 -040013935 lpfc_update_rcv_time_stamp(vport);
James Smart4f774512009-05-22 14:52:35 -040013936 return dmabuf;
13937 }
James Smart45ed1192009-10-02 15:17:02 -040013938 /* move this sequence to the tail to indicate a young sequence */
13939 list_move_tail(&seq_dmabuf->hbuf.list, &vport->rcv_buffer_list);
13940 seq_dmabuf->time_stamp = jiffies;
13941 lpfc_update_rcv_time_stamp(vport);
James Smarteeead812009-12-21 17:01:23 -050013942 if (list_empty(&seq_dmabuf->dbuf.list)) {
13943 temp_hdr = dmabuf->hbuf.virt;
13944 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
13945 return seq_dmabuf;
13946 }
James Smart4f774512009-05-22 14:52:35 -040013947 /* find the correct place in the sequence to insert this frame */
13948 list_for_each_entry_reverse(d_buf, &seq_dmabuf->dbuf.list, list) {
13949 temp_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
13950 temp_hdr = (struct fc_frame_header *)temp_dmabuf->hbuf.virt;
13951 /*
13952 * If the frame's sequence count is greater than the frame on
13953 * the list then insert the frame right after this frame
13954 */
James Smarteeead812009-12-21 17:01:23 -050013955 if (be16_to_cpu(new_hdr->fh_seq_cnt) >
13956 be16_to_cpu(temp_hdr->fh_seq_cnt)) {
James Smart4f774512009-05-22 14:52:35 -040013957 list_add(&dmabuf->dbuf.list, &temp_dmabuf->dbuf.list);
13958 return seq_dmabuf;
13959 }
13960 }
13961 return NULL;
13962}
13963
13964/**
James Smart6669f9b2009-10-02 15:16:45 -040013965 * lpfc_sli4_abort_partial_seq - Abort partially assembled unsol sequence
13966 * @vport: pointer to a vitural port
13967 * @dmabuf: pointer to a dmabuf that describes the FC sequence
13968 *
13969 * This function tries to abort from the partially assembed sequence, described
13970 * by the information from basic abbort @dmabuf. It checks to see whether such
13971 * partially assembled sequence held by the driver. If so, it shall free up all
13972 * the frames from the partially assembled sequence.
13973 *
13974 * Return
13975 * true -- if there is matching partially assembled sequence present and all
13976 * the frames freed with the sequence;
13977 * false -- if there is no matching partially assembled sequence present so
13978 * nothing got aborted in the lower layer driver
13979 **/
13980static bool
13981lpfc_sli4_abort_partial_seq(struct lpfc_vport *vport,
13982 struct hbq_dmabuf *dmabuf)
13983{
13984 struct fc_frame_header *new_hdr;
13985 struct fc_frame_header *temp_hdr;
13986 struct lpfc_dmabuf *d_buf, *n_buf, *h_buf;
13987 struct hbq_dmabuf *seq_dmabuf = NULL;
13988
13989 /* Use the hdr_buf to find the sequence that matches this frame */
13990 INIT_LIST_HEAD(&dmabuf->dbuf.list);
13991 INIT_LIST_HEAD(&dmabuf->hbuf.list);
13992 new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
13993 list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
13994 temp_hdr = (struct fc_frame_header *)h_buf->virt;
13995 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
13996 (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
13997 (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
13998 continue;
13999 /* found a pending sequence that matches this frame */
14000 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
14001 break;
14002 }
14003
14004 /* Free up all the frames from the partially assembled sequence */
14005 if (seq_dmabuf) {
14006 list_for_each_entry_safe(d_buf, n_buf,
14007 &seq_dmabuf->dbuf.list, list) {
14008 list_del_init(&d_buf->list);
14009 lpfc_in_buf_free(vport->phba, d_buf);
14010 }
14011 return true;
14012 }
14013 return false;
14014}
14015
14016/**
James Smart546fc852011-03-11 16:06:29 -050014017 * lpfc_sli4_seq_abort_rsp_cmpl - BLS ABORT RSP seq abort iocb complete handler
James Smart6669f9b2009-10-02 15:16:45 -040014018 * @phba: Pointer to HBA context object.
14019 * @cmd_iocbq: pointer to the command iocbq structure.
14020 * @rsp_iocbq: pointer to the response iocbq structure.
14021 *
James Smart546fc852011-03-11 16:06:29 -050014022 * This function handles the sequence abort response iocb command complete
James Smart6669f9b2009-10-02 15:16:45 -040014023 * event. It properly releases the memory allocated to the sequence abort
14024 * accept iocb.
14025 **/
14026static void
James Smart546fc852011-03-11 16:06:29 -050014027lpfc_sli4_seq_abort_rsp_cmpl(struct lpfc_hba *phba,
James Smart6669f9b2009-10-02 15:16:45 -040014028 struct lpfc_iocbq *cmd_iocbq,
14029 struct lpfc_iocbq *rsp_iocbq)
14030{
14031 if (cmd_iocbq)
14032 lpfc_sli_release_iocbq(phba, cmd_iocbq);
14033}
14034
14035/**
James Smart6d368e52011-05-24 11:44:12 -040014036 * lpfc_sli4_xri_inrange - check xri is in range of xris owned by driver.
14037 * @phba: Pointer to HBA context object.
14038 * @xri: xri id in transaction.
14039 *
14040 * This function validates the xri maps to the known range of XRIs allocated an
14041 * used by the driver.
14042 **/
James Smart7851fe22011-07-22 18:36:52 -040014043uint16_t
James Smart6d368e52011-05-24 11:44:12 -040014044lpfc_sli4_xri_inrange(struct lpfc_hba *phba,
14045 uint16_t xri)
14046{
14047 int i;
14048
14049 for (i = 0; i < phba->sli4_hba.max_cfg_param.max_xri; i++) {
14050 if (xri == phba->sli4_hba.xri_ids[i])
14051 return i;
14052 }
14053 return NO_XRI;
14054}
14055
14056
14057/**
James Smart546fc852011-03-11 16:06:29 -050014058 * lpfc_sli4_seq_abort_rsp - bls rsp to sequence abort
James Smart6669f9b2009-10-02 15:16:45 -040014059 * @phba: Pointer to HBA context object.
14060 * @fc_hdr: pointer to a FC frame header.
14061 *
James Smart546fc852011-03-11 16:06:29 -050014062 * This function sends a basic response to a previous unsol sequence abort
James Smart6669f9b2009-10-02 15:16:45 -040014063 * event after aborting the sequence handling.
14064 **/
14065static void
James Smart546fc852011-03-11 16:06:29 -050014066lpfc_sli4_seq_abort_rsp(struct lpfc_hba *phba,
James Smart6669f9b2009-10-02 15:16:45 -040014067 struct fc_frame_header *fc_hdr)
14068{
14069 struct lpfc_iocbq *ctiocb = NULL;
14070 struct lpfc_nodelist *ndlp;
James Smart5ffc2662009-11-18 15:39:44 -050014071 uint16_t oxid, rxid;
14072 uint32_t sid, fctl;
James Smart6669f9b2009-10-02 15:16:45 -040014073 IOCB_t *icmd;
James Smart546fc852011-03-11 16:06:29 -050014074 int rc;
James Smart6669f9b2009-10-02 15:16:45 -040014075
14076 if (!lpfc_is_link_up(phba))
14077 return;
14078
14079 sid = sli4_sid_from_fc_hdr(fc_hdr);
14080 oxid = be16_to_cpu(fc_hdr->fh_ox_id);
James Smart5ffc2662009-11-18 15:39:44 -050014081 rxid = be16_to_cpu(fc_hdr->fh_rx_id);
James Smart6669f9b2009-10-02 15:16:45 -040014082
14083 ndlp = lpfc_findnode_did(phba->pport, sid);
14084 if (!ndlp) {
14085 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
14086 "1268 Find ndlp returned NULL for oxid:x%x "
14087 "SID:x%x\n", oxid, sid);
14088 return;
14089 }
James Smart6d368e52011-05-24 11:44:12 -040014090 if (lpfc_sli4_xri_inrange(phba, rxid))
James Smart19ca7602010-11-20 23:11:55 -050014091 lpfc_set_rrq_active(phba, ndlp, rxid, oxid, 0);
James Smart6669f9b2009-10-02 15:16:45 -040014092
James Smart546fc852011-03-11 16:06:29 -050014093 /* Allocate buffer for rsp iocb */
James Smart6669f9b2009-10-02 15:16:45 -040014094 ctiocb = lpfc_sli_get_iocbq(phba);
14095 if (!ctiocb)
14096 return;
14097
James Smart5ffc2662009-11-18 15:39:44 -050014098 /* Extract the F_CTL field from FC_HDR */
14099 fctl = sli4_fctl_from_fc_hdr(fc_hdr);
14100
James Smart6669f9b2009-10-02 15:16:45 -040014101 icmd = &ctiocb->iocb;
James Smart6669f9b2009-10-02 15:16:45 -040014102 icmd->un.xseq64.bdl.bdeSize = 0;
James Smart5ffc2662009-11-18 15:39:44 -050014103 icmd->un.xseq64.bdl.ulpIoTag32 = 0;
James Smart6669f9b2009-10-02 15:16:45 -040014104 icmd->un.xseq64.w5.hcsw.Dfctl = 0;
14105 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_ACC;
14106 icmd->un.xseq64.w5.hcsw.Type = FC_TYPE_BLS;
14107
14108 /* Fill in the rest of iocb fields */
14109 icmd->ulpCommand = CMD_XMIT_BLS_RSP64_CX;
14110 icmd->ulpBdeCount = 0;
14111 icmd->ulpLe = 1;
14112 icmd->ulpClass = CLASS3;
James Smart6d368e52011-05-24 11:44:12 -040014113 icmd->ulpContext = phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
James Smartbe858b62010-12-15 17:57:20 -050014114 ctiocb->context1 = ndlp;
James Smart6669f9b2009-10-02 15:16:45 -040014115
James Smart6669f9b2009-10-02 15:16:45 -040014116 ctiocb->iocb_cmpl = NULL;
14117 ctiocb->vport = phba->pport;
James Smart546fc852011-03-11 16:06:29 -050014118 ctiocb->iocb_cmpl = lpfc_sli4_seq_abort_rsp_cmpl;
James Smart6d368e52011-05-24 11:44:12 -040014119 ctiocb->sli4_lxritag = NO_XRI;
James Smart546fc852011-03-11 16:06:29 -050014120 ctiocb->sli4_xritag = NO_XRI;
14121
14122 /* If the oxid maps to the FCP XRI range or if it is out of range,
14123 * send a BLS_RJT. The driver no longer has that exchange.
14124 * Override the IOCB for a BA_RJT.
14125 */
14126 if (oxid > (phba->sli4_hba.max_cfg_param.max_xri +
14127 phba->sli4_hba.max_cfg_param.xri_base) ||
14128 oxid > (lpfc_sli4_get_els_iocb_cnt(phba) +
14129 phba->sli4_hba.max_cfg_param.xri_base)) {
14130 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_RJT;
14131 bf_set(lpfc_vndr_code, &icmd->un.bls_rsp, 0);
14132 bf_set(lpfc_rsn_expln, &icmd->un.bls_rsp, FC_BA_RJT_INV_XID);
14133 bf_set(lpfc_rsn_code, &icmd->un.bls_rsp, FC_BA_RJT_UNABLE);
14134 }
James Smart6669f9b2009-10-02 15:16:45 -040014135
James Smart5ffc2662009-11-18 15:39:44 -050014136 if (fctl & FC_FC_EX_CTX) {
14137 /* ABTS sent by responder to CT exchange, construction
14138 * of BA_ACC will use OX_ID from ABTS for the XRI_TAG
14139 * field and RX_ID from ABTS for RX_ID field.
14140 */
James Smart546fc852011-03-11 16:06:29 -050014141 bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_RSP);
14142 bf_set(lpfc_abts_rxid, &icmd->un.bls_rsp, rxid);
James Smart5ffc2662009-11-18 15:39:44 -050014143 } else {
14144 /* ABTS sent by initiator to CT exchange, construction
14145 * of BA_ACC will need to allocate a new XRI as for the
14146 * XRI_TAG and RX_ID fields.
14147 */
James Smart546fc852011-03-11 16:06:29 -050014148 bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_INT);
14149 bf_set(lpfc_abts_rxid, &icmd->un.bls_rsp, NO_XRI);
James Smart5ffc2662009-11-18 15:39:44 -050014150 }
James Smart546fc852011-03-11 16:06:29 -050014151 bf_set(lpfc_abts_oxid, &icmd->un.bls_rsp, oxid);
James Smart5ffc2662009-11-18 15:39:44 -050014152
James Smart546fc852011-03-11 16:06:29 -050014153 /* Xmit CT abts response on exchange <xid> */
James Smart6669f9b2009-10-02 15:16:45 -040014154 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
James Smart546fc852011-03-11 16:06:29 -050014155 "1200 Send BLS cmd x%x on oxid x%x Data: x%x\n",
14156 icmd->un.xseq64.w5.hcsw.Rctl, oxid, phba->link_state);
14157
14158 rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, ctiocb, 0);
14159 if (rc == IOCB_ERROR) {
14160 lpfc_printf_log(phba, KERN_ERR, LOG_ELS,
14161 "2925 Failed to issue CT ABTS RSP x%x on "
14162 "xri x%x, Data x%x\n",
14163 icmd->un.xseq64.w5.hcsw.Rctl, oxid,
14164 phba->link_state);
14165 lpfc_sli_release_iocbq(phba, ctiocb);
14166 }
James Smart6669f9b2009-10-02 15:16:45 -040014167}
14168
14169/**
14170 * lpfc_sli4_handle_unsol_abort - Handle sli-4 unsolicited abort event
14171 * @vport: Pointer to the vport on which this sequence was received
14172 * @dmabuf: pointer to a dmabuf that describes the FC sequence
14173 *
14174 * This function handles an SLI-4 unsolicited abort event. If the unsolicited
14175 * receive sequence is only partially assembed by the driver, it shall abort
14176 * the partially assembled frames for the sequence. Otherwise, if the
14177 * unsolicited receive sequence has been completely assembled and passed to
14178 * the Upper Layer Protocol (UPL), it then mark the per oxid status for the
14179 * unsolicited sequence has been aborted. After that, it will issue a basic
14180 * accept to accept the abort.
14181 **/
14182void
14183lpfc_sli4_handle_unsol_abort(struct lpfc_vport *vport,
14184 struct hbq_dmabuf *dmabuf)
14185{
14186 struct lpfc_hba *phba = vport->phba;
14187 struct fc_frame_header fc_hdr;
James Smart5ffc2662009-11-18 15:39:44 -050014188 uint32_t fctl;
James Smart6669f9b2009-10-02 15:16:45 -040014189 bool abts_par;
14190
James Smart6669f9b2009-10-02 15:16:45 -040014191 /* Make a copy of fc_hdr before the dmabuf being released */
14192 memcpy(&fc_hdr, dmabuf->hbuf.virt, sizeof(struct fc_frame_header));
James Smart5ffc2662009-11-18 15:39:44 -050014193 fctl = sli4_fctl_from_fc_hdr(&fc_hdr);
James Smart6669f9b2009-10-02 15:16:45 -040014194
James Smart5ffc2662009-11-18 15:39:44 -050014195 if (fctl & FC_FC_EX_CTX) {
14196 /*
14197 * ABTS sent by responder to exchange, just free the buffer
14198 */
James Smart6669f9b2009-10-02 15:16:45 -040014199 lpfc_in_buf_free(phba, &dmabuf->dbuf);
James Smart5ffc2662009-11-18 15:39:44 -050014200 } else {
14201 /*
14202 * ABTS sent by initiator to exchange, need to do cleanup
14203 */
14204 /* Try to abort partially assembled seq */
14205 abts_par = lpfc_sli4_abort_partial_seq(vport, dmabuf);
14206
14207 /* Send abort to ULP if partially seq abort failed */
14208 if (abts_par == false)
14209 lpfc_sli4_send_seq_to_ulp(vport, dmabuf);
14210 else
14211 lpfc_in_buf_free(phba, &dmabuf->dbuf);
14212 }
James Smart6669f9b2009-10-02 15:16:45 -040014213 /* Send basic accept (BA_ACC) to the abort requester */
James Smart546fc852011-03-11 16:06:29 -050014214 lpfc_sli4_seq_abort_rsp(phba, &fc_hdr);
James Smart6669f9b2009-10-02 15:16:45 -040014215}
14216
14217/**
James Smart4f774512009-05-22 14:52:35 -040014218 * lpfc_seq_complete - Indicates if a sequence is complete
14219 * @dmabuf: pointer to a dmabuf that describes the FC sequence
14220 *
14221 * This function checks the sequence, starting with the frame described by
14222 * @dmabuf, to see if all the frames associated with this sequence are present.
14223 * the frames associated with this sequence are linked to the @dmabuf using the
14224 * dbuf list. This function looks for two major things. 1) That the first frame
14225 * has a sequence count of zero. 2) There is a frame with last frame of sequence
14226 * set. 3) That there are no holes in the sequence count. The function will
14227 * return 1 when the sequence is complete, otherwise it will return 0.
14228 **/
14229static int
14230lpfc_seq_complete(struct hbq_dmabuf *dmabuf)
14231{
14232 struct fc_frame_header *hdr;
14233 struct lpfc_dmabuf *d_buf;
14234 struct hbq_dmabuf *seq_dmabuf;
14235 uint32_t fctl;
14236 int seq_count = 0;
14237
14238 hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
14239 /* make sure first fame of sequence has a sequence count of zero */
14240 if (hdr->fh_seq_cnt != seq_count)
14241 return 0;
14242 fctl = (hdr->fh_f_ctl[0] << 16 |
14243 hdr->fh_f_ctl[1] << 8 |
14244 hdr->fh_f_ctl[2]);
14245 /* If last frame of sequence we can return success. */
14246 if (fctl & FC_FC_END_SEQ)
14247 return 1;
14248 list_for_each_entry(d_buf, &dmabuf->dbuf.list, list) {
14249 seq_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
14250 hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
14251 /* If there is a hole in the sequence count then fail. */
James Smarteeead812009-12-21 17:01:23 -050014252 if (++seq_count != be16_to_cpu(hdr->fh_seq_cnt))
James Smart4f774512009-05-22 14:52:35 -040014253 return 0;
14254 fctl = (hdr->fh_f_ctl[0] << 16 |
14255 hdr->fh_f_ctl[1] << 8 |
14256 hdr->fh_f_ctl[2]);
14257 /* If last frame of sequence we can return success. */
14258 if (fctl & FC_FC_END_SEQ)
14259 return 1;
14260 }
14261 return 0;
14262}
14263
14264/**
14265 * lpfc_prep_seq - Prep sequence for ULP processing
14266 * @vport: Pointer to the vport on which this sequence was received
14267 * @dmabuf: pointer to a dmabuf that describes the FC sequence
14268 *
14269 * This function takes a sequence, described by a list of frames, and creates
14270 * a list of iocbq structures to describe the sequence. This iocbq list will be
14271 * used to issue to the generic unsolicited sequence handler. This routine
14272 * returns a pointer to the first iocbq in the list. If the function is unable
14273 * to allocate an iocbq then it throw out the received frames that were not
14274 * able to be described and return a pointer to the first iocbq. If unable to
14275 * allocate any iocbqs (including the first) this function will return NULL.
14276 **/
14277static struct lpfc_iocbq *
14278lpfc_prep_seq(struct lpfc_vport *vport, struct hbq_dmabuf *seq_dmabuf)
14279{
James Smart7851fe22011-07-22 18:36:52 -040014280 struct hbq_dmabuf *hbq_buf;
James Smart4f774512009-05-22 14:52:35 -040014281 struct lpfc_dmabuf *d_buf, *n_buf;
14282 struct lpfc_iocbq *first_iocbq, *iocbq;
14283 struct fc_frame_header *fc_hdr;
14284 uint32_t sid;
James Smart7851fe22011-07-22 18:36:52 -040014285 uint32_t len, tot_len;
James Smarteeead812009-12-21 17:01:23 -050014286 struct ulp_bde64 *pbde;
James Smart4f774512009-05-22 14:52:35 -040014287
14288 fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
14289 /* remove from receive buffer list */
14290 list_del_init(&seq_dmabuf->hbuf.list);
James Smart45ed1192009-10-02 15:17:02 -040014291 lpfc_update_rcv_time_stamp(vport);
James Smart4f774512009-05-22 14:52:35 -040014292 /* get the Remote Port's SID */
James Smart6669f9b2009-10-02 15:16:45 -040014293 sid = sli4_sid_from_fc_hdr(fc_hdr);
James Smart7851fe22011-07-22 18:36:52 -040014294 tot_len = 0;
James Smart4f774512009-05-22 14:52:35 -040014295 /* Get an iocbq struct to fill in. */
14296 first_iocbq = lpfc_sli_get_iocbq(vport->phba);
14297 if (first_iocbq) {
14298 /* Initialize the first IOCB. */
James Smart8fa38512009-07-19 10:01:03 -040014299 first_iocbq->iocb.unsli3.rcvsli3.acc_len = 0;
James Smart4f774512009-05-22 14:52:35 -040014300 first_iocbq->iocb.ulpStatus = IOSTAT_SUCCESS;
14301 first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_SEQ64_CX;
James Smart7851fe22011-07-22 18:36:52 -040014302 first_iocbq->iocb.ulpContext = NO_XRI;
14303 first_iocbq->iocb.unsli3.rcvsli3.ox_id =
14304 be16_to_cpu(fc_hdr->fh_ox_id);
14305 /* iocbq is prepped for internal consumption. Physical vpi. */
14306 first_iocbq->iocb.unsli3.rcvsli3.vpi =
14307 vport->phba->vpi_ids[vport->vpi];
James Smart4f774512009-05-22 14:52:35 -040014308 /* put the first buffer into the first IOCBq */
14309 first_iocbq->context2 = &seq_dmabuf->dbuf;
14310 first_iocbq->context3 = NULL;
14311 first_iocbq->iocb.ulpBdeCount = 1;
14312 first_iocbq->iocb.un.cont64[0].tus.f.bdeSize =
14313 LPFC_DATA_BUF_SIZE;
14314 first_iocbq->iocb.un.rcvels.remoteID = sid;
James Smart7851fe22011-07-22 18:36:52 -040014315 tot_len = bf_get(lpfc_rcqe_length,
James Smart4d9ab992009-10-02 15:16:39 -040014316 &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
James Smart7851fe22011-07-22 18:36:52 -040014317 first_iocbq->iocb.unsli3.rcvsli3.acc_len = tot_len;
James Smart4f774512009-05-22 14:52:35 -040014318 }
14319 iocbq = first_iocbq;
14320 /*
14321 * Each IOCBq can have two Buffers assigned, so go through the list
14322 * of buffers for this sequence and save two buffers in each IOCBq
14323 */
14324 list_for_each_entry_safe(d_buf, n_buf, &seq_dmabuf->dbuf.list, list) {
14325 if (!iocbq) {
14326 lpfc_in_buf_free(vport->phba, d_buf);
14327 continue;
14328 }
14329 if (!iocbq->context3) {
14330 iocbq->context3 = d_buf;
14331 iocbq->iocb.ulpBdeCount++;
James Smarteeead812009-12-21 17:01:23 -050014332 pbde = (struct ulp_bde64 *)
14333 &iocbq->iocb.unsli3.sli3Words[4];
14334 pbde->tus.f.bdeSize = LPFC_DATA_BUF_SIZE;
James Smart7851fe22011-07-22 18:36:52 -040014335
14336 /* We need to get the size out of the right CQE */
14337 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
14338 len = bf_get(lpfc_rcqe_length,
14339 &hbq_buf->cq_event.cqe.rcqe_cmpl);
14340 iocbq->iocb.unsli3.rcvsli3.acc_len += len;
14341 tot_len += len;
James Smart4f774512009-05-22 14:52:35 -040014342 } else {
14343 iocbq = lpfc_sli_get_iocbq(vport->phba);
14344 if (!iocbq) {
14345 if (first_iocbq) {
14346 first_iocbq->iocb.ulpStatus =
14347 IOSTAT_FCP_RSP_ERROR;
14348 first_iocbq->iocb.un.ulpWord[4] =
14349 IOERR_NO_RESOURCES;
14350 }
14351 lpfc_in_buf_free(vport->phba, d_buf);
14352 continue;
14353 }
14354 iocbq->context2 = d_buf;
14355 iocbq->context3 = NULL;
14356 iocbq->iocb.ulpBdeCount = 1;
14357 iocbq->iocb.un.cont64[0].tus.f.bdeSize =
14358 LPFC_DATA_BUF_SIZE;
James Smart7851fe22011-07-22 18:36:52 -040014359
14360 /* We need to get the size out of the right CQE */
14361 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
14362 len = bf_get(lpfc_rcqe_length,
14363 &hbq_buf->cq_event.cqe.rcqe_cmpl);
14364 tot_len += len;
14365 iocbq->iocb.unsli3.rcvsli3.acc_len = tot_len;
14366
James Smart4f774512009-05-22 14:52:35 -040014367 iocbq->iocb.un.rcvels.remoteID = sid;
14368 list_add_tail(&iocbq->list, &first_iocbq->list);
14369 }
14370 }
14371 return first_iocbq;
14372}
14373
James Smart6669f9b2009-10-02 15:16:45 -040014374static void
14375lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *vport,
14376 struct hbq_dmabuf *seq_dmabuf)
14377{
14378 struct fc_frame_header *fc_hdr;
14379 struct lpfc_iocbq *iocbq, *curr_iocb, *next_iocb;
14380 struct lpfc_hba *phba = vport->phba;
14381
14382 fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
14383 iocbq = lpfc_prep_seq(vport, seq_dmabuf);
14384 if (!iocbq) {
14385 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14386 "2707 Ring %d handler: Failed to allocate "
14387 "iocb Rctl x%x Type x%x received\n",
14388 LPFC_ELS_RING,
14389 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
14390 return;
14391 }
14392 if (!lpfc_complete_unsol_iocb(phba,
14393 &phba->sli.ring[LPFC_ELS_RING],
14394 iocbq, fc_hdr->fh_r_ctl,
14395 fc_hdr->fh_type))
James Smart6d368e52011-05-24 11:44:12 -040014396 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart6669f9b2009-10-02 15:16:45 -040014397 "2540 Ring %d handler: unexpected Rctl "
14398 "x%x Type x%x received\n",
14399 LPFC_ELS_RING,
14400 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
14401
14402 /* Free iocb created in lpfc_prep_seq */
14403 list_for_each_entry_safe(curr_iocb, next_iocb,
14404 &iocbq->list, list) {
14405 list_del_init(&curr_iocb->list);
14406 lpfc_sli_release_iocbq(phba, curr_iocb);
14407 }
14408 lpfc_sli_release_iocbq(phba, iocbq);
14409}
14410
James Smart4f774512009-05-22 14:52:35 -040014411/**
14412 * lpfc_sli4_handle_received_buffer - Handle received buffers from firmware
14413 * @phba: Pointer to HBA context object.
14414 *
14415 * This function is called with no lock held. This function processes all
14416 * the received buffers and gives it to upper layers when a received buffer
14417 * indicates that it is the final frame in the sequence. The interrupt
14418 * service routine processes received buffers at interrupt contexts and adds
14419 * received dma buffers to the rb_pend_list queue and signals the worker thread.
14420 * Worker thread calls lpfc_sli4_handle_received_buffer, which will call the
14421 * appropriate receive function when the final frame in a sequence is received.
14422 **/
James Smart4d9ab992009-10-02 15:16:39 -040014423void
14424lpfc_sli4_handle_received_buffer(struct lpfc_hba *phba,
14425 struct hbq_dmabuf *dmabuf)
James Smart4f774512009-05-22 14:52:35 -040014426{
James Smart4d9ab992009-10-02 15:16:39 -040014427 struct hbq_dmabuf *seq_dmabuf;
James Smart4f774512009-05-22 14:52:35 -040014428 struct fc_frame_header *fc_hdr;
14429 struct lpfc_vport *vport;
14430 uint32_t fcfi;
James Smart4f774512009-05-22 14:52:35 -040014431
James Smart4f774512009-05-22 14:52:35 -040014432 /* Process each received buffer */
James Smart4d9ab992009-10-02 15:16:39 -040014433 fc_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
14434 /* check to see if this a valid type of frame */
14435 if (lpfc_fc_frame_check(phba, fc_hdr)) {
14436 lpfc_in_buf_free(phba, &dmabuf->dbuf);
14437 return;
14438 }
James Smart7851fe22011-07-22 18:36:52 -040014439 if ((bf_get(lpfc_cqe_code,
14440 &dmabuf->cq_event.cqe.rcqe_cmpl) == CQE_CODE_RECEIVE_V1))
14441 fcfi = bf_get(lpfc_rcqe_fcf_id_v1,
14442 &dmabuf->cq_event.cqe.rcqe_cmpl);
14443 else
14444 fcfi = bf_get(lpfc_rcqe_fcf_id,
14445 &dmabuf->cq_event.cqe.rcqe_cmpl);
James Smart4d9ab992009-10-02 15:16:39 -040014446 vport = lpfc_fc_frame_to_vport(phba, fc_hdr, fcfi);
James Smartc8685952009-11-18 15:39:16 -050014447 if (!vport || !(vport->vpi_state & LPFC_VPI_REGISTERED)) {
James Smart4d9ab992009-10-02 15:16:39 -040014448 /* throw out the frame */
14449 lpfc_in_buf_free(phba, &dmabuf->dbuf);
14450 return;
14451 }
James Smart6669f9b2009-10-02 15:16:45 -040014452 /* Handle the basic abort sequence (BA_ABTS) event */
14453 if (fc_hdr->fh_r_ctl == FC_RCTL_BA_ABTS) {
14454 lpfc_sli4_handle_unsol_abort(vport, dmabuf);
14455 return;
14456 }
14457
James Smart4d9ab992009-10-02 15:16:39 -040014458 /* Link this frame */
14459 seq_dmabuf = lpfc_fc_frame_add(vport, dmabuf);
14460 if (!seq_dmabuf) {
14461 /* unable to add frame to vport - throw it out */
14462 lpfc_in_buf_free(phba, &dmabuf->dbuf);
14463 return;
14464 }
14465 /* If not last frame in sequence continue processing frames. */
James Smartdef9c7a2009-12-21 17:02:28 -050014466 if (!lpfc_seq_complete(seq_dmabuf))
James Smart4d9ab992009-10-02 15:16:39 -040014467 return;
James Smartdef9c7a2009-12-21 17:02:28 -050014468
James Smart6669f9b2009-10-02 15:16:45 -040014469 /* Send the complete sequence to the upper layer protocol */
14470 lpfc_sli4_send_seq_to_ulp(vport, seq_dmabuf);
James Smart4f774512009-05-22 14:52:35 -040014471}
James Smart6fb120a2009-05-22 14:52:59 -040014472
14473/**
14474 * lpfc_sli4_post_all_rpi_hdrs - Post the rpi header memory region to the port
14475 * @phba: pointer to lpfc hba data structure.
14476 *
14477 * This routine is invoked to post rpi header templates to the
14478 * HBA consistent with the SLI-4 interface spec. This routine
James Smart49198b32010-04-06 15:04:33 -040014479 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
14480 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
James Smart6fb120a2009-05-22 14:52:59 -040014481 *
14482 * This routine does not require any locks. It's usage is expected
14483 * to be driver load or reset recovery when the driver is
14484 * sequential.
14485 *
14486 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020014487 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -040014488 * -EIO - The mailbox failed to complete successfully.
James Smart6fb120a2009-05-22 14:52:59 -040014489 * When this error occurs, the driver is not guaranteed
14490 * to have any rpi regions posted to the device and
14491 * must either attempt to repost the regions or take a
14492 * fatal error.
14493 **/
14494int
14495lpfc_sli4_post_all_rpi_hdrs(struct lpfc_hba *phba)
14496{
14497 struct lpfc_rpi_hdr *rpi_page;
14498 uint32_t rc = 0;
James Smart6d368e52011-05-24 11:44:12 -040014499 uint16_t lrpi = 0;
James Smart6fb120a2009-05-22 14:52:59 -040014500
James Smart6d368e52011-05-24 11:44:12 -040014501 /* SLI4 ports that support extents do not require RPI headers. */
14502 if (!phba->sli4_hba.rpi_hdrs_in_use)
14503 goto exit;
14504 if (phba->sli4_hba.extents_in_use)
14505 return -EIO;
14506
James Smart6fb120a2009-05-22 14:52:59 -040014507 list_for_each_entry(rpi_page, &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
James Smart6d368e52011-05-24 11:44:12 -040014508 /*
14509 * Assign the rpi headers a physical rpi only if the driver
14510 * has not initialized those resources. A port reset only
14511 * needs the headers posted.
14512 */
14513 if (bf_get(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags) !=
14514 LPFC_RPI_RSRC_RDY)
14515 rpi_page->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
14516
James Smart6fb120a2009-05-22 14:52:59 -040014517 rc = lpfc_sli4_post_rpi_hdr(phba, rpi_page);
14518 if (rc != MBX_SUCCESS) {
14519 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14520 "2008 Error %d posting all rpi "
14521 "headers\n", rc);
14522 rc = -EIO;
14523 break;
14524 }
14525 }
14526
James Smart6d368e52011-05-24 11:44:12 -040014527 exit:
14528 bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags,
14529 LPFC_RPI_RSRC_RDY);
James Smart6fb120a2009-05-22 14:52:59 -040014530 return rc;
14531}
14532
14533/**
14534 * lpfc_sli4_post_rpi_hdr - Post an rpi header memory region to the port
14535 * @phba: pointer to lpfc hba data structure.
14536 * @rpi_page: pointer to the rpi memory region.
14537 *
14538 * This routine is invoked to post a single rpi header to the
14539 * HBA consistent with the SLI-4 interface spec. This memory region
14540 * maps up to 64 rpi context regions.
14541 *
14542 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020014543 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -040014544 * -ENOMEM - No available memory
14545 * -EIO - The mailbox failed to complete successfully.
James Smart6fb120a2009-05-22 14:52:59 -040014546 **/
14547int
14548lpfc_sli4_post_rpi_hdr(struct lpfc_hba *phba, struct lpfc_rpi_hdr *rpi_page)
14549{
14550 LPFC_MBOXQ_t *mboxq;
14551 struct lpfc_mbx_post_hdr_tmpl *hdr_tmpl;
14552 uint32_t rc = 0;
James Smart6fb120a2009-05-22 14:52:59 -040014553 uint32_t shdr_status, shdr_add_status;
14554 union lpfc_sli4_cfg_shdr *shdr;
14555
James Smart6d368e52011-05-24 11:44:12 -040014556 /* SLI4 ports that support extents do not require RPI headers. */
14557 if (!phba->sli4_hba.rpi_hdrs_in_use)
14558 return rc;
14559 if (phba->sli4_hba.extents_in_use)
14560 return -EIO;
14561
James Smart6fb120a2009-05-22 14:52:59 -040014562 /* The port is notified of the header region via a mailbox command. */
14563 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14564 if (!mboxq) {
14565 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14566 "2001 Unable to allocate memory for issuing "
14567 "SLI_CONFIG_SPECIAL mailbox command\n");
14568 return -ENOMEM;
14569 }
14570
14571 /* Post all rpi memory regions to the port. */
14572 hdr_tmpl = &mboxq->u.mqe.un.hdr_tmpl;
James Smart6fb120a2009-05-22 14:52:59 -040014573 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
14574 LPFC_MBOX_OPCODE_FCOE_POST_HDR_TEMPLATE,
14575 sizeof(struct lpfc_mbx_post_hdr_tmpl) -
James Smartfedd3b72011-02-16 12:39:24 -050014576 sizeof(struct lpfc_sli4_cfg_mhdr),
14577 LPFC_SLI4_MBX_EMBED);
James Smart6d368e52011-05-24 11:44:12 -040014578
14579
14580 /* Post the physical rpi to the port for this rpi header. */
James Smart6fb120a2009-05-22 14:52:59 -040014581 bf_set(lpfc_mbx_post_hdr_tmpl_rpi_offset, hdr_tmpl,
14582 rpi_page->start_rpi);
James Smart6d368e52011-05-24 11:44:12 -040014583 bf_set(lpfc_mbx_post_hdr_tmpl_page_cnt,
14584 hdr_tmpl, rpi_page->page_count);
14585
James Smart6fb120a2009-05-22 14:52:59 -040014586 hdr_tmpl->rpi_paddr_lo = putPaddrLow(rpi_page->dmabuf->phys);
14587 hdr_tmpl->rpi_paddr_hi = putPaddrHigh(rpi_page->dmabuf->phys);
James Smartf1126682009-06-10 17:22:44 -040014588 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
James Smart6fb120a2009-05-22 14:52:59 -040014589 shdr = (union lpfc_sli4_cfg_shdr *) &hdr_tmpl->header.cfg_shdr;
14590 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14591 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14592 if (rc != MBX_TIMEOUT)
14593 mempool_free(mboxq, phba->mbox_mem_pool);
14594 if (shdr_status || shdr_add_status || rc) {
14595 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14596 "2514 POST_RPI_HDR mailbox failed with "
14597 "status x%x add_status x%x, mbx status x%x\n",
14598 shdr_status, shdr_add_status, rc);
14599 rc = -ENXIO;
14600 }
14601 return rc;
14602}
14603
14604/**
14605 * lpfc_sli4_alloc_rpi - Get an available rpi in the device's range
14606 * @phba: pointer to lpfc hba data structure.
14607 *
14608 * This routine is invoked to post rpi header templates to the
14609 * HBA consistent with the SLI-4 interface spec. This routine
James Smart49198b32010-04-06 15:04:33 -040014610 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
14611 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
James Smart6fb120a2009-05-22 14:52:59 -040014612 *
14613 * Returns
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020014614 * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
James Smart6fb120a2009-05-22 14:52:59 -040014615 * LPFC_RPI_ALLOC_ERROR if no rpis are available.
14616 **/
14617int
14618lpfc_sli4_alloc_rpi(struct lpfc_hba *phba)
14619{
James Smart6d368e52011-05-24 11:44:12 -040014620 unsigned long rpi;
14621 uint16_t max_rpi, rpi_limit;
14622 uint16_t rpi_remaining, lrpi = 0;
James Smart6fb120a2009-05-22 14:52:59 -040014623 struct lpfc_rpi_hdr *rpi_hdr;
14624
14625 max_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
James Smart6fb120a2009-05-22 14:52:59 -040014626 rpi_limit = phba->sli4_hba.next_rpi;
14627
14628 /*
James Smart6d368e52011-05-24 11:44:12 -040014629 * Fetch the next logical rpi. Because this index is logical,
14630 * the driver starts at 0 each time.
James Smart6fb120a2009-05-22 14:52:59 -040014631 */
14632 spin_lock_irq(&phba->hbalock);
James Smart6d368e52011-05-24 11:44:12 -040014633 rpi = find_next_zero_bit(phba->sli4_hba.rpi_bmask, rpi_limit, 0);
14634 if (rpi >= rpi_limit)
James Smart6fb120a2009-05-22 14:52:59 -040014635 rpi = LPFC_RPI_ALLOC_ERROR;
14636 else {
14637 set_bit(rpi, phba->sli4_hba.rpi_bmask);
14638 phba->sli4_hba.max_cfg_param.rpi_used++;
14639 phba->sli4_hba.rpi_count++;
14640 }
14641
14642 /*
14643 * Don't try to allocate more rpi header regions if the device limit
James Smart6d368e52011-05-24 11:44:12 -040014644 * has been exhausted.
James Smart6fb120a2009-05-22 14:52:59 -040014645 */
14646 if ((rpi == LPFC_RPI_ALLOC_ERROR) &&
14647 (phba->sli4_hba.rpi_count >= max_rpi)) {
14648 spin_unlock_irq(&phba->hbalock);
14649 return rpi;
14650 }
14651
14652 /*
James Smart6d368e52011-05-24 11:44:12 -040014653 * RPI header postings are not required for SLI4 ports capable of
14654 * extents.
14655 */
14656 if (!phba->sli4_hba.rpi_hdrs_in_use) {
14657 spin_unlock_irq(&phba->hbalock);
14658 return rpi;
14659 }
14660
14661 /*
James Smart6fb120a2009-05-22 14:52:59 -040014662 * If the driver is running low on rpi resources, allocate another
14663 * page now. Note that the next_rpi value is used because
14664 * it represents how many are actually in use whereas max_rpi notes
14665 * how many are supported max by the device.
14666 */
James Smart6d368e52011-05-24 11:44:12 -040014667 rpi_remaining = phba->sli4_hba.next_rpi - phba->sli4_hba.rpi_count;
James Smart6fb120a2009-05-22 14:52:59 -040014668 spin_unlock_irq(&phba->hbalock);
14669 if (rpi_remaining < LPFC_RPI_LOW_WATER_MARK) {
14670 rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
14671 if (!rpi_hdr) {
14672 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14673 "2002 Error Could not grow rpi "
14674 "count\n");
14675 } else {
James Smart6d368e52011-05-24 11:44:12 -040014676 lrpi = rpi_hdr->start_rpi;
14677 rpi_hdr->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
James Smart6fb120a2009-05-22 14:52:59 -040014678 lpfc_sli4_post_rpi_hdr(phba, rpi_hdr);
14679 }
14680 }
14681
14682 return rpi;
14683}
14684
14685/**
14686 * lpfc_sli4_free_rpi - Release an rpi for reuse.
14687 * @phba: pointer to lpfc hba data structure.
14688 *
14689 * This routine is invoked to release an rpi to the pool of
14690 * available rpis maintained by the driver.
14691 **/
14692void
James Smartd7c47992010-06-08 18:31:54 -040014693__lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
14694{
14695 if (test_and_clear_bit(rpi, phba->sli4_hba.rpi_bmask)) {
14696 phba->sli4_hba.rpi_count--;
14697 phba->sli4_hba.max_cfg_param.rpi_used--;
14698 }
14699}
14700
14701/**
14702 * lpfc_sli4_free_rpi - Release an rpi for reuse.
14703 * @phba: pointer to lpfc hba data structure.
14704 *
14705 * This routine is invoked to release an rpi to the pool of
14706 * available rpis maintained by the driver.
14707 **/
14708void
James Smart6fb120a2009-05-22 14:52:59 -040014709lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
14710{
14711 spin_lock_irq(&phba->hbalock);
James Smartd7c47992010-06-08 18:31:54 -040014712 __lpfc_sli4_free_rpi(phba, rpi);
James Smart6fb120a2009-05-22 14:52:59 -040014713 spin_unlock_irq(&phba->hbalock);
14714}
14715
14716/**
14717 * lpfc_sli4_remove_rpis - Remove the rpi bitmask region
14718 * @phba: pointer to lpfc hba data structure.
14719 *
14720 * This routine is invoked to remove the memory region that
14721 * provided rpi via a bitmask.
14722 **/
14723void
14724lpfc_sli4_remove_rpis(struct lpfc_hba *phba)
14725{
14726 kfree(phba->sli4_hba.rpi_bmask);
James Smart6d368e52011-05-24 11:44:12 -040014727 kfree(phba->sli4_hba.rpi_ids);
14728 bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
James Smart6fb120a2009-05-22 14:52:59 -040014729}
14730
14731/**
14732 * lpfc_sli4_resume_rpi - Remove the rpi bitmask region
14733 * @phba: pointer to lpfc hba data structure.
14734 *
14735 * This routine is invoked to remove the memory region that
14736 * provided rpi via a bitmask.
14737 **/
14738int
14739lpfc_sli4_resume_rpi(struct lpfc_nodelist *ndlp)
14740{
14741 LPFC_MBOXQ_t *mboxq;
14742 struct lpfc_hba *phba = ndlp->phba;
14743 int rc;
14744
14745 /* The port is notified of the header region via a mailbox command. */
14746 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14747 if (!mboxq)
14748 return -ENOMEM;
14749
14750 /* Post all rpi memory regions to the port. */
14751 lpfc_resume_rpi(mboxq, ndlp);
14752 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
14753 if (rc == MBX_NOT_FINISHED) {
14754 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14755 "2010 Resume RPI Mailbox failed "
14756 "status %d, mbxStatus x%x\n", rc,
14757 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
14758 mempool_free(mboxq, phba->mbox_mem_pool);
14759 return -EIO;
14760 }
14761 return 0;
14762}
14763
14764/**
14765 * lpfc_sli4_init_vpi - Initialize a vpi with the port
James Smart76a95d72010-11-20 23:11:48 -050014766 * @vport: Pointer to the vport for which the vpi is being initialized
James Smart6fb120a2009-05-22 14:52:59 -040014767 *
James Smart76a95d72010-11-20 23:11:48 -050014768 * This routine is invoked to activate a vpi with the port.
James Smart6fb120a2009-05-22 14:52:59 -040014769 *
14770 * Returns:
14771 * 0 success
14772 * -Evalue otherwise
14773 **/
14774int
James Smart76a95d72010-11-20 23:11:48 -050014775lpfc_sli4_init_vpi(struct lpfc_vport *vport)
James Smart6fb120a2009-05-22 14:52:59 -040014776{
14777 LPFC_MBOXQ_t *mboxq;
14778 int rc = 0;
James Smart6a9c52c2009-10-02 15:16:51 -040014779 int retval = MBX_SUCCESS;
James Smart6fb120a2009-05-22 14:52:59 -040014780 uint32_t mbox_tmo;
James Smart76a95d72010-11-20 23:11:48 -050014781 struct lpfc_hba *phba = vport->phba;
James Smart6fb120a2009-05-22 14:52:59 -040014782 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14783 if (!mboxq)
14784 return -ENOMEM;
James Smart76a95d72010-11-20 23:11:48 -050014785 lpfc_init_vpi(phba, mboxq, vport->vpi);
James Smarta183a152011-10-10 21:32:43 -040014786 mbox_tmo = lpfc_mbox_tmo_val(phba, mboxq);
James Smart6fb120a2009-05-22 14:52:59 -040014787 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
James Smart6fb120a2009-05-22 14:52:59 -040014788 if (rc != MBX_SUCCESS) {
James Smart76a95d72010-11-20 23:11:48 -050014789 lpfc_printf_vlog(vport, KERN_ERR, LOG_SLI,
James Smart6fb120a2009-05-22 14:52:59 -040014790 "2022 INIT VPI Mailbox failed "
14791 "status %d, mbxStatus x%x\n", rc,
14792 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
James Smart6a9c52c2009-10-02 15:16:51 -040014793 retval = -EIO;
James Smart6fb120a2009-05-22 14:52:59 -040014794 }
James Smart6a9c52c2009-10-02 15:16:51 -040014795 if (rc != MBX_TIMEOUT)
James Smart76a95d72010-11-20 23:11:48 -050014796 mempool_free(mboxq, vport->phba->mbox_mem_pool);
James Smart6a9c52c2009-10-02 15:16:51 -040014797
14798 return retval;
James Smart6fb120a2009-05-22 14:52:59 -040014799}
14800
14801/**
14802 * lpfc_mbx_cmpl_add_fcf_record - add fcf mbox completion handler.
14803 * @phba: pointer to lpfc hba data structure.
14804 * @mboxq: Pointer to mailbox object.
14805 *
14806 * This routine is invoked to manually add a single FCF record. The caller
14807 * must pass a completely initialized FCF_Record. This routine takes
14808 * care of the nonembedded mailbox operations.
14809 **/
14810static void
14811lpfc_mbx_cmpl_add_fcf_record(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
14812{
14813 void *virt_addr;
14814 union lpfc_sli4_cfg_shdr *shdr;
14815 uint32_t shdr_status, shdr_add_status;
14816
14817 virt_addr = mboxq->sge_array->addr[0];
14818 /* The IOCTL status is embedded in the mailbox subheader. */
14819 shdr = (union lpfc_sli4_cfg_shdr *) virt_addr;
14820 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14821 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14822
14823 if ((shdr_status || shdr_add_status) &&
14824 (shdr_status != STATUS_FCF_IN_USE))
14825 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14826 "2558 ADD_FCF_RECORD mailbox failed with "
14827 "status x%x add_status x%x\n",
14828 shdr_status, shdr_add_status);
14829
14830 lpfc_sli4_mbox_cmd_free(phba, mboxq);
14831}
14832
14833/**
14834 * lpfc_sli4_add_fcf_record - Manually add an FCF Record.
14835 * @phba: pointer to lpfc hba data structure.
14836 * @fcf_record: pointer to the initialized fcf record to add.
14837 *
14838 * This routine is invoked to manually add a single FCF record. The caller
14839 * must pass a completely initialized FCF_Record. This routine takes
14840 * care of the nonembedded mailbox operations.
14841 **/
14842int
14843lpfc_sli4_add_fcf_record(struct lpfc_hba *phba, struct fcf_record *fcf_record)
14844{
14845 int rc = 0;
14846 LPFC_MBOXQ_t *mboxq;
14847 uint8_t *bytep;
14848 void *virt_addr;
14849 dma_addr_t phys_addr;
14850 struct lpfc_mbx_sge sge;
14851 uint32_t alloc_len, req_len;
14852 uint32_t fcfindex;
14853
14854 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14855 if (!mboxq) {
14856 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14857 "2009 Failed to allocate mbox for ADD_FCF cmd\n");
14858 return -ENOMEM;
14859 }
14860
14861 req_len = sizeof(struct fcf_record) + sizeof(union lpfc_sli4_cfg_shdr) +
14862 sizeof(uint32_t);
14863
14864 /* Allocate DMA memory and set up the non-embedded mailbox command */
14865 alloc_len = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
14866 LPFC_MBOX_OPCODE_FCOE_ADD_FCF,
14867 req_len, LPFC_SLI4_MBX_NEMBED);
14868 if (alloc_len < req_len) {
14869 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14870 "2523 Allocated DMA memory size (x%x) is "
14871 "less than the requested DMA memory "
14872 "size (x%x)\n", alloc_len, req_len);
14873 lpfc_sli4_mbox_cmd_free(phba, mboxq);
14874 return -ENOMEM;
14875 }
14876
14877 /*
14878 * Get the first SGE entry from the non-embedded DMA memory. This
14879 * routine only uses a single SGE.
14880 */
14881 lpfc_sli4_mbx_sge_get(mboxq, 0, &sge);
14882 phys_addr = getPaddr(sge.pa_hi, sge.pa_lo);
James Smart6fb120a2009-05-22 14:52:59 -040014883 virt_addr = mboxq->sge_array->addr[0];
14884 /*
14885 * Configure the FCF record for FCFI 0. This is the driver's
14886 * hardcoded default and gets used in nonFIP mode.
14887 */
14888 fcfindex = bf_get(lpfc_fcf_record_fcf_index, fcf_record);
14889 bytep = virt_addr + sizeof(union lpfc_sli4_cfg_shdr);
14890 lpfc_sli_pcimem_bcopy(&fcfindex, bytep, sizeof(uint32_t));
14891
14892 /*
14893 * Copy the fcf_index and the FCF Record Data. The data starts after
14894 * the FCoE header plus word10. The data copy needs to be endian
14895 * correct.
14896 */
14897 bytep += sizeof(uint32_t);
14898 lpfc_sli_pcimem_bcopy(fcf_record, bytep, sizeof(struct fcf_record));
14899 mboxq->vport = phba->pport;
14900 mboxq->mbox_cmpl = lpfc_mbx_cmpl_add_fcf_record;
14901 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
14902 if (rc == MBX_NOT_FINISHED) {
14903 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14904 "2515 ADD_FCF_RECORD mailbox failed with "
14905 "status 0x%x\n", rc);
14906 lpfc_sli4_mbox_cmd_free(phba, mboxq);
14907 rc = -EIO;
14908 } else
14909 rc = 0;
14910
14911 return rc;
14912}
14913
14914/**
14915 * lpfc_sli4_build_dflt_fcf_record - Build the driver's default FCF Record.
14916 * @phba: pointer to lpfc hba data structure.
14917 * @fcf_record: pointer to the fcf record to write the default data.
14918 * @fcf_index: FCF table entry index.
14919 *
14920 * This routine is invoked to build the driver's default FCF record. The
14921 * values used are hardcoded. This routine handles memory initialization.
14922 *
14923 **/
14924void
14925lpfc_sli4_build_dflt_fcf_record(struct lpfc_hba *phba,
14926 struct fcf_record *fcf_record,
14927 uint16_t fcf_index)
14928{
14929 memset(fcf_record, 0, sizeof(struct fcf_record));
14930 fcf_record->max_rcv_size = LPFC_FCOE_MAX_RCV_SIZE;
14931 fcf_record->fka_adv_period = LPFC_FCOE_FKA_ADV_PER;
14932 fcf_record->fip_priority = LPFC_FCOE_FIP_PRIORITY;
14933 bf_set(lpfc_fcf_record_mac_0, fcf_record, phba->fc_map[0]);
14934 bf_set(lpfc_fcf_record_mac_1, fcf_record, phba->fc_map[1]);
14935 bf_set(lpfc_fcf_record_mac_2, fcf_record, phba->fc_map[2]);
14936 bf_set(lpfc_fcf_record_mac_3, fcf_record, LPFC_FCOE_FCF_MAC3);
14937 bf_set(lpfc_fcf_record_mac_4, fcf_record, LPFC_FCOE_FCF_MAC4);
14938 bf_set(lpfc_fcf_record_mac_5, fcf_record, LPFC_FCOE_FCF_MAC5);
14939 bf_set(lpfc_fcf_record_fc_map_0, fcf_record, phba->fc_map[0]);
14940 bf_set(lpfc_fcf_record_fc_map_1, fcf_record, phba->fc_map[1]);
14941 bf_set(lpfc_fcf_record_fc_map_2, fcf_record, phba->fc_map[2]);
14942 bf_set(lpfc_fcf_record_fcf_valid, fcf_record, 1);
James Smart0c287582009-06-10 17:22:56 -040014943 bf_set(lpfc_fcf_record_fcf_avail, fcf_record, 1);
James Smart6fb120a2009-05-22 14:52:59 -040014944 bf_set(lpfc_fcf_record_fcf_index, fcf_record, fcf_index);
14945 bf_set(lpfc_fcf_record_mac_addr_prov, fcf_record,
14946 LPFC_FCF_FPMA | LPFC_FCF_SPMA);
14947 /* Set the VLAN bit map */
14948 if (phba->valid_vlan) {
14949 fcf_record->vlan_bitmap[phba->vlan_id / 8]
14950 = 1 << (phba->vlan_id % 8);
14951 }
14952}
14953
14954/**
James Smart0c9ab6f2010-02-26 14:15:57 -050014955 * lpfc_sli4_fcf_scan_read_fcf_rec - Read hba fcf record for fcf scan.
James Smart6fb120a2009-05-22 14:52:59 -040014956 * @phba: pointer to lpfc hba data structure.
14957 * @fcf_index: FCF table entry offset.
14958 *
James Smart0c9ab6f2010-02-26 14:15:57 -050014959 * This routine is invoked to scan the entire FCF table by reading FCF
14960 * record and processing it one at a time starting from the @fcf_index
14961 * for initial FCF discovery or fast FCF failover rediscovery.
14962 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030014963 * Return 0 if the mailbox command is submitted successfully, none 0
James Smart0c9ab6f2010-02-26 14:15:57 -050014964 * otherwise.
James Smart6fb120a2009-05-22 14:52:59 -040014965 **/
14966int
James Smart0c9ab6f2010-02-26 14:15:57 -050014967lpfc_sli4_fcf_scan_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
James Smart6fb120a2009-05-22 14:52:59 -040014968{
14969 int rc = 0, error;
14970 LPFC_MBOXQ_t *mboxq;
James Smart6fb120a2009-05-22 14:52:59 -040014971
James Smart32b97932009-07-19 10:01:21 -040014972 phba->fcoe_eventtag_at_fcf_scan = phba->fcoe_eventtag;
James Smart6fb120a2009-05-22 14:52:59 -040014973 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14974 if (!mboxq) {
14975 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14976 "2000 Failed to allocate mbox for "
14977 "READ_FCF cmd\n");
James Smart4d9ab992009-10-02 15:16:39 -040014978 error = -ENOMEM;
James Smart0c9ab6f2010-02-26 14:15:57 -050014979 goto fail_fcf_scan;
James Smart6fb120a2009-05-22 14:52:59 -040014980 }
James Smartecfd03c2010-02-12 14:41:27 -050014981 /* Construct the read FCF record mailbox command */
James Smart0c9ab6f2010-02-26 14:15:57 -050014982 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
James Smartecfd03c2010-02-12 14:41:27 -050014983 if (rc) {
14984 error = -EINVAL;
James Smart0c9ab6f2010-02-26 14:15:57 -050014985 goto fail_fcf_scan;
James Smart6fb120a2009-05-22 14:52:59 -040014986 }
James Smartecfd03c2010-02-12 14:41:27 -050014987 /* Issue the mailbox command asynchronously */
James Smart6fb120a2009-05-22 14:52:59 -040014988 mboxq->vport = phba->pport;
James Smart0c9ab6f2010-02-26 14:15:57 -050014989 mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_scan_read_fcf_rec;
James Smarta93ff372010-10-22 11:06:08 -040014990
14991 spin_lock_irq(&phba->hbalock);
14992 phba->hba_flag |= FCF_TS_INPROG;
14993 spin_unlock_irq(&phba->hbalock);
14994
James Smart6fb120a2009-05-22 14:52:59 -040014995 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
James Smartecfd03c2010-02-12 14:41:27 -050014996 if (rc == MBX_NOT_FINISHED)
James Smart6fb120a2009-05-22 14:52:59 -040014997 error = -EIO;
James Smartecfd03c2010-02-12 14:41:27 -050014998 else {
James Smart38b92ef2010-08-04 16:11:39 -040014999 /* Reset eligible FCF count for new scan */
15000 if (fcf_index == LPFC_FCOE_FCF_GET_FIRST)
James Smart999d8132010-03-15 11:24:56 -040015001 phba->fcf.eligible_fcf_cnt = 0;
James Smart6fb120a2009-05-22 14:52:59 -040015002 error = 0;
James Smart32b97932009-07-19 10:01:21 -040015003 }
James Smart0c9ab6f2010-02-26 14:15:57 -050015004fail_fcf_scan:
James Smart4d9ab992009-10-02 15:16:39 -040015005 if (error) {
15006 if (mboxq)
15007 lpfc_sli4_mbox_cmd_free(phba, mboxq);
James Smarta93ff372010-10-22 11:06:08 -040015008 /* FCF scan failed, clear FCF_TS_INPROG flag */
James Smart4d9ab992009-10-02 15:16:39 -040015009 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -040015010 phba->hba_flag &= ~FCF_TS_INPROG;
James Smart4d9ab992009-10-02 15:16:39 -040015011 spin_unlock_irq(&phba->hbalock);
15012 }
James Smart6fb120a2009-05-22 14:52:59 -040015013 return error;
15014}
James Smarta0c87cb2009-07-19 10:01:10 -040015015
15016/**
James Smarta93ff372010-10-22 11:06:08 -040015017 * lpfc_sli4_fcf_rr_read_fcf_rec - Read hba fcf record for roundrobin fcf.
James Smart0c9ab6f2010-02-26 14:15:57 -050015018 * @phba: pointer to lpfc hba data structure.
15019 * @fcf_index: FCF table entry offset.
15020 *
15021 * This routine is invoked to read an FCF record indicated by @fcf_index
James Smarta93ff372010-10-22 11:06:08 -040015022 * and to use it for FLOGI roundrobin FCF failover.
James Smart0c9ab6f2010-02-26 14:15:57 -050015023 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030015024 * Return 0 if the mailbox command is submitted successfully, none 0
James Smart0c9ab6f2010-02-26 14:15:57 -050015025 * otherwise.
15026 **/
15027int
15028lpfc_sli4_fcf_rr_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
15029{
15030 int rc = 0, error;
15031 LPFC_MBOXQ_t *mboxq;
15032
15033 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15034 if (!mboxq) {
15035 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
15036 "2763 Failed to allocate mbox for "
15037 "READ_FCF cmd\n");
15038 error = -ENOMEM;
15039 goto fail_fcf_read;
15040 }
15041 /* Construct the read FCF record mailbox command */
15042 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
15043 if (rc) {
15044 error = -EINVAL;
15045 goto fail_fcf_read;
15046 }
15047 /* Issue the mailbox command asynchronously */
15048 mboxq->vport = phba->pport;
15049 mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_rr_read_fcf_rec;
15050 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
15051 if (rc == MBX_NOT_FINISHED)
15052 error = -EIO;
15053 else
15054 error = 0;
15055
15056fail_fcf_read:
15057 if (error && mboxq)
15058 lpfc_sli4_mbox_cmd_free(phba, mboxq);
15059 return error;
15060}
15061
15062/**
15063 * lpfc_sli4_read_fcf_rec - Read hba fcf record for update eligible fcf bmask.
15064 * @phba: pointer to lpfc hba data structure.
15065 * @fcf_index: FCF table entry offset.
15066 *
15067 * This routine is invoked to read an FCF record indicated by @fcf_index to
James Smarta93ff372010-10-22 11:06:08 -040015068 * determine whether it's eligible for FLOGI roundrobin failover list.
James Smart0c9ab6f2010-02-26 14:15:57 -050015069 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030015070 * Return 0 if the mailbox command is submitted successfully, none 0
James Smart0c9ab6f2010-02-26 14:15:57 -050015071 * otherwise.
15072 **/
15073int
15074lpfc_sli4_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
15075{
15076 int rc = 0, error;
15077 LPFC_MBOXQ_t *mboxq;
15078
15079 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15080 if (!mboxq) {
15081 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
15082 "2758 Failed to allocate mbox for "
15083 "READ_FCF cmd\n");
15084 error = -ENOMEM;
15085 goto fail_fcf_read;
15086 }
15087 /* Construct the read FCF record mailbox command */
15088 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
15089 if (rc) {
15090 error = -EINVAL;
15091 goto fail_fcf_read;
15092 }
15093 /* Issue the mailbox command asynchronously */
15094 mboxq->vport = phba->pport;
15095 mboxq->mbox_cmpl = lpfc_mbx_cmpl_read_fcf_rec;
15096 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
15097 if (rc == MBX_NOT_FINISHED)
15098 error = -EIO;
15099 else
15100 error = 0;
15101
15102fail_fcf_read:
15103 if (error && mboxq)
15104 lpfc_sli4_mbox_cmd_free(phba, mboxq);
15105 return error;
15106}
15107
15108/**
James Smart7d791df2011-07-22 18:37:52 -040015109 * lpfc_check_next_fcf_pri
15110 * phba pointer to the lpfc_hba struct for this port.
15111 * This routine is called from the lpfc_sli4_fcf_rr_next_index_get
15112 * routine when the rr_bmask is empty. The FCF indecies are put into the
15113 * rr_bmask based on their priority level. Starting from the highest priority
15114 * to the lowest. The most likely FCF candidate will be in the highest
15115 * priority group. When this routine is called it searches the fcf_pri list for
15116 * next lowest priority group and repopulates the rr_bmask with only those
15117 * fcf_indexes.
15118 * returns:
15119 * 1=success 0=failure
15120 **/
15121int
15122lpfc_check_next_fcf_pri_level(struct lpfc_hba *phba)
15123{
15124 uint16_t next_fcf_pri;
15125 uint16_t last_index;
15126 struct lpfc_fcf_pri *fcf_pri;
15127 int rc;
15128 int ret = 0;
15129
15130 last_index = find_first_bit(phba->fcf.fcf_rr_bmask,
15131 LPFC_SLI4_FCF_TBL_INDX_MAX);
15132 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
15133 "3060 Last IDX %d\n", last_index);
15134 if (list_empty(&phba->fcf.fcf_pri_list)) {
15135 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
15136 "3061 Last IDX %d\n", last_index);
15137 return 0; /* Empty rr list */
15138 }
15139 next_fcf_pri = 0;
15140 /*
15141 * Clear the rr_bmask and set all of the bits that are at this
15142 * priority.
15143 */
15144 memset(phba->fcf.fcf_rr_bmask, 0,
15145 sizeof(*phba->fcf.fcf_rr_bmask));
15146 spin_lock_irq(&phba->hbalock);
15147 list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
15148 if (fcf_pri->fcf_rec.flag & LPFC_FCF_FLOGI_FAILED)
15149 continue;
15150 /*
15151 * the 1st priority that has not FLOGI failed
15152 * will be the highest.
15153 */
15154 if (!next_fcf_pri)
15155 next_fcf_pri = fcf_pri->fcf_rec.priority;
15156 spin_unlock_irq(&phba->hbalock);
15157 if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
15158 rc = lpfc_sli4_fcf_rr_index_set(phba,
15159 fcf_pri->fcf_rec.fcf_index);
15160 if (rc)
15161 return 0;
15162 }
15163 spin_lock_irq(&phba->hbalock);
15164 }
15165 /*
15166 * if next_fcf_pri was not set above and the list is not empty then
15167 * we have failed flogis on all of them. So reset flogi failed
15168 * and start at the begining.
15169 */
15170 if (!next_fcf_pri && !list_empty(&phba->fcf.fcf_pri_list)) {
15171 list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
15172 fcf_pri->fcf_rec.flag &= ~LPFC_FCF_FLOGI_FAILED;
15173 /*
15174 * the 1st priority that has not FLOGI failed
15175 * will be the highest.
15176 */
15177 if (!next_fcf_pri)
15178 next_fcf_pri = fcf_pri->fcf_rec.priority;
15179 spin_unlock_irq(&phba->hbalock);
15180 if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
15181 rc = lpfc_sli4_fcf_rr_index_set(phba,
15182 fcf_pri->fcf_rec.fcf_index);
15183 if (rc)
15184 return 0;
15185 }
15186 spin_lock_irq(&phba->hbalock);
15187 }
15188 } else
15189 ret = 1;
15190 spin_unlock_irq(&phba->hbalock);
15191
15192 return ret;
15193}
15194/**
James Smart0c9ab6f2010-02-26 14:15:57 -050015195 * lpfc_sli4_fcf_rr_next_index_get - Get next eligible fcf record index
15196 * @phba: pointer to lpfc hba data structure.
15197 *
15198 * This routine is to get the next eligible FCF record index in a round
15199 * robin fashion. If the next eligible FCF record index equals to the
James Smarta93ff372010-10-22 11:06:08 -040015200 * initial roundrobin FCF record index, LPFC_FCOE_FCF_NEXT_NONE (0xFFFF)
James Smart0c9ab6f2010-02-26 14:15:57 -050015201 * shall be returned, otherwise, the next eligible FCF record's index
15202 * shall be returned.
15203 **/
15204uint16_t
15205lpfc_sli4_fcf_rr_next_index_get(struct lpfc_hba *phba)
15206{
15207 uint16_t next_fcf_index;
15208
James Smart3804dc82010-07-14 15:31:37 -040015209 /* Search start from next bit of currently registered FCF index */
James Smart7d791df2011-07-22 18:37:52 -040015210next_priority:
James Smart3804dc82010-07-14 15:31:37 -040015211 next_fcf_index = (phba->fcf.current_rec.fcf_indx + 1) %
15212 LPFC_SLI4_FCF_TBL_INDX_MAX;
James Smart0c9ab6f2010-02-26 14:15:57 -050015213 next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
15214 LPFC_SLI4_FCF_TBL_INDX_MAX,
James Smart3804dc82010-07-14 15:31:37 -040015215 next_fcf_index);
15216
James Smart0c9ab6f2010-02-26 14:15:57 -050015217 /* Wrap around condition on phba->fcf.fcf_rr_bmask */
James Smart7d791df2011-07-22 18:37:52 -040015218 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
15219 /*
15220 * If we have wrapped then we need to clear the bits that
15221 * have been tested so that we can detect when we should
15222 * change the priority level.
15223 */
James Smart0c9ab6f2010-02-26 14:15:57 -050015224 next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
15225 LPFC_SLI4_FCF_TBL_INDX_MAX, 0);
James Smart7d791df2011-07-22 18:37:52 -040015226 }
15227
James Smart0c9ab6f2010-02-26 14:15:57 -050015228
James Smart3804dc82010-07-14 15:31:37 -040015229 /* Check roundrobin failover list empty condition */
James Smart7d791df2011-07-22 18:37:52 -040015230 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX ||
15231 next_fcf_index == phba->fcf.current_rec.fcf_indx) {
15232 /*
15233 * If next fcf index is not found check if there are lower
15234 * Priority level fcf's in the fcf_priority list.
15235 * Set up the rr_bmask with all of the avaiable fcf bits
15236 * at that level and continue the selection process.
15237 */
15238 if (lpfc_check_next_fcf_pri_level(phba))
15239 goto next_priority;
James Smart3804dc82010-07-14 15:31:37 -040015240 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
15241 "2844 No roundrobin failover FCF available\n");
James Smart7d791df2011-07-22 18:37:52 -040015242 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX)
15243 return LPFC_FCOE_FCF_NEXT_NONE;
15244 else {
15245 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
15246 "3063 Only FCF available idx %d, flag %x\n",
15247 next_fcf_index,
15248 phba->fcf.fcf_pri[next_fcf_index].fcf_rec.flag);
15249 return next_fcf_index;
15250 }
James Smart3804dc82010-07-14 15:31:37 -040015251 }
15252
James Smart7d791df2011-07-22 18:37:52 -040015253 if (next_fcf_index < LPFC_SLI4_FCF_TBL_INDX_MAX &&
15254 phba->fcf.fcf_pri[next_fcf_index].fcf_rec.flag &
15255 LPFC_FCF_FLOGI_FAILED)
15256 goto next_priority;
15257
James Smart3804dc82010-07-14 15:31:37 -040015258 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040015259 "2845 Get next roundrobin failover FCF (x%x)\n",
15260 next_fcf_index);
15261
James Smart0c9ab6f2010-02-26 14:15:57 -050015262 return next_fcf_index;
15263}
15264
15265/**
15266 * lpfc_sli4_fcf_rr_index_set - Set bmask with eligible fcf record index
15267 * @phba: pointer to lpfc hba data structure.
15268 *
15269 * This routine sets the FCF record index in to the eligible bmask for
James Smarta93ff372010-10-22 11:06:08 -040015270 * roundrobin failover search. It checks to make sure that the index
James Smart0c9ab6f2010-02-26 14:15:57 -050015271 * does not go beyond the range of the driver allocated bmask dimension
15272 * before setting the bit.
15273 *
15274 * Returns 0 if the index bit successfully set, otherwise, it returns
15275 * -EINVAL.
15276 **/
15277int
15278lpfc_sli4_fcf_rr_index_set(struct lpfc_hba *phba, uint16_t fcf_index)
15279{
15280 if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
15281 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040015282 "2610 FCF (x%x) reached driver's book "
15283 "keeping dimension:x%x\n",
James Smart0c9ab6f2010-02-26 14:15:57 -050015284 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
15285 return -EINVAL;
15286 }
15287 /* Set the eligible FCF record index bmask */
15288 set_bit(fcf_index, phba->fcf.fcf_rr_bmask);
15289
James Smart3804dc82010-07-14 15:31:37 -040015290 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040015291 "2790 Set FCF (x%x) to roundrobin FCF failover "
James Smart3804dc82010-07-14 15:31:37 -040015292 "bmask\n", fcf_index);
15293
James Smart0c9ab6f2010-02-26 14:15:57 -050015294 return 0;
15295}
15296
15297/**
James Smart3804dc82010-07-14 15:31:37 -040015298 * lpfc_sli4_fcf_rr_index_clear - Clear bmask from eligible fcf record index
James Smart0c9ab6f2010-02-26 14:15:57 -050015299 * @phba: pointer to lpfc hba data structure.
15300 *
15301 * This routine clears the FCF record index from the eligible bmask for
James Smarta93ff372010-10-22 11:06:08 -040015302 * roundrobin failover search. It checks to make sure that the index
James Smart0c9ab6f2010-02-26 14:15:57 -050015303 * does not go beyond the range of the driver allocated bmask dimension
15304 * before clearing the bit.
15305 **/
15306void
15307lpfc_sli4_fcf_rr_index_clear(struct lpfc_hba *phba, uint16_t fcf_index)
15308{
James Smart7d791df2011-07-22 18:37:52 -040015309 struct lpfc_fcf_pri *fcf_pri;
James Smart0c9ab6f2010-02-26 14:15:57 -050015310 if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
15311 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040015312 "2762 FCF (x%x) reached driver's book "
15313 "keeping dimension:x%x\n",
James Smart0c9ab6f2010-02-26 14:15:57 -050015314 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
15315 return;
15316 }
15317 /* Clear the eligible FCF record index bmask */
James Smart7d791df2011-07-22 18:37:52 -040015318 spin_lock_irq(&phba->hbalock);
15319 list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
15320 if (fcf_pri->fcf_rec.fcf_index == fcf_index) {
15321 list_del_init(&fcf_pri->list);
15322 break;
15323 }
15324 }
15325 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -050015326 clear_bit(fcf_index, phba->fcf.fcf_rr_bmask);
James Smart3804dc82010-07-14 15:31:37 -040015327
15328 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040015329 "2791 Clear FCF (x%x) from roundrobin failover "
James Smart3804dc82010-07-14 15:31:37 -040015330 "bmask\n", fcf_index);
James Smart0c9ab6f2010-02-26 14:15:57 -050015331}
15332
15333/**
James Smartecfd03c2010-02-12 14:41:27 -050015334 * lpfc_mbx_cmpl_redisc_fcf_table - completion routine for rediscover FCF table
15335 * @phba: pointer to lpfc hba data structure.
15336 *
15337 * This routine is the completion routine for the rediscover FCF table mailbox
15338 * command. If the mailbox command returned failure, it will try to stop the
15339 * FCF rediscover wait timer.
15340 **/
15341void
15342lpfc_mbx_cmpl_redisc_fcf_table(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
15343{
15344 struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
15345 uint32_t shdr_status, shdr_add_status;
15346
15347 redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
15348
15349 shdr_status = bf_get(lpfc_mbox_hdr_status,
15350 &redisc_fcf->header.cfg_shdr.response);
15351 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
15352 &redisc_fcf->header.cfg_shdr.response);
15353 if (shdr_status || shdr_add_status) {
James Smart0c9ab6f2010-02-26 14:15:57 -050015354 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smartecfd03c2010-02-12 14:41:27 -050015355 "2746 Requesting for FCF rediscovery failed "
15356 "status x%x add_status x%x\n",
15357 shdr_status, shdr_add_status);
James Smart0c9ab6f2010-02-26 14:15:57 -050015358 if (phba->fcf.fcf_flag & FCF_ACVL_DISC) {
James Smartfc2b9892010-02-26 14:15:29 -050015359 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -050015360 phba->fcf.fcf_flag &= ~FCF_ACVL_DISC;
James Smartfc2b9892010-02-26 14:15:29 -050015361 spin_unlock_irq(&phba->hbalock);
15362 /*
15363 * CVL event triggered FCF rediscover request failed,
15364 * last resort to re-try current registered FCF entry.
15365 */
15366 lpfc_retry_pport_discovery(phba);
15367 } else {
15368 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -050015369 phba->fcf.fcf_flag &= ~FCF_DEAD_DISC;
James Smartfc2b9892010-02-26 14:15:29 -050015370 spin_unlock_irq(&phba->hbalock);
15371 /*
15372 * DEAD FCF event triggered FCF rediscover request
15373 * failed, last resort to fail over as a link down
15374 * to FCF registration.
15375 */
15376 lpfc_sli4_fcf_dead_failthrough(phba);
15377 }
James Smart0c9ab6f2010-02-26 14:15:57 -050015378 } else {
15379 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040015380 "2775 Start FCF rediscover quiescent timer\n");
James Smartecfd03c2010-02-12 14:41:27 -050015381 /*
15382 * Start FCF rediscovery wait timer for pending FCF
15383 * before rescan FCF record table.
15384 */
15385 lpfc_fcf_redisc_wait_start_timer(phba);
James Smart0c9ab6f2010-02-26 14:15:57 -050015386 }
James Smartecfd03c2010-02-12 14:41:27 -050015387
15388 mempool_free(mbox, phba->mbox_mem_pool);
15389}
15390
15391/**
James Smart3804dc82010-07-14 15:31:37 -040015392 * lpfc_sli4_redisc_fcf_table - Request to rediscover entire FCF table by port.
James Smartecfd03c2010-02-12 14:41:27 -050015393 * @phba: pointer to lpfc hba data structure.
15394 *
15395 * This routine is invoked to request for rediscovery of the entire FCF table
15396 * by the port.
15397 **/
15398int
15399lpfc_sli4_redisc_fcf_table(struct lpfc_hba *phba)
15400{
15401 LPFC_MBOXQ_t *mbox;
15402 struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
15403 int rc, length;
15404
James Smart0c9ab6f2010-02-26 14:15:57 -050015405 /* Cancel retry delay timers to all vports before FCF rediscover */
15406 lpfc_cancel_all_vport_retry_delay_timer(phba);
15407
James Smartecfd03c2010-02-12 14:41:27 -050015408 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15409 if (!mbox) {
15410 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15411 "2745 Failed to allocate mbox for "
15412 "requesting FCF rediscover.\n");
15413 return -ENOMEM;
15414 }
15415
15416 length = (sizeof(struct lpfc_mbx_redisc_fcf_tbl) -
15417 sizeof(struct lpfc_sli4_cfg_mhdr));
15418 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15419 LPFC_MBOX_OPCODE_FCOE_REDISCOVER_FCF,
15420 length, LPFC_SLI4_MBX_EMBED);
15421
15422 redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
15423 /* Set count to 0 for invalidating the entire FCF database */
15424 bf_set(lpfc_mbx_redisc_fcf_count, redisc_fcf, 0);
15425
15426 /* Issue the mailbox command asynchronously */
15427 mbox->vport = phba->pport;
15428 mbox->mbox_cmpl = lpfc_mbx_cmpl_redisc_fcf_table;
15429 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
15430
15431 if (rc == MBX_NOT_FINISHED) {
15432 mempool_free(mbox, phba->mbox_mem_pool);
15433 return -EIO;
15434 }
15435 return 0;
15436}
15437
15438/**
James Smartfc2b9892010-02-26 14:15:29 -050015439 * lpfc_sli4_fcf_dead_failthrough - Failthrough routine to fcf dead event
15440 * @phba: pointer to lpfc hba data structure.
15441 *
15442 * This function is the failover routine as a last resort to the FCF DEAD
15443 * event when driver failed to perform fast FCF failover.
15444 **/
15445void
15446lpfc_sli4_fcf_dead_failthrough(struct lpfc_hba *phba)
15447{
15448 uint32_t link_state;
15449
15450 /*
15451 * Last resort as FCF DEAD event failover will treat this as
15452 * a link down, but save the link state because we don't want
15453 * it to be changed to Link Down unless it is already down.
15454 */
15455 link_state = phba->link_state;
15456 lpfc_linkdown(phba);
15457 phba->link_state = link_state;
15458
15459 /* Unregister FCF if no devices connected to it */
15460 lpfc_unregister_unused_fcf(phba);
15461}
15462
15463/**
James Smart026abb82011-12-13 13:20:45 -050015464 * lpfc_sli_get_config_region23 - Get sli3 port region 23 data.
James Smarta0c87cb2009-07-19 10:01:10 -040015465 * @phba: pointer to lpfc hba data structure.
James Smart026abb82011-12-13 13:20:45 -050015466 * @rgn23_data: pointer to configure region 23 data.
James Smarta0c87cb2009-07-19 10:01:10 -040015467 *
James Smart026abb82011-12-13 13:20:45 -050015468 * This function gets SLI3 port configure region 23 data through memory dump
15469 * mailbox command. When it successfully retrieves data, the size of the data
15470 * will be returned, otherwise, 0 will be returned.
James Smarta0c87cb2009-07-19 10:01:10 -040015471 **/
James Smart026abb82011-12-13 13:20:45 -050015472static uint32_t
15473lpfc_sli_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)
James Smarta0c87cb2009-07-19 10:01:10 -040015474{
15475 LPFC_MBOXQ_t *pmb = NULL;
15476 MAILBOX_t *mb;
James Smart026abb82011-12-13 13:20:45 -050015477 uint32_t offset = 0;
James Smarta0c87cb2009-07-19 10:01:10 -040015478 int rc;
15479
James Smart026abb82011-12-13 13:20:45 -050015480 if (!rgn23_data)
15481 return 0;
15482
James Smarta0c87cb2009-07-19 10:01:10 -040015483 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15484 if (!pmb) {
15485 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart026abb82011-12-13 13:20:45 -050015486 "2600 failed to allocate mailbox memory\n");
15487 return 0;
James Smarta0c87cb2009-07-19 10:01:10 -040015488 }
15489 mb = &pmb->u.mb;
15490
James Smarta0c87cb2009-07-19 10:01:10 -040015491 do {
15492 lpfc_dump_mem(phba, pmb, offset, DMP_REGION_23);
15493 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
15494
15495 if (rc != MBX_SUCCESS) {
15496 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smart026abb82011-12-13 13:20:45 -050015497 "2601 failed to read config "
15498 "region 23, rc 0x%x Status 0x%x\n",
15499 rc, mb->mbxStatus);
James Smarta0c87cb2009-07-19 10:01:10 -040015500 mb->un.varDmp.word_cnt = 0;
15501 }
15502 /*
15503 * dump mem may return a zero when finished or we got a
15504 * mailbox error, either way we are done.
15505 */
15506 if (mb->un.varDmp.word_cnt == 0)
15507 break;
15508 if (mb->un.varDmp.word_cnt > DMP_RGN23_SIZE - offset)
15509 mb->un.varDmp.word_cnt = DMP_RGN23_SIZE - offset;
15510
15511 lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
James Smart026abb82011-12-13 13:20:45 -050015512 rgn23_data + offset,
15513 mb->un.varDmp.word_cnt);
James Smarta0c87cb2009-07-19 10:01:10 -040015514 offset += mb->un.varDmp.word_cnt;
15515 } while (mb->un.varDmp.word_cnt && offset < DMP_RGN23_SIZE);
15516
James Smart026abb82011-12-13 13:20:45 -050015517 mempool_free(pmb, phba->mbox_mem_pool);
15518 return offset;
15519}
15520
15521/**
15522 * lpfc_sli4_get_config_region23 - Get sli4 port region 23 data.
15523 * @phba: pointer to lpfc hba data structure.
15524 * @rgn23_data: pointer to configure region 23 data.
15525 *
15526 * This function gets SLI4 port configure region 23 data through memory dump
15527 * mailbox command. When it successfully retrieves data, the size of the data
15528 * will be returned, otherwise, 0 will be returned.
15529 **/
15530static uint32_t
15531lpfc_sli4_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)
15532{
15533 LPFC_MBOXQ_t *mboxq = NULL;
15534 struct lpfc_dmabuf *mp = NULL;
15535 struct lpfc_mqe *mqe;
15536 uint32_t data_length = 0;
15537 int rc;
15538
15539 if (!rgn23_data)
15540 return 0;
15541
15542 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15543 if (!mboxq) {
15544 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15545 "3105 failed to allocate mailbox memory\n");
15546 return 0;
15547 }
15548
15549 if (lpfc_sli4_dump_cfg_rg23(phba, mboxq))
15550 goto out;
15551 mqe = &mboxq->u.mqe;
15552 mp = (struct lpfc_dmabuf *) mboxq->context1;
15553 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
15554 if (rc)
15555 goto out;
15556 data_length = mqe->un.mb_words[5];
15557 if (data_length == 0)
15558 goto out;
15559 if (data_length > DMP_RGN23_SIZE) {
15560 data_length = 0;
15561 goto out;
15562 }
15563 lpfc_sli_pcimem_bcopy((char *)mp->virt, rgn23_data, data_length);
15564out:
15565 mempool_free(mboxq, phba->mbox_mem_pool);
15566 if (mp) {
15567 lpfc_mbuf_free(phba, mp->virt, mp->phys);
15568 kfree(mp);
15569 }
15570 return data_length;
15571}
15572
15573/**
15574 * lpfc_sli_read_link_ste - Read region 23 to decide if link is disabled.
15575 * @phba: pointer to lpfc hba data structure.
15576 *
15577 * This function read region 23 and parse TLV for port status to
15578 * decide if the user disaled the port. If the TLV indicates the
15579 * port is disabled, the hba_flag is set accordingly.
15580 **/
15581void
15582lpfc_sli_read_link_ste(struct lpfc_hba *phba)
15583{
15584 uint8_t *rgn23_data = NULL;
15585 uint32_t if_type, data_size, sub_tlv_len, tlv_offset;
15586 uint32_t offset = 0;
15587
15588 /* Get adapter Region 23 data */
15589 rgn23_data = kzalloc(DMP_RGN23_SIZE, GFP_KERNEL);
15590 if (!rgn23_data)
15591 goto out;
15592
15593 if (phba->sli_rev < LPFC_SLI_REV4)
15594 data_size = lpfc_sli_get_config_region23(phba, rgn23_data);
15595 else {
15596 if_type = bf_get(lpfc_sli_intf_if_type,
15597 &phba->sli4_hba.sli_intf);
15598 if (if_type == LPFC_SLI_INTF_IF_TYPE_0)
15599 goto out;
15600 data_size = lpfc_sli4_get_config_region23(phba, rgn23_data);
15601 }
James Smarta0c87cb2009-07-19 10:01:10 -040015602
15603 if (!data_size)
15604 goto out;
15605
15606 /* Check the region signature first */
15607 if (memcmp(&rgn23_data[offset], LPFC_REGION23_SIGNATURE, 4)) {
15608 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15609 "2619 Config region 23 has bad signature\n");
15610 goto out;
15611 }
15612 offset += 4;
15613
15614 /* Check the data structure version */
15615 if (rgn23_data[offset] != LPFC_REGION23_VERSION) {
15616 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15617 "2620 Config region 23 has bad version\n");
15618 goto out;
15619 }
15620 offset += 4;
15621
15622 /* Parse TLV entries in the region */
15623 while (offset < data_size) {
15624 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC)
15625 break;
15626 /*
15627 * If the TLV is not driver specific TLV or driver id is
15628 * not linux driver id, skip the record.
15629 */
15630 if ((rgn23_data[offset] != DRIVER_SPECIFIC_TYPE) ||
15631 (rgn23_data[offset + 2] != LINUX_DRIVER_ID) ||
15632 (rgn23_data[offset + 3] != 0)) {
15633 offset += rgn23_data[offset + 1] * 4 + 4;
15634 continue;
15635 }
15636
15637 /* Driver found a driver specific TLV in the config region */
15638 sub_tlv_len = rgn23_data[offset + 1] * 4;
15639 offset += 4;
15640 tlv_offset = 0;
15641
15642 /*
15643 * Search for configured port state sub-TLV.
15644 */
15645 while ((offset < data_size) &&
15646 (tlv_offset < sub_tlv_len)) {
15647 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC) {
15648 offset += 4;
15649 tlv_offset += 4;
15650 break;
15651 }
15652 if (rgn23_data[offset] != PORT_STE_TYPE) {
15653 offset += rgn23_data[offset + 1] * 4 + 4;
15654 tlv_offset += rgn23_data[offset + 1] * 4 + 4;
15655 continue;
15656 }
15657
15658 /* This HBA contains PORT_STE configured */
15659 if (!rgn23_data[offset + 2])
15660 phba->hba_flag |= LINK_DISABLED;
15661
15662 goto out;
15663 }
15664 }
James Smart026abb82011-12-13 13:20:45 -050015665
James Smarta0c87cb2009-07-19 10:01:10 -040015666out:
James Smarta0c87cb2009-07-19 10:01:10 -040015667 kfree(rgn23_data);
15668 return;
15669}
James Smart695a8142010-01-26 23:08:03 -050015670
15671/**
James Smart52d52442011-05-24 11:42:45 -040015672 * lpfc_wr_object - write an object to the firmware
15673 * @phba: HBA structure that indicates port to create a queue on.
15674 * @dmabuf_list: list of dmabufs to write to the port.
15675 * @size: the total byte value of the objects to write to the port.
15676 * @offset: the current offset to be used to start the transfer.
15677 *
15678 * This routine will create a wr_object mailbox command to send to the port.
15679 * the mailbox command will be constructed using the dma buffers described in
15680 * @dmabuf_list to create a list of BDEs. This routine will fill in as many
15681 * BDEs that the imbedded mailbox can support. The @offset variable will be
15682 * used to indicate the starting offset of the transfer and will also return
15683 * the offset after the write object mailbox has completed. @size is used to
15684 * determine the end of the object and whether the eof bit should be set.
15685 *
15686 * Return 0 is successful and offset will contain the the new offset to use
15687 * for the next write.
15688 * Return negative value for error cases.
15689 **/
15690int
15691lpfc_wr_object(struct lpfc_hba *phba, struct list_head *dmabuf_list,
15692 uint32_t size, uint32_t *offset)
15693{
15694 struct lpfc_mbx_wr_object *wr_object;
15695 LPFC_MBOXQ_t *mbox;
15696 int rc = 0, i = 0;
15697 uint32_t shdr_status, shdr_add_status;
15698 uint32_t mbox_tmo;
15699 union lpfc_sli4_cfg_shdr *shdr;
15700 struct lpfc_dmabuf *dmabuf;
15701 uint32_t written = 0;
15702
15703 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15704 if (!mbox)
15705 return -ENOMEM;
15706
15707 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
15708 LPFC_MBOX_OPCODE_WRITE_OBJECT,
15709 sizeof(struct lpfc_mbx_wr_object) -
15710 sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
15711
15712 wr_object = (struct lpfc_mbx_wr_object *)&mbox->u.mqe.un.wr_object;
15713 wr_object->u.request.write_offset = *offset;
15714 sprintf((uint8_t *)wr_object->u.request.object_name, "/");
15715 wr_object->u.request.object_name[0] =
15716 cpu_to_le32(wr_object->u.request.object_name[0]);
15717 bf_set(lpfc_wr_object_eof, &wr_object->u.request, 0);
15718 list_for_each_entry(dmabuf, dmabuf_list, list) {
15719 if (i >= LPFC_MBX_WR_CONFIG_MAX_BDE || written >= size)
15720 break;
15721 wr_object->u.request.bde[i].addrLow = putPaddrLow(dmabuf->phys);
15722 wr_object->u.request.bde[i].addrHigh =
15723 putPaddrHigh(dmabuf->phys);
15724 if (written + SLI4_PAGE_SIZE >= size) {
15725 wr_object->u.request.bde[i].tus.f.bdeSize =
15726 (size - written);
15727 written += (size - written);
15728 bf_set(lpfc_wr_object_eof, &wr_object->u.request, 1);
15729 } else {
15730 wr_object->u.request.bde[i].tus.f.bdeSize =
15731 SLI4_PAGE_SIZE;
15732 written += SLI4_PAGE_SIZE;
15733 }
15734 i++;
15735 }
15736 wr_object->u.request.bde_count = i;
15737 bf_set(lpfc_wr_object_write_length, &wr_object->u.request, written);
15738 if (!phba->sli4_hba.intr_enable)
15739 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15740 else {
James Smarta183a152011-10-10 21:32:43 -040015741 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart52d52442011-05-24 11:42:45 -040015742 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
15743 }
15744 /* The IOCTL status is embedded in the mailbox subheader. */
15745 shdr = (union lpfc_sli4_cfg_shdr *) &wr_object->header.cfg_shdr;
15746 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15747 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15748 if (rc != MBX_TIMEOUT)
15749 mempool_free(mbox, phba->mbox_mem_pool);
15750 if (shdr_status || shdr_add_status || rc) {
15751 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15752 "3025 Write Object mailbox failed with "
15753 "status x%x add_status x%x, mbx status x%x\n",
15754 shdr_status, shdr_add_status, rc);
15755 rc = -ENXIO;
15756 } else
15757 *offset += wr_object->u.response.actual_write_length;
15758 return rc;
15759}
15760
15761/**
James Smart695a8142010-01-26 23:08:03 -050015762 * lpfc_cleanup_pending_mbox - Free up vport discovery mailbox commands.
15763 * @vport: pointer to vport data structure.
15764 *
15765 * This function iterate through the mailboxq and clean up all REG_LOGIN
15766 * and REG_VPI mailbox commands associated with the vport. This function
15767 * is called when driver want to restart discovery of the vport due to
15768 * a Clear Virtual Link event.
15769 **/
15770void
15771lpfc_cleanup_pending_mbox(struct lpfc_vport *vport)
15772{
15773 struct lpfc_hba *phba = vport->phba;
15774 LPFC_MBOXQ_t *mb, *nextmb;
15775 struct lpfc_dmabuf *mp;
James Smart78730cf2010-04-06 15:06:30 -040015776 struct lpfc_nodelist *ndlp;
James Smartd439d282010-09-29 11:18:45 -040015777 struct lpfc_nodelist *act_mbx_ndlp = NULL;
James Smart589a52d2010-07-14 15:30:54 -040015778 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smartd439d282010-09-29 11:18:45 -040015779 LIST_HEAD(mbox_cmd_list);
James Smart63e801c2010-11-20 23:14:19 -050015780 uint8_t restart_loop;
James Smart695a8142010-01-26 23:08:03 -050015781
James Smartd439d282010-09-29 11:18:45 -040015782 /* Clean up internally queued mailbox commands with the vport */
James Smart695a8142010-01-26 23:08:03 -050015783 spin_lock_irq(&phba->hbalock);
15784 list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
15785 if (mb->vport != vport)
15786 continue;
15787
15788 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
15789 (mb->u.mb.mbxCommand != MBX_REG_VPI))
15790 continue;
15791
James Smartd439d282010-09-29 11:18:45 -040015792 list_del(&mb->list);
15793 list_add_tail(&mb->list, &mbox_cmd_list);
15794 }
15795 /* Clean up active mailbox command with the vport */
15796 mb = phba->sli.mbox_active;
15797 if (mb && (mb->vport == vport)) {
15798 if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) ||
15799 (mb->u.mb.mbxCommand == MBX_REG_VPI))
15800 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
15801 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
15802 act_mbx_ndlp = (struct lpfc_nodelist *)mb->context2;
15803 /* Put reference count for delayed processing */
15804 act_mbx_ndlp = lpfc_nlp_get(act_mbx_ndlp);
15805 /* Unregister the RPI when mailbox complete */
15806 mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
15807 }
15808 }
James Smart63e801c2010-11-20 23:14:19 -050015809 /* Cleanup any mailbox completions which are not yet processed */
15810 do {
15811 restart_loop = 0;
15812 list_for_each_entry(mb, &phba->sli.mboxq_cmpl, list) {
15813 /*
15814 * If this mailox is already processed or it is
15815 * for another vport ignore it.
15816 */
15817 if ((mb->vport != vport) ||
15818 (mb->mbox_flag & LPFC_MBX_IMED_UNREG))
15819 continue;
15820
15821 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
15822 (mb->u.mb.mbxCommand != MBX_REG_VPI))
15823 continue;
15824
15825 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
15826 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
15827 ndlp = (struct lpfc_nodelist *)mb->context2;
15828 /* Unregister the RPI when mailbox complete */
15829 mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
15830 restart_loop = 1;
15831 spin_unlock_irq(&phba->hbalock);
15832 spin_lock(shost->host_lock);
15833 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
15834 spin_unlock(shost->host_lock);
15835 spin_lock_irq(&phba->hbalock);
15836 break;
15837 }
15838 }
15839 } while (restart_loop);
15840
James Smartd439d282010-09-29 11:18:45 -040015841 spin_unlock_irq(&phba->hbalock);
15842
15843 /* Release the cleaned-up mailbox commands */
15844 while (!list_empty(&mbox_cmd_list)) {
15845 list_remove_head(&mbox_cmd_list, mb, LPFC_MBOXQ_t, list);
James Smart695a8142010-01-26 23:08:03 -050015846 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
15847 mp = (struct lpfc_dmabuf *) (mb->context1);
15848 if (mp) {
15849 __lpfc_mbuf_free(phba, mp->virt, mp->phys);
15850 kfree(mp);
15851 }
James Smart78730cf2010-04-06 15:06:30 -040015852 ndlp = (struct lpfc_nodelist *) mb->context2;
James Smartd439d282010-09-29 11:18:45 -040015853 mb->context2 = NULL;
James Smart78730cf2010-04-06 15:06:30 -040015854 if (ndlp) {
Dan Carpenterec21b3b2010-08-08 00:15:17 +020015855 spin_lock(shost->host_lock);
James Smart589a52d2010-07-14 15:30:54 -040015856 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
Dan Carpenterec21b3b2010-08-08 00:15:17 +020015857 spin_unlock(shost->host_lock);
James Smart78730cf2010-04-06 15:06:30 -040015858 lpfc_nlp_put(ndlp);
James Smart78730cf2010-04-06 15:06:30 -040015859 }
James Smart695a8142010-01-26 23:08:03 -050015860 }
James Smart695a8142010-01-26 23:08:03 -050015861 mempool_free(mb, phba->mbox_mem_pool);
15862 }
James Smartd439d282010-09-29 11:18:45 -040015863
15864 /* Release the ndlp with the cleaned-up active mailbox command */
15865 if (act_mbx_ndlp) {
15866 spin_lock(shost->host_lock);
15867 act_mbx_ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
15868 spin_unlock(shost->host_lock);
15869 lpfc_nlp_put(act_mbx_ndlp);
James Smart695a8142010-01-26 23:08:03 -050015870 }
James Smart695a8142010-01-26 23:08:03 -050015871}
15872
James Smart2a9bf3d2010-06-07 15:24:45 -040015873/**
15874 * lpfc_drain_txq - Drain the txq
15875 * @phba: Pointer to HBA context object.
15876 *
15877 * This function attempt to submit IOCBs on the txq
15878 * to the adapter. For SLI4 adapters, the txq contains
15879 * ELS IOCBs that have been deferred because the there
15880 * are no SGLs. This congestion can occur with large
15881 * vport counts during node discovery.
15882 **/
15883
15884uint32_t
15885lpfc_drain_txq(struct lpfc_hba *phba)
15886{
15887 LIST_HEAD(completions);
15888 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
15889 struct lpfc_iocbq *piocbq = 0;
15890 unsigned long iflags = 0;
15891 char *fail_msg = NULL;
15892 struct lpfc_sglq *sglq;
15893 union lpfc_wqe wqe;
15894
15895 spin_lock_irqsave(&phba->hbalock, iflags);
15896 if (pring->txq_cnt > pring->txq_max)
15897 pring->txq_max = pring->txq_cnt;
15898
15899 spin_unlock_irqrestore(&phba->hbalock, iflags);
15900
15901 while (pring->txq_cnt) {
15902 spin_lock_irqsave(&phba->hbalock, iflags);
15903
James Smart19ca7602010-11-20 23:11:55 -050015904 piocbq = lpfc_sli_ringtx_get(phba, pring);
15905 sglq = __lpfc_sli_get_sglq(phba, piocbq);
James Smart2a9bf3d2010-06-07 15:24:45 -040015906 if (!sglq) {
James Smart19ca7602010-11-20 23:11:55 -050015907 __lpfc_sli_ringtx_put(phba, pring, piocbq);
James Smart2a9bf3d2010-06-07 15:24:45 -040015908 spin_unlock_irqrestore(&phba->hbalock, iflags);
15909 break;
15910 } else {
James Smart2a9bf3d2010-06-07 15:24:45 -040015911 if (!piocbq) {
15912 /* The txq_cnt out of sync. This should
15913 * never happen
15914 */
15915 sglq = __lpfc_clear_active_sglq(phba,
James Smart6d368e52011-05-24 11:44:12 -040015916 sglq->sli4_lxritag);
James Smart2a9bf3d2010-06-07 15:24:45 -040015917 spin_unlock_irqrestore(&phba->hbalock, iflags);
15918 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15919 "2823 txq empty and txq_cnt is %d\n ",
15920 pring->txq_cnt);
15921 break;
15922 }
15923 }
15924
15925 /* The xri and iocb resources secured,
15926 * attempt to issue request
15927 */
James Smart6d368e52011-05-24 11:44:12 -040015928 piocbq->sli4_lxritag = sglq->sli4_lxritag;
James Smart2a9bf3d2010-06-07 15:24:45 -040015929 piocbq->sli4_xritag = sglq->sli4_xritag;
15930 if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocbq, sglq))
15931 fail_msg = "to convert bpl to sgl";
15932 else if (lpfc_sli4_iocb2wqe(phba, piocbq, &wqe))
15933 fail_msg = "to convert iocb to wqe";
15934 else if (lpfc_sli4_wq_put(phba->sli4_hba.els_wq, &wqe))
15935 fail_msg = " - Wq is full";
15936 else
15937 lpfc_sli_ringtxcmpl_put(phba, pring, piocbq);
15938
15939 if (fail_msg) {
15940 /* Failed means we can't issue and need to cancel */
15941 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15942 "2822 IOCB failed %s iotag 0x%x "
15943 "xri 0x%x\n",
15944 fail_msg,
15945 piocbq->iotag, piocbq->sli4_xritag);
15946 list_add_tail(&piocbq->list, &completions);
15947 }
15948 spin_unlock_irqrestore(&phba->hbalock, iflags);
15949 }
15950
James Smart2a9bf3d2010-06-07 15:24:45 -040015951 /* Cancel all the IOCBs that cannot be issued */
15952 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
15953 IOERR_SLI_ABORTED);
15954
15955 return pring->txq_cnt;
15956}