blob: 3333e64703a3f31cae18087ee0f5270f783c42aa [file] [log] [blame]
dea31012005-04-17 16:05:31 -05001/*******************************************************************
2 * This file is part of the Emulex Linux Device Driver for *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04003 * Fibre Channel Host Bus Adapters. *
James Smartbdcd2b92012-03-01 22:33:52 -05004 * Copyright (C) 2004-2012 Emulex. All rights reserved. *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04005 * EMULEX and SLI are trademarks of Emulex. *
dea31012005-04-17 16:05:31 -05006 * www.emulex.com *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04007 * Portions Copyright (C) 2004-2005 Christoph Hellwig *
dea31012005-04-17 16:05:31 -05008 * *
9 * This program is free software; you can redistribute it and/or *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -040010 * modify it under the terms of version 2 of the GNU General *
11 * Public License as published by the Free Software Foundation. *
12 * This program is distributed in the hope that it will be useful. *
13 * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND *
14 * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, *
15 * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE *
16 * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
17 * TO BE LEGALLY INVALID. See the GNU General Public License for *
18 * more details, a copy of which can be found in the file COPYING *
19 * included with this package. *
dea31012005-04-17 16:05:31 -050020 *******************************************************************/
21
dea31012005-04-17 16:05:31 -050022#include <linux/blkdev.h>
23#include <linux/pci.h>
24#include <linux/interrupt.h>
25#include <linux/delay.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090026#include <linux/slab.h>
dea31012005-04-17 16:05:31 -050027
James.Smart@Emulex.Com91886522005-08-10 15:03:09 -040028#include <scsi/scsi.h>
dea31012005-04-17 16:05:31 -050029#include <scsi/scsi_cmnd.h>
30#include <scsi/scsi_device.h>
31#include <scsi/scsi_host.h>
James.Smart@Emulex.Comf888ba32005-08-10 15:03:01 -040032#include <scsi/scsi_transport_fc.h>
James Smartda0436e2009-05-22 14:51:39 -040033#include <scsi/fc/fc_fs.h>
James Smart0d878412009-10-02 15:16:56 -040034#include <linux/aer.h>
dea31012005-04-17 16:05:31 -050035
James Smartda0436e2009-05-22 14:51:39 -040036#include "lpfc_hw4.h"
dea31012005-04-17 16:05:31 -050037#include "lpfc_hw.h"
38#include "lpfc_sli.h"
James Smartda0436e2009-05-22 14:51:39 -040039#include "lpfc_sli4.h"
James Smartea2151b2008-09-07 11:52:10 -040040#include "lpfc_nl.h"
dea31012005-04-17 16:05:31 -050041#include "lpfc_disc.h"
42#include "lpfc_scsi.h"
43#include "lpfc.h"
44#include "lpfc_crtn.h"
45#include "lpfc_logmsg.h"
46#include "lpfc_compat.h"
James Smart858c9f62007-06-17 19:56:39 -050047#include "lpfc_debugfs.h"
James Smart04c68492009-05-22 14:52:52 -040048#include "lpfc_vport.h"
dea31012005-04-17 16:05:31 -050049
50/* There are only four IOCB completion types. */
51typedef enum _lpfc_iocb_type {
52 LPFC_UNKNOWN_IOCB,
53 LPFC_UNSOL_IOCB,
54 LPFC_SOL_IOCB,
55 LPFC_ABORT_IOCB
56} lpfc_iocb_type;
57
James Smart4f774512009-05-22 14:52:35 -040058
59/* Provide function prototypes local to this module. */
60static int lpfc_sli_issue_mbox_s4(struct lpfc_hba *, LPFC_MBOXQ_t *,
61 uint32_t);
62static int lpfc_sli4_read_rev(struct lpfc_hba *, LPFC_MBOXQ_t *,
James Smart45ed1192009-10-02 15:17:02 -040063 uint8_t *, uint32_t *);
64static struct lpfc_iocbq *lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *,
65 struct lpfc_iocbq *);
James Smart6669f9b2009-10-02 15:16:45 -040066static void lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *,
67 struct hbq_dmabuf *);
James Smart05580562011-05-24 11:40:48 -040068static int lpfc_sli4_fp_handle_wcqe(struct lpfc_hba *, struct lpfc_queue *,
69 struct lpfc_cqe *);
James Smart8a9d2e82012-05-09 21:16:12 -040070static int lpfc_sli4_post_els_sgl_list(struct lpfc_hba *, struct list_head *,
71 int);
James Smart05580562011-05-24 11:40:48 -040072
James Smart4f774512009-05-22 14:52:35 -040073static IOCB_t *
74lpfc_get_iocb_from_iocbq(struct lpfc_iocbq *iocbq)
75{
76 return &iocbq->iocb;
77}
78
79/**
80 * lpfc_sli4_wq_put - Put a Work Queue Entry on an Work Queue
81 * @q: The Work Queue to operate on.
82 * @wqe: The work Queue Entry to put on the Work queue.
83 *
84 * This routine will copy the contents of @wqe to the next available entry on
85 * the @q. This function will then ring the Work Queue Doorbell to signal the
86 * HBA to start processing the Work Queue Entry. This function returns 0 if
87 * successful. If no entries are available on @q then this function will return
88 * -ENOMEM.
89 * The caller is expected to hold the hbalock when calling this routine.
90 **/
91static uint32_t
92lpfc_sli4_wq_put(struct lpfc_queue *q, union lpfc_wqe *wqe)
93{
James Smart2e90f4b2011-12-13 13:22:37 -050094 union lpfc_wqe *temp_wqe;
James Smart4f774512009-05-22 14:52:35 -040095 struct lpfc_register doorbell;
96 uint32_t host_index;
97
James Smart2e90f4b2011-12-13 13:22:37 -050098 /* sanity check on queue memory */
99 if (unlikely(!q))
100 return -ENOMEM;
101 temp_wqe = q->qe[q->host_index].wqe;
102
James Smart4f774512009-05-22 14:52:35 -0400103 /* If the host has not yet processed the next entry then we are done */
104 if (((q->host_index + 1) % q->entry_count) == q->hba_index)
105 return -ENOMEM;
106 /* set consumption flag every once in a while */
James Smartff78d8f2011-12-13 13:21:35 -0500107 if (!((q->host_index + 1) % q->entry_repost))
James Smartf0d9bcc2010-10-22 11:07:09 -0400108 bf_set(wqe_wqec, &wqe->generic.wqe_com, 1);
James Smartfedd3b72011-02-16 12:39:24 -0500109 if (q->phba->sli3_options & LPFC_SLI4_PHWQ_ENABLED)
110 bf_set(wqe_wqid, &wqe->generic.wqe_com, q->queue_id);
James Smart4f774512009-05-22 14:52:35 -0400111 lpfc_sli_pcimem_bcopy(wqe, temp_wqe, q->entry_size);
112
113 /* Update the host index before invoking device */
114 host_index = q->host_index;
115 q->host_index = ((q->host_index + 1) % q->entry_count);
116
117 /* Ring Doorbell */
118 doorbell.word0 = 0;
119 bf_set(lpfc_wq_doorbell_num_posted, &doorbell, 1);
120 bf_set(lpfc_wq_doorbell_index, &doorbell, host_index);
121 bf_set(lpfc_wq_doorbell_id, &doorbell, q->queue_id);
122 writel(doorbell.word0, q->phba->sli4_hba.WQDBregaddr);
123 readl(q->phba->sli4_hba.WQDBregaddr); /* Flush */
124
125 return 0;
126}
127
128/**
129 * lpfc_sli4_wq_release - Updates internal hba index for WQ
130 * @q: The Work Queue to operate on.
131 * @index: The index to advance the hba index to.
132 *
133 * This routine will update the HBA index of a queue to reflect consumption of
134 * Work Queue Entries by the HBA. When the HBA indicates that it has consumed
135 * an entry the host calls this function to update the queue's internal
136 * pointers. This routine returns the number of entries that were consumed by
137 * the HBA.
138 **/
139static uint32_t
140lpfc_sli4_wq_release(struct lpfc_queue *q, uint32_t index)
141{
142 uint32_t released = 0;
143
James Smart2e90f4b2011-12-13 13:22:37 -0500144 /* sanity check on queue memory */
145 if (unlikely(!q))
146 return 0;
147
James Smart4f774512009-05-22 14:52:35 -0400148 if (q->hba_index == index)
149 return 0;
150 do {
151 q->hba_index = ((q->hba_index + 1) % q->entry_count);
152 released++;
153 } while (q->hba_index != index);
154 return released;
155}
156
157/**
158 * lpfc_sli4_mq_put - Put a Mailbox Queue Entry on an Mailbox Queue
159 * @q: The Mailbox Queue to operate on.
160 * @wqe: The Mailbox Queue Entry to put on the Work queue.
161 *
162 * This routine will copy the contents of @mqe to the next available entry on
163 * the @q. This function will then ring the Work Queue Doorbell to signal the
164 * HBA to start processing the Work Queue Entry. This function returns 0 if
165 * successful. If no entries are available on @q then this function will return
166 * -ENOMEM.
167 * The caller is expected to hold the hbalock when calling this routine.
168 **/
169static uint32_t
170lpfc_sli4_mq_put(struct lpfc_queue *q, struct lpfc_mqe *mqe)
171{
James Smart2e90f4b2011-12-13 13:22:37 -0500172 struct lpfc_mqe *temp_mqe;
James Smart4f774512009-05-22 14:52:35 -0400173 struct lpfc_register doorbell;
174 uint32_t host_index;
175
James Smart2e90f4b2011-12-13 13:22:37 -0500176 /* sanity check on queue memory */
177 if (unlikely(!q))
178 return -ENOMEM;
179 temp_mqe = q->qe[q->host_index].mqe;
180
James Smart4f774512009-05-22 14:52:35 -0400181 /* If the host has not yet processed the next entry then we are done */
182 if (((q->host_index + 1) % q->entry_count) == q->hba_index)
183 return -ENOMEM;
184 lpfc_sli_pcimem_bcopy(mqe, temp_mqe, q->entry_size);
185 /* Save off the mailbox pointer for completion */
186 q->phba->mbox = (MAILBOX_t *)temp_mqe;
187
188 /* Update the host index before invoking device */
189 host_index = q->host_index;
190 q->host_index = ((q->host_index + 1) % q->entry_count);
191
192 /* Ring Doorbell */
193 doorbell.word0 = 0;
194 bf_set(lpfc_mq_doorbell_num_posted, &doorbell, 1);
195 bf_set(lpfc_mq_doorbell_id, &doorbell, q->queue_id);
196 writel(doorbell.word0, q->phba->sli4_hba.MQDBregaddr);
197 readl(q->phba->sli4_hba.MQDBregaddr); /* Flush */
198 return 0;
199}
200
201/**
202 * lpfc_sli4_mq_release - Updates internal hba index for MQ
203 * @q: The Mailbox Queue to operate on.
204 *
205 * This routine will update the HBA index of a queue to reflect consumption of
206 * a Mailbox Queue Entry by the HBA. When the HBA indicates that it has consumed
207 * an entry the host calls this function to update the queue's internal
208 * pointers. This routine returns the number of entries that were consumed by
209 * the HBA.
210 **/
211static uint32_t
212lpfc_sli4_mq_release(struct lpfc_queue *q)
213{
James Smart2e90f4b2011-12-13 13:22:37 -0500214 /* sanity check on queue memory */
215 if (unlikely(!q))
216 return 0;
217
James Smart4f774512009-05-22 14:52:35 -0400218 /* Clear the mailbox pointer for completion */
219 q->phba->mbox = NULL;
220 q->hba_index = ((q->hba_index + 1) % q->entry_count);
221 return 1;
222}
223
224/**
225 * lpfc_sli4_eq_get - Gets the next valid EQE from a EQ
226 * @q: The Event Queue to get the first valid EQE from
227 *
228 * This routine will get the first valid Event Queue Entry from @q, update
229 * the queue's internal hba index, and return the EQE. If no valid EQEs are in
230 * the Queue (no more work to do), or the Queue is full of EQEs that have been
231 * processed, but not popped back to the HBA then this routine will return NULL.
232 **/
233static struct lpfc_eqe *
234lpfc_sli4_eq_get(struct lpfc_queue *q)
235{
James Smart2e90f4b2011-12-13 13:22:37 -0500236 struct lpfc_eqe *eqe;
237
238 /* sanity check on queue memory */
239 if (unlikely(!q))
240 return NULL;
241 eqe = q->qe[q->hba_index].eqe;
James Smart4f774512009-05-22 14:52:35 -0400242
243 /* If the next EQE is not valid then we are done */
James Smartcb5172e2010-03-15 11:25:07 -0400244 if (!bf_get_le32(lpfc_eqe_valid, eqe))
James Smart4f774512009-05-22 14:52:35 -0400245 return NULL;
246 /* If the host has not yet processed the next entry then we are done */
247 if (((q->hba_index + 1) % q->entry_count) == q->host_index)
248 return NULL;
249
250 q->hba_index = ((q->hba_index + 1) % q->entry_count);
251 return eqe;
252}
253
254/**
255 * lpfc_sli4_eq_release - Indicates the host has finished processing an EQ
256 * @q: The Event Queue that the host has completed processing for.
257 * @arm: Indicates whether the host wants to arms this CQ.
258 *
259 * This routine will mark all Event Queue Entries on @q, from the last
260 * known completed entry to the last entry that was processed, as completed
261 * by clearing the valid bit for each completion queue entry. Then it will
262 * notify the HBA, by ringing the doorbell, that the EQEs have been processed.
263 * The internal host index in the @q will be updated by this routine to indicate
264 * that the host has finished processing the entries. The @arm parameter
265 * indicates that the queue should be rearmed when ringing the doorbell.
266 *
267 * This function will return the number of EQEs that were popped.
268 **/
269uint32_t
270lpfc_sli4_eq_release(struct lpfc_queue *q, bool arm)
271{
272 uint32_t released = 0;
273 struct lpfc_eqe *temp_eqe;
274 struct lpfc_register doorbell;
275
James Smart2e90f4b2011-12-13 13:22:37 -0500276 /* sanity check on queue memory */
277 if (unlikely(!q))
278 return 0;
279
James Smart4f774512009-05-22 14:52:35 -0400280 /* while there are valid entries */
281 while (q->hba_index != q->host_index) {
282 temp_eqe = q->qe[q->host_index].eqe;
James Smartcb5172e2010-03-15 11:25:07 -0400283 bf_set_le32(lpfc_eqe_valid, temp_eqe, 0);
James Smart4f774512009-05-22 14:52:35 -0400284 released++;
285 q->host_index = ((q->host_index + 1) % q->entry_count);
286 }
287 if (unlikely(released == 0 && !arm))
288 return 0;
289
290 /* ring doorbell for number popped */
291 doorbell.word0 = 0;
292 if (arm) {
293 bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);
294 bf_set(lpfc_eqcq_doorbell_eqci, &doorbell, 1);
295 }
296 bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, released);
297 bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_EVENT);
James Smart6b5151f2012-01-18 16:24:06 -0500298 bf_set(lpfc_eqcq_doorbell_eqid_hi, &doorbell,
299 (q->queue_id >> LPFC_EQID_HI_FIELD_SHIFT));
300 bf_set(lpfc_eqcq_doorbell_eqid_lo, &doorbell, q->queue_id);
James Smart4f774512009-05-22 14:52:35 -0400301 writel(doorbell.word0, q->phba->sli4_hba.EQCQDBregaddr);
James Smarta747c9c2009-11-18 15:41:10 -0500302 /* PCI read to flush PCI pipeline on re-arming for INTx mode */
303 if ((q->phba->intr_type == INTx) && (arm == LPFC_QUEUE_REARM))
304 readl(q->phba->sli4_hba.EQCQDBregaddr);
James Smart4f774512009-05-22 14:52:35 -0400305 return released;
306}
307
308/**
309 * lpfc_sli4_cq_get - Gets the next valid CQE from a CQ
310 * @q: The Completion Queue to get the first valid CQE from
311 *
312 * This routine will get the first valid Completion Queue Entry from @q, update
313 * the queue's internal hba index, and return the CQE. If no valid CQEs are in
314 * the Queue (no more work to do), or the Queue is full of CQEs that have been
315 * processed, but not popped back to the HBA then this routine will return NULL.
316 **/
317static struct lpfc_cqe *
318lpfc_sli4_cq_get(struct lpfc_queue *q)
319{
320 struct lpfc_cqe *cqe;
321
James Smart2e90f4b2011-12-13 13:22:37 -0500322 /* sanity check on queue memory */
323 if (unlikely(!q))
324 return NULL;
325
James Smart4f774512009-05-22 14:52:35 -0400326 /* If the next CQE is not valid then we are done */
James Smartcb5172e2010-03-15 11:25:07 -0400327 if (!bf_get_le32(lpfc_cqe_valid, q->qe[q->hba_index].cqe))
James Smart4f774512009-05-22 14:52:35 -0400328 return NULL;
329 /* If the host has not yet processed the next entry then we are done */
330 if (((q->hba_index + 1) % q->entry_count) == q->host_index)
331 return NULL;
332
333 cqe = q->qe[q->hba_index].cqe;
334 q->hba_index = ((q->hba_index + 1) % q->entry_count);
335 return cqe;
336}
337
338/**
339 * lpfc_sli4_cq_release - Indicates the host has finished processing a CQ
340 * @q: The Completion Queue that the host has completed processing for.
341 * @arm: Indicates whether the host wants to arms this CQ.
342 *
343 * This routine will mark all Completion queue entries on @q, from the last
344 * known completed entry to the last entry that was processed, as completed
345 * by clearing the valid bit for each completion queue entry. Then it will
346 * notify the HBA, by ringing the doorbell, that the CQEs have been processed.
347 * The internal host index in the @q will be updated by this routine to indicate
348 * that the host has finished processing the entries. The @arm parameter
349 * indicates that the queue should be rearmed when ringing the doorbell.
350 *
351 * This function will return the number of CQEs that were released.
352 **/
353uint32_t
354lpfc_sli4_cq_release(struct lpfc_queue *q, bool arm)
355{
356 uint32_t released = 0;
357 struct lpfc_cqe *temp_qe;
358 struct lpfc_register doorbell;
359
James Smart2e90f4b2011-12-13 13:22:37 -0500360 /* sanity check on queue memory */
361 if (unlikely(!q))
362 return 0;
James Smart4f774512009-05-22 14:52:35 -0400363 /* while there are valid entries */
364 while (q->hba_index != q->host_index) {
365 temp_qe = q->qe[q->host_index].cqe;
James Smartcb5172e2010-03-15 11:25:07 -0400366 bf_set_le32(lpfc_cqe_valid, temp_qe, 0);
James Smart4f774512009-05-22 14:52:35 -0400367 released++;
368 q->host_index = ((q->host_index + 1) % q->entry_count);
369 }
370 if (unlikely(released == 0 && !arm))
371 return 0;
372
373 /* ring doorbell for number popped */
374 doorbell.word0 = 0;
375 if (arm)
376 bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);
377 bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, released);
378 bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_COMPLETION);
James Smart6b5151f2012-01-18 16:24:06 -0500379 bf_set(lpfc_eqcq_doorbell_cqid_hi, &doorbell,
380 (q->queue_id >> LPFC_CQID_HI_FIELD_SHIFT));
381 bf_set(lpfc_eqcq_doorbell_cqid_lo, &doorbell, q->queue_id);
James Smart4f774512009-05-22 14:52:35 -0400382 writel(doorbell.word0, q->phba->sli4_hba.EQCQDBregaddr);
383 return released;
384}
385
386/**
387 * lpfc_sli4_rq_put - Put a Receive Buffer Queue Entry on a Receive Queue
388 * @q: The Header Receive Queue to operate on.
389 * @wqe: The Receive Queue Entry to put on the Receive queue.
390 *
391 * This routine will copy the contents of @wqe to the next available entry on
392 * the @q. This function will then ring the Receive Queue Doorbell to signal the
393 * HBA to start processing the Receive Queue Entry. This function returns the
394 * index that the rqe was copied to if successful. If no entries are available
395 * on @q then this function will return -ENOMEM.
396 * The caller is expected to hold the hbalock when calling this routine.
397 **/
398static int
399lpfc_sli4_rq_put(struct lpfc_queue *hq, struct lpfc_queue *dq,
400 struct lpfc_rqe *hrqe, struct lpfc_rqe *drqe)
401{
James Smart2e90f4b2011-12-13 13:22:37 -0500402 struct lpfc_rqe *temp_hrqe;
403 struct lpfc_rqe *temp_drqe;
James Smart4f774512009-05-22 14:52:35 -0400404 struct lpfc_register doorbell;
405 int put_index = hq->host_index;
406
James Smart2e90f4b2011-12-13 13:22:37 -0500407 /* sanity check on queue memory */
408 if (unlikely(!hq) || unlikely(!dq))
409 return -ENOMEM;
410 temp_hrqe = hq->qe[hq->host_index].rqe;
411 temp_drqe = dq->qe[dq->host_index].rqe;
412
James Smart4f774512009-05-22 14:52:35 -0400413 if (hq->type != LPFC_HRQ || dq->type != LPFC_DRQ)
414 return -EINVAL;
415 if (hq->host_index != dq->host_index)
416 return -EINVAL;
417 /* If the host has not yet processed the next entry then we are done */
418 if (((hq->host_index + 1) % hq->entry_count) == hq->hba_index)
419 return -EBUSY;
420 lpfc_sli_pcimem_bcopy(hrqe, temp_hrqe, hq->entry_size);
421 lpfc_sli_pcimem_bcopy(drqe, temp_drqe, dq->entry_size);
422
423 /* Update the host index to point to the next slot */
424 hq->host_index = ((hq->host_index + 1) % hq->entry_count);
425 dq->host_index = ((dq->host_index + 1) % dq->entry_count);
426
427 /* Ring The Header Receive Queue Doorbell */
James Smart73d91e52011-10-10 21:32:10 -0400428 if (!(hq->host_index % hq->entry_repost)) {
James Smart4f774512009-05-22 14:52:35 -0400429 doorbell.word0 = 0;
430 bf_set(lpfc_rq_doorbell_num_posted, &doorbell,
James Smart73d91e52011-10-10 21:32:10 -0400431 hq->entry_repost);
James Smart4f774512009-05-22 14:52:35 -0400432 bf_set(lpfc_rq_doorbell_id, &doorbell, hq->queue_id);
433 writel(doorbell.word0, hq->phba->sli4_hba.RQDBregaddr);
434 }
435 return put_index;
436}
437
438/**
439 * lpfc_sli4_rq_release - Updates internal hba index for RQ
440 * @q: The Header Receive Queue to operate on.
441 *
442 * This routine will update the HBA index of a queue to reflect consumption of
443 * one Receive Queue Entry by the HBA. When the HBA indicates that it has
444 * consumed an entry the host calls this function to update the queue's
445 * internal pointers. This routine returns the number of entries that were
446 * consumed by the HBA.
447 **/
448static uint32_t
449lpfc_sli4_rq_release(struct lpfc_queue *hq, struct lpfc_queue *dq)
450{
James Smart2e90f4b2011-12-13 13:22:37 -0500451 /* sanity check on queue memory */
452 if (unlikely(!hq) || unlikely(!dq))
453 return 0;
454
James Smart4f774512009-05-22 14:52:35 -0400455 if ((hq->type != LPFC_HRQ) || (dq->type != LPFC_DRQ))
456 return 0;
457 hq->hba_index = ((hq->hba_index + 1) % hq->entry_count);
458 dq->hba_index = ((dq->hba_index + 1) % dq->entry_count);
459 return 1;
460}
461
James Smarte59058c2008-08-24 21:49:00 -0400462/**
James Smart3621a712009-04-06 18:47:14 -0400463 * lpfc_cmd_iocb - Get next command iocb entry in the ring
James Smarte59058c2008-08-24 21:49:00 -0400464 * @phba: Pointer to HBA context object.
465 * @pring: Pointer to driver SLI ring object.
466 *
467 * This function returns pointer to next command iocb entry
468 * in the command ring. The caller must hold hbalock to prevent
469 * other threads consume the next command iocb.
470 * SLI-2/SLI-3 provide different sized iocbs.
471 **/
James Smarted957682007-06-17 19:56:37 -0500472static inline IOCB_t *
473lpfc_cmd_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
474{
475 return (IOCB_t *) (((char *) pring->cmdringaddr) +
476 pring->cmdidx * phba->iocb_cmd_size);
477}
478
James Smarte59058c2008-08-24 21:49:00 -0400479/**
James Smart3621a712009-04-06 18:47:14 -0400480 * lpfc_resp_iocb - Get next response iocb entry in the ring
James Smarte59058c2008-08-24 21:49:00 -0400481 * @phba: Pointer to HBA context object.
482 * @pring: Pointer to driver SLI ring object.
483 *
484 * This function returns pointer to next response iocb entry
485 * in the response ring. The caller must hold hbalock to make sure
486 * that no other thread consume the next response iocb.
487 * SLI-2/SLI-3 provide different sized iocbs.
488 **/
James Smarted957682007-06-17 19:56:37 -0500489static inline IOCB_t *
490lpfc_resp_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
491{
492 return (IOCB_t *) (((char *) pring->rspringaddr) +
493 pring->rspidx * phba->iocb_rsp_size);
494}
495
James Smarte59058c2008-08-24 21:49:00 -0400496/**
James Smart3621a712009-04-06 18:47:14 -0400497 * __lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
James Smarte59058c2008-08-24 21:49:00 -0400498 * @phba: Pointer to HBA context object.
499 *
500 * This function is called with hbalock held. This function
501 * allocates a new driver iocb object from the iocb pool. If the
502 * allocation is successful, it returns pointer to the newly
503 * allocated iocb object else it returns NULL.
504 **/
James Smart4f2e66c2012-05-09 21:17:07 -0400505struct lpfc_iocbq *
James Smart2e0fef82007-06-17 19:56:36 -0500506__lpfc_sli_get_iocbq(struct lpfc_hba *phba)
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400507{
508 struct list_head *lpfc_iocb_list = &phba->lpfc_iocb_list;
509 struct lpfc_iocbq * iocbq = NULL;
510
511 list_remove_head(lpfc_iocb_list, iocbq, struct lpfc_iocbq, list);
James Smart2a9bf3d2010-06-07 15:24:45 -0400512 if (iocbq)
513 phba->iocb_cnt++;
514 if (phba->iocb_cnt > phba->iocb_max)
515 phba->iocb_max = phba->iocb_cnt;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400516 return iocbq;
517}
518
James Smarte59058c2008-08-24 21:49:00 -0400519/**
James Smartda0436e2009-05-22 14:51:39 -0400520 * __lpfc_clear_active_sglq - Remove the active sglq for this XRI.
521 * @phba: Pointer to HBA context object.
522 * @xritag: XRI value.
523 *
524 * This function clears the sglq pointer from the array of acive
525 * sglq's. The xritag that is passed in is used to index into the
526 * array. Before the xritag can be used it needs to be adjusted
527 * by subtracting the xribase.
528 *
529 * Returns sglq ponter = success, NULL = Failure.
530 **/
531static struct lpfc_sglq *
532__lpfc_clear_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
533{
James Smartda0436e2009-05-22 14:51:39 -0400534 struct lpfc_sglq *sglq;
James Smart6d368e52011-05-24 11:44:12 -0400535
536 sglq = phba->sli4_hba.lpfc_sglq_active_list[xritag];
537 phba->sli4_hba.lpfc_sglq_active_list[xritag] = NULL;
James Smartda0436e2009-05-22 14:51:39 -0400538 return sglq;
539}
540
541/**
542 * __lpfc_get_active_sglq - Get the active sglq for this XRI.
543 * @phba: Pointer to HBA context object.
544 * @xritag: XRI value.
545 *
546 * This function returns the sglq pointer from the array of acive
547 * sglq's. The xritag that is passed in is used to index into the
548 * array. Before the xritag can be used it needs to be adjusted
549 * by subtracting the xribase.
550 *
551 * Returns sglq ponter = success, NULL = Failure.
552 **/
James Smart0f65ff62010-02-26 14:14:23 -0500553struct lpfc_sglq *
James Smartda0436e2009-05-22 14:51:39 -0400554__lpfc_get_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
555{
James Smartda0436e2009-05-22 14:51:39 -0400556 struct lpfc_sglq *sglq;
James Smart6d368e52011-05-24 11:44:12 -0400557
558 sglq = phba->sli4_hba.lpfc_sglq_active_list[xritag];
James Smartda0436e2009-05-22 14:51:39 -0400559 return sglq;
560}
561
562/**
James Smart1151e3e2011-02-16 12:39:35 -0500563 * lpfc_clr_rrq_active - Clears RRQ active bit in xri_bitmap.
James Smart19ca7602010-11-20 23:11:55 -0500564 * @phba: Pointer to HBA context object.
565 * @xritag: xri used in this exchange.
566 * @rrq: The RRQ to be cleared.
567 *
James Smart19ca7602010-11-20 23:11:55 -0500568 **/
James Smart1151e3e2011-02-16 12:39:35 -0500569void
570lpfc_clr_rrq_active(struct lpfc_hba *phba,
571 uint16_t xritag,
572 struct lpfc_node_rrq *rrq)
James Smart19ca7602010-11-20 23:11:55 -0500573{
James Smart1151e3e2011-02-16 12:39:35 -0500574 struct lpfc_nodelist *ndlp = NULL;
James Smart19ca7602010-11-20 23:11:55 -0500575
James Smart1151e3e2011-02-16 12:39:35 -0500576 if ((rrq->vport) && NLP_CHK_NODE_ACT(rrq->ndlp))
577 ndlp = lpfc_findnode_did(rrq->vport, rrq->nlp_DID);
James Smart19ca7602010-11-20 23:11:55 -0500578
579 /* The target DID could have been swapped (cable swap)
580 * we should use the ndlp from the findnode if it is
581 * available.
582 */
James Smart1151e3e2011-02-16 12:39:35 -0500583 if ((!ndlp) && rrq->ndlp)
James Smart19ca7602010-11-20 23:11:55 -0500584 ndlp = rrq->ndlp;
585
James Smart1151e3e2011-02-16 12:39:35 -0500586 if (!ndlp)
587 goto out;
588
James Smart6d368e52011-05-24 11:44:12 -0400589 if (test_and_clear_bit(xritag, ndlp->active_rrqs.xri_bitmap)) {
James Smart19ca7602010-11-20 23:11:55 -0500590 rrq->send_rrq = 0;
591 rrq->xritag = 0;
592 rrq->rrq_stop_time = 0;
593 }
James Smart1151e3e2011-02-16 12:39:35 -0500594out:
James Smart19ca7602010-11-20 23:11:55 -0500595 mempool_free(rrq, phba->rrq_pool);
596}
597
598/**
599 * lpfc_handle_rrq_active - Checks if RRQ has waithed RATOV.
600 * @phba: Pointer to HBA context object.
601 *
602 * This function is called with hbalock held. This function
603 * Checks if stop_time (ratov from setting rrq active) has
604 * been reached, if it has and the send_rrq flag is set then
605 * it will call lpfc_send_rrq. If the send_rrq flag is not set
606 * then it will just call the routine to clear the rrq and
607 * free the rrq resource.
608 * The timer is set to the next rrq that is going to expire before
609 * leaving the routine.
610 *
611 **/
612void
613lpfc_handle_rrq_active(struct lpfc_hba *phba)
614{
615 struct lpfc_node_rrq *rrq;
616 struct lpfc_node_rrq *nextrrq;
617 unsigned long next_time;
618 unsigned long iflags;
James Smart1151e3e2011-02-16 12:39:35 -0500619 LIST_HEAD(send_rrq);
James Smart19ca7602010-11-20 23:11:55 -0500620
621 spin_lock_irqsave(&phba->hbalock, iflags);
622 phba->hba_flag &= ~HBA_RRQ_ACTIVE;
623 next_time = jiffies + HZ * (phba->fc_ratov + 1);
624 list_for_each_entry_safe(rrq, nextrrq,
James Smart1151e3e2011-02-16 12:39:35 -0500625 &phba->active_rrq_list, list) {
626 if (time_after(jiffies, rrq->rrq_stop_time))
627 list_move(&rrq->list, &send_rrq);
628 else if (time_before(rrq->rrq_stop_time, next_time))
James Smart19ca7602010-11-20 23:11:55 -0500629 next_time = rrq->rrq_stop_time;
630 }
631 spin_unlock_irqrestore(&phba->hbalock, iflags);
632 if (!list_empty(&phba->active_rrq_list))
633 mod_timer(&phba->rrq_tmr, next_time);
James Smart1151e3e2011-02-16 12:39:35 -0500634 list_for_each_entry_safe(rrq, nextrrq, &send_rrq, list) {
635 list_del(&rrq->list);
636 if (!rrq->send_rrq)
637 /* this call will free the rrq */
638 lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
639 else if (lpfc_send_rrq(phba, rrq)) {
640 /* if we send the rrq then the completion handler
641 * will clear the bit in the xribitmap.
642 */
643 lpfc_clr_rrq_active(phba, rrq->xritag,
644 rrq);
645 }
646 }
James Smart19ca7602010-11-20 23:11:55 -0500647}
648
649/**
650 * lpfc_get_active_rrq - Get the active RRQ for this exchange.
651 * @vport: Pointer to vport context object.
652 * @xri: The xri used in the exchange.
653 * @did: The targets DID for this exchange.
654 *
655 * returns NULL = rrq not found in the phba->active_rrq_list.
656 * rrq = rrq for this xri and target.
657 **/
658struct lpfc_node_rrq *
659lpfc_get_active_rrq(struct lpfc_vport *vport, uint16_t xri, uint32_t did)
660{
661 struct lpfc_hba *phba = vport->phba;
662 struct lpfc_node_rrq *rrq;
663 struct lpfc_node_rrq *nextrrq;
664 unsigned long iflags;
665
666 if (phba->sli_rev != LPFC_SLI_REV4)
667 return NULL;
668 spin_lock_irqsave(&phba->hbalock, iflags);
669 list_for_each_entry_safe(rrq, nextrrq, &phba->active_rrq_list, list) {
670 if (rrq->vport == vport && rrq->xritag == xri &&
671 rrq->nlp_DID == did){
672 list_del(&rrq->list);
673 spin_unlock_irqrestore(&phba->hbalock, iflags);
674 return rrq;
675 }
676 }
677 spin_unlock_irqrestore(&phba->hbalock, iflags);
678 return NULL;
679}
680
681/**
682 * lpfc_cleanup_vports_rrqs - Remove and clear the active RRQ for this vport.
683 * @vport: Pointer to vport context object.
James Smart1151e3e2011-02-16 12:39:35 -0500684 * @ndlp: Pointer to the lpfc_node_list structure.
685 * If ndlp is NULL Remove all active RRQs for this vport from the
686 * phba->active_rrq_list and clear the rrq.
687 * If ndlp is not NULL then only remove rrqs for this vport & this ndlp.
James Smart19ca7602010-11-20 23:11:55 -0500688 **/
689void
James Smart1151e3e2011-02-16 12:39:35 -0500690lpfc_cleanup_vports_rrqs(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
James Smart19ca7602010-11-20 23:11:55 -0500691
692{
693 struct lpfc_hba *phba = vport->phba;
694 struct lpfc_node_rrq *rrq;
695 struct lpfc_node_rrq *nextrrq;
696 unsigned long iflags;
James Smart1151e3e2011-02-16 12:39:35 -0500697 LIST_HEAD(rrq_list);
James Smart19ca7602010-11-20 23:11:55 -0500698
699 if (phba->sli_rev != LPFC_SLI_REV4)
700 return;
James Smart1151e3e2011-02-16 12:39:35 -0500701 if (!ndlp) {
702 lpfc_sli4_vport_delete_els_xri_aborted(vport);
703 lpfc_sli4_vport_delete_fcp_xri_aborted(vport);
James Smart19ca7602010-11-20 23:11:55 -0500704 }
James Smart1151e3e2011-02-16 12:39:35 -0500705 spin_lock_irqsave(&phba->hbalock, iflags);
706 list_for_each_entry_safe(rrq, nextrrq, &phba->active_rrq_list, list)
707 if ((rrq->vport == vport) && (!ndlp || rrq->ndlp == ndlp))
708 list_move(&rrq->list, &rrq_list);
James Smart19ca7602010-11-20 23:11:55 -0500709 spin_unlock_irqrestore(&phba->hbalock, iflags);
James Smart1151e3e2011-02-16 12:39:35 -0500710
711 list_for_each_entry_safe(rrq, nextrrq, &rrq_list, list) {
712 list_del(&rrq->list);
713 lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
714 }
James Smart19ca7602010-11-20 23:11:55 -0500715}
716
717/**
718 * lpfc_cleanup_wt_rrqs - Remove all rrq's from the active list.
719 * @phba: Pointer to HBA context object.
720 *
721 * Remove all rrqs from the phba->active_rrq_list and free them by
722 * calling __lpfc_clr_active_rrq
723 *
724 **/
725void
726lpfc_cleanup_wt_rrqs(struct lpfc_hba *phba)
727{
728 struct lpfc_node_rrq *rrq;
729 struct lpfc_node_rrq *nextrrq;
730 unsigned long next_time;
731 unsigned long iflags;
James Smart1151e3e2011-02-16 12:39:35 -0500732 LIST_HEAD(rrq_list);
James Smart19ca7602010-11-20 23:11:55 -0500733
734 if (phba->sli_rev != LPFC_SLI_REV4)
735 return;
736 spin_lock_irqsave(&phba->hbalock, iflags);
737 phba->hba_flag &= ~HBA_RRQ_ACTIVE;
738 next_time = jiffies + HZ * (phba->fc_ratov * 2);
James Smart1151e3e2011-02-16 12:39:35 -0500739 list_splice_init(&phba->active_rrq_list, &rrq_list);
James Smart19ca7602010-11-20 23:11:55 -0500740 spin_unlock_irqrestore(&phba->hbalock, iflags);
James Smart1151e3e2011-02-16 12:39:35 -0500741
742 list_for_each_entry_safe(rrq, nextrrq, &rrq_list, list) {
743 list_del(&rrq->list);
744 lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
745 }
James Smart19ca7602010-11-20 23:11:55 -0500746 if (!list_empty(&phba->active_rrq_list))
747 mod_timer(&phba->rrq_tmr, next_time);
748}
749
750
751/**
James Smart1151e3e2011-02-16 12:39:35 -0500752 * lpfc_test_rrq_active - Test RRQ bit in xri_bitmap.
James Smart19ca7602010-11-20 23:11:55 -0500753 * @phba: Pointer to HBA context object.
754 * @ndlp: Targets nodelist pointer for this exchange.
755 * @xritag the xri in the bitmap to test.
756 *
757 * This function is called with hbalock held. This function
758 * returns 0 = rrq not active for this xri
759 * 1 = rrq is valid for this xri.
760 **/
James Smart1151e3e2011-02-16 12:39:35 -0500761int
762lpfc_test_rrq_active(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
James Smart19ca7602010-11-20 23:11:55 -0500763 uint16_t xritag)
764{
James Smart19ca7602010-11-20 23:11:55 -0500765 if (!ndlp)
766 return 0;
James Smart6d368e52011-05-24 11:44:12 -0400767 if (test_bit(xritag, ndlp->active_rrqs.xri_bitmap))
James Smart19ca7602010-11-20 23:11:55 -0500768 return 1;
769 else
770 return 0;
771}
772
773/**
774 * lpfc_set_rrq_active - set RRQ active bit in xri_bitmap.
775 * @phba: Pointer to HBA context object.
776 * @ndlp: nodelist pointer for this target.
777 * @xritag: xri used in this exchange.
778 * @rxid: Remote Exchange ID.
779 * @send_rrq: Flag used to determine if we should send rrq els cmd.
780 *
781 * This function takes the hbalock.
782 * The active bit is always set in the active rrq xri_bitmap even
783 * if there is no slot avaiable for the other rrq information.
784 *
785 * returns 0 rrq actived for this xri
786 * < 0 No memory or invalid ndlp.
787 **/
788int
789lpfc_set_rrq_active(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
James Smartb42c07c2012-01-18 16:25:55 -0500790 uint16_t xritag, uint16_t rxid, uint16_t send_rrq)
James Smart19ca7602010-11-20 23:11:55 -0500791{
James Smart19ca7602010-11-20 23:11:55 -0500792 unsigned long iflags;
James Smartb42c07c2012-01-18 16:25:55 -0500793 struct lpfc_node_rrq *rrq;
794 int empty;
795
796 if (!ndlp)
797 return -EINVAL;
798
799 if (!phba->cfg_enable_rrq)
800 return -EINVAL;
James Smart19ca7602010-11-20 23:11:55 -0500801
802 spin_lock_irqsave(&phba->hbalock, iflags);
James Smartb42c07c2012-01-18 16:25:55 -0500803 if (phba->pport->load_flag & FC_UNLOADING) {
804 phba->hba_flag &= ~HBA_RRQ_ACTIVE;
805 goto out;
806 }
807
808 /*
809 * set the active bit even if there is no mem available.
810 */
811 if (NLP_CHK_FREE_REQ(ndlp))
812 goto out;
813
814 if (ndlp->vport && (ndlp->vport->load_flag & FC_UNLOADING))
815 goto out;
816
817 if (test_and_set_bit(xritag, ndlp->active_rrqs.xri_bitmap))
818 goto out;
819
James Smart19ca7602010-11-20 23:11:55 -0500820 spin_unlock_irqrestore(&phba->hbalock, iflags);
James Smartb42c07c2012-01-18 16:25:55 -0500821 rrq = mempool_alloc(phba->rrq_pool, GFP_KERNEL);
822 if (!rrq) {
823 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
824 "3155 Unable to allocate RRQ xri:0x%x rxid:0x%x"
825 " DID:0x%x Send:%d\n",
826 xritag, rxid, ndlp->nlp_DID, send_rrq);
827 return -EINVAL;
828 }
829 rrq->send_rrq = send_rrq;
830 rrq->xritag = xritag;
831 rrq->rrq_stop_time = jiffies + HZ * (phba->fc_ratov + 1);
832 rrq->ndlp = ndlp;
833 rrq->nlp_DID = ndlp->nlp_DID;
834 rrq->vport = ndlp->vport;
835 rrq->rxid = rxid;
836 rrq->send_rrq = send_rrq;
837 spin_lock_irqsave(&phba->hbalock, iflags);
838 empty = list_empty(&phba->active_rrq_list);
839 list_add_tail(&rrq->list, &phba->active_rrq_list);
840 phba->hba_flag |= HBA_RRQ_ACTIVE;
841 if (empty)
842 lpfc_worker_wake_up(phba);
843 spin_unlock_irqrestore(&phba->hbalock, iflags);
844 return 0;
845out:
846 spin_unlock_irqrestore(&phba->hbalock, iflags);
847 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
848 "2921 Can't set rrq active xri:0x%x rxid:0x%x"
849 " DID:0x%x Send:%d\n",
850 xritag, rxid, ndlp->nlp_DID, send_rrq);
851 return -EINVAL;
James Smart19ca7602010-11-20 23:11:55 -0500852}
853
854/**
James Smartda0436e2009-05-22 14:51:39 -0400855 * __lpfc_sli_get_sglq - Allocates an iocb object from sgl pool
856 * @phba: Pointer to HBA context object.
James Smart19ca7602010-11-20 23:11:55 -0500857 * @piocb: Pointer to the iocbq.
James Smartda0436e2009-05-22 14:51:39 -0400858 *
859 * This function is called with hbalock held. This function
James Smart6d368e52011-05-24 11:44:12 -0400860 * gets a new driver sglq object from the sglq list. If the
James Smartda0436e2009-05-22 14:51:39 -0400861 * list is not empty then it is successful, it returns pointer to the newly
862 * allocated sglq object else it returns NULL.
863 **/
864static struct lpfc_sglq *
James Smart19ca7602010-11-20 23:11:55 -0500865__lpfc_sli_get_sglq(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq)
James Smartda0436e2009-05-22 14:51:39 -0400866{
867 struct list_head *lpfc_sgl_list = &phba->sli4_hba.lpfc_sgl_list;
868 struct lpfc_sglq *sglq = NULL;
James Smart19ca7602010-11-20 23:11:55 -0500869 struct lpfc_sglq *start_sglq = NULL;
James Smart19ca7602010-11-20 23:11:55 -0500870 struct lpfc_scsi_buf *lpfc_cmd;
871 struct lpfc_nodelist *ndlp;
872 int found = 0;
873
874 if (piocbq->iocb_flag & LPFC_IO_FCP) {
875 lpfc_cmd = (struct lpfc_scsi_buf *) piocbq->context1;
876 ndlp = lpfc_cmd->rdata->pnode;
James Smartbe858b62010-12-15 17:57:20 -0500877 } else if ((piocbq->iocb.ulpCommand == CMD_GEN_REQUEST64_CR) &&
878 !(piocbq->iocb_flag & LPFC_IO_LIBDFC))
James Smart19ca7602010-11-20 23:11:55 -0500879 ndlp = piocbq->context_un.ndlp;
James Smart93d1379e2012-05-09 21:19:34 -0400880 else if ((piocbq->iocb.ulpCommand == CMD_ELS_REQUEST64_CR) &&
881 (piocbq->iocb_flag & LPFC_IO_LIBDFC))
882 ndlp = piocbq->context_un.ndlp;
James Smart19ca7602010-11-20 23:11:55 -0500883 else
884 ndlp = piocbq->context1;
885
James Smartda0436e2009-05-22 14:51:39 -0400886 list_remove_head(lpfc_sgl_list, sglq, struct lpfc_sglq, list);
James Smart19ca7602010-11-20 23:11:55 -0500887 start_sglq = sglq;
888 while (!found) {
889 if (!sglq)
890 return NULL;
James Smartee0f4fe2012-05-09 21:19:14 -0400891 if (lpfc_test_rrq_active(phba, ndlp, sglq->sli4_lxritag)) {
James Smart19ca7602010-11-20 23:11:55 -0500892 /* This xri has an rrq outstanding for this DID.
893 * put it back in the list and get another xri.
894 */
895 list_add_tail(&sglq->list, lpfc_sgl_list);
896 sglq = NULL;
897 list_remove_head(lpfc_sgl_list, sglq,
898 struct lpfc_sglq, list);
899 if (sglq == start_sglq) {
900 sglq = NULL;
901 break;
902 } else
903 continue;
904 }
905 sglq->ndlp = ndlp;
906 found = 1;
James Smart6d368e52011-05-24 11:44:12 -0400907 phba->sli4_hba.lpfc_sglq_active_list[sglq->sli4_lxritag] = sglq;
James Smart19ca7602010-11-20 23:11:55 -0500908 sglq->state = SGL_ALLOCATED;
909 }
James Smartda0436e2009-05-22 14:51:39 -0400910 return sglq;
911}
912
913/**
James Smart3621a712009-04-06 18:47:14 -0400914 * lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
James Smarte59058c2008-08-24 21:49:00 -0400915 * @phba: Pointer to HBA context object.
916 *
917 * This function is called with no lock held. This function
918 * allocates a new driver iocb object from the iocb pool. If the
919 * allocation is successful, it returns pointer to the newly
920 * allocated iocb object else it returns NULL.
921 **/
James Smart2e0fef82007-06-17 19:56:36 -0500922struct lpfc_iocbq *
923lpfc_sli_get_iocbq(struct lpfc_hba *phba)
James Bottomley604a3e32005-10-29 10:28:33 -0500924{
James Smart2e0fef82007-06-17 19:56:36 -0500925 struct lpfc_iocbq * iocbq = NULL;
926 unsigned long iflags;
927
928 spin_lock_irqsave(&phba->hbalock, iflags);
929 iocbq = __lpfc_sli_get_iocbq(phba);
930 spin_unlock_irqrestore(&phba->hbalock, iflags);
931 return iocbq;
932}
933
James Smarte59058c2008-08-24 21:49:00 -0400934/**
James Smart4f774512009-05-22 14:52:35 -0400935 * __lpfc_sli_release_iocbq_s4 - Release iocb to the iocb pool
936 * @phba: Pointer to HBA context object.
937 * @iocbq: Pointer to driver iocb object.
938 *
939 * This function is called with hbalock held to release driver
940 * iocb object to the iocb pool. The iotag in the iocb object
941 * does not change for each use of the iocb object. This function
942 * clears all other fields of the iocb object when it is freed.
943 * The sqlq structure that holds the xritag and phys and virtual
944 * mappings for the scatter gather list is retrieved from the
945 * active array of sglq. The get of the sglq pointer also clears
946 * the entry in the array. If the status of the IO indiactes that
947 * this IO was aborted then the sglq entry it put on the
948 * lpfc_abts_els_sgl_list until the CQ_ABORTED_XRI is received. If the
949 * IO has good status or fails for any other reason then the sglq
950 * entry is added to the free list (lpfc_sgl_list).
951 **/
952static void
953__lpfc_sli_release_iocbq_s4(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
954{
955 struct lpfc_sglq *sglq;
956 size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
James Smart2a9bf3d2010-06-07 15:24:45 -0400957 unsigned long iflag = 0;
958 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
James Smart4f774512009-05-22 14:52:35 -0400959
960 if (iocbq->sli4_xritag == NO_XRI)
961 sglq = NULL;
962 else
James Smart6d368e52011-05-24 11:44:12 -0400963 sglq = __lpfc_clear_active_sglq(phba, iocbq->sli4_lxritag);
964
James Smart4f774512009-05-22 14:52:35 -0400965 if (sglq) {
James Smart0f65ff62010-02-26 14:14:23 -0500966 if ((iocbq->iocb_flag & LPFC_EXCHANGE_BUSY) &&
967 (sglq->state != SGL_XRI_ABORTED)) {
James Smart4f774512009-05-22 14:52:35 -0400968 spin_lock_irqsave(&phba->sli4_hba.abts_sgl_list_lock,
969 iflag);
970 list_add(&sglq->list,
971 &phba->sli4_hba.lpfc_abts_els_sgl_list);
972 spin_unlock_irqrestore(
973 &phba->sli4_hba.abts_sgl_list_lock, iflag);
James Smart0f65ff62010-02-26 14:14:23 -0500974 } else {
975 sglq->state = SGL_FREED;
James Smart19ca7602010-11-20 23:11:55 -0500976 sglq->ndlp = NULL;
James Smartfedd3b72011-02-16 12:39:24 -0500977 list_add_tail(&sglq->list,
978 &phba->sli4_hba.lpfc_sgl_list);
James Smart2a9bf3d2010-06-07 15:24:45 -0400979
980 /* Check if TXQ queue needs to be serviced */
James Smart589a52d2010-07-14 15:30:54 -0400981 if (pring->txq_cnt)
James Smart2a9bf3d2010-06-07 15:24:45 -0400982 lpfc_worker_wake_up(phba);
James Smart0f65ff62010-02-26 14:14:23 -0500983 }
James Smart4f774512009-05-22 14:52:35 -0400984 }
985
986
987 /*
988 * Clean all volatile data fields, preserve iotag and node struct.
989 */
990 memset((char *)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
James Smart6d368e52011-05-24 11:44:12 -0400991 iocbq->sli4_lxritag = NO_XRI;
James Smart4f774512009-05-22 14:52:35 -0400992 iocbq->sli4_xritag = NO_XRI;
993 list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
994}
995
James Smart2a9bf3d2010-06-07 15:24:45 -0400996
James Smart4f774512009-05-22 14:52:35 -0400997/**
James Smart3772a992009-05-22 14:50:54 -0400998 * __lpfc_sli_release_iocbq_s3 - Release iocb to the iocb pool
999 * @phba: Pointer to HBA context object.
1000 * @iocbq: Pointer to driver iocb object.
1001 *
1002 * This function is called with hbalock held to release driver
1003 * iocb object to the iocb pool. The iotag in the iocb object
1004 * does not change for each use of the iocb object. This function
1005 * clears all other fields of the iocb object when it is freed.
1006 **/
1007static void
1008__lpfc_sli_release_iocbq_s3(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1009{
1010 size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
1011
1012 /*
1013 * Clean all volatile data fields, preserve iotag and node struct.
1014 */
1015 memset((char*)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
1016 iocbq->sli4_xritag = NO_XRI;
1017 list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
1018}
1019
1020/**
James Smart3621a712009-04-06 18:47:14 -04001021 * __lpfc_sli_release_iocbq - Release iocb to the iocb pool
James Smarte59058c2008-08-24 21:49:00 -04001022 * @phba: Pointer to HBA context object.
1023 * @iocbq: Pointer to driver iocb object.
1024 *
1025 * This function is called with hbalock held to release driver
1026 * iocb object to the iocb pool. The iotag in the iocb object
1027 * does not change for each use of the iocb object. This function
1028 * clears all other fields of the iocb object when it is freed.
1029 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01001030static void
James Smart2e0fef82007-06-17 19:56:36 -05001031__lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1032{
James Smart3772a992009-05-22 14:50:54 -04001033 phba->__lpfc_sli_release_iocbq(phba, iocbq);
James Smart2a9bf3d2010-06-07 15:24:45 -04001034 phba->iocb_cnt--;
James Bottomley604a3e32005-10-29 10:28:33 -05001035}
1036
James Smarte59058c2008-08-24 21:49:00 -04001037/**
James Smart3621a712009-04-06 18:47:14 -04001038 * lpfc_sli_release_iocbq - Release iocb to the iocb pool
James Smarte59058c2008-08-24 21:49:00 -04001039 * @phba: Pointer to HBA context object.
1040 * @iocbq: Pointer to driver iocb object.
1041 *
1042 * This function is called with no lock held to release the iocb to
1043 * iocb pool.
1044 **/
James Smart2e0fef82007-06-17 19:56:36 -05001045void
1046lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1047{
1048 unsigned long iflags;
1049
1050 /*
1051 * Clean all volatile data fields, preserve iotag and node struct.
1052 */
1053 spin_lock_irqsave(&phba->hbalock, iflags);
1054 __lpfc_sli_release_iocbq(phba, iocbq);
1055 spin_unlock_irqrestore(&phba->hbalock, iflags);
1056}
1057
James Smarte59058c2008-08-24 21:49:00 -04001058/**
James Smarta257bf92009-04-06 18:48:10 -04001059 * lpfc_sli_cancel_iocbs - Cancel all iocbs from a list.
1060 * @phba: Pointer to HBA context object.
1061 * @iocblist: List of IOCBs.
1062 * @ulpstatus: ULP status in IOCB command field.
1063 * @ulpWord4: ULP word-4 in IOCB command field.
1064 *
1065 * This function is called with a list of IOCBs to cancel. It cancels the IOCB
1066 * on the list by invoking the complete callback function associated with the
1067 * IOCB with the provided @ulpstatus and @ulpword4 set to the IOCB commond
1068 * fields.
1069 **/
1070void
1071lpfc_sli_cancel_iocbs(struct lpfc_hba *phba, struct list_head *iocblist,
1072 uint32_t ulpstatus, uint32_t ulpWord4)
1073{
1074 struct lpfc_iocbq *piocb;
1075
1076 while (!list_empty(iocblist)) {
1077 list_remove_head(iocblist, piocb, struct lpfc_iocbq, list);
1078
1079 if (!piocb->iocb_cmpl)
1080 lpfc_sli_release_iocbq(phba, piocb);
1081 else {
1082 piocb->iocb.ulpStatus = ulpstatus;
1083 piocb->iocb.un.ulpWord[4] = ulpWord4;
1084 (piocb->iocb_cmpl) (phba, piocb, piocb);
1085 }
1086 }
1087 return;
1088}
1089
1090/**
James Smart3621a712009-04-06 18:47:14 -04001091 * lpfc_sli_iocb_cmd_type - Get the iocb type
1092 * @iocb_cmnd: iocb command code.
James Smarte59058c2008-08-24 21:49:00 -04001093 *
1094 * This function is called by ring event handler function to get the iocb type.
1095 * This function translates the iocb command to an iocb command type used to
1096 * decide the final disposition of each completed IOCB.
1097 * The function returns
1098 * LPFC_UNKNOWN_IOCB if it is an unsupported iocb
1099 * LPFC_SOL_IOCB if it is a solicited iocb completion
1100 * LPFC_ABORT_IOCB if it is an abort iocb
1101 * LPFC_UNSOL_IOCB if it is an unsolicited iocb
1102 *
1103 * The caller is not required to hold any lock.
1104 **/
dea31012005-04-17 16:05:31 -05001105static lpfc_iocb_type
1106lpfc_sli_iocb_cmd_type(uint8_t iocb_cmnd)
1107{
1108 lpfc_iocb_type type = LPFC_UNKNOWN_IOCB;
1109
1110 if (iocb_cmnd > CMD_MAX_IOCB_CMD)
1111 return 0;
1112
1113 switch (iocb_cmnd) {
1114 case CMD_XMIT_SEQUENCE_CR:
1115 case CMD_XMIT_SEQUENCE_CX:
1116 case CMD_XMIT_BCAST_CN:
1117 case CMD_XMIT_BCAST_CX:
1118 case CMD_ELS_REQUEST_CR:
1119 case CMD_ELS_REQUEST_CX:
1120 case CMD_CREATE_XRI_CR:
1121 case CMD_CREATE_XRI_CX:
1122 case CMD_GET_RPI_CN:
1123 case CMD_XMIT_ELS_RSP_CX:
1124 case CMD_GET_RPI_CR:
1125 case CMD_FCP_IWRITE_CR:
1126 case CMD_FCP_IWRITE_CX:
1127 case CMD_FCP_IREAD_CR:
1128 case CMD_FCP_IREAD_CX:
1129 case CMD_FCP_ICMND_CR:
1130 case CMD_FCP_ICMND_CX:
James Smartf5603512006-12-02 13:35:43 -05001131 case CMD_FCP_TSEND_CX:
1132 case CMD_FCP_TRSP_CX:
1133 case CMD_FCP_TRECEIVE_CX:
1134 case CMD_FCP_AUTO_TRSP_CX:
dea31012005-04-17 16:05:31 -05001135 case CMD_ADAPTER_MSG:
1136 case CMD_ADAPTER_DUMP:
1137 case CMD_XMIT_SEQUENCE64_CR:
1138 case CMD_XMIT_SEQUENCE64_CX:
1139 case CMD_XMIT_BCAST64_CN:
1140 case CMD_XMIT_BCAST64_CX:
1141 case CMD_ELS_REQUEST64_CR:
1142 case CMD_ELS_REQUEST64_CX:
1143 case CMD_FCP_IWRITE64_CR:
1144 case CMD_FCP_IWRITE64_CX:
1145 case CMD_FCP_IREAD64_CR:
1146 case CMD_FCP_IREAD64_CX:
1147 case CMD_FCP_ICMND64_CR:
1148 case CMD_FCP_ICMND64_CX:
James Smartf5603512006-12-02 13:35:43 -05001149 case CMD_FCP_TSEND64_CX:
1150 case CMD_FCP_TRSP64_CX:
1151 case CMD_FCP_TRECEIVE64_CX:
dea31012005-04-17 16:05:31 -05001152 case CMD_GEN_REQUEST64_CR:
1153 case CMD_GEN_REQUEST64_CX:
1154 case CMD_XMIT_ELS_RSP64_CX:
James Smartda0436e2009-05-22 14:51:39 -04001155 case DSSCMD_IWRITE64_CR:
1156 case DSSCMD_IWRITE64_CX:
1157 case DSSCMD_IREAD64_CR:
1158 case DSSCMD_IREAD64_CX:
dea31012005-04-17 16:05:31 -05001159 type = LPFC_SOL_IOCB;
1160 break;
1161 case CMD_ABORT_XRI_CN:
1162 case CMD_ABORT_XRI_CX:
1163 case CMD_CLOSE_XRI_CN:
1164 case CMD_CLOSE_XRI_CX:
1165 case CMD_XRI_ABORTED_CX:
1166 case CMD_ABORT_MXRI64_CN:
James Smart6669f9b2009-10-02 15:16:45 -04001167 case CMD_XMIT_BLS_RSP64_CX:
dea31012005-04-17 16:05:31 -05001168 type = LPFC_ABORT_IOCB;
1169 break;
1170 case CMD_RCV_SEQUENCE_CX:
1171 case CMD_RCV_ELS_REQ_CX:
1172 case CMD_RCV_SEQUENCE64_CX:
1173 case CMD_RCV_ELS_REQ64_CX:
James Smart57127f12007-10-27 13:37:05 -04001174 case CMD_ASYNC_STATUS:
James Smarted957682007-06-17 19:56:37 -05001175 case CMD_IOCB_RCV_SEQ64_CX:
1176 case CMD_IOCB_RCV_ELS64_CX:
1177 case CMD_IOCB_RCV_CONT64_CX:
James Smart3163f722008-02-08 18:50:25 -05001178 case CMD_IOCB_RET_XRI64_CX:
dea31012005-04-17 16:05:31 -05001179 type = LPFC_UNSOL_IOCB;
1180 break;
James Smart3163f722008-02-08 18:50:25 -05001181 case CMD_IOCB_XMIT_MSEQ64_CR:
1182 case CMD_IOCB_XMIT_MSEQ64_CX:
1183 case CMD_IOCB_RCV_SEQ_LIST64_CX:
1184 case CMD_IOCB_RCV_ELS_LIST64_CX:
1185 case CMD_IOCB_CLOSE_EXTENDED_CN:
1186 case CMD_IOCB_ABORT_EXTENDED_CN:
1187 case CMD_IOCB_RET_HBQE64_CN:
1188 case CMD_IOCB_FCP_IBIDIR64_CR:
1189 case CMD_IOCB_FCP_IBIDIR64_CX:
1190 case CMD_IOCB_FCP_ITASKMGT64_CX:
1191 case CMD_IOCB_LOGENTRY_CN:
1192 case CMD_IOCB_LOGENTRY_ASYNC_CN:
1193 printk("%s - Unhandled SLI-3 Command x%x\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07001194 __func__, iocb_cmnd);
James Smart3163f722008-02-08 18:50:25 -05001195 type = LPFC_UNKNOWN_IOCB;
1196 break;
dea31012005-04-17 16:05:31 -05001197 default:
1198 type = LPFC_UNKNOWN_IOCB;
1199 break;
1200 }
1201
1202 return type;
1203}
1204
James Smarte59058c2008-08-24 21:49:00 -04001205/**
James Smart3621a712009-04-06 18:47:14 -04001206 * lpfc_sli_ring_map - Issue config_ring mbox for all rings
James Smarte59058c2008-08-24 21:49:00 -04001207 * @phba: Pointer to HBA context object.
1208 *
1209 * This function is called from SLI initialization code
1210 * to configure every ring of the HBA's SLI interface. The
1211 * caller is not required to hold any lock. This function issues
1212 * a config_ring mailbox command for each ring.
1213 * This function returns zero if successful else returns a negative
1214 * error code.
1215 **/
dea31012005-04-17 16:05:31 -05001216static int
James Smarted957682007-06-17 19:56:37 -05001217lpfc_sli_ring_map(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05001218{
1219 struct lpfc_sli *psli = &phba->sli;
James Smarted957682007-06-17 19:56:37 -05001220 LPFC_MBOXQ_t *pmb;
1221 MAILBOX_t *pmbox;
1222 int i, rc, ret = 0;
dea31012005-04-17 16:05:31 -05001223
James Smarted957682007-06-17 19:56:37 -05001224 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1225 if (!pmb)
1226 return -ENOMEM;
James Smart04c68492009-05-22 14:52:52 -04001227 pmbox = &pmb->u.mb;
James Smarted957682007-06-17 19:56:37 -05001228 phba->link_state = LPFC_INIT_MBX_CMDS;
dea31012005-04-17 16:05:31 -05001229 for (i = 0; i < psli->num_rings; i++) {
dea31012005-04-17 16:05:31 -05001230 lpfc_config_ring(phba, i, pmb);
1231 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
1232 if (rc != MBX_SUCCESS) {
James Smart92d7f7b2007-06-17 19:56:38 -05001233 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04001234 "0446 Adapter failed to init (%d), "
dea31012005-04-17 16:05:31 -05001235 "mbxCmd x%x CFG_RING, mbxStatus x%x, "
1236 "ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04001237 rc, pmbox->mbxCommand,
1238 pmbox->mbxStatus, i);
James Smart2e0fef82007-06-17 19:56:36 -05001239 phba->link_state = LPFC_HBA_ERROR;
James Smarted957682007-06-17 19:56:37 -05001240 ret = -ENXIO;
1241 break;
dea31012005-04-17 16:05:31 -05001242 }
1243 }
James Smarted957682007-06-17 19:56:37 -05001244 mempool_free(pmb, phba->mbox_mem_pool);
1245 return ret;
dea31012005-04-17 16:05:31 -05001246}
1247
James Smarte59058c2008-08-24 21:49:00 -04001248/**
James Smart3621a712009-04-06 18:47:14 -04001249 * lpfc_sli_ringtxcmpl_put - Adds new iocb to the txcmplq
James Smarte59058c2008-08-24 21:49:00 -04001250 * @phba: Pointer to HBA context object.
1251 * @pring: Pointer to driver SLI ring object.
1252 * @piocb: Pointer to the driver iocb object.
1253 *
1254 * This function is called with hbalock held. The function adds the
1255 * new iocb to txcmplq of the given ring. This function always returns
1256 * 0. If this function is called for ELS ring, this function checks if
1257 * there is a vport associated with the ELS command. This function also
1258 * starts els_tmofunc timer if this is an ELS command.
1259 **/
dea31012005-04-17 16:05:31 -05001260static int
James Smart2e0fef82007-06-17 19:56:36 -05001261lpfc_sli_ringtxcmpl_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1262 struct lpfc_iocbq *piocb)
dea31012005-04-17 16:05:31 -05001263{
dea31012005-04-17 16:05:31 -05001264 list_add_tail(&piocb->list, &pring->txcmplq);
James Smart4f2e66c2012-05-09 21:17:07 -04001265 piocb->iocb_flag |= LPFC_IO_ON_TXCMPLQ;
dea31012005-04-17 16:05:31 -05001266 pring->txcmplq_cnt++;
James Smart2a9bf3d2010-06-07 15:24:45 -04001267 if (pring->txcmplq_cnt > pring->txcmplq_max)
1268 pring->txcmplq_max = pring->txcmplq_cnt;
1269
James Smart92d7f7b2007-06-17 19:56:38 -05001270 if ((unlikely(pring->ringno == LPFC_ELS_RING)) &&
1271 (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
1272 (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
1273 if (!piocb->vport)
1274 BUG();
1275 else
1276 mod_timer(&piocb->vport->els_tmofunc,
1277 jiffies + HZ * (phba->fc_ratov << 1));
1278 }
1279
dea31012005-04-17 16:05:31 -05001280
James Smart2e0fef82007-06-17 19:56:36 -05001281 return 0;
dea31012005-04-17 16:05:31 -05001282}
1283
James Smarte59058c2008-08-24 21:49:00 -04001284/**
James Smart3621a712009-04-06 18:47:14 -04001285 * lpfc_sli_ringtx_get - Get first element of the txq
James Smarte59058c2008-08-24 21:49:00 -04001286 * @phba: Pointer to HBA context object.
1287 * @pring: Pointer to driver SLI ring object.
1288 *
1289 * This function is called with hbalock held to get next
1290 * iocb in txq of the given ring. If there is any iocb in
1291 * the txq, the function returns first iocb in the list after
1292 * removing the iocb from the list, else it returns NULL.
1293 **/
James Smart2a9bf3d2010-06-07 15:24:45 -04001294struct lpfc_iocbq *
James Smart2e0fef82007-06-17 19:56:36 -05001295lpfc_sli_ringtx_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -05001296{
dea31012005-04-17 16:05:31 -05001297 struct lpfc_iocbq *cmd_iocb;
1298
James Smart858c9f62007-06-17 19:56:39 -05001299 list_remove_head((&pring->txq), cmd_iocb, struct lpfc_iocbq, list);
1300 if (cmd_iocb != NULL)
dea31012005-04-17 16:05:31 -05001301 pring->txq_cnt--;
James Smart2e0fef82007-06-17 19:56:36 -05001302 return cmd_iocb;
dea31012005-04-17 16:05:31 -05001303}
1304
James Smarte59058c2008-08-24 21:49:00 -04001305/**
James Smart3621a712009-04-06 18:47:14 -04001306 * lpfc_sli_next_iocb_slot - Get next iocb slot in the ring
James Smarte59058c2008-08-24 21:49:00 -04001307 * @phba: Pointer to HBA context object.
1308 * @pring: Pointer to driver SLI ring object.
1309 *
1310 * This function is called with hbalock held and the caller must post the
1311 * iocb without releasing the lock. If the caller releases the lock,
1312 * iocb slot returned by the function is not guaranteed to be available.
1313 * The function returns pointer to the next available iocb slot if there
1314 * is available slot in the ring, else it returns NULL.
1315 * If the get index of the ring is ahead of the put index, the function
1316 * will post an error attention event to the worker thread to take the
1317 * HBA to offline state.
1318 **/
dea31012005-04-17 16:05:31 -05001319static IOCB_t *
1320lpfc_sli_next_iocb_slot (struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
1321{
James Smart34b02dc2008-08-24 21:49:55 -04001322 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
dea31012005-04-17 16:05:31 -05001323 uint32_t max_cmd_idx = pring->numCiocb;
dea31012005-04-17 16:05:31 -05001324 if ((pring->next_cmdidx == pring->cmdidx) &&
1325 (++pring->next_cmdidx >= max_cmd_idx))
1326 pring->next_cmdidx = 0;
1327
1328 if (unlikely(pring->local_getidx == pring->next_cmdidx)) {
1329
1330 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
1331
1332 if (unlikely(pring->local_getidx >= max_cmd_idx)) {
1333 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001334 "0315 Ring %d issue: portCmdGet %d "
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02001335 "is bigger than cmd ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04001336 pring->ringno,
dea31012005-04-17 16:05:31 -05001337 pring->local_getidx, max_cmd_idx);
1338
James Smart2e0fef82007-06-17 19:56:36 -05001339 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05001340 /*
1341 * All error attention handlers are posted to
1342 * worker thread
1343 */
1344 phba->work_ha |= HA_ERATT;
1345 phba->work_hs = HS_FFER3;
James Smart92d7f7b2007-06-17 19:56:38 -05001346
James Smart5e9d9b82008-06-14 22:52:53 -04001347 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -05001348
1349 return NULL;
1350 }
1351
1352 if (pring->local_getidx == pring->next_cmdidx)
1353 return NULL;
1354 }
1355
James Smarted957682007-06-17 19:56:37 -05001356 return lpfc_cmd_iocb(phba, pring);
dea31012005-04-17 16:05:31 -05001357}
1358
James Smarte59058c2008-08-24 21:49:00 -04001359/**
James Smart3621a712009-04-06 18:47:14 -04001360 * lpfc_sli_next_iotag - Get an iotag for the iocb
James Smarte59058c2008-08-24 21:49:00 -04001361 * @phba: Pointer to HBA context object.
1362 * @iocbq: Pointer to driver iocb object.
1363 *
1364 * This function gets an iotag for the iocb. If there is no unused iotag and
1365 * the iocbq_lookup_len < 0xffff, this function allocates a bigger iotag_lookup
1366 * array and assigns a new iotag.
1367 * The function returns the allocated iotag if successful, else returns zero.
1368 * Zero is not a valid iotag.
1369 * The caller is not required to hold any lock.
1370 **/
James Bottomley604a3e32005-10-29 10:28:33 -05001371uint16_t
James Smart2e0fef82007-06-17 19:56:36 -05001372lpfc_sli_next_iotag(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
dea31012005-04-17 16:05:31 -05001373{
James Smart2e0fef82007-06-17 19:56:36 -05001374 struct lpfc_iocbq **new_arr;
1375 struct lpfc_iocbq **old_arr;
James Bottomley604a3e32005-10-29 10:28:33 -05001376 size_t new_len;
1377 struct lpfc_sli *psli = &phba->sli;
1378 uint16_t iotag;
dea31012005-04-17 16:05:31 -05001379
James Smart2e0fef82007-06-17 19:56:36 -05001380 spin_lock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001381 iotag = psli->last_iotag;
1382 if(++iotag < psli->iocbq_lookup_len) {
1383 psli->last_iotag = iotag;
1384 psli->iocbq_lookup[iotag] = iocbq;
James Smart2e0fef82007-06-17 19:56:36 -05001385 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001386 iocbq->iotag = iotag;
1387 return iotag;
James Smart2e0fef82007-06-17 19:56:36 -05001388 } else if (psli->iocbq_lookup_len < (0xffff
James Bottomley604a3e32005-10-29 10:28:33 -05001389 - LPFC_IOCBQ_LOOKUP_INCREMENT)) {
1390 new_len = psli->iocbq_lookup_len + LPFC_IOCBQ_LOOKUP_INCREMENT;
James Smart2e0fef82007-06-17 19:56:36 -05001391 spin_unlock_irq(&phba->hbalock);
1392 new_arr = kzalloc(new_len * sizeof (struct lpfc_iocbq *),
James Bottomley604a3e32005-10-29 10:28:33 -05001393 GFP_KERNEL);
1394 if (new_arr) {
James Smart2e0fef82007-06-17 19:56:36 -05001395 spin_lock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001396 old_arr = psli->iocbq_lookup;
1397 if (new_len <= psli->iocbq_lookup_len) {
1398 /* highly unprobable case */
1399 kfree(new_arr);
1400 iotag = psli->last_iotag;
1401 if(++iotag < psli->iocbq_lookup_len) {
1402 psli->last_iotag = iotag;
1403 psli->iocbq_lookup[iotag] = iocbq;
James Smart2e0fef82007-06-17 19:56:36 -05001404 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001405 iocbq->iotag = iotag;
1406 return iotag;
1407 }
James Smart2e0fef82007-06-17 19:56:36 -05001408 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001409 return 0;
1410 }
1411 if (psli->iocbq_lookup)
1412 memcpy(new_arr, old_arr,
1413 ((psli->last_iotag + 1) *
James Smart311464e2007-08-02 11:10:37 -04001414 sizeof (struct lpfc_iocbq *)));
James Bottomley604a3e32005-10-29 10:28:33 -05001415 psli->iocbq_lookup = new_arr;
1416 psli->iocbq_lookup_len = new_len;
1417 psli->last_iotag = iotag;
1418 psli->iocbq_lookup[iotag] = iocbq;
James Smart2e0fef82007-06-17 19:56:36 -05001419 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001420 iocbq->iotag = iotag;
1421 kfree(old_arr);
1422 return iotag;
1423 }
James Smart8f6d98d2006-08-01 07:34:00 -04001424 } else
James Smart2e0fef82007-06-17 19:56:36 -05001425 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001426
James Smartbc739052010-08-04 16:11:18 -04001427 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001428 "0318 Failed to allocate IOTAG.last IOTAG is %d\n",
1429 psli->last_iotag);
dea31012005-04-17 16:05:31 -05001430
James Bottomley604a3e32005-10-29 10:28:33 -05001431 return 0;
dea31012005-04-17 16:05:31 -05001432}
1433
James Smarte59058c2008-08-24 21:49:00 -04001434/**
James Smart3621a712009-04-06 18:47:14 -04001435 * lpfc_sli_submit_iocb - Submit an iocb to the firmware
James Smarte59058c2008-08-24 21:49:00 -04001436 * @phba: Pointer to HBA context object.
1437 * @pring: Pointer to driver SLI ring object.
1438 * @iocb: Pointer to iocb slot in the ring.
1439 * @nextiocb: Pointer to driver iocb object which need to be
1440 * posted to firmware.
1441 *
1442 * This function is called with hbalock held to post a new iocb to
1443 * the firmware. This function copies the new iocb to ring iocb slot and
1444 * updates the ring pointers. It adds the new iocb to txcmplq if there is
1445 * a completion call back for this iocb else the function will free the
1446 * iocb object.
1447 **/
dea31012005-04-17 16:05:31 -05001448static void
1449lpfc_sli_submit_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1450 IOCB_t *iocb, struct lpfc_iocbq *nextiocb)
1451{
1452 /*
James Bottomley604a3e32005-10-29 10:28:33 -05001453 * Set up an iotag
dea31012005-04-17 16:05:31 -05001454 */
James Bottomley604a3e32005-10-29 10:28:33 -05001455 nextiocb->iocb.ulpIoTag = (nextiocb->iocb_cmpl) ? nextiocb->iotag : 0;
dea31012005-04-17 16:05:31 -05001456
James Smarte2a0a9d2008-12-04 22:40:02 -05001457
James Smarta58cbd52007-08-02 11:09:43 -04001458 if (pring->ringno == LPFC_ELS_RING) {
1459 lpfc_debugfs_slow_ring_trc(phba,
1460 "IOCB cmd ring: wd4:x%08x wd6:x%08x wd7:x%08x",
1461 *(((uint32_t *) &nextiocb->iocb) + 4),
1462 *(((uint32_t *) &nextiocb->iocb) + 6),
1463 *(((uint32_t *) &nextiocb->iocb) + 7));
1464 }
1465
dea31012005-04-17 16:05:31 -05001466 /*
1467 * Issue iocb command to adapter
1468 */
James Smart92d7f7b2007-06-17 19:56:38 -05001469 lpfc_sli_pcimem_bcopy(&nextiocb->iocb, iocb, phba->iocb_cmd_size);
dea31012005-04-17 16:05:31 -05001470 wmb();
1471 pring->stats.iocb_cmd++;
1472
1473 /*
1474 * If there is no completion routine to call, we can release the
1475 * IOCB buffer back right now. For IOCBs, like QUE_RING_BUF,
1476 * that have no rsp ring completion, iocb_cmpl MUST be NULL.
1477 */
1478 if (nextiocb->iocb_cmpl)
1479 lpfc_sli_ringtxcmpl_put(phba, pring, nextiocb);
James Bottomley604a3e32005-10-29 10:28:33 -05001480 else
James Smart2e0fef82007-06-17 19:56:36 -05001481 __lpfc_sli_release_iocbq(phba, nextiocb);
dea31012005-04-17 16:05:31 -05001482
1483 /*
1484 * Let the HBA know what IOCB slot will be the next one the
1485 * driver will put a command into.
1486 */
1487 pring->cmdidx = pring->next_cmdidx;
James Smarted957682007-06-17 19:56:37 -05001488 writel(pring->cmdidx, &phba->host_gp[pring->ringno].cmdPutInx);
dea31012005-04-17 16:05:31 -05001489}
1490
James Smarte59058c2008-08-24 21:49:00 -04001491/**
James Smart3621a712009-04-06 18:47:14 -04001492 * lpfc_sli_update_full_ring - Update the chip attention register
James Smarte59058c2008-08-24 21:49:00 -04001493 * @phba: Pointer to HBA context object.
1494 * @pring: Pointer to driver SLI ring object.
1495 *
1496 * The caller is not required to hold any lock for calling this function.
1497 * This function updates the chip attention bits for the ring to inform firmware
1498 * that there are pending work to be done for this ring and requests an
1499 * interrupt when there is space available in the ring. This function is
1500 * called when the driver is unable to post more iocbs to the ring due
1501 * to unavailability of space in the ring.
1502 **/
dea31012005-04-17 16:05:31 -05001503static void
James Smart2e0fef82007-06-17 19:56:36 -05001504lpfc_sli_update_full_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -05001505{
1506 int ringno = pring->ringno;
1507
1508 pring->flag |= LPFC_CALL_RING_AVAILABLE;
1509
1510 wmb();
1511
1512 /*
1513 * Set ring 'ringno' to SET R0CE_REQ in Chip Att register.
1514 * The HBA will tell us when an IOCB entry is available.
1515 */
1516 writel((CA_R0ATT|CA_R0CE_REQ) << (ringno*4), phba->CAregaddr);
1517 readl(phba->CAregaddr); /* flush */
1518
1519 pring->stats.iocb_cmd_full++;
1520}
1521
James Smarte59058c2008-08-24 21:49:00 -04001522/**
James Smart3621a712009-04-06 18:47:14 -04001523 * lpfc_sli_update_ring - Update chip attention register
James Smarte59058c2008-08-24 21:49:00 -04001524 * @phba: Pointer to HBA context object.
1525 * @pring: Pointer to driver SLI ring object.
1526 *
1527 * This function updates the chip attention register bit for the
1528 * given ring to inform HBA that there is more work to be done
1529 * in this ring. The caller is not required to hold any lock.
1530 **/
dea31012005-04-17 16:05:31 -05001531static void
James Smart2e0fef82007-06-17 19:56:36 -05001532lpfc_sli_update_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -05001533{
1534 int ringno = pring->ringno;
1535
1536 /*
1537 * Tell the HBA that there is work to do in this ring.
1538 */
James Smart34b02dc2008-08-24 21:49:55 -04001539 if (!(phba->sli3_options & LPFC_SLI3_CRP_ENABLED)) {
1540 wmb();
1541 writel(CA_R0ATT << (ringno * 4), phba->CAregaddr);
1542 readl(phba->CAregaddr); /* flush */
1543 }
dea31012005-04-17 16:05:31 -05001544}
1545
James Smarte59058c2008-08-24 21:49:00 -04001546/**
James Smart3621a712009-04-06 18:47:14 -04001547 * lpfc_sli_resume_iocb - Process iocbs in the txq
James Smarte59058c2008-08-24 21:49:00 -04001548 * @phba: Pointer to HBA context object.
1549 * @pring: Pointer to driver SLI ring object.
1550 *
1551 * This function is called with hbalock held to post pending iocbs
1552 * in the txq to the firmware. This function is called when driver
1553 * detects space available in the ring.
1554 **/
dea31012005-04-17 16:05:31 -05001555static void
James Smart2e0fef82007-06-17 19:56:36 -05001556lpfc_sli_resume_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -05001557{
1558 IOCB_t *iocb;
1559 struct lpfc_iocbq *nextiocb;
1560
1561 /*
1562 * Check to see if:
1563 * (a) there is anything on the txq to send
1564 * (b) link is up
1565 * (c) link attention events can be processed (fcp ring only)
1566 * (d) IOCB processing is not blocked by the outstanding mbox command.
1567 */
1568 if (pring->txq_cnt &&
James Smart2e0fef82007-06-17 19:56:36 -05001569 lpfc_is_link_up(phba) &&
dea31012005-04-17 16:05:31 -05001570 (pring->ringno != phba->sli.fcp_ring ||
James Smart0b727fe2007-10-27 13:37:25 -04001571 phba->sli.sli_flag & LPFC_PROCESS_LA)) {
dea31012005-04-17 16:05:31 -05001572
1573 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
1574 (nextiocb = lpfc_sli_ringtx_get(phba, pring)))
1575 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
1576
1577 if (iocb)
1578 lpfc_sli_update_ring(phba, pring);
1579 else
1580 lpfc_sli_update_full_ring(phba, pring);
1581 }
1582
1583 return;
1584}
1585
James Smarte59058c2008-08-24 21:49:00 -04001586/**
James Smart3621a712009-04-06 18:47:14 -04001587 * lpfc_sli_next_hbq_slot - Get next hbq entry for the HBQ
James Smarte59058c2008-08-24 21:49:00 -04001588 * @phba: Pointer to HBA context object.
1589 * @hbqno: HBQ number.
1590 *
1591 * This function is called with hbalock held to get the next
1592 * available slot for the given HBQ. If there is free slot
1593 * available for the HBQ it will return pointer to the next available
1594 * HBQ entry else it will return NULL.
1595 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01001596static struct lpfc_hbq_entry *
James Smarted957682007-06-17 19:56:37 -05001597lpfc_sli_next_hbq_slot(struct lpfc_hba *phba, uint32_t hbqno)
1598{
1599 struct hbq_s *hbqp = &phba->hbqs[hbqno];
1600
1601 if (hbqp->next_hbqPutIdx == hbqp->hbqPutIdx &&
1602 ++hbqp->next_hbqPutIdx >= hbqp->entry_count)
1603 hbqp->next_hbqPutIdx = 0;
1604
1605 if (unlikely(hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)) {
James Smart92d7f7b2007-06-17 19:56:38 -05001606 uint32_t raw_index = phba->hbq_get[hbqno];
James Smarted957682007-06-17 19:56:37 -05001607 uint32_t getidx = le32_to_cpu(raw_index);
1608
1609 hbqp->local_hbqGetIdx = getidx;
1610
1611 if (unlikely(hbqp->local_hbqGetIdx >= hbqp->entry_count)) {
1612 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -05001613 LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04001614 "1802 HBQ %d: local_hbqGetIdx "
James Smarted957682007-06-17 19:56:37 -05001615 "%u is > than hbqp->entry_count %u\n",
James Smarte8b62012007-08-02 11:10:09 -04001616 hbqno, hbqp->local_hbqGetIdx,
James Smarted957682007-06-17 19:56:37 -05001617 hbqp->entry_count);
1618
1619 phba->link_state = LPFC_HBA_ERROR;
1620 return NULL;
1621 }
1622
1623 if (hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)
1624 return NULL;
1625 }
1626
James Smart51ef4c22007-08-02 11:10:31 -04001627 return (struct lpfc_hbq_entry *) phba->hbqs[hbqno].hbq_virt +
1628 hbqp->hbqPutIdx;
James Smarted957682007-06-17 19:56:37 -05001629}
1630
James Smarte59058c2008-08-24 21:49:00 -04001631/**
James Smart3621a712009-04-06 18:47:14 -04001632 * lpfc_sli_hbqbuf_free_all - Free all the hbq buffers
James Smarte59058c2008-08-24 21:49:00 -04001633 * @phba: Pointer to HBA context object.
1634 *
1635 * This function is called with no lock held to free all the
1636 * hbq buffers while uninitializing the SLI interface. It also
1637 * frees the HBQ buffers returned by the firmware but not yet
1638 * processed by the upper layers.
1639 **/
James Smarted957682007-06-17 19:56:37 -05001640void
1641lpfc_sli_hbqbuf_free_all(struct lpfc_hba *phba)
1642{
James Smart92d7f7b2007-06-17 19:56:38 -05001643 struct lpfc_dmabuf *dmabuf, *next_dmabuf;
1644 struct hbq_dmabuf *hbq_buf;
James Smart3163f722008-02-08 18:50:25 -05001645 unsigned long flags;
James Smart51ef4c22007-08-02 11:10:31 -04001646 int i, hbq_count;
James Smart3163f722008-02-08 18:50:25 -05001647 uint32_t hbqno;
James Smarted957682007-06-17 19:56:37 -05001648
James Smart51ef4c22007-08-02 11:10:31 -04001649 hbq_count = lpfc_sli_hbq_count();
James Smarted957682007-06-17 19:56:37 -05001650 /* Return all memory used by all HBQs */
James Smart3163f722008-02-08 18:50:25 -05001651 spin_lock_irqsave(&phba->hbalock, flags);
James Smart51ef4c22007-08-02 11:10:31 -04001652 for (i = 0; i < hbq_count; ++i) {
1653 list_for_each_entry_safe(dmabuf, next_dmabuf,
1654 &phba->hbqs[i].hbq_buffer_list, list) {
1655 hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
1656 list_del(&hbq_buf->dbuf.list);
1657 (phba->hbqs[i].hbq_free_buffer)(phba, hbq_buf);
1658 }
James Smarta8adb832007-10-27 13:37:53 -04001659 phba->hbqs[i].buffer_count = 0;
James Smarted957682007-06-17 19:56:37 -05001660 }
James Smart3163f722008-02-08 18:50:25 -05001661 /* Return all HBQ buffer that are in-fly */
James Smart3772a992009-05-22 14:50:54 -04001662 list_for_each_entry_safe(dmabuf, next_dmabuf, &phba->rb_pend_list,
1663 list) {
James Smart3163f722008-02-08 18:50:25 -05001664 hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
1665 list_del(&hbq_buf->dbuf.list);
1666 if (hbq_buf->tag == -1) {
1667 (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
1668 (phba, hbq_buf);
1669 } else {
1670 hbqno = hbq_buf->tag >> 16;
1671 if (hbqno >= LPFC_MAX_HBQS)
1672 (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
1673 (phba, hbq_buf);
1674 else
1675 (phba->hbqs[hbqno].hbq_free_buffer)(phba,
1676 hbq_buf);
1677 }
1678 }
1679
1680 /* Mark the HBQs not in use */
1681 phba->hbq_in_use = 0;
1682 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smarted957682007-06-17 19:56:37 -05001683}
1684
James Smarte59058c2008-08-24 21:49:00 -04001685/**
James Smart3621a712009-04-06 18:47:14 -04001686 * lpfc_sli_hbq_to_firmware - Post the hbq buffer to firmware
James Smarte59058c2008-08-24 21:49:00 -04001687 * @phba: Pointer to HBA context object.
1688 * @hbqno: HBQ number.
1689 * @hbq_buf: Pointer to HBQ buffer.
1690 *
1691 * This function is called with the hbalock held to post a
1692 * hbq buffer to the firmware. If the function finds an empty
1693 * slot in the HBQ, it will post the buffer. The function will return
1694 * pointer to the hbq entry if it successfully post the buffer
1695 * else it will return NULL.
1696 **/
James Smart3772a992009-05-22 14:50:54 -04001697static int
James Smarted957682007-06-17 19:56:37 -05001698lpfc_sli_hbq_to_firmware(struct lpfc_hba *phba, uint32_t hbqno,
James Smart92d7f7b2007-06-17 19:56:38 -05001699 struct hbq_dmabuf *hbq_buf)
James Smarted957682007-06-17 19:56:37 -05001700{
James Smart3772a992009-05-22 14:50:54 -04001701 return phba->lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buf);
1702}
1703
1704/**
1705 * lpfc_sli_hbq_to_firmware_s3 - Post the hbq buffer to SLI3 firmware
1706 * @phba: Pointer to HBA context object.
1707 * @hbqno: HBQ number.
1708 * @hbq_buf: Pointer to HBQ buffer.
1709 *
1710 * This function is called with the hbalock held to post a hbq buffer to the
1711 * firmware. If the function finds an empty slot in the HBQ, it will post the
1712 * buffer and place it on the hbq_buffer_list. The function will return zero if
1713 * it successfully post the buffer else it will return an error.
1714 **/
1715static int
1716lpfc_sli_hbq_to_firmware_s3(struct lpfc_hba *phba, uint32_t hbqno,
1717 struct hbq_dmabuf *hbq_buf)
1718{
James Smarted957682007-06-17 19:56:37 -05001719 struct lpfc_hbq_entry *hbqe;
James Smart92d7f7b2007-06-17 19:56:38 -05001720 dma_addr_t physaddr = hbq_buf->dbuf.phys;
James Smarted957682007-06-17 19:56:37 -05001721
1722 /* Get next HBQ entry slot to use */
1723 hbqe = lpfc_sli_next_hbq_slot(phba, hbqno);
1724 if (hbqe) {
1725 struct hbq_s *hbqp = &phba->hbqs[hbqno];
1726
James Smart92d7f7b2007-06-17 19:56:38 -05001727 hbqe->bde.addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
1728 hbqe->bde.addrLow = le32_to_cpu(putPaddrLow(physaddr));
James Smart51ef4c22007-08-02 11:10:31 -04001729 hbqe->bde.tus.f.bdeSize = hbq_buf->size;
James Smarted957682007-06-17 19:56:37 -05001730 hbqe->bde.tus.f.bdeFlags = 0;
James Smart92d7f7b2007-06-17 19:56:38 -05001731 hbqe->bde.tus.w = le32_to_cpu(hbqe->bde.tus.w);
1732 hbqe->buffer_tag = le32_to_cpu(hbq_buf->tag);
1733 /* Sync SLIM */
James Smarted957682007-06-17 19:56:37 -05001734 hbqp->hbqPutIdx = hbqp->next_hbqPutIdx;
1735 writel(hbqp->hbqPutIdx, phba->hbq_put + hbqno);
James Smart92d7f7b2007-06-17 19:56:38 -05001736 /* flush */
James Smarted957682007-06-17 19:56:37 -05001737 readl(phba->hbq_put + hbqno);
James Smart51ef4c22007-08-02 11:10:31 -04001738 list_add_tail(&hbq_buf->dbuf.list, &hbqp->hbq_buffer_list);
James Smart3772a992009-05-22 14:50:54 -04001739 return 0;
1740 } else
1741 return -ENOMEM;
James Smarted957682007-06-17 19:56:37 -05001742}
1743
James Smart4f774512009-05-22 14:52:35 -04001744/**
1745 * lpfc_sli_hbq_to_firmware_s4 - Post the hbq buffer to SLI4 firmware
1746 * @phba: Pointer to HBA context object.
1747 * @hbqno: HBQ number.
1748 * @hbq_buf: Pointer to HBQ buffer.
1749 *
1750 * This function is called with the hbalock held to post an RQE to the SLI4
1751 * firmware. If able to post the RQE to the RQ it will queue the hbq entry to
1752 * the hbq_buffer_list and return zero, otherwise it will return an error.
1753 **/
1754static int
1755lpfc_sli_hbq_to_firmware_s4(struct lpfc_hba *phba, uint32_t hbqno,
1756 struct hbq_dmabuf *hbq_buf)
1757{
1758 int rc;
1759 struct lpfc_rqe hrqe;
1760 struct lpfc_rqe drqe;
1761
1762 hrqe.address_lo = putPaddrLow(hbq_buf->hbuf.phys);
1763 hrqe.address_hi = putPaddrHigh(hbq_buf->hbuf.phys);
1764 drqe.address_lo = putPaddrLow(hbq_buf->dbuf.phys);
1765 drqe.address_hi = putPaddrHigh(hbq_buf->dbuf.phys);
1766 rc = lpfc_sli4_rq_put(phba->sli4_hba.hdr_rq, phba->sli4_hba.dat_rq,
1767 &hrqe, &drqe);
1768 if (rc < 0)
1769 return rc;
1770 hbq_buf->tag = rc;
1771 list_add_tail(&hbq_buf->dbuf.list, &phba->hbqs[hbqno].hbq_buffer_list);
1772 return 0;
1773}
1774
James Smarte59058c2008-08-24 21:49:00 -04001775/* HBQ for ELS and CT traffic. */
James Smart92d7f7b2007-06-17 19:56:38 -05001776static struct lpfc_hbq_init lpfc_els_hbq = {
1777 .rn = 1,
James Smartdef9c7a2009-12-21 17:02:28 -05001778 .entry_count = 256,
James Smart92d7f7b2007-06-17 19:56:38 -05001779 .mask_count = 0,
1780 .profile = 0,
James Smart51ef4c22007-08-02 11:10:31 -04001781 .ring_mask = (1 << LPFC_ELS_RING),
James Smart92d7f7b2007-06-17 19:56:38 -05001782 .buffer_count = 0,
James Smarta257bf92009-04-06 18:48:10 -04001783 .init_count = 40,
1784 .add_count = 40,
James Smart92d7f7b2007-06-17 19:56:38 -05001785};
James Smarted957682007-06-17 19:56:37 -05001786
James Smarte59058c2008-08-24 21:49:00 -04001787/* HBQ for the extra ring if needed */
James Smart51ef4c22007-08-02 11:10:31 -04001788static struct lpfc_hbq_init lpfc_extra_hbq = {
1789 .rn = 1,
1790 .entry_count = 200,
1791 .mask_count = 0,
1792 .profile = 0,
1793 .ring_mask = (1 << LPFC_EXTRA_RING),
1794 .buffer_count = 0,
1795 .init_count = 0,
1796 .add_count = 5,
1797};
1798
James Smarte59058c2008-08-24 21:49:00 -04001799/* Array of HBQs */
James Smart78b2d852007-08-02 11:10:21 -04001800struct lpfc_hbq_init *lpfc_hbq_defs[] = {
James Smart92d7f7b2007-06-17 19:56:38 -05001801 &lpfc_els_hbq,
James Smart51ef4c22007-08-02 11:10:31 -04001802 &lpfc_extra_hbq,
James Smart92d7f7b2007-06-17 19:56:38 -05001803};
1804
James Smarte59058c2008-08-24 21:49:00 -04001805/**
James Smart3621a712009-04-06 18:47:14 -04001806 * lpfc_sli_hbqbuf_fill_hbqs - Post more hbq buffers to HBQ
James Smarte59058c2008-08-24 21:49:00 -04001807 * @phba: Pointer to HBA context object.
1808 * @hbqno: HBQ number.
1809 * @count: Number of HBQ buffers to be posted.
1810 *
James Smartd7c255b2008-08-24 21:50:00 -04001811 * This function is called with no lock held to post more hbq buffers to the
1812 * given HBQ. The function returns the number of HBQ buffers successfully
1813 * posted.
James Smarte59058c2008-08-24 21:49:00 -04001814 **/
James Smart311464e2007-08-02 11:10:37 -04001815static int
James Smart92d7f7b2007-06-17 19:56:38 -05001816lpfc_sli_hbqbuf_fill_hbqs(struct lpfc_hba *phba, uint32_t hbqno, uint32_t count)
1817{
James Smartd7c255b2008-08-24 21:50:00 -04001818 uint32_t i, posted = 0;
James Smart3163f722008-02-08 18:50:25 -05001819 unsigned long flags;
James Smart92d7f7b2007-06-17 19:56:38 -05001820 struct hbq_dmabuf *hbq_buffer;
James Smartd7c255b2008-08-24 21:50:00 -04001821 LIST_HEAD(hbq_buf_list);
Matthew Wilcoxeafe1df2008-02-21 05:44:33 -07001822 if (!phba->hbqs[hbqno].hbq_alloc_buffer)
James Smart51ef4c22007-08-02 11:10:31 -04001823 return 0;
James Smart51ef4c22007-08-02 11:10:31 -04001824
James Smartd7c255b2008-08-24 21:50:00 -04001825 if ((phba->hbqs[hbqno].buffer_count + count) >
1826 lpfc_hbq_defs[hbqno]->entry_count)
1827 count = lpfc_hbq_defs[hbqno]->entry_count -
1828 phba->hbqs[hbqno].buffer_count;
1829 if (!count)
1830 return 0;
1831 /* Allocate HBQ entries */
1832 for (i = 0; i < count; i++) {
1833 hbq_buffer = (phba->hbqs[hbqno].hbq_alloc_buffer)(phba);
1834 if (!hbq_buffer)
1835 break;
1836 list_add_tail(&hbq_buffer->dbuf.list, &hbq_buf_list);
1837 }
James Smart3163f722008-02-08 18:50:25 -05001838 /* Check whether HBQ is still in use */
1839 spin_lock_irqsave(&phba->hbalock, flags);
Matthew Wilcoxeafe1df2008-02-21 05:44:33 -07001840 if (!phba->hbq_in_use)
James Smartd7c255b2008-08-24 21:50:00 -04001841 goto err;
1842 while (!list_empty(&hbq_buf_list)) {
1843 list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
1844 dbuf.list);
1845 hbq_buffer->tag = (phba->hbqs[hbqno].buffer_count |
1846 (hbqno << 16));
James Smart3772a992009-05-22 14:50:54 -04001847 if (!lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer)) {
James Smarta8adb832007-10-27 13:37:53 -04001848 phba->hbqs[hbqno].buffer_count++;
James Smartd7c255b2008-08-24 21:50:00 -04001849 posted++;
1850 } else
James Smart51ef4c22007-08-02 11:10:31 -04001851 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
James Smart92d7f7b2007-06-17 19:56:38 -05001852 }
James Smart3163f722008-02-08 18:50:25 -05001853 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smartd7c255b2008-08-24 21:50:00 -04001854 return posted;
1855err:
1856 spin_unlock_irqrestore(&phba->hbalock, flags);
1857 while (!list_empty(&hbq_buf_list)) {
1858 list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
1859 dbuf.list);
1860 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
1861 }
James Smart92d7f7b2007-06-17 19:56:38 -05001862 return 0;
James Smarted957682007-06-17 19:56:37 -05001863}
1864
James Smarte59058c2008-08-24 21:49:00 -04001865/**
James Smart3621a712009-04-06 18:47:14 -04001866 * lpfc_sli_hbqbuf_add_hbqs - Post more HBQ buffers to firmware
James Smarte59058c2008-08-24 21:49:00 -04001867 * @phba: Pointer to HBA context object.
1868 * @qno: HBQ number.
1869 *
1870 * This function posts more buffers to the HBQ. This function
James Smartd7c255b2008-08-24 21:50:00 -04001871 * is called with no lock held. The function returns the number of HBQ entries
1872 * successfully allocated.
James Smarte59058c2008-08-24 21:49:00 -04001873 **/
James Smarted957682007-06-17 19:56:37 -05001874int
James Smart92d7f7b2007-06-17 19:56:38 -05001875lpfc_sli_hbqbuf_add_hbqs(struct lpfc_hba *phba, uint32_t qno)
James Smarted957682007-06-17 19:56:37 -05001876{
James Smartdef9c7a2009-12-21 17:02:28 -05001877 if (phba->sli_rev == LPFC_SLI_REV4)
1878 return 0;
1879 else
1880 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
1881 lpfc_hbq_defs[qno]->add_count);
James Smarted957682007-06-17 19:56:37 -05001882}
1883
James Smarte59058c2008-08-24 21:49:00 -04001884/**
James Smart3621a712009-04-06 18:47:14 -04001885 * lpfc_sli_hbqbuf_init_hbqs - Post initial buffers to the HBQ
James Smarte59058c2008-08-24 21:49:00 -04001886 * @phba: Pointer to HBA context object.
1887 * @qno: HBQ queue number.
1888 *
1889 * This function is called from SLI initialization code path with
1890 * no lock held to post initial HBQ buffers to firmware. The
James Smartd7c255b2008-08-24 21:50:00 -04001891 * function returns the number of HBQ entries successfully allocated.
James Smarte59058c2008-08-24 21:49:00 -04001892 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01001893static int
James Smart92d7f7b2007-06-17 19:56:38 -05001894lpfc_sli_hbqbuf_init_hbqs(struct lpfc_hba *phba, uint32_t qno)
James Smarted957682007-06-17 19:56:37 -05001895{
James Smartdef9c7a2009-12-21 17:02:28 -05001896 if (phba->sli_rev == LPFC_SLI_REV4)
1897 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
James Smart73d91e52011-10-10 21:32:10 -04001898 lpfc_hbq_defs[qno]->entry_count);
James Smartdef9c7a2009-12-21 17:02:28 -05001899 else
1900 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
1901 lpfc_hbq_defs[qno]->init_count);
James Smarted957682007-06-17 19:56:37 -05001902}
1903
James Smarte59058c2008-08-24 21:49:00 -04001904/**
James Smart3772a992009-05-22 14:50:54 -04001905 * lpfc_sli_hbqbuf_get - Remove the first hbq off of an hbq list
1906 * @phba: Pointer to HBA context object.
1907 * @hbqno: HBQ number.
1908 *
1909 * This function removes the first hbq buffer on an hbq list and returns a
1910 * pointer to that buffer. If it finds no buffers on the list it returns NULL.
1911 **/
1912static struct hbq_dmabuf *
1913lpfc_sli_hbqbuf_get(struct list_head *rb_list)
1914{
1915 struct lpfc_dmabuf *d_buf;
1916
1917 list_remove_head(rb_list, d_buf, struct lpfc_dmabuf, list);
1918 if (!d_buf)
1919 return NULL;
1920 return container_of(d_buf, struct hbq_dmabuf, dbuf);
1921}
1922
1923/**
James Smart3621a712009-04-06 18:47:14 -04001924 * lpfc_sli_hbqbuf_find - Find the hbq buffer associated with a tag
James Smarte59058c2008-08-24 21:49:00 -04001925 * @phba: Pointer to HBA context object.
1926 * @tag: Tag of the hbq buffer.
1927 *
1928 * This function is called with hbalock held. This function searches
1929 * for the hbq buffer associated with the given tag in the hbq buffer
1930 * list. If it finds the hbq buffer, it returns the hbq_buffer other wise
1931 * it returns NULL.
1932 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01001933static struct hbq_dmabuf *
James Smarted957682007-06-17 19:56:37 -05001934lpfc_sli_hbqbuf_find(struct lpfc_hba *phba, uint32_t tag)
1935{
James Smart92d7f7b2007-06-17 19:56:38 -05001936 struct lpfc_dmabuf *d_buf;
1937 struct hbq_dmabuf *hbq_buf;
James Smart51ef4c22007-08-02 11:10:31 -04001938 uint32_t hbqno;
James Smarted957682007-06-17 19:56:37 -05001939
James Smart51ef4c22007-08-02 11:10:31 -04001940 hbqno = tag >> 16;
Jesper Juhla0a74e452007-08-09 20:47:15 +02001941 if (hbqno >= LPFC_MAX_HBQS)
James Smart51ef4c22007-08-02 11:10:31 -04001942 return NULL;
1943
James Smart3772a992009-05-22 14:50:54 -04001944 spin_lock_irq(&phba->hbalock);
James Smart51ef4c22007-08-02 11:10:31 -04001945 list_for_each_entry(d_buf, &phba->hbqs[hbqno].hbq_buffer_list, list) {
James Smart92d7f7b2007-06-17 19:56:38 -05001946 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
James Smart51ef4c22007-08-02 11:10:31 -04001947 if (hbq_buf->tag == tag) {
James Smart3772a992009-05-22 14:50:54 -04001948 spin_unlock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -05001949 return hbq_buf;
James Smarted957682007-06-17 19:56:37 -05001950 }
1951 }
James Smart3772a992009-05-22 14:50:54 -04001952 spin_unlock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -05001953 lpfc_printf_log(phba, KERN_ERR, LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04001954 "1803 Bad hbq tag. Data: x%x x%x\n",
James Smarta8adb832007-10-27 13:37:53 -04001955 tag, phba->hbqs[tag >> 16].buffer_count);
James Smart92d7f7b2007-06-17 19:56:38 -05001956 return NULL;
James Smarted957682007-06-17 19:56:37 -05001957}
1958
James Smarte59058c2008-08-24 21:49:00 -04001959/**
James Smart3621a712009-04-06 18:47:14 -04001960 * lpfc_sli_free_hbq - Give back the hbq buffer to firmware
James Smarte59058c2008-08-24 21:49:00 -04001961 * @phba: Pointer to HBA context object.
1962 * @hbq_buffer: Pointer to HBQ buffer.
1963 *
1964 * This function is called with hbalock. This function gives back
1965 * the hbq buffer to firmware. If the HBQ does not have space to
1966 * post the buffer, it will free the buffer.
1967 **/
James Smarted957682007-06-17 19:56:37 -05001968void
James Smart51ef4c22007-08-02 11:10:31 -04001969lpfc_sli_free_hbq(struct lpfc_hba *phba, struct hbq_dmabuf *hbq_buffer)
James Smarted957682007-06-17 19:56:37 -05001970{
1971 uint32_t hbqno;
1972
James Smart51ef4c22007-08-02 11:10:31 -04001973 if (hbq_buffer) {
1974 hbqno = hbq_buffer->tag >> 16;
James Smart3772a992009-05-22 14:50:54 -04001975 if (lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer))
James Smart51ef4c22007-08-02 11:10:31 -04001976 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
James Smarted957682007-06-17 19:56:37 -05001977 }
1978}
1979
James Smarte59058c2008-08-24 21:49:00 -04001980/**
James Smart3621a712009-04-06 18:47:14 -04001981 * lpfc_sli_chk_mbx_command - Check if the mailbox is a legitimate mailbox
James Smarte59058c2008-08-24 21:49:00 -04001982 * @mbxCommand: mailbox command code.
1983 *
1984 * This function is called by the mailbox event handler function to verify
1985 * that the completed mailbox command is a legitimate mailbox command. If the
1986 * completed mailbox is not known to the function, it will return MBX_SHUTDOWN
1987 * and the mailbox event handler will take the HBA offline.
1988 **/
dea31012005-04-17 16:05:31 -05001989static int
1990lpfc_sli_chk_mbx_command(uint8_t mbxCommand)
1991{
1992 uint8_t ret;
1993
1994 switch (mbxCommand) {
1995 case MBX_LOAD_SM:
1996 case MBX_READ_NV:
1997 case MBX_WRITE_NV:
James Smarta8adb832007-10-27 13:37:53 -04001998 case MBX_WRITE_VPARMS:
dea31012005-04-17 16:05:31 -05001999 case MBX_RUN_BIU_DIAG:
2000 case MBX_INIT_LINK:
2001 case MBX_DOWN_LINK:
2002 case MBX_CONFIG_LINK:
2003 case MBX_CONFIG_RING:
2004 case MBX_RESET_RING:
2005 case MBX_READ_CONFIG:
2006 case MBX_READ_RCONFIG:
2007 case MBX_READ_SPARM:
2008 case MBX_READ_STATUS:
2009 case MBX_READ_RPI:
2010 case MBX_READ_XRI:
2011 case MBX_READ_REV:
2012 case MBX_READ_LNK_STAT:
2013 case MBX_REG_LOGIN:
2014 case MBX_UNREG_LOGIN:
dea31012005-04-17 16:05:31 -05002015 case MBX_CLEAR_LA:
2016 case MBX_DUMP_MEMORY:
2017 case MBX_DUMP_CONTEXT:
2018 case MBX_RUN_DIAGS:
2019 case MBX_RESTART:
2020 case MBX_UPDATE_CFG:
2021 case MBX_DOWN_LOAD:
2022 case MBX_DEL_LD_ENTRY:
2023 case MBX_RUN_PROGRAM:
2024 case MBX_SET_MASK:
James Smart09372822008-01-11 01:52:54 -05002025 case MBX_SET_VARIABLE:
dea31012005-04-17 16:05:31 -05002026 case MBX_UNREG_D_ID:
Jamie Wellnitz41415862006-02-28 19:25:27 -05002027 case MBX_KILL_BOARD:
dea31012005-04-17 16:05:31 -05002028 case MBX_CONFIG_FARP:
Jamie Wellnitz41415862006-02-28 19:25:27 -05002029 case MBX_BEACON:
dea31012005-04-17 16:05:31 -05002030 case MBX_LOAD_AREA:
2031 case MBX_RUN_BIU_DIAG64:
2032 case MBX_CONFIG_PORT:
2033 case MBX_READ_SPARM64:
2034 case MBX_READ_RPI64:
2035 case MBX_REG_LOGIN64:
James Smart76a95d72010-11-20 23:11:48 -05002036 case MBX_READ_TOPOLOGY:
James Smart09372822008-01-11 01:52:54 -05002037 case MBX_WRITE_WWN:
dea31012005-04-17 16:05:31 -05002038 case MBX_SET_DEBUG:
2039 case MBX_LOAD_EXP_ROM:
James Smart57127f12007-10-27 13:37:05 -04002040 case MBX_ASYNCEVT_ENABLE:
James Smart92d7f7b2007-06-17 19:56:38 -05002041 case MBX_REG_VPI:
2042 case MBX_UNREG_VPI:
James Smart858c9f62007-06-17 19:56:39 -05002043 case MBX_HEARTBEAT:
James Smart84774a42008-08-24 21:50:06 -04002044 case MBX_PORT_CAPABILITIES:
2045 case MBX_PORT_IOV_CONTROL:
James Smart04c68492009-05-22 14:52:52 -04002046 case MBX_SLI4_CONFIG:
2047 case MBX_SLI4_REQ_FTRS:
2048 case MBX_REG_FCFI:
2049 case MBX_UNREG_FCFI:
2050 case MBX_REG_VFI:
2051 case MBX_UNREG_VFI:
2052 case MBX_INIT_VPI:
2053 case MBX_INIT_VFI:
2054 case MBX_RESUME_RPI:
James Smartc7495932010-04-06 15:05:28 -04002055 case MBX_READ_EVENT_LOG_STATUS:
2056 case MBX_READ_EVENT_LOG:
James Smartdcf2a4e2010-09-29 11:18:53 -04002057 case MBX_SECURITY_MGMT:
2058 case MBX_AUTH_PORT:
dea31012005-04-17 16:05:31 -05002059 ret = mbxCommand;
2060 break;
2061 default:
2062 ret = MBX_SHUTDOWN;
2063 break;
2064 }
James Smart2e0fef82007-06-17 19:56:36 -05002065 return ret;
dea31012005-04-17 16:05:31 -05002066}
James Smarte59058c2008-08-24 21:49:00 -04002067
2068/**
James Smart3621a712009-04-06 18:47:14 -04002069 * lpfc_sli_wake_mbox_wait - lpfc_sli_issue_mbox_wait mbox completion handler
James Smarte59058c2008-08-24 21:49:00 -04002070 * @phba: Pointer to HBA context object.
2071 * @pmboxq: Pointer to mailbox command.
2072 *
2073 * This is completion handler function for mailbox commands issued from
2074 * lpfc_sli_issue_mbox_wait function. This function is called by the
2075 * mailbox event handler function with no lock held. This function
2076 * will wake up thread waiting on the wait queue pointed by context1
2077 * of the mailbox.
2078 **/
James Smart04c68492009-05-22 14:52:52 -04002079void
James Smart2e0fef82007-06-17 19:56:36 -05002080lpfc_sli_wake_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
dea31012005-04-17 16:05:31 -05002081{
2082 wait_queue_head_t *pdone_q;
James Smart858c9f62007-06-17 19:56:39 -05002083 unsigned long drvr_flag;
dea31012005-04-17 16:05:31 -05002084
2085 /*
2086 * If pdone_q is empty, the driver thread gave up waiting and
2087 * continued running.
2088 */
James Smart7054a602007-04-25 09:52:34 -04002089 pmboxq->mbox_flag |= LPFC_MBX_WAKE;
James Smart858c9f62007-06-17 19:56:39 -05002090 spin_lock_irqsave(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05002091 pdone_q = (wait_queue_head_t *) pmboxq->context1;
2092 if (pdone_q)
2093 wake_up_interruptible(pdone_q);
James Smart858c9f62007-06-17 19:56:39 -05002094 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05002095 return;
2096}
2097
James Smarte59058c2008-08-24 21:49:00 -04002098
2099/**
James Smart3621a712009-04-06 18:47:14 -04002100 * lpfc_sli_def_mbox_cmpl - Default mailbox completion handler
James Smarte59058c2008-08-24 21:49:00 -04002101 * @phba: Pointer to HBA context object.
2102 * @pmb: Pointer to mailbox object.
2103 *
2104 * This function is the default mailbox completion handler. It
2105 * frees the memory resources associated with the completed mailbox
2106 * command. If the completed command is a REG_LOGIN mailbox command,
2107 * this function will issue a UREG_LOGIN to re-claim the RPI.
2108 **/
dea31012005-04-17 16:05:31 -05002109void
James Smart2e0fef82007-06-17 19:56:36 -05002110lpfc_sli_def_mbox_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05002111{
James Smartd439d282010-09-29 11:18:45 -04002112 struct lpfc_vport *vport = pmb->vport;
dea31012005-04-17 16:05:31 -05002113 struct lpfc_dmabuf *mp;
James Smartd439d282010-09-29 11:18:45 -04002114 struct lpfc_nodelist *ndlp;
James Smart5af5eee2010-10-22 11:06:38 -04002115 struct Scsi_Host *shost;
James Smart04c68492009-05-22 14:52:52 -04002116 uint16_t rpi, vpi;
James Smart7054a602007-04-25 09:52:34 -04002117 int rc;
2118
dea31012005-04-17 16:05:31 -05002119 mp = (struct lpfc_dmabuf *) (pmb->context1);
James Smart7054a602007-04-25 09:52:34 -04002120
dea31012005-04-17 16:05:31 -05002121 if (mp) {
2122 lpfc_mbuf_free(phba, mp->virt, mp->phys);
2123 kfree(mp);
2124 }
James Smart7054a602007-04-25 09:52:34 -04002125
2126 /*
2127 * If a REG_LOGIN succeeded after node is destroyed or node
2128 * is in re-discovery driver need to cleanup the RPI.
2129 */
James Smart2e0fef82007-06-17 19:56:36 -05002130 if (!(phba->pport->load_flag & FC_UNLOADING) &&
James Smart04c68492009-05-22 14:52:52 -04002131 pmb->u.mb.mbxCommand == MBX_REG_LOGIN64 &&
2132 !pmb->u.mb.mbxStatus) {
2133 rpi = pmb->u.mb.un.varWords[0];
James Smart6d368e52011-05-24 11:44:12 -04002134 vpi = pmb->u.mb.un.varRegLogin.vpi;
James Smart04c68492009-05-22 14:52:52 -04002135 lpfc_unreg_login(phba, vpi, rpi, pmb);
James Smart92d7f7b2007-06-17 19:56:38 -05002136 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart7054a602007-04-25 09:52:34 -04002137 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
2138 if (rc != MBX_NOT_FINISHED)
2139 return;
2140 }
2141
James Smart695a8142010-01-26 23:08:03 -05002142 if ((pmb->u.mb.mbxCommand == MBX_REG_VPI) &&
2143 !(phba->pport->load_flag & FC_UNLOADING) &&
2144 !pmb->u.mb.mbxStatus) {
James Smart5af5eee2010-10-22 11:06:38 -04002145 shost = lpfc_shost_from_vport(vport);
2146 spin_lock_irq(shost->host_lock);
2147 vport->vpi_state |= LPFC_VPI_REGISTERED;
2148 vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
2149 spin_unlock_irq(shost->host_lock);
James Smart695a8142010-01-26 23:08:03 -05002150 }
2151
James Smartd439d282010-09-29 11:18:45 -04002152 if (pmb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
2153 ndlp = (struct lpfc_nodelist *)pmb->context2;
2154 lpfc_nlp_put(ndlp);
2155 pmb->context2 = NULL;
2156 }
2157
James Smartdcf2a4e2010-09-29 11:18:53 -04002158 /* Check security permission status on INIT_LINK mailbox command */
2159 if ((pmb->u.mb.mbxCommand == MBX_INIT_LINK) &&
2160 (pmb->u.mb.mbxStatus == MBXERR_SEC_NO_PERMISSION))
2161 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
2162 "2860 SLI authentication is required "
2163 "for INIT_LINK but has not done yet\n");
2164
James Smart04c68492009-05-22 14:52:52 -04002165 if (bf_get(lpfc_mqe_command, &pmb->u.mqe) == MBX_SLI4_CONFIG)
2166 lpfc_sli4_mbox_cmd_free(phba, pmb);
2167 else
2168 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05002169}
2170
James Smarte59058c2008-08-24 21:49:00 -04002171/**
James Smart3621a712009-04-06 18:47:14 -04002172 * lpfc_sli_handle_mb_event - Handle mailbox completions from firmware
James Smarte59058c2008-08-24 21:49:00 -04002173 * @phba: Pointer to HBA context object.
2174 *
2175 * This function is called with no lock held. This function processes all
2176 * the completed mailbox commands and gives it to upper layers. The interrupt
2177 * service routine processes mailbox completion interrupt and adds completed
2178 * mailbox commands to the mboxq_cmpl queue and signals the worker thread.
2179 * Worker thread call lpfc_sli_handle_mb_event, which will return the
2180 * completed mailbox commands in mboxq_cmpl queue to the upper layers. This
2181 * function returns the mailbox commands to the upper layer by calling the
2182 * completion handler function of each mailbox.
2183 **/
dea31012005-04-17 16:05:31 -05002184int
James Smart2e0fef82007-06-17 19:56:36 -05002185lpfc_sli_handle_mb_event(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05002186{
James Smart92d7f7b2007-06-17 19:56:38 -05002187 MAILBOX_t *pmbox;
dea31012005-04-17 16:05:31 -05002188 LPFC_MBOXQ_t *pmb;
James Smart92d7f7b2007-06-17 19:56:38 -05002189 int rc;
2190 LIST_HEAD(cmplq);
dea31012005-04-17 16:05:31 -05002191
2192 phba->sli.slistat.mbox_event++;
2193
James Smart92d7f7b2007-06-17 19:56:38 -05002194 /* Get all completed mailboxe buffers into the cmplq */
2195 spin_lock_irq(&phba->hbalock);
2196 list_splice_init(&phba->sli.mboxq_cmpl, &cmplq);
2197 spin_unlock_irq(&phba->hbalock);
2198
dea31012005-04-17 16:05:31 -05002199 /* Get a Mailbox buffer to setup mailbox commands for callback */
James Smart92d7f7b2007-06-17 19:56:38 -05002200 do {
2201 list_remove_head(&cmplq, pmb, LPFC_MBOXQ_t, list);
2202 if (pmb == NULL)
2203 break;
2204
James Smart04c68492009-05-22 14:52:52 -04002205 pmbox = &pmb->u.mb;
dea31012005-04-17 16:05:31 -05002206
James Smart858c9f62007-06-17 19:56:39 -05002207 if (pmbox->mbxCommand != MBX_HEARTBEAT) {
2208 if (pmb->vport) {
2209 lpfc_debugfs_disc_trc(pmb->vport,
2210 LPFC_DISC_TRC_MBOX_VPORT,
2211 "MBOX cmpl vport: cmd:x%x mb:x%x x%x",
2212 (uint32_t)pmbox->mbxCommand,
2213 pmbox->un.varWords[0],
2214 pmbox->un.varWords[1]);
2215 }
2216 else {
2217 lpfc_debugfs_disc_trc(phba->pport,
2218 LPFC_DISC_TRC_MBOX,
2219 "MBOX cmpl: cmd:x%x mb:x%x x%x",
2220 (uint32_t)pmbox->mbxCommand,
2221 pmbox->un.varWords[0],
2222 pmbox->un.varWords[1]);
2223 }
2224 }
2225
dea31012005-04-17 16:05:31 -05002226 /*
2227 * It is a fatal error if unknown mbox command completion.
2228 */
2229 if (lpfc_sli_chk_mbx_command(pmbox->mbxCommand) ==
2230 MBX_SHUTDOWN) {
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02002231 /* Unknown mailbox command compl */
James Smart92d7f7b2007-06-17 19:56:38 -05002232 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002233 "(%d):0323 Unknown Mailbox command "
James Smarta183a152011-10-10 21:32:43 -04002234 "x%x (x%x/x%x) Cmpl\n",
James Smart92d7f7b2007-06-17 19:56:38 -05002235 pmb->vport ? pmb->vport->vpi : 0,
James Smart04c68492009-05-22 14:52:52 -04002236 pmbox->mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04002237 lpfc_sli_config_mbox_subsys_get(phba,
2238 pmb),
2239 lpfc_sli_config_mbox_opcode_get(phba,
2240 pmb));
James Smart2e0fef82007-06-17 19:56:36 -05002241 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05002242 phba->work_hs = HS_FFER3;
2243 lpfc_handle_eratt(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05002244 continue;
dea31012005-04-17 16:05:31 -05002245 }
2246
dea31012005-04-17 16:05:31 -05002247 if (pmbox->mbxStatus) {
2248 phba->sli.slistat.mbox_stat_err++;
2249 if (pmbox->mbxStatus == MBXERR_NO_RESOURCES) {
2250 /* Mbox cmd cmpl error - RETRYing */
James Smart92d7f7b2007-06-17 19:56:38 -05002251 lpfc_printf_log(phba, KERN_INFO,
James Smarta183a152011-10-10 21:32:43 -04002252 LOG_MBOX | LOG_SLI,
2253 "(%d):0305 Mbox cmd cmpl "
2254 "error - RETRYing Data: x%x "
2255 "(x%x/x%x) x%x x%x x%x\n",
2256 pmb->vport ? pmb->vport->vpi : 0,
2257 pmbox->mbxCommand,
2258 lpfc_sli_config_mbox_subsys_get(phba,
2259 pmb),
2260 lpfc_sli_config_mbox_opcode_get(phba,
2261 pmb),
2262 pmbox->mbxStatus,
2263 pmbox->un.varWords[0],
2264 pmb->vport->port_state);
dea31012005-04-17 16:05:31 -05002265 pmbox->mbxStatus = 0;
2266 pmbox->mbxOwner = OWN_HOST;
dea31012005-04-17 16:05:31 -05002267 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
James Smart04c68492009-05-22 14:52:52 -04002268 if (rc != MBX_NOT_FINISHED)
James Smart92d7f7b2007-06-17 19:56:38 -05002269 continue;
dea31012005-04-17 16:05:31 -05002270 }
2271 }
2272
2273 /* Mailbox cmd <cmd> Cmpl <cmpl> */
James Smart92d7f7b2007-06-17 19:56:38 -05002274 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04002275 "(%d):0307 Mailbox cmd x%x (x%x/x%x) Cmpl x%p "
dea31012005-04-17 16:05:31 -05002276 "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 -05002277 pmb->vport ? pmb->vport->vpi : 0,
dea31012005-04-17 16:05:31 -05002278 pmbox->mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04002279 lpfc_sli_config_mbox_subsys_get(phba, pmb),
2280 lpfc_sli_config_mbox_opcode_get(phba, pmb),
dea31012005-04-17 16:05:31 -05002281 pmb->mbox_cmpl,
2282 *((uint32_t *) pmbox),
2283 pmbox->un.varWords[0],
2284 pmbox->un.varWords[1],
2285 pmbox->un.varWords[2],
2286 pmbox->un.varWords[3],
2287 pmbox->un.varWords[4],
2288 pmbox->un.varWords[5],
2289 pmbox->un.varWords[6],
2290 pmbox->un.varWords[7]);
2291
James Smart92d7f7b2007-06-17 19:56:38 -05002292 if (pmb->mbox_cmpl)
dea31012005-04-17 16:05:31 -05002293 pmb->mbox_cmpl(phba,pmb);
James Smart92d7f7b2007-06-17 19:56:38 -05002294 } while (1);
James Smart2e0fef82007-06-17 19:56:36 -05002295 return 0;
dea31012005-04-17 16:05:31 -05002296}
James Smart92d7f7b2007-06-17 19:56:38 -05002297
James Smarte59058c2008-08-24 21:49:00 -04002298/**
James Smart3621a712009-04-06 18:47:14 -04002299 * lpfc_sli_get_buff - Get the buffer associated with the buffer tag
James Smarte59058c2008-08-24 21:49:00 -04002300 * @phba: Pointer to HBA context object.
2301 * @pring: Pointer to driver SLI ring object.
2302 * @tag: buffer tag.
2303 *
2304 * This function is called with no lock held. When QUE_BUFTAG_BIT bit
2305 * is set in the tag the buffer is posted for a particular exchange,
2306 * the function will return the buffer without replacing the buffer.
2307 * If the buffer is for unsolicited ELS or CT traffic, this function
2308 * returns the buffer and also posts another buffer to the firmware.
2309 **/
James Smart76bb24e2007-10-27 13:38:00 -04002310static struct lpfc_dmabuf *
2311lpfc_sli_get_buff(struct lpfc_hba *phba,
James Smart9f1e1b52008-12-04 22:39:40 -05002312 struct lpfc_sli_ring *pring,
2313 uint32_t tag)
James Smart76bb24e2007-10-27 13:38:00 -04002314{
James Smart9f1e1b52008-12-04 22:39:40 -05002315 struct hbq_dmabuf *hbq_entry;
2316
James Smart76bb24e2007-10-27 13:38:00 -04002317 if (tag & QUE_BUFTAG_BIT)
2318 return lpfc_sli_ring_taggedbuf_get(phba, pring, tag);
James Smart9f1e1b52008-12-04 22:39:40 -05002319 hbq_entry = lpfc_sli_hbqbuf_find(phba, tag);
2320 if (!hbq_entry)
2321 return NULL;
2322 return &hbq_entry->dbuf;
James Smart76bb24e2007-10-27 13:38:00 -04002323}
James Smart57127f12007-10-27 13:37:05 -04002324
James Smart3772a992009-05-22 14:50:54 -04002325/**
2326 * lpfc_complete_unsol_iocb - Complete an unsolicited sequence
2327 * @phba: Pointer to HBA context object.
2328 * @pring: Pointer to driver SLI ring object.
2329 * @saveq: Pointer to the iocbq struct representing the sequence starting frame.
2330 * @fch_r_ctl: the r_ctl for the first frame of the sequence.
2331 * @fch_type: the type for the first frame of the sequence.
2332 *
2333 * This function is called with no lock held. This function uses the r_ctl and
2334 * type of the received sequence to find the correct callback function to call
2335 * to process the sequence.
2336 **/
2337static int
2338lpfc_complete_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2339 struct lpfc_iocbq *saveq, uint32_t fch_r_ctl,
2340 uint32_t fch_type)
2341{
2342 int i;
2343
2344 /* unSolicited Responses */
2345 if (pring->prt[0].profile) {
2346 if (pring->prt[0].lpfc_sli_rcv_unsol_event)
2347 (pring->prt[0].lpfc_sli_rcv_unsol_event) (phba, pring,
2348 saveq);
2349 return 1;
2350 }
2351 /* We must search, based on rctl / type
2352 for the right routine */
2353 for (i = 0; i < pring->num_mask; i++) {
2354 if ((pring->prt[i].rctl == fch_r_ctl) &&
2355 (pring->prt[i].type == fch_type)) {
2356 if (pring->prt[i].lpfc_sli_rcv_unsol_event)
2357 (pring->prt[i].lpfc_sli_rcv_unsol_event)
2358 (phba, pring, saveq);
2359 return 1;
2360 }
2361 }
2362 return 0;
2363}
James Smarte59058c2008-08-24 21:49:00 -04002364
2365/**
James Smart3621a712009-04-06 18:47:14 -04002366 * lpfc_sli_process_unsol_iocb - Unsolicited iocb handler
James Smarte59058c2008-08-24 21:49:00 -04002367 * @phba: Pointer to HBA context object.
2368 * @pring: Pointer to driver SLI ring object.
2369 * @saveq: Pointer to the unsolicited iocb.
2370 *
2371 * This function is called with no lock held by the ring event handler
2372 * when there is an unsolicited iocb posted to the response ring by the
2373 * firmware. This function gets the buffer associated with the iocbs
2374 * and calls the event handler for the ring. This function handles both
2375 * qring buffers and hbq buffers.
2376 * When the function returns 1 the caller can free the iocb object otherwise
2377 * upper layer functions will free the iocb objects.
2378 **/
dea31012005-04-17 16:05:31 -05002379static int
2380lpfc_sli_process_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2381 struct lpfc_iocbq *saveq)
2382{
2383 IOCB_t * irsp;
2384 WORD5 * w5p;
2385 uint32_t Rctl, Type;
James Smart3772a992009-05-22 14:50:54 -04002386 uint32_t match;
James Smart76bb24e2007-10-27 13:38:00 -04002387 struct lpfc_iocbq *iocbq;
James Smart3163f722008-02-08 18:50:25 -05002388 struct lpfc_dmabuf *dmzbuf;
dea31012005-04-17 16:05:31 -05002389
2390 match = 0;
2391 irsp = &(saveq->iocb);
James Smart57127f12007-10-27 13:37:05 -04002392
2393 if (irsp->ulpCommand == CMD_ASYNC_STATUS) {
2394 if (pring->lpfc_sli_rcv_async_status)
2395 pring->lpfc_sli_rcv_async_status(phba, pring, saveq);
2396 else
2397 lpfc_printf_log(phba,
2398 KERN_WARNING,
2399 LOG_SLI,
2400 "0316 Ring %d handler: unexpected "
2401 "ASYNC_STATUS iocb received evt_code "
2402 "0x%x\n",
2403 pring->ringno,
2404 irsp->un.asyncstat.evt_code);
2405 return 1;
2406 }
2407
James Smart3163f722008-02-08 18:50:25 -05002408 if ((irsp->ulpCommand == CMD_IOCB_RET_XRI64_CX) &&
2409 (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)) {
2410 if (irsp->ulpBdeCount > 0) {
2411 dmzbuf = lpfc_sli_get_buff(phba, pring,
2412 irsp->un.ulpWord[3]);
2413 lpfc_in_buf_free(phba, dmzbuf);
2414 }
2415
2416 if (irsp->ulpBdeCount > 1) {
2417 dmzbuf = lpfc_sli_get_buff(phba, pring,
2418 irsp->unsli3.sli3Words[3]);
2419 lpfc_in_buf_free(phba, dmzbuf);
2420 }
2421
2422 if (irsp->ulpBdeCount > 2) {
2423 dmzbuf = lpfc_sli_get_buff(phba, pring,
2424 irsp->unsli3.sli3Words[7]);
2425 lpfc_in_buf_free(phba, dmzbuf);
2426 }
2427
2428 return 1;
2429 }
2430
James Smart92d7f7b2007-06-17 19:56:38 -05002431 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
James Smart76bb24e2007-10-27 13:38:00 -04002432 if (irsp->ulpBdeCount != 0) {
2433 saveq->context2 = lpfc_sli_get_buff(phba, pring,
James Smart92d7f7b2007-06-17 19:56:38 -05002434 irsp->un.ulpWord[3]);
James Smart76bb24e2007-10-27 13:38:00 -04002435 if (!saveq->context2)
2436 lpfc_printf_log(phba,
2437 KERN_ERR,
2438 LOG_SLI,
2439 "0341 Ring %d Cannot find buffer for "
2440 "an unsolicited iocb. tag 0x%x\n",
2441 pring->ringno,
2442 irsp->un.ulpWord[3]);
James Smart76bb24e2007-10-27 13:38:00 -04002443 }
2444 if (irsp->ulpBdeCount == 2) {
2445 saveq->context3 = lpfc_sli_get_buff(phba, pring,
James Smart51ef4c22007-08-02 11:10:31 -04002446 irsp->unsli3.sli3Words[7]);
James Smart76bb24e2007-10-27 13:38:00 -04002447 if (!saveq->context3)
2448 lpfc_printf_log(phba,
2449 KERN_ERR,
2450 LOG_SLI,
2451 "0342 Ring %d Cannot find buffer for an"
2452 " unsolicited iocb. tag 0x%x\n",
2453 pring->ringno,
2454 irsp->unsli3.sli3Words[7]);
2455 }
2456 list_for_each_entry(iocbq, &saveq->list, list) {
James Smart76bb24e2007-10-27 13:38:00 -04002457 irsp = &(iocbq->iocb);
James Smart76bb24e2007-10-27 13:38:00 -04002458 if (irsp->ulpBdeCount != 0) {
2459 iocbq->context2 = lpfc_sli_get_buff(phba, pring,
2460 irsp->un.ulpWord[3]);
James Smart9c2face2008-01-11 01:53:18 -05002461 if (!iocbq->context2)
James Smart76bb24e2007-10-27 13:38:00 -04002462 lpfc_printf_log(phba,
2463 KERN_ERR,
2464 LOG_SLI,
2465 "0343 Ring %d Cannot find "
2466 "buffer for an unsolicited iocb"
2467 ". tag 0x%x\n", pring->ringno,
2468 irsp->un.ulpWord[3]);
2469 }
2470 if (irsp->ulpBdeCount == 2) {
2471 iocbq->context3 = lpfc_sli_get_buff(phba, pring,
2472 irsp->unsli3.sli3Words[7]);
James Smart9c2face2008-01-11 01:53:18 -05002473 if (!iocbq->context3)
James Smart76bb24e2007-10-27 13:38:00 -04002474 lpfc_printf_log(phba,
2475 KERN_ERR,
2476 LOG_SLI,
2477 "0344 Ring %d Cannot find "
2478 "buffer for an unsolicited "
2479 "iocb. tag 0x%x\n",
2480 pring->ringno,
2481 irsp->unsli3.sli3Words[7]);
2482 }
2483 }
James Smart92d7f7b2007-06-17 19:56:38 -05002484 }
James Smart9c2face2008-01-11 01:53:18 -05002485 if (irsp->ulpBdeCount != 0 &&
2486 (irsp->ulpCommand == CMD_IOCB_RCV_CONT64_CX ||
2487 irsp->ulpStatus == IOSTAT_INTERMED_RSP)) {
2488 int found = 0;
2489
2490 /* search continue save q for same XRI */
2491 list_for_each_entry(iocbq, &pring->iocb_continue_saveq, clist) {
James Smart7851fe22011-07-22 18:36:52 -04002492 if (iocbq->iocb.unsli3.rcvsli3.ox_id ==
2493 saveq->iocb.unsli3.rcvsli3.ox_id) {
James Smart9c2face2008-01-11 01:53:18 -05002494 list_add_tail(&saveq->list, &iocbq->list);
2495 found = 1;
2496 break;
2497 }
2498 }
2499 if (!found)
2500 list_add_tail(&saveq->clist,
2501 &pring->iocb_continue_saveq);
2502 if (saveq->iocb.ulpStatus != IOSTAT_INTERMED_RSP) {
2503 list_del_init(&iocbq->clist);
2504 saveq = iocbq;
2505 irsp = &(saveq->iocb);
2506 } else
2507 return 0;
2508 }
2509 if ((irsp->ulpCommand == CMD_RCV_ELS_REQ64_CX) ||
2510 (irsp->ulpCommand == CMD_RCV_ELS_REQ_CX) ||
2511 (irsp->ulpCommand == CMD_IOCB_RCV_ELS64_CX)) {
James Smart6a9c52c2009-10-02 15:16:51 -04002512 Rctl = FC_RCTL_ELS_REQ;
2513 Type = FC_TYPE_ELS;
James Smart9c2face2008-01-11 01:53:18 -05002514 } else {
2515 w5p = (WORD5 *)&(saveq->iocb.un.ulpWord[5]);
2516 Rctl = w5p->hcsw.Rctl;
2517 Type = w5p->hcsw.Type;
2518
2519 /* Firmware Workaround */
2520 if ((Rctl == 0) && (pring->ringno == LPFC_ELS_RING) &&
2521 (irsp->ulpCommand == CMD_RCV_SEQUENCE64_CX ||
2522 irsp->ulpCommand == CMD_IOCB_RCV_SEQ64_CX)) {
James Smart6a9c52c2009-10-02 15:16:51 -04002523 Rctl = FC_RCTL_ELS_REQ;
2524 Type = FC_TYPE_ELS;
James Smart9c2face2008-01-11 01:53:18 -05002525 w5p->hcsw.Rctl = Rctl;
2526 w5p->hcsw.Type = Type;
2527 }
2528 }
James Smart92d7f7b2007-06-17 19:56:38 -05002529
James Smart3772a992009-05-22 14:50:54 -04002530 if (!lpfc_complete_unsol_iocb(phba, pring, saveq, Rctl, Type))
James Smart92d7f7b2007-06-17 19:56:38 -05002531 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002532 "0313 Ring %d handler: unexpected Rctl x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05002533 "Type x%x received\n",
James Smarte8b62012007-08-02 11:10:09 -04002534 pring->ringno, Rctl, Type);
James Smart3772a992009-05-22 14:50:54 -04002535
James Smart92d7f7b2007-06-17 19:56:38 -05002536 return 1;
dea31012005-04-17 16:05:31 -05002537}
2538
James Smarte59058c2008-08-24 21:49:00 -04002539/**
James Smart3621a712009-04-06 18:47:14 -04002540 * lpfc_sli_iocbq_lookup - Find command iocb for the given response iocb
James Smarte59058c2008-08-24 21:49:00 -04002541 * @phba: Pointer to HBA context object.
2542 * @pring: Pointer to driver SLI ring object.
2543 * @prspiocb: Pointer to response iocb object.
2544 *
2545 * This function looks up the iocb_lookup table to get the command iocb
2546 * corresponding to the given response iocb using the iotag of the
2547 * response iocb. This function is called with the hbalock held.
2548 * This function returns the command iocb object if it finds the command
2549 * iocb else returns NULL.
2550 **/
dea31012005-04-17 16:05:31 -05002551static struct lpfc_iocbq *
James Smart2e0fef82007-06-17 19:56:36 -05002552lpfc_sli_iocbq_lookup(struct lpfc_hba *phba,
2553 struct lpfc_sli_ring *pring,
2554 struct lpfc_iocbq *prspiocb)
dea31012005-04-17 16:05:31 -05002555{
dea31012005-04-17 16:05:31 -05002556 struct lpfc_iocbq *cmd_iocb = NULL;
2557 uint16_t iotag;
2558
James Bottomley604a3e32005-10-29 10:28:33 -05002559 iotag = prspiocb->iocb.ulpIoTag;
dea31012005-04-17 16:05:31 -05002560
James Bottomley604a3e32005-10-29 10:28:33 -05002561 if (iotag != 0 && iotag <= phba->sli.last_iotag) {
2562 cmd_iocb = phba->sli.iocbq_lookup[iotag];
James Smart92d7f7b2007-06-17 19:56:38 -05002563 list_del_init(&cmd_iocb->list);
James Smart4f2e66c2012-05-09 21:17:07 -04002564 if (cmd_iocb->iocb_flag & LPFC_IO_ON_TXCMPLQ) {
James Smart2a9bf3d2010-06-07 15:24:45 -04002565 pring->txcmplq_cnt--;
James Smart4f2e66c2012-05-09 21:17:07 -04002566 cmd_iocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
James Smart2a9bf3d2010-06-07 15:24:45 -04002567 }
James Bottomley604a3e32005-10-29 10:28:33 -05002568 return cmd_iocb;
dea31012005-04-17 16:05:31 -05002569 }
2570
dea31012005-04-17 16:05:31 -05002571 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002572 "0317 iotag x%x is out off "
James Bottomley604a3e32005-10-29 10:28:33 -05002573 "range: max iotag x%x wd0 x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04002574 iotag, phba->sli.last_iotag,
James Bottomley604a3e32005-10-29 10:28:33 -05002575 *(((uint32_t *) &prspiocb->iocb) + 7));
dea31012005-04-17 16:05:31 -05002576 return NULL;
2577}
2578
James Smarte59058c2008-08-24 21:49:00 -04002579/**
James Smart3772a992009-05-22 14:50:54 -04002580 * lpfc_sli_iocbq_lookup_by_tag - Find command iocb for the iotag
2581 * @phba: Pointer to HBA context object.
2582 * @pring: Pointer to driver SLI ring object.
2583 * @iotag: IOCB tag.
2584 *
2585 * This function looks up the iocb_lookup table to get the command iocb
2586 * corresponding to the given iotag. This function is called with the
2587 * hbalock held.
2588 * This function returns the command iocb object if it finds the command
2589 * iocb else returns NULL.
2590 **/
2591static struct lpfc_iocbq *
2592lpfc_sli_iocbq_lookup_by_tag(struct lpfc_hba *phba,
2593 struct lpfc_sli_ring *pring, uint16_t iotag)
2594{
2595 struct lpfc_iocbq *cmd_iocb;
2596
2597 if (iotag != 0 && iotag <= phba->sli.last_iotag) {
2598 cmd_iocb = phba->sli.iocbq_lookup[iotag];
James Smart4f2e66c2012-05-09 21:17:07 -04002599 if (cmd_iocb->iocb_flag & LPFC_IO_ON_TXCMPLQ) {
2600 /* remove from txcmpl queue list */
2601 list_del_init(&cmd_iocb->list);
2602 cmd_iocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
James Smart2a9bf3d2010-06-07 15:24:45 -04002603 pring->txcmplq_cnt--;
James Smart4f2e66c2012-05-09 21:17:07 -04002604 return cmd_iocb;
James Smart2a9bf3d2010-06-07 15:24:45 -04002605 }
James Smart3772a992009-05-22 14:50:54 -04002606 }
James Smart3772a992009-05-22 14:50:54 -04002607 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2608 "0372 iotag x%x is out off range: max iotag (x%x)\n",
2609 iotag, phba->sli.last_iotag);
2610 return NULL;
2611}
2612
2613/**
James Smart3621a712009-04-06 18:47:14 -04002614 * lpfc_sli_process_sol_iocb - process solicited iocb completion
James Smarte59058c2008-08-24 21:49:00 -04002615 * @phba: Pointer to HBA context object.
2616 * @pring: Pointer to driver SLI ring object.
2617 * @saveq: Pointer to the response iocb to be processed.
2618 *
2619 * This function is called by the ring event handler for non-fcp
2620 * rings when there is a new response iocb in the response ring.
2621 * The caller is not required to hold any locks. This function
2622 * gets the command iocb associated with the response iocb and
2623 * calls the completion handler for the command iocb. If there
2624 * is no completion handler, the function will free the resources
2625 * associated with command iocb. If the response iocb is for
2626 * an already aborted command iocb, the status of the completion
2627 * is changed to IOSTAT_LOCAL_REJECT/IOERR_SLI_ABORTED.
2628 * This function always returns 1.
2629 **/
dea31012005-04-17 16:05:31 -05002630static int
James Smart2e0fef82007-06-17 19:56:36 -05002631lpfc_sli_process_sol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
dea31012005-04-17 16:05:31 -05002632 struct lpfc_iocbq *saveq)
2633{
James Smart2e0fef82007-06-17 19:56:36 -05002634 struct lpfc_iocbq *cmdiocbp;
dea31012005-04-17 16:05:31 -05002635 int rc = 1;
2636 unsigned long iflag;
2637
2638 /* Based on the iotag field, get the cmd IOCB from the txcmplq */
James Smart2e0fef82007-06-17 19:56:36 -05002639 spin_lock_irqsave(&phba->hbalock, iflag);
James Bottomley604a3e32005-10-29 10:28:33 -05002640 cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring, saveq);
James Smart2e0fef82007-06-17 19:56:36 -05002641 spin_unlock_irqrestore(&phba->hbalock, iflag);
2642
dea31012005-04-17 16:05:31 -05002643 if (cmdiocbp) {
2644 if (cmdiocbp->iocb_cmpl) {
2645 /*
James Smartea2151b2008-09-07 11:52:10 -04002646 * If an ELS command failed send an event to mgmt
2647 * application.
2648 */
2649 if (saveq->iocb.ulpStatus &&
2650 (pring->ringno == LPFC_ELS_RING) &&
2651 (cmdiocbp->iocb.ulpCommand ==
2652 CMD_ELS_REQUEST64_CR))
2653 lpfc_send_els_failure_event(phba,
2654 cmdiocbp, saveq);
2655
2656 /*
dea31012005-04-17 16:05:31 -05002657 * Post all ELS completions to the worker thread.
2658 * All other are passed to the completion callback.
2659 */
2660 if (pring->ringno == LPFC_ELS_RING) {
James Smart341af102010-01-26 23:07:37 -05002661 if ((phba->sli_rev < LPFC_SLI_REV4) &&
2662 (cmdiocbp->iocb_flag &
2663 LPFC_DRIVER_ABORTED)) {
2664 spin_lock_irqsave(&phba->hbalock,
2665 iflag);
James Smart07951072007-04-25 09:51:38 -04002666 cmdiocbp->iocb_flag &=
2667 ~LPFC_DRIVER_ABORTED;
James Smart341af102010-01-26 23:07:37 -05002668 spin_unlock_irqrestore(&phba->hbalock,
2669 iflag);
James Smart07951072007-04-25 09:51:38 -04002670 saveq->iocb.ulpStatus =
2671 IOSTAT_LOCAL_REJECT;
2672 saveq->iocb.un.ulpWord[4] =
2673 IOERR_SLI_ABORTED;
James Smart0ff10d42008-01-11 01:52:36 -05002674
2675 /* Firmware could still be in progress
2676 * of DMAing payload, so don't free data
2677 * buffer till after a hbeat.
2678 */
James Smart341af102010-01-26 23:07:37 -05002679 spin_lock_irqsave(&phba->hbalock,
2680 iflag);
James Smart0ff10d42008-01-11 01:52:36 -05002681 saveq->iocb_flag |= LPFC_DELAY_MEM_FREE;
James Smart341af102010-01-26 23:07:37 -05002682 spin_unlock_irqrestore(&phba->hbalock,
2683 iflag);
2684 }
James Smart0f65ff62010-02-26 14:14:23 -05002685 if (phba->sli_rev == LPFC_SLI_REV4) {
2686 if (saveq->iocb_flag &
2687 LPFC_EXCHANGE_BUSY) {
2688 /* Set cmdiocb flag for the
2689 * exchange busy so sgl (xri)
2690 * will not be released until
2691 * the abort xri is received
2692 * from hba.
2693 */
2694 spin_lock_irqsave(
2695 &phba->hbalock, iflag);
2696 cmdiocbp->iocb_flag |=
2697 LPFC_EXCHANGE_BUSY;
2698 spin_unlock_irqrestore(
2699 &phba->hbalock, iflag);
2700 }
2701 if (cmdiocbp->iocb_flag &
2702 LPFC_DRIVER_ABORTED) {
2703 /*
2704 * Clear LPFC_DRIVER_ABORTED
2705 * bit in case it was driver
2706 * initiated abort.
2707 */
2708 spin_lock_irqsave(
2709 &phba->hbalock, iflag);
2710 cmdiocbp->iocb_flag &=
2711 ~LPFC_DRIVER_ABORTED;
2712 spin_unlock_irqrestore(
2713 &phba->hbalock, iflag);
2714 cmdiocbp->iocb.ulpStatus =
2715 IOSTAT_LOCAL_REJECT;
2716 cmdiocbp->iocb.un.ulpWord[4] =
2717 IOERR_ABORT_REQUESTED;
2718 /*
2719 * For SLI4, irsiocb contains
2720 * NO_XRI in sli_xritag, it
2721 * shall not affect releasing
2722 * sgl (xri) process.
2723 */
2724 saveq->iocb.ulpStatus =
2725 IOSTAT_LOCAL_REJECT;
2726 saveq->iocb.un.ulpWord[4] =
2727 IOERR_SLI_ABORTED;
2728 spin_lock_irqsave(
2729 &phba->hbalock, iflag);
2730 saveq->iocb_flag |=
2731 LPFC_DELAY_MEM_FREE;
2732 spin_unlock_irqrestore(
2733 &phba->hbalock, iflag);
2734 }
James Smart07951072007-04-25 09:51:38 -04002735 }
dea31012005-04-17 16:05:31 -05002736 }
James Smart2e0fef82007-06-17 19:56:36 -05002737 (cmdiocbp->iocb_cmpl) (phba, cmdiocbp, saveq);
James Bottomley604a3e32005-10-29 10:28:33 -05002738 } else
2739 lpfc_sli_release_iocbq(phba, cmdiocbp);
dea31012005-04-17 16:05:31 -05002740 } else {
2741 /*
2742 * Unknown initiating command based on the response iotag.
2743 * This could be the case on the ELS ring because of
2744 * lpfc_els_abort().
2745 */
2746 if (pring->ringno != LPFC_ELS_RING) {
2747 /*
2748 * Ring <ringno> handler: unexpected completion IoTag
2749 * <IoTag>
2750 */
James Smarta257bf92009-04-06 18:48:10 -04002751 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002752 "0322 Ring %d handler: "
2753 "unexpected completion IoTag x%x "
2754 "Data: x%x x%x x%x x%x\n",
2755 pring->ringno,
2756 saveq->iocb.ulpIoTag,
2757 saveq->iocb.ulpStatus,
2758 saveq->iocb.un.ulpWord[4],
2759 saveq->iocb.ulpCommand,
2760 saveq->iocb.ulpContext);
dea31012005-04-17 16:05:31 -05002761 }
2762 }
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04002763
dea31012005-04-17 16:05:31 -05002764 return rc;
2765}
2766
James Smarte59058c2008-08-24 21:49:00 -04002767/**
James Smart3621a712009-04-06 18:47:14 -04002768 * lpfc_sli_rsp_pointers_error - Response ring pointer error handler
James Smarte59058c2008-08-24 21:49:00 -04002769 * @phba: Pointer to HBA context object.
2770 * @pring: Pointer to driver SLI ring object.
2771 *
2772 * This function is called from the iocb ring event handlers when
2773 * put pointer is ahead of the get pointer for a ring. This function signal
2774 * an error attention condition to the worker thread and the worker
2775 * thread will transition the HBA to offline state.
2776 **/
James Smart2e0fef82007-06-17 19:56:36 -05002777static void
2778lpfc_sli_rsp_pointers_error(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002779{
James Smart34b02dc2008-08-24 21:49:55 -04002780 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002781 /*
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02002782 * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002783 * rsp ring <portRspMax>
2784 */
2785 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002786 "0312 Ring %d handler: portRspPut %d "
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02002787 "is bigger than rsp ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04002788 pring->ringno, le32_to_cpu(pgp->rspPutInx),
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002789 pring->numRiocb);
2790
James Smart2e0fef82007-06-17 19:56:36 -05002791 phba->link_state = LPFC_HBA_ERROR;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002792
2793 /*
2794 * All error attention handlers are posted to
2795 * worker thread
2796 */
2797 phba->work_ha |= HA_ERATT;
2798 phba->work_hs = HS_FFER3;
James Smart92d7f7b2007-06-17 19:56:38 -05002799
James Smart5e9d9b82008-06-14 22:52:53 -04002800 lpfc_worker_wake_up(phba);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002801
2802 return;
2803}
2804
James Smarte59058c2008-08-24 21:49:00 -04002805/**
James Smart3621a712009-04-06 18:47:14 -04002806 * lpfc_poll_eratt - Error attention polling timer timeout handler
James Smart93996272008-08-24 21:50:30 -04002807 * @ptr: Pointer to address of HBA context object.
2808 *
2809 * This function is invoked by the Error Attention polling timer when the
2810 * timer times out. It will check the SLI Error Attention register for
2811 * possible attention events. If so, it will post an Error Attention event
2812 * and wake up worker thread to process it. Otherwise, it will set up the
2813 * Error Attention polling timer for the next poll.
2814 **/
2815void lpfc_poll_eratt(unsigned long ptr)
2816{
2817 struct lpfc_hba *phba;
2818 uint32_t eratt = 0;
2819
2820 phba = (struct lpfc_hba *)ptr;
2821
2822 /* Check chip HA register for error event */
2823 eratt = lpfc_sli_check_eratt(phba);
2824
2825 if (eratt)
2826 /* Tell the worker thread there is work to do */
2827 lpfc_worker_wake_up(phba);
2828 else
2829 /* Restart the timer for next eratt poll */
2830 mod_timer(&phba->eratt_poll, jiffies +
2831 HZ * LPFC_ERATT_POLL_INTERVAL);
2832 return;
2833}
2834
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002835
James Smarte59058c2008-08-24 21:49:00 -04002836/**
James Smart3621a712009-04-06 18:47:14 -04002837 * lpfc_sli_handle_fast_ring_event - Handle ring events on FCP ring
James Smarte59058c2008-08-24 21:49:00 -04002838 * @phba: Pointer to HBA context object.
2839 * @pring: Pointer to driver SLI ring object.
2840 * @mask: Host attention register mask for this ring.
2841 *
2842 * This function is called from the interrupt context when there is a ring
2843 * event for the fcp ring. The caller does not hold any lock.
2844 * The function processes each response iocb in the response ring until it
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002845 * finds an iocb with LE bit set and chains all the iocbs up to the iocb with
James Smarte59058c2008-08-24 21:49:00 -04002846 * LE bit set. The function will call the completion handler of the command iocb
2847 * if the response iocb indicates a completion for a command iocb or it is
2848 * an abort completion. The function will call lpfc_sli_process_unsol_iocb
2849 * function if this is an unsolicited iocb.
dea31012005-04-17 16:05:31 -05002850 * This routine presumes LPFC_FCP_RING handling and doesn't bother
James Smart45ed1192009-10-02 15:17:02 -04002851 * to check it explicitly.
2852 */
2853int
James Smart2e0fef82007-06-17 19:56:36 -05002854lpfc_sli_handle_fast_ring_event(struct lpfc_hba *phba,
2855 struct lpfc_sli_ring *pring, uint32_t mask)
dea31012005-04-17 16:05:31 -05002856{
James Smart34b02dc2008-08-24 21:49:55 -04002857 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
dea31012005-04-17 16:05:31 -05002858 IOCB_t *irsp = NULL;
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04002859 IOCB_t *entry = NULL;
dea31012005-04-17 16:05:31 -05002860 struct lpfc_iocbq *cmdiocbq = NULL;
2861 struct lpfc_iocbq rspiocbq;
dea31012005-04-17 16:05:31 -05002862 uint32_t status;
2863 uint32_t portRspPut, portRspMax;
2864 int rc = 1;
2865 lpfc_iocb_type type;
2866 unsigned long iflag;
2867 uint32_t rsp_cmpl = 0;
dea31012005-04-17 16:05:31 -05002868
James Smart2e0fef82007-06-17 19:56:36 -05002869 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05002870 pring->stats.iocb_event++;
2871
dea31012005-04-17 16:05:31 -05002872 /*
2873 * The next available response entry should never exceed the maximum
2874 * entries. If it does, treat it as an adapter hardware error.
2875 */
2876 portRspMax = pring->numRiocb;
2877 portRspPut = le32_to_cpu(pgp->rspPutInx);
2878 if (unlikely(portRspPut >= portRspMax)) {
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002879 lpfc_sli_rsp_pointers_error(phba, pring);
James Smart2e0fef82007-06-17 19:56:36 -05002880 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05002881 return 1;
2882 }
James Smart45ed1192009-10-02 15:17:02 -04002883 if (phba->fcp_ring_in_use) {
2884 spin_unlock_irqrestore(&phba->hbalock, iflag);
2885 return 1;
2886 } else
2887 phba->fcp_ring_in_use = 1;
dea31012005-04-17 16:05:31 -05002888
2889 rmb();
2890 while (pring->rspidx != portRspPut) {
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04002891 /*
2892 * Fetch an entry off the ring and copy it into a local data
2893 * structure. The copy involves a byte-swap since the
2894 * network byte order and pci byte orders are different.
2895 */
James Smarted957682007-06-17 19:56:37 -05002896 entry = lpfc_resp_iocb(phba, pring);
James Smart858c9f62007-06-17 19:56:39 -05002897 phba->last_completion_time = jiffies;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002898
2899 if (++pring->rspidx >= portRspMax)
2900 pring->rspidx = 0;
2901
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04002902 lpfc_sli_pcimem_bcopy((uint32_t *) entry,
2903 (uint32_t *) &rspiocbq.iocb,
James Smarted957682007-06-17 19:56:37 -05002904 phba->iocb_rsp_size);
James Smarta4bc3372006-12-02 13:34:16 -05002905 INIT_LIST_HEAD(&(rspiocbq.list));
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04002906 irsp = &rspiocbq.iocb;
2907
dea31012005-04-17 16:05:31 -05002908 type = lpfc_sli_iocb_cmd_type(irsp->ulpCommand & CMD_IOCB_MASK);
2909 pring->stats.iocb_rsp++;
2910 rsp_cmpl++;
2911
2912 if (unlikely(irsp->ulpStatus)) {
James Smart92d7f7b2007-06-17 19:56:38 -05002913 /*
2914 * If resource errors reported from HBA, reduce
2915 * queuedepths of the SCSI device.
2916 */
2917 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
2918 (irsp->un.ulpWord[4] == IOERR_NO_RESOURCES)) {
2919 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart3772a992009-05-22 14:50:54 -04002920 phba->lpfc_rampdown_queue_depth(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05002921 spin_lock_irqsave(&phba->hbalock, iflag);
2922 }
2923
dea31012005-04-17 16:05:31 -05002924 /* Rsp ring <ringno> error: IOCB */
2925 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002926 "0336 Rsp Ring %d error: IOCB Data: "
James Smart92d7f7b2007-06-17 19:56:38 -05002927 "x%x x%x x%x x%x x%x x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04002928 pring->ringno,
James Smart92d7f7b2007-06-17 19:56:38 -05002929 irsp->un.ulpWord[0],
2930 irsp->un.ulpWord[1],
2931 irsp->un.ulpWord[2],
2932 irsp->un.ulpWord[3],
2933 irsp->un.ulpWord[4],
2934 irsp->un.ulpWord[5],
James Smartd7c255b2008-08-24 21:50:00 -04002935 *(uint32_t *)&irsp->un1,
2936 *((uint32_t *)&irsp->un1 + 1));
dea31012005-04-17 16:05:31 -05002937 }
2938
2939 switch (type) {
2940 case LPFC_ABORT_IOCB:
2941 case LPFC_SOL_IOCB:
2942 /*
2943 * Idle exchange closed via ABTS from port. No iocb
2944 * resources need to be recovered.
2945 */
2946 if (unlikely(irsp->ulpCommand == CMD_XRI_ABORTED_CX)) {
James Smartdca94792006-08-01 07:34:08 -04002947 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002948 "0333 IOCB cmd 0x%x"
James Smartdca94792006-08-01 07:34:08 -04002949 " processed. Skipping"
James Smart92d7f7b2007-06-17 19:56:38 -05002950 " completion\n",
James Smartdca94792006-08-01 07:34:08 -04002951 irsp->ulpCommand);
dea31012005-04-17 16:05:31 -05002952 break;
2953 }
2954
James Bottomley604a3e32005-10-29 10:28:33 -05002955 cmdiocbq = lpfc_sli_iocbq_lookup(phba, pring,
2956 &rspiocbq);
James Smart0f65ff62010-02-26 14:14:23 -05002957 if (unlikely(!cmdiocbq))
2958 break;
2959 if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED)
2960 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
2961 if (cmdiocbq->iocb_cmpl) {
2962 spin_unlock_irqrestore(&phba->hbalock, iflag);
2963 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
2964 &rspiocbq);
2965 spin_lock_irqsave(&phba->hbalock, iflag);
2966 }
dea31012005-04-17 16:05:31 -05002967 break;
James Smarta4bc3372006-12-02 13:34:16 -05002968 case LPFC_UNSOL_IOCB:
James Smart2e0fef82007-06-17 19:56:36 -05002969 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta4bc3372006-12-02 13:34:16 -05002970 lpfc_sli_process_unsol_iocb(phba, pring, &rspiocbq);
James Smart2e0fef82007-06-17 19:56:36 -05002971 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta4bc3372006-12-02 13:34:16 -05002972 break;
dea31012005-04-17 16:05:31 -05002973 default:
2974 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
2975 char adaptermsg[LPFC_MAX_ADPTMSG];
2976 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
2977 memcpy(&adaptermsg[0], (uint8_t *) irsp,
2978 MAX_MSG_DATA);
Joe Perches898eb712007-10-18 03:06:30 -07002979 dev_warn(&((phba->pcidev)->dev),
2980 "lpfc%d: %s\n",
dea31012005-04-17 16:05:31 -05002981 phba->brd_no, adaptermsg);
2982 } else {
2983 /* Unknown IOCB command */
2984 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002985 "0334 Unknown IOCB command "
James Smart92d7f7b2007-06-17 19:56:38 -05002986 "Data: x%x, x%x x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04002987 type, irsp->ulpCommand,
James Smart92d7f7b2007-06-17 19:56:38 -05002988 irsp->ulpStatus,
2989 irsp->ulpIoTag,
2990 irsp->ulpContext);
dea31012005-04-17 16:05:31 -05002991 }
2992 break;
2993 }
2994
2995 /*
2996 * The response IOCB has been processed. Update the ring
2997 * pointer in SLIM. If the port response put pointer has not
2998 * been updated, sync the pgp->rspPutInx and fetch the new port
2999 * response put pointer.
3000 */
James Smarted957682007-06-17 19:56:37 -05003001 writel(pring->rspidx, &phba->host_gp[pring->ringno].rspGetInx);
dea31012005-04-17 16:05:31 -05003002
3003 if (pring->rspidx == portRspPut)
3004 portRspPut = le32_to_cpu(pgp->rspPutInx);
3005 }
3006
3007 if ((rsp_cmpl > 0) && (mask & HA_R0RE_REQ)) {
3008 pring->stats.iocb_rsp_full++;
3009 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
3010 writel(status, phba->CAregaddr);
3011 readl(phba->CAregaddr);
3012 }
3013 if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
3014 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
3015 pring->stats.iocb_cmd_empty++;
3016
3017 /* Force update of the local copy of cmdGetInx */
3018 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
3019 lpfc_sli_resume_iocb(phba, pring);
3020
3021 if ((pring->lpfc_sli_cmd_available))
3022 (pring->lpfc_sli_cmd_available) (phba, pring);
3023
3024 }
3025
James Smart45ed1192009-10-02 15:17:02 -04003026 phba->fcp_ring_in_use = 0;
James Smart2e0fef82007-06-17 19:56:36 -05003027 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003028 return rc;
3029}
3030
James Smarte59058c2008-08-24 21:49:00 -04003031/**
James Smart3772a992009-05-22 14:50:54 -04003032 * lpfc_sli_sp_handle_rspiocb - Handle slow-path response iocb
3033 * @phba: Pointer to HBA context object.
3034 * @pring: Pointer to driver SLI ring object.
3035 * @rspiocbp: Pointer to driver response IOCB object.
3036 *
3037 * This function is called from the worker thread when there is a slow-path
3038 * response IOCB to process. This function chains all the response iocbs until
3039 * seeing the iocb with the LE bit set. The function will call
3040 * lpfc_sli_process_sol_iocb function if the response iocb indicates a
3041 * completion of a command iocb. The function will call the
3042 * lpfc_sli_process_unsol_iocb function if this is an unsolicited iocb.
3043 * The function frees the resources or calls the completion handler if this
3044 * iocb is an abort completion. The function returns NULL when the response
3045 * iocb has the LE bit set and all the chained iocbs are processed, otherwise
3046 * this function shall chain the iocb on to the iocb_continueq and return the
3047 * response iocb passed in.
3048 **/
3049static struct lpfc_iocbq *
3050lpfc_sli_sp_handle_rspiocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
3051 struct lpfc_iocbq *rspiocbp)
3052{
3053 struct lpfc_iocbq *saveq;
3054 struct lpfc_iocbq *cmdiocbp;
3055 struct lpfc_iocbq *next_iocb;
3056 IOCB_t *irsp = NULL;
3057 uint32_t free_saveq;
3058 uint8_t iocb_cmd_type;
3059 lpfc_iocb_type type;
3060 unsigned long iflag;
3061 int rc;
3062
3063 spin_lock_irqsave(&phba->hbalock, iflag);
3064 /* First add the response iocb to the countinueq list */
3065 list_add_tail(&rspiocbp->list, &(pring->iocb_continueq));
3066 pring->iocb_continueq_cnt++;
3067
Justin P. Mattock70f23fd2011-05-10 10:16:21 +02003068 /* Now, determine whether the list is completed for processing */
James Smart3772a992009-05-22 14:50:54 -04003069 irsp = &rspiocbp->iocb;
3070 if (irsp->ulpLe) {
3071 /*
3072 * By default, the driver expects to free all resources
3073 * associated with this iocb completion.
3074 */
3075 free_saveq = 1;
3076 saveq = list_get_first(&pring->iocb_continueq,
3077 struct lpfc_iocbq, list);
3078 irsp = &(saveq->iocb);
3079 list_del_init(&pring->iocb_continueq);
3080 pring->iocb_continueq_cnt = 0;
3081
3082 pring->stats.iocb_rsp++;
3083
3084 /*
3085 * If resource errors reported from HBA, reduce
3086 * queuedepths of the SCSI device.
3087 */
3088 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
3089 (irsp->un.ulpWord[4] == IOERR_NO_RESOURCES)) {
3090 spin_unlock_irqrestore(&phba->hbalock, iflag);
3091 phba->lpfc_rampdown_queue_depth(phba);
3092 spin_lock_irqsave(&phba->hbalock, iflag);
3093 }
3094
3095 if (irsp->ulpStatus) {
3096 /* Rsp ring <ringno> error: IOCB */
3097 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
3098 "0328 Rsp Ring %d error: "
3099 "IOCB Data: "
3100 "x%x x%x x%x x%x "
3101 "x%x x%x x%x x%x "
3102 "x%x x%x x%x x%x "
3103 "x%x x%x x%x x%x\n",
3104 pring->ringno,
3105 irsp->un.ulpWord[0],
3106 irsp->un.ulpWord[1],
3107 irsp->un.ulpWord[2],
3108 irsp->un.ulpWord[3],
3109 irsp->un.ulpWord[4],
3110 irsp->un.ulpWord[5],
3111 *(((uint32_t *) irsp) + 6),
3112 *(((uint32_t *) irsp) + 7),
3113 *(((uint32_t *) irsp) + 8),
3114 *(((uint32_t *) irsp) + 9),
3115 *(((uint32_t *) irsp) + 10),
3116 *(((uint32_t *) irsp) + 11),
3117 *(((uint32_t *) irsp) + 12),
3118 *(((uint32_t *) irsp) + 13),
3119 *(((uint32_t *) irsp) + 14),
3120 *(((uint32_t *) irsp) + 15));
3121 }
3122
3123 /*
3124 * Fetch the IOCB command type and call the correct completion
3125 * routine. Solicited and Unsolicited IOCBs on the ELS ring
3126 * get freed back to the lpfc_iocb_list by the discovery
3127 * kernel thread.
3128 */
3129 iocb_cmd_type = irsp->ulpCommand & CMD_IOCB_MASK;
3130 type = lpfc_sli_iocb_cmd_type(iocb_cmd_type);
3131 switch (type) {
3132 case LPFC_SOL_IOCB:
3133 spin_unlock_irqrestore(&phba->hbalock, iflag);
3134 rc = lpfc_sli_process_sol_iocb(phba, pring, saveq);
3135 spin_lock_irqsave(&phba->hbalock, iflag);
3136 break;
3137
3138 case LPFC_UNSOL_IOCB:
3139 spin_unlock_irqrestore(&phba->hbalock, iflag);
3140 rc = lpfc_sli_process_unsol_iocb(phba, pring, saveq);
3141 spin_lock_irqsave(&phba->hbalock, iflag);
3142 if (!rc)
3143 free_saveq = 0;
3144 break;
3145
3146 case LPFC_ABORT_IOCB:
3147 cmdiocbp = NULL;
3148 if (irsp->ulpCommand != CMD_XRI_ABORTED_CX)
3149 cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring,
3150 saveq);
3151 if (cmdiocbp) {
3152 /* Call the specified completion routine */
3153 if (cmdiocbp->iocb_cmpl) {
3154 spin_unlock_irqrestore(&phba->hbalock,
3155 iflag);
3156 (cmdiocbp->iocb_cmpl)(phba, cmdiocbp,
3157 saveq);
3158 spin_lock_irqsave(&phba->hbalock,
3159 iflag);
3160 } else
3161 __lpfc_sli_release_iocbq(phba,
3162 cmdiocbp);
3163 }
3164 break;
3165
3166 case LPFC_UNKNOWN_IOCB:
3167 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
3168 char adaptermsg[LPFC_MAX_ADPTMSG];
3169 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
3170 memcpy(&adaptermsg[0], (uint8_t *)irsp,
3171 MAX_MSG_DATA);
3172 dev_warn(&((phba->pcidev)->dev),
3173 "lpfc%d: %s\n",
3174 phba->brd_no, adaptermsg);
3175 } else {
3176 /* Unknown IOCB command */
3177 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3178 "0335 Unknown IOCB "
3179 "command Data: x%x "
3180 "x%x x%x x%x\n",
3181 irsp->ulpCommand,
3182 irsp->ulpStatus,
3183 irsp->ulpIoTag,
3184 irsp->ulpContext);
3185 }
3186 break;
3187 }
3188
3189 if (free_saveq) {
3190 list_for_each_entry_safe(rspiocbp, next_iocb,
3191 &saveq->list, list) {
3192 list_del(&rspiocbp->list);
3193 __lpfc_sli_release_iocbq(phba, rspiocbp);
3194 }
3195 __lpfc_sli_release_iocbq(phba, saveq);
3196 }
3197 rspiocbp = NULL;
3198 }
3199 spin_unlock_irqrestore(&phba->hbalock, iflag);
3200 return rspiocbp;
3201}
3202
3203/**
3204 * lpfc_sli_handle_slow_ring_event - Wrapper func for handling slow-path iocbs
James Smarte59058c2008-08-24 21:49:00 -04003205 * @phba: Pointer to HBA context object.
3206 * @pring: Pointer to driver SLI ring object.
3207 * @mask: Host attention register mask for this ring.
3208 *
James Smart3772a992009-05-22 14:50:54 -04003209 * This routine wraps the actual slow_ring event process routine from the
3210 * API jump table function pointer from the lpfc_hba struct.
James Smarte59058c2008-08-24 21:49:00 -04003211 **/
James Smart3772a992009-05-22 14:50:54 -04003212void
James Smart2e0fef82007-06-17 19:56:36 -05003213lpfc_sli_handle_slow_ring_event(struct lpfc_hba *phba,
3214 struct lpfc_sli_ring *pring, uint32_t mask)
dea31012005-04-17 16:05:31 -05003215{
James Smart3772a992009-05-22 14:50:54 -04003216 phba->lpfc_sli_handle_slow_ring_event(phba, pring, mask);
3217}
3218
3219/**
3220 * lpfc_sli_handle_slow_ring_event_s3 - Handle SLI3 ring event for non-FCP rings
3221 * @phba: Pointer to HBA context object.
3222 * @pring: Pointer to driver SLI ring object.
3223 * @mask: Host attention register mask for this ring.
3224 *
3225 * This function is called from the worker thread when there is a ring event
3226 * for non-fcp rings. The caller does not hold any lock. The function will
3227 * remove each response iocb in the response ring and calls the handle
3228 * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
3229 **/
3230static void
3231lpfc_sli_handle_slow_ring_event_s3(struct lpfc_hba *phba,
3232 struct lpfc_sli_ring *pring, uint32_t mask)
3233{
James Smart34b02dc2008-08-24 21:49:55 -04003234 struct lpfc_pgp *pgp;
dea31012005-04-17 16:05:31 -05003235 IOCB_t *entry;
3236 IOCB_t *irsp = NULL;
3237 struct lpfc_iocbq *rspiocbp = NULL;
dea31012005-04-17 16:05:31 -05003238 uint32_t portRspPut, portRspMax;
dea31012005-04-17 16:05:31 -05003239 unsigned long iflag;
James Smart3772a992009-05-22 14:50:54 -04003240 uint32_t status;
dea31012005-04-17 16:05:31 -05003241
James Smart34b02dc2008-08-24 21:49:55 -04003242 pgp = &phba->port_gp[pring->ringno];
James Smart2e0fef82007-06-17 19:56:36 -05003243 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003244 pring->stats.iocb_event++;
3245
dea31012005-04-17 16:05:31 -05003246 /*
3247 * The next available response entry should never exceed the maximum
3248 * entries. If it does, treat it as an adapter hardware error.
3249 */
3250 portRspMax = pring->numRiocb;
3251 portRspPut = le32_to_cpu(pgp->rspPutInx);
3252 if (portRspPut >= portRspMax) {
3253 /*
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02003254 * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
dea31012005-04-17 16:05:31 -05003255 * rsp ring <portRspMax>
3256 */
James Smarted957682007-06-17 19:56:37 -05003257 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003258 "0303 Ring %d handler: portRspPut %d "
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02003259 "is bigger than rsp ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04003260 pring->ringno, portRspPut, portRspMax);
dea31012005-04-17 16:05:31 -05003261
James Smart2e0fef82007-06-17 19:56:36 -05003262 phba->link_state = LPFC_HBA_ERROR;
3263 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003264
3265 phba->work_hs = HS_FFER3;
3266 lpfc_handle_eratt(phba);
3267
James Smart3772a992009-05-22 14:50:54 -04003268 return;
dea31012005-04-17 16:05:31 -05003269 }
3270
3271 rmb();
dea31012005-04-17 16:05:31 -05003272 while (pring->rspidx != portRspPut) {
3273 /*
3274 * Build a completion list and call the appropriate handler.
3275 * The process is to get the next available response iocb, get
3276 * a free iocb from the list, copy the response data into the
3277 * free iocb, insert to the continuation list, and update the
3278 * next response index to slim. This process makes response
3279 * iocb's in the ring available to DMA as fast as possible but
3280 * pays a penalty for a copy operation. Since the iocb is
3281 * only 32 bytes, this penalty is considered small relative to
3282 * the PCI reads for register values and a slim write. When
3283 * the ulpLe field is set, the entire Command has been
3284 * received.
3285 */
James Smarted957682007-06-17 19:56:37 -05003286 entry = lpfc_resp_iocb(phba, pring);
3287
James Smart858c9f62007-06-17 19:56:39 -05003288 phba->last_completion_time = jiffies;
James Smart2e0fef82007-06-17 19:56:36 -05003289 rspiocbp = __lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05003290 if (rspiocbp == NULL) {
3291 printk(KERN_ERR "%s: out of buffers! Failing "
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07003292 "completion.\n", __func__);
dea31012005-04-17 16:05:31 -05003293 break;
3294 }
3295
James Smarted957682007-06-17 19:56:37 -05003296 lpfc_sli_pcimem_bcopy(entry, &rspiocbp->iocb,
3297 phba->iocb_rsp_size);
dea31012005-04-17 16:05:31 -05003298 irsp = &rspiocbp->iocb;
3299
3300 if (++pring->rspidx >= portRspMax)
3301 pring->rspidx = 0;
3302
James Smarta58cbd52007-08-02 11:09:43 -04003303 if (pring->ringno == LPFC_ELS_RING) {
3304 lpfc_debugfs_slow_ring_trc(phba,
3305 "IOCB rsp ring: wd4:x%08x wd6:x%08x wd7:x%08x",
3306 *(((uint32_t *) irsp) + 4),
3307 *(((uint32_t *) irsp) + 6),
3308 *(((uint32_t *) irsp) + 7));
3309 }
3310
James Smarted957682007-06-17 19:56:37 -05003311 writel(pring->rspidx, &phba->host_gp[pring->ringno].rspGetInx);
dea31012005-04-17 16:05:31 -05003312
James Smart3772a992009-05-22 14:50:54 -04003313 spin_unlock_irqrestore(&phba->hbalock, iflag);
3314 /* Handle the response IOCB */
3315 rspiocbp = lpfc_sli_sp_handle_rspiocb(phba, pring, rspiocbp);
3316 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003317
3318 /*
3319 * If the port response put pointer has not been updated, sync
3320 * the pgp->rspPutInx in the MAILBOX_tand fetch the new port
3321 * response put pointer.
3322 */
3323 if (pring->rspidx == portRspPut) {
3324 portRspPut = le32_to_cpu(pgp->rspPutInx);
3325 }
3326 } /* while (pring->rspidx != portRspPut) */
3327
James Smart92d7f7b2007-06-17 19:56:38 -05003328 if ((rspiocbp != NULL) && (mask & HA_R0RE_REQ)) {
dea31012005-04-17 16:05:31 -05003329 /* At least one response entry has been freed */
3330 pring->stats.iocb_rsp_full++;
3331 /* SET RxRE_RSP in Chip Att register */
3332 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
3333 writel(status, phba->CAregaddr);
3334 readl(phba->CAregaddr); /* flush */
3335 }
3336 if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
3337 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
3338 pring->stats.iocb_cmd_empty++;
3339
3340 /* Force update of the local copy of cmdGetInx */
3341 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
3342 lpfc_sli_resume_iocb(phba, pring);
3343
3344 if ((pring->lpfc_sli_cmd_available))
3345 (pring->lpfc_sli_cmd_available) (phba, pring);
3346
3347 }
3348
James Smart2e0fef82007-06-17 19:56:36 -05003349 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart3772a992009-05-22 14:50:54 -04003350 return;
dea31012005-04-17 16:05:31 -05003351}
3352
James Smarte59058c2008-08-24 21:49:00 -04003353/**
James Smart4f774512009-05-22 14:52:35 -04003354 * lpfc_sli_handle_slow_ring_event_s4 - Handle SLI4 slow-path els events
3355 * @phba: Pointer to HBA context object.
3356 * @pring: Pointer to driver SLI ring object.
3357 * @mask: Host attention register mask for this ring.
3358 *
3359 * This function is called from the worker thread when there is a pending
3360 * ELS response iocb on the driver internal slow-path response iocb worker
3361 * queue. The caller does not hold any lock. The function will remove each
3362 * response iocb from the response worker queue and calls the handle
3363 * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
3364 **/
3365static void
3366lpfc_sli_handle_slow_ring_event_s4(struct lpfc_hba *phba,
3367 struct lpfc_sli_ring *pring, uint32_t mask)
3368{
3369 struct lpfc_iocbq *irspiocbq;
James Smart4d9ab992009-10-02 15:16:39 -04003370 struct hbq_dmabuf *dmabuf;
3371 struct lpfc_cq_event *cq_event;
James Smart4f774512009-05-22 14:52:35 -04003372 unsigned long iflag;
3373
James Smart45ed1192009-10-02 15:17:02 -04003374 spin_lock_irqsave(&phba->hbalock, iflag);
3375 phba->hba_flag &= ~HBA_SP_QUEUE_EVT;
3376 spin_unlock_irqrestore(&phba->hbalock, iflag);
3377 while (!list_empty(&phba->sli4_hba.sp_queue_event)) {
James Smart4f774512009-05-22 14:52:35 -04003378 /* Get the response iocb from the head of work queue */
3379 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart45ed1192009-10-02 15:17:02 -04003380 list_remove_head(&phba->sli4_hba.sp_queue_event,
James Smart4d9ab992009-10-02 15:16:39 -04003381 cq_event, struct lpfc_cq_event, list);
James Smart4f774512009-05-22 14:52:35 -04003382 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart4d9ab992009-10-02 15:16:39 -04003383
3384 switch (bf_get(lpfc_wcqe_c_code, &cq_event->cqe.wcqe_cmpl)) {
3385 case CQE_CODE_COMPL_WQE:
3386 irspiocbq = container_of(cq_event, struct lpfc_iocbq,
3387 cq_event);
James Smart45ed1192009-10-02 15:17:02 -04003388 /* Translate ELS WCQE to response IOCBQ */
3389 irspiocbq = lpfc_sli4_els_wcqe_to_rspiocbq(phba,
3390 irspiocbq);
3391 if (irspiocbq)
3392 lpfc_sli_sp_handle_rspiocb(phba, pring,
3393 irspiocbq);
James Smart4d9ab992009-10-02 15:16:39 -04003394 break;
3395 case CQE_CODE_RECEIVE:
James Smart7851fe22011-07-22 18:36:52 -04003396 case CQE_CODE_RECEIVE_V1:
James Smart4d9ab992009-10-02 15:16:39 -04003397 dmabuf = container_of(cq_event, struct hbq_dmabuf,
3398 cq_event);
3399 lpfc_sli4_handle_received_buffer(phba, dmabuf);
3400 break;
3401 default:
3402 break;
3403 }
James Smart4f774512009-05-22 14:52:35 -04003404 }
3405}
3406
3407/**
James Smart3621a712009-04-06 18:47:14 -04003408 * lpfc_sli_abort_iocb_ring - Abort all iocbs in the ring
James Smarte59058c2008-08-24 21:49:00 -04003409 * @phba: Pointer to HBA context object.
3410 * @pring: Pointer to driver SLI ring object.
3411 *
3412 * This function aborts all iocbs in the given ring and frees all the iocb
3413 * objects in txq. This function issues an abort iocb for all the iocb commands
3414 * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
3415 * the return of this function. The caller is not required to hold any locks.
3416 **/
James Smart2e0fef82007-06-17 19:56:36 -05003417void
dea31012005-04-17 16:05:31 -05003418lpfc_sli_abort_iocb_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
3419{
James Smart2534ba72007-04-25 09:52:20 -04003420 LIST_HEAD(completions);
dea31012005-04-17 16:05:31 -05003421 struct lpfc_iocbq *iocb, *next_iocb;
dea31012005-04-17 16:05:31 -05003422
James Smart92d7f7b2007-06-17 19:56:38 -05003423 if (pring->ringno == LPFC_ELS_RING) {
3424 lpfc_fabric_abort_hba(phba);
3425 }
3426
dea31012005-04-17 16:05:31 -05003427 /* Error everything on txq and txcmplq
3428 * First do the txq.
3429 */
James Smart2e0fef82007-06-17 19:56:36 -05003430 spin_lock_irq(&phba->hbalock);
James Smart2534ba72007-04-25 09:52:20 -04003431 list_splice_init(&pring->txq, &completions);
dea31012005-04-17 16:05:31 -05003432 pring->txq_cnt = 0;
dea31012005-04-17 16:05:31 -05003433
3434 /* Next issue ABTS for everything on the txcmplq */
James Smart2534ba72007-04-25 09:52:20 -04003435 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
3436 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
3437
James Smart2e0fef82007-06-17 19:56:36 -05003438 spin_unlock_irq(&phba->hbalock);
James Smart2534ba72007-04-25 09:52:20 -04003439
James Smarta257bf92009-04-06 18:48:10 -04003440 /* Cancel all the IOCBs from the completions list */
3441 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
3442 IOERR_SLI_ABORTED);
dea31012005-04-17 16:05:31 -05003443}
3444
James Smarte59058c2008-08-24 21:49:00 -04003445/**
James Smart3621a712009-04-06 18:47:14 -04003446 * lpfc_sli_flush_fcp_rings - flush all iocbs in the fcp ring
James Smarta8e497d2008-08-24 21:50:11 -04003447 * @phba: Pointer to HBA context object.
3448 *
3449 * This function flushes all iocbs in the fcp ring and frees all the iocb
3450 * objects in txq and txcmplq. This function will not issue abort iocbs
3451 * for all the iocb commands in txcmplq, they will just be returned with
3452 * IOERR_SLI_DOWN. This function is invoked with EEH when device's PCI
3453 * slot has been permanently disabled.
3454 **/
3455void
3456lpfc_sli_flush_fcp_rings(struct lpfc_hba *phba)
3457{
3458 LIST_HEAD(txq);
3459 LIST_HEAD(txcmplq);
James Smarta8e497d2008-08-24 21:50:11 -04003460 struct lpfc_sli *psli = &phba->sli;
3461 struct lpfc_sli_ring *pring;
3462
3463 /* Currently, only one fcp ring */
3464 pring = &psli->ring[psli->fcp_ring];
3465
3466 spin_lock_irq(&phba->hbalock);
3467 /* Retrieve everything on txq */
3468 list_splice_init(&pring->txq, &txq);
3469 pring->txq_cnt = 0;
3470
3471 /* Retrieve everything on the txcmplq */
3472 list_splice_init(&pring->txcmplq, &txcmplq);
3473 pring->txcmplq_cnt = 0;
James Smart4f2e66c2012-05-09 21:17:07 -04003474
3475 /* Indicate the I/O queues are flushed */
3476 phba->hba_flag |= HBA_FCP_IOQ_FLUSH;
James Smarta8e497d2008-08-24 21:50:11 -04003477 spin_unlock_irq(&phba->hbalock);
3478
3479 /* Flush the txq */
James Smarta257bf92009-04-06 18:48:10 -04003480 lpfc_sli_cancel_iocbs(phba, &txq, IOSTAT_LOCAL_REJECT,
3481 IOERR_SLI_DOWN);
James Smarta8e497d2008-08-24 21:50:11 -04003482
3483 /* Flush the txcmpq */
James Smarta257bf92009-04-06 18:48:10 -04003484 lpfc_sli_cancel_iocbs(phba, &txcmplq, IOSTAT_LOCAL_REJECT,
3485 IOERR_SLI_DOWN);
James Smarta8e497d2008-08-24 21:50:11 -04003486}
3487
3488/**
James Smart3772a992009-05-22 14:50:54 -04003489 * lpfc_sli_brdready_s3 - Check for sli3 host ready status
James Smarte59058c2008-08-24 21:49:00 -04003490 * @phba: Pointer to HBA context object.
3491 * @mask: Bit mask to be checked.
3492 *
3493 * This function reads the host status register and compares
3494 * with the provided bit mask to check if HBA completed
3495 * the restart. This function will wait in a loop for the
3496 * HBA to complete restart. If the HBA does not restart within
3497 * 15 iterations, the function will reset the HBA again. The
3498 * function returns 1 when HBA fail to restart otherwise returns
3499 * zero.
3500 **/
James Smart3772a992009-05-22 14:50:54 -04003501static int
3502lpfc_sli_brdready_s3(struct lpfc_hba *phba, uint32_t mask)
dea31012005-04-17 16:05:31 -05003503{
Jamie Wellnitz41415862006-02-28 19:25:27 -05003504 uint32_t status;
3505 int i = 0;
3506 int retval = 0;
dea31012005-04-17 16:05:31 -05003507
Jamie Wellnitz41415862006-02-28 19:25:27 -05003508 /* Read the HBA Host Status Register */
James Smart9940b972011-03-11 16:06:12 -05003509 if (lpfc_readl(phba->HSregaddr, &status))
3510 return 1;
dea31012005-04-17 16:05:31 -05003511
Jamie Wellnitz41415862006-02-28 19:25:27 -05003512 /*
3513 * Check status register every 100ms for 5 retries, then every
3514 * 500ms for 5, then every 2.5 sec for 5, then reset board and
3515 * every 2.5 sec for 4.
3516 * Break our of the loop if errors occurred during init.
3517 */
3518 while (((status & mask) != mask) &&
3519 !(status & HS_FFERM) &&
3520 i++ < 20) {
dea31012005-04-17 16:05:31 -05003521
Jamie Wellnitz41415862006-02-28 19:25:27 -05003522 if (i <= 5)
3523 msleep(10);
3524 else if (i <= 10)
3525 msleep(500);
3526 else
3527 msleep(2500);
dea31012005-04-17 16:05:31 -05003528
Jamie Wellnitz41415862006-02-28 19:25:27 -05003529 if (i == 15) {
James Smart2e0fef82007-06-17 19:56:36 -05003530 /* Do post */
James Smart92d7f7b2007-06-17 19:56:38 -05003531 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003532 lpfc_sli_brdrestart(phba);
3533 }
3534 /* Read the HBA Host Status Register */
James Smart9940b972011-03-11 16:06:12 -05003535 if (lpfc_readl(phba->HSregaddr, &status)) {
3536 retval = 1;
3537 break;
3538 }
dea31012005-04-17 16:05:31 -05003539 }
dea31012005-04-17 16:05:31 -05003540
Jamie Wellnitz41415862006-02-28 19:25:27 -05003541 /* Check to see if any errors occurred during init */
3542 if ((status & HS_FFERM) || (i >= 20)) {
James Smarte40a02c2010-02-26 14:13:54 -05003543 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3544 "2751 Adapter failed to restart, "
3545 "status reg x%x, FW Data: A8 x%x AC x%x\n",
3546 status,
3547 readl(phba->MBslimaddr + 0xa8),
3548 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05003549 phba->link_state = LPFC_HBA_ERROR;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003550 retval = 1;
3551 }
dea31012005-04-17 16:05:31 -05003552
Jamie Wellnitz41415862006-02-28 19:25:27 -05003553 return retval;
dea31012005-04-17 16:05:31 -05003554}
3555
James Smartda0436e2009-05-22 14:51:39 -04003556/**
3557 * lpfc_sli_brdready_s4 - Check for sli4 host ready status
3558 * @phba: Pointer to HBA context object.
3559 * @mask: Bit mask to be checked.
3560 *
3561 * This function checks the host status register to check if HBA is
3562 * ready. This function will wait in a loop for the HBA to be ready
3563 * If the HBA is not ready , the function will will reset the HBA PCI
3564 * function again. The function returns 1 when HBA fail to be ready
3565 * otherwise returns zero.
3566 **/
3567static int
3568lpfc_sli_brdready_s4(struct lpfc_hba *phba, uint32_t mask)
3569{
3570 uint32_t status;
3571 int retval = 0;
3572
3573 /* Read the HBA Host Status Register */
3574 status = lpfc_sli4_post_status_check(phba);
3575
3576 if (status) {
3577 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
3578 lpfc_sli_brdrestart(phba);
3579 status = lpfc_sli4_post_status_check(phba);
3580 }
3581
3582 /* Check to see if any errors occurred during init */
3583 if (status) {
3584 phba->link_state = LPFC_HBA_ERROR;
3585 retval = 1;
3586 } else
3587 phba->sli4_hba.intr_enable = 0;
3588
3589 return retval;
3590}
3591
3592/**
3593 * lpfc_sli_brdready - Wrapper func for checking the hba readyness
3594 * @phba: Pointer to HBA context object.
3595 * @mask: Bit mask to be checked.
3596 *
3597 * This routine wraps the actual SLI3 or SLI4 hba readyness check routine
3598 * from the API jump table function pointer from the lpfc_hba struct.
3599 **/
3600int
3601lpfc_sli_brdready(struct lpfc_hba *phba, uint32_t mask)
3602{
3603 return phba->lpfc_sli_brdready(phba, mask);
3604}
3605
James Smart92908312006-03-07 15:04:13 -05003606#define BARRIER_TEST_PATTERN (0xdeadbeef)
3607
James Smarte59058c2008-08-24 21:49:00 -04003608/**
James Smart3621a712009-04-06 18:47:14 -04003609 * lpfc_reset_barrier - Make HBA ready for HBA reset
James Smarte59058c2008-08-24 21:49:00 -04003610 * @phba: Pointer to HBA context object.
3611 *
James Smart1b511972011-12-13 13:23:09 -05003612 * This function is called before resetting an HBA. This function is called
3613 * with hbalock held and requests HBA to quiesce DMAs before a reset.
James Smarte59058c2008-08-24 21:49:00 -04003614 **/
James Smart2e0fef82007-06-17 19:56:36 -05003615void lpfc_reset_barrier(struct lpfc_hba *phba)
James Smart92908312006-03-07 15:04:13 -05003616{
James Smart65a29c12006-07-06 15:50:50 -04003617 uint32_t __iomem *resp_buf;
3618 uint32_t __iomem *mbox_buf;
James Smart92908312006-03-07 15:04:13 -05003619 volatile uint32_t mbox;
James Smart9940b972011-03-11 16:06:12 -05003620 uint32_t hc_copy, ha_copy, resp_data;
James Smart92908312006-03-07 15:04:13 -05003621 int i;
3622 uint8_t hdrtype;
3623
3624 pci_read_config_byte(phba->pcidev, PCI_HEADER_TYPE, &hdrtype);
3625 if (hdrtype != 0x80 ||
3626 (FC_JEDEC_ID(phba->vpd.rev.biuRev) != HELIOS_JEDEC_ID &&
3627 FC_JEDEC_ID(phba->vpd.rev.biuRev) != THOR_JEDEC_ID))
3628 return;
3629
3630 /*
3631 * Tell the other part of the chip to suspend temporarily all
3632 * its DMA activity.
3633 */
James Smart65a29c12006-07-06 15:50:50 -04003634 resp_buf = phba->MBslimaddr;
James Smart92908312006-03-07 15:04:13 -05003635
3636 /* Disable the error attention */
James Smart9940b972011-03-11 16:06:12 -05003637 if (lpfc_readl(phba->HCregaddr, &hc_copy))
3638 return;
James Smart92908312006-03-07 15:04:13 -05003639 writel((hc_copy & ~HC_ERINT_ENA), phba->HCregaddr);
3640 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05003641 phba->link_flag |= LS_IGNORE_ERATT;
James Smart92908312006-03-07 15:04:13 -05003642
James Smart9940b972011-03-11 16:06:12 -05003643 if (lpfc_readl(phba->HAregaddr, &ha_copy))
3644 return;
3645 if (ha_copy & HA_ERATT) {
James Smart92908312006-03-07 15:04:13 -05003646 /* Clear Chip error bit */
3647 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05003648 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05003649 }
3650
3651 mbox = 0;
3652 ((MAILBOX_t *)&mbox)->mbxCommand = MBX_KILL_BOARD;
3653 ((MAILBOX_t *)&mbox)->mbxOwner = OWN_CHIP;
3654
3655 writel(BARRIER_TEST_PATTERN, (resp_buf + 1));
James Smart65a29c12006-07-06 15:50:50 -04003656 mbox_buf = phba->MBslimaddr;
James Smart92908312006-03-07 15:04:13 -05003657 writel(mbox, mbox_buf);
3658
James Smart9940b972011-03-11 16:06:12 -05003659 for (i = 0; i < 50; i++) {
3660 if (lpfc_readl((resp_buf + 1), &resp_data))
3661 return;
3662 if (resp_data != ~(BARRIER_TEST_PATTERN))
3663 mdelay(1);
3664 else
3665 break;
3666 }
3667 resp_data = 0;
3668 if (lpfc_readl((resp_buf + 1), &resp_data))
3669 return;
3670 if (resp_data != ~(BARRIER_TEST_PATTERN)) {
James Smartf4b4c682009-05-22 14:53:12 -04003671 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE ||
James Smart2e0fef82007-06-17 19:56:36 -05003672 phba->pport->stopped)
James Smart92908312006-03-07 15:04:13 -05003673 goto restore_hc;
3674 else
3675 goto clear_errat;
3676 }
3677
3678 ((MAILBOX_t *)&mbox)->mbxOwner = OWN_HOST;
James Smart9940b972011-03-11 16:06:12 -05003679 resp_data = 0;
3680 for (i = 0; i < 500; i++) {
3681 if (lpfc_readl(resp_buf, &resp_data))
3682 return;
3683 if (resp_data != mbox)
3684 mdelay(1);
3685 else
3686 break;
3687 }
James Smart92908312006-03-07 15:04:13 -05003688
3689clear_errat:
3690
James Smart9940b972011-03-11 16:06:12 -05003691 while (++i < 500) {
3692 if (lpfc_readl(phba->HAregaddr, &ha_copy))
3693 return;
3694 if (!(ha_copy & HA_ERATT))
3695 mdelay(1);
3696 else
3697 break;
3698 }
James Smart92908312006-03-07 15:04:13 -05003699
3700 if (readl(phba->HAregaddr) & HA_ERATT) {
3701 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05003702 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05003703 }
3704
3705restore_hc:
James Smart2e0fef82007-06-17 19:56:36 -05003706 phba->link_flag &= ~LS_IGNORE_ERATT;
James Smart92908312006-03-07 15:04:13 -05003707 writel(hc_copy, phba->HCregaddr);
3708 readl(phba->HCregaddr); /* flush */
3709}
3710
James Smarte59058c2008-08-24 21:49:00 -04003711/**
James Smart3621a712009-04-06 18:47:14 -04003712 * lpfc_sli_brdkill - Issue a kill_board mailbox command
James Smarte59058c2008-08-24 21:49:00 -04003713 * @phba: Pointer to HBA context object.
3714 *
3715 * This function issues a kill_board mailbox command and waits for
3716 * the error attention interrupt. This function is called for stopping
3717 * the firmware processing. The caller is not required to hold any
3718 * locks. This function calls lpfc_hba_down_post function to free
3719 * any pending commands after the kill. The function will return 1 when it
3720 * fails to kill the board else will return 0.
3721 **/
Jamie Wellnitz41415862006-02-28 19:25:27 -05003722int
James Smart2e0fef82007-06-17 19:56:36 -05003723lpfc_sli_brdkill(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05003724{
Jamie Wellnitz41415862006-02-28 19:25:27 -05003725 struct lpfc_sli *psli;
3726 LPFC_MBOXQ_t *pmb;
3727 uint32_t status;
3728 uint32_t ha_copy;
3729 int retval;
3730 int i = 0;
3731
3732 psli = &phba->sli;
3733
3734 /* Kill HBA */
James Smarted957682007-06-17 19:56:37 -05003735 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003736 "0329 Kill HBA Data: x%x x%x\n",
3737 phba->pport->port_state, psli->sli_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003738
James Smart98c9ea52007-10-27 13:37:33 -04003739 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3740 if (!pmb)
Jamie Wellnitz41415862006-02-28 19:25:27 -05003741 return 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003742
3743 /* Disable the error attention */
James Smart2e0fef82007-06-17 19:56:36 -05003744 spin_lock_irq(&phba->hbalock);
James Smart9940b972011-03-11 16:06:12 -05003745 if (lpfc_readl(phba->HCregaddr, &status)) {
3746 spin_unlock_irq(&phba->hbalock);
3747 mempool_free(pmb, phba->mbox_mem_pool);
3748 return 1;
3749 }
Jamie Wellnitz41415862006-02-28 19:25:27 -05003750 status &= ~HC_ERINT_ENA;
3751 writel(status, phba->HCregaddr);
3752 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05003753 phba->link_flag |= LS_IGNORE_ERATT;
3754 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003755
3756 lpfc_kill_board(phba, pmb);
3757 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
3758 retval = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
3759
3760 if (retval != MBX_SUCCESS) {
3761 if (retval != MBX_BUSY)
3762 mempool_free(pmb, phba->mbox_mem_pool);
James Smarte40a02c2010-02-26 14:13:54 -05003763 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3764 "2752 KILL_BOARD command failed retval %d\n",
3765 retval);
James Smart2e0fef82007-06-17 19:56:36 -05003766 spin_lock_irq(&phba->hbalock);
3767 phba->link_flag &= ~LS_IGNORE_ERATT;
3768 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003769 return 1;
3770 }
3771
James Smartf4b4c682009-05-22 14:53:12 -04003772 spin_lock_irq(&phba->hbalock);
3773 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
3774 spin_unlock_irq(&phba->hbalock);
James Smart92908312006-03-07 15:04:13 -05003775
Jamie Wellnitz41415862006-02-28 19:25:27 -05003776 mempool_free(pmb, phba->mbox_mem_pool);
3777
3778 /* There is no completion for a KILL_BOARD mbox cmd. Check for an error
3779 * attention every 100ms for 3 seconds. If we don't get ERATT after
3780 * 3 seconds we still set HBA_ERROR state because the status of the
3781 * board is now undefined.
3782 */
James Smart9940b972011-03-11 16:06:12 -05003783 if (lpfc_readl(phba->HAregaddr, &ha_copy))
3784 return 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003785 while ((i++ < 30) && !(ha_copy & HA_ERATT)) {
3786 mdelay(100);
James Smart9940b972011-03-11 16:06:12 -05003787 if (lpfc_readl(phba->HAregaddr, &ha_copy))
3788 return 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003789 }
3790
3791 del_timer_sync(&psli->mbox_tmo);
James Smart92908312006-03-07 15:04:13 -05003792 if (ha_copy & HA_ERATT) {
3793 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05003794 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05003795 }
James Smart2e0fef82007-06-17 19:56:36 -05003796 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003797 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart04c68492009-05-22 14:52:52 -04003798 psli->mbox_active = NULL;
James Smart2e0fef82007-06-17 19:56:36 -05003799 phba->link_flag &= ~LS_IGNORE_ERATT;
3800 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003801
Jamie Wellnitz41415862006-02-28 19:25:27 -05003802 lpfc_hba_down_post(phba);
James Smart2e0fef82007-06-17 19:56:36 -05003803 phba->link_state = LPFC_HBA_ERROR;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003804
James Smart2e0fef82007-06-17 19:56:36 -05003805 return ha_copy & HA_ERATT ? 0 : 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003806}
3807
James Smarte59058c2008-08-24 21:49:00 -04003808/**
James Smart3772a992009-05-22 14:50:54 -04003809 * lpfc_sli_brdreset - Reset a sli-2 or sli-3 HBA
James Smarte59058c2008-08-24 21:49:00 -04003810 * @phba: Pointer to HBA context object.
3811 *
3812 * This function resets the HBA by writing HC_INITFF to the control
3813 * register. After the HBA resets, this function resets all the iocb ring
3814 * indices. This function disables PCI layer parity checking during
3815 * the reset.
3816 * This function returns 0 always.
3817 * The caller is not required to hold any locks.
3818 **/
Jamie Wellnitz41415862006-02-28 19:25:27 -05003819int
James Smart2e0fef82007-06-17 19:56:36 -05003820lpfc_sli_brdreset(struct lpfc_hba *phba)
Jamie Wellnitz41415862006-02-28 19:25:27 -05003821{
3822 struct lpfc_sli *psli;
dea31012005-04-17 16:05:31 -05003823 struct lpfc_sli_ring *pring;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003824 uint16_t cfg_value;
dea31012005-04-17 16:05:31 -05003825 int i;
dea31012005-04-17 16:05:31 -05003826
Jamie Wellnitz41415862006-02-28 19:25:27 -05003827 psli = &phba->sli;
dea31012005-04-17 16:05:31 -05003828
Jamie Wellnitz41415862006-02-28 19:25:27 -05003829 /* Reset HBA */
3830 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003831 "0325 Reset HBA Data: x%x x%x\n",
James Smart2e0fef82007-06-17 19:56:36 -05003832 phba->pport->port_state, psli->sli_flag);
dea31012005-04-17 16:05:31 -05003833
3834 /* perform board reset */
3835 phba->fc_eventTag = 0;
James Smart4d9ab992009-10-02 15:16:39 -04003836 phba->link_events = 0;
James Smart2e0fef82007-06-17 19:56:36 -05003837 phba->pport->fc_myDID = 0;
3838 phba->pport->fc_prevDID = 0;
dea31012005-04-17 16:05:31 -05003839
Jamie Wellnitz41415862006-02-28 19:25:27 -05003840 /* Turn off parity checking and serr during the physical reset */
3841 pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
3842 pci_write_config_word(phba->pcidev, PCI_COMMAND,
3843 (cfg_value &
3844 ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
3845
James Smart3772a992009-05-22 14:50:54 -04003846 psli->sli_flag &= ~(LPFC_SLI_ACTIVE | LPFC_PROCESS_LA);
3847
Jamie Wellnitz41415862006-02-28 19:25:27 -05003848 /* Now toggle INITFF bit in the Host Control Register */
3849 writel(HC_INITFF, phba->HCregaddr);
3850 mdelay(1);
3851 readl(phba->HCregaddr); /* flush */
3852 writel(0, phba->HCregaddr);
3853 readl(phba->HCregaddr); /* flush */
3854
3855 /* Restore PCI cmd register */
3856 pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
dea31012005-04-17 16:05:31 -05003857
3858 /* Initialize relevant SLI info */
Jamie Wellnitz41415862006-02-28 19:25:27 -05003859 for (i = 0; i < psli->num_rings; i++) {
3860 pring = &psli->ring[i];
dea31012005-04-17 16:05:31 -05003861 pring->flag = 0;
3862 pring->rspidx = 0;
3863 pring->next_cmdidx = 0;
3864 pring->local_getidx = 0;
3865 pring->cmdidx = 0;
3866 pring->missbufcnt = 0;
3867 }
dea31012005-04-17 16:05:31 -05003868
James Smart2e0fef82007-06-17 19:56:36 -05003869 phba->link_state = LPFC_WARM_START;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003870 return 0;
3871}
3872
James Smarte59058c2008-08-24 21:49:00 -04003873/**
James Smartda0436e2009-05-22 14:51:39 -04003874 * lpfc_sli4_brdreset - Reset a sli-4 HBA
3875 * @phba: Pointer to HBA context object.
3876 *
3877 * This function resets a SLI4 HBA. This function disables PCI layer parity
3878 * checking during resets the device. The caller is not required to hold
3879 * any locks.
3880 *
3881 * This function returns 0 always.
3882 **/
3883int
3884lpfc_sli4_brdreset(struct lpfc_hba *phba)
3885{
3886 struct lpfc_sli *psli = &phba->sli;
3887 uint16_t cfg_value;
James Smart27b01b82012-05-09 21:19:44 -04003888 int rc;
James Smartda0436e2009-05-22 14:51:39 -04003889
3890 /* Reset HBA */
3891 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3892 "0295 Reset HBA Data: x%x x%x\n",
3893 phba->pport->port_state, psli->sli_flag);
3894
3895 /* perform board reset */
3896 phba->fc_eventTag = 0;
James Smart4d9ab992009-10-02 15:16:39 -04003897 phba->link_events = 0;
James Smartda0436e2009-05-22 14:51:39 -04003898 phba->pport->fc_myDID = 0;
3899 phba->pport->fc_prevDID = 0;
3900
James Smartda0436e2009-05-22 14:51:39 -04003901 spin_lock_irq(&phba->hbalock);
3902 psli->sli_flag &= ~(LPFC_PROCESS_LA);
3903 phba->fcf.fcf_flag = 0;
James Smartda0436e2009-05-22 14:51:39 -04003904 spin_unlock_irq(&phba->hbalock);
3905
3906 /* Now physically reset the device */
3907 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3908 "0389 Performing PCI function reset!\n");
James Smartbe858b62010-12-15 17:57:20 -05003909
3910 /* Turn off parity checking and serr during the physical reset */
3911 pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
3912 pci_write_config_word(phba->pcidev, PCI_COMMAND, (cfg_value &
3913 ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
3914
James Smartda0436e2009-05-22 14:51:39 -04003915 /* Perform FCoE PCI function reset */
James Smart2e90f4b2011-12-13 13:22:37 -05003916 lpfc_sli4_queue_destroy(phba);
James Smart27b01b82012-05-09 21:19:44 -04003917 rc = lpfc_pci_function_reset(phba);
James Smartda0436e2009-05-22 14:51:39 -04003918
James Smartbe858b62010-12-15 17:57:20 -05003919 /* Restore PCI cmd register */
3920 pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
3921
James Smart27b01b82012-05-09 21:19:44 -04003922 return rc;
James Smartda0436e2009-05-22 14:51:39 -04003923}
3924
3925/**
3926 * lpfc_sli_brdrestart_s3 - Restart a sli-3 hba
James Smarte59058c2008-08-24 21:49:00 -04003927 * @phba: Pointer to HBA context object.
3928 *
3929 * This function is called in the SLI initialization code path to
3930 * restart the HBA. The caller is not required to hold any lock.
3931 * This function writes MBX_RESTART mailbox command to the SLIM and
3932 * resets the HBA. At the end of the function, it calls lpfc_hba_down_post
3933 * function to free any pending commands. The function enables
3934 * POST only during the first initialization. The function returns zero.
3935 * The function does not guarantee completion of MBX_RESTART mailbox
3936 * command before the return of this function.
3937 **/
James Smartda0436e2009-05-22 14:51:39 -04003938static int
3939lpfc_sli_brdrestart_s3(struct lpfc_hba *phba)
Jamie Wellnitz41415862006-02-28 19:25:27 -05003940{
3941 MAILBOX_t *mb;
3942 struct lpfc_sli *psli;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003943 volatile uint32_t word0;
3944 void __iomem *to_slim;
James Smart0d878412009-10-02 15:16:56 -04003945 uint32_t hba_aer_enabled;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003946
James Smart2e0fef82007-06-17 19:56:36 -05003947 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003948
James Smart0d878412009-10-02 15:16:56 -04003949 /* Take PCIe device Advanced Error Reporting (AER) state */
3950 hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
3951
Jamie Wellnitz41415862006-02-28 19:25:27 -05003952 psli = &phba->sli;
3953
3954 /* Restart HBA */
3955 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003956 "0337 Restart HBA Data: x%x x%x\n",
James Smart2e0fef82007-06-17 19:56:36 -05003957 phba->pport->port_state, psli->sli_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003958
3959 word0 = 0;
3960 mb = (MAILBOX_t *) &word0;
3961 mb->mbxCommand = MBX_RESTART;
3962 mb->mbxHc = 1;
3963
James Smart92908312006-03-07 15:04:13 -05003964 lpfc_reset_barrier(phba);
3965
Jamie Wellnitz41415862006-02-28 19:25:27 -05003966 to_slim = phba->MBslimaddr;
3967 writel(*(uint32_t *) mb, to_slim);
3968 readl(to_slim); /* flush */
3969
3970 /* Only skip post after fc_ffinit is completed */
James Smarteaf15d52008-12-04 22:39:29 -05003971 if (phba->pport->port_state)
Jamie Wellnitz41415862006-02-28 19:25:27 -05003972 word0 = 1; /* This is really setting up word1 */
James Smarteaf15d52008-12-04 22:39:29 -05003973 else
Jamie Wellnitz41415862006-02-28 19:25:27 -05003974 word0 = 0; /* This is really setting up word1 */
James Smart65a29c12006-07-06 15:50:50 -04003975 to_slim = phba->MBslimaddr + sizeof (uint32_t);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003976 writel(*(uint32_t *) mb, to_slim);
3977 readl(to_slim); /* flush */
3978
3979 lpfc_sli_brdreset(phba);
James Smart2e0fef82007-06-17 19:56:36 -05003980 phba->pport->stopped = 0;
3981 phba->link_state = LPFC_INIT_START;
James Smartda0436e2009-05-22 14:51:39 -04003982 phba->hba_flag = 0;
James Smart2e0fef82007-06-17 19:56:36 -05003983 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003984
James Smart64ba8812006-08-02 15:24:34 -04003985 memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
3986 psli->stats_start = get_seconds();
3987
James Smarteaf15d52008-12-04 22:39:29 -05003988 /* Give the INITFF and Post time to settle. */
3989 mdelay(100);
dea31012005-04-17 16:05:31 -05003990
James Smart0d878412009-10-02 15:16:56 -04003991 /* Reset HBA AER if it was enabled, note hba_flag was reset above */
3992 if (hba_aer_enabled)
3993 pci_disable_pcie_error_reporting(phba->pcidev);
3994
Jamie Wellnitz41415862006-02-28 19:25:27 -05003995 lpfc_hba_down_post(phba);
dea31012005-04-17 16:05:31 -05003996
3997 return 0;
3998}
3999
James Smarte59058c2008-08-24 21:49:00 -04004000/**
James Smartda0436e2009-05-22 14:51:39 -04004001 * lpfc_sli_brdrestart_s4 - Restart the sli-4 hba
4002 * @phba: Pointer to HBA context object.
4003 *
4004 * This function is called in the SLI initialization code path to restart
4005 * a SLI4 HBA. The caller is not required to hold any lock.
4006 * At the end of the function, it calls lpfc_hba_down_post function to
4007 * free any pending commands.
4008 **/
4009static int
4010lpfc_sli_brdrestart_s4(struct lpfc_hba *phba)
4011{
4012 struct lpfc_sli *psli = &phba->sli;
James Smart75baf692010-06-08 18:31:21 -04004013 uint32_t hba_aer_enabled;
James Smart27b01b82012-05-09 21:19:44 -04004014 int rc;
James Smartda0436e2009-05-22 14:51:39 -04004015
4016 /* Restart HBA */
4017 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4018 "0296 Restart HBA Data: x%x x%x\n",
4019 phba->pport->port_state, psli->sli_flag);
4020
James Smart75baf692010-06-08 18:31:21 -04004021 /* Take PCIe device Advanced Error Reporting (AER) state */
4022 hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
4023
James Smart27b01b82012-05-09 21:19:44 -04004024 rc = lpfc_sli4_brdreset(phba);
James Smartda0436e2009-05-22 14:51:39 -04004025
4026 spin_lock_irq(&phba->hbalock);
4027 phba->pport->stopped = 0;
4028 phba->link_state = LPFC_INIT_START;
4029 phba->hba_flag = 0;
4030 spin_unlock_irq(&phba->hbalock);
4031
4032 memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
4033 psli->stats_start = get_seconds();
4034
James Smart75baf692010-06-08 18:31:21 -04004035 /* Reset HBA AER if it was enabled, note hba_flag was reset above */
4036 if (hba_aer_enabled)
4037 pci_disable_pcie_error_reporting(phba->pcidev);
4038
James Smartda0436e2009-05-22 14:51:39 -04004039 lpfc_hba_down_post(phba);
4040
James Smart27b01b82012-05-09 21:19:44 -04004041 return rc;
James Smartda0436e2009-05-22 14:51:39 -04004042}
4043
4044/**
4045 * lpfc_sli_brdrestart - Wrapper func for restarting hba
4046 * @phba: Pointer to HBA context object.
4047 *
4048 * This routine wraps the actual SLI3 or SLI4 hba restart routine from the
4049 * API jump table function pointer from the lpfc_hba struct.
4050**/
4051int
4052lpfc_sli_brdrestart(struct lpfc_hba *phba)
4053{
4054 return phba->lpfc_sli_brdrestart(phba);
4055}
4056
4057/**
James Smart3621a712009-04-06 18:47:14 -04004058 * lpfc_sli_chipset_init - Wait for the restart of the HBA after a restart
James Smarte59058c2008-08-24 21:49:00 -04004059 * @phba: Pointer to HBA context object.
4060 *
4061 * This function is called after a HBA restart to wait for successful
4062 * restart of the HBA. Successful restart of the HBA is indicated by
4063 * HS_FFRDY and HS_MBRDY bits. If the HBA fails to restart even after 15
4064 * iteration, the function will restart the HBA again. The function returns
4065 * zero if HBA successfully restarted else returns negative error code.
4066 **/
dea31012005-04-17 16:05:31 -05004067static int
4068lpfc_sli_chipset_init(struct lpfc_hba *phba)
4069{
4070 uint32_t status, i = 0;
4071
4072 /* Read the HBA Host Status Register */
James Smart9940b972011-03-11 16:06:12 -05004073 if (lpfc_readl(phba->HSregaddr, &status))
4074 return -EIO;
dea31012005-04-17 16:05:31 -05004075
4076 /* Check status register to see what current state is */
4077 i = 0;
4078 while ((status & (HS_FFRDY | HS_MBRDY)) != (HS_FFRDY | HS_MBRDY)) {
4079
James Smartdcf2a4e2010-09-29 11:18:53 -04004080 /* Check every 10ms for 10 retries, then every 100ms for 90
4081 * retries, then every 1 sec for 50 retires for a total of
4082 * ~60 seconds before reset the board again and check every
4083 * 1 sec for 50 retries. The up to 60 seconds before the
4084 * board ready is required by the Falcon FIPS zeroization
4085 * complete, and any reset the board in between shall cause
4086 * restart of zeroization, further delay the board ready.
dea31012005-04-17 16:05:31 -05004087 */
James Smartdcf2a4e2010-09-29 11:18:53 -04004088 if (i++ >= 200) {
dea31012005-04-17 16:05:31 -05004089 /* Adapter failed to init, timeout, status reg
4090 <status> */
James Smarted957682007-06-17 19:56:37 -05004091 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004092 "0436 Adapter failed to init, "
James Smart09372822008-01-11 01:52:54 -05004093 "timeout, status reg x%x, "
4094 "FW Data: A8 x%x AC x%x\n", status,
4095 readl(phba->MBslimaddr + 0xa8),
4096 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05004097 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05004098 return -ETIMEDOUT;
4099 }
4100
4101 /* Check to see if any errors occurred during init */
4102 if (status & HS_FFERM) {
4103 /* ERROR: During chipset initialization */
4104 /* Adapter failed to init, chipset, status reg
4105 <status> */
James Smarted957682007-06-17 19:56:37 -05004106 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004107 "0437 Adapter failed to init, "
James Smart09372822008-01-11 01:52:54 -05004108 "chipset, status reg x%x, "
4109 "FW Data: A8 x%x AC x%x\n", status,
4110 readl(phba->MBslimaddr + 0xa8),
4111 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05004112 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05004113 return -EIO;
4114 }
4115
James Smartdcf2a4e2010-09-29 11:18:53 -04004116 if (i <= 10)
dea31012005-04-17 16:05:31 -05004117 msleep(10);
James Smartdcf2a4e2010-09-29 11:18:53 -04004118 else if (i <= 100)
4119 msleep(100);
4120 else
4121 msleep(1000);
dea31012005-04-17 16:05:31 -05004122
James Smartdcf2a4e2010-09-29 11:18:53 -04004123 if (i == 150) {
4124 /* Do post */
James Smart92d7f7b2007-06-17 19:56:38 -05004125 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004126 lpfc_sli_brdrestart(phba);
dea31012005-04-17 16:05:31 -05004127 }
4128 /* Read the HBA Host Status Register */
James Smart9940b972011-03-11 16:06:12 -05004129 if (lpfc_readl(phba->HSregaddr, &status))
4130 return -EIO;
dea31012005-04-17 16:05:31 -05004131 }
4132
4133 /* Check to see if any errors occurred during init */
4134 if (status & HS_FFERM) {
4135 /* ERROR: During chipset initialization */
4136 /* Adapter failed to init, chipset, status reg <status> */
James Smarted957682007-06-17 19:56:37 -05004137 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004138 "0438 Adapter failed to init, chipset, "
James Smart09372822008-01-11 01:52:54 -05004139 "status reg x%x, "
4140 "FW Data: A8 x%x AC x%x\n", status,
4141 readl(phba->MBslimaddr + 0xa8),
4142 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05004143 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05004144 return -EIO;
4145 }
4146
4147 /* Clear all interrupt enable conditions */
4148 writel(0, phba->HCregaddr);
4149 readl(phba->HCregaddr); /* flush */
4150
4151 /* setup host attn register */
4152 writel(0xffffffff, phba->HAregaddr);
4153 readl(phba->HAregaddr); /* flush */
4154 return 0;
4155}
4156
James Smarte59058c2008-08-24 21:49:00 -04004157/**
James Smart3621a712009-04-06 18:47:14 -04004158 * lpfc_sli_hbq_count - Get the number of HBQs to be configured
James Smarte59058c2008-08-24 21:49:00 -04004159 *
4160 * This function calculates and returns the number of HBQs required to be
4161 * configured.
4162 **/
James Smart78b2d852007-08-02 11:10:21 -04004163int
James Smarted957682007-06-17 19:56:37 -05004164lpfc_sli_hbq_count(void)
4165{
James Smart92d7f7b2007-06-17 19:56:38 -05004166 return ARRAY_SIZE(lpfc_hbq_defs);
James Smarted957682007-06-17 19:56:37 -05004167}
4168
James Smarte59058c2008-08-24 21:49:00 -04004169/**
James Smart3621a712009-04-06 18:47:14 -04004170 * lpfc_sli_hbq_entry_count - Calculate total number of hbq entries
James Smarte59058c2008-08-24 21:49:00 -04004171 *
4172 * This function adds the number of hbq entries in every HBQ to get
4173 * the total number of hbq entries required for the HBA and returns
4174 * the total count.
4175 **/
James Smarted957682007-06-17 19:56:37 -05004176static int
4177lpfc_sli_hbq_entry_count(void)
4178{
4179 int hbq_count = lpfc_sli_hbq_count();
4180 int count = 0;
4181 int i;
4182
4183 for (i = 0; i < hbq_count; ++i)
James Smart92d7f7b2007-06-17 19:56:38 -05004184 count += lpfc_hbq_defs[i]->entry_count;
James Smarted957682007-06-17 19:56:37 -05004185 return count;
4186}
4187
James Smarte59058c2008-08-24 21:49:00 -04004188/**
James Smart3621a712009-04-06 18:47:14 -04004189 * lpfc_sli_hbq_size - Calculate memory required for all hbq entries
James Smarte59058c2008-08-24 21:49:00 -04004190 *
4191 * This function calculates amount of memory required for all hbq entries
4192 * to be configured and returns the total memory required.
4193 **/
dea31012005-04-17 16:05:31 -05004194int
James Smarted957682007-06-17 19:56:37 -05004195lpfc_sli_hbq_size(void)
4196{
4197 return lpfc_sli_hbq_entry_count() * sizeof(struct lpfc_hbq_entry);
4198}
4199
James Smarte59058c2008-08-24 21:49:00 -04004200/**
James Smart3621a712009-04-06 18:47:14 -04004201 * lpfc_sli_hbq_setup - configure and initialize HBQs
James Smarte59058c2008-08-24 21:49:00 -04004202 * @phba: Pointer to HBA context object.
4203 *
4204 * This function is called during the SLI initialization to configure
4205 * all the HBQs and post buffers to the HBQ. The caller is not
4206 * required to hold any locks. This function will return zero if successful
4207 * else it will return negative error code.
4208 **/
James Smarted957682007-06-17 19:56:37 -05004209static int
4210lpfc_sli_hbq_setup(struct lpfc_hba *phba)
4211{
4212 int hbq_count = lpfc_sli_hbq_count();
4213 LPFC_MBOXQ_t *pmb;
4214 MAILBOX_t *pmbox;
4215 uint32_t hbqno;
4216 uint32_t hbq_entry_index;
James Smarted957682007-06-17 19:56:37 -05004217
James Smart92d7f7b2007-06-17 19:56:38 -05004218 /* Get a Mailbox buffer to setup mailbox
4219 * commands for HBA initialization
4220 */
James Smarted957682007-06-17 19:56:37 -05004221 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4222
4223 if (!pmb)
4224 return -ENOMEM;
4225
James Smart04c68492009-05-22 14:52:52 -04004226 pmbox = &pmb->u.mb;
James Smarted957682007-06-17 19:56:37 -05004227
4228 /* Initialize the struct lpfc_sli_hbq structure for each hbq */
4229 phba->link_state = LPFC_INIT_MBX_CMDS;
James Smart3163f722008-02-08 18:50:25 -05004230 phba->hbq_in_use = 1;
James Smarted957682007-06-17 19:56:37 -05004231
4232 hbq_entry_index = 0;
4233 for (hbqno = 0; hbqno < hbq_count; ++hbqno) {
4234 phba->hbqs[hbqno].next_hbqPutIdx = 0;
4235 phba->hbqs[hbqno].hbqPutIdx = 0;
4236 phba->hbqs[hbqno].local_hbqGetIdx = 0;
4237 phba->hbqs[hbqno].entry_count =
James Smart92d7f7b2007-06-17 19:56:38 -05004238 lpfc_hbq_defs[hbqno]->entry_count;
James Smart51ef4c22007-08-02 11:10:31 -04004239 lpfc_config_hbq(phba, hbqno, lpfc_hbq_defs[hbqno],
4240 hbq_entry_index, pmb);
James Smarted957682007-06-17 19:56:37 -05004241 hbq_entry_index += phba->hbqs[hbqno].entry_count;
4242
4243 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
4244 /* Adapter failed to init, mbxCmd <cmd> CFG_RING,
4245 mbxStatus <status>, ring <num> */
4246
4247 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -05004248 LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04004249 "1805 Adapter failed to init. "
James Smarted957682007-06-17 19:56:37 -05004250 "Data: x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04004251 pmbox->mbxCommand,
James Smarted957682007-06-17 19:56:37 -05004252 pmbox->mbxStatus, hbqno);
4253
4254 phba->link_state = LPFC_HBA_ERROR;
4255 mempool_free(pmb, phba->mbox_mem_pool);
James Smart6e7288d2010-06-07 15:23:35 -04004256 return -ENXIO;
James Smarted957682007-06-17 19:56:37 -05004257 }
4258 }
4259 phba->hbq_count = hbq_count;
4260
James Smarted957682007-06-17 19:56:37 -05004261 mempool_free(pmb, phba->mbox_mem_pool);
4262
James Smart92d7f7b2007-06-17 19:56:38 -05004263 /* Initially populate or replenish the HBQs */
James Smartd7c255b2008-08-24 21:50:00 -04004264 for (hbqno = 0; hbqno < hbq_count; ++hbqno)
4265 lpfc_sli_hbqbuf_init_hbqs(phba, hbqno);
James Smarted957682007-06-17 19:56:37 -05004266 return 0;
4267}
4268
James Smarte59058c2008-08-24 21:49:00 -04004269/**
James Smart4f774512009-05-22 14:52:35 -04004270 * lpfc_sli4_rb_setup - Initialize and post RBs to HBA
4271 * @phba: Pointer to HBA context object.
4272 *
4273 * This function is called during the SLI initialization to configure
4274 * all the HBQs and post buffers to the HBQ. The caller is not
4275 * required to hold any locks. This function will return zero if successful
4276 * else it will return negative error code.
4277 **/
4278static int
4279lpfc_sli4_rb_setup(struct lpfc_hba *phba)
4280{
4281 phba->hbq_in_use = 1;
4282 phba->hbqs[0].entry_count = lpfc_hbq_defs[0]->entry_count;
4283 phba->hbq_count = 1;
4284 /* Initially populate or replenish the HBQs */
4285 lpfc_sli_hbqbuf_init_hbqs(phba, 0);
4286 return 0;
4287}
4288
4289/**
James Smart3621a712009-04-06 18:47:14 -04004290 * lpfc_sli_config_port - Issue config port mailbox command
James Smarte59058c2008-08-24 21:49:00 -04004291 * @phba: Pointer to HBA context object.
4292 * @sli_mode: sli mode - 2/3
4293 *
4294 * This function is called by the sli intialization code path
4295 * to issue config_port mailbox command. This function restarts the
4296 * HBA firmware and issues a config_port mailbox command to configure
4297 * the SLI interface in the sli mode specified by sli_mode
4298 * variable. The caller is not required to hold any locks.
4299 * The function returns 0 if successful, else returns negative error
4300 * code.
4301 **/
James Smart93996272008-08-24 21:50:30 -04004302int
4303lpfc_sli_config_port(struct lpfc_hba *phba, int sli_mode)
dea31012005-04-17 16:05:31 -05004304{
4305 LPFC_MBOXQ_t *pmb;
4306 uint32_t resetcount = 0, rc = 0, done = 0;
4307
4308 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4309 if (!pmb) {
James Smart2e0fef82007-06-17 19:56:36 -05004310 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05004311 return -ENOMEM;
4312 }
4313
James Smarted957682007-06-17 19:56:37 -05004314 phba->sli_rev = sli_mode;
dea31012005-04-17 16:05:31 -05004315 while (resetcount < 2 && !done) {
James Smart2e0fef82007-06-17 19:56:36 -05004316 spin_lock_irq(&phba->hbalock);
James Smart1c067a42006-08-01 07:33:52 -04004317 phba->sli.sli_flag |= LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05004318 spin_unlock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -05004319 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004320 lpfc_sli_brdrestart(phba);
dea31012005-04-17 16:05:31 -05004321 rc = lpfc_sli_chipset_init(phba);
4322 if (rc)
4323 break;
4324
James Smart2e0fef82007-06-17 19:56:36 -05004325 spin_lock_irq(&phba->hbalock);
James Smart1c067a42006-08-01 07:33:52 -04004326 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05004327 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004328 resetcount++;
4329
James Smarted957682007-06-17 19:56:37 -05004330 /* Call pre CONFIG_PORT mailbox command initialization. A
4331 * value of 0 means the call was successful. Any other
4332 * nonzero value is a failure, but if ERESTART is returned,
4333 * the driver may reset the HBA and try again.
4334 */
dea31012005-04-17 16:05:31 -05004335 rc = lpfc_config_port_prep(phba);
4336 if (rc == -ERESTART) {
James Smarted957682007-06-17 19:56:37 -05004337 phba->link_state = LPFC_LINK_UNKNOWN;
dea31012005-04-17 16:05:31 -05004338 continue;
James Smart34b02dc2008-08-24 21:49:55 -04004339 } else if (rc)
dea31012005-04-17 16:05:31 -05004340 break;
James Smart6d368e52011-05-24 11:44:12 -04004341
James Smart2e0fef82007-06-17 19:56:36 -05004342 phba->link_state = LPFC_INIT_MBX_CMDS;
dea31012005-04-17 16:05:31 -05004343 lpfc_config_port(phba, pmb);
4344 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
James Smart34b02dc2008-08-24 21:49:55 -04004345 phba->sli3_options &= ~(LPFC_SLI3_NPIV_ENABLED |
4346 LPFC_SLI3_HBQ_ENABLED |
4347 LPFC_SLI3_CRP_ENABLED |
James Smartbc739052010-08-04 16:11:18 -04004348 LPFC_SLI3_BG_ENABLED |
4349 LPFC_SLI3_DSS_ENABLED);
James Smarted957682007-06-17 19:56:37 -05004350 if (rc != MBX_SUCCESS) {
dea31012005-04-17 16:05:31 -05004351 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004352 "0442 Adapter failed to init, mbxCmd x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05004353 "CONFIG_PORT, mbxStatus x%x Data: x%x\n",
James Smart04c68492009-05-22 14:52:52 -04004354 pmb->u.mb.mbxCommand, pmb->u.mb.mbxStatus, 0);
James Smart2e0fef82007-06-17 19:56:36 -05004355 spin_lock_irq(&phba->hbalock);
James Smart04c68492009-05-22 14:52:52 -04004356 phba->sli.sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05004357 spin_unlock_irq(&phba->hbalock);
4358 rc = -ENXIO;
James Smart04c68492009-05-22 14:52:52 -04004359 } else {
4360 /* Allow asynchronous mailbox command to go through */
4361 spin_lock_irq(&phba->hbalock);
4362 phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
4363 spin_unlock_irq(&phba->hbalock);
James Smarted957682007-06-17 19:56:37 -05004364 done = 1;
James Smartcb69f7d2011-12-13 13:21:57 -05004365
4366 if ((pmb->u.mb.un.varCfgPort.casabt == 1) &&
4367 (pmb->u.mb.un.varCfgPort.gasabt == 0))
4368 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
4369 "3110 Port did not grant ASABT\n");
James Smart04c68492009-05-22 14:52:52 -04004370 }
dea31012005-04-17 16:05:31 -05004371 }
James Smarted957682007-06-17 19:56:37 -05004372 if (!done) {
4373 rc = -EINVAL;
4374 goto do_prep_failed;
4375 }
James Smart04c68492009-05-22 14:52:52 -04004376 if (pmb->u.mb.un.varCfgPort.sli_mode == 3) {
4377 if (!pmb->u.mb.un.varCfgPort.cMA) {
James Smart34b02dc2008-08-24 21:49:55 -04004378 rc = -ENXIO;
4379 goto do_prep_failed;
4380 }
James Smart04c68492009-05-22 14:52:52 -04004381 if (phba->max_vpi && pmb->u.mb.un.varCfgPort.gmv) {
James Smart34b02dc2008-08-24 21:49:55 -04004382 phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
James Smart04c68492009-05-22 14:52:52 -04004383 phba->max_vpi = pmb->u.mb.un.varCfgPort.max_vpi;
4384 phba->max_vports = (phba->max_vpi > phba->max_vports) ?
4385 phba->max_vpi : phba->max_vports;
4386
James Smart34b02dc2008-08-24 21:49:55 -04004387 } else
4388 phba->max_vpi = 0;
James Smartbc739052010-08-04 16:11:18 -04004389 phba->fips_level = 0;
4390 phba->fips_spec_rev = 0;
4391 if (pmb->u.mb.un.varCfgPort.gdss) {
James Smart04c68492009-05-22 14:52:52 -04004392 phba->sli3_options |= LPFC_SLI3_DSS_ENABLED;
James Smartbc739052010-08-04 16:11:18 -04004393 phba->fips_level = pmb->u.mb.un.varCfgPort.fips_level;
4394 phba->fips_spec_rev = pmb->u.mb.un.varCfgPort.fips_rev;
4395 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4396 "2850 Security Crypto Active. FIPS x%d "
4397 "(Spec Rev: x%d)",
4398 phba->fips_level, phba->fips_spec_rev);
4399 }
4400 if (pmb->u.mb.un.varCfgPort.sec_err) {
4401 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4402 "2856 Config Port Security Crypto "
4403 "Error: x%x ",
4404 pmb->u.mb.un.varCfgPort.sec_err);
4405 }
James Smart04c68492009-05-22 14:52:52 -04004406 if (pmb->u.mb.un.varCfgPort.gerbm)
James Smart34b02dc2008-08-24 21:49:55 -04004407 phba->sli3_options |= LPFC_SLI3_HBQ_ENABLED;
James Smart04c68492009-05-22 14:52:52 -04004408 if (pmb->u.mb.un.varCfgPort.gcrp)
James Smart34b02dc2008-08-24 21:49:55 -04004409 phba->sli3_options |= LPFC_SLI3_CRP_ENABLED;
James Smart6e7288d2010-06-07 15:23:35 -04004410
4411 phba->hbq_get = phba->mbox->us.s3_pgp.hbq_get;
4412 phba->port_gp = phba->mbox->us.s3_pgp.port;
James Smarte2a0a9d2008-12-04 22:40:02 -05004413
4414 if (phba->cfg_enable_bg) {
James Smart04c68492009-05-22 14:52:52 -04004415 if (pmb->u.mb.un.varCfgPort.gbg)
James Smarte2a0a9d2008-12-04 22:40:02 -05004416 phba->sli3_options |= LPFC_SLI3_BG_ENABLED;
4417 else
4418 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4419 "0443 Adapter did not grant "
4420 "BlockGuard\n");
4421 }
James Smart34b02dc2008-08-24 21:49:55 -04004422 } else {
James Smart8f34f4c2008-12-04 22:39:23 -05004423 phba->hbq_get = NULL;
James Smart34b02dc2008-08-24 21:49:55 -04004424 phba->port_gp = phba->mbox->us.s2.port;
James Smartd7c255b2008-08-24 21:50:00 -04004425 phba->max_vpi = 0;
James Smarted957682007-06-17 19:56:37 -05004426 }
James Smart92d7f7b2007-06-17 19:56:38 -05004427do_prep_failed:
James Smarted957682007-06-17 19:56:37 -05004428 mempool_free(pmb, phba->mbox_mem_pool);
4429 return rc;
4430}
4431
James Smarte59058c2008-08-24 21:49:00 -04004432
4433/**
James Smart3621a712009-04-06 18:47:14 -04004434 * lpfc_sli_hba_setup - SLI intialization function
James Smarte59058c2008-08-24 21:49:00 -04004435 * @phba: Pointer to HBA context object.
4436 *
4437 * This function is the main SLI intialization function. This function
4438 * is called by the HBA intialization code, HBA reset code and HBA
4439 * error attention handler code. Caller is not required to hold any
4440 * locks. This function issues config_port mailbox command to configure
4441 * the SLI, setup iocb rings and HBQ rings. In the end the function
4442 * calls the config_port_post function to issue init_link mailbox
4443 * command and to start the discovery. The function will return zero
4444 * if successful, else it will return negative error code.
4445 **/
James Smarted957682007-06-17 19:56:37 -05004446int
4447lpfc_sli_hba_setup(struct lpfc_hba *phba)
4448{
4449 uint32_t rc;
James Smart6d368e52011-05-24 11:44:12 -04004450 int mode = 3, i;
4451 int longs;
James Smarted957682007-06-17 19:56:37 -05004452
4453 switch (lpfc_sli_mode) {
4454 case 2:
James Smart78b2d852007-08-02 11:10:21 -04004455 if (phba->cfg_enable_npiv) {
James Smart92d7f7b2007-06-17 19:56:38 -05004456 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04004457 "1824 NPIV enabled: Override lpfc_sli_mode "
James Smart92d7f7b2007-06-17 19:56:38 -05004458 "parameter (%d) to auto (0).\n",
James Smarte8b62012007-08-02 11:10:09 -04004459 lpfc_sli_mode);
James Smart92d7f7b2007-06-17 19:56:38 -05004460 break;
4461 }
James Smarted957682007-06-17 19:56:37 -05004462 mode = 2;
4463 break;
4464 case 0:
4465 case 3:
4466 break;
4467 default:
James Smart92d7f7b2007-06-17 19:56:38 -05004468 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04004469 "1819 Unrecognized lpfc_sli_mode "
4470 "parameter: %d.\n", lpfc_sli_mode);
James Smarted957682007-06-17 19:56:37 -05004471
4472 break;
4473 }
4474
James Smart93996272008-08-24 21:50:30 -04004475 rc = lpfc_sli_config_port(phba, mode);
4476
James Smarted957682007-06-17 19:56:37 -05004477 if (rc && lpfc_sli_mode == 3)
James Smart92d7f7b2007-06-17 19:56:38 -05004478 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04004479 "1820 Unable to select SLI-3. "
4480 "Not supported by adapter.\n");
James Smarted957682007-06-17 19:56:37 -05004481 if (rc && mode != 2)
James Smart93996272008-08-24 21:50:30 -04004482 rc = lpfc_sli_config_port(phba, 2);
James Smarted957682007-06-17 19:56:37 -05004483 if (rc)
dea31012005-04-17 16:05:31 -05004484 goto lpfc_sli_hba_setup_error;
4485
James Smart0d878412009-10-02 15:16:56 -04004486 /* Enable PCIe device Advanced Error Reporting (AER) if configured */
4487 if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
4488 rc = pci_enable_pcie_error_reporting(phba->pcidev);
4489 if (!rc) {
4490 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4491 "2709 This device supports "
4492 "Advanced Error Reporting (AER)\n");
4493 spin_lock_irq(&phba->hbalock);
4494 phba->hba_flag |= HBA_AER_ENABLED;
4495 spin_unlock_irq(&phba->hbalock);
4496 } else {
4497 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4498 "2708 This device does not support "
4499 "Advanced Error Reporting (AER)\n");
4500 phba->cfg_aer_support = 0;
4501 }
4502 }
4503
James Smarted957682007-06-17 19:56:37 -05004504 if (phba->sli_rev == 3) {
4505 phba->iocb_cmd_size = SLI3_IOCB_CMD_SIZE;
4506 phba->iocb_rsp_size = SLI3_IOCB_RSP_SIZE;
James Smarted957682007-06-17 19:56:37 -05004507 } else {
4508 phba->iocb_cmd_size = SLI2_IOCB_CMD_SIZE;
4509 phba->iocb_rsp_size = SLI2_IOCB_RSP_SIZE;
James Smart92d7f7b2007-06-17 19:56:38 -05004510 phba->sli3_options = 0;
James Smarted957682007-06-17 19:56:37 -05004511 }
4512
4513 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004514 "0444 Firmware in SLI %x mode. Max_vpi %d\n",
4515 phba->sli_rev, phba->max_vpi);
James Smarted957682007-06-17 19:56:37 -05004516 rc = lpfc_sli_ring_map(phba);
dea31012005-04-17 16:05:31 -05004517
4518 if (rc)
4519 goto lpfc_sli_hba_setup_error;
4520
James Smart6d368e52011-05-24 11:44:12 -04004521 /* Initialize VPIs. */
4522 if (phba->sli_rev == LPFC_SLI_REV3) {
4523 /*
4524 * The VPI bitmask and physical ID array are allocated
4525 * and initialized once only - at driver load. A port
4526 * reset doesn't need to reinitialize this memory.
4527 */
4528 if ((phba->vpi_bmask == NULL) && (phba->vpi_ids == NULL)) {
4529 longs = (phba->max_vpi + BITS_PER_LONG) / BITS_PER_LONG;
4530 phba->vpi_bmask = kzalloc(longs * sizeof(unsigned long),
4531 GFP_KERNEL);
4532 if (!phba->vpi_bmask) {
4533 rc = -ENOMEM;
4534 goto lpfc_sli_hba_setup_error;
4535 }
4536
4537 phba->vpi_ids = kzalloc(
4538 (phba->max_vpi+1) * sizeof(uint16_t),
4539 GFP_KERNEL);
4540 if (!phba->vpi_ids) {
4541 kfree(phba->vpi_bmask);
4542 rc = -ENOMEM;
4543 goto lpfc_sli_hba_setup_error;
4544 }
4545 for (i = 0; i < phba->max_vpi; i++)
4546 phba->vpi_ids[i] = i;
4547 }
4548 }
4549
James Smart93996272008-08-24 21:50:30 -04004550 /* Init HBQs */
James Smarted957682007-06-17 19:56:37 -05004551 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
4552 rc = lpfc_sli_hbq_setup(phba);
4553 if (rc)
4554 goto lpfc_sli_hba_setup_error;
4555 }
James Smart04c68492009-05-22 14:52:52 -04004556 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004557 phba->sli.sli_flag |= LPFC_PROCESS_LA;
James Smart04c68492009-05-22 14:52:52 -04004558 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004559
4560 rc = lpfc_config_port_post(phba);
4561 if (rc)
4562 goto lpfc_sli_hba_setup_error;
4563
James Smarted957682007-06-17 19:56:37 -05004564 return rc;
4565
James Smart92d7f7b2007-06-17 19:56:38 -05004566lpfc_sli_hba_setup_error:
James Smart2e0fef82007-06-17 19:56:36 -05004567 phba->link_state = LPFC_HBA_ERROR;
James Smarte40a02c2010-02-26 14:13:54 -05004568 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004569 "0445 Firmware initialization failed\n");
dea31012005-04-17 16:05:31 -05004570 return rc;
4571}
4572
James Smartda0436e2009-05-22 14:51:39 -04004573/**
4574 * lpfc_sli4_read_fcoe_params - Read fcoe params from conf region
4575 * @phba: Pointer to HBA context object.
4576 * @mboxq: mailbox pointer.
4577 * This function issue a dump mailbox command to read config region
4578 * 23 and parse the records in the region and populate driver
4579 * data structure.
4580 **/
4581static int
James Smartff78d8f2011-12-13 13:21:35 -05004582lpfc_sli4_read_fcoe_params(struct lpfc_hba *phba)
James Smartda0436e2009-05-22 14:51:39 -04004583{
James Smartff78d8f2011-12-13 13:21:35 -05004584 LPFC_MBOXQ_t *mboxq;
James Smartda0436e2009-05-22 14:51:39 -04004585 struct lpfc_dmabuf *mp;
4586 struct lpfc_mqe *mqe;
4587 uint32_t data_length;
4588 int rc;
4589
4590 /* Program the default value of vlan_id and fc_map */
4591 phba->valid_vlan = 0;
4592 phba->fc_map[0] = LPFC_FCOE_FCF_MAP0;
4593 phba->fc_map[1] = LPFC_FCOE_FCF_MAP1;
4594 phba->fc_map[2] = LPFC_FCOE_FCF_MAP2;
4595
James Smartff78d8f2011-12-13 13:21:35 -05004596 mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4597 if (!mboxq)
James Smartda0436e2009-05-22 14:51:39 -04004598 return -ENOMEM;
4599
James Smartff78d8f2011-12-13 13:21:35 -05004600 mqe = &mboxq->u.mqe;
4601 if (lpfc_sli4_dump_cfg_rg23(phba, mboxq)) {
4602 rc = -ENOMEM;
4603 goto out_free_mboxq;
4604 }
4605
James Smartda0436e2009-05-22 14:51:39 -04004606 mp = (struct lpfc_dmabuf *) mboxq->context1;
4607 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4608
4609 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
4610 "(%d):2571 Mailbox cmd x%x Status x%x "
4611 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x "
4612 "x%x x%x x%x x%x x%x x%x x%x x%x x%x "
4613 "CQ: x%x x%x x%x x%x\n",
4614 mboxq->vport ? mboxq->vport->vpi : 0,
4615 bf_get(lpfc_mqe_command, mqe),
4616 bf_get(lpfc_mqe_status, mqe),
4617 mqe->un.mb_words[0], mqe->un.mb_words[1],
4618 mqe->un.mb_words[2], mqe->un.mb_words[3],
4619 mqe->un.mb_words[4], mqe->un.mb_words[5],
4620 mqe->un.mb_words[6], mqe->un.mb_words[7],
4621 mqe->un.mb_words[8], mqe->un.mb_words[9],
4622 mqe->un.mb_words[10], mqe->un.mb_words[11],
4623 mqe->un.mb_words[12], mqe->un.mb_words[13],
4624 mqe->un.mb_words[14], mqe->un.mb_words[15],
4625 mqe->un.mb_words[16], mqe->un.mb_words[50],
4626 mboxq->mcqe.word0,
4627 mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
4628 mboxq->mcqe.trailer);
4629
4630 if (rc) {
4631 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4632 kfree(mp);
James Smartff78d8f2011-12-13 13:21:35 -05004633 rc = -EIO;
4634 goto out_free_mboxq;
James Smartda0436e2009-05-22 14:51:39 -04004635 }
4636 data_length = mqe->un.mb_words[5];
James Smarta0c87cb2009-07-19 10:01:10 -04004637 if (data_length > DMP_RGN23_SIZE) {
James Smartd11e31d2009-06-10 17:23:06 -04004638 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4639 kfree(mp);
James Smartff78d8f2011-12-13 13:21:35 -05004640 rc = -EIO;
4641 goto out_free_mboxq;
James Smartd11e31d2009-06-10 17:23:06 -04004642 }
James Smartda0436e2009-05-22 14:51:39 -04004643
4644 lpfc_parse_fcoe_conf(phba, mp->virt, data_length);
4645 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4646 kfree(mp);
James Smartff78d8f2011-12-13 13:21:35 -05004647 rc = 0;
4648
4649out_free_mboxq:
4650 mempool_free(mboxq, phba->mbox_mem_pool);
4651 return rc;
James Smartda0436e2009-05-22 14:51:39 -04004652}
4653
4654/**
4655 * lpfc_sli4_read_rev - Issue READ_REV and collect vpd data
4656 * @phba: pointer to lpfc hba data structure.
4657 * @mboxq: pointer to the LPFC_MBOXQ_t structure.
4658 * @vpd: pointer to the memory to hold resulting port vpd data.
4659 * @vpd_size: On input, the number of bytes allocated to @vpd.
4660 * On output, the number of data bytes in @vpd.
4661 *
4662 * This routine executes a READ_REV SLI4 mailbox command. In
4663 * addition, this routine gets the port vpd data.
4664 *
4665 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02004666 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -04004667 * -ENOMEM - could not allocated memory.
James Smartda0436e2009-05-22 14:51:39 -04004668 **/
4669static int
4670lpfc_sli4_read_rev(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
4671 uint8_t *vpd, uint32_t *vpd_size)
4672{
4673 int rc = 0;
4674 uint32_t dma_size;
4675 struct lpfc_dmabuf *dmabuf;
4676 struct lpfc_mqe *mqe;
4677
4678 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
4679 if (!dmabuf)
4680 return -ENOMEM;
4681
4682 /*
4683 * Get a DMA buffer for the vpd data resulting from the READ_REV
4684 * mailbox command.
4685 */
4686 dma_size = *vpd_size;
4687 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
4688 dma_size,
4689 &dmabuf->phys,
4690 GFP_KERNEL);
4691 if (!dmabuf->virt) {
4692 kfree(dmabuf);
4693 return -ENOMEM;
4694 }
4695 memset(dmabuf->virt, 0, dma_size);
4696
4697 /*
4698 * The SLI4 implementation of READ_REV conflicts at word1,
4699 * bits 31:16 and SLI4 adds vpd functionality not present
4700 * in SLI3. This code corrects the conflicts.
4701 */
4702 lpfc_read_rev(phba, mboxq);
4703 mqe = &mboxq->u.mqe;
4704 mqe->un.read_rev.vpd_paddr_high = putPaddrHigh(dmabuf->phys);
4705 mqe->un.read_rev.vpd_paddr_low = putPaddrLow(dmabuf->phys);
4706 mqe->un.read_rev.word1 &= 0x0000FFFF;
4707 bf_set(lpfc_mbx_rd_rev_vpd, &mqe->un.read_rev, 1);
4708 bf_set(lpfc_mbx_rd_rev_avail_len, &mqe->un.read_rev, dma_size);
4709
4710 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4711 if (rc) {
4712 dma_free_coherent(&phba->pcidev->dev, dma_size,
4713 dmabuf->virt, dmabuf->phys);
James Smartdef9c7a2009-12-21 17:02:28 -05004714 kfree(dmabuf);
James Smartda0436e2009-05-22 14:51:39 -04004715 return -EIO;
4716 }
4717
James Smartda0436e2009-05-22 14:51:39 -04004718 /*
4719 * The available vpd length cannot be bigger than the
4720 * DMA buffer passed to the port. Catch the less than
4721 * case and update the caller's size.
4722 */
4723 if (mqe->un.read_rev.avail_vpd_len < *vpd_size)
4724 *vpd_size = mqe->un.read_rev.avail_vpd_len;
4725
James Smartd7c47992010-06-08 18:31:54 -04004726 memcpy(vpd, dmabuf->virt, *vpd_size);
4727
James Smartda0436e2009-05-22 14:51:39 -04004728 dma_free_coherent(&phba->pcidev->dev, dma_size,
4729 dmabuf->virt, dmabuf->phys);
4730 kfree(dmabuf);
4731 return 0;
4732}
4733
4734/**
James Smartcd1c8302011-10-10 21:33:25 -04004735 * lpfc_sli4_retrieve_pport_name - Retrieve SLI4 device physical port name
4736 * @phba: pointer to lpfc hba data structure.
4737 *
4738 * This routine retrieves SLI4 device physical port name this PCI function
4739 * is attached to.
4740 *
4741 * Return codes
4742 * 0 - sucessful
4743 * otherwise - failed to retrieve physical port name
4744 **/
4745static int
4746lpfc_sli4_retrieve_pport_name(struct lpfc_hba *phba)
4747{
4748 LPFC_MBOXQ_t *mboxq;
James Smartcd1c8302011-10-10 21:33:25 -04004749 struct lpfc_mbx_get_cntl_attributes *mbx_cntl_attr;
4750 struct lpfc_controller_attribute *cntl_attr;
4751 struct lpfc_mbx_get_port_name *get_port_name;
4752 void *virtaddr = NULL;
4753 uint32_t alloclen, reqlen;
4754 uint32_t shdr_status, shdr_add_status;
4755 union lpfc_sli4_cfg_shdr *shdr;
4756 char cport_name = 0;
4757 int rc;
4758
4759 /* We assume nothing at this point */
4760 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_INVAL;
4761 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_NON;
4762
4763 mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4764 if (!mboxq)
4765 return -ENOMEM;
James Smartcd1c8302011-10-10 21:33:25 -04004766 /* obtain link type and link number via READ_CONFIG */
James Smartff78d8f2011-12-13 13:21:35 -05004767 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_INVAL;
4768 lpfc_sli4_read_config(phba);
4769 if (phba->sli4_hba.lnk_info.lnk_dv == LPFC_LNK_DAT_VAL)
4770 goto retrieve_ppname;
James Smartcd1c8302011-10-10 21:33:25 -04004771
4772 /* obtain link type and link number via COMMON_GET_CNTL_ATTRIBUTES */
4773 reqlen = sizeof(struct lpfc_mbx_get_cntl_attributes);
4774 alloclen = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
4775 LPFC_MBOX_OPCODE_GET_CNTL_ATTRIBUTES, reqlen,
4776 LPFC_SLI4_MBX_NEMBED);
4777 if (alloclen < reqlen) {
4778 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4779 "3084 Allocated DMA memory size (%d) is "
4780 "less than the requested DMA memory size "
4781 "(%d)\n", alloclen, reqlen);
4782 rc = -ENOMEM;
4783 goto out_free_mboxq;
4784 }
4785 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4786 virtaddr = mboxq->sge_array->addr[0];
4787 mbx_cntl_attr = (struct lpfc_mbx_get_cntl_attributes *)virtaddr;
4788 shdr = &mbx_cntl_attr->cfg_shdr;
4789 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
4790 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
4791 if (shdr_status || shdr_add_status || rc) {
4792 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
4793 "3085 Mailbox x%x (x%x/x%x) failed, "
4794 "rc:x%x, status:x%x, add_status:x%x\n",
4795 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
4796 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
4797 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
4798 rc, shdr_status, shdr_add_status);
4799 rc = -ENXIO;
4800 goto out_free_mboxq;
4801 }
4802 cntl_attr = &mbx_cntl_attr->cntl_attr;
4803 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_VAL;
4804 phba->sli4_hba.lnk_info.lnk_tp =
4805 bf_get(lpfc_cntl_attr_lnk_type, cntl_attr);
4806 phba->sli4_hba.lnk_info.lnk_no =
4807 bf_get(lpfc_cntl_attr_lnk_numb, cntl_attr);
4808 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4809 "3086 lnk_type:%d, lnk_numb:%d\n",
4810 phba->sli4_hba.lnk_info.lnk_tp,
4811 phba->sli4_hba.lnk_info.lnk_no);
4812
4813retrieve_ppname:
4814 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
4815 LPFC_MBOX_OPCODE_GET_PORT_NAME,
4816 sizeof(struct lpfc_mbx_get_port_name) -
4817 sizeof(struct lpfc_sli4_cfg_mhdr),
4818 LPFC_SLI4_MBX_EMBED);
4819 get_port_name = &mboxq->u.mqe.un.get_port_name;
4820 shdr = (union lpfc_sli4_cfg_shdr *)&get_port_name->header.cfg_shdr;
4821 bf_set(lpfc_mbox_hdr_version, &shdr->request, LPFC_OPCODE_VERSION_1);
4822 bf_set(lpfc_mbx_get_port_name_lnk_type, &get_port_name->u.request,
4823 phba->sli4_hba.lnk_info.lnk_tp);
4824 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4825 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
4826 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
4827 if (shdr_status || shdr_add_status || rc) {
4828 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
4829 "3087 Mailbox x%x (x%x/x%x) failed: "
4830 "rc:x%x, status:x%x, add_status:x%x\n",
4831 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
4832 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
4833 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
4834 rc, shdr_status, shdr_add_status);
4835 rc = -ENXIO;
4836 goto out_free_mboxq;
4837 }
4838 switch (phba->sli4_hba.lnk_info.lnk_no) {
4839 case LPFC_LINK_NUMBER_0:
4840 cport_name = bf_get(lpfc_mbx_get_port_name_name0,
4841 &get_port_name->u.response);
4842 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
4843 break;
4844 case LPFC_LINK_NUMBER_1:
4845 cport_name = bf_get(lpfc_mbx_get_port_name_name1,
4846 &get_port_name->u.response);
4847 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
4848 break;
4849 case LPFC_LINK_NUMBER_2:
4850 cport_name = bf_get(lpfc_mbx_get_port_name_name2,
4851 &get_port_name->u.response);
4852 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
4853 break;
4854 case LPFC_LINK_NUMBER_3:
4855 cport_name = bf_get(lpfc_mbx_get_port_name_name3,
4856 &get_port_name->u.response);
4857 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
4858 break;
4859 default:
4860 break;
4861 }
4862
4863 if (phba->sli4_hba.pport_name_sta == LPFC_SLI4_PPNAME_GET) {
4864 phba->Port[0] = cport_name;
4865 phba->Port[1] = '\0';
4866 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4867 "3091 SLI get port name: %s\n", phba->Port);
4868 }
4869
4870out_free_mboxq:
4871 if (rc != MBX_TIMEOUT) {
4872 if (bf_get(lpfc_mqe_command, &mboxq->u.mqe) == MBX_SLI4_CONFIG)
4873 lpfc_sli4_mbox_cmd_free(phba, mboxq);
4874 else
4875 mempool_free(mboxq, phba->mbox_mem_pool);
4876 }
4877 return rc;
4878}
4879
4880/**
James Smartda0436e2009-05-22 14:51:39 -04004881 * lpfc_sli4_arm_cqeq_intr - Arm sli-4 device completion and event queues
4882 * @phba: pointer to lpfc hba data structure.
4883 *
4884 * This routine is called to explicitly arm the SLI4 device's completion and
4885 * event queues
4886 **/
4887static void
4888lpfc_sli4_arm_cqeq_intr(struct lpfc_hba *phba)
4889{
4890 uint8_t fcp_eqidx;
4891
4892 lpfc_sli4_cq_release(phba->sli4_hba.mbx_cq, LPFC_QUEUE_REARM);
4893 lpfc_sli4_cq_release(phba->sli4_hba.els_cq, LPFC_QUEUE_REARM);
James Smart05580562011-05-24 11:40:48 -04004894 fcp_eqidx = 0;
James Smart2e90f4b2011-12-13 13:22:37 -05004895 if (phba->sli4_hba.fcp_cq) {
4896 do
4897 lpfc_sli4_cq_release(phba->sli4_hba.fcp_cq[fcp_eqidx],
4898 LPFC_QUEUE_REARM);
4899 while (++fcp_eqidx < phba->cfg_fcp_eq_count);
4900 }
James Smartda0436e2009-05-22 14:51:39 -04004901 lpfc_sli4_eq_release(phba->sli4_hba.sp_eq, LPFC_QUEUE_REARM);
James Smart2e90f4b2011-12-13 13:22:37 -05004902 if (phba->sli4_hba.fp_eq) {
4903 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count;
4904 fcp_eqidx++)
4905 lpfc_sli4_eq_release(phba->sli4_hba.fp_eq[fcp_eqidx],
4906 LPFC_QUEUE_REARM);
4907 }
James Smartda0436e2009-05-22 14:51:39 -04004908}
4909
4910/**
James Smart6d368e52011-05-24 11:44:12 -04004911 * lpfc_sli4_get_avail_extnt_rsrc - Get available resource extent count.
4912 * @phba: Pointer to HBA context object.
4913 * @type: The resource extent type.
James Smartb76f2dc2011-07-22 18:37:42 -04004914 * @extnt_count: buffer to hold port available extent count.
4915 * @extnt_size: buffer to hold element count per extent.
James Smart6d368e52011-05-24 11:44:12 -04004916 *
James Smartb76f2dc2011-07-22 18:37:42 -04004917 * This function calls the port and retrievs the number of available
4918 * extents and their size for a particular extent type.
4919 *
4920 * Returns: 0 if successful. Nonzero otherwise.
James Smart6d368e52011-05-24 11:44:12 -04004921 **/
James Smartb76f2dc2011-07-22 18:37:42 -04004922int
James Smart6d368e52011-05-24 11:44:12 -04004923lpfc_sli4_get_avail_extnt_rsrc(struct lpfc_hba *phba, uint16_t type,
4924 uint16_t *extnt_count, uint16_t *extnt_size)
4925{
4926 int rc = 0;
4927 uint32_t length;
4928 uint32_t mbox_tmo;
4929 struct lpfc_mbx_get_rsrc_extent_info *rsrc_info;
4930 LPFC_MBOXQ_t *mbox;
4931
4932 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4933 if (!mbox)
4934 return -ENOMEM;
4935
4936 /* Find out how many extents are available for this resource type */
4937 length = (sizeof(struct lpfc_mbx_get_rsrc_extent_info) -
4938 sizeof(struct lpfc_sli4_cfg_mhdr));
4939 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
4940 LPFC_MBOX_OPCODE_GET_RSRC_EXTENT_INFO,
4941 length, LPFC_SLI4_MBX_EMBED);
4942
4943 /* Send an extents count of 0 - the GET doesn't use it. */
4944 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, 0, type,
4945 LPFC_SLI4_MBX_EMBED);
4946 if (unlikely(rc)) {
4947 rc = -EIO;
4948 goto err_exit;
4949 }
4950
4951 if (!phba->sli4_hba.intr_enable)
4952 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
4953 else {
James Smarta183a152011-10-10 21:32:43 -04004954 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart6d368e52011-05-24 11:44:12 -04004955 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
4956 }
4957 if (unlikely(rc)) {
4958 rc = -EIO;
4959 goto err_exit;
4960 }
4961
4962 rsrc_info = &mbox->u.mqe.un.rsrc_extent_info;
4963 if (bf_get(lpfc_mbox_hdr_status,
4964 &rsrc_info->header.cfg_shdr.response)) {
4965 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
4966 "2930 Failed to get resource extents "
4967 "Status 0x%x Add'l Status 0x%x\n",
4968 bf_get(lpfc_mbox_hdr_status,
4969 &rsrc_info->header.cfg_shdr.response),
4970 bf_get(lpfc_mbox_hdr_add_status,
4971 &rsrc_info->header.cfg_shdr.response));
4972 rc = -EIO;
4973 goto err_exit;
4974 }
4975
4976 *extnt_count = bf_get(lpfc_mbx_get_rsrc_extent_info_cnt,
4977 &rsrc_info->u.rsp);
4978 *extnt_size = bf_get(lpfc_mbx_get_rsrc_extent_info_size,
4979 &rsrc_info->u.rsp);
James Smart8a9d2e82012-05-09 21:16:12 -04004980
4981 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4982 "3162 Retrieved extents type-%d from port: count:%d, "
4983 "size:%d\n", type, *extnt_count, *extnt_size);
4984
4985err_exit:
James Smart6d368e52011-05-24 11:44:12 -04004986 mempool_free(mbox, phba->mbox_mem_pool);
4987 return rc;
4988}
4989
4990/**
4991 * lpfc_sli4_chk_avail_extnt_rsrc - Check for available SLI4 resource extents.
4992 * @phba: Pointer to HBA context object.
4993 * @type: The extent type to check.
4994 *
4995 * This function reads the current available extents from the port and checks
4996 * if the extent count or extent size has changed since the last access.
4997 * Callers use this routine post port reset to understand if there is a
4998 * extent reprovisioning requirement.
4999 *
5000 * Returns:
5001 * -Error: error indicates problem.
5002 * 1: Extent count or size has changed.
5003 * 0: No changes.
5004 **/
5005static int
5006lpfc_sli4_chk_avail_extnt_rsrc(struct lpfc_hba *phba, uint16_t type)
5007{
5008 uint16_t curr_ext_cnt, rsrc_ext_cnt;
5009 uint16_t size_diff, rsrc_ext_size;
5010 int rc = 0;
5011 struct lpfc_rsrc_blks *rsrc_entry;
5012 struct list_head *rsrc_blk_list = NULL;
5013
5014 size_diff = 0;
5015 curr_ext_cnt = 0;
5016 rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,
5017 &rsrc_ext_cnt,
5018 &rsrc_ext_size);
5019 if (unlikely(rc))
5020 return -EIO;
5021
5022 switch (type) {
5023 case LPFC_RSC_TYPE_FCOE_RPI:
5024 rsrc_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;
5025 break;
5026 case LPFC_RSC_TYPE_FCOE_VPI:
5027 rsrc_blk_list = &phba->lpfc_vpi_blk_list;
5028 break;
5029 case LPFC_RSC_TYPE_FCOE_XRI:
5030 rsrc_blk_list = &phba->sli4_hba.lpfc_xri_blk_list;
5031 break;
5032 case LPFC_RSC_TYPE_FCOE_VFI:
5033 rsrc_blk_list = &phba->sli4_hba.lpfc_vfi_blk_list;
5034 break;
5035 default:
5036 break;
5037 }
5038
5039 list_for_each_entry(rsrc_entry, rsrc_blk_list, list) {
5040 curr_ext_cnt++;
5041 if (rsrc_entry->rsrc_size != rsrc_ext_size)
5042 size_diff++;
5043 }
5044
5045 if (curr_ext_cnt != rsrc_ext_cnt || size_diff != 0)
5046 rc = 1;
5047
5048 return rc;
5049}
5050
5051/**
5052 * lpfc_sli4_cfg_post_extnts -
5053 * @phba: Pointer to HBA context object.
5054 * @extnt_cnt - number of available extents.
5055 * @type - the extent type (rpi, xri, vfi, vpi).
5056 * @emb - buffer to hold either MBX_EMBED or MBX_NEMBED operation.
5057 * @mbox - pointer to the caller's allocated mailbox structure.
5058 *
5059 * This function executes the extents allocation request. It also
5060 * takes care of the amount of memory needed to allocate or get the
5061 * allocated extents. It is the caller's responsibility to evaluate
5062 * the response.
5063 *
5064 * Returns:
5065 * -Error: Error value describes the condition found.
5066 * 0: if successful
5067 **/
5068static int
James Smart8a9d2e82012-05-09 21:16:12 -04005069lpfc_sli4_cfg_post_extnts(struct lpfc_hba *phba, uint16_t extnt_cnt,
James Smart6d368e52011-05-24 11:44:12 -04005070 uint16_t type, bool *emb, LPFC_MBOXQ_t *mbox)
5071{
5072 int rc = 0;
5073 uint32_t req_len;
5074 uint32_t emb_len;
5075 uint32_t alloc_len, mbox_tmo;
5076
5077 /* Calculate the total requested length of the dma memory */
James Smart8a9d2e82012-05-09 21:16:12 -04005078 req_len = extnt_cnt * sizeof(uint16_t);
James Smart6d368e52011-05-24 11:44:12 -04005079
5080 /*
5081 * Calculate the size of an embedded mailbox. The uint32_t
5082 * accounts for extents-specific word.
5083 */
5084 emb_len = sizeof(MAILBOX_t) - sizeof(struct mbox_header) -
5085 sizeof(uint32_t);
5086
5087 /*
5088 * Presume the allocation and response will fit into an embedded
5089 * mailbox. If not true, reconfigure to a non-embedded mailbox.
5090 */
5091 *emb = LPFC_SLI4_MBX_EMBED;
5092 if (req_len > emb_len) {
James Smart8a9d2e82012-05-09 21:16:12 -04005093 req_len = extnt_cnt * sizeof(uint16_t) +
James Smart6d368e52011-05-24 11:44:12 -04005094 sizeof(union lpfc_sli4_cfg_shdr) +
5095 sizeof(uint32_t);
5096 *emb = LPFC_SLI4_MBX_NEMBED;
5097 }
5098
5099 alloc_len = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5100 LPFC_MBOX_OPCODE_ALLOC_RSRC_EXTENT,
5101 req_len, *emb);
5102 if (alloc_len < req_len) {
5103 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartb76f2dc2011-07-22 18:37:42 -04005104 "2982 Allocated DMA memory size (x%x) is "
James Smart6d368e52011-05-24 11:44:12 -04005105 "less than the requested DMA memory "
5106 "size (x%x)\n", alloc_len, req_len);
5107 return -ENOMEM;
5108 }
James Smart8a9d2e82012-05-09 21:16:12 -04005109 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, extnt_cnt, type, *emb);
James Smart6d368e52011-05-24 11:44:12 -04005110 if (unlikely(rc))
5111 return -EIO;
5112
5113 if (!phba->sli4_hba.intr_enable)
5114 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5115 else {
James Smarta183a152011-10-10 21:32:43 -04005116 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart6d368e52011-05-24 11:44:12 -04005117 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5118 }
5119
5120 if (unlikely(rc))
5121 rc = -EIO;
5122 return rc;
5123}
5124
5125/**
5126 * lpfc_sli4_alloc_extent - Allocate an SLI4 resource extent.
5127 * @phba: Pointer to HBA context object.
5128 * @type: The resource extent type to allocate.
5129 *
5130 * This function allocates the number of elements for the specified
5131 * resource type.
5132 **/
5133static int
5134lpfc_sli4_alloc_extent(struct lpfc_hba *phba, uint16_t type)
5135{
5136 bool emb = false;
5137 uint16_t rsrc_id_cnt, rsrc_cnt, rsrc_size;
5138 uint16_t rsrc_id, rsrc_start, j, k;
5139 uint16_t *ids;
5140 int i, rc;
5141 unsigned long longs;
5142 unsigned long *bmask;
5143 struct lpfc_rsrc_blks *rsrc_blks;
5144 LPFC_MBOXQ_t *mbox;
5145 uint32_t length;
5146 struct lpfc_id_range *id_array = NULL;
5147 void *virtaddr = NULL;
5148 struct lpfc_mbx_nembed_rsrc_extent *n_rsrc;
5149 struct lpfc_mbx_alloc_rsrc_extents *rsrc_ext;
5150 struct list_head *ext_blk_list;
5151
5152 rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,
5153 &rsrc_cnt,
5154 &rsrc_size);
5155 if (unlikely(rc))
5156 return -EIO;
5157
5158 if ((rsrc_cnt == 0) || (rsrc_size == 0)) {
5159 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
5160 "3009 No available Resource Extents "
5161 "for resource type 0x%x: Count: 0x%x, "
5162 "Size 0x%x\n", type, rsrc_cnt,
5163 rsrc_size);
5164 return -ENOMEM;
5165 }
5166
James Smart8a9d2e82012-05-09 21:16:12 -04005167 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_INIT | LOG_SLI,
5168 "2903 Post resource extents type-0x%x: "
5169 "count:%d, size %d\n", type, rsrc_cnt, rsrc_size);
James Smart6d368e52011-05-24 11:44:12 -04005170
5171 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5172 if (!mbox)
5173 return -ENOMEM;
5174
James Smart8a9d2e82012-05-09 21:16:12 -04005175 rc = lpfc_sli4_cfg_post_extnts(phba, rsrc_cnt, type, &emb, mbox);
James Smart6d368e52011-05-24 11:44:12 -04005176 if (unlikely(rc)) {
5177 rc = -EIO;
5178 goto err_exit;
5179 }
5180
5181 /*
5182 * Figure out where the response is located. Then get local pointers
5183 * to the response data. The port does not guarantee to respond to
5184 * all extents counts request so update the local variable with the
5185 * allocated count from the port.
5186 */
5187 if (emb == LPFC_SLI4_MBX_EMBED) {
5188 rsrc_ext = &mbox->u.mqe.un.alloc_rsrc_extents;
5189 id_array = &rsrc_ext->u.rsp.id[0];
5190 rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, &rsrc_ext->u.rsp);
5191 } else {
5192 virtaddr = mbox->sge_array->addr[0];
5193 n_rsrc = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
5194 rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, n_rsrc);
5195 id_array = &n_rsrc->id;
5196 }
5197
5198 longs = ((rsrc_cnt * rsrc_size) + BITS_PER_LONG - 1) / BITS_PER_LONG;
5199 rsrc_id_cnt = rsrc_cnt * rsrc_size;
5200
5201 /*
5202 * Based on the resource size and count, correct the base and max
5203 * resource values.
5204 */
5205 length = sizeof(struct lpfc_rsrc_blks);
5206 switch (type) {
5207 case LPFC_RSC_TYPE_FCOE_RPI:
5208 phba->sli4_hba.rpi_bmask = kzalloc(longs *
5209 sizeof(unsigned long),
5210 GFP_KERNEL);
5211 if (unlikely(!phba->sli4_hba.rpi_bmask)) {
5212 rc = -ENOMEM;
5213 goto err_exit;
5214 }
5215 phba->sli4_hba.rpi_ids = kzalloc(rsrc_id_cnt *
5216 sizeof(uint16_t),
5217 GFP_KERNEL);
5218 if (unlikely(!phba->sli4_hba.rpi_ids)) {
5219 kfree(phba->sli4_hba.rpi_bmask);
5220 rc = -ENOMEM;
5221 goto err_exit;
5222 }
5223
5224 /*
5225 * The next_rpi was initialized with the maximum available
5226 * count but the port may allocate a smaller number. Catch
5227 * that case and update the next_rpi.
5228 */
5229 phba->sli4_hba.next_rpi = rsrc_id_cnt;
5230
5231 /* Initialize local ptrs for common extent processing later. */
5232 bmask = phba->sli4_hba.rpi_bmask;
5233 ids = phba->sli4_hba.rpi_ids;
5234 ext_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;
5235 break;
5236 case LPFC_RSC_TYPE_FCOE_VPI:
5237 phba->vpi_bmask = kzalloc(longs *
5238 sizeof(unsigned long),
5239 GFP_KERNEL);
5240 if (unlikely(!phba->vpi_bmask)) {
5241 rc = -ENOMEM;
5242 goto err_exit;
5243 }
5244 phba->vpi_ids = kzalloc(rsrc_id_cnt *
5245 sizeof(uint16_t),
5246 GFP_KERNEL);
5247 if (unlikely(!phba->vpi_ids)) {
5248 kfree(phba->vpi_bmask);
5249 rc = -ENOMEM;
5250 goto err_exit;
5251 }
5252
5253 /* Initialize local ptrs for common extent processing later. */
5254 bmask = phba->vpi_bmask;
5255 ids = phba->vpi_ids;
5256 ext_blk_list = &phba->lpfc_vpi_blk_list;
5257 break;
5258 case LPFC_RSC_TYPE_FCOE_XRI:
5259 phba->sli4_hba.xri_bmask = kzalloc(longs *
5260 sizeof(unsigned long),
5261 GFP_KERNEL);
5262 if (unlikely(!phba->sli4_hba.xri_bmask)) {
5263 rc = -ENOMEM;
5264 goto err_exit;
5265 }
James Smart8a9d2e82012-05-09 21:16:12 -04005266 phba->sli4_hba.max_cfg_param.xri_used = 0;
James Smart6d368e52011-05-24 11:44:12 -04005267 phba->sli4_hba.xri_ids = kzalloc(rsrc_id_cnt *
5268 sizeof(uint16_t),
5269 GFP_KERNEL);
5270 if (unlikely(!phba->sli4_hba.xri_ids)) {
5271 kfree(phba->sli4_hba.xri_bmask);
5272 rc = -ENOMEM;
5273 goto err_exit;
5274 }
5275
5276 /* Initialize local ptrs for common extent processing later. */
5277 bmask = phba->sli4_hba.xri_bmask;
5278 ids = phba->sli4_hba.xri_ids;
5279 ext_blk_list = &phba->sli4_hba.lpfc_xri_blk_list;
5280 break;
5281 case LPFC_RSC_TYPE_FCOE_VFI:
5282 phba->sli4_hba.vfi_bmask = kzalloc(longs *
5283 sizeof(unsigned long),
5284 GFP_KERNEL);
5285 if (unlikely(!phba->sli4_hba.vfi_bmask)) {
5286 rc = -ENOMEM;
5287 goto err_exit;
5288 }
5289 phba->sli4_hba.vfi_ids = kzalloc(rsrc_id_cnt *
5290 sizeof(uint16_t),
5291 GFP_KERNEL);
5292 if (unlikely(!phba->sli4_hba.vfi_ids)) {
5293 kfree(phba->sli4_hba.vfi_bmask);
5294 rc = -ENOMEM;
5295 goto err_exit;
5296 }
5297
5298 /* Initialize local ptrs for common extent processing later. */
5299 bmask = phba->sli4_hba.vfi_bmask;
5300 ids = phba->sli4_hba.vfi_ids;
5301 ext_blk_list = &phba->sli4_hba.lpfc_vfi_blk_list;
5302 break;
5303 default:
5304 /* Unsupported Opcode. Fail call. */
5305 id_array = NULL;
5306 bmask = NULL;
5307 ids = NULL;
5308 ext_blk_list = NULL;
5309 goto err_exit;
5310 }
5311
5312 /*
5313 * Complete initializing the extent configuration with the
5314 * allocated ids assigned to this function. The bitmask serves
5315 * as an index into the array and manages the available ids. The
5316 * array just stores the ids communicated to the port via the wqes.
5317 */
5318 for (i = 0, j = 0, k = 0; i < rsrc_cnt; i++) {
5319 if ((i % 2) == 0)
5320 rsrc_id = bf_get(lpfc_mbx_rsrc_id_word4_0,
5321 &id_array[k]);
5322 else
5323 rsrc_id = bf_get(lpfc_mbx_rsrc_id_word4_1,
5324 &id_array[k]);
5325
5326 rsrc_blks = kzalloc(length, GFP_KERNEL);
5327 if (unlikely(!rsrc_blks)) {
5328 rc = -ENOMEM;
5329 kfree(bmask);
5330 kfree(ids);
5331 goto err_exit;
5332 }
5333 rsrc_blks->rsrc_start = rsrc_id;
5334 rsrc_blks->rsrc_size = rsrc_size;
5335 list_add_tail(&rsrc_blks->list, ext_blk_list);
5336 rsrc_start = rsrc_id;
5337 if ((type == LPFC_RSC_TYPE_FCOE_XRI) && (j == 0))
5338 phba->sli4_hba.scsi_xri_start = rsrc_start +
5339 lpfc_sli4_get_els_iocb_cnt(phba);
5340
5341 while (rsrc_id < (rsrc_start + rsrc_size)) {
5342 ids[j] = rsrc_id;
5343 rsrc_id++;
5344 j++;
5345 }
5346 /* Entire word processed. Get next word.*/
5347 if ((i % 2) == 1)
5348 k++;
5349 }
5350 err_exit:
5351 lpfc_sli4_mbox_cmd_free(phba, mbox);
5352 return rc;
5353}
5354
5355/**
5356 * lpfc_sli4_dealloc_extent - Deallocate an SLI4 resource extent.
5357 * @phba: Pointer to HBA context object.
5358 * @type: the extent's type.
5359 *
5360 * This function deallocates all extents of a particular resource type.
5361 * SLI4 does not allow for deallocating a particular extent range. It
5362 * is the caller's responsibility to release all kernel memory resources.
5363 **/
5364static int
5365lpfc_sli4_dealloc_extent(struct lpfc_hba *phba, uint16_t type)
5366{
5367 int rc;
5368 uint32_t length, mbox_tmo = 0;
5369 LPFC_MBOXQ_t *mbox;
5370 struct lpfc_mbx_dealloc_rsrc_extents *dealloc_rsrc;
5371 struct lpfc_rsrc_blks *rsrc_blk, *rsrc_blk_next;
5372
5373 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5374 if (!mbox)
5375 return -ENOMEM;
5376
5377 /*
5378 * This function sends an embedded mailbox because it only sends the
5379 * the resource type. All extents of this type are released by the
5380 * port.
5381 */
5382 length = (sizeof(struct lpfc_mbx_dealloc_rsrc_extents) -
5383 sizeof(struct lpfc_sli4_cfg_mhdr));
5384 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5385 LPFC_MBOX_OPCODE_DEALLOC_RSRC_EXTENT,
5386 length, LPFC_SLI4_MBX_EMBED);
5387
5388 /* Send an extents count of 0 - the dealloc doesn't use it. */
5389 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, 0, type,
5390 LPFC_SLI4_MBX_EMBED);
5391 if (unlikely(rc)) {
5392 rc = -EIO;
5393 goto out_free_mbox;
5394 }
5395 if (!phba->sli4_hba.intr_enable)
5396 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5397 else {
James Smarta183a152011-10-10 21:32:43 -04005398 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart6d368e52011-05-24 11:44:12 -04005399 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5400 }
5401 if (unlikely(rc)) {
5402 rc = -EIO;
5403 goto out_free_mbox;
5404 }
5405
5406 dealloc_rsrc = &mbox->u.mqe.un.dealloc_rsrc_extents;
5407 if (bf_get(lpfc_mbox_hdr_status,
5408 &dealloc_rsrc->header.cfg_shdr.response)) {
5409 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
5410 "2919 Failed to release resource extents "
5411 "for type %d - Status 0x%x Add'l Status 0x%x. "
5412 "Resource memory not released.\n",
5413 type,
5414 bf_get(lpfc_mbox_hdr_status,
5415 &dealloc_rsrc->header.cfg_shdr.response),
5416 bf_get(lpfc_mbox_hdr_add_status,
5417 &dealloc_rsrc->header.cfg_shdr.response));
5418 rc = -EIO;
5419 goto out_free_mbox;
5420 }
5421
5422 /* Release kernel memory resources for the specific type. */
5423 switch (type) {
5424 case LPFC_RSC_TYPE_FCOE_VPI:
5425 kfree(phba->vpi_bmask);
5426 kfree(phba->vpi_ids);
5427 bf_set(lpfc_vpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5428 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5429 &phba->lpfc_vpi_blk_list, list) {
5430 list_del_init(&rsrc_blk->list);
5431 kfree(rsrc_blk);
5432 }
5433 break;
5434 case LPFC_RSC_TYPE_FCOE_XRI:
5435 kfree(phba->sli4_hba.xri_bmask);
5436 kfree(phba->sli4_hba.xri_ids);
James Smart6d368e52011-05-24 11:44:12 -04005437 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5438 &phba->sli4_hba.lpfc_xri_blk_list, list) {
5439 list_del_init(&rsrc_blk->list);
5440 kfree(rsrc_blk);
5441 }
5442 break;
5443 case LPFC_RSC_TYPE_FCOE_VFI:
5444 kfree(phba->sli4_hba.vfi_bmask);
5445 kfree(phba->sli4_hba.vfi_ids);
5446 bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5447 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5448 &phba->sli4_hba.lpfc_vfi_blk_list, list) {
5449 list_del_init(&rsrc_blk->list);
5450 kfree(rsrc_blk);
5451 }
5452 break;
5453 case LPFC_RSC_TYPE_FCOE_RPI:
5454 /* RPI bitmask and physical id array are cleaned up earlier. */
5455 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5456 &phba->sli4_hba.lpfc_rpi_blk_list, list) {
5457 list_del_init(&rsrc_blk->list);
5458 kfree(rsrc_blk);
5459 }
5460 break;
5461 default:
5462 break;
5463 }
5464
5465 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5466
5467 out_free_mbox:
5468 mempool_free(mbox, phba->mbox_mem_pool);
5469 return rc;
5470}
5471
5472/**
5473 * lpfc_sli4_alloc_resource_identifiers - Allocate all SLI4 resource extents.
5474 * @phba: Pointer to HBA context object.
5475 *
5476 * This function allocates all SLI4 resource identifiers.
5477 **/
5478int
5479lpfc_sli4_alloc_resource_identifiers(struct lpfc_hba *phba)
5480{
5481 int i, rc, error = 0;
5482 uint16_t count, base;
5483 unsigned long longs;
5484
James Smartff78d8f2011-12-13 13:21:35 -05005485 if (!phba->sli4_hba.rpi_hdrs_in_use)
5486 phba->sli4_hba.next_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
James Smart6d368e52011-05-24 11:44:12 -04005487 if (phba->sli4_hba.extents_in_use) {
5488 /*
5489 * The port supports resource extents. The XRI, VPI, VFI, RPI
5490 * resource extent count must be read and allocated before
5491 * provisioning the resource id arrays.
5492 */
5493 if (bf_get(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags) ==
5494 LPFC_IDX_RSRC_RDY) {
5495 /*
5496 * Extent-based resources are set - the driver could
5497 * be in a port reset. Figure out if any corrective
5498 * actions need to be taken.
5499 */
5500 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
5501 LPFC_RSC_TYPE_FCOE_VFI);
5502 if (rc != 0)
5503 error++;
5504 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
5505 LPFC_RSC_TYPE_FCOE_VPI);
5506 if (rc != 0)
5507 error++;
5508 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
5509 LPFC_RSC_TYPE_FCOE_XRI);
5510 if (rc != 0)
5511 error++;
5512 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
5513 LPFC_RSC_TYPE_FCOE_RPI);
5514 if (rc != 0)
5515 error++;
5516
5517 /*
5518 * It's possible that the number of resources
5519 * provided to this port instance changed between
5520 * resets. Detect this condition and reallocate
5521 * resources. Otherwise, there is no action.
5522 */
5523 if (error) {
5524 lpfc_printf_log(phba, KERN_INFO,
5525 LOG_MBOX | LOG_INIT,
5526 "2931 Detected extent resource "
5527 "change. Reallocating all "
5528 "extents.\n");
5529 rc = lpfc_sli4_dealloc_extent(phba,
5530 LPFC_RSC_TYPE_FCOE_VFI);
5531 rc = lpfc_sli4_dealloc_extent(phba,
5532 LPFC_RSC_TYPE_FCOE_VPI);
5533 rc = lpfc_sli4_dealloc_extent(phba,
5534 LPFC_RSC_TYPE_FCOE_XRI);
5535 rc = lpfc_sli4_dealloc_extent(phba,
5536 LPFC_RSC_TYPE_FCOE_RPI);
5537 } else
5538 return 0;
5539 }
5540
5541 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_VFI);
5542 if (unlikely(rc))
5543 goto err_exit;
5544
5545 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_VPI);
5546 if (unlikely(rc))
5547 goto err_exit;
5548
5549 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_RPI);
5550 if (unlikely(rc))
5551 goto err_exit;
5552
5553 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_XRI);
5554 if (unlikely(rc))
5555 goto err_exit;
5556 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags,
5557 LPFC_IDX_RSRC_RDY);
5558 return rc;
5559 } else {
5560 /*
5561 * The port does not support resource extents. The XRI, VPI,
5562 * VFI, RPI resource ids were determined from READ_CONFIG.
5563 * Just allocate the bitmasks and provision the resource id
5564 * arrays. If a port reset is active, the resources don't
5565 * need any action - just exit.
5566 */
5567 if (bf_get(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags) ==
James Smartff78d8f2011-12-13 13:21:35 -05005568 LPFC_IDX_RSRC_RDY) {
5569 lpfc_sli4_dealloc_resource_identifiers(phba);
5570 lpfc_sli4_remove_rpis(phba);
5571 }
James Smart6d368e52011-05-24 11:44:12 -04005572 /* RPIs. */
5573 count = phba->sli4_hba.max_cfg_param.max_rpi;
5574 base = phba->sli4_hba.max_cfg_param.rpi_base;
5575 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
5576 phba->sli4_hba.rpi_bmask = kzalloc(longs *
5577 sizeof(unsigned long),
5578 GFP_KERNEL);
5579 if (unlikely(!phba->sli4_hba.rpi_bmask)) {
5580 rc = -ENOMEM;
5581 goto err_exit;
5582 }
5583 phba->sli4_hba.rpi_ids = kzalloc(count *
5584 sizeof(uint16_t),
5585 GFP_KERNEL);
5586 if (unlikely(!phba->sli4_hba.rpi_ids)) {
5587 rc = -ENOMEM;
5588 goto free_rpi_bmask;
5589 }
5590
5591 for (i = 0; i < count; i++)
5592 phba->sli4_hba.rpi_ids[i] = base + i;
5593
5594 /* VPIs. */
5595 count = phba->sli4_hba.max_cfg_param.max_vpi;
5596 base = phba->sli4_hba.max_cfg_param.vpi_base;
5597 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
5598 phba->vpi_bmask = kzalloc(longs *
5599 sizeof(unsigned long),
5600 GFP_KERNEL);
5601 if (unlikely(!phba->vpi_bmask)) {
5602 rc = -ENOMEM;
5603 goto free_rpi_ids;
5604 }
5605 phba->vpi_ids = kzalloc(count *
5606 sizeof(uint16_t),
5607 GFP_KERNEL);
5608 if (unlikely(!phba->vpi_ids)) {
5609 rc = -ENOMEM;
5610 goto free_vpi_bmask;
5611 }
5612
5613 for (i = 0; i < count; i++)
5614 phba->vpi_ids[i] = base + i;
5615
5616 /* XRIs. */
5617 count = phba->sli4_hba.max_cfg_param.max_xri;
5618 base = phba->sli4_hba.max_cfg_param.xri_base;
5619 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
5620 phba->sli4_hba.xri_bmask = kzalloc(longs *
5621 sizeof(unsigned long),
5622 GFP_KERNEL);
5623 if (unlikely(!phba->sli4_hba.xri_bmask)) {
5624 rc = -ENOMEM;
5625 goto free_vpi_ids;
5626 }
James Smart41899be2012-03-01 22:34:19 -05005627 phba->sli4_hba.max_cfg_param.xri_used = 0;
James Smart6d368e52011-05-24 11:44:12 -04005628 phba->sli4_hba.xri_ids = kzalloc(count *
5629 sizeof(uint16_t),
5630 GFP_KERNEL);
5631 if (unlikely(!phba->sli4_hba.xri_ids)) {
5632 rc = -ENOMEM;
5633 goto free_xri_bmask;
5634 }
5635
5636 for (i = 0; i < count; i++)
5637 phba->sli4_hba.xri_ids[i] = base + i;
5638
5639 /* VFIs. */
5640 count = phba->sli4_hba.max_cfg_param.max_vfi;
5641 base = phba->sli4_hba.max_cfg_param.vfi_base;
5642 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
5643 phba->sli4_hba.vfi_bmask = kzalloc(longs *
5644 sizeof(unsigned long),
5645 GFP_KERNEL);
5646 if (unlikely(!phba->sli4_hba.vfi_bmask)) {
5647 rc = -ENOMEM;
5648 goto free_xri_ids;
5649 }
5650 phba->sli4_hba.vfi_ids = kzalloc(count *
5651 sizeof(uint16_t),
5652 GFP_KERNEL);
5653 if (unlikely(!phba->sli4_hba.vfi_ids)) {
5654 rc = -ENOMEM;
5655 goto free_vfi_bmask;
5656 }
5657
5658 for (i = 0; i < count; i++)
5659 phba->sli4_hba.vfi_ids[i] = base + i;
5660
5661 /*
5662 * Mark all resources ready. An HBA reset doesn't need
5663 * to reset the initialization.
5664 */
5665 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags,
5666 LPFC_IDX_RSRC_RDY);
5667 return 0;
5668 }
5669
5670 free_vfi_bmask:
5671 kfree(phba->sli4_hba.vfi_bmask);
5672 free_xri_ids:
5673 kfree(phba->sli4_hba.xri_ids);
5674 free_xri_bmask:
5675 kfree(phba->sli4_hba.xri_bmask);
5676 free_vpi_ids:
5677 kfree(phba->vpi_ids);
5678 free_vpi_bmask:
5679 kfree(phba->vpi_bmask);
5680 free_rpi_ids:
5681 kfree(phba->sli4_hba.rpi_ids);
5682 free_rpi_bmask:
5683 kfree(phba->sli4_hba.rpi_bmask);
5684 err_exit:
5685 return rc;
5686}
5687
5688/**
5689 * lpfc_sli4_dealloc_resource_identifiers - Deallocate all SLI4 resource extents.
5690 * @phba: Pointer to HBA context object.
5691 *
5692 * This function allocates the number of elements for the specified
5693 * resource type.
5694 **/
5695int
5696lpfc_sli4_dealloc_resource_identifiers(struct lpfc_hba *phba)
5697{
5698 if (phba->sli4_hba.extents_in_use) {
5699 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VPI);
5700 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_RPI);
5701 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_XRI);
5702 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VFI);
5703 } else {
5704 kfree(phba->vpi_bmask);
5705 kfree(phba->vpi_ids);
5706 bf_set(lpfc_vpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5707 kfree(phba->sli4_hba.xri_bmask);
5708 kfree(phba->sli4_hba.xri_ids);
James Smart6d368e52011-05-24 11:44:12 -04005709 kfree(phba->sli4_hba.vfi_bmask);
5710 kfree(phba->sli4_hba.vfi_ids);
5711 bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5712 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5713 }
5714
5715 return 0;
5716}
5717
5718/**
James Smartb76f2dc2011-07-22 18:37:42 -04005719 * lpfc_sli4_get_allocated_extnts - Get the port's allocated extents.
5720 * @phba: Pointer to HBA context object.
5721 * @type: The resource extent type.
5722 * @extnt_count: buffer to hold port extent count response
5723 * @extnt_size: buffer to hold port extent size response.
5724 *
5725 * This function calls the port to read the host allocated extents
5726 * for a particular type.
5727 **/
5728int
5729lpfc_sli4_get_allocated_extnts(struct lpfc_hba *phba, uint16_t type,
5730 uint16_t *extnt_cnt, uint16_t *extnt_size)
5731{
5732 bool emb;
5733 int rc = 0;
5734 uint16_t curr_blks = 0;
5735 uint32_t req_len, emb_len;
5736 uint32_t alloc_len, mbox_tmo;
5737 struct list_head *blk_list_head;
5738 struct lpfc_rsrc_blks *rsrc_blk;
5739 LPFC_MBOXQ_t *mbox;
5740 void *virtaddr = NULL;
5741 struct lpfc_mbx_nembed_rsrc_extent *n_rsrc;
5742 struct lpfc_mbx_alloc_rsrc_extents *rsrc_ext;
5743 union lpfc_sli4_cfg_shdr *shdr;
5744
5745 switch (type) {
5746 case LPFC_RSC_TYPE_FCOE_VPI:
5747 blk_list_head = &phba->lpfc_vpi_blk_list;
5748 break;
5749 case LPFC_RSC_TYPE_FCOE_XRI:
5750 blk_list_head = &phba->sli4_hba.lpfc_xri_blk_list;
5751 break;
5752 case LPFC_RSC_TYPE_FCOE_VFI:
5753 blk_list_head = &phba->sli4_hba.lpfc_vfi_blk_list;
5754 break;
5755 case LPFC_RSC_TYPE_FCOE_RPI:
5756 blk_list_head = &phba->sli4_hba.lpfc_rpi_blk_list;
5757 break;
5758 default:
5759 return -EIO;
5760 }
5761
5762 /* Count the number of extents currently allocatd for this type. */
5763 list_for_each_entry(rsrc_blk, blk_list_head, list) {
5764 if (curr_blks == 0) {
5765 /*
5766 * The GET_ALLOCATED mailbox does not return the size,
5767 * just the count. The size should be just the size
5768 * stored in the current allocated block and all sizes
5769 * for an extent type are the same so set the return
5770 * value now.
5771 */
5772 *extnt_size = rsrc_blk->rsrc_size;
5773 }
5774 curr_blks++;
5775 }
5776
5777 /* Calculate the total requested length of the dma memory. */
5778 req_len = curr_blks * sizeof(uint16_t);
5779
5780 /*
5781 * Calculate the size of an embedded mailbox. The uint32_t
5782 * accounts for extents-specific word.
5783 */
5784 emb_len = sizeof(MAILBOX_t) - sizeof(struct mbox_header) -
5785 sizeof(uint32_t);
5786
5787 /*
5788 * Presume the allocation and response will fit into an embedded
5789 * mailbox. If not true, reconfigure to a non-embedded mailbox.
5790 */
5791 emb = LPFC_SLI4_MBX_EMBED;
5792 req_len = emb_len;
5793 if (req_len > emb_len) {
5794 req_len = curr_blks * sizeof(uint16_t) +
5795 sizeof(union lpfc_sli4_cfg_shdr) +
5796 sizeof(uint32_t);
5797 emb = LPFC_SLI4_MBX_NEMBED;
5798 }
5799
5800 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5801 if (!mbox)
5802 return -ENOMEM;
5803 memset(mbox, 0, sizeof(LPFC_MBOXQ_t));
5804
5805 alloc_len = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5806 LPFC_MBOX_OPCODE_GET_ALLOC_RSRC_EXTENT,
5807 req_len, emb);
5808 if (alloc_len < req_len) {
5809 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5810 "2983 Allocated DMA memory size (x%x) is "
5811 "less than the requested DMA memory "
5812 "size (x%x)\n", alloc_len, req_len);
5813 rc = -ENOMEM;
5814 goto err_exit;
5815 }
5816 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, curr_blks, type, emb);
5817 if (unlikely(rc)) {
5818 rc = -EIO;
5819 goto err_exit;
5820 }
5821
5822 if (!phba->sli4_hba.intr_enable)
5823 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5824 else {
James Smarta183a152011-10-10 21:32:43 -04005825 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smartb76f2dc2011-07-22 18:37:42 -04005826 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5827 }
5828
5829 if (unlikely(rc)) {
5830 rc = -EIO;
5831 goto err_exit;
5832 }
5833
5834 /*
5835 * Figure out where the response is located. Then get local pointers
5836 * to the response data. The port does not guarantee to respond to
5837 * all extents counts request so update the local variable with the
5838 * allocated count from the port.
5839 */
5840 if (emb == LPFC_SLI4_MBX_EMBED) {
5841 rsrc_ext = &mbox->u.mqe.un.alloc_rsrc_extents;
5842 shdr = &rsrc_ext->header.cfg_shdr;
5843 *extnt_cnt = bf_get(lpfc_mbx_rsrc_cnt, &rsrc_ext->u.rsp);
5844 } else {
5845 virtaddr = mbox->sge_array->addr[0];
5846 n_rsrc = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
5847 shdr = &n_rsrc->cfg_shdr;
5848 *extnt_cnt = bf_get(lpfc_mbx_rsrc_cnt, n_rsrc);
5849 }
5850
5851 if (bf_get(lpfc_mbox_hdr_status, &shdr->response)) {
5852 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
5853 "2984 Failed to read allocated resources "
5854 "for type %d - Status 0x%x Add'l Status 0x%x.\n",
5855 type,
5856 bf_get(lpfc_mbox_hdr_status, &shdr->response),
5857 bf_get(lpfc_mbox_hdr_add_status, &shdr->response));
5858 rc = -EIO;
5859 goto err_exit;
5860 }
5861 err_exit:
5862 lpfc_sli4_mbox_cmd_free(phba, mbox);
5863 return rc;
5864}
5865
5866/**
James Smart8a9d2e82012-05-09 21:16:12 -04005867 * lpfc_sli4_repost_els_sgl_list - Repsot the els buffers sgl pages as block
5868 * @phba: pointer to lpfc hba data structure.
5869 *
5870 * This routine walks the list of els buffers that have been allocated and
5871 * repost them to the port by using SGL block post. This is needed after a
5872 * pci_function_reset/warm_start or start. It attempts to construct blocks
5873 * of els buffer sgls which contains contiguous xris and uses the non-embedded
5874 * SGL block post mailbox commands to post them to the port. For single els
5875 * buffer sgl with non-contiguous xri, if any, it shall use embedded SGL post
5876 * mailbox command for posting.
5877 *
5878 * Returns: 0 = success, non-zero failure.
5879 **/
5880static int
5881lpfc_sli4_repost_els_sgl_list(struct lpfc_hba *phba)
5882{
5883 struct lpfc_sglq *sglq_entry = NULL;
5884 struct lpfc_sglq *sglq_entry_next = NULL;
5885 struct lpfc_sglq *sglq_entry_first = NULL;
5886 int status, post_cnt = 0, num_posted = 0, block_cnt = 0;
5887 int last_xritag = NO_XRI;
5888 LIST_HEAD(prep_sgl_list);
5889 LIST_HEAD(blck_sgl_list);
5890 LIST_HEAD(allc_sgl_list);
5891 LIST_HEAD(post_sgl_list);
5892 LIST_HEAD(free_sgl_list);
5893
5894 spin_lock(&phba->hbalock);
5895 list_splice_init(&phba->sli4_hba.lpfc_sgl_list, &allc_sgl_list);
5896 spin_unlock(&phba->hbalock);
5897
5898 list_for_each_entry_safe(sglq_entry, sglq_entry_next,
5899 &allc_sgl_list, list) {
5900 list_del_init(&sglq_entry->list);
5901 block_cnt++;
5902 if ((last_xritag != NO_XRI) &&
5903 (sglq_entry->sli4_xritag != last_xritag + 1)) {
5904 /* a hole in xri block, form a sgl posting block */
5905 list_splice_init(&prep_sgl_list, &blck_sgl_list);
5906 post_cnt = block_cnt - 1;
5907 /* prepare list for next posting block */
5908 list_add_tail(&sglq_entry->list, &prep_sgl_list);
5909 block_cnt = 1;
5910 } else {
5911 /* prepare list for next posting block */
5912 list_add_tail(&sglq_entry->list, &prep_sgl_list);
5913 /* enough sgls for non-embed sgl mbox command */
5914 if (block_cnt == LPFC_NEMBED_MBOX_SGL_CNT) {
5915 list_splice_init(&prep_sgl_list,
5916 &blck_sgl_list);
5917 post_cnt = block_cnt;
5918 block_cnt = 0;
5919 }
5920 }
5921 num_posted++;
5922
5923 /* keep track of last sgl's xritag */
5924 last_xritag = sglq_entry->sli4_xritag;
5925
5926 /* end of repost sgl list condition for els buffers */
5927 if (num_posted == phba->sli4_hba.els_xri_cnt) {
5928 if (post_cnt == 0) {
5929 list_splice_init(&prep_sgl_list,
5930 &blck_sgl_list);
5931 post_cnt = block_cnt;
5932 } else if (block_cnt == 1) {
5933 status = lpfc_sli4_post_sgl(phba,
5934 sglq_entry->phys, 0,
5935 sglq_entry->sli4_xritag);
5936 if (!status) {
5937 /* successful, put sgl to posted list */
5938 list_add_tail(&sglq_entry->list,
5939 &post_sgl_list);
5940 } else {
5941 /* Failure, put sgl to free list */
5942 lpfc_printf_log(phba, KERN_WARNING,
5943 LOG_SLI,
5944 "3159 Failed to post els "
5945 "sgl, xritag:x%x\n",
5946 sglq_entry->sli4_xritag);
5947 list_add_tail(&sglq_entry->list,
5948 &free_sgl_list);
5949 spin_lock_irq(&phba->hbalock);
5950 phba->sli4_hba.els_xri_cnt--;
5951 spin_unlock_irq(&phba->hbalock);
5952 }
5953 }
5954 }
5955
5956 /* continue until a nembed page worth of sgls */
5957 if (post_cnt == 0)
5958 continue;
5959
5960 /* post the els buffer list sgls as a block */
5961 status = lpfc_sli4_post_els_sgl_list(phba, &blck_sgl_list,
5962 post_cnt);
5963
5964 if (!status) {
5965 /* success, put sgl list to posted sgl list */
5966 list_splice_init(&blck_sgl_list, &post_sgl_list);
5967 } else {
5968 /* Failure, put sgl list to free sgl list */
5969 sglq_entry_first = list_first_entry(&blck_sgl_list,
5970 struct lpfc_sglq,
5971 list);
5972 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
5973 "3160 Failed to post els sgl-list, "
5974 "xritag:x%x-x%x\n",
5975 sglq_entry_first->sli4_xritag,
5976 (sglq_entry_first->sli4_xritag +
5977 post_cnt - 1));
5978 list_splice_init(&blck_sgl_list, &free_sgl_list);
5979 spin_lock_irq(&phba->hbalock);
5980 phba->sli4_hba.els_xri_cnt -= post_cnt;
5981 spin_unlock_irq(&phba->hbalock);
5982 }
5983
5984 /* don't reset xirtag due to hole in xri block */
5985 if (block_cnt == 0)
5986 last_xritag = NO_XRI;
5987
5988 /* reset els sgl post count for next round of posting */
5989 post_cnt = 0;
5990 }
5991
5992 /* free the els sgls failed to post */
5993 lpfc_free_sgl_list(phba, &free_sgl_list);
5994
5995 /* push els sgls posted to the availble list */
5996 if (!list_empty(&post_sgl_list)) {
5997 spin_lock(&phba->hbalock);
5998 list_splice_init(&post_sgl_list,
5999 &phba->sli4_hba.lpfc_sgl_list);
6000 spin_unlock(&phba->hbalock);
6001 } else {
6002 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6003 "3161 Failure to post els sgl to port.\n");
6004 return -EIO;
6005 }
6006 return 0;
6007}
6008
6009/**
James Smartda0436e2009-05-22 14:51:39 -04006010 * lpfc_sli4_hba_setup - SLI4 device intialization PCI function
6011 * @phba: Pointer to HBA context object.
6012 *
6013 * This function is the main SLI4 device intialization PCI function. This
6014 * function is called by the HBA intialization code, HBA reset code and
6015 * HBA error attention handler code. Caller is not required to hold any
6016 * locks.
6017 **/
6018int
6019lpfc_sli4_hba_setup(struct lpfc_hba *phba)
6020{
6021 int rc;
6022 LPFC_MBOXQ_t *mboxq;
6023 struct lpfc_mqe *mqe;
6024 uint8_t *vpd;
6025 uint32_t vpd_size;
6026 uint32_t ftr_rsp = 0;
6027 struct Scsi_Host *shost = lpfc_shost_from_vport(phba->pport);
6028 struct lpfc_vport *vport = phba->pport;
6029 struct lpfc_dmabuf *mp;
6030
6031 /* Perform a PCI function reset to start from clean */
6032 rc = lpfc_pci_function_reset(phba);
6033 if (unlikely(rc))
6034 return -ENODEV;
6035
6036 /* Check the HBA Host Status Register for readyness */
6037 rc = lpfc_sli4_post_status_check(phba);
6038 if (unlikely(rc))
6039 return -ENODEV;
6040 else {
6041 spin_lock_irq(&phba->hbalock);
6042 phba->sli.sli_flag |= LPFC_SLI_ACTIVE;
6043 spin_unlock_irq(&phba->hbalock);
6044 }
6045
6046 /*
6047 * Allocate a single mailbox container for initializing the
6048 * port.
6049 */
6050 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6051 if (!mboxq)
6052 return -ENOMEM;
6053
James Smartda0436e2009-05-22 14:51:39 -04006054 /* Issue READ_REV to collect vpd and FW information. */
James Smart49198b32010-04-06 15:04:33 -04006055 vpd_size = SLI4_PAGE_SIZE;
James Smartda0436e2009-05-22 14:51:39 -04006056 vpd = kzalloc(vpd_size, GFP_KERNEL);
6057 if (!vpd) {
6058 rc = -ENOMEM;
6059 goto out_free_mbox;
6060 }
6061
6062 rc = lpfc_sli4_read_rev(phba, mboxq, vpd, &vpd_size);
James Smart76a95d72010-11-20 23:11:48 -05006063 if (unlikely(rc)) {
6064 kfree(vpd);
6065 goto out_free_mbox;
6066 }
James Smartda0436e2009-05-22 14:51:39 -04006067 mqe = &mboxq->u.mqe;
James Smartf1126682009-06-10 17:22:44 -04006068 phba->sli_rev = bf_get(lpfc_mbx_rd_rev_sli_lvl, &mqe->un.read_rev);
6069 if (bf_get(lpfc_mbx_rd_rev_fcoe, &mqe->un.read_rev))
James Smart76a95d72010-11-20 23:11:48 -05006070 phba->hba_flag |= HBA_FCOE_MODE;
6071 else
6072 phba->hba_flag &= ~HBA_FCOE_MODE;
James Smart45ed1192009-10-02 15:17:02 -04006073
6074 if (bf_get(lpfc_mbx_rd_rev_cee_ver, &mqe->un.read_rev) ==
6075 LPFC_DCBX_CEE_MODE)
6076 phba->hba_flag |= HBA_FIP_SUPPORT;
6077 else
6078 phba->hba_flag &= ~HBA_FIP_SUPPORT;
6079
James Smart4f2e66c2012-05-09 21:17:07 -04006080 phba->hba_flag &= ~HBA_FCP_IOQ_FLUSH;
6081
James Smartc31098c2011-04-16 11:03:33 -04006082 if (phba->sli_rev != LPFC_SLI_REV4) {
James Smartda0436e2009-05-22 14:51:39 -04006083 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6084 "0376 READ_REV Error. SLI Level %d "
6085 "FCoE enabled %d\n",
James Smart76a95d72010-11-20 23:11:48 -05006086 phba->sli_rev, phba->hba_flag & HBA_FCOE_MODE);
James Smartda0436e2009-05-22 14:51:39 -04006087 rc = -EIO;
James Smart76a95d72010-11-20 23:11:48 -05006088 kfree(vpd);
6089 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04006090 }
James Smartcd1c8302011-10-10 21:33:25 -04006091
6092 /*
James Smartff78d8f2011-12-13 13:21:35 -05006093 * Continue initialization with default values even if driver failed
6094 * to read FCoE param config regions, only read parameters if the
6095 * board is FCoE
6096 */
6097 if (phba->hba_flag & HBA_FCOE_MODE &&
6098 lpfc_sli4_read_fcoe_params(phba))
6099 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_INIT,
6100 "2570 Failed to read FCoE parameters\n");
6101
6102 /*
James Smartcd1c8302011-10-10 21:33:25 -04006103 * Retrieve sli4 device physical port name, failure of doing it
6104 * is considered as non-fatal.
6105 */
6106 rc = lpfc_sli4_retrieve_pport_name(phba);
6107 if (!rc)
6108 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
6109 "3080 Successful retrieving SLI4 device "
6110 "physical port name: %s.\n", phba->Port);
6111
James Smartda0436e2009-05-22 14:51:39 -04006112 /*
6113 * Evaluate the read rev and vpd data. Populate the driver
6114 * state with the results. If this routine fails, the failure
6115 * is not fatal as the driver will use generic values.
6116 */
6117 rc = lpfc_parse_vpd(phba, vpd, vpd_size);
6118 if (unlikely(!rc)) {
6119 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6120 "0377 Error %d parsing vpd. "
6121 "Using defaults.\n", rc);
6122 rc = 0;
6123 }
James Smart76a95d72010-11-20 23:11:48 -05006124 kfree(vpd);
James Smartda0436e2009-05-22 14:51:39 -04006125
James Smartf1126682009-06-10 17:22:44 -04006126 /* Save information as VPD data */
6127 phba->vpd.rev.biuRev = mqe->un.read_rev.first_hw_rev;
6128 phba->vpd.rev.smRev = mqe->un.read_rev.second_hw_rev;
6129 phba->vpd.rev.endecRev = mqe->un.read_rev.third_hw_rev;
6130 phba->vpd.rev.fcphHigh = bf_get(lpfc_mbx_rd_rev_fcph_high,
6131 &mqe->un.read_rev);
6132 phba->vpd.rev.fcphLow = bf_get(lpfc_mbx_rd_rev_fcph_low,
6133 &mqe->un.read_rev);
6134 phba->vpd.rev.feaLevelHigh = bf_get(lpfc_mbx_rd_rev_ftr_lvl_high,
6135 &mqe->un.read_rev);
6136 phba->vpd.rev.feaLevelLow = bf_get(lpfc_mbx_rd_rev_ftr_lvl_low,
6137 &mqe->un.read_rev);
6138 phba->vpd.rev.sli1FwRev = mqe->un.read_rev.fw_id_rev;
6139 memcpy(phba->vpd.rev.sli1FwName, mqe->un.read_rev.fw_name, 16);
6140 phba->vpd.rev.sli2FwRev = mqe->un.read_rev.ulp_fw_id_rev;
6141 memcpy(phba->vpd.rev.sli2FwName, mqe->un.read_rev.ulp_fw_name, 16);
6142 phba->vpd.rev.opFwRev = mqe->un.read_rev.fw_id_rev;
6143 memcpy(phba->vpd.rev.opFwName, mqe->un.read_rev.fw_name, 16);
6144 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
6145 "(%d):0380 READ_REV Status x%x "
6146 "fw_rev:%s fcphHi:%x fcphLo:%x flHi:%x flLo:%x\n",
6147 mboxq->vport ? mboxq->vport->vpi : 0,
6148 bf_get(lpfc_mqe_status, mqe),
6149 phba->vpd.rev.opFwName,
6150 phba->vpd.rev.fcphHigh, phba->vpd.rev.fcphLow,
6151 phba->vpd.rev.feaLevelHigh, phba->vpd.rev.feaLevelLow);
James Smartda0436e2009-05-22 14:51:39 -04006152
6153 /*
6154 * Discover the port's supported feature set and match it against the
6155 * hosts requests.
6156 */
6157 lpfc_request_features(phba, mboxq);
6158 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6159 if (unlikely(rc)) {
6160 rc = -EIO;
James Smart76a95d72010-11-20 23:11:48 -05006161 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04006162 }
6163
6164 /*
6165 * The port must support FCP initiator mode as this is the
6166 * only mode running in the host.
6167 */
6168 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_fcpi, &mqe->un.req_ftrs))) {
6169 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
6170 "0378 No support for fcpi mode.\n");
6171 ftr_rsp++;
6172 }
James Smartfedd3b72011-02-16 12:39:24 -05006173 if (bf_get(lpfc_mbx_rq_ftr_rsp_perfh, &mqe->un.req_ftrs))
6174 phba->sli3_options |= LPFC_SLI4_PERFH_ENABLED;
6175 else
6176 phba->sli3_options &= ~LPFC_SLI4_PERFH_ENABLED;
James Smartda0436e2009-05-22 14:51:39 -04006177 /*
6178 * If the port cannot support the host's requested features
6179 * then turn off the global config parameters to disable the
6180 * feature in the driver. This is not a fatal error.
6181 */
James Smartbf086112011-08-21 21:48:13 -04006182 phba->sli3_options &= ~LPFC_SLI3_BG_ENABLED;
6183 if (phba->cfg_enable_bg) {
6184 if (bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs))
6185 phba->sli3_options |= LPFC_SLI3_BG_ENABLED;
6186 else
6187 ftr_rsp++;
6188 }
James Smartda0436e2009-05-22 14:51:39 -04006189
6190 if (phba->max_vpi && phba->cfg_enable_npiv &&
6191 !(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
6192 ftr_rsp++;
6193
6194 if (ftr_rsp) {
6195 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
6196 "0379 Feature Mismatch Data: x%08x %08x "
6197 "x%x x%x x%x\n", mqe->un.req_ftrs.word2,
6198 mqe->un.req_ftrs.word3, phba->cfg_enable_bg,
6199 phba->cfg_enable_npiv, phba->max_vpi);
6200 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs)))
6201 phba->cfg_enable_bg = 0;
6202 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
6203 phba->cfg_enable_npiv = 0;
6204 }
6205
6206 /* These SLI3 features are assumed in SLI4 */
6207 spin_lock_irq(&phba->hbalock);
6208 phba->sli3_options |= (LPFC_SLI3_NPIV_ENABLED | LPFC_SLI3_HBQ_ENABLED);
6209 spin_unlock_irq(&phba->hbalock);
6210
James Smart6d368e52011-05-24 11:44:12 -04006211 /*
6212 * Allocate all resources (xri,rpi,vpi,vfi) now. Subsequent
6213 * calls depends on these resources to complete port setup.
6214 */
6215 rc = lpfc_sli4_alloc_resource_identifiers(phba);
6216 if (rc) {
6217 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6218 "2920 Failed to alloc Resource IDs "
6219 "rc = x%x\n", rc);
6220 goto out_free_mbox;
6221 }
6222
James Smartda0436e2009-05-22 14:51:39 -04006223 /* Read the port's service parameters. */
James Smart9f1177a2010-02-26 14:12:57 -05006224 rc = lpfc_read_sparam(phba, mboxq, vport->vpi);
6225 if (rc) {
6226 phba->link_state = LPFC_HBA_ERROR;
6227 rc = -ENOMEM;
James Smart76a95d72010-11-20 23:11:48 -05006228 goto out_free_mbox;
James Smart9f1177a2010-02-26 14:12:57 -05006229 }
6230
James Smartda0436e2009-05-22 14:51:39 -04006231 mboxq->vport = vport;
6232 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6233 mp = (struct lpfc_dmabuf *) mboxq->context1;
6234 if (rc == MBX_SUCCESS) {
6235 memcpy(&vport->fc_sparam, mp->virt, sizeof(struct serv_parm));
6236 rc = 0;
6237 }
6238
6239 /*
6240 * This memory was allocated by the lpfc_read_sparam routine. Release
6241 * it to the mbuf pool.
6242 */
6243 lpfc_mbuf_free(phba, mp->virt, mp->phys);
6244 kfree(mp);
6245 mboxq->context1 = NULL;
6246 if (unlikely(rc)) {
6247 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6248 "0382 READ_SPARAM command failed "
6249 "status %d, mbxStatus x%x\n",
6250 rc, bf_get(lpfc_mqe_status, mqe));
6251 phba->link_state = LPFC_HBA_ERROR;
6252 rc = -EIO;
James Smart76a95d72010-11-20 23:11:48 -05006253 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04006254 }
6255
James Smart05580562011-05-24 11:40:48 -04006256 lpfc_update_vport_wwn(vport);
James Smartda0436e2009-05-22 14:51:39 -04006257
6258 /* Update the fc_host data structures with new wwn. */
6259 fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
6260 fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
6261
James Smart8a9d2e82012-05-09 21:16:12 -04006262 /* update host els and scsi xri-sgl sizes and mappings */
6263 rc = lpfc_sli4_xri_sgl_update(phba);
6264 if (unlikely(rc)) {
6265 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6266 "1400 Failed to update xri-sgl size and "
6267 "mapping: %d\n", rc);
6268 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04006269 }
6270
James Smart8a9d2e82012-05-09 21:16:12 -04006271 /* register the els sgl pool to the port */
6272 rc = lpfc_sli4_repost_els_sgl_list(phba);
6273 if (unlikely(rc)) {
6274 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6275 "0582 Error %d during els sgl post "
6276 "operation\n", rc);
6277 rc = -ENODEV;
6278 goto out_free_mbox;
6279 }
6280
6281 /* register the allocated scsi sgl pool to the port */
James Smartda0436e2009-05-22 14:51:39 -04006282 rc = lpfc_sli4_repost_scsi_sgl_list(phba);
6283 if (unlikely(rc)) {
James Smart6d368e52011-05-24 11:44:12 -04006284 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smart6a9c52c2009-10-02 15:16:51 -04006285 "0383 Error %d during scsi sgl post "
6286 "operation\n", rc);
James Smartda0436e2009-05-22 14:51:39 -04006287 /* Some Scsi buffers were moved to the abort scsi list */
6288 /* A pci function reset will repost them */
6289 rc = -ENODEV;
James Smart76a95d72010-11-20 23:11:48 -05006290 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04006291 }
6292
6293 /* Post the rpi header region to the device. */
6294 rc = lpfc_sli4_post_all_rpi_hdrs(phba);
6295 if (unlikely(rc)) {
6296 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6297 "0393 Error %d during rpi post operation\n",
6298 rc);
6299 rc = -ENODEV;
James Smart76a95d72010-11-20 23:11:48 -05006300 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04006301 }
James Smart97f2ecf2012-03-01 22:35:23 -05006302 lpfc_sli4_node_prep(phba);
James Smartda0436e2009-05-22 14:51:39 -04006303
James Smart5350d872011-10-10 21:33:49 -04006304 /* Create all the SLI4 queues */
6305 rc = lpfc_sli4_queue_create(phba);
6306 if (rc) {
6307 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6308 "3089 Failed to allocate queues\n");
6309 rc = -ENODEV;
6310 goto out_stop_timers;
6311 }
James Smartda0436e2009-05-22 14:51:39 -04006312 /* Set up all the queues to the device */
6313 rc = lpfc_sli4_queue_setup(phba);
6314 if (unlikely(rc)) {
6315 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6316 "0381 Error %d during queue setup.\n ", rc);
James Smart5350d872011-10-10 21:33:49 -04006317 goto out_destroy_queue;
James Smartda0436e2009-05-22 14:51:39 -04006318 }
6319
6320 /* Arm the CQs and then EQs on device */
6321 lpfc_sli4_arm_cqeq_intr(phba);
6322
6323 /* Indicate device interrupt mode */
6324 phba->sli4_hba.intr_enable = 1;
6325
6326 /* Allow asynchronous mailbox command to go through */
6327 spin_lock_irq(&phba->hbalock);
6328 phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
6329 spin_unlock_irq(&phba->hbalock);
6330
6331 /* Post receive buffers to the device */
6332 lpfc_sli4_rb_setup(phba);
6333
James Smartfc2b9892010-02-26 14:15:29 -05006334 /* Reset HBA FCF states after HBA reset */
6335 phba->fcf.fcf_flag = 0;
6336 phba->fcf.current_rec.flag = 0;
6337
James Smartda0436e2009-05-22 14:51:39 -04006338 /* Start the ELS watchdog timer */
James Smart8fa38512009-07-19 10:01:03 -04006339 mod_timer(&vport->els_tmofunc,
6340 jiffies + HZ * (phba->fc_ratov * 2));
James Smartda0436e2009-05-22 14:51:39 -04006341
6342 /* Start heart beat timer */
6343 mod_timer(&phba->hb_tmofunc,
6344 jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
6345 phba->hb_outstanding = 0;
6346 phba->last_completion_time = jiffies;
6347
6348 /* Start error attention (ERATT) polling timer */
6349 mod_timer(&phba->eratt_poll, jiffies + HZ * LPFC_ERATT_POLL_INTERVAL);
6350
James Smart75baf692010-06-08 18:31:21 -04006351 /* Enable PCIe device Advanced Error Reporting (AER) if configured */
6352 if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
6353 rc = pci_enable_pcie_error_reporting(phba->pcidev);
6354 if (!rc) {
6355 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6356 "2829 This device supports "
6357 "Advanced Error Reporting (AER)\n");
6358 spin_lock_irq(&phba->hbalock);
6359 phba->hba_flag |= HBA_AER_ENABLED;
6360 spin_unlock_irq(&phba->hbalock);
6361 } else {
6362 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6363 "2830 This device does not support "
6364 "Advanced Error Reporting (AER)\n");
6365 phba->cfg_aer_support = 0;
6366 }
James Smart0a96e972011-07-22 18:37:28 -04006367 rc = 0;
James Smart75baf692010-06-08 18:31:21 -04006368 }
6369
James Smart76a95d72010-11-20 23:11:48 -05006370 if (!(phba->hba_flag & HBA_FCOE_MODE)) {
6371 /*
6372 * The FC Port needs to register FCFI (index 0)
6373 */
6374 lpfc_reg_fcfi(phba, mboxq);
6375 mboxq->vport = phba->pport;
6376 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
James Smart9589b0622011-04-16 11:03:17 -04006377 if (rc != MBX_SUCCESS)
James Smart76a95d72010-11-20 23:11:48 -05006378 goto out_unset_queue;
James Smart9589b0622011-04-16 11:03:17 -04006379 rc = 0;
6380 phba->fcf.fcfi = bf_get(lpfc_reg_fcfi_fcfi,
6381 &mboxq->u.mqe.un.reg_fcfi);
James Smart026abb82011-12-13 13:20:45 -05006382
6383 /* Check if the port is configured to be disabled */
6384 lpfc_sli_read_link_ste(phba);
James Smart76a95d72010-11-20 23:11:48 -05006385 }
James Smart026abb82011-12-13 13:20:45 -05006386
James Smartda0436e2009-05-22 14:51:39 -04006387 /*
6388 * The port is ready, set the host's link state to LINK_DOWN
6389 * in preparation for link interrupts.
6390 */
James Smartda0436e2009-05-22 14:51:39 -04006391 spin_lock_irq(&phba->hbalock);
6392 phba->link_state = LPFC_LINK_DOWN;
6393 spin_unlock_irq(&phba->hbalock);
James Smart026abb82011-12-13 13:20:45 -05006394 if (!(phba->hba_flag & HBA_FCOE_MODE) &&
6395 (phba->hba_flag & LINK_DISABLED)) {
6396 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_SLI,
6397 "3103 Adapter Link is disabled.\n");
6398 lpfc_down_link(phba, mboxq);
6399 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6400 if (rc != MBX_SUCCESS) {
6401 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_SLI,
6402 "3104 Adapter failed to issue "
6403 "DOWN_LINK mbox cmd, rc:x%x\n", rc);
6404 goto out_unset_queue;
6405 }
6406 } else if (phba->cfg_suppress_link_up == LPFC_INITIALIZE_LINK) {
James Smart1b511972011-12-13 13:23:09 -05006407 /* don't perform init_link on SLI4 FC port loopback test */
6408 if (!(phba->link_flag & LS_LOOPBACK_MODE)) {
6409 rc = phba->lpfc_hba_init_link(phba, MBX_NOWAIT);
6410 if (rc)
6411 goto out_unset_queue;
6412 }
James Smart5350d872011-10-10 21:33:49 -04006413 }
6414 mempool_free(mboxq, phba->mbox_mem_pool);
6415 return rc;
James Smart76a95d72010-11-20 23:11:48 -05006416out_unset_queue:
James Smartda0436e2009-05-22 14:51:39 -04006417 /* Unset all the queues set up in this routine when error out */
James Smart5350d872011-10-10 21:33:49 -04006418 lpfc_sli4_queue_unset(phba);
6419out_destroy_queue:
6420 lpfc_sli4_queue_destroy(phba);
James Smartda0436e2009-05-22 14:51:39 -04006421out_stop_timers:
James Smart5350d872011-10-10 21:33:49 -04006422 lpfc_stop_hba_timers(phba);
James Smartda0436e2009-05-22 14:51:39 -04006423out_free_mbox:
6424 mempool_free(mboxq, phba->mbox_mem_pool);
6425 return rc;
6426}
James Smarte59058c2008-08-24 21:49:00 -04006427
6428/**
James Smart3621a712009-04-06 18:47:14 -04006429 * lpfc_mbox_timeout - Timeout call back function for mbox timer
James Smarte59058c2008-08-24 21:49:00 -04006430 * @ptr: context object - pointer to hba structure.
dea31012005-04-17 16:05:31 -05006431 *
James Smarte59058c2008-08-24 21:49:00 -04006432 * This is the callback function for mailbox timer. The mailbox
6433 * timer is armed when a new mailbox command is issued and the timer
6434 * is deleted when the mailbox complete. The function is called by
6435 * the kernel timer code when a mailbox does not complete within
6436 * expected time. This function wakes up the worker thread to
6437 * process the mailbox timeout and returns. All the processing is
6438 * done by the worker thread function lpfc_mbox_timeout_handler.
6439 **/
dea31012005-04-17 16:05:31 -05006440void
6441lpfc_mbox_timeout(unsigned long ptr)
6442{
James Smart92d7f7b2007-06-17 19:56:38 -05006443 struct lpfc_hba *phba = (struct lpfc_hba *) ptr;
dea31012005-04-17 16:05:31 -05006444 unsigned long iflag;
James Smart2e0fef82007-06-17 19:56:36 -05006445 uint32_t tmo_posted;
dea31012005-04-17 16:05:31 -05006446
James Smart2e0fef82007-06-17 19:56:36 -05006447 spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
James Smart92d7f7b2007-06-17 19:56:38 -05006448 tmo_posted = phba->pport->work_port_events & WORKER_MBOX_TMO;
James Smart2e0fef82007-06-17 19:56:36 -05006449 if (!tmo_posted)
6450 phba->pport->work_port_events |= WORKER_MBOX_TMO;
6451 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
6452
James Smart5e9d9b82008-06-14 22:52:53 -04006453 if (!tmo_posted)
6454 lpfc_worker_wake_up(phba);
6455 return;
dea31012005-04-17 16:05:31 -05006456}
6457
James Smarte59058c2008-08-24 21:49:00 -04006458
6459/**
James Smart3621a712009-04-06 18:47:14 -04006460 * lpfc_mbox_timeout_handler - Worker thread function to handle mailbox timeout
James Smarte59058c2008-08-24 21:49:00 -04006461 * @phba: Pointer to HBA context object.
6462 *
6463 * This function is called from worker thread when a mailbox command times out.
6464 * The caller is not required to hold any locks. This function will reset the
6465 * HBA and recover all the pending commands.
6466 **/
dea31012005-04-17 16:05:31 -05006467void
6468lpfc_mbox_timeout_handler(struct lpfc_hba *phba)
6469{
James Smart2e0fef82007-06-17 19:56:36 -05006470 LPFC_MBOXQ_t *pmbox = phba->sli.mbox_active;
James Smart04c68492009-05-22 14:52:52 -04006471 MAILBOX_t *mb = &pmbox->u.mb;
James Smart1dcb58e2007-04-25 09:51:30 -04006472 struct lpfc_sli *psli = &phba->sli;
6473 struct lpfc_sli_ring *pring;
dea31012005-04-17 16:05:31 -05006474
James Smarta257bf92009-04-06 18:48:10 -04006475 /* Check the pmbox pointer first. There is a race condition
6476 * between the mbox timeout handler getting executed in the
6477 * worklist and the mailbox actually completing. When this
6478 * race condition occurs, the mbox_active will be NULL.
6479 */
6480 spin_lock_irq(&phba->hbalock);
6481 if (pmbox == NULL) {
6482 lpfc_printf_log(phba, KERN_WARNING,
6483 LOG_MBOX | LOG_SLI,
6484 "0353 Active Mailbox cleared - mailbox timeout "
6485 "exiting\n");
6486 spin_unlock_irq(&phba->hbalock);
6487 return;
6488 }
6489
dea31012005-04-17 16:05:31 -05006490 /* Mbox cmd <mbxCommand> timeout */
James Smarted957682007-06-17 19:56:37 -05006491 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04006492 "0310 Mailbox command x%x timeout Data: x%x x%x x%p\n",
James Smart92d7f7b2007-06-17 19:56:38 -05006493 mb->mbxCommand,
6494 phba->pport->port_state,
6495 phba->sli.sli_flag,
6496 phba->sli.mbox_active);
James Smarta257bf92009-04-06 18:48:10 -04006497 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05006498
James Smart1dcb58e2007-04-25 09:51:30 -04006499 /* Setting state unknown so lpfc_sli_abort_iocb_ring
6500 * would get IOCB_ERROR from lpfc_sli_issue_iocb, allowing
Lucas De Marchi25985ed2011-03-30 22:57:33 -03006501 * it to fail all outstanding SCSI IO.
James Smart1dcb58e2007-04-25 09:51:30 -04006502 */
James Smart2e0fef82007-06-17 19:56:36 -05006503 spin_lock_irq(&phba->pport->work_port_lock);
6504 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
6505 spin_unlock_irq(&phba->pport->work_port_lock);
6506 spin_lock_irq(&phba->hbalock);
6507 phba->link_state = LPFC_LINK_UNKNOWN;
James Smartf4b4c682009-05-22 14:53:12 -04006508 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05006509 spin_unlock_irq(&phba->hbalock);
James Smart1dcb58e2007-04-25 09:51:30 -04006510
6511 pring = &psli->ring[psli->fcp_ring];
6512 lpfc_sli_abort_iocb_ring(phba, pring);
6513
6514 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smart76bb24e2007-10-27 13:38:00 -04006515 "0345 Resetting board due to mailbox timeout\n");
James Smart3772a992009-05-22 14:50:54 -04006516
6517 /* Reset the HBA device */
6518 lpfc_reset_hba(phba);
dea31012005-04-17 16:05:31 -05006519}
6520
James Smarte59058c2008-08-24 21:49:00 -04006521/**
James Smart3772a992009-05-22 14:50:54 -04006522 * lpfc_sli_issue_mbox_s3 - Issue an SLI3 mailbox command to firmware
James Smarte59058c2008-08-24 21:49:00 -04006523 * @phba: Pointer to HBA context object.
6524 * @pmbox: Pointer to mailbox object.
6525 * @flag: Flag indicating how the mailbox need to be processed.
6526 *
6527 * This function is called by discovery code and HBA management code
James Smart3772a992009-05-22 14:50:54 -04006528 * to submit a mailbox command to firmware with SLI-3 interface spec. This
6529 * function gets the hbalock to protect the data structures.
James Smarte59058c2008-08-24 21:49:00 -04006530 * The mailbox command can be submitted in polling mode, in which case
6531 * this function will wait in a polling loop for the completion of the
6532 * mailbox.
6533 * If the mailbox is submitted in no_wait mode (not polling) the
6534 * function will submit the command and returns immediately without waiting
6535 * for the mailbox completion. The no_wait is supported only when HBA
6536 * is in SLI2/SLI3 mode - interrupts are enabled.
6537 * The SLI interface allows only one mailbox pending at a time. If the
6538 * mailbox is issued in polling mode and there is already a mailbox
6539 * pending, then the function will return an error. If the mailbox is issued
6540 * in NO_WAIT mode and there is a mailbox pending already, the function
6541 * will return MBX_BUSY after queuing the mailbox into mailbox queue.
6542 * The sli layer owns the mailbox object until the completion of mailbox
6543 * command if this function return MBX_BUSY or MBX_SUCCESS. For all other
6544 * return codes the caller owns the mailbox command after the return of
6545 * the function.
6546 **/
James Smart3772a992009-05-22 14:50:54 -04006547static int
6548lpfc_sli_issue_mbox_s3(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox,
6549 uint32_t flag)
dea31012005-04-17 16:05:31 -05006550{
dea31012005-04-17 16:05:31 -05006551 MAILBOX_t *mb;
James Smart2e0fef82007-06-17 19:56:36 -05006552 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05006553 uint32_t status, evtctr;
James Smart9940b972011-03-11 16:06:12 -05006554 uint32_t ha_copy, hc_copy;
dea31012005-04-17 16:05:31 -05006555 int i;
James Smart09372822008-01-11 01:52:54 -05006556 unsigned long timeout;
dea31012005-04-17 16:05:31 -05006557 unsigned long drvr_flag = 0;
James Smart34b02dc2008-08-24 21:49:55 -04006558 uint32_t word0, ldata;
dea31012005-04-17 16:05:31 -05006559 void __iomem *to_slim;
James Smart58da1ff2008-04-07 10:15:56 -04006560 int processing_queue = 0;
6561
6562 spin_lock_irqsave(&phba->hbalock, drvr_flag);
6563 if (!pmbox) {
James Smart8568a4d2009-07-19 10:01:16 -04006564 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart58da1ff2008-04-07 10:15:56 -04006565 /* processing mbox queue from intr_handler */
James Smart3772a992009-05-22 14:50:54 -04006566 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
6567 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
6568 return MBX_SUCCESS;
6569 }
James Smart58da1ff2008-04-07 10:15:56 -04006570 processing_queue = 1;
James Smart58da1ff2008-04-07 10:15:56 -04006571 pmbox = lpfc_mbox_get(phba);
6572 if (!pmbox) {
6573 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
6574 return MBX_SUCCESS;
6575 }
6576 }
dea31012005-04-17 16:05:31 -05006577
James Smarted957682007-06-17 19:56:37 -05006578 if (pmbox->mbox_cmpl && pmbox->mbox_cmpl != lpfc_sli_def_mbox_cmpl &&
James Smart92d7f7b2007-06-17 19:56:38 -05006579 pmbox->mbox_cmpl != lpfc_sli_wake_mbox_wait) {
James Smarted957682007-06-17 19:56:37 -05006580 if(!pmbox->vport) {
James Smart58da1ff2008-04-07 10:15:56 -04006581 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
James Smarted957682007-06-17 19:56:37 -05006582 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -05006583 LOG_MBOX | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04006584 "1806 Mbox x%x failed. No vport\n",
James Smart3772a992009-05-22 14:50:54 -04006585 pmbox->u.mb.mbxCommand);
James Smarted957682007-06-17 19:56:37 -05006586 dump_stack();
James Smart58da1ff2008-04-07 10:15:56 -04006587 goto out_not_finished;
James Smarted957682007-06-17 19:56:37 -05006588 }
6589 }
6590
Linas Vepstas8d63f372007-02-14 14:28:36 -06006591 /* If the PCI channel is in offline state, do not post mbox. */
James Smart58da1ff2008-04-07 10:15:56 -04006592 if (unlikely(pci_channel_offline(phba->pcidev))) {
6593 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
6594 goto out_not_finished;
6595 }
Linas Vepstas8d63f372007-02-14 14:28:36 -06006596
James Smarta257bf92009-04-06 18:48:10 -04006597 /* If HBA has a deferred error attention, fail the iocb. */
6598 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
6599 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
6600 goto out_not_finished;
6601 }
6602
dea31012005-04-17 16:05:31 -05006603 psli = &phba->sli;
James Smart92d7f7b2007-06-17 19:56:38 -05006604
James Smart3772a992009-05-22 14:50:54 -04006605 mb = &pmbox->u.mb;
dea31012005-04-17 16:05:31 -05006606 status = MBX_SUCCESS;
6607
James Smart2e0fef82007-06-17 19:56:36 -05006608 if (phba->link_state == LPFC_HBA_ERROR) {
6609 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05006610
6611 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04006612 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6613 "(%d):0311 Mailbox command x%x cannot "
6614 "issue Data: x%x x%x\n",
6615 pmbox->vport ? pmbox->vport->vpi : 0,
6616 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04006617 goto out_not_finished;
Jamie Wellnitz41415862006-02-28 19:25:27 -05006618 }
6619
James Smart9940b972011-03-11 16:06:12 -05006620 if (mb->mbxCommand != MBX_KILL_BOARD && flag & MBX_NOWAIT) {
6621 if (lpfc_readl(phba->HCregaddr, &hc_copy) ||
6622 !(hc_copy & HC_MBINT_ENA)) {
6623 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
6624 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smart3772a992009-05-22 14:50:54 -04006625 "(%d):2528 Mailbox command x%x cannot "
6626 "issue Data: x%x x%x\n",
6627 pmbox->vport ? pmbox->vport->vpi : 0,
6628 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
James Smart9940b972011-03-11 16:06:12 -05006629 goto out_not_finished;
6630 }
James Smart92908312006-03-07 15:04:13 -05006631 }
6632
dea31012005-04-17 16:05:31 -05006633 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
6634 /* Polling for a mbox command when another one is already active
6635 * is not allowed in SLI. Also, the driver must have established
6636 * SLI2 mode to queue and process multiple mbox commands.
6637 */
6638
6639 if (flag & MBX_POLL) {
James Smart2e0fef82007-06-17 19:56:36 -05006640 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05006641
6642 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04006643 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6644 "(%d):2529 Mailbox command x%x "
6645 "cannot issue Data: x%x x%x\n",
6646 pmbox->vport ? pmbox->vport->vpi : 0,
6647 pmbox->u.mb.mbxCommand,
6648 psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04006649 goto out_not_finished;
dea31012005-04-17 16:05:31 -05006650 }
6651
James Smart3772a992009-05-22 14:50:54 -04006652 if (!(psli->sli_flag & LPFC_SLI_ACTIVE)) {
James Smart2e0fef82007-06-17 19:56:36 -05006653 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05006654 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04006655 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6656 "(%d):2530 Mailbox command x%x "
6657 "cannot issue Data: x%x x%x\n",
6658 pmbox->vport ? pmbox->vport->vpi : 0,
6659 pmbox->u.mb.mbxCommand,
6660 psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04006661 goto out_not_finished;
dea31012005-04-17 16:05:31 -05006662 }
6663
dea31012005-04-17 16:05:31 -05006664 /* Another mailbox command is still being processed, queue this
6665 * command to be processed later.
6666 */
6667 lpfc_mbox_put(phba, pmbox);
6668
6669 /* Mbox cmd issue - BUSY */
James Smarted957682007-06-17 19:56:37 -05006670 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04006671 "(%d):0308 Mbox cmd issue - BUSY Data: "
James Smart92d7f7b2007-06-17 19:56:38 -05006672 "x%x x%x x%x x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -05006673 pmbox->vport ? pmbox->vport->vpi : 0xffffff,
6674 mb->mbxCommand, phba->pport->port_state,
6675 psli->sli_flag, flag);
dea31012005-04-17 16:05:31 -05006676
6677 psli->slistat.mbox_busy++;
James Smart2e0fef82007-06-17 19:56:36 -05006678 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05006679
James Smart858c9f62007-06-17 19:56:39 -05006680 if (pmbox->vport) {
6681 lpfc_debugfs_disc_trc(pmbox->vport,
6682 LPFC_DISC_TRC_MBOX_VPORT,
6683 "MBOX Bsy vport: cmd:x%x mb:x%x x%x",
6684 (uint32_t)mb->mbxCommand,
6685 mb->un.varWords[0], mb->un.varWords[1]);
6686 }
6687 else {
6688 lpfc_debugfs_disc_trc(phba->pport,
6689 LPFC_DISC_TRC_MBOX,
6690 "MBOX Bsy: cmd:x%x mb:x%x x%x",
6691 (uint32_t)mb->mbxCommand,
6692 mb->un.varWords[0], mb->un.varWords[1]);
6693 }
6694
James Smart2e0fef82007-06-17 19:56:36 -05006695 return MBX_BUSY;
dea31012005-04-17 16:05:31 -05006696 }
6697
dea31012005-04-17 16:05:31 -05006698 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
6699
6700 /* If we are not polling, we MUST be in SLI2 mode */
6701 if (flag != MBX_POLL) {
James Smart3772a992009-05-22 14:50:54 -04006702 if (!(psli->sli_flag & LPFC_SLI_ACTIVE) &&
Jamie Wellnitz41415862006-02-28 19:25:27 -05006703 (mb->mbxCommand != MBX_KILL_BOARD)) {
dea31012005-04-17 16:05:31 -05006704 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05006705 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05006706 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04006707 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6708 "(%d):2531 Mailbox command x%x "
6709 "cannot issue Data: x%x x%x\n",
6710 pmbox->vport ? pmbox->vport->vpi : 0,
6711 pmbox->u.mb.mbxCommand,
6712 psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04006713 goto out_not_finished;
dea31012005-04-17 16:05:31 -05006714 }
6715 /* timeout active mbox command */
James Smarta309a6b2006-08-01 07:33:43 -04006716 mod_timer(&psli->mbox_tmo, (jiffies +
James Smarta183a152011-10-10 21:32:43 -04006717 (HZ * lpfc_mbox_tmo_val(phba, pmbox))));
dea31012005-04-17 16:05:31 -05006718 }
6719
6720 /* Mailbox cmd <cmd> issue */
James Smarted957682007-06-17 19:56:37 -05006721 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04006722 "(%d):0309 Mailbox cmd x%x issue Data: x%x x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05006723 "x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04006724 pmbox->vport ? pmbox->vport->vpi : 0,
James Smart92d7f7b2007-06-17 19:56:38 -05006725 mb->mbxCommand, phba->pport->port_state,
6726 psli->sli_flag, flag);
dea31012005-04-17 16:05:31 -05006727
James Smart858c9f62007-06-17 19:56:39 -05006728 if (mb->mbxCommand != MBX_HEARTBEAT) {
6729 if (pmbox->vport) {
6730 lpfc_debugfs_disc_trc(pmbox->vport,
6731 LPFC_DISC_TRC_MBOX_VPORT,
6732 "MBOX Send vport: cmd:x%x mb:x%x x%x",
6733 (uint32_t)mb->mbxCommand,
6734 mb->un.varWords[0], mb->un.varWords[1]);
6735 }
6736 else {
6737 lpfc_debugfs_disc_trc(phba->pport,
6738 LPFC_DISC_TRC_MBOX,
6739 "MBOX Send: cmd:x%x mb:x%x x%x",
6740 (uint32_t)mb->mbxCommand,
6741 mb->un.varWords[0], mb->un.varWords[1]);
6742 }
6743 }
6744
dea31012005-04-17 16:05:31 -05006745 psli->slistat.mbox_cmd++;
6746 evtctr = psli->slistat.mbox_event;
6747
6748 /* next set own bit for the adapter and copy over command word */
6749 mb->mbxOwner = OWN_CHIP;
6750
James Smart3772a992009-05-22 14:50:54 -04006751 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
James Smart7a470272010-03-15 11:25:20 -04006752 /* Populate mbox extension offset word. */
6753 if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len) {
6754 *(((uint32_t *)mb) + pmbox->mbox_offset_word)
6755 = (uint8_t *)phba->mbox_ext
6756 - (uint8_t *)phba->mbox;
6757 }
6758
6759 /* Copy the mailbox extension data */
6760 if (pmbox->in_ext_byte_len && pmbox->context2) {
6761 lpfc_sli_pcimem_bcopy(pmbox->context2,
6762 (uint8_t *)phba->mbox_ext,
6763 pmbox->in_ext_byte_len);
6764 }
6765 /* Copy command data to host SLIM area */
James Smart34b02dc2008-08-24 21:49:55 -04006766 lpfc_sli_pcimem_bcopy(mb, phba->mbox, MAILBOX_CMD_SIZE);
dea31012005-04-17 16:05:31 -05006767 } else {
James Smart7a470272010-03-15 11:25:20 -04006768 /* Populate mbox extension offset word. */
6769 if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len)
6770 *(((uint32_t *)mb) + pmbox->mbox_offset_word)
6771 = MAILBOX_HBA_EXT_OFFSET;
6772
6773 /* Copy the mailbox extension data */
6774 if (pmbox->in_ext_byte_len && pmbox->context2) {
6775 lpfc_memcpy_to_slim(phba->MBslimaddr +
6776 MAILBOX_HBA_EXT_OFFSET,
6777 pmbox->context2, pmbox->in_ext_byte_len);
6778
6779 }
James Smart92908312006-03-07 15:04:13 -05006780 if (mb->mbxCommand == MBX_CONFIG_PORT) {
dea31012005-04-17 16:05:31 -05006781 /* copy command data into host mbox for cmpl */
James Smart34b02dc2008-08-24 21:49:55 -04006782 lpfc_sli_pcimem_bcopy(mb, phba->mbox, MAILBOX_CMD_SIZE);
dea31012005-04-17 16:05:31 -05006783 }
6784
6785 /* First copy mbox command data to HBA SLIM, skip past first
6786 word */
6787 to_slim = phba->MBslimaddr + sizeof (uint32_t);
6788 lpfc_memcpy_to_slim(to_slim, &mb->un.varWords[0],
6789 MAILBOX_CMD_SIZE - sizeof (uint32_t));
6790
6791 /* Next copy over first word, with mbxOwner set */
James Smart34b02dc2008-08-24 21:49:55 -04006792 ldata = *((uint32_t *)mb);
dea31012005-04-17 16:05:31 -05006793 to_slim = phba->MBslimaddr;
6794 writel(ldata, to_slim);
6795 readl(to_slim); /* flush */
6796
6797 if (mb->mbxCommand == MBX_CONFIG_PORT) {
6798 /* switch over to host mailbox */
James Smart3772a992009-05-22 14:50:54 -04006799 psli->sli_flag |= LPFC_SLI_ACTIVE;
dea31012005-04-17 16:05:31 -05006800 }
6801 }
6802
6803 wmb();
dea31012005-04-17 16:05:31 -05006804
6805 switch (flag) {
6806 case MBX_NOWAIT:
James Smart09372822008-01-11 01:52:54 -05006807 /* Set up reference to mailbox command */
dea31012005-04-17 16:05:31 -05006808 psli->mbox_active = pmbox;
James Smart09372822008-01-11 01:52:54 -05006809 /* Interrupt board to do it */
6810 writel(CA_MBATT, phba->CAregaddr);
6811 readl(phba->CAregaddr); /* flush */
6812 /* Don't wait for it to finish, just return */
dea31012005-04-17 16:05:31 -05006813 break;
6814
6815 case MBX_POLL:
James Smart09372822008-01-11 01:52:54 -05006816 /* Set up null reference to mailbox command */
dea31012005-04-17 16:05:31 -05006817 psli->mbox_active = NULL;
James Smart09372822008-01-11 01:52:54 -05006818 /* Interrupt board to do it */
6819 writel(CA_MBATT, phba->CAregaddr);
6820 readl(phba->CAregaddr); /* flush */
6821
James Smart3772a992009-05-22 14:50:54 -04006822 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05006823 /* First read mbox status word */
James Smart34b02dc2008-08-24 21:49:55 -04006824 word0 = *((uint32_t *)phba->mbox);
dea31012005-04-17 16:05:31 -05006825 word0 = le32_to_cpu(word0);
6826 } else {
6827 /* First read mbox status word */
James Smart9940b972011-03-11 16:06:12 -05006828 if (lpfc_readl(phba->MBslimaddr, &word0)) {
6829 spin_unlock_irqrestore(&phba->hbalock,
6830 drvr_flag);
6831 goto out_not_finished;
6832 }
dea31012005-04-17 16:05:31 -05006833 }
6834
6835 /* Read the HBA Host Attention Register */
James Smart9940b972011-03-11 16:06:12 -05006836 if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
6837 spin_unlock_irqrestore(&phba->hbalock,
6838 drvr_flag);
6839 goto out_not_finished;
6840 }
James Smarta183a152011-10-10 21:32:43 -04006841 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, pmbox) *
6842 1000) + jiffies;
James Smart09372822008-01-11 01:52:54 -05006843 i = 0;
dea31012005-04-17 16:05:31 -05006844 /* Wait for command to complete */
Jamie Wellnitz41415862006-02-28 19:25:27 -05006845 while (((word0 & OWN_CHIP) == OWN_CHIP) ||
6846 (!(ha_copy & HA_MBATT) &&
James Smart2e0fef82007-06-17 19:56:36 -05006847 (phba->link_state > LPFC_WARM_START))) {
James Smart09372822008-01-11 01:52:54 -05006848 if (time_after(jiffies, timeout)) {
dea31012005-04-17 16:05:31 -05006849 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05006850 spin_unlock_irqrestore(&phba->hbalock,
dea31012005-04-17 16:05:31 -05006851 drvr_flag);
James Smart58da1ff2008-04-07 10:15:56 -04006852 goto out_not_finished;
dea31012005-04-17 16:05:31 -05006853 }
6854
6855 /* Check if we took a mbox interrupt while we were
6856 polling */
6857 if (((word0 & OWN_CHIP) != OWN_CHIP)
6858 && (evtctr != psli->slistat.mbox_event))
6859 break;
6860
James Smart09372822008-01-11 01:52:54 -05006861 if (i++ > 10) {
6862 spin_unlock_irqrestore(&phba->hbalock,
6863 drvr_flag);
6864 msleep(1);
6865 spin_lock_irqsave(&phba->hbalock, drvr_flag);
6866 }
dea31012005-04-17 16:05:31 -05006867
James Smart3772a992009-05-22 14:50:54 -04006868 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05006869 /* First copy command data */
James Smart34b02dc2008-08-24 21:49:55 -04006870 word0 = *((uint32_t *)phba->mbox);
dea31012005-04-17 16:05:31 -05006871 word0 = le32_to_cpu(word0);
6872 if (mb->mbxCommand == MBX_CONFIG_PORT) {
6873 MAILBOX_t *slimmb;
James Smart34b02dc2008-08-24 21:49:55 -04006874 uint32_t slimword0;
dea31012005-04-17 16:05:31 -05006875 /* Check real SLIM for any errors */
6876 slimword0 = readl(phba->MBslimaddr);
6877 slimmb = (MAILBOX_t *) & slimword0;
6878 if (((slimword0 & OWN_CHIP) != OWN_CHIP)
6879 && slimmb->mbxStatus) {
6880 psli->sli_flag &=
James Smart3772a992009-05-22 14:50:54 -04006881 ~LPFC_SLI_ACTIVE;
dea31012005-04-17 16:05:31 -05006882 word0 = slimword0;
6883 }
6884 }
6885 } else {
6886 /* First copy command data */
6887 word0 = readl(phba->MBslimaddr);
6888 }
6889 /* Read the HBA Host Attention Register */
James Smart9940b972011-03-11 16:06:12 -05006890 if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
6891 spin_unlock_irqrestore(&phba->hbalock,
6892 drvr_flag);
6893 goto out_not_finished;
6894 }
dea31012005-04-17 16:05:31 -05006895 }
6896
James Smart3772a992009-05-22 14:50:54 -04006897 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05006898 /* copy results back to user */
James Smart34b02dc2008-08-24 21:49:55 -04006899 lpfc_sli_pcimem_bcopy(phba->mbox, mb, MAILBOX_CMD_SIZE);
James Smart7a470272010-03-15 11:25:20 -04006900 /* Copy the mailbox extension data */
6901 if (pmbox->out_ext_byte_len && pmbox->context2) {
6902 lpfc_sli_pcimem_bcopy(phba->mbox_ext,
6903 pmbox->context2,
6904 pmbox->out_ext_byte_len);
6905 }
dea31012005-04-17 16:05:31 -05006906 } else {
6907 /* First copy command data */
6908 lpfc_memcpy_from_slim(mb, phba->MBslimaddr,
6909 MAILBOX_CMD_SIZE);
James Smart7a470272010-03-15 11:25:20 -04006910 /* Copy the mailbox extension data */
6911 if (pmbox->out_ext_byte_len && pmbox->context2) {
6912 lpfc_memcpy_from_slim(pmbox->context2,
6913 phba->MBslimaddr +
6914 MAILBOX_HBA_EXT_OFFSET,
6915 pmbox->out_ext_byte_len);
dea31012005-04-17 16:05:31 -05006916 }
6917 }
6918
6919 writel(HA_MBATT, phba->HAregaddr);
6920 readl(phba->HAregaddr); /* flush */
6921
6922 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
6923 status = mb->mbxStatus;
6924 }
6925
James Smart2e0fef82007-06-17 19:56:36 -05006926 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
6927 return status;
James Smart58da1ff2008-04-07 10:15:56 -04006928
6929out_not_finished:
6930 if (processing_queue) {
James Smartda0436e2009-05-22 14:51:39 -04006931 pmbox->u.mb.mbxStatus = MBX_NOT_FINISHED;
James Smart58da1ff2008-04-07 10:15:56 -04006932 lpfc_mbox_cmpl_put(phba, pmbox);
6933 }
6934 return MBX_NOT_FINISHED;
dea31012005-04-17 16:05:31 -05006935}
6936
James Smarte59058c2008-08-24 21:49:00 -04006937/**
James Smartf1126682009-06-10 17:22:44 -04006938 * lpfc_sli4_async_mbox_block - Block posting SLI4 asynchronous mailbox command
6939 * @phba: Pointer to HBA context object.
6940 *
6941 * The function blocks the posting of SLI4 asynchronous mailbox commands from
6942 * the driver internal pending mailbox queue. It will then try to wait out the
6943 * possible outstanding mailbox command before return.
6944 *
6945 * Returns:
6946 * 0 - the outstanding mailbox command completed; otherwise, the wait for
6947 * the outstanding mailbox command timed out.
6948 **/
6949static int
6950lpfc_sli4_async_mbox_block(struct lpfc_hba *phba)
6951{
6952 struct lpfc_sli *psli = &phba->sli;
James Smartf1126682009-06-10 17:22:44 -04006953 int rc = 0;
James Smarta183a152011-10-10 21:32:43 -04006954 unsigned long timeout = 0;
James Smartf1126682009-06-10 17:22:44 -04006955
6956 /* Mark the asynchronous mailbox command posting as blocked */
6957 spin_lock_irq(&phba->hbalock);
6958 psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
James Smartf1126682009-06-10 17:22:44 -04006959 /* Determine how long we might wait for the active mailbox
6960 * command to be gracefully completed by firmware.
6961 */
James Smarta183a152011-10-10 21:32:43 -04006962 if (phba->sli.mbox_active)
6963 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
6964 phba->sli.mbox_active) *
6965 1000) + jiffies;
6966 spin_unlock_irq(&phba->hbalock);
6967
James Smartf1126682009-06-10 17:22:44 -04006968 /* Wait for the outstnading mailbox command to complete */
6969 while (phba->sli.mbox_active) {
6970 /* Check active mailbox complete status every 2ms */
6971 msleep(2);
6972 if (time_after(jiffies, timeout)) {
6973 /* Timeout, marked the outstanding cmd not complete */
6974 rc = 1;
6975 break;
6976 }
6977 }
6978
6979 /* Can not cleanly block async mailbox command, fails it */
6980 if (rc) {
6981 spin_lock_irq(&phba->hbalock);
6982 psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
6983 spin_unlock_irq(&phba->hbalock);
6984 }
6985 return rc;
6986}
6987
6988/**
6989 * lpfc_sli4_async_mbox_unblock - Block posting SLI4 async mailbox command
6990 * @phba: Pointer to HBA context object.
6991 *
6992 * The function unblocks and resume posting of SLI4 asynchronous mailbox
6993 * commands from the driver internal pending mailbox queue. It makes sure
6994 * that there is no outstanding mailbox command before resuming posting
6995 * asynchronous mailbox commands. If, for any reason, there is outstanding
6996 * mailbox command, it will try to wait it out before resuming asynchronous
6997 * mailbox command posting.
6998 **/
6999static void
7000lpfc_sli4_async_mbox_unblock(struct lpfc_hba *phba)
7001{
7002 struct lpfc_sli *psli = &phba->sli;
7003
7004 spin_lock_irq(&phba->hbalock);
7005 if (!(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
7006 /* Asynchronous mailbox posting is not blocked, do nothing */
7007 spin_unlock_irq(&phba->hbalock);
7008 return;
7009 }
7010
7011 /* Outstanding synchronous mailbox command is guaranteed to be done,
7012 * successful or timeout, after timing-out the outstanding mailbox
7013 * command shall always be removed, so just unblock posting async
7014 * mailbox command and resume
7015 */
7016 psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
7017 spin_unlock_irq(&phba->hbalock);
7018
7019 /* wake up worker thread to post asynchronlous mailbox command */
7020 lpfc_worker_wake_up(phba);
7021}
7022
7023/**
James Smartda0436e2009-05-22 14:51:39 -04007024 * lpfc_sli4_post_sync_mbox - Post an SLI4 mailbox to the bootstrap mailbox
7025 * @phba: Pointer to HBA context object.
7026 * @mboxq: Pointer to mailbox object.
7027 *
7028 * The function posts a mailbox to the port. The mailbox is expected
7029 * to be comletely filled in and ready for the port to operate on it.
7030 * This routine executes a synchronous completion operation on the
7031 * mailbox by polling for its completion.
7032 *
7033 * The caller must not be holding any locks when calling this routine.
7034 *
7035 * Returns:
7036 * MBX_SUCCESS - mailbox posted successfully
7037 * Any of the MBX error values.
7038 **/
7039static int
7040lpfc_sli4_post_sync_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
7041{
7042 int rc = MBX_SUCCESS;
7043 unsigned long iflag;
7044 uint32_t db_ready;
7045 uint32_t mcqe_status;
7046 uint32_t mbx_cmnd;
7047 unsigned long timeout;
7048 struct lpfc_sli *psli = &phba->sli;
7049 struct lpfc_mqe *mb = &mboxq->u.mqe;
7050 struct lpfc_bmbx_create *mbox_rgn;
7051 struct dma_address *dma_address;
7052 struct lpfc_register bmbx_reg;
7053
7054 /*
7055 * Only one mailbox can be active to the bootstrap mailbox region
7056 * at a time and there is no queueing provided.
7057 */
7058 spin_lock_irqsave(&phba->hbalock, iflag);
7059 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
7060 spin_unlock_irqrestore(&phba->hbalock, iflag);
7061 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04007062 "(%d):2532 Mailbox command x%x (x%x/x%x) "
James Smartda0436e2009-05-22 14:51:39 -04007063 "cannot issue Data: x%x x%x\n",
7064 mboxq->vport ? mboxq->vport->vpi : 0,
7065 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04007066 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7067 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04007068 psli->sli_flag, MBX_POLL);
7069 return MBXERR_ERROR;
7070 }
7071 /* The server grabs the token and owns it until release */
7072 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
7073 phba->sli.mbox_active = mboxq;
7074 spin_unlock_irqrestore(&phba->hbalock, iflag);
7075
7076 /*
7077 * Initialize the bootstrap memory region to avoid stale data areas
7078 * in the mailbox post. Then copy the caller's mailbox contents to
7079 * the bmbx mailbox region.
7080 */
7081 mbx_cmnd = bf_get(lpfc_mqe_command, mb);
7082 memset(phba->sli4_hba.bmbx.avirt, 0, sizeof(struct lpfc_bmbx_create));
7083 lpfc_sli_pcimem_bcopy(mb, phba->sli4_hba.bmbx.avirt,
7084 sizeof(struct lpfc_mqe));
7085
7086 /* Post the high mailbox dma address to the port and wait for ready. */
7087 dma_address = &phba->sli4_hba.bmbx.dma_address;
7088 writel(dma_address->addr_hi, phba->sli4_hba.BMBXregaddr);
7089
James Smarta183a152011-10-10 21:32:43 -04007090 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mboxq)
James Smartda0436e2009-05-22 14:51:39 -04007091 * 1000) + jiffies;
7092 do {
7093 bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
7094 db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
7095 if (!db_ready)
7096 msleep(2);
7097
7098 if (time_after(jiffies, timeout)) {
7099 rc = MBXERR_ERROR;
7100 goto exit;
7101 }
7102 } while (!db_ready);
7103
7104 /* Post the low mailbox dma address to the port. */
7105 writel(dma_address->addr_lo, phba->sli4_hba.BMBXregaddr);
James Smarta183a152011-10-10 21:32:43 -04007106 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mboxq)
James Smartda0436e2009-05-22 14:51:39 -04007107 * 1000) + jiffies;
7108 do {
7109 bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
7110 db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
7111 if (!db_ready)
7112 msleep(2);
7113
7114 if (time_after(jiffies, timeout)) {
7115 rc = MBXERR_ERROR;
7116 goto exit;
7117 }
7118 } while (!db_ready);
7119
7120 /*
7121 * Read the CQ to ensure the mailbox has completed.
7122 * If so, update the mailbox status so that the upper layers
7123 * can complete the request normally.
7124 */
7125 lpfc_sli_pcimem_bcopy(phba->sli4_hba.bmbx.avirt, mb,
7126 sizeof(struct lpfc_mqe));
7127 mbox_rgn = (struct lpfc_bmbx_create *) phba->sli4_hba.bmbx.avirt;
7128 lpfc_sli_pcimem_bcopy(&mbox_rgn->mcqe, &mboxq->mcqe,
7129 sizeof(struct lpfc_mcqe));
7130 mcqe_status = bf_get(lpfc_mcqe_status, &mbox_rgn->mcqe);
James Smart05580562011-05-24 11:40:48 -04007131 /*
7132 * When the CQE status indicates a failure and the mailbox status
7133 * indicates success then copy the CQE status into the mailbox status
7134 * (and prefix it with x4000).
7135 */
James Smartda0436e2009-05-22 14:51:39 -04007136 if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
James Smart05580562011-05-24 11:40:48 -04007137 if (bf_get(lpfc_mqe_status, mb) == MBX_SUCCESS)
7138 bf_set(lpfc_mqe_status, mb,
7139 (LPFC_MBX_ERROR_RANGE | mcqe_status));
James Smartda0436e2009-05-22 14:51:39 -04007140 rc = MBXERR_ERROR;
James Smartd7c47992010-06-08 18:31:54 -04007141 } else
7142 lpfc_sli4_swap_str(phba, mboxq);
James Smartda0436e2009-05-22 14:51:39 -04007143
7144 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04007145 "(%d):0356 Mailbox cmd x%x (x%x/x%x) Status x%x "
James Smartda0436e2009-05-22 14:51:39 -04007146 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x"
7147 " x%x x%x CQ: x%x x%x x%x x%x\n",
James Smarta183a152011-10-10 21:32:43 -04007148 mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
7149 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7150 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04007151 bf_get(lpfc_mqe_status, mb),
7152 mb->un.mb_words[0], mb->un.mb_words[1],
7153 mb->un.mb_words[2], mb->un.mb_words[3],
7154 mb->un.mb_words[4], mb->un.mb_words[5],
7155 mb->un.mb_words[6], mb->un.mb_words[7],
7156 mb->un.mb_words[8], mb->un.mb_words[9],
7157 mb->un.mb_words[10], mb->un.mb_words[11],
7158 mb->un.mb_words[12], mboxq->mcqe.word0,
7159 mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
7160 mboxq->mcqe.trailer);
7161exit:
7162 /* We are holding the token, no needed for lock when release */
7163 spin_lock_irqsave(&phba->hbalock, iflag);
7164 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
7165 phba->sli.mbox_active = NULL;
7166 spin_unlock_irqrestore(&phba->hbalock, iflag);
7167 return rc;
7168}
7169
7170/**
7171 * lpfc_sli_issue_mbox_s4 - Issue an SLI4 mailbox command to firmware
7172 * @phba: Pointer to HBA context object.
7173 * @pmbox: Pointer to mailbox object.
7174 * @flag: Flag indicating how the mailbox need to be processed.
7175 *
7176 * This function is called by discovery code and HBA management code to submit
7177 * a mailbox command to firmware with SLI-4 interface spec.
7178 *
7179 * Return codes the caller owns the mailbox command after the return of the
7180 * function.
7181 **/
7182static int
7183lpfc_sli_issue_mbox_s4(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
7184 uint32_t flag)
7185{
7186 struct lpfc_sli *psli = &phba->sli;
7187 unsigned long iflags;
7188 int rc;
7189
James Smartb76f2dc2011-07-22 18:37:42 -04007190 /* dump from issue mailbox command if setup */
7191 lpfc_idiag_mbxacc_dump_issue_mbox(phba, &mboxq->u.mb);
7192
James Smart8fa38512009-07-19 10:01:03 -04007193 rc = lpfc_mbox_dev_check(phba);
7194 if (unlikely(rc)) {
7195 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04007196 "(%d):2544 Mailbox command x%x (x%x/x%x) "
James Smart8fa38512009-07-19 10:01:03 -04007197 "cannot issue Data: x%x x%x\n",
7198 mboxq->vport ? mboxq->vport->vpi : 0,
7199 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04007200 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7201 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smart8fa38512009-07-19 10:01:03 -04007202 psli->sli_flag, flag);
7203 goto out_not_finished;
7204 }
7205
James Smartda0436e2009-05-22 14:51:39 -04007206 /* Detect polling mode and jump to a handler */
7207 if (!phba->sli4_hba.intr_enable) {
7208 if (flag == MBX_POLL)
7209 rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
7210 else
7211 rc = -EIO;
7212 if (rc != MBX_SUCCESS)
James Smart05580562011-05-24 11:40:48 -04007213 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
James Smartda0436e2009-05-22 14:51:39 -04007214 "(%d):2541 Mailbox command x%x "
James Smartcc459f12012-05-09 21:18:30 -04007215 "(x%x/x%x) failure: "
7216 "mqe_sta: x%x mcqe_sta: x%x/x%x "
7217 "Data: x%x x%x\n,",
James Smartda0436e2009-05-22 14:51:39 -04007218 mboxq->vport ? mboxq->vport->vpi : 0,
7219 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04007220 lpfc_sli_config_mbox_subsys_get(phba,
7221 mboxq),
7222 lpfc_sli_config_mbox_opcode_get(phba,
7223 mboxq),
James Smartcc459f12012-05-09 21:18:30 -04007224 bf_get(lpfc_mqe_status, &mboxq->u.mqe),
7225 bf_get(lpfc_mcqe_status, &mboxq->mcqe),
7226 bf_get(lpfc_mcqe_ext_status,
7227 &mboxq->mcqe),
James Smartda0436e2009-05-22 14:51:39 -04007228 psli->sli_flag, flag);
7229 return rc;
7230 } else if (flag == MBX_POLL) {
James Smartf1126682009-06-10 17:22:44 -04007231 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
7232 "(%d):2542 Try to issue mailbox command "
James Smarta183a152011-10-10 21:32:43 -04007233 "x%x (x%x/x%x) synchronously ahead of async"
James Smartf1126682009-06-10 17:22:44 -04007234 "mailbox command queue: x%x x%x\n",
James Smartda0436e2009-05-22 14:51:39 -04007235 mboxq->vport ? mboxq->vport->vpi : 0,
7236 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04007237 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7238 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04007239 psli->sli_flag, flag);
James Smartf1126682009-06-10 17:22:44 -04007240 /* Try to block the asynchronous mailbox posting */
7241 rc = lpfc_sli4_async_mbox_block(phba);
7242 if (!rc) {
7243 /* Successfully blocked, now issue sync mbox cmd */
7244 rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
7245 if (rc != MBX_SUCCESS)
James Smartcc459f12012-05-09 21:18:30 -04007246 lpfc_printf_log(phba, KERN_WARNING,
James Smarta183a152011-10-10 21:32:43 -04007247 LOG_MBOX | LOG_SLI,
James Smartcc459f12012-05-09 21:18:30 -04007248 "(%d):2597 Sync Mailbox command "
7249 "x%x (x%x/x%x) failure: "
7250 "mqe_sta: x%x mcqe_sta: x%x/x%x "
7251 "Data: x%x x%x\n,",
7252 mboxq->vport ? mboxq->vport->vpi : 0,
James Smarta183a152011-10-10 21:32:43 -04007253 mboxq->u.mb.mbxCommand,
7254 lpfc_sli_config_mbox_subsys_get(phba,
7255 mboxq),
7256 lpfc_sli_config_mbox_opcode_get(phba,
7257 mboxq),
James Smartcc459f12012-05-09 21:18:30 -04007258 bf_get(lpfc_mqe_status, &mboxq->u.mqe),
7259 bf_get(lpfc_mcqe_status, &mboxq->mcqe),
7260 bf_get(lpfc_mcqe_ext_status,
7261 &mboxq->mcqe),
James Smarta183a152011-10-10 21:32:43 -04007262 psli->sli_flag, flag);
James Smartf1126682009-06-10 17:22:44 -04007263 /* Unblock the async mailbox posting afterward */
7264 lpfc_sli4_async_mbox_unblock(phba);
7265 }
7266 return rc;
James Smartda0436e2009-05-22 14:51:39 -04007267 }
7268
7269 /* Now, interrupt mode asynchrous mailbox command */
7270 rc = lpfc_mbox_cmd_check(phba, mboxq);
7271 if (rc) {
7272 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04007273 "(%d):2543 Mailbox command x%x (x%x/x%x) "
James Smartda0436e2009-05-22 14:51:39 -04007274 "cannot issue Data: x%x x%x\n",
7275 mboxq->vport ? mboxq->vport->vpi : 0,
7276 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04007277 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7278 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04007279 psli->sli_flag, flag);
7280 goto out_not_finished;
7281 }
James Smartda0436e2009-05-22 14:51:39 -04007282
7283 /* Put the mailbox command to the driver internal FIFO */
7284 psli->slistat.mbox_busy++;
7285 spin_lock_irqsave(&phba->hbalock, iflags);
7286 lpfc_mbox_put(phba, mboxq);
7287 spin_unlock_irqrestore(&phba->hbalock, iflags);
7288 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
7289 "(%d):0354 Mbox cmd issue - Enqueue Data: "
James Smarta183a152011-10-10 21:32:43 -04007290 "x%x (x%x/x%x) x%x x%x x%x\n",
James Smartda0436e2009-05-22 14:51:39 -04007291 mboxq->vport ? mboxq->vport->vpi : 0xffffff,
7292 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
James Smarta183a152011-10-10 21:32:43 -04007293 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7294 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04007295 phba->pport->port_state,
7296 psli->sli_flag, MBX_NOWAIT);
7297 /* Wake up worker thread to transport mailbox command from head */
7298 lpfc_worker_wake_up(phba);
7299
7300 return MBX_BUSY;
7301
7302out_not_finished:
7303 return MBX_NOT_FINISHED;
7304}
7305
7306/**
7307 * lpfc_sli4_post_async_mbox - Post an SLI4 mailbox command to device
7308 * @phba: Pointer to HBA context object.
7309 *
7310 * This function is called by worker thread to send a mailbox command to
7311 * SLI4 HBA firmware.
7312 *
7313 **/
7314int
7315lpfc_sli4_post_async_mbox(struct lpfc_hba *phba)
7316{
7317 struct lpfc_sli *psli = &phba->sli;
7318 LPFC_MBOXQ_t *mboxq;
7319 int rc = MBX_SUCCESS;
7320 unsigned long iflags;
7321 struct lpfc_mqe *mqe;
7322 uint32_t mbx_cmnd;
7323
7324 /* Check interrupt mode before post async mailbox command */
7325 if (unlikely(!phba->sli4_hba.intr_enable))
7326 return MBX_NOT_FINISHED;
7327
7328 /* Check for mailbox command service token */
7329 spin_lock_irqsave(&phba->hbalock, iflags);
7330 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
7331 spin_unlock_irqrestore(&phba->hbalock, iflags);
7332 return MBX_NOT_FINISHED;
7333 }
7334 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
7335 spin_unlock_irqrestore(&phba->hbalock, iflags);
7336 return MBX_NOT_FINISHED;
7337 }
7338 if (unlikely(phba->sli.mbox_active)) {
7339 spin_unlock_irqrestore(&phba->hbalock, iflags);
7340 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7341 "0384 There is pending active mailbox cmd\n");
7342 return MBX_NOT_FINISHED;
7343 }
7344 /* Take the mailbox command service token */
7345 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
7346
7347 /* Get the next mailbox command from head of queue */
7348 mboxq = lpfc_mbox_get(phba);
7349
7350 /* If no more mailbox command waiting for post, we're done */
7351 if (!mboxq) {
7352 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
7353 spin_unlock_irqrestore(&phba->hbalock, iflags);
7354 return MBX_SUCCESS;
7355 }
7356 phba->sli.mbox_active = mboxq;
7357 spin_unlock_irqrestore(&phba->hbalock, iflags);
7358
7359 /* Check device readiness for posting mailbox command */
7360 rc = lpfc_mbox_dev_check(phba);
7361 if (unlikely(rc))
7362 /* Driver clean routine will clean up pending mailbox */
7363 goto out_not_finished;
7364
7365 /* Prepare the mbox command to be posted */
7366 mqe = &mboxq->u.mqe;
7367 mbx_cmnd = bf_get(lpfc_mqe_command, mqe);
7368
7369 /* Start timer for the mbox_tmo and log some mailbox post messages */
7370 mod_timer(&psli->mbox_tmo, (jiffies +
James Smarta183a152011-10-10 21:32:43 -04007371 (HZ * lpfc_mbox_tmo_val(phba, mboxq))));
James Smartda0436e2009-05-22 14:51:39 -04007372
7373 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04007374 "(%d):0355 Mailbox cmd x%x (x%x/x%x) issue Data: "
James Smartda0436e2009-05-22 14:51:39 -04007375 "x%x x%x\n",
7376 mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
James Smarta183a152011-10-10 21:32:43 -04007377 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7378 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04007379 phba->pport->port_state, psli->sli_flag);
7380
7381 if (mbx_cmnd != MBX_HEARTBEAT) {
7382 if (mboxq->vport) {
7383 lpfc_debugfs_disc_trc(mboxq->vport,
7384 LPFC_DISC_TRC_MBOX_VPORT,
7385 "MBOX Send vport: cmd:x%x mb:x%x x%x",
7386 mbx_cmnd, mqe->un.mb_words[0],
7387 mqe->un.mb_words[1]);
7388 } else {
7389 lpfc_debugfs_disc_trc(phba->pport,
7390 LPFC_DISC_TRC_MBOX,
7391 "MBOX Send: cmd:x%x mb:x%x x%x",
7392 mbx_cmnd, mqe->un.mb_words[0],
7393 mqe->un.mb_words[1]);
7394 }
7395 }
7396 psli->slistat.mbox_cmd++;
7397
7398 /* Post the mailbox command to the port */
7399 rc = lpfc_sli4_mq_put(phba->sli4_hba.mbx_wq, mqe);
7400 if (rc != MBX_SUCCESS) {
7401 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04007402 "(%d):2533 Mailbox command x%x (x%x/x%x) "
James Smartda0436e2009-05-22 14:51:39 -04007403 "cannot issue Data: x%x x%x\n",
7404 mboxq->vport ? mboxq->vport->vpi : 0,
7405 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04007406 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7407 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04007408 psli->sli_flag, MBX_NOWAIT);
7409 goto out_not_finished;
7410 }
7411
7412 return rc;
7413
7414out_not_finished:
7415 spin_lock_irqsave(&phba->hbalock, iflags);
James Smartd7069f02012-03-01 22:36:29 -05007416 if (phba->sli.mbox_active) {
7417 mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
7418 __lpfc_mbox_cmpl_put(phba, mboxq);
7419 /* Release the token */
7420 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
7421 phba->sli.mbox_active = NULL;
7422 }
James Smartda0436e2009-05-22 14:51:39 -04007423 spin_unlock_irqrestore(&phba->hbalock, iflags);
7424
7425 return MBX_NOT_FINISHED;
7426}
7427
7428/**
7429 * lpfc_sli_issue_mbox - Wrapper func for issuing mailbox command
7430 * @phba: Pointer to HBA context object.
7431 * @pmbox: Pointer to mailbox object.
7432 * @flag: Flag indicating how the mailbox need to be processed.
7433 *
7434 * This routine wraps the actual SLI3 or SLI4 mailbox issuing routine from
7435 * the API jump table function pointer from the lpfc_hba struct.
7436 *
7437 * Return codes the caller owns the mailbox command after the return of the
7438 * function.
7439 **/
7440int
7441lpfc_sli_issue_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox, uint32_t flag)
7442{
7443 return phba->lpfc_sli_issue_mbox(phba, pmbox, flag);
7444}
7445
7446/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03007447 * lpfc_mbox_api_table_setup - Set up mbox api function jump table
James Smartda0436e2009-05-22 14:51:39 -04007448 * @phba: The hba struct for which this call is being executed.
7449 * @dev_grp: The HBA PCI-Device group number.
7450 *
7451 * This routine sets up the mbox interface API function jump table in @phba
7452 * struct.
7453 * Returns: 0 - success, -ENODEV - failure.
7454 **/
7455int
7456lpfc_mbox_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
7457{
7458
7459 switch (dev_grp) {
7460 case LPFC_PCI_DEV_LP:
7461 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s3;
7462 phba->lpfc_sli_handle_slow_ring_event =
7463 lpfc_sli_handle_slow_ring_event_s3;
7464 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s3;
7465 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s3;
7466 phba->lpfc_sli_brdready = lpfc_sli_brdready_s3;
7467 break;
7468 case LPFC_PCI_DEV_OC:
7469 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s4;
7470 phba->lpfc_sli_handle_slow_ring_event =
7471 lpfc_sli_handle_slow_ring_event_s4;
7472 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s4;
7473 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s4;
7474 phba->lpfc_sli_brdready = lpfc_sli_brdready_s4;
7475 break;
7476 default:
7477 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7478 "1420 Invalid HBA PCI-device group: 0x%x\n",
7479 dev_grp);
7480 return -ENODEV;
7481 break;
7482 }
7483 return 0;
7484}
7485
7486/**
James Smart3621a712009-04-06 18:47:14 -04007487 * __lpfc_sli_ringtx_put - Add an iocb to the txq
James Smarte59058c2008-08-24 21:49:00 -04007488 * @phba: Pointer to HBA context object.
7489 * @pring: Pointer to driver SLI ring object.
7490 * @piocb: Pointer to address of newly added command iocb.
7491 *
7492 * This function is called with hbalock held to add a command
7493 * iocb to the txq when SLI layer cannot submit the command iocb
7494 * to the ring.
7495 **/
James Smart2a9bf3d2010-06-07 15:24:45 -04007496void
James Smart92d7f7b2007-06-17 19:56:38 -05007497__lpfc_sli_ringtx_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -05007498 struct lpfc_iocbq *piocb)
dea31012005-04-17 16:05:31 -05007499{
7500 /* Insert the caller's iocb in the txq tail for later processing. */
7501 list_add_tail(&piocb->list, &pring->txq);
7502 pring->txq_cnt++;
dea31012005-04-17 16:05:31 -05007503}
7504
James Smarte59058c2008-08-24 21:49:00 -04007505/**
James Smart3621a712009-04-06 18:47:14 -04007506 * lpfc_sli_next_iocb - Get the next iocb in the txq
James Smarte59058c2008-08-24 21:49:00 -04007507 * @phba: Pointer to HBA context object.
7508 * @pring: Pointer to driver SLI ring object.
7509 * @piocb: Pointer to address of newly added command iocb.
7510 *
7511 * This function is called with hbalock held before a new
7512 * iocb is submitted to the firmware. This function checks
7513 * txq to flush the iocbs in txq to Firmware before
7514 * submitting new iocbs to the Firmware.
7515 * If there are iocbs in the txq which need to be submitted
7516 * to firmware, lpfc_sli_next_iocb returns the first element
7517 * of the txq after dequeuing it from txq.
7518 * If there is no iocb in the txq then the function will return
7519 * *piocb and *piocb is set to NULL. Caller needs to check
7520 * *piocb to find if there are more commands in the txq.
7521 **/
dea31012005-04-17 16:05:31 -05007522static struct lpfc_iocbq *
7523lpfc_sli_next_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -05007524 struct lpfc_iocbq **piocb)
dea31012005-04-17 16:05:31 -05007525{
7526 struct lpfc_iocbq * nextiocb;
7527
7528 nextiocb = lpfc_sli_ringtx_get(phba, pring);
7529 if (!nextiocb) {
7530 nextiocb = *piocb;
7531 *piocb = NULL;
7532 }
7533
7534 return nextiocb;
7535}
7536
James Smarte59058c2008-08-24 21:49:00 -04007537/**
James Smart3772a992009-05-22 14:50:54 -04007538 * __lpfc_sli_issue_iocb_s3 - SLI3 device lockless ver of lpfc_sli_issue_iocb
James Smarte59058c2008-08-24 21:49:00 -04007539 * @phba: Pointer to HBA context object.
James Smart3772a992009-05-22 14:50:54 -04007540 * @ring_number: SLI ring number to issue iocb on.
James Smarte59058c2008-08-24 21:49:00 -04007541 * @piocb: Pointer to command iocb.
7542 * @flag: Flag indicating if this command can be put into txq.
7543 *
James Smart3772a992009-05-22 14:50:54 -04007544 * __lpfc_sli_issue_iocb_s3 is used by other functions in the driver to issue
7545 * an iocb command to an HBA with SLI-3 interface spec. If the PCI slot is
7546 * recovering from error state, if HBA is resetting or if LPFC_STOP_IOCB_EVENT
7547 * flag is turned on, the function returns IOCB_ERROR. When the link is down,
7548 * this function allows only iocbs for posting buffers. This function finds
7549 * next available slot in the command ring and posts the command to the
7550 * available slot and writes the port attention register to request HBA start
7551 * processing new iocb. If there is no slot available in the ring and
7552 * flag & SLI_IOCB_RET_IOCB is set, the new iocb is added to the txq, otherwise
7553 * the function returns IOCB_BUSY.
James Smarte59058c2008-08-24 21:49:00 -04007554 *
James Smart3772a992009-05-22 14:50:54 -04007555 * This function is called with hbalock held. The function will return success
7556 * after it successfully submit the iocb to firmware or after adding to the
7557 * txq.
James Smarte59058c2008-08-24 21:49:00 -04007558 **/
James Smart98c9ea52007-10-27 13:37:33 -04007559static int
James Smart3772a992009-05-22 14:50:54 -04007560__lpfc_sli_issue_iocb_s3(struct lpfc_hba *phba, uint32_t ring_number,
dea31012005-04-17 16:05:31 -05007561 struct lpfc_iocbq *piocb, uint32_t flag)
7562{
7563 struct lpfc_iocbq *nextiocb;
7564 IOCB_t *iocb;
James Smart3772a992009-05-22 14:50:54 -04007565 struct lpfc_sli_ring *pring = &phba->sli.ring[ring_number];
dea31012005-04-17 16:05:31 -05007566
James Smart92d7f7b2007-06-17 19:56:38 -05007567 if (piocb->iocb_cmpl && (!piocb->vport) &&
7568 (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
7569 (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
7570 lpfc_printf_log(phba, KERN_ERR,
7571 LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04007572 "1807 IOCB x%x failed. No vport\n",
James Smart92d7f7b2007-06-17 19:56:38 -05007573 piocb->iocb.ulpCommand);
7574 dump_stack();
7575 return IOCB_ERROR;
7576 }
7577
7578
Linas Vepstas8d63f372007-02-14 14:28:36 -06007579 /* If the PCI channel is in offline state, do not post iocbs. */
7580 if (unlikely(pci_channel_offline(phba->pcidev)))
7581 return IOCB_ERROR;
7582
James Smarta257bf92009-04-06 18:48:10 -04007583 /* If HBA has a deferred error attention, fail the iocb. */
7584 if (unlikely(phba->hba_flag & DEFER_ERATT))
7585 return IOCB_ERROR;
7586
dea31012005-04-17 16:05:31 -05007587 /*
7588 * We should never get an IOCB if we are in a < LINK_DOWN state
7589 */
James Smart2e0fef82007-06-17 19:56:36 -05007590 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
dea31012005-04-17 16:05:31 -05007591 return IOCB_ERROR;
7592
7593 /*
7594 * Check to see if we are blocking IOCB processing because of a
James Smart0b727fe2007-10-27 13:37:25 -04007595 * outstanding event.
dea31012005-04-17 16:05:31 -05007596 */
James Smart0b727fe2007-10-27 13:37:25 -04007597 if (unlikely(pring->flag & LPFC_STOP_IOCB_EVENT))
dea31012005-04-17 16:05:31 -05007598 goto iocb_busy;
7599
James Smart2e0fef82007-06-17 19:56:36 -05007600 if (unlikely(phba->link_state == LPFC_LINK_DOWN)) {
dea31012005-04-17 16:05:31 -05007601 /*
James Smart2680eea2007-04-25 09:52:55 -04007602 * Only CREATE_XRI, CLOSE_XRI, and QUE_RING_BUF
dea31012005-04-17 16:05:31 -05007603 * can be issued if the link is not up.
7604 */
7605 switch (piocb->iocb.ulpCommand) {
James Smart84774a42008-08-24 21:50:06 -04007606 case CMD_GEN_REQUEST64_CR:
7607 case CMD_GEN_REQUEST64_CX:
7608 if (!(phba->sli.sli_flag & LPFC_MENLO_MAINT) ||
7609 (piocb->iocb.un.genreq64.w5.hcsw.Rctl !=
James Smart6a9c52c2009-10-02 15:16:51 -04007610 FC_RCTL_DD_UNSOL_CMD) ||
James Smart84774a42008-08-24 21:50:06 -04007611 (piocb->iocb.un.genreq64.w5.hcsw.Type !=
7612 MENLO_TRANSPORT_TYPE))
7613
7614 goto iocb_busy;
7615 break;
dea31012005-04-17 16:05:31 -05007616 case CMD_QUE_RING_BUF_CN:
7617 case CMD_QUE_RING_BUF64_CN:
dea31012005-04-17 16:05:31 -05007618 /*
7619 * For IOCBs, like QUE_RING_BUF, that have no rsp ring
7620 * completion, iocb_cmpl MUST be 0.
7621 */
7622 if (piocb->iocb_cmpl)
7623 piocb->iocb_cmpl = NULL;
7624 /*FALLTHROUGH*/
7625 case CMD_CREATE_XRI_CR:
James Smart2680eea2007-04-25 09:52:55 -04007626 case CMD_CLOSE_XRI_CN:
7627 case CMD_CLOSE_XRI_CX:
dea31012005-04-17 16:05:31 -05007628 break;
7629 default:
7630 goto iocb_busy;
7631 }
7632
7633 /*
7634 * For FCP commands, we must be in a state where we can process link
7635 * attention events.
7636 */
7637 } else if (unlikely(pring->ringno == phba->sli.fcp_ring &&
James Smart92d7f7b2007-06-17 19:56:38 -05007638 !(phba->sli.sli_flag & LPFC_PROCESS_LA))) {
dea31012005-04-17 16:05:31 -05007639 goto iocb_busy;
James Smart92d7f7b2007-06-17 19:56:38 -05007640 }
dea31012005-04-17 16:05:31 -05007641
dea31012005-04-17 16:05:31 -05007642 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
7643 (nextiocb = lpfc_sli_next_iocb(phba, pring, &piocb)))
7644 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
7645
7646 if (iocb)
7647 lpfc_sli_update_ring(phba, pring);
7648 else
7649 lpfc_sli_update_full_ring(phba, pring);
7650
7651 if (!piocb)
7652 return IOCB_SUCCESS;
7653
7654 goto out_busy;
7655
7656 iocb_busy:
7657 pring->stats.iocb_cmd_delay++;
7658
7659 out_busy:
7660
7661 if (!(flag & SLI_IOCB_RET_IOCB)) {
James Smart92d7f7b2007-06-17 19:56:38 -05007662 __lpfc_sli_ringtx_put(phba, pring, piocb);
dea31012005-04-17 16:05:31 -05007663 return IOCB_SUCCESS;
7664 }
7665
7666 return IOCB_BUSY;
7667}
7668
James Smart3772a992009-05-22 14:50:54 -04007669/**
James Smart4f774512009-05-22 14:52:35 -04007670 * lpfc_sli4_bpl2sgl - Convert the bpl/bde to a sgl.
7671 * @phba: Pointer to HBA context object.
7672 * @piocb: Pointer to command iocb.
7673 * @sglq: Pointer to the scatter gather queue object.
7674 *
7675 * This routine converts the bpl or bde that is in the IOCB
7676 * to a sgl list for the sli4 hardware. The physical address
7677 * of the bpl/bde is converted back to a virtual address.
7678 * If the IOCB contains a BPL then the list of BDE's is
7679 * converted to sli4_sge's. If the IOCB contains a single
7680 * BDE then it is converted to a single sli_sge.
7681 * The IOCB is still in cpu endianess so the contents of
7682 * the bpl can be used without byte swapping.
7683 *
7684 * Returns valid XRI = Success, NO_XRI = Failure.
7685**/
7686static uint16_t
7687lpfc_sli4_bpl2sgl(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq,
7688 struct lpfc_sglq *sglq)
7689{
7690 uint16_t xritag = NO_XRI;
7691 struct ulp_bde64 *bpl = NULL;
7692 struct ulp_bde64 bde;
7693 struct sli4_sge *sgl = NULL;
James Smart1b511972011-12-13 13:23:09 -05007694 struct lpfc_dmabuf *dmabuf;
James Smart4f774512009-05-22 14:52:35 -04007695 IOCB_t *icmd;
7696 int numBdes = 0;
7697 int i = 0;
James Smart63e801c2010-11-20 23:14:19 -05007698 uint32_t offset = 0; /* accumulated offset in the sg request list */
7699 int inbound = 0; /* number of sg reply entries inbound from firmware */
James Smart4f774512009-05-22 14:52:35 -04007700
7701 if (!piocbq || !sglq)
7702 return xritag;
7703
7704 sgl = (struct sli4_sge *)sglq->sgl;
7705 icmd = &piocbq->iocb;
James Smart6b5151f2012-01-18 16:24:06 -05007706 if (icmd->ulpCommand == CMD_XMIT_BLS_RSP64_CX)
7707 return sglq->sli4_xritag;
James Smart4f774512009-05-22 14:52:35 -04007708 if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
7709 numBdes = icmd->un.genreq64.bdl.bdeSize /
7710 sizeof(struct ulp_bde64);
7711 /* The addrHigh and addrLow fields within the IOCB
7712 * have not been byteswapped yet so there is no
7713 * need to swap them back.
7714 */
James Smart1b511972011-12-13 13:23:09 -05007715 if (piocbq->context3)
7716 dmabuf = (struct lpfc_dmabuf *)piocbq->context3;
7717 else
7718 return xritag;
James Smart4f774512009-05-22 14:52:35 -04007719
James Smart1b511972011-12-13 13:23:09 -05007720 bpl = (struct ulp_bde64 *)dmabuf->virt;
James Smart4f774512009-05-22 14:52:35 -04007721 if (!bpl)
7722 return xritag;
7723
7724 for (i = 0; i < numBdes; i++) {
7725 /* Should already be byte swapped. */
James Smart28baac72010-02-12 14:42:03 -05007726 sgl->addr_hi = bpl->addrHigh;
7727 sgl->addr_lo = bpl->addrLow;
7728
James Smart05580562011-05-24 11:40:48 -04007729 sgl->word2 = le32_to_cpu(sgl->word2);
James Smart4f774512009-05-22 14:52:35 -04007730 if ((i+1) == numBdes)
7731 bf_set(lpfc_sli4_sge_last, sgl, 1);
7732 else
7733 bf_set(lpfc_sli4_sge_last, sgl, 0);
James Smart28baac72010-02-12 14:42:03 -05007734 /* swap the size field back to the cpu so we
7735 * can assign it to the sgl.
7736 */
7737 bde.tus.w = le32_to_cpu(bpl->tus.w);
7738 sgl->sge_len = cpu_to_le32(bde.tus.f.bdeSize);
James Smart63e801c2010-11-20 23:14:19 -05007739 /* The offsets in the sgl need to be accumulated
7740 * separately for the request and reply lists.
7741 * The request is always first, the reply follows.
7742 */
7743 if (piocbq->iocb.ulpCommand == CMD_GEN_REQUEST64_CR) {
7744 /* add up the reply sg entries */
7745 if (bpl->tus.f.bdeFlags == BUFF_TYPE_BDE_64I)
7746 inbound++;
7747 /* first inbound? reset the offset */
7748 if (inbound == 1)
7749 offset = 0;
7750 bf_set(lpfc_sli4_sge_offset, sgl, offset);
James Smartf9bb2da2011-10-10 21:34:11 -04007751 bf_set(lpfc_sli4_sge_type, sgl,
7752 LPFC_SGE_TYPE_DATA);
James Smart63e801c2010-11-20 23:14:19 -05007753 offset += bde.tus.f.bdeSize;
7754 }
James Smart546fc852011-03-11 16:06:29 -05007755 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart4f774512009-05-22 14:52:35 -04007756 bpl++;
7757 sgl++;
7758 }
7759 } else if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BDE_64) {
7760 /* The addrHigh and addrLow fields of the BDE have not
7761 * been byteswapped yet so they need to be swapped
7762 * before putting them in the sgl.
7763 */
7764 sgl->addr_hi =
7765 cpu_to_le32(icmd->un.genreq64.bdl.addrHigh);
7766 sgl->addr_lo =
7767 cpu_to_le32(icmd->un.genreq64.bdl.addrLow);
James Smart05580562011-05-24 11:40:48 -04007768 sgl->word2 = le32_to_cpu(sgl->word2);
James Smart4f774512009-05-22 14:52:35 -04007769 bf_set(lpfc_sli4_sge_last, sgl, 1);
7770 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart28baac72010-02-12 14:42:03 -05007771 sgl->sge_len =
7772 cpu_to_le32(icmd->un.genreq64.bdl.bdeSize);
James Smart4f774512009-05-22 14:52:35 -04007773 }
7774 return sglq->sli4_xritag;
7775}
7776
7777/**
7778 * lpfc_sli4_scmd_to_wqidx_distr - scsi command to SLI4 WQ index distribution
7779 * @phba: Pointer to HBA context object.
James Smart4f774512009-05-22 14:52:35 -04007780 *
James Smarta93ff372010-10-22 11:06:08 -04007781 * This routine performs a roundrobin SCSI command to SLI4 FCP WQ index
James Smart8fa38512009-07-19 10:01:03 -04007782 * distribution. This is called by __lpfc_sli_issue_iocb_s4() with the hbalock
7783 * held.
James Smart4f774512009-05-22 14:52:35 -04007784 *
7785 * Return: index into SLI4 fast-path FCP queue index.
7786 **/
7787static uint32_t
James Smart8fa38512009-07-19 10:01:03 -04007788lpfc_sli4_scmd_to_wqidx_distr(struct lpfc_hba *phba)
James Smart4f774512009-05-22 14:52:35 -04007789{
James Smart8fa38512009-07-19 10:01:03 -04007790 ++phba->fcp_qidx;
7791 if (phba->fcp_qidx >= phba->cfg_fcp_wq_count)
7792 phba->fcp_qidx = 0;
James Smart4f774512009-05-22 14:52:35 -04007793
James Smart8fa38512009-07-19 10:01:03 -04007794 return phba->fcp_qidx;
James Smart4f774512009-05-22 14:52:35 -04007795}
7796
7797/**
7798 * lpfc_sli_iocb2wqe - Convert the IOCB to a work queue entry.
7799 * @phba: Pointer to HBA context object.
7800 * @piocb: Pointer to command iocb.
7801 * @wqe: Pointer to the work queue entry.
7802 *
7803 * This routine converts the iocb command to its Work Queue Entry
7804 * equivalent. The wqe pointer should not have any fields set when
7805 * this routine is called because it will memcpy over them.
7806 * This routine does not set the CQ_ID or the WQEC bits in the
7807 * wqe.
7808 *
7809 * Returns: 0 = Success, IOCB_ERROR = Failure.
7810 **/
7811static int
7812lpfc_sli4_iocb2wqe(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq,
7813 union lpfc_wqe *wqe)
7814{
James Smart5ffc2662009-11-18 15:39:44 -05007815 uint32_t xmit_len = 0, total_len = 0;
James Smart4f774512009-05-22 14:52:35 -04007816 uint8_t ct = 0;
7817 uint32_t fip;
7818 uint32_t abort_tag;
7819 uint8_t command_type = ELS_COMMAND_NON_FIP;
7820 uint8_t cmnd;
7821 uint16_t xritag;
James Smartdcf2a4e2010-09-29 11:18:53 -04007822 uint16_t abrt_iotag;
7823 struct lpfc_iocbq *abrtiocbq;
James Smart4f774512009-05-22 14:52:35 -04007824 struct ulp_bde64 *bpl = NULL;
James Smartf0d9bcc2010-10-22 11:07:09 -04007825 uint32_t els_id = LPFC_ELS_ID_DEFAULT;
James Smart5ffc2662009-11-18 15:39:44 -05007826 int numBdes, i;
7827 struct ulp_bde64 bde;
James Smartc31098c2011-04-16 11:03:33 -04007828 struct lpfc_nodelist *ndlp;
James Smartff78d8f2011-12-13 13:21:35 -05007829 uint32_t *pcmd;
James Smart1b511972011-12-13 13:23:09 -05007830 uint32_t if_type;
James Smart4f774512009-05-22 14:52:35 -04007831
James Smart45ed1192009-10-02 15:17:02 -04007832 fip = phba->hba_flag & HBA_FIP_SUPPORT;
James Smart4f774512009-05-22 14:52:35 -04007833 /* The fcp commands will set command type */
James Smart0c287582009-06-10 17:22:56 -04007834 if (iocbq->iocb_flag & LPFC_IO_FCP)
James Smart4f774512009-05-22 14:52:35 -04007835 command_type = FCP_COMMAND;
James Smartc8685952009-11-18 15:39:16 -05007836 else if (fip && (iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK))
James Smart0c287582009-06-10 17:22:56 -04007837 command_type = ELS_COMMAND_FIP;
7838 else
7839 command_type = ELS_COMMAND_NON_FIP;
7840
James Smart4f774512009-05-22 14:52:35 -04007841 /* Some of the fields are in the right position already */
7842 memcpy(wqe, &iocbq->iocb, sizeof(union lpfc_wqe));
7843 abort_tag = (uint32_t) iocbq->iotag;
7844 xritag = iocbq->sli4_xritag;
James Smartf0d9bcc2010-10-22 11:07:09 -04007845 wqe->generic.wqe_com.word7 = 0; /* The ct field has moved so reset */
James Smart4f774512009-05-22 14:52:35 -04007846 /* words0-2 bpl convert bde */
7847 if (iocbq->iocb.un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
James Smart5ffc2662009-11-18 15:39:44 -05007848 numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
7849 sizeof(struct ulp_bde64);
James Smart4f774512009-05-22 14:52:35 -04007850 bpl = (struct ulp_bde64 *)
7851 ((struct lpfc_dmabuf *)iocbq->context3)->virt;
7852 if (!bpl)
7853 return IOCB_ERROR;
7854
7855 /* Should already be byte swapped. */
7856 wqe->generic.bde.addrHigh = le32_to_cpu(bpl->addrHigh);
7857 wqe->generic.bde.addrLow = le32_to_cpu(bpl->addrLow);
7858 /* swap the size field back to the cpu so we
7859 * can assign it to the sgl.
7860 */
7861 wqe->generic.bde.tus.w = le32_to_cpu(bpl->tus.w);
James Smart5ffc2662009-11-18 15:39:44 -05007862 xmit_len = wqe->generic.bde.tus.f.bdeSize;
7863 total_len = 0;
7864 for (i = 0; i < numBdes; i++) {
7865 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
7866 total_len += bde.tus.f.bdeSize;
7867 }
James Smart4f774512009-05-22 14:52:35 -04007868 } else
James Smart5ffc2662009-11-18 15:39:44 -05007869 xmit_len = iocbq->iocb.un.fcpi64.bdl.bdeSize;
James Smart4f774512009-05-22 14:52:35 -04007870
7871 iocbq->iocb.ulpIoTag = iocbq->iotag;
7872 cmnd = iocbq->iocb.ulpCommand;
7873
7874 switch (iocbq->iocb.ulpCommand) {
7875 case CMD_ELS_REQUEST64_CR:
James Smart93d1379e2012-05-09 21:19:34 -04007876 if (iocbq->iocb_flag & LPFC_IO_LIBDFC)
7877 ndlp = iocbq->context_un.ndlp;
7878 else
7879 ndlp = (struct lpfc_nodelist *)iocbq->context1;
James Smart4f774512009-05-22 14:52:35 -04007880 if (!iocbq->iocb.ulpLe) {
7881 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
7882 "2007 Only Limited Edition cmd Format"
7883 " supported 0x%x\n",
7884 iocbq->iocb.ulpCommand);
7885 return IOCB_ERROR;
7886 }
James Smartff78d8f2011-12-13 13:21:35 -05007887
James Smart5ffc2662009-11-18 15:39:44 -05007888 wqe->els_req.payload_len = xmit_len;
James Smart4f774512009-05-22 14:52:35 -04007889 /* Els_reguest64 has a TMO */
7890 bf_set(wqe_tmo, &wqe->els_req.wqe_com,
7891 iocbq->iocb.ulpTimeout);
7892 /* Need a VF for word 4 set the vf bit*/
7893 bf_set(els_req64_vf, &wqe->els_req, 0);
7894 /* And a VFID for word 12 */
7895 bf_set(els_req64_vfid, &wqe->els_req, 0);
James Smart4f774512009-05-22 14:52:35 -04007896 ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
James Smartf0d9bcc2010-10-22 11:07:09 -04007897 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
7898 iocbq->iocb.ulpContext);
7899 bf_set(wqe_ct, &wqe->els_req.wqe_com, ct);
7900 bf_set(wqe_pu, &wqe->els_req.wqe_com, 0);
James Smart4f774512009-05-22 14:52:35 -04007901 /* CCP CCPE PV PRI in word10 were set in the memcpy */
James Smartff78d8f2011-12-13 13:21:35 -05007902 if (command_type == ELS_COMMAND_FIP)
James Smartc8685952009-11-18 15:39:16 -05007903 els_id = ((iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK)
7904 >> LPFC_FIP_ELS_ID_SHIFT);
James Smartff78d8f2011-12-13 13:21:35 -05007905 pcmd = (uint32_t *) (((struct lpfc_dmabuf *)
7906 iocbq->context2)->virt);
James Smart1b511972011-12-13 13:23:09 -05007907 if_type = bf_get(lpfc_sli_intf_if_type,
7908 &phba->sli4_hba.sli_intf);
7909 if (if_type == LPFC_SLI_INTF_IF_TYPE_2) {
James Smartff78d8f2011-12-13 13:21:35 -05007910 if (pcmd && (*pcmd == ELS_CMD_FLOGI ||
James Smartcb69f7d2011-12-13 13:21:57 -05007911 *pcmd == ELS_CMD_SCR ||
James Smart6b5151f2012-01-18 16:24:06 -05007912 *pcmd == ELS_CMD_FDISC ||
James Smartbdcd2b92012-03-01 22:33:52 -05007913 *pcmd == ELS_CMD_LOGO ||
James Smartff78d8f2011-12-13 13:21:35 -05007914 *pcmd == ELS_CMD_PLOGI)) {
7915 bf_set(els_req64_sp, &wqe->els_req, 1);
7916 bf_set(els_req64_sid, &wqe->els_req,
7917 iocbq->vport->fc_myDID);
James Smart939723a2012-05-09 21:19:03 -04007918 if ((*pcmd == ELS_CMD_FLOGI) &&
7919 !(phba->fc_topology ==
7920 LPFC_TOPOLOGY_LOOP))
7921 bf_set(els_req64_sid, &wqe->els_req, 0);
James Smartff78d8f2011-12-13 13:21:35 -05007922 bf_set(wqe_ct, &wqe->els_req.wqe_com, 1);
7923 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
James Smarta7dd9c02012-05-09 21:16:50 -04007924 phba->vpi_ids[iocbq->vport->vpi]);
James Smart3ef6d242012-01-18 16:23:48 -05007925 } else if (pcmd && iocbq->context1) {
James Smartff78d8f2011-12-13 13:21:35 -05007926 bf_set(wqe_ct, &wqe->els_req.wqe_com, 0);
7927 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
7928 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
7929 }
James Smartc8685952009-11-18 15:39:16 -05007930 }
James Smart6d368e52011-05-24 11:44:12 -04007931 bf_set(wqe_temp_rpi, &wqe->els_req.wqe_com,
7932 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
James Smartf0d9bcc2010-10-22 11:07:09 -04007933 bf_set(wqe_els_id, &wqe->els_req.wqe_com, els_id);
7934 bf_set(wqe_dbde, &wqe->els_req.wqe_com, 1);
7935 bf_set(wqe_iod, &wqe->els_req.wqe_com, LPFC_WQE_IOD_READ);
7936 bf_set(wqe_qosd, &wqe->els_req.wqe_com, 1);
7937 bf_set(wqe_lenloc, &wqe->els_req.wqe_com, LPFC_WQE_LENLOC_NONE);
7938 bf_set(wqe_ebde_cnt, &wqe->els_req.wqe_com, 0);
James Smart7851fe22011-07-22 18:36:52 -04007939 break;
James Smart5ffc2662009-11-18 15:39:44 -05007940 case CMD_XMIT_SEQUENCE64_CX:
James Smartf0d9bcc2010-10-22 11:07:09 -04007941 bf_set(wqe_ctxt_tag, &wqe->xmit_sequence.wqe_com,
7942 iocbq->iocb.un.ulpWord[3]);
7943 bf_set(wqe_rcvoxid, &wqe->xmit_sequence.wqe_com,
James Smart7851fe22011-07-22 18:36:52 -04007944 iocbq->iocb.unsli3.rcvsli3.ox_id);
James Smart5ffc2662009-11-18 15:39:44 -05007945 /* The entire sequence is transmitted for this IOCB */
7946 xmit_len = total_len;
7947 cmnd = CMD_XMIT_SEQUENCE64_CR;
James Smart1b511972011-12-13 13:23:09 -05007948 if (phba->link_flag & LS_LOOPBACK_MODE)
7949 bf_set(wqe_xo, &wqe->xmit_sequence.wge_ctl, 1);
James Smart4f774512009-05-22 14:52:35 -04007950 case CMD_XMIT_SEQUENCE64_CR:
James Smartf0d9bcc2010-10-22 11:07:09 -04007951 /* word3 iocb=io_tag32 wqe=reserved */
7952 wqe->xmit_sequence.rsvd3 = 0;
James Smart4f774512009-05-22 14:52:35 -04007953 /* word4 relative_offset memcpy */
7954 /* word5 r_ctl/df_ctl memcpy */
James Smartf0d9bcc2010-10-22 11:07:09 -04007955 bf_set(wqe_pu, &wqe->xmit_sequence.wqe_com, 0);
7956 bf_set(wqe_dbde, &wqe->xmit_sequence.wqe_com, 1);
7957 bf_set(wqe_iod, &wqe->xmit_sequence.wqe_com,
7958 LPFC_WQE_IOD_WRITE);
7959 bf_set(wqe_lenloc, &wqe->xmit_sequence.wqe_com,
7960 LPFC_WQE_LENLOC_WORD12);
7961 bf_set(wqe_ebde_cnt, &wqe->xmit_sequence.wqe_com, 0);
James Smart5ffc2662009-11-18 15:39:44 -05007962 wqe->xmit_sequence.xmit_len = xmit_len;
7963 command_type = OTHER_COMMAND;
James Smart7851fe22011-07-22 18:36:52 -04007964 break;
James Smart4f774512009-05-22 14:52:35 -04007965 case CMD_XMIT_BCAST64_CN:
James Smartf0d9bcc2010-10-22 11:07:09 -04007966 /* word3 iocb=iotag32 wqe=seq_payload_len */
7967 wqe->xmit_bcast64.seq_payload_len = xmit_len;
James Smart4f774512009-05-22 14:52:35 -04007968 /* word4 iocb=rsvd wqe=rsvd */
7969 /* word5 iocb=rctl/type/df_ctl wqe=rctl/type/df_ctl memcpy */
7970 /* word6 iocb=ctxt_tag/io_tag wqe=ctxt_tag/xri */
James Smartf0d9bcc2010-10-22 11:07:09 -04007971 bf_set(wqe_ct, &wqe->xmit_bcast64.wqe_com,
James Smart4f774512009-05-22 14:52:35 -04007972 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
James Smartf0d9bcc2010-10-22 11:07:09 -04007973 bf_set(wqe_dbde, &wqe->xmit_bcast64.wqe_com, 1);
7974 bf_set(wqe_iod, &wqe->xmit_bcast64.wqe_com, LPFC_WQE_IOD_WRITE);
7975 bf_set(wqe_lenloc, &wqe->xmit_bcast64.wqe_com,
7976 LPFC_WQE_LENLOC_WORD3);
7977 bf_set(wqe_ebde_cnt, &wqe->xmit_bcast64.wqe_com, 0);
James Smart7851fe22011-07-22 18:36:52 -04007978 break;
James Smart4f774512009-05-22 14:52:35 -04007979 case CMD_FCP_IWRITE64_CR:
7980 command_type = FCP_COMMAND_DATA_OUT;
James Smartf0d9bcc2010-10-22 11:07:09 -04007981 /* word3 iocb=iotag wqe=payload_offset_len */
7982 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
7983 wqe->fcp_iwrite.payload_offset_len =
James Smart5ffc2662009-11-18 15:39:44 -05007984 xmit_len + sizeof(struct fcp_rsp);
James Smartf0d9bcc2010-10-22 11:07:09 -04007985 /* word4 iocb=parameter wqe=total_xfer_length memcpy */
7986 /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
7987 bf_set(wqe_erp, &wqe->fcp_iwrite.wqe_com,
7988 iocbq->iocb.ulpFCP2Rcvy);
7989 bf_set(wqe_lnk, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpXS);
7990 /* Always open the exchange */
7991 bf_set(wqe_xc, &wqe->fcp_iwrite.wqe_com, 0);
James Smartf0d9bcc2010-10-22 11:07:09 -04007992 bf_set(wqe_iod, &wqe->fcp_iwrite.wqe_com, LPFC_WQE_IOD_WRITE);
7993 bf_set(wqe_lenloc, &wqe->fcp_iwrite.wqe_com,
7994 LPFC_WQE_LENLOC_WORD4);
7995 bf_set(wqe_ebde_cnt, &wqe->fcp_iwrite.wqe_com, 0);
7996 bf_set(wqe_pu, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpPU);
James Smartacd68592012-01-18 16:25:09 -05007997 if (iocbq->iocb_flag & LPFC_IO_DIF) {
7998 iocbq->iocb_flag &= ~LPFC_IO_DIF;
7999 bf_set(wqe_dif, &wqe->generic.wqe_com, 1);
8000 }
8001 bf_set(wqe_dbde, &wqe->fcp_iwrite.wqe_com, 1);
James Smart7851fe22011-07-22 18:36:52 -04008002 break;
James Smartf0d9bcc2010-10-22 11:07:09 -04008003 case CMD_FCP_IREAD64_CR:
8004 /* word3 iocb=iotag wqe=payload_offset_len */
8005 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
8006 wqe->fcp_iread.payload_offset_len =
8007 xmit_len + sizeof(struct fcp_rsp);
8008 /* word4 iocb=parameter wqe=total_xfer_length memcpy */
8009 /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
8010 bf_set(wqe_erp, &wqe->fcp_iread.wqe_com,
8011 iocbq->iocb.ulpFCP2Rcvy);
8012 bf_set(wqe_lnk, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpXS);
James Smart4f774512009-05-22 14:52:35 -04008013 /* Always open the exchange */
8014 bf_set(wqe_xc, &wqe->fcp_iread.wqe_com, 0);
James Smartf0d9bcc2010-10-22 11:07:09 -04008015 bf_set(wqe_iod, &wqe->fcp_iread.wqe_com, LPFC_WQE_IOD_READ);
8016 bf_set(wqe_lenloc, &wqe->fcp_iread.wqe_com,
8017 LPFC_WQE_LENLOC_WORD4);
8018 bf_set(wqe_ebde_cnt, &wqe->fcp_iread.wqe_com, 0);
8019 bf_set(wqe_pu, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpPU);
James Smartacd68592012-01-18 16:25:09 -05008020 if (iocbq->iocb_flag & LPFC_IO_DIF) {
8021 iocbq->iocb_flag &= ~LPFC_IO_DIF;
8022 bf_set(wqe_dif, &wqe->generic.wqe_com, 1);
8023 }
8024 bf_set(wqe_dbde, &wqe->fcp_iread.wqe_com, 1);
James Smart7851fe22011-07-22 18:36:52 -04008025 break;
James Smartf1126682009-06-10 17:22:44 -04008026 case CMD_FCP_ICMND64_CR:
James Smartf0d9bcc2010-10-22 11:07:09 -04008027 /* word3 iocb=IO_TAG wqe=reserved */
8028 wqe->fcp_icmd.rsrvd3 = 0;
8029 bf_set(wqe_pu, &wqe->fcp_icmd.wqe_com, 0);
James Smartf1126682009-06-10 17:22:44 -04008030 /* Always open the exchange */
James Smartf0d9bcc2010-10-22 11:07:09 -04008031 bf_set(wqe_xc, &wqe->fcp_icmd.wqe_com, 0);
8032 bf_set(wqe_dbde, &wqe->fcp_icmd.wqe_com, 1);
8033 bf_set(wqe_iod, &wqe->fcp_icmd.wqe_com, LPFC_WQE_IOD_WRITE);
8034 bf_set(wqe_qosd, &wqe->fcp_icmd.wqe_com, 1);
8035 bf_set(wqe_lenloc, &wqe->fcp_icmd.wqe_com,
8036 LPFC_WQE_LENLOC_NONE);
8037 bf_set(wqe_ebde_cnt, &wqe->fcp_icmd.wqe_com, 0);
James Smart7851fe22011-07-22 18:36:52 -04008038 break;
James Smart4f774512009-05-22 14:52:35 -04008039 case CMD_GEN_REQUEST64_CR:
James Smart63e801c2010-11-20 23:14:19 -05008040 /* For this command calculate the xmit length of the
8041 * request bde.
8042 */
8043 xmit_len = 0;
8044 numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
8045 sizeof(struct ulp_bde64);
8046 for (i = 0; i < numBdes; i++) {
James Smart63e801c2010-11-20 23:14:19 -05008047 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
James Smart546fc852011-03-11 16:06:29 -05008048 if (bde.tus.f.bdeFlags != BUFF_TYPE_BDE_64)
8049 break;
James Smart63e801c2010-11-20 23:14:19 -05008050 xmit_len += bde.tus.f.bdeSize;
8051 }
James Smartf0d9bcc2010-10-22 11:07:09 -04008052 /* word3 iocb=IO_TAG wqe=request_payload_len */
8053 wqe->gen_req.request_payload_len = xmit_len;
8054 /* word4 iocb=parameter wqe=relative_offset memcpy */
8055 /* word5 [rctl, type, df_ctl, la] copied in memcpy */
James Smart4f774512009-05-22 14:52:35 -04008056 /* word6 context tag copied in memcpy */
8057 if (iocbq->iocb.ulpCt_h || iocbq->iocb.ulpCt_l) {
8058 ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
8059 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8060 "2015 Invalid CT %x command 0x%x\n",
8061 ct, iocbq->iocb.ulpCommand);
8062 return IOCB_ERROR;
8063 }
James Smartf0d9bcc2010-10-22 11:07:09 -04008064 bf_set(wqe_ct, &wqe->gen_req.wqe_com, 0);
8065 bf_set(wqe_tmo, &wqe->gen_req.wqe_com, iocbq->iocb.ulpTimeout);
8066 bf_set(wqe_pu, &wqe->gen_req.wqe_com, iocbq->iocb.ulpPU);
8067 bf_set(wqe_dbde, &wqe->gen_req.wqe_com, 1);
8068 bf_set(wqe_iod, &wqe->gen_req.wqe_com, LPFC_WQE_IOD_READ);
8069 bf_set(wqe_qosd, &wqe->gen_req.wqe_com, 1);
8070 bf_set(wqe_lenloc, &wqe->gen_req.wqe_com, LPFC_WQE_LENLOC_NONE);
8071 bf_set(wqe_ebde_cnt, &wqe->gen_req.wqe_com, 0);
James Smart4f774512009-05-22 14:52:35 -04008072 command_type = OTHER_COMMAND;
James Smart7851fe22011-07-22 18:36:52 -04008073 break;
James Smart4f774512009-05-22 14:52:35 -04008074 case CMD_XMIT_ELS_RSP64_CX:
James Smartc31098c2011-04-16 11:03:33 -04008075 ndlp = (struct lpfc_nodelist *)iocbq->context1;
James Smart4f774512009-05-22 14:52:35 -04008076 /* words0-2 BDE memcpy */
James Smartf0d9bcc2010-10-22 11:07:09 -04008077 /* word3 iocb=iotag32 wqe=response_payload_len */
8078 wqe->xmit_els_rsp.response_payload_len = xmit_len;
James Smart939723a2012-05-09 21:19:03 -04008079 /* word4 */
8080 wqe->xmit_els_rsp.word4 = 0;
James Smart4f774512009-05-22 14:52:35 -04008081 /* word5 iocb=rsvd wge=did */
8082 bf_set(wqe_els_did, &wqe->xmit_els_rsp.wqe_dest,
James Smart939723a2012-05-09 21:19:03 -04008083 iocbq->iocb.un.xseq64.xmit_els_remoteID);
8084
8085 if_type = bf_get(lpfc_sli_intf_if_type,
8086 &phba->sli4_hba.sli_intf);
8087 if (if_type == LPFC_SLI_INTF_IF_TYPE_2) {
8088 if (iocbq->vport->fc_flag & FC_PT2PT) {
8089 bf_set(els_rsp64_sp, &wqe->xmit_els_rsp, 1);
8090 bf_set(els_rsp64_sid, &wqe->xmit_els_rsp,
8091 iocbq->vport->fc_myDID);
8092 if (iocbq->vport->fc_myDID == Fabric_DID) {
8093 bf_set(wqe_els_did,
8094 &wqe->xmit_els_rsp.wqe_dest, 0);
8095 }
8096 }
8097 }
James Smartf0d9bcc2010-10-22 11:07:09 -04008098 bf_set(wqe_ct, &wqe->xmit_els_rsp.wqe_com,
8099 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
8100 bf_set(wqe_pu, &wqe->xmit_els_rsp.wqe_com, iocbq->iocb.ulpPU);
8101 bf_set(wqe_rcvoxid, &wqe->xmit_els_rsp.wqe_com,
James Smart7851fe22011-07-22 18:36:52 -04008102 iocbq->iocb.unsli3.rcvsli3.ox_id);
James Smart4f774512009-05-22 14:52:35 -04008103 if (!iocbq->iocb.ulpCt_h && iocbq->iocb.ulpCt_l)
James Smartf0d9bcc2010-10-22 11:07:09 -04008104 bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
James Smart6d368e52011-05-24 11:44:12 -04008105 phba->vpi_ids[iocbq->vport->vpi]);
James Smartf0d9bcc2010-10-22 11:07:09 -04008106 bf_set(wqe_dbde, &wqe->xmit_els_rsp.wqe_com, 1);
8107 bf_set(wqe_iod, &wqe->xmit_els_rsp.wqe_com, LPFC_WQE_IOD_WRITE);
8108 bf_set(wqe_qosd, &wqe->xmit_els_rsp.wqe_com, 1);
8109 bf_set(wqe_lenloc, &wqe->xmit_els_rsp.wqe_com,
8110 LPFC_WQE_LENLOC_WORD3);
8111 bf_set(wqe_ebde_cnt, &wqe->xmit_els_rsp.wqe_com, 0);
James Smart6d368e52011-05-24 11:44:12 -04008112 bf_set(wqe_rsp_temp_rpi, &wqe->xmit_els_rsp,
8113 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
James Smartff78d8f2011-12-13 13:21:35 -05008114 pcmd = (uint32_t *) (((struct lpfc_dmabuf *)
8115 iocbq->context2)->virt);
8116 if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
James Smart939723a2012-05-09 21:19:03 -04008117 bf_set(els_rsp64_sp, &wqe->xmit_els_rsp, 1);
8118 bf_set(els_rsp64_sid, &wqe->xmit_els_rsp,
James Smartff78d8f2011-12-13 13:21:35 -05008119 iocbq->vport->fc_myDID);
James Smart939723a2012-05-09 21:19:03 -04008120 bf_set(wqe_ct, &wqe->xmit_els_rsp.wqe_com, 1);
8121 bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
James Smartff78d8f2011-12-13 13:21:35 -05008122 phba->vpi_ids[phba->pport->vpi]);
8123 }
James Smart4f774512009-05-22 14:52:35 -04008124 command_type = OTHER_COMMAND;
James Smart7851fe22011-07-22 18:36:52 -04008125 break;
James Smart4f774512009-05-22 14:52:35 -04008126 case CMD_CLOSE_XRI_CN:
8127 case CMD_ABORT_XRI_CN:
8128 case CMD_ABORT_XRI_CX:
8129 /* words 0-2 memcpy should be 0 rserved */
8130 /* port will send abts */
James Smartdcf2a4e2010-09-29 11:18:53 -04008131 abrt_iotag = iocbq->iocb.un.acxri.abortContextTag;
8132 if (abrt_iotag != 0 && abrt_iotag <= phba->sli.last_iotag) {
8133 abrtiocbq = phba->sli.iocbq_lookup[abrt_iotag];
8134 fip = abrtiocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK;
8135 } else
8136 fip = 0;
8137
8138 if ((iocbq->iocb.ulpCommand == CMD_CLOSE_XRI_CN) || fip)
James Smart4f774512009-05-22 14:52:35 -04008139 /*
James Smartdcf2a4e2010-09-29 11:18:53 -04008140 * The link is down, or the command was ELS_FIP
8141 * so the fw does not need to send abts
James Smart4f774512009-05-22 14:52:35 -04008142 * on the wire.
8143 */
8144 bf_set(abort_cmd_ia, &wqe->abort_cmd, 1);
8145 else
8146 bf_set(abort_cmd_ia, &wqe->abort_cmd, 0);
8147 bf_set(abort_cmd_criteria, &wqe->abort_cmd, T_XRI_TAG);
James Smartf0d9bcc2010-10-22 11:07:09 -04008148 /* word5 iocb=CONTEXT_TAG|IO_TAG wqe=reserved */
8149 wqe->abort_cmd.rsrvd5 = 0;
8150 bf_set(wqe_ct, &wqe->abort_cmd.wqe_com,
James Smart4f774512009-05-22 14:52:35 -04008151 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
8152 abort_tag = iocbq->iocb.un.acxri.abortIoTag;
James Smart4f774512009-05-22 14:52:35 -04008153 /*
8154 * The abort handler will send us CMD_ABORT_XRI_CN or
8155 * CMD_CLOSE_XRI_CN and the fw only accepts CMD_ABORT_XRI_CX
8156 */
James Smartf0d9bcc2010-10-22 11:07:09 -04008157 bf_set(wqe_cmnd, &wqe->abort_cmd.wqe_com, CMD_ABORT_XRI_CX);
8158 bf_set(wqe_qosd, &wqe->abort_cmd.wqe_com, 1);
8159 bf_set(wqe_lenloc, &wqe->abort_cmd.wqe_com,
8160 LPFC_WQE_LENLOC_NONE);
James Smart4f774512009-05-22 14:52:35 -04008161 cmnd = CMD_ABORT_XRI_CX;
8162 command_type = OTHER_COMMAND;
8163 xritag = 0;
James Smart7851fe22011-07-22 18:36:52 -04008164 break;
James Smart6669f9b2009-10-02 15:16:45 -04008165 case CMD_XMIT_BLS_RSP64_CX:
James Smart6b5151f2012-01-18 16:24:06 -05008166 ndlp = (struct lpfc_nodelist *)iocbq->context1;
James Smart546fc852011-03-11 16:06:29 -05008167 /* As BLS ABTS RSP WQE is very different from other WQEs,
James Smart6669f9b2009-10-02 15:16:45 -04008168 * we re-construct this WQE here based on information in
8169 * iocbq from scratch.
8170 */
8171 memset(wqe, 0, sizeof(union lpfc_wqe));
James Smart5ffc2662009-11-18 15:39:44 -05008172 /* OX_ID is invariable to who sent ABTS to CT exchange */
James Smart6669f9b2009-10-02 15:16:45 -04008173 bf_set(xmit_bls_rsp64_oxid, &wqe->xmit_bls_rsp,
James Smart546fc852011-03-11 16:06:29 -05008174 bf_get(lpfc_abts_oxid, &iocbq->iocb.un.bls_rsp));
8175 if (bf_get(lpfc_abts_orig, &iocbq->iocb.un.bls_rsp) ==
James Smart5ffc2662009-11-18 15:39:44 -05008176 LPFC_ABTS_UNSOL_INT) {
8177 /* ABTS sent by initiator to CT exchange, the
8178 * RX_ID field will be filled with the newly
8179 * allocated responder XRI.
8180 */
8181 bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
8182 iocbq->sli4_xritag);
8183 } else {
8184 /* ABTS sent by responder to CT exchange, the
8185 * RX_ID field will be filled with the responder
8186 * RX_ID from ABTS.
8187 */
8188 bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
James Smart546fc852011-03-11 16:06:29 -05008189 bf_get(lpfc_abts_rxid, &iocbq->iocb.un.bls_rsp));
James Smart5ffc2662009-11-18 15:39:44 -05008190 }
James Smart6669f9b2009-10-02 15:16:45 -04008191 bf_set(xmit_bls_rsp64_seqcnthi, &wqe->xmit_bls_rsp, 0xffff);
8192 bf_set(wqe_xmit_bls_pt, &wqe->xmit_bls_rsp.wqe_dest, 0x1);
James Smart6b5151f2012-01-18 16:24:06 -05008193
8194 /* Use CT=VPI */
8195 bf_set(wqe_els_did, &wqe->xmit_bls_rsp.wqe_dest,
8196 ndlp->nlp_DID);
8197 bf_set(xmit_bls_rsp64_temprpi, &wqe->xmit_bls_rsp,
8198 iocbq->iocb.ulpContext);
8199 bf_set(wqe_ct, &wqe->xmit_bls_rsp.wqe_com, 1);
James Smart6669f9b2009-10-02 15:16:45 -04008200 bf_set(wqe_ctxt_tag, &wqe->xmit_bls_rsp.wqe_com,
James Smart6b5151f2012-01-18 16:24:06 -05008201 phba->vpi_ids[phba->pport->vpi]);
James Smartf0d9bcc2010-10-22 11:07:09 -04008202 bf_set(wqe_qosd, &wqe->xmit_bls_rsp.wqe_com, 1);
8203 bf_set(wqe_lenloc, &wqe->xmit_bls_rsp.wqe_com,
8204 LPFC_WQE_LENLOC_NONE);
James Smart6669f9b2009-10-02 15:16:45 -04008205 /* Overwrite the pre-set comnd type with OTHER_COMMAND */
8206 command_type = OTHER_COMMAND;
James Smart546fc852011-03-11 16:06:29 -05008207 if (iocbq->iocb.un.xseq64.w5.hcsw.Rctl == FC_RCTL_BA_RJT) {
8208 bf_set(xmit_bls_rsp64_rjt_vspec, &wqe->xmit_bls_rsp,
8209 bf_get(lpfc_vndr_code, &iocbq->iocb.un.bls_rsp));
8210 bf_set(xmit_bls_rsp64_rjt_expc, &wqe->xmit_bls_rsp,
8211 bf_get(lpfc_rsn_expln, &iocbq->iocb.un.bls_rsp));
8212 bf_set(xmit_bls_rsp64_rjt_rsnc, &wqe->xmit_bls_rsp,
8213 bf_get(lpfc_rsn_code, &iocbq->iocb.un.bls_rsp));
8214 }
8215
James Smart7851fe22011-07-22 18:36:52 -04008216 break;
James Smart4f774512009-05-22 14:52:35 -04008217 case CMD_XRI_ABORTED_CX:
8218 case CMD_CREATE_XRI_CR: /* Do we expect to use this? */
James Smart4f774512009-05-22 14:52:35 -04008219 case CMD_IOCB_FCP_IBIDIR64_CR: /* bidirectional xfer */
8220 case CMD_FCP_TSEND64_CX: /* Target mode send xfer-ready */
8221 case CMD_FCP_TRSP64_CX: /* Target mode rcv */
8222 case CMD_FCP_AUTO_TRSP_CX: /* Auto target rsp */
8223 default:
8224 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8225 "2014 Invalid command 0x%x\n",
8226 iocbq->iocb.ulpCommand);
8227 return IOCB_ERROR;
James Smart7851fe22011-07-22 18:36:52 -04008228 break;
James Smart4f774512009-05-22 14:52:35 -04008229 }
James Smart6d368e52011-05-24 11:44:12 -04008230
James Smartf0d9bcc2010-10-22 11:07:09 -04008231 bf_set(wqe_xri_tag, &wqe->generic.wqe_com, xritag);
8232 bf_set(wqe_reqtag, &wqe->generic.wqe_com, iocbq->iotag);
8233 wqe->generic.wqe_com.abort_tag = abort_tag;
8234 bf_set(wqe_cmd_type, &wqe->generic.wqe_com, command_type);
8235 bf_set(wqe_cmnd, &wqe->generic.wqe_com, cmnd);
8236 bf_set(wqe_class, &wqe->generic.wqe_com, iocbq->iocb.ulpClass);
8237 bf_set(wqe_cqid, &wqe->generic.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
James Smart4f774512009-05-22 14:52:35 -04008238 return 0;
8239}
8240
8241/**
8242 * __lpfc_sli_issue_iocb_s4 - SLI4 device lockless ver of lpfc_sli_issue_iocb
8243 * @phba: Pointer to HBA context object.
8244 * @ring_number: SLI ring number to issue iocb on.
8245 * @piocb: Pointer to command iocb.
8246 * @flag: Flag indicating if this command can be put into txq.
8247 *
8248 * __lpfc_sli_issue_iocb_s4 is used by other functions in the driver to issue
8249 * an iocb command to an HBA with SLI-4 interface spec.
8250 *
8251 * This function is called with hbalock held. The function will return success
8252 * after it successfully submit the iocb to firmware or after adding to the
8253 * txq.
8254 **/
8255static int
8256__lpfc_sli_issue_iocb_s4(struct lpfc_hba *phba, uint32_t ring_number,
8257 struct lpfc_iocbq *piocb, uint32_t flag)
8258{
8259 struct lpfc_sglq *sglq;
James Smart4f774512009-05-22 14:52:35 -04008260 union lpfc_wqe wqe;
8261 struct lpfc_sli_ring *pring = &phba->sli.ring[ring_number];
James Smart4f774512009-05-22 14:52:35 -04008262
8263 if (piocb->sli4_xritag == NO_XRI) {
8264 if (piocb->iocb.ulpCommand == CMD_ABORT_XRI_CN ||
James Smart6b5151f2012-01-18 16:24:06 -05008265 piocb->iocb.ulpCommand == CMD_CLOSE_XRI_CN)
James Smart4f774512009-05-22 14:52:35 -04008266 sglq = NULL;
8267 else {
James Smart2a9bf3d2010-06-07 15:24:45 -04008268 if (pring->txq_cnt) {
8269 if (!(flag & SLI_IOCB_RET_IOCB)) {
8270 __lpfc_sli_ringtx_put(phba,
8271 pring, piocb);
8272 return IOCB_SUCCESS;
8273 } else {
8274 return IOCB_BUSY;
8275 }
8276 } else {
James Smart6d368e52011-05-24 11:44:12 -04008277 sglq = __lpfc_sli_get_sglq(phba, piocb);
James Smart2a9bf3d2010-06-07 15:24:45 -04008278 if (!sglq) {
8279 if (!(flag & SLI_IOCB_RET_IOCB)) {
8280 __lpfc_sli_ringtx_put(phba,
8281 pring,
8282 piocb);
8283 return IOCB_SUCCESS;
8284 } else
8285 return IOCB_BUSY;
8286 }
8287 }
James Smart4f774512009-05-22 14:52:35 -04008288 }
8289 } else if (piocb->iocb_flag & LPFC_IO_FCP) {
James Smart6d368e52011-05-24 11:44:12 -04008290 /* These IO's already have an XRI and a mapped sgl. */
8291 sglq = NULL;
James Smart4f774512009-05-22 14:52:35 -04008292 } else {
James Smart6d368e52011-05-24 11:44:12 -04008293 /*
8294 * This is a continuation of a commandi,(CX) so this
James Smart4f774512009-05-22 14:52:35 -04008295 * sglq is on the active list
8296 */
8297 sglq = __lpfc_get_active_sglq(phba, piocb->sli4_xritag);
8298 if (!sglq)
8299 return IOCB_ERROR;
8300 }
8301
8302 if (sglq) {
James Smart6d368e52011-05-24 11:44:12 -04008303 piocb->sli4_lxritag = sglq->sli4_lxritag;
James Smart2a9bf3d2010-06-07 15:24:45 -04008304 piocb->sli4_xritag = sglq->sli4_xritag;
James Smart2a9bf3d2010-06-07 15:24:45 -04008305 if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocb, sglq))
James Smart4f774512009-05-22 14:52:35 -04008306 return IOCB_ERROR;
8307 }
8308
8309 if (lpfc_sli4_iocb2wqe(phba, piocb, &wqe))
8310 return IOCB_ERROR;
8311
James Smart341af102010-01-26 23:07:37 -05008312 if ((piocb->iocb_flag & LPFC_IO_FCP) ||
8313 (piocb->iocb_flag & LPFC_USE_FCPWQIDX)) {
James Smart5ffc2662009-11-18 15:39:44 -05008314 /*
8315 * For FCP command IOCB, get a new WQ index to distribute
8316 * WQE across the WQsr. On the other hand, for abort IOCB,
8317 * it carries the same WQ index to the original command
8318 * IOCB.
8319 */
James Smart341af102010-01-26 23:07:37 -05008320 if (piocb->iocb_flag & LPFC_IO_FCP)
James Smart5ffc2662009-11-18 15:39:44 -05008321 piocb->fcp_wqidx = lpfc_sli4_scmd_to_wqidx_distr(phba);
James Smart2e90f4b2011-12-13 13:22:37 -05008322 if (unlikely(!phba->sli4_hba.fcp_wq))
8323 return IOCB_ERROR;
James Smart5ffc2662009-11-18 15:39:44 -05008324 if (lpfc_sli4_wq_put(phba->sli4_hba.fcp_wq[piocb->fcp_wqidx],
8325 &wqe))
James Smart4f774512009-05-22 14:52:35 -04008326 return IOCB_ERROR;
8327 } else {
8328 if (lpfc_sli4_wq_put(phba->sli4_hba.els_wq, &wqe))
8329 return IOCB_ERROR;
8330 }
8331 lpfc_sli_ringtxcmpl_put(phba, pring, piocb);
8332
8333 return 0;
8334}
8335
8336/**
James Smart3772a992009-05-22 14:50:54 -04008337 * __lpfc_sli_issue_iocb - Wrapper func of lockless version for issuing iocb
8338 *
8339 * This routine wraps the actual lockless version for issusing IOCB function
8340 * pointer from the lpfc_hba struct.
8341 *
8342 * Return codes:
8343 * IOCB_ERROR - Error
8344 * IOCB_SUCCESS - Success
8345 * IOCB_BUSY - Busy
8346 **/
James Smart2a9bf3d2010-06-07 15:24:45 -04008347int
James Smart3772a992009-05-22 14:50:54 -04008348__lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
8349 struct lpfc_iocbq *piocb, uint32_t flag)
8350{
8351 return phba->__lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
8352}
8353
8354/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03008355 * lpfc_sli_api_table_setup - Set up sli api function jump table
James Smart3772a992009-05-22 14:50:54 -04008356 * @phba: The hba struct for which this call is being executed.
8357 * @dev_grp: The HBA PCI-Device group number.
8358 *
8359 * This routine sets up the SLI interface API function jump table in @phba
8360 * struct.
8361 * Returns: 0 - success, -ENODEV - failure.
8362 **/
8363int
8364lpfc_sli_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
8365{
8366
8367 switch (dev_grp) {
8368 case LPFC_PCI_DEV_LP:
8369 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s3;
8370 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s3;
8371 break;
James Smart4f774512009-05-22 14:52:35 -04008372 case LPFC_PCI_DEV_OC:
8373 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s4;
8374 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s4;
8375 break;
James Smart3772a992009-05-22 14:50:54 -04008376 default:
8377 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8378 "1419 Invalid HBA PCI-device group: 0x%x\n",
8379 dev_grp);
8380 return -ENODEV;
8381 break;
8382 }
8383 phba->lpfc_get_iocb_from_iocbq = lpfc_get_iocb_from_iocbq;
8384 return 0;
8385}
James Smart92d7f7b2007-06-17 19:56:38 -05008386
James Smarte59058c2008-08-24 21:49:00 -04008387/**
James Smart3621a712009-04-06 18:47:14 -04008388 * lpfc_sli_issue_iocb - Wrapper function for __lpfc_sli_issue_iocb
James Smarte59058c2008-08-24 21:49:00 -04008389 * @phba: Pointer to HBA context object.
8390 * @pring: Pointer to driver SLI ring object.
8391 * @piocb: Pointer to command iocb.
8392 * @flag: Flag indicating if this command can be put into txq.
8393 *
8394 * lpfc_sli_issue_iocb is a wrapper around __lpfc_sli_issue_iocb
8395 * function. This function gets the hbalock and calls
8396 * __lpfc_sli_issue_iocb function and will return the error returned
8397 * by __lpfc_sli_issue_iocb function. This wrapper is used by
8398 * functions which do not hold hbalock.
8399 **/
James Smart92d7f7b2007-06-17 19:56:38 -05008400int
James Smart3772a992009-05-22 14:50:54 -04008401lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
James Smart92d7f7b2007-06-17 19:56:38 -05008402 struct lpfc_iocbq *piocb, uint32_t flag)
8403{
8404 unsigned long iflags;
8405 int rc;
8406
8407 spin_lock_irqsave(&phba->hbalock, iflags);
James Smart3772a992009-05-22 14:50:54 -04008408 rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
James Smart92d7f7b2007-06-17 19:56:38 -05008409 spin_unlock_irqrestore(&phba->hbalock, iflags);
8410
8411 return rc;
8412}
8413
James Smarte59058c2008-08-24 21:49:00 -04008414/**
James Smart3621a712009-04-06 18:47:14 -04008415 * lpfc_extra_ring_setup - Extra ring setup function
James Smarte59058c2008-08-24 21:49:00 -04008416 * @phba: Pointer to HBA context object.
8417 *
8418 * This function is called while driver attaches with the
8419 * HBA to setup the extra ring. The extra ring is used
8420 * only when driver needs to support target mode functionality
8421 * or IP over FC functionalities.
8422 *
8423 * This function is called with no lock held.
8424 **/
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05008425static int
8426lpfc_extra_ring_setup( struct lpfc_hba *phba)
8427{
8428 struct lpfc_sli *psli;
8429 struct lpfc_sli_ring *pring;
8430
8431 psli = &phba->sli;
8432
8433 /* Adjust cmd/rsp ring iocb entries more evenly */
James Smarta4bc3372006-12-02 13:34:16 -05008434
8435 /* Take some away from the FCP ring */
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05008436 pring = &psli->ring[psli->fcp_ring];
8437 pring->numCiocb -= SLI2_IOCB_CMD_R1XTRA_ENTRIES;
8438 pring->numRiocb -= SLI2_IOCB_RSP_R1XTRA_ENTRIES;
8439 pring->numCiocb -= SLI2_IOCB_CMD_R3XTRA_ENTRIES;
8440 pring->numRiocb -= SLI2_IOCB_RSP_R3XTRA_ENTRIES;
8441
James Smarta4bc3372006-12-02 13:34:16 -05008442 /* and give them to the extra ring */
8443 pring = &psli->ring[psli->extra_ring];
8444
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05008445 pring->numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
8446 pring->numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
8447 pring->numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
8448 pring->numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
8449
8450 /* Setup default profile for this ring */
8451 pring->iotag_max = 4096;
8452 pring->num_mask = 1;
8453 pring->prt[0].profile = 0; /* Mask 0 */
James Smarta4bc3372006-12-02 13:34:16 -05008454 pring->prt[0].rctl = phba->cfg_multi_ring_rctl;
8455 pring->prt[0].type = phba->cfg_multi_ring_type;
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05008456 pring->prt[0].lpfc_sli_rcv_unsol_event = NULL;
8457 return 0;
8458}
8459
James Smartcb69f7d2011-12-13 13:21:57 -05008460/* lpfc_sli_abts_recover_port - Recover a port that failed an ABTS.
8461 * @vport: pointer to virtual port object.
8462 * @ndlp: nodelist pointer for the impacted rport.
8463 *
8464 * The driver calls this routine in response to a XRI ABORT CQE
8465 * event from the port. In this event, the driver is required to
8466 * recover its login to the rport even though its login may be valid
8467 * from the driver's perspective. The failed ABTS notice from the
8468 * port indicates the rport is not responding.
8469 */
8470static void
8471lpfc_sli_abts_recover_port(struct lpfc_vport *vport,
8472 struct lpfc_nodelist *ndlp)
8473{
8474 struct Scsi_Host *shost;
8475 struct lpfc_hba *phba;
8476 unsigned long flags = 0;
8477
8478 shost = lpfc_shost_from_vport(vport);
8479 phba = vport->phba;
8480 if (ndlp->nlp_state != NLP_STE_MAPPED_NODE) {
8481 lpfc_printf_log(phba, KERN_INFO,
8482 LOG_SLI, "3093 No rport recovery needed. "
8483 "rport in state 0x%x\n",
8484 ndlp->nlp_state);
8485 return;
8486 }
8487 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8488 "3094 Start rport recovery on shost id 0x%x "
8489 "fc_id 0x%06x vpi 0x%x rpi 0x%x state 0x%x "
8490 "flags 0x%x\n",
8491 shost->host_no, ndlp->nlp_DID,
8492 vport->vpi, ndlp->nlp_rpi, ndlp->nlp_state,
8493 ndlp->nlp_flag);
8494 /*
8495 * The rport is not responding. Don't attempt ADISC recovery.
8496 * Remove the FCP-2 flag to force a PLOGI.
8497 */
8498 spin_lock_irqsave(shost->host_lock, flags);
8499 ndlp->nlp_fcp_info &= ~NLP_FCP_2_DEVICE;
8500 spin_unlock_irqrestore(shost->host_lock, flags);
8501 lpfc_disc_state_machine(vport, ndlp, NULL,
8502 NLP_EVT_DEVICE_RECOVERY);
8503 lpfc_cancel_retry_delay_tmo(vport, ndlp);
8504 spin_lock_irqsave(shost->host_lock, flags);
8505 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
8506 spin_unlock_irqrestore(shost->host_lock, flags);
8507 lpfc_disc_start(vport);
8508}
8509
8510/* lpfc_sli_abts_err_handler - handle a failed ABTS request from an SLI3 port.
8511 * @phba: Pointer to HBA context object.
8512 * @iocbq: Pointer to iocb object.
8513 *
8514 * The async_event handler calls this routine when it receives
8515 * an ASYNC_STATUS_CN event from the port. The port generates
8516 * this event when an Abort Sequence request to an rport fails
8517 * twice in succession. The abort could be originated by the
8518 * driver or by the port. The ABTS could have been for an ELS
8519 * or FCP IO. The port only generates this event when an ABTS
8520 * fails to complete after one retry.
8521 */
8522static void
8523lpfc_sli_abts_err_handler(struct lpfc_hba *phba,
8524 struct lpfc_iocbq *iocbq)
8525{
8526 struct lpfc_nodelist *ndlp = NULL;
8527 uint16_t rpi = 0, vpi = 0;
8528 struct lpfc_vport *vport = NULL;
8529
8530 /* The rpi in the ulpContext is vport-sensitive. */
8531 vpi = iocbq->iocb.un.asyncstat.sub_ctxt_tag;
8532 rpi = iocbq->iocb.ulpContext;
8533
8534 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
8535 "3092 Port generated ABTS async event "
8536 "on vpi %d rpi %d status 0x%x\n",
8537 vpi, rpi, iocbq->iocb.ulpStatus);
8538
8539 vport = lpfc_find_vport_by_vpid(phba, vpi);
8540 if (!vport)
8541 goto err_exit;
8542 ndlp = lpfc_findnode_rpi(vport, rpi);
8543 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp))
8544 goto err_exit;
8545
8546 if (iocbq->iocb.ulpStatus == IOSTAT_LOCAL_REJECT)
8547 lpfc_sli_abts_recover_port(vport, ndlp);
8548 return;
8549
8550 err_exit:
8551 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
8552 "3095 Event Context not found, no "
8553 "action on vpi %d rpi %d status 0x%x, reason 0x%x\n",
8554 iocbq->iocb.ulpContext, iocbq->iocb.ulpStatus,
8555 vpi, rpi);
8556}
8557
8558/* lpfc_sli4_abts_err_handler - handle a failed ABTS request from an SLI4 port.
8559 * @phba: pointer to HBA context object.
8560 * @ndlp: nodelist pointer for the impacted rport.
8561 * @axri: pointer to the wcqe containing the failed exchange.
8562 *
8563 * The driver calls this routine when it receives an ABORT_XRI_FCP CQE from the
8564 * port. The port generates this event when an abort exchange request to an
8565 * rport fails twice in succession with no reply. The abort could be originated
8566 * by the driver or by the port. The ABTS could have been for an ELS or FCP IO.
8567 */
8568void
8569lpfc_sli4_abts_err_handler(struct lpfc_hba *phba,
8570 struct lpfc_nodelist *ndlp,
8571 struct sli4_wcqe_xri_aborted *axri)
8572{
8573 struct lpfc_vport *vport;
James Smart5c1db2a2012-03-01 22:34:36 -05008574 uint32_t ext_status = 0;
James Smartcb69f7d2011-12-13 13:21:57 -05008575
James Smart6b5151f2012-01-18 16:24:06 -05008576 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
James Smartcb69f7d2011-12-13 13:21:57 -05008577 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
8578 "3115 Node Context not found, driver "
8579 "ignoring abts err event\n");
James Smart6b5151f2012-01-18 16:24:06 -05008580 return;
8581 }
8582
James Smartcb69f7d2011-12-13 13:21:57 -05008583 vport = ndlp->vport;
8584 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
8585 "3116 Port generated FCP XRI ABORT event on "
James Smart5c1db2a2012-03-01 22:34:36 -05008586 "vpi %d rpi %d xri x%x status 0x%x parameter x%x\n",
James Smartcb69f7d2011-12-13 13:21:57 -05008587 ndlp->vport->vpi, ndlp->nlp_rpi,
8588 bf_get(lpfc_wcqe_xa_xri, axri),
James Smart5c1db2a2012-03-01 22:34:36 -05008589 bf_get(lpfc_wcqe_xa_status, axri),
8590 axri->parameter);
James Smartcb69f7d2011-12-13 13:21:57 -05008591
James Smart5c1db2a2012-03-01 22:34:36 -05008592 /*
8593 * Catch the ABTS protocol failure case. Older OCe FW releases returned
8594 * LOCAL_REJECT and 0 for a failed ABTS exchange and later OCe and
8595 * LPe FW releases returned LOCAL_REJECT and SEQUENCE_TIMEOUT.
8596 */
8597 ext_status = axri->parameter & WCQE_PARAM_MASK;
8598 if ((bf_get(lpfc_wcqe_xa_status, axri) == IOSTAT_LOCAL_REJECT) &&
8599 ((ext_status == IOERR_SEQUENCE_TIMEOUT) || (ext_status == 0)))
James Smartcb69f7d2011-12-13 13:21:57 -05008600 lpfc_sli_abts_recover_port(vport, ndlp);
8601}
8602
James Smarte59058c2008-08-24 21:49:00 -04008603/**
James Smart3621a712009-04-06 18:47:14 -04008604 * lpfc_sli_async_event_handler - ASYNC iocb handler function
James Smarte59058c2008-08-24 21:49:00 -04008605 * @phba: Pointer to HBA context object.
8606 * @pring: Pointer to driver SLI ring object.
8607 * @iocbq: Pointer to iocb object.
8608 *
8609 * This function is called by the slow ring event handler
8610 * function when there is an ASYNC event iocb in the ring.
8611 * This function is called with no lock held.
8612 * Currently this function handles only temperature related
8613 * ASYNC events. The function decodes the temperature sensor
8614 * event message and posts events for the management applications.
8615 **/
James Smart98c9ea52007-10-27 13:37:33 -04008616static void
James Smart57127f12007-10-27 13:37:05 -04008617lpfc_sli_async_event_handler(struct lpfc_hba * phba,
8618 struct lpfc_sli_ring * pring, struct lpfc_iocbq * iocbq)
8619{
8620 IOCB_t *icmd;
8621 uint16_t evt_code;
James Smart57127f12007-10-27 13:37:05 -04008622 struct temp_event temp_event_data;
8623 struct Scsi_Host *shost;
James Smarta257bf92009-04-06 18:48:10 -04008624 uint32_t *iocb_w;
James Smart57127f12007-10-27 13:37:05 -04008625
8626 icmd = &iocbq->iocb;
8627 evt_code = icmd->un.asyncstat.evt_code;
James Smart57127f12007-10-27 13:37:05 -04008628
James Smartcb69f7d2011-12-13 13:21:57 -05008629 switch (evt_code) {
8630 case ASYNC_TEMP_WARN:
8631 case ASYNC_TEMP_SAFE:
8632 temp_event_data.data = (uint32_t) icmd->ulpContext;
8633 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
8634 if (evt_code == ASYNC_TEMP_WARN) {
8635 temp_event_data.event_code = LPFC_THRESHOLD_TEMP;
8636 lpfc_printf_log(phba, KERN_ERR, LOG_TEMP,
8637 "0347 Adapter is very hot, please take "
8638 "corrective action. temperature : %d Celsius\n",
8639 (uint32_t) icmd->ulpContext);
8640 } else {
8641 temp_event_data.event_code = LPFC_NORMAL_TEMP;
8642 lpfc_printf_log(phba, KERN_ERR, LOG_TEMP,
8643 "0340 Adapter temperature is OK now. "
8644 "temperature : %d Celsius\n",
8645 (uint32_t) icmd->ulpContext);
8646 }
8647
8648 /* Send temperature change event to applications */
8649 shost = lpfc_shost_from_vport(phba->pport);
8650 fc_host_post_vendor_event(shost, fc_get_event_number(),
8651 sizeof(temp_event_data), (char *) &temp_event_data,
8652 LPFC_NL_VENDOR_ID);
8653 break;
8654 case ASYNC_STATUS_CN:
8655 lpfc_sli_abts_err_handler(phba, iocbq);
8656 break;
8657 default:
James Smarta257bf92009-04-06 18:48:10 -04008658 iocb_w = (uint32_t *) icmd;
James Smartcb69f7d2011-12-13 13:21:57 -05008659 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart76bb24e2007-10-27 13:38:00 -04008660 "0346 Ring %d handler: unexpected ASYNC_STATUS"
James Smarte4e74272009-07-19 10:01:38 -04008661 " evt_code 0x%x\n"
James Smarta257bf92009-04-06 18:48:10 -04008662 "W0 0x%08x W1 0x%08x W2 0x%08x W3 0x%08x\n"
8663 "W4 0x%08x W5 0x%08x W6 0x%08x W7 0x%08x\n"
8664 "W8 0x%08x W9 0x%08x W10 0x%08x W11 0x%08x\n"
8665 "W12 0x%08x W13 0x%08x W14 0x%08x W15 0x%08x\n",
James Smartcb69f7d2011-12-13 13:21:57 -05008666 pring->ringno, icmd->un.asyncstat.evt_code,
James Smarta257bf92009-04-06 18:48:10 -04008667 iocb_w[0], iocb_w[1], iocb_w[2], iocb_w[3],
8668 iocb_w[4], iocb_w[5], iocb_w[6], iocb_w[7],
8669 iocb_w[8], iocb_w[9], iocb_w[10], iocb_w[11],
8670 iocb_w[12], iocb_w[13], iocb_w[14], iocb_w[15]);
8671
James Smartcb69f7d2011-12-13 13:21:57 -05008672 break;
James Smart57127f12007-10-27 13:37:05 -04008673 }
James Smart57127f12007-10-27 13:37:05 -04008674}
8675
8676
James Smarte59058c2008-08-24 21:49:00 -04008677/**
James Smart3621a712009-04-06 18:47:14 -04008678 * lpfc_sli_setup - SLI ring setup function
James Smarte59058c2008-08-24 21:49:00 -04008679 * @phba: Pointer to HBA context object.
8680 *
8681 * lpfc_sli_setup sets up rings of the SLI interface with
8682 * number of iocbs per ring and iotags. This function is
8683 * called while driver attach to the HBA and before the
8684 * interrupts are enabled. So there is no need for locking.
8685 *
8686 * This function always returns 0.
8687 **/
dea31012005-04-17 16:05:31 -05008688int
8689lpfc_sli_setup(struct lpfc_hba *phba)
8690{
James Smarted957682007-06-17 19:56:37 -05008691 int i, totiocbsize = 0;
dea31012005-04-17 16:05:31 -05008692 struct lpfc_sli *psli = &phba->sli;
8693 struct lpfc_sli_ring *pring;
8694
8695 psli->num_rings = MAX_CONFIGURED_RINGS;
8696 psli->sli_flag = 0;
8697 psli->fcp_ring = LPFC_FCP_RING;
8698 psli->next_ring = LPFC_FCP_NEXT_RING;
James Smarta4bc3372006-12-02 13:34:16 -05008699 psli->extra_ring = LPFC_EXTRA_RING;
dea31012005-04-17 16:05:31 -05008700
James Bottomley604a3e32005-10-29 10:28:33 -05008701 psli->iocbq_lookup = NULL;
8702 psli->iocbq_lookup_len = 0;
8703 psli->last_iotag = 0;
8704
dea31012005-04-17 16:05:31 -05008705 for (i = 0; i < psli->num_rings; i++) {
8706 pring = &psli->ring[i];
8707 switch (i) {
8708 case LPFC_FCP_RING: /* ring 0 - FCP */
8709 /* numCiocb and numRiocb are used in config_port */
8710 pring->numCiocb = SLI2_IOCB_CMD_R0_ENTRIES;
8711 pring->numRiocb = SLI2_IOCB_RSP_R0_ENTRIES;
8712 pring->numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
8713 pring->numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
8714 pring->numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
8715 pring->numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
James Smarted957682007-06-17 19:56:37 -05008716 pring->sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05008717 SLI3_IOCB_CMD_SIZE :
8718 SLI2_IOCB_CMD_SIZE;
James Smarted957682007-06-17 19:56:37 -05008719 pring->sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05008720 SLI3_IOCB_RSP_SIZE :
8721 SLI2_IOCB_RSP_SIZE;
dea31012005-04-17 16:05:31 -05008722 pring->iotag_ctr = 0;
8723 pring->iotag_max =
James Smart92d7f7b2007-06-17 19:56:38 -05008724 (phba->cfg_hba_queue_depth * 2);
dea31012005-04-17 16:05:31 -05008725 pring->fast_iotag = pring->iotag_max;
8726 pring->num_mask = 0;
8727 break;
James Smarta4bc3372006-12-02 13:34:16 -05008728 case LPFC_EXTRA_RING: /* ring 1 - EXTRA */
dea31012005-04-17 16:05:31 -05008729 /* numCiocb and numRiocb are used in config_port */
8730 pring->numCiocb = SLI2_IOCB_CMD_R1_ENTRIES;
8731 pring->numRiocb = SLI2_IOCB_RSP_R1_ENTRIES;
James Smarted957682007-06-17 19:56:37 -05008732 pring->sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05008733 SLI3_IOCB_CMD_SIZE :
8734 SLI2_IOCB_CMD_SIZE;
James Smarted957682007-06-17 19:56:37 -05008735 pring->sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05008736 SLI3_IOCB_RSP_SIZE :
8737 SLI2_IOCB_RSP_SIZE;
James Smart2e0fef82007-06-17 19:56:36 -05008738 pring->iotag_max = phba->cfg_hba_queue_depth;
dea31012005-04-17 16:05:31 -05008739 pring->num_mask = 0;
8740 break;
8741 case LPFC_ELS_RING: /* ring 2 - ELS / CT */
8742 /* numCiocb and numRiocb are used in config_port */
8743 pring->numCiocb = SLI2_IOCB_CMD_R2_ENTRIES;
8744 pring->numRiocb = SLI2_IOCB_RSP_R2_ENTRIES;
James Smarted957682007-06-17 19:56:37 -05008745 pring->sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05008746 SLI3_IOCB_CMD_SIZE :
8747 SLI2_IOCB_CMD_SIZE;
James Smarted957682007-06-17 19:56:37 -05008748 pring->sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05008749 SLI3_IOCB_RSP_SIZE :
8750 SLI2_IOCB_RSP_SIZE;
dea31012005-04-17 16:05:31 -05008751 pring->fast_iotag = 0;
8752 pring->iotag_ctr = 0;
8753 pring->iotag_max = 4096;
James Smart57127f12007-10-27 13:37:05 -04008754 pring->lpfc_sli_rcv_async_status =
8755 lpfc_sli_async_event_handler;
James Smart6669f9b2009-10-02 15:16:45 -04008756 pring->num_mask = LPFC_MAX_RING_MASK;
dea31012005-04-17 16:05:31 -05008757 pring->prt[0].profile = 0; /* Mask 0 */
James Smart6a9c52c2009-10-02 15:16:51 -04008758 pring->prt[0].rctl = FC_RCTL_ELS_REQ;
8759 pring->prt[0].type = FC_TYPE_ELS;
dea31012005-04-17 16:05:31 -05008760 pring->prt[0].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05008761 lpfc_els_unsol_event;
dea31012005-04-17 16:05:31 -05008762 pring->prt[1].profile = 0; /* Mask 1 */
James Smart6a9c52c2009-10-02 15:16:51 -04008763 pring->prt[1].rctl = FC_RCTL_ELS_REP;
8764 pring->prt[1].type = FC_TYPE_ELS;
dea31012005-04-17 16:05:31 -05008765 pring->prt[1].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05008766 lpfc_els_unsol_event;
dea31012005-04-17 16:05:31 -05008767 pring->prt[2].profile = 0; /* Mask 2 */
8768 /* NameServer Inquiry */
James Smart6a9c52c2009-10-02 15:16:51 -04008769 pring->prt[2].rctl = FC_RCTL_DD_UNSOL_CTL;
dea31012005-04-17 16:05:31 -05008770 /* NameServer */
James Smart6a9c52c2009-10-02 15:16:51 -04008771 pring->prt[2].type = FC_TYPE_CT;
dea31012005-04-17 16:05:31 -05008772 pring->prt[2].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05008773 lpfc_ct_unsol_event;
dea31012005-04-17 16:05:31 -05008774 pring->prt[3].profile = 0; /* Mask 3 */
8775 /* NameServer response */
James Smart6a9c52c2009-10-02 15:16:51 -04008776 pring->prt[3].rctl = FC_RCTL_DD_SOL_CTL;
dea31012005-04-17 16:05:31 -05008777 /* NameServer */
James Smart6a9c52c2009-10-02 15:16:51 -04008778 pring->prt[3].type = FC_TYPE_CT;
dea31012005-04-17 16:05:31 -05008779 pring->prt[3].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05008780 lpfc_ct_unsol_event;
James Smart6669f9b2009-10-02 15:16:45 -04008781 /* abort unsolicited sequence */
8782 pring->prt[4].profile = 0; /* Mask 4 */
8783 pring->prt[4].rctl = FC_RCTL_BA_ABTS;
8784 pring->prt[4].type = FC_TYPE_BLS;
8785 pring->prt[4].lpfc_sli_rcv_unsol_event =
8786 lpfc_sli4_ct_abort_unsol_event;
dea31012005-04-17 16:05:31 -05008787 break;
8788 }
James Smarted957682007-06-17 19:56:37 -05008789 totiocbsize += (pring->numCiocb * pring->sizeCiocb) +
James Smart92d7f7b2007-06-17 19:56:38 -05008790 (pring->numRiocb * pring->sizeRiocb);
dea31012005-04-17 16:05:31 -05008791 }
James Smarted957682007-06-17 19:56:37 -05008792 if (totiocbsize > MAX_SLIM_IOCB_SIZE) {
dea31012005-04-17 16:05:31 -05008793 /* Too many cmd / rsp ring entries in SLI2 SLIM */
James Smarte8b62012007-08-02 11:10:09 -04008794 printk(KERN_ERR "%d:0462 Too many cmd / rsp ring entries in "
8795 "SLI2 SLIM Data: x%x x%lx\n",
8796 phba->brd_no, totiocbsize,
8797 (unsigned long) MAX_SLIM_IOCB_SIZE);
dea31012005-04-17 16:05:31 -05008798 }
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05008799 if (phba->cfg_multi_ring_support == 2)
8800 lpfc_extra_ring_setup(phba);
dea31012005-04-17 16:05:31 -05008801
8802 return 0;
8803}
8804
James Smarte59058c2008-08-24 21:49:00 -04008805/**
James Smart3621a712009-04-06 18:47:14 -04008806 * lpfc_sli_queue_setup - Queue initialization function
James Smarte59058c2008-08-24 21:49:00 -04008807 * @phba: Pointer to HBA context object.
8808 *
8809 * lpfc_sli_queue_setup sets up mailbox queues and iocb queues for each
8810 * ring. This function also initializes ring indices of each ring.
8811 * This function is called during the initialization of the SLI
8812 * interface of an HBA.
8813 * This function is called with no lock held and always returns
8814 * 1.
8815 **/
dea31012005-04-17 16:05:31 -05008816int
James Smart2e0fef82007-06-17 19:56:36 -05008817lpfc_sli_queue_setup(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05008818{
8819 struct lpfc_sli *psli;
8820 struct lpfc_sli_ring *pring;
James Bottomley604a3e32005-10-29 10:28:33 -05008821 int i;
dea31012005-04-17 16:05:31 -05008822
8823 psli = &phba->sli;
James Smart2e0fef82007-06-17 19:56:36 -05008824 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05008825 INIT_LIST_HEAD(&psli->mboxq);
James Smart92d7f7b2007-06-17 19:56:38 -05008826 INIT_LIST_HEAD(&psli->mboxq_cmpl);
dea31012005-04-17 16:05:31 -05008827 /* Initialize list headers for txq and txcmplq as double linked lists */
8828 for (i = 0; i < psli->num_rings; i++) {
8829 pring = &psli->ring[i];
8830 pring->ringno = i;
8831 pring->next_cmdidx = 0;
8832 pring->local_getidx = 0;
8833 pring->cmdidx = 0;
8834 INIT_LIST_HEAD(&pring->txq);
8835 INIT_LIST_HEAD(&pring->txcmplq);
8836 INIT_LIST_HEAD(&pring->iocb_continueq);
James Smart9c2face2008-01-11 01:53:18 -05008837 INIT_LIST_HEAD(&pring->iocb_continue_saveq);
dea31012005-04-17 16:05:31 -05008838 INIT_LIST_HEAD(&pring->postbufq);
dea31012005-04-17 16:05:31 -05008839 }
James Smart2e0fef82007-06-17 19:56:36 -05008840 spin_unlock_irq(&phba->hbalock);
8841 return 1;
dea31012005-04-17 16:05:31 -05008842}
8843
James Smarte59058c2008-08-24 21:49:00 -04008844/**
James Smart04c68492009-05-22 14:52:52 -04008845 * lpfc_sli_mbox_sys_flush - Flush mailbox command sub-system
8846 * @phba: Pointer to HBA context object.
8847 *
8848 * This routine flushes the mailbox command subsystem. It will unconditionally
8849 * flush all the mailbox commands in the three possible stages in the mailbox
8850 * command sub-system: pending mailbox command queue; the outstanding mailbox
8851 * command; and completed mailbox command queue. It is caller's responsibility
8852 * to make sure that the driver is in the proper state to flush the mailbox
8853 * command sub-system. Namely, the posting of mailbox commands into the
8854 * pending mailbox command queue from the various clients must be stopped;
8855 * either the HBA is in a state that it will never works on the outstanding
8856 * mailbox command (such as in EEH or ERATT conditions) or the outstanding
8857 * mailbox command has been completed.
8858 **/
8859static void
8860lpfc_sli_mbox_sys_flush(struct lpfc_hba *phba)
8861{
8862 LIST_HEAD(completions);
8863 struct lpfc_sli *psli = &phba->sli;
8864 LPFC_MBOXQ_t *pmb;
8865 unsigned long iflag;
8866
8867 /* Flush all the mailbox commands in the mbox system */
8868 spin_lock_irqsave(&phba->hbalock, iflag);
8869 /* The pending mailbox command queue */
8870 list_splice_init(&phba->sli.mboxq, &completions);
8871 /* The outstanding active mailbox command */
8872 if (psli->mbox_active) {
8873 list_add_tail(&psli->mbox_active->list, &completions);
8874 psli->mbox_active = NULL;
8875 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
8876 }
8877 /* The completed mailbox command queue */
8878 list_splice_init(&phba->sli.mboxq_cmpl, &completions);
8879 spin_unlock_irqrestore(&phba->hbalock, iflag);
8880
8881 /* Return all flushed mailbox commands with MBX_NOT_FINISHED status */
8882 while (!list_empty(&completions)) {
8883 list_remove_head(&completions, pmb, LPFC_MBOXQ_t, list);
8884 pmb->u.mb.mbxStatus = MBX_NOT_FINISHED;
8885 if (pmb->mbox_cmpl)
8886 pmb->mbox_cmpl(phba, pmb);
8887 }
8888}
8889
8890/**
James Smart3621a712009-04-06 18:47:14 -04008891 * lpfc_sli_host_down - Vport cleanup function
James Smarte59058c2008-08-24 21:49:00 -04008892 * @vport: Pointer to virtual port object.
8893 *
8894 * lpfc_sli_host_down is called to clean up the resources
8895 * associated with a vport before destroying virtual
8896 * port data structures.
8897 * This function does following operations:
8898 * - Free discovery resources associated with this virtual
8899 * port.
8900 * - Free iocbs associated with this virtual port in
8901 * the txq.
8902 * - Send abort for all iocb commands associated with this
8903 * vport in txcmplq.
8904 *
8905 * This function is called with no lock held and always returns 1.
8906 **/
dea31012005-04-17 16:05:31 -05008907int
James Smart92d7f7b2007-06-17 19:56:38 -05008908lpfc_sli_host_down(struct lpfc_vport *vport)
8909{
James Smart858c9f62007-06-17 19:56:39 -05008910 LIST_HEAD(completions);
James Smart92d7f7b2007-06-17 19:56:38 -05008911 struct lpfc_hba *phba = vport->phba;
8912 struct lpfc_sli *psli = &phba->sli;
8913 struct lpfc_sli_ring *pring;
8914 struct lpfc_iocbq *iocb, *next_iocb;
James Smart92d7f7b2007-06-17 19:56:38 -05008915 int i;
8916 unsigned long flags = 0;
8917 uint16_t prev_pring_flag;
8918
8919 lpfc_cleanup_discovery_resources(vport);
8920
8921 spin_lock_irqsave(&phba->hbalock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -05008922 for (i = 0; i < psli->num_rings; i++) {
8923 pring = &psli->ring[i];
8924 prev_pring_flag = pring->flag;
James Smart5e9d9b82008-06-14 22:52:53 -04008925 /* Only slow rings */
8926 if (pring->ringno == LPFC_ELS_RING) {
James Smart858c9f62007-06-17 19:56:39 -05008927 pring->flag |= LPFC_DEFERRED_RING_EVENT;
James Smart5e9d9b82008-06-14 22:52:53 -04008928 /* Set the lpfc data pending flag */
8929 set_bit(LPFC_DATA_READY, &phba->data_flags);
8930 }
James Smart92d7f7b2007-06-17 19:56:38 -05008931 /*
8932 * Error everything on the txq since these iocbs have not been
8933 * given to the FW yet.
8934 */
James Smart92d7f7b2007-06-17 19:56:38 -05008935 list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
8936 if (iocb->vport != vport)
8937 continue;
James Smart858c9f62007-06-17 19:56:39 -05008938 list_move_tail(&iocb->list, &completions);
James Smart92d7f7b2007-06-17 19:56:38 -05008939 pring->txq_cnt--;
James Smart92d7f7b2007-06-17 19:56:38 -05008940 }
8941
8942 /* Next issue ABTS for everything on the txcmplq */
8943 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq,
8944 list) {
8945 if (iocb->vport != vport)
8946 continue;
8947 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
8948 }
8949
8950 pring->flag = prev_pring_flag;
8951 }
8952
8953 spin_unlock_irqrestore(&phba->hbalock, flags);
8954
James Smarta257bf92009-04-06 18:48:10 -04008955 /* Cancel all the IOCBs from the completions list */
8956 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
8957 IOERR_SLI_DOWN);
James Smart92d7f7b2007-06-17 19:56:38 -05008958 return 1;
8959}
8960
James Smarte59058c2008-08-24 21:49:00 -04008961/**
James Smart3621a712009-04-06 18:47:14 -04008962 * lpfc_sli_hba_down - Resource cleanup function for the HBA
James Smarte59058c2008-08-24 21:49:00 -04008963 * @phba: Pointer to HBA context object.
8964 *
8965 * This function cleans up all iocb, buffers, mailbox commands
8966 * while shutting down the HBA. This function is called with no
8967 * lock held and always returns 1.
8968 * This function does the following to cleanup driver resources:
8969 * - Free discovery resources for each virtual port
8970 * - Cleanup any pending fabric iocbs
8971 * - Iterate through the iocb txq and free each entry
8972 * in the list.
8973 * - Free up any buffer posted to the HBA
8974 * - Free mailbox commands in the mailbox queue.
8975 **/
James Smart92d7f7b2007-06-17 19:56:38 -05008976int
James Smart2e0fef82007-06-17 19:56:36 -05008977lpfc_sli_hba_down(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05008978{
James Smart2534ba72007-04-25 09:52:20 -04008979 LIST_HEAD(completions);
James Smart2e0fef82007-06-17 19:56:36 -05008980 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05008981 struct lpfc_sli_ring *pring;
James Smart0ff10d42008-01-11 01:52:36 -05008982 struct lpfc_dmabuf *buf_ptr;
dea31012005-04-17 16:05:31 -05008983 unsigned long flags = 0;
James Smart04c68492009-05-22 14:52:52 -04008984 int i;
8985
8986 /* Shutdown the mailbox command sub-system */
James Smart618a5232012-06-12 13:54:36 -04008987 lpfc_sli_mbox_sys_shutdown(phba, LPFC_MBX_WAIT);
dea31012005-04-17 16:05:31 -05008988
dea31012005-04-17 16:05:31 -05008989 lpfc_hba_down_prep(phba);
8990
James Smart92d7f7b2007-06-17 19:56:38 -05008991 lpfc_fabric_abort_hba(phba);
8992
James Smart2e0fef82007-06-17 19:56:36 -05008993 spin_lock_irqsave(&phba->hbalock, flags);
dea31012005-04-17 16:05:31 -05008994 for (i = 0; i < psli->num_rings; i++) {
8995 pring = &psli->ring[i];
James Smart5e9d9b82008-06-14 22:52:53 -04008996 /* Only slow rings */
8997 if (pring->ringno == LPFC_ELS_RING) {
James Smart858c9f62007-06-17 19:56:39 -05008998 pring->flag |= LPFC_DEFERRED_RING_EVENT;
James Smart5e9d9b82008-06-14 22:52:53 -04008999 /* Set the lpfc data pending flag */
9000 set_bit(LPFC_DATA_READY, &phba->data_flags);
9001 }
dea31012005-04-17 16:05:31 -05009002
9003 /*
9004 * Error everything on the txq since these iocbs have not been
9005 * given to the FW yet.
9006 */
James Smart2534ba72007-04-25 09:52:20 -04009007 list_splice_init(&pring->txq, &completions);
dea31012005-04-17 16:05:31 -05009008 pring->txq_cnt = 0;
9009
dea31012005-04-17 16:05:31 -05009010 }
James Smart2e0fef82007-06-17 19:56:36 -05009011 spin_unlock_irqrestore(&phba->hbalock, flags);
dea31012005-04-17 16:05:31 -05009012
James Smarta257bf92009-04-06 18:48:10 -04009013 /* Cancel all the IOCBs from the completions list */
9014 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
9015 IOERR_SLI_DOWN);
James Smart2534ba72007-04-25 09:52:20 -04009016
James Smart0ff10d42008-01-11 01:52:36 -05009017 spin_lock_irqsave(&phba->hbalock, flags);
9018 list_splice_init(&phba->elsbuf, &completions);
9019 phba->elsbuf_cnt = 0;
9020 phba->elsbuf_prev_cnt = 0;
9021 spin_unlock_irqrestore(&phba->hbalock, flags);
9022
9023 while (!list_empty(&completions)) {
9024 list_remove_head(&completions, buf_ptr,
9025 struct lpfc_dmabuf, list);
9026 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
9027 kfree(buf_ptr);
9028 }
9029
dea31012005-04-17 16:05:31 -05009030 /* Return any active mbox cmds */
9031 del_timer_sync(&psli->mbox_tmo);
James Smart92d7f7b2007-06-17 19:56:38 -05009032
James Smartda0436e2009-05-22 14:51:39 -04009033 spin_lock_irqsave(&phba->pport->work_port_lock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -05009034 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
James Smartda0436e2009-05-22 14:51:39 -04009035 spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -05009036
James Smartda0436e2009-05-22 14:51:39 -04009037 return 1;
9038}
James Smart92d7f7b2007-06-17 19:56:38 -05009039
James Smartda0436e2009-05-22 14:51:39 -04009040/**
James Smart3621a712009-04-06 18:47:14 -04009041 * lpfc_sli_pcimem_bcopy - SLI memory copy function
James Smarte59058c2008-08-24 21:49:00 -04009042 * @srcp: Source memory pointer.
9043 * @destp: Destination memory pointer.
9044 * @cnt: Number of words required to be copied.
9045 *
9046 * This function is used for copying data between driver memory
9047 * and the SLI memory. This function also changes the endianness
9048 * of each word if native endianness is different from SLI
9049 * endianness. This function can be called with or without
9050 * lock.
9051 **/
dea31012005-04-17 16:05:31 -05009052void
9053lpfc_sli_pcimem_bcopy(void *srcp, void *destp, uint32_t cnt)
9054{
9055 uint32_t *src = srcp;
9056 uint32_t *dest = destp;
9057 uint32_t ldata;
9058 int i;
9059
9060 for (i = 0; i < (int)cnt; i += sizeof (uint32_t)) {
9061 ldata = *src;
9062 ldata = le32_to_cpu(ldata);
9063 *dest = ldata;
9064 src++;
9065 dest++;
9066 }
9067}
9068
James Smarte59058c2008-08-24 21:49:00 -04009069
9070/**
James Smarta0c87cb2009-07-19 10:01:10 -04009071 * lpfc_sli_bemem_bcopy - SLI memory copy function
9072 * @srcp: Source memory pointer.
9073 * @destp: Destination memory pointer.
9074 * @cnt: Number of words required to be copied.
9075 *
9076 * This function is used for copying data between a data structure
9077 * with big endian representation to local endianness.
9078 * This function can be called with or without lock.
9079 **/
9080void
9081lpfc_sli_bemem_bcopy(void *srcp, void *destp, uint32_t cnt)
9082{
9083 uint32_t *src = srcp;
9084 uint32_t *dest = destp;
9085 uint32_t ldata;
9086 int i;
9087
9088 for (i = 0; i < (int)cnt; i += sizeof(uint32_t)) {
9089 ldata = *src;
9090 ldata = be32_to_cpu(ldata);
9091 *dest = ldata;
9092 src++;
9093 dest++;
9094 }
9095}
9096
9097/**
James Smart3621a712009-04-06 18:47:14 -04009098 * lpfc_sli_ringpostbuf_put - Function to add a buffer to postbufq
James Smarte59058c2008-08-24 21:49:00 -04009099 * @phba: Pointer to HBA context object.
9100 * @pring: Pointer to driver SLI ring object.
9101 * @mp: Pointer to driver buffer object.
9102 *
9103 * This function is called with no lock held.
9104 * It always return zero after adding the buffer to the postbufq
9105 * buffer list.
9106 **/
dea31012005-04-17 16:05:31 -05009107int
James Smart2e0fef82007-06-17 19:56:36 -05009108lpfc_sli_ringpostbuf_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
9109 struct lpfc_dmabuf *mp)
dea31012005-04-17 16:05:31 -05009110{
9111 /* Stick struct lpfc_dmabuf at end of postbufq so driver can look it up
9112 later */
James Smart2e0fef82007-06-17 19:56:36 -05009113 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05009114 list_add_tail(&mp->list, &pring->postbufq);
dea31012005-04-17 16:05:31 -05009115 pring->postbufq_cnt++;
James Smart2e0fef82007-06-17 19:56:36 -05009116 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05009117 return 0;
9118}
9119
James Smarte59058c2008-08-24 21:49:00 -04009120/**
James Smart3621a712009-04-06 18:47:14 -04009121 * lpfc_sli_get_buffer_tag - allocates a tag for a CMD_QUE_XRI64_CX buffer
James Smarte59058c2008-08-24 21:49:00 -04009122 * @phba: Pointer to HBA context object.
9123 *
9124 * When HBQ is enabled, buffers are searched based on tags. This function
9125 * allocates a tag for buffer posted using CMD_QUE_XRI64_CX iocb. The
9126 * tag is bit wise or-ed with QUE_BUFTAG_BIT to make sure that the tag
9127 * does not conflict with tags of buffer posted for unsolicited events.
9128 * The function returns the allocated tag. The function is called with
9129 * no locks held.
9130 **/
James Smart76bb24e2007-10-27 13:38:00 -04009131uint32_t
9132lpfc_sli_get_buffer_tag(struct lpfc_hba *phba)
9133{
9134 spin_lock_irq(&phba->hbalock);
9135 phba->buffer_tag_count++;
9136 /*
9137 * Always set the QUE_BUFTAG_BIT to distiguish between
9138 * a tag assigned by HBQ.
9139 */
9140 phba->buffer_tag_count |= QUE_BUFTAG_BIT;
9141 spin_unlock_irq(&phba->hbalock);
9142 return phba->buffer_tag_count;
9143}
9144
James Smarte59058c2008-08-24 21:49:00 -04009145/**
James Smart3621a712009-04-06 18:47:14 -04009146 * lpfc_sli_ring_taggedbuf_get - find HBQ buffer associated with given tag
James Smarte59058c2008-08-24 21:49:00 -04009147 * @phba: Pointer to HBA context object.
9148 * @pring: Pointer to driver SLI ring object.
9149 * @tag: Buffer tag.
9150 *
9151 * Buffers posted using CMD_QUE_XRI64_CX iocb are in pring->postbufq
9152 * list. After HBA DMA data to these buffers, CMD_IOCB_RET_XRI64_CX
9153 * iocb is posted to the response ring with the tag of the buffer.
9154 * This function searches the pring->postbufq list using the tag
9155 * to find buffer associated with CMD_IOCB_RET_XRI64_CX
9156 * iocb. If the buffer is found then lpfc_dmabuf object of the
9157 * buffer is returned to the caller else NULL is returned.
9158 * This function is called with no lock held.
9159 **/
James Smart76bb24e2007-10-27 13:38:00 -04009160struct lpfc_dmabuf *
9161lpfc_sli_ring_taggedbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
9162 uint32_t tag)
9163{
9164 struct lpfc_dmabuf *mp, *next_mp;
9165 struct list_head *slp = &pring->postbufq;
9166
Lucas De Marchi25985ed2011-03-30 22:57:33 -03009167 /* Search postbufq, from the beginning, looking for a match on tag */
James Smart76bb24e2007-10-27 13:38:00 -04009168 spin_lock_irq(&phba->hbalock);
9169 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
9170 if (mp->buffer_tag == tag) {
9171 list_del_init(&mp->list);
9172 pring->postbufq_cnt--;
9173 spin_unlock_irq(&phba->hbalock);
9174 return mp;
9175 }
9176 }
9177
9178 spin_unlock_irq(&phba->hbalock);
9179 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartd7c255b2008-08-24 21:50:00 -04009180 "0402 Cannot find virtual addr for buffer tag on "
James Smart76bb24e2007-10-27 13:38:00 -04009181 "ring %d Data x%lx x%p x%p x%x\n",
9182 pring->ringno, (unsigned long) tag,
9183 slp->next, slp->prev, pring->postbufq_cnt);
9184
9185 return NULL;
9186}
dea31012005-04-17 16:05:31 -05009187
James Smarte59058c2008-08-24 21:49:00 -04009188/**
James Smart3621a712009-04-06 18:47:14 -04009189 * lpfc_sli_ringpostbuf_get - search buffers for unsolicited CT and ELS events
James Smarte59058c2008-08-24 21:49:00 -04009190 * @phba: Pointer to HBA context object.
9191 * @pring: Pointer to driver SLI ring object.
9192 * @phys: DMA address of the buffer.
9193 *
9194 * This function searches the buffer list using the dma_address
9195 * of unsolicited event to find the driver's lpfc_dmabuf object
9196 * corresponding to the dma_address. The function returns the
9197 * lpfc_dmabuf object if a buffer is found else it returns NULL.
9198 * This function is called by the ct and els unsolicited event
9199 * handlers to get the buffer associated with the unsolicited
9200 * event.
9201 *
9202 * This function is called with no lock held.
9203 **/
dea31012005-04-17 16:05:31 -05009204struct lpfc_dmabuf *
9205lpfc_sli_ringpostbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
9206 dma_addr_t phys)
9207{
9208 struct lpfc_dmabuf *mp, *next_mp;
9209 struct list_head *slp = &pring->postbufq;
9210
Lucas De Marchi25985ed2011-03-30 22:57:33 -03009211 /* Search postbufq, from the beginning, looking for a match on phys */
James Smart2e0fef82007-06-17 19:56:36 -05009212 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05009213 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
9214 if (mp->phys == phys) {
9215 list_del_init(&mp->list);
9216 pring->postbufq_cnt--;
James Smart2e0fef82007-06-17 19:56:36 -05009217 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05009218 return mp;
9219 }
9220 }
9221
James Smart2e0fef82007-06-17 19:56:36 -05009222 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05009223 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04009224 "0410 Cannot find virtual addr for mapped buf on "
dea31012005-04-17 16:05:31 -05009225 "ring %d Data x%llx x%p x%p x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04009226 pring->ringno, (unsigned long long)phys,
dea31012005-04-17 16:05:31 -05009227 slp->next, slp->prev, pring->postbufq_cnt);
9228 return NULL;
9229}
9230
James Smarte59058c2008-08-24 21:49:00 -04009231/**
James Smart3621a712009-04-06 18:47:14 -04009232 * lpfc_sli_abort_els_cmpl - Completion handler for the els abort iocbs
James Smarte59058c2008-08-24 21:49:00 -04009233 * @phba: Pointer to HBA context object.
9234 * @cmdiocb: Pointer to driver command iocb object.
9235 * @rspiocb: Pointer to driver response iocb object.
9236 *
9237 * This function is the completion handler for the abort iocbs for
9238 * ELS commands. This function is called from the ELS ring event
9239 * handler with no lock held. This function frees memory resources
9240 * associated with the abort iocb.
9241 **/
dea31012005-04-17 16:05:31 -05009242static void
James Smart2e0fef82007-06-17 19:56:36 -05009243lpfc_sli_abort_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
9244 struct lpfc_iocbq *rspiocb)
dea31012005-04-17 16:05:31 -05009245{
James Smart2e0fef82007-06-17 19:56:36 -05009246 IOCB_t *irsp = &rspiocb->iocb;
James Smart2680eea2007-04-25 09:52:55 -04009247 uint16_t abort_iotag, abort_context;
James Smartff78d8f2011-12-13 13:21:35 -05009248 struct lpfc_iocbq *abort_iocb = NULL;
James Smart2680eea2007-04-25 09:52:55 -04009249
9250 if (irsp->ulpStatus) {
James Smartff78d8f2011-12-13 13:21:35 -05009251
9252 /*
9253 * Assume that the port already completed and returned, or
9254 * will return the iocb. Just Log the message.
9255 */
James Smart2680eea2007-04-25 09:52:55 -04009256 abort_context = cmdiocb->iocb.un.acxri.abortContextTag;
9257 abort_iotag = cmdiocb->iocb.un.acxri.abortIoTag;
9258
James Smart2e0fef82007-06-17 19:56:36 -05009259 spin_lock_irq(&phba->hbalock);
James Smart45ed1192009-10-02 15:17:02 -04009260 if (phba->sli_rev < LPFC_SLI_REV4) {
9261 if (abort_iotag != 0 &&
9262 abort_iotag <= phba->sli.last_iotag)
9263 abort_iocb =
9264 phba->sli.iocbq_lookup[abort_iotag];
9265 } else
9266 /* For sli4 the abort_tag is the XRI,
9267 * so the abort routine puts the iotag of the iocb
9268 * being aborted in the context field of the abort
9269 * IOCB.
9270 */
9271 abort_iocb = phba->sli.iocbq_lookup[abort_context];
James Smart2680eea2007-04-25 09:52:55 -04009272
James Smart2a9bf3d2010-06-07 15:24:45 -04009273 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS | LOG_SLI,
9274 "0327 Cannot abort els iocb %p "
9275 "with tag %x context %x, abort status %x, "
9276 "abort code %x\n",
9277 abort_iocb, abort_iotag, abort_context,
9278 irsp->ulpStatus, irsp->un.ulpWord[4]);
James Smart2680eea2007-04-25 09:52:55 -04009279
James Smartff78d8f2011-12-13 13:21:35 -05009280 spin_unlock_irq(&phba->hbalock);
James Smart2680eea2007-04-25 09:52:55 -04009281 }
James Bottomley604a3e32005-10-29 10:28:33 -05009282 lpfc_sli_release_iocbq(phba, cmdiocb);
dea31012005-04-17 16:05:31 -05009283 return;
9284}
9285
James Smarte59058c2008-08-24 21:49:00 -04009286/**
James Smart3621a712009-04-06 18:47:14 -04009287 * lpfc_ignore_els_cmpl - Completion handler for aborted ELS command
James Smarte59058c2008-08-24 21:49:00 -04009288 * @phba: Pointer to HBA context object.
9289 * @cmdiocb: Pointer to driver command iocb object.
9290 * @rspiocb: Pointer to driver response iocb object.
9291 *
9292 * The function is called from SLI ring event handler with no
9293 * lock held. This function is the completion handler for ELS commands
9294 * which are aborted. The function frees memory resources used for
9295 * the aborted ELS commands.
9296 **/
James Smart92d7f7b2007-06-17 19:56:38 -05009297static void
9298lpfc_ignore_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
9299 struct lpfc_iocbq *rspiocb)
9300{
9301 IOCB_t *irsp = &rspiocb->iocb;
9302
9303 /* ELS cmd tag <ulpIoTag> completes */
9304 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
James Smartd7c255b2008-08-24 21:50:00 -04009305 "0139 Ignoring ELS cmd tag x%x completion Data: "
James Smart92d7f7b2007-06-17 19:56:38 -05009306 "x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04009307 irsp->ulpIoTag, irsp->ulpStatus,
James Smart92d7f7b2007-06-17 19:56:38 -05009308 irsp->un.ulpWord[4], irsp->ulpTimeout);
James Smart858c9f62007-06-17 19:56:39 -05009309 if (cmdiocb->iocb.ulpCommand == CMD_GEN_REQUEST64_CR)
9310 lpfc_ct_free_iocb(phba, cmdiocb);
9311 else
9312 lpfc_els_free_iocb(phba, cmdiocb);
James Smart92d7f7b2007-06-17 19:56:38 -05009313 return;
9314}
9315
James Smarte59058c2008-08-24 21:49:00 -04009316/**
James Smart5af5eee2010-10-22 11:06:38 -04009317 * lpfc_sli_abort_iotag_issue - Issue abort for a command iocb
James Smarte59058c2008-08-24 21:49:00 -04009318 * @phba: Pointer to HBA context object.
9319 * @pring: Pointer to driver SLI ring object.
9320 * @cmdiocb: Pointer to driver command iocb object.
9321 *
James Smart5af5eee2010-10-22 11:06:38 -04009322 * This function issues an abort iocb for the provided command iocb down to
9323 * the port. Other than the case the outstanding command iocb is an abort
9324 * request, this function issues abort out unconditionally. This function is
9325 * called with hbalock held. The function returns 0 when it fails due to
9326 * memory allocation failure or when the command iocb is an abort request.
James Smarte59058c2008-08-24 21:49:00 -04009327 **/
James Smart5af5eee2010-10-22 11:06:38 -04009328static int
9329lpfc_sli_abort_iotag_issue(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -05009330 struct lpfc_iocbq *cmdiocb)
dea31012005-04-17 16:05:31 -05009331{
James Smart2e0fef82007-06-17 19:56:36 -05009332 struct lpfc_vport *vport = cmdiocb->vport;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009333 struct lpfc_iocbq *abtsiocbp;
dea31012005-04-17 16:05:31 -05009334 IOCB_t *icmd = NULL;
9335 IOCB_t *iabt = NULL;
James Smart5af5eee2010-10-22 11:06:38 -04009336 int retval;
James Smart07951072007-04-25 09:51:38 -04009337
James Smart92d7f7b2007-06-17 19:56:38 -05009338 /*
9339 * There are certain command types we don't want to abort. And we
9340 * don't want to abort commands that are already in the process of
9341 * being aborted.
James Smart07951072007-04-25 09:51:38 -04009342 */
9343 icmd = &cmdiocb->iocb;
James Smart2e0fef82007-06-17 19:56:36 -05009344 if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
James Smart92d7f7b2007-06-17 19:56:38 -05009345 icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
9346 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
James Smart07951072007-04-25 09:51:38 -04009347 return 0;
9348
dea31012005-04-17 16:05:31 -05009349 /* issue ABTS for this IOCB based on iotag */
James Smart92d7f7b2007-06-17 19:56:38 -05009350 abtsiocbp = __lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05009351 if (abtsiocbp == NULL)
9352 return 0;
dea31012005-04-17 16:05:31 -05009353
James Smart07951072007-04-25 09:51:38 -04009354 /* This signals the response to set the correct status
James Smart341af102010-01-26 23:07:37 -05009355 * before calling the completion handler
James Smart07951072007-04-25 09:51:38 -04009356 */
9357 cmdiocb->iocb_flag |= LPFC_DRIVER_ABORTED;
9358
dea31012005-04-17 16:05:31 -05009359 iabt = &abtsiocbp->iocb;
James Smart07951072007-04-25 09:51:38 -04009360 iabt->un.acxri.abortType = ABORT_TYPE_ABTS;
9361 iabt->un.acxri.abortContextTag = icmd->ulpContext;
James Smart45ed1192009-10-02 15:17:02 -04009362 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smartda0436e2009-05-22 14:51:39 -04009363 iabt->un.acxri.abortIoTag = cmdiocb->sli4_xritag;
James Smart45ed1192009-10-02 15:17:02 -04009364 iabt->un.acxri.abortContextTag = cmdiocb->iotag;
9365 }
James Smartda0436e2009-05-22 14:51:39 -04009366 else
9367 iabt->un.acxri.abortIoTag = icmd->ulpIoTag;
dea31012005-04-17 16:05:31 -05009368 iabt->ulpLe = 1;
James Smart07951072007-04-25 09:51:38 -04009369 iabt->ulpClass = icmd->ulpClass;
dea31012005-04-17 16:05:31 -05009370
James Smart5ffc2662009-11-18 15:39:44 -05009371 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
9372 abtsiocbp->fcp_wqidx = cmdiocb->fcp_wqidx;
James Smart341af102010-01-26 23:07:37 -05009373 if (cmdiocb->iocb_flag & LPFC_IO_FCP)
9374 abtsiocbp->iocb_flag |= LPFC_USE_FCPWQIDX;
James Smart5ffc2662009-11-18 15:39:44 -05009375
James Smart2e0fef82007-06-17 19:56:36 -05009376 if (phba->link_state >= LPFC_LINK_UP)
James Smart07951072007-04-25 09:51:38 -04009377 iabt->ulpCommand = CMD_ABORT_XRI_CN;
9378 else
9379 iabt->ulpCommand = CMD_CLOSE_XRI_CN;
9380
9381 abtsiocbp->iocb_cmpl = lpfc_sli_abort_els_cmpl;
James Smart5b8bd0c2007-04-25 09:52:49 -04009382
James Smarte8b62012007-08-02 11:10:09 -04009383 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
9384 "0339 Abort xri x%x, original iotag x%x, "
9385 "abort cmd iotag x%x\n",
James Smart2a9bf3d2010-06-07 15:24:45 -04009386 iabt->un.acxri.abortIoTag,
James Smarte8b62012007-08-02 11:10:09 -04009387 iabt->un.acxri.abortContextTag,
James Smart2a9bf3d2010-06-07 15:24:45 -04009388 abtsiocbp->iotag);
James Smartda0436e2009-05-22 14:51:39 -04009389 retval = __lpfc_sli_issue_iocb(phba, pring->ringno, abtsiocbp, 0);
James Smart07951072007-04-25 09:51:38 -04009390
James Smartd7c255b2008-08-24 21:50:00 -04009391 if (retval)
9392 __lpfc_sli_release_iocbq(phba, abtsiocbp);
James Smart5af5eee2010-10-22 11:06:38 -04009393
9394 /*
9395 * Caller to this routine should check for IOCB_ERROR
9396 * and handle it properly. This routine no longer removes
9397 * iocb off txcmplq and call compl in case of IOCB_ERROR.
9398 */
9399 return retval;
9400}
9401
9402/**
9403 * lpfc_sli_issue_abort_iotag - Abort function for a command iocb
9404 * @phba: Pointer to HBA context object.
9405 * @pring: Pointer to driver SLI ring object.
9406 * @cmdiocb: Pointer to driver command iocb object.
9407 *
9408 * This function issues an abort iocb for the provided command iocb. In case
9409 * of unloading, the abort iocb will not be issued to commands on the ELS
9410 * ring. Instead, the callback function shall be changed to those commands
9411 * so that nothing happens when them finishes. This function is called with
9412 * hbalock held. The function returns 0 when the command iocb is an abort
9413 * request.
9414 **/
9415int
9416lpfc_sli_issue_abort_iotag(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
9417 struct lpfc_iocbq *cmdiocb)
9418{
9419 struct lpfc_vport *vport = cmdiocb->vport;
9420 int retval = IOCB_ERROR;
9421 IOCB_t *icmd = NULL;
9422
9423 /*
9424 * There are certain command types we don't want to abort. And we
9425 * don't want to abort commands that are already in the process of
9426 * being aborted.
9427 */
9428 icmd = &cmdiocb->iocb;
9429 if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
9430 icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
9431 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
9432 return 0;
9433
9434 /*
9435 * If we're unloading, don't abort iocb on the ELS ring, but change
9436 * the callback so that nothing happens when it finishes.
9437 */
9438 if ((vport->load_flag & FC_UNLOADING) &&
9439 (pring->ringno == LPFC_ELS_RING)) {
9440 if (cmdiocb->iocb_flag & LPFC_IO_FABRIC)
9441 cmdiocb->fabric_iocb_cmpl = lpfc_ignore_els_cmpl;
9442 else
9443 cmdiocb->iocb_cmpl = lpfc_ignore_els_cmpl;
9444 goto abort_iotag_exit;
9445 }
9446
9447 /* Now, we try to issue the abort to the cmdiocb out */
9448 retval = lpfc_sli_abort_iotag_issue(phba, pring, cmdiocb);
9449
James Smart07951072007-04-25 09:51:38 -04009450abort_iotag_exit:
James Smart2e0fef82007-06-17 19:56:36 -05009451 /*
9452 * Caller to this routine should check for IOCB_ERROR
9453 * and handle it properly. This routine no longer removes
9454 * iocb off txcmplq and call compl in case of IOCB_ERROR.
James Smart07951072007-04-25 09:51:38 -04009455 */
James Smart2e0fef82007-06-17 19:56:36 -05009456 return retval;
dea31012005-04-17 16:05:31 -05009457}
9458
James Smarte59058c2008-08-24 21:49:00 -04009459/**
James Smart5af5eee2010-10-22 11:06:38 -04009460 * lpfc_sli_iocb_ring_abort - Unconditionally abort all iocbs on an iocb ring
9461 * @phba: Pointer to HBA context object.
9462 * @pring: Pointer to driver SLI ring object.
9463 *
9464 * This function aborts all iocbs in the given ring and frees all the iocb
9465 * objects in txq. This function issues abort iocbs unconditionally for all
9466 * the iocb commands in txcmplq. The iocbs in the txcmplq is not guaranteed
9467 * to complete before the return of this function. The caller is not required
9468 * to hold any locks.
9469 **/
9470static void
9471lpfc_sli_iocb_ring_abort(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
9472{
9473 LIST_HEAD(completions);
9474 struct lpfc_iocbq *iocb, *next_iocb;
9475
9476 if (pring->ringno == LPFC_ELS_RING)
9477 lpfc_fabric_abort_hba(phba);
9478
9479 spin_lock_irq(&phba->hbalock);
9480
9481 /* Take off all the iocbs on txq for cancelling */
9482 list_splice_init(&pring->txq, &completions);
9483 pring->txq_cnt = 0;
9484
9485 /* Next issue ABTS for everything on the txcmplq */
9486 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
9487 lpfc_sli_abort_iotag_issue(phba, pring, iocb);
9488
9489 spin_unlock_irq(&phba->hbalock);
9490
9491 /* Cancel all the IOCBs from the completions list */
9492 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
9493 IOERR_SLI_ABORTED);
9494}
9495
9496/**
9497 * lpfc_sli_hba_iocb_abort - Abort all iocbs to an hba.
9498 * @phba: pointer to lpfc HBA data structure.
9499 *
9500 * This routine will abort all pending and outstanding iocbs to an HBA.
9501 **/
9502void
9503lpfc_sli_hba_iocb_abort(struct lpfc_hba *phba)
9504{
9505 struct lpfc_sli *psli = &phba->sli;
9506 struct lpfc_sli_ring *pring;
9507 int i;
9508
9509 for (i = 0; i < psli->num_rings; i++) {
9510 pring = &psli->ring[i];
9511 lpfc_sli_iocb_ring_abort(phba, pring);
9512 }
9513}
9514
9515/**
James Smart3621a712009-04-06 18:47:14 -04009516 * lpfc_sli_validate_fcp_iocb - find commands associated with a vport or LUN
James Smarte59058c2008-08-24 21:49:00 -04009517 * @iocbq: Pointer to driver iocb object.
9518 * @vport: Pointer to driver virtual port object.
9519 * @tgt_id: SCSI ID of the target.
9520 * @lun_id: LUN ID of the scsi device.
9521 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST
9522 *
James Smart3621a712009-04-06 18:47:14 -04009523 * This function acts as an iocb filter for functions which abort or count
James Smarte59058c2008-08-24 21:49:00 -04009524 * all FCP iocbs pending on a lun/SCSI target/SCSI host. It will return
9525 * 0 if the filtering criteria is met for the given iocb and will return
9526 * 1 if the filtering criteria is not met.
9527 * If ctx_cmd == LPFC_CTX_LUN, the function returns 0 only if the
9528 * given iocb is for the SCSI device specified by vport, tgt_id and
9529 * lun_id parameter.
9530 * If ctx_cmd == LPFC_CTX_TGT, the function returns 0 only if the
9531 * given iocb is for the SCSI target specified by vport and tgt_id
9532 * parameters.
9533 * If ctx_cmd == LPFC_CTX_HOST, the function returns 0 only if the
9534 * given iocb is for the SCSI host associated with the given vport.
9535 * This function is called with no locks held.
9536 **/
dea31012005-04-17 16:05:31 -05009537static int
James Smart51ef4c22007-08-02 11:10:31 -04009538lpfc_sli_validate_fcp_iocb(struct lpfc_iocbq *iocbq, struct lpfc_vport *vport,
9539 uint16_t tgt_id, uint64_t lun_id,
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009540 lpfc_ctx_cmd ctx_cmd)
dea31012005-04-17 16:05:31 -05009541{
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009542 struct lpfc_scsi_buf *lpfc_cmd;
dea31012005-04-17 16:05:31 -05009543 int rc = 1;
9544
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009545 if (!(iocbq->iocb_flag & LPFC_IO_FCP))
9546 return rc;
9547
James Smart51ef4c22007-08-02 11:10:31 -04009548 if (iocbq->vport != vport)
9549 return rc;
9550
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009551 lpfc_cmd = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009552
James Smart495a7142008-06-14 22:52:59 -04009553 if (lpfc_cmd->pCmd == NULL)
dea31012005-04-17 16:05:31 -05009554 return rc;
9555
9556 switch (ctx_cmd) {
9557 case LPFC_CTX_LUN:
James Smart495a7142008-06-14 22:52:59 -04009558 if ((lpfc_cmd->rdata->pnode) &&
9559 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id) &&
9560 (scsilun_to_int(&lpfc_cmd->fcp_cmnd->fcp_lun) == lun_id))
dea31012005-04-17 16:05:31 -05009561 rc = 0;
9562 break;
9563 case LPFC_CTX_TGT:
James Smart495a7142008-06-14 22:52:59 -04009564 if ((lpfc_cmd->rdata->pnode) &&
9565 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id))
dea31012005-04-17 16:05:31 -05009566 rc = 0;
9567 break;
dea31012005-04-17 16:05:31 -05009568 case LPFC_CTX_HOST:
9569 rc = 0;
9570 break;
9571 default:
9572 printk(KERN_ERR "%s: Unknown context cmd type, value %d\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07009573 __func__, ctx_cmd);
dea31012005-04-17 16:05:31 -05009574 break;
9575 }
9576
9577 return rc;
9578}
9579
James Smarte59058c2008-08-24 21:49:00 -04009580/**
James Smart3621a712009-04-06 18:47:14 -04009581 * lpfc_sli_sum_iocb - Function to count the number of FCP iocbs pending
James Smarte59058c2008-08-24 21:49:00 -04009582 * @vport: Pointer to virtual port.
9583 * @tgt_id: SCSI ID of the target.
9584 * @lun_id: LUN ID of the scsi device.
9585 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
9586 *
9587 * This function returns number of FCP commands pending for the vport.
9588 * When ctx_cmd == LPFC_CTX_LUN, the function returns number of FCP
9589 * commands pending on the vport associated with SCSI device specified
9590 * by tgt_id and lun_id parameters.
9591 * When ctx_cmd == LPFC_CTX_TGT, the function returns number of FCP
9592 * commands pending on the vport associated with SCSI target specified
9593 * by tgt_id parameter.
9594 * When ctx_cmd == LPFC_CTX_HOST, the function returns number of FCP
9595 * commands pending on the vport.
9596 * This function returns the number of iocbs which satisfy the filter.
9597 * This function is called without any lock held.
9598 **/
dea31012005-04-17 16:05:31 -05009599int
James Smart51ef4c22007-08-02 11:10:31 -04009600lpfc_sli_sum_iocb(struct lpfc_vport *vport, uint16_t tgt_id, uint64_t lun_id,
9601 lpfc_ctx_cmd ctx_cmd)
dea31012005-04-17 16:05:31 -05009602{
James Smart51ef4c22007-08-02 11:10:31 -04009603 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009604 struct lpfc_iocbq *iocbq;
9605 int sum, i;
dea31012005-04-17 16:05:31 -05009606
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009607 for (i = 1, sum = 0; i <= phba->sli.last_iotag; i++) {
9608 iocbq = phba->sli.iocbq_lookup[i];
dea31012005-04-17 16:05:31 -05009609
James Smart51ef4c22007-08-02 11:10:31 -04009610 if (lpfc_sli_validate_fcp_iocb (iocbq, vport, tgt_id, lun_id,
9611 ctx_cmd) == 0)
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009612 sum++;
dea31012005-04-17 16:05:31 -05009613 }
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009614
dea31012005-04-17 16:05:31 -05009615 return sum;
9616}
9617
James Smarte59058c2008-08-24 21:49:00 -04009618/**
James Smart3621a712009-04-06 18:47:14 -04009619 * lpfc_sli_abort_fcp_cmpl - Completion handler function for aborted FCP IOCBs
James Smarte59058c2008-08-24 21:49:00 -04009620 * @phba: Pointer to HBA context object
9621 * @cmdiocb: Pointer to command iocb object.
9622 * @rspiocb: Pointer to response iocb object.
9623 *
9624 * This function is called when an aborted FCP iocb completes. This
9625 * function is called by the ring event handler with no lock held.
9626 * This function frees the iocb.
9627 **/
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -04009628void
James Smart2e0fef82007-06-17 19:56:36 -05009629lpfc_sli_abort_fcp_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
9630 struct lpfc_iocbq *rspiocb)
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -04009631{
James Smartcb69f7d2011-12-13 13:21:57 -05009632 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
9633 "3096 ABORT_XRI_CN completing on xri x%x "
9634 "original iotag x%x, abort cmd iotag x%x "
9635 "status 0x%x, reason 0x%x\n",
9636 cmdiocb->iocb.un.acxri.abortContextTag,
9637 cmdiocb->iocb.un.acxri.abortIoTag,
9638 cmdiocb->iotag, rspiocb->iocb.ulpStatus,
9639 rspiocb->iocb.un.ulpWord[4]);
James Bottomley604a3e32005-10-29 10:28:33 -05009640 lpfc_sli_release_iocbq(phba, cmdiocb);
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -04009641 return;
9642}
9643
James Smarte59058c2008-08-24 21:49:00 -04009644/**
James Smart3621a712009-04-06 18:47:14 -04009645 * lpfc_sli_abort_iocb - issue abort for all commands on a host/target/LUN
James Smarte59058c2008-08-24 21:49:00 -04009646 * @vport: Pointer to virtual port.
9647 * @pring: Pointer to driver SLI ring object.
9648 * @tgt_id: SCSI ID of the target.
9649 * @lun_id: LUN ID of the scsi device.
9650 * @abort_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
9651 *
9652 * This function sends an abort command for every SCSI command
9653 * associated with the given virtual port pending on the ring
9654 * filtered by lpfc_sli_validate_fcp_iocb function.
9655 * When abort_cmd == LPFC_CTX_LUN, the function sends abort only to the
9656 * FCP iocbs associated with lun specified by tgt_id and lun_id
9657 * parameters
9658 * When abort_cmd == LPFC_CTX_TGT, the function sends abort only to the
9659 * FCP iocbs associated with SCSI target specified by tgt_id parameter.
9660 * When abort_cmd == LPFC_CTX_HOST, the function sends abort to all
9661 * FCP iocbs associated with virtual port.
9662 * This function returns number of iocbs it failed to abort.
9663 * This function is called with no locks held.
9664 **/
dea31012005-04-17 16:05:31 -05009665int
James Smart51ef4c22007-08-02 11:10:31 -04009666lpfc_sli_abort_iocb(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
9667 uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd abort_cmd)
dea31012005-04-17 16:05:31 -05009668{
James Smart51ef4c22007-08-02 11:10:31 -04009669 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009670 struct lpfc_iocbq *iocbq;
9671 struct lpfc_iocbq *abtsiocb;
dea31012005-04-17 16:05:31 -05009672 IOCB_t *cmd = NULL;
dea31012005-04-17 16:05:31 -05009673 int errcnt = 0, ret_val = 0;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009674 int i;
dea31012005-04-17 16:05:31 -05009675
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009676 for (i = 1; i <= phba->sli.last_iotag; i++) {
9677 iocbq = phba->sli.iocbq_lookup[i];
dea31012005-04-17 16:05:31 -05009678
James Smart51ef4c22007-08-02 11:10:31 -04009679 if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
James Smart2e0fef82007-06-17 19:56:36 -05009680 abort_cmd) != 0)
dea31012005-04-17 16:05:31 -05009681 continue;
9682
9683 /* issue ABTS for this IOCB based on iotag */
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009684 abtsiocb = lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05009685 if (abtsiocb == NULL) {
9686 errcnt++;
9687 continue;
9688 }
dea31012005-04-17 16:05:31 -05009689
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009690 cmd = &iocbq->iocb;
dea31012005-04-17 16:05:31 -05009691 abtsiocb->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
9692 abtsiocb->iocb.un.acxri.abortContextTag = cmd->ulpContext;
James Smartda0436e2009-05-22 14:51:39 -04009693 if (phba->sli_rev == LPFC_SLI_REV4)
9694 abtsiocb->iocb.un.acxri.abortIoTag = iocbq->sli4_xritag;
9695 else
9696 abtsiocb->iocb.un.acxri.abortIoTag = cmd->ulpIoTag;
dea31012005-04-17 16:05:31 -05009697 abtsiocb->iocb.ulpLe = 1;
9698 abtsiocb->iocb.ulpClass = cmd->ulpClass;
James Smart2e0fef82007-06-17 19:56:36 -05009699 abtsiocb->vport = phba->pport;
dea31012005-04-17 16:05:31 -05009700
James Smart5ffc2662009-11-18 15:39:44 -05009701 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
9702 abtsiocb->fcp_wqidx = iocbq->fcp_wqidx;
James Smart341af102010-01-26 23:07:37 -05009703 if (iocbq->iocb_flag & LPFC_IO_FCP)
9704 abtsiocb->iocb_flag |= LPFC_USE_FCPWQIDX;
James Smart5ffc2662009-11-18 15:39:44 -05009705
James Smart2e0fef82007-06-17 19:56:36 -05009706 if (lpfc_is_link_up(phba))
dea31012005-04-17 16:05:31 -05009707 abtsiocb->iocb.ulpCommand = CMD_ABORT_XRI_CN;
9708 else
9709 abtsiocb->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
9710
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -04009711 /* Setup callback routine and issue the command. */
9712 abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
James Smartda0436e2009-05-22 14:51:39 -04009713 ret_val = lpfc_sli_issue_iocb(phba, pring->ringno,
9714 abtsiocb, 0);
dea31012005-04-17 16:05:31 -05009715 if (ret_val == IOCB_ERROR) {
James Bottomley604a3e32005-10-29 10:28:33 -05009716 lpfc_sli_release_iocbq(phba, abtsiocb);
dea31012005-04-17 16:05:31 -05009717 errcnt++;
9718 continue;
9719 }
9720 }
9721
9722 return errcnt;
9723}
9724
James Smarte59058c2008-08-24 21:49:00 -04009725/**
James Smart3621a712009-04-06 18:47:14 -04009726 * lpfc_sli_wake_iocb_wait - lpfc_sli_issue_iocb_wait's completion handler
James Smarte59058c2008-08-24 21:49:00 -04009727 * @phba: Pointer to HBA context object.
9728 * @cmdiocbq: Pointer to command iocb.
9729 * @rspiocbq: Pointer to response iocb.
9730 *
9731 * This function is the completion handler for iocbs issued using
9732 * lpfc_sli_issue_iocb_wait function. This function is called by the
9733 * ring event handler function without any lock held. This function
9734 * can be called from both worker thread context and interrupt
9735 * context. This function also can be called from other thread which
9736 * cleans up the SLI layer objects.
9737 * This function copy the contents of the response iocb to the
9738 * response iocb memory object provided by the caller of
9739 * lpfc_sli_issue_iocb_wait and then wakes up the thread which
9740 * sleeps for the iocb completion.
9741 **/
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009742static void
9743lpfc_sli_wake_iocb_wait(struct lpfc_hba *phba,
9744 struct lpfc_iocbq *cmdiocbq,
9745 struct lpfc_iocbq *rspiocbq)
dea31012005-04-17 16:05:31 -05009746{
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009747 wait_queue_head_t *pdone_q;
9748 unsigned long iflags;
James Smart0f65ff62010-02-26 14:14:23 -05009749 struct lpfc_scsi_buf *lpfc_cmd;
dea31012005-04-17 16:05:31 -05009750
James Smart2e0fef82007-06-17 19:56:36 -05009751 spin_lock_irqsave(&phba->hbalock, iflags);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009752 cmdiocbq->iocb_flag |= LPFC_IO_WAKE;
9753 if (cmdiocbq->context2 && rspiocbq)
9754 memcpy(&((struct lpfc_iocbq *)cmdiocbq->context2)->iocb,
9755 &rspiocbq->iocb, sizeof(IOCB_t));
9756
James Smart0f65ff62010-02-26 14:14:23 -05009757 /* Set the exchange busy flag for task management commands */
9758 if ((cmdiocbq->iocb_flag & LPFC_IO_FCP) &&
9759 !(cmdiocbq->iocb_flag & LPFC_IO_LIBDFC)) {
9760 lpfc_cmd = container_of(cmdiocbq, struct lpfc_scsi_buf,
9761 cur_iocbq);
9762 lpfc_cmd->exch_busy = rspiocbq->iocb_flag & LPFC_EXCHANGE_BUSY;
9763 }
9764
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009765 pdone_q = cmdiocbq->context_un.wait_queue;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009766 if (pdone_q)
9767 wake_up(pdone_q);
James Smart858c9f62007-06-17 19:56:39 -05009768 spin_unlock_irqrestore(&phba->hbalock, iflags);
dea31012005-04-17 16:05:31 -05009769 return;
9770}
9771
James Smarte59058c2008-08-24 21:49:00 -04009772/**
James Smartd11e31d2009-06-10 17:23:06 -04009773 * lpfc_chk_iocb_flg - Test IOCB flag with lock held.
9774 * @phba: Pointer to HBA context object..
9775 * @piocbq: Pointer to command iocb.
9776 * @flag: Flag to test.
9777 *
9778 * This routine grabs the hbalock and then test the iocb_flag to
9779 * see if the passed in flag is set.
9780 * Returns:
9781 * 1 if flag is set.
9782 * 0 if flag is not set.
9783 **/
9784static int
9785lpfc_chk_iocb_flg(struct lpfc_hba *phba,
9786 struct lpfc_iocbq *piocbq, uint32_t flag)
9787{
9788 unsigned long iflags;
9789 int ret;
9790
9791 spin_lock_irqsave(&phba->hbalock, iflags);
9792 ret = piocbq->iocb_flag & flag;
9793 spin_unlock_irqrestore(&phba->hbalock, iflags);
9794 return ret;
9795
9796}
9797
9798/**
James Smart3621a712009-04-06 18:47:14 -04009799 * lpfc_sli_issue_iocb_wait - Synchronous function to issue iocb commands
James Smarte59058c2008-08-24 21:49:00 -04009800 * @phba: Pointer to HBA context object..
9801 * @pring: Pointer to sli ring.
9802 * @piocb: Pointer to command iocb.
9803 * @prspiocbq: Pointer to response iocb.
9804 * @timeout: Timeout in number of seconds.
9805 *
9806 * This function issues the iocb to firmware and waits for the
9807 * iocb to complete. If the iocb command is not
9808 * completed within timeout seconds, it returns IOCB_TIMEDOUT.
9809 * Caller should not free the iocb resources if this function
9810 * returns IOCB_TIMEDOUT.
9811 * The function waits for the iocb completion using an
9812 * non-interruptible wait.
9813 * This function will sleep while waiting for iocb completion.
9814 * So, this function should not be called from any context which
9815 * does not allow sleeping. Due to the same reason, this function
9816 * cannot be called with interrupt disabled.
9817 * This function assumes that the iocb completions occur while
9818 * this function sleep. So, this function cannot be called from
9819 * the thread which process iocb completion for this ring.
9820 * This function clears the iocb_flag of the iocb object before
9821 * issuing the iocb and the iocb completion handler sets this
9822 * flag and wakes this thread when the iocb completes.
9823 * The contents of the response iocb will be copied to prspiocbq
9824 * by the completion handler when the command completes.
9825 * This function returns IOCB_SUCCESS when success.
9826 * This function is called with no lock held.
9827 **/
dea31012005-04-17 16:05:31 -05009828int
James Smart2e0fef82007-06-17 19:56:36 -05009829lpfc_sli_issue_iocb_wait(struct lpfc_hba *phba,
James Smartda0436e2009-05-22 14:51:39 -04009830 uint32_t ring_number,
James Smart2e0fef82007-06-17 19:56:36 -05009831 struct lpfc_iocbq *piocb,
9832 struct lpfc_iocbq *prspiocbq,
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009833 uint32_t timeout)
dea31012005-04-17 16:05:31 -05009834{
Peter Zijlstra7259f0d2006-10-29 22:46:36 -08009835 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009836 long timeleft, timeout_req = 0;
9837 int retval = IOCB_SUCCESS;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05009838 uint32_t creg_val;
James Smart2a9bf3d2010-06-07 15:24:45 -04009839 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
dea31012005-04-17 16:05:31 -05009840 /*
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009841 * If the caller has provided a response iocbq buffer, then context2
9842 * is NULL or its an error.
dea31012005-04-17 16:05:31 -05009843 */
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009844 if (prspiocbq) {
9845 if (piocb->context2)
9846 return IOCB_ERROR;
9847 piocb->context2 = prspiocbq;
dea31012005-04-17 16:05:31 -05009848 }
9849
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009850 piocb->iocb_cmpl = lpfc_sli_wake_iocb_wait;
9851 piocb->context_un.wait_queue = &done_q;
9852 piocb->iocb_flag &= ~LPFC_IO_WAKE;
dea31012005-04-17 16:05:31 -05009853
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05009854 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
James Smart9940b972011-03-11 16:06:12 -05009855 if (lpfc_readl(phba->HCregaddr, &creg_val))
9856 return IOCB_ERROR;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05009857 creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
9858 writel(creg_val, phba->HCregaddr);
9859 readl(phba->HCregaddr); /* flush */
9860 }
9861
James Smart2a9bf3d2010-06-07 15:24:45 -04009862 retval = lpfc_sli_issue_iocb(phba, ring_number, piocb,
9863 SLI_IOCB_RET_IOCB);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009864 if (retval == IOCB_SUCCESS) {
9865 timeout_req = timeout * HZ;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009866 timeleft = wait_event_timeout(done_q,
James Smartd11e31d2009-06-10 17:23:06 -04009867 lpfc_chk_iocb_flg(phba, piocb, LPFC_IO_WAKE),
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009868 timeout_req);
dea31012005-04-17 16:05:31 -05009869
James Smart7054a602007-04-25 09:52:34 -04009870 if (piocb->iocb_flag & LPFC_IO_WAKE) {
9871 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04009872 "0331 IOCB wake signaled\n");
James Smart7054a602007-04-25 09:52:34 -04009873 } else if (timeleft == 0) {
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009874 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04009875 "0338 IOCB wait timeout error - no "
9876 "wake response Data x%x\n", timeout);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009877 retval = IOCB_TIMEDOUT;
James Smart7054a602007-04-25 09:52:34 -04009878 } else {
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009879 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04009880 "0330 IOCB wake NOT set, "
9881 "Data x%x x%lx\n",
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009882 timeout, (timeleft / jiffies));
9883 retval = IOCB_TIMEDOUT;
dea31012005-04-17 16:05:31 -05009884 }
James Smart2a9bf3d2010-06-07 15:24:45 -04009885 } else if (retval == IOCB_BUSY) {
9886 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
9887 "2818 Max IOCBs %d txq cnt %d txcmplq cnt %d\n",
9888 phba->iocb_cnt, pring->txq_cnt, pring->txcmplq_cnt);
9889 return retval;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009890 } else {
9891 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smartd7c255b2008-08-24 21:50:00 -04009892 "0332 IOCB wait issue failed, Data x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04009893 retval);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009894 retval = IOCB_ERROR;
dea31012005-04-17 16:05:31 -05009895 }
9896
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05009897 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
James Smart9940b972011-03-11 16:06:12 -05009898 if (lpfc_readl(phba->HCregaddr, &creg_val))
9899 return IOCB_ERROR;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05009900 creg_val &= ~(HC_R0INT_ENA << LPFC_FCP_RING);
9901 writel(creg_val, phba->HCregaddr);
9902 readl(phba->HCregaddr); /* flush */
9903 }
9904
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009905 if (prspiocbq)
9906 piocb->context2 = NULL;
9907
9908 piocb->context_un.wait_queue = NULL;
9909 piocb->iocb_cmpl = NULL;
dea31012005-04-17 16:05:31 -05009910 return retval;
9911}
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009912
James Smarte59058c2008-08-24 21:49:00 -04009913/**
James Smart3621a712009-04-06 18:47:14 -04009914 * lpfc_sli_issue_mbox_wait - Synchronous function to issue mailbox
James Smarte59058c2008-08-24 21:49:00 -04009915 * @phba: Pointer to HBA context object.
9916 * @pmboxq: Pointer to driver mailbox object.
9917 * @timeout: Timeout in number of seconds.
9918 *
9919 * This function issues the mailbox to firmware and waits for the
9920 * mailbox command to complete. If the mailbox command is not
9921 * completed within timeout seconds, it returns MBX_TIMEOUT.
9922 * The function waits for the mailbox completion using an
9923 * interruptible wait. If the thread is woken up due to a
9924 * signal, MBX_TIMEOUT error is returned to the caller. Caller
9925 * should not free the mailbox resources, if this function returns
9926 * MBX_TIMEOUT.
9927 * This function will sleep while waiting for mailbox completion.
9928 * So, this function should not be called from any context which
9929 * does not allow sleeping. Due to the same reason, this function
9930 * cannot be called with interrupt disabled.
9931 * This function assumes that the mailbox completion occurs while
9932 * this function sleep. So, this function cannot be called from
9933 * the worker thread which processes mailbox completion.
9934 * This function is called in the context of HBA management
9935 * applications.
9936 * This function returns MBX_SUCCESS when successful.
9937 * This function is called with no lock held.
9938 **/
dea31012005-04-17 16:05:31 -05009939int
James Smart2e0fef82007-06-17 19:56:36 -05009940lpfc_sli_issue_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq,
dea31012005-04-17 16:05:31 -05009941 uint32_t timeout)
9942{
Peter Zijlstra7259f0d2006-10-29 22:46:36 -08009943 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
dea31012005-04-17 16:05:31 -05009944 int retval;
James Smart858c9f62007-06-17 19:56:39 -05009945 unsigned long flag;
dea31012005-04-17 16:05:31 -05009946
9947 /* The caller must leave context1 empty. */
James Smart98c9ea52007-10-27 13:37:33 -04009948 if (pmboxq->context1)
James Smart2e0fef82007-06-17 19:56:36 -05009949 return MBX_NOT_FINISHED;
dea31012005-04-17 16:05:31 -05009950
James Smart495a7142008-06-14 22:52:59 -04009951 pmboxq->mbox_flag &= ~LPFC_MBX_WAKE;
dea31012005-04-17 16:05:31 -05009952 /* setup wake call as IOCB callback */
9953 pmboxq->mbox_cmpl = lpfc_sli_wake_mbox_wait;
9954 /* setup context field to pass wait_queue pointer to wake function */
9955 pmboxq->context1 = &done_q;
9956
dea31012005-04-17 16:05:31 -05009957 /* now issue the command */
9958 retval = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
dea31012005-04-17 16:05:31 -05009959 if (retval == MBX_BUSY || retval == MBX_SUCCESS) {
James Smart7054a602007-04-25 09:52:34 -04009960 wait_event_interruptible_timeout(done_q,
9961 pmboxq->mbox_flag & LPFC_MBX_WAKE,
9962 timeout * HZ);
9963
James Smart858c9f62007-06-17 19:56:39 -05009964 spin_lock_irqsave(&phba->hbalock, flag);
dea31012005-04-17 16:05:31 -05009965 pmboxq->context1 = NULL;
James Smart7054a602007-04-25 09:52:34 -04009966 /*
9967 * if LPFC_MBX_WAKE flag is set the mailbox is completed
9968 * else do not free the resources.
9969 */
James Smartd7c47992010-06-08 18:31:54 -04009970 if (pmboxq->mbox_flag & LPFC_MBX_WAKE) {
dea31012005-04-17 16:05:31 -05009971 retval = MBX_SUCCESS;
James Smartd7c47992010-06-08 18:31:54 -04009972 lpfc_sli4_swap_str(phba, pmboxq);
9973 } else {
James Smart7054a602007-04-25 09:52:34 -04009974 retval = MBX_TIMEOUT;
James Smart858c9f62007-06-17 19:56:39 -05009975 pmboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
9976 }
9977 spin_unlock_irqrestore(&phba->hbalock, flag);
dea31012005-04-17 16:05:31 -05009978 }
9979
dea31012005-04-17 16:05:31 -05009980 return retval;
9981}
9982
James Smarte59058c2008-08-24 21:49:00 -04009983/**
James Smart3772a992009-05-22 14:50:54 -04009984 * lpfc_sli_mbox_sys_shutdown - shutdown mailbox command sub-system
James Smarte59058c2008-08-24 21:49:00 -04009985 * @phba: Pointer to HBA context.
9986 *
James Smart3772a992009-05-22 14:50:54 -04009987 * This function is called to shutdown the driver's mailbox sub-system.
9988 * It first marks the mailbox sub-system is in a block state to prevent
9989 * the asynchronous mailbox command from issued off the pending mailbox
9990 * command queue. If the mailbox command sub-system shutdown is due to
9991 * HBA error conditions such as EEH or ERATT, this routine shall invoke
9992 * the mailbox sub-system flush routine to forcefully bring down the
9993 * mailbox sub-system. Otherwise, if it is due to normal condition (such
9994 * as with offline or HBA function reset), this routine will wait for the
9995 * outstanding mailbox command to complete before invoking the mailbox
9996 * sub-system flush routine to gracefully bring down mailbox sub-system.
James Smarte59058c2008-08-24 21:49:00 -04009997 **/
James Smart3772a992009-05-22 14:50:54 -04009998void
James Smart618a5232012-06-12 13:54:36 -04009999lpfc_sli_mbox_sys_shutdown(struct lpfc_hba *phba, int mbx_action)
James Smartb4c02652006-07-06 15:50:43 -040010000{
James Smart3772a992009-05-22 14:50:54 -040010001 struct lpfc_sli *psli = &phba->sli;
James Smart3772a992009-05-22 14:50:54 -040010002 unsigned long timeout;
10003
James Smart618a5232012-06-12 13:54:36 -040010004 if (mbx_action == LPFC_MBX_NO_WAIT) {
10005 /* delay 100ms for port state */
10006 msleep(100);
10007 lpfc_sli_mbox_sys_flush(phba);
10008 return;
10009 }
James Smarta183a152011-10-10 21:32:43 -040010010 timeout = msecs_to_jiffies(LPFC_MBOX_TMO * 1000) + jiffies;
James Smartd7069f02012-03-01 22:36:29 -050010011
James Smart3772a992009-05-22 14:50:54 -040010012 spin_lock_irq(&phba->hbalock);
10013 psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
James Smart3772a992009-05-22 14:50:54 -040010014
10015 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
James Smart3772a992009-05-22 14:50:54 -040010016 /* Determine how long we might wait for the active mailbox
10017 * command to be gracefully completed by firmware.
10018 */
James Smarta183a152011-10-10 21:32:43 -040010019 if (phba->sli.mbox_active)
10020 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
10021 phba->sli.mbox_active) *
10022 1000) + jiffies;
10023 spin_unlock_irq(&phba->hbalock);
10024
James Smart3772a992009-05-22 14:50:54 -040010025 while (phba->sli.mbox_active) {
10026 /* Check active mailbox complete status every 2ms */
10027 msleep(2);
10028 if (time_after(jiffies, timeout))
10029 /* Timeout, let the mailbox flush routine to
10030 * forcefully release active mailbox command
10031 */
10032 break;
10033 }
James Smartd7069f02012-03-01 22:36:29 -050010034 } else
10035 spin_unlock_irq(&phba->hbalock);
10036
James Smart3772a992009-05-22 14:50:54 -040010037 lpfc_sli_mbox_sys_flush(phba);
10038}
10039
10040/**
10041 * lpfc_sli_eratt_read - read sli-3 error attention events
10042 * @phba: Pointer to HBA context.
10043 *
10044 * This function is called to read the SLI3 device error attention registers
10045 * for possible error attention events. The caller must hold the hostlock
10046 * with spin_lock_irq().
10047 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030010048 * This function returns 1 when there is Error Attention in the Host Attention
James Smart3772a992009-05-22 14:50:54 -040010049 * Register and returns 0 otherwise.
10050 **/
10051static int
10052lpfc_sli_eratt_read(struct lpfc_hba *phba)
10053{
James Smarted957682007-06-17 19:56:37 -050010054 uint32_t ha_copy;
James Smartb4c02652006-07-06 15:50:43 -040010055
James Smart3772a992009-05-22 14:50:54 -040010056 /* Read chip Host Attention (HA) register */
James Smart9940b972011-03-11 16:06:12 -050010057 if (lpfc_readl(phba->HAregaddr, &ha_copy))
10058 goto unplug_err;
10059
James Smart3772a992009-05-22 14:50:54 -040010060 if (ha_copy & HA_ERATT) {
10061 /* Read host status register to retrieve error event */
James Smart9940b972011-03-11 16:06:12 -050010062 if (lpfc_sli_read_hs(phba))
10063 goto unplug_err;
James Smartb4c02652006-07-06 15:50:43 -040010064
James Smart3772a992009-05-22 14:50:54 -040010065 /* Check if there is a deferred error condition is active */
10066 if ((HS_FFER1 & phba->work_hs) &&
10067 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
James Smartdcf2a4e2010-09-29 11:18:53 -040010068 HS_FFER6 | HS_FFER7 | HS_FFER8) & phba->work_hs)) {
James Smart3772a992009-05-22 14:50:54 -040010069 phba->hba_flag |= DEFER_ERATT;
James Smart3772a992009-05-22 14:50:54 -040010070 /* Clear all interrupt enable conditions */
10071 writel(0, phba->HCregaddr);
10072 readl(phba->HCregaddr);
10073 }
10074
10075 /* Set the driver HA work bitmap */
James Smart3772a992009-05-22 14:50:54 -040010076 phba->work_ha |= HA_ERATT;
10077 /* Indicate polling handles this ERATT */
10078 phba->hba_flag |= HBA_ERATT_HANDLED;
James Smart3772a992009-05-22 14:50:54 -040010079 return 1;
James Smartb4c02652006-07-06 15:50:43 -040010080 }
James Smart3772a992009-05-22 14:50:54 -040010081 return 0;
James Smart9940b972011-03-11 16:06:12 -050010082
10083unplug_err:
10084 /* Set the driver HS work bitmap */
10085 phba->work_hs |= UNPLUG_ERR;
10086 /* Set the driver HA work bitmap */
10087 phba->work_ha |= HA_ERATT;
10088 /* Indicate polling handles this ERATT */
10089 phba->hba_flag |= HBA_ERATT_HANDLED;
10090 return 1;
James Smartb4c02652006-07-06 15:50:43 -040010091}
10092
James Smarte59058c2008-08-24 21:49:00 -040010093/**
James Smartda0436e2009-05-22 14:51:39 -040010094 * lpfc_sli4_eratt_read - read sli-4 error attention events
10095 * @phba: Pointer to HBA context.
10096 *
10097 * This function is called to read the SLI4 device error attention registers
10098 * for possible error attention events. The caller must hold the hostlock
10099 * with spin_lock_irq().
10100 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030010101 * This function returns 1 when there is Error Attention in the Host Attention
James Smartda0436e2009-05-22 14:51:39 -040010102 * Register and returns 0 otherwise.
10103 **/
10104static int
10105lpfc_sli4_eratt_read(struct lpfc_hba *phba)
10106{
10107 uint32_t uerr_sta_hi, uerr_sta_lo;
James Smart2fcee4b2010-12-15 17:57:46 -050010108 uint32_t if_type, portsmphr;
10109 struct lpfc_register portstat_reg;
James Smartda0436e2009-05-22 14:51:39 -040010110
James Smart2fcee4b2010-12-15 17:57:46 -050010111 /*
10112 * For now, use the SLI4 device internal unrecoverable error
James Smartda0436e2009-05-22 14:51:39 -040010113 * registers for error attention. This can be changed later.
10114 */
James Smart2fcee4b2010-12-15 17:57:46 -050010115 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
10116 switch (if_type) {
10117 case LPFC_SLI_INTF_IF_TYPE_0:
James Smart9940b972011-03-11 16:06:12 -050010118 if (lpfc_readl(phba->sli4_hba.u.if_type0.UERRLOregaddr,
10119 &uerr_sta_lo) ||
10120 lpfc_readl(phba->sli4_hba.u.if_type0.UERRHIregaddr,
10121 &uerr_sta_hi)) {
10122 phba->work_hs |= UNPLUG_ERR;
10123 phba->work_ha |= HA_ERATT;
10124 phba->hba_flag |= HBA_ERATT_HANDLED;
10125 return 1;
10126 }
James Smart2fcee4b2010-12-15 17:57:46 -050010127 if ((~phba->sli4_hba.ue_mask_lo & uerr_sta_lo) ||
10128 (~phba->sli4_hba.ue_mask_hi & uerr_sta_hi)) {
10129 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10130 "1423 HBA Unrecoverable error: "
10131 "uerr_lo_reg=0x%x, uerr_hi_reg=0x%x, "
10132 "ue_mask_lo_reg=0x%x, "
10133 "ue_mask_hi_reg=0x%x\n",
10134 uerr_sta_lo, uerr_sta_hi,
10135 phba->sli4_hba.ue_mask_lo,
10136 phba->sli4_hba.ue_mask_hi);
10137 phba->work_status[0] = uerr_sta_lo;
10138 phba->work_status[1] = uerr_sta_hi;
10139 phba->work_ha |= HA_ERATT;
10140 phba->hba_flag |= HBA_ERATT_HANDLED;
10141 return 1;
10142 }
10143 break;
10144 case LPFC_SLI_INTF_IF_TYPE_2:
James Smart9940b972011-03-11 16:06:12 -050010145 if (lpfc_readl(phba->sli4_hba.u.if_type2.STATUSregaddr,
10146 &portstat_reg.word0) ||
10147 lpfc_readl(phba->sli4_hba.PSMPHRregaddr,
10148 &portsmphr)){
10149 phba->work_hs |= UNPLUG_ERR;
10150 phba->work_ha |= HA_ERATT;
10151 phba->hba_flag |= HBA_ERATT_HANDLED;
10152 return 1;
10153 }
James Smart2fcee4b2010-12-15 17:57:46 -050010154 if (bf_get(lpfc_sliport_status_err, &portstat_reg)) {
10155 phba->work_status[0] =
10156 readl(phba->sli4_hba.u.if_type2.ERR1regaddr);
10157 phba->work_status[1] =
10158 readl(phba->sli4_hba.u.if_type2.ERR2regaddr);
10159 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart2e90f4b2011-12-13 13:22:37 -050010160 "2885 Port Status Event: "
James Smart2fcee4b2010-12-15 17:57:46 -050010161 "port status reg 0x%x, "
10162 "port smphr reg 0x%x, "
10163 "error 1=0x%x, error 2=0x%x\n",
10164 portstat_reg.word0,
10165 portsmphr,
10166 phba->work_status[0],
10167 phba->work_status[1]);
10168 phba->work_ha |= HA_ERATT;
10169 phba->hba_flag |= HBA_ERATT_HANDLED;
10170 return 1;
10171 }
10172 break;
10173 case LPFC_SLI_INTF_IF_TYPE_1:
10174 default:
James Smarta747c9c2009-11-18 15:41:10 -050010175 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart2fcee4b2010-12-15 17:57:46 -050010176 "2886 HBA Error Attention on unsupported "
10177 "if type %d.", if_type);
James Smarta747c9c2009-11-18 15:41:10 -050010178 return 1;
James Smartda0436e2009-05-22 14:51:39 -040010179 }
James Smart2fcee4b2010-12-15 17:57:46 -050010180
James Smartda0436e2009-05-22 14:51:39 -040010181 return 0;
10182}
10183
10184/**
James Smart3621a712009-04-06 18:47:14 -040010185 * lpfc_sli_check_eratt - check error attention events
James Smart93996272008-08-24 21:50:30 -040010186 * @phba: Pointer to HBA context.
10187 *
James Smart3772a992009-05-22 14:50:54 -040010188 * This function is called from timer soft interrupt context to check HBA's
James Smart93996272008-08-24 21:50:30 -040010189 * error attention register bit for error attention events.
10190 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030010191 * This function returns 1 when there is Error Attention in the Host Attention
James Smart93996272008-08-24 21:50:30 -040010192 * Register and returns 0 otherwise.
10193 **/
10194int
10195lpfc_sli_check_eratt(struct lpfc_hba *phba)
10196{
10197 uint32_t ha_copy;
10198
10199 /* If somebody is waiting to handle an eratt, don't process it
10200 * here. The brdkill function will do this.
10201 */
10202 if (phba->link_flag & LS_IGNORE_ERATT)
10203 return 0;
10204
10205 /* Check if interrupt handler handles this ERATT */
10206 spin_lock_irq(&phba->hbalock);
10207 if (phba->hba_flag & HBA_ERATT_HANDLED) {
10208 /* Interrupt handler has handled ERATT */
10209 spin_unlock_irq(&phba->hbalock);
10210 return 0;
10211 }
10212
James Smarta257bf92009-04-06 18:48:10 -040010213 /*
10214 * If there is deferred error attention, do not check for error
10215 * attention
10216 */
10217 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
10218 spin_unlock_irq(&phba->hbalock);
10219 return 0;
10220 }
10221
James Smart3772a992009-05-22 14:50:54 -040010222 /* If PCI channel is offline, don't process it */
10223 if (unlikely(pci_channel_offline(phba->pcidev))) {
James Smart93996272008-08-24 21:50:30 -040010224 spin_unlock_irq(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -040010225 return 0;
10226 }
10227
10228 switch (phba->sli_rev) {
10229 case LPFC_SLI_REV2:
10230 case LPFC_SLI_REV3:
10231 /* Read chip Host Attention (HA) register */
10232 ha_copy = lpfc_sli_eratt_read(phba);
10233 break;
James Smartda0436e2009-05-22 14:51:39 -040010234 case LPFC_SLI_REV4:
James Smart2fcee4b2010-12-15 17:57:46 -050010235 /* Read device Uncoverable Error (UERR) registers */
James Smartda0436e2009-05-22 14:51:39 -040010236 ha_copy = lpfc_sli4_eratt_read(phba);
10237 break;
James Smart3772a992009-05-22 14:50:54 -040010238 default:
10239 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10240 "0299 Invalid SLI revision (%d)\n",
10241 phba->sli_rev);
10242 ha_copy = 0;
10243 break;
James Smart93996272008-08-24 21:50:30 -040010244 }
10245 spin_unlock_irq(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -040010246
10247 return ha_copy;
10248}
10249
10250/**
10251 * lpfc_intr_state_check - Check device state for interrupt handling
10252 * @phba: Pointer to HBA context.
10253 *
10254 * This inline routine checks whether a device or its PCI slot is in a state
10255 * that the interrupt should be handled.
10256 *
10257 * This function returns 0 if the device or the PCI slot is in a state that
10258 * interrupt should be handled, otherwise -EIO.
10259 */
10260static inline int
10261lpfc_intr_state_check(struct lpfc_hba *phba)
10262{
10263 /* If the pci channel is offline, ignore all the interrupts */
10264 if (unlikely(pci_channel_offline(phba->pcidev)))
10265 return -EIO;
10266
10267 /* Update device level interrupt statistics */
10268 phba->sli.slistat.sli_intr++;
10269
10270 /* Ignore all interrupts during initialization. */
10271 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
10272 return -EIO;
10273
James Smart93996272008-08-24 21:50:30 -040010274 return 0;
10275}
10276
10277/**
James Smart3772a992009-05-22 14:50:54 -040010278 * lpfc_sli_sp_intr_handler - Slow-path interrupt handler to SLI-3 device
James Smarte59058c2008-08-24 21:49:00 -040010279 * @irq: Interrupt number.
10280 * @dev_id: The device context pointer.
10281 *
James Smart93996272008-08-24 21:50:30 -040010282 * This function is directly called from the PCI layer as an interrupt
James Smart3772a992009-05-22 14:50:54 -040010283 * service routine when device with SLI-3 interface spec is enabled with
10284 * MSI-X multi-message interrupt mode and there are slow-path events in
10285 * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
10286 * interrupt mode, this function is called as part of the device-level
10287 * interrupt handler. When the PCI slot is in error recovery or the HBA
10288 * is undergoing initialization, the interrupt handler will not process
10289 * the interrupt. The link attention and ELS ring attention events are
10290 * handled by the worker thread. The interrupt handler signals the worker
10291 * thread and returns for these events. This function is called without
10292 * any lock held. It gets the hbalock to access and update SLI data
James Smart93996272008-08-24 21:50:30 -040010293 * structures.
10294 *
10295 * This function returns IRQ_HANDLED when interrupt is handled else it
10296 * returns IRQ_NONE.
James Smarte59058c2008-08-24 21:49:00 -040010297 **/
dea31012005-04-17 16:05:31 -050010298irqreturn_t
James Smart3772a992009-05-22 14:50:54 -040010299lpfc_sli_sp_intr_handler(int irq, void *dev_id)
dea31012005-04-17 16:05:31 -050010300{
James Smart2e0fef82007-06-17 19:56:36 -050010301 struct lpfc_hba *phba;
James Smarta747c9c2009-11-18 15:41:10 -050010302 uint32_t ha_copy, hc_copy;
dea31012005-04-17 16:05:31 -050010303 uint32_t work_ha_copy;
10304 unsigned long status;
James Smart5b75da22008-12-04 22:39:35 -050010305 unsigned long iflag;
dea31012005-04-17 16:05:31 -050010306 uint32_t control;
10307
James Smart92d7f7b2007-06-17 19:56:38 -050010308 MAILBOX_t *mbox, *pmbox;
James Smart858c9f62007-06-17 19:56:39 -050010309 struct lpfc_vport *vport;
10310 struct lpfc_nodelist *ndlp;
10311 struct lpfc_dmabuf *mp;
James Smart92d7f7b2007-06-17 19:56:38 -050010312 LPFC_MBOXQ_t *pmb;
10313 int rc;
10314
dea31012005-04-17 16:05:31 -050010315 /*
10316 * Get the driver's phba structure from the dev_id and
10317 * assume the HBA is not interrupting.
10318 */
James Smart93996272008-08-24 21:50:30 -040010319 phba = (struct lpfc_hba *)dev_id;
dea31012005-04-17 16:05:31 -050010320
10321 if (unlikely(!phba))
10322 return IRQ_NONE;
10323
dea31012005-04-17 16:05:31 -050010324 /*
James Smart93996272008-08-24 21:50:30 -040010325 * Stuff needs to be attented to when this function is invoked as an
10326 * individual interrupt handler in MSI-X multi-message interrupt mode
dea31012005-04-17 16:05:31 -050010327 */
James Smart93996272008-08-24 21:50:30 -040010328 if (phba->intr_type == MSIX) {
James Smart3772a992009-05-22 14:50:54 -040010329 /* Check device state for handling interrupt */
10330 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -040010331 return IRQ_NONE;
10332 /* Need to read HA REG for slow-path events */
James Smart5b75da22008-12-04 22:39:35 -050010333 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart9940b972011-03-11 16:06:12 -050010334 if (lpfc_readl(phba->HAregaddr, &ha_copy))
10335 goto unplug_error;
James Smart93996272008-08-24 21:50:30 -040010336 /* If somebody is waiting to handle an eratt don't process it
10337 * here. The brdkill function will do this.
10338 */
10339 if (phba->link_flag & LS_IGNORE_ERATT)
10340 ha_copy &= ~HA_ERATT;
10341 /* Check the need for handling ERATT in interrupt handler */
10342 if (ha_copy & HA_ERATT) {
10343 if (phba->hba_flag & HBA_ERATT_HANDLED)
10344 /* ERATT polling has handled ERATT */
10345 ha_copy &= ~HA_ERATT;
10346 else
10347 /* Indicate interrupt handler handles ERATT */
10348 phba->hba_flag |= HBA_ERATT_HANDLED;
10349 }
James Smarta257bf92009-04-06 18:48:10 -040010350
10351 /*
10352 * If there is deferred error attention, do not check for any
10353 * interrupt.
10354 */
10355 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
James Smart3772a992009-05-22 14:50:54 -040010356 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -040010357 return IRQ_NONE;
10358 }
10359
James Smart93996272008-08-24 21:50:30 -040010360 /* Clear up only attention source related to slow-path */
James Smart9940b972011-03-11 16:06:12 -050010361 if (lpfc_readl(phba->HCregaddr, &hc_copy))
10362 goto unplug_error;
10363
James Smarta747c9c2009-11-18 15:41:10 -050010364 writel(hc_copy & ~(HC_MBINT_ENA | HC_R2INT_ENA |
10365 HC_LAINT_ENA | HC_ERINT_ENA),
10366 phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -040010367 writel((ha_copy & (HA_MBATT | HA_R2_CLR_MSK)),
10368 phba->HAregaddr);
James Smarta747c9c2009-11-18 15:41:10 -050010369 writel(hc_copy, phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -040010370 readl(phba->HAregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -050010371 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -040010372 } else
10373 ha_copy = phba->ha_copy;
dea31012005-04-17 16:05:31 -050010374
dea31012005-04-17 16:05:31 -050010375 work_ha_copy = ha_copy & phba->work_ha_mask;
10376
James Smart93996272008-08-24 21:50:30 -040010377 if (work_ha_copy) {
dea31012005-04-17 16:05:31 -050010378 if (work_ha_copy & HA_LATT) {
10379 if (phba->sli.sli_flag & LPFC_PROCESS_LA) {
10380 /*
10381 * Turn off Link Attention interrupts
10382 * until CLEAR_LA done
10383 */
James Smart5b75da22008-12-04 22:39:35 -050010384 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -050010385 phba->sli.sli_flag &= ~LPFC_PROCESS_LA;
James Smart9940b972011-03-11 16:06:12 -050010386 if (lpfc_readl(phba->HCregaddr, &control))
10387 goto unplug_error;
dea31012005-04-17 16:05:31 -050010388 control &= ~HC_LAINT_ENA;
10389 writel(control, phba->HCregaddr);
10390 readl(phba->HCregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -050010391 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -050010392 }
10393 else
10394 work_ha_copy &= ~HA_LATT;
10395 }
10396
James Smart93996272008-08-24 21:50:30 -040010397 if (work_ha_copy & ~(HA_ERATT | HA_MBATT | HA_LATT)) {
James Smart858c9f62007-06-17 19:56:39 -050010398 /*
10399 * Turn off Slow Rings interrupts, LPFC_ELS_RING is
10400 * the only slow ring.
10401 */
10402 status = (work_ha_copy &
10403 (HA_RXMASK << (4*LPFC_ELS_RING)));
10404 status >>= (4*LPFC_ELS_RING);
10405 if (status & HA_RXMASK) {
James Smart5b75da22008-12-04 22:39:35 -050010406 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart9940b972011-03-11 16:06:12 -050010407 if (lpfc_readl(phba->HCregaddr, &control))
10408 goto unplug_error;
James Smarta58cbd52007-08-02 11:09:43 -040010409
10410 lpfc_debugfs_slow_ring_trc(phba,
10411 "ISR slow ring: ctl:x%x stat:x%x isrcnt:x%x",
10412 control, status,
10413 (uint32_t)phba->sli.slistat.sli_intr);
10414
James Smart858c9f62007-06-17 19:56:39 -050010415 if (control & (HC_R0INT_ENA << LPFC_ELS_RING)) {
James Smarta58cbd52007-08-02 11:09:43 -040010416 lpfc_debugfs_slow_ring_trc(phba,
10417 "ISR Disable ring:"
10418 "pwork:x%x hawork:x%x wait:x%x",
10419 phba->work_ha, work_ha_copy,
10420 (uint32_t)((unsigned long)
James Smart5e9d9b82008-06-14 22:52:53 -040010421 &phba->work_waitq));
James Smarta58cbd52007-08-02 11:09:43 -040010422
James Smart858c9f62007-06-17 19:56:39 -050010423 control &=
10424 ~(HC_R0INT_ENA << LPFC_ELS_RING);
dea31012005-04-17 16:05:31 -050010425 writel(control, phba->HCregaddr);
10426 readl(phba->HCregaddr); /* flush */
dea31012005-04-17 16:05:31 -050010427 }
James Smarta58cbd52007-08-02 11:09:43 -040010428 else {
10429 lpfc_debugfs_slow_ring_trc(phba,
10430 "ISR slow ring: pwork:"
10431 "x%x hawork:x%x wait:x%x",
10432 phba->work_ha, work_ha_copy,
10433 (uint32_t)((unsigned long)
James Smart5e9d9b82008-06-14 22:52:53 -040010434 &phba->work_waitq));
James Smarta58cbd52007-08-02 11:09:43 -040010435 }
James Smart5b75da22008-12-04 22:39:35 -050010436 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -050010437 }
10438 }
James Smart5b75da22008-12-04 22:39:35 -050010439 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -040010440 if (work_ha_copy & HA_ERATT) {
James Smart9940b972011-03-11 16:06:12 -050010441 if (lpfc_sli_read_hs(phba))
10442 goto unplug_error;
James Smarta257bf92009-04-06 18:48:10 -040010443 /*
10444 * Check if there is a deferred error condition
10445 * is active
10446 */
10447 if ((HS_FFER1 & phba->work_hs) &&
10448 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
James Smartdcf2a4e2010-09-29 11:18:53 -040010449 HS_FFER6 | HS_FFER7 | HS_FFER8) &
10450 phba->work_hs)) {
James Smarta257bf92009-04-06 18:48:10 -040010451 phba->hba_flag |= DEFER_ERATT;
10452 /* Clear all interrupt enable conditions */
10453 writel(0, phba->HCregaddr);
10454 readl(phba->HCregaddr);
10455 }
10456 }
10457
James Smart93996272008-08-24 21:50:30 -040010458 if ((work_ha_copy & HA_MBATT) && (phba->sli.mbox_active)) {
James Smart92d7f7b2007-06-17 19:56:38 -050010459 pmb = phba->sli.mbox_active;
James Smart04c68492009-05-22 14:52:52 -040010460 pmbox = &pmb->u.mb;
James Smart34b02dc2008-08-24 21:49:55 -040010461 mbox = phba->mbox;
James Smart858c9f62007-06-17 19:56:39 -050010462 vport = pmb->vport;
James Smart92d7f7b2007-06-17 19:56:38 -050010463
10464 /* First check out the status word */
10465 lpfc_sli_pcimem_bcopy(mbox, pmbox, sizeof(uint32_t));
10466 if (pmbox->mbxOwner != OWN_HOST) {
James Smart5b75da22008-12-04 22:39:35 -050010467 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart92d7f7b2007-06-17 19:56:38 -050010468 /*
10469 * Stray Mailbox Interrupt, mbxCommand <cmd>
10470 * mbxStatus <status>
10471 */
James Smart09372822008-01-11 01:52:54 -050010472 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
James Smart92d7f7b2007-06-17 19:56:38 -050010473 LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -040010474 "(%d):0304 Stray Mailbox "
James Smart92d7f7b2007-06-17 19:56:38 -050010475 "Interrupt mbxCommand x%x "
10476 "mbxStatus x%x\n",
James Smarte8b62012007-08-02 11:10:09 -040010477 (vport ? vport->vpi : 0),
James Smart92d7f7b2007-06-17 19:56:38 -050010478 pmbox->mbxCommand,
10479 pmbox->mbxStatus);
James Smart09372822008-01-11 01:52:54 -050010480 /* clear mailbox attention bit */
10481 work_ha_copy &= ~HA_MBATT;
10482 } else {
James Smart97eab632008-04-07 10:16:05 -040010483 phba->sli.mbox_active = NULL;
James Smart5b75da22008-12-04 22:39:35 -050010484 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart09372822008-01-11 01:52:54 -050010485 phba->last_completion_time = jiffies;
10486 del_timer(&phba->sli.mbox_tmo);
James Smart09372822008-01-11 01:52:54 -050010487 if (pmb->mbox_cmpl) {
10488 lpfc_sli_pcimem_bcopy(mbox, pmbox,
10489 MAILBOX_CMD_SIZE);
James Smart7a470272010-03-15 11:25:20 -040010490 if (pmb->out_ext_byte_len &&
10491 pmb->context2)
10492 lpfc_sli_pcimem_bcopy(
10493 phba->mbox_ext,
10494 pmb->context2,
10495 pmb->out_ext_byte_len);
James Smart858c9f62007-06-17 19:56:39 -050010496 }
James Smart09372822008-01-11 01:52:54 -050010497 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
10498 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
10499
10500 lpfc_debugfs_disc_trc(vport,
10501 LPFC_DISC_TRC_MBOX_VPORT,
10502 "MBOX dflt rpi: : "
10503 "status:x%x rpi:x%x",
10504 (uint32_t)pmbox->mbxStatus,
10505 pmbox->un.varWords[0], 0);
10506
10507 if (!pmbox->mbxStatus) {
10508 mp = (struct lpfc_dmabuf *)
10509 (pmb->context1);
10510 ndlp = (struct lpfc_nodelist *)
10511 pmb->context2;
10512
10513 /* Reg_LOGIN of dflt RPI was
10514 * successful. new lets get
10515 * rid of the RPI using the
10516 * same mbox buffer.
10517 */
10518 lpfc_unreg_login(phba,
10519 vport->vpi,
10520 pmbox->un.varWords[0],
10521 pmb);
10522 pmb->mbox_cmpl =
10523 lpfc_mbx_cmpl_dflt_rpi;
10524 pmb->context1 = mp;
10525 pmb->context2 = ndlp;
10526 pmb->vport = vport;
James Smart58da1ff2008-04-07 10:15:56 -040010527 rc = lpfc_sli_issue_mbox(phba,
10528 pmb,
10529 MBX_NOWAIT);
10530 if (rc != MBX_BUSY)
10531 lpfc_printf_log(phba,
10532 KERN_ERR,
10533 LOG_MBOX | LOG_SLI,
James Smartd7c255b2008-08-24 21:50:00 -040010534 "0350 rc should have"
James Smart6a9c52c2009-10-02 15:16:51 -040010535 "been MBX_BUSY\n");
James Smart3772a992009-05-22 14:50:54 -040010536 if (rc != MBX_NOT_FINISHED)
10537 goto send_current_mbox;
James Smart09372822008-01-11 01:52:54 -050010538 }
10539 }
James Smart5b75da22008-12-04 22:39:35 -050010540 spin_lock_irqsave(
10541 &phba->pport->work_port_lock,
10542 iflag);
James Smart09372822008-01-11 01:52:54 -050010543 phba->pport->work_port_events &=
10544 ~WORKER_MBOX_TMO;
James Smart5b75da22008-12-04 22:39:35 -050010545 spin_unlock_irqrestore(
10546 &phba->pport->work_port_lock,
10547 iflag);
James Smart09372822008-01-11 01:52:54 -050010548 lpfc_mbox_cmpl_put(phba, pmb);
James Smart858c9f62007-06-17 19:56:39 -050010549 }
James Smart97eab632008-04-07 10:16:05 -040010550 } else
James Smart5b75da22008-12-04 22:39:35 -050010551 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -040010552
James Smart92d7f7b2007-06-17 19:56:38 -050010553 if ((work_ha_copy & HA_MBATT) &&
10554 (phba->sli.mbox_active == NULL)) {
James Smart858c9f62007-06-17 19:56:39 -050010555send_current_mbox:
James Smart92d7f7b2007-06-17 19:56:38 -050010556 /* Process next mailbox command if there is one */
James Smart58da1ff2008-04-07 10:15:56 -040010557 do {
10558 rc = lpfc_sli_issue_mbox(phba, NULL,
10559 MBX_NOWAIT);
10560 } while (rc == MBX_NOT_FINISHED);
10561 if (rc != MBX_SUCCESS)
10562 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
10563 LOG_SLI, "0349 rc should be "
James Smart6a9c52c2009-10-02 15:16:51 -040010564 "MBX_SUCCESS\n");
James Smart92d7f7b2007-06-17 19:56:38 -050010565 }
10566
James Smart5b75da22008-12-04 22:39:35 -050010567 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -050010568 phba->work_ha |= work_ha_copy;
James Smart5b75da22008-12-04 22:39:35 -050010569 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart5e9d9b82008-06-14 22:52:53 -040010570 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -050010571 }
James Smart93996272008-08-24 21:50:30 -040010572 return IRQ_HANDLED;
James Smart9940b972011-03-11 16:06:12 -050010573unplug_error:
10574 spin_unlock_irqrestore(&phba->hbalock, iflag);
10575 return IRQ_HANDLED;
dea31012005-04-17 16:05:31 -050010576
James Smart3772a992009-05-22 14:50:54 -040010577} /* lpfc_sli_sp_intr_handler */
James Smart93996272008-08-24 21:50:30 -040010578
10579/**
James Smart3772a992009-05-22 14:50:54 -040010580 * lpfc_sli_fp_intr_handler - Fast-path interrupt handler to SLI-3 device.
James Smart93996272008-08-24 21:50:30 -040010581 * @irq: Interrupt number.
10582 * @dev_id: The device context pointer.
10583 *
10584 * This function is directly called from the PCI layer as an interrupt
James Smart3772a992009-05-22 14:50:54 -040010585 * service routine when device with SLI-3 interface spec is enabled with
10586 * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
10587 * ring event in the HBA. However, when the device is enabled with either
10588 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
10589 * device-level interrupt handler. When the PCI slot is in error recovery
10590 * or the HBA is undergoing initialization, the interrupt handler will not
10591 * process the interrupt. The SCSI FCP fast-path ring event are handled in
10592 * the intrrupt context. This function is called without any lock held.
10593 * It gets the hbalock to access and update SLI data structures.
James Smart93996272008-08-24 21:50:30 -040010594 *
10595 * This function returns IRQ_HANDLED when interrupt is handled else it
10596 * returns IRQ_NONE.
10597 **/
10598irqreturn_t
James Smart3772a992009-05-22 14:50:54 -040010599lpfc_sli_fp_intr_handler(int irq, void *dev_id)
James Smart93996272008-08-24 21:50:30 -040010600{
10601 struct lpfc_hba *phba;
10602 uint32_t ha_copy;
10603 unsigned long status;
James Smart5b75da22008-12-04 22:39:35 -050010604 unsigned long iflag;
James Smart93996272008-08-24 21:50:30 -040010605
10606 /* Get the driver's phba structure from the dev_id and
10607 * assume the HBA is not interrupting.
10608 */
10609 phba = (struct lpfc_hba *) dev_id;
10610
10611 if (unlikely(!phba))
10612 return IRQ_NONE;
dea31012005-04-17 16:05:31 -050010613
10614 /*
James Smart93996272008-08-24 21:50:30 -040010615 * Stuff needs to be attented to when this function is invoked as an
10616 * individual interrupt handler in MSI-X multi-message interrupt mode
dea31012005-04-17 16:05:31 -050010617 */
James Smart93996272008-08-24 21:50:30 -040010618 if (phba->intr_type == MSIX) {
James Smart3772a992009-05-22 14:50:54 -040010619 /* Check device state for handling interrupt */
10620 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -040010621 return IRQ_NONE;
10622 /* Need to read HA REG for FCP ring and other ring events */
James Smart9940b972011-03-11 16:06:12 -050010623 if (lpfc_readl(phba->HAregaddr, &ha_copy))
10624 return IRQ_HANDLED;
James Smart93996272008-08-24 21:50:30 -040010625 /* Clear up only attention source related to fast-path */
James Smart5b75da22008-12-04 22:39:35 -050010626 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -040010627 /*
10628 * If there is deferred error attention, do not check for
10629 * any interrupt.
10630 */
10631 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
James Smart3772a992009-05-22 14:50:54 -040010632 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -040010633 return IRQ_NONE;
10634 }
James Smart93996272008-08-24 21:50:30 -040010635 writel((ha_copy & (HA_R0_CLR_MSK | HA_R1_CLR_MSK)),
10636 phba->HAregaddr);
10637 readl(phba->HAregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -050010638 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -040010639 } else
10640 ha_copy = phba->ha_copy;
10641
10642 /*
10643 * Process all events on FCP ring. Take the optimized path for FCP IO.
10644 */
10645 ha_copy &= ~(phba->work_ha_mask);
10646
10647 status = (ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
dea31012005-04-17 16:05:31 -050010648 status >>= (4*LPFC_FCP_RING);
James Smart858c9f62007-06-17 19:56:39 -050010649 if (status & HA_RXMASK)
dea31012005-04-17 16:05:31 -050010650 lpfc_sli_handle_fast_ring_event(phba,
10651 &phba->sli.ring[LPFC_FCP_RING],
10652 status);
James Smarta4bc3372006-12-02 13:34:16 -050010653
10654 if (phba->cfg_multi_ring_support == 2) {
10655 /*
James Smart93996272008-08-24 21:50:30 -040010656 * Process all events on extra ring. Take the optimized path
10657 * for extra ring IO.
James Smarta4bc3372006-12-02 13:34:16 -050010658 */
James Smart93996272008-08-24 21:50:30 -040010659 status = (ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
James Smarta4bc3372006-12-02 13:34:16 -050010660 status >>= (4*LPFC_EXTRA_RING);
James Smart858c9f62007-06-17 19:56:39 -050010661 if (status & HA_RXMASK) {
James Smarta4bc3372006-12-02 13:34:16 -050010662 lpfc_sli_handle_fast_ring_event(phba,
10663 &phba->sli.ring[LPFC_EXTRA_RING],
10664 status);
10665 }
10666 }
dea31012005-04-17 16:05:31 -050010667 return IRQ_HANDLED;
James Smart3772a992009-05-22 14:50:54 -040010668} /* lpfc_sli_fp_intr_handler */
dea31012005-04-17 16:05:31 -050010669
James Smart93996272008-08-24 21:50:30 -040010670/**
James Smart3772a992009-05-22 14:50:54 -040010671 * lpfc_sli_intr_handler - Device-level interrupt handler to SLI-3 device
James Smart93996272008-08-24 21:50:30 -040010672 * @irq: Interrupt number.
10673 * @dev_id: The device context pointer.
10674 *
James Smart3772a992009-05-22 14:50:54 -040010675 * This function is the HBA device-level interrupt handler to device with
10676 * SLI-3 interface spec, called from the PCI layer when either MSI or
10677 * Pin-IRQ interrupt mode is enabled and there is an event in the HBA which
10678 * requires driver attention. This function invokes the slow-path interrupt
10679 * attention handling function and fast-path interrupt attention handling
10680 * function in turn to process the relevant HBA attention events. This
10681 * function is called without any lock held. It gets the hbalock to access
10682 * and update SLI data structures.
James Smart93996272008-08-24 21:50:30 -040010683 *
10684 * This function returns IRQ_HANDLED when interrupt is handled, else it
10685 * returns IRQ_NONE.
10686 **/
10687irqreturn_t
James Smart3772a992009-05-22 14:50:54 -040010688lpfc_sli_intr_handler(int irq, void *dev_id)
James Smart93996272008-08-24 21:50:30 -040010689{
10690 struct lpfc_hba *phba;
10691 irqreturn_t sp_irq_rc, fp_irq_rc;
10692 unsigned long status1, status2;
James Smarta747c9c2009-11-18 15:41:10 -050010693 uint32_t hc_copy;
James Smart93996272008-08-24 21:50:30 -040010694
10695 /*
10696 * Get the driver's phba structure from the dev_id and
10697 * assume the HBA is not interrupting.
10698 */
10699 phba = (struct lpfc_hba *) dev_id;
10700
10701 if (unlikely(!phba))
10702 return IRQ_NONE;
10703
James Smart3772a992009-05-22 14:50:54 -040010704 /* Check device state for handling interrupt */
10705 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -040010706 return IRQ_NONE;
10707
10708 spin_lock(&phba->hbalock);
James Smart9940b972011-03-11 16:06:12 -050010709 if (lpfc_readl(phba->HAregaddr, &phba->ha_copy)) {
10710 spin_unlock(&phba->hbalock);
10711 return IRQ_HANDLED;
10712 }
10713
James Smart93996272008-08-24 21:50:30 -040010714 if (unlikely(!phba->ha_copy)) {
10715 spin_unlock(&phba->hbalock);
10716 return IRQ_NONE;
10717 } else if (phba->ha_copy & HA_ERATT) {
10718 if (phba->hba_flag & HBA_ERATT_HANDLED)
10719 /* ERATT polling has handled ERATT */
10720 phba->ha_copy &= ~HA_ERATT;
10721 else
10722 /* Indicate interrupt handler handles ERATT */
10723 phba->hba_flag |= HBA_ERATT_HANDLED;
10724 }
10725
James Smarta257bf92009-04-06 18:48:10 -040010726 /*
10727 * If there is deferred error attention, do not check for any interrupt.
10728 */
10729 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
Dan Carpenterec21b3b2010-08-08 00:15:17 +020010730 spin_unlock(&phba->hbalock);
James Smarta257bf92009-04-06 18:48:10 -040010731 return IRQ_NONE;
10732 }
10733
James Smart93996272008-08-24 21:50:30 -040010734 /* Clear attention sources except link and error attentions */
James Smart9940b972011-03-11 16:06:12 -050010735 if (lpfc_readl(phba->HCregaddr, &hc_copy)) {
10736 spin_unlock(&phba->hbalock);
10737 return IRQ_HANDLED;
10738 }
James Smarta747c9c2009-11-18 15:41:10 -050010739 writel(hc_copy & ~(HC_MBINT_ENA | HC_R0INT_ENA | HC_R1INT_ENA
10740 | HC_R2INT_ENA | HC_LAINT_ENA | HC_ERINT_ENA),
10741 phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -040010742 writel((phba->ha_copy & ~(HA_LATT | HA_ERATT)), phba->HAregaddr);
James Smarta747c9c2009-11-18 15:41:10 -050010743 writel(hc_copy, phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -040010744 readl(phba->HAregaddr); /* flush */
10745 spin_unlock(&phba->hbalock);
10746
10747 /*
10748 * Invokes slow-path host attention interrupt handling as appropriate.
10749 */
10750
10751 /* status of events with mailbox and link attention */
10752 status1 = phba->ha_copy & (HA_MBATT | HA_LATT | HA_ERATT);
10753
10754 /* status of events with ELS ring */
10755 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_ELS_RING)));
10756 status2 >>= (4*LPFC_ELS_RING);
10757
10758 if (status1 || (status2 & HA_RXMASK))
James Smart3772a992009-05-22 14:50:54 -040010759 sp_irq_rc = lpfc_sli_sp_intr_handler(irq, dev_id);
James Smart93996272008-08-24 21:50:30 -040010760 else
10761 sp_irq_rc = IRQ_NONE;
10762
10763 /*
10764 * Invoke fast-path host attention interrupt handling as appropriate.
10765 */
10766
10767 /* status of events with FCP ring */
10768 status1 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
10769 status1 >>= (4*LPFC_FCP_RING);
10770
10771 /* status of events with extra ring */
10772 if (phba->cfg_multi_ring_support == 2) {
10773 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
10774 status2 >>= (4*LPFC_EXTRA_RING);
10775 } else
10776 status2 = 0;
10777
10778 if ((status1 & HA_RXMASK) || (status2 & HA_RXMASK))
James Smart3772a992009-05-22 14:50:54 -040010779 fp_irq_rc = lpfc_sli_fp_intr_handler(irq, dev_id);
James Smart93996272008-08-24 21:50:30 -040010780 else
10781 fp_irq_rc = IRQ_NONE;
10782
10783 /* Return device-level interrupt handling status */
10784 return (sp_irq_rc == IRQ_HANDLED) ? sp_irq_rc : fp_irq_rc;
James Smart3772a992009-05-22 14:50:54 -040010785} /* lpfc_sli_intr_handler */
James Smart4f774512009-05-22 14:52:35 -040010786
10787/**
10788 * lpfc_sli4_fcp_xri_abort_event_proc - Process fcp xri abort event
10789 * @phba: pointer to lpfc hba data structure.
10790 *
10791 * This routine is invoked by the worker thread to process all the pending
10792 * SLI4 FCP abort XRI events.
10793 **/
10794void lpfc_sli4_fcp_xri_abort_event_proc(struct lpfc_hba *phba)
10795{
10796 struct lpfc_cq_event *cq_event;
10797
10798 /* First, declare the fcp xri abort event has been handled */
10799 spin_lock_irq(&phba->hbalock);
10800 phba->hba_flag &= ~FCP_XRI_ABORT_EVENT;
10801 spin_unlock_irq(&phba->hbalock);
10802 /* Now, handle all the fcp xri abort events */
10803 while (!list_empty(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue)) {
10804 /* Get the first event from the head of the event queue */
10805 spin_lock_irq(&phba->hbalock);
10806 list_remove_head(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue,
10807 cq_event, struct lpfc_cq_event, list);
10808 spin_unlock_irq(&phba->hbalock);
10809 /* Notify aborted XRI for FCP work queue */
10810 lpfc_sli4_fcp_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
10811 /* Free the event processed back to the free pool */
10812 lpfc_sli4_cq_event_release(phba, cq_event);
10813 }
10814}
10815
10816/**
10817 * lpfc_sli4_els_xri_abort_event_proc - Process els xri abort event
10818 * @phba: pointer to lpfc hba data structure.
10819 *
10820 * This routine is invoked by the worker thread to process all the pending
10821 * SLI4 els abort xri events.
10822 **/
10823void lpfc_sli4_els_xri_abort_event_proc(struct lpfc_hba *phba)
10824{
10825 struct lpfc_cq_event *cq_event;
10826
10827 /* First, declare the els xri abort event has been handled */
10828 spin_lock_irq(&phba->hbalock);
10829 phba->hba_flag &= ~ELS_XRI_ABORT_EVENT;
10830 spin_unlock_irq(&phba->hbalock);
10831 /* Now, handle all the els xri abort events */
10832 while (!list_empty(&phba->sli4_hba.sp_els_xri_aborted_work_queue)) {
10833 /* Get the first event from the head of the event queue */
10834 spin_lock_irq(&phba->hbalock);
10835 list_remove_head(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
10836 cq_event, struct lpfc_cq_event, list);
10837 spin_unlock_irq(&phba->hbalock);
10838 /* Notify aborted XRI for ELS work queue */
10839 lpfc_sli4_els_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
10840 /* Free the event processed back to the free pool */
10841 lpfc_sli4_cq_event_release(phba, cq_event);
10842 }
10843}
10844
James Smart341af102010-01-26 23:07:37 -050010845/**
10846 * lpfc_sli4_iocb_param_transfer - Transfer pIocbOut and cmpl status to pIocbIn
10847 * @phba: pointer to lpfc hba data structure
10848 * @pIocbIn: pointer to the rspiocbq
10849 * @pIocbOut: pointer to the cmdiocbq
10850 * @wcqe: pointer to the complete wcqe
10851 *
10852 * This routine transfers the fields of a command iocbq to a response iocbq
10853 * by copying all the IOCB fields from command iocbq and transferring the
10854 * completion status information from the complete wcqe.
10855 **/
James Smart4f774512009-05-22 14:52:35 -040010856static void
James Smart341af102010-01-26 23:07:37 -050010857lpfc_sli4_iocb_param_transfer(struct lpfc_hba *phba,
10858 struct lpfc_iocbq *pIocbIn,
James Smart4f774512009-05-22 14:52:35 -040010859 struct lpfc_iocbq *pIocbOut,
10860 struct lpfc_wcqe_complete *wcqe)
10861{
James Smart341af102010-01-26 23:07:37 -050010862 unsigned long iflags;
James Smartacd68592012-01-18 16:25:09 -050010863 uint32_t status;
James Smart4f774512009-05-22 14:52:35 -040010864 size_t offset = offsetof(struct lpfc_iocbq, iocb);
10865
10866 memcpy((char *)pIocbIn + offset, (char *)pIocbOut + offset,
10867 sizeof(struct lpfc_iocbq) - offset);
James Smart4f774512009-05-22 14:52:35 -040010868 /* Map WCQE parameters into irspiocb parameters */
James Smartacd68592012-01-18 16:25:09 -050010869 status = bf_get(lpfc_wcqe_c_status, wcqe);
10870 pIocbIn->iocb.ulpStatus = (status & LPFC_IOCB_STATUS_MASK);
James Smart4f774512009-05-22 14:52:35 -040010871 if (pIocbOut->iocb_flag & LPFC_IO_FCP)
10872 if (pIocbIn->iocb.ulpStatus == IOSTAT_FCP_RSP_ERROR)
10873 pIocbIn->iocb.un.fcpi.fcpi_parm =
10874 pIocbOut->iocb.un.fcpi.fcpi_parm -
10875 wcqe->total_data_placed;
10876 else
10877 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
James Smart695a8142010-01-26 23:08:03 -050010878 else {
James Smart4f774512009-05-22 14:52:35 -040010879 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
James Smart695a8142010-01-26 23:08:03 -050010880 pIocbIn->iocb.un.genreq64.bdl.bdeSize = wcqe->total_data_placed;
10881 }
James Smart341af102010-01-26 23:07:37 -050010882
James Smartacd68592012-01-18 16:25:09 -050010883 /* Convert BG errors for completion status */
10884 if (status == CQE_STATUS_DI_ERROR) {
10885 pIocbIn->iocb.ulpStatus = IOSTAT_LOCAL_REJECT;
10886
10887 if (bf_get(lpfc_wcqe_c_bg_edir, wcqe))
10888 pIocbIn->iocb.un.ulpWord[4] = IOERR_RX_DMA_FAILED;
10889 else
10890 pIocbIn->iocb.un.ulpWord[4] = IOERR_TX_DMA_FAILED;
10891
10892 pIocbIn->iocb.unsli3.sli3_bg.bgstat = 0;
10893 if (bf_get(lpfc_wcqe_c_bg_ge, wcqe)) /* Guard Check failed */
10894 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
10895 BGS_GUARD_ERR_MASK;
10896 if (bf_get(lpfc_wcqe_c_bg_ae, wcqe)) /* App Tag Check failed */
10897 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
10898 BGS_APPTAG_ERR_MASK;
10899 if (bf_get(lpfc_wcqe_c_bg_re, wcqe)) /* Ref Tag Check failed */
10900 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
10901 BGS_REFTAG_ERR_MASK;
10902
10903 /* Check to see if there was any good data before the error */
10904 if (bf_get(lpfc_wcqe_c_bg_tdpv, wcqe)) {
10905 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
10906 BGS_HI_WATER_MARK_PRESENT_MASK;
10907 pIocbIn->iocb.unsli3.sli3_bg.bghm =
10908 wcqe->total_data_placed;
10909 }
10910
10911 /*
10912 * Set ALL the error bits to indicate we don't know what
10913 * type of error it is.
10914 */
10915 if (!pIocbIn->iocb.unsli3.sli3_bg.bgstat)
10916 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
10917 (BGS_REFTAG_ERR_MASK | BGS_APPTAG_ERR_MASK |
10918 BGS_GUARD_ERR_MASK);
10919 }
10920
James Smart341af102010-01-26 23:07:37 -050010921 /* Pick up HBA exchange busy condition */
10922 if (bf_get(lpfc_wcqe_c_xb, wcqe)) {
10923 spin_lock_irqsave(&phba->hbalock, iflags);
10924 pIocbIn->iocb_flag |= LPFC_EXCHANGE_BUSY;
10925 spin_unlock_irqrestore(&phba->hbalock, iflags);
10926 }
James Smart4f774512009-05-22 14:52:35 -040010927}
10928
10929/**
James Smart45ed1192009-10-02 15:17:02 -040010930 * lpfc_sli4_els_wcqe_to_rspiocbq - Get response iocbq from els wcqe
10931 * @phba: Pointer to HBA context object.
10932 * @wcqe: Pointer to work-queue completion queue entry.
10933 *
10934 * This routine handles an ELS work-queue completion event and construct
10935 * a pseudo response ELS IODBQ from the SLI4 ELS WCQE for the common
10936 * discovery engine to handle.
10937 *
10938 * Return: Pointer to the receive IOCBQ, NULL otherwise.
10939 **/
10940static struct lpfc_iocbq *
10941lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *phba,
10942 struct lpfc_iocbq *irspiocbq)
10943{
10944 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
10945 struct lpfc_iocbq *cmdiocbq;
10946 struct lpfc_wcqe_complete *wcqe;
10947 unsigned long iflags;
10948
10949 wcqe = &irspiocbq->cq_event.cqe.wcqe_cmpl;
10950 spin_lock_irqsave(&phba->hbalock, iflags);
10951 pring->stats.iocb_event++;
10952 /* Look up the ELS command IOCB and create pseudo response IOCB */
10953 cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
10954 bf_get(lpfc_wcqe_c_request_tag, wcqe));
10955 spin_unlock_irqrestore(&phba->hbalock, iflags);
10956
10957 if (unlikely(!cmdiocbq)) {
10958 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
10959 "0386 ELS complete with no corresponding "
10960 "cmdiocb: iotag (%d)\n",
10961 bf_get(lpfc_wcqe_c_request_tag, wcqe));
10962 lpfc_sli_release_iocbq(phba, irspiocbq);
10963 return NULL;
10964 }
10965
10966 /* Fake the irspiocbq and copy necessary response information */
James Smart341af102010-01-26 23:07:37 -050010967 lpfc_sli4_iocb_param_transfer(phba, irspiocbq, cmdiocbq, wcqe);
James Smart45ed1192009-10-02 15:17:02 -040010968
10969 return irspiocbq;
10970}
10971
10972/**
James Smart04c68492009-05-22 14:52:52 -040010973 * lpfc_sli4_sp_handle_async_event - Handle an asynchroous event
10974 * @phba: Pointer to HBA context object.
10975 * @cqe: Pointer to mailbox completion queue entry.
10976 *
10977 * This routine process a mailbox completion queue entry with asynchrous
10978 * event.
10979 *
10980 * Return: true if work posted to worker thread, otherwise false.
10981 **/
10982static bool
10983lpfc_sli4_sp_handle_async_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
10984{
10985 struct lpfc_cq_event *cq_event;
10986 unsigned long iflags;
10987
10988 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
10989 "0392 Async Event: word0:x%x, word1:x%x, "
10990 "word2:x%x, word3:x%x\n", mcqe->word0,
10991 mcqe->mcqe_tag0, mcqe->mcqe_tag1, mcqe->trailer);
10992
10993 /* Allocate a new internal CQ_EVENT entry */
10994 cq_event = lpfc_sli4_cq_event_alloc(phba);
10995 if (!cq_event) {
10996 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10997 "0394 Failed to allocate CQ_EVENT entry\n");
10998 return false;
10999 }
11000
11001 /* Move the CQE into an asynchronous event entry */
11002 memcpy(&cq_event->cqe, mcqe, sizeof(struct lpfc_mcqe));
11003 spin_lock_irqsave(&phba->hbalock, iflags);
11004 list_add_tail(&cq_event->list, &phba->sli4_hba.sp_asynce_work_queue);
11005 /* Set the async event flag */
11006 phba->hba_flag |= ASYNC_EVENT;
11007 spin_unlock_irqrestore(&phba->hbalock, iflags);
11008
11009 return true;
11010}
11011
11012/**
11013 * lpfc_sli4_sp_handle_mbox_event - Handle a mailbox completion event
11014 * @phba: Pointer to HBA context object.
11015 * @cqe: Pointer to mailbox completion queue entry.
11016 *
11017 * This routine process a mailbox completion queue entry with mailbox
11018 * completion event.
11019 *
11020 * Return: true if work posted to worker thread, otherwise false.
11021 **/
11022static bool
11023lpfc_sli4_sp_handle_mbox_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
11024{
11025 uint32_t mcqe_status;
11026 MAILBOX_t *mbox, *pmbox;
11027 struct lpfc_mqe *mqe;
11028 struct lpfc_vport *vport;
11029 struct lpfc_nodelist *ndlp;
11030 struct lpfc_dmabuf *mp;
11031 unsigned long iflags;
11032 LPFC_MBOXQ_t *pmb;
11033 bool workposted = false;
11034 int rc;
11035
11036 /* If not a mailbox complete MCQE, out by checking mailbox consume */
11037 if (!bf_get(lpfc_trailer_completed, mcqe))
11038 goto out_no_mqe_complete;
11039
11040 /* Get the reference to the active mbox command */
11041 spin_lock_irqsave(&phba->hbalock, iflags);
11042 pmb = phba->sli.mbox_active;
11043 if (unlikely(!pmb)) {
11044 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
11045 "1832 No pending MBOX command to handle\n");
11046 spin_unlock_irqrestore(&phba->hbalock, iflags);
11047 goto out_no_mqe_complete;
11048 }
11049 spin_unlock_irqrestore(&phba->hbalock, iflags);
11050 mqe = &pmb->u.mqe;
11051 pmbox = (MAILBOX_t *)&pmb->u.mqe;
11052 mbox = phba->mbox;
11053 vport = pmb->vport;
11054
11055 /* Reset heartbeat timer */
11056 phba->last_completion_time = jiffies;
11057 del_timer(&phba->sli.mbox_tmo);
11058
11059 /* Move mbox data to caller's mailbox region, do endian swapping */
11060 if (pmb->mbox_cmpl && mbox)
11061 lpfc_sli_pcimem_bcopy(mbox, mqe, sizeof(struct lpfc_mqe));
James Smart04c68492009-05-22 14:52:52 -040011062
James Smart73d91e52011-10-10 21:32:10 -040011063 /*
11064 * For mcqe errors, conditionally move a modified error code to
11065 * the mbox so that the error will not be missed.
11066 */
11067 mcqe_status = bf_get(lpfc_mcqe_status, mcqe);
11068 if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
11069 if (bf_get(lpfc_mqe_status, mqe) == MBX_SUCCESS)
11070 bf_set(lpfc_mqe_status, mqe,
11071 (LPFC_MBX_ERROR_RANGE | mcqe_status));
11072 }
James Smart04c68492009-05-22 14:52:52 -040011073 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
11074 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
11075 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_MBOX_VPORT,
11076 "MBOX dflt rpi: status:x%x rpi:x%x",
11077 mcqe_status,
11078 pmbox->un.varWords[0], 0);
11079 if (mcqe_status == MB_CQE_STATUS_SUCCESS) {
11080 mp = (struct lpfc_dmabuf *)(pmb->context1);
11081 ndlp = (struct lpfc_nodelist *)pmb->context2;
11082 /* Reg_LOGIN of dflt RPI was successful. Now lets get
11083 * RID of the PPI using the same mbox buffer.
11084 */
11085 lpfc_unreg_login(phba, vport->vpi,
11086 pmbox->un.varWords[0], pmb);
11087 pmb->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
11088 pmb->context1 = mp;
11089 pmb->context2 = ndlp;
11090 pmb->vport = vport;
11091 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
11092 if (rc != MBX_BUSY)
11093 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
11094 LOG_SLI, "0385 rc should "
11095 "have been MBX_BUSY\n");
11096 if (rc != MBX_NOT_FINISHED)
11097 goto send_current_mbox;
11098 }
11099 }
11100 spin_lock_irqsave(&phba->pport->work_port_lock, iflags);
11101 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
11102 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflags);
11103
11104 /* There is mailbox completion work to do */
11105 spin_lock_irqsave(&phba->hbalock, iflags);
11106 __lpfc_mbox_cmpl_put(phba, pmb);
11107 phba->work_ha |= HA_MBATT;
11108 spin_unlock_irqrestore(&phba->hbalock, iflags);
11109 workposted = true;
11110
11111send_current_mbox:
11112 spin_lock_irqsave(&phba->hbalock, iflags);
11113 /* Release the mailbox command posting token */
11114 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
11115 /* Setting active mailbox pointer need to be in sync to flag clear */
11116 phba->sli.mbox_active = NULL;
11117 spin_unlock_irqrestore(&phba->hbalock, iflags);
11118 /* Wake up worker thread to post the next pending mailbox command */
11119 lpfc_worker_wake_up(phba);
11120out_no_mqe_complete:
11121 if (bf_get(lpfc_trailer_consumed, mcqe))
11122 lpfc_sli4_mq_release(phba->sli4_hba.mbx_wq);
11123 return workposted;
11124}
11125
11126/**
11127 * lpfc_sli4_sp_handle_mcqe - Process a mailbox completion queue entry
11128 * @phba: Pointer to HBA context object.
11129 * @cqe: Pointer to mailbox completion queue entry.
11130 *
11131 * This routine process a mailbox completion queue entry, it invokes the
11132 * proper mailbox complete handling or asynchrous event handling routine
11133 * according to the MCQE's async bit.
11134 *
11135 * Return: true if work posted to worker thread, otherwise false.
11136 **/
11137static bool
11138lpfc_sli4_sp_handle_mcqe(struct lpfc_hba *phba, struct lpfc_cqe *cqe)
11139{
11140 struct lpfc_mcqe mcqe;
11141 bool workposted;
11142
11143 /* Copy the mailbox MCQE and convert endian order as needed */
11144 lpfc_sli_pcimem_bcopy(cqe, &mcqe, sizeof(struct lpfc_mcqe));
11145
11146 /* Invoke the proper event handling routine */
11147 if (!bf_get(lpfc_trailer_async, &mcqe))
11148 workposted = lpfc_sli4_sp_handle_mbox_event(phba, &mcqe);
11149 else
11150 workposted = lpfc_sli4_sp_handle_async_event(phba, &mcqe);
11151 return workposted;
11152}
11153
11154/**
James Smart4f774512009-05-22 14:52:35 -040011155 * lpfc_sli4_sp_handle_els_wcqe - Handle els work-queue completion event
11156 * @phba: Pointer to HBA context object.
11157 * @wcqe: Pointer to work-queue completion queue entry.
11158 *
11159 * This routine handles an ELS work-queue completion event.
11160 *
11161 * Return: true if work posted to worker thread, otherwise false.
11162 **/
11163static bool
11164lpfc_sli4_sp_handle_els_wcqe(struct lpfc_hba *phba,
11165 struct lpfc_wcqe_complete *wcqe)
11166{
James Smart4f774512009-05-22 14:52:35 -040011167 struct lpfc_iocbq *irspiocbq;
11168 unsigned long iflags;
James Smart2a9bf3d2010-06-07 15:24:45 -040011169 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_FCP_RING];
James Smart4f774512009-05-22 14:52:35 -040011170
James Smart45ed1192009-10-02 15:17:02 -040011171 /* Get an irspiocbq for later ELS response processing use */
James Smart4f774512009-05-22 14:52:35 -040011172 irspiocbq = lpfc_sli_get_iocbq(phba);
11173 if (!irspiocbq) {
11174 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart2a9bf3d2010-06-07 15:24:45 -040011175 "0387 NO IOCBQ data: txq_cnt=%d iocb_cnt=%d "
11176 "fcp_txcmplq_cnt=%d, els_txcmplq_cnt=%d\n",
11177 pring->txq_cnt, phba->iocb_cnt,
11178 phba->sli.ring[LPFC_FCP_RING].txcmplq_cnt,
11179 phba->sli.ring[LPFC_ELS_RING].txcmplq_cnt);
James Smart45ed1192009-10-02 15:17:02 -040011180 return false;
James Smart4f774512009-05-22 14:52:35 -040011181 }
James Smart4f774512009-05-22 14:52:35 -040011182
James Smart45ed1192009-10-02 15:17:02 -040011183 /* Save off the slow-path queue event for work thread to process */
11184 memcpy(&irspiocbq->cq_event.cqe.wcqe_cmpl, wcqe, sizeof(*wcqe));
James Smart4f774512009-05-22 14:52:35 -040011185 spin_lock_irqsave(&phba->hbalock, iflags);
James Smart4d9ab992009-10-02 15:16:39 -040011186 list_add_tail(&irspiocbq->cq_event.list,
James Smart45ed1192009-10-02 15:17:02 -040011187 &phba->sli4_hba.sp_queue_event);
11188 phba->hba_flag |= HBA_SP_QUEUE_EVT;
James Smart4f774512009-05-22 14:52:35 -040011189 spin_unlock_irqrestore(&phba->hbalock, iflags);
James Smart4f774512009-05-22 14:52:35 -040011190
James Smart45ed1192009-10-02 15:17:02 -040011191 return true;
James Smart4f774512009-05-22 14:52:35 -040011192}
11193
11194/**
11195 * lpfc_sli4_sp_handle_rel_wcqe - Handle slow-path WQ entry consumed event
11196 * @phba: Pointer to HBA context object.
11197 * @wcqe: Pointer to work-queue completion queue entry.
11198 *
11199 * This routine handles slow-path WQ entry comsumed event by invoking the
11200 * proper WQ release routine to the slow-path WQ.
11201 **/
11202static void
11203lpfc_sli4_sp_handle_rel_wcqe(struct lpfc_hba *phba,
11204 struct lpfc_wcqe_release *wcqe)
11205{
James Smart2e90f4b2011-12-13 13:22:37 -050011206 /* sanity check on queue memory */
11207 if (unlikely(!phba->sli4_hba.els_wq))
11208 return;
James Smart4f774512009-05-22 14:52:35 -040011209 /* Check for the slow-path ELS work queue */
11210 if (bf_get(lpfc_wcqe_r_wq_id, wcqe) == phba->sli4_hba.els_wq->queue_id)
11211 lpfc_sli4_wq_release(phba->sli4_hba.els_wq,
11212 bf_get(lpfc_wcqe_r_wqe_index, wcqe));
11213 else
11214 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11215 "2579 Slow-path wqe consume event carries "
11216 "miss-matched qid: wcqe-qid=x%x, sp-qid=x%x\n",
11217 bf_get(lpfc_wcqe_r_wqe_index, wcqe),
11218 phba->sli4_hba.els_wq->queue_id);
11219}
11220
11221/**
11222 * lpfc_sli4_sp_handle_abort_xri_wcqe - Handle a xri abort event
11223 * @phba: Pointer to HBA context object.
11224 * @cq: Pointer to a WQ completion queue.
11225 * @wcqe: Pointer to work-queue completion queue entry.
11226 *
11227 * This routine handles an XRI abort event.
11228 *
11229 * Return: true if work posted to worker thread, otherwise false.
11230 **/
11231static bool
11232lpfc_sli4_sp_handle_abort_xri_wcqe(struct lpfc_hba *phba,
11233 struct lpfc_queue *cq,
11234 struct sli4_wcqe_xri_aborted *wcqe)
11235{
11236 bool workposted = false;
11237 struct lpfc_cq_event *cq_event;
11238 unsigned long iflags;
11239
11240 /* Allocate a new internal CQ_EVENT entry */
11241 cq_event = lpfc_sli4_cq_event_alloc(phba);
11242 if (!cq_event) {
11243 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11244 "0602 Failed to allocate CQ_EVENT entry\n");
11245 return false;
11246 }
11247
11248 /* Move the CQE into the proper xri abort event list */
11249 memcpy(&cq_event->cqe, wcqe, sizeof(struct sli4_wcqe_xri_aborted));
11250 switch (cq->subtype) {
11251 case LPFC_FCP:
11252 spin_lock_irqsave(&phba->hbalock, iflags);
11253 list_add_tail(&cq_event->list,
11254 &phba->sli4_hba.sp_fcp_xri_aborted_work_queue);
11255 /* Set the fcp xri abort event flag */
11256 phba->hba_flag |= FCP_XRI_ABORT_EVENT;
11257 spin_unlock_irqrestore(&phba->hbalock, iflags);
11258 workposted = true;
11259 break;
11260 case LPFC_ELS:
11261 spin_lock_irqsave(&phba->hbalock, iflags);
11262 list_add_tail(&cq_event->list,
11263 &phba->sli4_hba.sp_els_xri_aborted_work_queue);
11264 /* Set the els xri abort event flag */
11265 phba->hba_flag |= ELS_XRI_ABORT_EVENT;
11266 spin_unlock_irqrestore(&phba->hbalock, iflags);
11267 workposted = true;
11268 break;
11269 default:
11270 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11271 "0603 Invalid work queue CQE subtype (x%x)\n",
11272 cq->subtype);
11273 workposted = false;
11274 break;
11275 }
11276 return workposted;
11277}
11278
11279/**
James Smart4d9ab992009-10-02 15:16:39 -040011280 * lpfc_sli4_sp_handle_rcqe - Process a receive-queue completion queue entry
James Smart4f774512009-05-22 14:52:35 -040011281 * @phba: Pointer to HBA context object.
James Smart4d9ab992009-10-02 15:16:39 -040011282 * @rcqe: Pointer to receive-queue completion queue entry.
James Smart4f774512009-05-22 14:52:35 -040011283 *
James Smart4d9ab992009-10-02 15:16:39 -040011284 * This routine process a receive-queue completion queue entry.
James Smart4f774512009-05-22 14:52:35 -040011285 *
11286 * Return: true if work posted to worker thread, otherwise false.
11287 **/
11288static bool
James Smart4d9ab992009-10-02 15:16:39 -040011289lpfc_sli4_sp_handle_rcqe(struct lpfc_hba *phba, struct lpfc_rcqe *rcqe)
11290{
11291 bool workposted = false;
11292 struct lpfc_queue *hrq = phba->sli4_hba.hdr_rq;
11293 struct lpfc_queue *drq = phba->sli4_hba.dat_rq;
11294 struct hbq_dmabuf *dma_buf;
James Smart7851fe22011-07-22 18:36:52 -040011295 uint32_t status, rq_id;
James Smart4d9ab992009-10-02 15:16:39 -040011296 unsigned long iflags;
11297
James Smart2e90f4b2011-12-13 13:22:37 -050011298 /* sanity check on queue memory */
11299 if (unlikely(!hrq) || unlikely(!drq))
11300 return workposted;
11301
James Smart7851fe22011-07-22 18:36:52 -040011302 if (bf_get(lpfc_cqe_code, rcqe) == CQE_CODE_RECEIVE_V1)
11303 rq_id = bf_get(lpfc_rcqe_rq_id_v1, rcqe);
11304 else
11305 rq_id = bf_get(lpfc_rcqe_rq_id, rcqe);
11306 if (rq_id != hrq->queue_id)
James Smart4d9ab992009-10-02 15:16:39 -040011307 goto out;
11308
11309 status = bf_get(lpfc_rcqe_status, rcqe);
11310 switch (status) {
11311 case FC_STATUS_RQ_BUF_LEN_EXCEEDED:
11312 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11313 "2537 Receive Frame Truncated!!\n");
11314 case FC_STATUS_RQ_SUCCESS:
James Smart5ffc2662009-11-18 15:39:44 -050011315 lpfc_sli4_rq_release(hrq, drq);
James Smart4d9ab992009-10-02 15:16:39 -040011316 spin_lock_irqsave(&phba->hbalock, iflags);
11317 dma_buf = lpfc_sli_hbqbuf_get(&phba->hbqs[0].hbq_buffer_list);
11318 if (!dma_buf) {
11319 spin_unlock_irqrestore(&phba->hbalock, iflags);
11320 goto out;
11321 }
11322 memcpy(&dma_buf->cq_event.cqe.rcqe_cmpl, rcqe, sizeof(*rcqe));
11323 /* save off the frame for the word thread to process */
11324 list_add_tail(&dma_buf->cq_event.list,
James Smart45ed1192009-10-02 15:17:02 -040011325 &phba->sli4_hba.sp_queue_event);
James Smart4d9ab992009-10-02 15:16:39 -040011326 /* Frame received */
James Smart45ed1192009-10-02 15:17:02 -040011327 phba->hba_flag |= HBA_SP_QUEUE_EVT;
James Smart4d9ab992009-10-02 15:16:39 -040011328 spin_unlock_irqrestore(&phba->hbalock, iflags);
11329 workposted = true;
11330 break;
11331 case FC_STATUS_INSUFF_BUF_NEED_BUF:
11332 case FC_STATUS_INSUFF_BUF_FRM_DISC:
11333 /* Post more buffers if possible */
11334 spin_lock_irqsave(&phba->hbalock, iflags);
11335 phba->hba_flag |= HBA_POST_RECEIVE_BUFFER;
11336 spin_unlock_irqrestore(&phba->hbalock, iflags);
11337 workposted = true;
11338 break;
11339 }
11340out:
11341 return workposted;
James Smart4d9ab992009-10-02 15:16:39 -040011342}
11343
11344/**
11345 * lpfc_sli4_sp_handle_cqe - Process a slow path completion queue entry
11346 * @phba: Pointer to HBA context object.
11347 * @cq: Pointer to the completion queue.
11348 * @wcqe: Pointer to a completion queue entry.
11349 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030011350 * This routine process a slow-path work-queue or receive queue completion queue
James Smart4d9ab992009-10-02 15:16:39 -040011351 * entry.
11352 *
11353 * Return: true if work posted to worker thread, otherwise false.
11354 **/
11355static bool
11356lpfc_sli4_sp_handle_cqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
James Smart4f774512009-05-22 14:52:35 -040011357 struct lpfc_cqe *cqe)
11358{
James Smart45ed1192009-10-02 15:17:02 -040011359 struct lpfc_cqe cqevt;
James Smart4f774512009-05-22 14:52:35 -040011360 bool workposted = false;
11361
11362 /* Copy the work queue CQE and convert endian order if needed */
James Smart45ed1192009-10-02 15:17:02 -040011363 lpfc_sli_pcimem_bcopy(cqe, &cqevt, sizeof(struct lpfc_cqe));
James Smart4f774512009-05-22 14:52:35 -040011364
11365 /* Check and process for different type of WCQE and dispatch */
James Smart45ed1192009-10-02 15:17:02 -040011366 switch (bf_get(lpfc_cqe_code, &cqevt)) {
James Smart4f774512009-05-22 14:52:35 -040011367 case CQE_CODE_COMPL_WQE:
James Smart45ed1192009-10-02 15:17:02 -040011368 /* Process the WQ/RQ complete event */
James Smartbc739052010-08-04 16:11:18 -040011369 phba->last_completion_time = jiffies;
James Smart4f774512009-05-22 14:52:35 -040011370 workposted = lpfc_sli4_sp_handle_els_wcqe(phba,
James Smart45ed1192009-10-02 15:17:02 -040011371 (struct lpfc_wcqe_complete *)&cqevt);
James Smart4f774512009-05-22 14:52:35 -040011372 break;
11373 case CQE_CODE_RELEASE_WQE:
11374 /* Process the WQ release event */
11375 lpfc_sli4_sp_handle_rel_wcqe(phba,
James Smart45ed1192009-10-02 15:17:02 -040011376 (struct lpfc_wcqe_release *)&cqevt);
James Smart4f774512009-05-22 14:52:35 -040011377 break;
11378 case CQE_CODE_XRI_ABORTED:
11379 /* Process the WQ XRI abort event */
James Smartbc739052010-08-04 16:11:18 -040011380 phba->last_completion_time = jiffies;
James Smart4f774512009-05-22 14:52:35 -040011381 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
James Smart45ed1192009-10-02 15:17:02 -040011382 (struct sli4_wcqe_xri_aborted *)&cqevt);
James Smart4f774512009-05-22 14:52:35 -040011383 break;
James Smart4d9ab992009-10-02 15:16:39 -040011384 case CQE_CODE_RECEIVE:
James Smart7851fe22011-07-22 18:36:52 -040011385 case CQE_CODE_RECEIVE_V1:
James Smart4d9ab992009-10-02 15:16:39 -040011386 /* Process the RQ event */
James Smartbc739052010-08-04 16:11:18 -040011387 phba->last_completion_time = jiffies;
James Smart4d9ab992009-10-02 15:16:39 -040011388 workposted = lpfc_sli4_sp_handle_rcqe(phba,
James Smart45ed1192009-10-02 15:17:02 -040011389 (struct lpfc_rcqe *)&cqevt);
James Smart4d9ab992009-10-02 15:16:39 -040011390 break;
James Smart4f774512009-05-22 14:52:35 -040011391 default:
11392 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11393 "0388 Not a valid WCQE code: x%x\n",
James Smart45ed1192009-10-02 15:17:02 -040011394 bf_get(lpfc_cqe_code, &cqevt));
James Smart4f774512009-05-22 14:52:35 -040011395 break;
11396 }
11397 return workposted;
11398}
11399
11400/**
James Smart4f774512009-05-22 14:52:35 -040011401 * lpfc_sli4_sp_handle_eqe - Process a slow-path event queue entry
11402 * @phba: Pointer to HBA context object.
11403 * @eqe: Pointer to fast-path event queue entry.
11404 *
11405 * This routine process a event queue entry from the slow-path event queue.
11406 * It will check the MajorCode and MinorCode to determine this is for a
11407 * completion event on a completion queue, if not, an error shall be logged
11408 * and just return. Otherwise, it will get to the corresponding completion
11409 * queue and process all the entries on that completion queue, rearm the
11410 * completion queue, and then return.
11411 *
11412 **/
11413static void
11414lpfc_sli4_sp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe)
11415{
11416 struct lpfc_queue *cq = NULL, *childq, *speq;
11417 struct lpfc_cqe *cqe;
11418 bool workposted = false;
11419 int ecount = 0;
11420 uint16_t cqid;
11421
James Smartcb5172e2010-03-15 11:25:07 -040011422 if (bf_get_le32(lpfc_eqe_major_code, eqe) != 0) {
James Smart4f774512009-05-22 14:52:35 -040011423 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11424 "0359 Not a valid slow-path completion "
11425 "event: majorcode=x%x, minorcode=x%x\n",
James Smartcb5172e2010-03-15 11:25:07 -040011426 bf_get_le32(lpfc_eqe_major_code, eqe),
11427 bf_get_le32(lpfc_eqe_minor_code, eqe));
James Smart4f774512009-05-22 14:52:35 -040011428 return;
11429 }
11430
11431 /* Get the reference to the corresponding CQ */
James Smartcb5172e2010-03-15 11:25:07 -040011432 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
James Smart4f774512009-05-22 14:52:35 -040011433
11434 /* Search for completion queue pointer matching this cqid */
11435 speq = phba->sli4_hba.sp_eq;
James Smart2e90f4b2011-12-13 13:22:37 -050011436 /* sanity check on queue memory */
11437 if (unlikely(!speq))
11438 return;
James Smart4f774512009-05-22 14:52:35 -040011439 list_for_each_entry(childq, &speq->child_list, list) {
11440 if (childq->queue_id == cqid) {
11441 cq = childq;
11442 break;
11443 }
11444 }
11445 if (unlikely(!cq)) {
James Smart75baf692010-06-08 18:31:21 -040011446 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
11447 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11448 "0365 Slow-path CQ identifier "
11449 "(%d) does not exist\n", cqid);
James Smart4f774512009-05-22 14:52:35 -040011450 return;
11451 }
11452
11453 /* Process all the entries to the CQ */
11454 switch (cq->type) {
11455 case LPFC_MCQ:
11456 while ((cqe = lpfc_sli4_cq_get(cq))) {
11457 workposted |= lpfc_sli4_sp_handle_mcqe(phba, cqe);
James Smart73d91e52011-10-10 21:32:10 -040011458 if (!(++ecount % cq->entry_repost))
James Smart4f774512009-05-22 14:52:35 -040011459 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
11460 }
11461 break;
11462 case LPFC_WCQ:
11463 while ((cqe = lpfc_sli4_cq_get(cq))) {
James Smart05580562011-05-24 11:40:48 -040011464 if (cq->subtype == LPFC_FCP)
11465 workposted |= lpfc_sli4_fp_handle_wcqe(phba, cq,
11466 cqe);
11467 else
11468 workposted |= lpfc_sli4_sp_handle_cqe(phba, cq,
11469 cqe);
James Smart73d91e52011-10-10 21:32:10 -040011470 if (!(++ecount % cq->entry_repost))
James Smart4f774512009-05-22 14:52:35 -040011471 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
11472 }
11473 break;
11474 default:
11475 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11476 "0370 Invalid completion queue type (%d)\n",
11477 cq->type);
11478 return;
11479 }
11480
11481 /* Catch the no cq entry condition, log an error */
11482 if (unlikely(ecount == 0))
11483 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11484 "0371 No entry from the CQ: identifier "
11485 "(x%x), type (%d)\n", cq->queue_id, cq->type);
11486
11487 /* In any case, flash and re-arm the RCQ */
11488 lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
11489
11490 /* wake up worker thread if there are works to be done */
11491 if (workposted)
11492 lpfc_worker_wake_up(phba);
11493}
11494
11495/**
11496 * lpfc_sli4_fp_handle_fcp_wcqe - Process fast-path work queue completion entry
11497 * @eqe: Pointer to fast-path completion queue entry.
11498 *
11499 * This routine process a fast-path work queue completion entry from fast-path
11500 * event queue for FCP command response completion.
11501 **/
11502static void
11503lpfc_sli4_fp_handle_fcp_wcqe(struct lpfc_hba *phba,
11504 struct lpfc_wcqe_complete *wcqe)
11505{
11506 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_FCP_RING];
11507 struct lpfc_iocbq *cmdiocbq;
11508 struct lpfc_iocbq irspiocbq;
11509 unsigned long iflags;
11510
11511 spin_lock_irqsave(&phba->hbalock, iflags);
11512 pring->stats.iocb_event++;
11513 spin_unlock_irqrestore(&phba->hbalock, iflags);
11514
11515 /* Check for response status */
11516 if (unlikely(bf_get(lpfc_wcqe_c_status, wcqe))) {
11517 /* If resource errors reported from HBA, reduce queue
11518 * depth of the SCSI device.
11519 */
11520 if ((bf_get(lpfc_wcqe_c_status, wcqe) ==
11521 IOSTAT_LOCAL_REJECT) &&
11522 (wcqe->parameter == IOERR_NO_RESOURCES)) {
11523 phba->lpfc_rampdown_queue_depth(phba);
11524 }
11525 /* Log the error status */
11526 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11527 "0373 FCP complete error: status=x%x, "
11528 "hw_status=x%x, total_data_specified=%d, "
11529 "parameter=x%x, word3=x%x\n",
11530 bf_get(lpfc_wcqe_c_status, wcqe),
11531 bf_get(lpfc_wcqe_c_hw_status, wcqe),
11532 wcqe->total_data_placed, wcqe->parameter,
11533 wcqe->word3);
11534 }
11535
11536 /* Look up the FCP command IOCB and create pseudo response IOCB */
11537 spin_lock_irqsave(&phba->hbalock, iflags);
11538 cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
11539 bf_get(lpfc_wcqe_c_request_tag, wcqe));
11540 spin_unlock_irqrestore(&phba->hbalock, iflags);
11541 if (unlikely(!cmdiocbq)) {
11542 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11543 "0374 FCP complete with no corresponding "
11544 "cmdiocb: iotag (%d)\n",
11545 bf_get(lpfc_wcqe_c_request_tag, wcqe));
11546 return;
11547 }
11548 if (unlikely(!cmdiocbq->iocb_cmpl)) {
11549 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11550 "0375 FCP cmdiocb not callback function "
11551 "iotag: (%d)\n",
11552 bf_get(lpfc_wcqe_c_request_tag, wcqe));
11553 return;
11554 }
11555
11556 /* Fake the irspiocb and copy necessary response information */
James Smart341af102010-01-26 23:07:37 -050011557 lpfc_sli4_iocb_param_transfer(phba, &irspiocbq, cmdiocbq, wcqe);
James Smart4f774512009-05-22 14:52:35 -040011558
James Smart0f65ff62010-02-26 14:14:23 -050011559 if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED) {
11560 spin_lock_irqsave(&phba->hbalock, iflags);
11561 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
11562 spin_unlock_irqrestore(&phba->hbalock, iflags);
11563 }
11564
James Smart4f774512009-05-22 14:52:35 -040011565 /* Pass the cmd_iocb and the rsp state to the upper layer */
11566 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq, &irspiocbq);
11567}
11568
11569/**
11570 * lpfc_sli4_fp_handle_rel_wcqe - Handle fast-path WQ entry consumed event
11571 * @phba: Pointer to HBA context object.
11572 * @cq: Pointer to completion queue.
11573 * @wcqe: Pointer to work-queue completion queue entry.
11574 *
11575 * This routine handles an fast-path WQ entry comsumed event by invoking the
11576 * proper WQ release routine to the slow-path WQ.
11577 **/
11578static void
11579lpfc_sli4_fp_handle_rel_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
11580 struct lpfc_wcqe_release *wcqe)
11581{
11582 struct lpfc_queue *childwq;
11583 bool wqid_matched = false;
11584 uint16_t fcp_wqid;
11585
11586 /* Check for fast-path FCP work queue release */
11587 fcp_wqid = bf_get(lpfc_wcqe_r_wq_id, wcqe);
11588 list_for_each_entry(childwq, &cq->child_list, list) {
11589 if (childwq->queue_id == fcp_wqid) {
11590 lpfc_sli4_wq_release(childwq,
11591 bf_get(lpfc_wcqe_r_wqe_index, wcqe));
11592 wqid_matched = true;
11593 break;
11594 }
11595 }
11596 /* Report warning log message if no match found */
11597 if (wqid_matched != true)
11598 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11599 "2580 Fast-path wqe consume event carries "
11600 "miss-matched qid: wcqe-qid=x%x\n", fcp_wqid);
11601}
11602
11603/**
11604 * lpfc_sli4_fp_handle_wcqe - Process fast-path work queue completion entry
11605 * @cq: Pointer to the completion queue.
11606 * @eqe: Pointer to fast-path completion queue entry.
11607 *
11608 * This routine process a fast-path work queue completion entry from fast-path
11609 * event queue for FCP command response completion.
11610 **/
11611static int
11612lpfc_sli4_fp_handle_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
11613 struct lpfc_cqe *cqe)
11614{
11615 struct lpfc_wcqe_release wcqe;
11616 bool workposted = false;
11617
11618 /* Copy the work queue CQE and convert endian order if needed */
11619 lpfc_sli_pcimem_bcopy(cqe, &wcqe, sizeof(struct lpfc_cqe));
11620
11621 /* Check and process for different type of WCQE and dispatch */
11622 switch (bf_get(lpfc_wcqe_c_code, &wcqe)) {
11623 case CQE_CODE_COMPL_WQE:
11624 /* Process the WQ complete event */
James Smart98fc5dd2010-06-07 15:24:29 -040011625 phba->last_completion_time = jiffies;
James Smart4f774512009-05-22 14:52:35 -040011626 lpfc_sli4_fp_handle_fcp_wcqe(phba,
11627 (struct lpfc_wcqe_complete *)&wcqe);
11628 break;
11629 case CQE_CODE_RELEASE_WQE:
11630 /* Process the WQ release event */
11631 lpfc_sli4_fp_handle_rel_wcqe(phba, cq,
11632 (struct lpfc_wcqe_release *)&wcqe);
11633 break;
11634 case CQE_CODE_XRI_ABORTED:
11635 /* Process the WQ XRI abort event */
James Smartbc739052010-08-04 16:11:18 -040011636 phba->last_completion_time = jiffies;
James Smart4f774512009-05-22 14:52:35 -040011637 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
11638 (struct sli4_wcqe_xri_aborted *)&wcqe);
11639 break;
11640 default:
11641 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11642 "0144 Not a valid WCQE code: x%x\n",
11643 bf_get(lpfc_wcqe_c_code, &wcqe));
11644 break;
11645 }
11646 return workposted;
11647}
11648
11649/**
11650 * lpfc_sli4_fp_handle_eqe - Process a fast-path event queue entry
11651 * @phba: Pointer to HBA context object.
11652 * @eqe: Pointer to fast-path event queue entry.
11653 *
11654 * This routine process a event queue entry from the fast-path event queue.
11655 * It will check the MajorCode and MinorCode to determine this is for a
11656 * completion event on a completion queue, if not, an error shall be logged
11657 * and just return. Otherwise, it will get to the corresponding completion
11658 * queue and process all the entries on the completion queue, rearm the
11659 * completion queue, and then return.
11660 **/
11661static void
11662lpfc_sli4_fp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
11663 uint32_t fcp_cqidx)
11664{
11665 struct lpfc_queue *cq;
11666 struct lpfc_cqe *cqe;
11667 bool workposted = false;
11668 uint16_t cqid;
11669 int ecount = 0;
11670
James Smartcb5172e2010-03-15 11:25:07 -040011671 if (unlikely(bf_get_le32(lpfc_eqe_major_code, eqe) != 0)) {
James Smart4f774512009-05-22 14:52:35 -040011672 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11673 "0366 Not a valid fast-path completion "
11674 "event: majorcode=x%x, minorcode=x%x\n",
James Smartcb5172e2010-03-15 11:25:07 -040011675 bf_get_le32(lpfc_eqe_major_code, eqe),
11676 bf_get_le32(lpfc_eqe_minor_code, eqe));
James Smart4f774512009-05-22 14:52:35 -040011677 return;
11678 }
11679
James Smart2e90f4b2011-12-13 13:22:37 -050011680 if (unlikely(!phba->sli4_hba.fcp_cq)) {
11681 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11682 "3146 Fast-path completion queues "
11683 "does not exist\n");
11684 return;
11685 }
James Smart4f774512009-05-22 14:52:35 -040011686 cq = phba->sli4_hba.fcp_cq[fcp_cqidx];
11687 if (unlikely(!cq)) {
James Smart75baf692010-06-08 18:31:21 -040011688 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
11689 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11690 "0367 Fast-path completion queue "
James Smart2e90f4b2011-12-13 13:22:37 -050011691 "(%d) does not exist\n", fcp_cqidx);
James Smart4f774512009-05-22 14:52:35 -040011692 return;
11693 }
11694
11695 /* Get the reference to the corresponding CQ */
James Smartcb5172e2010-03-15 11:25:07 -040011696 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
James Smart4f774512009-05-22 14:52:35 -040011697 if (unlikely(cqid != cq->queue_id)) {
11698 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11699 "0368 Miss-matched fast-path completion "
11700 "queue identifier: eqcqid=%d, fcpcqid=%d\n",
11701 cqid, cq->queue_id);
11702 return;
11703 }
11704
11705 /* Process all the entries to the CQ */
11706 while ((cqe = lpfc_sli4_cq_get(cq))) {
11707 workposted |= lpfc_sli4_fp_handle_wcqe(phba, cq, cqe);
James Smart73d91e52011-10-10 21:32:10 -040011708 if (!(++ecount % cq->entry_repost))
James Smart4f774512009-05-22 14:52:35 -040011709 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
11710 }
11711
11712 /* Catch the no cq entry condition */
11713 if (unlikely(ecount == 0))
11714 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11715 "0369 No entry from fast-path completion "
11716 "queue fcpcqid=%d\n", cq->queue_id);
11717
11718 /* In any case, flash and re-arm the CQ */
11719 lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
11720
11721 /* wake up worker thread if there are works to be done */
11722 if (workposted)
11723 lpfc_worker_wake_up(phba);
11724}
11725
11726static void
11727lpfc_sli4_eq_flush(struct lpfc_hba *phba, struct lpfc_queue *eq)
11728{
11729 struct lpfc_eqe *eqe;
11730
11731 /* walk all the EQ entries and drop on the floor */
11732 while ((eqe = lpfc_sli4_eq_get(eq)))
11733 ;
11734
11735 /* Clear and re-arm the EQ */
11736 lpfc_sli4_eq_release(eq, LPFC_QUEUE_REARM);
11737}
11738
11739/**
11740 * lpfc_sli4_sp_intr_handler - Slow-path interrupt handler to SLI-4 device
11741 * @irq: Interrupt number.
11742 * @dev_id: The device context pointer.
11743 *
11744 * This function is directly called from the PCI layer as an interrupt
11745 * service routine when device with SLI-4 interface spec is enabled with
11746 * MSI-X multi-message interrupt mode and there are slow-path events in
11747 * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
11748 * interrupt mode, this function is called as part of the device-level
11749 * interrupt handler. When the PCI slot is in error recovery or the HBA is
11750 * undergoing initialization, the interrupt handler will not process the
11751 * interrupt. The link attention and ELS ring attention events are handled
11752 * by the worker thread. The interrupt handler signals the worker thread
11753 * and returns for these events. This function is called without any lock
11754 * held. It gets the hbalock to access and update SLI data structures.
11755 *
11756 * This function returns IRQ_HANDLED when interrupt is handled else it
11757 * returns IRQ_NONE.
11758 **/
11759irqreturn_t
11760lpfc_sli4_sp_intr_handler(int irq, void *dev_id)
11761{
11762 struct lpfc_hba *phba;
11763 struct lpfc_queue *speq;
11764 struct lpfc_eqe *eqe;
11765 unsigned long iflag;
11766 int ecount = 0;
11767
11768 /*
11769 * Get the driver's phba structure from the dev_id
11770 */
11771 phba = (struct lpfc_hba *)dev_id;
11772
11773 if (unlikely(!phba))
11774 return IRQ_NONE;
11775
11776 /* Get to the EQ struct associated with this vector */
11777 speq = phba->sli4_hba.sp_eq;
James Smart5350d872011-10-10 21:33:49 -040011778 if (unlikely(!speq))
11779 return IRQ_NONE;
James Smart4f774512009-05-22 14:52:35 -040011780
11781 /* Check device state for handling interrupt */
11782 if (unlikely(lpfc_intr_state_check(phba))) {
11783 /* Check again for link_state with lock held */
11784 spin_lock_irqsave(&phba->hbalock, iflag);
11785 if (phba->link_state < LPFC_LINK_DOWN)
11786 /* Flush, clear interrupt, and rearm the EQ */
11787 lpfc_sli4_eq_flush(phba, speq);
11788 spin_unlock_irqrestore(&phba->hbalock, iflag);
11789 return IRQ_NONE;
11790 }
11791
11792 /*
11793 * Process all the event on FCP slow-path EQ
11794 */
11795 while ((eqe = lpfc_sli4_eq_get(speq))) {
11796 lpfc_sli4_sp_handle_eqe(phba, eqe);
James Smart73d91e52011-10-10 21:32:10 -040011797 if (!(++ecount % speq->entry_repost))
James Smart4f774512009-05-22 14:52:35 -040011798 lpfc_sli4_eq_release(speq, LPFC_QUEUE_NOARM);
11799 }
11800
11801 /* Always clear and re-arm the slow-path EQ */
11802 lpfc_sli4_eq_release(speq, LPFC_QUEUE_REARM);
11803
11804 /* Catch the no cq entry condition */
11805 if (unlikely(ecount == 0)) {
11806 if (phba->intr_type == MSIX)
11807 /* MSI-X treated interrupt served as no EQ share INT */
11808 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11809 "0357 MSI-X interrupt with no EQE\n");
11810 else
11811 /* Non MSI-X treated on interrupt as EQ share INT */
11812 return IRQ_NONE;
11813 }
11814
11815 return IRQ_HANDLED;
11816} /* lpfc_sli4_sp_intr_handler */
11817
11818/**
11819 * lpfc_sli4_fp_intr_handler - Fast-path interrupt handler to SLI-4 device
11820 * @irq: Interrupt number.
11821 * @dev_id: The device context pointer.
11822 *
11823 * This function is directly called from the PCI layer as an interrupt
11824 * service routine when device with SLI-4 interface spec is enabled with
11825 * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
11826 * ring event in the HBA. However, when the device is enabled with either
11827 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
11828 * device-level interrupt handler. When the PCI slot is in error recovery
11829 * or the HBA is undergoing initialization, the interrupt handler will not
11830 * process the interrupt. The SCSI FCP fast-path ring event are handled in
11831 * the intrrupt context. This function is called without any lock held.
11832 * It gets the hbalock to access and update SLI data structures. Note that,
11833 * the FCP EQ to FCP CQ are one-to-one map such that the FCP EQ index is
11834 * equal to that of FCP CQ index.
11835 *
11836 * This function returns IRQ_HANDLED when interrupt is handled else it
11837 * returns IRQ_NONE.
11838 **/
11839irqreturn_t
11840lpfc_sli4_fp_intr_handler(int irq, void *dev_id)
11841{
11842 struct lpfc_hba *phba;
11843 struct lpfc_fcp_eq_hdl *fcp_eq_hdl;
11844 struct lpfc_queue *fpeq;
11845 struct lpfc_eqe *eqe;
11846 unsigned long iflag;
11847 int ecount = 0;
11848 uint32_t fcp_eqidx;
11849
11850 /* Get the driver's phba structure from the dev_id */
11851 fcp_eq_hdl = (struct lpfc_fcp_eq_hdl *)dev_id;
11852 phba = fcp_eq_hdl->phba;
11853 fcp_eqidx = fcp_eq_hdl->idx;
11854
11855 if (unlikely(!phba))
11856 return IRQ_NONE;
James Smart5350d872011-10-10 21:33:49 -040011857 if (unlikely(!phba->sli4_hba.fp_eq))
11858 return IRQ_NONE;
James Smart4f774512009-05-22 14:52:35 -040011859
11860 /* Get to the EQ struct associated with this vector */
11861 fpeq = phba->sli4_hba.fp_eq[fcp_eqidx];
James Smart2e90f4b2011-12-13 13:22:37 -050011862 if (unlikely(!fpeq))
11863 return IRQ_NONE;
James Smart4f774512009-05-22 14:52:35 -040011864
11865 /* Check device state for handling interrupt */
11866 if (unlikely(lpfc_intr_state_check(phba))) {
11867 /* Check again for link_state with lock held */
11868 spin_lock_irqsave(&phba->hbalock, iflag);
11869 if (phba->link_state < LPFC_LINK_DOWN)
11870 /* Flush, clear interrupt, and rearm the EQ */
11871 lpfc_sli4_eq_flush(phba, fpeq);
11872 spin_unlock_irqrestore(&phba->hbalock, iflag);
11873 return IRQ_NONE;
11874 }
11875
11876 /*
11877 * Process all the event on FCP fast-path EQ
11878 */
11879 while ((eqe = lpfc_sli4_eq_get(fpeq))) {
11880 lpfc_sli4_fp_handle_eqe(phba, eqe, fcp_eqidx);
James Smart73d91e52011-10-10 21:32:10 -040011881 if (!(++ecount % fpeq->entry_repost))
James Smart4f774512009-05-22 14:52:35 -040011882 lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_NOARM);
11883 }
11884
11885 /* Always clear and re-arm the fast-path EQ */
11886 lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_REARM);
11887
11888 if (unlikely(ecount == 0)) {
11889 if (phba->intr_type == MSIX)
11890 /* MSI-X treated interrupt served as no EQ share INT */
11891 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11892 "0358 MSI-X interrupt with no EQE\n");
11893 else
11894 /* Non MSI-X treated on interrupt as EQ share INT */
11895 return IRQ_NONE;
11896 }
11897
11898 return IRQ_HANDLED;
11899} /* lpfc_sli4_fp_intr_handler */
11900
11901/**
11902 * lpfc_sli4_intr_handler - Device-level interrupt handler for SLI-4 device
11903 * @irq: Interrupt number.
11904 * @dev_id: The device context pointer.
11905 *
11906 * This function is the device-level interrupt handler to device with SLI-4
11907 * interface spec, called from the PCI layer when either MSI or Pin-IRQ
11908 * interrupt mode is enabled and there is an event in the HBA which requires
11909 * driver attention. This function invokes the slow-path interrupt attention
11910 * handling function and fast-path interrupt attention handling function in
11911 * turn to process the relevant HBA attention events. This function is called
11912 * without any lock held. It gets the hbalock to access and update SLI data
11913 * structures.
11914 *
11915 * This function returns IRQ_HANDLED when interrupt is handled, else it
11916 * returns IRQ_NONE.
11917 **/
11918irqreturn_t
11919lpfc_sli4_intr_handler(int irq, void *dev_id)
11920{
11921 struct lpfc_hba *phba;
11922 irqreturn_t sp_irq_rc, fp_irq_rc;
11923 bool fp_handled = false;
11924 uint32_t fcp_eqidx;
11925
11926 /* Get the driver's phba structure from the dev_id */
11927 phba = (struct lpfc_hba *)dev_id;
11928
11929 if (unlikely(!phba))
11930 return IRQ_NONE;
11931
11932 /*
11933 * Invokes slow-path host attention interrupt handling as appropriate.
11934 */
11935 sp_irq_rc = lpfc_sli4_sp_intr_handler(irq, dev_id);
11936
11937 /*
11938 * Invoke fast-path host attention interrupt handling as appropriate.
11939 */
11940 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count; fcp_eqidx++) {
11941 fp_irq_rc = lpfc_sli4_fp_intr_handler(irq,
11942 &phba->sli4_hba.fcp_eq_hdl[fcp_eqidx]);
11943 if (fp_irq_rc == IRQ_HANDLED)
11944 fp_handled |= true;
11945 }
11946
11947 return (fp_handled == true) ? IRQ_HANDLED : sp_irq_rc;
11948} /* lpfc_sli4_intr_handler */
11949
11950/**
11951 * lpfc_sli4_queue_free - free a queue structure and associated memory
11952 * @queue: The queue structure to free.
11953 *
Uwe Kleine-Königb5950762010-11-01 15:38:34 -040011954 * This function frees a queue structure and the DMAable memory used for
James Smart4f774512009-05-22 14:52:35 -040011955 * the host resident queue. This function must be called after destroying the
11956 * queue on the HBA.
11957 **/
11958void
11959lpfc_sli4_queue_free(struct lpfc_queue *queue)
11960{
11961 struct lpfc_dmabuf *dmabuf;
11962
11963 if (!queue)
11964 return;
11965
11966 while (!list_empty(&queue->page_list)) {
11967 list_remove_head(&queue->page_list, dmabuf, struct lpfc_dmabuf,
11968 list);
James Smart49198b32010-04-06 15:04:33 -040011969 dma_free_coherent(&queue->phba->pcidev->dev, SLI4_PAGE_SIZE,
James Smart4f774512009-05-22 14:52:35 -040011970 dmabuf->virt, dmabuf->phys);
11971 kfree(dmabuf);
11972 }
11973 kfree(queue);
11974 return;
11975}
11976
11977/**
11978 * lpfc_sli4_queue_alloc - Allocate and initialize a queue structure
11979 * @phba: The HBA that this queue is being created on.
11980 * @entry_size: The size of each queue entry for this queue.
11981 * @entry count: The number of entries that this queue will handle.
11982 *
11983 * This function allocates a queue structure and the DMAable memory used for
11984 * the host resident queue. This function must be called before creating the
11985 * queue on the HBA.
11986 **/
11987struct lpfc_queue *
11988lpfc_sli4_queue_alloc(struct lpfc_hba *phba, uint32_t entry_size,
11989 uint32_t entry_count)
11990{
11991 struct lpfc_queue *queue;
11992 struct lpfc_dmabuf *dmabuf;
11993 int x, total_qe_count;
11994 void *dma_pointer;
James Smartcb5172e2010-03-15 11:25:07 -040011995 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smart4f774512009-05-22 14:52:35 -040011996
James Smartcb5172e2010-03-15 11:25:07 -040011997 if (!phba->sli4_hba.pc_sli4_params.supported)
11998 hw_page_size = SLI4_PAGE_SIZE;
11999
James Smart4f774512009-05-22 14:52:35 -040012000 queue = kzalloc(sizeof(struct lpfc_queue) +
12001 (sizeof(union sli4_qe) * entry_count), GFP_KERNEL);
12002 if (!queue)
12003 return NULL;
James Smartcb5172e2010-03-15 11:25:07 -040012004 queue->page_count = (ALIGN(entry_size * entry_count,
12005 hw_page_size))/hw_page_size;
James Smart4f774512009-05-22 14:52:35 -040012006 INIT_LIST_HEAD(&queue->list);
12007 INIT_LIST_HEAD(&queue->page_list);
12008 INIT_LIST_HEAD(&queue->child_list);
12009 for (x = 0, total_qe_count = 0; x < queue->page_count; x++) {
12010 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
12011 if (!dmabuf)
12012 goto out_fail;
12013 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
James Smartcb5172e2010-03-15 11:25:07 -040012014 hw_page_size, &dmabuf->phys,
James Smart4f774512009-05-22 14:52:35 -040012015 GFP_KERNEL);
12016 if (!dmabuf->virt) {
12017 kfree(dmabuf);
12018 goto out_fail;
12019 }
James Smartcb5172e2010-03-15 11:25:07 -040012020 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -040012021 dmabuf->buffer_tag = x;
12022 list_add_tail(&dmabuf->list, &queue->page_list);
12023 /* initialize queue's entry array */
12024 dma_pointer = dmabuf->virt;
12025 for (; total_qe_count < entry_count &&
James Smartcb5172e2010-03-15 11:25:07 -040012026 dma_pointer < (hw_page_size + dmabuf->virt);
James Smart4f774512009-05-22 14:52:35 -040012027 total_qe_count++, dma_pointer += entry_size) {
12028 queue->qe[total_qe_count].address = dma_pointer;
12029 }
12030 }
12031 queue->entry_size = entry_size;
12032 queue->entry_count = entry_count;
James Smart73d91e52011-10-10 21:32:10 -040012033
12034 /*
12035 * entry_repost is calculated based on the number of entries in the
12036 * queue. This works out except for RQs. If buffers are NOT initially
12037 * posted for every RQE, entry_repost should be adjusted accordingly.
12038 */
12039 queue->entry_repost = (entry_count >> 3);
12040 if (queue->entry_repost < LPFC_QUEUE_MIN_REPOST)
12041 queue->entry_repost = LPFC_QUEUE_MIN_REPOST;
James Smart4f774512009-05-22 14:52:35 -040012042 queue->phba = phba;
12043
12044 return queue;
12045out_fail:
12046 lpfc_sli4_queue_free(queue);
12047 return NULL;
12048}
12049
12050/**
12051 * lpfc_eq_create - Create an Event Queue on the HBA
12052 * @phba: HBA structure that indicates port to create a queue on.
12053 * @eq: The queue structure to use to create the event queue.
12054 * @imax: The maximum interrupt per second limit.
12055 *
12056 * This function creates an event queue, as detailed in @eq, on a port,
12057 * described by @phba by sending an EQ_CREATE mailbox command to the HBA.
12058 *
12059 * The @phba struct is used to send mailbox command to HBA. The @eq struct
12060 * is used to get the entry count and entry size that are necessary to
12061 * determine the number of pages to allocate and use for this queue. This
12062 * function will send the EQ_CREATE mailbox command to the HBA to setup the
12063 * event queue. This function is asynchronous and will wait for the mailbox
12064 * command to finish before continuing.
12065 *
12066 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040012067 * memory this function will return -ENOMEM. If the queue create mailbox command
12068 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040012069 **/
12070uint32_t
12071lpfc_eq_create(struct lpfc_hba *phba, struct lpfc_queue *eq, uint16_t imax)
12072{
12073 struct lpfc_mbx_eq_create *eq_create;
12074 LPFC_MBOXQ_t *mbox;
12075 int rc, length, status = 0;
12076 struct lpfc_dmabuf *dmabuf;
12077 uint32_t shdr_status, shdr_add_status;
12078 union lpfc_sli4_cfg_shdr *shdr;
12079 uint16_t dmult;
James Smart49198b32010-04-06 15:04:33 -040012080 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
12081
James Smart2e90f4b2011-12-13 13:22:37 -050012082 /* sanity check on queue memory */
12083 if (!eq)
12084 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040012085 if (!phba->sli4_hba.pc_sli4_params.supported)
12086 hw_page_size = SLI4_PAGE_SIZE;
James Smart4f774512009-05-22 14:52:35 -040012087
12088 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12089 if (!mbox)
12090 return -ENOMEM;
12091 length = (sizeof(struct lpfc_mbx_eq_create) -
12092 sizeof(struct lpfc_sli4_cfg_mhdr));
12093 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
12094 LPFC_MBOX_OPCODE_EQ_CREATE,
12095 length, LPFC_SLI4_MBX_EMBED);
12096 eq_create = &mbox->u.mqe.un.eq_create;
12097 bf_set(lpfc_mbx_eq_create_num_pages, &eq_create->u.request,
12098 eq->page_count);
12099 bf_set(lpfc_eq_context_size, &eq_create->u.request.context,
12100 LPFC_EQE_SIZE);
12101 bf_set(lpfc_eq_context_valid, &eq_create->u.request.context, 1);
12102 /* Calculate delay multiper from maximum interrupt per second */
12103 dmult = LPFC_DMULT_CONST/imax - 1;
12104 bf_set(lpfc_eq_context_delay_multi, &eq_create->u.request.context,
12105 dmult);
12106 switch (eq->entry_count) {
12107 default:
12108 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12109 "0360 Unsupported EQ count. (%d)\n",
12110 eq->entry_count);
12111 if (eq->entry_count < 256)
12112 return -EINVAL;
12113 /* otherwise default to smallest count (drop through) */
12114 case 256:
12115 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
12116 LPFC_EQ_CNT_256);
12117 break;
12118 case 512:
12119 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
12120 LPFC_EQ_CNT_512);
12121 break;
12122 case 1024:
12123 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
12124 LPFC_EQ_CNT_1024);
12125 break;
12126 case 2048:
12127 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
12128 LPFC_EQ_CNT_2048);
12129 break;
12130 case 4096:
12131 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
12132 LPFC_EQ_CNT_4096);
12133 break;
12134 }
12135 list_for_each_entry(dmabuf, &eq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040012136 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -040012137 eq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
12138 putPaddrLow(dmabuf->phys);
12139 eq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
12140 putPaddrHigh(dmabuf->phys);
12141 }
12142 mbox->vport = phba->pport;
12143 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
12144 mbox->context1 = NULL;
12145 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
12146 shdr = (union lpfc_sli4_cfg_shdr *) &eq_create->header.cfg_shdr;
12147 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12148 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12149 if (shdr_status || shdr_add_status || rc) {
12150 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12151 "2500 EQ_CREATE mailbox failed with "
12152 "status x%x add_status x%x, mbx status x%x\n",
12153 shdr_status, shdr_add_status, rc);
12154 status = -ENXIO;
12155 }
12156 eq->type = LPFC_EQ;
12157 eq->subtype = LPFC_NONE;
12158 eq->queue_id = bf_get(lpfc_mbx_eq_create_q_id, &eq_create->u.response);
12159 if (eq->queue_id == 0xFFFF)
12160 status = -ENXIO;
12161 eq->host_index = 0;
12162 eq->hba_index = 0;
12163
James Smart8fa38512009-07-19 10:01:03 -040012164 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040012165 return status;
12166}
12167
12168/**
12169 * lpfc_cq_create - Create a Completion Queue on the HBA
12170 * @phba: HBA structure that indicates port to create a queue on.
12171 * @cq: The queue structure to use to create the completion queue.
12172 * @eq: The event queue to bind this completion queue to.
12173 *
12174 * This function creates a completion queue, as detailed in @wq, on a port,
12175 * described by @phba by sending a CQ_CREATE mailbox command to the HBA.
12176 *
12177 * The @phba struct is used to send mailbox command to HBA. The @cq struct
12178 * is used to get the entry count and entry size that are necessary to
12179 * determine the number of pages to allocate and use for this queue. The @eq
12180 * is used to indicate which event queue to bind this completion queue to. This
12181 * function will send the CQ_CREATE mailbox command to the HBA to setup the
12182 * completion queue. This function is asynchronous and will wait for the mailbox
12183 * command to finish before continuing.
12184 *
12185 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040012186 * memory this function will return -ENOMEM. If the queue create mailbox command
12187 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040012188 **/
12189uint32_t
12190lpfc_cq_create(struct lpfc_hba *phba, struct lpfc_queue *cq,
12191 struct lpfc_queue *eq, uint32_t type, uint32_t subtype)
12192{
12193 struct lpfc_mbx_cq_create *cq_create;
12194 struct lpfc_dmabuf *dmabuf;
12195 LPFC_MBOXQ_t *mbox;
12196 int rc, length, status = 0;
12197 uint32_t shdr_status, shdr_add_status;
12198 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040012199 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
12200
James Smart2e90f4b2011-12-13 13:22:37 -050012201 /* sanity check on queue memory */
12202 if (!cq || !eq)
12203 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040012204 if (!phba->sli4_hba.pc_sli4_params.supported)
12205 hw_page_size = SLI4_PAGE_SIZE;
12206
James Smart4f774512009-05-22 14:52:35 -040012207 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12208 if (!mbox)
12209 return -ENOMEM;
12210 length = (sizeof(struct lpfc_mbx_cq_create) -
12211 sizeof(struct lpfc_sli4_cfg_mhdr));
12212 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
12213 LPFC_MBOX_OPCODE_CQ_CREATE,
12214 length, LPFC_SLI4_MBX_EMBED);
12215 cq_create = &mbox->u.mqe.un.cq_create;
James Smart5a6f1332011-03-11 16:05:35 -050012216 shdr = (union lpfc_sli4_cfg_shdr *) &cq_create->header.cfg_shdr;
James Smart4f774512009-05-22 14:52:35 -040012217 bf_set(lpfc_mbx_cq_create_num_pages, &cq_create->u.request,
12218 cq->page_count);
12219 bf_set(lpfc_cq_context_event, &cq_create->u.request.context, 1);
12220 bf_set(lpfc_cq_context_valid, &cq_create->u.request.context, 1);
James Smart5a6f1332011-03-11 16:05:35 -050012221 bf_set(lpfc_mbox_hdr_version, &shdr->request,
12222 phba->sli4_hba.pc_sli4_params.cqv);
12223 if (phba->sli4_hba.pc_sli4_params.cqv == LPFC_Q_CREATE_VERSION_2) {
James Smartc31098c2011-04-16 11:03:33 -040012224 /* FW only supports 1. Should be PAGE_SIZE/SLI4_PAGE_SIZE */
12225 bf_set(lpfc_mbx_cq_create_page_size, &cq_create->u.request, 1);
James Smart5a6f1332011-03-11 16:05:35 -050012226 bf_set(lpfc_cq_eq_id_2, &cq_create->u.request.context,
12227 eq->queue_id);
12228 } else {
12229 bf_set(lpfc_cq_eq_id, &cq_create->u.request.context,
12230 eq->queue_id);
12231 }
James Smart4f774512009-05-22 14:52:35 -040012232 switch (cq->entry_count) {
12233 default:
12234 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12235 "0361 Unsupported CQ count. (%d)\n",
12236 cq->entry_count);
James Smart4f4c1862012-06-12 13:54:02 -040012237 if (cq->entry_count < 256) {
12238 status = -EINVAL;
12239 goto out;
12240 }
James Smart4f774512009-05-22 14:52:35 -040012241 /* otherwise default to smallest count (drop through) */
12242 case 256:
12243 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
12244 LPFC_CQ_CNT_256);
12245 break;
12246 case 512:
12247 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
12248 LPFC_CQ_CNT_512);
12249 break;
12250 case 1024:
12251 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
12252 LPFC_CQ_CNT_1024);
12253 break;
12254 }
12255 list_for_each_entry(dmabuf, &cq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040012256 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -040012257 cq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
12258 putPaddrLow(dmabuf->phys);
12259 cq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
12260 putPaddrHigh(dmabuf->phys);
12261 }
12262 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
12263
12264 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart4f774512009-05-22 14:52:35 -040012265 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12266 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12267 if (shdr_status || shdr_add_status || rc) {
12268 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12269 "2501 CQ_CREATE mailbox failed with "
12270 "status x%x add_status x%x, mbx status x%x\n",
12271 shdr_status, shdr_add_status, rc);
12272 status = -ENXIO;
12273 goto out;
12274 }
12275 cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
12276 if (cq->queue_id == 0xFFFF) {
12277 status = -ENXIO;
12278 goto out;
12279 }
12280 /* link the cq onto the parent eq child list */
12281 list_add_tail(&cq->list, &eq->child_list);
12282 /* Set up completion queue's type and subtype */
12283 cq->type = type;
12284 cq->subtype = subtype;
12285 cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
James Smart2a622bf2011-02-16 12:40:06 -050012286 cq->assoc_qid = eq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040012287 cq->host_index = 0;
12288 cq->hba_index = 0;
James Smart4f774512009-05-22 14:52:35 -040012289
James Smart8fa38512009-07-19 10:01:03 -040012290out:
12291 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040012292 return status;
12293}
12294
12295/**
James Smartb19a0612010-04-06 14:48:51 -040012296 * lpfc_mq_create_fb_init - Send MCC_CREATE without async events registration
James Smart04c68492009-05-22 14:52:52 -040012297 * @phba: HBA structure that indicates port to create a queue on.
12298 * @mq: The queue structure to use to create the mailbox queue.
James Smartb19a0612010-04-06 14:48:51 -040012299 * @mbox: An allocated pointer to type LPFC_MBOXQ_t
12300 * @cq: The completion queue to associate with this cq.
James Smart04c68492009-05-22 14:52:52 -040012301 *
James Smartb19a0612010-04-06 14:48:51 -040012302 * This function provides failback (fb) functionality when the
12303 * mq_create_ext fails on older FW generations. It's purpose is identical
12304 * to mq_create_ext otherwise.
James Smart04c68492009-05-22 14:52:52 -040012305 *
James Smartb19a0612010-04-06 14:48:51 -040012306 * This routine cannot fail as all attributes were previously accessed and
12307 * initialized in mq_create_ext.
James Smart04c68492009-05-22 14:52:52 -040012308 **/
James Smartb19a0612010-04-06 14:48:51 -040012309static void
12310lpfc_mq_create_fb_init(struct lpfc_hba *phba, struct lpfc_queue *mq,
12311 LPFC_MBOXQ_t *mbox, struct lpfc_queue *cq)
James Smart04c68492009-05-22 14:52:52 -040012312{
12313 struct lpfc_mbx_mq_create *mq_create;
12314 struct lpfc_dmabuf *dmabuf;
James Smartb19a0612010-04-06 14:48:51 -040012315 int length;
James Smart04c68492009-05-22 14:52:52 -040012316
James Smart04c68492009-05-22 14:52:52 -040012317 length = (sizeof(struct lpfc_mbx_mq_create) -
12318 sizeof(struct lpfc_sli4_cfg_mhdr));
12319 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
12320 LPFC_MBOX_OPCODE_MQ_CREATE,
12321 length, LPFC_SLI4_MBX_EMBED);
12322 mq_create = &mbox->u.mqe.un.mq_create;
12323 bf_set(lpfc_mbx_mq_create_num_pages, &mq_create->u.request,
James Smartb19a0612010-04-06 14:48:51 -040012324 mq->page_count);
James Smart04c68492009-05-22 14:52:52 -040012325 bf_set(lpfc_mq_context_cq_id, &mq_create->u.request.context,
James Smartb19a0612010-04-06 14:48:51 -040012326 cq->queue_id);
James Smart04c68492009-05-22 14:52:52 -040012327 bf_set(lpfc_mq_context_valid, &mq_create->u.request.context, 1);
12328 switch (mq->entry_count) {
James Smart04c68492009-05-22 14:52:52 -040012329 case 16:
James Smart5a6f1332011-03-11 16:05:35 -050012330 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
12331 LPFC_MQ_RING_SIZE_16);
James Smart04c68492009-05-22 14:52:52 -040012332 break;
12333 case 32:
James Smart5a6f1332011-03-11 16:05:35 -050012334 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
12335 LPFC_MQ_RING_SIZE_32);
James Smart04c68492009-05-22 14:52:52 -040012336 break;
12337 case 64:
James Smart5a6f1332011-03-11 16:05:35 -050012338 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
12339 LPFC_MQ_RING_SIZE_64);
James Smart04c68492009-05-22 14:52:52 -040012340 break;
12341 case 128:
James Smart5a6f1332011-03-11 16:05:35 -050012342 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
12343 LPFC_MQ_RING_SIZE_128);
James Smart04c68492009-05-22 14:52:52 -040012344 break;
12345 }
12346 list_for_each_entry(dmabuf, &mq->page_list, list) {
12347 mq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
James Smartb19a0612010-04-06 14:48:51 -040012348 putPaddrLow(dmabuf->phys);
James Smart04c68492009-05-22 14:52:52 -040012349 mq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
James Smartb19a0612010-04-06 14:48:51 -040012350 putPaddrHigh(dmabuf->phys);
12351 }
12352}
12353
12354/**
12355 * lpfc_mq_create - Create a mailbox Queue on the HBA
12356 * @phba: HBA structure that indicates port to create a queue on.
12357 * @mq: The queue structure to use to create the mailbox queue.
12358 * @cq: The completion queue to associate with this cq.
12359 * @subtype: The queue's subtype.
12360 *
12361 * This function creates a mailbox queue, as detailed in @mq, on a port,
12362 * described by @phba by sending a MQ_CREATE mailbox command to the HBA.
12363 *
12364 * The @phba struct is used to send mailbox command to HBA. The @cq struct
12365 * is used to get the entry count and entry size that are necessary to
12366 * determine the number of pages to allocate and use for this queue. This
12367 * function will send the MQ_CREATE mailbox command to the HBA to setup the
12368 * mailbox queue. This function is asynchronous and will wait for the mailbox
12369 * command to finish before continuing.
12370 *
12371 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040012372 * memory this function will return -ENOMEM. If the queue create mailbox command
12373 * fails this function will return -ENXIO.
James Smartb19a0612010-04-06 14:48:51 -040012374 **/
12375int32_t
12376lpfc_mq_create(struct lpfc_hba *phba, struct lpfc_queue *mq,
12377 struct lpfc_queue *cq, uint32_t subtype)
12378{
12379 struct lpfc_mbx_mq_create *mq_create;
12380 struct lpfc_mbx_mq_create_ext *mq_create_ext;
12381 struct lpfc_dmabuf *dmabuf;
12382 LPFC_MBOXQ_t *mbox;
12383 int rc, length, status = 0;
12384 uint32_t shdr_status, shdr_add_status;
12385 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040012386 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smartb19a0612010-04-06 14:48:51 -040012387
James Smart2e90f4b2011-12-13 13:22:37 -050012388 /* sanity check on queue memory */
12389 if (!mq || !cq)
12390 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040012391 if (!phba->sli4_hba.pc_sli4_params.supported)
12392 hw_page_size = SLI4_PAGE_SIZE;
James Smartb19a0612010-04-06 14:48:51 -040012393
12394 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12395 if (!mbox)
12396 return -ENOMEM;
12397 length = (sizeof(struct lpfc_mbx_mq_create_ext) -
12398 sizeof(struct lpfc_sli4_cfg_mhdr));
12399 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
12400 LPFC_MBOX_OPCODE_MQ_CREATE_EXT,
12401 length, LPFC_SLI4_MBX_EMBED);
12402
12403 mq_create_ext = &mbox->u.mqe.un.mq_create_ext;
James Smart5a6f1332011-03-11 16:05:35 -050012404 shdr = (union lpfc_sli4_cfg_shdr *) &mq_create_ext->header.cfg_shdr;
James Smart70f3c072010-12-15 17:57:33 -050012405 bf_set(lpfc_mbx_mq_create_ext_num_pages,
12406 &mq_create_ext->u.request, mq->page_count);
12407 bf_set(lpfc_mbx_mq_create_ext_async_evt_link,
12408 &mq_create_ext->u.request, 1);
12409 bf_set(lpfc_mbx_mq_create_ext_async_evt_fip,
James Smartb19a0612010-04-06 14:48:51 -040012410 &mq_create_ext->u.request, 1);
12411 bf_set(lpfc_mbx_mq_create_ext_async_evt_group5,
12412 &mq_create_ext->u.request, 1);
James Smart70f3c072010-12-15 17:57:33 -050012413 bf_set(lpfc_mbx_mq_create_ext_async_evt_fc,
12414 &mq_create_ext->u.request, 1);
12415 bf_set(lpfc_mbx_mq_create_ext_async_evt_sli,
12416 &mq_create_ext->u.request, 1);
James Smartb19a0612010-04-06 14:48:51 -040012417 bf_set(lpfc_mq_context_valid, &mq_create_ext->u.request.context, 1);
James Smart5a6f1332011-03-11 16:05:35 -050012418 bf_set(lpfc_mbox_hdr_version, &shdr->request,
12419 phba->sli4_hba.pc_sli4_params.mqv);
12420 if (phba->sli4_hba.pc_sli4_params.mqv == LPFC_Q_CREATE_VERSION_1)
12421 bf_set(lpfc_mbx_mq_create_ext_cq_id, &mq_create_ext->u.request,
12422 cq->queue_id);
12423 else
12424 bf_set(lpfc_mq_context_cq_id, &mq_create_ext->u.request.context,
12425 cq->queue_id);
James Smartb19a0612010-04-06 14:48:51 -040012426 switch (mq->entry_count) {
12427 default:
12428 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12429 "0362 Unsupported MQ count. (%d)\n",
12430 mq->entry_count);
James Smart4f4c1862012-06-12 13:54:02 -040012431 if (mq->entry_count < 16) {
12432 status = -EINVAL;
12433 goto out;
12434 }
James Smartb19a0612010-04-06 14:48:51 -040012435 /* otherwise default to smallest count (drop through) */
12436 case 16:
James Smart5a6f1332011-03-11 16:05:35 -050012437 bf_set(lpfc_mq_context_ring_size,
12438 &mq_create_ext->u.request.context,
12439 LPFC_MQ_RING_SIZE_16);
James Smartb19a0612010-04-06 14:48:51 -040012440 break;
12441 case 32:
James Smart5a6f1332011-03-11 16:05:35 -050012442 bf_set(lpfc_mq_context_ring_size,
12443 &mq_create_ext->u.request.context,
12444 LPFC_MQ_RING_SIZE_32);
James Smartb19a0612010-04-06 14:48:51 -040012445 break;
12446 case 64:
James Smart5a6f1332011-03-11 16:05:35 -050012447 bf_set(lpfc_mq_context_ring_size,
12448 &mq_create_ext->u.request.context,
12449 LPFC_MQ_RING_SIZE_64);
James Smartb19a0612010-04-06 14:48:51 -040012450 break;
12451 case 128:
James Smart5a6f1332011-03-11 16:05:35 -050012452 bf_set(lpfc_mq_context_ring_size,
12453 &mq_create_ext->u.request.context,
12454 LPFC_MQ_RING_SIZE_128);
James Smartb19a0612010-04-06 14:48:51 -040012455 break;
12456 }
12457 list_for_each_entry(dmabuf, &mq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040012458 memset(dmabuf->virt, 0, hw_page_size);
James Smartb19a0612010-04-06 14:48:51 -040012459 mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_lo =
12460 putPaddrLow(dmabuf->phys);
12461 mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_hi =
James Smart04c68492009-05-22 14:52:52 -040012462 putPaddrHigh(dmabuf->phys);
12463 }
12464 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
James Smartb19a0612010-04-06 14:48:51 -040012465 mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
12466 &mq_create_ext->u.response);
12467 if (rc != MBX_SUCCESS) {
12468 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
12469 "2795 MQ_CREATE_EXT failed with "
12470 "status x%x. Failback to MQ_CREATE.\n",
12471 rc);
12472 lpfc_mq_create_fb_init(phba, mq, mbox, cq);
12473 mq_create = &mbox->u.mqe.un.mq_create;
12474 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
12475 shdr = (union lpfc_sli4_cfg_shdr *) &mq_create->header.cfg_shdr;
12476 mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
12477 &mq_create->u.response);
12478 }
12479
James Smart04c68492009-05-22 14:52:52 -040012480 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart04c68492009-05-22 14:52:52 -040012481 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12482 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12483 if (shdr_status || shdr_add_status || rc) {
12484 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12485 "2502 MQ_CREATE mailbox failed with "
12486 "status x%x add_status x%x, mbx status x%x\n",
12487 shdr_status, shdr_add_status, rc);
12488 status = -ENXIO;
12489 goto out;
12490 }
James Smart04c68492009-05-22 14:52:52 -040012491 if (mq->queue_id == 0xFFFF) {
12492 status = -ENXIO;
12493 goto out;
12494 }
12495 mq->type = LPFC_MQ;
James Smart2a622bf2011-02-16 12:40:06 -050012496 mq->assoc_qid = cq->queue_id;
James Smart04c68492009-05-22 14:52:52 -040012497 mq->subtype = subtype;
12498 mq->host_index = 0;
12499 mq->hba_index = 0;
12500
12501 /* link the mq onto the parent cq child list */
12502 list_add_tail(&mq->list, &cq->child_list);
12503out:
James Smart8fa38512009-07-19 10:01:03 -040012504 mempool_free(mbox, phba->mbox_mem_pool);
James Smart04c68492009-05-22 14:52:52 -040012505 return status;
12506}
12507
12508/**
James Smart4f774512009-05-22 14:52:35 -040012509 * lpfc_wq_create - Create a Work Queue on the HBA
12510 * @phba: HBA structure that indicates port to create a queue on.
12511 * @wq: The queue structure to use to create the work queue.
12512 * @cq: The completion queue to bind this work queue to.
12513 * @subtype: The subtype of the work queue indicating its functionality.
12514 *
12515 * This function creates a work queue, as detailed in @wq, on a port, described
12516 * by @phba by sending a WQ_CREATE mailbox command to the HBA.
12517 *
12518 * The @phba struct is used to send mailbox command to HBA. The @wq struct
12519 * is used to get the entry count and entry size that are necessary to
12520 * determine the number of pages to allocate and use for this queue. The @cq
12521 * is used to indicate which completion queue to bind this work queue to. This
12522 * function will send the WQ_CREATE mailbox command to the HBA to setup the
12523 * work queue. This function is asynchronous and will wait for the mailbox
12524 * command to finish before continuing.
12525 *
12526 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040012527 * memory this function will return -ENOMEM. If the queue create mailbox command
12528 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040012529 **/
12530uint32_t
12531lpfc_wq_create(struct lpfc_hba *phba, struct lpfc_queue *wq,
12532 struct lpfc_queue *cq, uint32_t subtype)
12533{
12534 struct lpfc_mbx_wq_create *wq_create;
12535 struct lpfc_dmabuf *dmabuf;
12536 LPFC_MBOXQ_t *mbox;
12537 int rc, length, status = 0;
12538 uint32_t shdr_status, shdr_add_status;
12539 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040012540 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smart5a6f1332011-03-11 16:05:35 -050012541 struct dma_address *page;
James Smart49198b32010-04-06 15:04:33 -040012542
James Smart2e90f4b2011-12-13 13:22:37 -050012543 /* sanity check on queue memory */
12544 if (!wq || !cq)
12545 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040012546 if (!phba->sli4_hba.pc_sli4_params.supported)
12547 hw_page_size = SLI4_PAGE_SIZE;
James Smart4f774512009-05-22 14:52:35 -040012548
12549 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12550 if (!mbox)
12551 return -ENOMEM;
12552 length = (sizeof(struct lpfc_mbx_wq_create) -
12553 sizeof(struct lpfc_sli4_cfg_mhdr));
12554 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
12555 LPFC_MBOX_OPCODE_FCOE_WQ_CREATE,
12556 length, LPFC_SLI4_MBX_EMBED);
12557 wq_create = &mbox->u.mqe.un.wq_create;
James Smart5a6f1332011-03-11 16:05:35 -050012558 shdr = (union lpfc_sli4_cfg_shdr *) &wq_create->header.cfg_shdr;
James Smart4f774512009-05-22 14:52:35 -040012559 bf_set(lpfc_mbx_wq_create_num_pages, &wq_create->u.request,
12560 wq->page_count);
12561 bf_set(lpfc_mbx_wq_create_cq_id, &wq_create->u.request,
12562 cq->queue_id);
James Smart5a6f1332011-03-11 16:05:35 -050012563 bf_set(lpfc_mbox_hdr_version, &shdr->request,
12564 phba->sli4_hba.pc_sli4_params.wqv);
12565 if (phba->sli4_hba.pc_sli4_params.wqv == LPFC_Q_CREATE_VERSION_1) {
12566 bf_set(lpfc_mbx_wq_create_wqe_count, &wq_create->u.request_1,
12567 wq->entry_count);
12568 switch (wq->entry_size) {
12569 default:
12570 case 64:
12571 bf_set(lpfc_mbx_wq_create_wqe_size,
12572 &wq_create->u.request_1,
12573 LPFC_WQ_WQE_SIZE_64);
12574 break;
12575 case 128:
12576 bf_set(lpfc_mbx_wq_create_wqe_size,
12577 &wq_create->u.request_1,
12578 LPFC_WQ_WQE_SIZE_128);
12579 break;
12580 }
12581 bf_set(lpfc_mbx_wq_create_page_size, &wq_create->u.request_1,
12582 (PAGE_SIZE/SLI4_PAGE_SIZE));
12583 page = wq_create->u.request_1.page;
12584 } else {
12585 page = wq_create->u.request.page;
12586 }
James Smart4f774512009-05-22 14:52:35 -040012587 list_for_each_entry(dmabuf, &wq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040012588 memset(dmabuf->virt, 0, hw_page_size);
James Smart5a6f1332011-03-11 16:05:35 -050012589 page[dmabuf->buffer_tag].addr_lo = putPaddrLow(dmabuf->phys);
12590 page[dmabuf->buffer_tag].addr_hi = putPaddrHigh(dmabuf->phys);
James Smart4f774512009-05-22 14:52:35 -040012591 }
12592 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
12593 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart4f774512009-05-22 14:52:35 -040012594 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12595 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12596 if (shdr_status || shdr_add_status || rc) {
12597 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12598 "2503 WQ_CREATE mailbox failed with "
12599 "status x%x add_status x%x, mbx status x%x\n",
12600 shdr_status, shdr_add_status, rc);
12601 status = -ENXIO;
12602 goto out;
12603 }
12604 wq->queue_id = bf_get(lpfc_mbx_wq_create_q_id, &wq_create->u.response);
12605 if (wq->queue_id == 0xFFFF) {
12606 status = -ENXIO;
12607 goto out;
12608 }
12609 wq->type = LPFC_WQ;
James Smart2a622bf2011-02-16 12:40:06 -050012610 wq->assoc_qid = cq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040012611 wq->subtype = subtype;
12612 wq->host_index = 0;
12613 wq->hba_index = 0;
James Smartff78d8f2011-12-13 13:21:35 -050012614 wq->entry_repost = LPFC_RELEASE_NOTIFICATION_INTERVAL;
James Smart4f774512009-05-22 14:52:35 -040012615
12616 /* link the wq onto the parent cq child list */
12617 list_add_tail(&wq->list, &cq->child_list);
12618out:
James Smart8fa38512009-07-19 10:01:03 -040012619 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040012620 return status;
12621}
12622
12623/**
James Smart73d91e52011-10-10 21:32:10 -040012624 * lpfc_rq_adjust_repost - Adjust entry_repost for an RQ
12625 * @phba: HBA structure that indicates port to create a queue on.
12626 * @rq: The queue structure to use for the receive queue.
12627 * @qno: The associated HBQ number
12628 *
12629 *
12630 * For SLI4 we need to adjust the RQ repost value based on
12631 * the number of buffers that are initially posted to the RQ.
12632 */
12633void
12634lpfc_rq_adjust_repost(struct lpfc_hba *phba, struct lpfc_queue *rq, int qno)
12635{
12636 uint32_t cnt;
12637
James Smart2e90f4b2011-12-13 13:22:37 -050012638 /* sanity check on queue memory */
12639 if (!rq)
12640 return;
James Smart73d91e52011-10-10 21:32:10 -040012641 cnt = lpfc_hbq_defs[qno]->entry_count;
12642
12643 /* Recalc repost for RQs based on buffers initially posted */
12644 cnt = (cnt >> 3);
12645 if (cnt < LPFC_QUEUE_MIN_REPOST)
12646 cnt = LPFC_QUEUE_MIN_REPOST;
12647
12648 rq->entry_repost = cnt;
12649}
12650
12651/**
James Smart4f774512009-05-22 14:52:35 -040012652 * lpfc_rq_create - Create a Receive Queue on the HBA
12653 * @phba: HBA structure that indicates port to create a queue on.
12654 * @hrq: The queue structure to use to create the header receive queue.
12655 * @drq: The queue structure to use to create the data receive queue.
12656 * @cq: The completion queue to bind this work queue to.
12657 *
12658 * This function creates a receive buffer queue pair , as detailed in @hrq and
12659 * @drq, on a port, described by @phba by sending a RQ_CREATE mailbox command
12660 * to the HBA.
12661 *
12662 * The @phba struct is used to send mailbox command to HBA. The @drq and @hrq
12663 * struct is used to get the entry count that is necessary to determine the
12664 * number of pages to use for this queue. The @cq is used to indicate which
12665 * completion queue to bind received buffers that are posted to these queues to.
12666 * This function will send the RQ_CREATE mailbox command to the HBA to setup the
12667 * receive queue pair. This function is asynchronous and will wait for the
12668 * mailbox command to finish before continuing.
12669 *
12670 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040012671 * memory this function will return -ENOMEM. If the queue create mailbox command
12672 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040012673 **/
12674uint32_t
12675lpfc_rq_create(struct lpfc_hba *phba, struct lpfc_queue *hrq,
12676 struct lpfc_queue *drq, struct lpfc_queue *cq, uint32_t subtype)
12677{
12678 struct lpfc_mbx_rq_create *rq_create;
12679 struct lpfc_dmabuf *dmabuf;
12680 LPFC_MBOXQ_t *mbox;
12681 int rc, length, status = 0;
12682 uint32_t shdr_status, shdr_add_status;
12683 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040012684 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
12685
James Smart2e90f4b2011-12-13 13:22:37 -050012686 /* sanity check on queue memory */
12687 if (!hrq || !drq || !cq)
12688 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040012689 if (!phba->sli4_hba.pc_sli4_params.supported)
12690 hw_page_size = SLI4_PAGE_SIZE;
James Smart4f774512009-05-22 14:52:35 -040012691
12692 if (hrq->entry_count != drq->entry_count)
12693 return -EINVAL;
12694 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12695 if (!mbox)
12696 return -ENOMEM;
12697 length = (sizeof(struct lpfc_mbx_rq_create) -
12698 sizeof(struct lpfc_sli4_cfg_mhdr));
12699 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
12700 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
12701 length, LPFC_SLI4_MBX_EMBED);
12702 rq_create = &mbox->u.mqe.un.rq_create;
James Smart5a6f1332011-03-11 16:05:35 -050012703 shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
12704 bf_set(lpfc_mbox_hdr_version, &shdr->request,
12705 phba->sli4_hba.pc_sli4_params.rqv);
12706 if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
12707 bf_set(lpfc_rq_context_rqe_count_1,
12708 &rq_create->u.request.context,
12709 hrq->entry_count);
12710 rq_create->u.request.context.buffer_size = LPFC_HDR_BUF_SIZE;
James Smartc31098c2011-04-16 11:03:33 -040012711 bf_set(lpfc_rq_context_rqe_size,
12712 &rq_create->u.request.context,
12713 LPFC_RQE_SIZE_8);
12714 bf_set(lpfc_rq_context_page_size,
12715 &rq_create->u.request.context,
12716 (PAGE_SIZE/SLI4_PAGE_SIZE));
James Smart5a6f1332011-03-11 16:05:35 -050012717 } else {
12718 switch (hrq->entry_count) {
12719 default:
12720 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12721 "2535 Unsupported RQ count. (%d)\n",
12722 hrq->entry_count);
James Smart4f4c1862012-06-12 13:54:02 -040012723 if (hrq->entry_count < 512) {
12724 status = -EINVAL;
12725 goto out;
12726 }
James Smart5a6f1332011-03-11 16:05:35 -050012727 /* otherwise default to smallest count (drop through) */
12728 case 512:
12729 bf_set(lpfc_rq_context_rqe_count,
12730 &rq_create->u.request.context,
12731 LPFC_RQ_RING_SIZE_512);
12732 break;
12733 case 1024:
12734 bf_set(lpfc_rq_context_rqe_count,
12735 &rq_create->u.request.context,
12736 LPFC_RQ_RING_SIZE_1024);
12737 break;
12738 case 2048:
12739 bf_set(lpfc_rq_context_rqe_count,
12740 &rq_create->u.request.context,
12741 LPFC_RQ_RING_SIZE_2048);
12742 break;
12743 case 4096:
12744 bf_set(lpfc_rq_context_rqe_count,
12745 &rq_create->u.request.context,
12746 LPFC_RQ_RING_SIZE_4096);
12747 break;
12748 }
12749 bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
12750 LPFC_HDR_BUF_SIZE);
James Smart4f774512009-05-22 14:52:35 -040012751 }
12752 bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
12753 cq->queue_id);
12754 bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
12755 hrq->page_count);
James Smart4f774512009-05-22 14:52:35 -040012756 list_for_each_entry(dmabuf, &hrq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040012757 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -040012758 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
12759 putPaddrLow(dmabuf->phys);
12760 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
12761 putPaddrHigh(dmabuf->phys);
12762 }
12763 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
12764 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart4f774512009-05-22 14:52:35 -040012765 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12766 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12767 if (shdr_status || shdr_add_status || rc) {
12768 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12769 "2504 RQ_CREATE mailbox failed with "
12770 "status x%x add_status x%x, mbx status x%x\n",
12771 shdr_status, shdr_add_status, rc);
12772 status = -ENXIO;
12773 goto out;
12774 }
12775 hrq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
12776 if (hrq->queue_id == 0xFFFF) {
12777 status = -ENXIO;
12778 goto out;
12779 }
12780 hrq->type = LPFC_HRQ;
James Smart2a622bf2011-02-16 12:40:06 -050012781 hrq->assoc_qid = cq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040012782 hrq->subtype = subtype;
12783 hrq->host_index = 0;
12784 hrq->hba_index = 0;
12785
12786 /* now create the data queue */
12787 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
12788 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
12789 length, LPFC_SLI4_MBX_EMBED);
James Smart5a6f1332011-03-11 16:05:35 -050012790 bf_set(lpfc_mbox_hdr_version, &shdr->request,
12791 phba->sli4_hba.pc_sli4_params.rqv);
12792 if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
12793 bf_set(lpfc_rq_context_rqe_count_1,
James Smartc31098c2011-04-16 11:03:33 -040012794 &rq_create->u.request.context, hrq->entry_count);
James Smart5a6f1332011-03-11 16:05:35 -050012795 rq_create->u.request.context.buffer_size = LPFC_DATA_BUF_SIZE;
James Smartc31098c2011-04-16 11:03:33 -040012796 bf_set(lpfc_rq_context_rqe_size, &rq_create->u.request.context,
12797 LPFC_RQE_SIZE_8);
12798 bf_set(lpfc_rq_context_page_size, &rq_create->u.request.context,
12799 (PAGE_SIZE/SLI4_PAGE_SIZE));
James Smart5a6f1332011-03-11 16:05:35 -050012800 } else {
12801 switch (drq->entry_count) {
12802 default:
12803 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12804 "2536 Unsupported RQ count. (%d)\n",
12805 drq->entry_count);
James Smart4f4c1862012-06-12 13:54:02 -040012806 if (drq->entry_count < 512) {
12807 status = -EINVAL;
12808 goto out;
12809 }
James Smart5a6f1332011-03-11 16:05:35 -050012810 /* otherwise default to smallest count (drop through) */
12811 case 512:
12812 bf_set(lpfc_rq_context_rqe_count,
12813 &rq_create->u.request.context,
12814 LPFC_RQ_RING_SIZE_512);
12815 break;
12816 case 1024:
12817 bf_set(lpfc_rq_context_rqe_count,
12818 &rq_create->u.request.context,
12819 LPFC_RQ_RING_SIZE_1024);
12820 break;
12821 case 2048:
12822 bf_set(lpfc_rq_context_rqe_count,
12823 &rq_create->u.request.context,
12824 LPFC_RQ_RING_SIZE_2048);
12825 break;
12826 case 4096:
12827 bf_set(lpfc_rq_context_rqe_count,
12828 &rq_create->u.request.context,
12829 LPFC_RQ_RING_SIZE_4096);
12830 break;
12831 }
12832 bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
12833 LPFC_DATA_BUF_SIZE);
James Smart4f774512009-05-22 14:52:35 -040012834 }
12835 bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
12836 cq->queue_id);
12837 bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
12838 drq->page_count);
James Smart4f774512009-05-22 14:52:35 -040012839 list_for_each_entry(dmabuf, &drq->page_list, list) {
12840 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
12841 putPaddrLow(dmabuf->phys);
12842 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
12843 putPaddrHigh(dmabuf->phys);
12844 }
12845 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
12846 /* The IOCTL status is embedded in the mailbox subheader. */
12847 shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
12848 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12849 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12850 if (shdr_status || shdr_add_status || rc) {
12851 status = -ENXIO;
12852 goto out;
12853 }
12854 drq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
12855 if (drq->queue_id == 0xFFFF) {
12856 status = -ENXIO;
12857 goto out;
12858 }
12859 drq->type = LPFC_DRQ;
James Smart2a622bf2011-02-16 12:40:06 -050012860 drq->assoc_qid = cq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040012861 drq->subtype = subtype;
12862 drq->host_index = 0;
12863 drq->hba_index = 0;
12864
12865 /* link the header and data RQs onto the parent cq child list */
12866 list_add_tail(&hrq->list, &cq->child_list);
12867 list_add_tail(&drq->list, &cq->child_list);
12868
12869out:
James Smart8fa38512009-07-19 10:01:03 -040012870 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040012871 return status;
12872}
12873
12874/**
12875 * lpfc_eq_destroy - Destroy an event Queue on the HBA
12876 * @eq: The queue structure associated with the queue to destroy.
12877 *
12878 * This function destroys a queue, as detailed in @eq by sending an mailbox
12879 * command, specific to the type of queue, to the HBA.
12880 *
12881 * The @eq struct is used to get the queue ID of the queue to destroy.
12882 *
12883 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040012884 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040012885 **/
12886uint32_t
12887lpfc_eq_destroy(struct lpfc_hba *phba, struct lpfc_queue *eq)
12888{
12889 LPFC_MBOXQ_t *mbox;
12890 int rc, length, status = 0;
12891 uint32_t shdr_status, shdr_add_status;
12892 union lpfc_sli4_cfg_shdr *shdr;
12893
James Smart2e90f4b2011-12-13 13:22:37 -050012894 /* sanity check on queue memory */
James Smart4f774512009-05-22 14:52:35 -040012895 if (!eq)
12896 return -ENODEV;
12897 mbox = mempool_alloc(eq->phba->mbox_mem_pool, GFP_KERNEL);
12898 if (!mbox)
12899 return -ENOMEM;
12900 length = (sizeof(struct lpfc_mbx_eq_destroy) -
12901 sizeof(struct lpfc_sli4_cfg_mhdr));
12902 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
12903 LPFC_MBOX_OPCODE_EQ_DESTROY,
12904 length, LPFC_SLI4_MBX_EMBED);
12905 bf_set(lpfc_mbx_eq_destroy_q_id, &mbox->u.mqe.un.eq_destroy.u.request,
12906 eq->queue_id);
12907 mbox->vport = eq->phba->pport;
12908 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
12909
12910 rc = lpfc_sli_issue_mbox(eq->phba, mbox, MBX_POLL);
12911 /* The IOCTL status is embedded in the mailbox subheader. */
12912 shdr = (union lpfc_sli4_cfg_shdr *)
12913 &mbox->u.mqe.un.eq_destroy.header.cfg_shdr;
12914 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12915 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12916 if (shdr_status || shdr_add_status || rc) {
12917 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12918 "2505 EQ_DESTROY mailbox failed with "
12919 "status x%x add_status x%x, mbx status x%x\n",
12920 shdr_status, shdr_add_status, rc);
12921 status = -ENXIO;
12922 }
12923
12924 /* Remove eq from any list */
12925 list_del_init(&eq->list);
James Smart8fa38512009-07-19 10:01:03 -040012926 mempool_free(mbox, eq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040012927 return status;
12928}
12929
12930/**
12931 * lpfc_cq_destroy - Destroy a Completion Queue on the HBA
12932 * @cq: The queue structure associated with the queue to destroy.
12933 *
12934 * This function destroys a queue, as detailed in @cq by sending an mailbox
12935 * command, specific to the type of queue, to the HBA.
12936 *
12937 * The @cq struct is used to get the queue ID of the queue to destroy.
12938 *
12939 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040012940 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040012941 **/
12942uint32_t
12943lpfc_cq_destroy(struct lpfc_hba *phba, struct lpfc_queue *cq)
12944{
12945 LPFC_MBOXQ_t *mbox;
12946 int rc, length, status = 0;
12947 uint32_t shdr_status, shdr_add_status;
12948 union lpfc_sli4_cfg_shdr *shdr;
12949
James Smart2e90f4b2011-12-13 13:22:37 -050012950 /* sanity check on queue memory */
James Smart4f774512009-05-22 14:52:35 -040012951 if (!cq)
12952 return -ENODEV;
12953 mbox = mempool_alloc(cq->phba->mbox_mem_pool, GFP_KERNEL);
12954 if (!mbox)
12955 return -ENOMEM;
12956 length = (sizeof(struct lpfc_mbx_cq_destroy) -
12957 sizeof(struct lpfc_sli4_cfg_mhdr));
12958 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
12959 LPFC_MBOX_OPCODE_CQ_DESTROY,
12960 length, LPFC_SLI4_MBX_EMBED);
12961 bf_set(lpfc_mbx_cq_destroy_q_id, &mbox->u.mqe.un.cq_destroy.u.request,
12962 cq->queue_id);
12963 mbox->vport = cq->phba->pport;
12964 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
12965 rc = lpfc_sli_issue_mbox(cq->phba, mbox, MBX_POLL);
12966 /* The IOCTL status is embedded in the mailbox subheader. */
12967 shdr = (union lpfc_sli4_cfg_shdr *)
12968 &mbox->u.mqe.un.wq_create.header.cfg_shdr;
12969 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12970 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12971 if (shdr_status || shdr_add_status || rc) {
12972 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12973 "2506 CQ_DESTROY mailbox failed with "
12974 "status x%x add_status x%x, mbx status x%x\n",
12975 shdr_status, shdr_add_status, rc);
12976 status = -ENXIO;
12977 }
12978 /* Remove cq from any list */
12979 list_del_init(&cq->list);
James Smart8fa38512009-07-19 10:01:03 -040012980 mempool_free(mbox, cq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040012981 return status;
12982}
12983
12984/**
James Smart04c68492009-05-22 14:52:52 -040012985 * lpfc_mq_destroy - Destroy a Mailbox Queue on the HBA
12986 * @qm: The queue structure associated with the queue to destroy.
12987 *
12988 * This function destroys a queue, as detailed in @mq by sending an mailbox
12989 * command, specific to the type of queue, to the HBA.
12990 *
12991 * The @mq struct is used to get the queue ID of the queue to destroy.
12992 *
12993 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040012994 * command fails this function will return -ENXIO.
James Smart04c68492009-05-22 14:52:52 -040012995 **/
12996uint32_t
12997lpfc_mq_destroy(struct lpfc_hba *phba, struct lpfc_queue *mq)
12998{
12999 LPFC_MBOXQ_t *mbox;
13000 int rc, length, status = 0;
13001 uint32_t shdr_status, shdr_add_status;
13002 union lpfc_sli4_cfg_shdr *shdr;
13003
James Smart2e90f4b2011-12-13 13:22:37 -050013004 /* sanity check on queue memory */
James Smart04c68492009-05-22 14:52:52 -040013005 if (!mq)
13006 return -ENODEV;
13007 mbox = mempool_alloc(mq->phba->mbox_mem_pool, GFP_KERNEL);
13008 if (!mbox)
13009 return -ENOMEM;
13010 length = (sizeof(struct lpfc_mbx_mq_destroy) -
13011 sizeof(struct lpfc_sli4_cfg_mhdr));
13012 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
13013 LPFC_MBOX_OPCODE_MQ_DESTROY,
13014 length, LPFC_SLI4_MBX_EMBED);
13015 bf_set(lpfc_mbx_mq_destroy_q_id, &mbox->u.mqe.un.mq_destroy.u.request,
13016 mq->queue_id);
13017 mbox->vport = mq->phba->pport;
13018 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
13019 rc = lpfc_sli_issue_mbox(mq->phba, mbox, MBX_POLL);
13020 /* The IOCTL status is embedded in the mailbox subheader. */
13021 shdr = (union lpfc_sli4_cfg_shdr *)
13022 &mbox->u.mqe.un.mq_destroy.header.cfg_shdr;
13023 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13024 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13025 if (shdr_status || shdr_add_status || rc) {
13026 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13027 "2507 MQ_DESTROY mailbox failed with "
13028 "status x%x add_status x%x, mbx status x%x\n",
13029 shdr_status, shdr_add_status, rc);
13030 status = -ENXIO;
13031 }
13032 /* Remove mq from any list */
13033 list_del_init(&mq->list);
James Smart8fa38512009-07-19 10:01:03 -040013034 mempool_free(mbox, mq->phba->mbox_mem_pool);
James Smart04c68492009-05-22 14:52:52 -040013035 return status;
13036}
13037
13038/**
James Smart4f774512009-05-22 14:52:35 -040013039 * lpfc_wq_destroy - Destroy a Work Queue on the HBA
13040 * @wq: The queue structure associated with the queue to destroy.
13041 *
13042 * This function destroys a queue, as detailed in @wq by sending an mailbox
13043 * command, specific to the type of queue, to the HBA.
13044 *
13045 * The @wq struct is used to get the queue ID of the queue to destroy.
13046 *
13047 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040013048 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040013049 **/
13050uint32_t
13051lpfc_wq_destroy(struct lpfc_hba *phba, struct lpfc_queue *wq)
13052{
13053 LPFC_MBOXQ_t *mbox;
13054 int rc, length, status = 0;
13055 uint32_t shdr_status, shdr_add_status;
13056 union lpfc_sli4_cfg_shdr *shdr;
13057
James Smart2e90f4b2011-12-13 13:22:37 -050013058 /* sanity check on queue memory */
James Smart4f774512009-05-22 14:52:35 -040013059 if (!wq)
13060 return -ENODEV;
13061 mbox = mempool_alloc(wq->phba->mbox_mem_pool, GFP_KERNEL);
13062 if (!mbox)
13063 return -ENOMEM;
13064 length = (sizeof(struct lpfc_mbx_wq_destroy) -
13065 sizeof(struct lpfc_sli4_cfg_mhdr));
13066 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
13067 LPFC_MBOX_OPCODE_FCOE_WQ_DESTROY,
13068 length, LPFC_SLI4_MBX_EMBED);
13069 bf_set(lpfc_mbx_wq_destroy_q_id, &mbox->u.mqe.un.wq_destroy.u.request,
13070 wq->queue_id);
13071 mbox->vport = wq->phba->pport;
13072 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
13073 rc = lpfc_sli_issue_mbox(wq->phba, mbox, MBX_POLL);
13074 shdr = (union lpfc_sli4_cfg_shdr *)
13075 &mbox->u.mqe.un.wq_destroy.header.cfg_shdr;
13076 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13077 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13078 if (shdr_status || shdr_add_status || rc) {
13079 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13080 "2508 WQ_DESTROY mailbox failed with "
13081 "status x%x add_status x%x, mbx status x%x\n",
13082 shdr_status, shdr_add_status, rc);
13083 status = -ENXIO;
13084 }
13085 /* Remove wq from any list */
13086 list_del_init(&wq->list);
James Smart8fa38512009-07-19 10:01:03 -040013087 mempool_free(mbox, wq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040013088 return status;
13089}
13090
13091/**
13092 * lpfc_rq_destroy - Destroy a Receive Queue on the HBA
13093 * @rq: The queue structure associated with the queue to destroy.
13094 *
13095 * This function destroys a queue, as detailed in @rq by sending an mailbox
13096 * command, specific to the type of queue, to the HBA.
13097 *
13098 * The @rq struct is used to get the queue ID of the queue to destroy.
13099 *
13100 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040013101 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040013102 **/
13103uint32_t
13104lpfc_rq_destroy(struct lpfc_hba *phba, struct lpfc_queue *hrq,
13105 struct lpfc_queue *drq)
13106{
13107 LPFC_MBOXQ_t *mbox;
13108 int rc, length, status = 0;
13109 uint32_t shdr_status, shdr_add_status;
13110 union lpfc_sli4_cfg_shdr *shdr;
13111
James Smart2e90f4b2011-12-13 13:22:37 -050013112 /* sanity check on queue memory */
James Smart4f774512009-05-22 14:52:35 -040013113 if (!hrq || !drq)
13114 return -ENODEV;
13115 mbox = mempool_alloc(hrq->phba->mbox_mem_pool, GFP_KERNEL);
13116 if (!mbox)
13117 return -ENOMEM;
13118 length = (sizeof(struct lpfc_mbx_rq_destroy) -
James Smartfedd3b72011-02-16 12:39:24 -050013119 sizeof(struct lpfc_sli4_cfg_mhdr));
James Smart4f774512009-05-22 14:52:35 -040013120 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
13121 LPFC_MBOX_OPCODE_FCOE_RQ_DESTROY,
13122 length, LPFC_SLI4_MBX_EMBED);
13123 bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
13124 hrq->queue_id);
13125 mbox->vport = hrq->phba->pport;
13126 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
13127 rc = lpfc_sli_issue_mbox(hrq->phba, mbox, MBX_POLL);
13128 /* The IOCTL status is embedded in the mailbox subheader. */
13129 shdr = (union lpfc_sli4_cfg_shdr *)
13130 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
13131 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13132 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13133 if (shdr_status || shdr_add_status || rc) {
13134 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13135 "2509 RQ_DESTROY mailbox failed with "
13136 "status x%x add_status x%x, mbx status x%x\n",
13137 shdr_status, shdr_add_status, rc);
13138 if (rc != MBX_TIMEOUT)
13139 mempool_free(mbox, hrq->phba->mbox_mem_pool);
13140 return -ENXIO;
13141 }
13142 bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
13143 drq->queue_id);
13144 rc = lpfc_sli_issue_mbox(drq->phba, mbox, MBX_POLL);
13145 shdr = (union lpfc_sli4_cfg_shdr *)
13146 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
13147 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13148 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13149 if (shdr_status || shdr_add_status || rc) {
13150 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13151 "2510 RQ_DESTROY mailbox failed with "
13152 "status x%x add_status x%x, mbx status x%x\n",
13153 shdr_status, shdr_add_status, rc);
13154 status = -ENXIO;
13155 }
13156 list_del_init(&hrq->list);
13157 list_del_init(&drq->list);
James Smart8fa38512009-07-19 10:01:03 -040013158 mempool_free(mbox, hrq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040013159 return status;
13160}
13161
13162/**
13163 * lpfc_sli4_post_sgl - Post scatter gather list for an XRI to HBA
13164 * @phba: The virtual port for which this call being executed.
13165 * @pdma_phys_addr0: Physical address of the 1st SGL page.
13166 * @pdma_phys_addr1: Physical address of the 2nd SGL page.
13167 * @xritag: the xritag that ties this io to the SGL pages.
13168 *
13169 * This routine will post the sgl pages for the IO that has the xritag
13170 * that is in the iocbq structure. The xritag is assigned during iocbq
13171 * creation and persists for as long as the driver is loaded.
13172 * if the caller has fewer than 256 scatter gather segments to map then
13173 * pdma_phys_addr1 should be 0.
13174 * If the caller needs to map more than 256 scatter gather segment then
13175 * pdma_phys_addr1 should be a valid physical address.
13176 * physical address for SGLs must be 64 byte aligned.
13177 * If you are going to map 2 SGL's then the first one must have 256 entries
13178 * the second sgl can have between 1 and 256 entries.
13179 *
13180 * Return codes:
13181 * 0 - Success
13182 * -ENXIO, -ENOMEM - Failure
13183 **/
13184int
13185lpfc_sli4_post_sgl(struct lpfc_hba *phba,
13186 dma_addr_t pdma_phys_addr0,
13187 dma_addr_t pdma_phys_addr1,
13188 uint16_t xritag)
13189{
13190 struct lpfc_mbx_post_sgl_pages *post_sgl_pages;
13191 LPFC_MBOXQ_t *mbox;
13192 int rc;
13193 uint32_t shdr_status, shdr_add_status;
James Smart6d368e52011-05-24 11:44:12 -040013194 uint32_t mbox_tmo;
James Smart4f774512009-05-22 14:52:35 -040013195 union lpfc_sli4_cfg_shdr *shdr;
13196
13197 if (xritag == NO_XRI) {
13198 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13199 "0364 Invalid param:\n");
13200 return -EINVAL;
13201 }
13202
13203 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
13204 if (!mbox)
13205 return -ENOMEM;
13206
13207 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
13208 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES,
13209 sizeof(struct lpfc_mbx_post_sgl_pages) -
James Smartfedd3b72011-02-16 12:39:24 -050013210 sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
James Smart4f774512009-05-22 14:52:35 -040013211
13212 post_sgl_pages = (struct lpfc_mbx_post_sgl_pages *)
13213 &mbox->u.mqe.un.post_sgl_pages;
13214 bf_set(lpfc_post_sgl_pages_xri, post_sgl_pages, xritag);
13215 bf_set(lpfc_post_sgl_pages_xricnt, post_sgl_pages, 1);
13216
13217 post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_lo =
13218 cpu_to_le32(putPaddrLow(pdma_phys_addr0));
13219 post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_hi =
13220 cpu_to_le32(putPaddrHigh(pdma_phys_addr0));
13221
13222 post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_lo =
13223 cpu_to_le32(putPaddrLow(pdma_phys_addr1));
13224 post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_hi =
13225 cpu_to_le32(putPaddrHigh(pdma_phys_addr1));
13226 if (!phba->sli4_hba.intr_enable)
13227 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
James Smart6d368e52011-05-24 11:44:12 -040013228 else {
James Smarta183a152011-10-10 21:32:43 -040013229 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart6d368e52011-05-24 11:44:12 -040013230 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
13231 }
James Smart4f774512009-05-22 14:52:35 -040013232 /* The IOCTL status is embedded in the mailbox subheader. */
13233 shdr = (union lpfc_sli4_cfg_shdr *) &post_sgl_pages->header.cfg_shdr;
13234 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13235 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13236 if (rc != MBX_TIMEOUT)
13237 mempool_free(mbox, phba->mbox_mem_pool);
13238 if (shdr_status || shdr_add_status || rc) {
13239 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13240 "2511 POST_SGL mailbox failed with "
13241 "status x%x add_status x%x, mbx status x%x\n",
13242 shdr_status, shdr_add_status, rc);
13243 rc = -ENXIO;
13244 }
13245 return 0;
13246}
James Smart4f774512009-05-22 14:52:35 -040013247
13248/**
James Smart88a2cfb2011-07-22 18:36:33 -040013249 * lpfc_sli4_alloc_xri - Get an available rpi in the device's range
James Smart6d368e52011-05-24 11:44:12 -040013250 * @phba: pointer to lpfc hba data structure.
13251 *
13252 * This routine is invoked to post rpi header templates to the
James Smart88a2cfb2011-07-22 18:36:33 -040013253 * HBA consistent with the SLI-4 interface spec. This routine
13254 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
13255 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
James Smart6d368e52011-05-24 11:44:12 -040013256 *
James Smart88a2cfb2011-07-22 18:36:33 -040013257 * Returns
13258 * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
13259 * LPFC_RPI_ALLOC_ERROR if no rpis are available.
13260 **/
James Smart6d368e52011-05-24 11:44:12 -040013261uint16_t
13262lpfc_sli4_alloc_xri(struct lpfc_hba *phba)
13263{
13264 unsigned long xri;
13265
13266 /*
13267 * Fetch the next logical xri. Because this index is logical,
13268 * the driver starts at 0 each time.
13269 */
13270 spin_lock_irq(&phba->hbalock);
13271 xri = find_next_zero_bit(phba->sli4_hba.xri_bmask,
13272 phba->sli4_hba.max_cfg_param.max_xri, 0);
13273 if (xri >= phba->sli4_hba.max_cfg_param.max_xri) {
13274 spin_unlock_irq(&phba->hbalock);
13275 return NO_XRI;
13276 } else {
13277 set_bit(xri, phba->sli4_hba.xri_bmask);
13278 phba->sli4_hba.max_cfg_param.xri_used++;
James Smart6d368e52011-05-24 11:44:12 -040013279 }
James Smart6d368e52011-05-24 11:44:12 -040013280 spin_unlock_irq(&phba->hbalock);
13281 return xri;
13282}
13283
13284/**
13285 * lpfc_sli4_free_xri - Release an xri for reuse.
13286 * @phba: pointer to lpfc hba data structure.
13287 *
13288 * This routine is invoked to release an xri to the pool of
13289 * available rpis maintained by the driver.
13290 **/
13291void
13292__lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
13293{
13294 if (test_and_clear_bit(xri, phba->sli4_hba.xri_bmask)) {
James Smart6d368e52011-05-24 11:44:12 -040013295 phba->sli4_hba.max_cfg_param.xri_used--;
13296 }
13297}
13298
13299/**
13300 * lpfc_sli4_free_xri - Release an xri for reuse.
13301 * @phba: pointer to lpfc hba data structure.
13302 *
13303 * This routine is invoked to release an xri to the pool of
13304 * available rpis maintained by the driver.
13305 **/
13306void
13307lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
13308{
13309 spin_lock_irq(&phba->hbalock);
13310 __lpfc_sli4_free_xri(phba, xri);
13311 spin_unlock_irq(&phba->hbalock);
13312}
13313
13314/**
James Smart4f774512009-05-22 14:52:35 -040013315 * lpfc_sli4_next_xritag - Get an xritag for the io
13316 * @phba: Pointer to HBA context object.
13317 *
13318 * This function gets an xritag for the iocb. If there is no unused xritag
13319 * it will return 0xffff.
13320 * The function returns the allocated xritag if successful, else returns zero.
13321 * Zero is not a valid xritag.
13322 * The caller is not required to hold any lock.
13323 **/
13324uint16_t
13325lpfc_sli4_next_xritag(struct lpfc_hba *phba)
13326{
James Smart6d368e52011-05-24 11:44:12 -040013327 uint16_t xri_index;
James Smart4f774512009-05-22 14:52:35 -040013328
James Smart6d368e52011-05-24 11:44:12 -040013329 xri_index = lpfc_sli4_alloc_xri(phba);
James Smart81378052012-05-09 21:17:37 -040013330 if (xri_index == NO_XRI)
13331 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13332 "2004 Failed to allocate XRI.last XRITAG is %d"
13333 " Max XRI is %d, Used XRI is %d\n",
13334 xri_index,
13335 phba->sli4_hba.max_cfg_param.max_xri,
13336 phba->sli4_hba.max_cfg_param.xri_used);
13337 return xri_index;
James Smart4f774512009-05-22 14:52:35 -040013338}
13339
13340/**
James Smart6d368e52011-05-24 11:44:12 -040013341 * lpfc_sli4_post_els_sgl_list - post a block of ELS sgls to the port.
James Smart4f774512009-05-22 14:52:35 -040013342 * @phba: pointer to lpfc hba data structure.
James Smart8a9d2e82012-05-09 21:16:12 -040013343 * @post_sgl_list: pointer to els sgl entry list.
13344 * @count: number of els sgl entries on the list.
James Smart4f774512009-05-22 14:52:35 -040013345 *
13346 * This routine is invoked to post a block of driver's sgl pages to the
13347 * HBA using non-embedded mailbox command. No Lock is held. This routine
13348 * is only called when the driver is loading and after all IO has been
13349 * stopped.
13350 **/
James Smart8a9d2e82012-05-09 21:16:12 -040013351static int
13352lpfc_sli4_post_els_sgl_list(struct lpfc_hba *phba,
13353 struct list_head *post_sgl_list,
13354 int post_cnt)
James Smart4f774512009-05-22 14:52:35 -040013355{
James Smart8a9d2e82012-05-09 21:16:12 -040013356 struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
James Smart4f774512009-05-22 14:52:35 -040013357 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
13358 struct sgl_page_pairs *sgl_pg_pairs;
13359 void *viraddr;
13360 LPFC_MBOXQ_t *mbox;
13361 uint32_t reqlen, alloclen, pg_pairs;
13362 uint32_t mbox_tmo;
James Smart8a9d2e82012-05-09 21:16:12 -040013363 uint16_t xritag_start = 0;
13364 int rc = 0;
James Smart4f774512009-05-22 14:52:35 -040013365 uint32_t shdr_status, shdr_add_status;
13366 union lpfc_sli4_cfg_shdr *shdr;
13367
James Smart8a9d2e82012-05-09 21:16:12 -040013368 reqlen = phba->sli4_hba.els_xri_cnt * sizeof(struct sgl_page_pairs) +
James Smart4f774512009-05-22 14:52:35 -040013369 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
James Smart49198b32010-04-06 15:04:33 -040013370 if (reqlen > SLI4_PAGE_SIZE) {
James Smart4f774512009-05-22 14:52:35 -040013371 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
13372 "2559 Block sgl registration required DMA "
13373 "size (%d) great than a page\n", reqlen);
13374 return -ENOMEM;
13375 }
13376 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
James Smart6d368e52011-05-24 11:44:12 -040013377 if (!mbox)
James Smart4f774512009-05-22 14:52:35 -040013378 return -ENOMEM;
James Smart4f774512009-05-22 14:52:35 -040013379
13380 /* Allocate DMA memory and set up the non-embedded mailbox command */
13381 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
13382 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
13383 LPFC_SLI4_MBX_NEMBED);
13384
13385 if (alloclen < reqlen) {
13386 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13387 "0285 Allocated DMA memory size (%d) is "
13388 "less than the requested DMA memory "
13389 "size (%d)\n", alloclen, reqlen);
13390 lpfc_sli4_mbox_cmd_free(phba, mbox);
13391 return -ENOMEM;
13392 }
James Smart4f774512009-05-22 14:52:35 -040013393 /* Set up the SGL pages in the non-embedded DMA pages */
James Smart6d368e52011-05-24 11:44:12 -040013394 viraddr = mbox->sge_array->addr[0];
James Smart4f774512009-05-22 14:52:35 -040013395 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
13396 sgl_pg_pairs = &sgl->sgl_pg_pairs;
13397
James Smart8a9d2e82012-05-09 21:16:12 -040013398 pg_pairs = 0;
13399 list_for_each_entry_safe(sglq_entry, sglq_next, post_sgl_list, list) {
James Smart4f774512009-05-22 14:52:35 -040013400 /* Set up the sge entry */
13401 sgl_pg_pairs->sgl_pg0_addr_lo =
13402 cpu_to_le32(putPaddrLow(sglq_entry->phys));
13403 sgl_pg_pairs->sgl_pg0_addr_hi =
13404 cpu_to_le32(putPaddrHigh(sglq_entry->phys));
13405 sgl_pg_pairs->sgl_pg1_addr_lo =
13406 cpu_to_le32(putPaddrLow(0));
13407 sgl_pg_pairs->sgl_pg1_addr_hi =
13408 cpu_to_le32(putPaddrHigh(0));
James Smart6d368e52011-05-24 11:44:12 -040013409
James Smart4f774512009-05-22 14:52:35 -040013410 /* Keep the first xritag on the list */
13411 if (pg_pairs == 0)
13412 xritag_start = sglq_entry->sli4_xritag;
13413 sgl_pg_pairs++;
James Smart8a9d2e82012-05-09 21:16:12 -040013414 pg_pairs++;
James Smart4f774512009-05-22 14:52:35 -040013415 }
James Smart6d368e52011-05-24 11:44:12 -040013416
13417 /* Complete initialization and perform endian conversion. */
James Smart4f774512009-05-22 14:52:35 -040013418 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
James Smart8a9d2e82012-05-09 21:16:12 -040013419 bf_set(lpfc_post_sgl_pages_xricnt, sgl, phba->sli4_hba.els_xri_cnt);
James Smart4f774512009-05-22 14:52:35 -040013420 sgl->word0 = cpu_to_le32(sgl->word0);
James Smart4f774512009-05-22 14:52:35 -040013421 if (!phba->sli4_hba.intr_enable)
13422 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
13423 else {
James Smarta183a152011-10-10 21:32:43 -040013424 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart4f774512009-05-22 14:52:35 -040013425 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
13426 }
13427 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
13428 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13429 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13430 if (rc != MBX_TIMEOUT)
13431 lpfc_sli4_mbox_cmd_free(phba, mbox);
13432 if (shdr_status || shdr_add_status || rc) {
13433 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13434 "2513 POST_SGL_BLOCK mailbox command failed "
13435 "status x%x add_status x%x mbx status x%x\n",
13436 shdr_status, shdr_add_status, rc);
13437 rc = -ENXIO;
13438 }
13439 return rc;
13440}
13441
13442/**
13443 * lpfc_sli4_post_scsi_sgl_block - post a block of scsi sgl list to firmware
13444 * @phba: pointer to lpfc hba data structure.
13445 * @sblist: pointer to scsi buffer list.
13446 * @count: number of scsi buffers on the list.
13447 *
13448 * This routine is invoked to post a block of @count scsi sgl pages from a
13449 * SCSI buffer list @sblist to the HBA using non-embedded mailbox command.
13450 * No Lock is held.
13451 *
13452 **/
13453int
James Smart8a9d2e82012-05-09 21:16:12 -040013454lpfc_sli4_post_scsi_sgl_block(struct lpfc_hba *phba,
13455 struct list_head *sblist,
13456 int count)
James Smart4f774512009-05-22 14:52:35 -040013457{
13458 struct lpfc_scsi_buf *psb;
13459 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
13460 struct sgl_page_pairs *sgl_pg_pairs;
13461 void *viraddr;
13462 LPFC_MBOXQ_t *mbox;
13463 uint32_t reqlen, alloclen, pg_pairs;
13464 uint32_t mbox_tmo;
13465 uint16_t xritag_start = 0;
13466 int rc = 0;
13467 uint32_t shdr_status, shdr_add_status;
13468 dma_addr_t pdma_phys_bpl1;
13469 union lpfc_sli4_cfg_shdr *shdr;
13470
13471 /* Calculate the requested length of the dma memory */
James Smart8a9d2e82012-05-09 21:16:12 -040013472 reqlen = count * sizeof(struct sgl_page_pairs) +
James Smart4f774512009-05-22 14:52:35 -040013473 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
James Smart49198b32010-04-06 15:04:33 -040013474 if (reqlen > SLI4_PAGE_SIZE) {
James Smart4f774512009-05-22 14:52:35 -040013475 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
13476 "0217 Block sgl registration required DMA "
13477 "size (%d) great than a page\n", reqlen);
13478 return -ENOMEM;
13479 }
13480 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
13481 if (!mbox) {
13482 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13483 "0283 Failed to allocate mbox cmd memory\n");
13484 return -ENOMEM;
13485 }
13486
13487 /* Allocate DMA memory and set up the non-embedded mailbox command */
13488 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
13489 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
13490 LPFC_SLI4_MBX_NEMBED);
13491
13492 if (alloclen < reqlen) {
13493 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13494 "2561 Allocated DMA memory size (%d) is "
13495 "less than the requested DMA memory "
13496 "size (%d)\n", alloclen, reqlen);
13497 lpfc_sli4_mbox_cmd_free(phba, mbox);
13498 return -ENOMEM;
13499 }
James Smart6d368e52011-05-24 11:44:12 -040013500
James Smart4f774512009-05-22 14:52:35 -040013501 /* Get the first SGE entry from the non-embedded DMA memory */
James Smart4f774512009-05-22 14:52:35 -040013502 viraddr = mbox->sge_array->addr[0];
13503
13504 /* Set up the SGL pages in the non-embedded DMA pages */
13505 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
13506 sgl_pg_pairs = &sgl->sgl_pg_pairs;
13507
13508 pg_pairs = 0;
13509 list_for_each_entry(psb, sblist, list) {
13510 /* Set up the sge entry */
13511 sgl_pg_pairs->sgl_pg0_addr_lo =
13512 cpu_to_le32(putPaddrLow(psb->dma_phys_bpl));
13513 sgl_pg_pairs->sgl_pg0_addr_hi =
13514 cpu_to_le32(putPaddrHigh(psb->dma_phys_bpl));
13515 if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
13516 pdma_phys_bpl1 = psb->dma_phys_bpl + SGL_PAGE_SIZE;
13517 else
13518 pdma_phys_bpl1 = 0;
13519 sgl_pg_pairs->sgl_pg1_addr_lo =
13520 cpu_to_le32(putPaddrLow(pdma_phys_bpl1));
13521 sgl_pg_pairs->sgl_pg1_addr_hi =
13522 cpu_to_le32(putPaddrHigh(pdma_phys_bpl1));
13523 /* Keep the first xritag on the list */
13524 if (pg_pairs == 0)
13525 xritag_start = psb->cur_iocbq.sli4_xritag;
13526 sgl_pg_pairs++;
13527 pg_pairs++;
13528 }
13529 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
13530 bf_set(lpfc_post_sgl_pages_xricnt, sgl, pg_pairs);
13531 /* Perform endian conversion if necessary */
13532 sgl->word0 = cpu_to_le32(sgl->word0);
13533
13534 if (!phba->sli4_hba.intr_enable)
13535 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
13536 else {
James Smarta183a152011-10-10 21:32:43 -040013537 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart4f774512009-05-22 14:52:35 -040013538 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
13539 }
13540 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
13541 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13542 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13543 if (rc != MBX_TIMEOUT)
13544 lpfc_sli4_mbox_cmd_free(phba, mbox);
13545 if (shdr_status || shdr_add_status || rc) {
13546 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13547 "2564 POST_SGL_BLOCK mailbox command failed "
13548 "status x%x add_status x%x mbx status x%x\n",
13549 shdr_status, shdr_add_status, rc);
13550 rc = -ENXIO;
13551 }
13552 return rc;
13553}
13554
13555/**
13556 * lpfc_fc_frame_check - Check that this frame is a valid frame to handle
13557 * @phba: pointer to lpfc_hba struct that the frame was received on
13558 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
13559 *
13560 * This function checks the fields in the @fc_hdr to see if the FC frame is a
13561 * valid type of frame that the LPFC driver will handle. This function will
13562 * return a zero if the frame is a valid frame or a non zero value when the
13563 * frame does not pass the check.
13564 **/
13565static int
13566lpfc_fc_frame_check(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr)
13567{
Tomas Henzl474ffb72010-12-22 16:52:40 +010013568 /* make rctl_names static to save stack space */
13569 static char *rctl_names[] = FC_RCTL_NAMES_INIT;
James Smart4f774512009-05-22 14:52:35 -040013570 char *type_names[] = FC_TYPE_NAMES_INIT;
13571 struct fc_vft_header *fc_vft_hdr;
James Smart546fc852011-03-11 16:06:29 -050013572 uint32_t *header = (uint32_t *) fc_hdr;
James Smart4f774512009-05-22 14:52:35 -040013573
13574 switch (fc_hdr->fh_r_ctl) {
13575 case FC_RCTL_DD_UNCAT: /* uncategorized information */
13576 case FC_RCTL_DD_SOL_DATA: /* solicited data */
13577 case FC_RCTL_DD_UNSOL_CTL: /* unsolicited control */
13578 case FC_RCTL_DD_SOL_CTL: /* solicited control or reply */
13579 case FC_RCTL_DD_UNSOL_DATA: /* unsolicited data */
13580 case FC_RCTL_DD_DATA_DESC: /* data descriptor */
13581 case FC_RCTL_DD_UNSOL_CMD: /* unsolicited command */
13582 case FC_RCTL_DD_CMD_STATUS: /* command status */
13583 case FC_RCTL_ELS_REQ: /* extended link services request */
13584 case FC_RCTL_ELS_REP: /* extended link services reply */
13585 case FC_RCTL_ELS4_REQ: /* FC-4 ELS request */
13586 case FC_RCTL_ELS4_REP: /* FC-4 ELS reply */
13587 case FC_RCTL_BA_NOP: /* basic link service NOP */
13588 case FC_RCTL_BA_ABTS: /* basic link service abort */
13589 case FC_RCTL_BA_RMC: /* remove connection */
13590 case FC_RCTL_BA_ACC: /* basic accept */
13591 case FC_RCTL_BA_RJT: /* basic reject */
13592 case FC_RCTL_BA_PRMT:
13593 case FC_RCTL_ACK_1: /* acknowledge_1 */
13594 case FC_RCTL_ACK_0: /* acknowledge_0 */
13595 case FC_RCTL_P_RJT: /* port reject */
13596 case FC_RCTL_F_RJT: /* fabric reject */
13597 case FC_RCTL_P_BSY: /* port busy */
13598 case FC_RCTL_F_BSY: /* fabric busy to data frame */
13599 case FC_RCTL_F_BSYL: /* fabric busy to link control frame */
13600 case FC_RCTL_LCR: /* link credit reset */
13601 case FC_RCTL_END: /* end */
13602 break;
13603 case FC_RCTL_VFTH: /* Virtual Fabric tagging Header */
13604 fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
13605 fc_hdr = &((struct fc_frame_header *)fc_vft_hdr)[1];
13606 return lpfc_fc_frame_check(phba, fc_hdr);
13607 default:
13608 goto drop;
13609 }
13610 switch (fc_hdr->fh_type) {
13611 case FC_TYPE_BLS:
13612 case FC_TYPE_ELS:
13613 case FC_TYPE_FCP:
13614 case FC_TYPE_CT:
13615 break;
13616 case FC_TYPE_IP:
13617 case FC_TYPE_ILS:
13618 default:
13619 goto drop;
13620 }
James Smart546fc852011-03-11 16:06:29 -050013621
James Smart4f774512009-05-22 14:52:35 -040013622 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
James Smart546fc852011-03-11 16:06:29 -050013623 "2538 Received frame rctl:%s type:%s "
13624 "Frame Data:%08x %08x %08x %08x %08x %08x\n",
James Smart4f774512009-05-22 14:52:35 -040013625 rctl_names[fc_hdr->fh_r_ctl],
James Smart546fc852011-03-11 16:06:29 -050013626 type_names[fc_hdr->fh_type],
13627 be32_to_cpu(header[0]), be32_to_cpu(header[1]),
13628 be32_to_cpu(header[2]), be32_to_cpu(header[3]),
13629 be32_to_cpu(header[4]), be32_to_cpu(header[5]));
James Smart4f774512009-05-22 14:52:35 -040013630 return 0;
13631drop:
13632 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
13633 "2539 Dropped frame rctl:%s type:%s\n",
13634 rctl_names[fc_hdr->fh_r_ctl],
13635 type_names[fc_hdr->fh_type]);
13636 return 1;
13637}
13638
13639/**
13640 * lpfc_fc_hdr_get_vfi - Get the VFI from an FC frame
13641 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
13642 *
13643 * This function processes the FC header to retrieve the VFI from the VF
13644 * header, if one exists. This function will return the VFI if one exists
13645 * or 0 if no VSAN Header exists.
13646 **/
13647static uint32_t
13648lpfc_fc_hdr_get_vfi(struct fc_frame_header *fc_hdr)
13649{
13650 struct fc_vft_header *fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
13651
13652 if (fc_hdr->fh_r_ctl != FC_RCTL_VFTH)
13653 return 0;
13654 return bf_get(fc_vft_hdr_vf_id, fc_vft_hdr);
13655}
13656
13657/**
13658 * lpfc_fc_frame_to_vport - Finds the vport that a frame is destined to
13659 * @phba: Pointer to the HBA structure to search for the vport on
13660 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
13661 * @fcfi: The FC Fabric ID that the frame came from
13662 *
13663 * This function searches the @phba for a vport that matches the content of the
13664 * @fc_hdr passed in and the @fcfi. This function uses the @fc_hdr to fetch the
13665 * VFI, if the Virtual Fabric Tagging Header exists, and the DID. This function
13666 * returns the matching vport pointer or NULL if unable to match frame to a
13667 * vport.
13668 **/
13669static struct lpfc_vport *
13670lpfc_fc_frame_to_vport(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr,
13671 uint16_t fcfi)
13672{
13673 struct lpfc_vport **vports;
13674 struct lpfc_vport *vport = NULL;
13675 int i;
13676 uint32_t did = (fc_hdr->fh_d_id[0] << 16 |
13677 fc_hdr->fh_d_id[1] << 8 |
13678 fc_hdr->fh_d_id[2]);
James Smart939723a2012-05-09 21:19:03 -040013679
James Smartbf086112011-08-21 21:48:13 -040013680 if (did == Fabric_DID)
13681 return phba->pport;
James Smart939723a2012-05-09 21:19:03 -040013682 if ((phba->pport->fc_flag & FC_PT2PT) &&
13683 !(phba->link_state == LPFC_HBA_READY))
13684 return phba->pport;
13685
James Smart4f774512009-05-22 14:52:35 -040013686 vports = lpfc_create_vport_work_array(phba);
13687 if (vports != NULL)
13688 for (i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
13689 if (phba->fcf.fcfi == fcfi &&
13690 vports[i]->vfi == lpfc_fc_hdr_get_vfi(fc_hdr) &&
13691 vports[i]->fc_myDID == did) {
13692 vport = vports[i];
13693 break;
13694 }
13695 }
13696 lpfc_destroy_vport_work_array(phba, vports);
13697 return vport;
13698}
13699
13700/**
James Smart45ed1192009-10-02 15:17:02 -040013701 * lpfc_update_rcv_time_stamp - Update vport's rcv seq time stamp
13702 * @vport: The vport to work on.
13703 *
13704 * This function updates the receive sequence time stamp for this vport. The
13705 * receive sequence time stamp indicates the time that the last frame of the
13706 * the sequence that has been idle for the longest amount of time was received.
13707 * the driver uses this time stamp to indicate if any received sequences have
13708 * timed out.
13709 **/
13710void
13711lpfc_update_rcv_time_stamp(struct lpfc_vport *vport)
13712{
13713 struct lpfc_dmabuf *h_buf;
13714 struct hbq_dmabuf *dmabuf = NULL;
13715
13716 /* get the oldest sequence on the rcv list */
13717 h_buf = list_get_first(&vport->rcv_buffer_list,
13718 struct lpfc_dmabuf, list);
13719 if (!h_buf)
13720 return;
13721 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
13722 vport->rcv_buffer_time_stamp = dmabuf->time_stamp;
13723}
13724
13725/**
13726 * lpfc_cleanup_rcv_buffers - Cleans up all outstanding receive sequences.
13727 * @vport: The vport that the received sequences were sent to.
13728 *
13729 * This function cleans up all outstanding received sequences. This is called
13730 * by the driver when a link event or user action invalidates all the received
13731 * sequences.
13732 **/
13733void
13734lpfc_cleanup_rcv_buffers(struct lpfc_vport *vport)
13735{
13736 struct lpfc_dmabuf *h_buf, *hnext;
13737 struct lpfc_dmabuf *d_buf, *dnext;
13738 struct hbq_dmabuf *dmabuf = NULL;
13739
13740 /* start with the oldest sequence on the rcv list */
13741 list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
13742 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
13743 list_del_init(&dmabuf->hbuf.list);
13744 list_for_each_entry_safe(d_buf, dnext,
13745 &dmabuf->dbuf.list, list) {
13746 list_del_init(&d_buf->list);
13747 lpfc_in_buf_free(vport->phba, d_buf);
13748 }
13749 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
13750 }
13751}
13752
13753/**
13754 * lpfc_rcv_seq_check_edtov - Cleans up timed out receive sequences.
13755 * @vport: The vport that the received sequences were sent to.
13756 *
13757 * This function determines whether any received sequences have timed out by
13758 * first checking the vport's rcv_buffer_time_stamp. If this time_stamp
13759 * indicates that there is at least one timed out sequence this routine will
13760 * go through the received sequences one at a time from most inactive to most
13761 * active to determine which ones need to be cleaned up. Once it has determined
13762 * that a sequence needs to be cleaned up it will simply free up the resources
13763 * without sending an abort.
13764 **/
13765void
13766lpfc_rcv_seq_check_edtov(struct lpfc_vport *vport)
13767{
13768 struct lpfc_dmabuf *h_buf, *hnext;
13769 struct lpfc_dmabuf *d_buf, *dnext;
13770 struct hbq_dmabuf *dmabuf = NULL;
13771 unsigned long timeout;
13772 int abort_count = 0;
13773
13774 timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
13775 vport->rcv_buffer_time_stamp);
13776 if (list_empty(&vport->rcv_buffer_list) ||
13777 time_before(jiffies, timeout))
13778 return;
13779 /* start with the oldest sequence on the rcv list */
13780 list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
13781 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
13782 timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
13783 dmabuf->time_stamp);
13784 if (time_before(jiffies, timeout))
13785 break;
13786 abort_count++;
13787 list_del_init(&dmabuf->hbuf.list);
13788 list_for_each_entry_safe(d_buf, dnext,
13789 &dmabuf->dbuf.list, list) {
13790 list_del_init(&d_buf->list);
13791 lpfc_in_buf_free(vport->phba, d_buf);
13792 }
13793 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
13794 }
13795 if (abort_count)
13796 lpfc_update_rcv_time_stamp(vport);
13797}
13798
13799/**
James Smart4f774512009-05-22 14:52:35 -040013800 * lpfc_fc_frame_add - Adds a frame to the vport's list of received sequences
13801 * @dmabuf: pointer to a dmabuf that describes the hdr and data of the FC frame
13802 *
13803 * This function searches through the existing incomplete sequences that have
13804 * been sent to this @vport. If the frame matches one of the incomplete
13805 * sequences then the dbuf in the @dmabuf is added to the list of frames that
13806 * make up that sequence. If no sequence is found that matches this frame then
13807 * the function will add the hbuf in the @dmabuf to the @vport's rcv_buffer_list
13808 * This function returns a pointer to the first dmabuf in the sequence list that
13809 * the frame was linked to.
13810 **/
13811static struct hbq_dmabuf *
13812lpfc_fc_frame_add(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
13813{
13814 struct fc_frame_header *new_hdr;
13815 struct fc_frame_header *temp_hdr;
13816 struct lpfc_dmabuf *d_buf;
13817 struct lpfc_dmabuf *h_buf;
13818 struct hbq_dmabuf *seq_dmabuf = NULL;
13819 struct hbq_dmabuf *temp_dmabuf = NULL;
13820
James Smart4d9ab992009-10-02 15:16:39 -040013821 INIT_LIST_HEAD(&dmabuf->dbuf.list);
James Smart45ed1192009-10-02 15:17:02 -040013822 dmabuf->time_stamp = jiffies;
James Smart4f774512009-05-22 14:52:35 -040013823 new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
13824 /* Use the hdr_buf to find the sequence that this frame belongs to */
13825 list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
13826 temp_hdr = (struct fc_frame_header *)h_buf->virt;
13827 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
13828 (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
13829 (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
13830 continue;
13831 /* found a pending sequence that matches this frame */
13832 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
13833 break;
13834 }
13835 if (!seq_dmabuf) {
13836 /*
13837 * This indicates first frame received for this sequence.
13838 * Queue the buffer on the vport's rcv_buffer_list.
13839 */
13840 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
James Smart45ed1192009-10-02 15:17:02 -040013841 lpfc_update_rcv_time_stamp(vport);
James Smart4f774512009-05-22 14:52:35 -040013842 return dmabuf;
13843 }
13844 temp_hdr = seq_dmabuf->hbuf.virt;
James Smarteeead812009-12-21 17:01:23 -050013845 if (be16_to_cpu(new_hdr->fh_seq_cnt) <
13846 be16_to_cpu(temp_hdr->fh_seq_cnt)) {
James Smart4d9ab992009-10-02 15:16:39 -040013847 list_del_init(&seq_dmabuf->hbuf.list);
13848 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
13849 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
James Smart45ed1192009-10-02 15:17:02 -040013850 lpfc_update_rcv_time_stamp(vport);
James Smart4f774512009-05-22 14:52:35 -040013851 return dmabuf;
13852 }
James Smart45ed1192009-10-02 15:17:02 -040013853 /* move this sequence to the tail to indicate a young sequence */
13854 list_move_tail(&seq_dmabuf->hbuf.list, &vport->rcv_buffer_list);
13855 seq_dmabuf->time_stamp = jiffies;
13856 lpfc_update_rcv_time_stamp(vport);
James Smarteeead812009-12-21 17:01:23 -050013857 if (list_empty(&seq_dmabuf->dbuf.list)) {
13858 temp_hdr = dmabuf->hbuf.virt;
13859 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
13860 return seq_dmabuf;
13861 }
James Smart4f774512009-05-22 14:52:35 -040013862 /* find the correct place in the sequence to insert this frame */
13863 list_for_each_entry_reverse(d_buf, &seq_dmabuf->dbuf.list, list) {
13864 temp_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
13865 temp_hdr = (struct fc_frame_header *)temp_dmabuf->hbuf.virt;
13866 /*
13867 * If the frame's sequence count is greater than the frame on
13868 * the list then insert the frame right after this frame
13869 */
James Smarteeead812009-12-21 17:01:23 -050013870 if (be16_to_cpu(new_hdr->fh_seq_cnt) >
13871 be16_to_cpu(temp_hdr->fh_seq_cnt)) {
James Smart4f774512009-05-22 14:52:35 -040013872 list_add(&dmabuf->dbuf.list, &temp_dmabuf->dbuf.list);
13873 return seq_dmabuf;
13874 }
13875 }
13876 return NULL;
13877}
13878
13879/**
James Smart6669f9b2009-10-02 15:16:45 -040013880 * lpfc_sli4_abort_partial_seq - Abort partially assembled unsol sequence
13881 * @vport: pointer to a vitural port
13882 * @dmabuf: pointer to a dmabuf that describes the FC sequence
13883 *
13884 * This function tries to abort from the partially assembed sequence, described
13885 * by the information from basic abbort @dmabuf. It checks to see whether such
13886 * partially assembled sequence held by the driver. If so, it shall free up all
13887 * the frames from the partially assembled sequence.
13888 *
13889 * Return
13890 * true -- if there is matching partially assembled sequence present and all
13891 * the frames freed with the sequence;
13892 * false -- if there is no matching partially assembled sequence present so
13893 * nothing got aborted in the lower layer driver
13894 **/
13895static bool
13896lpfc_sli4_abort_partial_seq(struct lpfc_vport *vport,
13897 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, *n_buf, *h_buf;
13902 struct hbq_dmabuf *seq_dmabuf = NULL;
13903
13904 /* Use the hdr_buf to find the sequence that matches this frame */
13905 INIT_LIST_HEAD(&dmabuf->dbuf.list);
13906 INIT_LIST_HEAD(&dmabuf->hbuf.list);
13907 new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
13908 list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
13909 temp_hdr = (struct fc_frame_header *)h_buf->virt;
13910 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
13911 (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
13912 (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
13913 continue;
13914 /* found a pending sequence that matches this frame */
13915 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
13916 break;
13917 }
13918
13919 /* Free up all the frames from the partially assembled sequence */
13920 if (seq_dmabuf) {
13921 list_for_each_entry_safe(d_buf, n_buf,
13922 &seq_dmabuf->dbuf.list, list) {
13923 list_del_init(&d_buf->list);
13924 lpfc_in_buf_free(vport->phba, d_buf);
13925 }
13926 return true;
13927 }
13928 return false;
13929}
13930
13931/**
James Smart546fc852011-03-11 16:06:29 -050013932 * lpfc_sli4_seq_abort_rsp_cmpl - BLS ABORT RSP seq abort iocb complete handler
James Smart6669f9b2009-10-02 15:16:45 -040013933 * @phba: Pointer to HBA context object.
13934 * @cmd_iocbq: pointer to the command iocbq structure.
13935 * @rsp_iocbq: pointer to the response iocbq structure.
13936 *
James Smart546fc852011-03-11 16:06:29 -050013937 * This function handles the sequence abort response iocb command complete
James Smart6669f9b2009-10-02 15:16:45 -040013938 * event. It properly releases the memory allocated to the sequence abort
13939 * accept iocb.
13940 **/
13941static void
James Smart546fc852011-03-11 16:06:29 -050013942lpfc_sli4_seq_abort_rsp_cmpl(struct lpfc_hba *phba,
James Smart6669f9b2009-10-02 15:16:45 -040013943 struct lpfc_iocbq *cmd_iocbq,
13944 struct lpfc_iocbq *rsp_iocbq)
13945{
13946 if (cmd_iocbq)
13947 lpfc_sli_release_iocbq(phba, cmd_iocbq);
James Smart6b5151f2012-01-18 16:24:06 -050013948
13949 /* Failure means BLS ABORT RSP did not get delivered to remote node*/
13950 if (rsp_iocbq && rsp_iocbq->iocb.ulpStatus)
13951 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13952 "3154 BLS ABORT RSP failed, data: x%x/x%x\n",
13953 rsp_iocbq->iocb.ulpStatus,
13954 rsp_iocbq->iocb.un.ulpWord[4]);
James Smart6669f9b2009-10-02 15:16:45 -040013955}
13956
13957/**
James Smart6d368e52011-05-24 11:44:12 -040013958 * lpfc_sli4_xri_inrange - check xri is in range of xris owned by driver.
13959 * @phba: Pointer to HBA context object.
13960 * @xri: xri id in transaction.
13961 *
13962 * This function validates the xri maps to the known range of XRIs allocated an
13963 * used by the driver.
13964 **/
James Smart7851fe22011-07-22 18:36:52 -040013965uint16_t
James Smart6d368e52011-05-24 11:44:12 -040013966lpfc_sli4_xri_inrange(struct lpfc_hba *phba,
13967 uint16_t xri)
13968{
13969 int i;
13970
13971 for (i = 0; i < phba->sli4_hba.max_cfg_param.max_xri; i++) {
13972 if (xri == phba->sli4_hba.xri_ids[i])
13973 return i;
13974 }
13975 return NO_XRI;
13976}
13977
James Smart6d368e52011-05-24 11:44:12 -040013978/**
James Smart546fc852011-03-11 16:06:29 -050013979 * lpfc_sli4_seq_abort_rsp - bls rsp to sequence abort
James Smart6669f9b2009-10-02 15:16:45 -040013980 * @phba: Pointer to HBA context object.
13981 * @fc_hdr: pointer to a FC frame header.
13982 *
James Smart546fc852011-03-11 16:06:29 -050013983 * This function sends a basic response to a previous unsol sequence abort
James Smart6669f9b2009-10-02 15:16:45 -040013984 * event after aborting the sequence handling.
13985 **/
13986static void
James Smart546fc852011-03-11 16:06:29 -050013987lpfc_sli4_seq_abort_rsp(struct lpfc_hba *phba,
James Smart6669f9b2009-10-02 15:16:45 -040013988 struct fc_frame_header *fc_hdr)
13989{
13990 struct lpfc_iocbq *ctiocb = NULL;
13991 struct lpfc_nodelist *ndlp;
James Smartee0f4fe2012-05-09 21:19:14 -040013992 uint16_t oxid, rxid, xri, lxri;
James Smart5ffc2662009-11-18 15:39:44 -050013993 uint32_t sid, fctl;
James Smart6669f9b2009-10-02 15:16:45 -040013994 IOCB_t *icmd;
James Smart546fc852011-03-11 16:06:29 -050013995 int rc;
James Smart6669f9b2009-10-02 15:16:45 -040013996
13997 if (!lpfc_is_link_up(phba))
13998 return;
13999
14000 sid = sli4_sid_from_fc_hdr(fc_hdr);
14001 oxid = be16_to_cpu(fc_hdr->fh_ox_id);
James Smart5ffc2662009-11-18 15:39:44 -050014002 rxid = be16_to_cpu(fc_hdr->fh_rx_id);
James Smart6669f9b2009-10-02 15:16:45 -040014003
14004 ndlp = lpfc_findnode_did(phba->pport, sid);
14005 if (!ndlp) {
14006 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
14007 "1268 Find ndlp returned NULL for oxid:x%x "
14008 "SID:x%x\n", oxid, sid);
14009 return;
14010 }
14011
James Smart546fc852011-03-11 16:06:29 -050014012 /* Allocate buffer for rsp iocb */
James Smart6669f9b2009-10-02 15:16:45 -040014013 ctiocb = lpfc_sli_get_iocbq(phba);
14014 if (!ctiocb)
14015 return;
14016
James Smart5ffc2662009-11-18 15:39:44 -050014017 /* Extract the F_CTL field from FC_HDR */
14018 fctl = sli4_fctl_from_fc_hdr(fc_hdr);
14019
James Smart6669f9b2009-10-02 15:16:45 -040014020 icmd = &ctiocb->iocb;
James Smart6669f9b2009-10-02 15:16:45 -040014021 icmd->un.xseq64.bdl.bdeSize = 0;
James Smart5ffc2662009-11-18 15:39:44 -050014022 icmd->un.xseq64.bdl.ulpIoTag32 = 0;
James Smart6669f9b2009-10-02 15:16:45 -040014023 icmd->un.xseq64.w5.hcsw.Dfctl = 0;
14024 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_ACC;
14025 icmd->un.xseq64.w5.hcsw.Type = FC_TYPE_BLS;
14026
14027 /* Fill in the rest of iocb fields */
14028 icmd->ulpCommand = CMD_XMIT_BLS_RSP64_CX;
14029 icmd->ulpBdeCount = 0;
14030 icmd->ulpLe = 1;
14031 icmd->ulpClass = CLASS3;
James Smart6d368e52011-05-24 11:44:12 -040014032 icmd->ulpContext = phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
James Smartbe858b62010-12-15 17:57:20 -050014033 ctiocb->context1 = ndlp;
James Smart6669f9b2009-10-02 15:16:45 -040014034
James Smart6669f9b2009-10-02 15:16:45 -040014035 ctiocb->iocb_cmpl = NULL;
14036 ctiocb->vport = phba->pport;
James Smart546fc852011-03-11 16:06:29 -050014037 ctiocb->iocb_cmpl = lpfc_sli4_seq_abort_rsp_cmpl;
James Smart6d368e52011-05-24 11:44:12 -040014038 ctiocb->sli4_lxritag = NO_XRI;
James Smart546fc852011-03-11 16:06:29 -050014039 ctiocb->sli4_xritag = NO_XRI;
14040
James Smartee0f4fe2012-05-09 21:19:14 -040014041 if (fctl & FC_FC_EX_CTX)
14042 /* Exchange responder sent the abort so we
14043 * own the oxid.
14044 */
14045 xri = oxid;
14046 else
14047 xri = rxid;
14048 lxri = lpfc_sli4_xri_inrange(phba, xri);
14049 if (lxri != NO_XRI)
14050 lpfc_set_rrq_active(phba, ndlp, lxri,
14051 (xri == oxid) ? rxid : oxid, 0);
James Smart546fc852011-03-11 16:06:29 -050014052 /* If the oxid maps to the FCP XRI range or if it is out of range,
14053 * send a BLS_RJT. The driver no longer has that exchange.
14054 * Override the IOCB for a BA_RJT.
14055 */
James Smartee0f4fe2012-05-09 21:19:14 -040014056 if (xri > (phba->sli4_hba.max_cfg_param.max_xri +
James Smart546fc852011-03-11 16:06:29 -050014057 phba->sli4_hba.max_cfg_param.xri_base) ||
James Smartee0f4fe2012-05-09 21:19:14 -040014058 xri > (lpfc_sli4_get_els_iocb_cnt(phba) +
James Smart546fc852011-03-11 16:06:29 -050014059 phba->sli4_hba.max_cfg_param.xri_base)) {
14060 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_RJT;
14061 bf_set(lpfc_vndr_code, &icmd->un.bls_rsp, 0);
14062 bf_set(lpfc_rsn_expln, &icmd->un.bls_rsp, FC_BA_RJT_INV_XID);
14063 bf_set(lpfc_rsn_code, &icmd->un.bls_rsp, FC_BA_RJT_UNABLE);
14064 }
James Smart6669f9b2009-10-02 15:16:45 -040014065
James Smart5ffc2662009-11-18 15:39:44 -050014066 if (fctl & FC_FC_EX_CTX) {
14067 /* ABTS sent by responder to CT exchange, construction
14068 * of BA_ACC will use OX_ID from ABTS for the XRI_TAG
14069 * field and RX_ID from ABTS for RX_ID field.
14070 */
James Smart546fc852011-03-11 16:06:29 -050014071 bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_RSP);
James Smart5ffc2662009-11-18 15:39:44 -050014072 } else {
14073 /* ABTS sent by initiator to CT exchange, construction
14074 * of BA_ACC will need to allocate a new XRI as for the
James Smartf09c3ac2012-03-01 22:33:29 -050014075 * XRI_TAG field.
James Smart5ffc2662009-11-18 15:39:44 -050014076 */
James Smart546fc852011-03-11 16:06:29 -050014077 bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_INT);
James Smart5ffc2662009-11-18 15:39:44 -050014078 }
James Smartf09c3ac2012-03-01 22:33:29 -050014079 bf_set(lpfc_abts_rxid, &icmd->un.bls_rsp, rxid);
James Smart546fc852011-03-11 16:06:29 -050014080 bf_set(lpfc_abts_oxid, &icmd->un.bls_rsp, oxid);
James Smart5ffc2662009-11-18 15:39:44 -050014081
James Smart546fc852011-03-11 16:06:29 -050014082 /* Xmit CT abts response on exchange <xid> */
James Smart6669f9b2009-10-02 15:16:45 -040014083 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
James Smart546fc852011-03-11 16:06:29 -050014084 "1200 Send BLS cmd x%x on oxid x%x Data: x%x\n",
14085 icmd->un.xseq64.w5.hcsw.Rctl, oxid, phba->link_state);
14086
14087 rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, ctiocb, 0);
14088 if (rc == IOCB_ERROR) {
14089 lpfc_printf_log(phba, KERN_ERR, LOG_ELS,
14090 "2925 Failed to issue CT ABTS RSP x%x on "
14091 "xri x%x, Data x%x\n",
14092 icmd->un.xseq64.w5.hcsw.Rctl, oxid,
14093 phba->link_state);
14094 lpfc_sli_release_iocbq(phba, ctiocb);
14095 }
James Smart6669f9b2009-10-02 15:16:45 -040014096}
14097
14098/**
14099 * lpfc_sli4_handle_unsol_abort - Handle sli-4 unsolicited abort event
14100 * @vport: Pointer to the vport on which this sequence was received
14101 * @dmabuf: pointer to a dmabuf that describes the FC sequence
14102 *
14103 * This function handles an SLI-4 unsolicited abort event. If the unsolicited
14104 * receive sequence is only partially assembed by the driver, it shall abort
14105 * the partially assembled frames for the sequence. Otherwise, if the
14106 * unsolicited receive sequence has been completely assembled and passed to
14107 * the Upper Layer Protocol (UPL), it then mark the per oxid status for the
14108 * unsolicited sequence has been aborted. After that, it will issue a basic
14109 * accept to accept the abort.
14110 **/
14111void
14112lpfc_sli4_handle_unsol_abort(struct lpfc_vport *vport,
14113 struct hbq_dmabuf *dmabuf)
14114{
14115 struct lpfc_hba *phba = vport->phba;
14116 struct fc_frame_header fc_hdr;
James Smart5ffc2662009-11-18 15:39:44 -050014117 uint32_t fctl;
James Smart6669f9b2009-10-02 15:16:45 -040014118 bool abts_par;
14119
James Smart6669f9b2009-10-02 15:16:45 -040014120 /* Make a copy of fc_hdr before the dmabuf being released */
14121 memcpy(&fc_hdr, dmabuf->hbuf.virt, sizeof(struct fc_frame_header));
James Smart5ffc2662009-11-18 15:39:44 -050014122 fctl = sli4_fctl_from_fc_hdr(&fc_hdr);
James Smart6669f9b2009-10-02 15:16:45 -040014123
James Smart5ffc2662009-11-18 15:39:44 -050014124 if (fctl & FC_FC_EX_CTX) {
14125 /*
14126 * ABTS sent by responder to exchange, just free the buffer
14127 */
James Smart6669f9b2009-10-02 15:16:45 -040014128 lpfc_in_buf_free(phba, &dmabuf->dbuf);
James Smart5ffc2662009-11-18 15:39:44 -050014129 } else {
14130 /*
14131 * ABTS sent by initiator to exchange, need to do cleanup
14132 */
14133 /* Try to abort partially assembled seq */
14134 abts_par = lpfc_sli4_abort_partial_seq(vport, dmabuf);
14135
14136 /* Send abort to ULP if partially seq abort failed */
14137 if (abts_par == false)
14138 lpfc_sli4_send_seq_to_ulp(vport, dmabuf);
14139 else
14140 lpfc_in_buf_free(phba, &dmabuf->dbuf);
14141 }
James Smart6669f9b2009-10-02 15:16:45 -040014142 /* Send basic accept (BA_ACC) to the abort requester */
James Smart546fc852011-03-11 16:06:29 -050014143 lpfc_sli4_seq_abort_rsp(phba, &fc_hdr);
James Smart6669f9b2009-10-02 15:16:45 -040014144}
14145
14146/**
James Smart4f774512009-05-22 14:52:35 -040014147 * lpfc_seq_complete - Indicates if a sequence is complete
14148 * @dmabuf: pointer to a dmabuf that describes the FC sequence
14149 *
14150 * This function checks the sequence, starting with the frame described by
14151 * @dmabuf, to see if all the frames associated with this sequence are present.
14152 * the frames associated with this sequence are linked to the @dmabuf using the
14153 * dbuf list. This function looks for two major things. 1) That the first frame
14154 * has a sequence count of zero. 2) There is a frame with last frame of sequence
14155 * set. 3) That there are no holes in the sequence count. The function will
14156 * return 1 when the sequence is complete, otherwise it will return 0.
14157 **/
14158static int
14159lpfc_seq_complete(struct hbq_dmabuf *dmabuf)
14160{
14161 struct fc_frame_header *hdr;
14162 struct lpfc_dmabuf *d_buf;
14163 struct hbq_dmabuf *seq_dmabuf;
14164 uint32_t fctl;
14165 int seq_count = 0;
14166
14167 hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
14168 /* make sure first fame of sequence has a sequence count of zero */
14169 if (hdr->fh_seq_cnt != seq_count)
14170 return 0;
14171 fctl = (hdr->fh_f_ctl[0] << 16 |
14172 hdr->fh_f_ctl[1] << 8 |
14173 hdr->fh_f_ctl[2]);
14174 /* If last frame of sequence we can return success. */
14175 if (fctl & FC_FC_END_SEQ)
14176 return 1;
14177 list_for_each_entry(d_buf, &dmabuf->dbuf.list, list) {
14178 seq_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
14179 hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
14180 /* If there is a hole in the sequence count then fail. */
James Smarteeead812009-12-21 17:01:23 -050014181 if (++seq_count != be16_to_cpu(hdr->fh_seq_cnt))
James Smart4f774512009-05-22 14:52:35 -040014182 return 0;
14183 fctl = (hdr->fh_f_ctl[0] << 16 |
14184 hdr->fh_f_ctl[1] << 8 |
14185 hdr->fh_f_ctl[2]);
14186 /* If last frame of sequence we can return success. */
14187 if (fctl & FC_FC_END_SEQ)
14188 return 1;
14189 }
14190 return 0;
14191}
14192
14193/**
14194 * lpfc_prep_seq - Prep sequence for ULP processing
14195 * @vport: Pointer to the vport on which this sequence was received
14196 * @dmabuf: pointer to a dmabuf that describes the FC sequence
14197 *
14198 * This function takes a sequence, described by a list of frames, and creates
14199 * a list of iocbq structures to describe the sequence. This iocbq list will be
14200 * used to issue to the generic unsolicited sequence handler. This routine
14201 * returns a pointer to the first iocbq in the list. If the function is unable
14202 * to allocate an iocbq then it throw out the received frames that were not
14203 * able to be described and return a pointer to the first iocbq. If unable to
14204 * allocate any iocbqs (including the first) this function will return NULL.
14205 **/
14206static struct lpfc_iocbq *
14207lpfc_prep_seq(struct lpfc_vport *vport, struct hbq_dmabuf *seq_dmabuf)
14208{
James Smart7851fe22011-07-22 18:36:52 -040014209 struct hbq_dmabuf *hbq_buf;
James Smart4f774512009-05-22 14:52:35 -040014210 struct lpfc_dmabuf *d_buf, *n_buf;
14211 struct lpfc_iocbq *first_iocbq, *iocbq;
14212 struct fc_frame_header *fc_hdr;
14213 uint32_t sid;
James Smart7851fe22011-07-22 18:36:52 -040014214 uint32_t len, tot_len;
James Smarteeead812009-12-21 17:01:23 -050014215 struct ulp_bde64 *pbde;
James Smart4f774512009-05-22 14:52:35 -040014216
14217 fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
14218 /* remove from receive buffer list */
14219 list_del_init(&seq_dmabuf->hbuf.list);
James Smart45ed1192009-10-02 15:17:02 -040014220 lpfc_update_rcv_time_stamp(vport);
James Smart4f774512009-05-22 14:52:35 -040014221 /* get the Remote Port's SID */
James Smart6669f9b2009-10-02 15:16:45 -040014222 sid = sli4_sid_from_fc_hdr(fc_hdr);
James Smart7851fe22011-07-22 18:36:52 -040014223 tot_len = 0;
James Smart4f774512009-05-22 14:52:35 -040014224 /* Get an iocbq struct to fill in. */
14225 first_iocbq = lpfc_sli_get_iocbq(vport->phba);
14226 if (first_iocbq) {
14227 /* Initialize the first IOCB. */
James Smart8fa38512009-07-19 10:01:03 -040014228 first_iocbq->iocb.unsli3.rcvsli3.acc_len = 0;
James Smart4f774512009-05-22 14:52:35 -040014229 first_iocbq->iocb.ulpStatus = IOSTAT_SUCCESS;
James Smart939723a2012-05-09 21:19:03 -040014230
14231 /* Check FC Header to see what TYPE of frame we are rcv'ing */
14232 if (sli4_type_from_fc_hdr(fc_hdr) == FC_TYPE_ELS) {
14233 first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_ELS64_CX;
14234 first_iocbq->iocb.un.rcvels.parmRo =
14235 sli4_did_from_fc_hdr(fc_hdr);
14236 first_iocbq->iocb.ulpPU = PARM_NPIV_DID;
14237 } else
14238 first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_SEQ64_CX;
James Smart7851fe22011-07-22 18:36:52 -040014239 first_iocbq->iocb.ulpContext = NO_XRI;
14240 first_iocbq->iocb.unsli3.rcvsli3.ox_id =
14241 be16_to_cpu(fc_hdr->fh_ox_id);
14242 /* iocbq is prepped for internal consumption. Physical vpi. */
14243 first_iocbq->iocb.unsli3.rcvsli3.vpi =
14244 vport->phba->vpi_ids[vport->vpi];
James Smart4f774512009-05-22 14:52:35 -040014245 /* put the first buffer into the first IOCBq */
14246 first_iocbq->context2 = &seq_dmabuf->dbuf;
14247 first_iocbq->context3 = NULL;
14248 first_iocbq->iocb.ulpBdeCount = 1;
14249 first_iocbq->iocb.un.cont64[0].tus.f.bdeSize =
14250 LPFC_DATA_BUF_SIZE;
14251 first_iocbq->iocb.un.rcvels.remoteID = sid;
James Smart7851fe22011-07-22 18:36:52 -040014252 tot_len = bf_get(lpfc_rcqe_length,
James Smart4d9ab992009-10-02 15:16:39 -040014253 &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
James Smart7851fe22011-07-22 18:36:52 -040014254 first_iocbq->iocb.unsli3.rcvsli3.acc_len = tot_len;
James Smart4f774512009-05-22 14:52:35 -040014255 }
14256 iocbq = first_iocbq;
14257 /*
14258 * Each IOCBq can have two Buffers assigned, so go through the list
14259 * of buffers for this sequence and save two buffers in each IOCBq
14260 */
14261 list_for_each_entry_safe(d_buf, n_buf, &seq_dmabuf->dbuf.list, list) {
14262 if (!iocbq) {
14263 lpfc_in_buf_free(vport->phba, d_buf);
14264 continue;
14265 }
14266 if (!iocbq->context3) {
14267 iocbq->context3 = d_buf;
14268 iocbq->iocb.ulpBdeCount++;
James Smarteeead812009-12-21 17:01:23 -050014269 pbde = (struct ulp_bde64 *)
14270 &iocbq->iocb.unsli3.sli3Words[4];
14271 pbde->tus.f.bdeSize = LPFC_DATA_BUF_SIZE;
James Smart7851fe22011-07-22 18:36:52 -040014272
14273 /* We need to get the size out of the right CQE */
14274 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
14275 len = bf_get(lpfc_rcqe_length,
14276 &hbq_buf->cq_event.cqe.rcqe_cmpl);
14277 iocbq->iocb.unsli3.rcvsli3.acc_len += len;
14278 tot_len += len;
James Smart4f774512009-05-22 14:52:35 -040014279 } else {
14280 iocbq = lpfc_sli_get_iocbq(vport->phba);
14281 if (!iocbq) {
14282 if (first_iocbq) {
14283 first_iocbq->iocb.ulpStatus =
14284 IOSTAT_FCP_RSP_ERROR;
14285 first_iocbq->iocb.un.ulpWord[4] =
14286 IOERR_NO_RESOURCES;
14287 }
14288 lpfc_in_buf_free(vport->phba, d_buf);
14289 continue;
14290 }
14291 iocbq->context2 = d_buf;
14292 iocbq->context3 = NULL;
14293 iocbq->iocb.ulpBdeCount = 1;
14294 iocbq->iocb.un.cont64[0].tus.f.bdeSize =
14295 LPFC_DATA_BUF_SIZE;
James Smart7851fe22011-07-22 18:36:52 -040014296
14297 /* We need to get the size out of the right CQE */
14298 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
14299 len = bf_get(lpfc_rcqe_length,
14300 &hbq_buf->cq_event.cqe.rcqe_cmpl);
14301 tot_len += len;
14302 iocbq->iocb.unsli3.rcvsli3.acc_len = tot_len;
14303
James Smart4f774512009-05-22 14:52:35 -040014304 iocbq->iocb.un.rcvels.remoteID = sid;
14305 list_add_tail(&iocbq->list, &first_iocbq->list);
14306 }
14307 }
14308 return first_iocbq;
14309}
14310
James Smart6669f9b2009-10-02 15:16:45 -040014311static void
14312lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *vport,
14313 struct hbq_dmabuf *seq_dmabuf)
14314{
14315 struct fc_frame_header *fc_hdr;
14316 struct lpfc_iocbq *iocbq, *curr_iocb, *next_iocb;
14317 struct lpfc_hba *phba = vport->phba;
14318
14319 fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
14320 iocbq = lpfc_prep_seq(vport, seq_dmabuf);
14321 if (!iocbq) {
14322 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14323 "2707 Ring %d handler: Failed to allocate "
14324 "iocb Rctl x%x Type x%x received\n",
14325 LPFC_ELS_RING,
14326 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
14327 return;
14328 }
14329 if (!lpfc_complete_unsol_iocb(phba,
14330 &phba->sli.ring[LPFC_ELS_RING],
14331 iocbq, fc_hdr->fh_r_ctl,
14332 fc_hdr->fh_type))
James Smart6d368e52011-05-24 11:44:12 -040014333 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart6669f9b2009-10-02 15:16:45 -040014334 "2540 Ring %d handler: unexpected Rctl "
14335 "x%x Type x%x received\n",
14336 LPFC_ELS_RING,
14337 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
14338
14339 /* Free iocb created in lpfc_prep_seq */
14340 list_for_each_entry_safe(curr_iocb, next_iocb,
14341 &iocbq->list, list) {
14342 list_del_init(&curr_iocb->list);
14343 lpfc_sli_release_iocbq(phba, curr_iocb);
14344 }
14345 lpfc_sli_release_iocbq(phba, iocbq);
14346}
14347
James Smart4f774512009-05-22 14:52:35 -040014348/**
14349 * lpfc_sli4_handle_received_buffer - Handle received buffers from firmware
14350 * @phba: Pointer to HBA context object.
14351 *
14352 * This function is called with no lock held. This function processes all
14353 * the received buffers and gives it to upper layers when a received buffer
14354 * indicates that it is the final frame in the sequence. The interrupt
14355 * service routine processes received buffers at interrupt contexts and adds
14356 * received dma buffers to the rb_pend_list queue and signals the worker thread.
14357 * Worker thread calls lpfc_sli4_handle_received_buffer, which will call the
14358 * appropriate receive function when the final frame in a sequence is received.
14359 **/
James Smart4d9ab992009-10-02 15:16:39 -040014360void
14361lpfc_sli4_handle_received_buffer(struct lpfc_hba *phba,
14362 struct hbq_dmabuf *dmabuf)
James Smart4f774512009-05-22 14:52:35 -040014363{
James Smart4d9ab992009-10-02 15:16:39 -040014364 struct hbq_dmabuf *seq_dmabuf;
James Smart4f774512009-05-22 14:52:35 -040014365 struct fc_frame_header *fc_hdr;
14366 struct lpfc_vport *vport;
14367 uint32_t fcfi;
James Smart939723a2012-05-09 21:19:03 -040014368 uint32_t did;
James Smart4f774512009-05-22 14:52:35 -040014369
James Smart4f774512009-05-22 14:52:35 -040014370 /* Process each received buffer */
James Smart4d9ab992009-10-02 15:16:39 -040014371 fc_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
14372 /* check to see if this a valid type of frame */
14373 if (lpfc_fc_frame_check(phba, fc_hdr)) {
14374 lpfc_in_buf_free(phba, &dmabuf->dbuf);
14375 return;
14376 }
James Smart7851fe22011-07-22 18:36:52 -040014377 if ((bf_get(lpfc_cqe_code,
14378 &dmabuf->cq_event.cqe.rcqe_cmpl) == CQE_CODE_RECEIVE_V1))
14379 fcfi = bf_get(lpfc_rcqe_fcf_id_v1,
14380 &dmabuf->cq_event.cqe.rcqe_cmpl);
14381 else
14382 fcfi = bf_get(lpfc_rcqe_fcf_id,
14383 &dmabuf->cq_event.cqe.rcqe_cmpl);
James Smart939723a2012-05-09 21:19:03 -040014384
James Smart4d9ab992009-10-02 15:16:39 -040014385 vport = lpfc_fc_frame_to_vport(phba, fc_hdr, fcfi);
James Smart939723a2012-05-09 21:19:03 -040014386 if (!vport) {
James Smart4d9ab992009-10-02 15:16:39 -040014387 /* throw out the frame */
14388 lpfc_in_buf_free(phba, &dmabuf->dbuf);
14389 return;
14390 }
James Smart939723a2012-05-09 21:19:03 -040014391
14392 /* d_id this frame is directed to */
14393 did = sli4_did_from_fc_hdr(fc_hdr);
14394
14395 /* vport is registered unless we rcv a FLOGI directed to Fabric_DID */
14396 if (!(vport->vpi_state & LPFC_VPI_REGISTERED) &&
14397 (did != Fabric_DID)) {
14398 /*
14399 * Throw out the frame if we are not pt2pt.
14400 * The pt2pt protocol allows for discovery frames
14401 * to be received without a registered VPI.
14402 */
14403 if (!(vport->fc_flag & FC_PT2PT) ||
14404 (phba->link_state == LPFC_HBA_READY)) {
14405 lpfc_in_buf_free(phba, &dmabuf->dbuf);
14406 return;
14407 }
14408 }
14409
James Smart6669f9b2009-10-02 15:16:45 -040014410 /* Handle the basic abort sequence (BA_ABTS) event */
14411 if (fc_hdr->fh_r_ctl == FC_RCTL_BA_ABTS) {
14412 lpfc_sli4_handle_unsol_abort(vport, dmabuf);
14413 return;
14414 }
14415
James Smart4d9ab992009-10-02 15:16:39 -040014416 /* Link this frame */
14417 seq_dmabuf = lpfc_fc_frame_add(vport, dmabuf);
14418 if (!seq_dmabuf) {
14419 /* unable to add frame to vport - throw it out */
14420 lpfc_in_buf_free(phba, &dmabuf->dbuf);
14421 return;
14422 }
14423 /* If not last frame in sequence continue processing frames. */
James Smartdef9c7a2009-12-21 17:02:28 -050014424 if (!lpfc_seq_complete(seq_dmabuf))
James Smart4d9ab992009-10-02 15:16:39 -040014425 return;
James Smartdef9c7a2009-12-21 17:02:28 -050014426
James Smart6669f9b2009-10-02 15:16:45 -040014427 /* Send the complete sequence to the upper layer protocol */
14428 lpfc_sli4_send_seq_to_ulp(vport, seq_dmabuf);
James Smart4f774512009-05-22 14:52:35 -040014429}
James Smart6fb120a2009-05-22 14:52:59 -040014430
14431/**
14432 * lpfc_sli4_post_all_rpi_hdrs - Post the rpi header memory region to the port
14433 * @phba: pointer to lpfc hba data structure.
14434 *
14435 * This routine is invoked to post rpi header templates to the
14436 * HBA consistent with the SLI-4 interface spec. This routine
James Smart49198b32010-04-06 15:04:33 -040014437 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
14438 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
James Smart6fb120a2009-05-22 14:52:59 -040014439 *
14440 * This routine does not require any locks. It's usage is expected
14441 * to be driver load or reset recovery when the driver is
14442 * sequential.
14443 *
14444 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020014445 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -040014446 * -EIO - The mailbox failed to complete successfully.
James Smart6fb120a2009-05-22 14:52:59 -040014447 * When this error occurs, the driver is not guaranteed
14448 * to have any rpi regions posted to the device and
14449 * must either attempt to repost the regions or take a
14450 * fatal error.
14451 **/
14452int
14453lpfc_sli4_post_all_rpi_hdrs(struct lpfc_hba *phba)
14454{
14455 struct lpfc_rpi_hdr *rpi_page;
14456 uint32_t rc = 0;
James Smart6d368e52011-05-24 11:44:12 -040014457 uint16_t lrpi = 0;
James Smart6fb120a2009-05-22 14:52:59 -040014458
James Smart6d368e52011-05-24 11:44:12 -040014459 /* SLI4 ports that support extents do not require RPI headers. */
14460 if (!phba->sli4_hba.rpi_hdrs_in_use)
14461 goto exit;
14462 if (phba->sli4_hba.extents_in_use)
14463 return -EIO;
14464
James Smart6fb120a2009-05-22 14:52:59 -040014465 list_for_each_entry(rpi_page, &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
James Smart6d368e52011-05-24 11:44:12 -040014466 /*
14467 * Assign the rpi headers a physical rpi only if the driver
14468 * has not initialized those resources. A port reset only
14469 * needs the headers posted.
14470 */
14471 if (bf_get(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags) !=
14472 LPFC_RPI_RSRC_RDY)
14473 rpi_page->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
14474
James Smart6fb120a2009-05-22 14:52:59 -040014475 rc = lpfc_sli4_post_rpi_hdr(phba, rpi_page);
14476 if (rc != MBX_SUCCESS) {
14477 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14478 "2008 Error %d posting all rpi "
14479 "headers\n", rc);
14480 rc = -EIO;
14481 break;
14482 }
14483 }
14484
James Smart6d368e52011-05-24 11:44:12 -040014485 exit:
14486 bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags,
14487 LPFC_RPI_RSRC_RDY);
James Smart6fb120a2009-05-22 14:52:59 -040014488 return rc;
14489}
14490
14491/**
14492 * lpfc_sli4_post_rpi_hdr - Post an rpi header memory region to the port
14493 * @phba: pointer to lpfc hba data structure.
14494 * @rpi_page: pointer to the rpi memory region.
14495 *
14496 * This routine is invoked to post a single rpi header to the
14497 * HBA consistent with the SLI-4 interface spec. This memory region
14498 * maps up to 64 rpi context regions.
14499 *
14500 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020014501 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -040014502 * -ENOMEM - No available memory
14503 * -EIO - The mailbox failed to complete successfully.
James Smart6fb120a2009-05-22 14:52:59 -040014504 **/
14505int
14506lpfc_sli4_post_rpi_hdr(struct lpfc_hba *phba, struct lpfc_rpi_hdr *rpi_page)
14507{
14508 LPFC_MBOXQ_t *mboxq;
14509 struct lpfc_mbx_post_hdr_tmpl *hdr_tmpl;
14510 uint32_t rc = 0;
James Smart6fb120a2009-05-22 14:52:59 -040014511 uint32_t shdr_status, shdr_add_status;
14512 union lpfc_sli4_cfg_shdr *shdr;
14513
James Smart6d368e52011-05-24 11:44:12 -040014514 /* SLI4 ports that support extents do not require RPI headers. */
14515 if (!phba->sli4_hba.rpi_hdrs_in_use)
14516 return rc;
14517 if (phba->sli4_hba.extents_in_use)
14518 return -EIO;
14519
James Smart6fb120a2009-05-22 14:52:59 -040014520 /* The port is notified of the header region via a mailbox command. */
14521 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14522 if (!mboxq) {
14523 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14524 "2001 Unable to allocate memory for issuing "
14525 "SLI_CONFIG_SPECIAL mailbox command\n");
14526 return -ENOMEM;
14527 }
14528
14529 /* Post all rpi memory regions to the port. */
14530 hdr_tmpl = &mboxq->u.mqe.un.hdr_tmpl;
James Smart6fb120a2009-05-22 14:52:59 -040014531 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
14532 LPFC_MBOX_OPCODE_FCOE_POST_HDR_TEMPLATE,
14533 sizeof(struct lpfc_mbx_post_hdr_tmpl) -
James Smartfedd3b72011-02-16 12:39:24 -050014534 sizeof(struct lpfc_sli4_cfg_mhdr),
14535 LPFC_SLI4_MBX_EMBED);
James Smart6d368e52011-05-24 11:44:12 -040014536
14537
14538 /* Post the physical rpi to the port for this rpi header. */
James Smart6fb120a2009-05-22 14:52:59 -040014539 bf_set(lpfc_mbx_post_hdr_tmpl_rpi_offset, hdr_tmpl,
14540 rpi_page->start_rpi);
James Smart6d368e52011-05-24 11:44:12 -040014541 bf_set(lpfc_mbx_post_hdr_tmpl_page_cnt,
14542 hdr_tmpl, rpi_page->page_count);
14543
James Smart6fb120a2009-05-22 14:52:59 -040014544 hdr_tmpl->rpi_paddr_lo = putPaddrLow(rpi_page->dmabuf->phys);
14545 hdr_tmpl->rpi_paddr_hi = putPaddrHigh(rpi_page->dmabuf->phys);
James Smartf1126682009-06-10 17:22:44 -040014546 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
James Smart6fb120a2009-05-22 14:52:59 -040014547 shdr = (union lpfc_sli4_cfg_shdr *) &hdr_tmpl->header.cfg_shdr;
14548 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14549 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14550 if (rc != MBX_TIMEOUT)
14551 mempool_free(mboxq, phba->mbox_mem_pool);
14552 if (shdr_status || shdr_add_status || rc) {
14553 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14554 "2514 POST_RPI_HDR mailbox failed with "
14555 "status x%x add_status x%x, mbx status x%x\n",
14556 shdr_status, shdr_add_status, rc);
14557 rc = -ENXIO;
14558 }
14559 return rc;
14560}
14561
14562/**
14563 * lpfc_sli4_alloc_rpi - Get an available rpi in the device's range
14564 * @phba: pointer to lpfc hba data structure.
14565 *
14566 * This routine is invoked to post rpi header templates to the
14567 * HBA consistent with the SLI-4 interface spec. This routine
James Smart49198b32010-04-06 15:04:33 -040014568 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
14569 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
James Smart6fb120a2009-05-22 14:52:59 -040014570 *
14571 * Returns
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020014572 * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
James Smart6fb120a2009-05-22 14:52:59 -040014573 * LPFC_RPI_ALLOC_ERROR if no rpis are available.
14574 **/
14575int
14576lpfc_sli4_alloc_rpi(struct lpfc_hba *phba)
14577{
James Smart6d368e52011-05-24 11:44:12 -040014578 unsigned long rpi;
14579 uint16_t max_rpi, rpi_limit;
14580 uint16_t rpi_remaining, lrpi = 0;
James Smart6fb120a2009-05-22 14:52:59 -040014581 struct lpfc_rpi_hdr *rpi_hdr;
14582
14583 max_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
James Smart6fb120a2009-05-22 14:52:59 -040014584 rpi_limit = phba->sli4_hba.next_rpi;
14585
14586 /*
James Smart6d368e52011-05-24 11:44:12 -040014587 * Fetch the next logical rpi. Because this index is logical,
14588 * the driver starts at 0 each time.
James Smart6fb120a2009-05-22 14:52:59 -040014589 */
14590 spin_lock_irq(&phba->hbalock);
James Smart6d368e52011-05-24 11:44:12 -040014591 rpi = find_next_zero_bit(phba->sli4_hba.rpi_bmask, rpi_limit, 0);
14592 if (rpi >= rpi_limit)
James Smart6fb120a2009-05-22 14:52:59 -040014593 rpi = LPFC_RPI_ALLOC_ERROR;
14594 else {
14595 set_bit(rpi, phba->sli4_hba.rpi_bmask);
14596 phba->sli4_hba.max_cfg_param.rpi_used++;
14597 phba->sli4_hba.rpi_count++;
14598 }
14599
14600 /*
14601 * Don't try to allocate more rpi header regions if the device limit
James Smart6d368e52011-05-24 11:44:12 -040014602 * has been exhausted.
James Smart6fb120a2009-05-22 14:52:59 -040014603 */
14604 if ((rpi == LPFC_RPI_ALLOC_ERROR) &&
14605 (phba->sli4_hba.rpi_count >= max_rpi)) {
14606 spin_unlock_irq(&phba->hbalock);
14607 return rpi;
14608 }
14609
14610 /*
James Smart6d368e52011-05-24 11:44:12 -040014611 * RPI header postings are not required for SLI4 ports capable of
14612 * extents.
14613 */
14614 if (!phba->sli4_hba.rpi_hdrs_in_use) {
14615 spin_unlock_irq(&phba->hbalock);
14616 return rpi;
14617 }
14618
14619 /*
James Smart6fb120a2009-05-22 14:52:59 -040014620 * If the driver is running low on rpi resources, allocate another
14621 * page now. Note that the next_rpi value is used because
14622 * it represents how many are actually in use whereas max_rpi notes
14623 * how many are supported max by the device.
14624 */
James Smart6d368e52011-05-24 11:44:12 -040014625 rpi_remaining = phba->sli4_hba.next_rpi - phba->sli4_hba.rpi_count;
James Smart6fb120a2009-05-22 14:52:59 -040014626 spin_unlock_irq(&phba->hbalock);
14627 if (rpi_remaining < LPFC_RPI_LOW_WATER_MARK) {
14628 rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
14629 if (!rpi_hdr) {
14630 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14631 "2002 Error Could not grow rpi "
14632 "count\n");
14633 } else {
James Smart6d368e52011-05-24 11:44:12 -040014634 lrpi = rpi_hdr->start_rpi;
14635 rpi_hdr->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
James Smart6fb120a2009-05-22 14:52:59 -040014636 lpfc_sli4_post_rpi_hdr(phba, rpi_hdr);
14637 }
14638 }
14639
14640 return rpi;
14641}
14642
14643/**
14644 * lpfc_sli4_free_rpi - Release an rpi for reuse.
14645 * @phba: pointer to lpfc hba data structure.
14646 *
14647 * This routine is invoked to release an rpi to the pool of
14648 * available rpis maintained by the driver.
14649 **/
14650void
James Smartd7c47992010-06-08 18:31:54 -040014651__lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
14652{
14653 if (test_and_clear_bit(rpi, phba->sli4_hba.rpi_bmask)) {
14654 phba->sli4_hba.rpi_count--;
14655 phba->sli4_hba.max_cfg_param.rpi_used--;
14656 }
14657}
14658
14659/**
14660 * lpfc_sli4_free_rpi - Release an rpi for reuse.
14661 * @phba: pointer to lpfc hba data structure.
14662 *
14663 * This routine is invoked to release an rpi to the pool of
14664 * available rpis maintained by the driver.
14665 **/
14666void
James Smart6fb120a2009-05-22 14:52:59 -040014667lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
14668{
14669 spin_lock_irq(&phba->hbalock);
James Smartd7c47992010-06-08 18:31:54 -040014670 __lpfc_sli4_free_rpi(phba, rpi);
James Smart6fb120a2009-05-22 14:52:59 -040014671 spin_unlock_irq(&phba->hbalock);
14672}
14673
14674/**
14675 * lpfc_sli4_remove_rpis - Remove the rpi bitmask region
14676 * @phba: pointer to lpfc hba data structure.
14677 *
14678 * This routine is invoked to remove the memory region that
14679 * provided rpi via a bitmask.
14680 **/
14681void
14682lpfc_sli4_remove_rpis(struct lpfc_hba *phba)
14683{
14684 kfree(phba->sli4_hba.rpi_bmask);
James Smart6d368e52011-05-24 11:44:12 -040014685 kfree(phba->sli4_hba.rpi_ids);
14686 bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
James Smart6fb120a2009-05-22 14:52:59 -040014687}
14688
14689/**
14690 * lpfc_sli4_resume_rpi - Remove the rpi bitmask region
14691 * @phba: pointer to lpfc hba data structure.
14692 *
14693 * This routine is invoked to remove the memory region that
14694 * provided rpi via a bitmask.
14695 **/
14696int
James Smart6b5151f2012-01-18 16:24:06 -050014697lpfc_sli4_resume_rpi(struct lpfc_nodelist *ndlp,
14698 void (*cmpl)(struct lpfc_hba *, LPFC_MBOXQ_t *), void *arg)
James Smart6fb120a2009-05-22 14:52:59 -040014699{
14700 LPFC_MBOXQ_t *mboxq;
14701 struct lpfc_hba *phba = ndlp->phba;
14702 int rc;
14703
14704 /* The port is notified of the header region via a mailbox command. */
14705 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14706 if (!mboxq)
14707 return -ENOMEM;
14708
14709 /* Post all rpi memory regions to the port. */
14710 lpfc_resume_rpi(mboxq, ndlp);
James Smart6b5151f2012-01-18 16:24:06 -050014711 if (cmpl) {
14712 mboxq->mbox_cmpl = cmpl;
14713 mboxq->context1 = arg;
14714 mboxq->context2 = ndlp;
James Smart72859902012-01-18 16:25:38 -050014715 } else
14716 mboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart6b5151f2012-01-18 16:24:06 -050014717 mboxq->vport = ndlp->vport;
James Smart6fb120a2009-05-22 14:52:59 -040014718 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
14719 if (rc == MBX_NOT_FINISHED) {
14720 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14721 "2010 Resume RPI Mailbox failed "
14722 "status %d, mbxStatus x%x\n", rc,
14723 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
14724 mempool_free(mboxq, phba->mbox_mem_pool);
14725 return -EIO;
14726 }
14727 return 0;
14728}
14729
14730/**
14731 * lpfc_sli4_init_vpi - Initialize a vpi with the port
James Smart76a95d72010-11-20 23:11:48 -050014732 * @vport: Pointer to the vport for which the vpi is being initialized
James Smart6fb120a2009-05-22 14:52:59 -040014733 *
James Smart76a95d72010-11-20 23:11:48 -050014734 * This routine is invoked to activate a vpi with the port.
James Smart6fb120a2009-05-22 14:52:59 -040014735 *
14736 * Returns:
14737 * 0 success
14738 * -Evalue otherwise
14739 **/
14740int
James Smart76a95d72010-11-20 23:11:48 -050014741lpfc_sli4_init_vpi(struct lpfc_vport *vport)
James Smart6fb120a2009-05-22 14:52:59 -040014742{
14743 LPFC_MBOXQ_t *mboxq;
14744 int rc = 0;
James Smart6a9c52c2009-10-02 15:16:51 -040014745 int retval = MBX_SUCCESS;
James Smart6fb120a2009-05-22 14:52:59 -040014746 uint32_t mbox_tmo;
James Smart76a95d72010-11-20 23:11:48 -050014747 struct lpfc_hba *phba = vport->phba;
James Smart6fb120a2009-05-22 14:52:59 -040014748 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14749 if (!mboxq)
14750 return -ENOMEM;
James Smart76a95d72010-11-20 23:11:48 -050014751 lpfc_init_vpi(phba, mboxq, vport->vpi);
James Smarta183a152011-10-10 21:32:43 -040014752 mbox_tmo = lpfc_mbox_tmo_val(phba, mboxq);
James Smart6fb120a2009-05-22 14:52:59 -040014753 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
James Smart6fb120a2009-05-22 14:52:59 -040014754 if (rc != MBX_SUCCESS) {
James Smart76a95d72010-11-20 23:11:48 -050014755 lpfc_printf_vlog(vport, KERN_ERR, LOG_SLI,
James Smart6fb120a2009-05-22 14:52:59 -040014756 "2022 INIT VPI Mailbox failed "
14757 "status %d, mbxStatus x%x\n", rc,
14758 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
James Smart6a9c52c2009-10-02 15:16:51 -040014759 retval = -EIO;
James Smart6fb120a2009-05-22 14:52:59 -040014760 }
James Smart6a9c52c2009-10-02 15:16:51 -040014761 if (rc != MBX_TIMEOUT)
James Smart76a95d72010-11-20 23:11:48 -050014762 mempool_free(mboxq, vport->phba->mbox_mem_pool);
James Smart6a9c52c2009-10-02 15:16:51 -040014763
14764 return retval;
James Smart6fb120a2009-05-22 14:52:59 -040014765}
14766
14767/**
14768 * lpfc_mbx_cmpl_add_fcf_record - add fcf mbox completion handler.
14769 * @phba: pointer to lpfc hba data structure.
14770 * @mboxq: Pointer to mailbox object.
14771 *
14772 * This routine is invoked to manually add a single FCF record. The caller
14773 * must pass a completely initialized FCF_Record. This routine takes
14774 * care of the nonembedded mailbox operations.
14775 **/
14776static void
14777lpfc_mbx_cmpl_add_fcf_record(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
14778{
14779 void *virt_addr;
14780 union lpfc_sli4_cfg_shdr *shdr;
14781 uint32_t shdr_status, shdr_add_status;
14782
14783 virt_addr = mboxq->sge_array->addr[0];
14784 /* The IOCTL status is embedded in the mailbox subheader. */
14785 shdr = (union lpfc_sli4_cfg_shdr *) virt_addr;
14786 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14787 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14788
14789 if ((shdr_status || shdr_add_status) &&
14790 (shdr_status != STATUS_FCF_IN_USE))
14791 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14792 "2558 ADD_FCF_RECORD mailbox failed with "
14793 "status x%x add_status x%x\n",
14794 shdr_status, shdr_add_status);
14795
14796 lpfc_sli4_mbox_cmd_free(phba, mboxq);
14797}
14798
14799/**
14800 * lpfc_sli4_add_fcf_record - Manually add an FCF Record.
14801 * @phba: pointer to lpfc hba data structure.
14802 * @fcf_record: pointer to the initialized fcf record to add.
14803 *
14804 * This routine is invoked to manually add a single FCF record. The caller
14805 * must pass a completely initialized FCF_Record. This routine takes
14806 * care of the nonembedded mailbox operations.
14807 **/
14808int
14809lpfc_sli4_add_fcf_record(struct lpfc_hba *phba, struct fcf_record *fcf_record)
14810{
14811 int rc = 0;
14812 LPFC_MBOXQ_t *mboxq;
14813 uint8_t *bytep;
14814 void *virt_addr;
14815 dma_addr_t phys_addr;
14816 struct lpfc_mbx_sge sge;
14817 uint32_t alloc_len, req_len;
14818 uint32_t fcfindex;
14819
14820 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14821 if (!mboxq) {
14822 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14823 "2009 Failed to allocate mbox for ADD_FCF cmd\n");
14824 return -ENOMEM;
14825 }
14826
14827 req_len = sizeof(struct fcf_record) + sizeof(union lpfc_sli4_cfg_shdr) +
14828 sizeof(uint32_t);
14829
14830 /* Allocate DMA memory and set up the non-embedded mailbox command */
14831 alloc_len = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
14832 LPFC_MBOX_OPCODE_FCOE_ADD_FCF,
14833 req_len, LPFC_SLI4_MBX_NEMBED);
14834 if (alloc_len < req_len) {
14835 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14836 "2523 Allocated DMA memory size (x%x) is "
14837 "less than the requested DMA memory "
14838 "size (x%x)\n", alloc_len, req_len);
14839 lpfc_sli4_mbox_cmd_free(phba, mboxq);
14840 return -ENOMEM;
14841 }
14842
14843 /*
14844 * Get the first SGE entry from the non-embedded DMA memory. This
14845 * routine only uses a single SGE.
14846 */
14847 lpfc_sli4_mbx_sge_get(mboxq, 0, &sge);
14848 phys_addr = getPaddr(sge.pa_hi, sge.pa_lo);
James Smart6fb120a2009-05-22 14:52:59 -040014849 virt_addr = mboxq->sge_array->addr[0];
14850 /*
14851 * Configure the FCF record for FCFI 0. This is the driver's
14852 * hardcoded default and gets used in nonFIP mode.
14853 */
14854 fcfindex = bf_get(lpfc_fcf_record_fcf_index, fcf_record);
14855 bytep = virt_addr + sizeof(union lpfc_sli4_cfg_shdr);
14856 lpfc_sli_pcimem_bcopy(&fcfindex, bytep, sizeof(uint32_t));
14857
14858 /*
14859 * Copy the fcf_index and the FCF Record Data. The data starts after
14860 * the FCoE header plus word10. The data copy needs to be endian
14861 * correct.
14862 */
14863 bytep += sizeof(uint32_t);
14864 lpfc_sli_pcimem_bcopy(fcf_record, bytep, sizeof(struct fcf_record));
14865 mboxq->vport = phba->pport;
14866 mboxq->mbox_cmpl = lpfc_mbx_cmpl_add_fcf_record;
14867 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
14868 if (rc == MBX_NOT_FINISHED) {
14869 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14870 "2515 ADD_FCF_RECORD mailbox failed with "
14871 "status 0x%x\n", rc);
14872 lpfc_sli4_mbox_cmd_free(phba, mboxq);
14873 rc = -EIO;
14874 } else
14875 rc = 0;
14876
14877 return rc;
14878}
14879
14880/**
14881 * lpfc_sli4_build_dflt_fcf_record - Build the driver's default FCF Record.
14882 * @phba: pointer to lpfc hba data structure.
14883 * @fcf_record: pointer to the fcf record to write the default data.
14884 * @fcf_index: FCF table entry index.
14885 *
14886 * This routine is invoked to build the driver's default FCF record. The
14887 * values used are hardcoded. This routine handles memory initialization.
14888 *
14889 **/
14890void
14891lpfc_sli4_build_dflt_fcf_record(struct lpfc_hba *phba,
14892 struct fcf_record *fcf_record,
14893 uint16_t fcf_index)
14894{
14895 memset(fcf_record, 0, sizeof(struct fcf_record));
14896 fcf_record->max_rcv_size = LPFC_FCOE_MAX_RCV_SIZE;
14897 fcf_record->fka_adv_period = LPFC_FCOE_FKA_ADV_PER;
14898 fcf_record->fip_priority = LPFC_FCOE_FIP_PRIORITY;
14899 bf_set(lpfc_fcf_record_mac_0, fcf_record, phba->fc_map[0]);
14900 bf_set(lpfc_fcf_record_mac_1, fcf_record, phba->fc_map[1]);
14901 bf_set(lpfc_fcf_record_mac_2, fcf_record, phba->fc_map[2]);
14902 bf_set(lpfc_fcf_record_mac_3, fcf_record, LPFC_FCOE_FCF_MAC3);
14903 bf_set(lpfc_fcf_record_mac_4, fcf_record, LPFC_FCOE_FCF_MAC4);
14904 bf_set(lpfc_fcf_record_mac_5, fcf_record, LPFC_FCOE_FCF_MAC5);
14905 bf_set(lpfc_fcf_record_fc_map_0, fcf_record, phba->fc_map[0]);
14906 bf_set(lpfc_fcf_record_fc_map_1, fcf_record, phba->fc_map[1]);
14907 bf_set(lpfc_fcf_record_fc_map_2, fcf_record, phba->fc_map[2]);
14908 bf_set(lpfc_fcf_record_fcf_valid, fcf_record, 1);
James Smart0c287582009-06-10 17:22:56 -040014909 bf_set(lpfc_fcf_record_fcf_avail, fcf_record, 1);
James Smart6fb120a2009-05-22 14:52:59 -040014910 bf_set(lpfc_fcf_record_fcf_index, fcf_record, fcf_index);
14911 bf_set(lpfc_fcf_record_mac_addr_prov, fcf_record,
14912 LPFC_FCF_FPMA | LPFC_FCF_SPMA);
14913 /* Set the VLAN bit map */
14914 if (phba->valid_vlan) {
14915 fcf_record->vlan_bitmap[phba->vlan_id / 8]
14916 = 1 << (phba->vlan_id % 8);
14917 }
14918}
14919
14920/**
James Smart0c9ab6f2010-02-26 14:15:57 -050014921 * lpfc_sli4_fcf_scan_read_fcf_rec - Read hba fcf record for fcf scan.
James Smart6fb120a2009-05-22 14:52:59 -040014922 * @phba: pointer to lpfc hba data structure.
14923 * @fcf_index: FCF table entry offset.
14924 *
James Smart0c9ab6f2010-02-26 14:15:57 -050014925 * This routine is invoked to scan the entire FCF table by reading FCF
14926 * record and processing it one at a time starting from the @fcf_index
14927 * for initial FCF discovery or fast FCF failover rediscovery.
14928 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030014929 * Return 0 if the mailbox command is submitted successfully, none 0
James Smart0c9ab6f2010-02-26 14:15:57 -050014930 * otherwise.
James Smart6fb120a2009-05-22 14:52:59 -040014931 **/
14932int
James Smart0c9ab6f2010-02-26 14:15:57 -050014933lpfc_sli4_fcf_scan_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
James Smart6fb120a2009-05-22 14:52:59 -040014934{
14935 int rc = 0, error;
14936 LPFC_MBOXQ_t *mboxq;
James Smart6fb120a2009-05-22 14:52:59 -040014937
James Smart32b97932009-07-19 10:01:21 -040014938 phba->fcoe_eventtag_at_fcf_scan = phba->fcoe_eventtag;
James Smart80c17842012-03-01 22:35:45 -050014939 phba->fcoe_cvl_eventtag_attn = phba->fcoe_cvl_eventtag;
James Smart6fb120a2009-05-22 14:52:59 -040014940 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14941 if (!mboxq) {
14942 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14943 "2000 Failed to allocate mbox for "
14944 "READ_FCF cmd\n");
James Smart4d9ab992009-10-02 15:16:39 -040014945 error = -ENOMEM;
James Smart0c9ab6f2010-02-26 14:15:57 -050014946 goto fail_fcf_scan;
James Smart6fb120a2009-05-22 14:52:59 -040014947 }
James Smartecfd03c2010-02-12 14:41:27 -050014948 /* Construct the read FCF record mailbox command */
James Smart0c9ab6f2010-02-26 14:15:57 -050014949 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
James Smartecfd03c2010-02-12 14:41:27 -050014950 if (rc) {
14951 error = -EINVAL;
James Smart0c9ab6f2010-02-26 14:15:57 -050014952 goto fail_fcf_scan;
James Smart6fb120a2009-05-22 14:52:59 -040014953 }
James Smartecfd03c2010-02-12 14:41:27 -050014954 /* Issue the mailbox command asynchronously */
James Smart6fb120a2009-05-22 14:52:59 -040014955 mboxq->vport = phba->pport;
James Smart0c9ab6f2010-02-26 14:15:57 -050014956 mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_scan_read_fcf_rec;
James Smarta93ff372010-10-22 11:06:08 -040014957
14958 spin_lock_irq(&phba->hbalock);
14959 phba->hba_flag |= FCF_TS_INPROG;
14960 spin_unlock_irq(&phba->hbalock);
14961
James Smart6fb120a2009-05-22 14:52:59 -040014962 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
James Smartecfd03c2010-02-12 14:41:27 -050014963 if (rc == MBX_NOT_FINISHED)
James Smart6fb120a2009-05-22 14:52:59 -040014964 error = -EIO;
James Smartecfd03c2010-02-12 14:41:27 -050014965 else {
James Smart38b92ef2010-08-04 16:11:39 -040014966 /* Reset eligible FCF count for new scan */
14967 if (fcf_index == LPFC_FCOE_FCF_GET_FIRST)
James Smart999d8132010-03-15 11:24:56 -040014968 phba->fcf.eligible_fcf_cnt = 0;
James Smart6fb120a2009-05-22 14:52:59 -040014969 error = 0;
James Smart32b97932009-07-19 10:01:21 -040014970 }
James Smart0c9ab6f2010-02-26 14:15:57 -050014971fail_fcf_scan:
James Smart4d9ab992009-10-02 15:16:39 -040014972 if (error) {
14973 if (mboxq)
14974 lpfc_sli4_mbox_cmd_free(phba, mboxq);
James Smarta93ff372010-10-22 11:06:08 -040014975 /* FCF scan failed, clear FCF_TS_INPROG flag */
James Smart4d9ab992009-10-02 15:16:39 -040014976 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -040014977 phba->hba_flag &= ~FCF_TS_INPROG;
James Smart4d9ab992009-10-02 15:16:39 -040014978 spin_unlock_irq(&phba->hbalock);
14979 }
James Smart6fb120a2009-05-22 14:52:59 -040014980 return error;
14981}
James Smarta0c87cb2009-07-19 10:01:10 -040014982
14983/**
James Smarta93ff372010-10-22 11:06:08 -040014984 * lpfc_sli4_fcf_rr_read_fcf_rec - Read hba fcf record for roundrobin fcf.
James Smart0c9ab6f2010-02-26 14:15:57 -050014985 * @phba: pointer to lpfc hba data structure.
14986 * @fcf_index: FCF table entry offset.
14987 *
14988 * This routine is invoked to read an FCF record indicated by @fcf_index
James Smarta93ff372010-10-22 11:06:08 -040014989 * and to use it for FLOGI roundrobin FCF failover.
James Smart0c9ab6f2010-02-26 14:15:57 -050014990 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030014991 * Return 0 if the mailbox command is submitted successfully, none 0
James Smart0c9ab6f2010-02-26 14:15:57 -050014992 * otherwise.
14993 **/
14994int
14995lpfc_sli4_fcf_rr_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
14996{
14997 int rc = 0, error;
14998 LPFC_MBOXQ_t *mboxq;
14999
15000 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15001 if (!mboxq) {
15002 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
15003 "2763 Failed to allocate mbox for "
15004 "READ_FCF cmd\n");
15005 error = -ENOMEM;
15006 goto fail_fcf_read;
15007 }
15008 /* Construct the read FCF record mailbox command */
15009 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
15010 if (rc) {
15011 error = -EINVAL;
15012 goto fail_fcf_read;
15013 }
15014 /* Issue the mailbox command asynchronously */
15015 mboxq->vport = phba->pport;
15016 mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_rr_read_fcf_rec;
15017 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
15018 if (rc == MBX_NOT_FINISHED)
15019 error = -EIO;
15020 else
15021 error = 0;
15022
15023fail_fcf_read:
15024 if (error && mboxq)
15025 lpfc_sli4_mbox_cmd_free(phba, mboxq);
15026 return error;
15027}
15028
15029/**
15030 * lpfc_sli4_read_fcf_rec - Read hba fcf record for update eligible fcf bmask.
15031 * @phba: pointer to lpfc hba data structure.
15032 * @fcf_index: FCF table entry offset.
15033 *
15034 * This routine is invoked to read an FCF record indicated by @fcf_index to
James Smarta93ff372010-10-22 11:06:08 -040015035 * determine whether it's eligible for FLOGI roundrobin failover list.
James Smart0c9ab6f2010-02-26 14:15:57 -050015036 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030015037 * Return 0 if the mailbox command is submitted successfully, none 0
James Smart0c9ab6f2010-02-26 14:15:57 -050015038 * otherwise.
15039 **/
15040int
15041lpfc_sli4_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
15042{
15043 int rc = 0, error;
15044 LPFC_MBOXQ_t *mboxq;
15045
15046 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15047 if (!mboxq) {
15048 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
15049 "2758 Failed to allocate mbox for "
15050 "READ_FCF cmd\n");
15051 error = -ENOMEM;
15052 goto fail_fcf_read;
15053 }
15054 /* Construct the read FCF record mailbox command */
15055 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
15056 if (rc) {
15057 error = -EINVAL;
15058 goto fail_fcf_read;
15059 }
15060 /* Issue the mailbox command asynchronously */
15061 mboxq->vport = phba->pport;
15062 mboxq->mbox_cmpl = lpfc_mbx_cmpl_read_fcf_rec;
15063 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
15064 if (rc == MBX_NOT_FINISHED)
15065 error = -EIO;
15066 else
15067 error = 0;
15068
15069fail_fcf_read:
15070 if (error && mboxq)
15071 lpfc_sli4_mbox_cmd_free(phba, mboxq);
15072 return error;
15073}
15074
15075/**
James Smart7d791df2011-07-22 18:37:52 -040015076 * lpfc_check_next_fcf_pri
15077 * phba pointer to the lpfc_hba struct for this port.
15078 * This routine is called from the lpfc_sli4_fcf_rr_next_index_get
15079 * routine when the rr_bmask is empty. The FCF indecies are put into the
15080 * rr_bmask based on their priority level. Starting from the highest priority
15081 * to the lowest. The most likely FCF candidate will be in the highest
15082 * priority group. When this routine is called it searches the fcf_pri list for
15083 * next lowest priority group and repopulates the rr_bmask with only those
15084 * fcf_indexes.
15085 * returns:
15086 * 1=success 0=failure
15087 **/
15088int
15089lpfc_check_next_fcf_pri_level(struct lpfc_hba *phba)
15090{
15091 uint16_t next_fcf_pri;
15092 uint16_t last_index;
15093 struct lpfc_fcf_pri *fcf_pri;
15094 int rc;
15095 int ret = 0;
15096
15097 last_index = find_first_bit(phba->fcf.fcf_rr_bmask,
15098 LPFC_SLI4_FCF_TBL_INDX_MAX);
15099 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
15100 "3060 Last IDX %d\n", last_index);
15101 if (list_empty(&phba->fcf.fcf_pri_list)) {
15102 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
15103 "3061 Last IDX %d\n", last_index);
15104 return 0; /* Empty rr list */
15105 }
15106 next_fcf_pri = 0;
15107 /*
15108 * Clear the rr_bmask and set all of the bits that are at this
15109 * priority.
15110 */
15111 memset(phba->fcf.fcf_rr_bmask, 0,
15112 sizeof(*phba->fcf.fcf_rr_bmask));
15113 spin_lock_irq(&phba->hbalock);
15114 list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
15115 if (fcf_pri->fcf_rec.flag & LPFC_FCF_FLOGI_FAILED)
15116 continue;
15117 /*
15118 * the 1st priority that has not FLOGI failed
15119 * will be the highest.
15120 */
15121 if (!next_fcf_pri)
15122 next_fcf_pri = fcf_pri->fcf_rec.priority;
15123 spin_unlock_irq(&phba->hbalock);
15124 if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
15125 rc = lpfc_sli4_fcf_rr_index_set(phba,
15126 fcf_pri->fcf_rec.fcf_index);
15127 if (rc)
15128 return 0;
15129 }
15130 spin_lock_irq(&phba->hbalock);
15131 }
15132 /*
15133 * if next_fcf_pri was not set above and the list is not empty then
15134 * we have failed flogis on all of them. So reset flogi failed
15135 * and start at the begining.
15136 */
15137 if (!next_fcf_pri && !list_empty(&phba->fcf.fcf_pri_list)) {
15138 list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
15139 fcf_pri->fcf_rec.flag &= ~LPFC_FCF_FLOGI_FAILED;
15140 /*
15141 * the 1st priority that has not FLOGI failed
15142 * will be the highest.
15143 */
15144 if (!next_fcf_pri)
15145 next_fcf_pri = fcf_pri->fcf_rec.priority;
15146 spin_unlock_irq(&phba->hbalock);
15147 if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
15148 rc = lpfc_sli4_fcf_rr_index_set(phba,
15149 fcf_pri->fcf_rec.fcf_index);
15150 if (rc)
15151 return 0;
15152 }
15153 spin_lock_irq(&phba->hbalock);
15154 }
15155 } else
15156 ret = 1;
15157 spin_unlock_irq(&phba->hbalock);
15158
15159 return ret;
15160}
15161/**
James Smart0c9ab6f2010-02-26 14:15:57 -050015162 * lpfc_sli4_fcf_rr_next_index_get - Get next eligible fcf record index
15163 * @phba: pointer to lpfc hba data structure.
15164 *
15165 * This routine is to get the next eligible FCF record index in a round
15166 * robin fashion. If the next eligible FCF record index equals to the
James Smarta93ff372010-10-22 11:06:08 -040015167 * initial roundrobin FCF record index, LPFC_FCOE_FCF_NEXT_NONE (0xFFFF)
James Smart0c9ab6f2010-02-26 14:15:57 -050015168 * shall be returned, otherwise, the next eligible FCF record's index
15169 * shall be returned.
15170 **/
15171uint16_t
15172lpfc_sli4_fcf_rr_next_index_get(struct lpfc_hba *phba)
15173{
15174 uint16_t next_fcf_index;
15175
James Smart3804dc82010-07-14 15:31:37 -040015176 /* Search start from next bit of currently registered FCF index */
James Smart7d791df2011-07-22 18:37:52 -040015177next_priority:
James Smart3804dc82010-07-14 15:31:37 -040015178 next_fcf_index = (phba->fcf.current_rec.fcf_indx + 1) %
15179 LPFC_SLI4_FCF_TBL_INDX_MAX;
James Smart0c9ab6f2010-02-26 14:15:57 -050015180 next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
15181 LPFC_SLI4_FCF_TBL_INDX_MAX,
James Smart3804dc82010-07-14 15:31:37 -040015182 next_fcf_index);
15183
James Smart0c9ab6f2010-02-26 14:15:57 -050015184 /* Wrap around condition on phba->fcf.fcf_rr_bmask */
James Smart7d791df2011-07-22 18:37:52 -040015185 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
15186 /*
15187 * If we have wrapped then we need to clear the bits that
15188 * have been tested so that we can detect when we should
15189 * change the priority level.
15190 */
James Smart0c9ab6f2010-02-26 14:15:57 -050015191 next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
15192 LPFC_SLI4_FCF_TBL_INDX_MAX, 0);
James Smart7d791df2011-07-22 18:37:52 -040015193 }
15194
James Smart0c9ab6f2010-02-26 14:15:57 -050015195
James Smart3804dc82010-07-14 15:31:37 -040015196 /* Check roundrobin failover list empty condition */
James Smart7d791df2011-07-22 18:37:52 -040015197 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX ||
15198 next_fcf_index == phba->fcf.current_rec.fcf_indx) {
15199 /*
15200 * If next fcf index is not found check if there are lower
15201 * Priority level fcf's in the fcf_priority list.
15202 * Set up the rr_bmask with all of the avaiable fcf bits
15203 * at that level and continue the selection process.
15204 */
15205 if (lpfc_check_next_fcf_pri_level(phba))
15206 goto next_priority;
James Smart3804dc82010-07-14 15:31:37 -040015207 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
15208 "2844 No roundrobin failover FCF available\n");
James Smart7d791df2011-07-22 18:37:52 -040015209 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX)
15210 return LPFC_FCOE_FCF_NEXT_NONE;
15211 else {
15212 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
15213 "3063 Only FCF available idx %d, flag %x\n",
15214 next_fcf_index,
15215 phba->fcf.fcf_pri[next_fcf_index].fcf_rec.flag);
15216 return next_fcf_index;
15217 }
James Smart3804dc82010-07-14 15:31:37 -040015218 }
15219
James Smart7d791df2011-07-22 18:37:52 -040015220 if (next_fcf_index < LPFC_SLI4_FCF_TBL_INDX_MAX &&
15221 phba->fcf.fcf_pri[next_fcf_index].fcf_rec.flag &
15222 LPFC_FCF_FLOGI_FAILED)
15223 goto next_priority;
15224
James Smart3804dc82010-07-14 15:31:37 -040015225 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040015226 "2845 Get next roundrobin failover FCF (x%x)\n",
15227 next_fcf_index);
15228
James Smart0c9ab6f2010-02-26 14:15:57 -050015229 return next_fcf_index;
15230}
15231
15232/**
15233 * lpfc_sli4_fcf_rr_index_set - Set bmask with eligible fcf record index
15234 * @phba: pointer to lpfc hba data structure.
15235 *
15236 * This routine sets the FCF record index in to the eligible bmask for
James Smarta93ff372010-10-22 11:06:08 -040015237 * roundrobin failover search. It checks to make sure that the index
James Smart0c9ab6f2010-02-26 14:15:57 -050015238 * does not go beyond the range of the driver allocated bmask dimension
15239 * before setting the bit.
15240 *
15241 * Returns 0 if the index bit successfully set, otherwise, it returns
15242 * -EINVAL.
15243 **/
15244int
15245lpfc_sli4_fcf_rr_index_set(struct lpfc_hba *phba, uint16_t fcf_index)
15246{
15247 if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
15248 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040015249 "2610 FCF (x%x) reached driver's book "
15250 "keeping dimension:x%x\n",
James Smart0c9ab6f2010-02-26 14:15:57 -050015251 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
15252 return -EINVAL;
15253 }
15254 /* Set the eligible FCF record index bmask */
15255 set_bit(fcf_index, phba->fcf.fcf_rr_bmask);
15256
James Smart3804dc82010-07-14 15:31:37 -040015257 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040015258 "2790 Set FCF (x%x) to roundrobin FCF failover "
James Smart3804dc82010-07-14 15:31:37 -040015259 "bmask\n", fcf_index);
15260
James Smart0c9ab6f2010-02-26 14:15:57 -050015261 return 0;
15262}
15263
15264/**
James Smart3804dc82010-07-14 15:31:37 -040015265 * lpfc_sli4_fcf_rr_index_clear - Clear bmask from eligible fcf record index
James Smart0c9ab6f2010-02-26 14:15:57 -050015266 * @phba: pointer to lpfc hba data structure.
15267 *
15268 * This routine clears the FCF record index from the eligible bmask for
James Smarta93ff372010-10-22 11:06:08 -040015269 * roundrobin failover search. It checks to make sure that the index
James Smart0c9ab6f2010-02-26 14:15:57 -050015270 * does not go beyond the range of the driver allocated bmask dimension
15271 * before clearing the bit.
15272 **/
15273void
15274lpfc_sli4_fcf_rr_index_clear(struct lpfc_hba *phba, uint16_t fcf_index)
15275{
James Smart7d791df2011-07-22 18:37:52 -040015276 struct lpfc_fcf_pri *fcf_pri;
James Smart0c9ab6f2010-02-26 14:15:57 -050015277 if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
15278 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040015279 "2762 FCF (x%x) reached driver's book "
15280 "keeping dimension:x%x\n",
James Smart0c9ab6f2010-02-26 14:15:57 -050015281 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
15282 return;
15283 }
15284 /* Clear the eligible FCF record index bmask */
James Smart7d791df2011-07-22 18:37:52 -040015285 spin_lock_irq(&phba->hbalock);
15286 list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
15287 if (fcf_pri->fcf_rec.fcf_index == fcf_index) {
15288 list_del_init(&fcf_pri->list);
15289 break;
15290 }
15291 }
15292 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -050015293 clear_bit(fcf_index, phba->fcf.fcf_rr_bmask);
James Smart3804dc82010-07-14 15:31:37 -040015294
15295 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040015296 "2791 Clear FCF (x%x) from roundrobin failover "
James Smart3804dc82010-07-14 15:31:37 -040015297 "bmask\n", fcf_index);
James Smart0c9ab6f2010-02-26 14:15:57 -050015298}
15299
15300/**
James Smartecfd03c2010-02-12 14:41:27 -050015301 * lpfc_mbx_cmpl_redisc_fcf_table - completion routine for rediscover FCF table
15302 * @phba: pointer to lpfc hba data structure.
15303 *
15304 * This routine is the completion routine for the rediscover FCF table mailbox
15305 * command. If the mailbox command returned failure, it will try to stop the
15306 * FCF rediscover wait timer.
15307 **/
15308void
15309lpfc_mbx_cmpl_redisc_fcf_table(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
15310{
15311 struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
15312 uint32_t shdr_status, shdr_add_status;
15313
15314 redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
15315
15316 shdr_status = bf_get(lpfc_mbox_hdr_status,
15317 &redisc_fcf->header.cfg_shdr.response);
15318 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
15319 &redisc_fcf->header.cfg_shdr.response);
15320 if (shdr_status || shdr_add_status) {
James Smart0c9ab6f2010-02-26 14:15:57 -050015321 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smartecfd03c2010-02-12 14:41:27 -050015322 "2746 Requesting for FCF rediscovery failed "
15323 "status x%x add_status x%x\n",
15324 shdr_status, shdr_add_status);
James Smart0c9ab6f2010-02-26 14:15:57 -050015325 if (phba->fcf.fcf_flag & FCF_ACVL_DISC) {
James Smartfc2b9892010-02-26 14:15:29 -050015326 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -050015327 phba->fcf.fcf_flag &= ~FCF_ACVL_DISC;
James Smartfc2b9892010-02-26 14:15:29 -050015328 spin_unlock_irq(&phba->hbalock);
15329 /*
15330 * CVL event triggered FCF rediscover request failed,
15331 * last resort to re-try current registered FCF entry.
15332 */
15333 lpfc_retry_pport_discovery(phba);
15334 } else {
15335 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -050015336 phba->fcf.fcf_flag &= ~FCF_DEAD_DISC;
James Smartfc2b9892010-02-26 14:15:29 -050015337 spin_unlock_irq(&phba->hbalock);
15338 /*
15339 * DEAD FCF event triggered FCF rediscover request
15340 * failed, last resort to fail over as a link down
15341 * to FCF registration.
15342 */
15343 lpfc_sli4_fcf_dead_failthrough(phba);
15344 }
James Smart0c9ab6f2010-02-26 14:15:57 -050015345 } else {
15346 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040015347 "2775 Start FCF rediscover quiescent timer\n");
James Smartecfd03c2010-02-12 14:41:27 -050015348 /*
15349 * Start FCF rediscovery wait timer for pending FCF
15350 * before rescan FCF record table.
15351 */
15352 lpfc_fcf_redisc_wait_start_timer(phba);
James Smart0c9ab6f2010-02-26 14:15:57 -050015353 }
James Smartecfd03c2010-02-12 14:41:27 -050015354
15355 mempool_free(mbox, phba->mbox_mem_pool);
15356}
15357
15358/**
James Smart3804dc82010-07-14 15:31:37 -040015359 * lpfc_sli4_redisc_fcf_table - Request to rediscover entire FCF table by port.
James Smartecfd03c2010-02-12 14:41:27 -050015360 * @phba: pointer to lpfc hba data structure.
15361 *
15362 * This routine is invoked to request for rediscovery of the entire FCF table
15363 * by the port.
15364 **/
15365int
15366lpfc_sli4_redisc_fcf_table(struct lpfc_hba *phba)
15367{
15368 LPFC_MBOXQ_t *mbox;
15369 struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
15370 int rc, length;
15371
James Smart0c9ab6f2010-02-26 14:15:57 -050015372 /* Cancel retry delay timers to all vports before FCF rediscover */
15373 lpfc_cancel_all_vport_retry_delay_timer(phba);
15374
James Smartecfd03c2010-02-12 14:41:27 -050015375 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15376 if (!mbox) {
15377 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15378 "2745 Failed to allocate mbox for "
15379 "requesting FCF rediscover.\n");
15380 return -ENOMEM;
15381 }
15382
15383 length = (sizeof(struct lpfc_mbx_redisc_fcf_tbl) -
15384 sizeof(struct lpfc_sli4_cfg_mhdr));
15385 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15386 LPFC_MBOX_OPCODE_FCOE_REDISCOVER_FCF,
15387 length, LPFC_SLI4_MBX_EMBED);
15388
15389 redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
15390 /* Set count to 0 for invalidating the entire FCF database */
15391 bf_set(lpfc_mbx_redisc_fcf_count, redisc_fcf, 0);
15392
15393 /* Issue the mailbox command asynchronously */
15394 mbox->vport = phba->pport;
15395 mbox->mbox_cmpl = lpfc_mbx_cmpl_redisc_fcf_table;
15396 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
15397
15398 if (rc == MBX_NOT_FINISHED) {
15399 mempool_free(mbox, phba->mbox_mem_pool);
15400 return -EIO;
15401 }
15402 return 0;
15403}
15404
15405/**
James Smartfc2b9892010-02-26 14:15:29 -050015406 * lpfc_sli4_fcf_dead_failthrough - Failthrough routine to fcf dead event
15407 * @phba: pointer to lpfc hba data structure.
15408 *
15409 * This function is the failover routine as a last resort to the FCF DEAD
15410 * event when driver failed to perform fast FCF failover.
15411 **/
15412void
15413lpfc_sli4_fcf_dead_failthrough(struct lpfc_hba *phba)
15414{
15415 uint32_t link_state;
15416
15417 /*
15418 * Last resort as FCF DEAD event failover will treat this as
15419 * a link down, but save the link state because we don't want
15420 * it to be changed to Link Down unless it is already down.
15421 */
15422 link_state = phba->link_state;
15423 lpfc_linkdown(phba);
15424 phba->link_state = link_state;
15425
15426 /* Unregister FCF if no devices connected to it */
15427 lpfc_unregister_unused_fcf(phba);
15428}
15429
15430/**
James Smart026abb82011-12-13 13:20:45 -050015431 * lpfc_sli_get_config_region23 - Get sli3 port region 23 data.
James Smarta0c87cb2009-07-19 10:01:10 -040015432 * @phba: pointer to lpfc hba data structure.
James Smart026abb82011-12-13 13:20:45 -050015433 * @rgn23_data: pointer to configure region 23 data.
James Smarta0c87cb2009-07-19 10:01:10 -040015434 *
James Smart026abb82011-12-13 13:20:45 -050015435 * This function gets SLI3 port configure region 23 data through memory dump
15436 * mailbox command. When it successfully retrieves data, the size of the data
15437 * will be returned, otherwise, 0 will be returned.
James Smarta0c87cb2009-07-19 10:01:10 -040015438 **/
James Smart026abb82011-12-13 13:20:45 -050015439static uint32_t
15440lpfc_sli_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)
James Smarta0c87cb2009-07-19 10:01:10 -040015441{
15442 LPFC_MBOXQ_t *pmb = NULL;
15443 MAILBOX_t *mb;
James Smart026abb82011-12-13 13:20:45 -050015444 uint32_t offset = 0;
James Smarta0c87cb2009-07-19 10:01:10 -040015445 int rc;
15446
James Smart026abb82011-12-13 13:20:45 -050015447 if (!rgn23_data)
15448 return 0;
15449
James Smarta0c87cb2009-07-19 10:01:10 -040015450 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15451 if (!pmb) {
15452 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart026abb82011-12-13 13:20:45 -050015453 "2600 failed to allocate mailbox memory\n");
15454 return 0;
James Smarta0c87cb2009-07-19 10:01:10 -040015455 }
15456 mb = &pmb->u.mb;
15457
James Smarta0c87cb2009-07-19 10:01:10 -040015458 do {
15459 lpfc_dump_mem(phba, pmb, offset, DMP_REGION_23);
15460 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
15461
15462 if (rc != MBX_SUCCESS) {
15463 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smart026abb82011-12-13 13:20:45 -050015464 "2601 failed to read config "
15465 "region 23, rc 0x%x Status 0x%x\n",
15466 rc, mb->mbxStatus);
James Smarta0c87cb2009-07-19 10:01:10 -040015467 mb->un.varDmp.word_cnt = 0;
15468 }
15469 /*
15470 * dump mem may return a zero when finished or we got a
15471 * mailbox error, either way we are done.
15472 */
15473 if (mb->un.varDmp.word_cnt == 0)
15474 break;
15475 if (mb->un.varDmp.word_cnt > DMP_RGN23_SIZE - offset)
15476 mb->un.varDmp.word_cnt = DMP_RGN23_SIZE - offset;
15477
15478 lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
James Smart026abb82011-12-13 13:20:45 -050015479 rgn23_data + offset,
15480 mb->un.varDmp.word_cnt);
James Smarta0c87cb2009-07-19 10:01:10 -040015481 offset += mb->un.varDmp.word_cnt;
15482 } while (mb->un.varDmp.word_cnt && offset < DMP_RGN23_SIZE);
15483
James Smart026abb82011-12-13 13:20:45 -050015484 mempool_free(pmb, phba->mbox_mem_pool);
15485 return offset;
15486}
15487
15488/**
15489 * lpfc_sli4_get_config_region23 - Get sli4 port region 23 data.
15490 * @phba: pointer to lpfc hba data structure.
15491 * @rgn23_data: pointer to configure region 23 data.
15492 *
15493 * This function gets SLI4 port configure region 23 data through memory dump
15494 * mailbox command. When it successfully retrieves data, the size of the data
15495 * will be returned, otherwise, 0 will be returned.
15496 **/
15497static uint32_t
15498lpfc_sli4_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)
15499{
15500 LPFC_MBOXQ_t *mboxq = NULL;
15501 struct lpfc_dmabuf *mp = NULL;
15502 struct lpfc_mqe *mqe;
15503 uint32_t data_length = 0;
15504 int rc;
15505
15506 if (!rgn23_data)
15507 return 0;
15508
15509 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15510 if (!mboxq) {
15511 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15512 "3105 failed to allocate mailbox memory\n");
15513 return 0;
15514 }
15515
15516 if (lpfc_sli4_dump_cfg_rg23(phba, mboxq))
15517 goto out;
15518 mqe = &mboxq->u.mqe;
15519 mp = (struct lpfc_dmabuf *) mboxq->context1;
15520 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
15521 if (rc)
15522 goto out;
15523 data_length = mqe->un.mb_words[5];
15524 if (data_length == 0)
15525 goto out;
15526 if (data_length > DMP_RGN23_SIZE) {
15527 data_length = 0;
15528 goto out;
15529 }
15530 lpfc_sli_pcimem_bcopy((char *)mp->virt, rgn23_data, data_length);
15531out:
15532 mempool_free(mboxq, phba->mbox_mem_pool);
15533 if (mp) {
15534 lpfc_mbuf_free(phba, mp->virt, mp->phys);
15535 kfree(mp);
15536 }
15537 return data_length;
15538}
15539
15540/**
15541 * lpfc_sli_read_link_ste - Read region 23 to decide if link is disabled.
15542 * @phba: pointer to lpfc hba data structure.
15543 *
15544 * This function read region 23 and parse TLV for port status to
15545 * decide if the user disaled the port. If the TLV indicates the
15546 * port is disabled, the hba_flag is set accordingly.
15547 **/
15548void
15549lpfc_sli_read_link_ste(struct lpfc_hba *phba)
15550{
15551 uint8_t *rgn23_data = NULL;
15552 uint32_t if_type, data_size, sub_tlv_len, tlv_offset;
15553 uint32_t offset = 0;
15554
15555 /* Get adapter Region 23 data */
15556 rgn23_data = kzalloc(DMP_RGN23_SIZE, GFP_KERNEL);
15557 if (!rgn23_data)
15558 goto out;
15559
15560 if (phba->sli_rev < LPFC_SLI_REV4)
15561 data_size = lpfc_sli_get_config_region23(phba, rgn23_data);
15562 else {
15563 if_type = bf_get(lpfc_sli_intf_if_type,
15564 &phba->sli4_hba.sli_intf);
15565 if (if_type == LPFC_SLI_INTF_IF_TYPE_0)
15566 goto out;
15567 data_size = lpfc_sli4_get_config_region23(phba, rgn23_data);
15568 }
James Smarta0c87cb2009-07-19 10:01:10 -040015569
15570 if (!data_size)
15571 goto out;
15572
15573 /* Check the region signature first */
15574 if (memcmp(&rgn23_data[offset], LPFC_REGION23_SIGNATURE, 4)) {
15575 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15576 "2619 Config region 23 has bad signature\n");
15577 goto out;
15578 }
15579 offset += 4;
15580
15581 /* Check the data structure version */
15582 if (rgn23_data[offset] != LPFC_REGION23_VERSION) {
15583 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15584 "2620 Config region 23 has bad version\n");
15585 goto out;
15586 }
15587 offset += 4;
15588
15589 /* Parse TLV entries in the region */
15590 while (offset < data_size) {
15591 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC)
15592 break;
15593 /*
15594 * If the TLV is not driver specific TLV or driver id is
15595 * not linux driver id, skip the record.
15596 */
15597 if ((rgn23_data[offset] != DRIVER_SPECIFIC_TYPE) ||
15598 (rgn23_data[offset + 2] != LINUX_DRIVER_ID) ||
15599 (rgn23_data[offset + 3] != 0)) {
15600 offset += rgn23_data[offset + 1] * 4 + 4;
15601 continue;
15602 }
15603
15604 /* Driver found a driver specific TLV in the config region */
15605 sub_tlv_len = rgn23_data[offset + 1] * 4;
15606 offset += 4;
15607 tlv_offset = 0;
15608
15609 /*
15610 * Search for configured port state sub-TLV.
15611 */
15612 while ((offset < data_size) &&
15613 (tlv_offset < sub_tlv_len)) {
15614 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC) {
15615 offset += 4;
15616 tlv_offset += 4;
15617 break;
15618 }
15619 if (rgn23_data[offset] != PORT_STE_TYPE) {
15620 offset += rgn23_data[offset + 1] * 4 + 4;
15621 tlv_offset += rgn23_data[offset + 1] * 4 + 4;
15622 continue;
15623 }
15624
15625 /* This HBA contains PORT_STE configured */
15626 if (!rgn23_data[offset + 2])
15627 phba->hba_flag |= LINK_DISABLED;
15628
15629 goto out;
15630 }
15631 }
James Smart026abb82011-12-13 13:20:45 -050015632
James Smarta0c87cb2009-07-19 10:01:10 -040015633out:
James Smarta0c87cb2009-07-19 10:01:10 -040015634 kfree(rgn23_data);
15635 return;
15636}
James Smart695a8142010-01-26 23:08:03 -050015637
15638/**
James Smart52d52442011-05-24 11:42:45 -040015639 * lpfc_wr_object - write an object to the firmware
15640 * @phba: HBA structure that indicates port to create a queue on.
15641 * @dmabuf_list: list of dmabufs to write to the port.
15642 * @size: the total byte value of the objects to write to the port.
15643 * @offset: the current offset to be used to start the transfer.
15644 *
15645 * This routine will create a wr_object mailbox command to send to the port.
15646 * the mailbox command will be constructed using the dma buffers described in
15647 * @dmabuf_list to create a list of BDEs. This routine will fill in as many
15648 * BDEs that the imbedded mailbox can support. The @offset variable will be
15649 * used to indicate the starting offset of the transfer and will also return
15650 * the offset after the write object mailbox has completed. @size is used to
15651 * determine the end of the object and whether the eof bit should be set.
15652 *
15653 * Return 0 is successful and offset will contain the the new offset to use
15654 * for the next write.
15655 * Return negative value for error cases.
15656 **/
15657int
15658lpfc_wr_object(struct lpfc_hba *phba, struct list_head *dmabuf_list,
15659 uint32_t size, uint32_t *offset)
15660{
15661 struct lpfc_mbx_wr_object *wr_object;
15662 LPFC_MBOXQ_t *mbox;
15663 int rc = 0, i = 0;
15664 uint32_t shdr_status, shdr_add_status;
15665 uint32_t mbox_tmo;
15666 union lpfc_sli4_cfg_shdr *shdr;
15667 struct lpfc_dmabuf *dmabuf;
15668 uint32_t written = 0;
15669
15670 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15671 if (!mbox)
15672 return -ENOMEM;
15673
15674 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
15675 LPFC_MBOX_OPCODE_WRITE_OBJECT,
15676 sizeof(struct lpfc_mbx_wr_object) -
15677 sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
15678
15679 wr_object = (struct lpfc_mbx_wr_object *)&mbox->u.mqe.un.wr_object;
15680 wr_object->u.request.write_offset = *offset;
15681 sprintf((uint8_t *)wr_object->u.request.object_name, "/");
15682 wr_object->u.request.object_name[0] =
15683 cpu_to_le32(wr_object->u.request.object_name[0]);
15684 bf_set(lpfc_wr_object_eof, &wr_object->u.request, 0);
15685 list_for_each_entry(dmabuf, dmabuf_list, list) {
15686 if (i >= LPFC_MBX_WR_CONFIG_MAX_BDE || written >= size)
15687 break;
15688 wr_object->u.request.bde[i].addrLow = putPaddrLow(dmabuf->phys);
15689 wr_object->u.request.bde[i].addrHigh =
15690 putPaddrHigh(dmabuf->phys);
15691 if (written + SLI4_PAGE_SIZE >= size) {
15692 wr_object->u.request.bde[i].tus.f.bdeSize =
15693 (size - written);
15694 written += (size - written);
15695 bf_set(lpfc_wr_object_eof, &wr_object->u.request, 1);
15696 } else {
15697 wr_object->u.request.bde[i].tus.f.bdeSize =
15698 SLI4_PAGE_SIZE;
15699 written += SLI4_PAGE_SIZE;
15700 }
15701 i++;
15702 }
15703 wr_object->u.request.bde_count = i;
15704 bf_set(lpfc_wr_object_write_length, &wr_object->u.request, written);
15705 if (!phba->sli4_hba.intr_enable)
15706 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15707 else {
James Smarta183a152011-10-10 21:32:43 -040015708 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart52d52442011-05-24 11:42:45 -040015709 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
15710 }
15711 /* The IOCTL status is embedded in the mailbox subheader. */
15712 shdr = (union lpfc_sli4_cfg_shdr *) &wr_object->header.cfg_shdr;
15713 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15714 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15715 if (rc != MBX_TIMEOUT)
15716 mempool_free(mbox, phba->mbox_mem_pool);
15717 if (shdr_status || shdr_add_status || rc) {
15718 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15719 "3025 Write Object mailbox failed with "
15720 "status x%x add_status x%x, mbx status x%x\n",
15721 shdr_status, shdr_add_status, rc);
15722 rc = -ENXIO;
15723 } else
15724 *offset += wr_object->u.response.actual_write_length;
15725 return rc;
15726}
15727
15728/**
James Smart695a8142010-01-26 23:08:03 -050015729 * lpfc_cleanup_pending_mbox - Free up vport discovery mailbox commands.
15730 * @vport: pointer to vport data structure.
15731 *
15732 * This function iterate through the mailboxq and clean up all REG_LOGIN
15733 * and REG_VPI mailbox commands associated with the vport. This function
15734 * is called when driver want to restart discovery of the vport due to
15735 * a Clear Virtual Link event.
15736 **/
15737void
15738lpfc_cleanup_pending_mbox(struct lpfc_vport *vport)
15739{
15740 struct lpfc_hba *phba = vport->phba;
15741 LPFC_MBOXQ_t *mb, *nextmb;
15742 struct lpfc_dmabuf *mp;
James Smart78730cf2010-04-06 15:06:30 -040015743 struct lpfc_nodelist *ndlp;
James Smartd439d282010-09-29 11:18:45 -040015744 struct lpfc_nodelist *act_mbx_ndlp = NULL;
James Smart589a52d2010-07-14 15:30:54 -040015745 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smartd439d282010-09-29 11:18:45 -040015746 LIST_HEAD(mbox_cmd_list);
James Smart63e801c2010-11-20 23:14:19 -050015747 uint8_t restart_loop;
James Smart695a8142010-01-26 23:08:03 -050015748
James Smartd439d282010-09-29 11:18:45 -040015749 /* Clean up internally queued mailbox commands with the vport */
James Smart695a8142010-01-26 23:08:03 -050015750 spin_lock_irq(&phba->hbalock);
15751 list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
15752 if (mb->vport != vport)
15753 continue;
15754
15755 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
15756 (mb->u.mb.mbxCommand != MBX_REG_VPI))
15757 continue;
15758
James Smartd439d282010-09-29 11:18:45 -040015759 list_del(&mb->list);
15760 list_add_tail(&mb->list, &mbox_cmd_list);
15761 }
15762 /* Clean up active mailbox command with the vport */
15763 mb = phba->sli.mbox_active;
15764 if (mb && (mb->vport == vport)) {
15765 if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) ||
15766 (mb->u.mb.mbxCommand == MBX_REG_VPI))
15767 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
15768 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
15769 act_mbx_ndlp = (struct lpfc_nodelist *)mb->context2;
15770 /* Put reference count for delayed processing */
15771 act_mbx_ndlp = lpfc_nlp_get(act_mbx_ndlp);
15772 /* Unregister the RPI when mailbox complete */
15773 mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
15774 }
15775 }
James Smart63e801c2010-11-20 23:14:19 -050015776 /* Cleanup any mailbox completions which are not yet processed */
15777 do {
15778 restart_loop = 0;
15779 list_for_each_entry(mb, &phba->sli.mboxq_cmpl, list) {
15780 /*
15781 * If this mailox is already processed or it is
15782 * for another vport ignore it.
15783 */
15784 if ((mb->vport != vport) ||
15785 (mb->mbox_flag & LPFC_MBX_IMED_UNREG))
15786 continue;
15787
15788 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
15789 (mb->u.mb.mbxCommand != MBX_REG_VPI))
15790 continue;
15791
15792 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
15793 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
15794 ndlp = (struct lpfc_nodelist *)mb->context2;
15795 /* Unregister the RPI when mailbox complete */
15796 mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
15797 restart_loop = 1;
15798 spin_unlock_irq(&phba->hbalock);
15799 spin_lock(shost->host_lock);
15800 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
15801 spin_unlock(shost->host_lock);
15802 spin_lock_irq(&phba->hbalock);
15803 break;
15804 }
15805 }
15806 } while (restart_loop);
15807
James Smartd439d282010-09-29 11:18:45 -040015808 spin_unlock_irq(&phba->hbalock);
15809
15810 /* Release the cleaned-up mailbox commands */
15811 while (!list_empty(&mbox_cmd_list)) {
15812 list_remove_head(&mbox_cmd_list, mb, LPFC_MBOXQ_t, list);
James Smart695a8142010-01-26 23:08:03 -050015813 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
15814 mp = (struct lpfc_dmabuf *) (mb->context1);
15815 if (mp) {
15816 __lpfc_mbuf_free(phba, mp->virt, mp->phys);
15817 kfree(mp);
15818 }
James Smart78730cf2010-04-06 15:06:30 -040015819 ndlp = (struct lpfc_nodelist *) mb->context2;
James Smartd439d282010-09-29 11:18:45 -040015820 mb->context2 = NULL;
James Smart78730cf2010-04-06 15:06:30 -040015821 if (ndlp) {
Dan Carpenterec21b3b2010-08-08 00:15:17 +020015822 spin_lock(shost->host_lock);
James Smart589a52d2010-07-14 15:30:54 -040015823 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
Dan Carpenterec21b3b2010-08-08 00:15:17 +020015824 spin_unlock(shost->host_lock);
James Smart78730cf2010-04-06 15:06:30 -040015825 lpfc_nlp_put(ndlp);
James Smart78730cf2010-04-06 15:06:30 -040015826 }
James Smart695a8142010-01-26 23:08:03 -050015827 }
James Smart695a8142010-01-26 23:08:03 -050015828 mempool_free(mb, phba->mbox_mem_pool);
15829 }
James Smartd439d282010-09-29 11:18:45 -040015830
15831 /* Release the ndlp with the cleaned-up active mailbox command */
15832 if (act_mbx_ndlp) {
15833 spin_lock(shost->host_lock);
15834 act_mbx_ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
15835 spin_unlock(shost->host_lock);
15836 lpfc_nlp_put(act_mbx_ndlp);
James Smart695a8142010-01-26 23:08:03 -050015837 }
James Smart695a8142010-01-26 23:08:03 -050015838}
15839
James Smart2a9bf3d2010-06-07 15:24:45 -040015840/**
15841 * lpfc_drain_txq - Drain the txq
15842 * @phba: Pointer to HBA context object.
15843 *
15844 * This function attempt to submit IOCBs on the txq
15845 * to the adapter. For SLI4 adapters, the txq contains
15846 * ELS IOCBs that have been deferred because the there
15847 * are no SGLs. This congestion can occur with large
15848 * vport counts during node discovery.
15849 **/
15850
15851uint32_t
15852lpfc_drain_txq(struct lpfc_hba *phba)
15853{
15854 LIST_HEAD(completions);
15855 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
15856 struct lpfc_iocbq *piocbq = 0;
15857 unsigned long iflags = 0;
15858 char *fail_msg = NULL;
15859 struct lpfc_sglq *sglq;
15860 union lpfc_wqe wqe;
15861
15862 spin_lock_irqsave(&phba->hbalock, iflags);
15863 if (pring->txq_cnt > pring->txq_max)
15864 pring->txq_max = pring->txq_cnt;
15865
15866 spin_unlock_irqrestore(&phba->hbalock, iflags);
15867
15868 while (pring->txq_cnt) {
15869 spin_lock_irqsave(&phba->hbalock, iflags);
15870
James Smart19ca7602010-11-20 23:11:55 -050015871 piocbq = lpfc_sli_ringtx_get(phba, pring);
James Smarta6298522012-06-12 13:54:11 -040015872 if (!piocbq) {
15873 spin_unlock_irqrestore(&phba->hbalock, iflags);
15874 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15875 "2823 txq empty and txq_cnt is %d\n ",
15876 pring->txq_cnt);
15877 break;
15878 }
James Smart19ca7602010-11-20 23:11:55 -050015879 sglq = __lpfc_sli_get_sglq(phba, piocbq);
James Smart2a9bf3d2010-06-07 15:24:45 -040015880 if (!sglq) {
James Smart19ca7602010-11-20 23:11:55 -050015881 __lpfc_sli_ringtx_put(phba, pring, piocbq);
James Smart2a9bf3d2010-06-07 15:24:45 -040015882 spin_unlock_irqrestore(&phba->hbalock, iflags);
15883 break;
James Smart2a9bf3d2010-06-07 15:24:45 -040015884 }
15885
15886 /* The xri and iocb resources secured,
15887 * attempt to issue request
15888 */
James Smart6d368e52011-05-24 11:44:12 -040015889 piocbq->sli4_lxritag = sglq->sli4_lxritag;
James Smart2a9bf3d2010-06-07 15:24:45 -040015890 piocbq->sli4_xritag = sglq->sli4_xritag;
15891 if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocbq, sglq))
15892 fail_msg = "to convert bpl to sgl";
15893 else if (lpfc_sli4_iocb2wqe(phba, piocbq, &wqe))
15894 fail_msg = "to convert iocb to wqe";
15895 else if (lpfc_sli4_wq_put(phba->sli4_hba.els_wq, &wqe))
15896 fail_msg = " - Wq is full";
15897 else
15898 lpfc_sli_ringtxcmpl_put(phba, pring, piocbq);
15899
15900 if (fail_msg) {
15901 /* Failed means we can't issue and need to cancel */
15902 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15903 "2822 IOCB failed %s iotag 0x%x "
15904 "xri 0x%x\n",
15905 fail_msg,
15906 piocbq->iotag, piocbq->sli4_xritag);
15907 list_add_tail(&piocbq->list, &completions);
15908 }
15909 spin_unlock_irqrestore(&phba->hbalock, iflags);
15910 }
15911
James Smart2a9bf3d2010-06-07 15:24:45 -040015912 /* Cancel all the IOCBs that cannot be issued */
15913 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
15914 IOERR_SLI_ABORTED);
15915
15916 return pring->txq_cnt;
15917}