blob: 332c6b716f81e396d86acec23a4133d3a21edd24 [file] [log] [blame]
dea31012005-04-17 16:05:31 -05001/*******************************************************************
2 * This file is part of the Emulex Linux Device Driver for *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04003 * Fibre Channel Host Bus Adapters. *
James Smart792581d2011-03-11 16:06:44 -05004 * Copyright (C) 2004-2011 Emulex. All rights reserved. *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04005 * EMULEX and SLI are trademarks of Emulex. *
dea31012005-04-17 16:05:31 -05006 * www.emulex.com *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04007 * Portions Copyright (C) 2004-2005 Christoph Hellwig *
dea31012005-04-17 16:05:31 -05008 * *
9 * This program is free software; you can redistribute it and/or *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -040010 * modify it under the terms of version 2 of the GNU General *
11 * Public License as published by the Free Software Foundation. *
12 * This program is distributed in the hope that it will be useful. *
13 * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND *
14 * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, *
15 * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE *
16 * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
17 * TO BE LEGALLY INVALID. See the GNU General Public License for *
18 * more details, a copy of which can be found in the file COPYING *
19 * included with this package. *
dea31012005-04-17 16:05:31 -050020 *******************************************************************/
21
dea31012005-04-17 16:05:31 -050022#include <linux/blkdev.h>
23#include <linux/pci.h>
24#include <linux/interrupt.h>
25#include <linux/delay.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090026#include <linux/slab.h>
dea31012005-04-17 16:05:31 -050027
James.Smart@Emulex.Com91886522005-08-10 15:03:09 -040028#include <scsi/scsi.h>
dea31012005-04-17 16:05:31 -050029#include <scsi/scsi_cmnd.h>
30#include <scsi/scsi_device.h>
31#include <scsi/scsi_host.h>
James.Smart@Emulex.Comf888ba32005-08-10 15:03:01 -040032#include <scsi/scsi_transport_fc.h>
James Smartda0436e2009-05-22 14:51:39 -040033#include <scsi/fc/fc_fs.h>
James Smart0d878412009-10-02 15:16:56 -040034#include <linux/aer.h>
dea31012005-04-17 16:05:31 -050035
James Smartda0436e2009-05-22 14:51:39 -040036#include "lpfc_hw4.h"
dea31012005-04-17 16:05:31 -050037#include "lpfc_hw.h"
38#include "lpfc_sli.h"
James Smartda0436e2009-05-22 14:51:39 -040039#include "lpfc_sli4.h"
James Smartea2151b2008-09-07 11:52:10 -040040#include "lpfc_nl.h"
dea31012005-04-17 16:05:31 -050041#include "lpfc_disc.h"
42#include "lpfc_scsi.h"
43#include "lpfc.h"
44#include "lpfc_crtn.h"
45#include "lpfc_logmsg.h"
46#include "lpfc_compat.h"
James Smart858c9f62007-06-17 19:56:39 -050047#include "lpfc_debugfs.h"
James Smart04c68492009-05-22 14:52:52 -040048#include "lpfc_vport.h"
dea31012005-04-17 16:05:31 -050049
50/* There are only four IOCB completion types. */
51typedef enum _lpfc_iocb_type {
52 LPFC_UNKNOWN_IOCB,
53 LPFC_UNSOL_IOCB,
54 LPFC_SOL_IOCB,
55 LPFC_ABORT_IOCB
56} lpfc_iocb_type;
57
James Smart4f774512009-05-22 14:52:35 -040058
59/* Provide function prototypes local to this module. */
60static int lpfc_sli_issue_mbox_s4(struct lpfc_hba *, LPFC_MBOXQ_t *,
61 uint32_t);
62static int lpfc_sli4_read_rev(struct lpfc_hba *, LPFC_MBOXQ_t *,
James Smart45ed1192009-10-02 15:17:02 -040063 uint8_t *, uint32_t *);
64static struct lpfc_iocbq *lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *,
65 struct lpfc_iocbq *);
James Smart6669f9b2009-10-02 15:16:45 -040066static void lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *,
67 struct hbq_dmabuf *);
James Smart05580562011-05-24 11:40:48 -040068static int lpfc_sli4_fp_handle_wcqe(struct lpfc_hba *, struct lpfc_queue *,
69 struct lpfc_cqe *);
70
James Smart4f774512009-05-22 14:52:35 -040071static IOCB_t *
72lpfc_get_iocb_from_iocbq(struct lpfc_iocbq *iocbq)
73{
74 return &iocbq->iocb;
75}
76
77/**
78 * lpfc_sli4_wq_put - Put a Work Queue Entry on an Work Queue
79 * @q: The Work Queue to operate on.
80 * @wqe: The work Queue Entry to put on the Work queue.
81 *
82 * This routine will copy the contents of @wqe to the next available entry on
83 * the @q. This function will then ring the Work Queue Doorbell to signal the
84 * HBA to start processing the Work Queue Entry. This function returns 0 if
85 * successful. If no entries are available on @q then this function will return
86 * -ENOMEM.
87 * The caller is expected to hold the hbalock when calling this routine.
88 **/
89static uint32_t
90lpfc_sli4_wq_put(struct lpfc_queue *q, union lpfc_wqe *wqe)
91{
92 union lpfc_wqe *temp_wqe = q->qe[q->host_index].wqe;
93 struct lpfc_register doorbell;
94 uint32_t host_index;
95
96 /* If the host has not yet processed the next entry then we are done */
97 if (((q->host_index + 1) % q->entry_count) == q->hba_index)
98 return -ENOMEM;
99 /* set consumption flag every once in a while */
100 if (!((q->host_index + 1) % LPFC_RELEASE_NOTIFICATION_INTERVAL))
James Smartf0d9bcc2010-10-22 11:07:09 -0400101 bf_set(wqe_wqec, &wqe->generic.wqe_com, 1);
James Smartfedd3b72011-02-16 12:39:24 -0500102 if (q->phba->sli3_options & LPFC_SLI4_PHWQ_ENABLED)
103 bf_set(wqe_wqid, &wqe->generic.wqe_com, q->queue_id);
James Smart4f774512009-05-22 14:52:35 -0400104 lpfc_sli_pcimem_bcopy(wqe, temp_wqe, q->entry_size);
105
106 /* Update the host index before invoking device */
107 host_index = q->host_index;
108 q->host_index = ((q->host_index + 1) % q->entry_count);
109
110 /* Ring Doorbell */
111 doorbell.word0 = 0;
112 bf_set(lpfc_wq_doorbell_num_posted, &doorbell, 1);
113 bf_set(lpfc_wq_doorbell_index, &doorbell, host_index);
114 bf_set(lpfc_wq_doorbell_id, &doorbell, q->queue_id);
115 writel(doorbell.word0, q->phba->sli4_hba.WQDBregaddr);
116 readl(q->phba->sli4_hba.WQDBregaddr); /* Flush */
117
118 return 0;
119}
120
121/**
122 * lpfc_sli4_wq_release - Updates internal hba index for WQ
123 * @q: The Work Queue to operate on.
124 * @index: The index to advance the hba index to.
125 *
126 * This routine will update the HBA index of a queue to reflect consumption of
127 * Work Queue Entries by the HBA. When the HBA indicates that it has consumed
128 * an entry the host calls this function to update the queue's internal
129 * pointers. This routine returns the number of entries that were consumed by
130 * the HBA.
131 **/
132static uint32_t
133lpfc_sli4_wq_release(struct lpfc_queue *q, uint32_t index)
134{
135 uint32_t released = 0;
136
137 if (q->hba_index == index)
138 return 0;
139 do {
140 q->hba_index = ((q->hba_index + 1) % q->entry_count);
141 released++;
142 } while (q->hba_index != index);
143 return released;
144}
145
146/**
147 * lpfc_sli4_mq_put - Put a Mailbox Queue Entry on an Mailbox Queue
148 * @q: The Mailbox Queue to operate on.
149 * @wqe: The Mailbox Queue Entry to put on the Work queue.
150 *
151 * This routine will copy the contents of @mqe to the next available entry on
152 * the @q. This function will then ring the Work Queue Doorbell to signal the
153 * HBA to start processing the Work Queue Entry. This function returns 0 if
154 * successful. If no entries are available on @q then this function will return
155 * -ENOMEM.
156 * The caller is expected to hold the hbalock when calling this routine.
157 **/
158static uint32_t
159lpfc_sli4_mq_put(struct lpfc_queue *q, struct lpfc_mqe *mqe)
160{
161 struct lpfc_mqe *temp_mqe = q->qe[q->host_index].mqe;
162 struct lpfc_register doorbell;
163 uint32_t host_index;
164
165 /* If the host has not yet processed the next entry then we are done */
166 if (((q->host_index + 1) % q->entry_count) == q->hba_index)
167 return -ENOMEM;
168 lpfc_sli_pcimem_bcopy(mqe, temp_mqe, q->entry_size);
169 /* Save off the mailbox pointer for completion */
170 q->phba->mbox = (MAILBOX_t *)temp_mqe;
171
172 /* Update the host index before invoking device */
173 host_index = q->host_index;
174 q->host_index = ((q->host_index + 1) % q->entry_count);
175
176 /* Ring Doorbell */
177 doorbell.word0 = 0;
178 bf_set(lpfc_mq_doorbell_num_posted, &doorbell, 1);
179 bf_set(lpfc_mq_doorbell_id, &doorbell, q->queue_id);
180 writel(doorbell.word0, q->phba->sli4_hba.MQDBregaddr);
181 readl(q->phba->sli4_hba.MQDBregaddr); /* Flush */
182 return 0;
183}
184
185/**
186 * lpfc_sli4_mq_release - Updates internal hba index for MQ
187 * @q: The Mailbox Queue to operate on.
188 *
189 * This routine will update the HBA index of a queue to reflect consumption of
190 * a Mailbox Queue Entry by the HBA. When the HBA indicates that it has consumed
191 * an entry the host calls this function to update the queue's internal
192 * pointers. This routine returns the number of entries that were consumed by
193 * the HBA.
194 **/
195static uint32_t
196lpfc_sli4_mq_release(struct lpfc_queue *q)
197{
198 /* Clear the mailbox pointer for completion */
199 q->phba->mbox = NULL;
200 q->hba_index = ((q->hba_index + 1) % q->entry_count);
201 return 1;
202}
203
204/**
205 * lpfc_sli4_eq_get - Gets the next valid EQE from a EQ
206 * @q: The Event Queue to get the first valid EQE from
207 *
208 * This routine will get the first valid Event Queue Entry from @q, update
209 * the queue's internal hba index, and return the EQE. If no valid EQEs are in
210 * the Queue (no more work to do), or the Queue is full of EQEs that have been
211 * processed, but not popped back to the HBA then this routine will return NULL.
212 **/
213static struct lpfc_eqe *
214lpfc_sli4_eq_get(struct lpfc_queue *q)
215{
216 struct lpfc_eqe *eqe = q->qe[q->hba_index].eqe;
217
218 /* If the next EQE is not valid then we are done */
James Smartcb5172e2010-03-15 11:25:07 -0400219 if (!bf_get_le32(lpfc_eqe_valid, eqe))
James Smart4f774512009-05-22 14:52:35 -0400220 return NULL;
221 /* If the host has not yet processed the next entry then we are done */
222 if (((q->hba_index + 1) % q->entry_count) == q->host_index)
223 return NULL;
224
225 q->hba_index = ((q->hba_index + 1) % q->entry_count);
226 return eqe;
227}
228
229/**
230 * lpfc_sli4_eq_release - Indicates the host has finished processing an EQ
231 * @q: The Event Queue that the host has completed processing for.
232 * @arm: Indicates whether the host wants to arms this CQ.
233 *
234 * This routine will mark all Event Queue Entries on @q, from the last
235 * known completed entry to the last entry that was processed, as completed
236 * by clearing the valid bit for each completion queue entry. Then it will
237 * notify the HBA, by ringing the doorbell, that the EQEs have been processed.
238 * The internal host index in the @q will be updated by this routine to indicate
239 * that the host has finished processing the entries. The @arm parameter
240 * indicates that the queue should be rearmed when ringing the doorbell.
241 *
242 * This function will return the number of EQEs that were popped.
243 **/
244uint32_t
245lpfc_sli4_eq_release(struct lpfc_queue *q, bool arm)
246{
247 uint32_t released = 0;
248 struct lpfc_eqe *temp_eqe;
249 struct lpfc_register doorbell;
250
251 /* while there are valid entries */
252 while (q->hba_index != q->host_index) {
253 temp_eqe = q->qe[q->host_index].eqe;
James Smartcb5172e2010-03-15 11:25:07 -0400254 bf_set_le32(lpfc_eqe_valid, temp_eqe, 0);
James Smart4f774512009-05-22 14:52:35 -0400255 released++;
256 q->host_index = ((q->host_index + 1) % q->entry_count);
257 }
258 if (unlikely(released == 0 && !arm))
259 return 0;
260
261 /* ring doorbell for number popped */
262 doorbell.word0 = 0;
263 if (arm) {
264 bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);
265 bf_set(lpfc_eqcq_doorbell_eqci, &doorbell, 1);
266 }
267 bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, released);
268 bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_EVENT);
269 bf_set(lpfc_eqcq_doorbell_eqid, &doorbell, q->queue_id);
270 writel(doorbell.word0, q->phba->sli4_hba.EQCQDBregaddr);
James Smarta747c9c2009-11-18 15:41:10 -0500271 /* PCI read to flush PCI pipeline on re-arming for INTx mode */
272 if ((q->phba->intr_type == INTx) && (arm == LPFC_QUEUE_REARM))
273 readl(q->phba->sli4_hba.EQCQDBregaddr);
James Smart4f774512009-05-22 14:52:35 -0400274 return released;
275}
276
277/**
278 * lpfc_sli4_cq_get - Gets the next valid CQE from a CQ
279 * @q: The Completion Queue to get the first valid CQE from
280 *
281 * This routine will get the first valid Completion Queue Entry from @q, update
282 * the queue's internal hba index, and return the CQE. If no valid CQEs are in
283 * the Queue (no more work to do), or the Queue is full of CQEs that have been
284 * processed, but not popped back to the HBA then this routine will return NULL.
285 **/
286static struct lpfc_cqe *
287lpfc_sli4_cq_get(struct lpfc_queue *q)
288{
289 struct lpfc_cqe *cqe;
290
291 /* If the next CQE is not valid then we are done */
James Smartcb5172e2010-03-15 11:25:07 -0400292 if (!bf_get_le32(lpfc_cqe_valid, q->qe[q->hba_index].cqe))
James Smart4f774512009-05-22 14:52:35 -0400293 return NULL;
294 /* If the host has not yet processed the next entry then we are done */
295 if (((q->hba_index + 1) % q->entry_count) == q->host_index)
296 return NULL;
297
298 cqe = q->qe[q->hba_index].cqe;
299 q->hba_index = ((q->hba_index + 1) % q->entry_count);
300 return cqe;
301}
302
303/**
304 * lpfc_sli4_cq_release - Indicates the host has finished processing a CQ
305 * @q: The Completion Queue that the host has completed processing for.
306 * @arm: Indicates whether the host wants to arms this CQ.
307 *
308 * This routine will mark all Completion queue entries on @q, from the last
309 * known completed entry to the last entry that was processed, as completed
310 * by clearing the valid bit for each completion queue entry. Then it will
311 * notify the HBA, by ringing the doorbell, that the CQEs have been processed.
312 * The internal host index in the @q will be updated by this routine to indicate
313 * that the host has finished processing the entries. The @arm parameter
314 * indicates that the queue should be rearmed when ringing the doorbell.
315 *
316 * This function will return the number of CQEs that were released.
317 **/
318uint32_t
319lpfc_sli4_cq_release(struct lpfc_queue *q, bool arm)
320{
321 uint32_t released = 0;
322 struct lpfc_cqe *temp_qe;
323 struct lpfc_register doorbell;
324
325 /* while there are valid entries */
326 while (q->hba_index != q->host_index) {
327 temp_qe = q->qe[q->host_index].cqe;
James Smartcb5172e2010-03-15 11:25:07 -0400328 bf_set_le32(lpfc_cqe_valid, temp_qe, 0);
James Smart4f774512009-05-22 14:52:35 -0400329 released++;
330 q->host_index = ((q->host_index + 1) % q->entry_count);
331 }
332 if (unlikely(released == 0 && !arm))
333 return 0;
334
335 /* ring doorbell for number popped */
336 doorbell.word0 = 0;
337 if (arm)
338 bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);
339 bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, released);
340 bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_COMPLETION);
341 bf_set(lpfc_eqcq_doorbell_cqid, &doorbell, q->queue_id);
342 writel(doorbell.word0, q->phba->sli4_hba.EQCQDBregaddr);
343 return released;
344}
345
346/**
347 * lpfc_sli4_rq_put - Put a Receive Buffer Queue Entry on a Receive Queue
348 * @q: The Header Receive Queue to operate on.
349 * @wqe: The Receive Queue Entry to put on the Receive queue.
350 *
351 * This routine will copy the contents of @wqe to the next available entry on
352 * the @q. This function will then ring the Receive Queue Doorbell to signal the
353 * HBA to start processing the Receive Queue Entry. This function returns the
354 * index that the rqe was copied to if successful. If no entries are available
355 * on @q then this function will return -ENOMEM.
356 * The caller is expected to hold the hbalock when calling this routine.
357 **/
358static int
359lpfc_sli4_rq_put(struct lpfc_queue *hq, struct lpfc_queue *dq,
360 struct lpfc_rqe *hrqe, struct lpfc_rqe *drqe)
361{
362 struct lpfc_rqe *temp_hrqe = hq->qe[hq->host_index].rqe;
363 struct lpfc_rqe *temp_drqe = dq->qe[dq->host_index].rqe;
364 struct lpfc_register doorbell;
365 int put_index = hq->host_index;
366
367 if (hq->type != LPFC_HRQ || dq->type != LPFC_DRQ)
368 return -EINVAL;
369 if (hq->host_index != dq->host_index)
370 return -EINVAL;
371 /* If the host has not yet processed the next entry then we are done */
372 if (((hq->host_index + 1) % hq->entry_count) == hq->hba_index)
373 return -EBUSY;
374 lpfc_sli_pcimem_bcopy(hrqe, temp_hrqe, hq->entry_size);
375 lpfc_sli_pcimem_bcopy(drqe, temp_drqe, dq->entry_size);
376
377 /* Update the host index to point to the next slot */
378 hq->host_index = ((hq->host_index + 1) % hq->entry_count);
379 dq->host_index = ((dq->host_index + 1) % dq->entry_count);
380
381 /* Ring The Header Receive Queue Doorbell */
James Smart73d91e52011-10-10 21:32:10 -0400382 if (!(hq->host_index % hq->entry_repost)) {
James Smart4f774512009-05-22 14:52:35 -0400383 doorbell.word0 = 0;
384 bf_set(lpfc_rq_doorbell_num_posted, &doorbell,
James Smart73d91e52011-10-10 21:32:10 -0400385 hq->entry_repost);
James Smart4f774512009-05-22 14:52:35 -0400386 bf_set(lpfc_rq_doorbell_id, &doorbell, hq->queue_id);
387 writel(doorbell.word0, hq->phba->sli4_hba.RQDBregaddr);
388 }
389 return put_index;
390}
391
392/**
393 * lpfc_sli4_rq_release - Updates internal hba index for RQ
394 * @q: The Header Receive Queue to operate on.
395 *
396 * This routine will update the HBA index of a queue to reflect consumption of
397 * one Receive Queue Entry by the HBA. When the HBA indicates that it has
398 * consumed an entry the host calls this function to update the queue's
399 * internal pointers. This routine returns the number of entries that were
400 * consumed by the HBA.
401 **/
402static uint32_t
403lpfc_sli4_rq_release(struct lpfc_queue *hq, struct lpfc_queue *dq)
404{
405 if ((hq->type != LPFC_HRQ) || (dq->type != LPFC_DRQ))
406 return 0;
407 hq->hba_index = ((hq->hba_index + 1) % hq->entry_count);
408 dq->hba_index = ((dq->hba_index + 1) % dq->entry_count);
409 return 1;
410}
411
James Smarte59058c2008-08-24 21:49:00 -0400412/**
James Smart3621a712009-04-06 18:47:14 -0400413 * lpfc_cmd_iocb - Get next command iocb entry in the ring
James Smarte59058c2008-08-24 21:49:00 -0400414 * @phba: Pointer to HBA context object.
415 * @pring: Pointer to driver SLI ring object.
416 *
417 * This function returns pointer to next command iocb entry
418 * in the command ring. The caller must hold hbalock to prevent
419 * other threads consume the next command iocb.
420 * SLI-2/SLI-3 provide different sized iocbs.
421 **/
James Smarted957682007-06-17 19:56:37 -0500422static inline IOCB_t *
423lpfc_cmd_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
424{
425 return (IOCB_t *) (((char *) pring->cmdringaddr) +
426 pring->cmdidx * phba->iocb_cmd_size);
427}
428
James Smarte59058c2008-08-24 21:49:00 -0400429/**
James Smart3621a712009-04-06 18:47:14 -0400430 * lpfc_resp_iocb - Get next response iocb entry in the ring
James Smarte59058c2008-08-24 21:49:00 -0400431 * @phba: Pointer to HBA context object.
432 * @pring: Pointer to driver SLI ring object.
433 *
434 * This function returns pointer to next response iocb entry
435 * in the response ring. The caller must hold hbalock to make sure
436 * that no other thread consume the next response iocb.
437 * SLI-2/SLI-3 provide different sized iocbs.
438 **/
James Smarted957682007-06-17 19:56:37 -0500439static inline IOCB_t *
440lpfc_resp_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
441{
442 return (IOCB_t *) (((char *) pring->rspringaddr) +
443 pring->rspidx * phba->iocb_rsp_size);
444}
445
James Smarte59058c2008-08-24 21:49:00 -0400446/**
James Smart3621a712009-04-06 18:47:14 -0400447 * __lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
James Smarte59058c2008-08-24 21:49:00 -0400448 * @phba: Pointer to HBA context object.
449 *
450 * This function is called with hbalock held. This function
451 * allocates a new driver iocb object from the iocb pool. If the
452 * allocation is successful, it returns pointer to the newly
453 * allocated iocb object else it returns NULL.
454 **/
James Smart2e0fef82007-06-17 19:56:36 -0500455static struct lpfc_iocbq *
456__lpfc_sli_get_iocbq(struct lpfc_hba *phba)
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400457{
458 struct list_head *lpfc_iocb_list = &phba->lpfc_iocb_list;
459 struct lpfc_iocbq * iocbq = NULL;
460
461 list_remove_head(lpfc_iocb_list, iocbq, struct lpfc_iocbq, list);
James Smart2a9bf3d2010-06-07 15:24:45 -0400462 if (iocbq)
463 phba->iocb_cnt++;
464 if (phba->iocb_cnt > phba->iocb_max)
465 phba->iocb_max = phba->iocb_cnt;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400466 return iocbq;
467}
468
James Smarte59058c2008-08-24 21:49:00 -0400469/**
James Smartda0436e2009-05-22 14:51:39 -0400470 * __lpfc_clear_active_sglq - Remove the active sglq for this XRI.
471 * @phba: Pointer to HBA context object.
472 * @xritag: XRI value.
473 *
474 * This function clears the sglq pointer from the array of acive
475 * sglq's. The xritag that is passed in is used to index into the
476 * array. Before the xritag can be used it needs to be adjusted
477 * by subtracting the xribase.
478 *
479 * Returns sglq ponter = success, NULL = Failure.
480 **/
481static struct lpfc_sglq *
482__lpfc_clear_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
483{
James Smartda0436e2009-05-22 14:51:39 -0400484 struct lpfc_sglq *sglq;
James Smart6d368e52011-05-24 11:44:12 -0400485
486 sglq = phba->sli4_hba.lpfc_sglq_active_list[xritag];
487 phba->sli4_hba.lpfc_sglq_active_list[xritag] = NULL;
James Smartda0436e2009-05-22 14:51:39 -0400488 return sglq;
489}
490
491/**
492 * __lpfc_get_active_sglq - Get the active sglq for this XRI.
493 * @phba: Pointer to HBA context object.
494 * @xritag: XRI value.
495 *
496 * This function returns the sglq pointer from the array of acive
497 * sglq's. The xritag that is passed in is used to index into the
498 * array. Before the xritag can be used it needs to be adjusted
499 * by subtracting the xribase.
500 *
501 * Returns sglq ponter = success, NULL = Failure.
502 **/
James Smart0f65ff62010-02-26 14:14:23 -0500503struct lpfc_sglq *
James Smartda0436e2009-05-22 14:51:39 -0400504__lpfc_get_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
505{
James Smartda0436e2009-05-22 14:51:39 -0400506 struct lpfc_sglq *sglq;
James Smart6d368e52011-05-24 11:44:12 -0400507
508 sglq = phba->sli4_hba.lpfc_sglq_active_list[xritag];
James Smartda0436e2009-05-22 14:51:39 -0400509 return sglq;
510}
511
512/**
James Smart19ca7602010-11-20 23:11:55 -0500513 * __lpfc_set_rrq_active - set RRQ active bit in the ndlp's xri_bitmap.
514 * @phba: Pointer to HBA context object.
515 * @ndlp: nodelist pointer for this target.
516 * @xritag: xri used in this exchange.
517 * @rxid: Remote Exchange ID.
518 * @send_rrq: Flag used to determine if we should send rrq els cmd.
519 *
520 * This function is called with hbalock held.
521 * The active bit is set in the ndlp's active rrq xri_bitmap. Allocates an
522 * rrq struct and adds it to the active_rrq_list.
523 *
524 * returns 0 for rrq slot for this xri
525 * < 0 Were not able to get rrq mem or invalid parameter.
526 **/
527static int
528__lpfc_set_rrq_active(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
529 uint16_t xritag, uint16_t rxid, uint16_t send_rrq)
530{
James Smart19ca7602010-11-20 23:11:55 -0500531 struct lpfc_node_rrq *rrq;
532 int empty;
James Smart1151e3e2011-02-16 12:39:35 -0500533 uint32_t did = 0;
534
535
536 if (!ndlp)
537 return -EINVAL;
538
539 if (!phba->cfg_enable_rrq)
540 return -EINVAL;
541
542 if (phba->pport->load_flag & FC_UNLOADING) {
543 phba->hba_flag &= ~HBA_RRQ_ACTIVE;
544 goto out;
545 }
546 did = ndlp->nlp_DID;
James Smart19ca7602010-11-20 23:11:55 -0500547
548 /*
549 * set the active bit even if there is no mem available.
550 */
James Smart1151e3e2011-02-16 12:39:35 -0500551 if (NLP_CHK_FREE_REQ(ndlp))
552 goto out;
553
554 if (ndlp->vport && (ndlp->vport->load_flag & FC_UNLOADING))
555 goto out;
556
James Smart6d368e52011-05-24 11:44:12 -0400557 if (test_and_set_bit(xritag, ndlp->active_rrqs.xri_bitmap))
James Smart1151e3e2011-02-16 12:39:35 -0500558 goto out;
559
James Smart19ca7602010-11-20 23:11:55 -0500560 rrq = mempool_alloc(phba->rrq_pool, GFP_KERNEL);
561 if (rrq) {
562 rrq->send_rrq = send_rrq;
James Smart7851fe22011-07-22 18:36:52 -0400563 rrq->xritag = xritag;
James Smart19ca7602010-11-20 23:11:55 -0500564 rrq->rrq_stop_time = jiffies + HZ * (phba->fc_ratov + 1);
565 rrq->ndlp = ndlp;
566 rrq->nlp_DID = ndlp->nlp_DID;
567 rrq->vport = ndlp->vport;
568 rrq->rxid = rxid;
569 empty = list_empty(&phba->active_rrq_list);
James Smart1151e3e2011-02-16 12:39:35 -0500570 rrq->send_rrq = send_rrq;
James Smart19ca7602010-11-20 23:11:55 -0500571 list_add_tail(&rrq->list, &phba->active_rrq_list);
572 if (!(phba->hba_flag & HBA_RRQ_ACTIVE)) {
573 phba->hba_flag |= HBA_RRQ_ACTIVE;
574 if (empty)
575 lpfc_worker_wake_up(phba);
576 }
577 return 0;
578 }
James Smart1151e3e2011-02-16 12:39:35 -0500579out:
580 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
581 "2921 Can't set rrq active xri:0x%x rxid:0x%x"
582 " DID:0x%x Send:%d\n",
583 xritag, rxid, did, send_rrq);
584 return -EINVAL;
James Smart19ca7602010-11-20 23:11:55 -0500585}
586
587/**
James Smart1151e3e2011-02-16 12:39:35 -0500588 * lpfc_clr_rrq_active - Clears RRQ active bit in xri_bitmap.
James Smart19ca7602010-11-20 23:11:55 -0500589 * @phba: Pointer to HBA context object.
590 * @xritag: xri used in this exchange.
591 * @rrq: The RRQ to be cleared.
592 *
James Smart19ca7602010-11-20 23:11:55 -0500593 **/
James Smart1151e3e2011-02-16 12:39:35 -0500594void
595lpfc_clr_rrq_active(struct lpfc_hba *phba,
596 uint16_t xritag,
597 struct lpfc_node_rrq *rrq)
James Smart19ca7602010-11-20 23:11:55 -0500598{
James Smart1151e3e2011-02-16 12:39:35 -0500599 struct lpfc_nodelist *ndlp = NULL;
James Smart19ca7602010-11-20 23:11:55 -0500600
James Smart1151e3e2011-02-16 12:39:35 -0500601 if ((rrq->vport) && NLP_CHK_NODE_ACT(rrq->ndlp))
602 ndlp = lpfc_findnode_did(rrq->vport, rrq->nlp_DID);
James Smart19ca7602010-11-20 23:11:55 -0500603
604 /* The target DID could have been swapped (cable swap)
605 * we should use the ndlp from the findnode if it is
606 * available.
607 */
James Smart1151e3e2011-02-16 12:39:35 -0500608 if ((!ndlp) && rrq->ndlp)
James Smart19ca7602010-11-20 23:11:55 -0500609 ndlp = rrq->ndlp;
610
James Smart1151e3e2011-02-16 12:39:35 -0500611 if (!ndlp)
612 goto out;
613
James Smart6d368e52011-05-24 11:44:12 -0400614 if (test_and_clear_bit(xritag, ndlp->active_rrqs.xri_bitmap)) {
James Smart19ca7602010-11-20 23:11:55 -0500615 rrq->send_rrq = 0;
616 rrq->xritag = 0;
617 rrq->rrq_stop_time = 0;
618 }
James Smart1151e3e2011-02-16 12:39:35 -0500619out:
James Smart19ca7602010-11-20 23:11:55 -0500620 mempool_free(rrq, phba->rrq_pool);
621}
622
623/**
624 * lpfc_handle_rrq_active - Checks if RRQ has waithed RATOV.
625 * @phba: Pointer to HBA context object.
626 *
627 * This function is called with hbalock held. This function
628 * Checks if stop_time (ratov from setting rrq active) has
629 * been reached, if it has and the send_rrq flag is set then
630 * it will call lpfc_send_rrq. If the send_rrq flag is not set
631 * then it will just call the routine to clear the rrq and
632 * free the rrq resource.
633 * The timer is set to the next rrq that is going to expire before
634 * leaving the routine.
635 *
636 **/
637void
638lpfc_handle_rrq_active(struct lpfc_hba *phba)
639{
640 struct lpfc_node_rrq *rrq;
641 struct lpfc_node_rrq *nextrrq;
642 unsigned long next_time;
643 unsigned long iflags;
James Smart1151e3e2011-02-16 12:39:35 -0500644 LIST_HEAD(send_rrq);
James Smart19ca7602010-11-20 23:11:55 -0500645
646 spin_lock_irqsave(&phba->hbalock, iflags);
647 phba->hba_flag &= ~HBA_RRQ_ACTIVE;
648 next_time = jiffies + HZ * (phba->fc_ratov + 1);
649 list_for_each_entry_safe(rrq, nextrrq,
James Smart1151e3e2011-02-16 12:39:35 -0500650 &phba->active_rrq_list, list) {
651 if (time_after(jiffies, rrq->rrq_stop_time))
652 list_move(&rrq->list, &send_rrq);
653 else if (time_before(rrq->rrq_stop_time, next_time))
James Smart19ca7602010-11-20 23:11:55 -0500654 next_time = rrq->rrq_stop_time;
655 }
656 spin_unlock_irqrestore(&phba->hbalock, iflags);
657 if (!list_empty(&phba->active_rrq_list))
658 mod_timer(&phba->rrq_tmr, next_time);
James Smart1151e3e2011-02-16 12:39:35 -0500659 list_for_each_entry_safe(rrq, nextrrq, &send_rrq, list) {
660 list_del(&rrq->list);
661 if (!rrq->send_rrq)
662 /* this call will free the rrq */
663 lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
664 else if (lpfc_send_rrq(phba, rrq)) {
665 /* if we send the rrq then the completion handler
666 * will clear the bit in the xribitmap.
667 */
668 lpfc_clr_rrq_active(phba, rrq->xritag,
669 rrq);
670 }
671 }
James Smart19ca7602010-11-20 23:11:55 -0500672}
673
674/**
675 * lpfc_get_active_rrq - Get the active RRQ for this exchange.
676 * @vport: Pointer to vport context object.
677 * @xri: The xri used in the exchange.
678 * @did: The targets DID for this exchange.
679 *
680 * returns NULL = rrq not found in the phba->active_rrq_list.
681 * rrq = rrq for this xri and target.
682 **/
683struct lpfc_node_rrq *
684lpfc_get_active_rrq(struct lpfc_vport *vport, uint16_t xri, uint32_t did)
685{
686 struct lpfc_hba *phba = vport->phba;
687 struct lpfc_node_rrq *rrq;
688 struct lpfc_node_rrq *nextrrq;
689 unsigned long iflags;
690
691 if (phba->sli_rev != LPFC_SLI_REV4)
692 return NULL;
693 spin_lock_irqsave(&phba->hbalock, iflags);
694 list_for_each_entry_safe(rrq, nextrrq, &phba->active_rrq_list, list) {
695 if (rrq->vport == vport && rrq->xritag == xri &&
696 rrq->nlp_DID == did){
697 list_del(&rrq->list);
698 spin_unlock_irqrestore(&phba->hbalock, iflags);
699 return rrq;
700 }
701 }
702 spin_unlock_irqrestore(&phba->hbalock, iflags);
703 return NULL;
704}
705
706/**
707 * lpfc_cleanup_vports_rrqs - Remove and clear the active RRQ for this vport.
708 * @vport: Pointer to vport context object.
James Smart1151e3e2011-02-16 12:39:35 -0500709 * @ndlp: Pointer to the lpfc_node_list structure.
710 * If ndlp is NULL Remove all active RRQs for this vport from the
711 * phba->active_rrq_list and clear the rrq.
712 * If ndlp is not NULL then only remove rrqs for this vport & this ndlp.
James Smart19ca7602010-11-20 23:11:55 -0500713 **/
714void
James Smart1151e3e2011-02-16 12:39:35 -0500715lpfc_cleanup_vports_rrqs(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
James Smart19ca7602010-11-20 23:11:55 -0500716
717{
718 struct lpfc_hba *phba = vport->phba;
719 struct lpfc_node_rrq *rrq;
720 struct lpfc_node_rrq *nextrrq;
721 unsigned long iflags;
James Smart1151e3e2011-02-16 12:39:35 -0500722 LIST_HEAD(rrq_list);
James Smart19ca7602010-11-20 23:11:55 -0500723
724 if (phba->sli_rev != LPFC_SLI_REV4)
725 return;
James Smart1151e3e2011-02-16 12:39:35 -0500726 if (!ndlp) {
727 lpfc_sli4_vport_delete_els_xri_aborted(vport);
728 lpfc_sli4_vport_delete_fcp_xri_aborted(vport);
James Smart19ca7602010-11-20 23:11:55 -0500729 }
James Smart1151e3e2011-02-16 12:39:35 -0500730 spin_lock_irqsave(&phba->hbalock, iflags);
731 list_for_each_entry_safe(rrq, nextrrq, &phba->active_rrq_list, list)
732 if ((rrq->vport == vport) && (!ndlp || rrq->ndlp == ndlp))
733 list_move(&rrq->list, &rrq_list);
James Smart19ca7602010-11-20 23:11:55 -0500734 spin_unlock_irqrestore(&phba->hbalock, iflags);
James Smart1151e3e2011-02-16 12:39:35 -0500735
736 list_for_each_entry_safe(rrq, nextrrq, &rrq_list, list) {
737 list_del(&rrq->list);
738 lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
739 }
James Smart19ca7602010-11-20 23:11:55 -0500740}
741
742/**
743 * lpfc_cleanup_wt_rrqs - Remove all rrq's from the active list.
744 * @phba: Pointer to HBA context object.
745 *
746 * Remove all rrqs from the phba->active_rrq_list and free them by
747 * calling __lpfc_clr_active_rrq
748 *
749 **/
750void
751lpfc_cleanup_wt_rrqs(struct lpfc_hba *phba)
752{
753 struct lpfc_node_rrq *rrq;
754 struct lpfc_node_rrq *nextrrq;
755 unsigned long next_time;
756 unsigned long iflags;
James Smart1151e3e2011-02-16 12:39:35 -0500757 LIST_HEAD(rrq_list);
James Smart19ca7602010-11-20 23:11:55 -0500758
759 if (phba->sli_rev != LPFC_SLI_REV4)
760 return;
761 spin_lock_irqsave(&phba->hbalock, iflags);
762 phba->hba_flag &= ~HBA_RRQ_ACTIVE;
763 next_time = jiffies + HZ * (phba->fc_ratov * 2);
James Smart1151e3e2011-02-16 12:39:35 -0500764 list_splice_init(&phba->active_rrq_list, &rrq_list);
James Smart19ca7602010-11-20 23:11:55 -0500765 spin_unlock_irqrestore(&phba->hbalock, iflags);
James Smart1151e3e2011-02-16 12:39:35 -0500766
767 list_for_each_entry_safe(rrq, nextrrq, &rrq_list, list) {
768 list_del(&rrq->list);
769 lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
770 }
James Smart19ca7602010-11-20 23:11:55 -0500771 if (!list_empty(&phba->active_rrq_list))
772 mod_timer(&phba->rrq_tmr, next_time);
773}
774
775
776/**
James Smart1151e3e2011-02-16 12:39:35 -0500777 * lpfc_test_rrq_active - Test RRQ bit in xri_bitmap.
James Smart19ca7602010-11-20 23:11:55 -0500778 * @phba: Pointer to HBA context object.
779 * @ndlp: Targets nodelist pointer for this exchange.
780 * @xritag the xri in the bitmap to test.
781 *
782 * This function is called with hbalock held. This function
783 * returns 0 = rrq not active for this xri
784 * 1 = rrq is valid for this xri.
785 **/
James Smart1151e3e2011-02-16 12:39:35 -0500786int
787lpfc_test_rrq_active(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
James Smart19ca7602010-11-20 23:11:55 -0500788 uint16_t xritag)
789{
James Smart19ca7602010-11-20 23:11:55 -0500790 if (!ndlp)
791 return 0;
James Smart6d368e52011-05-24 11:44:12 -0400792 if (test_bit(xritag, ndlp->active_rrqs.xri_bitmap))
James Smart19ca7602010-11-20 23:11:55 -0500793 return 1;
794 else
795 return 0;
796}
797
798/**
799 * lpfc_set_rrq_active - set RRQ active bit in xri_bitmap.
800 * @phba: Pointer to HBA context object.
801 * @ndlp: nodelist pointer for this target.
802 * @xritag: xri used in this exchange.
803 * @rxid: Remote Exchange ID.
804 * @send_rrq: Flag used to determine if we should send rrq els cmd.
805 *
806 * This function takes the hbalock.
807 * The active bit is always set in the active rrq xri_bitmap even
808 * if there is no slot avaiable for the other rrq information.
809 *
810 * returns 0 rrq actived for this xri
811 * < 0 No memory or invalid ndlp.
812 **/
813int
814lpfc_set_rrq_active(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
815 uint16_t xritag, uint16_t rxid, uint16_t send_rrq)
816{
817 int ret;
818 unsigned long iflags;
819
820 spin_lock_irqsave(&phba->hbalock, iflags);
821 ret = __lpfc_set_rrq_active(phba, ndlp, xritag, rxid, send_rrq);
822 spin_unlock_irqrestore(&phba->hbalock, iflags);
823 return ret;
824}
825
826/**
James Smartda0436e2009-05-22 14:51:39 -0400827 * __lpfc_sli_get_sglq - Allocates an iocb object from sgl pool
828 * @phba: Pointer to HBA context object.
James Smart19ca7602010-11-20 23:11:55 -0500829 * @piocb: Pointer to the iocbq.
James Smartda0436e2009-05-22 14:51:39 -0400830 *
831 * This function is called with hbalock held. This function
James Smart6d368e52011-05-24 11:44:12 -0400832 * gets a new driver sglq object from the sglq list. If the
James Smartda0436e2009-05-22 14:51:39 -0400833 * list is not empty then it is successful, it returns pointer to the newly
834 * allocated sglq object else it returns NULL.
835 **/
836static struct lpfc_sglq *
James Smart19ca7602010-11-20 23:11:55 -0500837__lpfc_sli_get_sglq(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq)
James Smartda0436e2009-05-22 14:51:39 -0400838{
839 struct list_head *lpfc_sgl_list = &phba->sli4_hba.lpfc_sgl_list;
840 struct lpfc_sglq *sglq = NULL;
James Smart19ca7602010-11-20 23:11:55 -0500841 struct lpfc_sglq *start_sglq = NULL;
James Smart19ca7602010-11-20 23:11:55 -0500842 struct lpfc_scsi_buf *lpfc_cmd;
843 struct lpfc_nodelist *ndlp;
844 int found = 0;
845
846 if (piocbq->iocb_flag & LPFC_IO_FCP) {
847 lpfc_cmd = (struct lpfc_scsi_buf *) piocbq->context1;
848 ndlp = lpfc_cmd->rdata->pnode;
James Smartbe858b62010-12-15 17:57:20 -0500849 } else if ((piocbq->iocb.ulpCommand == CMD_GEN_REQUEST64_CR) &&
850 !(piocbq->iocb_flag & LPFC_IO_LIBDFC))
James Smart19ca7602010-11-20 23:11:55 -0500851 ndlp = piocbq->context_un.ndlp;
James Smart19ca7602010-11-20 23:11:55 -0500852 else
853 ndlp = piocbq->context1;
854
James Smartda0436e2009-05-22 14:51:39 -0400855 list_remove_head(lpfc_sgl_list, sglq, struct lpfc_sglq, list);
James Smart19ca7602010-11-20 23:11:55 -0500856 start_sglq = sglq;
857 while (!found) {
858 if (!sglq)
859 return NULL;
James Smart1151e3e2011-02-16 12:39:35 -0500860 if (lpfc_test_rrq_active(phba, ndlp, sglq->sli4_xritag)) {
James Smart19ca7602010-11-20 23:11:55 -0500861 /* This xri has an rrq outstanding for this DID.
862 * put it back in the list and get another xri.
863 */
864 list_add_tail(&sglq->list, lpfc_sgl_list);
865 sglq = NULL;
866 list_remove_head(lpfc_sgl_list, sglq,
867 struct lpfc_sglq, list);
868 if (sglq == start_sglq) {
869 sglq = NULL;
870 break;
871 } else
872 continue;
873 }
874 sglq->ndlp = ndlp;
875 found = 1;
James Smart6d368e52011-05-24 11:44:12 -0400876 phba->sli4_hba.lpfc_sglq_active_list[sglq->sli4_lxritag] = sglq;
James Smart19ca7602010-11-20 23:11:55 -0500877 sglq->state = SGL_ALLOCATED;
878 }
James Smartda0436e2009-05-22 14:51:39 -0400879 return sglq;
880}
881
882/**
James Smart3621a712009-04-06 18:47:14 -0400883 * lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
James Smarte59058c2008-08-24 21:49:00 -0400884 * @phba: Pointer to HBA context object.
885 *
886 * This function is called with no lock held. This function
887 * allocates a new driver iocb object from the iocb pool. If the
888 * allocation is successful, it returns pointer to the newly
889 * allocated iocb object else it returns NULL.
890 **/
James Smart2e0fef82007-06-17 19:56:36 -0500891struct lpfc_iocbq *
892lpfc_sli_get_iocbq(struct lpfc_hba *phba)
James Bottomley604a3e32005-10-29 10:28:33 -0500893{
James Smart2e0fef82007-06-17 19:56:36 -0500894 struct lpfc_iocbq * iocbq = NULL;
895 unsigned long iflags;
896
897 spin_lock_irqsave(&phba->hbalock, iflags);
898 iocbq = __lpfc_sli_get_iocbq(phba);
899 spin_unlock_irqrestore(&phba->hbalock, iflags);
900 return iocbq;
901}
902
James Smarte59058c2008-08-24 21:49:00 -0400903/**
James Smart4f774512009-05-22 14:52:35 -0400904 * __lpfc_sli_release_iocbq_s4 - Release iocb to the iocb pool
905 * @phba: Pointer to HBA context object.
906 * @iocbq: Pointer to driver iocb object.
907 *
908 * This function is called with hbalock held to release driver
909 * iocb object to the iocb pool. The iotag in the iocb object
910 * does not change for each use of the iocb object. This function
911 * clears all other fields of the iocb object when it is freed.
912 * The sqlq structure that holds the xritag and phys and virtual
913 * mappings for the scatter gather list is retrieved from the
914 * active array of sglq. The get of the sglq pointer also clears
915 * the entry in the array. If the status of the IO indiactes that
916 * this IO was aborted then the sglq entry it put on the
917 * lpfc_abts_els_sgl_list until the CQ_ABORTED_XRI is received. If the
918 * IO has good status or fails for any other reason then the sglq
919 * entry is added to the free list (lpfc_sgl_list).
920 **/
921static void
922__lpfc_sli_release_iocbq_s4(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
923{
924 struct lpfc_sglq *sglq;
925 size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
James Smart2a9bf3d2010-06-07 15:24:45 -0400926 unsigned long iflag = 0;
927 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
James Smart4f774512009-05-22 14:52:35 -0400928
929 if (iocbq->sli4_xritag == NO_XRI)
930 sglq = NULL;
931 else
James Smart6d368e52011-05-24 11:44:12 -0400932 sglq = __lpfc_clear_active_sglq(phba, iocbq->sli4_lxritag);
933
James Smart4f774512009-05-22 14:52:35 -0400934 if (sglq) {
James Smart0f65ff62010-02-26 14:14:23 -0500935 if ((iocbq->iocb_flag & LPFC_EXCHANGE_BUSY) &&
936 (sglq->state != SGL_XRI_ABORTED)) {
James Smart4f774512009-05-22 14:52:35 -0400937 spin_lock_irqsave(&phba->sli4_hba.abts_sgl_list_lock,
938 iflag);
939 list_add(&sglq->list,
940 &phba->sli4_hba.lpfc_abts_els_sgl_list);
941 spin_unlock_irqrestore(
942 &phba->sli4_hba.abts_sgl_list_lock, iflag);
James Smart0f65ff62010-02-26 14:14:23 -0500943 } else {
944 sglq->state = SGL_FREED;
James Smart19ca7602010-11-20 23:11:55 -0500945 sglq->ndlp = NULL;
James Smartfedd3b72011-02-16 12:39:24 -0500946 list_add_tail(&sglq->list,
947 &phba->sli4_hba.lpfc_sgl_list);
James Smart2a9bf3d2010-06-07 15:24:45 -0400948
949 /* Check if TXQ queue needs to be serviced */
James Smart589a52d2010-07-14 15:30:54 -0400950 if (pring->txq_cnt)
James Smart2a9bf3d2010-06-07 15:24:45 -0400951 lpfc_worker_wake_up(phba);
James Smart0f65ff62010-02-26 14:14:23 -0500952 }
James Smart4f774512009-05-22 14:52:35 -0400953 }
954
955
956 /*
957 * Clean all volatile data fields, preserve iotag and node struct.
958 */
959 memset((char *)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
James Smart6d368e52011-05-24 11:44:12 -0400960 iocbq->sli4_lxritag = NO_XRI;
James Smart4f774512009-05-22 14:52:35 -0400961 iocbq->sli4_xritag = NO_XRI;
962 list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
963}
964
James Smart2a9bf3d2010-06-07 15:24:45 -0400965
James Smart4f774512009-05-22 14:52:35 -0400966/**
James Smart3772a992009-05-22 14:50:54 -0400967 * __lpfc_sli_release_iocbq_s3 - Release iocb to the iocb pool
968 * @phba: Pointer to HBA context object.
969 * @iocbq: Pointer to driver iocb object.
970 *
971 * This function is called with hbalock held to release driver
972 * iocb object to the iocb pool. The iotag in the iocb object
973 * does not change for each use of the iocb object. This function
974 * clears all other fields of the iocb object when it is freed.
975 **/
976static void
977__lpfc_sli_release_iocbq_s3(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
978{
979 size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
980
981 /*
982 * Clean all volatile data fields, preserve iotag and node struct.
983 */
984 memset((char*)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
985 iocbq->sli4_xritag = NO_XRI;
986 list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
987}
988
989/**
James Smart3621a712009-04-06 18:47:14 -0400990 * __lpfc_sli_release_iocbq - Release iocb to the iocb pool
James Smarte59058c2008-08-24 21:49:00 -0400991 * @phba: Pointer to HBA context object.
992 * @iocbq: Pointer to driver iocb object.
993 *
994 * This function is called with hbalock held to release driver
995 * iocb object to the iocb pool. The iotag in the iocb object
996 * does not change for each use of the iocb object. This function
997 * clears all other fields of the iocb object when it is freed.
998 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +0100999static void
James Smart2e0fef82007-06-17 19:56:36 -05001000__lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1001{
James Smart3772a992009-05-22 14:50:54 -04001002 phba->__lpfc_sli_release_iocbq(phba, iocbq);
James Smart2a9bf3d2010-06-07 15:24:45 -04001003 phba->iocb_cnt--;
James Bottomley604a3e32005-10-29 10:28:33 -05001004}
1005
James Smarte59058c2008-08-24 21:49:00 -04001006/**
James Smart3621a712009-04-06 18:47:14 -04001007 * lpfc_sli_release_iocbq - Release iocb to the iocb pool
James Smarte59058c2008-08-24 21:49:00 -04001008 * @phba: Pointer to HBA context object.
1009 * @iocbq: Pointer to driver iocb object.
1010 *
1011 * This function is called with no lock held to release the iocb to
1012 * iocb pool.
1013 **/
James Smart2e0fef82007-06-17 19:56:36 -05001014void
1015lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1016{
1017 unsigned long iflags;
1018
1019 /*
1020 * Clean all volatile data fields, preserve iotag and node struct.
1021 */
1022 spin_lock_irqsave(&phba->hbalock, iflags);
1023 __lpfc_sli_release_iocbq(phba, iocbq);
1024 spin_unlock_irqrestore(&phba->hbalock, iflags);
1025}
1026
James Smarte59058c2008-08-24 21:49:00 -04001027/**
James Smarta257bf92009-04-06 18:48:10 -04001028 * lpfc_sli_cancel_iocbs - Cancel all iocbs from a list.
1029 * @phba: Pointer to HBA context object.
1030 * @iocblist: List of IOCBs.
1031 * @ulpstatus: ULP status in IOCB command field.
1032 * @ulpWord4: ULP word-4 in IOCB command field.
1033 *
1034 * This function is called with a list of IOCBs to cancel. It cancels the IOCB
1035 * on the list by invoking the complete callback function associated with the
1036 * IOCB with the provided @ulpstatus and @ulpword4 set to the IOCB commond
1037 * fields.
1038 **/
1039void
1040lpfc_sli_cancel_iocbs(struct lpfc_hba *phba, struct list_head *iocblist,
1041 uint32_t ulpstatus, uint32_t ulpWord4)
1042{
1043 struct lpfc_iocbq *piocb;
1044
1045 while (!list_empty(iocblist)) {
1046 list_remove_head(iocblist, piocb, struct lpfc_iocbq, list);
1047
1048 if (!piocb->iocb_cmpl)
1049 lpfc_sli_release_iocbq(phba, piocb);
1050 else {
1051 piocb->iocb.ulpStatus = ulpstatus;
1052 piocb->iocb.un.ulpWord[4] = ulpWord4;
1053 (piocb->iocb_cmpl) (phba, piocb, piocb);
1054 }
1055 }
1056 return;
1057}
1058
1059/**
James Smart3621a712009-04-06 18:47:14 -04001060 * lpfc_sli_iocb_cmd_type - Get the iocb type
1061 * @iocb_cmnd: iocb command code.
James Smarte59058c2008-08-24 21:49:00 -04001062 *
1063 * This function is called by ring event handler function to get the iocb type.
1064 * This function translates the iocb command to an iocb command type used to
1065 * decide the final disposition of each completed IOCB.
1066 * The function returns
1067 * LPFC_UNKNOWN_IOCB if it is an unsupported iocb
1068 * LPFC_SOL_IOCB if it is a solicited iocb completion
1069 * LPFC_ABORT_IOCB if it is an abort iocb
1070 * LPFC_UNSOL_IOCB if it is an unsolicited iocb
1071 *
1072 * The caller is not required to hold any lock.
1073 **/
dea31012005-04-17 16:05:31 -05001074static lpfc_iocb_type
1075lpfc_sli_iocb_cmd_type(uint8_t iocb_cmnd)
1076{
1077 lpfc_iocb_type type = LPFC_UNKNOWN_IOCB;
1078
1079 if (iocb_cmnd > CMD_MAX_IOCB_CMD)
1080 return 0;
1081
1082 switch (iocb_cmnd) {
1083 case CMD_XMIT_SEQUENCE_CR:
1084 case CMD_XMIT_SEQUENCE_CX:
1085 case CMD_XMIT_BCAST_CN:
1086 case CMD_XMIT_BCAST_CX:
1087 case CMD_ELS_REQUEST_CR:
1088 case CMD_ELS_REQUEST_CX:
1089 case CMD_CREATE_XRI_CR:
1090 case CMD_CREATE_XRI_CX:
1091 case CMD_GET_RPI_CN:
1092 case CMD_XMIT_ELS_RSP_CX:
1093 case CMD_GET_RPI_CR:
1094 case CMD_FCP_IWRITE_CR:
1095 case CMD_FCP_IWRITE_CX:
1096 case CMD_FCP_IREAD_CR:
1097 case CMD_FCP_IREAD_CX:
1098 case CMD_FCP_ICMND_CR:
1099 case CMD_FCP_ICMND_CX:
James Smartf5603512006-12-02 13:35:43 -05001100 case CMD_FCP_TSEND_CX:
1101 case CMD_FCP_TRSP_CX:
1102 case CMD_FCP_TRECEIVE_CX:
1103 case CMD_FCP_AUTO_TRSP_CX:
dea31012005-04-17 16:05:31 -05001104 case CMD_ADAPTER_MSG:
1105 case CMD_ADAPTER_DUMP:
1106 case CMD_XMIT_SEQUENCE64_CR:
1107 case CMD_XMIT_SEQUENCE64_CX:
1108 case CMD_XMIT_BCAST64_CN:
1109 case CMD_XMIT_BCAST64_CX:
1110 case CMD_ELS_REQUEST64_CR:
1111 case CMD_ELS_REQUEST64_CX:
1112 case CMD_FCP_IWRITE64_CR:
1113 case CMD_FCP_IWRITE64_CX:
1114 case CMD_FCP_IREAD64_CR:
1115 case CMD_FCP_IREAD64_CX:
1116 case CMD_FCP_ICMND64_CR:
1117 case CMD_FCP_ICMND64_CX:
James Smartf5603512006-12-02 13:35:43 -05001118 case CMD_FCP_TSEND64_CX:
1119 case CMD_FCP_TRSP64_CX:
1120 case CMD_FCP_TRECEIVE64_CX:
dea31012005-04-17 16:05:31 -05001121 case CMD_GEN_REQUEST64_CR:
1122 case CMD_GEN_REQUEST64_CX:
1123 case CMD_XMIT_ELS_RSP64_CX:
James Smartda0436e2009-05-22 14:51:39 -04001124 case DSSCMD_IWRITE64_CR:
1125 case DSSCMD_IWRITE64_CX:
1126 case DSSCMD_IREAD64_CR:
1127 case DSSCMD_IREAD64_CX:
dea31012005-04-17 16:05:31 -05001128 type = LPFC_SOL_IOCB;
1129 break;
1130 case CMD_ABORT_XRI_CN:
1131 case CMD_ABORT_XRI_CX:
1132 case CMD_CLOSE_XRI_CN:
1133 case CMD_CLOSE_XRI_CX:
1134 case CMD_XRI_ABORTED_CX:
1135 case CMD_ABORT_MXRI64_CN:
James Smart6669f9b2009-10-02 15:16:45 -04001136 case CMD_XMIT_BLS_RSP64_CX:
dea31012005-04-17 16:05:31 -05001137 type = LPFC_ABORT_IOCB;
1138 break;
1139 case CMD_RCV_SEQUENCE_CX:
1140 case CMD_RCV_ELS_REQ_CX:
1141 case CMD_RCV_SEQUENCE64_CX:
1142 case CMD_RCV_ELS_REQ64_CX:
James Smart57127f12007-10-27 13:37:05 -04001143 case CMD_ASYNC_STATUS:
James Smarted957682007-06-17 19:56:37 -05001144 case CMD_IOCB_RCV_SEQ64_CX:
1145 case CMD_IOCB_RCV_ELS64_CX:
1146 case CMD_IOCB_RCV_CONT64_CX:
James Smart3163f722008-02-08 18:50:25 -05001147 case CMD_IOCB_RET_XRI64_CX:
dea31012005-04-17 16:05:31 -05001148 type = LPFC_UNSOL_IOCB;
1149 break;
James Smart3163f722008-02-08 18:50:25 -05001150 case CMD_IOCB_XMIT_MSEQ64_CR:
1151 case CMD_IOCB_XMIT_MSEQ64_CX:
1152 case CMD_IOCB_RCV_SEQ_LIST64_CX:
1153 case CMD_IOCB_RCV_ELS_LIST64_CX:
1154 case CMD_IOCB_CLOSE_EXTENDED_CN:
1155 case CMD_IOCB_ABORT_EXTENDED_CN:
1156 case CMD_IOCB_RET_HBQE64_CN:
1157 case CMD_IOCB_FCP_IBIDIR64_CR:
1158 case CMD_IOCB_FCP_IBIDIR64_CX:
1159 case CMD_IOCB_FCP_ITASKMGT64_CX:
1160 case CMD_IOCB_LOGENTRY_CN:
1161 case CMD_IOCB_LOGENTRY_ASYNC_CN:
1162 printk("%s - Unhandled SLI-3 Command x%x\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07001163 __func__, iocb_cmnd);
James Smart3163f722008-02-08 18:50:25 -05001164 type = LPFC_UNKNOWN_IOCB;
1165 break;
dea31012005-04-17 16:05:31 -05001166 default:
1167 type = LPFC_UNKNOWN_IOCB;
1168 break;
1169 }
1170
1171 return type;
1172}
1173
James Smarte59058c2008-08-24 21:49:00 -04001174/**
James Smart3621a712009-04-06 18:47:14 -04001175 * lpfc_sli_ring_map - Issue config_ring mbox for all rings
James Smarte59058c2008-08-24 21:49:00 -04001176 * @phba: Pointer to HBA context object.
1177 *
1178 * This function is called from SLI initialization code
1179 * to configure every ring of the HBA's SLI interface. The
1180 * caller is not required to hold any lock. This function issues
1181 * a config_ring mailbox command for each ring.
1182 * This function returns zero if successful else returns a negative
1183 * error code.
1184 **/
dea31012005-04-17 16:05:31 -05001185static int
James Smarted957682007-06-17 19:56:37 -05001186lpfc_sli_ring_map(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05001187{
1188 struct lpfc_sli *psli = &phba->sli;
James Smarted957682007-06-17 19:56:37 -05001189 LPFC_MBOXQ_t *pmb;
1190 MAILBOX_t *pmbox;
1191 int i, rc, ret = 0;
dea31012005-04-17 16:05:31 -05001192
James Smarted957682007-06-17 19:56:37 -05001193 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1194 if (!pmb)
1195 return -ENOMEM;
James Smart04c68492009-05-22 14:52:52 -04001196 pmbox = &pmb->u.mb;
James Smarted957682007-06-17 19:56:37 -05001197 phba->link_state = LPFC_INIT_MBX_CMDS;
dea31012005-04-17 16:05:31 -05001198 for (i = 0; i < psli->num_rings; i++) {
dea31012005-04-17 16:05:31 -05001199 lpfc_config_ring(phba, i, pmb);
1200 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
1201 if (rc != MBX_SUCCESS) {
James Smart92d7f7b2007-06-17 19:56:38 -05001202 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04001203 "0446 Adapter failed to init (%d), "
dea31012005-04-17 16:05:31 -05001204 "mbxCmd x%x CFG_RING, mbxStatus x%x, "
1205 "ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04001206 rc, pmbox->mbxCommand,
1207 pmbox->mbxStatus, i);
James Smart2e0fef82007-06-17 19:56:36 -05001208 phba->link_state = LPFC_HBA_ERROR;
James Smarted957682007-06-17 19:56:37 -05001209 ret = -ENXIO;
1210 break;
dea31012005-04-17 16:05:31 -05001211 }
1212 }
James Smarted957682007-06-17 19:56:37 -05001213 mempool_free(pmb, phba->mbox_mem_pool);
1214 return ret;
dea31012005-04-17 16:05:31 -05001215}
1216
James Smarte59058c2008-08-24 21:49:00 -04001217/**
James Smart3621a712009-04-06 18:47:14 -04001218 * lpfc_sli_ringtxcmpl_put - Adds new iocb to the txcmplq
James Smarte59058c2008-08-24 21:49:00 -04001219 * @phba: Pointer to HBA context object.
1220 * @pring: Pointer to driver SLI ring object.
1221 * @piocb: Pointer to the driver iocb object.
1222 *
1223 * This function is called with hbalock held. The function adds the
1224 * new iocb to txcmplq of the given ring. This function always returns
1225 * 0. If this function is called for ELS ring, this function checks if
1226 * there is a vport associated with the ELS command. This function also
1227 * starts els_tmofunc timer if this is an ELS command.
1228 **/
dea31012005-04-17 16:05:31 -05001229static int
James Smart2e0fef82007-06-17 19:56:36 -05001230lpfc_sli_ringtxcmpl_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1231 struct lpfc_iocbq *piocb)
dea31012005-04-17 16:05:31 -05001232{
dea31012005-04-17 16:05:31 -05001233 list_add_tail(&piocb->list, &pring->txcmplq);
James Smart2a9bf3d2010-06-07 15:24:45 -04001234 piocb->iocb_flag |= LPFC_IO_ON_Q;
dea31012005-04-17 16:05:31 -05001235 pring->txcmplq_cnt++;
James Smart2a9bf3d2010-06-07 15:24:45 -04001236 if (pring->txcmplq_cnt > pring->txcmplq_max)
1237 pring->txcmplq_max = pring->txcmplq_cnt;
1238
James Smart92d7f7b2007-06-17 19:56:38 -05001239 if ((unlikely(pring->ringno == LPFC_ELS_RING)) &&
1240 (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
1241 (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
1242 if (!piocb->vport)
1243 BUG();
1244 else
1245 mod_timer(&piocb->vport->els_tmofunc,
1246 jiffies + HZ * (phba->fc_ratov << 1));
1247 }
1248
dea31012005-04-17 16:05:31 -05001249
James Smart2e0fef82007-06-17 19:56:36 -05001250 return 0;
dea31012005-04-17 16:05:31 -05001251}
1252
James Smarte59058c2008-08-24 21:49:00 -04001253/**
James Smart3621a712009-04-06 18:47:14 -04001254 * lpfc_sli_ringtx_get - Get first element of the txq
James Smarte59058c2008-08-24 21:49:00 -04001255 * @phba: Pointer to HBA context object.
1256 * @pring: Pointer to driver SLI ring object.
1257 *
1258 * This function is called with hbalock held to get next
1259 * iocb in txq of the given ring. If there is any iocb in
1260 * the txq, the function returns first iocb in the list after
1261 * removing the iocb from the list, else it returns NULL.
1262 **/
James Smart2a9bf3d2010-06-07 15:24:45 -04001263struct lpfc_iocbq *
James Smart2e0fef82007-06-17 19:56:36 -05001264lpfc_sli_ringtx_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -05001265{
dea31012005-04-17 16:05:31 -05001266 struct lpfc_iocbq *cmd_iocb;
1267
James Smart858c9f62007-06-17 19:56:39 -05001268 list_remove_head((&pring->txq), cmd_iocb, struct lpfc_iocbq, list);
1269 if (cmd_iocb != NULL)
dea31012005-04-17 16:05:31 -05001270 pring->txq_cnt--;
James Smart2e0fef82007-06-17 19:56:36 -05001271 return cmd_iocb;
dea31012005-04-17 16:05:31 -05001272}
1273
James Smarte59058c2008-08-24 21:49:00 -04001274/**
James Smart3621a712009-04-06 18:47:14 -04001275 * lpfc_sli_next_iocb_slot - Get next iocb slot in the ring
James Smarte59058c2008-08-24 21:49:00 -04001276 * @phba: Pointer to HBA context object.
1277 * @pring: Pointer to driver SLI ring object.
1278 *
1279 * This function is called with hbalock held and the caller must post the
1280 * iocb without releasing the lock. If the caller releases the lock,
1281 * iocb slot returned by the function is not guaranteed to be available.
1282 * The function returns pointer to the next available iocb slot if there
1283 * is available slot in the ring, else it returns NULL.
1284 * If the get index of the ring is ahead of the put index, the function
1285 * will post an error attention event to the worker thread to take the
1286 * HBA to offline state.
1287 **/
dea31012005-04-17 16:05:31 -05001288static IOCB_t *
1289lpfc_sli_next_iocb_slot (struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
1290{
James Smart34b02dc2008-08-24 21:49:55 -04001291 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
dea31012005-04-17 16:05:31 -05001292 uint32_t max_cmd_idx = pring->numCiocb;
dea31012005-04-17 16:05:31 -05001293 if ((pring->next_cmdidx == pring->cmdidx) &&
1294 (++pring->next_cmdidx >= max_cmd_idx))
1295 pring->next_cmdidx = 0;
1296
1297 if (unlikely(pring->local_getidx == pring->next_cmdidx)) {
1298
1299 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
1300
1301 if (unlikely(pring->local_getidx >= max_cmd_idx)) {
1302 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001303 "0315 Ring %d issue: portCmdGet %d "
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02001304 "is bigger than cmd ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04001305 pring->ringno,
dea31012005-04-17 16:05:31 -05001306 pring->local_getidx, max_cmd_idx);
1307
James Smart2e0fef82007-06-17 19:56:36 -05001308 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05001309 /*
1310 * All error attention handlers are posted to
1311 * worker thread
1312 */
1313 phba->work_ha |= HA_ERATT;
1314 phba->work_hs = HS_FFER3;
James Smart92d7f7b2007-06-17 19:56:38 -05001315
James Smart5e9d9b82008-06-14 22:52:53 -04001316 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -05001317
1318 return NULL;
1319 }
1320
1321 if (pring->local_getidx == pring->next_cmdidx)
1322 return NULL;
1323 }
1324
James Smarted957682007-06-17 19:56:37 -05001325 return lpfc_cmd_iocb(phba, pring);
dea31012005-04-17 16:05:31 -05001326}
1327
James Smarte59058c2008-08-24 21:49:00 -04001328/**
James Smart3621a712009-04-06 18:47:14 -04001329 * lpfc_sli_next_iotag - Get an iotag for the iocb
James Smarte59058c2008-08-24 21:49:00 -04001330 * @phba: Pointer to HBA context object.
1331 * @iocbq: Pointer to driver iocb object.
1332 *
1333 * This function gets an iotag for the iocb. If there is no unused iotag and
1334 * the iocbq_lookup_len < 0xffff, this function allocates a bigger iotag_lookup
1335 * array and assigns a new iotag.
1336 * The function returns the allocated iotag if successful, else returns zero.
1337 * Zero is not a valid iotag.
1338 * The caller is not required to hold any lock.
1339 **/
James Bottomley604a3e32005-10-29 10:28:33 -05001340uint16_t
James Smart2e0fef82007-06-17 19:56:36 -05001341lpfc_sli_next_iotag(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
dea31012005-04-17 16:05:31 -05001342{
James Smart2e0fef82007-06-17 19:56:36 -05001343 struct lpfc_iocbq **new_arr;
1344 struct lpfc_iocbq **old_arr;
James Bottomley604a3e32005-10-29 10:28:33 -05001345 size_t new_len;
1346 struct lpfc_sli *psli = &phba->sli;
1347 uint16_t iotag;
dea31012005-04-17 16:05:31 -05001348
James Smart2e0fef82007-06-17 19:56:36 -05001349 spin_lock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001350 iotag = psli->last_iotag;
1351 if(++iotag < psli->iocbq_lookup_len) {
1352 psli->last_iotag = iotag;
1353 psli->iocbq_lookup[iotag] = iocbq;
James Smart2e0fef82007-06-17 19:56:36 -05001354 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001355 iocbq->iotag = iotag;
1356 return iotag;
James Smart2e0fef82007-06-17 19:56:36 -05001357 } else if (psli->iocbq_lookup_len < (0xffff
James Bottomley604a3e32005-10-29 10:28:33 -05001358 - LPFC_IOCBQ_LOOKUP_INCREMENT)) {
1359 new_len = psli->iocbq_lookup_len + LPFC_IOCBQ_LOOKUP_INCREMENT;
James Smart2e0fef82007-06-17 19:56:36 -05001360 spin_unlock_irq(&phba->hbalock);
1361 new_arr = kzalloc(new_len * sizeof (struct lpfc_iocbq *),
James Bottomley604a3e32005-10-29 10:28:33 -05001362 GFP_KERNEL);
1363 if (new_arr) {
James Smart2e0fef82007-06-17 19:56:36 -05001364 spin_lock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001365 old_arr = psli->iocbq_lookup;
1366 if (new_len <= psli->iocbq_lookup_len) {
1367 /* highly unprobable case */
1368 kfree(new_arr);
1369 iotag = psli->last_iotag;
1370 if(++iotag < psli->iocbq_lookup_len) {
1371 psli->last_iotag = iotag;
1372 psli->iocbq_lookup[iotag] = iocbq;
James Smart2e0fef82007-06-17 19:56:36 -05001373 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001374 iocbq->iotag = iotag;
1375 return iotag;
1376 }
James Smart2e0fef82007-06-17 19:56:36 -05001377 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001378 return 0;
1379 }
1380 if (psli->iocbq_lookup)
1381 memcpy(new_arr, old_arr,
1382 ((psli->last_iotag + 1) *
James Smart311464e2007-08-02 11:10:37 -04001383 sizeof (struct lpfc_iocbq *)));
James Bottomley604a3e32005-10-29 10:28:33 -05001384 psli->iocbq_lookup = new_arr;
1385 psli->iocbq_lookup_len = new_len;
1386 psli->last_iotag = iotag;
1387 psli->iocbq_lookup[iotag] = iocbq;
James Smart2e0fef82007-06-17 19:56:36 -05001388 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001389 iocbq->iotag = iotag;
1390 kfree(old_arr);
1391 return iotag;
1392 }
James Smart8f6d98d2006-08-01 07:34:00 -04001393 } else
James Smart2e0fef82007-06-17 19:56:36 -05001394 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001395
James Smartbc739052010-08-04 16:11:18 -04001396 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001397 "0318 Failed to allocate IOTAG.last IOTAG is %d\n",
1398 psli->last_iotag);
dea31012005-04-17 16:05:31 -05001399
James Bottomley604a3e32005-10-29 10:28:33 -05001400 return 0;
dea31012005-04-17 16:05:31 -05001401}
1402
James Smarte59058c2008-08-24 21:49:00 -04001403/**
James Smart3621a712009-04-06 18:47:14 -04001404 * lpfc_sli_submit_iocb - Submit an iocb to the firmware
James Smarte59058c2008-08-24 21:49:00 -04001405 * @phba: Pointer to HBA context object.
1406 * @pring: Pointer to driver SLI ring object.
1407 * @iocb: Pointer to iocb slot in the ring.
1408 * @nextiocb: Pointer to driver iocb object which need to be
1409 * posted to firmware.
1410 *
1411 * This function is called with hbalock held to post a new iocb to
1412 * the firmware. This function copies the new iocb to ring iocb slot and
1413 * updates the ring pointers. It adds the new iocb to txcmplq if there is
1414 * a completion call back for this iocb else the function will free the
1415 * iocb object.
1416 **/
dea31012005-04-17 16:05:31 -05001417static void
1418lpfc_sli_submit_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1419 IOCB_t *iocb, struct lpfc_iocbq *nextiocb)
1420{
1421 /*
James Bottomley604a3e32005-10-29 10:28:33 -05001422 * Set up an iotag
dea31012005-04-17 16:05:31 -05001423 */
James Bottomley604a3e32005-10-29 10:28:33 -05001424 nextiocb->iocb.ulpIoTag = (nextiocb->iocb_cmpl) ? nextiocb->iotag : 0;
dea31012005-04-17 16:05:31 -05001425
James Smarte2a0a9d2008-12-04 22:40:02 -05001426
James Smarta58cbd52007-08-02 11:09:43 -04001427 if (pring->ringno == LPFC_ELS_RING) {
1428 lpfc_debugfs_slow_ring_trc(phba,
1429 "IOCB cmd ring: wd4:x%08x wd6:x%08x wd7:x%08x",
1430 *(((uint32_t *) &nextiocb->iocb) + 4),
1431 *(((uint32_t *) &nextiocb->iocb) + 6),
1432 *(((uint32_t *) &nextiocb->iocb) + 7));
1433 }
1434
dea31012005-04-17 16:05:31 -05001435 /*
1436 * Issue iocb command to adapter
1437 */
James Smart92d7f7b2007-06-17 19:56:38 -05001438 lpfc_sli_pcimem_bcopy(&nextiocb->iocb, iocb, phba->iocb_cmd_size);
dea31012005-04-17 16:05:31 -05001439 wmb();
1440 pring->stats.iocb_cmd++;
1441
1442 /*
1443 * If there is no completion routine to call, we can release the
1444 * IOCB buffer back right now. For IOCBs, like QUE_RING_BUF,
1445 * that have no rsp ring completion, iocb_cmpl MUST be NULL.
1446 */
1447 if (nextiocb->iocb_cmpl)
1448 lpfc_sli_ringtxcmpl_put(phba, pring, nextiocb);
James Bottomley604a3e32005-10-29 10:28:33 -05001449 else
James Smart2e0fef82007-06-17 19:56:36 -05001450 __lpfc_sli_release_iocbq(phba, nextiocb);
dea31012005-04-17 16:05:31 -05001451
1452 /*
1453 * Let the HBA know what IOCB slot will be the next one the
1454 * driver will put a command into.
1455 */
1456 pring->cmdidx = pring->next_cmdidx;
James Smarted957682007-06-17 19:56:37 -05001457 writel(pring->cmdidx, &phba->host_gp[pring->ringno].cmdPutInx);
dea31012005-04-17 16:05:31 -05001458}
1459
James Smarte59058c2008-08-24 21:49:00 -04001460/**
James Smart3621a712009-04-06 18:47:14 -04001461 * lpfc_sli_update_full_ring - Update the chip attention register
James Smarte59058c2008-08-24 21:49:00 -04001462 * @phba: Pointer to HBA context object.
1463 * @pring: Pointer to driver SLI ring object.
1464 *
1465 * The caller is not required to hold any lock for calling this function.
1466 * This function updates the chip attention bits for the ring to inform firmware
1467 * that there are pending work to be done for this ring and requests an
1468 * interrupt when there is space available in the ring. This function is
1469 * called when the driver is unable to post more iocbs to the ring due
1470 * to unavailability of space in the ring.
1471 **/
dea31012005-04-17 16:05:31 -05001472static void
James Smart2e0fef82007-06-17 19:56:36 -05001473lpfc_sli_update_full_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -05001474{
1475 int ringno = pring->ringno;
1476
1477 pring->flag |= LPFC_CALL_RING_AVAILABLE;
1478
1479 wmb();
1480
1481 /*
1482 * Set ring 'ringno' to SET R0CE_REQ in Chip Att register.
1483 * The HBA will tell us when an IOCB entry is available.
1484 */
1485 writel((CA_R0ATT|CA_R0CE_REQ) << (ringno*4), phba->CAregaddr);
1486 readl(phba->CAregaddr); /* flush */
1487
1488 pring->stats.iocb_cmd_full++;
1489}
1490
James Smarte59058c2008-08-24 21:49:00 -04001491/**
James Smart3621a712009-04-06 18:47:14 -04001492 * lpfc_sli_update_ring - Update 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 * This function updates the chip attention register bit for the
1497 * given ring to inform HBA that there is more work to be done
1498 * in this ring. The caller is not required to hold any lock.
1499 **/
dea31012005-04-17 16:05:31 -05001500static void
James Smart2e0fef82007-06-17 19:56:36 -05001501lpfc_sli_update_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -05001502{
1503 int ringno = pring->ringno;
1504
1505 /*
1506 * Tell the HBA that there is work to do in this ring.
1507 */
James Smart34b02dc2008-08-24 21:49:55 -04001508 if (!(phba->sli3_options & LPFC_SLI3_CRP_ENABLED)) {
1509 wmb();
1510 writel(CA_R0ATT << (ringno * 4), phba->CAregaddr);
1511 readl(phba->CAregaddr); /* flush */
1512 }
dea31012005-04-17 16:05:31 -05001513}
1514
James Smarte59058c2008-08-24 21:49:00 -04001515/**
James Smart3621a712009-04-06 18:47:14 -04001516 * lpfc_sli_resume_iocb - Process iocbs in the txq
James Smarte59058c2008-08-24 21:49:00 -04001517 * @phba: Pointer to HBA context object.
1518 * @pring: Pointer to driver SLI ring object.
1519 *
1520 * This function is called with hbalock held to post pending iocbs
1521 * in the txq to the firmware. This function is called when driver
1522 * detects space available in the ring.
1523 **/
dea31012005-04-17 16:05:31 -05001524static void
James Smart2e0fef82007-06-17 19:56:36 -05001525lpfc_sli_resume_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -05001526{
1527 IOCB_t *iocb;
1528 struct lpfc_iocbq *nextiocb;
1529
1530 /*
1531 * Check to see if:
1532 * (a) there is anything on the txq to send
1533 * (b) link is up
1534 * (c) link attention events can be processed (fcp ring only)
1535 * (d) IOCB processing is not blocked by the outstanding mbox command.
1536 */
1537 if (pring->txq_cnt &&
James Smart2e0fef82007-06-17 19:56:36 -05001538 lpfc_is_link_up(phba) &&
dea31012005-04-17 16:05:31 -05001539 (pring->ringno != phba->sli.fcp_ring ||
James Smart0b727fe2007-10-27 13:37:25 -04001540 phba->sli.sli_flag & LPFC_PROCESS_LA)) {
dea31012005-04-17 16:05:31 -05001541
1542 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
1543 (nextiocb = lpfc_sli_ringtx_get(phba, pring)))
1544 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
1545
1546 if (iocb)
1547 lpfc_sli_update_ring(phba, pring);
1548 else
1549 lpfc_sli_update_full_ring(phba, pring);
1550 }
1551
1552 return;
1553}
1554
James Smarte59058c2008-08-24 21:49:00 -04001555/**
James Smart3621a712009-04-06 18:47:14 -04001556 * lpfc_sli_next_hbq_slot - Get next hbq entry for the HBQ
James Smarte59058c2008-08-24 21:49:00 -04001557 * @phba: Pointer to HBA context object.
1558 * @hbqno: HBQ number.
1559 *
1560 * This function is called with hbalock held to get the next
1561 * available slot for the given HBQ. If there is free slot
1562 * available for the HBQ it will return pointer to the next available
1563 * HBQ entry else it will return NULL.
1564 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01001565static struct lpfc_hbq_entry *
James Smarted957682007-06-17 19:56:37 -05001566lpfc_sli_next_hbq_slot(struct lpfc_hba *phba, uint32_t hbqno)
1567{
1568 struct hbq_s *hbqp = &phba->hbqs[hbqno];
1569
1570 if (hbqp->next_hbqPutIdx == hbqp->hbqPutIdx &&
1571 ++hbqp->next_hbqPutIdx >= hbqp->entry_count)
1572 hbqp->next_hbqPutIdx = 0;
1573
1574 if (unlikely(hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)) {
James Smart92d7f7b2007-06-17 19:56:38 -05001575 uint32_t raw_index = phba->hbq_get[hbqno];
James Smarted957682007-06-17 19:56:37 -05001576 uint32_t getidx = le32_to_cpu(raw_index);
1577
1578 hbqp->local_hbqGetIdx = getidx;
1579
1580 if (unlikely(hbqp->local_hbqGetIdx >= hbqp->entry_count)) {
1581 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -05001582 LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04001583 "1802 HBQ %d: local_hbqGetIdx "
James Smarted957682007-06-17 19:56:37 -05001584 "%u is > than hbqp->entry_count %u\n",
James Smarte8b62012007-08-02 11:10:09 -04001585 hbqno, hbqp->local_hbqGetIdx,
James Smarted957682007-06-17 19:56:37 -05001586 hbqp->entry_count);
1587
1588 phba->link_state = LPFC_HBA_ERROR;
1589 return NULL;
1590 }
1591
1592 if (hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)
1593 return NULL;
1594 }
1595
James Smart51ef4c22007-08-02 11:10:31 -04001596 return (struct lpfc_hbq_entry *) phba->hbqs[hbqno].hbq_virt +
1597 hbqp->hbqPutIdx;
James Smarted957682007-06-17 19:56:37 -05001598}
1599
James Smarte59058c2008-08-24 21:49:00 -04001600/**
James Smart3621a712009-04-06 18:47:14 -04001601 * lpfc_sli_hbqbuf_free_all - Free all the hbq buffers
James Smarte59058c2008-08-24 21:49:00 -04001602 * @phba: Pointer to HBA context object.
1603 *
1604 * This function is called with no lock held to free all the
1605 * hbq buffers while uninitializing the SLI interface. It also
1606 * frees the HBQ buffers returned by the firmware but not yet
1607 * processed by the upper layers.
1608 **/
James Smarted957682007-06-17 19:56:37 -05001609void
1610lpfc_sli_hbqbuf_free_all(struct lpfc_hba *phba)
1611{
James Smart92d7f7b2007-06-17 19:56:38 -05001612 struct lpfc_dmabuf *dmabuf, *next_dmabuf;
1613 struct hbq_dmabuf *hbq_buf;
James Smart3163f722008-02-08 18:50:25 -05001614 unsigned long flags;
James Smart51ef4c22007-08-02 11:10:31 -04001615 int i, hbq_count;
James Smart3163f722008-02-08 18:50:25 -05001616 uint32_t hbqno;
James Smarted957682007-06-17 19:56:37 -05001617
James Smart51ef4c22007-08-02 11:10:31 -04001618 hbq_count = lpfc_sli_hbq_count();
James Smarted957682007-06-17 19:56:37 -05001619 /* Return all memory used by all HBQs */
James Smart3163f722008-02-08 18:50:25 -05001620 spin_lock_irqsave(&phba->hbalock, flags);
James Smart51ef4c22007-08-02 11:10:31 -04001621 for (i = 0; i < hbq_count; ++i) {
1622 list_for_each_entry_safe(dmabuf, next_dmabuf,
1623 &phba->hbqs[i].hbq_buffer_list, list) {
1624 hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
1625 list_del(&hbq_buf->dbuf.list);
1626 (phba->hbqs[i].hbq_free_buffer)(phba, hbq_buf);
1627 }
James Smarta8adb832007-10-27 13:37:53 -04001628 phba->hbqs[i].buffer_count = 0;
James Smarted957682007-06-17 19:56:37 -05001629 }
James Smart3163f722008-02-08 18:50:25 -05001630 /* Return all HBQ buffer that are in-fly */
James Smart3772a992009-05-22 14:50:54 -04001631 list_for_each_entry_safe(dmabuf, next_dmabuf, &phba->rb_pend_list,
1632 list) {
James Smart3163f722008-02-08 18:50:25 -05001633 hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
1634 list_del(&hbq_buf->dbuf.list);
1635 if (hbq_buf->tag == -1) {
1636 (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
1637 (phba, hbq_buf);
1638 } else {
1639 hbqno = hbq_buf->tag >> 16;
1640 if (hbqno >= LPFC_MAX_HBQS)
1641 (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
1642 (phba, hbq_buf);
1643 else
1644 (phba->hbqs[hbqno].hbq_free_buffer)(phba,
1645 hbq_buf);
1646 }
1647 }
1648
1649 /* Mark the HBQs not in use */
1650 phba->hbq_in_use = 0;
1651 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smarted957682007-06-17 19:56:37 -05001652}
1653
James Smarte59058c2008-08-24 21:49:00 -04001654/**
James Smart3621a712009-04-06 18:47:14 -04001655 * lpfc_sli_hbq_to_firmware - Post the hbq buffer to firmware
James Smarte59058c2008-08-24 21:49:00 -04001656 * @phba: Pointer to HBA context object.
1657 * @hbqno: HBQ number.
1658 * @hbq_buf: Pointer to HBQ buffer.
1659 *
1660 * This function is called with the hbalock held to post a
1661 * hbq buffer to the firmware. If the function finds an empty
1662 * slot in the HBQ, it will post the buffer. The function will return
1663 * pointer to the hbq entry if it successfully post the buffer
1664 * else it will return NULL.
1665 **/
James Smart3772a992009-05-22 14:50:54 -04001666static int
James Smarted957682007-06-17 19:56:37 -05001667lpfc_sli_hbq_to_firmware(struct lpfc_hba *phba, uint32_t hbqno,
James Smart92d7f7b2007-06-17 19:56:38 -05001668 struct hbq_dmabuf *hbq_buf)
James Smarted957682007-06-17 19:56:37 -05001669{
James Smart3772a992009-05-22 14:50:54 -04001670 return phba->lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buf);
1671}
1672
1673/**
1674 * lpfc_sli_hbq_to_firmware_s3 - Post the hbq buffer to SLI3 firmware
1675 * @phba: Pointer to HBA context object.
1676 * @hbqno: HBQ number.
1677 * @hbq_buf: Pointer to HBQ buffer.
1678 *
1679 * This function is called with the hbalock held to post a hbq buffer to the
1680 * firmware. If the function finds an empty slot in the HBQ, it will post the
1681 * buffer and place it on the hbq_buffer_list. The function will return zero if
1682 * it successfully post the buffer else it will return an error.
1683 **/
1684static int
1685lpfc_sli_hbq_to_firmware_s3(struct lpfc_hba *phba, uint32_t hbqno,
1686 struct hbq_dmabuf *hbq_buf)
1687{
James Smarted957682007-06-17 19:56:37 -05001688 struct lpfc_hbq_entry *hbqe;
James Smart92d7f7b2007-06-17 19:56:38 -05001689 dma_addr_t physaddr = hbq_buf->dbuf.phys;
James Smarted957682007-06-17 19:56:37 -05001690
1691 /* Get next HBQ entry slot to use */
1692 hbqe = lpfc_sli_next_hbq_slot(phba, hbqno);
1693 if (hbqe) {
1694 struct hbq_s *hbqp = &phba->hbqs[hbqno];
1695
James Smart92d7f7b2007-06-17 19:56:38 -05001696 hbqe->bde.addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
1697 hbqe->bde.addrLow = le32_to_cpu(putPaddrLow(physaddr));
James Smart51ef4c22007-08-02 11:10:31 -04001698 hbqe->bde.tus.f.bdeSize = hbq_buf->size;
James Smarted957682007-06-17 19:56:37 -05001699 hbqe->bde.tus.f.bdeFlags = 0;
James Smart92d7f7b2007-06-17 19:56:38 -05001700 hbqe->bde.tus.w = le32_to_cpu(hbqe->bde.tus.w);
1701 hbqe->buffer_tag = le32_to_cpu(hbq_buf->tag);
1702 /* Sync SLIM */
James Smarted957682007-06-17 19:56:37 -05001703 hbqp->hbqPutIdx = hbqp->next_hbqPutIdx;
1704 writel(hbqp->hbqPutIdx, phba->hbq_put + hbqno);
James Smart92d7f7b2007-06-17 19:56:38 -05001705 /* flush */
James Smarted957682007-06-17 19:56:37 -05001706 readl(phba->hbq_put + hbqno);
James Smart51ef4c22007-08-02 11:10:31 -04001707 list_add_tail(&hbq_buf->dbuf.list, &hbqp->hbq_buffer_list);
James Smart3772a992009-05-22 14:50:54 -04001708 return 0;
1709 } else
1710 return -ENOMEM;
James Smarted957682007-06-17 19:56:37 -05001711}
1712
James Smart4f774512009-05-22 14:52:35 -04001713/**
1714 * lpfc_sli_hbq_to_firmware_s4 - Post the hbq buffer to SLI4 firmware
1715 * @phba: Pointer to HBA context object.
1716 * @hbqno: HBQ number.
1717 * @hbq_buf: Pointer to HBQ buffer.
1718 *
1719 * This function is called with the hbalock held to post an RQE to the SLI4
1720 * firmware. If able to post the RQE to the RQ it will queue the hbq entry to
1721 * the hbq_buffer_list and return zero, otherwise it will return an error.
1722 **/
1723static int
1724lpfc_sli_hbq_to_firmware_s4(struct lpfc_hba *phba, uint32_t hbqno,
1725 struct hbq_dmabuf *hbq_buf)
1726{
1727 int rc;
1728 struct lpfc_rqe hrqe;
1729 struct lpfc_rqe drqe;
1730
1731 hrqe.address_lo = putPaddrLow(hbq_buf->hbuf.phys);
1732 hrqe.address_hi = putPaddrHigh(hbq_buf->hbuf.phys);
1733 drqe.address_lo = putPaddrLow(hbq_buf->dbuf.phys);
1734 drqe.address_hi = putPaddrHigh(hbq_buf->dbuf.phys);
1735 rc = lpfc_sli4_rq_put(phba->sli4_hba.hdr_rq, phba->sli4_hba.dat_rq,
1736 &hrqe, &drqe);
1737 if (rc < 0)
1738 return rc;
1739 hbq_buf->tag = rc;
1740 list_add_tail(&hbq_buf->dbuf.list, &phba->hbqs[hbqno].hbq_buffer_list);
1741 return 0;
1742}
1743
James Smarte59058c2008-08-24 21:49:00 -04001744/* HBQ for ELS and CT traffic. */
James Smart92d7f7b2007-06-17 19:56:38 -05001745static struct lpfc_hbq_init lpfc_els_hbq = {
1746 .rn = 1,
James Smartdef9c7a2009-12-21 17:02:28 -05001747 .entry_count = 256,
James Smart92d7f7b2007-06-17 19:56:38 -05001748 .mask_count = 0,
1749 .profile = 0,
James Smart51ef4c22007-08-02 11:10:31 -04001750 .ring_mask = (1 << LPFC_ELS_RING),
James Smart92d7f7b2007-06-17 19:56:38 -05001751 .buffer_count = 0,
James Smarta257bf92009-04-06 18:48:10 -04001752 .init_count = 40,
1753 .add_count = 40,
James Smart92d7f7b2007-06-17 19:56:38 -05001754};
James Smarted957682007-06-17 19:56:37 -05001755
James Smarte59058c2008-08-24 21:49:00 -04001756/* HBQ for the extra ring if needed */
James Smart51ef4c22007-08-02 11:10:31 -04001757static struct lpfc_hbq_init lpfc_extra_hbq = {
1758 .rn = 1,
1759 .entry_count = 200,
1760 .mask_count = 0,
1761 .profile = 0,
1762 .ring_mask = (1 << LPFC_EXTRA_RING),
1763 .buffer_count = 0,
1764 .init_count = 0,
1765 .add_count = 5,
1766};
1767
James Smarte59058c2008-08-24 21:49:00 -04001768/* Array of HBQs */
James Smart78b2d852007-08-02 11:10:21 -04001769struct lpfc_hbq_init *lpfc_hbq_defs[] = {
James Smart92d7f7b2007-06-17 19:56:38 -05001770 &lpfc_els_hbq,
James Smart51ef4c22007-08-02 11:10:31 -04001771 &lpfc_extra_hbq,
James Smart92d7f7b2007-06-17 19:56:38 -05001772};
1773
James Smarte59058c2008-08-24 21:49:00 -04001774/**
James Smart3621a712009-04-06 18:47:14 -04001775 * lpfc_sli_hbqbuf_fill_hbqs - Post more hbq buffers to HBQ
James Smarte59058c2008-08-24 21:49:00 -04001776 * @phba: Pointer to HBA context object.
1777 * @hbqno: HBQ number.
1778 * @count: Number of HBQ buffers to be posted.
1779 *
James Smartd7c255b2008-08-24 21:50:00 -04001780 * This function is called with no lock held to post more hbq buffers to the
1781 * given HBQ. The function returns the number of HBQ buffers successfully
1782 * posted.
James Smarte59058c2008-08-24 21:49:00 -04001783 **/
James Smart311464e2007-08-02 11:10:37 -04001784static int
James Smart92d7f7b2007-06-17 19:56:38 -05001785lpfc_sli_hbqbuf_fill_hbqs(struct lpfc_hba *phba, uint32_t hbqno, uint32_t count)
1786{
James Smartd7c255b2008-08-24 21:50:00 -04001787 uint32_t i, posted = 0;
James Smart3163f722008-02-08 18:50:25 -05001788 unsigned long flags;
James Smart92d7f7b2007-06-17 19:56:38 -05001789 struct hbq_dmabuf *hbq_buffer;
James Smartd7c255b2008-08-24 21:50:00 -04001790 LIST_HEAD(hbq_buf_list);
Matthew Wilcoxeafe1df2008-02-21 05:44:33 -07001791 if (!phba->hbqs[hbqno].hbq_alloc_buffer)
James Smart51ef4c22007-08-02 11:10:31 -04001792 return 0;
James Smart51ef4c22007-08-02 11:10:31 -04001793
James Smartd7c255b2008-08-24 21:50:00 -04001794 if ((phba->hbqs[hbqno].buffer_count + count) >
1795 lpfc_hbq_defs[hbqno]->entry_count)
1796 count = lpfc_hbq_defs[hbqno]->entry_count -
1797 phba->hbqs[hbqno].buffer_count;
1798 if (!count)
1799 return 0;
1800 /* Allocate HBQ entries */
1801 for (i = 0; i < count; i++) {
1802 hbq_buffer = (phba->hbqs[hbqno].hbq_alloc_buffer)(phba);
1803 if (!hbq_buffer)
1804 break;
1805 list_add_tail(&hbq_buffer->dbuf.list, &hbq_buf_list);
1806 }
James Smart3163f722008-02-08 18:50:25 -05001807 /* Check whether HBQ is still in use */
1808 spin_lock_irqsave(&phba->hbalock, flags);
Matthew Wilcoxeafe1df2008-02-21 05:44:33 -07001809 if (!phba->hbq_in_use)
James Smartd7c255b2008-08-24 21:50:00 -04001810 goto err;
1811 while (!list_empty(&hbq_buf_list)) {
1812 list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
1813 dbuf.list);
1814 hbq_buffer->tag = (phba->hbqs[hbqno].buffer_count |
1815 (hbqno << 16));
James Smart3772a992009-05-22 14:50:54 -04001816 if (!lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer)) {
James Smarta8adb832007-10-27 13:37:53 -04001817 phba->hbqs[hbqno].buffer_count++;
James Smartd7c255b2008-08-24 21:50:00 -04001818 posted++;
1819 } else
James Smart51ef4c22007-08-02 11:10:31 -04001820 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
James Smart92d7f7b2007-06-17 19:56:38 -05001821 }
James Smart3163f722008-02-08 18:50:25 -05001822 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smartd7c255b2008-08-24 21:50:00 -04001823 return posted;
1824err:
1825 spin_unlock_irqrestore(&phba->hbalock, flags);
1826 while (!list_empty(&hbq_buf_list)) {
1827 list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
1828 dbuf.list);
1829 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
1830 }
James Smart92d7f7b2007-06-17 19:56:38 -05001831 return 0;
James Smarted957682007-06-17 19:56:37 -05001832}
1833
James Smarte59058c2008-08-24 21:49:00 -04001834/**
James Smart3621a712009-04-06 18:47:14 -04001835 * lpfc_sli_hbqbuf_add_hbqs - Post more HBQ buffers to firmware
James Smarte59058c2008-08-24 21:49:00 -04001836 * @phba: Pointer to HBA context object.
1837 * @qno: HBQ number.
1838 *
1839 * This function posts more buffers to the HBQ. This function
James Smartd7c255b2008-08-24 21:50:00 -04001840 * is called with no lock held. The function returns the number of HBQ entries
1841 * successfully allocated.
James Smarte59058c2008-08-24 21:49:00 -04001842 **/
James Smarted957682007-06-17 19:56:37 -05001843int
James Smart92d7f7b2007-06-17 19:56:38 -05001844lpfc_sli_hbqbuf_add_hbqs(struct lpfc_hba *phba, uint32_t qno)
James Smarted957682007-06-17 19:56:37 -05001845{
James Smartdef9c7a2009-12-21 17:02:28 -05001846 if (phba->sli_rev == LPFC_SLI_REV4)
1847 return 0;
1848 else
1849 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
1850 lpfc_hbq_defs[qno]->add_count);
James Smarted957682007-06-17 19:56:37 -05001851}
1852
James Smarte59058c2008-08-24 21:49:00 -04001853/**
James Smart3621a712009-04-06 18:47:14 -04001854 * lpfc_sli_hbqbuf_init_hbqs - Post initial buffers to the HBQ
James Smarte59058c2008-08-24 21:49:00 -04001855 * @phba: Pointer to HBA context object.
1856 * @qno: HBQ queue number.
1857 *
1858 * This function is called from SLI initialization code path with
1859 * no lock held to post initial HBQ buffers to firmware. The
James Smartd7c255b2008-08-24 21:50:00 -04001860 * function returns the number of HBQ entries successfully allocated.
James Smarte59058c2008-08-24 21:49:00 -04001861 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01001862static int
James Smart92d7f7b2007-06-17 19:56:38 -05001863lpfc_sli_hbqbuf_init_hbqs(struct lpfc_hba *phba, uint32_t qno)
James Smarted957682007-06-17 19:56:37 -05001864{
James Smartdef9c7a2009-12-21 17:02:28 -05001865 if (phba->sli_rev == LPFC_SLI_REV4)
1866 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
James Smart73d91e52011-10-10 21:32:10 -04001867 lpfc_hbq_defs[qno]->entry_count);
James Smartdef9c7a2009-12-21 17:02:28 -05001868 else
1869 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
1870 lpfc_hbq_defs[qno]->init_count);
James Smarted957682007-06-17 19:56:37 -05001871}
1872
James Smarte59058c2008-08-24 21:49:00 -04001873/**
James Smart3772a992009-05-22 14:50:54 -04001874 * lpfc_sli_hbqbuf_get - Remove the first hbq off of an hbq list
1875 * @phba: Pointer to HBA context object.
1876 * @hbqno: HBQ number.
1877 *
1878 * This function removes the first hbq buffer on an hbq list and returns a
1879 * pointer to that buffer. If it finds no buffers on the list it returns NULL.
1880 **/
1881static struct hbq_dmabuf *
1882lpfc_sli_hbqbuf_get(struct list_head *rb_list)
1883{
1884 struct lpfc_dmabuf *d_buf;
1885
1886 list_remove_head(rb_list, d_buf, struct lpfc_dmabuf, list);
1887 if (!d_buf)
1888 return NULL;
1889 return container_of(d_buf, struct hbq_dmabuf, dbuf);
1890}
1891
1892/**
James Smart3621a712009-04-06 18:47:14 -04001893 * lpfc_sli_hbqbuf_find - Find the hbq buffer associated with a tag
James Smarte59058c2008-08-24 21:49:00 -04001894 * @phba: Pointer to HBA context object.
1895 * @tag: Tag of the hbq buffer.
1896 *
1897 * This function is called with hbalock held. This function searches
1898 * for the hbq buffer associated with the given tag in the hbq buffer
1899 * list. If it finds the hbq buffer, it returns the hbq_buffer other wise
1900 * it returns NULL.
1901 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01001902static struct hbq_dmabuf *
James Smarted957682007-06-17 19:56:37 -05001903lpfc_sli_hbqbuf_find(struct lpfc_hba *phba, uint32_t tag)
1904{
James Smart92d7f7b2007-06-17 19:56:38 -05001905 struct lpfc_dmabuf *d_buf;
1906 struct hbq_dmabuf *hbq_buf;
James Smart51ef4c22007-08-02 11:10:31 -04001907 uint32_t hbqno;
James Smarted957682007-06-17 19:56:37 -05001908
James Smart51ef4c22007-08-02 11:10:31 -04001909 hbqno = tag >> 16;
Jesper Juhla0a74e452007-08-09 20:47:15 +02001910 if (hbqno >= LPFC_MAX_HBQS)
James Smart51ef4c22007-08-02 11:10:31 -04001911 return NULL;
1912
James Smart3772a992009-05-22 14:50:54 -04001913 spin_lock_irq(&phba->hbalock);
James Smart51ef4c22007-08-02 11:10:31 -04001914 list_for_each_entry(d_buf, &phba->hbqs[hbqno].hbq_buffer_list, list) {
James Smart92d7f7b2007-06-17 19:56:38 -05001915 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
James Smart51ef4c22007-08-02 11:10:31 -04001916 if (hbq_buf->tag == tag) {
James Smart3772a992009-05-22 14:50:54 -04001917 spin_unlock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -05001918 return hbq_buf;
James Smarted957682007-06-17 19:56:37 -05001919 }
1920 }
James Smart3772a992009-05-22 14:50:54 -04001921 spin_unlock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -05001922 lpfc_printf_log(phba, KERN_ERR, LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04001923 "1803 Bad hbq tag. Data: x%x x%x\n",
James Smarta8adb832007-10-27 13:37:53 -04001924 tag, phba->hbqs[tag >> 16].buffer_count);
James Smart92d7f7b2007-06-17 19:56:38 -05001925 return NULL;
James Smarted957682007-06-17 19:56:37 -05001926}
1927
James Smarte59058c2008-08-24 21:49:00 -04001928/**
James Smart3621a712009-04-06 18:47:14 -04001929 * lpfc_sli_free_hbq - Give back the hbq buffer to firmware
James Smarte59058c2008-08-24 21:49:00 -04001930 * @phba: Pointer to HBA context object.
1931 * @hbq_buffer: Pointer to HBQ buffer.
1932 *
1933 * This function is called with hbalock. This function gives back
1934 * the hbq buffer to firmware. If the HBQ does not have space to
1935 * post the buffer, it will free the buffer.
1936 **/
James Smarted957682007-06-17 19:56:37 -05001937void
James Smart51ef4c22007-08-02 11:10:31 -04001938lpfc_sli_free_hbq(struct lpfc_hba *phba, struct hbq_dmabuf *hbq_buffer)
James Smarted957682007-06-17 19:56:37 -05001939{
1940 uint32_t hbqno;
1941
James Smart51ef4c22007-08-02 11:10:31 -04001942 if (hbq_buffer) {
1943 hbqno = hbq_buffer->tag >> 16;
James Smart3772a992009-05-22 14:50:54 -04001944 if (lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer))
James Smart51ef4c22007-08-02 11:10:31 -04001945 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
James Smarted957682007-06-17 19:56:37 -05001946 }
1947}
1948
James Smarte59058c2008-08-24 21:49:00 -04001949/**
James Smart3621a712009-04-06 18:47:14 -04001950 * lpfc_sli_chk_mbx_command - Check if the mailbox is a legitimate mailbox
James Smarte59058c2008-08-24 21:49:00 -04001951 * @mbxCommand: mailbox command code.
1952 *
1953 * This function is called by the mailbox event handler function to verify
1954 * that the completed mailbox command is a legitimate mailbox command. If the
1955 * completed mailbox is not known to the function, it will return MBX_SHUTDOWN
1956 * and the mailbox event handler will take the HBA offline.
1957 **/
dea31012005-04-17 16:05:31 -05001958static int
1959lpfc_sli_chk_mbx_command(uint8_t mbxCommand)
1960{
1961 uint8_t ret;
1962
1963 switch (mbxCommand) {
1964 case MBX_LOAD_SM:
1965 case MBX_READ_NV:
1966 case MBX_WRITE_NV:
James Smarta8adb832007-10-27 13:37:53 -04001967 case MBX_WRITE_VPARMS:
dea31012005-04-17 16:05:31 -05001968 case MBX_RUN_BIU_DIAG:
1969 case MBX_INIT_LINK:
1970 case MBX_DOWN_LINK:
1971 case MBX_CONFIG_LINK:
1972 case MBX_CONFIG_RING:
1973 case MBX_RESET_RING:
1974 case MBX_READ_CONFIG:
1975 case MBX_READ_RCONFIG:
1976 case MBX_READ_SPARM:
1977 case MBX_READ_STATUS:
1978 case MBX_READ_RPI:
1979 case MBX_READ_XRI:
1980 case MBX_READ_REV:
1981 case MBX_READ_LNK_STAT:
1982 case MBX_REG_LOGIN:
1983 case MBX_UNREG_LOGIN:
dea31012005-04-17 16:05:31 -05001984 case MBX_CLEAR_LA:
1985 case MBX_DUMP_MEMORY:
1986 case MBX_DUMP_CONTEXT:
1987 case MBX_RUN_DIAGS:
1988 case MBX_RESTART:
1989 case MBX_UPDATE_CFG:
1990 case MBX_DOWN_LOAD:
1991 case MBX_DEL_LD_ENTRY:
1992 case MBX_RUN_PROGRAM:
1993 case MBX_SET_MASK:
James Smart09372822008-01-11 01:52:54 -05001994 case MBX_SET_VARIABLE:
dea31012005-04-17 16:05:31 -05001995 case MBX_UNREG_D_ID:
Jamie Wellnitz41415862006-02-28 19:25:27 -05001996 case MBX_KILL_BOARD:
dea31012005-04-17 16:05:31 -05001997 case MBX_CONFIG_FARP:
Jamie Wellnitz41415862006-02-28 19:25:27 -05001998 case MBX_BEACON:
dea31012005-04-17 16:05:31 -05001999 case MBX_LOAD_AREA:
2000 case MBX_RUN_BIU_DIAG64:
2001 case MBX_CONFIG_PORT:
2002 case MBX_READ_SPARM64:
2003 case MBX_READ_RPI64:
2004 case MBX_REG_LOGIN64:
James Smart76a95d72010-11-20 23:11:48 -05002005 case MBX_READ_TOPOLOGY:
James Smart09372822008-01-11 01:52:54 -05002006 case MBX_WRITE_WWN:
dea31012005-04-17 16:05:31 -05002007 case MBX_SET_DEBUG:
2008 case MBX_LOAD_EXP_ROM:
James Smart57127f12007-10-27 13:37:05 -04002009 case MBX_ASYNCEVT_ENABLE:
James Smart92d7f7b2007-06-17 19:56:38 -05002010 case MBX_REG_VPI:
2011 case MBX_UNREG_VPI:
James Smart858c9f62007-06-17 19:56:39 -05002012 case MBX_HEARTBEAT:
James Smart84774a42008-08-24 21:50:06 -04002013 case MBX_PORT_CAPABILITIES:
2014 case MBX_PORT_IOV_CONTROL:
James Smart04c68492009-05-22 14:52:52 -04002015 case MBX_SLI4_CONFIG:
2016 case MBX_SLI4_REQ_FTRS:
2017 case MBX_REG_FCFI:
2018 case MBX_UNREG_FCFI:
2019 case MBX_REG_VFI:
2020 case MBX_UNREG_VFI:
2021 case MBX_INIT_VPI:
2022 case MBX_INIT_VFI:
2023 case MBX_RESUME_RPI:
James Smartc7495932010-04-06 15:05:28 -04002024 case MBX_READ_EVENT_LOG_STATUS:
2025 case MBX_READ_EVENT_LOG:
James Smartdcf2a4e2010-09-29 11:18:53 -04002026 case MBX_SECURITY_MGMT:
2027 case MBX_AUTH_PORT:
dea31012005-04-17 16:05:31 -05002028 ret = mbxCommand;
2029 break;
2030 default:
2031 ret = MBX_SHUTDOWN;
2032 break;
2033 }
James Smart2e0fef82007-06-17 19:56:36 -05002034 return ret;
dea31012005-04-17 16:05:31 -05002035}
James Smarte59058c2008-08-24 21:49:00 -04002036
2037/**
James Smart3621a712009-04-06 18:47:14 -04002038 * lpfc_sli_wake_mbox_wait - lpfc_sli_issue_mbox_wait mbox completion handler
James Smarte59058c2008-08-24 21:49:00 -04002039 * @phba: Pointer to HBA context object.
2040 * @pmboxq: Pointer to mailbox command.
2041 *
2042 * This is completion handler function for mailbox commands issued from
2043 * lpfc_sli_issue_mbox_wait function. This function is called by the
2044 * mailbox event handler function with no lock held. This function
2045 * will wake up thread waiting on the wait queue pointed by context1
2046 * of the mailbox.
2047 **/
James Smart04c68492009-05-22 14:52:52 -04002048void
James Smart2e0fef82007-06-17 19:56:36 -05002049lpfc_sli_wake_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
dea31012005-04-17 16:05:31 -05002050{
2051 wait_queue_head_t *pdone_q;
James Smart858c9f62007-06-17 19:56:39 -05002052 unsigned long drvr_flag;
dea31012005-04-17 16:05:31 -05002053
2054 /*
2055 * If pdone_q is empty, the driver thread gave up waiting and
2056 * continued running.
2057 */
James Smart7054a602007-04-25 09:52:34 -04002058 pmboxq->mbox_flag |= LPFC_MBX_WAKE;
James Smart858c9f62007-06-17 19:56:39 -05002059 spin_lock_irqsave(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05002060 pdone_q = (wait_queue_head_t *) pmboxq->context1;
2061 if (pdone_q)
2062 wake_up_interruptible(pdone_q);
James Smart858c9f62007-06-17 19:56:39 -05002063 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05002064 return;
2065}
2066
James Smarte59058c2008-08-24 21:49:00 -04002067
2068/**
James Smart3621a712009-04-06 18:47:14 -04002069 * lpfc_sli_def_mbox_cmpl - Default mailbox completion handler
James Smarte59058c2008-08-24 21:49:00 -04002070 * @phba: Pointer to HBA context object.
2071 * @pmb: Pointer to mailbox object.
2072 *
2073 * This function is the default mailbox completion handler. It
2074 * frees the memory resources associated with the completed mailbox
2075 * command. If the completed command is a REG_LOGIN mailbox command,
2076 * this function will issue a UREG_LOGIN to re-claim the RPI.
2077 **/
dea31012005-04-17 16:05:31 -05002078void
James Smart2e0fef82007-06-17 19:56:36 -05002079lpfc_sli_def_mbox_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05002080{
James Smartd439d282010-09-29 11:18:45 -04002081 struct lpfc_vport *vport = pmb->vport;
dea31012005-04-17 16:05:31 -05002082 struct lpfc_dmabuf *mp;
James Smartd439d282010-09-29 11:18:45 -04002083 struct lpfc_nodelist *ndlp;
James Smart5af5eee2010-10-22 11:06:38 -04002084 struct Scsi_Host *shost;
James Smart04c68492009-05-22 14:52:52 -04002085 uint16_t rpi, vpi;
James Smart7054a602007-04-25 09:52:34 -04002086 int rc;
2087
dea31012005-04-17 16:05:31 -05002088 mp = (struct lpfc_dmabuf *) (pmb->context1);
James Smart7054a602007-04-25 09:52:34 -04002089
dea31012005-04-17 16:05:31 -05002090 if (mp) {
2091 lpfc_mbuf_free(phba, mp->virt, mp->phys);
2092 kfree(mp);
2093 }
James Smart7054a602007-04-25 09:52:34 -04002094
2095 /*
2096 * If a REG_LOGIN succeeded after node is destroyed or node
2097 * is in re-discovery driver need to cleanup the RPI.
2098 */
James Smart2e0fef82007-06-17 19:56:36 -05002099 if (!(phba->pport->load_flag & FC_UNLOADING) &&
James Smart04c68492009-05-22 14:52:52 -04002100 pmb->u.mb.mbxCommand == MBX_REG_LOGIN64 &&
2101 !pmb->u.mb.mbxStatus) {
2102 rpi = pmb->u.mb.un.varWords[0];
James Smart6d368e52011-05-24 11:44:12 -04002103 vpi = pmb->u.mb.un.varRegLogin.vpi;
James Smart04c68492009-05-22 14:52:52 -04002104 lpfc_unreg_login(phba, vpi, rpi, pmb);
James Smart92d7f7b2007-06-17 19:56:38 -05002105 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart7054a602007-04-25 09:52:34 -04002106 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
2107 if (rc != MBX_NOT_FINISHED)
2108 return;
2109 }
2110
James Smart695a8142010-01-26 23:08:03 -05002111 if ((pmb->u.mb.mbxCommand == MBX_REG_VPI) &&
2112 !(phba->pport->load_flag & FC_UNLOADING) &&
2113 !pmb->u.mb.mbxStatus) {
James Smart5af5eee2010-10-22 11:06:38 -04002114 shost = lpfc_shost_from_vport(vport);
2115 spin_lock_irq(shost->host_lock);
2116 vport->vpi_state |= LPFC_VPI_REGISTERED;
2117 vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
2118 spin_unlock_irq(shost->host_lock);
James Smart695a8142010-01-26 23:08:03 -05002119 }
2120
James Smartd439d282010-09-29 11:18:45 -04002121 if (pmb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
2122 ndlp = (struct lpfc_nodelist *)pmb->context2;
2123 lpfc_nlp_put(ndlp);
2124 pmb->context2 = NULL;
2125 }
2126
James Smartdcf2a4e2010-09-29 11:18:53 -04002127 /* Check security permission status on INIT_LINK mailbox command */
2128 if ((pmb->u.mb.mbxCommand == MBX_INIT_LINK) &&
2129 (pmb->u.mb.mbxStatus == MBXERR_SEC_NO_PERMISSION))
2130 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
2131 "2860 SLI authentication is required "
2132 "for INIT_LINK but has not done yet\n");
2133
James Smart04c68492009-05-22 14:52:52 -04002134 if (bf_get(lpfc_mqe_command, &pmb->u.mqe) == MBX_SLI4_CONFIG)
2135 lpfc_sli4_mbox_cmd_free(phba, pmb);
2136 else
2137 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05002138}
2139
James Smarte59058c2008-08-24 21:49:00 -04002140/**
James Smart3621a712009-04-06 18:47:14 -04002141 * lpfc_sli_handle_mb_event - Handle mailbox completions from firmware
James Smarte59058c2008-08-24 21:49:00 -04002142 * @phba: Pointer to HBA context object.
2143 *
2144 * This function is called with no lock held. This function processes all
2145 * the completed mailbox commands and gives it to upper layers. The interrupt
2146 * service routine processes mailbox completion interrupt and adds completed
2147 * mailbox commands to the mboxq_cmpl queue and signals the worker thread.
2148 * Worker thread call lpfc_sli_handle_mb_event, which will return the
2149 * completed mailbox commands in mboxq_cmpl queue to the upper layers. This
2150 * function returns the mailbox commands to the upper layer by calling the
2151 * completion handler function of each mailbox.
2152 **/
dea31012005-04-17 16:05:31 -05002153int
James Smart2e0fef82007-06-17 19:56:36 -05002154lpfc_sli_handle_mb_event(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05002155{
James Smart92d7f7b2007-06-17 19:56:38 -05002156 MAILBOX_t *pmbox;
dea31012005-04-17 16:05:31 -05002157 LPFC_MBOXQ_t *pmb;
James Smart92d7f7b2007-06-17 19:56:38 -05002158 int rc;
2159 LIST_HEAD(cmplq);
dea31012005-04-17 16:05:31 -05002160
2161 phba->sli.slistat.mbox_event++;
2162
James Smart92d7f7b2007-06-17 19:56:38 -05002163 /* Get all completed mailboxe buffers into the cmplq */
2164 spin_lock_irq(&phba->hbalock);
2165 list_splice_init(&phba->sli.mboxq_cmpl, &cmplq);
2166 spin_unlock_irq(&phba->hbalock);
2167
dea31012005-04-17 16:05:31 -05002168 /* Get a Mailbox buffer to setup mailbox commands for callback */
James Smart92d7f7b2007-06-17 19:56:38 -05002169 do {
2170 list_remove_head(&cmplq, pmb, LPFC_MBOXQ_t, list);
2171 if (pmb == NULL)
2172 break;
2173
James Smart04c68492009-05-22 14:52:52 -04002174 pmbox = &pmb->u.mb;
dea31012005-04-17 16:05:31 -05002175
James Smart858c9f62007-06-17 19:56:39 -05002176 if (pmbox->mbxCommand != MBX_HEARTBEAT) {
2177 if (pmb->vport) {
2178 lpfc_debugfs_disc_trc(pmb->vport,
2179 LPFC_DISC_TRC_MBOX_VPORT,
2180 "MBOX cmpl vport: cmd:x%x mb:x%x x%x",
2181 (uint32_t)pmbox->mbxCommand,
2182 pmbox->un.varWords[0],
2183 pmbox->un.varWords[1]);
2184 }
2185 else {
2186 lpfc_debugfs_disc_trc(phba->pport,
2187 LPFC_DISC_TRC_MBOX,
2188 "MBOX cmpl: cmd:x%x mb:x%x x%x",
2189 (uint32_t)pmbox->mbxCommand,
2190 pmbox->un.varWords[0],
2191 pmbox->un.varWords[1]);
2192 }
2193 }
2194
dea31012005-04-17 16:05:31 -05002195 /*
2196 * It is a fatal error if unknown mbox command completion.
2197 */
2198 if (lpfc_sli_chk_mbx_command(pmbox->mbxCommand) ==
2199 MBX_SHUTDOWN) {
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02002200 /* Unknown mailbox command compl */
James Smart92d7f7b2007-06-17 19:56:38 -05002201 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002202 "(%d):0323 Unknown Mailbox command "
James Smart04c68492009-05-22 14:52:52 -04002203 "x%x (x%x) Cmpl\n",
James Smart92d7f7b2007-06-17 19:56:38 -05002204 pmb->vport ? pmb->vport->vpi : 0,
James Smart04c68492009-05-22 14:52:52 -04002205 pmbox->mbxCommand,
2206 lpfc_sli4_mbox_opcode_get(phba, pmb));
James Smart2e0fef82007-06-17 19:56:36 -05002207 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05002208 phba->work_hs = HS_FFER3;
2209 lpfc_handle_eratt(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05002210 continue;
dea31012005-04-17 16:05:31 -05002211 }
2212
dea31012005-04-17 16:05:31 -05002213 if (pmbox->mbxStatus) {
2214 phba->sli.slistat.mbox_stat_err++;
2215 if (pmbox->mbxStatus == MBXERR_NO_RESOURCES) {
2216 /* Mbox cmd cmpl error - RETRYing */
James Smart92d7f7b2007-06-17 19:56:38 -05002217 lpfc_printf_log(phba, KERN_INFO,
2218 LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002219 "(%d):0305 Mbox cmd cmpl "
James Smart92d7f7b2007-06-17 19:56:38 -05002220 "error - RETRYing Data: x%x "
James Smart04c68492009-05-22 14:52:52 -04002221 "(x%x) x%x x%x x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -05002222 pmb->vport ? pmb->vport->vpi :0,
2223 pmbox->mbxCommand,
James Smart04c68492009-05-22 14:52:52 -04002224 lpfc_sli4_mbox_opcode_get(phba,
2225 pmb),
James Smart92d7f7b2007-06-17 19:56:38 -05002226 pmbox->mbxStatus,
2227 pmbox->un.varWords[0],
2228 pmb->vport->port_state);
dea31012005-04-17 16:05:31 -05002229 pmbox->mbxStatus = 0;
2230 pmbox->mbxOwner = OWN_HOST;
dea31012005-04-17 16:05:31 -05002231 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
James Smart04c68492009-05-22 14:52:52 -04002232 if (rc != MBX_NOT_FINISHED)
James Smart92d7f7b2007-06-17 19:56:38 -05002233 continue;
dea31012005-04-17 16:05:31 -05002234 }
2235 }
2236
2237 /* Mailbox cmd <cmd> Cmpl <cmpl> */
James Smart92d7f7b2007-06-17 19:56:38 -05002238 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smart04c68492009-05-22 14:52:52 -04002239 "(%d):0307 Mailbox cmd x%x (x%x) Cmpl x%p "
dea31012005-04-17 16:05:31 -05002240 "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 -05002241 pmb->vport ? pmb->vport->vpi : 0,
dea31012005-04-17 16:05:31 -05002242 pmbox->mbxCommand,
James Smart04c68492009-05-22 14:52:52 -04002243 lpfc_sli4_mbox_opcode_get(phba, pmb),
dea31012005-04-17 16:05:31 -05002244 pmb->mbox_cmpl,
2245 *((uint32_t *) pmbox),
2246 pmbox->un.varWords[0],
2247 pmbox->un.varWords[1],
2248 pmbox->un.varWords[2],
2249 pmbox->un.varWords[3],
2250 pmbox->un.varWords[4],
2251 pmbox->un.varWords[5],
2252 pmbox->un.varWords[6],
2253 pmbox->un.varWords[7]);
2254
James Smart92d7f7b2007-06-17 19:56:38 -05002255 if (pmb->mbox_cmpl)
dea31012005-04-17 16:05:31 -05002256 pmb->mbox_cmpl(phba,pmb);
James Smart92d7f7b2007-06-17 19:56:38 -05002257 } while (1);
James Smart2e0fef82007-06-17 19:56:36 -05002258 return 0;
dea31012005-04-17 16:05:31 -05002259}
James Smart92d7f7b2007-06-17 19:56:38 -05002260
James Smarte59058c2008-08-24 21:49:00 -04002261/**
James Smart3621a712009-04-06 18:47:14 -04002262 * lpfc_sli_get_buff - Get the buffer associated with the buffer tag
James Smarte59058c2008-08-24 21:49:00 -04002263 * @phba: Pointer to HBA context object.
2264 * @pring: Pointer to driver SLI ring object.
2265 * @tag: buffer tag.
2266 *
2267 * This function is called with no lock held. When QUE_BUFTAG_BIT bit
2268 * is set in the tag the buffer is posted for a particular exchange,
2269 * the function will return the buffer without replacing the buffer.
2270 * If the buffer is for unsolicited ELS or CT traffic, this function
2271 * returns the buffer and also posts another buffer to the firmware.
2272 **/
James Smart76bb24e2007-10-27 13:38:00 -04002273static struct lpfc_dmabuf *
2274lpfc_sli_get_buff(struct lpfc_hba *phba,
James Smart9f1e1b52008-12-04 22:39:40 -05002275 struct lpfc_sli_ring *pring,
2276 uint32_t tag)
James Smart76bb24e2007-10-27 13:38:00 -04002277{
James Smart9f1e1b52008-12-04 22:39:40 -05002278 struct hbq_dmabuf *hbq_entry;
2279
James Smart76bb24e2007-10-27 13:38:00 -04002280 if (tag & QUE_BUFTAG_BIT)
2281 return lpfc_sli_ring_taggedbuf_get(phba, pring, tag);
James Smart9f1e1b52008-12-04 22:39:40 -05002282 hbq_entry = lpfc_sli_hbqbuf_find(phba, tag);
2283 if (!hbq_entry)
2284 return NULL;
2285 return &hbq_entry->dbuf;
James Smart76bb24e2007-10-27 13:38:00 -04002286}
James Smart57127f12007-10-27 13:37:05 -04002287
James Smart3772a992009-05-22 14:50:54 -04002288/**
2289 * lpfc_complete_unsol_iocb - Complete an unsolicited sequence
2290 * @phba: Pointer to HBA context object.
2291 * @pring: Pointer to driver SLI ring object.
2292 * @saveq: Pointer to the iocbq struct representing the sequence starting frame.
2293 * @fch_r_ctl: the r_ctl for the first frame of the sequence.
2294 * @fch_type: the type for the first frame of the sequence.
2295 *
2296 * This function is called with no lock held. This function uses the r_ctl and
2297 * type of the received sequence to find the correct callback function to call
2298 * to process the sequence.
2299 **/
2300static int
2301lpfc_complete_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2302 struct lpfc_iocbq *saveq, uint32_t fch_r_ctl,
2303 uint32_t fch_type)
2304{
2305 int i;
2306
2307 /* unSolicited Responses */
2308 if (pring->prt[0].profile) {
2309 if (pring->prt[0].lpfc_sli_rcv_unsol_event)
2310 (pring->prt[0].lpfc_sli_rcv_unsol_event) (phba, pring,
2311 saveq);
2312 return 1;
2313 }
2314 /* We must search, based on rctl / type
2315 for the right routine */
2316 for (i = 0; i < pring->num_mask; i++) {
2317 if ((pring->prt[i].rctl == fch_r_ctl) &&
2318 (pring->prt[i].type == fch_type)) {
2319 if (pring->prt[i].lpfc_sli_rcv_unsol_event)
2320 (pring->prt[i].lpfc_sli_rcv_unsol_event)
2321 (phba, pring, saveq);
2322 return 1;
2323 }
2324 }
2325 return 0;
2326}
James Smarte59058c2008-08-24 21:49:00 -04002327
2328/**
James Smart3621a712009-04-06 18:47:14 -04002329 * lpfc_sli_process_unsol_iocb - Unsolicited iocb handler
James Smarte59058c2008-08-24 21:49:00 -04002330 * @phba: Pointer to HBA context object.
2331 * @pring: Pointer to driver SLI ring object.
2332 * @saveq: Pointer to the unsolicited iocb.
2333 *
2334 * This function is called with no lock held by the ring event handler
2335 * when there is an unsolicited iocb posted to the response ring by the
2336 * firmware. This function gets the buffer associated with the iocbs
2337 * and calls the event handler for the ring. This function handles both
2338 * qring buffers and hbq buffers.
2339 * When the function returns 1 the caller can free the iocb object otherwise
2340 * upper layer functions will free the iocb objects.
2341 **/
dea31012005-04-17 16:05:31 -05002342static int
2343lpfc_sli_process_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2344 struct lpfc_iocbq *saveq)
2345{
2346 IOCB_t * irsp;
2347 WORD5 * w5p;
2348 uint32_t Rctl, Type;
James Smart3772a992009-05-22 14:50:54 -04002349 uint32_t match;
James Smart76bb24e2007-10-27 13:38:00 -04002350 struct lpfc_iocbq *iocbq;
James Smart3163f722008-02-08 18:50:25 -05002351 struct lpfc_dmabuf *dmzbuf;
dea31012005-04-17 16:05:31 -05002352
2353 match = 0;
2354 irsp = &(saveq->iocb);
James Smart57127f12007-10-27 13:37:05 -04002355
2356 if (irsp->ulpCommand == CMD_ASYNC_STATUS) {
2357 if (pring->lpfc_sli_rcv_async_status)
2358 pring->lpfc_sli_rcv_async_status(phba, pring, saveq);
2359 else
2360 lpfc_printf_log(phba,
2361 KERN_WARNING,
2362 LOG_SLI,
2363 "0316 Ring %d handler: unexpected "
2364 "ASYNC_STATUS iocb received evt_code "
2365 "0x%x\n",
2366 pring->ringno,
2367 irsp->un.asyncstat.evt_code);
2368 return 1;
2369 }
2370
James Smart3163f722008-02-08 18:50:25 -05002371 if ((irsp->ulpCommand == CMD_IOCB_RET_XRI64_CX) &&
2372 (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)) {
2373 if (irsp->ulpBdeCount > 0) {
2374 dmzbuf = lpfc_sli_get_buff(phba, pring,
2375 irsp->un.ulpWord[3]);
2376 lpfc_in_buf_free(phba, dmzbuf);
2377 }
2378
2379 if (irsp->ulpBdeCount > 1) {
2380 dmzbuf = lpfc_sli_get_buff(phba, pring,
2381 irsp->unsli3.sli3Words[3]);
2382 lpfc_in_buf_free(phba, dmzbuf);
2383 }
2384
2385 if (irsp->ulpBdeCount > 2) {
2386 dmzbuf = lpfc_sli_get_buff(phba, pring,
2387 irsp->unsli3.sli3Words[7]);
2388 lpfc_in_buf_free(phba, dmzbuf);
2389 }
2390
2391 return 1;
2392 }
2393
James Smart92d7f7b2007-06-17 19:56:38 -05002394 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
James Smart76bb24e2007-10-27 13:38:00 -04002395 if (irsp->ulpBdeCount != 0) {
2396 saveq->context2 = lpfc_sli_get_buff(phba, pring,
James Smart92d7f7b2007-06-17 19:56:38 -05002397 irsp->un.ulpWord[3]);
James Smart76bb24e2007-10-27 13:38:00 -04002398 if (!saveq->context2)
2399 lpfc_printf_log(phba,
2400 KERN_ERR,
2401 LOG_SLI,
2402 "0341 Ring %d Cannot find buffer for "
2403 "an unsolicited iocb. tag 0x%x\n",
2404 pring->ringno,
2405 irsp->un.ulpWord[3]);
James Smart76bb24e2007-10-27 13:38:00 -04002406 }
2407 if (irsp->ulpBdeCount == 2) {
2408 saveq->context3 = lpfc_sli_get_buff(phba, pring,
James Smart51ef4c22007-08-02 11:10:31 -04002409 irsp->unsli3.sli3Words[7]);
James Smart76bb24e2007-10-27 13:38:00 -04002410 if (!saveq->context3)
2411 lpfc_printf_log(phba,
2412 KERN_ERR,
2413 LOG_SLI,
2414 "0342 Ring %d Cannot find buffer for an"
2415 " unsolicited iocb. tag 0x%x\n",
2416 pring->ringno,
2417 irsp->unsli3.sli3Words[7]);
2418 }
2419 list_for_each_entry(iocbq, &saveq->list, list) {
James Smart76bb24e2007-10-27 13:38:00 -04002420 irsp = &(iocbq->iocb);
James Smart76bb24e2007-10-27 13:38:00 -04002421 if (irsp->ulpBdeCount != 0) {
2422 iocbq->context2 = lpfc_sli_get_buff(phba, pring,
2423 irsp->un.ulpWord[3]);
James Smart9c2face2008-01-11 01:53:18 -05002424 if (!iocbq->context2)
James Smart76bb24e2007-10-27 13:38:00 -04002425 lpfc_printf_log(phba,
2426 KERN_ERR,
2427 LOG_SLI,
2428 "0343 Ring %d Cannot find "
2429 "buffer for an unsolicited iocb"
2430 ". tag 0x%x\n", pring->ringno,
2431 irsp->un.ulpWord[3]);
2432 }
2433 if (irsp->ulpBdeCount == 2) {
2434 iocbq->context3 = lpfc_sli_get_buff(phba, pring,
2435 irsp->unsli3.sli3Words[7]);
James Smart9c2face2008-01-11 01:53:18 -05002436 if (!iocbq->context3)
James Smart76bb24e2007-10-27 13:38:00 -04002437 lpfc_printf_log(phba,
2438 KERN_ERR,
2439 LOG_SLI,
2440 "0344 Ring %d Cannot find "
2441 "buffer for an unsolicited "
2442 "iocb. tag 0x%x\n",
2443 pring->ringno,
2444 irsp->unsli3.sli3Words[7]);
2445 }
2446 }
James Smart92d7f7b2007-06-17 19:56:38 -05002447 }
James Smart9c2face2008-01-11 01:53:18 -05002448 if (irsp->ulpBdeCount != 0 &&
2449 (irsp->ulpCommand == CMD_IOCB_RCV_CONT64_CX ||
2450 irsp->ulpStatus == IOSTAT_INTERMED_RSP)) {
2451 int found = 0;
2452
2453 /* search continue save q for same XRI */
2454 list_for_each_entry(iocbq, &pring->iocb_continue_saveq, clist) {
James Smart7851fe22011-07-22 18:36:52 -04002455 if (iocbq->iocb.unsli3.rcvsli3.ox_id ==
2456 saveq->iocb.unsli3.rcvsli3.ox_id) {
James Smart9c2face2008-01-11 01:53:18 -05002457 list_add_tail(&saveq->list, &iocbq->list);
2458 found = 1;
2459 break;
2460 }
2461 }
2462 if (!found)
2463 list_add_tail(&saveq->clist,
2464 &pring->iocb_continue_saveq);
2465 if (saveq->iocb.ulpStatus != IOSTAT_INTERMED_RSP) {
2466 list_del_init(&iocbq->clist);
2467 saveq = iocbq;
2468 irsp = &(saveq->iocb);
2469 } else
2470 return 0;
2471 }
2472 if ((irsp->ulpCommand == CMD_RCV_ELS_REQ64_CX) ||
2473 (irsp->ulpCommand == CMD_RCV_ELS_REQ_CX) ||
2474 (irsp->ulpCommand == CMD_IOCB_RCV_ELS64_CX)) {
James Smart6a9c52c2009-10-02 15:16:51 -04002475 Rctl = FC_RCTL_ELS_REQ;
2476 Type = FC_TYPE_ELS;
James Smart9c2face2008-01-11 01:53:18 -05002477 } else {
2478 w5p = (WORD5 *)&(saveq->iocb.un.ulpWord[5]);
2479 Rctl = w5p->hcsw.Rctl;
2480 Type = w5p->hcsw.Type;
2481
2482 /* Firmware Workaround */
2483 if ((Rctl == 0) && (pring->ringno == LPFC_ELS_RING) &&
2484 (irsp->ulpCommand == CMD_RCV_SEQUENCE64_CX ||
2485 irsp->ulpCommand == CMD_IOCB_RCV_SEQ64_CX)) {
James Smart6a9c52c2009-10-02 15:16:51 -04002486 Rctl = FC_RCTL_ELS_REQ;
2487 Type = FC_TYPE_ELS;
James Smart9c2face2008-01-11 01:53:18 -05002488 w5p->hcsw.Rctl = Rctl;
2489 w5p->hcsw.Type = Type;
2490 }
2491 }
James Smart92d7f7b2007-06-17 19:56:38 -05002492
James Smart3772a992009-05-22 14:50:54 -04002493 if (!lpfc_complete_unsol_iocb(phba, pring, saveq, Rctl, Type))
James Smart92d7f7b2007-06-17 19:56:38 -05002494 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002495 "0313 Ring %d handler: unexpected Rctl x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05002496 "Type x%x received\n",
James Smarte8b62012007-08-02 11:10:09 -04002497 pring->ringno, Rctl, Type);
James Smart3772a992009-05-22 14:50:54 -04002498
James Smart92d7f7b2007-06-17 19:56:38 -05002499 return 1;
dea31012005-04-17 16:05:31 -05002500}
2501
James Smarte59058c2008-08-24 21:49:00 -04002502/**
James Smart3621a712009-04-06 18:47:14 -04002503 * lpfc_sli_iocbq_lookup - Find command iocb for the given response iocb
James Smarte59058c2008-08-24 21:49:00 -04002504 * @phba: Pointer to HBA context object.
2505 * @pring: Pointer to driver SLI ring object.
2506 * @prspiocb: Pointer to response iocb object.
2507 *
2508 * This function looks up the iocb_lookup table to get the command iocb
2509 * corresponding to the given response iocb using the iotag of the
2510 * response iocb. This function is called with the hbalock held.
2511 * This function returns the command iocb object if it finds the command
2512 * iocb else returns NULL.
2513 **/
dea31012005-04-17 16:05:31 -05002514static struct lpfc_iocbq *
James Smart2e0fef82007-06-17 19:56:36 -05002515lpfc_sli_iocbq_lookup(struct lpfc_hba *phba,
2516 struct lpfc_sli_ring *pring,
2517 struct lpfc_iocbq *prspiocb)
dea31012005-04-17 16:05:31 -05002518{
dea31012005-04-17 16:05:31 -05002519 struct lpfc_iocbq *cmd_iocb = NULL;
2520 uint16_t iotag;
2521
James Bottomley604a3e32005-10-29 10:28:33 -05002522 iotag = prspiocb->iocb.ulpIoTag;
dea31012005-04-17 16:05:31 -05002523
James Bottomley604a3e32005-10-29 10:28:33 -05002524 if (iotag != 0 && iotag <= phba->sli.last_iotag) {
2525 cmd_iocb = phba->sli.iocbq_lookup[iotag];
James Smart92d7f7b2007-06-17 19:56:38 -05002526 list_del_init(&cmd_iocb->list);
James Smart2a9bf3d2010-06-07 15:24:45 -04002527 if (cmd_iocb->iocb_flag & LPFC_IO_ON_Q) {
2528 pring->txcmplq_cnt--;
2529 cmd_iocb->iocb_flag &= ~LPFC_IO_ON_Q;
2530 }
James Bottomley604a3e32005-10-29 10:28:33 -05002531 return cmd_iocb;
dea31012005-04-17 16:05:31 -05002532 }
2533
dea31012005-04-17 16:05:31 -05002534 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002535 "0317 iotag x%x is out off "
James Bottomley604a3e32005-10-29 10:28:33 -05002536 "range: max iotag x%x wd0 x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04002537 iotag, phba->sli.last_iotag,
James Bottomley604a3e32005-10-29 10:28:33 -05002538 *(((uint32_t *) &prspiocb->iocb) + 7));
dea31012005-04-17 16:05:31 -05002539 return NULL;
2540}
2541
James Smarte59058c2008-08-24 21:49:00 -04002542/**
James Smart3772a992009-05-22 14:50:54 -04002543 * lpfc_sli_iocbq_lookup_by_tag - Find command iocb for the iotag
2544 * @phba: Pointer to HBA context object.
2545 * @pring: Pointer to driver SLI ring object.
2546 * @iotag: IOCB tag.
2547 *
2548 * This function looks up the iocb_lookup table to get the command iocb
2549 * corresponding to the given iotag. This function is called with the
2550 * hbalock held.
2551 * This function returns the command iocb object if it finds the command
2552 * iocb else returns NULL.
2553 **/
2554static struct lpfc_iocbq *
2555lpfc_sli_iocbq_lookup_by_tag(struct lpfc_hba *phba,
2556 struct lpfc_sli_ring *pring, uint16_t iotag)
2557{
2558 struct lpfc_iocbq *cmd_iocb;
2559
2560 if (iotag != 0 && iotag <= phba->sli.last_iotag) {
2561 cmd_iocb = phba->sli.iocbq_lookup[iotag];
2562 list_del_init(&cmd_iocb->list);
James Smart2a9bf3d2010-06-07 15:24:45 -04002563 if (cmd_iocb->iocb_flag & LPFC_IO_ON_Q) {
2564 cmd_iocb->iocb_flag &= ~LPFC_IO_ON_Q;
2565 pring->txcmplq_cnt--;
2566 }
James Smart3772a992009-05-22 14:50:54 -04002567 return cmd_iocb;
2568 }
2569
2570 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2571 "0372 iotag x%x is out off range: max iotag (x%x)\n",
2572 iotag, phba->sli.last_iotag);
2573 return NULL;
2574}
2575
2576/**
James Smart3621a712009-04-06 18:47:14 -04002577 * lpfc_sli_process_sol_iocb - process solicited iocb completion
James Smarte59058c2008-08-24 21:49:00 -04002578 * @phba: Pointer to HBA context object.
2579 * @pring: Pointer to driver SLI ring object.
2580 * @saveq: Pointer to the response iocb to be processed.
2581 *
2582 * This function is called by the ring event handler for non-fcp
2583 * rings when there is a new response iocb in the response ring.
2584 * The caller is not required to hold any locks. This function
2585 * gets the command iocb associated with the response iocb and
2586 * calls the completion handler for the command iocb. If there
2587 * is no completion handler, the function will free the resources
2588 * associated with command iocb. If the response iocb is for
2589 * an already aborted command iocb, the status of the completion
2590 * is changed to IOSTAT_LOCAL_REJECT/IOERR_SLI_ABORTED.
2591 * This function always returns 1.
2592 **/
dea31012005-04-17 16:05:31 -05002593static int
James Smart2e0fef82007-06-17 19:56:36 -05002594lpfc_sli_process_sol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
dea31012005-04-17 16:05:31 -05002595 struct lpfc_iocbq *saveq)
2596{
James Smart2e0fef82007-06-17 19:56:36 -05002597 struct lpfc_iocbq *cmdiocbp;
dea31012005-04-17 16:05:31 -05002598 int rc = 1;
2599 unsigned long iflag;
2600
2601 /* Based on the iotag field, get the cmd IOCB from the txcmplq */
James Smart2e0fef82007-06-17 19:56:36 -05002602 spin_lock_irqsave(&phba->hbalock, iflag);
James Bottomley604a3e32005-10-29 10:28:33 -05002603 cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring, saveq);
James Smart2e0fef82007-06-17 19:56:36 -05002604 spin_unlock_irqrestore(&phba->hbalock, iflag);
2605
dea31012005-04-17 16:05:31 -05002606 if (cmdiocbp) {
2607 if (cmdiocbp->iocb_cmpl) {
2608 /*
James Smartea2151b2008-09-07 11:52:10 -04002609 * If an ELS command failed send an event to mgmt
2610 * application.
2611 */
2612 if (saveq->iocb.ulpStatus &&
2613 (pring->ringno == LPFC_ELS_RING) &&
2614 (cmdiocbp->iocb.ulpCommand ==
2615 CMD_ELS_REQUEST64_CR))
2616 lpfc_send_els_failure_event(phba,
2617 cmdiocbp, saveq);
2618
2619 /*
dea31012005-04-17 16:05:31 -05002620 * Post all ELS completions to the worker thread.
2621 * All other are passed to the completion callback.
2622 */
2623 if (pring->ringno == LPFC_ELS_RING) {
James Smart341af102010-01-26 23:07:37 -05002624 if ((phba->sli_rev < LPFC_SLI_REV4) &&
2625 (cmdiocbp->iocb_flag &
2626 LPFC_DRIVER_ABORTED)) {
2627 spin_lock_irqsave(&phba->hbalock,
2628 iflag);
James Smart07951072007-04-25 09:51:38 -04002629 cmdiocbp->iocb_flag &=
2630 ~LPFC_DRIVER_ABORTED;
James Smart341af102010-01-26 23:07:37 -05002631 spin_unlock_irqrestore(&phba->hbalock,
2632 iflag);
James Smart07951072007-04-25 09:51:38 -04002633 saveq->iocb.ulpStatus =
2634 IOSTAT_LOCAL_REJECT;
2635 saveq->iocb.un.ulpWord[4] =
2636 IOERR_SLI_ABORTED;
James Smart0ff10d42008-01-11 01:52:36 -05002637
2638 /* Firmware could still be in progress
2639 * of DMAing payload, so don't free data
2640 * buffer till after a hbeat.
2641 */
James Smart341af102010-01-26 23:07:37 -05002642 spin_lock_irqsave(&phba->hbalock,
2643 iflag);
James Smart0ff10d42008-01-11 01:52:36 -05002644 saveq->iocb_flag |= LPFC_DELAY_MEM_FREE;
James Smart341af102010-01-26 23:07:37 -05002645 spin_unlock_irqrestore(&phba->hbalock,
2646 iflag);
2647 }
James Smart0f65ff62010-02-26 14:14:23 -05002648 if (phba->sli_rev == LPFC_SLI_REV4) {
2649 if (saveq->iocb_flag &
2650 LPFC_EXCHANGE_BUSY) {
2651 /* Set cmdiocb flag for the
2652 * exchange busy so sgl (xri)
2653 * will not be released until
2654 * the abort xri is received
2655 * from hba.
2656 */
2657 spin_lock_irqsave(
2658 &phba->hbalock, iflag);
2659 cmdiocbp->iocb_flag |=
2660 LPFC_EXCHANGE_BUSY;
2661 spin_unlock_irqrestore(
2662 &phba->hbalock, iflag);
2663 }
2664 if (cmdiocbp->iocb_flag &
2665 LPFC_DRIVER_ABORTED) {
2666 /*
2667 * Clear LPFC_DRIVER_ABORTED
2668 * bit in case it was driver
2669 * initiated abort.
2670 */
2671 spin_lock_irqsave(
2672 &phba->hbalock, iflag);
2673 cmdiocbp->iocb_flag &=
2674 ~LPFC_DRIVER_ABORTED;
2675 spin_unlock_irqrestore(
2676 &phba->hbalock, iflag);
2677 cmdiocbp->iocb.ulpStatus =
2678 IOSTAT_LOCAL_REJECT;
2679 cmdiocbp->iocb.un.ulpWord[4] =
2680 IOERR_ABORT_REQUESTED;
2681 /*
2682 * For SLI4, irsiocb contains
2683 * NO_XRI in sli_xritag, it
2684 * shall not affect releasing
2685 * sgl (xri) process.
2686 */
2687 saveq->iocb.ulpStatus =
2688 IOSTAT_LOCAL_REJECT;
2689 saveq->iocb.un.ulpWord[4] =
2690 IOERR_SLI_ABORTED;
2691 spin_lock_irqsave(
2692 &phba->hbalock, iflag);
2693 saveq->iocb_flag |=
2694 LPFC_DELAY_MEM_FREE;
2695 spin_unlock_irqrestore(
2696 &phba->hbalock, iflag);
2697 }
James Smart07951072007-04-25 09:51:38 -04002698 }
dea31012005-04-17 16:05:31 -05002699 }
James Smart2e0fef82007-06-17 19:56:36 -05002700 (cmdiocbp->iocb_cmpl) (phba, cmdiocbp, saveq);
James Bottomley604a3e32005-10-29 10:28:33 -05002701 } else
2702 lpfc_sli_release_iocbq(phba, cmdiocbp);
dea31012005-04-17 16:05:31 -05002703 } else {
2704 /*
2705 * Unknown initiating command based on the response iotag.
2706 * This could be the case on the ELS ring because of
2707 * lpfc_els_abort().
2708 */
2709 if (pring->ringno != LPFC_ELS_RING) {
2710 /*
2711 * Ring <ringno> handler: unexpected completion IoTag
2712 * <IoTag>
2713 */
James Smarta257bf92009-04-06 18:48:10 -04002714 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002715 "0322 Ring %d handler: "
2716 "unexpected completion IoTag x%x "
2717 "Data: x%x x%x x%x x%x\n",
2718 pring->ringno,
2719 saveq->iocb.ulpIoTag,
2720 saveq->iocb.ulpStatus,
2721 saveq->iocb.un.ulpWord[4],
2722 saveq->iocb.ulpCommand,
2723 saveq->iocb.ulpContext);
dea31012005-04-17 16:05:31 -05002724 }
2725 }
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04002726
dea31012005-04-17 16:05:31 -05002727 return rc;
2728}
2729
James Smarte59058c2008-08-24 21:49:00 -04002730/**
James Smart3621a712009-04-06 18:47:14 -04002731 * lpfc_sli_rsp_pointers_error - Response ring pointer error handler
James Smarte59058c2008-08-24 21:49:00 -04002732 * @phba: Pointer to HBA context object.
2733 * @pring: Pointer to driver SLI ring object.
2734 *
2735 * This function is called from the iocb ring event handlers when
2736 * put pointer is ahead of the get pointer for a ring. This function signal
2737 * an error attention condition to the worker thread and the worker
2738 * thread will transition the HBA to offline state.
2739 **/
James Smart2e0fef82007-06-17 19:56:36 -05002740static void
2741lpfc_sli_rsp_pointers_error(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002742{
James Smart34b02dc2008-08-24 21:49:55 -04002743 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002744 /*
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02002745 * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002746 * rsp ring <portRspMax>
2747 */
2748 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002749 "0312 Ring %d handler: portRspPut %d "
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02002750 "is bigger than rsp ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04002751 pring->ringno, le32_to_cpu(pgp->rspPutInx),
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002752 pring->numRiocb);
2753
James Smart2e0fef82007-06-17 19:56:36 -05002754 phba->link_state = LPFC_HBA_ERROR;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002755
2756 /*
2757 * All error attention handlers are posted to
2758 * worker thread
2759 */
2760 phba->work_ha |= HA_ERATT;
2761 phba->work_hs = HS_FFER3;
James Smart92d7f7b2007-06-17 19:56:38 -05002762
James Smart5e9d9b82008-06-14 22:52:53 -04002763 lpfc_worker_wake_up(phba);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002764
2765 return;
2766}
2767
James Smarte59058c2008-08-24 21:49:00 -04002768/**
James Smart3621a712009-04-06 18:47:14 -04002769 * lpfc_poll_eratt - Error attention polling timer timeout handler
James Smart93996272008-08-24 21:50:30 -04002770 * @ptr: Pointer to address of HBA context object.
2771 *
2772 * This function is invoked by the Error Attention polling timer when the
2773 * timer times out. It will check the SLI Error Attention register for
2774 * possible attention events. If so, it will post an Error Attention event
2775 * and wake up worker thread to process it. Otherwise, it will set up the
2776 * Error Attention polling timer for the next poll.
2777 **/
2778void lpfc_poll_eratt(unsigned long ptr)
2779{
2780 struct lpfc_hba *phba;
2781 uint32_t eratt = 0;
2782
2783 phba = (struct lpfc_hba *)ptr;
2784
2785 /* Check chip HA register for error event */
2786 eratt = lpfc_sli_check_eratt(phba);
2787
2788 if (eratt)
2789 /* Tell the worker thread there is work to do */
2790 lpfc_worker_wake_up(phba);
2791 else
2792 /* Restart the timer for next eratt poll */
2793 mod_timer(&phba->eratt_poll, jiffies +
2794 HZ * LPFC_ERATT_POLL_INTERVAL);
2795 return;
2796}
2797
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002798
James Smarte59058c2008-08-24 21:49:00 -04002799/**
James Smart3621a712009-04-06 18:47:14 -04002800 * lpfc_sli_handle_fast_ring_event - Handle ring events on FCP ring
James Smarte59058c2008-08-24 21:49:00 -04002801 * @phba: Pointer to HBA context object.
2802 * @pring: Pointer to driver SLI ring object.
2803 * @mask: Host attention register mask for this ring.
2804 *
2805 * This function is called from the interrupt context when there is a ring
2806 * event for the fcp ring. The caller does not hold any lock.
2807 * The function processes each response iocb in the response ring until it
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002808 * finds an iocb with LE bit set and chains all the iocbs up to the iocb with
James Smarte59058c2008-08-24 21:49:00 -04002809 * LE bit set. The function will call the completion handler of the command iocb
2810 * if the response iocb indicates a completion for a command iocb or it is
2811 * an abort completion. The function will call lpfc_sli_process_unsol_iocb
2812 * function if this is an unsolicited iocb.
dea31012005-04-17 16:05:31 -05002813 * This routine presumes LPFC_FCP_RING handling and doesn't bother
James Smart45ed1192009-10-02 15:17:02 -04002814 * to check it explicitly.
2815 */
2816int
James Smart2e0fef82007-06-17 19:56:36 -05002817lpfc_sli_handle_fast_ring_event(struct lpfc_hba *phba,
2818 struct lpfc_sli_ring *pring, uint32_t mask)
dea31012005-04-17 16:05:31 -05002819{
James Smart34b02dc2008-08-24 21:49:55 -04002820 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
dea31012005-04-17 16:05:31 -05002821 IOCB_t *irsp = NULL;
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04002822 IOCB_t *entry = NULL;
dea31012005-04-17 16:05:31 -05002823 struct lpfc_iocbq *cmdiocbq = NULL;
2824 struct lpfc_iocbq rspiocbq;
dea31012005-04-17 16:05:31 -05002825 uint32_t status;
2826 uint32_t portRspPut, portRspMax;
2827 int rc = 1;
2828 lpfc_iocb_type type;
2829 unsigned long iflag;
2830 uint32_t rsp_cmpl = 0;
dea31012005-04-17 16:05:31 -05002831
James Smart2e0fef82007-06-17 19:56:36 -05002832 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05002833 pring->stats.iocb_event++;
2834
dea31012005-04-17 16:05:31 -05002835 /*
2836 * The next available response entry should never exceed the maximum
2837 * entries. If it does, treat it as an adapter hardware error.
2838 */
2839 portRspMax = pring->numRiocb;
2840 portRspPut = le32_to_cpu(pgp->rspPutInx);
2841 if (unlikely(portRspPut >= portRspMax)) {
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002842 lpfc_sli_rsp_pointers_error(phba, pring);
James Smart2e0fef82007-06-17 19:56:36 -05002843 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05002844 return 1;
2845 }
James Smart45ed1192009-10-02 15:17:02 -04002846 if (phba->fcp_ring_in_use) {
2847 spin_unlock_irqrestore(&phba->hbalock, iflag);
2848 return 1;
2849 } else
2850 phba->fcp_ring_in_use = 1;
dea31012005-04-17 16:05:31 -05002851
2852 rmb();
2853 while (pring->rspidx != portRspPut) {
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04002854 /*
2855 * Fetch an entry off the ring and copy it into a local data
2856 * structure. The copy involves a byte-swap since the
2857 * network byte order and pci byte orders are different.
2858 */
James Smarted957682007-06-17 19:56:37 -05002859 entry = lpfc_resp_iocb(phba, pring);
James Smart858c9f62007-06-17 19:56:39 -05002860 phba->last_completion_time = jiffies;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002861
2862 if (++pring->rspidx >= portRspMax)
2863 pring->rspidx = 0;
2864
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04002865 lpfc_sli_pcimem_bcopy((uint32_t *) entry,
2866 (uint32_t *) &rspiocbq.iocb,
James Smarted957682007-06-17 19:56:37 -05002867 phba->iocb_rsp_size);
James Smarta4bc3372006-12-02 13:34:16 -05002868 INIT_LIST_HEAD(&(rspiocbq.list));
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04002869 irsp = &rspiocbq.iocb;
2870
dea31012005-04-17 16:05:31 -05002871 type = lpfc_sli_iocb_cmd_type(irsp->ulpCommand & CMD_IOCB_MASK);
2872 pring->stats.iocb_rsp++;
2873 rsp_cmpl++;
2874
2875 if (unlikely(irsp->ulpStatus)) {
James Smart92d7f7b2007-06-17 19:56:38 -05002876 /*
2877 * If resource errors reported from HBA, reduce
2878 * queuedepths of the SCSI device.
2879 */
2880 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
2881 (irsp->un.ulpWord[4] == IOERR_NO_RESOURCES)) {
2882 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart3772a992009-05-22 14:50:54 -04002883 phba->lpfc_rampdown_queue_depth(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05002884 spin_lock_irqsave(&phba->hbalock, iflag);
2885 }
2886
dea31012005-04-17 16:05:31 -05002887 /* Rsp ring <ringno> error: IOCB */
2888 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002889 "0336 Rsp Ring %d error: IOCB Data: "
James Smart92d7f7b2007-06-17 19:56:38 -05002890 "x%x x%x x%x x%x x%x x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04002891 pring->ringno,
James Smart92d7f7b2007-06-17 19:56:38 -05002892 irsp->un.ulpWord[0],
2893 irsp->un.ulpWord[1],
2894 irsp->un.ulpWord[2],
2895 irsp->un.ulpWord[3],
2896 irsp->un.ulpWord[4],
2897 irsp->un.ulpWord[5],
James Smartd7c255b2008-08-24 21:50:00 -04002898 *(uint32_t *)&irsp->un1,
2899 *((uint32_t *)&irsp->un1 + 1));
dea31012005-04-17 16:05:31 -05002900 }
2901
2902 switch (type) {
2903 case LPFC_ABORT_IOCB:
2904 case LPFC_SOL_IOCB:
2905 /*
2906 * Idle exchange closed via ABTS from port. No iocb
2907 * resources need to be recovered.
2908 */
2909 if (unlikely(irsp->ulpCommand == CMD_XRI_ABORTED_CX)) {
James Smartdca94792006-08-01 07:34:08 -04002910 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002911 "0333 IOCB cmd 0x%x"
James Smartdca94792006-08-01 07:34:08 -04002912 " processed. Skipping"
James Smart92d7f7b2007-06-17 19:56:38 -05002913 " completion\n",
James Smartdca94792006-08-01 07:34:08 -04002914 irsp->ulpCommand);
dea31012005-04-17 16:05:31 -05002915 break;
2916 }
2917
James Bottomley604a3e32005-10-29 10:28:33 -05002918 cmdiocbq = lpfc_sli_iocbq_lookup(phba, pring,
2919 &rspiocbq);
James Smart0f65ff62010-02-26 14:14:23 -05002920 if (unlikely(!cmdiocbq))
2921 break;
2922 if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED)
2923 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
2924 if (cmdiocbq->iocb_cmpl) {
2925 spin_unlock_irqrestore(&phba->hbalock, iflag);
2926 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
2927 &rspiocbq);
2928 spin_lock_irqsave(&phba->hbalock, iflag);
2929 }
dea31012005-04-17 16:05:31 -05002930 break;
James Smarta4bc3372006-12-02 13:34:16 -05002931 case LPFC_UNSOL_IOCB:
James Smart2e0fef82007-06-17 19:56:36 -05002932 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta4bc3372006-12-02 13:34:16 -05002933 lpfc_sli_process_unsol_iocb(phba, pring, &rspiocbq);
James Smart2e0fef82007-06-17 19:56:36 -05002934 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta4bc3372006-12-02 13:34:16 -05002935 break;
dea31012005-04-17 16:05:31 -05002936 default:
2937 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
2938 char adaptermsg[LPFC_MAX_ADPTMSG];
2939 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
2940 memcpy(&adaptermsg[0], (uint8_t *) irsp,
2941 MAX_MSG_DATA);
Joe Perches898eb712007-10-18 03:06:30 -07002942 dev_warn(&((phba->pcidev)->dev),
2943 "lpfc%d: %s\n",
dea31012005-04-17 16:05:31 -05002944 phba->brd_no, adaptermsg);
2945 } else {
2946 /* Unknown IOCB command */
2947 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002948 "0334 Unknown IOCB command "
James Smart92d7f7b2007-06-17 19:56:38 -05002949 "Data: x%x, x%x x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04002950 type, irsp->ulpCommand,
James Smart92d7f7b2007-06-17 19:56:38 -05002951 irsp->ulpStatus,
2952 irsp->ulpIoTag,
2953 irsp->ulpContext);
dea31012005-04-17 16:05:31 -05002954 }
2955 break;
2956 }
2957
2958 /*
2959 * The response IOCB has been processed. Update the ring
2960 * pointer in SLIM. If the port response put pointer has not
2961 * been updated, sync the pgp->rspPutInx and fetch the new port
2962 * response put pointer.
2963 */
James Smarted957682007-06-17 19:56:37 -05002964 writel(pring->rspidx, &phba->host_gp[pring->ringno].rspGetInx);
dea31012005-04-17 16:05:31 -05002965
2966 if (pring->rspidx == portRspPut)
2967 portRspPut = le32_to_cpu(pgp->rspPutInx);
2968 }
2969
2970 if ((rsp_cmpl > 0) && (mask & HA_R0RE_REQ)) {
2971 pring->stats.iocb_rsp_full++;
2972 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
2973 writel(status, phba->CAregaddr);
2974 readl(phba->CAregaddr);
2975 }
2976 if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
2977 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
2978 pring->stats.iocb_cmd_empty++;
2979
2980 /* Force update of the local copy of cmdGetInx */
2981 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
2982 lpfc_sli_resume_iocb(phba, pring);
2983
2984 if ((pring->lpfc_sli_cmd_available))
2985 (pring->lpfc_sli_cmd_available) (phba, pring);
2986
2987 }
2988
James Smart45ed1192009-10-02 15:17:02 -04002989 phba->fcp_ring_in_use = 0;
James Smart2e0fef82007-06-17 19:56:36 -05002990 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05002991 return rc;
2992}
2993
James Smarte59058c2008-08-24 21:49:00 -04002994/**
James Smart3772a992009-05-22 14:50:54 -04002995 * lpfc_sli_sp_handle_rspiocb - Handle slow-path response iocb
2996 * @phba: Pointer to HBA context object.
2997 * @pring: Pointer to driver SLI ring object.
2998 * @rspiocbp: Pointer to driver response IOCB object.
2999 *
3000 * This function is called from the worker thread when there is a slow-path
3001 * response IOCB to process. This function chains all the response iocbs until
3002 * seeing the iocb with the LE bit set. The function will call
3003 * lpfc_sli_process_sol_iocb function if the response iocb indicates a
3004 * completion of a command iocb. The function will call the
3005 * lpfc_sli_process_unsol_iocb function if this is an unsolicited iocb.
3006 * The function frees the resources or calls the completion handler if this
3007 * iocb is an abort completion. The function returns NULL when the response
3008 * iocb has the LE bit set and all the chained iocbs are processed, otherwise
3009 * this function shall chain the iocb on to the iocb_continueq and return the
3010 * response iocb passed in.
3011 **/
3012static struct lpfc_iocbq *
3013lpfc_sli_sp_handle_rspiocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
3014 struct lpfc_iocbq *rspiocbp)
3015{
3016 struct lpfc_iocbq *saveq;
3017 struct lpfc_iocbq *cmdiocbp;
3018 struct lpfc_iocbq *next_iocb;
3019 IOCB_t *irsp = NULL;
3020 uint32_t free_saveq;
3021 uint8_t iocb_cmd_type;
3022 lpfc_iocb_type type;
3023 unsigned long iflag;
3024 int rc;
3025
3026 spin_lock_irqsave(&phba->hbalock, iflag);
3027 /* First add the response iocb to the countinueq list */
3028 list_add_tail(&rspiocbp->list, &(pring->iocb_continueq));
3029 pring->iocb_continueq_cnt++;
3030
Justin P. Mattock70f23fd2011-05-10 10:16:21 +02003031 /* Now, determine whether the list is completed for processing */
James Smart3772a992009-05-22 14:50:54 -04003032 irsp = &rspiocbp->iocb;
3033 if (irsp->ulpLe) {
3034 /*
3035 * By default, the driver expects to free all resources
3036 * associated with this iocb completion.
3037 */
3038 free_saveq = 1;
3039 saveq = list_get_first(&pring->iocb_continueq,
3040 struct lpfc_iocbq, list);
3041 irsp = &(saveq->iocb);
3042 list_del_init(&pring->iocb_continueq);
3043 pring->iocb_continueq_cnt = 0;
3044
3045 pring->stats.iocb_rsp++;
3046
3047 /*
3048 * If resource errors reported from HBA, reduce
3049 * queuedepths of the SCSI device.
3050 */
3051 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
3052 (irsp->un.ulpWord[4] == IOERR_NO_RESOURCES)) {
3053 spin_unlock_irqrestore(&phba->hbalock, iflag);
3054 phba->lpfc_rampdown_queue_depth(phba);
3055 spin_lock_irqsave(&phba->hbalock, iflag);
3056 }
3057
3058 if (irsp->ulpStatus) {
3059 /* Rsp ring <ringno> error: IOCB */
3060 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
3061 "0328 Rsp Ring %d error: "
3062 "IOCB Data: "
3063 "x%x x%x x%x x%x "
3064 "x%x x%x x%x x%x "
3065 "x%x x%x x%x x%x "
3066 "x%x x%x x%x x%x\n",
3067 pring->ringno,
3068 irsp->un.ulpWord[0],
3069 irsp->un.ulpWord[1],
3070 irsp->un.ulpWord[2],
3071 irsp->un.ulpWord[3],
3072 irsp->un.ulpWord[4],
3073 irsp->un.ulpWord[5],
3074 *(((uint32_t *) irsp) + 6),
3075 *(((uint32_t *) irsp) + 7),
3076 *(((uint32_t *) irsp) + 8),
3077 *(((uint32_t *) irsp) + 9),
3078 *(((uint32_t *) irsp) + 10),
3079 *(((uint32_t *) irsp) + 11),
3080 *(((uint32_t *) irsp) + 12),
3081 *(((uint32_t *) irsp) + 13),
3082 *(((uint32_t *) irsp) + 14),
3083 *(((uint32_t *) irsp) + 15));
3084 }
3085
3086 /*
3087 * Fetch the IOCB command type and call the correct completion
3088 * routine. Solicited and Unsolicited IOCBs on the ELS ring
3089 * get freed back to the lpfc_iocb_list by the discovery
3090 * kernel thread.
3091 */
3092 iocb_cmd_type = irsp->ulpCommand & CMD_IOCB_MASK;
3093 type = lpfc_sli_iocb_cmd_type(iocb_cmd_type);
3094 switch (type) {
3095 case LPFC_SOL_IOCB:
3096 spin_unlock_irqrestore(&phba->hbalock, iflag);
3097 rc = lpfc_sli_process_sol_iocb(phba, pring, saveq);
3098 spin_lock_irqsave(&phba->hbalock, iflag);
3099 break;
3100
3101 case LPFC_UNSOL_IOCB:
3102 spin_unlock_irqrestore(&phba->hbalock, iflag);
3103 rc = lpfc_sli_process_unsol_iocb(phba, pring, saveq);
3104 spin_lock_irqsave(&phba->hbalock, iflag);
3105 if (!rc)
3106 free_saveq = 0;
3107 break;
3108
3109 case LPFC_ABORT_IOCB:
3110 cmdiocbp = NULL;
3111 if (irsp->ulpCommand != CMD_XRI_ABORTED_CX)
3112 cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring,
3113 saveq);
3114 if (cmdiocbp) {
3115 /* Call the specified completion routine */
3116 if (cmdiocbp->iocb_cmpl) {
3117 spin_unlock_irqrestore(&phba->hbalock,
3118 iflag);
3119 (cmdiocbp->iocb_cmpl)(phba, cmdiocbp,
3120 saveq);
3121 spin_lock_irqsave(&phba->hbalock,
3122 iflag);
3123 } else
3124 __lpfc_sli_release_iocbq(phba,
3125 cmdiocbp);
3126 }
3127 break;
3128
3129 case LPFC_UNKNOWN_IOCB:
3130 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
3131 char adaptermsg[LPFC_MAX_ADPTMSG];
3132 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
3133 memcpy(&adaptermsg[0], (uint8_t *)irsp,
3134 MAX_MSG_DATA);
3135 dev_warn(&((phba->pcidev)->dev),
3136 "lpfc%d: %s\n",
3137 phba->brd_no, adaptermsg);
3138 } else {
3139 /* Unknown IOCB command */
3140 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3141 "0335 Unknown IOCB "
3142 "command Data: x%x "
3143 "x%x x%x x%x\n",
3144 irsp->ulpCommand,
3145 irsp->ulpStatus,
3146 irsp->ulpIoTag,
3147 irsp->ulpContext);
3148 }
3149 break;
3150 }
3151
3152 if (free_saveq) {
3153 list_for_each_entry_safe(rspiocbp, next_iocb,
3154 &saveq->list, list) {
3155 list_del(&rspiocbp->list);
3156 __lpfc_sli_release_iocbq(phba, rspiocbp);
3157 }
3158 __lpfc_sli_release_iocbq(phba, saveq);
3159 }
3160 rspiocbp = NULL;
3161 }
3162 spin_unlock_irqrestore(&phba->hbalock, iflag);
3163 return rspiocbp;
3164}
3165
3166/**
3167 * lpfc_sli_handle_slow_ring_event - Wrapper func for handling slow-path iocbs
James Smarte59058c2008-08-24 21:49:00 -04003168 * @phba: Pointer to HBA context object.
3169 * @pring: Pointer to driver SLI ring object.
3170 * @mask: Host attention register mask for this ring.
3171 *
James Smart3772a992009-05-22 14:50:54 -04003172 * This routine wraps the actual slow_ring event process routine from the
3173 * API jump table function pointer from the lpfc_hba struct.
James Smarte59058c2008-08-24 21:49:00 -04003174 **/
James Smart3772a992009-05-22 14:50:54 -04003175void
James Smart2e0fef82007-06-17 19:56:36 -05003176lpfc_sli_handle_slow_ring_event(struct lpfc_hba *phba,
3177 struct lpfc_sli_ring *pring, uint32_t mask)
dea31012005-04-17 16:05:31 -05003178{
James Smart3772a992009-05-22 14:50:54 -04003179 phba->lpfc_sli_handle_slow_ring_event(phba, pring, mask);
3180}
3181
3182/**
3183 * lpfc_sli_handle_slow_ring_event_s3 - Handle SLI3 ring event for non-FCP rings
3184 * @phba: Pointer to HBA context object.
3185 * @pring: Pointer to driver SLI ring object.
3186 * @mask: Host attention register mask for this ring.
3187 *
3188 * This function is called from the worker thread when there is a ring event
3189 * for non-fcp rings. The caller does not hold any lock. The function will
3190 * remove each response iocb in the response ring and calls the handle
3191 * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
3192 **/
3193static void
3194lpfc_sli_handle_slow_ring_event_s3(struct lpfc_hba *phba,
3195 struct lpfc_sli_ring *pring, uint32_t mask)
3196{
James Smart34b02dc2008-08-24 21:49:55 -04003197 struct lpfc_pgp *pgp;
dea31012005-04-17 16:05:31 -05003198 IOCB_t *entry;
3199 IOCB_t *irsp = NULL;
3200 struct lpfc_iocbq *rspiocbp = NULL;
dea31012005-04-17 16:05:31 -05003201 uint32_t portRspPut, portRspMax;
dea31012005-04-17 16:05:31 -05003202 unsigned long iflag;
James Smart3772a992009-05-22 14:50:54 -04003203 uint32_t status;
dea31012005-04-17 16:05:31 -05003204
James Smart34b02dc2008-08-24 21:49:55 -04003205 pgp = &phba->port_gp[pring->ringno];
James Smart2e0fef82007-06-17 19:56:36 -05003206 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003207 pring->stats.iocb_event++;
3208
dea31012005-04-17 16:05:31 -05003209 /*
3210 * The next available response entry should never exceed the maximum
3211 * entries. If it does, treat it as an adapter hardware error.
3212 */
3213 portRspMax = pring->numRiocb;
3214 portRspPut = le32_to_cpu(pgp->rspPutInx);
3215 if (portRspPut >= portRspMax) {
3216 /*
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02003217 * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
dea31012005-04-17 16:05:31 -05003218 * rsp ring <portRspMax>
3219 */
James Smarted957682007-06-17 19:56:37 -05003220 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003221 "0303 Ring %d handler: portRspPut %d "
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02003222 "is bigger than rsp ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04003223 pring->ringno, portRspPut, portRspMax);
dea31012005-04-17 16:05:31 -05003224
James Smart2e0fef82007-06-17 19:56:36 -05003225 phba->link_state = LPFC_HBA_ERROR;
3226 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003227
3228 phba->work_hs = HS_FFER3;
3229 lpfc_handle_eratt(phba);
3230
James Smart3772a992009-05-22 14:50:54 -04003231 return;
dea31012005-04-17 16:05:31 -05003232 }
3233
3234 rmb();
dea31012005-04-17 16:05:31 -05003235 while (pring->rspidx != portRspPut) {
3236 /*
3237 * Build a completion list and call the appropriate handler.
3238 * The process is to get the next available response iocb, get
3239 * a free iocb from the list, copy the response data into the
3240 * free iocb, insert to the continuation list, and update the
3241 * next response index to slim. This process makes response
3242 * iocb's in the ring available to DMA as fast as possible but
3243 * pays a penalty for a copy operation. Since the iocb is
3244 * only 32 bytes, this penalty is considered small relative to
3245 * the PCI reads for register values and a slim write. When
3246 * the ulpLe field is set, the entire Command has been
3247 * received.
3248 */
James Smarted957682007-06-17 19:56:37 -05003249 entry = lpfc_resp_iocb(phba, pring);
3250
James Smart858c9f62007-06-17 19:56:39 -05003251 phba->last_completion_time = jiffies;
James Smart2e0fef82007-06-17 19:56:36 -05003252 rspiocbp = __lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05003253 if (rspiocbp == NULL) {
3254 printk(KERN_ERR "%s: out of buffers! Failing "
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07003255 "completion.\n", __func__);
dea31012005-04-17 16:05:31 -05003256 break;
3257 }
3258
James Smarted957682007-06-17 19:56:37 -05003259 lpfc_sli_pcimem_bcopy(entry, &rspiocbp->iocb,
3260 phba->iocb_rsp_size);
dea31012005-04-17 16:05:31 -05003261 irsp = &rspiocbp->iocb;
3262
3263 if (++pring->rspidx >= portRspMax)
3264 pring->rspidx = 0;
3265
James Smarta58cbd52007-08-02 11:09:43 -04003266 if (pring->ringno == LPFC_ELS_RING) {
3267 lpfc_debugfs_slow_ring_trc(phba,
3268 "IOCB rsp ring: wd4:x%08x wd6:x%08x wd7:x%08x",
3269 *(((uint32_t *) irsp) + 4),
3270 *(((uint32_t *) irsp) + 6),
3271 *(((uint32_t *) irsp) + 7));
3272 }
3273
James Smarted957682007-06-17 19:56:37 -05003274 writel(pring->rspidx, &phba->host_gp[pring->ringno].rspGetInx);
dea31012005-04-17 16:05:31 -05003275
James Smart3772a992009-05-22 14:50:54 -04003276 spin_unlock_irqrestore(&phba->hbalock, iflag);
3277 /* Handle the response IOCB */
3278 rspiocbp = lpfc_sli_sp_handle_rspiocb(phba, pring, rspiocbp);
3279 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003280
3281 /*
3282 * If the port response put pointer has not been updated, sync
3283 * the pgp->rspPutInx in the MAILBOX_tand fetch the new port
3284 * response put pointer.
3285 */
3286 if (pring->rspidx == portRspPut) {
3287 portRspPut = le32_to_cpu(pgp->rspPutInx);
3288 }
3289 } /* while (pring->rspidx != portRspPut) */
3290
James Smart92d7f7b2007-06-17 19:56:38 -05003291 if ((rspiocbp != NULL) && (mask & HA_R0RE_REQ)) {
dea31012005-04-17 16:05:31 -05003292 /* At least one response entry has been freed */
3293 pring->stats.iocb_rsp_full++;
3294 /* SET RxRE_RSP in Chip Att register */
3295 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
3296 writel(status, phba->CAregaddr);
3297 readl(phba->CAregaddr); /* flush */
3298 }
3299 if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
3300 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
3301 pring->stats.iocb_cmd_empty++;
3302
3303 /* Force update of the local copy of cmdGetInx */
3304 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
3305 lpfc_sli_resume_iocb(phba, pring);
3306
3307 if ((pring->lpfc_sli_cmd_available))
3308 (pring->lpfc_sli_cmd_available) (phba, pring);
3309
3310 }
3311
James Smart2e0fef82007-06-17 19:56:36 -05003312 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart3772a992009-05-22 14:50:54 -04003313 return;
dea31012005-04-17 16:05:31 -05003314}
3315
James Smarte59058c2008-08-24 21:49:00 -04003316/**
James Smart4f774512009-05-22 14:52:35 -04003317 * lpfc_sli_handle_slow_ring_event_s4 - Handle SLI4 slow-path els events
3318 * @phba: Pointer to HBA context object.
3319 * @pring: Pointer to driver SLI ring object.
3320 * @mask: Host attention register mask for this ring.
3321 *
3322 * This function is called from the worker thread when there is a pending
3323 * ELS response iocb on the driver internal slow-path response iocb worker
3324 * queue. The caller does not hold any lock. The function will remove each
3325 * response iocb from the response worker queue and calls the handle
3326 * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
3327 **/
3328static void
3329lpfc_sli_handle_slow_ring_event_s4(struct lpfc_hba *phba,
3330 struct lpfc_sli_ring *pring, uint32_t mask)
3331{
3332 struct lpfc_iocbq *irspiocbq;
James Smart4d9ab992009-10-02 15:16:39 -04003333 struct hbq_dmabuf *dmabuf;
3334 struct lpfc_cq_event *cq_event;
James Smart4f774512009-05-22 14:52:35 -04003335 unsigned long iflag;
3336
James Smart45ed1192009-10-02 15:17:02 -04003337 spin_lock_irqsave(&phba->hbalock, iflag);
3338 phba->hba_flag &= ~HBA_SP_QUEUE_EVT;
3339 spin_unlock_irqrestore(&phba->hbalock, iflag);
3340 while (!list_empty(&phba->sli4_hba.sp_queue_event)) {
James Smart4f774512009-05-22 14:52:35 -04003341 /* Get the response iocb from the head of work queue */
3342 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart45ed1192009-10-02 15:17:02 -04003343 list_remove_head(&phba->sli4_hba.sp_queue_event,
James Smart4d9ab992009-10-02 15:16:39 -04003344 cq_event, struct lpfc_cq_event, list);
James Smart4f774512009-05-22 14:52:35 -04003345 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart4d9ab992009-10-02 15:16:39 -04003346
3347 switch (bf_get(lpfc_wcqe_c_code, &cq_event->cqe.wcqe_cmpl)) {
3348 case CQE_CODE_COMPL_WQE:
3349 irspiocbq = container_of(cq_event, struct lpfc_iocbq,
3350 cq_event);
James Smart45ed1192009-10-02 15:17:02 -04003351 /* Translate ELS WCQE to response IOCBQ */
3352 irspiocbq = lpfc_sli4_els_wcqe_to_rspiocbq(phba,
3353 irspiocbq);
3354 if (irspiocbq)
3355 lpfc_sli_sp_handle_rspiocb(phba, pring,
3356 irspiocbq);
James Smart4d9ab992009-10-02 15:16:39 -04003357 break;
3358 case CQE_CODE_RECEIVE:
James Smart7851fe22011-07-22 18:36:52 -04003359 case CQE_CODE_RECEIVE_V1:
James Smart4d9ab992009-10-02 15:16:39 -04003360 dmabuf = container_of(cq_event, struct hbq_dmabuf,
3361 cq_event);
3362 lpfc_sli4_handle_received_buffer(phba, dmabuf);
3363 break;
3364 default:
3365 break;
3366 }
James Smart4f774512009-05-22 14:52:35 -04003367 }
3368}
3369
3370/**
James Smart3621a712009-04-06 18:47:14 -04003371 * lpfc_sli_abort_iocb_ring - Abort all iocbs in the ring
James Smarte59058c2008-08-24 21:49:00 -04003372 * @phba: Pointer to HBA context object.
3373 * @pring: Pointer to driver SLI ring object.
3374 *
3375 * This function aborts all iocbs in the given ring and frees all the iocb
3376 * objects in txq. This function issues an abort iocb for all the iocb commands
3377 * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
3378 * the return of this function. The caller is not required to hold any locks.
3379 **/
James Smart2e0fef82007-06-17 19:56:36 -05003380void
dea31012005-04-17 16:05:31 -05003381lpfc_sli_abort_iocb_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
3382{
James Smart2534ba72007-04-25 09:52:20 -04003383 LIST_HEAD(completions);
dea31012005-04-17 16:05:31 -05003384 struct lpfc_iocbq *iocb, *next_iocb;
dea31012005-04-17 16:05:31 -05003385
James Smart92d7f7b2007-06-17 19:56:38 -05003386 if (pring->ringno == LPFC_ELS_RING) {
3387 lpfc_fabric_abort_hba(phba);
3388 }
3389
dea31012005-04-17 16:05:31 -05003390 /* Error everything on txq and txcmplq
3391 * First do the txq.
3392 */
James Smart2e0fef82007-06-17 19:56:36 -05003393 spin_lock_irq(&phba->hbalock);
James Smart2534ba72007-04-25 09:52:20 -04003394 list_splice_init(&pring->txq, &completions);
dea31012005-04-17 16:05:31 -05003395 pring->txq_cnt = 0;
dea31012005-04-17 16:05:31 -05003396
3397 /* Next issue ABTS for everything on the txcmplq */
James Smart2534ba72007-04-25 09:52:20 -04003398 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
3399 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
3400
James Smart2e0fef82007-06-17 19:56:36 -05003401 spin_unlock_irq(&phba->hbalock);
James Smart2534ba72007-04-25 09:52:20 -04003402
James Smarta257bf92009-04-06 18:48:10 -04003403 /* Cancel all the IOCBs from the completions list */
3404 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
3405 IOERR_SLI_ABORTED);
dea31012005-04-17 16:05:31 -05003406}
3407
James Smarte59058c2008-08-24 21:49:00 -04003408/**
James Smart3621a712009-04-06 18:47:14 -04003409 * lpfc_sli_flush_fcp_rings - flush all iocbs in the fcp ring
James Smarta8e497d2008-08-24 21:50:11 -04003410 * @phba: Pointer to HBA context object.
3411 *
3412 * This function flushes all iocbs in the fcp ring and frees all the iocb
3413 * objects in txq and txcmplq. This function will not issue abort iocbs
3414 * for all the iocb commands in txcmplq, they will just be returned with
3415 * IOERR_SLI_DOWN. This function is invoked with EEH when device's PCI
3416 * slot has been permanently disabled.
3417 **/
3418void
3419lpfc_sli_flush_fcp_rings(struct lpfc_hba *phba)
3420{
3421 LIST_HEAD(txq);
3422 LIST_HEAD(txcmplq);
James Smarta8e497d2008-08-24 21:50:11 -04003423 struct lpfc_sli *psli = &phba->sli;
3424 struct lpfc_sli_ring *pring;
3425
3426 /* Currently, only one fcp ring */
3427 pring = &psli->ring[psli->fcp_ring];
3428
3429 spin_lock_irq(&phba->hbalock);
3430 /* Retrieve everything on txq */
3431 list_splice_init(&pring->txq, &txq);
3432 pring->txq_cnt = 0;
3433
3434 /* Retrieve everything on the txcmplq */
3435 list_splice_init(&pring->txcmplq, &txcmplq);
3436 pring->txcmplq_cnt = 0;
3437 spin_unlock_irq(&phba->hbalock);
3438
3439 /* Flush the txq */
James Smarta257bf92009-04-06 18:48:10 -04003440 lpfc_sli_cancel_iocbs(phba, &txq, IOSTAT_LOCAL_REJECT,
3441 IOERR_SLI_DOWN);
James Smarta8e497d2008-08-24 21:50:11 -04003442
3443 /* Flush the txcmpq */
James Smarta257bf92009-04-06 18:48:10 -04003444 lpfc_sli_cancel_iocbs(phba, &txcmplq, IOSTAT_LOCAL_REJECT,
3445 IOERR_SLI_DOWN);
James Smarta8e497d2008-08-24 21:50:11 -04003446}
3447
3448/**
James Smart3772a992009-05-22 14:50:54 -04003449 * lpfc_sli_brdready_s3 - Check for sli3 host ready status
James Smarte59058c2008-08-24 21:49:00 -04003450 * @phba: Pointer to HBA context object.
3451 * @mask: Bit mask to be checked.
3452 *
3453 * This function reads the host status register and compares
3454 * with the provided bit mask to check if HBA completed
3455 * the restart. This function will wait in a loop for the
3456 * HBA to complete restart. If the HBA does not restart within
3457 * 15 iterations, the function will reset the HBA again. The
3458 * function returns 1 when HBA fail to restart otherwise returns
3459 * zero.
3460 **/
James Smart3772a992009-05-22 14:50:54 -04003461static int
3462lpfc_sli_brdready_s3(struct lpfc_hba *phba, uint32_t mask)
dea31012005-04-17 16:05:31 -05003463{
Jamie Wellnitz41415862006-02-28 19:25:27 -05003464 uint32_t status;
3465 int i = 0;
3466 int retval = 0;
dea31012005-04-17 16:05:31 -05003467
Jamie Wellnitz41415862006-02-28 19:25:27 -05003468 /* Read the HBA Host Status Register */
James Smart9940b972011-03-11 16:06:12 -05003469 if (lpfc_readl(phba->HSregaddr, &status))
3470 return 1;
dea31012005-04-17 16:05:31 -05003471
Jamie Wellnitz41415862006-02-28 19:25:27 -05003472 /*
3473 * Check status register every 100ms for 5 retries, then every
3474 * 500ms for 5, then every 2.5 sec for 5, then reset board and
3475 * every 2.5 sec for 4.
3476 * Break our of the loop if errors occurred during init.
3477 */
3478 while (((status & mask) != mask) &&
3479 !(status & HS_FFERM) &&
3480 i++ < 20) {
dea31012005-04-17 16:05:31 -05003481
Jamie Wellnitz41415862006-02-28 19:25:27 -05003482 if (i <= 5)
3483 msleep(10);
3484 else if (i <= 10)
3485 msleep(500);
3486 else
3487 msleep(2500);
dea31012005-04-17 16:05:31 -05003488
Jamie Wellnitz41415862006-02-28 19:25:27 -05003489 if (i == 15) {
James Smart2e0fef82007-06-17 19:56:36 -05003490 /* Do post */
James Smart92d7f7b2007-06-17 19:56:38 -05003491 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003492 lpfc_sli_brdrestart(phba);
3493 }
3494 /* Read the HBA Host Status Register */
James Smart9940b972011-03-11 16:06:12 -05003495 if (lpfc_readl(phba->HSregaddr, &status)) {
3496 retval = 1;
3497 break;
3498 }
dea31012005-04-17 16:05:31 -05003499 }
dea31012005-04-17 16:05:31 -05003500
Jamie Wellnitz41415862006-02-28 19:25:27 -05003501 /* Check to see if any errors occurred during init */
3502 if ((status & HS_FFERM) || (i >= 20)) {
James Smarte40a02c2010-02-26 14:13:54 -05003503 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3504 "2751 Adapter failed to restart, "
3505 "status reg x%x, FW Data: A8 x%x AC x%x\n",
3506 status,
3507 readl(phba->MBslimaddr + 0xa8),
3508 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05003509 phba->link_state = LPFC_HBA_ERROR;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003510 retval = 1;
3511 }
dea31012005-04-17 16:05:31 -05003512
Jamie Wellnitz41415862006-02-28 19:25:27 -05003513 return retval;
dea31012005-04-17 16:05:31 -05003514}
3515
James Smartda0436e2009-05-22 14:51:39 -04003516/**
3517 * lpfc_sli_brdready_s4 - Check for sli4 host ready status
3518 * @phba: Pointer to HBA context object.
3519 * @mask: Bit mask to be checked.
3520 *
3521 * This function checks the host status register to check if HBA is
3522 * ready. This function will wait in a loop for the HBA to be ready
3523 * If the HBA is not ready , the function will will reset the HBA PCI
3524 * function again. The function returns 1 when HBA fail to be ready
3525 * otherwise returns zero.
3526 **/
3527static int
3528lpfc_sli_brdready_s4(struct lpfc_hba *phba, uint32_t mask)
3529{
3530 uint32_t status;
3531 int retval = 0;
3532
3533 /* Read the HBA Host Status Register */
3534 status = lpfc_sli4_post_status_check(phba);
3535
3536 if (status) {
3537 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
3538 lpfc_sli_brdrestart(phba);
3539 status = lpfc_sli4_post_status_check(phba);
3540 }
3541
3542 /* Check to see if any errors occurred during init */
3543 if (status) {
3544 phba->link_state = LPFC_HBA_ERROR;
3545 retval = 1;
3546 } else
3547 phba->sli4_hba.intr_enable = 0;
3548
3549 return retval;
3550}
3551
3552/**
3553 * lpfc_sli_brdready - Wrapper func for checking the hba readyness
3554 * @phba: Pointer to HBA context object.
3555 * @mask: Bit mask to be checked.
3556 *
3557 * This routine wraps the actual SLI3 or SLI4 hba readyness check routine
3558 * from the API jump table function pointer from the lpfc_hba struct.
3559 **/
3560int
3561lpfc_sli_brdready(struct lpfc_hba *phba, uint32_t mask)
3562{
3563 return phba->lpfc_sli_brdready(phba, mask);
3564}
3565
James Smart92908312006-03-07 15:04:13 -05003566#define BARRIER_TEST_PATTERN (0xdeadbeef)
3567
James Smarte59058c2008-08-24 21:49:00 -04003568/**
James Smart3621a712009-04-06 18:47:14 -04003569 * lpfc_reset_barrier - Make HBA ready for HBA reset
James Smarte59058c2008-08-24 21:49:00 -04003570 * @phba: Pointer to HBA context object.
3571 *
3572 * This function is called before resetting an HBA. This
3573 * function requests HBA to quiesce DMAs before a reset.
3574 **/
James Smart2e0fef82007-06-17 19:56:36 -05003575void lpfc_reset_barrier(struct lpfc_hba *phba)
James Smart92908312006-03-07 15:04:13 -05003576{
James Smart65a29c12006-07-06 15:50:50 -04003577 uint32_t __iomem *resp_buf;
3578 uint32_t __iomem *mbox_buf;
James Smart92908312006-03-07 15:04:13 -05003579 volatile uint32_t mbox;
James Smart9940b972011-03-11 16:06:12 -05003580 uint32_t hc_copy, ha_copy, resp_data;
James Smart92908312006-03-07 15:04:13 -05003581 int i;
3582 uint8_t hdrtype;
3583
3584 pci_read_config_byte(phba->pcidev, PCI_HEADER_TYPE, &hdrtype);
3585 if (hdrtype != 0x80 ||
3586 (FC_JEDEC_ID(phba->vpd.rev.biuRev) != HELIOS_JEDEC_ID &&
3587 FC_JEDEC_ID(phba->vpd.rev.biuRev) != THOR_JEDEC_ID))
3588 return;
3589
3590 /*
3591 * Tell the other part of the chip to suspend temporarily all
3592 * its DMA activity.
3593 */
James Smart65a29c12006-07-06 15:50:50 -04003594 resp_buf = phba->MBslimaddr;
James Smart92908312006-03-07 15:04:13 -05003595
3596 /* Disable the error attention */
James Smart9940b972011-03-11 16:06:12 -05003597 if (lpfc_readl(phba->HCregaddr, &hc_copy))
3598 return;
James Smart92908312006-03-07 15:04:13 -05003599 writel((hc_copy & ~HC_ERINT_ENA), phba->HCregaddr);
3600 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05003601 phba->link_flag |= LS_IGNORE_ERATT;
James Smart92908312006-03-07 15:04:13 -05003602
James Smart9940b972011-03-11 16:06:12 -05003603 if (lpfc_readl(phba->HAregaddr, &ha_copy))
3604 return;
3605 if (ha_copy & HA_ERATT) {
James Smart92908312006-03-07 15:04:13 -05003606 /* Clear Chip error bit */
3607 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05003608 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05003609 }
3610
3611 mbox = 0;
3612 ((MAILBOX_t *)&mbox)->mbxCommand = MBX_KILL_BOARD;
3613 ((MAILBOX_t *)&mbox)->mbxOwner = OWN_CHIP;
3614
3615 writel(BARRIER_TEST_PATTERN, (resp_buf + 1));
James Smart65a29c12006-07-06 15:50:50 -04003616 mbox_buf = phba->MBslimaddr;
James Smart92908312006-03-07 15:04:13 -05003617 writel(mbox, mbox_buf);
3618
James Smart9940b972011-03-11 16:06:12 -05003619 for (i = 0; i < 50; i++) {
3620 if (lpfc_readl((resp_buf + 1), &resp_data))
3621 return;
3622 if (resp_data != ~(BARRIER_TEST_PATTERN))
3623 mdelay(1);
3624 else
3625 break;
3626 }
3627 resp_data = 0;
3628 if (lpfc_readl((resp_buf + 1), &resp_data))
3629 return;
3630 if (resp_data != ~(BARRIER_TEST_PATTERN)) {
James Smartf4b4c682009-05-22 14:53:12 -04003631 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE ||
James Smart2e0fef82007-06-17 19:56:36 -05003632 phba->pport->stopped)
James Smart92908312006-03-07 15:04:13 -05003633 goto restore_hc;
3634 else
3635 goto clear_errat;
3636 }
3637
3638 ((MAILBOX_t *)&mbox)->mbxOwner = OWN_HOST;
James Smart9940b972011-03-11 16:06:12 -05003639 resp_data = 0;
3640 for (i = 0; i < 500; i++) {
3641 if (lpfc_readl(resp_buf, &resp_data))
3642 return;
3643 if (resp_data != mbox)
3644 mdelay(1);
3645 else
3646 break;
3647 }
James Smart92908312006-03-07 15:04:13 -05003648
3649clear_errat:
3650
James Smart9940b972011-03-11 16:06:12 -05003651 while (++i < 500) {
3652 if (lpfc_readl(phba->HAregaddr, &ha_copy))
3653 return;
3654 if (!(ha_copy & HA_ERATT))
3655 mdelay(1);
3656 else
3657 break;
3658 }
James Smart92908312006-03-07 15:04:13 -05003659
3660 if (readl(phba->HAregaddr) & HA_ERATT) {
3661 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05003662 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05003663 }
3664
3665restore_hc:
James Smart2e0fef82007-06-17 19:56:36 -05003666 phba->link_flag &= ~LS_IGNORE_ERATT;
James Smart92908312006-03-07 15:04:13 -05003667 writel(hc_copy, phba->HCregaddr);
3668 readl(phba->HCregaddr); /* flush */
3669}
3670
James Smarte59058c2008-08-24 21:49:00 -04003671/**
James Smart3621a712009-04-06 18:47:14 -04003672 * lpfc_sli_brdkill - Issue a kill_board mailbox command
James Smarte59058c2008-08-24 21:49:00 -04003673 * @phba: Pointer to HBA context object.
3674 *
3675 * This function issues a kill_board mailbox command and waits for
3676 * the error attention interrupt. This function is called for stopping
3677 * the firmware processing. The caller is not required to hold any
3678 * locks. This function calls lpfc_hba_down_post function to free
3679 * any pending commands after the kill. The function will return 1 when it
3680 * fails to kill the board else will return 0.
3681 **/
Jamie Wellnitz41415862006-02-28 19:25:27 -05003682int
James Smart2e0fef82007-06-17 19:56:36 -05003683lpfc_sli_brdkill(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05003684{
Jamie Wellnitz41415862006-02-28 19:25:27 -05003685 struct lpfc_sli *psli;
3686 LPFC_MBOXQ_t *pmb;
3687 uint32_t status;
3688 uint32_t ha_copy;
3689 int retval;
3690 int i = 0;
3691
3692 psli = &phba->sli;
3693
3694 /* Kill HBA */
James Smarted957682007-06-17 19:56:37 -05003695 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003696 "0329 Kill HBA Data: x%x x%x\n",
3697 phba->pport->port_state, psli->sli_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003698
James Smart98c9ea52007-10-27 13:37:33 -04003699 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3700 if (!pmb)
Jamie Wellnitz41415862006-02-28 19:25:27 -05003701 return 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003702
3703 /* Disable the error attention */
James Smart2e0fef82007-06-17 19:56:36 -05003704 spin_lock_irq(&phba->hbalock);
James Smart9940b972011-03-11 16:06:12 -05003705 if (lpfc_readl(phba->HCregaddr, &status)) {
3706 spin_unlock_irq(&phba->hbalock);
3707 mempool_free(pmb, phba->mbox_mem_pool);
3708 return 1;
3709 }
Jamie Wellnitz41415862006-02-28 19:25:27 -05003710 status &= ~HC_ERINT_ENA;
3711 writel(status, phba->HCregaddr);
3712 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05003713 phba->link_flag |= LS_IGNORE_ERATT;
3714 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003715
3716 lpfc_kill_board(phba, pmb);
3717 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
3718 retval = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
3719
3720 if (retval != MBX_SUCCESS) {
3721 if (retval != MBX_BUSY)
3722 mempool_free(pmb, phba->mbox_mem_pool);
James Smarte40a02c2010-02-26 14:13:54 -05003723 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3724 "2752 KILL_BOARD command failed retval %d\n",
3725 retval);
James Smart2e0fef82007-06-17 19:56:36 -05003726 spin_lock_irq(&phba->hbalock);
3727 phba->link_flag &= ~LS_IGNORE_ERATT;
3728 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003729 return 1;
3730 }
3731
James Smartf4b4c682009-05-22 14:53:12 -04003732 spin_lock_irq(&phba->hbalock);
3733 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
3734 spin_unlock_irq(&phba->hbalock);
James Smart92908312006-03-07 15:04:13 -05003735
Jamie Wellnitz41415862006-02-28 19:25:27 -05003736 mempool_free(pmb, phba->mbox_mem_pool);
3737
3738 /* There is no completion for a KILL_BOARD mbox cmd. Check for an error
3739 * attention every 100ms for 3 seconds. If we don't get ERATT after
3740 * 3 seconds we still set HBA_ERROR state because the status of the
3741 * board is now undefined.
3742 */
James Smart9940b972011-03-11 16:06:12 -05003743 if (lpfc_readl(phba->HAregaddr, &ha_copy))
3744 return 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003745 while ((i++ < 30) && !(ha_copy & HA_ERATT)) {
3746 mdelay(100);
James Smart9940b972011-03-11 16:06:12 -05003747 if (lpfc_readl(phba->HAregaddr, &ha_copy))
3748 return 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003749 }
3750
3751 del_timer_sync(&psli->mbox_tmo);
James Smart92908312006-03-07 15:04:13 -05003752 if (ha_copy & HA_ERATT) {
3753 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05003754 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05003755 }
James Smart2e0fef82007-06-17 19:56:36 -05003756 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003757 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart04c68492009-05-22 14:52:52 -04003758 psli->mbox_active = NULL;
James Smart2e0fef82007-06-17 19:56:36 -05003759 phba->link_flag &= ~LS_IGNORE_ERATT;
3760 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003761
Jamie Wellnitz41415862006-02-28 19:25:27 -05003762 lpfc_hba_down_post(phba);
James Smart2e0fef82007-06-17 19:56:36 -05003763 phba->link_state = LPFC_HBA_ERROR;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003764
James Smart2e0fef82007-06-17 19:56:36 -05003765 return ha_copy & HA_ERATT ? 0 : 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003766}
3767
James Smarte59058c2008-08-24 21:49:00 -04003768/**
James Smart3772a992009-05-22 14:50:54 -04003769 * lpfc_sli_brdreset - Reset a sli-2 or sli-3 HBA
James Smarte59058c2008-08-24 21:49:00 -04003770 * @phba: Pointer to HBA context object.
3771 *
3772 * This function resets the HBA by writing HC_INITFF to the control
3773 * register. After the HBA resets, this function resets all the iocb ring
3774 * indices. This function disables PCI layer parity checking during
3775 * the reset.
3776 * This function returns 0 always.
3777 * The caller is not required to hold any locks.
3778 **/
Jamie Wellnitz41415862006-02-28 19:25:27 -05003779int
James Smart2e0fef82007-06-17 19:56:36 -05003780lpfc_sli_brdreset(struct lpfc_hba *phba)
Jamie Wellnitz41415862006-02-28 19:25:27 -05003781{
3782 struct lpfc_sli *psli;
dea31012005-04-17 16:05:31 -05003783 struct lpfc_sli_ring *pring;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003784 uint16_t cfg_value;
dea31012005-04-17 16:05:31 -05003785 int i;
dea31012005-04-17 16:05:31 -05003786
Jamie Wellnitz41415862006-02-28 19:25:27 -05003787 psli = &phba->sli;
dea31012005-04-17 16:05:31 -05003788
Jamie Wellnitz41415862006-02-28 19:25:27 -05003789 /* Reset HBA */
3790 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003791 "0325 Reset HBA Data: x%x x%x\n",
James Smart2e0fef82007-06-17 19:56:36 -05003792 phba->pport->port_state, psli->sli_flag);
dea31012005-04-17 16:05:31 -05003793
3794 /* perform board reset */
3795 phba->fc_eventTag = 0;
James Smart4d9ab992009-10-02 15:16:39 -04003796 phba->link_events = 0;
James Smart2e0fef82007-06-17 19:56:36 -05003797 phba->pport->fc_myDID = 0;
3798 phba->pport->fc_prevDID = 0;
dea31012005-04-17 16:05:31 -05003799
Jamie Wellnitz41415862006-02-28 19:25:27 -05003800 /* Turn off parity checking and serr during the physical reset */
3801 pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
3802 pci_write_config_word(phba->pcidev, PCI_COMMAND,
3803 (cfg_value &
3804 ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
3805
James Smart3772a992009-05-22 14:50:54 -04003806 psli->sli_flag &= ~(LPFC_SLI_ACTIVE | LPFC_PROCESS_LA);
3807
Jamie Wellnitz41415862006-02-28 19:25:27 -05003808 /* Now toggle INITFF bit in the Host Control Register */
3809 writel(HC_INITFF, phba->HCregaddr);
3810 mdelay(1);
3811 readl(phba->HCregaddr); /* flush */
3812 writel(0, phba->HCregaddr);
3813 readl(phba->HCregaddr); /* flush */
3814
3815 /* Restore PCI cmd register */
3816 pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
dea31012005-04-17 16:05:31 -05003817
3818 /* Initialize relevant SLI info */
Jamie Wellnitz41415862006-02-28 19:25:27 -05003819 for (i = 0; i < psli->num_rings; i++) {
3820 pring = &psli->ring[i];
dea31012005-04-17 16:05:31 -05003821 pring->flag = 0;
3822 pring->rspidx = 0;
3823 pring->next_cmdidx = 0;
3824 pring->local_getidx = 0;
3825 pring->cmdidx = 0;
3826 pring->missbufcnt = 0;
3827 }
dea31012005-04-17 16:05:31 -05003828
James Smart2e0fef82007-06-17 19:56:36 -05003829 phba->link_state = LPFC_WARM_START;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003830 return 0;
3831}
3832
James Smarte59058c2008-08-24 21:49:00 -04003833/**
James Smartda0436e2009-05-22 14:51:39 -04003834 * lpfc_sli4_brdreset - Reset a sli-4 HBA
3835 * @phba: Pointer to HBA context object.
3836 *
3837 * This function resets a SLI4 HBA. This function disables PCI layer parity
3838 * checking during resets the device. The caller is not required to hold
3839 * any locks.
3840 *
3841 * This function returns 0 always.
3842 **/
3843int
3844lpfc_sli4_brdreset(struct lpfc_hba *phba)
3845{
3846 struct lpfc_sli *psli = &phba->sli;
3847 uint16_t cfg_value;
3848 uint8_t qindx;
3849
3850 /* Reset HBA */
3851 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3852 "0295 Reset HBA Data: x%x x%x\n",
3853 phba->pport->port_state, psli->sli_flag);
3854
3855 /* perform board reset */
3856 phba->fc_eventTag = 0;
James Smart4d9ab992009-10-02 15:16:39 -04003857 phba->link_events = 0;
James Smartda0436e2009-05-22 14:51:39 -04003858 phba->pport->fc_myDID = 0;
3859 phba->pport->fc_prevDID = 0;
3860
James Smartda0436e2009-05-22 14:51:39 -04003861 spin_lock_irq(&phba->hbalock);
3862 psli->sli_flag &= ~(LPFC_PROCESS_LA);
3863 phba->fcf.fcf_flag = 0;
3864 /* Clean up the child queue list for the CQs */
3865 list_del_init(&phba->sli4_hba.mbx_wq->list);
3866 list_del_init(&phba->sli4_hba.els_wq->list);
3867 list_del_init(&phba->sli4_hba.hdr_rq->list);
3868 list_del_init(&phba->sli4_hba.dat_rq->list);
3869 list_del_init(&phba->sli4_hba.mbx_cq->list);
3870 list_del_init(&phba->sli4_hba.els_cq->list);
James Smartda0436e2009-05-22 14:51:39 -04003871 for (qindx = 0; qindx < phba->cfg_fcp_wq_count; qindx++)
3872 list_del_init(&phba->sli4_hba.fcp_wq[qindx]->list);
James Smart05580562011-05-24 11:40:48 -04003873 qindx = 0;
3874 do
James Smartda0436e2009-05-22 14:51:39 -04003875 list_del_init(&phba->sli4_hba.fcp_cq[qindx]->list);
James Smart05580562011-05-24 11:40:48 -04003876 while (++qindx < phba->cfg_fcp_eq_count);
James Smartda0436e2009-05-22 14:51:39 -04003877 spin_unlock_irq(&phba->hbalock);
3878
3879 /* Now physically reset the device */
3880 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3881 "0389 Performing PCI function reset!\n");
James Smartbe858b62010-12-15 17:57:20 -05003882
3883 /* Turn off parity checking and serr during the physical reset */
3884 pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
3885 pci_write_config_word(phba->pcidev, PCI_COMMAND, (cfg_value &
3886 ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
3887
James Smartda0436e2009-05-22 14:51:39 -04003888 /* Perform FCoE PCI function reset */
3889 lpfc_pci_function_reset(phba);
3890
James Smartbe858b62010-12-15 17:57:20 -05003891 /* Restore PCI cmd register */
3892 pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
3893
James Smartda0436e2009-05-22 14:51:39 -04003894 return 0;
3895}
3896
3897/**
3898 * lpfc_sli_brdrestart_s3 - Restart a sli-3 hba
James Smarte59058c2008-08-24 21:49:00 -04003899 * @phba: Pointer to HBA context object.
3900 *
3901 * This function is called in the SLI initialization code path to
3902 * restart the HBA. The caller is not required to hold any lock.
3903 * This function writes MBX_RESTART mailbox command to the SLIM and
3904 * resets the HBA. At the end of the function, it calls lpfc_hba_down_post
3905 * function to free any pending commands. The function enables
3906 * POST only during the first initialization. The function returns zero.
3907 * The function does not guarantee completion of MBX_RESTART mailbox
3908 * command before the return of this function.
3909 **/
James Smartda0436e2009-05-22 14:51:39 -04003910static int
3911lpfc_sli_brdrestart_s3(struct lpfc_hba *phba)
Jamie Wellnitz41415862006-02-28 19:25:27 -05003912{
3913 MAILBOX_t *mb;
3914 struct lpfc_sli *psli;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003915 volatile uint32_t word0;
3916 void __iomem *to_slim;
James Smart0d878412009-10-02 15:16:56 -04003917 uint32_t hba_aer_enabled;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003918
James Smart2e0fef82007-06-17 19:56:36 -05003919 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003920
James Smart0d878412009-10-02 15:16:56 -04003921 /* Take PCIe device Advanced Error Reporting (AER) state */
3922 hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
3923
Jamie Wellnitz41415862006-02-28 19:25:27 -05003924 psli = &phba->sli;
3925
3926 /* Restart HBA */
3927 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003928 "0337 Restart HBA Data: x%x x%x\n",
James Smart2e0fef82007-06-17 19:56:36 -05003929 phba->pport->port_state, psli->sli_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003930
3931 word0 = 0;
3932 mb = (MAILBOX_t *) &word0;
3933 mb->mbxCommand = MBX_RESTART;
3934 mb->mbxHc = 1;
3935
James Smart92908312006-03-07 15:04:13 -05003936 lpfc_reset_barrier(phba);
3937
Jamie Wellnitz41415862006-02-28 19:25:27 -05003938 to_slim = phba->MBslimaddr;
3939 writel(*(uint32_t *) mb, to_slim);
3940 readl(to_slim); /* flush */
3941
3942 /* Only skip post after fc_ffinit is completed */
James Smarteaf15d52008-12-04 22:39:29 -05003943 if (phba->pport->port_state)
Jamie Wellnitz41415862006-02-28 19:25:27 -05003944 word0 = 1; /* This is really setting up word1 */
James Smarteaf15d52008-12-04 22:39:29 -05003945 else
Jamie Wellnitz41415862006-02-28 19:25:27 -05003946 word0 = 0; /* This is really setting up word1 */
James Smart65a29c12006-07-06 15:50:50 -04003947 to_slim = phba->MBslimaddr + sizeof (uint32_t);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003948 writel(*(uint32_t *) mb, to_slim);
3949 readl(to_slim); /* flush */
3950
3951 lpfc_sli_brdreset(phba);
James Smart2e0fef82007-06-17 19:56:36 -05003952 phba->pport->stopped = 0;
3953 phba->link_state = LPFC_INIT_START;
James Smartda0436e2009-05-22 14:51:39 -04003954 phba->hba_flag = 0;
James Smart2e0fef82007-06-17 19:56:36 -05003955 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003956
James Smart64ba8812006-08-02 15:24:34 -04003957 memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
3958 psli->stats_start = get_seconds();
3959
James Smarteaf15d52008-12-04 22:39:29 -05003960 /* Give the INITFF and Post time to settle. */
3961 mdelay(100);
dea31012005-04-17 16:05:31 -05003962
James Smart0d878412009-10-02 15:16:56 -04003963 /* Reset HBA AER if it was enabled, note hba_flag was reset above */
3964 if (hba_aer_enabled)
3965 pci_disable_pcie_error_reporting(phba->pcidev);
3966
Jamie Wellnitz41415862006-02-28 19:25:27 -05003967 lpfc_hba_down_post(phba);
dea31012005-04-17 16:05:31 -05003968
3969 return 0;
3970}
3971
James Smarte59058c2008-08-24 21:49:00 -04003972/**
James Smartda0436e2009-05-22 14:51:39 -04003973 * lpfc_sli_brdrestart_s4 - Restart the sli-4 hba
3974 * @phba: Pointer to HBA context object.
3975 *
3976 * This function is called in the SLI initialization code path to restart
3977 * a SLI4 HBA. The caller is not required to hold any lock.
3978 * At the end of the function, it calls lpfc_hba_down_post function to
3979 * free any pending commands.
3980 **/
3981static int
3982lpfc_sli_brdrestart_s4(struct lpfc_hba *phba)
3983{
3984 struct lpfc_sli *psli = &phba->sli;
James Smart75baf692010-06-08 18:31:21 -04003985 uint32_t hba_aer_enabled;
James Smartda0436e2009-05-22 14:51:39 -04003986
3987 /* Restart HBA */
3988 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3989 "0296 Restart HBA Data: x%x x%x\n",
3990 phba->pport->port_state, psli->sli_flag);
3991
James Smart75baf692010-06-08 18:31:21 -04003992 /* Take PCIe device Advanced Error Reporting (AER) state */
3993 hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
3994
James Smartda0436e2009-05-22 14:51:39 -04003995 lpfc_sli4_brdreset(phba);
3996
3997 spin_lock_irq(&phba->hbalock);
3998 phba->pport->stopped = 0;
3999 phba->link_state = LPFC_INIT_START;
4000 phba->hba_flag = 0;
4001 spin_unlock_irq(&phba->hbalock);
4002
4003 memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
4004 psli->stats_start = get_seconds();
4005
James Smart75baf692010-06-08 18:31:21 -04004006 /* Reset HBA AER if it was enabled, note hba_flag was reset above */
4007 if (hba_aer_enabled)
4008 pci_disable_pcie_error_reporting(phba->pcidev);
4009
James Smartda0436e2009-05-22 14:51:39 -04004010 lpfc_hba_down_post(phba);
4011
4012 return 0;
4013}
4014
4015/**
4016 * lpfc_sli_brdrestart - Wrapper func for restarting hba
4017 * @phba: Pointer to HBA context object.
4018 *
4019 * This routine wraps the actual SLI3 or SLI4 hba restart routine from the
4020 * API jump table function pointer from the lpfc_hba struct.
4021**/
4022int
4023lpfc_sli_brdrestart(struct lpfc_hba *phba)
4024{
4025 return phba->lpfc_sli_brdrestart(phba);
4026}
4027
4028/**
James Smart3621a712009-04-06 18:47:14 -04004029 * lpfc_sli_chipset_init - Wait for the restart of the HBA after a restart
James Smarte59058c2008-08-24 21:49:00 -04004030 * @phba: Pointer to HBA context object.
4031 *
4032 * This function is called after a HBA restart to wait for successful
4033 * restart of the HBA. Successful restart of the HBA is indicated by
4034 * HS_FFRDY and HS_MBRDY bits. If the HBA fails to restart even after 15
4035 * iteration, the function will restart the HBA again. The function returns
4036 * zero if HBA successfully restarted else returns negative error code.
4037 **/
dea31012005-04-17 16:05:31 -05004038static int
4039lpfc_sli_chipset_init(struct lpfc_hba *phba)
4040{
4041 uint32_t status, i = 0;
4042
4043 /* Read the HBA Host Status Register */
James Smart9940b972011-03-11 16:06:12 -05004044 if (lpfc_readl(phba->HSregaddr, &status))
4045 return -EIO;
dea31012005-04-17 16:05:31 -05004046
4047 /* Check status register to see what current state is */
4048 i = 0;
4049 while ((status & (HS_FFRDY | HS_MBRDY)) != (HS_FFRDY | HS_MBRDY)) {
4050
James Smartdcf2a4e2010-09-29 11:18:53 -04004051 /* Check every 10ms for 10 retries, then every 100ms for 90
4052 * retries, then every 1 sec for 50 retires for a total of
4053 * ~60 seconds before reset the board again and check every
4054 * 1 sec for 50 retries. The up to 60 seconds before the
4055 * board ready is required by the Falcon FIPS zeroization
4056 * complete, and any reset the board in between shall cause
4057 * restart of zeroization, further delay the board ready.
dea31012005-04-17 16:05:31 -05004058 */
James Smartdcf2a4e2010-09-29 11:18:53 -04004059 if (i++ >= 200) {
dea31012005-04-17 16:05:31 -05004060 /* Adapter failed to init, timeout, status reg
4061 <status> */
James Smarted957682007-06-17 19:56:37 -05004062 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004063 "0436 Adapter failed to init, "
James Smart09372822008-01-11 01:52:54 -05004064 "timeout, status reg x%x, "
4065 "FW Data: A8 x%x AC x%x\n", status,
4066 readl(phba->MBslimaddr + 0xa8),
4067 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05004068 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05004069 return -ETIMEDOUT;
4070 }
4071
4072 /* Check to see if any errors occurred during init */
4073 if (status & HS_FFERM) {
4074 /* ERROR: During chipset initialization */
4075 /* Adapter failed to init, chipset, status reg
4076 <status> */
James Smarted957682007-06-17 19:56:37 -05004077 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004078 "0437 Adapter failed to init, "
James Smart09372822008-01-11 01:52:54 -05004079 "chipset, status reg x%x, "
4080 "FW Data: A8 x%x AC x%x\n", status,
4081 readl(phba->MBslimaddr + 0xa8),
4082 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05004083 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05004084 return -EIO;
4085 }
4086
James Smartdcf2a4e2010-09-29 11:18:53 -04004087 if (i <= 10)
dea31012005-04-17 16:05:31 -05004088 msleep(10);
James Smartdcf2a4e2010-09-29 11:18:53 -04004089 else if (i <= 100)
4090 msleep(100);
4091 else
4092 msleep(1000);
dea31012005-04-17 16:05:31 -05004093
James Smartdcf2a4e2010-09-29 11:18:53 -04004094 if (i == 150) {
4095 /* Do post */
James Smart92d7f7b2007-06-17 19:56:38 -05004096 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004097 lpfc_sli_brdrestart(phba);
dea31012005-04-17 16:05:31 -05004098 }
4099 /* Read the HBA Host Status Register */
James Smart9940b972011-03-11 16:06:12 -05004100 if (lpfc_readl(phba->HSregaddr, &status))
4101 return -EIO;
dea31012005-04-17 16:05:31 -05004102 }
4103
4104 /* Check to see if any errors occurred during init */
4105 if (status & HS_FFERM) {
4106 /* ERROR: During chipset initialization */
4107 /* Adapter failed to init, chipset, status reg <status> */
James Smarted957682007-06-17 19:56:37 -05004108 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004109 "0438 Adapter failed to init, chipset, "
James Smart09372822008-01-11 01:52:54 -05004110 "status reg x%x, "
4111 "FW Data: A8 x%x AC x%x\n", status,
4112 readl(phba->MBslimaddr + 0xa8),
4113 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05004114 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05004115 return -EIO;
4116 }
4117
4118 /* Clear all interrupt enable conditions */
4119 writel(0, phba->HCregaddr);
4120 readl(phba->HCregaddr); /* flush */
4121
4122 /* setup host attn register */
4123 writel(0xffffffff, phba->HAregaddr);
4124 readl(phba->HAregaddr); /* flush */
4125 return 0;
4126}
4127
James Smarte59058c2008-08-24 21:49:00 -04004128/**
James Smart3621a712009-04-06 18:47:14 -04004129 * lpfc_sli_hbq_count - Get the number of HBQs to be configured
James Smarte59058c2008-08-24 21:49:00 -04004130 *
4131 * This function calculates and returns the number of HBQs required to be
4132 * configured.
4133 **/
James Smart78b2d852007-08-02 11:10:21 -04004134int
James Smarted957682007-06-17 19:56:37 -05004135lpfc_sli_hbq_count(void)
4136{
James Smart92d7f7b2007-06-17 19:56:38 -05004137 return ARRAY_SIZE(lpfc_hbq_defs);
James Smarted957682007-06-17 19:56:37 -05004138}
4139
James Smarte59058c2008-08-24 21:49:00 -04004140/**
James Smart3621a712009-04-06 18:47:14 -04004141 * lpfc_sli_hbq_entry_count - Calculate total number of hbq entries
James Smarte59058c2008-08-24 21:49:00 -04004142 *
4143 * This function adds the number of hbq entries in every HBQ to get
4144 * the total number of hbq entries required for the HBA and returns
4145 * the total count.
4146 **/
James Smarted957682007-06-17 19:56:37 -05004147static int
4148lpfc_sli_hbq_entry_count(void)
4149{
4150 int hbq_count = lpfc_sli_hbq_count();
4151 int count = 0;
4152 int i;
4153
4154 for (i = 0; i < hbq_count; ++i)
James Smart92d7f7b2007-06-17 19:56:38 -05004155 count += lpfc_hbq_defs[i]->entry_count;
James Smarted957682007-06-17 19:56:37 -05004156 return count;
4157}
4158
James Smarte59058c2008-08-24 21:49:00 -04004159/**
James Smart3621a712009-04-06 18:47:14 -04004160 * lpfc_sli_hbq_size - Calculate memory required for all hbq entries
James Smarte59058c2008-08-24 21:49:00 -04004161 *
4162 * This function calculates amount of memory required for all hbq entries
4163 * to be configured and returns the total memory required.
4164 **/
dea31012005-04-17 16:05:31 -05004165int
James Smarted957682007-06-17 19:56:37 -05004166lpfc_sli_hbq_size(void)
4167{
4168 return lpfc_sli_hbq_entry_count() * sizeof(struct lpfc_hbq_entry);
4169}
4170
James Smarte59058c2008-08-24 21:49:00 -04004171/**
James Smart3621a712009-04-06 18:47:14 -04004172 * lpfc_sli_hbq_setup - configure and initialize HBQs
James Smarte59058c2008-08-24 21:49:00 -04004173 * @phba: Pointer to HBA context object.
4174 *
4175 * This function is called during the SLI initialization to configure
4176 * all the HBQs and post buffers to the HBQ. The caller is not
4177 * required to hold any locks. This function will return zero if successful
4178 * else it will return negative error code.
4179 **/
James Smarted957682007-06-17 19:56:37 -05004180static int
4181lpfc_sli_hbq_setup(struct lpfc_hba *phba)
4182{
4183 int hbq_count = lpfc_sli_hbq_count();
4184 LPFC_MBOXQ_t *pmb;
4185 MAILBOX_t *pmbox;
4186 uint32_t hbqno;
4187 uint32_t hbq_entry_index;
James Smarted957682007-06-17 19:56:37 -05004188
James Smart92d7f7b2007-06-17 19:56:38 -05004189 /* Get a Mailbox buffer to setup mailbox
4190 * commands for HBA initialization
4191 */
James Smarted957682007-06-17 19:56:37 -05004192 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4193
4194 if (!pmb)
4195 return -ENOMEM;
4196
James Smart04c68492009-05-22 14:52:52 -04004197 pmbox = &pmb->u.mb;
James Smarted957682007-06-17 19:56:37 -05004198
4199 /* Initialize the struct lpfc_sli_hbq structure for each hbq */
4200 phba->link_state = LPFC_INIT_MBX_CMDS;
James Smart3163f722008-02-08 18:50:25 -05004201 phba->hbq_in_use = 1;
James Smarted957682007-06-17 19:56:37 -05004202
4203 hbq_entry_index = 0;
4204 for (hbqno = 0; hbqno < hbq_count; ++hbqno) {
4205 phba->hbqs[hbqno].next_hbqPutIdx = 0;
4206 phba->hbqs[hbqno].hbqPutIdx = 0;
4207 phba->hbqs[hbqno].local_hbqGetIdx = 0;
4208 phba->hbqs[hbqno].entry_count =
James Smart92d7f7b2007-06-17 19:56:38 -05004209 lpfc_hbq_defs[hbqno]->entry_count;
James Smart51ef4c22007-08-02 11:10:31 -04004210 lpfc_config_hbq(phba, hbqno, lpfc_hbq_defs[hbqno],
4211 hbq_entry_index, pmb);
James Smarted957682007-06-17 19:56:37 -05004212 hbq_entry_index += phba->hbqs[hbqno].entry_count;
4213
4214 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
4215 /* Adapter failed to init, mbxCmd <cmd> CFG_RING,
4216 mbxStatus <status>, ring <num> */
4217
4218 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -05004219 LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04004220 "1805 Adapter failed to init. "
James Smarted957682007-06-17 19:56:37 -05004221 "Data: x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04004222 pmbox->mbxCommand,
James Smarted957682007-06-17 19:56:37 -05004223 pmbox->mbxStatus, hbqno);
4224
4225 phba->link_state = LPFC_HBA_ERROR;
4226 mempool_free(pmb, phba->mbox_mem_pool);
James Smart6e7288d2010-06-07 15:23:35 -04004227 return -ENXIO;
James Smarted957682007-06-17 19:56:37 -05004228 }
4229 }
4230 phba->hbq_count = hbq_count;
4231
James Smarted957682007-06-17 19:56:37 -05004232 mempool_free(pmb, phba->mbox_mem_pool);
4233
James Smart92d7f7b2007-06-17 19:56:38 -05004234 /* Initially populate or replenish the HBQs */
James Smartd7c255b2008-08-24 21:50:00 -04004235 for (hbqno = 0; hbqno < hbq_count; ++hbqno)
4236 lpfc_sli_hbqbuf_init_hbqs(phba, hbqno);
James Smarted957682007-06-17 19:56:37 -05004237 return 0;
4238}
4239
James Smarte59058c2008-08-24 21:49:00 -04004240/**
James Smart4f774512009-05-22 14:52:35 -04004241 * lpfc_sli4_rb_setup - Initialize and post RBs to HBA
4242 * @phba: Pointer to HBA context object.
4243 *
4244 * This function is called during the SLI initialization to configure
4245 * all the HBQs and post buffers to the HBQ. The caller is not
4246 * required to hold any locks. This function will return zero if successful
4247 * else it will return negative error code.
4248 **/
4249static int
4250lpfc_sli4_rb_setup(struct lpfc_hba *phba)
4251{
4252 phba->hbq_in_use = 1;
4253 phba->hbqs[0].entry_count = lpfc_hbq_defs[0]->entry_count;
4254 phba->hbq_count = 1;
4255 /* Initially populate or replenish the HBQs */
4256 lpfc_sli_hbqbuf_init_hbqs(phba, 0);
4257 return 0;
4258}
4259
4260/**
James Smart3621a712009-04-06 18:47:14 -04004261 * lpfc_sli_config_port - Issue config port mailbox command
James Smarte59058c2008-08-24 21:49:00 -04004262 * @phba: Pointer to HBA context object.
4263 * @sli_mode: sli mode - 2/3
4264 *
4265 * This function is called by the sli intialization code path
4266 * to issue config_port mailbox command. This function restarts the
4267 * HBA firmware and issues a config_port mailbox command to configure
4268 * the SLI interface in the sli mode specified by sli_mode
4269 * variable. The caller is not required to hold any locks.
4270 * The function returns 0 if successful, else returns negative error
4271 * code.
4272 **/
James Smart93996272008-08-24 21:50:30 -04004273int
4274lpfc_sli_config_port(struct lpfc_hba *phba, int sli_mode)
dea31012005-04-17 16:05:31 -05004275{
4276 LPFC_MBOXQ_t *pmb;
4277 uint32_t resetcount = 0, rc = 0, done = 0;
4278
4279 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4280 if (!pmb) {
James Smart2e0fef82007-06-17 19:56:36 -05004281 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05004282 return -ENOMEM;
4283 }
4284
James Smarted957682007-06-17 19:56:37 -05004285 phba->sli_rev = sli_mode;
dea31012005-04-17 16:05:31 -05004286 while (resetcount < 2 && !done) {
James Smart2e0fef82007-06-17 19:56:36 -05004287 spin_lock_irq(&phba->hbalock);
James Smart1c067a42006-08-01 07:33:52 -04004288 phba->sli.sli_flag |= LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05004289 spin_unlock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -05004290 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004291 lpfc_sli_brdrestart(phba);
dea31012005-04-17 16:05:31 -05004292 rc = lpfc_sli_chipset_init(phba);
4293 if (rc)
4294 break;
4295
James Smart2e0fef82007-06-17 19:56:36 -05004296 spin_lock_irq(&phba->hbalock);
James Smart1c067a42006-08-01 07:33:52 -04004297 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05004298 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004299 resetcount++;
4300
James Smarted957682007-06-17 19:56:37 -05004301 /* Call pre CONFIG_PORT mailbox command initialization. A
4302 * value of 0 means the call was successful. Any other
4303 * nonzero value is a failure, but if ERESTART is returned,
4304 * the driver may reset the HBA and try again.
4305 */
dea31012005-04-17 16:05:31 -05004306 rc = lpfc_config_port_prep(phba);
4307 if (rc == -ERESTART) {
James Smarted957682007-06-17 19:56:37 -05004308 phba->link_state = LPFC_LINK_UNKNOWN;
dea31012005-04-17 16:05:31 -05004309 continue;
James Smart34b02dc2008-08-24 21:49:55 -04004310 } else if (rc)
dea31012005-04-17 16:05:31 -05004311 break;
James Smart6d368e52011-05-24 11:44:12 -04004312
James Smart2e0fef82007-06-17 19:56:36 -05004313 phba->link_state = LPFC_INIT_MBX_CMDS;
dea31012005-04-17 16:05:31 -05004314 lpfc_config_port(phba, pmb);
4315 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
James Smart34b02dc2008-08-24 21:49:55 -04004316 phba->sli3_options &= ~(LPFC_SLI3_NPIV_ENABLED |
4317 LPFC_SLI3_HBQ_ENABLED |
4318 LPFC_SLI3_CRP_ENABLED |
James Smartbc739052010-08-04 16:11:18 -04004319 LPFC_SLI3_BG_ENABLED |
4320 LPFC_SLI3_DSS_ENABLED);
James Smarted957682007-06-17 19:56:37 -05004321 if (rc != MBX_SUCCESS) {
dea31012005-04-17 16:05:31 -05004322 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004323 "0442 Adapter failed to init, mbxCmd x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05004324 "CONFIG_PORT, mbxStatus x%x Data: x%x\n",
James Smart04c68492009-05-22 14:52:52 -04004325 pmb->u.mb.mbxCommand, pmb->u.mb.mbxStatus, 0);
James Smart2e0fef82007-06-17 19:56:36 -05004326 spin_lock_irq(&phba->hbalock);
James Smart04c68492009-05-22 14:52:52 -04004327 phba->sli.sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05004328 spin_unlock_irq(&phba->hbalock);
4329 rc = -ENXIO;
James Smart04c68492009-05-22 14:52:52 -04004330 } else {
4331 /* Allow asynchronous mailbox command to go through */
4332 spin_lock_irq(&phba->hbalock);
4333 phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
4334 spin_unlock_irq(&phba->hbalock);
James Smarted957682007-06-17 19:56:37 -05004335 done = 1;
James Smart04c68492009-05-22 14:52:52 -04004336 }
dea31012005-04-17 16:05:31 -05004337 }
James Smarted957682007-06-17 19:56:37 -05004338 if (!done) {
4339 rc = -EINVAL;
4340 goto do_prep_failed;
4341 }
James Smart04c68492009-05-22 14:52:52 -04004342 if (pmb->u.mb.un.varCfgPort.sli_mode == 3) {
4343 if (!pmb->u.mb.un.varCfgPort.cMA) {
James Smart34b02dc2008-08-24 21:49:55 -04004344 rc = -ENXIO;
4345 goto do_prep_failed;
4346 }
James Smart04c68492009-05-22 14:52:52 -04004347 if (phba->max_vpi && pmb->u.mb.un.varCfgPort.gmv) {
James Smart34b02dc2008-08-24 21:49:55 -04004348 phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
James Smart04c68492009-05-22 14:52:52 -04004349 phba->max_vpi = pmb->u.mb.un.varCfgPort.max_vpi;
4350 phba->max_vports = (phba->max_vpi > phba->max_vports) ?
4351 phba->max_vpi : phba->max_vports;
4352
James Smart34b02dc2008-08-24 21:49:55 -04004353 } else
4354 phba->max_vpi = 0;
James Smartbc739052010-08-04 16:11:18 -04004355 phba->fips_level = 0;
4356 phba->fips_spec_rev = 0;
4357 if (pmb->u.mb.un.varCfgPort.gdss) {
James Smart04c68492009-05-22 14:52:52 -04004358 phba->sli3_options |= LPFC_SLI3_DSS_ENABLED;
James Smartbc739052010-08-04 16:11:18 -04004359 phba->fips_level = pmb->u.mb.un.varCfgPort.fips_level;
4360 phba->fips_spec_rev = pmb->u.mb.un.varCfgPort.fips_rev;
4361 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4362 "2850 Security Crypto Active. FIPS x%d "
4363 "(Spec Rev: x%d)",
4364 phba->fips_level, phba->fips_spec_rev);
4365 }
4366 if (pmb->u.mb.un.varCfgPort.sec_err) {
4367 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4368 "2856 Config Port Security Crypto "
4369 "Error: x%x ",
4370 pmb->u.mb.un.varCfgPort.sec_err);
4371 }
James Smart04c68492009-05-22 14:52:52 -04004372 if (pmb->u.mb.un.varCfgPort.gerbm)
James Smart34b02dc2008-08-24 21:49:55 -04004373 phba->sli3_options |= LPFC_SLI3_HBQ_ENABLED;
James Smart04c68492009-05-22 14:52:52 -04004374 if (pmb->u.mb.un.varCfgPort.gcrp)
James Smart34b02dc2008-08-24 21:49:55 -04004375 phba->sli3_options |= LPFC_SLI3_CRP_ENABLED;
James Smart6e7288d2010-06-07 15:23:35 -04004376
4377 phba->hbq_get = phba->mbox->us.s3_pgp.hbq_get;
4378 phba->port_gp = phba->mbox->us.s3_pgp.port;
James Smarte2a0a9d2008-12-04 22:40:02 -05004379
4380 if (phba->cfg_enable_bg) {
James Smart04c68492009-05-22 14:52:52 -04004381 if (pmb->u.mb.un.varCfgPort.gbg)
James Smarte2a0a9d2008-12-04 22:40:02 -05004382 phba->sli3_options |= LPFC_SLI3_BG_ENABLED;
4383 else
4384 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4385 "0443 Adapter did not grant "
4386 "BlockGuard\n");
4387 }
James Smart34b02dc2008-08-24 21:49:55 -04004388 } else {
James Smart8f34f4c2008-12-04 22:39:23 -05004389 phba->hbq_get = NULL;
James Smart34b02dc2008-08-24 21:49:55 -04004390 phba->port_gp = phba->mbox->us.s2.port;
James Smartd7c255b2008-08-24 21:50:00 -04004391 phba->max_vpi = 0;
James Smarted957682007-06-17 19:56:37 -05004392 }
James Smart92d7f7b2007-06-17 19:56:38 -05004393do_prep_failed:
James Smarted957682007-06-17 19:56:37 -05004394 mempool_free(pmb, phba->mbox_mem_pool);
4395 return rc;
4396}
4397
James Smarte59058c2008-08-24 21:49:00 -04004398
4399/**
James Smart3621a712009-04-06 18:47:14 -04004400 * lpfc_sli_hba_setup - SLI intialization function
James Smarte59058c2008-08-24 21:49:00 -04004401 * @phba: Pointer to HBA context object.
4402 *
4403 * This function is the main SLI intialization function. This function
4404 * is called by the HBA intialization code, HBA reset code and HBA
4405 * error attention handler code. Caller is not required to hold any
4406 * locks. This function issues config_port mailbox command to configure
4407 * the SLI, setup iocb rings and HBQ rings. In the end the function
4408 * calls the config_port_post function to issue init_link mailbox
4409 * command and to start the discovery. The function will return zero
4410 * if successful, else it will return negative error code.
4411 **/
James Smarted957682007-06-17 19:56:37 -05004412int
4413lpfc_sli_hba_setup(struct lpfc_hba *phba)
4414{
4415 uint32_t rc;
James Smart6d368e52011-05-24 11:44:12 -04004416 int mode = 3, i;
4417 int longs;
James Smarted957682007-06-17 19:56:37 -05004418
4419 switch (lpfc_sli_mode) {
4420 case 2:
James Smart78b2d852007-08-02 11:10:21 -04004421 if (phba->cfg_enable_npiv) {
James Smart92d7f7b2007-06-17 19:56:38 -05004422 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04004423 "1824 NPIV enabled: Override lpfc_sli_mode "
James Smart92d7f7b2007-06-17 19:56:38 -05004424 "parameter (%d) to auto (0).\n",
James Smarte8b62012007-08-02 11:10:09 -04004425 lpfc_sli_mode);
James Smart92d7f7b2007-06-17 19:56:38 -05004426 break;
4427 }
James Smarted957682007-06-17 19:56:37 -05004428 mode = 2;
4429 break;
4430 case 0:
4431 case 3:
4432 break;
4433 default:
James Smart92d7f7b2007-06-17 19:56:38 -05004434 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04004435 "1819 Unrecognized lpfc_sli_mode "
4436 "parameter: %d.\n", lpfc_sli_mode);
James Smarted957682007-06-17 19:56:37 -05004437
4438 break;
4439 }
4440
James Smart93996272008-08-24 21:50:30 -04004441 rc = lpfc_sli_config_port(phba, mode);
4442
James Smarted957682007-06-17 19:56:37 -05004443 if (rc && lpfc_sli_mode == 3)
James Smart92d7f7b2007-06-17 19:56:38 -05004444 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04004445 "1820 Unable to select SLI-3. "
4446 "Not supported by adapter.\n");
James Smarted957682007-06-17 19:56:37 -05004447 if (rc && mode != 2)
James Smart93996272008-08-24 21:50:30 -04004448 rc = lpfc_sli_config_port(phba, 2);
James Smarted957682007-06-17 19:56:37 -05004449 if (rc)
dea31012005-04-17 16:05:31 -05004450 goto lpfc_sli_hba_setup_error;
4451
James Smart0d878412009-10-02 15:16:56 -04004452 /* Enable PCIe device Advanced Error Reporting (AER) if configured */
4453 if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
4454 rc = pci_enable_pcie_error_reporting(phba->pcidev);
4455 if (!rc) {
4456 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4457 "2709 This device supports "
4458 "Advanced Error Reporting (AER)\n");
4459 spin_lock_irq(&phba->hbalock);
4460 phba->hba_flag |= HBA_AER_ENABLED;
4461 spin_unlock_irq(&phba->hbalock);
4462 } else {
4463 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4464 "2708 This device does not support "
4465 "Advanced Error Reporting (AER)\n");
4466 phba->cfg_aer_support = 0;
4467 }
4468 }
4469
James Smarted957682007-06-17 19:56:37 -05004470 if (phba->sli_rev == 3) {
4471 phba->iocb_cmd_size = SLI3_IOCB_CMD_SIZE;
4472 phba->iocb_rsp_size = SLI3_IOCB_RSP_SIZE;
James Smarted957682007-06-17 19:56:37 -05004473 } else {
4474 phba->iocb_cmd_size = SLI2_IOCB_CMD_SIZE;
4475 phba->iocb_rsp_size = SLI2_IOCB_RSP_SIZE;
James Smart92d7f7b2007-06-17 19:56:38 -05004476 phba->sli3_options = 0;
James Smarted957682007-06-17 19:56:37 -05004477 }
4478
4479 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004480 "0444 Firmware in SLI %x mode. Max_vpi %d\n",
4481 phba->sli_rev, phba->max_vpi);
James Smarted957682007-06-17 19:56:37 -05004482 rc = lpfc_sli_ring_map(phba);
dea31012005-04-17 16:05:31 -05004483
4484 if (rc)
4485 goto lpfc_sli_hba_setup_error;
4486
James Smart6d368e52011-05-24 11:44:12 -04004487 /* Initialize VPIs. */
4488 if (phba->sli_rev == LPFC_SLI_REV3) {
4489 /*
4490 * The VPI bitmask and physical ID array are allocated
4491 * and initialized once only - at driver load. A port
4492 * reset doesn't need to reinitialize this memory.
4493 */
4494 if ((phba->vpi_bmask == NULL) && (phba->vpi_ids == NULL)) {
4495 longs = (phba->max_vpi + BITS_PER_LONG) / BITS_PER_LONG;
4496 phba->vpi_bmask = kzalloc(longs * sizeof(unsigned long),
4497 GFP_KERNEL);
4498 if (!phba->vpi_bmask) {
4499 rc = -ENOMEM;
4500 goto lpfc_sli_hba_setup_error;
4501 }
4502
4503 phba->vpi_ids = kzalloc(
4504 (phba->max_vpi+1) * sizeof(uint16_t),
4505 GFP_KERNEL);
4506 if (!phba->vpi_ids) {
4507 kfree(phba->vpi_bmask);
4508 rc = -ENOMEM;
4509 goto lpfc_sli_hba_setup_error;
4510 }
4511 for (i = 0; i < phba->max_vpi; i++)
4512 phba->vpi_ids[i] = i;
4513 }
4514 }
4515
James Smart93996272008-08-24 21:50:30 -04004516 /* Init HBQs */
James Smarted957682007-06-17 19:56:37 -05004517 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
4518 rc = lpfc_sli_hbq_setup(phba);
4519 if (rc)
4520 goto lpfc_sli_hba_setup_error;
4521 }
James Smart04c68492009-05-22 14:52:52 -04004522 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004523 phba->sli.sli_flag |= LPFC_PROCESS_LA;
James Smart04c68492009-05-22 14:52:52 -04004524 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004525
4526 rc = lpfc_config_port_post(phba);
4527 if (rc)
4528 goto lpfc_sli_hba_setup_error;
4529
James Smarted957682007-06-17 19:56:37 -05004530 return rc;
4531
James Smart92d7f7b2007-06-17 19:56:38 -05004532lpfc_sli_hba_setup_error:
James Smart2e0fef82007-06-17 19:56:36 -05004533 phba->link_state = LPFC_HBA_ERROR;
James Smarte40a02c2010-02-26 14:13:54 -05004534 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004535 "0445 Firmware initialization failed\n");
dea31012005-04-17 16:05:31 -05004536 return rc;
4537}
4538
James Smartda0436e2009-05-22 14:51:39 -04004539/**
4540 * lpfc_sli4_read_fcoe_params - Read fcoe params from conf region
4541 * @phba: Pointer to HBA context object.
4542 * @mboxq: mailbox pointer.
4543 * This function issue a dump mailbox command to read config region
4544 * 23 and parse the records in the region and populate driver
4545 * data structure.
4546 **/
4547static int
4548lpfc_sli4_read_fcoe_params(struct lpfc_hba *phba,
4549 LPFC_MBOXQ_t *mboxq)
4550{
4551 struct lpfc_dmabuf *mp;
4552 struct lpfc_mqe *mqe;
4553 uint32_t data_length;
4554 int rc;
4555
4556 /* Program the default value of vlan_id and fc_map */
4557 phba->valid_vlan = 0;
4558 phba->fc_map[0] = LPFC_FCOE_FCF_MAP0;
4559 phba->fc_map[1] = LPFC_FCOE_FCF_MAP1;
4560 phba->fc_map[2] = LPFC_FCOE_FCF_MAP2;
4561
4562 mqe = &mboxq->u.mqe;
4563 if (lpfc_dump_fcoe_param(phba, mboxq))
4564 return -ENOMEM;
4565
4566 mp = (struct lpfc_dmabuf *) mboxq->context1;
4567 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4568
4569 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
4570 "(%d):2571 Mailbox cmd x%x Status x%x "
4571 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x "
4572 "x%x x%x x%x x%x x%x x%x x%x x%x x%x "
4573 "CQ: x%x x%x x%x x%x\n",
4574 mboxq->vport ? mboxq->vport->vpi : 0,
4575 bf_get(lpfc_mqe_command, mqe),
4576 bf_get(lpfc_mqe_status, mqe),
4577 mqe->un.mb_words[0], mqe->un.mb_words[1],
4578 mqe->un.mb_words[2], mqe->un.mb_words[3],
4579 mqe->un.mb_words[4], mqe->un.mb_words[5],
4580 mqe->un.mb_words[6], mqe->un.mb_words[7],
4581 mqe->un.mb_words[8], mqe->un.mb_words[9],
4582 mqe->un.mb_words[10], mqe->un.mb_words[11],
4583 mqe->un.mb_words[12], mqe->un.mb_words[13],
4584 mqe->un.mb_words[14], mqe->un.mb_words[15],
4585 mqe->un.mb_words[16], mqe->un.mb_words[50],
4586 mboxq->mcqe.word0,
4587 mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
4588 mboxq->mcqe.trailer);
4589
4590 if (rc) {
4591 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4592 kfree(mp);
4593 return -EIO;
4594 }
4595 data_length = mqe->un.mb_words[5];
James Smarta0c87cb2009-07-19 10:01:10 -04004596 if (data_length > DMP_RGN23_SIZE) {
James Smartd11e31d2009-06-10 17:23:06 -04004597 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4598 kfree(mp);
James Smartda0436e2009-05-22 14:51:39 -04004599 return -EIO;
James Smartd11e31d2009-06-10 17:23:06 -04004600 }
James Smartda0436e2009-05-22 14:51:39 -04004601
4602 lpfc_parse_fcoe_conf(phba, mp->virt, data_length);
4603 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4604 kfree(mp);
4605 return 0;
4606}
4607
4608/**
4609 * lpfc_sli4_read_rev - Issue READ_REV and collect vpd data
4610 * @phba: pointer to lpfc hba data structure.
4611 * @mboxq: pointer to the LPFC_MBOXQ_t structure.
4612 * @vpd: pointer to the memory to hold resulting port vpd data.
4613 * @vpd_size: On input, the number of bytes allocated to @vpd.
4614 * On output, the number of data bytes in @vpd.
4615 *
4616 * This routine executes a READ_REV SLI4 mailbox command. In
4617 * addition, this routine gets the port vpd data.
4618 *
4619 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02004620 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -04004621 * -ENOMEM - could not allocated memory.
James Smartda0436e2009-05-22 14:51:39 -04004622 **/
4623static int
4624lpfc_sli4_read_rev(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
4625 uint8_t *vpd, uint32_t *vpd_size)
4626{
4627 int rc = 0;
4628 uint32_t dma_size;
4629 struct lpfc_dmabuf *dmabuf;
4630 struct lpfc_mqe *mqe;
4631
4632 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
4633 if (!dmabuf)
4634 return -ENOMEM;
4635
4636 /*
4637 * Get a DMA buffer for the vpd data resulting from the READ_REV
4638 * mailbox command.
4639 */
4640 dma_size = *vpd_size;
4641 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
4642 dma_size,
4643 &dmabuf->phys,
4644 GFP_KERNEL);
4645 if (!dmabuf->virt) {
4646 kfree(dmabuf);
4647 return -ENOMEM;
4648 }
4649 memset(dmabuf->virt, 0, dma_size);
4650
4651 /*
4652 * The SLI4 implementation of READ_REV conflicts at word1,
4653 * bits 31:16 and SLI4 adds vpd functionality not present
4654 * in SLI3. This code corrects the conflicts.
4655 */
4656 lpfc_read_rev(phba, mboxq);
4657 mqe = &mboxq->u.mqe;
4658 mqe->un.read_rev.vpd_paddr_high = putPaddrHigh(dmabuf->phys);
4659 mqe->un.read_rev.vpd_paddr_low = putPaddrLow(dmabuf->phys);
4660 mqe->un.read_rev.word1 &= 0x0000FFFF;
4661 bf_set(lpfc_mbx_rd_rev_vpd, &mqe->un.read_rev, 1);
4662 bf_set(lpfc_mbx_rd_rev_avail_len, &mqe->un.read_rev, dma_size);
4663
4664 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4665 if (rc) {
4666 dma_free_coherent(&phba->pcidev->dev, dma_size,
4667 dmabuf->virt, dmabuf->phys);
James Smartdef9c7a2009-12-21 17:02:28 -05004668 kfree(dmabuf);
James Smartda0436e2009-05-22 14:51:39 -04004669 return -EIO;
4670 }
4671
James Smartda0436e2009-05-22 14:51:39 -04004672 /*
4673 * The available vpd length cannot be bigger than the
4674 * DMA buffer passed to the port. Catch the less than
4675 * case and update the caller's size.
4676 */
4677 if (mqe->un.read_rev.avail_vpd_len < *vpd_size)
4678 *vpd_size = mqe->un.read_rev.avail_vpd_len;
4679
James Smartd7c47992010-06-08 18:31:54 -04004680 memcpy(vpd, dmabuf->virt, *vpd_size);
4681
James Smartda0436e2009-05-22 14:51:39 -04004682 dma_free_coherent(&phba->pcidev->dev, dma_size,
4683 dmabuf->virt, dmabuf->phys);
4684 kfree(dmabuf);
4685 return 0;
4686}
4687
4688/**
4689 * lpfc_sli4_arm_cqeq_intr - Arm sli-4 device completion and event queues
4690 * @phba: pointer to lpfc hba data structure.
4691 *
4692 * This routine is called to explicitly arm the SLI4 device's completion and
4693 * event queues
4694 **/
4695static void
4696lpfc_sli4_arm_cqeq_intr(struct lpfc_hba *phba)
4697{
4698 uint8_t fcp_eqidx;
4699
4700 lpfc_sli4_cq_release(phba->sli4_hba.mbx_cq, LPFC_QUEUE_REARM);
4701 lpfc_sli4_cq_release(phba->sli4_hba.els_cq, LPFC_QUEUE_REARM);
James Smart05580562011-05-24 11:40:48 -04004702 fcp_eqidx = 0;
4703 do
James Smartda0436e2009-05-22 14:51:39 -04004704 lpfc_sli4_cq_release(phba->sli4_hba.fcp_cq[fcp_eqidx],
4705 LPFC_QUEUE_REARM);
James Smart05580562011-05-24 11:40:48 -04004706 while (++fcp_eqidx < phba->cfg_fcp_eq_count);
James Smartda0436e2009-05-22 14:51:39 -04004707 lpfc_sli4_eq_release(phba->sli4_hba.sp_eq, LPFC_QUEUE_REARM);
4708 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count; fcp_eqidx++)
4709 lpfc_sli4_eq_release(phba->sli4_hba.fp_eq[fcp_eqidx],
4710 LPFC_QUEUE_REARM);
4711}
4712
4713/**
James Smart6d368e52011-05-24 11:44:12 -04004714 * lpfc_sli4_get_avail_extnt_rsrc - Get available resource extent count.
4715 * @phba: Pointer to HBA context object.
4716 * @type: The resource extent type.
James Smartb76f2dc2011-07-22 18:37:42 -04004717 * @extnt_count: buffer to hold port available extent count.
4718 * @extnt_size: buffer to hold element count per extent.
James Smart6d368e52011-05-24 11:44:12 -04004719 *
James Smartb76f2dc2011-07-22 18:37:42 -04004720 * This function calls the port and retrievs the number of available
4721 * extents and their size for a particular extent type.
4722 *
4723 * Returns: 0 if successful. Nonzero otherwise.
James Smart6d368e52011-05-24 11:44:12 -04004724 **/
James Smartb76f2dc2011-07-22 18:37:42 -04004725int
James Smart6d368e52011-05-24 11:44:12 -04004726lpfc_sli4_get_avail_extnt_rsrc(struct lpfc_hba *phba, uint16_t type,
4727 uint16_t *extnt_count, uint16_t *extnt_size)
4728{
4729 int rc = 0;
4730 uint32_t length;
4731 uint32_t mbox_tmo;
4732 struct lpfc_mbx_get_rsrc_extent_info *rsrc_info;
4733 LPFC_MBOXQ_t *mbox;
4734
4735 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4736 if (!mbox)
4737 return -ENOMEM;
4738
4739 /* Find out how many extents are available for this resource type */
4740 length = (sizeof(struct lpfc_mbx_get_rsrc_extent_info) -
4741 sizeof(struct lpfc_sli4_cfg_mhdr));
4742 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
4743 LPFC_MBOX_OPCODE_GET_RSRC_EXTENT_INFO,
4744 length, LPFC_SLI4_MBX_EMBED);
4745
4746 /* Send an extents count of 0 - the GET doesn't use it. */
4747 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, 0, type,
4748 LPFC_SLI4_MBX_EMBED);
4749 if (unlikely(rc)) {
4750 rc = -EIO;
4751 goto err_exit;
4752 }
4753
4754 if (!phba->sli4_hba.intr_enable)
4755 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
4756 else {
4757 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_SLI4_CONFIG);
4758 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
4759 }
4760 if (unlikely(rc)) {
4761 rc = -EIO;
4762 goto err_exit;
4763 }
4764
4765 rsrc_info = &mbox->u.mqe.un.rsrc_extent_info;
4766 if (bf_get(lpfc_mbox_hdr_status,
4767 &rsrc_info->header.cfg_shdr.response)) {
4768 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
4769 "2930 Failed to get resource extents "
4770 "Status 0x%x Add'l Status 0x%x\n",
4771 bf_get(lpfc_mbox_hdr_status,
4772 &rsrc_info->header.cfg_shdr.response),
4773 bf_get(lpfc_mbox_hdr_add_status,
4774 &rsrc_info->header.cfg_shdr.response));
4775 rc = -EIO;
4776 goto err_exit;
4777 }
4778
4779 *extnt_count = bf_get(lpfc_mbx_get_rsrc_extent_info_cnt,
4780 &rsrc_info->u.rsp);
4781 *extnt_size = bf_get(lpfc_mbx_get_rsrc_extent_info_size,
4782 &rsrc_info->u.rsp);
4783 err_exit:
4784 mempool_free(mbox, phba->mbox_mem_pool);
4785 return rc;
4786}
4787
4788/**
4789 * lpfc_sli4_chk_avail_extnt_rsrc - Check for available SLI4 resource extents.
4790 * @phba: Pointer to HBA context object.
4791 * @type: The extent type to check.
4792 *
4793 * This function reads the current available extents from the port and checks
4794 * if the extent count or extent size has changed since the last access.
4795 * Callers use this routine post port reset to understand if there is a
4796 * extent reprovisioning requirement.
4797 *
4798 * Returns:
4799 * -Error: error indicates problem.
4800 * 1: Extent count or size has changed.
4801 * 0: No changes.
4802 **/
4803static int
4804lpfc_sli4_chk_avail_extnt_rsrc(struct lpfc_hba *phba, uint16_t type)
4805{
4806 uint16_t curr_ext_cnt, rsrc_ext_cnt;
4807 uint16_t size_diff, rsrc_ext_size;
4808 int rc = 0;
4809 struct lpfc_rsrc_blks *rsrc_entry;
4810 struct list_head *rsrc_blk_list = NULL;
4811
4812 size_diff = 0;
4813 curr_ext_cnt = 0;
4814 rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,
4815 &rsrc_ext_cnt,
4816 &rsrc_ext_size);
4817 if (unlikely(rc))
4818 return -EIO;
4819
4820 switch (type) {
4821 case LPFC_RSC_TYPE_FCOE_RPI:
4822 rsrc_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;
4823 break;
4824 case LPFC_RSC_TYPE_FCOE_VPI:
4825 rsrc_blk_list = &phba->lpfc_vpi_blk_list;
4826 break;
4827 case LPFC_RSC_TYPE_FCOE_XRI:
4828 rsrc_blk_list = &phba->sli4_hba.lpfc_xri_blk_list;
4829 break;
4830 case LPFC_RSC_TYPE_FCOE_VFI:
4831 rsrc_blk_list = &phba->sli4_hba.lpfc_vfi_blk_list;
4832 break;
4833 default:
4834 break;
4835 }
4836
4837 list_for_each_entry(rsrc_entry, rsrc_blk_list, list) {
4838 curr_ext_cnt++;
4839 if (rsrc_entry->rsrc_size != rsrc_ext_size)
4840 size_diff++;
4841 }
4842
4843 if (curr_ext_cnt != rsrc_ext_cnt || size_diff != 0)
4844 rc = 1;
4845
4846 return rc;
4847}
4848
4849/**
4850 * lpfc_sli4_cfg_post_extnts -
4851 * @phba: Pointer to HBA context object.
4852 * @extnt_cnt - number of available extents.
4853 * @type - the extent type (rpi, xri, vfi, vpi).
4854 * @emb - buffer to hold either MBX_EMBED or MBX_NEMBED operation.
4855 * @mbox - pointer to the caller's allocated mailbox structure.
4856 *
4857 * This function executes the extents allocation request. It also
4858 * takes care of the amount of memory needed to allocate or get the
4859 * allocated extents. It is the caller's responsibility to evaluate
4860 * the response.
4861 *
4862 * Returns:
4863 * -Error: Error value describes the condition found.
4864 * 0: if successful
4865 **/
4866static int
4867lpfc_sli4_cfg_post_extnts(struct lpfc_hba *phba, uint16_t *extnt_cnt,
4868 uint16_t type, bool *emb, LPFC_MBOXQ_t *mbox)
4869{
4870 int rc = 0;
4871 uint32_t req_len;
4872 uint32_t emb_len;
4873 uint32_t alloc_len, mbox_tmo;
4874
4875 /* Calculate the total requested length of the dma memory */
4876 req_len = *extnt_cnt * sizeof(uint16_t);
4877
4878 /*
4879 * Calculate the size of an embedded mailbox. The uint32_t
4880 * accounts for extents-specific word.
4881 */
4882 emb_len = sizeof(MAILBOX_t) - sizeof(struct mbox_header) -
4883 sizeof(uint32_t);
4884
4885 /*
4886 * Presume the allocation and response will fit into an embedded
4887 * mailbox. If not true, reconfigure to a non-embedded mailbox.
4888 */
4889 *emb = LPFC_SLI4_MBX_EMBED;
4890 if (req_len > emb_len) {
4891 req_len = *extnt_cnt * sizeof(uint16_t) +
4892 sizeof(union lpfc_sli4_cfg_shdr) +
4893 sizeof(uint32_t);
4894 *emb = LPFC_SLI4_MBX_NEMBED;
4895 }
4896
4897 alloc_len = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
4898 LPFC_MBOX_OPCODE_ALLOC_RSRC_EXTENT,
4899 req_len, *emb);
4900 if (alloc_len < req_len) {
4901 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartb76f2dc2011-07-22 18:37:42 -04004902 "2982 Allocated DMA memory size (x%x) is "
James Smart6d368e52011-05-24 11:44:12 -04004903 "less than the requested DMA memory "
4904 "size (x%x)\n", alloc_len, req_len);
4905 return -ENOMEM;
4906 }
4907 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, *extnt_cnt, type, *emb);
4908 if (unlikely(rc))
4909 return -EIO;
4910
4911 if (!phba->sli4_hba.intr_enable)
4912 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
4913 else {
4914 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_SLI4_CONFIG);
4915 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
4916 }
4917
4918 if (unlikely(rc))
4919 rc = -EIO;
4920 return rc;
4921}
4922
4923/**
4924 * lpfc_sli4_alloc_extent - Allocate an SLI4 resource extent.
4925 * @phba: Pointer to HBA context object.
4926 * @type: The resource extent type to allocate.
4927 *
4928 * This function allocates the number of elements for the specified
4929 * resource type.
4930 **/
4931static int
4932lpfc_sli4_alloc_extent(struct lpfc_hba *phba, uint16_t type)
4933{
4934 bool emb = false;
4935 uint16_t rsrc_id_cnt, rsrc_cnt, rsrc_size;
4936 uint16_t rsrc_id, rsrc_start, j, k;
4937 uint16_t *ids;
4938 int i, rc;
4939 unsigned long longs;
4940 unsigned long *bmask;
4941 struct lpfc_rsrc_blks *rsrc_blks;
4942 LPFC_MBOXQ_t *mbox;
4943 uint32_t length;
4944 struct lpfc_id_range *id_array = NULL;
4945 void *virtaddr = NULL;
4946 struct lpfc_mbx_nembed_rsrc_extent *n_rsrc;
4947 struct lpfc_mbx_alloc_rsrc_extents *rsrc_ext;
4948 struct list_head *ext_blk_list;
4949
4950 rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,
4951 &rsrc_cnt,
4952 &rsrc_size);
4953 if (unlikely(rc))
4954 return -EIO;
4955
4956 if ((rsrc_cnt == 0) || (rsrc_size == 0)) {
4957 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
4958 "3009 No available Resource Extents "
4959 "for resource type 0x%x: Count: 0x%x, "
4960 "Size 0x%x\n", type, rsrc_cnt,
4961 rsrc_size);
4962 return -ENOMEM;
4963 }
4964
4965 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_INIT,
4966 "2903 Available Resource Extents "
4967 "for resource type 0x%x: Count: 0x%x, "
4968 "Size 0x%x\n", type, rsrc_cnt,
4969 rsrc_size);
4970
4971 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4972 if (!mbox)
4973 return -ENOMEM;
4974
4975 rc = lpfc_sli4_cfg_post_extnts(phba, &rsrc_cnt, type, &emb, mbox);
4976 if (unlikely(rc)) {
4977 rc = -EIO;
4978 goto err_exit;
4979 }
4980
4981 /*
4982 * Figure out where the response is located. Then get local pointers
4983 * to the response data. The port does not guarantee to respond to
4984 * all extents counts request so update the local variable with the
4985 * allocated count from the port.
4986 */
4987 if (emb == LPFC_SLI4_MBX_EMBED) {
4988 rsrc_ext = &mbox->u.mqe.un.alloc_rsrc_extents;
4989 id_array = &rsrc_ext->u.rsp.id[0];
4990 rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, &rsrc_ext->u.rsp);
4991 } else {
4992 virtaddr = mbox->sge_array->addr[0];
4993 n_rsrc = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
4994 rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, n_rsrc);
4995 id_array = &n_rsrc->id;
4996 }
4997
4998 longs = ((rsrc_cnt * rsrc_size) + BITS_PER_LONG - 1) / BITS_PER_LONG;
4999 rsrc_id_cnt = rsrc_cnt * rsrc_size;
5000
5001 /*
5002 * Based on the resource size and count, correct the base and max
5003 * resource values.
5004 */
5005 length = sizeof(struct lpfc_rsrc_blks);
5006 switch (type) {
5007 case LPFC_RSC_TYPE_FCOE_RPI:
5008 phba->sli4_hba.rpi_bmask = kzalloc(longs *
5009 sizeof(unsigned long),
5010 GFP_KERNEL);
5011 if (unlikely(!phba->sli4_hba.rpi_bmask)) {
5012 rc = -ENOMEM;
5013 goto err_exit;
5014 }
5015 phba->sli4_hba.rpi_ids = kzalloc(rsrc_id_cnt *
5016 sizeof(uint16_t),
5017 GFP_KERNEL);
5018 if (unlikely(!phba->sli4_hba.rpi_ids)) {
5019 kfree(phba->sli4_hba.rpi_bmask);
5020 rc = -ENOMEM;
5021 goto err_exit;
5022 }
5023
5024 /*
5025 * The next_rpi was initialized with the maximum available
5026 * count but the port may allocate a smaller number. Catch
5027 * that case and update the next_rpi.
5028 */
5029 phba->sli4_hba.next_rpi = rsrc_id_cnt;
5030
5031 /* Initialize local ptrs for common extent processing later. */
5032 bmask = phba->sli4_hba.rpi_bmask;
5033 ids = phba->sli4_hba.rpi_ids;
5034 ext_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;
5035 break;
5036 case LPFC_RSC_TYPE_FCOE_VPI:
5037 phba->vpi_bmask = kzalloc(longs *
5038 sizeof(unsigned long),
5039 GFP_KERNEL);
5040 if (unlikely(!phba->vpi_bmask)) {
5041 rc = -ENOMEM;
5042 goto err_exit;
5043 }
5044 phba->vpi_ids = kzalloc(rsrc_id_cnt *
5045 sizeof(uint16_t),
5046 GFP_KERNEL);
5047 if (unlikely(!phba->vpi_ids)) {
5048 kfree(phba->vpi_bmask);
5049 rc = -ENOMEM;
5050 goto err_exit;
5051 }
5052
5053 /* Initialize local ptrs for common extent processing later. */
5054 bmask = phba->vpi_bmask;
5055 ids = phba->vpi_ids;
5056 ext_blk_list = &phba->lpfc_vpi_blk_list;
5057 break;
5058 case LPFC_RSC_TYPE_FCOE_XRI:
5059 phba->sli4_hba.xri_bmask = kzalloc(longs *
5060 sizeof(unsigned long),
5061 GFP_KERNEL);
5062 if (unlikely(!phba->sli4_hba.xri_bmask)) {
5063 rc = -ENOMEM;
5064 goto err_exit;
5065 }
5066 phba->sli4_hba.xri_ids = kzalloc(rsrc_id_cnt *
5067 sizeof(uint16_t),
5068 GFP_KERNEL);
5069 if (unlikely(!phba->sli4_hba.xri_ids)) {
5070 kfree(phba->sli4_hba.xri_bmask);
5071 rc = -ENOMEM;
5072 goto err_exit;
5073 }
5074
5075 /* Initialize local ptrs for common extent processing later. */
5076 bmask = phba->sli4_hba.xri_bmask;
5077 ids = phba->sli4_hba.xri_ids;
5078 ext_blk_list = &phba->sli4_hba.lpfc_xri_blk_list;
5079 break;
5080 case LPFC_RSC_TYPE_FCOE_VFI:
5081 phba->sli4_hba.vfi_bmask = kzalloc(longs *
5082 sizeof(unsigned long),
5083 GFP_KERNEL);
5084 if (unlikely(!phba->sli4_hba.vfi_bmask)) {
5085 rc = -ENOMEM;
5086 goto err_exit;
5087 }
5088 phba->sli4_hba.vfi_ids = kzalloc(rsrc_id_cnt *
5089 sizeof(uint16_t),
5090 GFP_KERNEL);
5091 if (unlikely(!phba->sli4_hba.vfi_ids)) {
5092 kfree(phba->sli4_hba.vfi_bmask);
5093 rc = -ENOMEM;
5094 goto err_exit;
5095 }
5096
5097 /* Initialize local ptrs for common extent processing later. */
5098 bmask = phba->sli4_hba.vfi_bmask;
5099 ids = phba->sli4_hba.vfi_ids;
5100 ext_blk_list = &phba->sli4_hba.lpfc_vfi_blk_list;
5101 break;
5102 default:
5103 /* Unsupported Opcode. Fail call. */
5104 id_array = NULL;
5105 bmask = NULL;
5106 ids = NULL;
5107 ext_blk_list = NULL;
5108 goto err_exit;
5109 }
5110
5111 /*
5112 * Complete initializing the extent configuration with the
5113 * allocated ids assigned to this function. The bitmask serves
5114 * as an index into the array and manages the available ids. The
5115 * array just stores the ids communicated to the port via the wqes.
5116 */
5117 for (i = 0, j = 0, k = 0; i < rsrc_cnt; i++) {
5118 if ((i % 2) == 0)
5119 rsrc_id = bf_get(lpfc_mbx_rsrc_id_word4_0,
5120 &id_array[k]);
5121 else
5122 rsrc_id = bf_get(lpfc_mbx_rsrc_id_word4_1,
5123 &id_array[k]);
5124
5125 rsrc_blks = kzalloc(length, GFP_KERNEL);
5126 if (unlikely(!rsrc_blks)) {
5127 rc = -ENOMEM;
5128 kfree(bmask);
5129 kfree(ids);
5130 goto err_exit;
5131 }
5132 rsrc_blks->rsrc_start = rsrc_id;
5133 rsrc_blks->rsrc_size = rsrc_size;
5134 list_add_tail(&rsrc_blks->list, ext_blk_list);
5135 rsrc_start = rsrc_id;
5136 if ((type == LPFC_RSC_TYPE_FCOE_XRI) && (j == 0))
5137 phba->sli4_hba.scsi_xri_start = rsrc_start +
5138 lpfc_sli4_get_els_iocb_cnt(phba);
5139
5140 while (rsrc_id < (rsrc_start + rsrc_size)) {
5141 ids[j] = rsrc_id;
5142 rsrc_id++;
5143 j++;
5144 }
5145 /* Entire word processed. Get next word.*/
5146 if ((i % 2) == 1)
5147 k++;
5148 }
5149 err_exit:
5150 lpfc_sli4_mbox_cmd_free(phba, mbox);
5151 return rc;
5152}
5153
5154/**
5155 * lpfc_sli4_dealloc_extent - Deallocate an SLI4 resource extent.
5156 * @phba: Pointer to HBA context object.
5157 * @type: the extent's type.
5158 *
5159 * This function deallocates all extents of a particular resource type.
5160 * SLI4 does not allow for deallocating a particular extent range. It
5161 * is the caller's responsibility to release all kernel memory resources.
5162 **/
5163static int
5164lpfc_sli4_dealloc_extent(struct lpfc_hba *phba, uint16_t type)
5165{
5166 int rc;
5167 uint32_t length, mbox_tmo = 0;
5168 LPFC_MBOXQ_t *mbox;
5169 struct lpfc_mbx_dealloc_rsrc_extents *dealloc_rsrc;
5170 struct lpfc_rsrc_blks *rsrc_blk, *rsrc_blk_next;
5171
5172 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5173 if (!mbox)
5174 return -ENOMEM;
5175
5176 /*
5177 * This function sends an embedded mailbox because it only sends the
5178 * the resource type. All extents of this type are released by the
5179 * port.
5180 */
5181 length = (sizeof(struct lpfc_mbx_dealloc_rsrc_extents) -
5182 sizeof(struct lpfc_sli4_cfg_mhdr));
5183 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5184 LPFC_MBOX_OPCODE_DEALLOC_RSRC_EXTENT,
5185 length, LPFC_SLI4_MBX_EMBED);
5186
5187 /* Send an extents count of 0 - the dealloc doesn't use it. */
5188 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, 0, type,
5189 LPFC_SLI4_MBX_EMBED);
5190 if (unlikely(rc)) {
5191 rc = -EIO;
5192 goto out_free_mbox;
5193 }
5194 if (!phba->sli4_hba.intr_enable)
5195 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5196 else {
5197 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox_tmo);
5198 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5199 }
5200 if (unlikely(rc)) {
5201 rc = -EIO;
5202 goto out_free_mbox;
5203 }
5204
5205 dealloc_rsrc = &mbox->u.mqe.un.dealloc_rsrc_extents;
5206 if (bf_get(lpfc_mbox_hdr_status,
5207 &dealloc_rsrc->header.cfg_shdr.response)) {
5208 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
5209 "2919 Failed to release resource extents "
5210 "for type %d - Status 0x%x Add'l Status 0x%x. "
5211 "Resource memory not released.\n",
5212 type,
5213 bf_get(lpfc_mbox_hdr_status,
5214 &dealloc_rsrc->header.cfg_shdr.response),
5215 bf_get(lpfc_mbox_hdr_add_status,
5216 &dealloc_rsrc->header.cfg_shdr.response));
5217 rc = -EIO;
5218 goto out_free_mbox;
5219 }
5220
5221 /* Release kernel memory resources for the specific type. */
5222 switch (type) {
5223 case LPFC_RSC_TYPE_FCOE_VPI:
5224 kfree(phba->vpi_bmask);
5225 kfree(phba->vpi_ids);
5226 bf_set(lpfc_vpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5227 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5228 &phba->lpfc_vpi_blk_list, list) {
5229 list_del_init(&rsrc_blk->list);
5230 kfree(rsrc_blk);
5231 }
5232 break;
5233 case LPFC_RSC_TYPE_FCOE_XRI:
5234 kfree(phba->sli4_hba.xri_bmask);
5235 kfree(phba->sli4_hba.xri_ids);
5236 bf_set(lpfc_xri_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5237 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5238 &phba->sli4_hba.lpfc_xri_blk_list, list) {
5239 list_del_init(&rsrc_blk->list);
5240 kfree(rsrc_blk);
5241 }
5242 break;
5243 case LPFC_RSC_TYPE_FCOE_VFI:
5244 kfree(phba->sli4_hba.vfi_bmask);
5245 kfree(phba->sli4_hba.vfi_ids);
5246 bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5247 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5248 &phba->sli4_hba.lpfc_vfi_blk_list, list) {
5249 list_del_init(&rsrc_blk->list);
5250 kfree(rsrc_blk);
5251 }
5252 break;
5253 case LPFC_RSC_TYPE_FCOE_RPI:
5254 /* RPI bitmask and physical id array are cleaned up earlier. */
5255 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5256 &phba->sli4_hba.lpfc_rpi_blk_list, list) {
5257 list_del_init(&rsrc_blk->list);
5258 kfree(rsrc_blk);
5259 }
5260 break;
5261 default:
5262 break;
5263 }
5264
5265 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5266
5267 out_free_mbox:
5268 mempool_free(mbox, phba->mbox_mem_pool);
5269 return rc;
5270}
5271
5272/**
5273 * lpfc_sli4_alloc_resource_identifiers - Allocate all SLI4 resource extents.
5274 * @phba: Pointer to HBA context object.
5275 *
5276 * This function allocates all SLI4 resource identifiers.
5277 **/
5278int
5279lpfc_sli4_alloc_resource_identifiers(struct lpfc_hba *phba)
5280{
5281 int i, rc, error = 0;
5282 uint16_t count, base;
5283 unsigned long longs;
5284
5285 if (phba->sli4_hba.extents_in_use) {
5286 /*
5287 * The port supports resource extents. The XRI, VPI, VFI, RPI
5288 * resource extent count must be read and allocated before
5289 * provisioning the resource id arrays.
5290 */
5291 if (bf_get(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags) ==
5292 LPFC_IDX_RSRC_RDY) {
5293 /*
5294 * Extent-based resources are set - the driver could
5295 * be in a port reset. Figure out if any corrective
5296 * actions need to be taken.
5297 */
5298 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
5299 LPFC_RSC_TYPE_FCOE_VFI);
5300 if (rc != 0)
5301 error++;
5302 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
5303 LPFC_RSC_TYPE_FCOE_VPI);
5304 if (rc != 0)
5305 error++;
5306 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
5307 LPFC_RSC_TYPE_FCOE_XRI);
5308 if (rc != 0)
5309 error++;
5310 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
5311 LPFC_RSC_TYPE_FCOE_RPI);
5312 if (rc != 0)
5313 error++;
5314
5315 /*
5316 * It's possible that the number of resources
5317 * provided to this port instance changed between
5318 * resets. Detect this condition and reallocate
5319 * resources. Otherwise, there is no action.
5320 */
5321 if (error) {
5322 lpfc_printf_log(phba, KERN_INFO,
5323 LOG_MBOX | LOG_INIT,
5324 "2931 Detected extent resource "
5325 "change. Reallocating all "
5326 "extents.\n");
5327 rc = lpfc_sli4_dealloc_extent(phba,
5328 LPFC_RSC_TYPE_FCOE_VFI);
5329 rc = lpfc_sli4_dealloc_extent(phba,
5330 LPFC_RSC_TYPE_FCOE_VPI);
5331 rc = lpfc_sli4_dealloc_extent(phba,
5332 LPFC_RSC_TYPE_FCOE_XRI);
5333 rc = lpfc_sli4_dealloc_extent(phba,
5334 LPFC_RSC_TYPE_FCOE_RPI);
5335 } else
5336 return 0;
5337 }
5338
5339 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_VFI);
5340 if (unlikely(rc))
5341 goto err_exit;
5342
5343 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_VPI);
5344 if (unlikely(rc))
5345 goto err_exit;
5346
5347 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_RPI);
5348 if (unlikely(rc))
5349 goto err_exit;
5350
5351 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_XRI);
5352 if (unlikely(rc))
5353 goto err_exit;
5354 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags,
5355 LPFC_IDX_RSRC_RDY);
5356 return rc;
5357 } else {
5358 /*
5359 * The port does not support resource extents. The XRI, VPI,
5360 * VFI, RPI resource ids were determined from READ_CONFIG.
5361 * Just allocate the bitmasks and provision the resource id
5362 * arrays. If a port reset is active, the resources don't
5363 * need any action - just exit.
5364 */
5365 if (bf_get(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags) ==
5366 LPFC_IDX_RSRC_RDY)
5367 return 0;
5368
5369 /* RPIs. */
5370 count = phba->sli4_hba.max_cfg_param.max_rpi;
5371 base = phba->sli4_hba.max_cfg_param.rpi_base;
5372 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
5373 phba->sli4_hba.rpi_bmask = kzalloc(longs *
5374 sizeof(unsigned long),
5375 GFP_KERNEL);
5376 if (unlikely(!phba->sli4_hba.rpi_bmask)) {
5377 rc = -ENOMEM;
5378 goto err_exit;
5379 }
5380 phba->sli4_hba.rpi_ids = kzalloc(count *
5381 sizeof(uint16_t),
5382 GFP_KERNEL);
5383 if (unlikely(!phba->sli4_hba.rpi_ids)) {
5384 rc = -ENOMEM;
5385 goto free_rpi_bmask;
5386 }
5387
5388 for (i = 0; i < count; i++)
5389 phba->sli4_hba.rpi_ids[i] = base + i;
5390
5391 /* VPIs. */
5392 count = phba->sli4_hba.max_cfg_param.max_vpi;
5393 base = phba->sli4_hba.max_cfg_param.vpi_base;
5394 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
5395 phba->vpi_bmask = kzalloc(longs *
5396 sizeof(unsigned long),
5397 GFP_KERNEL);
5398 if (unlikely(!phba->vpi_bmask)) {
5399 rc = -ENOMEM;
5400 goto free_rpi_ids;
5401 }
5402 phba->vpi_ids = kzalloc(count *
5403 sizeof(uint16_t),
5404 GFP_KERNEL);
5405 if (unlikely(!phba->vpi_ids)) {
5406 rc = -ENOMEM;
5407 goto free_vpi_bmask;
5408 }
5409
5410 for (i = 0; i < count; i++)
5411 phba->vpi_ids[i] = base + i;
5412
5413 /* XRIs. */
5414 count = phba->sli4_hba.max_cfg_param.max_xri;
5415 base = phba->sli4_hba.max_cfg_param.xri_base;
5416 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
5417 phba->sli4_hba.xri_bmask = kzalloc(longs *
5418 sizeof(unsigned long),
5419 GFP_KERNEL);
5420 if (unlikely(!phba->sli4_hba.xri_bmask)) {
5421 rc = -ENOMEM;
5422 goto free_vpi_ids;
5423 }
5424 phba->sli4_hba.xri_ids = kzalloc(count *
5425 sizeof(uint16_t),
5426 GFP_KERNEL);
5427 if (unlikely(!phba->sli4_hba.xri_ids)) {
5428 rc = -ENOMEM;
5429 goto free_xri_bmask;
5430 }
5431
5432 for (i = 0; i < count; i++)
5433 phba->sli4_hba.xri_ids[i] = base + i;
5434
5435 /* VFIs. */
5436 count = phba->sli4_hba.max_cfg_param.max_vfi;
5437 base = phba->sli4_hba.max_cfg_param.vfi_base;
5438 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
5439 phba->sli4_hba.vfi_bmask = kzalloc(longs *
5440 sizeof(unsigned long),
5441 GFP_KERNEL);
5442 if (unlikely(!phba->sli4_hba.vfi_bmask)) {
5443 rc = -ENOMEM;
5444 goto free_xri_ids;
5445 }
5446 phba->sli4_hba.vfi_ids = kzalloc(count *
5447 sizeof(uint16_t),
5448 GFP_KERNEL);
5449 if (unlikely(!phba->sli4_hba.vfi_ids)) {
5450 rc = -ENOMEM;
5451 goto free_vfi_bmask;
5452 }
5453
5454 for (i = 0; i < count; i++)
5455 phba->sli4_hba.vfi_ids[i] = base + i;
5456
5457 /*
5458 * Mark all resources ready. An HBA reset doesn't need
5459 * to reset the initialization.
5460 */
5461 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags,
5462 LPFC_IDX_RSRC_RDY);
5463 return 0;
5464 }
5465
5466 free_vfi_bmask:
5467 kfree(phba->sli4_hba.vfi_bmask);
5468 free_xri_ids:
5469 kfree(phba->sli4_hba.xri_ids);
5470 free_xri_bmask:
5471 kfree(phba->sli4_hba.xri_bmask);
5472 free_vpi_ids:
5473 kfree(phba->vpi_ids);
5474 free_vpi_bmask:
5475 kfree(phba->vpi_bmask);
5476 free_rpi_ids:
5477 kfree(phba->sli4_hba.rpi_ids);
5478 free_rpi_bmask:
5479 kfree(phba->sli4_hba.rpi_bmask);
5480 err_exit:
5481 return rc;
5482}
5483
5484/**
5485 * lpfc_sli4_dealloc_resource_identifiers - Deallocate all SLI4 resource extents.
5486 * @phba: Pointer to HBA context object.
5487 *
5488 * This function allocates the number of elements for the specified
5489 * resource type.
5490 **/
5491int
5492lpfc_sli4_dealloc_resource_identifiers(struct lpfc_hba *phba)
5493{
5494 if (phba->sli4_hba.extents_in_use) {
5495 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VPI);
5496 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_RPI);
5497 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_XRI);
5498 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VFI);
5499 } else {
5500 kfree(phba->vpi_bmask);
5501 kfree(phba->vpi_ids);
5502 bf_set(lpfc_vpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5503 kfree(phba->sli4_hba.xri_bmask);
5504 kfree(phba->sli4_hba.xri_ids);
5505 bf_set(lpfc_xri_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5506 kfree(phba->sli4_hba.vfi_bmask);
5507 kfree(phba->sli4_hba.vfi_ids);
5508 bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5509 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5510 }
5511
5512 return 0;
5513}
5514
5515/**
James Smartb76f2dc2011-07-22 18:37:42 -04005516 * lpfc_sli4_get_allocated_extnts - Get the port's allocated extents.
5517 * @phba: Pointer to HBA context object.
5518 * @type: The resource extent type.
5519 * @extnt_count: buffer to hold port extent count response
5520 * @extnt_size: buffer to hold port extent size response.
5521 *
5522 * This function calls the port to read the host allocated extents
5523 * for a particular type.
5524 **/
5525int
5526lpfc_sli4_get_allocated_extnts(struct lpfc_hba *phba, uint16_t type,
5527 uint16_t *extnt_cnt, uint16_t *extnt_size)
5528{
5529 bool emb;
5530 int rc = 0;
5531 uint16_t curr_blks = 0;
5532 uint32_t req_len, emb_len;
5533 uint32_t alloc_len, mbox_tmo;
5534 struct list_head *blk_list_head;
5535 struct lpfc_rsrc_blks *rsrc_blk;
5536 LPFC_MBOXQ_t *mbox;
5537 void *virtaddr = NULL;
5538 struct lpfc_mbx_nembed_rsrc_extent *n_rsrc;
5539 struct lpfc_mbx_alloc_rsrc_extents *rsrc_ext;
5540 union lpfc_sli4_cfg_shdr *shdr;
5541
5542 switch (type) {
5543 case LPFC_RSC_TYPE_FCOE_VPI:
5544 blk_list_head = &phba->lpfc_vpi_blk_list;
5545 break;
5546 case LPFC_RSC_TYPE_FCOE_XRI:
5547 blk_list_head = &phba->sli4_hba.lpfc_xri_blk_list;
5548 break;
5549 case LPFC_RSC_TYPE_FCOE_VFI:
5550 blk_list_head = &phba->sli4_hba.lpfc_vfi_blk_list;
5551 break;
5552 case LPFC_RSC_TYPE_FCOE_RPI:
5553 blk_list_head = &phba->sli4_hba.lpfc_rpi_blk_list;
5554 break;
5555 default:
5556 return -EIO;
5557 }
5558
5559 /* Count the number of extents currently allocatd for this type. */
5560 list_for_each_entry(rsrc_blk, blk_list_head, list) {
5561 if (curr_blks == 0) {
5562 /*
5563 * The GET_ALLOCATED mailbox does not return the size,
5564 * just the count. The size should be just the size
5565 * stored in the current allocated block and all sizes
5566 * for an extent type are the same so set the return
5567 * value now.
5568 */
5569 *extnt_size = rsrc_blk->rsrc_size;
5570 }
5571 curr_blks++;
5572 }
5573
5574 /* Calculate the total requested length of the dma memory. */
5575 req_len = curr_blks * sizeof(uint16_t);
5576
5577 /*
5578 * Calculate the size of an embedded mailbox. The uint32_t
5579 * accounts for extents-specific word.
5580 */
5581 emb_len = sizeof(MAILBOX_t) - sizeof(struct mbox_header) -
5582 sizeof(uint32_t);
5583
5584 /*
5585 * Presume the allocation and response will fit into an embedded
5586 * mailbox. If not true, reconfigure to a non-embedded mailbox.
5587 */
5588 emb = LPFC_SLI4_MBX_EMBED;
5589 req_len = emb_len;
5590 if (req_len > emb_len) {
5591 req_len = curr_blks * sizeof(uint16_t) +
5592 sizeof(union lpfc_sli4_cfg_shdr) +
5593 sizeof(uint32_t);
5594 emb = LPFC_SLI4_MBX_NEMBED;
5595 }
5596
5597 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5598 if (!mbox)
5599 return -ENOMEM;
5600 memset(mbox, 0, sizeof(LPFC_MBOXQ_t));
5601
5602 alloc_len = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5603 LPFC_MBOX_OPCODE_GET_ALLOC_RSRC_EXTENT,
5604 req_len, emb);
5605 if (alloc_len < req_len) {
5606 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5607 "2983 Allocated DMA memory size (x%x) is "
5608 "less than the requested DMA memory "
5609 "size (x%x)\n", alloc_len, req_len);
5610 rc = -ENOMEM;
5611 goto err_exit;
5612 }
5613 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, curr_blks, type, emb);
5614 if (unlikely(rc)) {
5615 rc = -EIO;
5616 goto err_exit;
5617 }
5618
5619 if (!phba->sli4_hba.intr_enable)
5620 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5621 else {
5622 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_SLI4_CONFIG);
5623 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5624 }
5625
5626 if (unlikely(rc)) {
5627 rc = -EIO;
5628 goto err_exit;
5629 }
5630
5631 /*
5632 * Figure out where the response is located. Then get local pointers
5633 * to the response data. The port does not guarantee to respond to
5634 * all extents counts request so update the local variable with the
5635 * allocated count from the port.
5636 */
5637 if (emb == LPFC_SLI4_MBX_EMBED) {
5638 rsrc_ext = &mbox->u.mqe.un.alloc_rsrc_extents;
5639 shdr = &rsrc_ext->header.cfg_shdr;
5640 *extnt_cnt = bf_get(lpfc_mbx_rsrc_cnt, &rsrc_ext->u.rsp);
5641 } else {
5642 virtaddr = mbox->sge_array->addr[0];
5643 n_rsrc = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
5644 shdr = &n_rsrc->cfg_shdr;
5645 *extnt_cnt = bf_get(lpfc_mbx_rsrc_cnt, n_rsrc);
5646 }
5647
5648 if (bf_get(lpfc_mbox_hdr_status, &shdr->response)) {
5649 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
5650 "2984 Failed to read allocated resources "
5651 "for type %d - Status 0x%x Add'l Status 0x%x.\n",
5652 type,
5653 bf_get(lpfc_mbox_hdr_status, &shdr->response),
5654 bf_get(lpfc_mbox_hdr_add_status, &shdr->response));
5655 rc = -EIO;
5656 goto err_exit;
5657 }
5658 err_exit:
5659 lpfc_sli4_mbox_cmd_free(phba, mbox);
5660 return rc;
5661}
5662
5663/**
James Smartda0436e2009-05-22 14:51:39 -04005664 * lpfc_sli4_hba_setup - SLI4 device intialization PCI function
5665 * @phba: Pointer to HBA context object.
5666 *
5667 * This function is the main SLI4 device intialization PCI function. This
5668 * function is called by the HBA intialization code, HBA reset code and
5669 * HBA error attention handler code. Caller is not required to hold any
5670 * locks.
5671 **/
5672int
5673lpfc_sli4_hba_setup(struct lpfc_hba *phba)
5674{
5675 int rc;
5676 LPFC_MBOXQ_t *mboxq;
5677 struct lpfc_mqe *mqe;
5678 uint8_t *vpd;
5679 uint32_t vpd_size;
5680 uint32_t ftr_rsp = 0;
5681 struct Scsi_Host *shost = lpfc_shost_from_vport(phba->pport);
5682 struct lpfc_vport *vport = phba->pport;
5683 struct lpfc_dmabuf *mp;
5684
5685 /* Perform a PCI function reset to start from clean */
5686 rc = lpfc_pci_function_reset(phba);
5687 if (unlikely(rc))
5688 return -ENODEV;
5689
5690 /* Check the HBA Host Status Register for readyness */
5691 rc = lpfc_sli4_post_status_check(phba);
5692 if (unlikely(rc))
5693 return -ENODEV;
5694 else {
5695 spin_lock_irq(&phba->hbalock);
5696 phba->sli.sli_flag |= LPFC_SLI_ACTIVE;
5697 spin_unlock_irq(&phba->hbalock);
5698 }
5699
5700 /*
5701 * Allocate a single mailbox container for initializing the
5702 * port.
5703 */
5704 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5705 if (!mboxq)
5706 return -ENOMEM;
5707
5708 /*
5709 * Continue initialization with default values even if driver failed
5710 * to read FCoE param config regions
5711 */
5712 if (lpfc_sli4_read_fcoe_params(phba, mboxq))
James Smart05580562011-05-24 11:40:48 -04005713 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_INIT,
James Smarte4e74272009-07-19 10:01:38 -04005714 "2570 Failed to read FCoE parameters\n");
James Smartda0436e2009-05-22 14:51:39 -04005715
5716 /* Issue READ_REV to collect vpd and FW information. */
James Smart49198b32010-04-06 15:04:33 -04005717 vpd_size = SLI4_PAGE_SIZE;
James Smartda0436e2009-05-22 14:51:39 -04005718 vpd = kzalloc(vpd_size, GFP_KERNEL);
5719 if (!vpd) {
5720 rc = -ENOMEM;
5721 goto out_free_mbox;
5722 }
5723
5724 rc = lpfc_sli4_read_rev(phba, mboxq, vpd, &vpd_size);
James Smart76a95d72010-11-20 23:11:48 -05005725 if (unlikely(rc)) {
5726 kfree(vpd);
5727 goto out_free_mbox;
5728 }
James Smartda0436e2009-05-22 14:51:39 -04005729 mqe = &mboxq->u.mqe;
James Smartf1126682009-06-10 17:22:44 -04005730 phba->sli_rev = bf_get(lpfc_mbx_rd_rev_sli_lvl, &mqe->un.read_rev);
5731 if (bf_get(lpfc_mbx_rd_rev_fcoe, &mqe->un.read_rev))
James Smart76a95d72010-11-20 23:11:48 -05005732 phba->hba_flag |= HBA_FCOE_MODE;
5733 else
5734 phba->hba_flag &= ~HBA_FCOE_MODE;
James Smart45ed1192009-10-02 15:17:02 -04005735
5736 if (bf_get(lpfc_mbx_rd_rev_cee_ver, &mqe->un.read_rev) ==
5737 LPFC_DCBX_CEE_MODE)
5738 phba->hba_flag |= HBA_FIP_SUPPORT;
5739 else
5740 phba->hba_flag &= ~HBA_FIP_SUPPORT;
5741
James Smartc31098c2011-04-16 11:03:33 -04005742 if (phba->sli_rev != LPFC_SLI_REV4) {
James Smartda0436e2009-05-22 14:51:39 -04005743 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5744 "0376 READ_REV Error. SLI Level %d "
5745 "FCoE enabled %d\n",
James Smart76a95d72010-11-20 23:11:48 -05005746 phba->sli_rev, phba->hba_flag & HBA_FCOE_MODE);
James Smartda0436e2009-05-22 14:51:39 -04005747 rc = -EIO;
James Smart76a95d72010-11-20 23:11:48 -05005748 kfree(vpd);
5749 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04005750 }
James Smartda0436e2009-05-22 14:51:39 -04005751 /*
5752 * Evaluate the read rev and vpd data. Populate the driver
5753 * state with the results. If this routine fails, the failure
5754 * is not fatal as the driver will use generic values.
5755 */
5756 rc = lpfc_parse_vpd(phba, vpd, vpd_size);
5757 if (unlikely(!rc)) {
5758 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5759 "0377 Error %d parsing vpd. "
5760 "Using defaults.\n", rc);
5761 rc = 0;
5762 }
James Smart76a95d72010-11-20 23:11:48 -05005763 kfree(vpd);
James Smartda0436e2009-05-22 14:51:39 -04005764
James Smartf1126682009-06-10 17:22:44 -04005765 /* Save information as VPD data */
5766 phba->vpd.rev.biuRev = mqe->un.read_rev.first_hw_rev;
5767 phba->vpd.rev.smRev = mqe->un.read_rev.second_hw_rev;
5768 phba->vpd.rev.endecRev = mqe->un.read_rev.third_hw_rev;
5769 phba->vpd.rev.fcphHigh = bf_get(lpfc_mbx_rd_rev_fcph_high,
5770 &mqe->un.read_rev);
5771 phba->vpd.rev.fcphLow = bf_get(lpfc_mbx_rd_rev_fcph_low,
5772 &mqe->un.read_rev);
5773 phba->vpd.rev.feaLevelHigh = bf_get(lpfc_mbx_rd_rev_ftr_lvl_high,
5774 &mqe->un.read_rev);
5775 phba->vpd.rev.feaLevelLow = bf_get(lpfc_mbx_rd_rev_ftr_lvl_low,
5776 &mqe->un.read_rev);
5777 phba->vpd.rev.sli1FwRev = mqe->un.read_rev.fw_id_rev;
5778 memcpy(phba->vpd.rev.sli1FwName, mqe->un.read_rev.fw_name, 16);
5779 phba->vpd.rev.sli2FwRev = mqe->un.read_rev.ulp_fw_id_rev;
5780 memcpy(phba->vpd.rev.sli2FwName, mqe->un.read_rev.ulp_fw_name, 16);
5781 phba->vpd.rev.opFwRev = mqe->un.read_rev.fw_id_rev;
5782 memcpy(phba->vpd.rev.opFwName, mqe->un.read_rev.fw_name, 16);
5783 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
5784 "(%d):0380 READ_REV Status x%x "
5785 "fw_rev:%s fcphHi:%x fcphLo:%x flHi:%x flLo:%x\n",
5786 mboxq->vport ? mboxq->vport->vpi : 0,
5787 bf_get(lpfc_mqe_status, mqe),
5788 phba->vpd.rev.opFwName,
5789 phba->vpd.rev.fcphHigh, phba->vpd.rev.fcphLow,
5790 phba->vpd.rev.feaLevelHigh, phba->vpd.rev.feaLevelLow);
James Smartda0436e2009-05-22 14:51:39 -04005791
5792 /*
5793 * Discover the port's supported feature set and match it against the
5794 * hosts requests.
5795 */
5796 lpfc_request_features(phba, mboxq);
5797 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5798 if (unlikely(rc)) {
5799 rc = -EIO;
James Smart76a95d72010-11-20 23:11:48 -05005800 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04005801 }
5802
5803 /*
5804 * The port must support FCP initiator mode as this is the
5805 * only mode running in the host.
5806 */
5807 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_fcpi, &mqe->un.req_ftrs))) {
5808 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
5809 "0378 No support for fcpi mode.\n");
5810 ftr_rsp++;
5811 }
James Smartfedd3b72011-02-16 12:39:24 -05005812 if (bf_get(lpfc_mbx_rq_ftr_rsp_perfh, &mqe->un.req_ftrs))
5813 phba->sli3_options |= LPFC_SLI4_PERFH_ENABLED;
5814 else
5815 phba->sli3_options &= ~LPFC_SLI4_PERFH_ENABLED;
James Smartda0436e2009-05-22 14:51:39 -04005816 /*
5817 * If the port cannot support the host's requested features
5818 * then turn off the global config parameters to disable the
5819 * feature in the driver. This is not a fatal error.
5820 */
James Smartbf086112011-08-21 21:48:13 -04005821 phba->sli3_options &= ~LPFC_SLI3_BG_ENABLED;
5822 if (phba->cfg_enable_bg) {
5823 if (bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs))
5824 phba->sli3_options |= LPFC_SLI3_BG_ENABLED;
5825 else
5826 ftr_rsp++;
5827 }
James Smartda0436e2009-05-22 14:51:39 -04005828
5829 if (phba->max_vpi && phba->cfg_enable_npiv &&
5830 !(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
5831 ftr_rsp++;
5832
5833 if (ftr_rsp) {
5834 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
5835 "0379 Feature Mismatch Data: x%08x %08x "
5836 "x%x x%x x%x\n", mqe->un.req_ftrs.word2,
5837 mqe->un.req_ftrs.word3, phba->cfg_enable_bg,
5838 phba->cfg_enable_npiv, phba->max_vpi);
5839 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs)))
5840 phba->cfg_enable_bg = 0;
5841 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
5842 phba->cfg_enable_npiv = 0;
5843 }
5844
5845 /* These SLI3 features are assumed in SLI4 */
5846 spin_lock_irq(&phba->hbalock);
5847 phba->sli3_options |= (LPFC_SLI3_NPIV_ENABLED | LPFC_SLI3_HBQ_ENABLED);
5848 spin_unlock_irq(&phba->hbalock);
5849
James Smart6d368e52011-05-24 11:44:12 -04005850 /*
5851 * Allocate all resources (xri,rpi,vpi,vfi) now. Subsequent
5852 * calls depends on these resources to complete port setup.
5853 */
5854 rc = lpfc_sli4_alloc_resource_identifiers(phba);
5855 if (rc) {
5856 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5857 "2920 Failed to alloc Resource IDs "
5858 "rc = x%x\n", rc);
5859 goto out_free_mbox;
5860 }
5861
James Smartda0436e2009-05-22 14:51:39 -04005862 /* Read the port's service parameters. */
James Smart9f1177a2010-02-26 14:12:57 -05005863 rc = lpfc_read_sparam(phba, mboxq, vport->vpi);
5864 if (rc) {
5865 phba->link_state = LPFC_HBA_ERROR;
5866 rc = -ENOMEM;
James Smart76a95d72010-11-20 23:11:48 -05005867 goto out_free_mbox;
James Smart9f1177a2010-02-26 14:12:57 -05005868 }
5869
James Smartda0436e2009-05-22 14:51:39 -04005870 mboxq->vport = vport;
5871 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5872 mp = (struct lpfc_dmabuf *) mboxq->context1;
5873 if (rc == MBX_SUCCESS) {
5874 memcpy(&vport->fc_sparam, mp->virt, sizeof(struct serv_parm));
5875 rc = 0;
5876 }
5877
5878 /*
5879 * This memory was allocated by the lpfc_read_sparam routine. Release
5880 * it to the mbuf pool.
5881 */
5882 lpfc_mbuf_free(phba, mp->virt, mp->phys);
5883 kfree(mp);
5884 mboxq->context1 = NULL;
5885 if (unlikely(rc)) {
5886 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5887 "0382 READ_SPARAM command failed "
5888 "status %d, mbxStatus x%x\n",
5889 rc, bf_get(lpfc_mqe_status, mqe));
5890 phba->link_state = LPFC_HBA_ERROR;
5891 rc = -EIO;
James Smart76a95d72010-11-20 23:11:48 -05005892 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04005893 }
5894
James Smart05580562011-05-24 11:40:48 -04005895 lpfc_update_vport_wwn(vport);
James Smartda0436e2009-05-22 14:51:39 -04005896
5897 /* Update the fc_host data structures with new wwn. */
5898 fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
5899 fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
5900
5901 /* Register SGL pool to the device using non-embedded mailbox command */
James Smart6d368e52011-05-24 11:44:12 -04005902 if (!phba->sli4_hba.extents_in_use) {
5903 rc = lpfc_sli4_post_els_sgl_list(phba);
5904 if (unlikely(rc)) {
5905 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5906 "0582 Error %d during els sgl post "
5907 "operation\n", rc);
5908 rc = -ENODEV;
5909 goto out_free_mbox;
5910 }
5911 } else {
5912 rc = lpfc_sli4_post_els_sgl_list_ext(phba);
5913 if (unlikely(rc)) {
5914 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5915 "2560 Error %d during els sgl post "
5916 "operation\n", rc);
5917 rc = -ENODEV;
5918 goto out_free_mbox;
5919 }
James Smartda0436e2009-05-22 14:51:39 -04005920 }
5921
5922 /* Register SCSI SGL pool to the device */
5923 rc = lpfc_sli4_repost_scsi_sgl_list(phba);
5924 if (unlikely(rc)) {
James Smart6d368e52011-05-24 11:44:12 -04005925 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smart6a9c52c2009-10-02 15:16:51 -04005926 "0383 Error %d during scsi sgl post "
5927 "operation\n", rc);
James Smartda0436e2009-05-22 14:51:39 -04005928 /* Some Scsi buffers were moved to the abort scsi list */
5929 /* A pci function reset will repost them */
5930 rc = -ENODEV;
James Smart76a95d72010-11-20 23:11:48 -05005931 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04005932 }
5933
5934 /* Post the rpi header region to the device. */
5935 rc = lpfc_sli4_post_all_rpi_hdrs(phba);
5936 if (unlikely(rc)) {
5937 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5938 "0393 Error %d during rpi post operation\n",
5939 rc);
5940 rc = -ENODEV;
James Smart76a95d72010-11-20 23:11:48 -05005941 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04005942 }
James Smartda0436e2009-05-22 14:51:39 -04005943
5944 /* Set up all the queues to the device */
5945 rc = lpfc_sli4_queue_setup(phba);
5946 if (unlikely(rc)) {
5947 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5948 "0381 Error %d during queue setup.\n ", rc);
5949 goto out_stop_timers;
5950 }
5951
5952 /* Arm the CQs and then EQs on device */
5953 lpfc_sli4_arm_cqeq_intr(phba);
5954
5955 /* Indicate device interrupt mode */
5956 phba->sli4_hba.intr_enable = 1;
5957
5958 /* Allow asynchronous mailbox command to go through */
5959 spin_lock_irq(&phba->hbalock);
5960 phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
5961 spin_unlock_irq(&phba->hbalock);
5962
5963 /* Post receive buffers to the device */
5964 lpfc_sli4_rb_setup(phba);
5965
James Smartfc2b9892010-02-26 14:15:29 -05005966 /* Reset HBA FCF states after HBA reset */
5967 phba->fcf.fcf_flag = 0;
5968 phba->fcf.current_rec.flag = 0;
5969
James Smartda0436e2009-05-22 14:51:39 -04005970 /* Start the ELS watchdog timer */
James Smart8fa38512009-07-19 10:01:03 -04005971 mod_timer(&vport->els_tmofunc,
5972 jiffies + HZ * (phba->fc_ratov * 2));
James Smartda0436e2009-05-22 14:51:39 -04005973
5974 /* Start heart beat timer */
5975 mod_timer(&phba->hb_tmofunc,
5976 jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
5977 phba->hb_outstanding = 0;
5978 phba->last_completion_time = jiffies;
5979
5980 /* Start error attention (ERATT) polling timer */
5981 mod_timer(&phba->eratt_poll, jiffies + HZ * LPFC_ERATT_POLL_INTERVAL);
5982
James Smart75baf692010-06-08 18:31:21 -04005983 /* Enable PCIe device Advanced Error Reporting (AER) if configured */
5984 if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
5985 rc = pci_enable_pcie_error_reporting(phba->pcidev);
5986 if (!rc) {
5987 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5988 "2829 This device supports "
5989 "Advanced Error Reporting (AER)\n");
5990 spin_lock_irq(&phba->hbalock);
5991 phba->hba_flag |= HBA_AER_ENABLED;
5992 spin_unlock_irq(&phba->hbalock);
5993 } else {
5994 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5995 "2830 This device does not support "
5996 "Advanced Error Reporting (AER)\n");
5997 phba->cfg_aer_support = 0;
5998 }
James Smart0a96e972011-07-22 18:37:28 -04005999 rc = 0;
James Smart75baf692010-06-08 18:31:21 -04006000 }
6001
James Smart76a95d72010-11-20 23:11:48 -05006002 if (!(phba->hba_flag & HBA_FCOE_MODE)) {
6003 /*
6004 * The FC Port needs to register FCFI (index 0)
6005 */
6006 lpfc_reg_fcfi(phba, mboxq);
6007 mboxq->vport = phba->pport;
6008 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
James Smart9589b0622011-04-16 11:03:17 -04006009 if (rc != MBX_SUCCESS)
James Smart76a95d72010-11-20 23:11:48 -05006010 goto out_unset_queue;
James Smart9589b0622011-04-16 11:03:17 -04006011 rc = 0;
6012 phba->fcf.fcfi = bf_get(lpfc_reg_fcfi_fcfi,
6013 &mboxq->u.mqe.un.reg_fcfi);
James Smart76a95d72010-11-20 23:11:48 -05006014 }
James Smartda0436e2009-05-22 14:51:39 -04006015 /*
6016 * The port is ready, set the host's link state to LINK_DOWN
6017 * in preparation for link interrupts.
6018 */
James Smartda0436e2009-05-22 14:51:39 -04006019 spin_lock_irq(&phba->hbalock);
6020 phba->link_state = LPFC_LINK_DOWN;
6021 spin_unlock_irq(&phba->hbalock);
James Smartfedd3b72011-02-16 12:39:24 -05006022 if (phba->cfg_suppress_link_up == LPFC_INITIALIZE_LINK)
6023 rc = phba->lpfc_hba_init_link(phba, MBX_NOWAIT);
James Smart76a95d72010-11-20 23:11:48 -05006024out_unset_queue:
James Smartda0436e2009-05-22 14:51:39 -04006025 /* Unset all the queues set up in this routine when error out */
6026 if (rc)
6027 lpfc_sli4_queue_unset(phba);
James Smartda0436e2009-05-22 14:51:39 -04006028out_stop_timers:
6029 if (rc)
6030 lpfc_stop_hba_timers(phba);
James Smartda0436e2009-05-22 14:51:39 -04006031out_free_mbox:
6032 mempool_free(mboxq, phba->mbox_mem_pool);
6033 return rc;
6034}
James Smarte59058c2008-08-24 21:49:00 -04006035
6036/**
James Smart3621a712009-04-06 18:47:14 -04006037 * lpfc_mbox_timeout - Timeout call back function for mbox timer
James Smarte59058c2008-08-24 21:49:00 -04006038 * @ptr: context object - pointer to hba structure.
dea31012005-04-17 16:05:31 -05006039 *
James Smarte59058c2008-08-24 21:49:00 -04006040 * This is the callback function for mailbox timer. The mailbox
6041 * timer is armed when a new mailbox command is issued and the timer
6042 * is deleted when the mailbox complete. The function is called by
6043 * the kernel timer code when a mailbox does not complete within
6044 * expected time. This function wakes up the worker thread to
6045 * process the mailbox timeout and returns. All the processing is
6046 * done by the worker thread function lpfc_mbox_timeout_handler.
6047 **/
dea31012005-04-17 16:05:31 -05006048void
6049lpfc_mbox_timeout(unsigned long ptr)
6050{
James Smart92d7f7b2007-06-17 19:56:38 -05006051 struct lpfc_hba *phba = (struct lpfc_hba *) ptr;
dea31012005-04-17 16:05:31 -05006052 unsigned long iflag;
James Smart2e0fef82007-06-17 19:56:36 -05006053 uint32_t tmo_posted;
dea31012005-04-17 16:05:31 -05006054
James Smart2e0fef82007-06-17 19:56:36 -05006055 spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
James Smart92d7f7b2007-06-17 19:56:38 -05006056 tmo_posted = phba->pport->work_port_events & WORKER_MBOX_TMO;
James Smart2e0fef82007-06-17 19:56:36 -05006057 if (!tmo_posted)
6058 phba->pport->work_port_events |= WORKER_MBOX_TMO;
6059 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
6060
James Smart5e9d9b82008-06-14 22:52:53 -04006061 if (!tmo_posted)
6062 lpfc_worker_wake_up(phba);
6063 return;
dea31012005-04-17 16:05:31 -05006064}
6065
James Smarte59058c2008-08-24 21:49:00 -04006066
6067/**
James Smart3621a712009-04-06 18:47:14 -04006068 * lpfc_mbox_timeout_handler - Worker thread function to handle mailbox timeout
James Smarte59058c2008-08-24 21:49:00 -04006069 * @phba: Pointer to HBA context object.
6070 *
6071 * This function is called from worker thread when a mailbox command times out.
6072 * The caller is not required to hold any locks. This function will reset the
6073 * HBA and recover all the pending commands.
6074 **/
dea31012005-04-17 16:05:31 -05006075void
6076lpfc_mbox_timeout_handler(struct lpfc_hba *phba)
6077{
James Smart2e0fef82007-06-17 19:56:36 -05006078 LPFC_MBOXQ_t *pmbox = phba->sli.mbox_active;
James Smart04c68492009-05-22 14:52:52 -04006079 MAILBOX_t *mb = &pmbox->u.mb;
James Smart1dcb58e2007-04-25 09:51:30 -04006080 struct lpfc_sli *psli = &phba->sli;
6081 struct lpfc_sli_ring *pring;
dea31012005-04-17 16:05:31 -05006082
James Smarta257bf92009-04-06 18:48:10 -04006083 /* Check the pmbox pointer first. There is a race condition
6084 * between the mbox timeout handler getting executed in the
6085 * worklist and the mailbox actually completing. When this
6086 * race condition occurs, the mbox_active will be NULL.
6087 */
6088 spin_lock_irq(&phba->hbalock);
6089 if (pmbox == NULL) {
6090 lpfc_printf_log(phba, KERN_WARNING,
6091 LOG_MBOX | LOG_SLI,
6092 "0353 Active Mailbox cleared - mailbox timeout "
6093 "exiting\n");
6094 spin_unlock_irq(&phba->hbalock);
6095 return;
6096 }
6097
dea31012005-04-17 16:05:31 -05006098 /* Mbox cmd <mbxCommand> timeout */
James Smarted957682007-06-17 19:56:37 -05006099 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04006100 "0310 Mailbox command x%x timeout Data: x%x x%x x%p\n",
James Smart92d7f7b2007-06-17 19:56:38 -05006101 mb->mbxCommand,
6102 phba->pport->port_state,
6103 phba->sli.sli_flag,
6104 phba->sli.mbox_active);
James Smarta257bf92009-04-06 18:48:10 -04006105 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05006106
James Smart1dcb58e2007-04-25 09:51:30 -04006107 /* Setting state unknown so lpfc_sli_abort_iocb_ring
6108 * would get IOCB_ERROR from lpfc_sli_issue_iocb, allowing
Lucas De Marchi25985ed2011-03-30 22:57:33 -03006109 * it to fail all outstanding SCSI IO.
James Smart1dcb58e2007-04-25 09:51:30 -04006110 */
James Smart2e0fef82007-06-17 19:56:36 -05006111 spin_lock_irq(&phba->pport->work_port_lock);
6112 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
6113 spin_unlock_irq(&phba->pport->work_port_lock);
6114 spin_lock_irq(&phba->hbalock);
6115 phba->link_state = LPFC_LINK_UNKNOWN;
James Smartf4b4c682009-05-22 14:53:12 -04006116 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05006117 spin_unlock_irq(&phba->hbalock);
James Smart1dcb58e2007-04-25 09:51:30 -04006118
6119 pring = &psli->ring[psli->fcp_ring];
6120 lpfc_sli_abort_iocb_ring(phba, pring);
6121
6122 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smart76bb24e2007-10-27 13:38:00 -04006123 "0345 Resetting board due to mailbox timeout\n");
James Smart3772a992009-05-22 14:50:54 -04006124
6125 /* Reset the HBA device */
6126 lpfc_reset_hba(phba);
dea31012005-04-17 16:05:31 -05006127}
6128
James Smarte59058c2008-08-24 21:49:00 -04006129/**
James Smart3772a992009-05-22 14:50:54 -04006130 * lpfc_sli_issue_mbox_s3 - Issue an SLI3 mailbox command to firmware
James Smarte59058c2008-08-24 21:49:00 -04006131 * @phba: Pointer to HBA context object.
6132 * @pmbox: Pointer to mailbox object.
6133 * @flag: Flag indicating how the mailbox need to be processed.
6134 *
6135 * This function is called by discovery code and HBA management code
James Smart3772a992009-05-22 14:50:54 -04006136 * to submit a mailbox command to firmware with SLI-3 interface spec. This
6137 * function gets the hbalock to protect the data structures.
James Smarte59058c2008-08-24 21:49:00 -04006138 * The mailbox command can be submitted in polling mode, in which case
6139 * this function will wait in a polling loop for the completion of the
6140 * mailbox.
6141 * If the mailbox is submitted in no_wait mode (not polling) the
6142 * function will submit the command and returns immediately without waiting
6143 * for the mailbox completion. The no_wait is supported only when HBA
6144 * is in SLI2/SLI3 mode - interrupts are enabled.
6145 * The SLI interface allows only one mailbox pending at a time. If the
6146 * mailbox is issued in polling mode and there is already a mailbox
6147 * pending, then the function will return an error. If the mailbox is issued
6148 * in NO_WAIT mode and there is a mailbox pending already, the function
6149 * will return MBX_BUSY after queuing the mailbox into mailbox queue.
6150 * The sli layer owns the mailbox object until the completion of mailbox
6151 * command if this function return MBX_BUSY or MBX_SUCCESS. For all other
6152 * return codes the caller owns the mailbox command after the return of
6153 * the function.
6154 **/
James Smart3772a992009-05-22 14:50:54 -04006155static int
6156lpfc_sli_issue_mbox_s3(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox,
6157 uint32_t flag)
dea31012005-04-17 16:05:31 -05006158{
dea31012005-04-17 16:05:31 -05006159 MAILBOX_t *mb;
James Smart2e0fef82007-06-17 19:56:36 -05006160 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05006161 uint32_t status, evtctr;
James Smart9940b972011-03-11 16:06:12 -05006162 uint32_t ha_copy, hc_copy;
dea31012005-04-17 16:05:31 -05006163 int i;
James Smart09372822008-01-11 01:52:54 -05006164 unsigned long timeout;
dea31012005-04-17 16:05:31 -05006165 unsigned long drvr_flag = 0;
James Smart34b02dc2008-08-24 21:49:55 -04006166 uint32_t word0, ldata;
dea31012005-04-17 16:05:31 -05006167 void __iomem *to_slim;
James Smart58da1ff2008-04-07 10:15:56 -04006168 int processing_queue = 0;
6169
6170 spin_lock_irqsave(&phba->hbalock, drvr_flag);
6171 if (!pmbox) {
James Smart8568a4d2009-07-19 10:01:16 -04006172 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart58da1ff2008-04-07 10:15:56 -04006173 /* processing mbox queue from intr_handler */
James Smart3772a992009-05-22 14:50:54 -04006174 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
6175 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
6176 return MBX_SUCCESS;
6177 }
James Smart58da1ff2008-04-07 10:15:56 -04006178 processing_queue = 1;
James Smart58da1ff2008-04-07 10:15:56 -04006179 pmbox = lpfc_mbox_get(phba);
6180 if (!pmbox) {
6181 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
6182 return MBX_SUCCESS;
6183 }
6184 }
dea31012005-04-17 16:05:31 -05006185
James Smarted957682007-06-17 19:56:37 -05006186 if (pmbox->mbox_cmpl && pmbox->mbox_cmpl != lpfc_sli_def_mbox_cmpl &&
James Smart92d7f7b2007-06-17 19:56:38 -05006187 pmbox->mbox_cmpl != lpfc_sli_wake_mbox_wait) {
James Smarted957682007-06-17 19:56:37 -05006188 if(!pmbox->vport) {
James Smart58da1ff2008-04-07 10:15:56 -04006189 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
James Smarted957682007-06-17 19:56:37 -05006190 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -05006191 LOG_MBOX | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04006192 "1806 Mbox x%x failed. No vport\n",
James Smart3772a992009-05-22 14:50:54 -04006193 pmbox->u.mb.mbxCommand);
James Smarted957682007-06-17 19:56:37 -05006194 dump_stack();
James Smart58da1ff2008-04-07 10:15:56 -04006195 goto out_not_finished;
James Smarted957682007-06-17 19:56:37 -05006196 }
6197 }
6198
Linas Vepstas8d63f372007-02-14 14:28:36 -06006199 /* If the PCI channel is in offline state, do not post mbox. */
James Smart58da1ff2008-04-07 10:15:56 -04006200 if (unlikely(pci_channel_offline(phba->pcidev))) {
6201 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
6202 goto out_not_finished;
6203 }
Linas Vepstas8d63f372007-02-14 14:28:36 -06006204
James Smarta257bf92009-04-06 18:48:10 -04006205 /* If HBA has a deferred error attention, fail the iocb. */
6206 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
6207 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
6208 goto out_not_finished;
6209 }
6210
dea31012005-04-17 16:05:31 -05006211 psli = &phba->sli;
James Smart92d7f7b2007-06-17 19:56:38 -05006212
James Smart3772a992009-05-22 14:50:54 -04006213 mb = &pmbox->u.mb;
dea31012005-04-17 16:05:31 -05006214 status = MBX_SUCCESS;
6215
James Smart2e0fef82007-06-17 19:56:36 -05006216 if (phba->link_state == LPFC_HBA_ERROR) {
6217 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05006218
6219 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04006220 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6221 "(%d):0311 Mailbox command x%x cannot "
6222 "issue Data: x%x x%x\n",
6223 pmbox->vport ? pmbox->vport->vpi : 0,
6224 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04006225 goto out_not_finished;
Jamie Wellnitz41415862006-02-28 19:25:27 -05006226 }
6227
James Smart9940b972011-03-11 16:06:12 -05006228 if (mb->mbxCommand != MBX_KILL_BOARD && flag & MBX_NOWAIT) {
6229 if (lpfc_readl(phba->HCregaddr, &hc_copy) ||
6230 !(hc_copy & HC_MBINT_ENA)) {
6231 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
6232 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smart3772a992009-05-22 14:50:54 -04006233 "(%d):2528 Mailbox command x%x cannot "
6234 "issue Data: x%x x%x\n",
6235 pmbox->vport ? pmbox->vport->vpi : 0,
6236 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
James Smart9940b972011-03-11 16:06:12 -05006237 goto out_not_finished;
6238 }
James Smart92908312006-03-07 15:04:13 -05006239 }
6240
dea31012005-04-17 16:05:31 -05006241 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
6242 /* Polling for a mbox command when another one is already active
6243 * is not allowed in SLI. Also, the driver must have established
6244 * SLI2 mode to queue and process multiple mbox commands.
6245 */
6246
6247 if (flag & MBX_POLL) {
James Smart2e0fef82007-06-17 19:56:36 -05006248 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05006249
6250 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04006251 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6252 "(%d):2529 Mailbox command x%x "
6253 "cannot issue Data: x%x x%x\n",
6254 pmbox->vport ? pmbox->vport->vpi : 0,
6255 pmbox->u.mb.mbxCommand,
6256 psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04006257 goto out_not_finished;
dea31012005-04-17 16:05:31 -05006258 }
6259
James Smart3772a992009-05-22 14:50:54 -04006260 if (!(psli->sli_flag & LPFC_SLI_ACTIVE)) {
James Smart2e0fef82007-06-17 19:56:36 -05006261 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05006262 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04006263 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6264 "(%d):2530 Mailbox command x%x "
6265 "cannot issue Data: x%x x%x\n",
6266 pmbox->vport ? pmbox->vport->vpi : 0,
6267 pmbox->u.mb.mbxCommand,
6268 psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04006269 goto out_not_finished;
dea31012005-04-17 16:05:31 -05006270 }
6271
dea31012005-04-17 16:05:31 -05006272 /* Another mailbox command is still being processed, queue this
6273 * command to be processed later.
6274 */
6275 lpfc_mbox_put(phba, pmbox);
6276
6277 /* Mbox cmd issue - BUSY */
James Smarted957682007-06-17 19:56:37 -05006278 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04006279 "(%d):0308 Mbox cmd issue - BUSY Data: "
James Smart92d7f7b2007-06-17 19:56:38 -05006280 "x%x x%x x%x x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -05006281 pmbox->vport ? pmbox->vport->vpi : 0xffffff,
6282 mb->mbxCommand, phba->pport->port_state,
6283 psli->sli_flag, flag);
dea31012005-04-17 16:05:31 -05006284
6285 psli->slistat.mbox_busy++;
James Smart2e0fef82007-06-17 19:56:36 -05006286 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05006287
James Smart858c9f62007-06-17 19:56:39 -05006288 if (pmbox->vport) {
6289 lpfc_debugfs_disc_trc(pmbox->vport,
6290 LPFC_DISC_TRC_MBOX_VPORT,
6291 "MBOX Bsy vport: cmd:x%x mb:x%x x%x",
6292 (uint32_t)mb->mbxCommand,
6293 mb->un.varWords[0], mb->un.varWords[1]);
6294 }
6295 else {
6296 lpfc_debugfs_disc_trc(phba->pport,
6297 LPFC_DISC_TRC_MBOX,
6298 "MBOX Bsy: cmd:x%x mb:x%x x%x",
6299 (uint32_t)mb->mbxCommand,
6300 mb->un.varWords[0], mb->un.varWords[1]);
6301 }
6302
James Smart2e0fef82007-06-17 19:56:36 -05006303 return MBX_BUSY;
dea31012005-04-17 16:05:31 -05006304 }
6305
dea31012005-04-17 16:05:31 -05006306 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
6307
6308 /* If we are not polling, we MUST be in SLI2 mode */
6309 if (flag != MBX_POLL) {
James Smart3772a992009-05-22 14:50:54 -04006310 if (!(psli->sli_flag & LPFC_SLI_ACTIVE) &&
Jamie Wellnitz41415862006-02-28 19:25:27 -05006311 (mb->mbxCommand != MBX_KILL_BOARD)) {
dea31012005-04-17 16:05:31 -05006312 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05006313 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05006314 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04006315 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6316 "(%d):2531 Mailbox command x%x "
6317 "cannot issue Data: x%x x%x\n",
6318 pmbox->vport ? pmbox->vport->vpi : 0,
6319 pmbox->u.mb.mbxCommand,
6320 psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04006321 goto out_not_finished;
dea31012005-04-17 16:05:31 -05006322 }
6323 /* timeout active mbox command */
James Smarta309a6b2006-08-01 07:33:43 -04006324 mod_timer(&psli->mbox_tmo, (jiffies +
6325 (HZ * lpfc_mbox_tmo_val(phba, mb->mbxCommand))));
dea31012005-04-17 16:05:31 -05006326 }
6327
6328 /* Mailbox cmd <cmd> issue */
James Smarted957682007-06-17 19:56:37 -05006329 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04006330 "(%d):0309 Mailbox cmd x%x issue Data: x%x x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05006331 "x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04006332 pmbox->vport ? pmbox->vport->vpi : 0,
James Smart92d7f7b2007-06-17 19:56:38 -05006333 mb->mbxCommand, phba->pport->port_state,
6334 psli->sli_flag, flag);
dea31012005-04-17 16:05:31 -05006335
James Smart858c9f62007-06-17 19:56:39 -05006336 if (mb->mbxCommand != MBX_HEARTBEAT) {
6337 if (pmbox->vport) {
6338 lpfc_debugfs_disc_trc(pmbox->vport,
6339 LPFC_DISC_TRC_MBOX_VPORT,
6340 "MBOX Send vport: cmd:x%x mb:x%x x%x",
6341 (uint32_t)mb->mbxCommand,
6342 mb->un.varWords[0], mb->un.varWords[1]);
6343 }
6344 else {
6345 lpfc_debugfs_disc_trc(phba->pport,
6346 LPFC_DISC_TRC_MBOX,
6347 "MBOX Send: cmd:x%x mb:x%x x%x",
6348 (uint32_t)mb->mbxCommand,
6349 mb->un.varWords[0], mb->un.varWords[1]);
6350 }
6351 }
6352
dea31012005-04-17 16:05:31 -05006353 psli->slistat.mbox_cmd++;
6354 evtctr = psli->slistat.mbox_event;
6355
6356 /* next set own bit for the adapter and copy over command word */
6357 mb->mbxOwner = OWN_CHIP;
6358
James Smart3772a992009-05-22 14:50:54 -04006359 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
James Smart7a470272010-03-15 11:25:20 -04006360 /* Populate mbox extension offset word. */
6361 if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len) {
6362 *(((uint32_t *)mb) + pmbox->mbox_offset_word)
6363 = (uint8_t *)phba->mbox_ext
6364 - (uint8_t *)phba->mbox;
6365 }
6366
6367 /* Copy the mailbox extension data */
6368 if (pmbox->in_ext_byte_len && pmbox->context2) {
6369 lpfc_sli_pcimem_bcopy(pmbox->context2,
6370 (uint8_t *)phba->mbox_ext,
6371 pmbox->in_ext_byte_len);
6372 }
6373 /* Copy command data to host SLIM area */
James Smart34b02dc2008-08-24 21:49:55 -04006374 lpfc_sli_pcimem_bcopy(mb, phba->mbox, MAILBOX_CMD_SIZE);
dea31012005-04-17 16:05:31 -05006375 } else {
James Smart7a470272010-03-15 11:25:20 -04006376 /* Populate mbox extension offset word. */
6377 if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len)
6378 *(((uint32_t *)mb) + pmbox->mbox_offset_word)
6379 = MAILBOX_HBA_EXT_OFFSET;
6380
6381 /* Copy the mailbox extension data */
6382 if (pmbox->in_ext_byte_len && pmbox->context2) {
6383 lpfc_memcpy_to_slim(phba->MBslimaddr +
6384 MAILBOX_HBA_EXT_OFFSET,
6385 pmbox->context2, pmbox->in_ext_byte_len);
6386
6387 }
James Smart92908312006-03-07 15:04:13 -05006388 if (mb->mbxCommand == MBX_CONFIG_PORT) {
dea31012005-04-17 16:05:31 -05006389 /* copy command data into host mbox for cmpl */
James Smart34b02dc2008-08-24 21:49:55 -04006390 lpfc_sli_pcimem_bcopy(mb, phba->mbox, MAILBOX_CMD_SIZE);
dea31012005-04-17 16:05:31 -05006391 }
6392
6393 /* First copy mbox command data to HBA SLIM, skip past first
6394 word */
6395 to_slim = phba->MBslimaddr + sizeof (uint32_t);
6396 lpfc_memcpy_to_slim(to_slim, &mb->un.varWords[0],
6397 MAILBOX_CMD_SIZE - sizeof (uint32_t));
6398
6399 /* Next copy over first word, with mbxOwner set */
James Smart34b02dc2008-08-24 21:49:55 -04006400 ldata = *((uint32_t *)mb);
dea31012005-04-17 16:05:31 -05006401 to_slim = phba->MBslimaddr;
6402 writel(ldata, to_slim);
6403 readl(to_slim); /* flush */
6404
6405 if (mb->mbxCommand == MBX_CONFIG_PORT) {
6406 /* switch over to host mailbox */
James Smart3772a992009-05-22 14:50:54 -04006407 psli->sli_flag |= LPFC_SLI_ACTIVE;
dea31012005-04-17 16:05:31 -05006408 }
6409 }
6410
6411 wmb();
dea31012005-04-17 16:05:31 -05006412
6413 switch (flag) {
6414 case MBX_NOWAIT:
James Smart09372822008-01-11 01:52:54 -05006415 /* Set up reference to mailbox command */
dea31012005-04-17 16:05:31 -05006416 psli->mbox_active = pmbox;
James Smart09372822008-01-11 01:52:54 -05006417 /* Interrupt board to do it */
6418 writel(CA_MBATT, phba->CAregaddr);
6419 readl(phba->CAregaddr); /* flush */
6420 /* Don't wait for it to finish, just return */
dea31012005-04-17 16:05:31 -05006421 break;
6422
6423 case MBX_POLL:
James Smart09372822008-01-11 01:52:54 -05006424 /* Set up null reference to mailbox command */
dea31012005-04-17 16:05:31 -05006425 psli->mbox_active = NULL;
James Smart09372822008-01-11 01:52:54 -05006426 /* Interrupt board to do it */
6427 writel(CA_MBATT, phba->CAregaddr);
6428 readl(phba->CAregaddr); /* flush */
6429
James Smart3772a992009-05-22 14:50:54 -04006430 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05006431 /* First read mbox status word */
James Smart34b02dc2008-08-24 21:49:55 -04006432 word0 = *((uint32_t *)phba->mbox);
dea31012005-04-17 16:05:31 -05006433 word0 = le32_to_cpu(word0);
6434 } else {
6435 /* First read mbox status word */
James Smart9940b972011-03-11 16:06:12 -05006436 if (lpfc_readl(phba->MBslimaddr, &word0)) {
6437 spin_unlock_irqrestore(&phba->hbalock,
6438 drvr_flag);
6439 goto out_not_finished;
6440 }
dea31012005-04-17 16:05:31 -05006441 }
6442
6443 /* Read the HBA Host Attention Register */
James Smart9940b972011-03-11 16:06:12 -05006444 if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
6445 spin_unlock_irqrestore(&phba->hbalock,
6446 drvr_flag);
6447 goto out_not_finished;
6448 }
James Smart09372822008-01-11 01:52:54 -05006449 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
6450 mb->mbxCommand) *
6451 1000) + jiffies;
6452 i = 0;
dea31012005-04-17 16:05:31 -05006453 /* Wait for command to complete */
Jamie Wellnitz41415862006-02-28 19:25:27 -05006454 while (((word0 & OWN_CHIP) == OWN_CHIP) ||
6455 (!(ha_copy & HA_MBATT) &&
James Smart2e0fef82007-06-17 19:56:36 -05006456 (phba->link_state > LPFC_WARM_START))) {
James Smart09372822008-01-11 01:52:54 -05006457 if (time_after(jiffies, timeout)) {
dea31012005-04-17 16:05:31 -05006458 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05006459 spin_unlock_irqrestore(&phba->hbalock,
dea31012005-04-17 16:05:31 -05006460 drvr_flag);
James Smart58da1ff2008-04-07 10:15:56 -04006461 goto out_not_finished;
dea31012005-04-17 16:05:31 -05006462 }
6463
6464 /* Check if we took a mbox interrupt while we were
6465 polling */
6466 if (((word0 & OWN_CHIP) != OWN_CHIP)
6467 && (evtctr != psli->slistat.mbox_event))
6468 break;
6469
James Smart09372822008-01-11 01:52:54 -05006470 if (i++ > 10) {
6471 spin_unlock_irqrestore(&phba->hbalock,
6472 drvr_flag);
6473 msleep(1);
6474 spin_lock_irqsave(&phba->hbalock, drvr_flag);
6475 }
dea31012005-04-17 16:05:31 -05006476
James Smart3772a992009-05-22 14:50:54 -04006477 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05006478 /* First copy command data */
James Smart34b02dc2008-08-24 21:49:55 -04006479 word0 = *((uint32_t *)phba->mbox);
dea31012005-04-17 16:05:31 -05006480 word0 = le32_to_cpu(word0);
6481 if (mb->mbxCommand == MBX_CONFIG_PORT) {
6482 MAILBOX_t *slimmb;
James Smart34b02dc2008-08-24 21:49:55 -04006483 uint32_t slimword0;
dea31012005-04-17 16:05:31 -05006484 /* Check real SLIM for any errors */
6485 slimword0 = readl(phba->MBslimaddr);
6486 slimmb = (MAILBOX_t *) & slimword0;
6487 if (((slimword0 & OWN_CHIP) != OWN_CHIP)
6488 && slimmb->mbxStatus) {
6489 psli->sli_flag &=
James Smart3772a992009-05-22 14:50:54 -04006490 ~LPFC_SLI_ACTIVE;
dea31012005-04-17 16:05:31 -05006491 word0 = slimword0;
6492 }
6493 }
6494 } else {
6495 /* First copy command data */
6496 word0 = readl(phba->MBslimaddr);
6497 }
6498 /* Read the HBA Host Attention Register */
James Smart9940b972011-03-11 16:06:12 -05006499 if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
6500 spin_unlock_irqrestore(&phba->hbalock,
6501 drvr_flag);
6502 goto out_not_finished;
6503 }
dea31012005-04-17 16:05:31 -05006504 }
6505
James Smart3772a992009-05-22 14:50:54 -04006506 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05006507 /* copy results back to user */
James Smart34b02dc2008-08-24 21:49:55 -04006508 lpfc_sli_pcimem_bcopy(phba->mbox, mb, MAILBOX_CMD_SIZE);
James Smart7a470272010-03-15 11:25:20 -04006509 /* Copy the mailbox extension data */
6510 if (pmbox->out_ext_byte_len && pmbox->context2) {
6511 lpfc_sli_pcimem_bcopy(phba->mbox_ext,
6512 pmbox->context2,
6513 pmbox->out_ext_byte_len);
6514 }
dea31012005-04-17 16:05:31 -05006515 } else {
6516 /* First copy command data */
6517 lpfc_memcpy_from_slim(mb, phba->MBslimaddr,
6518 MAILBOX_CMD_SIZE);
James Smart7a470272010-03-15 11:25:20 -04006519 /* Copy the mailbox extension data */
6520 if (pmbox->out_ext_byte_len && pmbox->context2) {
6521 lpfc_memcpy_from_slim(pmbox->context2,
6522 phba->MBslimaddr +
6523 MAILBOX_HBA_EXT_OFFSET,
6524 pmbox->out_ext_byte_len);
dea31012005-04-17 16:05:31 -05006525 }
6526 }
6527
6528 writel(HA_MBATT, phba->HAregaddr);
6529 readl(phba->HAregaddr); /* flush */
6530
6531 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
6532 status = mb->mbxStatus;
6533 }
6534
James Smart2e0fef82007-06-17 19:56:36 -05006535 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
6536 return status;
James Smart58da1ff2008-04-07 10:15:56 -04006537
6538out_not_finished:
6539 if (processing_queue) {
James Smartda0436e2009-05-22 14:51:39 -04006540 pmbox->u.mb.mbxStatus = MBX_NOT_FINISHED;
James Smart58da1ff2008-04-07 10:15:56 -04006541 lpfc_mbox_cmpl_put(phba, pmbox);
6542 }
6543 return MBX_NOT_FINISHED;
dea31012005-04-17 16:05:31 -05006544}
6545
James Smarte59058c2008-08-24 21:49:00 -04006546/**
James Smartf1126682009-06-10 17:22:44 -04006547 * lpfc_sli4_async_mbox_block - Block posting SLI4 asynchronous mailbox command
6548 * @phba: Pointer to HBA context object.
6549 *
6550 * The function blocks the posting of SLI4 asynchronous mailbox commands from
6551 * the driver internal pending mailbox queue. It will then try to wait out the
6552 * possible outstanding mailbox command before return.
6553 *
6554 * Returns:
6555 * 0 - the outstanding mailbox command completed; otherwise, the wait for
6556 * the outstanding mailbox command timed out.
6557 **/
6558static int
6559lpfc_sli4_async_mbox_block(struct lpfc_hba *phba)
6560{
6561 struct lpfc_sli *psli = &phba->sli;
6562 uint8_t actcmd = MBX_HEARTBEAT;
6563 int rc = 0;
6564 unsigned long timeout;
6565
6566 /* Mark the asynchronous mailbox command posting as blocked */
6567 spin_lock_irq(&phba->hbalock);
6568 psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
6569 if (phba->sli.mbox_active)
6570 actcmd = phba->sli.mbox_active->u.mb.mbxCommand;
6571 spin_unlock_irq(&phba->hbalock);
6572 /* Determine how long we might wait for the active mailbox
6573 * command to be gracefully completed by firmware.
6574 */
6575 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, actcmd) * 1000) +
6576 jiffies;
6577 /* Wait for the outstnading mailbox command to complete */
6578 while (phba->sli.mbox_active) {
6579 /* Check active mailbox complete status every 2ms */
6580 msleep(2);
6581 if (time_after(jiffies, timeout)) {
6582 /* Timeout, marked the outstanding cmd not complete */
6583 rc = 1;
6584 break;
6585 }
6586 }
6587
6588 /* Can not cleanly block async mailbox command, fails it */
6589 if (rc) {
6590 spin_lock_irq(&phba->hbalock);
6591 psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
6592 spin_unlock_irq(&phba->hbalock);
6593 }
6594 return rc;
6595}
6596
6597/**
6598 * lpfc_sli4_async_mbox_unblock - Block posting SLI4 async mailbox command
6599 * @phba: Pointer to HBA context object.
6600 *
6601 * The function unblocks and resume posting of SLI4 asynchronous mailbox
6602 * commands from the driver internal pending mailbox queue. It makes sure
6603 * that there is no outstanding mailbox command before resuming posting
6604 * asynchronous mailbox commands. If, for any reason, there is outstanding
6605 * mailbox command, it will try to wait it out before resuming asynchronous
6606 * mailbox command posting.
6607 **/
6608static void
6609lpfc_sli4_async_mbox_unblock(struct lpfc_hba *phba)
6610{
6611 struct lpfc_sli *psli = &phba->sli;
6612
6613 spin_lock_irq(&phba->hbalock);
6614 if (!(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
6615 /* Asynchronous mailbox posting is not blocked, do nothing */
6616 spin_unlock_irq(&phba->hbalock);
6617 return;
6618 }
6619
6620 /* Outstanding synchronous mailbox command is guaranteed to be done,
6621 * successful or timeout, after timing-out the outstanding mailbox
6622 * command shall always be removed, so just unblock posting async
6623 * mailbox command and resume
6624 */
6625 psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
6626 spin_unlock_irq(&phba->hbalock);
6627
6628 /* wake up worker thread to post asynchronlous mailbox command */
6629 lpfc_worker_wake_up(phba);
6630}
6631
6632/**
James Smartda0436e2009-05-22 14:51:39 -04006633 * lpfc_sli4_post_sync_mbox - Post an SLI4 mailbox to the bootstrap mailbox
6634 * @phba: Pointer to HBA context object.
6635 * @mboxq: Pointer to mailbox object.
6636 *
6637 * The function posts a mailbox to the port. The mailbox is expected
6638 * to be comletely filled in and ready for the port to operate on it.
6639 * This routine executes a synchronous completion operation on the
6640 * mailbox by polling for its completion.
6641 *
6642 * The caller must not be holding any locks when calling this routine.
6643 *
6644 * Returns:
6645 * MBX_SUCCESS - mailbox posted successfully
6646 * Any of the MBX error values.
6647 **/
6648static int
6649lpfc_sli4_post_sync_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
6650{
6651 int rc = MBX_SUCCESS;
6652 unsigned long iflag;
6653 uint32_t db_ready;
6654 uint32_t mcqe_status;
6655 uint32_t mbx_cmnd;
6656 unsigned long timeout;
6657 struct lpfc_sli *psli = &phba->sli;
6658 struct lpfc_mqe *mb = &mboxq->u.mqe;
6659 struct lpfc_bmbx_create *mbox_rgn;
6660 struct dma_address *dma_address;
6661 struct lpfc_register bmbx_reg;
6662
6663 /*
6664 * Only one mailbox can be active to the bootstrap mailbox region
6665 * at a time and there is no queueing provided.
6666 */
6667 spin_lock_irqsave(&phba->hbalock, iflag);
6668 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
6669 spin_unlock_irqrestore(&phba->hbalock, iflag);
6670 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6671 "(%d):2532 Mailbox command x%x (x%x) "
6672 "cannot issue Data: x%x x%x\n",
6673 mboxq->vport ? mboxq->vport->vpi : 0,
6674 mboxq->u.mb.mbxCommand,
6675 lpfc_sli4_mbox_opcode_get(phba, mboxq),
6676 psli->sli_flag, MBX_POLL);
6677 return MBXERR_ERROR;
6678 }
6679 /* The server grabs the token and owns it until release */
6680 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
6681 phba->sli.mbox_active = mboxq;
6682 spin_unlock_irqrestore(&phba->hbalock, iflag);
6683
6684 /*
6685 * Initialize the bootstrap memory region to avoid stale data areas
6686 * in the mailbox post. Then copy the caller's mailbox contents to
6687 * the bmbx mailbox region.
6688 */
6689 mbx_cmnd = bf_get(lpfc_mqe_command, mb);
6690 memset(phba->sli4_hba.bmbx.avirt, 0, sizeof(struct lpfc_bmbx_create));
6691 lpfc_sli_pcimem_bcopy(mb, phba->sli4_hba.bmbx.avirt,
6692 sizeof(struct lpfc_mqe));
6693
6694 /* Post the high mailbox dma address to the port and wait for ready. */
6695 dma_address = &phba->sli4_hba.bmbx.dma_address;
6696 writel(dma_address->addr_hi, phba->sli4_hba.BMBXregaddr);
6697
6698 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mbx_cmnd)
6699 * 1000) + jiffies;
6700 do {
6701 bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
6702 db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
6703 if (!db_ready)
6704 msleep(2);
6705
6706 if (time_after(jiffies, timeout)) {
6707 rc = MBXERR_ERROR;
6708 goto exit;
6709 }
6710 } while (!db_ready);
6711
6712 /* Post the low mailbox dma address to the port. */
6713 writel(dma_address->addr_lo, phba->sli4_hba.BMBXregaddr);
6714 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mbx_cmnd)
6715 * 1000) + jiffies;
6716 do {
6717 bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
6718 db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
6719 if (!db_ready)
6720 msleep(2);
6721
6722 if (time_after(jiffies, timeout)) {
6723 rc = MBXERR_ERROR;
6724 goto exit;
6725 }
6726 } while (!db_ready);
6727
6728 /*
6729 * Read the CQ to ensure the mailbox has completed.
6730 * If so, update the mailbox status so that the upper layers
6731 * can complete the request normally.
6732 */
6733 lpfc_sli_pcimem_bcopy(phba->sli4_hba.bmbx.avirt, mb,
6734 sizeof(struct lpfc_mqe));
6735 mbox_rgn = (struct lpfc_bmbx_create *) phba->sli4_hba.bmbx.avirt;
6736 lpfc_sli_pcimem_bcopy(&mbox_rgn->mcqe, &mboxq->mcqe,
6737 sizeof(struct lpfc_mcqe));
6738 mcqe_status = bf_get(lpfc_mcqe_status, &mbox_rgn->mcqe);
James Smart05580562011-05-24 11:40:48 -04006739 /*
6740 * When the CQE status indicates a failure and the mailbox status
6741 * indicates success then copy the CQE status into the mailbox status
6742 * (and prefix it with x4000).
6743 */
James Smartda0436e2009-05-22 14:51:39 -04006744 if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
James Smart05580562011-05-24 11:40:48 -04006745 if (bf_get(lpfc_mqe_status, mb) == MBX_SUCCESS)
6746 bf_set(lpfc_mqe_status, mb,
6747 (LPFC_MBX_ERROR_RANGE | mcqe_status));
James Smartda0436e2009-05-22 14:51:39 -04006748 rc = MBXERR_ERROR;
James Smartd7c47992010-06-08 18:31:54 -04006749 } else
6750 lpfc_sli4_swap_str(phba, mboxq);
James Smartda0436e2009-05-22 14:51:39 -04006751
6752 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
6753 "(%d):0356 Mailbox cmd x%x (x%x) Status x%x "
6754 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x"
6755 " x%x x%x CQ: x%x x%x x%x x%x\n",
6756 mboxq->vport ? mboxq->vport->vpi : 0,
6757 mbx_cmnd, lpfc_sli4_mbox_opcode_get(phba, mboxq),
6758 bf_get(lpfc_mqe_status, mb),
6759 mb->un.mb_words[0], mb->un.mb_words[1],
6760 mb->un.mb_words[2], mb->un.mb_words[3],
6761 mb->un.mb_words[4], mb->un.mb_words[5],
6762 mb->un.mb_words[6], mb->un.mb_words[7],
6763 mb->un.mb_words[8], mb->un.mb_words[9],
6764 mb->un.mb_words[10], mb->un.mb_words[11],
6765 mb->un.mb_words[12], mboxq->mcqe.word0,
6766 mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
6767 mboxq->mcqe.trailer);
6768exit:
6769 /* We are holding the token, no needed for lock when release */
6770 spin_lock_irqsave(&phba->hbalock, iflag);
6771 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
6772 phba->sli.mbox_active = NULL;
6773 spin_unlock_irqrestore(&phba->hbalock, iflag);
6774 return rc;
6775}
6776
6777/**
6778 * lpfc_sli_issue_mbox_s4 - Issue an SLI4 mailbox command to firmware
6779 * @phba: Pointer to HBA context object.
6780 * @pmbox: Pointer to mailbox object.
6781 * @flag: Flag indicating how the mailbox need to be processed.
6782 *
6783 * This function is called by discovery code and HBA management code to submit
6784 * a mailbox command to firmware with SLI-4 interface spec.
6785 *
6786 * Return codes the caller owns the mailbox command after the return of the
6787 * function.
6788 **/
6789static int
6790lpfc_sli_issue_mbox_s4(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
6791 uint32_t flag)
6792{
6793 struct lpfc_sli *psli = &phba->sli;
6794 unsigned long iflags;
6795 int rc;
6796
James Smartb76f2dc2011-07-22 18:37:42 -04006797 /* dump from issue mailbox command if setup */
6798 lpfc_idiag_mbxacc_dump_issue_mbox(phba, &mboxq->u.mb);
6799
James Smart8fa38512009-07-19 10:01:03 -04006800 rc = lpfc_mbox_dev_check(phba);
6801 if (unlikely(rc)) {
6802 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6803 "(%d):2544 Mailbox command x%x (x%x) "
6804 "cannot issue Data: x%x x%x\n",
6805 mboxq->vport ? mboxq->vport->vpi : 0,
6806 mboxq->u.mb.mbxCommand,
6807 lpfc_sli4_mbox_opcode_get(phba, mboxq),
6808 psli->sli_flag, flag);
6809 goto out_not_finished;
6810 }
6811
James Smartda0436e2009-05-22 14:51:39 -04006812 /* Detect polling mode and jump to a handler */
6813 if (!phba->sli4_hba.intr_enable) {
6814 if (flag == MBX_POLL)
6815 rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
6816 else
6817 rc = -EIO;
6818 if (rc != MBX_SUCCESS)
James Smart05580562011-05-24 11:40:48 -04006819 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
James Smartda0436e2009-05-22 14:51:39 -04006820 "(%d):2541 Mailbox command x%x "
6821 "(x%x) cannot issue Data: x%x x%x\n",
6822 mboxq->vport ? mboxq->vport->vpi : 0,
6823 mboxq->u.mb.mbxCommand,
6824 lpfc_sli4_mbox_opcode_get(phba, mboxq),
6825 psli->sli_flag, flag);
6826 return rc;
6827 } else if (flag == MBX_POLL) {
James Smartf1126682009-06-10 17:22:44 -04006828 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
6829 "(%d):2542 Try to issue mailbox command "
6830 "x%x (x%x) synchronously ahead of async"
6831 "mailbox command queue: x%x x%x\n",
James Smartda0436e2009-05-22 14:51:39 -04006832 mboxq->vport ? mboxq->vport->vpi : 0,
6833 mboxq->u.mb.mbxCommand,
6834 lpfc_sli4_mbox_opcode_get(phba, mboxq),
6835 psli->sli_flag, flag);
James Smartf1126682009-06-10 17:22:44 -04006836 /* Try to block the asynchronous mailbox posting */
6837 rc = lpfc_sli4_async_mbox_block(phba);
6838 if (!rc) {
6839 /* Successfully blocked, now issue sync mbox cmd */
6840 rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
6841 if (rc != MBX_SUCCESS)
6842 lpfc_printf_log(phba, KERN_ERR,
6843 LOG_MBOX | LOG_SLI,
6844 "(%d):2597 Mailbox command "
6845 "x%x (x%x) cannot issue "
6846 "Data: x%x x%x\n",
6847 mboxq->vport ?
6848 mboxq->vport->vpi : 0,
6849 mboxq->u.mb.mbxCommand,
6850 lpfc_sli4_mbox_opcode_get(phba,
6851 mboxq),
6852 psli->sli_flag, flag);
6853 /* Unblock the async mailbox posting afterward */
6854 lpfc_sli4_async_mbox_unblock(phba);
6855 }
6856 return rc;
James Smartda0436e2009-05-22 14:51:39 -04006857 }
6858
6859 /* Now, interrupt mode asynchrous mailbox command */
6860 rc = lpfc_mbox_cmd_check(phba, mboxq);
6861 if (rc) {
6862 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6863 "(%d):2543 Mailbox command x%x (x%x) "
6864 "cannot issue Data: x%x x%x\n",
6865 mboxq->vport ? mboxq->vport->vpi : 0,
6866 mboxq->u.mb.mbxCommand,
6867 lpfc_sli4_mbox_opcode_get(phba, mboxq),
6868 psli->sli_flag, flag);
6869 goto out_not_finished;
6870 }
James Smartda0436e2009-05-22 14:51:39 -04006871
6872 /* Put the mailbox command to the driver internal FIFO */
6873 psli->slistat.mbox_busy++;
6874 spin_lock_irqsave(&phba->hbalock, iflags);
6875 lpfc_mbox_put(phba, mboxq);
6876 spin_unlock_irqrestore(&phba->hbalock, iflags);
6877 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
6878 "(%d):0354 Mbox cmd issue - Enqueue Data: "
6879 "x%x (x%x) x%x x%x x%x\n",
6880 mboxq->vport ? mboxq->vport->vpi : 0xffffff,
6881 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
6882 lpfc_sli4_mbox_opcode_get(phba, mboxq),
6883 phba->pport->port_state,
6884 psli->sli_flag, MBX_NOWAIT);
6885 /* Wake up worker thread to transport mailbox command from head */
6886 lpfc_worker_wake_up(phba);
6887
6888 return MBX_BUSY;
6889
6890out_not_finished:
6891 return MBX_NOT_FINISHED;
6892}
6893
6894/**
6895 * lpfc_sli4_post_async_mbox - Post an SLI4 mailbox command to device
6896 * @phba: Pointer to HBA context object.
6897 *
6898 * This function is called by worker thread to send a mailbox command to
6899 * SLI4 HBA firmware.
6900 *
6901 **/
6902int
6903lpfc_sli4_post_async_mbox(struct lpfc_hba *phba)
6904{
6905 struct lpfc_sli *psli = &phba->sli;
6906 LPFC_MBOXQ_t *mboxq;
6907 int rc = MBX_SUCCESS;
6908 unsigned long iflags;
6909 struct lpfc_mqe *mqe;
6910 uint32_t mbx_cmnd;
6911
6912 /* Check interrupt mode before post async mailbox command */
6913 if (unlikely(!phba->sli4_hba.intr_enable))
6914 return MBX_NOT_FINISHED;
6915
6916 /* Check for mailbox command service token */
6917 spin_lock_irqsave(&phba->hbalock, iflags);
6918 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
6919 spin_unlock_irqrestore(&phba->hbalock, iflags);
6920 return MBX_NOT_FINISHED;
6921 }
6922 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
6923 spin_unlock_irqrestore(&phba->hbalock, iflags);
6924 return MBX_NOT_FINISHED;
6925 }
6926 if (unlikely(phba->sli.mbox_active)) {
6927 spin_unlock_irqrestore(&phba->hbalock, iflags);
6928 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6929 "0384 There is pending active mailbox cmd\n");
6930 return MBX_NOT_FINISHED;
6931 }
6932 /* Take the mailbox command service token */
6933 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
6934
6935 /* Get the next mailbox command from head of queue */
6936 mboxq = lpfc_mbox_get(phba);
6937
6938 /* If no more mailbox command waiting for post, we're done */
6939 if (!mboxq) {
6940 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
6941 spin_unlock_irqrestore(&phba->hbalock, iflags);
6942 return MBX_SUCCESS;
6943 }
6944 phba->sli.mbox_active = mboxq;
6945 spin_unlock_irqrestore(&phba->hbalock, iflags);
6946
6947 /* Check device readiness for posting mailbox command */
6948 rc = lpfc_mbox_dev_check(phba);
6949 if (unlikely(rc))
6950 /* Driver clean routine will clean up pending mailbox */
6951 goto out_not_finished;
6952
6953 /* Prepare the mbox command to be posted */
6954 mqe = &mboxq->u.mqe;
6955 mbx_cmnd = bf_get(lpfc_mqe_command, mqe);
6956
6957 /* Start timer for the mbox_tmo and log some mailbox post messages */
6958 mod_timer(&psli->mbox_tmo, (jiffies +
6959 (HZ * lpfc_mbox_tmo_val(phba, mbx_cmnd))));
6960
6961 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
6962 "(%d):0355 Mailbox cmd x%x (x%x) issue Data: "
6963 "x%x x%x\n",
6964 mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
6965 lpfc_sli4_mbox_opcode_get(phba, mboxq),
6966 phba->pport->port_state, psli->sli_flag);
6967
6968 if (mbx_cmnd != MBX_HEARTBEAT) {
6969 if (mboxq->vport) {
6970 lpfc_debugfs_disc_trc(mboxq->vport,
6971 LPFC_DISC_TRC_MBOX_VPORT,
6972 "MBOX Send vport: cmd:x%x mb:x%x x%x",
6973 mbx_cmnd, mqe->un.mb_words[0],
6974 mqe->un.mb_words[1]);
6975 } else {
6976 lpfc_debugfs_disc_trc(phba->pport,
6977 LPFC_DISC_TRC_MBOX,
6978 "MBOX Send: cmd:x%x mb:x%x x%x",
6979 mbx_cmnd, mqe->un.mb_words[0],
6980 mqe->un.mb_words[1]);
6981 }
6982 }
6983 psli->slistat.mbox_cmd++;
6984
6985 /* Post the mailbox command to the port */
6986 rc = lpfc_sli4_mq_put(phba->sli4_hba.mbx_wq, mqe);
6987 if (rc != MBX_SUCCESS) {
6988 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6989 "(%d):2533 Mailbox command x%x (x%x) "
6990 "cannot issue Data: x%x x%x\n",
6991 mboxq->vport ? mboxq->vport->vpi : 0,
6992 mboxq->u.mb.mbxCommand,
6993 lpfc_sli4_mbox_opcode_get(phba, mboxq),
6994 psli->sli_flag, MBX_NOWAIT);
6995 goto out_not_finished;
6996 }
6997
6998 return rc;
6999
7000out_not_finished:
7001 spin_lock_irqsave(&phba->hbalock, iflags);
7002 mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
7003 __lpfc_mbox_cmpl_put(phba, mboxq);
7004 /* Release the token */
7005 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
7006 phba->sli.mbox_active = NULL;
7007 spin_unlock_irqrestore(&phba->hbalock, iflags);
7008
7009 return MBX_NOT_FINISHED;
7010}
7011
7012/**
7013 * lpfc_sli_issue_mbox - Wrapper func for issuing mailbox command
7014 * @phba: Pointer to HBA context object.
7015 * @pmbox: Pointer to mailbox object.
7016 * @flag: Flag indicating how the mailbox need to be processed.
7017 *
7018 * This routine wraps the actual SLI3 or SLI4 mailbox issuing routine from
7019 * the API jump table function pointer from the lpfc_hba struct.
7020 *
7021 * Return codes the caller owns the mailbox command after the return of the
7022 * function.
7023 **/
7024int
7025lpfc_sli_issue_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox, uint32_t flag)
7026{
7027 return phba->lpfc_sli_issue_mbox(phba, pmbox, flag);
7028}
7029
7030/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03007031 * lpfc_mbox_api_table_setup - Set up mbox api function jump table
James Smartda0436e2009-05-22 14:51:39 -04007032 * @phba: The hba struct for which this call is being executed.
7033 * @dev_grp: The HBA PCI-Device group number.
7034 *
7035 * This routine sets up the mbox interface API function jump table in @phba
7036 * struct.
7037 * Returns: 0 - success, -ENODEV - failure.
7038 **/
7039int
7040lpfc_mbox_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
7041{
7042
7043 switch (dev_grp) {
7044 case LPFC_PCI_DEV_LP:
7045 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s3;
7046 phba->lpfc_sli_handle_slow_ring_event =
7047 lpfc_sli_handle_slow_ring_event_s3;
7048 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s3;
7049 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s3;
7050 phba->lpfc_sli_brdready = lpfc_sli_brdready_s3;
7051 break;
7052 case LPFC_PCI_DEV_OC:
7053 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s4;
7054 phba->lpfc_sli_handle_slow_ring_event =
7055 lpfc_sli_handle_slow_ring_event_s4;
7056 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s4;
7057 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s4;
7058 phba->lpfc_sli_brdready = lpfc_sli_brdready_s4;
7059 break;
7060 default:
7061 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7062 "1420 Invalid HBA PCI-device group: 0x%x\n",
7063 dev_grp);
7064 return -ENODEV;
7065 break;
7066 }
7067 return 0;
7068}
7069
7070/**
James Smart3621a712009-04-06 18:47:14 -04007071 * __lpfc_sli_ringtx_put - Add an iocb to the txq
James Smarte59058c2008-08-24 21:49:00 -04007072 * @phba: Pointer to HBA context object.
7073 * @pring: Pointer to driver SLI ring object.
7074 * @piocb: Pointer to address of newly added command iocb.
7075 *
7076 * This function is called with hbalock held to add a command
7077 * iocb to the txq when SLI layer cannot submit the command iocb
7078 * to the ring.
7079 **/
James Smart2a9bf3d2010-06-07 15:24:45 -04007080void
James Smart92d7f7b2007-06-17 19:56:38 -05007081__lpfc_sli_ringtx_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -05007082 struct lpfc_iocbq *piocb)
dea31012005-04-17 16:05:31 -05007083{
7084 /* Insert the caller's iocb in the txq tail for later processing. */
7085 list_add_tail(&piocb->list, &pring->txq);
7086 pring->txq_cnt++;
dea31012005-04-17 16:05:31 -05007087}
7088
James Smarte59058c2008-08-24 21:49:00 -04007089/**
James Smart3621a712009-04-06 18:47:14 -04007090 * lpfc_sli_next_iocb - Get the next iocb in the txq
James Smarte59058c2008-08-24 21:49:00 -04007091 * @phba: Pointer to HBA context object.
7092 * @pring: Pointer to driver SLI ring object.
7093 * @piocb: Pointer to address of newly added command iocb.
7094 *
7095 * This function is called with hbalock held before a new
7096 * iocb is submitted to the firmware. This function checks
7097 * txq to flush the iocbs in txq to Firmware before
7098 * submitting new iocbs to the Firmware.
7099 * If there are iocbs in the txq which need to be submitted
7100 * to firmware, lpfc_sli_next_iocb returns the first element
7101 * of the txq after dequeuing it from txq.
7102 * If there is no iocb in the txq then the function will return
7103 * *piocb and *piocb is set to NULL. Caller needs to check
7104 * *piocb to find if there are more commands in the txq.
7105 **/
dea31012005-04-17 16:05:31 -05007106static struct lpfc_iocbq *
7107lpfc_sli_next_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -05007108 struct lpfc_iocbq **piocb)
dea31012005-04-17 16:05:31 -05007109{
7110 struct lpfc_iocbq * nextiocb;
7111
7112 nextiocb = lpfc_sli_ringtx_get(phba, pring);
7113 if (!nextiocb) {
7114 nextiocb = *piocb;
7115 *piocb = NULL;
7116 }
7117
7118 return nextiocb;
7119}
7120
James Smarte59058c2008-08-24 21:49:00 -04007121/**
James Smart3772a992009-05-22 14:50:54 -04007122 * __lpfc_sli_issue_iocb_s3 - SLI3 device lockless ver of lpfc_sli_issue_iocb
James Smarte59058c2008-08-24 21:49:00 -04007123 * @phba: Pointer to HBA context object.
James Smart3772a992009-05-22 14:50:54 -04007124 * @ring_number: SLI ring number to issue iocb on.
James Smarte59058c2008-08-24 21:49:00 -04007125 * @piocb: Pointer to command iocb.
7126 * @flag: Flag indicating if this command can be put into txq.
7127 *
James Smart3772a992009-05-22 14:50:54 -04007128 * __lpfc_sli_issue_iocb_s3 is used by other functions in the driver to issue
7129 * an iocb command to an HBA with SLI-3 interface spec. If the PCI slot is
7130 * recovering from error state, if HBA is resetting or if LPFC_STOP_IOCB_EVENT
7131 * flag is turned on, the function returns IOCB_ERROR. When the link is down,
7132 * this function allows only iocbs for posting buffers. This function finds
7133 * next available slot in the command ring and posts the command to the
7134 * available slot and writes the port attention register to request HBA start
7135 * processing new iocb. If there is no slot available in the ring and
7136 * flag & SLI_IOCB_RET_IOCB is set, the new iocb is added to the txq, otherwise
7137 * the function returns IOCB_BUSY.
James Smarte59058c2008-08-24 21:49:00 -04007138 *
James Smart3772a992009-05-22 14:50:54 -04007139 * This function is called with hbalock held. The function will return success
7140 * after it successfully submit the iocb to firmware or after adding to the
7141 * txq.
James Smarte59058c2008-08-24 21:49:00 -04007142 **/
James Smart98c9ea52007-10-27 13:37:33 -04007143static int
James Smart3772a992009-05-22 14:50:54 -04007144__lpfc_sli_issue_iocb_s3(struct lpfc_hba *phba, uint32_t ring_number,
dea31012005-04-17 16:05:31 -05007145 struct lpfc_iocbq *piocb, uint32_t flag)
7146{
7147 struct lpfc_iocbq *nextiocb;
7148 IOCB_t *iocb;
James Smart3772a992009-05-22 14:50:54 -04007149 struct lpfc_sli_ring *pring = &phba->sli.ring[ring_number];
dea31012005-04-17 16:05:31 -05007150
James Smart92d7f7b2007-06-17 19:56:38 -05007151 if (piocb->iocb_cmpl && (!piocb->vport) &&
7152 (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
7153 (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
7154 lpfc_printf_log(phba, KERN_ERR,
7155 LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04007156 "1807 IOCB x%x failed. No vport\n",
James Smart92d7f7b2007-06-17 19:56:38 -05007157 piocb->iocb.ulpCommand);
7158 dump_stack();
7159 return IOCB_ERROR;
7160 }
7161
7162
Linas Vepstas8d63f372007-02-14 14:28:36 -06007163 /* If the PCI channel is in offline state, do not post iocbs. */
7164 if (unlikely(pci_channel_offline(phba->pcidev)))
7165 return IOCB_ERROR;
7166
James Smarta257bf92009-04-06 18:48:10 -04007167 /* If HBA has a deferred error attention, fail the iocb. */
7168 if (unlikely(phba->hba_flag & DEFER_ERATT))
7169 return IOCB_ERROR;
7170
dea31012005-04-17 16:05:31 -05007171 /*
7172 * We should never get an IOCB if we are in a < LINK_DOWN state
7173 */
James Smart2e0fef82007-06-17 19:56:36 -05007174 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
dea31012005-04-17 16:05:31 -05007175 return IOCB_ERROR;
7176
7177 /*
7178 * Check to see if we are blocking IOCB processing because of a
James Smart0b727fe2007-10-27 13:37:25 -04007179 * outstanding event.
dea31012005-04-17 16:05:31 -05007180 */
James Smart0b727fe2007-10-27 13:37:25 -04007181 if (unlikely(pring->flag & LPFC_STOP_IOCB_EVENT))
dea31012005-04-17 16:05:31 -05007182 goto iocb_busy;
7183
James Smart2e0fef82007-06-17 19:56:36 -05007184 if (unlikely(phba->link_state == LPFC_LINK_DOWN)) {
dea31012005-04-17 16:05:31 -05007185 /*
James Smart2680eea2007-04-25 09:52:55 -04007186 * Only CREATE_XRI, CLOSE_XRI, and QUE_RING_BUF
dea31012005-04-17 16:05:31 -05007187 * can be issued if the link is not up.
7188 */
7189 switch (piocb->iocb.ulpCommand) {
James Smart84774a42008-08-24 21:50:06 -04007190 case CMD_GEN_REQUEST64_CR:
7191 case CMD_GEN_REQUEST64_CX:
7192 if (!(phba->sli.sli_flag & LPFC_MENLO_MAINT) ||
7193 (piocb->iocb.un.genreq64.w5.hcsw.Rctl !=
James Smart6a9c52c2009-10-02 15:16:51 -04007194 FC_RCTL_DD_UNSOL_CMD) ||
James Smart84774a42008-08-24 21:50:06 -04007195 (piocb->iocb.un.genreq64.w5.hcsw.Type !=
7196 MENLO_TRANSPORT_TYPE))
7197
7198 goto iocb_busy;
7199 break;
dea31012005-04-17 16:05:31 -05007200 case CMD_QUE_RING_BUF_CN:
7201 case CMD_QUE_RING_BUF64_CN:
dea31012005-04-17 16:05:31 -05007202 /*
7203 * For IOCBs, like QUE_RING_BUF, that have no rsp ring
7204 * completion, iocb_cmpl MUST be 0.
7205 */
7206 if (piocb->iocb_cmpl)
7207 piocb->iocb_cmpl = NULL;
7208 /*FALLTHROUGH*/
7209 case CMD_CREATE_XRI_CR:
James Smart2680eea2007-04-25 09:52:55 -04007210 case CMD_CLOSE_XRI_CN:
7211 case CMD_CLOSE_XRI_CX:
dea31012005-04-17 16:05:31 -05007212 break;
7213 default:
7214 goto iocb_busy;
7215 }
7216
7217 /*
7218 * For FCP commands, we must be in a state where we can process link
7219 * attention events.
7220 */
7221 } else if (unlikely(pring->ringno == phba->sli.fcp_ring &&
James Smart92d7f7b2007-06-17 19:56:38 -05007222 !(phba->sli.sli_flag & LPFC_PROCESS_LA))) {
dea31012005-04-17 16:05:31 -05007223 goto iocb_busy;
James Smart92d7f7b2007-06-17 19:56:38 -05007224 }
dea31012005-04-17 16:05:31 -05007225
dea31012005-04-17 16:05:31 -05007226 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
7227 (nextiocb = lpfc_sli_next_iocb(phba, pring, &piocb)))
7228 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
7229
7230 if (iocb)
7231 lpfc_sli_update_ring(phba, pring);
7232 else
7233 lpfc_sli_update_full_ring(phba, pring);
7234
7235 if (!piocb)
7236 return IOCB_SUCCESS;
7237
7238 goto out_busy;
7239
7240 iocb_busy:
7241 pring->stats.iocb_cmd_delay++;
7242
7243 out_busy:
7244
7245 if (!(flag & SLI_IOCB_RET_IOCB)) {
James Smart92d7f7b2007-06-17 19:56:38 -05007246 __lpfc_sli_ringtx_put(phba, pring, piocb);
dea31012005-04-17 16:05:31 -05007247 return IOCB_SUCCESS;
7248 }
7249
7250 return IOCB_BUSY;
7251}
7252
James Smart3772a992009-05-22 14:50:54 -04007253/**
James Smart4f774512009-05-22 14:52:35 -04007254 * lpfc_sli4_bpl2sgl - Convert the bpl/bde to a sgl.
7255 * @phba: Pointer to HBA context object.
7256 * @piocb: Pointer to command iocb.
7257 * @sglq: Pointer to the scatter gather queue object.
7258 *
7259 * This routine converts the bpl or bde that is in the IOCB
7260 * to a sgl list for the sli4 hardware. The physical address
7261 * of the bpl/bde is converted back to a virtual address.
7262 * If the IOCB contains a BPL then the list of BDE's is
7263 * converted to sli4_sge's. If the IOCB contains a single
7264 * BDE then it is converted to a single sli_sge.
7265 * The IOCB is still in cpu endianess so the contents of
7266 * the bpl can be used without byte swapping.
7267 *
7268 * Returns valid XRI = Success, NO_XRI = Failure.
7269**/
7270static uint16_t
7271lpfc_sli4_bpl2sgl(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq,
7272 struct lpfc_sglq *sglq)
7273{
7274 uint16_t xritag = NO_XRI;
7275 struct ulp_bde64 *bpl = NULL;
7276 struct ulp_bde64 bde;
7277 struct sli4_sge *sgl = NULL;
7278 IOCB_t *icmd;
7279 int numBdes = 0;
7280 int i = 0;
James Smart63e801c2010-11-20 23:14:19 -05007281 uint32_t offset = 0; /* accumulated offset in the sg request list */
7282 int inbound = 0; /* number of sg reply entries inbound from firmware */
James Smart4f774512009-05-22 14:52:35 -04007283
7284 if (!piocbq || !sglq)
7285 return xritag;
7286
7287 sgl = (struct sli4_sge *)sglq->sgl;
7288 icmd = &piocbq->iocb;
7289 if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
7290 numBdes = icmd->un.genreq64.bdl.bdeSize /
7291 sizeof(struct ulp_bde64);
7292 /* The addrHigh and addrLow fields within the IOCB
7293 * have not been byteswapped yet so there is no
7294 * need to swap them back.
7295 */
7296 bpl = (struct ulp_bde64 *)
7297 ((struct lpfc_dmabuf *)piocbq->context3)->virt;
7298
7299 if (!bpl)
7300 return xritag;
7301
7302 for (i = 0; i < numBdes; i++) {
7303 /* Should already be byte swapped. */
James Smart28baac72010-02-12 14:42:03 -05007304 sgl->addr_hi = bpl->addrHigh;
7305 sgl->addr_lo = bpl->addrLow;
7306
James Smart05580562011-05-24 11:40:48 -04007307 sgl->word2 = le32_to_cpu(sgl->word2);
James Smart4f774512009-05-22 14:52:35 -04007308 if ((i+1) == numBdes)
7309 bf_set(lpfc_sli4_sge_last, sgl, 1);
7310 else
7311 bf_set(lpfc_sli4_sge_last, sgl, 0);
James Smart28baac72010-02-12 14:42:03 -05007312 /* swap the size field back to the cpu so we
7313 * can assign it to the sgl.
7314 */
7315 bde.tus.w = le32_to_cpu(bpl->tus.w);
7316 sgl->sge_len = cpu_to_le32(bde.tus.f.bdeSize);
James Smart63e801c2010-11-20 23:14:19 -05007317 /* The offsets in the sgl need to be accumulated
7318 * separately for the request and reply lists.
7319 * The request is always first, the reply follows.
7320 */
7321 if (piocbq->iocb.ulpCommand == CMD_GEN_REQUEST64_CR) {
7322 /* add up the reply sg entries */
7323 if (bpl->tus.f.bdeFlags == BUFF_TYPE_BDE_64I)
7324 inbound++;
7325 /* first inbound? reset the offset */
7326 if (inbound == 1)
7327 offset = 0;
7328 bf_set(lpfc_sli4_sge_offset, sgl, offset);
7329 offset += bde.tus.f.bdeSize;
7330 }
James Smart546fc852011-03-11 16:06:29 -05007331 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart4f774512009-05-22 14:52:35 -04007332 bpl++;
7333 sgl++;
7334 }
7335 } else if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BDE_64) {
7336 /* The addrHigh and addrLow fields of the BDE have not
7337 * been byteswapped yet so they need to be swapped
7338 * before putting them in the sgl.
7339 */
7340 sgl->addr_hi =
7341 cpu_to_le32(icmd->un.genreq64.bdl.addrHigh);
7342 sgl->addr_lo =
7343 cpu_to_le32(icmd->un.genreq64.bdl.addrLow);
James Smart05580562011-05-24 11:40:48 -04007344 sgl->word2 = le32_to_cpu(sgl->word2);
James Smart4f774512009-05-22 14:52:35 -04007345 bf_set(lpfc_sli4_sge_last, sgl, 1);
7346 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart28baac72010-02-12 14:42:03 -05007347 sgl->sge_len =
7348 cpu_to_le32(icmd->un.genreq64.bdl.bdeSize);
James Smart4f774512009-05-22 14:52:35 -04007349 }
7350 return sglq->sli4_xritag;
7351}
7352
7353/**
7354 * lpfc_sli4_scmd_to_wqidx_distr - scsi command to SLI4 WQ index distribution
7355 * @phba: Pointer to HBA context object.
James Smart4f774512009-05-22 14:52:35 -04007356 *
James Smarta93ff372010-10-22 11:06:08 -04007357 * This routine performs a roundrobin SCSI command to SLI4 FCP WQ index
James Smart8fa38512009-07-19 10:01:03 -04007358 * distribution. This is called by __lpfc_sli_issue_iocb_s4() with the hbalock
7359 * held.
James Smart4f774512009-05-22 14:52:35 -04007360 *
7361 * Return: index into SLI4 fast-path FCP queue index.
7362 **/
7363static uint32_t
James Smart8fa38512009-07-19 10:01:03 -04007364lpfc_sli4_scmd_to_wqidx_distr(struct lpfc_hba *phba)
James Smart4f774512009-05-22 14:52:35 -04007365{
James Smart8fa38512009-07-19 10:01:03 -04007366 ++phba->fcp_qidx;
7367 if (phba->fcp_qidx >= phba->cfg_fcp_wq_count)
7368 phba->fcp_qidx = 0;
James Smart4f774512009-05-22 14:52:35 -04007369
James Smart8fa38512009-07-19 10:01:03 -04007370 return phba->fcp_qidx;
James Smart4f774512009-05-22 14:52:35 -04007371}
7372
7373/**
7374 * lpfc_sli_iocb2wqe - Convert the IOCB to a work queue entry.
7375 * @phba: Pointer to HBA context object.
7376 * @piocb: Pointer to command iocb.
7377 * @wqe: Pointer to the work queue entry.
7378 *
7379 * This routine converts the iocb command to its Work Queue Entry
7380 * equivalent. The wqe pointer should not have any fields set when
7381 * this routine is called because it will memcpy over them.
7382 * This routine does not set the CQ_ID or the WQEC bits in the
7383 * wqe.
7384 *
7385 * Returns: 0 = Success, IOCB_ERROR = Failure.
7386 **/
7387static int
7388lpfc_sli4_iocb2wqe(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq,
7389 union lpfc_wqe *wqe)
7390{
James Smart5ffc2662009-11-18 15:39:44 -05007391 uint32_t xmit_len = 0, total_len = 0;
James Smart4f774512009-05-22 14:52:35 -04007392 uint8_t ct = 0;
7393 uint32_t fip;
7394 uint32_t abort_tag;
7395 uint8_t command_type = ELS_COMMAND_NON_FIP;
7396 uint8_t cmnd;
7397 uint16_t xritag;
James Smartdcf2a4e2010-09-29 11:18:53 -04007398 uint16_t abrt_iotag;
7399 struct lpfc_iocbq *abrtiocbq;
James Smart4f774512009-05-22 14:52:35 -04007400 struct ulp_bde64 *bpl = NULL;
James Smartf0d9bcc2010-10-22 11:07:09 -04007401 uint32_t els_id = LPFC_ELS_ID_DEFAULT;
James Smart5ffc2662009-11-18 15:39:44 -05007402 int numBdes, i;
7403 struct ulp_bde64 bde;
James Smartc31098c2011-04-16 11:03:33 -04007404 struct lpfc_nodelist *ndlp;
James Smart4f774512009-05-22 14:52:35 -04007405
James Smart45ed1192009-10-02 15:17:02 -04007406 fip = phba->hba_flag & HBA_FIP_SUPPORT;
James Smart4f774512009-05-22 14:52:35 -04007407 /* The fcp commands will set command type */
James Smart0c287582009-06-10 17:22:56 -04007408 if (iocbq->iocb_flag & LPFC_IO_FCP)
James Smart4f774512009-05-22 14:52:35 -04007409 command_type = FCP_COMMAND;
James Smartc8685952009-11-18 15:39:16 -05007410 else if (fip && (iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK))
James Smart0c287582009-06-10 17:22:56 -04007411 command_type = ELS_COMMAND_FIP;
7412 else
7413 command_type = ELS_COMMAND_NON_FIP;
7414
James Smart4f774512009-05-22 14:52:35 -04007415 /* Some of the fields are in the right position already */
7416 memcpy(wqe, &iocbq->iocb, sizeof(union lpfc_wqe));
7417 abort_tag = (uint32_t) iocbq->iotag;
7418 xritag = iocbq->sli4_xritag;
James Smartf0d9bcc2010-10-22 11:07:09 -04007419 wqe->generic.wqe_com.word7 = 0; /* The ct field has moved so reset */
James Smart4f774512009-05-22 14:52:35 -04007420 /* words0-2 bpl convert bde */
7421 if (iocbq->iocb.un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
James Smart5ffc2662009-11-18 15:39:44 -05007422 numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
7423 sizeof(struct ulp_bde64);
James Smart4f774512009-05-22 14:52:35 -04007424 bpl = (struct ulp_bde64 *)
7425 ((struct lpfc_dmabuf *)iocbq->context3)->virt;
7426 if (!bpl)
7427 return IOCB_ERROR;
7428
7429 /* Should already be byte swapped. */
7430 wqe->generic.bde.addrHigh = le32_to_cpu(bpl->addrHigh);
7431 wqe->generic.bde.addrLow = le32_to_cpu(bpl->addrLow);
7432 /* swap the size field back to the cpu so we
7433 * can assign it to the sgl.
7434 */
7435 wqe->generic.bde.tus.w = le32_to_cpu(bpl->tus.w);
James Smart5ffc2662009-11-18 15:39:44 -05007436 xmit_len = wqe->generic.bde.tus.f.bdeSize;
7437 total_len = 0;
7438 for (i = 0; i < numBdes; i++) {
7439 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
7440 total_len += bde.tus.f.bdeSize;
7441 }
James Smart4f774512009-05-22 14:52:35 -04007442 } else
James Smart5ffc2662009-11-18 15:39:44 -05007443 xmit_len = iocbq->iocb.un.fcpi64.bdl.bdeSize;
James Smart4f774512009-05-22 14:52:35 -04007444
7445 iocbq->iocb.ulpIoTag = iocbq->iotag;
7446 cmnd = iocbq->iocb.ulpCommand;
7447
7448 switch (iocbq->iocb.ulpCommand) {
7449 case CMD_ELS_REQUEST64_CR:
James Smartc31098c2011-04-16 11:03:33 -04007450 ndlp = (struct lpfc_nodelist *)iocbq->context1;
James Smart4f774512009-05-22 14:52:35 -04007451 if (!iocbq->iocb.ulpLe) {
7452 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
7453 "2007 Only Limited Edition cmd Format"
7454 " supported 0x%x\n",
7455 iocbq->iocb.ulpCommand);
7456 return IOCB_ERROR;
7457 }
James Smart5ffc2662009-11-18 15:39:44 -05007458 wqe->els_req.payload_len = xmit_len;
James Smart4f774512009-05-22 14:52:35 -04007459 /* Els_reguest64 has a TMO */
7460 bf_set(wqe_tmo, &wqe->els_req.wqe_com,
7461 iocbq->iocb.ulpTimeout);
7462 /* Need a VF for word 4 set the vf bit*/
7463 bf_set(els_req64_vf, &wqe->els_req, 0);
7464 /* And a VFID for word 12 */
7465 bf_set(els_req64_vfid, &wqe->els_req, 0);
James Smart4f774512009-05-22 14:52:35 -04007466 ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
James Smartf0d9bcc2010-10-22 11:07:09 -04007467 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
7468 iocbq->iocb.ulpContext);
7469 bf_set(wqe_ct, &wqe->els_req.wqe_com, ct);
7470 bf_set(wqe_pu, &wqe->els_req.wqe_com, 0);
James Smart4f774512009-05-22 14:52:35 -04007471 /* CCP CCPE PV PRI in word10 were set in the memcpy */
James Smartc8685952009-11-18 15:39:16 -05007472 if (command_type == ELS_COMMAND_FIP) {
7473 els_id = ((iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK)
7474 >> LPFC_FIP_ELS_ID_SHIFT);
7475 }
James Smart6d368e52011-05-24 11:44:12 -04007476 bf_set(wqe_temp_rpi, &wqe->els_req.wqe_com,
7477 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
James Smartf0d9bcc2010-10-22 11:07:09 -04007478 bf_set(wqe_els_id, &wqe->els_req.wqe_com, els_id);
7479 bf_set(wqe_dbde, &wqe->els_req.wqe_com, 1);
7480 bf_set(wqe_iod, &wqe->els_req.wqe_com, LPFC_WQE_IOD_READ);
7481 bf_set(wqe_qosd, &wqe->els_req.wqe_com, 1);
7482 bf_set(wqe_lenloc, &wqe->els_req.wqe_com, LPFC_WQE_LENLOC_NONE);
7483 bf_set(wqe_ebde_cnt, &wqe->els_req.wqe_com, 0);
James Smart7851fe22011-07-22 18:36:52 -04007484 break;
James Smart5ffc2662009-11-18 15:39:44 -05007485 case CMD_XMIT_SEQUENCE64_CX:
James Smartf0d9bcc2010-10-22 11:07:09 -04007486 bf_set(wqe_ctxt_tag, &wqe->xmit_sequence.wqe_com,
7487 iocbq->iocb.un.ulpWord[3]);
7488 bf_set(wqe_rcvoxid, &wqe->xmit_sequence.wqe_com,
James Smart7851fe22011-07-22 18:36:52 -04007489 iocbq->iocb.unsli3.rcvsli3.ox_id);
James Smart5ffc2662009-11-18 15:39:44 -05007490 /* The entire sequence is transmitted for this IOCB */
7491 xmit_len = total_len;
7492 cmnd = CMD_XMIT_SEQUENCE64_CR;
James Smart4f774512009-05-22 14:52:35 -04007493 case CMD_XMIT_SEQUENCE64_CR:
James Smartf0d9bcc2010-10-22 11:07:09 -04007494 /* word3 iocb=io_tag32 wqe=reserved */
7495 wqe->xmit_sequence.rsvd3 = 0;
James Smart4f774512009-05-22 14:52:35 -04007496 /* word4 relative_offset memcpy */
7497 /* word5 r_ctl/df_ctl memcpy */
James Smartf0d9bcc2010-10-22 11:07:09 -04007498 bf_set(wqe_pu, &wqe->xmit_sequence.wqe_com, 0);
7499 bf_set(wqe_dbde, &wqe->xmit_sequence.wqe_com, 1);
7500 bf_set(wqe_iod, &wqe->xmit_sequence.wqe_com,
7501 LPFC_WQE_IOD_WRITE);
7502 bf_set(wqe_lenloc, &wqe->xmit_sequence.wqe_com,
7503 LPFC_WQE_LENLOC_WORD12);
7504 bf_set(wqe_ebde_cnt, &wqe->xmit_sequence.wqe_com, 0);
James Smart5ffc2662009-11-18 15:39:44 -05007505 wqe->xmit_sequence.xmit_len = xmit_len;
7506 command_type = OTHER_COMMAND;
James Smart7851fe22011-07-22 18:36:52 -04007507 break;
James Smart4f774512009-05-22 14:52:35 -04007508 case CMD_XMIT_BCAST64_CN:
James Smartf0d9bcc2010-10-22 11:07:09 -04007509 /* word3 iocb=iotag32 wqe=seq_payload_len */
7510 wqe->xmit_bcast64.seq_payload_len = xmit_len;
James Smart4f774512009-05-22 14:52:35 -04007511 /* word4 iocb=rsvd wqe=rsvd */
7512 /* word5 iocb=rctl/type/df_ctl wqe=rctl/type/df_ctl memcpy */
7513 /* word6 iocb=ctxt_tag/io_tag wqe=ctxt_tag/xri */
James Smartf0d9bcc2010-10-22 11:07:09 -04007514 bf_set(wqe_ct, &wqe->xmit_bcast64.wqe_com,
James Smart4f774512009-05-22 14:52:35 -04007515 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
James Smartf0d9bcc2010-10-22 11:07:09 -04007516 bf_set(wqe_dbde, &wqe->xmit_bcast64.wqe_com, 1);
7517 bf_set(wqe_iod, &wqe->xmit_bcast64.wqe_com, LPFC_WQE_IOD_WRITE);
7518 bf_set(wqe_lenloc, &wqe->xmit_bcast64.wqe_com,
7519 LPFC_WQE_LENLOC_WORD3);
7520 bf_set(wqe_ebde_cnt, &wqe->xmit_bcast64.wqe_com, 0);
James Smart7851fe22011-07-22 18:36:52 -04007521 break;
James Smart4f774512009-05-22 14:52:35 -04007522 case CMD_FCP_IWRITE64_CR:
7523 command_type = FCP_COMMAND_DATA_OUT;
James Smartf0d9bcc2010-10-22 11:07:09 -04007524 /* word3 iocb=iotag wqe=payload_offset_len */
7525 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
7526 wqe->fcp_iwrite.payload_offset_len =
James Smart5ffc2662009-11-18 15:39:44 -05007527 xmit_len + sizeof(struct fcp_rsp);
James Smartf0d9bcc2010-10-22 11:07:09 -04007528 /* word4 iocb=parameter wqe=total_xfer_length memcpy */
7529 /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
7530 bf_set(wqe_erp, &wqe->fcp_iwrite.wqe_com,
7531 iocbq->iocb.ulpFCP2Rcvy);
7532 bf_set(wqe_lnk, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpXS);
7533 /* Always open the exchange */
7534 bf_set(wqe_xc, &wqe->fcp_iwrite.wqe_com, 0);
7535 bf_set(wqe_dbde, &wqe->fcp_iwrite.wqe_com, 1);
7536 bf_set(wqe_iod, &wqe->fcp_iwrite.wqe_com, LPFC_WQE_IOD_WRITE);
7537 bf_set(wqe_lenloc, &wqe->fcp_iwrite.wqe_com,
7538 LPFC_WQE_LENLOC_WORD4);
7539 bf_set(wqe_ebde_cnt, &wqe->fcp_iwrite.wqe_com, 0);
7540 bf_set(wqe_pu, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpPU);
James Smart7851fe22011-07-22 18:36:52 -04007541 break;
James Smartf0d9bcc2010-10-22 11:07:09 -04007542 case CMD_FCP_IREAD64_CR:
7543 /* word3 iocb=iotag wqe=payload_offset_len */
7544 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
7545 wqe->fcp_iread.payload_offset_len =
7546 xmit_len + sizeof(struct fcp_rsp);
7547 /* word4 iocb=parameter wqe=total_xfer_length memcpy */
7548 /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
7549 bf_set(wqe_erp, &wqe->fcp_iread.wqe_com,
7550 iocbq->iocb.ulpFCP2Rcvy);
7551 bf_set(wqe_lnk, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpXS);
James Smart4f774512009-05-22 14:52:35 -04007552 /* Always open the exchange */
7553 bf_set(wqe_xc, &wqe->fcp_iread.wqe_com, 0);
James Smartf0d9bcc2010-10-22 11:07:09 -04007554 bf_set(wqe_dbde, &wqe->fcp_iread.wqe_com, 1);
7555 bf_set(wqe_iod, &wqe->fcp_iread.wqe_com, LPFC_WQE_IOD_READ);
7556 bf_set(wqe_lenloc, &wqe->fcp_iread.wqe_com,
7557 LPFC_WQE_LENLOC_WORD4);
7558 bf_set(wqe_ebde_cnt, &wqe->fcp_iread.wqe_com, 0);
7559 bf_set(wqe_pu, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpPU);
James Smart7851fe22011-07-22 18:36:52 -04007560 break;
James Smartf1126682009-06-10 17:22:44 -04007561 case CMD_FCP_ICMND64_CR:
James Smartf0d9bcc2010-10-22 11:07:09 -04007562 /* word3 iocb=IO_TAG wqe=reserved */
7563 wqe->fcp_icmd.rsrvd3 = 0;
7564 bf_set(wqe_pu, &wqe->fcp_icmd.wqe_com, 0);
James Smartf1126682009-06-10 17:22:44 -04007565 /* Always open the exchange */
James Smartf0d9bcc2010-10-22 11:07:09 -04007566 bf_set(wqe_xc, &wqe->fcp_icmd.wqe_com, 0);
7567 bf_set(wqe_dbde, &wqe->fcp_icmd.wqe_com, 1);
7568 bf_set(wqe_iod, &wqe->fcp_icmd.wqe_com, LPFC_WQE_IOD_WRITE);
7569 bf_set(wqe_qosd, &wqe->fcp_icmd.wqe_com, 1);
7570 bf_set(wqe_lenloc, &wqe->fcp_icmd.wqe_com,
7571 LPFC_WQE_LENLOC_NONE);
7572 bf_set(wqe_ebde_cnt, &wqe->fcp_icmd.wqe_com, 0);
James Smart7851fe22011-07-22 18:36:52 -04007573 break;
James Smart4f774512009-05-22 14:52:35 -04007574 case CMD_GEN_REQUEST64_CR:
James Smart63e801c2010-11-20 23:14:19 -05007575 /* For this command calculate the xmit length of the
7576 * request bde.
7577 */
7578 xmit_len = 0;
7579 numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
7580 sizeof(struct ulp_bde64);
7581 for (i = 0; i < numBdes; i++) {
James Smart63e801c2010-11-20 23:14:19 -05007582 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
James Smart546fc852011-03-11 16:06:29 -05007583 if (bde.tus.f.bdeFlags != BUFF_TYPE_BDE_64)
7584 break;
James Smart63e801c2010-11-20 23:14:19 -05007585 xmit_len += bde.tus.f.bdeSize;
7586 }
James Smartf0d9bcc2010-10-22 11:07:09 -04007587 /* word3 iocb=IO_TAG wqe=request_payload_len */
7588 wqe->gen_req.request_payload_len = xmit_len;
7589 /* word4 iocb=parameter wqe=relative_offset memcpy */
7590 /* word5 [rctl, type, df_ctl, la] copied in memcpy */
James Smart4f774512009-05-22 14:52:35 -04007591 /* word6 context tag copied in memcpy */
7592 if (iocbq->iocb.ulpCt_h || iocbq->iocb.ulpCt_l) {
7593 ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
7594 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
7595 "2015 Invalid CT %x command 0x%x\n",
7596 ct, iocbq->iocb.ulpCommand);
7597 return IOCB_ERROR;
7598 }
James Smartf0d9bcc2010-10-22 11:07:09 -04007599 bf_set(wqe_ct, &wqe->gen_req.wqe_com, 0);
7600 bf_set(wqe_tmo, &wqe->gen_req.wqe_com, iocbq->iocb.ulpTimeout);
7601 bf_set(wqe_pu, &wqe->gen_req.wqe_com, iocbq->iocb.ulpPU);
7602 bf_set(wqe_dbde, &wqe->gen_req.wqe_com, 1);
7603 bf_set(wqe_iod, &wqe->gen_req.wqe_com, LPFC_WQE_IOD_READ);
7604 bf_set(wqe_qosd, &wqe->gen_req.wqe_com, 1);
7605 bf_set(wqe_lenloc, &wqe->gen_req.wqe_com, LPFC_WQE_LENLOC_NONE);
7606 bf_set(wqe_ebde_cnt, &wqe->gen_req.wqe_com, 0);
James Smart4f774512009-05-22 14:52:35 -04007607 command_type = OTHER_COMMAND;
James Smart7851fe22011-07-22 18:36:52 -04007608 break;
James Smart4f774512009-05-22 14:52:35 -04007609 case CMD_XMIT_ELS_RSP64_CX:
James Smartc31098c2011-04-16 11:03:33 -04007610 ndlp = (struct lpfc_nodelist *)iocbq->context1;
James Smart4f774512009-05-22 14:52:35 -04007611 /* words0-2 BDE memcpy */
James Smartf0d9bcc2010-10-22 11:07:09 -04007612 /* word3 iocb=iotag32 wqe=response_payload_len */
7613 wqe->xmit_els_rsp.response_payload_len = xmit_len;
James Smart4f774512009-05-22 14:52:35 -04007614 /* word4 iocb=did wge=rsvd. */
James Smartf0d9bcc2010-10-22 11:07:09 -04007615 wqe->xmit_els_rsp.rsvd4 = 0;
James Smart4f774512009-05-22 14:52:35 -04007616 /* word5 iocb=rsvd wge=did */
7617 bf_set(wqe_els_did, &wqe->xmit_els_rsp.wqe_dest,
7618 iocbq->iocb.un.elsreq64.remoteID);
James Smartf0d9bcc2010-10-22 11:07:09 -04007619 bf_set(wqe_ct, &wqe->xmit_els_rsp.wqe_com,
7620 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
7621 bf_set(wqe_pu, &wqe->xmit_els_rsp.wqe_com, iocbq->iocb.ulpPU);
7622 bf_set(wqe_rcvoxid, &wqe->xmit_els_rsp.wqe_com,
James Smart7851fe22011-07-22 18:36:52 -04007623 iocbq->iocb.unsli3.rcvsli3.ox_id);
James Smart4f774512009-05-22 14:52:35 -04007624 if (!iocbq->iocb.ulpCt_h && iocbq->iocb.ulpCt_l)
James Smartf0d9bcc2010-10-22 11:07:09 -04007625 bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
James Smart6d368e52011-05-24 11:44:12 -04007626 phba->vpi_ids[iocbq->vport->vpi]);
James Smartf0d9bcc2010-10-22 11:07:09 -04007627 bf_set(wqe_dbde, &wqe->xmit_els_rsp.wqe_com, 1);
7628 bf_set(wqe_iod, &wqe->xmit_els_rsp.wqe_com, LPFC_WQE_IOD_WRITE);
7629 bf_set(wqe_qosd, &wqe->xmit_els_rsp.wqe_com, 1);
7630 bf_set(wqe_lenloc, &wqe->xmit_els_rsp.wqe_com,
7631 LPFC_WQE_LENLOC_WORD3);
7632 bf_set(wqe_ebde_cnt, &wqe->xmit_els_rsp.wqe_com, 0);
James Smart6d368e52011-05-24 11:44:12 -04007633 bf_set(wqe_rsp_temp_rpi, &wqe->xmit_els_rsp,
7634 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
James Smart4f774512009-05-22 14:52:35 -04007635 command_type = OTHER_COMMAND;
James Smart7851fe22011-07-22 18:36:52 -04007636 break;
James Smart4f774512009-05-22 14:52:35 -04007637 case CMD_CLOSE_XRI_CN:
7638 case CMD_ABORT_XRI_CN:
7639 case CMD_ABORT_XRI_CX:
7640 /* words 0-2 memcpy should be 0 rserved */
7641 /* port will send abts */
James Smartdcf2a4e2010-09-29 11:18:53 -04007642 abrt_iotag = iocbq->iocb.un.acxri.abortContextTag;
7643 if (abrt_iotag != 0 && abrt_iotag <= phba->sli.last_iotag) {
7644 abrtiocbq = phba->sli.iocbq_lookup[abrt_iotag];
7645 fip = abrtiocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK;
7646 } else
7647 fip = 0;
7648
7649 if ((iocbq->iocb.ulpCommand == CMD_CLOSE_XRI_CN) || fip)
James Smart4f774512009-05-22 14:52:35 -04007650 /*
James Smartdcf2a4e2010-09-29 11:18:53 -04007651 * The link is down, or the command was ELS_FIP
7652 * so the fw does not need to send abts
James Smart4f774512009-05-22 14:52:35 -04007653 * on the wire.
7654 */
7655 bf_set(abort_cmd_ia, &wqe->abort_cmd, 1);
7656 else
7657 bf_set(abort_cmd_ia, &wqe->abort_cmd, 0);
7658 bf_set(abort_cmd_criteria, &wqe->abort_cmd, T_XRI_TAG);
James Smartf0d9bcc2010-10-22 11:07:09 -04007659 /* word5 iocb=CONTEXT_TAG|IO_TAG wqe=reserved */
7660 wqe->abort_cmd.rsrvd5 = 0;
7661 bf_set(wqe_ct, &wqe->abort_cmd.wqe_com,
James Smart4f774512009-05-22 14:52:35 -04007662 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
7663 abort_tag = iocbq->iocb.un.acxri.abortIoTag;
James Smart4f774512009-05-22 14:52:35 -04007664 /*
7665 * The abort handler will send us CMD_ABORT_XRI_CN or
7666 * CMD_CLOSE_XRI_CN and the fw only accepts CMD_ABORT_XRI_CX
7667 */
James Smartf0d9bcc2010-10-22 11:07:09 -04007668 bf_set(wqe_cmnd, &wqe->abort_cmd.wqe_com, CMD_ABORT_XRI_CX);
7669 bf_set(wqe_qosd, &wqe->abort_cmd.wqe_com, 1);
7670 bf_set(wqe_lenloc, &wqe->abort_cmd.wqe_com,
7671 LPFC_WQE_LENLOC_NONE);
James Smart4f774512009-05-22 14:52:35 -04007672 cmnd = CMD_ABORT_XRI_CX;
7673 command_type = OTHER_COMMAND;
7674 xritag = 0;
James Smart7851fe22011-07-22 18:36:52 -04007675 break;
James Smart6669f9b2009-10-02 15:16:45 -04007676 case CMD_XMIT_BLS_RSP64_CX:
James Smart546fc852011-03-11 16:06:29 -05007677 /* As BLS ABTS RSP WQE is very different from other WQEs,
James Smart6669f9b2009-10-02 15:16:45 -04007678 * we re-construct this WQE here based on information in
7679 * iocbq from scratch.
7680 */
7681 memset(wqe, 0, sizeof(union lpfc_wqe));
James Smart5ffc2662009-11-18 15:39:44 -05007682 /* OX_ID is invariable to who sent ABTS to CT exchange */
James Smart6669f9b2009-10-02 15:16:45 -04007683 bf_set(xmit_bls_rsp64_oxid, &wqe->xmit_bls_rsp,
James Smart546fc852011-03-11 16:06:29 -05007684 bf_get(lpfc_abts_oxid, &iocbq->iocb.un.bls_rsp));
7685 if (bf_get(lpfc_abts_orig, &iocbq->iocb.un.bls_rsp) ==
James Smart5ffc2662009-11-18 15:39:44 -05007686 LPFC_ABTS_UNSOL_INT) {
7687 /* ABTS sent by initiator to CT exchange, the
7688 * RX_ID field will be filled with the newly
7689 * allocated responder XRI.
7690 */
7691 bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
7692 iocbq->sli4_xritag);
7693 } else {
7694 /* ABTS sent by responder to CT exchange, the
7695 * RX_ID field will be filled with the responder
7696 * RX_ID from ABTS.
7697 */
7698 bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
James Smart546fc852011-03-11 16:06:29 -05007699 bf_get(lpfc_abts_rxid, &iocbq->iocb.un.bls_rsp));
James Smart5ffc2662009-11-18 15:39:44 -05007700 }
James Smart6669f9b2009-10-02 15:16:45 -04007701 bf_set(xmit_bls_rsp64_seqcnthi, &wqe->xmit_bls_rsp, 0xffff);
7702 bf_set(wqe_xmit_bls_pt, &wqe->xmit_bls_rsp.wqe_dest, 0x1);
7703 bf_set(wqe_ctxt_tag, &wqe->xmit_bls_rsp.wqe_com,
7704 iocbq->iocb.ulpContext);
James Smartf0d9bcc2010-10-22 11:07:09 -04007705 bf_set(wqe_qosd, &wqe->xmit_bls_rsp.wqe_com, 1);
7706 bf_set(wqe_lenloc, &wqe->xmit_bls_rsp.wqe_com,
7707 LPFC_WQE_LENLOC_NONE);
James Smart6669f9b2009-10-02 15:16:45 -04007708 /* Overwrite the pre-set comnd type with OTHER_COMMAND */
7709 command_type = OTHER_COMMAND;
James Smart546fc852011-03-11 16:06:29 -05007710 if (iocbq->iocb.un.xseq64.w5.hcsw.Rctl == FC_RCTL_BA_RJT) {
7711 bf_set(xmit_bls_rsp64_rjt_vspec, &wqe->xmit_bls_rsp,
7712 bf_get(lpfc_vndr_code, &iocbq->iocb.un.bls_rsp));
7713 bf_set(xmit_bls_rsp64_rjt_expc, &wqe->xmit_bls_rsp,
7714 bf_get(lpfc_rsn_expln, &iocbq->iocb.un.bls_rsp));
7715 bf_set(xmit_bls_rsp64_rjt_rsnc, &wqe->xmit_bls_rsp,
7716 bf_get(lpfc_rsn_code, &iocbq->iocb.un.bls_rsp));
7717 }
7718
James Smart7851fe22011-07-22 18:36:52 -04007719 break;
James Smart4f774512009-05-22 14:52:35 -04007720 case CMD_XRI_ABORTED_CX:
7721 case CMD_CREATE_XRI_CR: /* Do we expect to use this? */
James Smart4f774512009-05-22 14:52:35 -04007722 case CMD_IOCB_FCP_IBIDIR64_CR: /* bidirectional xfer */
7723 case CMD_FCP_TSEND64_CX: /* Target mode send xfer-ready */
7724 case CMD_FCP_TRSP64_CX: /* Target mode rcv */
7725 case CMD_FCP_AUTO_TRSP_CX: /* Auto target rsp */
7726 default:
7727 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
7728 "2014 Invalid command 0x%x\n",
7729 iocbq->iocb.ulpCommand);
7730 return IOCB_ERROR;
James Smart7851fe22011-07-22 18:36:52 -04007731 break;
James Smart4f774512009-05-22 14:52:35 -04007732 }
James Smart6d368e52011-05-24 11:44:12 -04007733
James Smartf0d9bcc2010-10-22 11:07:09 -04007734 bf_set(wqe_xri_tag, &wqe->generic.wqe_com, xritag);
7735 bf_set(wqe_reqtag, &wqe->generic.wqe_com, iocbq->iotag);
7736 wqe->generic.wqe_com.abort_tag = abort_tag;
7737 bf_set(wqe_cmd_type, &wqe->generic.wqe_com, command_type);
7738 bf_set(wqe_cmnd, &wqe->generic.wqe_com, cmnd);
7739 bf_set(wqe_class, &wqe->generic.wqe_com, iocbq->iocb.ulpClass);
7740 bf_set(wqe_cqid, &wqe->generic.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
James Smart4f774512009-05-22 14:52:35 -04007741 return 0;
7742}
7743
7744/**
7745 * __lpfc_sli_issue_iocb_s4 - SLI4 device lockless ver of lpfc_sli_issue_iocb
7746 * @phba: Pointer to HBA context object.
7747 * @ring_number: SLI ring number to issue iocb on.
7748 * @piocb: Pointer to command iocb.
7749 * @flag: Flag indicating if this command can be put into txq.
7750 *
7751 * __lpfc_sli_issue_iocb_s4 is used by other functions in the driver to issue
7752 * an iocb command to an HBA with SLI-4 interface spec.
7753 *
7754 * This function is called with hbalock held. The function will return success
7755 * after it successfully submit the iocb to firmware or after adding to the
7756 * txq.
7757 **/
7758static int
7759__lpfc_sli_issue_iocb_s4(struct lpfc_hba *phba, uint32_t ring_number,
7760 struct lpfc_iocbq *piocb, uint32_t flag)
7761{
7762 struct lpfc_sglq *sglq;
James Smart4f774512009-05-22 14:52:35 -04007763 union lpfc_wqe wqe;
7764 struct lpfc_sli_ring *pring = &phba->sli.ring[ring_number];
James Smart4f774512009-05-22 14:52:35 -04007765
7766 if (piocb->sli4_xritag == NO_XRI) {
7767 if (piocb->iocb.ulpCommand == CMD_ABORT_XRI_CN ||
James Smart546fc852011-03-11 16:06:29 -05007768 piocb->iocb.ulpCommand == CMD_CLOSE_XRI_CN ||
7769 piocb->iocb.ulpCommand == CMD_XMIT_BLS_RSP64_CX)
James Smart4f774512009-05-22 14:52:35 -04007770 sglq = NULL;
7771 else {
James Smart2a9bf3d2010-06-07 15:24:45 -04007772 if (pring->txq_cnt) {
7773 if (!(flag & SLI_IOCB_RET_IOCB)) {
7774 __lpfc_sli_ringtx_put(phba,
7775 pring, piocb);
7776 return IOCB_SUCCESS;
7777 } else {
7778 return IOCB_BUSY;
7779 }
7780 } else {
James Smart6d368e52011-05-24 11:44:12 -04007781 sglq = __lpfc_sli_get_sglq(phba, piocb);
James Smart2a9bf3d2010-06-07 15:24:45 -04007782 if (!sglq) {
7783 if (!(flag & SLI_IOCB_RET_IOCB)) {
7784 __lpfc_sli_ringtx_put(phba,
7785 pring,
7786 piocb);
7787 return IOCB_SUCCESS;
7788 } else
7789 return IOCB_BUSY;
7790 }
7791 }
James Smart4f774512009-05-22 14:52:35 -04007792 }
7793 } else if (piocb->iocb_flag & LPFC_IO_FCP) {
James Smart6d368e52011-05-24 11:44:12 -04007794 /* These IO's already have an XRI and a mapped sgl. */
7795 sglq = NULL;
James Smart4f774512009-05-22 14:52:35 -04007796 } else {
James Smart6d368e52011-05-24 11:44:12 -04007797 /*
7798 * This is a continuation of a commandi,(CX) so this
James Smart4f774512009-05-22 14:52:35 -04007799 * sglq is on the active list
7800 */
7801 sglq = __lpfc_get_active_sglq(phba, piocb->sli4_xritag);
7802 if (!sglq)
7803 return IOCB_ERROR;
7804 }
7805
7806 if (sglq) {
James Smart6d368e52011-05-24 11:44:12 -04007807 piocb->sli4_lxritag = sglq->sli4_lxritag;
James Smart2a9bf3d2010-06-07 15:24:45 -04007808 piocb->sli4_xritag = sglq->sli4_xritag;
James Smart2a9bf3d2010-06-07 15:24:45 -04007809 if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocb, sglq))
James Smart4f774512009-05-22 14:52:35 -04007810 return IOCB_ERROR;
7811 }
7812
7813 if (lpfc_sli4_iocb2wqe(phba, piocb, &wqe))
7814 return IOCB_ERROR;
7815
James Smart341af102010-01-26 23:07:37 -05007816 if ((piocb->iocb_flag & LPFC_IO_FCP) ||
7817 (piocb->iocb_flag & LPFC_USE_FCPWQIDX)) {
James Smart5ffc2662009-11-18 15:39:44 -05007818 /*
7819 * For FCP command IOCB, get a new WQ index to distribute
7820 * WQE across the WQsr. On the other hand, for abort IOCB,
7821 * it carries the same WQ index to the original command
7822 * IOCB.
7823 */
James Smart341af102010-01-26 23:07:37 -05007824 if (piocb->iocb_flag & LPFC_IO_FCP)
James Smart5ffc2662009-11-18 15:39:44 -05007825 piocb->fcp_wqidx = lpfc_sli4_scmd_to_wqidx_distr(phba);
7826 if (lpfc_sli4_wq_put(phba->sli4_hba.fcp_wq[piocb->fcp_wqidx],
7827 &wqe))
James Smart4f774512009-05-22 14:52:35 -04007828 return IOCB_ERROR;
7829 } else {
7830 if (lpfc_sli4_wq_put(phba->sli4_hba.els_wq, &wqe))
7831 return IOCB_ERROR;
7832 }
7833 lpfc_sli_ringtxcmpl_put(phba, pring, piocb);
7834
7835 return 0;
7836}
7837
7838/**
James Smart3772a992009-05-22 14:50:54 -04007839 * __lpfc_sli_issue_iocb - Wrapper func of lockless version for issuing iocb
7840 *
7841 * This routine wraps the actual lockless version for issusing IOCB function
7842 * pointer from the lpfc_hba struct.
7843 *
7844 * Return codes:
7845 * IOCB_ERROR - Error
7846 * IOCB_SUCCESS - Success
7847 * IOCB_BUSY - Busy
7848 **/
James Smart2a9bf3d2010-06-07 15:24:45 -04007849int
James Smart3772a992009-05-22 14:50:54 -04007850__lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
7851 struct lpfc_iocbq *piocb, uint32_t flag)
7852{
7853 return phba->__lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
7854}
7855
7856/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03007857 * lpfc_sli_api_table_setup - Set up sli api function jump table
James Smart3772a992009-05-22 14:50:54 -04007858 * @phba: The hba struct for which this call is being executed.
7859 * @dev_grp: The HBA PCI-Device group number.
7860 *
7861 * This routine sets up the SLI interface API function jump table in @phba
7862 * struct.
7863 * Returns: 0 - success, -ENODEV - failure.
7864 **/
7865int
7866lpfc_sli_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
7867{
7868
7869 switch (dev_grp) {
7870 case LPFC_PCI_DEV_LP:
7871 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s3;
7872 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s3;
7873 break;
James Smart4f774512009-05-22 14:52:35 -04007874 case LPFC_PCI_DEV_OC:
7875 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s4;
7876 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s4;
7877 break;
James Smart3772a992009-05-22 14:50:54 -04007878 default:
7879 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7880 "1419 Invalid HBA PCI-device group: 0x%x\n",
7881 dev_grp);
7882 return -ENODEV;
7883 break;
7884 }
7885 phba->lpfc_get_iocb_from_iocbq = lpfc_get_iocb_from_iocbq;
7886 return 0;
7887}
James Smart92d7f7b2007-06-17 19:56:38 -05007888
James Smarte59058c2008-08-24 21:49:00 -04007889/**
James Smart3621a712009-04-06 18:47:14 -04007890 * lpfc_sli_issue_iocb - Wrapper function for __lpfc_sli_issue_iocb
James Smarte59058c2008-08-24 21:49:00 -04007891 * @phba: Pointer to HBA context object.
7892 * @pring: Pointer to driver SLI ring object.
7893 * @piocb: Pointer to command iocb.
7894 * @flag: Flag indicating if this command can be put into txq.
7895 *
7896 * lpfc_sli_issue_iocb is a wrapper around __lpfc_sli_issue_iocb
7897 * function. This function gets the hbalock and calls
7898 * __lpfc_sli_issue_iocb function and will return the error returned
7899 * by __lpfc_sli_issue_iocb function. This wrapper is used by
7900 * functions which do not hold hbalock.
7901 **/
James Smart92d7f7b2007-06-17 19:56:38 -05007902int
James Smart3772a992009-05-22 14:50:54 -04007903lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
James Smart92d7f7b2007-06-17 19:56:38 -05007904 struct lpfc_iocbq *piocb, uint32_t flag)
7905{
7906 unsigned long iflags;
7907 int rc;
7908
7909 spin_lock_irqsave(&phba->hbalock, iflags);
James Smart3772a992009-05-22 14:50:54 -04007910 rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
James Smart92d7f7b2007-06-17 19:56:38 -05007911 spin_unlock_irqrestore(&phba->hbalock, iflags);
7912
7913 return rc;
7914}
7915
James Smarte59058c2008-08-24 21:49:00 -04007916/**
James Smart3621a712009-04-06 18:47:14 -04007917 * lpfc_extra_ring_setup - Extra ring setup function
James Smarte59058c2008-08-24 21:49:00 -04007918 * @phba: Pointer to HBA context object.
7919 *
7920 * This function is called while driver attaches with the
7921 * HBA to setup the extra ring. The extra ring is used
7922 * only when driver needs to support target mode functionality
7923 * or IP over FC functionalities.
7924 *
7925 * This function is called with no lock held.
7926 **/
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05007927static int
7928lpfc_extra_ring_setup( struct lpfc_hba *phba)
7929{
7930 struct lpfc_sli *psli;
7931 struct lpfc_sli_ring *pring;
7932
7933 psli = &phba->sli;
7934
7935 /* Adjust cmd/rsp ring iocb entries more evenly */
James Smarta4bc3372006-12-02 13:34:16 -05007936
7937 /* Take some away from the FCP ring */
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05007938 pring = &psli->ring[psli->fcp_ring];
7939 pring->numCiocb -= SLI2_IOCB_CMD_R1XTRA_ENTRIES;
7940 pring->numRiocb -= SLI2_IOCB_RSP_R1XTRA_ENTRIES;
7941 pring->numCiocb -= SLI2_IOCB_CMD_R3XTRA_ENTRIES;
7942 pring->numRiocb -= SLI2_IOCB_RSP_R3XTRA_ENTRIES;
7943
James Smarta4bc3372006-12-02 13:34:16 -05007944 /* and give them to the extra ring */
7945 pring = &psli->ring[psli->extra_ring];
7946
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05007947 pring->numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
7948 pring->numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
7949 pring->numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
7950 pring->numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
7951
7952 /* Setup default profile for this ring */
7953 pring->iotag_max = 4096;
7954 pring->num_mask = 1;
7955 pring->prt[0].profile = 0; /* Mask 0 */
James Smarta4bc3372006-12-02 13:34:16 -05007956 pring->prt[0].rctl = phba->cfg_multi_ring_rctl;
7957 pring->prt[0].type = phba->cfg_multi_ring_type;
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05007958 pring->prt[0].lpfc_sli_rcv_unsol_event = NULL;
7959 return 0;
7960}
7961
James Smarte59058c2008-08-24 21:49:00 -04007962/**
James Smart3621a712009-04-06 18:47:14 -04007963 * lpfc_sli_async_event_handler - ASYNC iocb handler function
James Smarte59058c2008-08-24 21:49:00 -04007964 * @phba: Pointer to HBA context object.
7965 * @pring: Pointer to driver SLI ring object.
7966 * @iocbq: Pointer to iocb object.
7967 *
7968 * This function is called by the slow ring event handler
7969 * function when there is an ASYNC event iocb in the ring.
7970 * This function is called with no lock held.
7971 * Currently this function handles only temperature related
7972 * ASYNC events. The function decodes the temperature sensor
7973 * event message and posts events for the management applications.
7974 **/
James Smart98c9ea52007-10-27 13:37:33 -04007975static void
James Smart57127f12007-10-27 13:37:05 -04007976lpfc_sli_async_event_handler(struct lpfc_hba * phba,
7977 struct lpfc_sli_ring * pring, struct lpfc_iocbq * iocbq)
7978{
7979 IOCB_t *icmd;
7980 uint16_t evt_code;
7981 uint16_t temp;
7982 struct temp_event temp_event_data;
7983 struct Scsi_Host *shost;
James Smarta257bf92009-04-06 18:48:10 -04007984 uint32_t *iocb_w;
James Smart57127f12007-10-27 13:37:05 -04007985
7986 icmd = &iocbq->iocb;
7987 evt_code = icmd->un.asyncstat.evt_code;
7988 temp = icmd->ulpContext;
7989
7990 if ((evt_code != ASYNC_TEMP_WARN) &&
7991 (evt_code != ASYNC_TEMP_SAFE)) {
James Smarta257bf92009-04-06 18:48:10 -04007992 iocb_w = (uint32_t *) icmd;
James Smart57127f12007-10-27 13:37:05 -04007993 lpfc_printf_log(phba,
7994 KERN_ERR,
7995 LOG_SLI,
James Smart76bb24e2007-10-27 13:38:00 -04007996 "0346 Ring %d handler: unexpected ASYNC_STATUS"
James Smarte4e74272009-07-19 10:01:38 -04007997 " evt_code 0x%x\n"
James Smarta257bf92009-04-06 18:48:10 -04007998 "W0 0x%08x W1 0x%08x W2 0x%08x W3 0x%08x\n"
7999 "W4 0x%08x W5 0x%08x W6 0x%08x W7 0x%08x\n"
8000 "W8 0x%08x W9 0x%08x W10 0x%08x W11 0x%08x\n"
8001 "W12 0x%08x W13 0x%08x W14 0x%08x W15 0x%08x\n",
James Smart57127f12007-10-27 13:37:05 -04008002 pring->ringno,
James Smarta257bf92009-04-06 18:48:10 -04008003 icmd->un.asyncstat.evt_code,
8004 iocb_w[0], iocb_w[1], iocb_w[2], iocb_w[3],
8005 iocb_w[4], iocb_w[5], iocb_w[6], iocb_w[7],
8006 iocb_w[8], iocb_w[9], iocb_w[10], iocb_w[11],
8007 iocb_w[12], iocb_w[13], iocb_w[14], iocb_w[15]);
8008
James Smart57127f12007-10-27 13:37:05 -04008009 return;
8010 }
8011 temp_event_data.data = (uint32_t)temp;
8012 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
8013 if (evt_code == ASYNC_TEMP_WARN) {
8014 temp_event_data.event_code = LPFC_THRESHOLD_TEMP;
8015 lpfc_printf_log(phba,
James Smart09372822008-01-11 01:52:54 -05008016 KERN_ERR,
James Smart57127f12007-10-27 13:37:05 -04008017 LOG_TEMP,
James Smart76bb24e2007-10-27 13:38:00 -04008018 "0347 Adapter is very hot, please take "
James Smart57127f12007-10-27 13:37:05 -04008019 "corrective action. temperature : %d Celsius\n",
8020 temp);
8021 }
8022 if (evt_code == ASYNC_TEMP_SAFE) {
8023 temp_event_data.event_code = LPFC_NORMAL_TEMP;
8024 lpfc_printf_log(phba,
James Smart09372822008-01-11 01:52:54 -05008025 KERN_ERR,
James Smart57127f12007-10-27 13:37:05 -04008026 LOG_TEMP,
8027 "0340 Adapter temperature is OK now. "
8028 "temperature : %d Celsius\n",
8029 temp);
8030 }
8031
8032 /* Send temperature change event to applications */
8033 shost = lpfc_shost_from_vport(phba->pport);
8034 fc_host_post_vendor_event(shost, fc_get_event_number(),
8035 sizeof(temp_event_data), (char *) &temp_event_data,
James Smartddcc50f2008-12-04 22:38:46 -05008036 LPFC_NL_VENDOR_ID);
James Smart57127f12007-10-27 13:37:05 -04008037
8038}
8039
8040
James Smarte59058c2008-08-24 21:49:00 -04008041/**
James Smart3621a712009-04-06 18:47:14 -04008042 * lpfc_sli_setup - SLI ring setup function
James Smarte59058c2008-08-24 21:49:00 -04008043 * @phba: Pointer to HBA context object.
8044 *
8045 * lpfc_sli_setup sets up rings of the SLI interface with
8046 * number of iocbs per ring and iotags. This function is
8047 * called while driver attach to the HBA and before the
8048 * interrupts are enabled. So there is no need for locking.
8049 *
8050 * This function always returns 0.
8051 **/
dea31012005-04-17 16:05:31 -05008052int
8053lpfc_sli_setup(struct lpfc_hba *phba)
8054{
James Smarted957682007-06-17 19:56:37 -05008055 int i, totiocbsize = 0;
dea31012005-04-17 16:05:31 -05008056 struct lpfc_sli *psli = &phba->sli;
8057 struct lpfc_sli_ring *pring;
8058
8059 psli->num_rings = MAX_CONFIGURED_RINGS;
8060 psli->sli_flag = 0;
8061 psli->fcp_ring = LPFC_FCP_RING;
8062 psli->next_ring = LPFC_FCP_NEXT_RING;
James Smarta4bc3372006-12-02 13:34:16 -05008063 psli->extra_ring = LPFC_EXTRA_RING;
dea31012005-04-17 16:05:31 -05008064
James Bottomley604a3e32005-10-29 10:28:33 -05008065 psli->iocbq_lookup = NULL;
8066 psli->iocbq_lookup_len = 0;
8067 psli->last_iotag = 0;
8068
dea31012005-04-17 16:05:31 -05008069 for (i = 0; i < psli->num_rings; i++) {
8070 pring = &psli->ring[i];
8071 switch (i) {
8072 case LPFC_FCP_RING: /* ring 0 - FCP */
8073 /* numCiocb and numRiocb are used in config_port */
8074 pring->numCiocb = SLI2_IOCB_CMD_R0_ENTRIES;
8075 pring->numRiocb = SLI2_IOCB_RSP_R0_ENTRIES;
8076 pring->numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
8077 pring->numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
8078 pring->numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
8079 pring->numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
James Smarted957682007-06-17 19:56:37 -05008080 pring->sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05008081 SLI3_IOCB_CMD_SIZE :
8082 SLI2_IOCB_CMD_SIZE;
James Smarted957682007-06-17 19:56:37 -05008083 pring->sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05008084 SLI3_IOCB_RSP_SIZE :
8085 SLI2_IOCB_RSP_SIZE;
dea31012005-04-17 16:05:31 -05008086 pring->iotag_ctr = 0;
8087 pring->iotag_max =
James Smart92d7f7b2007-06-17 19:56:38 -05008088 (phba->cfg_hba_queue_depth * 2);
dea31012005-04-17 16:05:31 -05008089 pring->fast_iotag = pring->iotag_max;
8090 pring->num_mask = 0;
8091 break;
James Smarta4bc3372006-12-02 13:34:16 -05008092 case LPFC_EXTRA_RING: /* ring 1 - EXTRA */
dea31012005-04-17 16:05:31 -05008093 /* numCiocb and numRiocb are used in config_port */
8094 pring->numCiocb = SLI2_IOCB_CMD_R1_ENTRIES;
8095 pring->numRiocb = SLI2_IOCB_RSP_R1_ENTRIES;
James Smarted957682007-06-17 19:56:37 -05008096 pring->sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05008097 SLI3_IOCB_CMD_SIZE :
8098 SLI2_IOCB_CMD_SIZE;
James Smarted957682007-06-17 19:56:37 -05008099 pring->sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05008100 SLI3_IOCB_RSP_SIZE :
8101 SLI2_IOCB_RSP_SIZE;
James Smart2e0fef82007-06-17 19:56:36 -05008102 pring->iotag_max = phba->cfg_hba_queue_depth;
dea31012005-04-17 16:05:31 -05008103 pring->num_mask = 0;
8104 break;
8105 case LPFC_ELS_RING: /* ring 2 - ELS / CT */
8106 /* numCiocb and numRiocb are used in config_port */
8107 pring->numCiocb = SLI2_IOCB_CMD_R2_ENTRIES;
8108 pring->numRiocb = SLI2_IOCB_RSP_R2_ENTRIES;
James Smarted957682007-06-17 19:56:37 -05008109 pring->sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05008110 SLI3_IOCB_CMD_SIZE :
8111 SLI2_IOCB_CMD_SIZE;
James Smarted957682007-06-17 19:56:37 -05008112 pring->sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05008113 SLI3_IOCB_RSP_SIZE :
8114 SLI2_IOCB_RSP_SIZE;
dea31012005-04-17 16:05:31 -05008115 pring->fast_iotag = 0;
8116 pring->iotag_ctr = 0;
8117 pring->iotag_max = 4096;
James Smart57127f12007-10-27 13:37:05 -04008118 pring->lpfc_sli_rcv_async_status =
8119 lpfc_sli_async_event_handler;
James Smart6669f9b2009-10-02 15:16:45 -04008120 pring->num_mask = LPFC_MAX_RING_MASK;
dea31012005-04-17 16:05:31 -05008121 pring->prt[0].profile = 0; /* Mask 0 */
James Smart6a9c52c2009-10-02 15:16:51 -04008122 pring->prt[0].rctl = FC_RCTL_ELS_REQ;
8123 pring->prt[0].type = FC_TYPE_ELS;
dea31012005-04-17 16:05:31 -05008124 pring->prt[0].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05008125 lpfc_els_unsol_event;
dea31012005-04-17 16:05:31 -05008126 pring->prt[1].profile = 0; /* Mask 1 */
James Smart6a9c52c2009-10-02 15:16:51 -04008127 pring->prt[1].rctl = FC_RCTL_ELS_REP;
8128 pring->prt[1].type = FC_TYPE_ELS;
dea31012005-04-17 16:05:31 -05008129 pring->prt[1].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05008130 lpfc_els_unsol_event;
dea31012005-04-17 16:05:31 -05008131 pring->prt[2].profile = 0; /* Mask 2 */
8132 /* NameServer Inquiry */
James Smart6a9c52c2009-10-02 15:16:51 -04008133 pring->prt[2].rctl = FC_RCTL_DD_UNSOL_CTL;
dea31012005-04-17 16:05:31 -05008134 /* NameServer */
James Smart6a9c52c2009-10-02 15:16:51 -04008135 pring->prt[2].type = FC_TYPE_CT;
dea31012005-04-17 16:05:31 -05008136 pring->prt[2].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05008137 lpfc_ct_unsol_event;
dea31012005-04-17 16:05:31 -05008138 pring->prt[3].profile = 0; /* Mask 3 */
8139 /* NameServer response */
James Smart6a9c52c2009-10-02 15:16:51 -04008140 pring->prt[3].rctl = FC_RCTL_DD_SOL_CTL;
dea31012005-04-17 16:05:31 -05008141 /* NameServer */
James Smart6a9c52c2009-10-02 15:16:51 -04008142 pring->prt[3].type = FC_TYPE_CT;
dea31012005-04-17 16:05:31 -05008143 pring->prt[3].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05008144 lpfc_ct_unsol_event;
James Smart6669f9b2009-10-02 15:16:45 -04008145 /* abort unsolicited sequence */
8146 pring->prt[4].profile = 0; /* Mask 4 */
8147 pring->prt[4].rctl = FC_RCTL_BA_ABTS;
8148 pring->prt[4].type = FC_TYPE_BLS;
8149 pring->prt[4].lpfc_sli_rcv_unsol_event =
8150 lpfc_sli4_ct_abort_unsol_event;
dea31012005-04-17 16:05:31 -05008151 break;
8152 }
James Smarted957682007-06-17 19:56:37 -05008153 totiocbsize += (pring->numCiocb * pring->sizeCiocb) +
James Smart92d7f7b2007-06-17 19:56:38 -05008154 (pring->numRiocb * pring->sizeRiocb);
dea31012005-04-17 16:05:31 -05008155 }
James Smarted957682007-06-17 19:56:37 -05008156 if (totiocbsize > MAX_SLIM_IOCB_SIZE) {
dea31012005-04-17 16:05:31 -05008157 /* Too many cmd / rsp ring entries in SLI2 SLIM */
James Smarte8b62012007-08-02 11:10:09 -04008158 printk(KERN_ERR "%d:0462 Too many cmd / rsp ring entries in "
8159 "SLI2 SLIM Data: x%x x%lx\n",
8160 phba->brd_no, totiocbsize,
8161 (unsigned long) MAX_SLIM_IOCB_SIZE);
dea31012005-04-17 16:05:31 -05008162 }
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05008163 if (phba->cfg_multi_ring_support == 2)
8164 lpfc_extra_ring_setup(phba);
dea31012005-04-17 16:05:31 -05008165
8166 return 0;
8167}
8168
James Smarte59058c2008-08-24 21:49:00 -04008169/**
James Smart3621a712009-04-06 18:47:14 -04008170 * lpfc_sli_queue_setup - Queue initialization function
James Smarte59058c2008-08-24 21:49:00 -04008171 * @phba: Pointer to HBA context object.
8172 *
8173 * lpfc_sli_queue_setup sets up mailbox queues and iocb queues for each
8174 * ring. This function also initializes ring indices of each ring.
8175 * This function is called during the initialization of the SLI
8176 * interface of an HBA.
8177 * This function is called with no lock held and always returns
8178 * 1.
8179 **/
dea31012005-04-17 16:05:31 -05008180int
James Smart2e0fef82007-06-17 19:56:36 -05008181lpfc_sli_queue_setup(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05008182{
8183 struct lpfc_sli *psli;
8184 struct lpfc_sli_ring *pring;
James Bottomley604a3e32005-10-29 10:28:33 -05008185 int i;
dea31012005-04-17 16:05:31 -05008186
8187 psli = &phba->sli;
James Smart2e0fef82007-06-17 19:56:36 -05008188 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05008189 INIT_LIST_HEAD(&psli->mboxq);
James Smart92d7f7b2007-06-17 19:56:38 -05008190 INIT_LIST_HEAD(&psli->mboxq_cmpl);
dea31012005-04-17 16:05:31 -05008191 /* Initialize list headers for txq and txcmplq as double linked lists */
8192 for (i = 0; i < psli->num_rings; i++) {
8193 pring = &psli->ring[i];
8194 pring->ringno = i;
8195 pring->next_cmdidx = 0;
8196 pring->local_getidx = 0;
8197 pring->cmdidx = 0;
8198 INIT_LIST_HEAD(&pring->txq);
8199 INIT_LIST_HEAD(&pring->txcmplq);
8200 INIT_LIST_HEAD(&pring->iocb_continueq);
James Smart9c2face2008-01-11 01:53:18 -05008201 INIT_LIST_HEAD(&pring->iocb_continue_saveq);
dea31012005-04-17 16:05:31 -05008202 INIT_LIST_HEAD(&pring->postbufq);
dea31012005-04-17 16:05:31 -05008203 }
James Smart2e0fef82007-06-17 19:56:36 -05008204 spin_unlock_irq(&phba->hbalock);
8205 return 1;
dea31012005-04-17 16:05:31 -05008206}
8207
James Smarte59058c2008-08-24 21:49:00 -04008208/**
James Smart04c68492009-05-22 14:52:52 -04008209 * lpfc_sli_mbox_sys_flush - Flush mailbox command sub-system
8210 * @phba: Pointer to HBA context object.
8211 *
8212 * This routine flushes the mailbox command subsystem. It will unconditionally
8213 * flush all the mailbox commands in the three possible stages in the mailbox
8214 * command sub-system: pending mailbox command queue; the outstanding mailbox
8215 * command; and completed mailbox command queue. It is caller's responsibility
8216 * to make sure that the driver is in the proper state to flush the mailbox
8217 * command sub-system. Namely, the posting of mailbox commands into the
8218 * pending mailbox command queue from the various clients must be stopped;
8219 * either the HBA is in a state that it will never works on the outstanding
8220 * mailbox command (such as in EEH or ERATT conditions) or the outstanding
8221 * mailbox command has been completed.
8222 **/
8223static void
8224lpfc_sli_mbox_sys_flush(struct lpfc_hba *phba)
8225{
8226 LIST_HEAD(completions);
8227 struct lpfc_sli *psli = &phba->sli;
8228 LPFC_MBOXQ_t *pmb;
8229 unsigned long iflag;
8230
8231 /* Flush all the mailbox commands in the mbox system */
8232 spin_lock_irqsave(&phba->hbalock, iflag);
8233 /* The pending mailbox command queue */
8234 list_splice_init(&phba->sli.mboxq, &completions);
8235 /* The outstanding active mailbox command */
8236 if (psli->mbox_active) {
8237 list_add_tail(&psli->mbox_active->list, &completions);
8238 psli->mbox_active = NULL;
8239 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
8240 }
8241 /* The completed mailbox command queue */
8242 list_splice_init(&phba->sli.mboxq_cmpl, &completions);
8243 spin_unlock_irqrestore(&phba->hbalock, iflag);
8244
8245 /* Return all flushed mailbox commands with MBX_NOT_FINISHED status */
8246 while (!list_empty(&completions)) {
8247 list_remove_head(&completions, pmb, LPFC_MBOXQ_t, list);
8248 pmb->u.mb.mbxStatus = MBX_NOT_FINISHED;
8249 if (pmb->mbox_cmpl)
8250 pmb->mbox_cmpl(phba, pmb);
8251 }
8252}
8253
8254/**
James Smart3621a712009-04-06 18:47:14 -04008255 * lpfc_sli_host_down - Vport cleanup function
James Smarte59058c2008-08-24 21:49:00 -04008256 * @vport: Pointer to virtual port object.
8257 *
8258 * lpfc_sli_host_down is called to clean up the resources
8259 * associated with a vport before destroying virtual
8260 * port data structures.
8261 * This function does following operations:
8262 * - Free discovery resources associated with this virtual
8263 * port.
8264 * - Free iocbs associated with this virtual port in
8265 * the txq.
8266 * - Send abort for all iocb commands associated with this
8267 * vport in txcmplq.
8268 *
8269 * This function is called with no lock held and always returns 1.
8270 **/
dea31012005-04-17 16:05:31 -05008271int
James Smart92d7f7b2007-06-17 19:56:38 -05008272lpfc_sli_host_down(struct lpfc_vport *vport)
8273{
James Smart858c9f62007-06-17 19:56:39 -05008274 LIST_HEAD(completions);
James Smart92d7f7b2007-06-17 19:56:38 -05008275 struct lpfc_hba *phba = vport->phba;
8276 struct lpfc_sli *psli = &phba->sli;
8277 struct lpfc_sli_ring *pring;
8278 struct lpfc_iocbq *iocb, *next_iocb;
James Smart92d7f7b2007-06-17 19:56:38 -05008279 int i;
8280 unsigned long flags = 0;
8281 uint16_t prev_pring_flag;
8282
8283 lpfc_cleanup_discovery_resources(vport);
8284
8285 spin_lock_irqsave(&phba->hbalock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -05008286 for (i = 0; i < psli->num_rings; i++) {
8287 pring = &psli->ring[i];
8288 prev_pring_flag = pring->flag;
James Smart5e9d9b82008-06-14 22:52:53 -04008289 /* Only slow rings */
8290 if (pring->ringno == LPFC_ELS_RING) {
James Smart858c9f62007-06-17 19:56:39 -05008291 pring->flag |= LPFC_DEFERRED_RING_EVENT;
James Smart5e9d9b82008-06-14 22:52:53 -04008292 /* Set the lpfc data pending flag */
8293 set_bit(LPFC_DATA_READY, &phba->data_flags);
8294 }
James Smart92d7f7b2007-06-17 19:56:38 -05008295 /*
8296 * Error everything on the txq since these iocbs have not been
8297 * given to the FW yet.
8298 */
James Smart92d7f7b2007-06-17 19:56:38 -05008299 list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
8300 if (iocb->vport != vport)
8301 continue;
James Smart858c9f62007-06-17 19:56:39 -05008302 list_move_tail(&iocb->list, &completions);
James Smart92d7f7b2007-06-17 19:56:38 -05008303 pring->txq_cnt--;
James Smart92d7f7b2007-06-17 19:56:38 -05008304 }
8305
8306 /* Next issue ABTS for everything on the txcmplq */
8307 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq,
8308 list) {
8309 if (iocb->vport != vport)
8310 continue;
8311 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
8312 }
8313
8314 pring->flag = prev_pring_flag;
8315 }
8316
8317 spin_unlock_irqrestore(&phba->hbalock, flags);
8318
James Smarta257bf92009-04-06 18:48:10 -04008319 /* Cancel all the IOCBs from the completions list */
8320 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
8321 IOERR_SLI_DOWN);
James Smart92d7f7b2007-06-17 19:56:38 -05008322 return 1;
8323}
8324
James Smarte59058c2008-08-24 21:49:00 -04008325/**
James Smart3621a712009-04-06 18:47:14 -04008326 * lpfc_sli_hba_down - Resource cleanup function for the HBA
James Smarte59058c2008-08-24 21:49:00 -04008327 * @phba: Pointer to HBA context object.
8328 *
8329 * This function cleans up all iocb, buffers, mailbox commands
8330 * while shutting down the HBA. This function is called with no
8331 * lock held and always returns 1.
8332 * This function does the following to cleanup driver resources:
8333 * - Free discovery resources for each virtual port
8334 * - Cleanup any pending fabric iocbs
8335 * - Iterate through the iocb txq and free each entry
8336 * in the list.
8337 * - Free up any buffer posted to the HBA
8338 * - Free mailbox commands in the mailbox queue.
8339 **/
James Smart92d7f7b2007-06-17 19:56:38 -05008340int
James Smart2e0fef82007-06-17 19:56:36 -05008341lpfc_sli_hba_down(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05008342{
James Smart2534ba72007-04-25 09:52:20 -04008343 LIST_HEAD(completions);
James Smart2e0fef82007-06-17 19:56:36 -05008344 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05008345 struct lpfc_sli_ring *pring;
James Smart0ff10d42008-01-11 01:52:36 -05008346 struct lpfc_dmabuf *buf_ptr;
dea31012005-04-17 16:05:31 -05008347 unsigned long flags = 0;
James Smart04c68492009-05-22 14:52:52 -04008348 int i;
8349
8350 /* Shutdown the mailbox command sub-system */
8351 lpfc_sli_mbox_sys_shutdown(phba);
dea31012005-04-17 16:05:31 -05008352
dea31012005-04-17 16:05:31 -05008353 lpfc_hba_down_prep(phba);
8354
James Smart92d7f7b2007-06-17 19:56:38 -05008355 lpfc_fabric_abort_hba(phba);
8356
James Smart2e0fef82007-06-17 19:56:36 -05008357 spin_lock_irqsave(&phba->hbalock, flags);
dea31012005-04-17 16:05:31 -05008358 for (i = 0; i < psli->num_rings; i++) {
8359 pring = &psli->ring[i];
James Smart5e9d9b82008-06-14 22:52:53 -04008360 /* Only slow rings */
8361 if (pring->ringno == LPFC_ELS_RING) {
James Smart858c9f62007-06-17 19:56:39 -05008362 pring->flag |= LPFC_DEFERRED_RING_EVENT;
James Smart5e9d9b82008-06-14 22:52:53 -04008363 /* Set the lpfc data pending flag */
8364 set_bit(LPFC_DATA_READY, &phba->data_flags);
8365 }
dea31012005-04-17 16:05:31 -05008366
8367 /*
8368 * Error everything on the txq since these iocbs have not been
8369 * given to the FW yet.
8370 */
James Smart2534ba72007-04-25 09:52:20 -04008371 list_splice_init(&pring->txq, &completions);
dea31012005-04-17 16:05:31 -05008372 pring->txq_cnt = 0;
8373
dea31012005-04-17 16:05:31 -05008374 }
James Smart2e0fef82007-06-17 19:56:36 -05008375 spin_unlock_irqrestore(&phba->hbalock, flags);
dea31012005-04-17 16:05:31 -05008376
James Smarta257bf92009-04-06 18:48:10 -04008377 /* Cancel all the IOCBs from the completions list */
8378 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
8379 IOERR_SLI_DOWN);
James Smart2534ba72007-04-25 09:52:20 -04008380
James Smart0ff10d42008-01-11 01:52:36 -05008381 spin_lock_irqsave(&phba->hbalock, flags);
8382 list_splice_init(&phba->elsbuf, &completions);
8383 phba->elsbuf_cnt = 0;
8384 phba->elsbuf_prev_cnt = 0;
8385 spin_unlock_irqrestore(&phba->hbalock, flags);
8386
8387 while (!list_empty(&completions)) {
8388 list_remove_head(&completions, buf_ptr,
8389 struct lpfc_dmabuf, list);
8390 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
8391 kfree(buf_ptr);
8392 }
8393
dea31012005-04-17 16:05:31 -05008394 /* Return any active mbox cmds */
8395 del_timer_sync(&psli->mbox_tmo);
James Smart92d7f7b2007-06-17 19:56:38 -05008396
James Smartda0436e2009-05-22 14:51:39 -04008397 spin_lock_irqsave(&phba->pport->work_port_lock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -05008398 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
James Smartda0436e2009-05-22 14:51:39 -04008399 spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -05008400
James Smartda0436e2009-05-22 14:51:39 -04008401 return 1;
8402}
James Smart92d7f7b2007-06-17 19:56:38 -05008403
James Smartda0436e2009-05-22 14:51:39 -04008404/**
James Smart3621a712009-04-06 18:47:14 -04008405 * lpfc_sli_pcimem_bcopy - SLI memory copy function
James Smarte59058c2008-08-24 21:49:00 -04008406 * @srcp: Source memory pointer.
8407 * @destp: Destination memory pointer.
8408 * @cnt: Number of words required to be copied.
8409 *
8410 * This function is used for copying data between driver memory
8411 * and the SLI memory. This function also changes the endianness
8412 * of each word if native endianness is different from SLI
8413 * endianness. This function can be called with or without
8414 * lock.
8415 **/
dea31012005-04-17 16:05:31 -05008416void
8417lpfc_sli_pcimem_bcopy(void *srcp, void *destp, uint32_t cnt)
8418{
8419 uint32_t *src = srcp;
8420 uint32_t *dest = destp;
8421 uint32_t ldata;
8422 int i;
8423
8424 for (i = 0; i < (int)cnt; i += sizeof (uint32_t)) {
8425 ldata = *src;
8426 ldata = le32_to_cpu(ldata);
8427 *dest = ldata;
8428 src++;
8429 dest++;
8430 }
8431}
8432
James Smarte59058c2008-08-24 21:49:00 -04008433
8434/**
James Smarta0c87cb2009-07-19 10:01:10 -04008435 * lpfc_sli_bemem_bcopy - SLI memory copy function
8436 * @srcp: Source memory pointer.
8437 * @destp: Destination memory pointer.
8438 * @cnt: Number of words required to be copied.
8439 *
8440 * This function is used for copying data between a data structure
8441 * with big endian representation to local endianness.
8442 * This function can be called with or without lock.
8443 **/
8444void
8445lpfc_sli_bemem_bcopy(void *srcp, void *destp, uint32_t cnt)
8446{
8447 uint32_t *src = srcp;
8448 uint32_t *dest = destp;
8449 uint32_t ldata;
8450 int i;
8451
8452 for (i = 0; i < (int)cnt; i += sizeof(uint32_t)) {
8453 ldata = *src;
8454 ldata = be32_to_cpu(ldata);
8455 *dest = ldata;
8456 src++;
8457 dest++;
8458 }
8459}
8460
8461/**
James Smart3621a712009-04-06 18:47:14 -04008462 * lpfc_sli_ringpostbuf_put - Function to add a buffer to postbufq
James Smarte59058c2008-08-24 21:49:00 -04008463 * @phba: Pointer to HBA context object.
8464 * @pring: Pointer to driver SLI ring object.
8465 * @mp: Pointer to driver buffer object.
8466 *
8467 * This function is called with no lock held.
8468 * It always return zero after adding the buffer to the postbufq
8469 * buffer list.
8470 **/
dea31012005-04-17 16:05:31 -05008471int
James Smart2e0fef82007-06-17 19:56:36 -05008472lpfc_sli_ringpostbuf_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
8473 struct lpfc_dmabuf *mp)
dea31012005-04-17 16:05:31 -05008474{
8475 /* Stick struct lpfc_dmabuf at end of postbufq so driver can look it up
8476 later */
James Smart2e0fef82007-06-17 19:56:36 -05008477 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05008478 list_add_tail(&mp->list, &pring->postbufq);
dea31012005-04-17 16:05:31 -05008479 pring->postbufq_cnt++;
James Smart2e0fef82007-06-17 19:56:36 -05008480 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05008481 return 0;
8482}
8483
James Smarte59058c2008-08-24 21:49:00 -04008484/**
James Smart3621a712009-04-06 18:47:14 -04008485 * lpfc_sli_get_buffer_tag - allocates a tag for a CMD_QUE_XRI64_CX buffer
James Smarte59058c2008-08-24 21:49:00 -04008486 * @phba: Pointer to HBA context object.
8487 *
8488 * When HBQ is enabled, buffers are searched based on tags. This function
8489 * allocates a tag for buffer posted using CMD_QUE_XRI64_CX iocb. The
8490 * tag is bit wise or-ed with QUE_BUFTAG_BIT to make sure that the tag
8491 * does not conflict with tags of buffer posted for unsolicited events.
8492 * The function returns the allocated tag. The function is called with
8493 * no locks held.
8494 **/
James Smart76bb24e2007-10-27 13:38:00 -04008495uint32_t
8496lpfc_sli_get_buffer_tag(struct lpfc_hba *phba)
8497{
8498 spin_lock_irq(&phba->hbalock);
8499 phba->buffer_tag_count++;
8500 /*
8501 * Always set the QUE_BUFTAG_BIT to distiguish between
8502 * a tag assigned by HBQ.
8503 */
8504 phba->buffer_tag_count |= QUE_BUFTAG_BIT;
8505 spin_unlock_irq(&phba->hbalock);
8506 return phba->buffer_tag_count;
8507}
8508
James Smarte59058c2008-08-24 21:49:00 -04008509/**
James Smart3621a712009-04-06 18:47:14 -04008510 * lpfc_sli_ring_taggedbuf_get - find HBQ buffer associated with given tag
James Smarte59058c2008-08-24 21:49:00 -04008511 * @phba: Pointer to HBA context object.
8512 * @pring: Pointer to driver SLI ring object.
8513 * @tag: Buffer tag.
8514 *
8515 * Buffers posted using CMD_QUE_XRI64_CX iocb are in pring->postbufq
8516 * list. After HBA DMA data to these buffers, CMD_IOCB_RET_XRI64_CX
8517 * iocb is posted to the response ring with the tag of the buffer.
8518 * This function searches the pring->postbufq list using the tag
8519 * to find buffer associated with CMD_IOCB_RET_XRI64_CX
8520 * iocb. If the buffer is found then lpfc_dmabuf object of the
8521 * buffer is returned to the caller else NULL is returned.
8522 * This function is called with no lock held.
8523 **/
James Smart76bb24e2007-10-27 13:38:00 -04008524struct lpfc_dmabuf *
8525lpfc_sli_ring_taggedbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
8526 uint32_t tag)
8527{
8528 struct lpfc_dmabuf *mp, *next_mp;
8529 struct list_head *slp = &pring->postbufq;
8530
Lucas De Marchi25985ed2011-03-30 22:57:33 -03008531 /* Search postbufq, from the beginning, looking for a match on tag */
James Smart76bb24e2007-10-27 13:38:00 -04008532 spin_lock_irq(&phba->hbalock);
8533 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
8534 if (mp->buffer_tag == tag) {
8535 list_del_init(&mp->list);
8536 pring->postbufq_cnt--;
8537 spin_unlock_irq(&phba->hbalock);
8538 return mp;
8539 }
8540 }
8541
8542 spin_unlock_irq(&phba->hbalock);
8543 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartd7c255b2008-08-24 21:50:00 -04008544 "0402 Cannot find virtual addr for buffer tag on "
James Smart76bb24e2007-10-27 13:38:00 -04008545 "ring %d Data x%lx x%p x%p x%x\n",
8546 pring->ringno, (unsigned long) tag,
8547 slp->next, slp->prev, pring->postbufq_cnt);
8548
8549 return NULL;
8550}
dea31012005-04-17 16:05:31 -05008551
James Smarte59058c2008-08-24 21:49:00 -04008552/**
James Smart3621a712009-04-06 18:47:14 -04008553 * lpfc_sli_ringpostbuf_get - search buffers for unsolicited CT and ELS events
James Smarte59058c2008-08-24 21:49:00 -04008554 * @phba: Pointer to HBA context object.
8555 * @pring: Pointer to driver SLI ring object.
8556 * @phys: DMA address of the buffer.
8557 *
8558 * This function searches the buffer list using the dma_address
8559 * of unsolicited event to find the driver's lpfc_dmabuf object
8560 * corresponding to the dma_address. The function returns the
8561 * lpfc_dmabuf object if a buffer is found else it returns NULL.
8562 * This function is called by the ct and els unsolicited event
8563 * handlers to get the buffer associated with the unsolicited
8564 * event.
8565 *
8566 * This function is called with no lock held.
8567 **/
dea31012005-04-17 16:05:31 -05008568struct lpfc_dmabuf *
8569lpfc_sli_ringpostbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
8570 dma_addr_t phys)
8571{
8572 struct lpfc_dmabuf *mp, *next_mp;
8573 struct list_head *slp = &pring->postbufq;
8574
Lucas De Marchi25985ed2011-03-30 22:57:33 -03008575 /* Search postbufq, from the beginning, looking for a match on phys */
James Smart2e0fef82007-06-17 19:56:36 -05008576 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05008577 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
8578 if (mp->phys == phys) {
8579 list_del_init(&mp->list);
8580 pring->postbufq_cnt--;
James Smart2e0fef82007-06-17 19:56:36 -05008581 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05008582 return mp;
8583 }
8584 }
8585
James Smart2e0fef82007-06-17 19:56:36 -05008586 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05008587 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04008588 "0410 Cannot find virtual addr for mapped buf on "
dea31012005-04-17 16:05:31 -05008589 "ring %d Data x%llx x%p x%p x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04008590 pring->ringno, (unsigned long long)phys,
dea31012005-04-17 16:05:31 -05008591 slp->next, slp->prev, pring->postbufq_cnt);
8592 return NULL;
8593}
8594
James Smarte59058c2008-08-24 21:49:00 -04008595/**
James Smart3621a712009-04-06 18:47:14 -04008596 * lpfc_sli_abort_els_cmpl - Completion handler for the els abort iocbs
James Smarte59058c2008-08-24 21:49:00 -04008597 * @phba: Pointer to HBA context object.
8598 * @cmdiocb: Pointer to driver command iocb object.
8599 * @rspiocb: Pointer to driver response iocb object.
8600 *
8601 * This function is the completion handler for the abort iocbs for
8602 * ELS commands. This function is called from the ELS ring event
8603 * handler with no lock held. This function frees memory resources
8604 * associated with the abort iocb.
8605 **/
dea31012005-04-17 16:05:31 -05008606static void
James Smart2e0fef82007-06-17 19:56:36 -05008607lpfc_sli_abort_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
8608 struct lpfc_iocbq *rspiocb)
dea31012005-04-17 16:05:31 -05008609{
James Smart2e0fef82007-06-17 19:56:36 -05008610 IOCB_t *irsp = &rspiocb->iocb;
James Smart2680eea2007-04-25 09:52:55 -04008611 uint16_t abort_iotag, abort_context;
James Smart92d7f7b2007-06-17 19:56:38 -05008612 struct lpfc_iocbq *abort_iocb;
James Smart2680eea2007-04-25 09:52:55 -04008613 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
8614
8615 abort_iocb = NULL;
James Smart2680eea2007-04-25 09:52:55 -04008616
8617 if (irsp->ulpStatus) {
8618 abort_context = cmdiocb->iocb.un.acxri.abortContextTag;
8619 abort_iotag = cmdiocb->iocb.un.acxri.abortIoTag;
8620
James Smart2e0fef82007-06-17 19:56:36 -05008621 spin_lock_irq(&phba->hbalock);
James Smart45ed1192009-10-02 15:17:02 -04008622 if (phba->sli_rev < LPFC_SLI_REV4) {
8623 if (abort_iotag != 0 &&
8624 abort_iotag <= phba->sli.last_iotag)
8625 abort_iocb =
8626 phba->sli.iocbq_lookup[abort_iotag];
8627 } else
8628 /* For sli4 the abort_tag is the XRI,
8629 * so the abort routine puts the iotag of the iocb
8630 * being aborted in the context field of the abort
8631 * IOCB.
8632 */
8633 abort_iocb = phba->sli.iocbq_lookup[abort_context];
James Smart2680eea2007-04-25 09:52:55 -04008634
James Smart2680eea2007-04-25 09:52:55 -04008635 /*
James Smart58da1ff2008-04-07 10:15:56 -04008636 * If the iocb is not found in Firmware queue the iocb
8637 * might have completed already. Do not free it again.
8638 */
James Smart9b379602008-04-07 10:16:00 -04008639 if (irsp->ulpStatus == IOSTAT_LOCAL_REJECT) {
James Smart45ed1192009-10-02 15:17:02 -04008640 if (irsp->un.ulpWord[4] != IOERR_NO_XRI) {
8641 spin_unlock_irq(&phba->hbalock);
8642 lpfc_sli_release_iocbq(phba, cmdiocb);
8643 return;
8644 }
8645 /* For SLI4 the ulpContext field for abort IOCB
8646 * holds the iotag of the IOCB being aborted so
8647 * the local abort_context needs to be reset to
8648 * match the aborted IOCBs ulpContext.
8649 */
8650 if (abort_iocb && phba->sli_rev == LPFC_SLI_REV4)
8651 abort_context = abort_iocb->iocb.ulpContext;
James Smart58da1ff2008-04-07 10:15:56 -04008652 }
James Smart2a9bf3d2010-06-07 15:24:45 -04008653
8654 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS | LOG_SLI,
8655 "0327 Cannot abort els iocb %p "
8656 "with tag %x context %x, abort status %x, "
8657 "abort code %x\n",
8658 abort_iocb, abort_iotag, abort_context,
8659 irsp->ulpStatus, irsp->un.ulpWord[4]);
James Smart58da1ff2008-04-07 10:15:56 -04008660 /*
James Smart2680eea2007-04-25 09:52:55 -04008661 * make sure we have the right iocbq before taking it
8662 * off the txcmplq and try to call completion routine.
8663 */
James Smart2e0fef82007-06-17 19:56:36 -05008664 if (!abort_iocb ||
8665 abort_iocb->iocb.ulpContext != abort_context ||
8666 (abort_iocb->iocb_flag & LPFC_DRIVER_ABORTED) == 0)
8667 spin_unlock_irq(&phba->hbalock);
James Smart341af102010-01-26 23:07:37 -05008668 else if (phba->sli_rev < LPFC_SLI_REV4) {
8669 /*
8670 * leave the SLI4 aborted command on the txcmplq
8671 * list and the command complete WCQE's XB bit
8672 * will tell whether the SGL (XRI) can be released
8673 * immediately or to the aborted SGL list for the
8674 * following abort XRI from the HBA.
8675 */
James Smart92d7f7b2007-06-17 19:56:38 -05008676 list_del_init(&abort_iocb->list);
James Smart2a9bf3d2010-06-07 15:24:45 -04008677 if (abort_iocb->iocb_flag & LPFC_IO_ON_Q) {
8678 abort_iocb->iocb_flag &= ~LPFC_IO_ON_Q;
8679 pring->txcmplq_cnt--;
8680 }
James Smart2680eea2007-04-25 09:52:55 -04008681
James Smart0ff10d42008-01-11 01:52:36 -05008682 /* Firmware could still be in progress of DMAing
8683 * payload, so don't free data buffer till after
8684 * a hbeat.
8685 */
8686 abort_iocb->iocb_flag |= LPFC_DELAY_MEM_FREE;
James Smart92d7f7b2007-06-17 19:56:38 -05008687 abort_iocb->iocb_flag &= ~LPFC_DRIVER_ABORTED;
James Smart341af102010-01-26 23:07:37 -05008688 spin_unlock_irq(&phba->hbalock);
8689
James Smart92d7f7b2007-06-17 19:56:38 -05008690 abort_iocb->iocb.ulpStatus = IOSTAT_LOCAL_REJECT;
James Smart341af102010-01-26 23:07:37 -05008691 abort_iocb->iocb.un.ulpWord[4] = IOERR_ABORT_REQUESTED;
James Smart92d7f7b2007-06-17 19:56:38 -05008692 (abort_iocb->iocb_cmpl)(phba, abort_iocb, abort_iocb);
James Smart49198b32010-04-06 15:04:33 -04008693 } else
8694 spin_unlock_irq(&phba->hbalock);
James Smart2680eea2007-04-25 09:52:55 -04008695 }
8696
James Bottomley604a3e32005-10-29 10:28:33 -05008697 lpfc_sli_release_iocbq(phba, cmdiocb);
dea31012005-04-17 16:05:31 -05008698 return;
8699}
8700
James Smarte59058c2008-08-24 21:49:00 -04008701/**
James Smart3621a712009-04-06 18:47:14 -04008702 * lpfc_ignore_els_cmpl - Completion handler for aborted ELS command
James Smarte59058c2008-08-24 21:49:00 -04008703 * @phba: Pointer to HBA context object.
8704 * @cmdiocb: Pointer to driver command iocb object.
8705 * @rspiocb: Pointer to driver response iocb object.
8706 *
8707 * The function is called from SLI ring event handler with no
8708 * lock held. This function is the completion handler for ELS commands
8709 * which are aborted. The function frees memory resources used for
8710 * the aborted ELS commands.
8711 **/
James Smart92d7f7b2007-06-17 19:56:38 -05008712static void
8713lpfc_ignore_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
8714 struct lpfc_iocbq *rspiocb)
8715{
8716 IOCB_t *irsp = &rspiocb->iocb;
8717
8718 /* ELS cmd tag <ulpIoTag> completes */
8719 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
James Smartd7c255b2008-08-24 21:50:00 -04008720 "0139 Ignoring ELS cmd tag x%x completion Data: "
James Smart92d7f7b2007-06-17 19:56:38 -05008721 "x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04008722 irsp->ulpIoTag, irsp->ulpStatus,
James Smart92d7f7b2007-06-17 19:56:38 -05008723 irsp->un.ulpWord[4], irsp->ulpTimeout);
James Smart858c9f62007-06-17 19:56:39 -05008724 if (cmdiocb->iocb.ulpCommand == CMD_GEN_REQUEST64_CR)
8725 lpfc_ct_free_iocb(phba, cmdiocb);
8726 else
8727 lpfc_els_free_iocb(phba, cmdiocb);
James Smart92d7f7b2007-06-17 19:56:38 -05008728 return;
8729}
8730
James Smarte59058c2008-08-24 21:49:00 -04008731/**
James Smart5af5eee2010-10-22 11:06:38 -04008732 * lpfc_sli_abort_iotag_issue - Issue abort for a command iocb
James Smarte59058c2008-08-24 21:49:00 -04008733 * @phba: Pointer to HBA context object.
8734 * @pring: Pointer to driver SLI ring object.
8735 * @cmdiocb: Pointer to driver command iocb object.
8736 *
James Smart5af5eee2010-10-22 11:06:38 -04008737 * This function issues an abort iocb for the provided command iocb down to
8738 * the port. Other than the case the outstanding command iocb is an abort
8739 * request, this function issues abort out unconditionally. This function is
8740 * called with hbalock held. The function returns 0 when it fails due to
8741 * memory allocation failure or when the command iocb is an abort request.
James Smarte59058c2008-08-24 21:49:00 -04008742 **/
James Smart5af5eee2010-10-22 11:06:38 -04008743static int
8744lpfc_sli_abort_iotag_issue(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -05008745 struct lpfc_iocbq *cmdiocb)
dea31012005-04-17 16:05:31 -05008746{
James Smart2e0fef82007-06-17 19:56:36 -05008747 struct lpfc_vport *vport = cmdiocb->vport;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04008748 struct lpfc_iocbq *abtsiocbp;
dea31012005-04-17 16:05:31 -05008749 IOCB_t *icmd = NULL;
8750 IOCB_t *iabt = NULL;
James Smart5af5eee2010-10-22 11:06:38 -04008751 int retval;
James Smart07951072007-04-25 09:51:38 -04008752
James Smart92d7f7b2007-06-17 19:56:38 -05008753 /*
8754 * There are certain command types we don't want to abort. And we
8755 * don't want to abort commands that are already in the process of
8756 * being aborted.
James Smart07951072007-04-25 09:51:38 -04008757 */
8758 icmd = &cmdiocb->iocb;
James Smart2e0fef82007-06-17 19:56:36 -05008759 if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
James Smart92d7f7b2007-06-17 19:56:38 -05008760 icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
8761 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
James Smart07951072007-04-25 09:51:38 -04008762 return 0;
8763
dea31012005-04-17 16:05:31 -05008764 /* issue ABTS for this IOCB based on iotag */
James Smart92d7f7b2007-06-17 19:56:38 -05008765 abtsiocbp = __lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05008766 if (abtsiocbp == NULL)
8767 return 0;
dea31012005-04-17 16:05:31 -05008768
James Smart07951072007-04-25 09:51:38 -04008769 /* This signals the response to set the correct status
James Smart341af102010-01-26 23:07:37 -05008770 * before calling the completion handler
James Smart07951072007-04-25 09:51:38 -04008771 */
8772 cmdiocb->iocb_flag |= LPFC_DRIVER_ABORTED;
8773
dea31012005-04-17 16:05:31 -05008774 iabt = &abtsiocbp->iocb;
James Smart07951072007-04-25 09:51:38 -04008775 iabt->un.acxri.abortType = ABORT_TYPE_ABTS;
8776 iabt->un.acxri.abortContextTag = icmd->ulpContext;
James Smart45ed1192009-10-02 15:17:02 -04008777 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smartda0436e2009-05-22 14:51:39 -04008778 iabt->un.acxri.abortIoTag = cmdiocb->sli4_xritag;
James Smart45ed1192009-10-02 15:17:02 -04008779 iabt->un.acxri.abortContextTag = cmdiocb->iotag;
8780 }
James Smartda0436e2009-05-22 14:51:39 -04008781 else
8782 iabt->un.acxri.abortIoTag = icmd->ulpIoTag;
dea31012005-04-17 16:05:31 -05008783 iabt->ulpLe = 1;
James Smart07951072007-04-25 09:51:38 -04008784 iabt->ulpClass = icmd->ulpClass;
dea31012005-04-17 16:05:31 -05008785
James Smart5ffc2662009-11-18 15:39:44 -05008786 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
8787 abtsiocbp->fcp_wqidx = cmdiocb->fcp_wqidx;
James Smart341af102010-01-26 23:07:37 -05008788 if (cmdiocb->iocb_flag & LPFC_IO_FCP)
8789 abtsiocbp->iocb_flag |= LPFC_USE_FCPWQIDX;
James Smart5ffc2662009-11-18 15:39:44 -05008790
James Smart2e0fef82007-06-17 19:56:36 -05008791 if (phba->link_state >= LPFC_LINK_UP)
James Smart07951072007-04-25 09:51:38 -04008792 iabt->ulpCommand = CMD_ABORT_XRI_CN;
8793 else
8794 iabt->ulpCommand = CMD_CLOSE_XRI_CN;
8795
8796 abtsiocbp->iocb_cmpl = lpfc_sli_abort_els_cmpl;
James Smart5b8bd0c2007-04-25 09:52:49 -04008797
James Smarte8b62012007-08-02 11:10:09 -04008798 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
8799 "0339 Abort xri x%x, original iotag x%x, "
8800 "abort cmd iotag x%x\n",
James Smart2a9bf3d2010-06-07 15:24:45 -04008801 iabt->un.acxri.abortIoTag,
James Smarte8b62012007-08-02 11:10:09 -04008802 iabt->un.acxri.abortContextTag,
James Smart2a9bf3d2010-06-07 15:24:45 -04008803 abtsiocbp->iotag);
James Smartda0436e2009-05-22 14:51:39 -04008804 retval = __lpfc_sli_issue_iocb(phba, pring->ringno, abtsiocbp, 0);
James Smart07951072007-04-25 09:51:38 -04008805
James Smartd7c255b2008-08-24 21:50:00 -04008806 if (retval)
8807 __lpfc_sli_release_iocbq(phba, abtsiocbp);
James Smart5af5eee2010-10-22 11:06:38 -04008808
8809 /*
8810 * Caller to this routine should check for IOCB_ERROR
8811 * and handle it properly. This routine no longer removes
8812 * iocb off txcmplq and call compl in case of IOCB_ERROR.
8813 */
8814 return retval;
8815}
8816
8817/**
8818 * lpfc_sli_issue_abort_iotag - Abort function for a command iocb
8819 * @phba: Pointer to HBA context object.
8820 * @pring: Pointer to driver SLI ring object.
8821 * @cmdiocb: Pointer to driver command iocb object.
8822 *
8823 * This function issues an abort iocb for the provided command iocb. In case
8824 * of unloading, the abort iocb will not be issued to commands on the ELS
8825 * ring. Instead, the callback function shall be changed to those commands
8826 * so that nothing happens when them finishes. This function is called with
8827 * hbalock held. The function returns 0 when the command iocb is an abort
8828 * request.
8829 **/
8830int
8831lpfc_sli_issue_abort_iotag(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
8832 struct lpfc_iocbq *cmdiocb)
8833{
8834 struct lpfc_vport *vport = cmdiocb->vport;
8835 int retval = IOCB_ERROR;
8836 IOCB_t *icmd = NULL;
8837
8838 /*
8839 * There are certain command types we don't want to abort. And we
8840 * don't want to abort commands that are already in the process of
8841 * being aborted.
8842 */
8843 icmd = &cmdiocb->iocb;
8844 if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
8845 icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
8846 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
8847 return 0;
8848
8849 /*
8850 * If we're unloading, don't abort iocb on the ELS ring, but change
8851 * the callback so that nothing happens when it finishes.
8852 */
8853 if ((vport->load_flag & FC_UNLOADING) &&
8854 (pring->ringno == LPFC_ELS_RING)) {
8855 if (cmdiocb->iocb_flag & LPFC_IO_FABRIC)
8856 cmdiocb->fabric_iocb_cmpl = lpfc_ignore_els_cmpl;
8857 else
8858 cmdiocb->iocb_cmpl = lpfc_ignore_els_cmpl;
8859 goto abort_iotag_exit;
8860 }
8861
8862 /* Now, we try to issue the abort to the cmdiocb out */
8863 retval = lpfc_sli_abort_iotag_issue(phba, pring, cmdiocb);
8864
James Smart07951072007-04-25 09:51:38 -04008865abort_iotag_exit:
James Smart2e0fef82007-06-17 19:56:36 -05008866 /*
8867 * Caller to this routine should check for IOCB_ERROR
8868 * and handle it properly. This routine no longer removes
8869 * iocb off txcmplq and call compl in case of IOCB_ERROR.
James Smart07951072007-04-25 09:51:38 -04008870 */
James Smart2e0fef82007-06-17 19:56:36 -05008871 return retval;
dea31012005-04-17 16:05:31 -05008872}
8873
James Smarte59058c2008-08-24 21:49:00 -04008874/**
James Smart5af5eee2010-10-22 11:06:38 -04008875 * lpfc_sli_iocb_ring_abort - Unconditionally abort all iocbs on an iocb ring
8876 * @phba: Pointer to HBA context object.
8877 * @pring: Pointer to driver SLI ring object.
8878 *
8879 * This function aborts all iocbs in the given ring and frees all the iocb
8880 * objects in txq. This function issues abort iocbs unconditionally for all
8881 * the iocb commands in txcmplq. The iocbs in the txcmplq is not guaranteed
8882 * to complete before the return of this function. The caller is not required
8883 * to hold any locks.
8884 **/
8885static void
8886lpfc_sli_iocb_ring_abort(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
8887{
8888 LIST_HEAD(completions);
8889 struct lpfc_iocbq *iocb, *next_iocb;
8890
8891 if (pring->ringno == LPFC_ELS_RING)
8892 lpfc_fabric_abort_hba(phba);
8893
8894 spin_lock_irq(&phba->hbalock);
8895
8896 /* Take off all the iocbs on txq for cancelling */
8897 list_splice_init(&pring->txq, &completions);
8898 pring->txq_cnt = 0;
8899
8900 /* Next issue ABTS for everything on the txcmplq */
8901 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
8902 lpfc_sli_abort_iotag_issue(phba, pring, iocb);
8903
8904 spin_unlock_irq(&phba->hbalock);
8905
8906 /* Cancel all the IOCBs from the completions list */
8907 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
8908 IOERR_SLI_ABORTED);
8909}
8910
8911/**
8912 * lpfc_sli_hba_iocb_abort - Abort all iocbs to an hba.
8913 * @phba: pointer to lpfc HBA data structure.
8914 *
8915 * This routine will abort all pending and outstanding iocbs to an HBA.
8916 **/
8917void
8918lpfc_sli_hba_iocb_abort(struct lpfc_hba *phba)
8919{
8920 struct lpfc_sli *psli = &phba->sli;
8921 struct lpfc_sli_ring *pring;
8922 int i;
8923
8924 for (i = 0; i < psli->num_rings; i++) {
8925 pring = &psli->ring[i];
8926 lpfc_sli_iocb_ring_abort(phba, pring);
8927 }
8928}
8929
8930/**
James Smart3621a712009-04-06 18:47:14 -04008931 * lpfc_sli_validate_fcp_iocb - find commands associated with a vport or LUN
James Smarte59058c2008-08-24 21:49:00 -04008932 * @iocbq: Pointer to driver iocb object.
8933 * @vport: Pointer to driver virtual port object.
8934 * @tgt_id: SCSI ID of the target.
8935 * @lun_id: LUN ID of the scsi device.
8936 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST
8937 *
James Smart3621a712009-04-06 18:47:14 -04008938 * This function acts as an iocb filter for functions which abort or count
James Smarte59058c2008-08-24 21:49:00 -04008939 * all FCP iocbs pending on a lun/SCSI target/SCSI host. It will return
8940 * 0 if the filtering criteria is met for the given iocb and will return
8941 * 1 if the filtering criteria is not met.
8942 * If ctx_cmd == LPFC_CTX_LUN, the function returns 0 only if the
8943 * given iocb is for the SCSI device specified by vport, tgt_id and
8944 * lun_id parameter.
8945 * If ctx_cmd == LPFC_CTX_TGT, the function returns 0 only if the
8946 * given iocb is for the SCSI target specified by vport and tgt_id
8947 * parameters.
8948 * If ctx_cmd == LPFC_CTX_HOST, the function returns 0 only if the
8949 * given iocb is for the SCSI host associated with the given vport.
8950 * This function is called with no locks held.
8951 **/
dea31012005-04-17 16:05:31 -05008952static int
James Smart51ef4c22007-08-02 11:10:31 -04008953lpfc_sli_validate_fcp_iocb(struct lpfc_iocbq *iocbq, struct lpfc_vport *vport,
8954 uint16_t tgt_id, uint64_t lun_id,
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04008955 lpfc_ctx_cmd ctx_cmd)
dea31012005-04-17 16:05:31 -05008956{
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04008957 struct lpfc_scsi_buf *lpfc_cmd;
dea31012005-04-17 16:05:31 -05008958 int rc = 1;
8959
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04008960 if (!(iocbq->iocb_flag & LPFC_IO_FCP))
8961 return rc;
8962
James Smart51ef4c22007-08-02 11:10:31 -04008963 if (iocbq->vport != vport)
8964 return rc;
8965
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04008966 lpfc_cmd = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04008967
James Smart495a7142008-06-14 22:52:59 -04008968 if (lpfc_cmd->pCmd == NULL)
dea31012005-04-17 16:05:31 -05008969 return rc;
8970
8971 switch (ctx_cmd) {
8972 case LPFC_CTX_LUN:
James Smart495a7142008-06-14 22:52:59 -04008973 if ((lpfc_cmd->rdata->pnode) &&
8974 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id) &&
8975 (scsilun_to_int(&lpfc_cmd->fcp_cmnd->fcp_lun) == lun_id))
dea31012005-04-17 16:05:31 -05008976 rc = 0;
8977 break;
8978 case LPFC_CTX_TGT:
James Smart495a7142008-06-14 22:52:59 -04008979 if ((lpfc_cmd->rdata->pnode) &&
8980 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id))
dea31012005-04-17 16:05:31 -05008981 rc = 0;
8982 break;
dea31012005-04-17 16:05:31 -05008983 case LPFC_CTX_HOST:
8984 rc = 0;
8985 break;
8986 default:
8987 printk(KERN_ERR "%s: Unknown context cmd type, value %d\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07008988 __func__, ctx_cmd);
dea31012005-04-17 16:05:31 -05008989 break;
8990 }
8991
8992 return rc;
8993}
8994
James Smarte59058c2008-08-24 21:49:00 -04008995/**
James Smart3621a712009-04-06 18:47:14 -04008996 * lpfc_sli_sum_iocb - Function to count the number of FCP iocbs pending
James Smarte59058c2008-08-24 21:49:00 -04008997 * @vport: Pointer to virtual port.
8998 * @tgt_id: SCSI ID of the target.
8999 * @lun_id: LUN ID of the scsi device.
9000 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
9001 *
9002 * This function returns number of FCP commands pending for the vport.
9003 * When ctx_cmd == LPFC_CTX_LUN, the function returns number of FCP
9004 * commands pending on the vport associated with SCSI device specified
9005 * by tgt_id and lun_id parameters.
9006 * When ctx_cmd == LPFC_CTX_TGT, the function returns number of FCP
9007 * commands pending on the vport associated with SCSI target specified
9008 * by tgt_id parameter.
9009 * When ctx_cmd == LPFC_CTX_HOST, the function returns number of FCP
9010 * commands pending on the vport.
9011 * This function returns the number of iocbs which satisfy the filter.
9012 * This function is called without any lock held.
9013 **/
dea31012005-04-17 16:05:31 -05009014int
James Smart51ef4c22007-08-02 11:10:31 -04009015lpfc_sli_sum_iocb(struct lpfc_vport *vport, uint16_t tgt_id, uint64_t lun_id,
9016 lpfc_ctx_cmd ctx_cmd)
dea31012005-04-17 16:05:31 -05009017{
James Smart51ef4c22007-08-02 11:10:31 -04009018 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009019 struct lpfc_iocbq *iocbq;
9020 int sum, i;
dea31012005-04-17 16:05:31 -05009021
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009022 for (i = 1, sum = 0; i <= phba->sli.last_iotag; i++) {
9023 iocbq = phba->sli.iocbq_lookup[i];
dea31012005-04-17 16:05:31 -05009024
James Smart51ef4c22007-08-02 11:10:31 -04009025 if (lpfc_sli_validate_fcp_iocb (iocbq, vport, tgt_id, lun_id,
9026 ctx_cmd) == 0)
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009027 sum++;
dea31012005-04-17 16:05:31 -05009028 }
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009029
dea31012005-04-17 16:05:31 -05009030 return sum;
9031}
9032
James Smarte59058c2008-08-24 21:49:00 -04009033/**
James Smart3621a712009-04-06 18:47:14 -04009034 * lpfc_sli_abort_fcp_cmpl - Completion handler function for aborted FCP IOCBs
James Smarte59058c2008-08-24 21:49:00 -04009035 * @phba: Pointer to HBA context object
9036 * @cmdiocb: Pointer to command iocb object.
9037 * @rspiocb: Pointer to response iocb object.
9038 *
9039 * This function is called when an aborted FCP iocb completes. This
9040 * function is called by the ring event handler with no lock held.
9041 * This function frees the iocb.
9042 **/
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -04009043void
James Smart2e0fef82007-06-17 19:56:36 -05009044lpfc_sli_abort_fcp_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
9045 struct lpfc_iocbq *rspiocb)
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -04009046{
James Bottomley604a3e32005-10-29 10:28:33 -05009047 lpfc_sli_release_iocbq(phba, cmdiocb);
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -04009048 return;
9049}
9050
James Smarte59058c2008-08-24 21:49:00 -04009051/**
James Smart3621a712009-04-06 18:47:14 -04009052 * lpfc_sli_abort_iocb - issue abort for all commands on a host/target/LUN
James Smarte59058c2008-08-24 21:49:00 -04009053 * @vport: Pointer to virtual port.
9054 * @pring: Pointer to driver SLI ring object.
9055 * @tgt_id: SCSI ID of the target.
9056 * @lun_id: LUN ID of the scsi device.
9057 * @abort_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
9058 *
9059 * This function sends an abort command for every SCSI command
9060 * associated with the given virtual port pending on the ring
9061 * filtered by lpfc_sli_validate_fcp_iocb function.
9062 * When abort_cmd == LPFC_CTX_LUN, the function sends abort only to the
9063 * FCP iocbs associated with lun specified by tgt_id and lun_id
9064 * parameters
9065 * When abort_cmd == LPFC_CTX_TGT, the function sends abort only to the
9066 * FCP iocbs associated with SCSI target specified by tgt_id parameter.
9067 * When abort_cmd == LPFC_CTX_HOST, the function sends abort to all
9068 * FCP iocbs associated with virtual port.
9069 * This function returns number of iocbs it failed to abort.
9070 * This function is called with no locks held.
9071 **/
dea31012005-04-17 16:05:31 -05009072int
James Smart51ef4c22007-08-02 11:10:31 -04009073lpfc_sli_abort_iocb(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
9074 uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd abort_cmd)
dea31012005-04-17 16:05:31 -05009075{
James Smart51ef4c22007-08-02 11:10:31 -04009076 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009077 struct lpfc_iocbq *iocbq;
9078 struct lpfc_iocbq *abtsiocb;
dea31012005-04-17 16:05:31 -05009079 IOCB_t *cmd = NULL;
dea31012005-04-17 16:05:31 -05009080 int errcnt = 0, ret_val = 0;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009081 int i;
dea31012005-04-17 16:05:31 -05009082
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009083 for (i = 1; i <= phba->sli.last_iotag; i++) {
9084 iocbq = phba->sli.iocbq_lookup[i];
dea31012005-04-17 16:05:31 -05009085
James Smart51ef4c22007-08-02 11:10:31 -04009086 if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
James Smart2e0fef82007-06-17 19:56:36 -05009087 abort_cmd) != 0)
dea31012005-04-17 16:05:31 -05009088 continue;
9089
9090 /* issue ABTS for this IOCB based on iotag */
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009091 abtsiocb = lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05009092 if (abtsiocb == NULL) {
9093 errcnt++;
9094 continue;
9095 }
dea31012005-04-17 16:05:31 -05009096
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009097 cmd = &iocbq->iocb;
dea31012005-04-17 16:05:31 -05009098 abtsiocb->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
9099 abtsiocb->iocb.un.acxri.abortContextTag = cmd->ulpContext;
James Smartda0436e2009-05-22 14:51:39 -04009100 if (phba->sli_rev == LPFC_SLI_REV4)
9101 abtsiocb->iocb.un.acxri.abortIoTag = iocbq->sli4_xritag;
9102 else
9103 abtsiocb->iocb.un.acxri.abortIoTag = cmd->ulpIoTag;
dea31012005-04-17 16:05:31 -05009104 abtsiocb->iocb.ulpLe = 1;
9105 abtsiocb->iocb.ulpClass = cmd->ulpClass;
James Smart2e0fef82007-06-17 19:56:36 -05009106 abtsiocb->vport = phba->pport;
dea31012005-04-17 16:05:31 -05009107
James Smart5ffc2662009-11-18 15:39:44 -05009108 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
9109 abtsiocb->fcp_wqidx = iocbq->fcp_wqidx;
James Smart341af102010-01-26 23:07:37 -05009110 if (iocbq->iocb_flag & LPFC_IO_FCP)
9111 abtsiocb->iocb_flag |= LPFC_USE_FCPWQIDX;
James Smart5ffc2662009-11-18 15:39:44 -05009112
James Smart2e0fef82007-06-17 19:56:36 -05009113 if (lpfc_is_link_up(phba))
dea31012005-04-17 16:05:31 -05009114 abtsiocb->iocb.ulpCommand = CMD_ABORT_XRI_CN;
9115 else
9116 abtsiocb->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
9117
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -04009118 /* Setup callback routine and issue the command. */
9119 abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
James Smartda0436e2009-05-22 14:51:39 -04009120 ret_val = lpfc_sli_issue_iocb(phba, pring->ringno,
9121 abtsiocb, 0);
dea31012005-04-17 16:05:31 -05009122 if (ret_val == IOCB_ERROR) {
James Bottomley604a3e32005-10-29 10:28:33 -05009123 lpfc_sli_release_iocbq(phba, abtsiocb);
dea31012005-04-17 16:05:31 -05009124 errcnt++;
9125 continue;
9126 }
9127 }
9128
9129 return errcnt;
9130}
9131
James Smarte59058c2008-08-24 21:49:00 -04009132/**
James Smart3621a712009-04-06 18:47:14 -04009133 * lpfc_sli_wake_iocb_wait - lpfc_sli_issue_iocb_wait's completion handler
James Smarte59058c2008-08-24 21:49:00 -04009134 * @phba: Pointer to HBA context object.
9135 * @cmdiocbq: Pointer to command iocb.
9136 * @rspiocbq: Pointer to response iocb.
9137 *
9138 * This function is the completion handler for iocbs issued using
9139 * lpfc_sli_issue_iocb_wait function. This function is called by the
9140 * ring event handler function without any lock held. This function
9141 * can be called from both worker thread context and interrupt
9142 * context. This function also can be called from other thread which
9143 * cleans up the SLI layer objects.
9144 * This function copy the contents of the response iocb to the
9145 * response iocb memory object provided by the caller of
9146 * lpfc_sli_issue_iocb_wait and then wakes up the thread which
9147 * sleeps for the iocb completion.
9148 **/
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009149static void
9150lpfc_sli_wake_iocb_wait(struct lpfc_hba *phba,
9151 struct lpfc_iocbq *cmdiocbq,
9152 struct lpfc_iocbq *rspiocbq)
dea31012005-04-17 16:05:31 -05009153{
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009154 wait_queue_head_t *pdone_q;
9155 unsigned long iflags;
James Smart0f65ff62010-02-26 14:14:23 -05009156 struct lpfc_scsi_buf *lpfc_cmd;
dea31012005-04-17 16:05:31 -05009157
James Smart2e0fef82007-06-17 19:56:36 -05009158 spin_lock_irqsave(&phba->hbalock, iflags);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009159 cmdiocbq->iocb_flag |= LPFC_IO_WAKE;
9160 if (cmdiocbq->context2 && rspiocbq)
9161 memcpy(&((struct lpfc_iocbq *)cmdiocbq->context2)->iocb,
9162 &rspiocbq->iocb, sizeof(IOCB_t));
9163
James Smart0f65ff62010-02-26 14:14:23 -05009164 /* Set the exchange busy flag for task management commands */
9165 if ((cmdiocbq->iocb_flag & LPFC_IO_FCP) &&
9166 !(cmdiocbq->iocb_flag & LPFC_IO_LIBDFC)) {
9167 lpfc_cmd = container_of(cmdiocbq, struct lpfc_scsi_buf,
9168 cur_iocbq);
9169 lpfc_cmd->exch_busy = rspiocbq->iocb_flag & LPFC_EXCHANGE_BUSY;
9170 }
9171
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009172 pdone_q = cmdiocbq->context_un.wait_queue;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009173 if (pdone_q)
9174 wake_up(pdone_q);
James Smart858c9f62007-06-17 19:56:39 -05009175 spin_unlock_irqrestore(&phba->hbalock, iflags);
dea31012005-04-17 16:05:31 -05009176 return;
9177}
9178
James Smarte59058c2008-08-24 21:49:00 -04009179/**
James Smartd11e31d2009-06-10 17:23:06 -04009180 * lpfc_chk_iocb_flg - Test IOCB flag with lock held.
9181 * @phba: Pointer to HBA context object..
9182 * @piocbq: Pointer to command iocb.
9183 * @flag: Flag to test.
9184 *
9185 * This routine grabs the hbalock and then test the iocb_flag to
9186 * see if the passed in flag is set.
9187 * Returns:
9188 * 1 if flag is set.
9189 * 0 if flag is not set.
9190 **/
9191static int
9192lpfc_chk_iocb_flg(struct lpfc_hba *phba,
9193 struct lpfc_iocbq *piocbq, uint32_t flag)
9194{
9195 unsigned long iflags;
9196 int ret;
9197
9198 spin_lock_irqsave(&phba->hbalock, iflags);
9199 ret = piocbq->iocb_flag & flag;
9200 spin_unlock_irqrestore(&phba->hbalock, iflags);
9201 return ret;
9202
9203}
9204
9205/**
James Smart3621a712009-04-06 18:47:14 -04009206 * lpfc_sli_issue_iocb_wait - Synchronous function to issue iocb commands
James Smarte59058c2008-08-24 21:49:00 -04009207 * @phba: Pointer to HBA context object..
9208 * @pring: Pointer to sli ring.
9209 * @piocb: Pointer to command iocb.
9210 * @prspiocbq: Pointer to response iocb.
9211 * @timeout: Timeout in number of seconds.
9212 *
9213 * This function issues the iocb to firmware and waits for the
9214 * iocb to complete. If the iocb command is not
9215 * completed within timeout seconds, it returns IOCB_TIMEDOUT.
9216 * Caller should not free the iocb resources if this function
9217 * returns IOCB_TIMEDOUT.
9218 * The function waits for the iocb completion using an
9219 * non-interruptible wait.
9220 * This function will sleep while waiting for iocb completion.
9221 * So, this function should not be called from any context which
9222 * does not allow sleeping. Due to the same reason, this function
9223 * cannot be called with interrupt disabled.
9224 * This function assumes that the iocb completions occur while
9225 * this function sleep. So, this function cannot be called from
9226 * the thread which process iocb completion for this ring.
9227 * This function clears the iocb_flag of the iocb object before
9228 * issuing the iocb and the iocb completion handler sets this
9229 * flag and wakes this thread when the iocb completes.
9230 * The contents of the response iocb will be copied to prspiocbq
9231 * by the completion handler when the command completes.
9232 * This function returns IOCB_SUCCESS when success.
9233 * This function is called with no lock held.
9234 **/
dea31012005-04-17 16:05:31 -05009235int
James Smart2e0fef82007-06-17 19:56:36 -05009236lpfc_sli_issue_iocb_wait(struct lpfc_hba *phba,
James Smartda0436e2009-05-22 14:51:39 -04009237 uint32_t ring_number,
James Smart2e0fef82007-06-17 19:56:36 -05009238 struct lpfc_iocbq *piocb,
9239 struct lpfc_iocbq *prspiocbq,
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009240 uint32_t timeout)
dea31012005-04-17 16:05:31 -05009241{
Peter Zijlstra7259f0d2006-10-29 22:46:36 -08009242 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009243 long timeleft, timeout_req = 0;
9244 int retval = IOCB_SUCCESS;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05009245 uint32_t creg_val;
James Smart2a9bf3d2010-06-07 15:24:45 -04009246 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
dea31012005-04-17 16:05:31 -05009247 /*
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009248 * If the caller has provided a response iocbq buffer, then context2
9249 * is NULL or its an error.
dea31012005-04-17 16:05:31 -05009250 */
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009251 if (prspiocbq) {
9252 if (piocb->context2)
9253 return IOCB_ERROR;
9254 piocb->context2 = prspiocbq;
dea31012005-04-17 16:05:31 -05009255 }
9256
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009257 piocb->iocb_cmpl = lpfc_sli_wake_iocb_wait;
9258 piocb->context_un.wait_queue = &done_q;
9259 piocb->iocb_flag &= ~LPFC_IO_WAKE;
dea31012005-04-17 16:05:31 -05009260
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05009261 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
James Smart9940b972011-03-11 16:06:12 -05009262 if (lpfc_readl(phba->HCregaddr, &creg_val))
9263 return IOCB_ERROR;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05009264 creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
9265 writel(creg_val, phba->HCregaddr);
9266 readl(phba->HCregaddr); /* flush */
9267 }
9268
James Smart2a9bf3d2010-06-07 15:24:45 -04009269 retval = lpfc_sli_issue_iocb(phba, ring_number, piocb,
9270 SLI_IOCB_RET_IOCB);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009271 if (retval == IOCB_SUCCESS) {
9272 timeout_req = timeout * HZ;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009273 timeleft = wait_event_timeout(done_q,
James Smartd11e31d2009-06-10 17:23:06 -04009274 lpfc_chk_iocb_flg(phba, piocb, LPFC_IO_WAKE),
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009275 timeout_req);
dea31012005-04-17 16:05:31 -05009276
James Smart7054a602007-04-25 09:52:34 -04009277 if (piocb->iocb_flag & LPFC_IO_WAKE) {
9278 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04009279 "0331 IOCB wake signaled\n");
James Smart7054a602007-04-25 09:52:34 -04009280 } else if (timeleft == 0) {
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009281 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04009282 "0338 IOCB wait timeout error - no "
9283 "wake response Data x%x\n", timeout);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009284 retval = IOCB_TIMEDOUT;
James Smart7054a602007-04-25 09:52:34 -04009285 } else {
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009286 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04009287 "0330 IOCB wake NOT set, "
9288 "Data x%x x%lx\n",
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009289 timeout, (timeleft / jiffies));
9290 retval = IOCB_TIMEDOUT;
dea31012005-04-17 16:05:31 -05009291 }
James Smart2a9bf3d2010-06-07 15:24:45 -04009292 } else if (retval == IOCB_BUSY) {
9293 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
9294 "2818 Max IOCBs %d txq cnt %d txcmplq cnt %d\n",
9295 phba->iocb_cnt, pring->txq_cnt, pring->txcmplq_cnt);
9296 return retval;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009297 } else {
9298 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smartd7c255b2008-08-24 21:50:00 -04009299 "0332 IOCB wait issue failed, Data x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04009300 retval);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009301 retval = IOCB_ERROR;
dea31012005-04-17 16:05:31 -05009302 }
9303
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05009304 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
James Smart9940b972011-03-11 16:06:12 -05009305 if (lpfc_readl(phba->HCregaddr, &creg_val))
9306 return IOCB_ERROR;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05009307 creg_val &= ~(HC_R0INT_ENA << LPFC_FCP_RING);
9308 writel(creg_val, phba->HCregaddr);
9309 readl(phba->HCregaddr); /* flush */
9310 }
9311
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009312 if (prspiocbq)
9313 piocb->context2 = NULL;
9314
9315 piocb->context_un.wait_queue = NULL;
9316 piocb->iocb_cmpl = NULL;
dea31012005-04-17 16:05:31 -05009317 return retval;
9318}
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009319
James Smarte59058c2008-08-24 21:49:00 -04009320/**
James Smart3621a712009-04-06 18:47:14 -04009321 * lpfc_sli_issue_mbox_wait - Synchronous function to issue mailbox
James Smarte59058c2008-08-24 21:49:00 -04009322 * @phba: Pointer to HBA context object.
9323 * @pmboxq: Pointer to driver mailbox object.
9324 * @timeout: Timeout in number of seconds.
9325 *
9326 * This function issues the mailbox to firmware and waits for the
9327 * mailbox command to complete. If the mailbox command is not
9328 * completed within timeout seconds, it returns MBX_TIMEOUT.
9329 * The function waits for the mailbox completion using an
9330 * interruptible wait. If the thread is woken up due to a
9331 * signal, MBX_TIMEOUT error is returned to the caller. Caller
9332 * should not free the mailbox resources, if this function returns
9333 * MBX_TIMEOUT.
9334 * This function will sleep while waiting for mailbox completion.
9335 * So, this function should not be called from any context which
9336 * does not allow sleeping. Due to the same reason, this function
9337 * cannot be called with interrupt disabled.
9338 * This function assumes that the mailbox completion occurs while
9339 * this function sleep. So, this function cannot be called from
9340 * the worker thread which processes mailbox completion.
9341 * This function is called in the context of HBA management
9342 * applications.
9343 * This function returns MBX_SUCCESS when successful.
9344 * This function is called with no lock held.
9345 **/
dea31012005-04-17 16:05:31 -05009346int
James Smart2e0fef82007-06-17 19:56:36 -05009347lpfc_sli_issue_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq,
dea31012005-04-17 16:05:31 -05009348 uint32_t timeout)
9349{
Peter Zijlstra7259f0d2006-10-29 22:46:36 -08009350 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
dea31012005-04-17 16:05:31 -05009351 int retval;
James Smart858c9f62007-06-17 19:56:39 -05009352 unsigned long flag;
dea31012005-04-17 16:05:31 -05009353
9354 /* The caller must leave context1 empty. */
James Smart98c9ea52007-10-27 13:37:33 -04009355 if (pmboxq->context1)
James Smart2e0fef82007-06-17 19:56:36 -05009356 return MBX_NOT_FINISHED;
dea31012005-04-17 16:05:31 -05009357
James Smart495a7142008-06-14 22:52:59 -04009358 pmboxq->mbox_flag &= ~LPFC_MBX_WAKE;
dea31012005-04-17 16:05:31 -05009359 /* setup wake call as IOCB callback */
9360 pmboxq->mbox_cmpl = lpfc_sli_wake_mbox_wait;
9361 /* setup context field to pass wait_queue pointer to wake function */
9362 pmboxq->context1 = &done_q;
9363
dea31012005-04-17 16:05:31 -05009364 /* now issue the command */
9365 retval = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
9366
9367 if (retval == MBX_BUSY || retval == MBX_SUCCESS) {
James Smart7054a602007-04-25 09:52:34 -04009368 wait_event_interruptible_timeout(done_q,
9369 pmboxq->mbox_flag & LPFC_MBX_WAKE,
9370 timeout * HZ);
9371
James Smart858c9f62007-06-17 19:56:39 -05009372 spin_lock_irqsave(&phba->hbalock, flag);
dea31012005-04-17 16:05:31 -05009373 pmboxq->context1 = NULL;
James Smart7054a602007-04-25 09:52:34 -04009374 /*
9375 * if LPFC_MBX_WAKE flag is set the mailbox is completed
9376 * else do not free the resources.
9377 */
James Smartd7c47992010-06-08 18:31:54 -04009378 if (pmboxq->mbox_flag & LPFC_MBX_WAKE) {
dea31012005-04-17 16:05:31 -05009379 retval = MBX_SUCCESS;
James Smartd7c47992010-06-08 18:31:54 -04009380 lpfc_sli4_swap_str(phba, pmboxq);
9381 } else {
James Smart7054a602007-04-25 09:52:34 -04009382 retval = MBX_TIMEOUT;
James Smart858c9f62007-06-17 19:56:39 -05009383 pmboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
9384 }
9385 spin_unlock_irqrestore(&phba->hbalock, flag);
dea31012005-04-17 16:05:31 -05009386 }
9387
dea31012005-04-17 16:05:31 -05009388 return retval;
9389}
9390
James Smarte59058c2008-08-24 21:49:00 -04009391/**
James Smart3772a992009-05-22 14:50:54 -04009392 * lpfc_sli_mbox_sys_shutdown - shutdown mailbox command sub-system
James Smarte59058c2008-08-24 21:49:00 -04009393 * @phba: Pointer to HBA context.
9394 *
James Smart3772a992009-05-22 14:50:54 -04009395 * This function is called to shutdown the driver's mailbox sub-system.
9396 * It first marks the mailbox sub-system is in a block state to prevent
9397 * the asynchronous mailbox command from issued off the pending mailbox
9398 * command queue. If the mailbox command sub-system shutdown is due to
9399 * HBA error conditions such as EEH or ERATT, this routine shall invoke
9400 * the mailbox sub-system flush routine to forcefully bring down the
9401 * mailbox sub-system. Otherwise, if it is due to normal condition (such
9402 * as with offline or HBA function reset), this routine will wait for the
9403 * outstanding mailbox command to complete before invoking the mailbox
9404 * sub-system flush routine to gracefully bring down mailbox sub-system.
James Smarte59058c2008-08-24 21:49:00 -04009405 **/
James Smart3772a992009-05-22 14:50:54 -04009406void
9407lpfc_sli_mbox_sys_shutdown(struct lpfc_hba *phba)
James Smartb4c02652006-07-06 15:50:43 -04009408{
James Smart3772a992009-05-22 14:50:54 -04009409 struct lpfc_sli *psli = &phba->sli;
9410 uint8_t actcmd = MBX_HEARTBEAT;
9411 unsigned long timeout;
9412
9413 spin_lock_irq(&phba->hbalock);
9414 psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
9415 spin_unlock_irq(&phba->hbalock);
9416
9417 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
9418 spin_lock_irq(&phba->hbalock);
9419 if (phba->sli.mbox_active)
9420 actcmd = phba->sli.mbox_active->u.mb.mbxCommand;
9421 spin_unlock_irq(&phba->hbalock);
9422 /* Determine how long we might wait for the active mailbox
9423 * command to be gracefully completed by firmware.
9424 */
9425 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, actcmd) *
9426 1000) + jiffies;
9427 while (phba->sli.mbox_active) {
9428 /* Check active mailbox complete status every 2ms */
9429 msleep(2);
9430 if (time_after(jiffies, timeout))
9431 /* Timeout, let the mailbox flush routine to
9432 * forcefully release active mailbox command
9433 */
9434 break;
9435 }
9436 }
9437 lpfc_sli_mbox_sys_flush(phba);
9438}
9439
9440/**
9441 * lpfc_sli_eratt_read - read sli-3 error attention events
9442 * @phba: Pointer to HBA context.
9443 *
9444 * This function is called to read the SLI3 device error attention registers
9445 * for possible error attention events. The caller must hold the hostlock
9446 * with spin_lock_irq().
9447 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03009448 * This function returns 1 when there is Error Attention in the Host Attention
James Smart3772a992009-05-22 14:50:54 -04009449 * Register and returns 0 otherwise.
9450 **/
9451static int
9452lpfc_sli_eratt_read(struct lpfc_hba *phba)
9453{
James Smarted957682007-06-17 19:56:37 -05009454 uint32_t ha_copy;
James Smartb4c02652006-07-06 15:50:43 -04009455
James Smart3772a992009-05-22 14:50:54 -04009456 /* Read chip Host Attention (HA) register */
James Smart9940b972011-03-11 16:06:12 -05009457 if (lpfc_readl(phba->HAregaddr, &ha_copy))
9458 goto unplug_err;
9459
James Smart3772a992009-05-22 14:50:54 -04009460 if (ha_copy & HA_ERATT) {
9461 /* Read host status register to retrieve error event */
James Smart9940b972011-03-11 16:06:12 -05009462 if (lpfc_sli_read_hs(phba))
9463 goto unplug_err;
James Smartb4c02652006-07-06 15:50:43 -04009464
James Smart3772a992009-05-22 14:50:54 -04009465 /* Check if there is a deferred error condition is active */
9466 if ((HS_FFER1 & phba->work_hs) &&
9467 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
James Smartdcf2a4e2010-09-29 11:18:53 -04009468 HS_FFER6 | HS_FFER7 | HS_FFER8) & phba->work_hs)) {
James Smart3772a992009-05-22 14:50:54 -04009469 phba->hba_flag |= DEFER_ERATT;
James Smart3772a992009-05-22 14:50:54 -04009470 /* Clear all interrupt enable conditions */
9471 writel(0, phba->HCregaddr);
9472 readl(phba->HCregaddr);
9473 }
9474
9475 /* Set the driver HA work bitmap */
James Smart3772a992009-05-22 14:50:54 -04009476 phba->work_ha |= HA_ERATT;
9477 /* Indicate polling handles this ERATT */
9478 phba->hba_flag |= HBA_ERATT_HANDLED;
James Smart3772a992009-05-22 14:50:54 -04009479 return 1;
James Smartb4c02652006-07-06 15:50:43 -04009480 }
James Smart3772a992009-05-22 14:50:54 -04009481 return 0;
James Smart9940b972011-03-11 16:06:12 -05009482
9483unplug_err:
9484 /* Set the driver HS work bitmap */
9485 phba->work_hs |= UNPLUG_ERR;
9486 /* Set the driver HA work bitmap */
9487 phba->work_ha |= HA_ERATT;
9488 /* Indicate polling handles this ERATT */
9489 phba->hba_flag |= HBA_ERATT_HANDLED;
9490 return 1;
James Smartb4c02652006-07-06 15:50:43 -04009491}
9492
James Smarte59058c2008-08-24 21:49:00 -04009493/**
James Smartda0436e2009-05-22 14:51:39 -04009494 * lpfc_sli4_eratt_read - read sli-4 error attention events
9495 * @phba: Pointer to HBA context.
9496 *
9497 * This function is called to read the SLI4 device error attention registers
9498 * for possible error attention events. The caller must hold the hostlock
9499 * with spin_lock_irq().
9500 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03009501 * This function returns 1 when there is Error Attention in the Host Attention
James Smartda0436e2009-05-22 14:51:39 -04009502 * Register and returns 0 otherwise.
9503 **/
9504static int
9505lpfc_sli4_eratt_read(struct lpfc_hba *phba)
9506{
9507 uint32_t uerr_sta_hi, uerr_sta_lo;
James Smart2fcee4b2010-12-15 17:57:46 -05009508 uint32_t if_type, portsmphr;
9509 struct lpfc_register portstat_reg;
James Smartda0436e2009-05-22 14:51:39 -04009510
James Smart2fcee4b2010-12-15 17:57:46 -05009511 /*
9512 * For now, use the SLI4 device internal unrecoverable error
James Smartda0436e2009-05-22 14:51:39 -04009513 * registers for error attention. This can be changed later.
9514 */
James Smart2fcee4b2010-12-15 17:57:46 -05009515 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
9516 switch (if_type) {
9517 case LPFC_SLI_INTF_IF_TYPE_0:
James Smart9940b972011-03-11 16:06:12 -05009518 if (lpfc_readl(phba->sli4_hba.u.if_type0.UERRLOregaddr,
9519 &uerr_sta_lo) ||
9520 lpfc_readl(phba->sli4_hba.u.if_type0.UERRHIregaddr,
9521 &uerr_sta_hi)) {
9522 phba->work_hs |= UNPLUG_ERR;
9523 phba->work_ha |= HA_ERATT;
9524 phba->hba_flag |= HBA_ERATT_HANDLED;
9525 return 1;
9526 }
James Smart2fcee4b2010-12-15 17:57:46 -05009527 if ((~phba->sli4_hba.ue_mask_lo & uerr_sta_lo) ||
9528 (~phba->sli4_hba.ue_mask_hi & uerr_sta_hi)) {
9529 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9530 "1423 HBA Unrecoverable error: "
9531 "uerr_lo_reg=0x%x, uerr_hi_reg=0x%x, "
9532 "ue_mask_lo_reg=0x%x, "
9533 "ue_mask_hi_reg=0x%x\n",
9534 uerr_sta_lo, uerr_sta_hi,
9535 phba->sli4_hba.ue_mask_lo,
9536 phba->sli4_hba.ue_mask_hi);
9537 phba->work_status[0] = uerr_sta_lo;
9538 phba->work_status[1] = uerr_sta_hi;
9539 phba->work_ha |= HA_ERATT;
9540 phba->hba_flag |= HBA_ERATT_HANDLED;
9541 return 1;
9542 }
9543 break;
9544 case LPFC_SLI_INTF_IF_TYPE_2:
James Smart9940b972011-03-11 16:06:12 -05009545 if (lpfc_readl(phba->sli4_hba.u.if_type2.STATUSregaddr,
9546 &portstat_reg.word0) ||
9547 lpfc_readl(phba->sli4_hba.PSMPHRregaddr,
9548 &portsmphr)){
9549 phba->work_hs |= UNPLUG_ERR;
9550 phba->work_ha |= HA_ERATT;
9551 phba->hba_flag |= HBA_ERATT_HANDLED;
9552 return 1;
9553 }
James Smart2fcee4b2010-12-15 17:57:46 -05009554 if (bf_get(lpfc_sliport_status_err, &portstat_reg)) {
9555 phba->work_status[0] =
9556 readl(phba->sli4_hba.u.if_type2.ERR1regaddr);
9557 phba->work_status[1] =
9558 readl(phba->sli4_hba.u.if_type2.ERR2regaddr);
9559 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9560 "2885 Port Error Detected: "
9561 "port status reg 0x%x, "
9562 "port smphr reg 0x%x, "
9563 "error 1=0x%x, error 2=0x%x\n",
9564 portstat_reg.word0,
9565 portsmphr,
9566 phba->work_status[0],
9567 phba->work_status[1]);
9568 phba->work_ha |= HA_ERATT;
9569 phba->hba_flag |= HBA_ERATT_HANDLED;
9570 return 1;
9571 }
9572 break;
9573 case LPFC_SLI_INTF_IF_TYPE_1:
9574 default:
James Smarta747c9c2009-11-18 15:41:10 -05009575 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart2fcee4b2010-12-15 17:57:46 -05009576 "2886 HBA Error Attention on unsupported "
9577 "if type %d.", if_type);
James Smarta747c9c2009-11-18 15:41:10 -05009578 return 1;
James Smartda0436e2009-05-22 14:51:39 -04009579 }
James Smart2fcee4b2010-12-15 17:57:46 -05009580
James Smartda0436e2009-05-22 14:51:39 -04009581 return 0;
9582}
9583
9584/**
James Smart3621a712009-04-06 18:47:14 -04009585 * lpfc_sli_check_eratt - check error attention events
James Smart93996272008-08-24 21:50:30 -04009586 * @phba: Pointer to HBA context.
9587 *
James Smart3772a992009-05-22 14:50:54 -04009588 * This function is called from timer soft interrupt context to check HBA's
James Smart93996272008-08-24 21:50:30 -04009589 * error attention register bit for error attention events.
9590 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03009591 * This function returns 1 when there is Error Attention in the Host Attention
James Smart93996272008-08-24 21:50:30 -04009592 * Register and returns 0 otherwise.
9593 **/
9594int
9595lpfc_sli_check_eratt(struct lpfc_hba *phba)
9596{
9597 uint32_t ha_copy;
9598
9599 /* If somebody is waiting to handle an eratt, don't process it
9600 * here. The brdkill function will do this.
9601 */
9602 if (phba->link_flag & LS_IGNORE_ERATT)
9603 return 0;
9604
9605 /* Check if interrupt handler handles this ERATT */
9606 spin_lock_irq(&phba->hbalock);
9607 if (phba->hba_flag & HBA_ERATT_HANDLED) {
9608 /* Interrupt handler has handled ERATT */
9609 spin_unlock_irq(&phba->hbalock);
9610 return 0;
9611 }
9612
James Smarta257bf92009-04-06 18:48:10 -04009613 /*
9614 * If there is deferred error attention, do not check for error
9615 * attention
9616 */
9617 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
9618 spin_unlock_irq(&phba->hbalock);
9619 return 0;
9620 }
9621
James Smart3772a992009-05-22 14:50:54 -04009622 /* If PCI channel is offline, don't process it */
9623 if (unlikely(pci_channel_offline(phba->pcidev))) {
James Smart93996272008-08-24 21:50:30 -04009624 spin_unlock_irq(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -04009625 return 0;
9626 }
9627
9628 switch (phba->sli_rev) {
9629 case LPFC_SLI_REV2:
9630 case LPFC_SLI_REV3:
9631 /* Read chip Host Attention (HA) register */
9632 ha_copy = lpfc_sli_eratt_read(phba);
9633 break;
James Smartda0436e2009-05-22 14:51:39 -04009634 case LPFC_SLI_REV4:
James Smart2fcee4b2010-12-15 17:57:46 -05009635 /* Read device Uncoverable Error (UERR) registers */
James Smartda0436e2009-05-22 14:51:39 -04009636 ha_copy = lpfc_sli4_eratt_read(phba);
9637 break;
James Smart3772a992009-05-22 14:50:54 -04009638 default:
9639 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9640 "0299 Invalid SLI revision (%d)\n",
9641 phba->sli_rev);
9642 ha_copy = 0;
9643 break;
James Smart93996272008-08-24 21:50:30 -04009644 }
9645 spin_unlock_irq(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -04009646
9647 return ha_copy;
9648}
9649
9650/**
9651 * lpfc_intr_state_check - Check device state for interrupt handling
9652 * @phba: Pointer to HBA context.
9653 *
9654 * This inline routine checks whether a device or its PCI slot is in a state
9655 * that the interrupt should be handled.
9656 *
9657 * This function returns 0 if the device or the PCI slot is in a state that
9658 * interrupt should be handled, otherwise -EIO.
9659 */
9660static inline int
9661lpfc_intr_state_check(struct lpfc_hba *phba)
9662{
9663 /* If the pci channel is offline, ignore all the interrupts */
9664 if (unlikely(pci_channel_offline(phba->pcidev)))
9665 return -EIO;
9666
9667 /* Update device level interrupt statistics */
9668 phba->sli.slistat.sli_intr++;
9669
9670 /* Ignore all interrupts during initialization. */
9671 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
9672 return -EIO;
9673
James Smart93996272008-08-24 21:50:30 -04009674 return 0;
9675}
9676
9677/**
James Smart3772a992009-05-22 14:50:54 -04009678 * lpfc_sli_sp_intr_handler - Slow-path interrupt handler to SLI-3 device
James Smarte59058c2008-08-24 21:49:00 -04009679 * @irq: Interrupt number.
9680 * @dev_id: The device context pointer.
9681 *
James Smart93996272008-08-24 21:50:30 -04009682 * This function is directly called from the PCI layer as an interrupt
James Smart3772a992009-05-22 14:50:54 -04009683 * service routine when device with SLI-3 interface spec is enabled with
9684 * MSI-X multi-message interrupt mode and there are slow-path events in
9685 * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
9686 * interrupt mode, this function is called as part of the device-level
9687 * interrupt handler. When the PCI slot is in error recovery or the HBA
9688 * is undergoing initialization, the interrupt handler will not process
9689 * the interrupt. The link attention and ELS ring attention events are
9690 * handled by the worker thread. The interrupt handler signals the worker
9691 * thread and returns for these events. This function is called without
9692 * any lock held. It gets the hbalock to access and update SLI data
James Smart93996272008-08-24 21:50:30 -04009693 * structures.
9694 *
9695 * This function returns IRQ_HANDLED when interrupt is handled else it
9696 * returns IRQ_NONE.
James Smarte59058c2008-08-24 21:49:00 -04009697 **/
dea31012005-04-17 16:05:31 -05009698irqreturn_t
James Smart3772a992009-05-22 14:50:54 -04009699lpfc_sli_sp_intr_handler(int irq, void *dev_id)
dea31012005-04-17 16:05:31 -05009700{
James Smart2e0fef82007-06-17 19:56:36 -05009701 struct lpfc_hba *phba;
James Smarta747c9c2009-11-18 15:41:10 -05009702 uint32_t ha_copy, hc_copy;
dea31012005-04-17 16:05:31 -05009703 uint32_t work_ha_copy;
9704 unsigned long status;
James Smart5b75da22008-12-04 22:39:35 -05009705 unsigned long iflag;
dea31012005-04-17 16:05:31 -05009706 uint32_t control;
9707
James Smart92d7f7b2007-06-17 19:56:38 -05009708 MAILBOX_t *mbox, *pmbox;
James Smart858c9f62007-06-17 19:56:39 -05009709 struct lpfc_vport *vport;
9710 struct lpfc_nodelist *ndlp;
9711 struct lpfc_dmabuf *mp;
James Smart92d7f7b2007-06-17 19:56:38 -05009712 LPFC_MBOXQ_t *pmb;
9713 int rc;
9714
dea31012005-04-17 16:05:31 -05009715 /*
9716 * Get the driver's phba structure from the dev_id and
9717 * assume the HBA is not interrupting.
9718 */
James Smart93996272008-08-24 21:50:30 -04009719 phba = (struct lpfc_hba *)dev_id;
dea31012005-04-17 16:05:31 -05009720
9721 if (unlikely(!phba))
9722 return IRQ_NONE;
9723
dea31012005-04-17 16:05:31 -05009724 /*
James Smart93996272008-08-24 21:50:30 -04009725 * Stuff needs to be attented to when this function is invoked as an
9726 * individual interrupt handler in MSI-X multi-message interrupt mode
dea31012005-04-17 16:05:31 -05009727 */
James Smart93996272008-08-24 21:50:30 -04009728 if (phba->intr_type == MSIX) {
James Smart3772a992009-05-22 14:50:54 -04009729 /* Check device state for handling interrupt */
9730 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -04009731 return IRQ_NONE;
9732 /* Need to read HA REG for slow-path events */
James Smart5b75da22008-12-04 22:39:35 -05009733 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart9940b972011-03-11 16:06:12 -05009734 if (lpfc_readl(phba->HAregaddr, &ha_copy))
9735 goto unplug_error;
James Smart93996272008-08-24 21:50:30 -04009736 /* If somebody is waiting to handle an eratt don't process it
9737 * here. The brdkill function will do this.
9738 */
9739 if (phba->link_flag & LS_IGNORE_ERATT)
9740 ha_copy &= ~HA_ERATT;
9741 /* Check the need for handling ERATT in interrupt handler */
9742 if (ha_copy & HA_ERATT) {
9743 if (phba->hba_flag & HBA_ERATT_HANDLED)
9744 /* ERATT polling has handled ERATT */
9745 ha_copy &= ~HA_ERATT;
9746 else
9747 /* Indicate interrupt handler handles ERATT */
9748 phba->hba_flag |= HBA_ERATT_HANDLED;
9749 }
James Smarta257bf92009-04-06 18:48:10 -04009750
9751 /*
9752 * If there is deferred error attention, do not check for any
9753 * interrupt.
9754 */
9755 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
James Smart3772a992009-05-22 14:50:54 -04009756 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -04009757 return IRQ_NONE;
9758 }
9759
James Smart93996272008-08-24 21:50:30 -04009760 /* Clear up only attention source related to slow-path */
James Smart9940b972011-03-11 16:06:12 -05009761 if (lpfc_readl(phba->HCregaddr, &hc_copy))
9762 goto unplug_error;
9763
James Smarta747c9c2009-11-18 15:41:10 -05009764 writel(hc_copy & ~(HC_MBINT_ENA | HC_R2INT_ENA |
9765 HC_LAINT_ENA | HC_ERINT_ENA),
9766 phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -04009767 writel((ha_copy & (HA_MBATT | HA_R2_CLR_MSK)),
9768 phba->HAregaddr);
James Smarta747c9c2009-11-18 15:41:10 -05009769 writel(hc_copy, phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -04009770 readl(phba->HAregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -05009771 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -04009772 } else
9773 ha_copy = phba->ha_copy;
dea31012005-04-17 16:05:31 -05009774
dea31012005-04-17 16:05:31 -05009775 work_ha_copy = ha_copy & phba->work_ha_mask;
9776
James Smart93996272008-08-24 21:50:30 -04009777 if (work_ha_copy) {
dea31012005-04-17 16:05:31 -05009778 if (work_ha_copy & HA_LATT) {
9779 if (phba->sli.sli_flag & LPFC_PROCESS_LA) {
9780 /*
9781 * Turn off Link Attention interrupts
9782 * until CLEAR_LA done
9783 */
James Smart5b75da22008-12-04 22:39:35 -05009784 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05009785 phba->sli.sli_flag &= ~LPFC_PROCESS_LA;
James Smart9940b972011-03-11 16:06:12 -05009786 if (lpfc_readl(phba->HCregaddr, &control))
9787 goto unplug_error;
dea31012005-04-17 16:05:31 -05009788 control &= ~HC_LAINT_ENA;
9789 writel(control, phba->HCregaddr);
9790 readl(phba->HCregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -05009791 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05009792 }
9793 else
9794 work_ha_copy &= ~HA_LATT;
9795 }
9796
James Smart93996272008-08-24 21:50:30 -04009797 if (work_ha_copy & ~(HA_ERATT | HA_MBATT | HA_LATT)) {
James Smart858c9f62007-06-17 19:56:39 -05009798 /*
9799 * Turn off Slow Rings interrupts, LPFC_ELS_RING is
9800 * the only slow ring.
9801 */
9802 status = (work_ha_copy &
9803 (HA_RXMASK << (4*LPFC_ELS_RING)));
9804 status >>= (4*LPFC_ELS_RING);
9805 if (status & HA_RXMASK) {
James Smart5b75da22008-12-04 22:39:35 -05009806 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart9940b972011-03-11 16:06:12 -05009807 if (lpfc_readl(phba->HCregaddr, &control))
9808 goto unplug_error;
James Smarta58cbd52007-08-02 11:09:43 -04009809
9810 lpfc_debugfs_slow_ring_trc(phba,
9811 "ISR slow ring: ctl:x%x stat:x%x isrcnt:x%x",
9812 control, status,
9813 (uint32_t)phba->sli.slistat.sli_intr);
9814
James Smart858c9f62007-06-17 19:56:39 -05009815 if (control & (HC_R0INT_ENA << LPFC_ELS_RING)) {
James Smarta58cbd52007-08-02 11:09:43 -04009816 lpfc_debugfs_slow_ring_trc(phba,
9817 "ISR Disable ring:"
9818 "pwork:x%x hawork:x%x wait:x%x",
9819 phba->work_ha, work_ha_copy,
9820 (uint32_t)((unsigned long)
James Smart5e9d9b82008-06-14 22:52:53 -04009821 &phba->work_waitq));
James Smarta58cbd52007-08-02 11:09:43 -04009822
James Smart858c9f62007-06-17 19:56:39 -05009823 control &=
9824 ~(HC_R0INT_ENA << LPFC_ELS_RING);
dea31012005-04-17 16:05:31 -05009825 writel(control, phba->HCregaddr);
9826 readl(phba->HCregaddr); /* flush */
dea31012005-04-17 16:05:31 -05009827 }
James Smarta58cbd52007-08-02 11:09:43 -04009828 else {
9829 lpfc_debugfs_slow_ring_trc(phba,
9830 "ISR slow ring: pwork:"
9831 "x%x hawork:x%x wait:x%x",
9832 phba->work_ha, work_ha_copy,
9833 (uint32_t)((unsigned long)
James Smart5e9d9b82008-06-14 22:52:53 -04009834 &phba->work_waitq));
James Smarta58cbd52007-08-02 11:09:43 -04009835 }
James Smart5b75da22008-12-04 22:39:35 -05009836 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05009837 }
9838 }
James Smart5b75da22008-12-04 22:39:35 -05009839 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -04009840 if (work_ha_copy & HA_ERATT) {
James Smart9940b972011-03-11 16:06:12 -05009841 if (lpfc_sli_read_hs(phba))
9842 goto unplug_error;
James Smarta257bf92009-04-06 18:48:10 -04009843 /*
9844 * Check if there is a deferred error condition
9845 * is active
9846 */
9847 if ((HS_FFER1 & phba->work_hs) &&
9848 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
James Smartdcf2a4e2010-09-29 11:18:53 -04009849 HS_FFER6 | HS_FFER7 | HS_FFER8) &
9850 phba->work_hs)) {
James Smarta257bf92009-04-06 18:48:10 -04009851 phba->hba_flag |= DEFER_ERATT;
9852 /* Clear all interrupt enable conditions */
9853 writel(0, phba->HCregaddr);
9854 readl(phba->HCregaddr);
9855 }
9856 }
9857
James Smart93996272008-08-24 21:50:30 -04009858 if ((work_ha_copy & HA_MBATT) && (phba->sli.mbox_active)) {
James Smart92d7f7b2007-06-17 19:56:38 -05009859 pmb = phba->sli.mbox_active;
James Smart04c68492009-05-22 14:52:52 -04009860 pmbox = &pmb->u.mb;
James Smart34b02dc2008-08-24 21:49:55 -04009861 mbox = phba->mbox;
James Smart858c9f62007-06-17 19:56:39 -05009862 vport = pmb->vport;
James Smart92d7f7b2007-06-17 19:56:38 -05009863
9864 /* First check out the status word */
9865 lpfc_sli_pcimem_bcopy(mbox, pmbox, sizeof(uint32_t));
9866 if (pmbox->mbxOwner != OWN_HOST) {
James Smart5b75da22008-12-04 22:39:35 -05009867 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart92d7f7b2007-06-17 19:56:38 -05009868 /*
9869 * Stray Mailbox Interrupt, mbxCommand <cmd>
9870 * mbxStatus <status>
9871 */
James Smart09372822008-01-11 01:52:54 -05009872 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
James Smart92d7f7b2007-06-17 19:56:38 -05009873 LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04009874 "(%d):0304 Stray Mailbox "
James Smart92d7f7b2007-06-17 19:56:38 -05009875 "Interrupt mbxCommand x%x "
9876 "mbxStatus x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04009877 (vport ? vport->vpi : 0),
James Smart92d7f7b2007-06-17 19:56:38 -05009878 pmbox->mbxCommand,
9879 pmbox->mbxStatus);
James Smart09372822008-01-11 01:52:54 -05009880 /* clear mailbox attention bit */
9881 work_ha_copy &= ~HA_MBATT;
9882 } else {
James Smart97eab632008-04-07 10:16:05 -04009883 phba->sli.mbox_active = NULL;
James Smart5b75da22008-12-04 22:39:35 -05009884 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart09372822008-01-11 01:52:54 -05009885 phba->last_completion_time = jiffies;
9886 del_timer(&phba->sli.mbox_tmo);
James Smart09372822008-01-11 01:52:54 -05009887 if (pmb->mbox_cmpl) {
9888 lpfc_sli_pcimem_bcopy(mbox, pmbox,
9889 MAILBOX_CMD_SIZE);
James Smart7a470272010-03-15 11:25:20 -04009890 if (pmb->out_ext_byte_len &&
9891 pmb->context2)
9892 lpfc_sli_pcimem_bcopy(
9893 phba->mbox_ext,
9894 pmb->context2,
9895 pmb->out_ext_byte_len);
James Smart858c9f62007-06-17 19:56:39 -05009896 }
James Smart09372822008-01-11 01:52:54 -05009897 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
9898 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
9899
9900 lpfc_debugfs_disc_trc(vport,
9901 LPFC_DISC_TRC_MBOX_VPORT,
9902 "MBOX dflt rpi: : "
9903 "status:x%x rpi:x%x",
9904 (uint32_t)pmbox->mbxStatus,
9905 pmbox->un.varWords[0], 0);
9906
9907 if (!pmbox->mbxStatus) {
9908 mp = (struct lpfc_dmabuf *)
9909 (pmb->context1);
9910 ndlp = (struct lpfc_nodelist *)
9911 pmb->context2;
9912
9913 /* Reg_LOGIN of dflt RPI was
9914 * successful. new lets get
9915 * rid of the RPI using the
9916 * same mbox buffer.
9917 */
9918 lpfc_unreg_login(phba,
9919 vport->vpi,
9920 pmbox->un.varWords[0],
9921 pmb);
9922 pmb->mbox_cmpl =
9923 lpfc_mbx_cmpl_dflt_rpi;
9924 pmb->context1 = mp;
9925 pmb->context2 = ndlp;
9926 pmb->vport = vport;
James Smart58da1ff2008-04-07 10:15:56 -04009927 rc = lpfc_sli_issue_mbox(phba,
9928 pmb,
9929 MBX_NOWAIT);
9930 if (rc != MBX_BUSY)
9931 lpfc_printf_log(phba,
9932 KERN_ERR,
9933 LOG_MBOX | LOG_SLI,
James Smartd7c255b2008-08-24 21:50:00 -04009934 "0350 rc should have"
James Smart6a9c52c2009-10-02 15:16:51 -04009935 "been MBX_BUSY\n");
James Smart3772a992009-05-22 14:50:54 -04009936 if (rc != MBX_NOT_FINISHED)
9937 goto send_current_mbox;
James Smart09372822008-01-11 01:52:54 -05009938 }
9939 }
James Smart5b75da22008-12-04 22:39:35 -05009940 spin_lock_irqsave(
9941 &phba->pport->work_port_lock,
9942 iflag);
James Smart09372822008-01-11 01:52:54 -05009943 phba->pport->work_port_events &=
9944 ~WORKER_MBOX_TMO;
James Smart5b75da22008-12-04 22:39:35 -05009945 spin_unlock_irqrestore(
9946 &phba->pport->work_port_lock,
9947 iflag);
James Smart09372822008-01-11 01:52:54 -05009948 lpfc_mbox_cmpl_put(phba, pmb);
James Smart858c9f62007-06-17 19:56:39 -05009949 }
James Smart97eab632008-04-07 10:16:05 -04009950 } else
James Smart5b75da22008-12-04 22:39:35 -05009951 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -04009952
James Smart92d7f7b2007-06-17 19:56:38 -05009953 if ((work_ha_copy & HA_MBATT) &&
9954 (phba->sli.mbox_active == NULL)) {
James Smart858c9f62007-06-17 19:56:39 -05009955send_current_mbox:
James Smart92d7f7b2007-06-17 19:56:38 -05009956 /* Process next mailbox command if there is one */
James Smart58da1ff2008-04-07 10:15:56 -04009957 do {
9958 rc = lpfc_sli_issue_mbox(phba, NULL,
9959 MBX_NOWAIT);
9960 } while (rc == MBX_NOT_FINISHED);
9961 if (rc != MBX_SUCCESS)
9962 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
9963 LOG_SLI, "0349 rc should be "
James Smart6a9c52c2009-10-02 15:16:51 -04009964 "MBX_SUCCESS\n");
James Smart92d7f7b2007-06-17 19:56:38 -05009965 }
9966
James Smart5b75da22008-12-04 22:39:35 -05009967 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05009968 phba->work_ha |= work_ha_copy;
James Smart5b75da22008-12-04 22:39:35 -05009969 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart5e9d9b82008-06-14 22:52:53 -04009970 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -05009971 }
James Smart93996272008-08-24 21:50:30 -04009972 return IRQ_HANDLED;
James Smart9940b972011-03-11 16:06:12 -05009973unplug_error:
9974 spin_unlock_irqrestore(&phba->hbalock, iflag);
9975 return IRQ_HANDLED;
dea31012005-04-17 16:05:31 -05009976
James Smart3772a992009-05-22 14:50:54 -04009977} /* lpfc_sli_sp_intr_handler */
James Smart93996272008-08-24 21:50:30 -04009978
9979/**
James Smart3772a992009-05-22 14:50:54 -04009980 * lpfc_sli_fp_intr_handler - Fast-path interrupt handler to SLI-3 device.
James Smart93996272008-08-24 21:50:30 -04009981 * @irq: Interrupt number.
9982 * @dev_id: The device context pointer.
9983 *
9984 * This function is directly called from the PCI layer as an interrupt
James Smart3772a992009-05-22 14:50:54 -04009985 * service routine when device with SLI-3 interface spec is enabled with
9986 * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
9987 * ring event in the HBA. However, when the device is enabled with either
9988 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
9989 * device-level interrupt handler. When the PCI slot is in error recovery
9990 * or the HBA is undergoing initialization, the interrupt handler will not
9991 * process the interrupt. The SCSI FCP fast-path ring event are handled in
9992 * the intrrupt context. This function is called without any lock held.
9993 * It gets the hbalock to access and update SLI data structures.
James Smart93996272008-08-24 21:50:30 -04009994 *
9995 * This function returns IRQ_HANDLED when interrupt is handled else it
9996 * returns IRQ_NONE.
9997 **/
9998irqreturn_t
James Smart3772a992009-05-22 14:50:54 -04009999lpfc_sli_fp_intr_handler(int irq, void *dev_id)
James Smart93996272008-08-24 21:50:30 -040010000{
10001 struct lpfc_hba *phba;
10002 uint32_t ha_copy;
10003 unsigned long status;
James Smart5b75da22008-12-04 22:39:35 -050010004 unsigned long iflag;
James Smart93996272008-08-24 21:50:30 -040010005
10006 /* Get the driver's phba structure from the dev_id and
10007 * assume the HBA is not interrupting.
10008 */
10009 phba = (struct lpfc_hba *) dev_id;
10010
10011 if (unlikely(!phba))
10012 return IRQ_NONE;
dea31012005-04-17 16:05:31 -050010013
10014 /*
James Smart93996272008-08-24 21:50:30 -040010015 * Stuff needs to be attented to when this function is invoked as an
10016 * individual interrupt handler in MSI-X multi-message interrupt mode
dea31012005-04-17 16:05:31 -050010017 */
James Smart93996272008-08-24 21:50:30 -040010018 if (phba->intr_type == MSIX) {
James Smart3772a992009-05-22 14:50:54 -040010019 /* Check device state for handling interrupt */
10020 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -040010021 return IRQ_NONE;
10022 /* Need to read HA REG for FCP ring and other ring events */
James Smart9940b972011-03-11 16:06:12 -050010023 if (lpfc_readl(phba->HAregaddr, &ha_copy))
10024 return IRQ_HANDLED;
James Smart93996272008-08-24 21:50:30 -040010025 /* Clear up only attention source related to fast-path */
James Smart5b75da22008-12-04 22:39:35 -050010026 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -040010027 /*
10028 * If there is deferred error attention, do not check for
10029 * any interrupt.
10030 */
10031 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
James Smart3772a992009-05-22 14:50:54 -040010032 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -040010033 return IRQ_NONE;
10034 }
James Smart93996272008-08-24 21:50:30 -040010035 writel((ha_copy & (HA_R0_CLR_MSK | HA_R1_CLR_MSK)),
10036 phba->HAregaddr);
10037 readl(phba->HAregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -050010038 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -040010039 } else
10040 ha_copy = phba->ha_copy;
10041
10042 /*
10043 * Process all events on FCP ring. Take the optimized path for FCP IO.
10044 */
10045 ha_copy &= ~(phba->work_ha_mask);
10046
10047 status = (ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
dea31012005-04-17 16:05:31 -050010048 status >>= (4*LPFC_FCP_RING);
James Smart858c9f62007-06-17 19:56:39 -050010049 if (status & HA_RXMASK)
dea31012005-04-17 16:05:31 -050010050 lpfc_sli_handle_fast_ring_event(phba,
10051 &phba->sli.ring[LPFC_FCP_RING],
10052 status);
James Smarta4bc3372006-12-02 13:34:16 -050010053
10054 if (phba->cfg_multi_ring_support == 2) {
10055 /*
James Smart93996272008-08-24 21:50:30 -040010056 * Process all events on extra ring. Take the optimized path
10057 * for extra ring IO.
James Smarta4bc3372006-12-02 13:34:16 -050010058 */
James Smart93996272008-08-24 21:50:30 -040010059 status = (ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
James Smarta4bc3372006-12-02 13:34:16 -050010060 status >>= (4*LPFC_EXTRA_RING);
James Smart858c9f62007-06-17 19:56:39 -050010061 if (status & HA_RXMASK) {
James Smarta4bc3372006-12-02 13:34:16 -050010062 lpfc_sli_handle_fast_ring_event(phba,
10063 &phba->sli.ring[LPFC_EXTRA_RING],
10064 status);
10065 }
10066 }
dea31012005-04-17 16:05:31 -050010067 return IRQ_HANDLED;
James Smart3772a992009-05-22 14:50:54 -040010068} /* lpfc_sli_fp_intr_handler */
dea31012005-04-17 16:05:31 -050010069
James Smart93996272008-08-24 21:50:30 -040010070/**
James Smart3772a992009-05-22 14:50:54 -040010071 * lpfc_sli_intr_handler - Device-level interrupt handler to SLI-3 device
James Smart93996272008-08-24 21:50:30 -040010072 * @irq: Interrupt number.
10073 * @dev_id: The device context pointer.
10074 *
James Smart3772a992009-05-22 14:50:54 -040010075 * This function is the HBA device-level interrupt handler to device with
10076 * SLI-3 interface spec, called from the PCI layer when either MSI or
10077 * Pin-IRQ interrupt mode is enabled and there is an event in the HBA which
10078 * requires driver attention. This function invokes the slow-path interrupt
10079 * attention handling function and fast-path interrupt attention handling
10080 * function in turn to process the relevant HBA attention events. This
10081 * function is called without any lock held. It gets the hbalock to access
10082 * and update SLI data structures.
James Smart93996272008-08-24 21:50:30 -040010083 *
10084 * This function returns IRQ_HANDLED when interrupt is handled, else it
10085 * returns IRQ_NONE.
10086 **/
10087irqreturn_t
James Smart3772a992009-05-22 14:50:54 -040010088lpfc_sli_intr_handler(int irq, void *dev_id)
James Smart93996272008-08-24 21:50:30 -040010089{
10090 struct lpfc_hba *phba;
10091 irqreturn_t sp_irq_rc, fp_irq_rc;
10092 unsigned long status1, status2;
James Smarta747c9c2009-11-18 15:41:10 -050010093 uint32_t hc_copy;
James Smart93996272008-08-24 21:50:30 -040010094
10095 /*
10096 * Get the driver's phba structure from the dev_id and
10097 * assume the HBA is not interrupting.
10098 */
10099 phba = (struct lpfc_hba *) dev_id;
10100
10101 if (unlikely(!phba))
10102 return IRQ_NONE;
10103
James Smart3772a992009-05-22 14:50:54 -040010104 /* Check device state for handling interrupt */
10105 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -040010106 return IRQ_NONE;
10107
10108 spin_lock(&phba->hbalock);
James Smart9940b972011-03-11 16:06:12 -050010109 if (lpfc_readl(phba->HAregaddr, &phba->ha_copy)) {
10110 spin_unlock(&phba->hbalock);
10111 return IRQ_HANDLED;
10112 }
10113
James Smart93996272008-08-24 21:50:30 -040010114 if (unlikely(!phba->ha_copy)) {
10115 spin_unlock(&phba->hbalock);
10116 return IRQ_NONE;
10117 } else if (phba->ha_copy & HA_ERATT) {
10118 if (phba->hba_flag & HBA_ERATT_HANDLED)
10119 /* ERATT polling has handled ERATT */
10120 phba->ha_copy &= ~HA_ERATT;
10121 else
10122 /* Indicate interrupt handler handles ERATT */
10123 phba->hba_flag |= HBA_ERATT_HANDLED;
10124 }
10125
James Smarta257bf92009-04-06 18:48:10 -040010126 /*
10127 * If there is deferred error attention, do not check for any interrupt.
10128 */
10129 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
Dan Carpenterec21b3b2010-08-08 00:15:17 +020010130 spin_unlock(&phba->hbalock);
James Smarta257bf92009-04-06 18:48:10 -040010131 return IRQ_NONE;
10132 }
10133
James Smart93996272008-08-24 21:50:30 -040010134 /* Clear attention sources except link and error attentions */
James Smart9940b972011-03-11 16:06:12 -050010135 if (lpfc_readl(phba->HCregaddr, &hc_copy)) {
10136 spin_unlock(&phba->hbalock);
10137 return IRQ_HANDLED;
10138 }
James Smarta747c9c2009-11-18 15:41:10 -050010139 writel(hc_copy & ~(HC_MBINT_ENA | HC_R0INT_ENA | HC_R1INT_ENA
10140 | HC_R2INT_ENA | HC_LAINT_ENA | HC_ERINT_ENA),
10141 phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -040010142 writel((phba->ha_copy & ~(HA_LATT | HA_ERATT)), phba->HAregaddr);
James Smarta747c9c2009-11-18 15:41:10 -050010143 writel(hc_copy, phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -040010144 readl(phba->HAregaddr); /* flush */
10145 spin_unlock(&phba->hbalock);
10146
10147 /*
10148 * Invokes slow-path host attention interrupt handling as appropriate.
10149 */
10150
10151 /* status of events with mailbox and link attention */
10152 status1 = phba->ha_copy & (HA_MBATT | HA_LATT | HA_ERATT);
10153
10154 /* status of events with ELS ring */
10155 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_ELS_RING)));
10156 status2 >>= (4*LPFC_ELS_RING);
10157
10158 if (status1 || (status2 & HA_RXMASK))
James Smart3772a992009-05-22 14:50:54 -040010159 sp_irq_rc = lpfc_sli_sp_intr_handler(irq, dev_id);
James Smart93996272008-08-24 21:50:30 -040010160 else
10161 sp_irq_rc = IRQ_NONE;
10162
10163 /*
10164 * Invoke fast-path host attention interrupt handling as appropriate.
10165 */
10166
10167 /* status of events with FCP ring */
10168 status1 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
10169 status1 >>= (4*LPFC_FCP_RING);
10170
10171 /* status of events with extra ring */
10172 if (phba->cfg_multi_ring_support == 2) {
10173 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
10174 status2 >>= (4*LPFC_EXTRA_RING);
10175 } else
10176 status2 = 0;
10177
10178 if ((status1 & HA_RXMASK) || (status2 & HA_RXMASK))
James Smart3772a992009-05-22 14:50:54 -040010179 fp_irq_rc = lpfc_sli_fp_intr_handler(irq, dev_id);
James Smart93996272008-08-24 21:50:30 -040010180 else
10181 fp_irq_rc = IRQ_NONE;
10182
10183 /* Return device-level interrupt handling status */
10184 return (sp_irq_rc == IRQ_HANDLED) ? sp_irq_rc : fp_irq_rc;
James Smart3772a992009-05-22 14:50:54 -040010185} /* lpfc_sli_intr_handler */
James Smart4f774512009-05-22 14:52:35 -040010186
10187/**
10188 * lpfc_sli4_fcp_xri_abort_event_proc - Process fcp xri abort event
10189 * @phba: pointer to lpfc hba data structure.
10190 *
10191 * This routine is invoked by the worker thread to process all the pending
10192 * SLI4 FCP abort XRI events.
10193 **/
10194void lpfc_sli4_fcp_xri_abort_event_proc(struct lpfc_hba *phba)
10195{
10196 struct lpfc_cq_event *cq_event;
10197
10198 /* First, declare the fcp xri abort event has been handled */
10199 spin_lock_irq(&phba->hbalock);
10200 phba->hba_flag &= ~FCP_XRI_ABORT_EVENT;
10201 spin_unlock_irq(&phba->hbalock);
10202 /* Now, handle all the fcp xri abort events */
10203 while (!list_empty(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue)) {
10204 /* Get the first event from the head of the event queue */
10205 spin_lock_irq(&phba->hbalock);
10206 list_remove_head(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue,
10207 cq_event, struct lpfc_cq_event, list);
10208 spin_unlock_irq(&phba->hbalock);
10209 /* Notify aborted XRI for FCP work queue */
10210 lpfc_sli4_fcp_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
10211 /* Free the event processed back to the free pool */
10212 lpfc_sli4_cq_event_release(phba, cq_event);
10213 }
10214}
10215
10216/**
10217 * lpfc_sli4_els_xri_abort_event_proc - Process els xri abort event
10218 * @phba: pointer to lpfc hba data structure.
10219 *
10220 * This routine is invoked by the worker thread to process all the pending
10221 * SLI4 els abort xri events.
10222 **/
10223void lpfc_sli4_els_xri_abort_event_proc(struct lpfc_hba *phba)
10224{
10225 struct lpfc_cq_event *cq_event;
10226
10227 /* First, declare the els xri abort event has been handled */
10228 spin_lock_irq(&phba->hbalock);
10229 phba->hba_flag &= ~ELS_XRI_ABORT_EVENT;
10230 spin_unlock_irq(&phba->hbalock);
10231 /* Now, handle all the els xri abort events */
10232 while (!list_empty(&phba->sli4_hba.sp_els_xri_aborted_work_queue)) {
10233 /* Get the first event from the head of the event queue */
10234 spin_lock_irq(&phba->hbalock);
10235 list_remove_head(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
10236 cq_event, struct lpfc_cq_event, list);
10237 spin_unlock_irq(&phba->hbalock);
10238 /* Notify aborted XRI for ELS work queue */
10239 lpfc_sli4_els_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
10240 /* Free the event processed back to the free pool */
10241 lpfc_sli4_cq_event_release(phba, cq_event);
10242 }
10243}
10244
James Smart341af102010-01-26 23:07:37 -050010245/**
10246 * lpfc_sli4_iocb_param_transfer - Transfer pIocbOut and cmpl status to pIocbIn
10247 * @phba: pointer to lpfc hba data structure
10248 * @pIocbIn: pointer to the rspiocbq
10249 * @pIocbOut: pointer to the cmdiocbq
10250 * @wcqe: pointer to the complete wcqe
10251 *
10252 * This routine transfers the fields of a command iocbq to a response iocbq
10253 * by copying all the IOCB fields from command iocbq and transferring the
10254 * completion status information from the complete wcqe.
10255 **/
James Smart4f774512009-05-22 14:52:35 -040010256static void
James Smart341af102010-01-26 23:07:37 -050010257lpfc_sli4_iocb_param_transfer(struct lpfc_hba *phba,
10258 struct lpfc_iocbq *pIocbIn,
James Smart4f774512009-05-22 14:52:35 -040010259 struct lpfc_iocbq *pIocbOut,
10260 struct lpfc_wcqe_complete *wcqe)
10261{
James Smart341af102010-01-26 23:07:37 -050010262 unsigned long iflags;
James Smart4f774512009-05-22 14:52:35 -040010263 size_t offset = offsetof(struct lpfc_iocbq, iocb);
10264
10265 memcpy((char *)pIocbIn + offset, (char *)pIocbOut + offset,
10266 sizeof(struct lpfc_iocbq) - offset);
James Smart4f774512009-05-22 14:52:35 -040010267 /* Map WCQE parameters into irspiocb parameters */
10268 pIocbIn->iocb.ulpStatus = bf_get(lpfc_wcqe_c_status, wcqe);
10269 if (pIocbOut->iocb_flag & LPFC_IO_FCP)
10270 if (pIocbIn->iocb.ulpStatus == IOSTAT_FCP_RSP_ERROR)
10271 pIocbIn->iocb.un.fcpi.fcpi_parm =
10272 pIocbOut->iocb.un.fcpi.fcpi_parm -
10273 wcqe->total_data_placed;
10274 else
10275 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
James Smart695a8142010-01-26 23:08:03 -050010276 else {
James Smart4f774512009-05-22 14:52:35 -040010277 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
James Smart695a8142010-01-26 23:08:03 -050010278 pIocbIn->iocb.un.genreq64.bdl.bdeSize = wcqe->total_data_placed;
10279 }
James Smart341af102010-01-26 23:07:37 -050010280
10281 /* Pick up HBA exchange busy condition */
10282 if (bf_get(lpfc_wcqe_c_xb, wcqe)) {
10283 spin_lock_irqsave(&phba->hbalock, iflags);
10284 pIocbIn->iocb_flag |= LPFC_EXCHANGE_BUSY;
10285 spin_unlock_irqrestore(&phba->hbalock, iflags);
10286 }
James Smart4f774512009-05-22 14:52:35 -040010287}
10288
10289/**
James Smart45ed1192009-10-02 15:17:02 -040010290 * lpfc_sli4_els_wcqe_to_rspiocbq - Get response iocbq from els wcqe
10291 * @phba: Pointer to HBA context object.
10292 * @wcqe: Pointer to work-queue completion queue entry.
10293 *
10294 * This routine handles an ELS work-queue completion event and construct
10295 * a pseudo response ELS IODBQ from the SLI4 ELS WCQE for the common
10296 * discovery engine to handle.
10297 *
10298 * Return: Pointer to the receive IOCBQ, NULL otherwise.
10299 **/
10300static struct lpfc_iocbq *
10301lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *phba,
10302 struct lpfc_iocbq *irspiocbq)
10303{
10304 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
10305 struct lpfc_iocbq *cmdiocbq;
10306 struct lpfc_wcqe_complete *wcqe;
10307 unsigned long iflags;
10308
10309 wcqe = &irspiocbq->cq_event.cqe.wcqe_cmpl;
10310 spin_lock_irqsave(&phba->hbalock, iflags);
10311 pring->stats.iocb_event++;
10312 /* Look up the ELS command IOCB and create pseudo response IOCB */
10313 cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
10314 bf_get(lpfc_wcqe_c_request_tag, wcqe));
10315 spin_unlock_irqrestore(&phba->hbalock, iflags);
10316
10317 if (unlikely(!cmdiocbq)) {
10318 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
10319 "0386 ELS complete with no corresponding "
10320 "cmdiocb: iotag (%d)\n",
10321 bf_get(lpfc_wcqe_c_request_tag, wcqe));
10322 lpfc_sli_release_iocbq(phba, irspiocbq);
10323 return NULL;
10324 }
10325
10326 /* Fake the irspiocbq and copy necessary response information */
James Smart341af102010-01-26 23:07:37 -050010327 lpfc_sli4_iocb_param_transfer(phba, irspiocbq, cmdiocbq, wcqe);
James Smart45ed1192009-10-02 15:17:02 -040010328
10329 return irspiocbq;
10330}
10331
10332/**
James Smart04c68492009-05-22 14:52:52 -040010333 * lpfc_sli4_sp_handle_async_event - Handle an asynchroous event
10334 * @phba: Pointer to HBA context object.
10335 * @cqe: Pointer to mailbox completion queue entry.
10336 *
10337 * This routine process a mailbox completion queue entry with asynchrous
10338 * event.
10339 *
10340 * Return: true if work posted to worker thread, otherwise false.
10341 **/
10342static bool
10343lpfc_sli4_sp_handle_async_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
10344{
10345 struct lpfc_cq_event *cq_event;
10346 unsigned long iflags;
10347
10348 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
10349 "0392 Async Event: word0:x%x, word1:x%x, "
10350 "word2:x%x, word3:x%x\n", mcqe->word0,
10351 mcqe->mcqe_tag0, mcqe->mcqe_tag1, mcqe->trailer);
10352
10353 /* Allocate a new internal CQ_EVENT entry */
10354 cq_event = lpfc_sli4_cq_event_alloc(phba);
10355 if (!cq_event) {
10356 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10357 "0394 Failed to allocate CQ_EVENT entry\n");
10358 return false;
10359 }
10360
10361 /* Move the CQE into an asynchronous event entry */
10362 memcpy(&cq_event->cqe, mcqe, sizeof(struct lpfc_mcqe));
10363 spin_lock_irqsave(&phba->hbalock, iflags);
10364 list_add_tail(&cq_event->list, &phba->sli4_hba.sp_asynce_work_queue);
10365 /* Set the async event flag */
10366 phba->hba_flag |= ASYNC_EVENT;
10367 spin_unlock_irqrestore(&phba->hbalock, iflags);
10368
10369 return true;
10370}
10371
10372/**
10373 * lpfc_sli4_sp_handle_mbox_event - Handle a mailbox completion event
10374 * @phba: Pointer to HBA context object.
10375 * @cqe: Pointer to mailbox completion queue entry.
10376 *
10377 * This routine process a mailbox completion queue entry with mailbox
10378 * completion event.
10379 *
10380 * Return: true if work posted to worker thread, otherwise false.
10381 **/
10382static bool
10383lpfc_sli4_sp_handle_mbox_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
10384{
10385 uint32_t mcqe_status;
10386 MAILBOX_t *mbox, *pmbox;
10387 struct lpfc_mqe *mqe;
10388 struct lpfc_vport *vport;
10389 struct lpfc_nodelist *ndlp;
10390 struct lpfc_dmabuf *mp;
10391 unsigned long iflags;
10392 LPFC_MBOXQ_t *pmb;
10393 bool workposted = false;
10394 int rc;
10395
10396 /* If not a mailbox complete MCQE, out by checking mailbox consume */
10397 if (!bf_get(lpfc_trailer_completed, mcqe))
10398 goto out_no_mqe_complete;
10399
10400 /* Get the reference to the active mbox command */
10401 spin_lock_irqsave(&phba->hbalock, iflags);
10402 pmb = phba->sli.mbox_active;
10403 if (unlikely(!pmb)) {
10404 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
10405 "1832 No pending MBOX command to handle\n");
10406 spin_unlock_irqrestore(&phba->hbalock, iflags);
10407 goto out_no_mqe_complete;
10408 }
10409 spin_unlock_irqrestore(&phba->hbalock, iflags);
10410 mqe = &pmb->u.mqe;
10411 pmbox = (MAILBOX_t *)&pmb->u.mqe;
10412 mbox = phba->mbox;
10413 vport = pmb->vport;
10414
10415 /* Reset heartbeat timer */
10416 phba->last_completion_time = jiffies;
10417 del_timer(&phba->sli.mbox_tmo);
10418
10419 /* Move mbox data to caller's mailbox region, do endian swapping */
10420 if (pmb->mbox_cmpl && mbox)
10421 lpfc_sli_pcimem_bcopy(mbox, mqe, sizeof(struct lpfc_mqe));
James Smart04c68492009-05-22 14:52:52 -040010422
James Smart73d91e52011-10-10 21:32:10 -040010423 /*
10424 * For mcqe errors, conditionally move a modified error code to
10425 * the mbox so that the error will not be missed.
10426 */
10427 mcqe_status = bf_get(lpfc_mcqe_status, mcqe);
10428 if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
10429 if (bf_get(lpfc_mqe_status, mqe) == MBX_SUCCESS)
10430 bf_set(lpfc_mqe_status, mqe,
10431 (LPFC_MBX_ERROR_RANGE | mcqe_status));
10432 }
James Smart04c68492009-05-22 14:52:52 -040010433 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
10434 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
10435 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_MBOX_VPORT,
10436 "MBOX dflt rpi: status:x%x rpi:x%x",
10437 mcqe_status,
10438 pmbox->un.varWords[0], 0);
10439 if (mcqe_status == MB_CQE_STATUS_SUCCESS) {
10440 mp = (struct lpfc_dmabuf *)(pmb->context1);
10441 ndlp = (struct lpfc_nodelist *)pmb->context2;
10442 /* Reg_LOGIN of dflt RPI was successful. Now lets get
10443 * RID of the PPI using the same mbox buffer.
10444 */
10445 lpfc_unreg_login(phba, vport->vpi,
10446 pmbox->un.varWords[0], pmb);
10447 pmb->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
10448 pmb->context1 = mp;
10449 pmb->context2 = ndlp;
10450 pmb->vport = vport;
10451 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
10452 if (rc != MBX_BUSY)
10453 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
10454 LOG_SLI, "0385 rc should "
10455 "have been MBX_BUSY\n");
10456 if (rc != MBX_NOT_FINISHED)
10457 goto send_current_mbox;
10458 }
10459 }
10460 spin_lock_irqsave(&phba->pport->work_port_lock, iflags);
10461 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
10462 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflags);
10463
10464 /* There is mailbox completion work to do */
10465 spin_lock_irqsave(&phba->hbalock, iflags);
10466 __lpfc_mbox_cmpl_put(phba, pmb);
10467 phba->work_ha |= HA_MBATT;
10468 spin_unlock_irqrestore(&phba->hbalock, iflags);
10469 workposted = true;
10470
10471send_current_mbox:
10472 spin_lock_irqsave(&phba->hbalock, iflags);
10473 /* Release the mailbox command posting token */
10474 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
10475 /* Setting active mailbox pointer need to be in sync to flag clear */
10476 phba->sli.mbox_active = NULL;
10477 spin_unlock_irqrestore(&phba->hbalock, iflags);
10478 /* Wake up worker thread to post the next pending mailbox command */
10479 lpfc_worker_wake_up(phba);
10480out_no_mqe_complete:
10481 if (bf_get(lpfc_trailer_consumed, mcqe))
10482 lpfc_sli4_mq_release(phba->sli4_hba.mbx_wq);
10483 return workposted;
10484}
10485
10486/**
10487 * lpfc_sli4_sp_handle_mcqe - Process a mailbox completion queue entry
10488 * @phba: Pointer to HBA context object.
10489 * @cqe: Pointer to mailbox completion queue entry.
10490 *
10491 * This routine process a mailbox completion queue entry, it invokes the
10492 * proper mailbox complete handling or asynchrous event handling routine
10493 * according to the MCQE's async bit.
10494 *
10495 * Return: true if work posted to worker thread, otherwise false.
10496 **/
10497static bool
10498lpfc_sli4_sp_handle_mcqe(struct lpfc_hba *phba, struct lpfc_cqe *cqe)
10499{
10500 struct lpfc_mcqe mcqe;
10501 bool workposted;
10502
10503 /* Copy the mailbox MCQE and convert endian order as needed */
10504 lpfc_sli_pcimem_bcopy(cqe, &mcqe, sizeof(struct lpfc_mcqe));
10505
10506 /* Invoke the proper event handling routine */
10507 if (!bf_get(lpfc_trailer_async, &mcqe))
10508 workposted = lpfc_sli4_sp_handle_mbox_event(phba, &mcqe);
10509 else
10510 workposted = lpfc_sli4_sp_handle_async_event(phba, &mcqe);
10511 return workposted;
10512}
10513
10514/**
James Smart4f774512009-05-22 14:52:35 -040010515 * lpfc_sli4_sp_handle_els_wcqe - Handle els work-queue completion event
10516 * @phba: Pointer to HBA context object.
10517 * @wcqe: Pointer to work-queue completion queue entry.
10518 *
10519 * This routine handles an ELS work-queue completion event.
10520 *
10521 * Return: true if work posted to worker thread, otherwise false.
10522 **/
10523static bool
10524lpfc_sli4_sp_handle_els_wcqe(struct lpfc_hba *phba,
10525 struct lpfc_wcqe_complete *wcqe)
10526{
James Smart4f774512009-05-22 14:52:35 -040010527 struct lpfc_iocbq *irspiocbq;
10528 unsigned long iflags;
James Smart2a9bf3d2010-06-07 15:24:45 -040010529 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_FCP_RING];
James Smart4f774512009-05-22 14:52:35 -040010530
James Smart45ed1192009-10-02 15:17:02 -040010531 /* Get an irspiocbq for later ELS response processing use */
James Smart4f774512009-05-22 14:52:35 -040010532 irspiocbq = lpfc_sli_get_iocbq(phba);
10533 if (!irspiocbq) {
10534 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart2a9bf3d2010-06-07 15:24:45 -040010535 "0387 NO IOCBQ data: txq_cnt=%d iocb_cnt=%d "
10536 "fcp_txcmplq_cnt=%d, els_txcmplq_cnt=%d\n",
10537 pring->txq_cnt, phba->iocb_cnt,
10538 phba->sli.ring[LPFC_FCP_RING].txcmplq_cnt,
10539 phba->sli.ring[LPFC_ELS_RING].txcmplq_cnt);
James Smart45ed1192009-10-02 15:17:02 -040010540 return false;
James Smart4f774512009-05-22 14:52:35 -040010541 }
James Smart4f774512009-05-22 14:52:35 -040010542
James Smart45ed1192009-10-02 15:17:02 -040010543 /* Save off the slow-path queue event for work thread to process */
10544 memcpy(&irspiocbq->cq_event.cqe.wcqe_cmpl, wcqe, sizeof(*wcqe));
James Smart4f774512009-05-22 14:52:35 -040010545 spin_lock_irqsave(&phba->hbalock, iflags);
James Smart4d9ab992009-10-02 15:16:39 -040010546 list_add_tail(&irspiocbq->cq_event.list,
James Smart45ed1192009-10-02 15:17:02 -040010547 &phba->sli4_hba.sp_queue_event);
10548 phba->hba_flag |= HBA_SP_QUEUE_EVT;
James Smart4f774512009-05-22 14:52:35 -040010549 spin_unlock_irqrestore(&phba->hbalock, iflags);
James Smart4f774512009-05-22 14:52:35 -040010550
James Smart45ed1192009-10-02 15:17:02 -040010551 return true;
James Smart4f774512009-05-22 14:52:35 -040010552}
10553
10554/**
10555 * lpfc_sli4_sp_handle_rel_wcqe - Handle slow-path WQ entry consumed event
10556 * @phba: Pointer to HBA context object.
10557 * @wcqe: Pointer to work-queue completion queue entry.
10558 *
10559 * This routine handles slow-path WQ entry comsumed event by invoking the
10560 * proper WQ release routine to the slow-path WQ.
10561 **/
10562static void
10563lpfc_sli4_sp_handle_rel_wcqe(struct lpfc_hba *phba,
10564 struct lpfc_wcqe_release *wcqe)
10565{
10566 /* Check for the slow-path ELS work queue */
10567 if (bf_get(lpfc_wcqe_r_wq_id, wcqe) == phba->sli4_hba.els_wq->queue_id)
10568 lpfc_sli4_wq_release(phba->sli4_hba.els_wq,
10569 bf_get(lpfc_wcqe_r_wqe_index, wcqe));
10570 else
10571 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
10572 "2579 Slow-path wqe consume event carries "
10573 "miss-matched qid: wcqe-qid=x%x, sp-qid=x%x\n",
10574 bf_get(lpfc_wcqe_r_wqe_index, wcqe),
10575 phba->sli4_hba.els_wq->queue_id);
10576}
10577
10578/**
10579 * lpfc_sli4_sp_handle_abort_xri_wcqe - Handle a xri abort event
10580 * @phba: Pointer to HBA context object.
10581 * @cq: Pointer to a WQ completion queue.
10582 * @wcqe: Pointer to work-queue completion queue entry.
10583 *
10584 * This routine handles an XRI abort event.
10585 *
10586 * Return: true if work posted to worker thread, otherwise false.
10587 **/
10588static bool
10589lpfc_sli4_sp_handle_abort_xri_wcqe(struct lpfc_hba *phba,
10590 struct lpfc_queue *cq,
10591 struct sli4_wcqe_xri_aborted *wcqe)
10592{
10593 bool workposted = false;
10594 struct lpfc_cq_event *cq_event;
10595 unsigned long iflags;
10596
10597 /* Allocate a new internal CQ_EVENT entry */
10598 cq_event = lpfc_sli4_cq_event_alloc(phba);
10599 if (!cq_event) {
10600 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10601 "0602 Failed to allocate CQ_EVENT entry\n");
10602 return false;
10603 }
10604
10605 /* Move the CQE into the proper xri abort event list */
10606 memcpy(&cq_event->cqe, wcqe, sizeof(struct sli4_wcqe_xri_aborted));
10607 switch (cq->subtype) {
10608 case LPFC_FCP:
10609 spin_lock_irqsave(&phba->hbalock, iflags);
10610 list_add_tail(&cq_event->list,
10611 &phba->sli4_hba.sp_fcp_xri_aborted_work_queue);
10612 /* Set the fcp xri abort event flag */
10613 phba->hba_flag |= FCP_XRI_ABORT_EVENT;
10614 spin_unlock_irqrestore(&phba->hbalock, iflags);
10615 workposted = true;
10616 break;
10617 case LPFC_ELS:
10618 spin_lock_irqsave(&phba->hbalock, iflags);
10619 list_add_tail(&cq_event->list,
10620 &phba->sli4_hba.sp_els_xri_aborted_work_queue);
10621 /* Set the els xri abort event flag */
10622 phba->hba_flag |= ELS_XRI_ABORT_EVENT;
10623 spin_unlock_irqrestore(&phba->hbalock, iflags);
10624 workposted = true;
10625 break;
10626 default:
10627 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10628 "0603 Invalid work queue CQE subtype (x%x)\n",
10629 cq->subtype);
10630 workposted = false;
10631 break;
10632 }
10633 return workposted;
10634}
10635
10636/**
James Smart4d9ab992009-10-02 15:16:39 -040010637 * lpfc_sli4_sp_handle_rcqe - Process a receive-queue completion queue entry
James Smart4f774512009-05-22 14:52:35 -040010638 * @phba: Pointer to HBA context object.
James Smart4d9ab992009-10-02 15:16:39 -040010639 * @rcqe: Pointer to receive-queue completion queue entry.
James Smart4f774512009-05-22 14:52:35 -040010640 *
James Smart4d9ab992009-10-02 15:16:39 -040010641 * This routine process a receive-queue completion queue entry.
James Smart4f774512009-05-22 14:52:35 -040010642 *
10643 * Return: true if work posted to worker thread, otherwise false.
10644 **/
10645static bool
James Smart4d9ab992009-10-02 15:16:39 -040010646lpfc_sli4_sp_handle_rcqe(struct lpfc_hba *phba, struct lpfc_rcqe *rcqe)
10647{
10648 bool workposted = false;
10649 struct lpfc_queue *hrq = phba->sli4_hba.hdr_rq;
10650 struct lpfc_queue *drq = phba->sli4_hba.dat_rq;
10651 struct hbq_dmabuf *dma_buf;
James Smart7851fe22011-07-22 18:36:52 -040010652 uint32_t status, rq_id;
James Smart4d9ab992009-10-02 15:16:39 -040010653 unsigned long iflags;
10654
James Smart7851fe22011-07-22 18:36:52 -040010655 if (bf_get(lpfc_cqe_code, rcqe) == CQE_CODE_RECEIVE_V1)
10656 rq_id = bf_get(lpfc_rcqe_rq_id_v1, rcqe);
10657 else
10658 rq_id = bf_get(lpfc_rcqe_rq_id, rcqe);
10659 if (rq_id != hrq->queue_id)
James Smart4d9ab992009-10-02 15:16:39 -040010660 goto out;
10661
10662 status = bf_get(lpfc_rcqe_status, rcqe);
10663 switch (status) {
10664 case FC_STATUS_RQ_BUF_LEN_EXCEEDED:
10665 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10666 "2537 Receive Frame Truncated!!\n");
10667 case FC_STATUS_RQ_SUCCESS:
James Smart5ffc2662009-11-18 15:39:44 -050010668 lpfc_sli4_rq_release(hrq, drq);
James Smart4d9ab992009-10-02 15:16:39 -040010669 spin_lock_irqsave(&phba->hbalock, iflags);
10670 dma_buf = lpfc_sli_hbqbuf_get(&phba->hbqs[0].hbq_buffer_list);
10671 if (!dma_buf) {
10672 spin_unlock_irqrestore(&phba->hbalock, iflags);
10673 goto out;
10674 }
10675 memcpy(&dma_buf->cq_event.cqe.rcqe_cmpl, rcqe, sizeof(*rcqe));
10676 /* save off the frame for the word thread to process */
10677 list_add_tail(&dma_buf->cq_event.list,
James Smart45ed1192009-10-02 15:17:02 -040010678 &phba->sli4_hba.sp_queue_event);
James Smart4d9ab992009-10-02 15:16:39 -040010679 /* Frame received */
James Smart45ed1192009-10-02 15:17:02 -040010680 phba->hba_flag |= HBA_SP_QUEUE_EVT;
James Smart4d9ab992009-10-02 15:16:39 -040010681 spin_unlock_irqrestore(&phba->hbalock, iflags);
10682 workposted = true;
10683 break;
10684 case FC_STATUS_INSUFF_BUF_NEED_BUF:
10685 case FC_STATUS_INSUFF_BUF_FRM_DISC:
10686 /* Post more buffers if possible */
10687 spin_lock_irqsave(&phba->hbalock, iflags);
10688 phba->hba_flag |= HBA_POST_RECEIVE_BUFFER;
10689 spin_unlock_irqrestore(&phba->hbalock, iflags);
10690 workposted = true;
10691 break;
10692 }
10693out:
10694 return workposted;
James Smart4d9ab992009-10-02 15:16:39 -040010695}
10696
10697/**
10698 * lpfc_sli4_sp_handle_cqe - Process a slow path completion queue entry
10699 * @phba: Pointer to HBA context object.
10700 * @cq: Pointer to the completion queue.
10701 * @wcqe: Pointer to a completion queue entry.
10702 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030010703 * This routine process a slow-path work-queue or receive queue completion queue
James Smart4d9ab992009-10-02 15:16:39 -040010704 * entry.
10705 *
10706 * Return: true if work posted to worker thread, otherwise false.
10707 **/
10708static bool
10709lpfc_sli4_sp_handle_cqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
James Smart4f774512009-05-22 14:52:35 -040010710 struct lpfc_cqe *cqe)
10711{
James Smart45ed1192009-10-02 15:17:02 -040010712 struct lpfc_cqe cqevt;
James Smart4f774512009-05-22 14:52:35 -040010713 bool workposted = false;
10714
10715 /* Copy the work queue CQE and convert endian order if needed */
James Smart45ed1192009-10-02 15:17:02 -040010716 lpfc_sli_pcimem_bcopy(cqe, &cqevt, sizeof(struct lpfc_cqe));
James Smart4f774512009-05-22 14:52:35 -040010717
10718 /* Check and process for different type of WCQE and dispatch */
James Smart45ed1192009-10-02 15:17:02 -040010719 switch (bf_get(lpfc_cqe_code, &cqevt)) {
James Smart4f774512009-05-22 14:52:35 -040010720 case CQE_CODE_COMPL_WQE:
James Smart45ed1192009-10-02 15:17:02 -040010721 /* Process the WQ/RQ complete event */
James Smartbc739052010-08-04 16:11:18 -040010722 phba->last_completion_time = jiffies;
James Smart4f774512009-05-22 14:52:35 -040010723 workposted = lpfc_sli4_sp_handle_els_wcqe(phba,
James Smart45ed1192009-10-02 15:17:02 -040010724 (struct lpfc_wcqe_complete *)&cqevt);
James Smart4f774512009-05-22 14:52:35 -040010725 break;
10726 case CQE_CODE_RELEASE_WQE:
10727 /* Process the WQ release event */
10728 lpfc_sli4_sp_handle_rel_wcqe(phba,
James Smart45ed1192009-10-02 15:17:02 -040010729 (struct lpfc_wcqe_release *)&cqevt);
James Smart4f774512009-05-22 14:52:35 -040010730 break;
10731 case CQE_CODE_XRI_ABORTED:
10732 /* Process the WQ XRI abort event */
James Smartbc739052010-08-04 16:11:18 -040010733 phba->last_completion_time = jiffies;
James Smart4f774512009-05-22 14:52:35 -040010734 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
James Smart45ed1192009-10-02 15:17:02 -040010735 (struct sli4_wcqe_xri_aborted *)&cqevt);
James Smart4f774512009-05-22 14:52:35 -040010736 break;
James Smart4d9ab992009-10-02 15:16:39 -040010737 case CQE_CODE_RECEIVE:
James Smart7851fe22011-07-22 18:36:52 -040010738 case CQE_CODE_RECEIVE_V1:
James Smart4d9ab992009-10-02 15:16:39 -040010739 /* Process the RQ event */
James Smartbc739052010-08-04 16:11:18 -040010740 phba->last_completion_time = jiffies;
James Smart4d9ab992009-10-02 15:16:39 -040010741 workposted = lpfc_sli4_sp_handle_rcqe(phba,
James Smart45ed1192009-10-02 15:17:02 -040010742 (struct lpfc_rcqe *)&cqevt);
James Smart4d9ab992009-10-02 15:16:39 -040010743 break;
James Smart4f774512009-05-22 14:52:35 -040010744 default:
10745 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10746 "0388 Not a valid WCQE code: x%x\n",
James Smart45ed1192009-10-02 15:17:02 -040010747 bf_get(lpfc_cqe_code, &cqevt));
James Smart4f774512009-05-22 14:52:35 -040010748 break;
10749 }
10750 return workposted;
10751}
10752
10753/**
James Smart4f774512009-05-22 14:52:35 -040010754 * lpfc_sli4_sp_handle_eqe - Process a slow-path event queue entry
10755 * @phba: Pointer to HBA context object.
10756 * @eqe: Pointer to fast-path event queue entry.
10757 *
10758 * This routine process a event queue entry from the slow-path event queue.
10759 * It will check the MajorCode and MinorCode to determine this is for a
10760 * completion event on a completion queue, if not, an error shall be logged
10761 * and just return. Otherwise, it will get to the corresponding completion
10762 * queue and process all the entries on that completion queue, rearm the
10763 * completion queue, and then return.
10764 *
10765 **/
10766static void
10767lpfc_sli4_sp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe)
10768{
10769 struct lpfc_queue *cq = NULL, *childq, *speq;
10770 struct lpfc_cqe *cqe;
10771 bool workposted = false;
10772 int ecount = 0;
10773 uint16_t cqid;
10774
James Smartcb5172e2010-03-15 11:25:07 -040010775 if (bf_get_le32(lpfc_eqe_major_code, eqe) != 0) {
James Smart4f774512009-05-22 14:52:35 -040010776 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10777 "0359 Not a valid slow-path completion "
10778 "event: majorcode=x%x, minorcode=x%x\n",
James Smartcb5172e2010-03-15 11:25:07 -040010779 bf_get_le32(lpfc_eqe_major_code, eqe),
10780 bf_get_le32(lpfc_eqe_minor_code, eqe));
James Smart4f774512009-05-22 14:52:35 -040010781 return;
10782 }
10783
10784 /* Get the reference to the corresponding CQ */
James Smartcb5172e2010-03-15 11:25:07 -040010785 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
James Smart4f774512009-05-22 14:52:35 -040010786
10787 /* Search for completion queue pointer matching this cqid */
10788 speq = phba->sli4_hba.sp_eq;
10789 list_for_each_entry(childq, &speq->child_list, list) {
10790 if (childq->queue_id == cqid) {
10791 cq = childq;
10792 break;
10793 }
10794 }
10795 if (unlikely(!cq)) {
James Smart75baf692010-06-08 18:31:21 -040010796 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
10797 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10798 "0365 Slow-path CQ identifier "
10799 "(%d) does not exist\n", cqid);
James Smart4f774512009-05-22 14:52:35 -040010800 return;
10801 }
10802
10803 /* Process all the entries to the CQ */
10804 switch (cq->type) {
10805 case LPFC_MCQ:
10806 while ((cqe = lpfc_sli4_cq_get(cq))) {
10807 workposted |= lpfc_sli4_sp_handle_mcqe(phba, cqe);
James Smart73d91e52011-10-10 21:32:10 -040010808 if (!(++ecount % cq->entry_repost))
James Smart4f774512009-05-22 14:52:35 -040010809 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
10810 }
10811 break;
10812 case LPFC_WCQ:
10813 while ((cqe = lpfc_sli4_cq_get(cq))) {
James Smart05580562011-05-24 11:40:48 -040010814 if (cq->subtype == LPFC_FCP)
10815 workposted |= lpfc_sli4_fp_handle_wcqe(phba, cq,
10816 cqe);
10817 else
10818 workposted |= lpfc_sli4_sp_handle_cqe(phba, cq,
10819 cqe);
James Smart73d91e52011-10-10 21:32:10 -040010820 if (!(++ecount % cq->entry_repost))
James Smart4f774512009-05-22 14:52:35 -040010821 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
10822 }
10823 break;
10824 default:
10825 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10826 "0370 Invalid completion queue type (%d)\n",
10827 cq->type);
10828 return;
10829 }
10830
10831 /* Catch the no cq entry condition, log an error */
10832 if (unlikely(ecount == 0))
10833 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10834 "0371 No entry from the CQ: identifier "
10835 "(x%x), type (%d)\n", cq->queue_id, cq->type);
10836
10837 /* In any case, flash and re-arm the RCQ */
10838 lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
10839
10840 /* wake up worker thread if there are works to be done */
10841 if (workposted)
10842 lpfc_worker_wake_up(phba);
10843}
10844
10845/**
10846 * lpfc_sli4_fp_handle_fcp_wcqe - Process fast-path work queue completion entry
10847 * @eqe: Pointer to fast-path completion queue entry.
10848 *
10849 * This routine process a fast-path work queue completion entry from fast-path
10850 * event queue for FCP command response completion.
10851 **/
10852static void
10853lpfc_sli4_fp_handle_fcp_wcqe(struct lpfc_hba *phba,
10854 struct lpfc_wcqe_complete *wcqe)
10855{
10856 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_FCP_RING];
10857 struct lpfc_iocbq *cmdiocbq;
10858 struct lpfc_iocbq irspiocbq;
10859 unsigned long iflags;
10860
10861 spin_lock_irqsave(&phba->hbalock, iflags);
10862 pring->stats.iocb_event++;
10863 spin_unlock_irqrestore(&phba->hbalock, iflags);
10864
10865 /* Check for response status */
10866 if (unlikely(bf_get(lpfc_wcqe_c_status, wcqe))) {
10867 /* If resource errors reported from HBA, reduce queue
10868 * depth of the SCSI device.
10869 */
10870 if ((bf_get(lpfc_wcqe_c_status, wcqe) ==
10871 IOSTAT_LOCAL_REJECT) &&
10872 (wcqe->parameter == IOERR_NO_RESOURCES)) {
10873 phba->lpfc_rampdown_queue_depth(phba);
10874 }
10875 /* Log the error status */
10876 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
10877 "0373 FCP complete error: status=x%x, "
10878 "hw_status=x%x, total_data_specified=%d, "
10879 "parameter=x%x, word3=x%x\n",
10880 bf_get(lpfc_wcqe_c_status, wcqe),
10881 bf_get(lpfc_wcqe_c_hw_status, wcqe),
10882 wcqe->total_data_placed, wcqe->parameter,
10883 wcqe->word3);
10884 }
10885
10886 /* Look up the FCP command IOCB and create pseudo response IOCB */
10887 spin_lock_irqsave(&phba->hbalock, iflags);
10888 cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
10889 bf_get(lpfc_wcqe_c_request_tag, wcqe));
10890 spin_unlock_irqrestore(&phba->hbalock, iflags);
10891 if (unlikely(!cmdiocbq)) {
10892 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
10893 "0374 FCP complete with no corresponding "
10894 "cmdiocb: iotag (%d)\n",
10895 bf_get(lpfc_wcqe_c_request_tag, wcqe));
10896 return;
10897 }
10898 if (unlikely(!cmdiocbq->iocb_cmpl)) {
10899 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
10900 "0375 FCP cmdiocb not callback function "
10901 "iotag: (%d)\n",
10902 bf_get(lpfc_wcqe_c_request_tag, wcqe));
10903 return;
10904 }
10905
10906 /* Fake the irspiocb and copy necessary response information */
James Smart341af102010-01-26 23:07:37 -050010907 lpfc_sli4_iocb_param_transfer(phba, &irspiocbq, cmdiocbq, wcqe);
James Smart4f774512009-05-22 14:52:35 -040010908
James Smart0f65ff62010-02-26 14:14:23 -050010909 if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED) {
10910 spin_lock_irqsave(&phba->hbalock, iflags);
10911 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
10912 spin_unlock_irqrestore(&phba->hbalock, iflags);
10913 }
10914
James Smart4f774512009-05-22 14:52:35 -040010915 /* Pass the cmd_iocb and the rsp state to the upper layer */
10916 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq, &irspiocbq);
10917}
10918
10919/**
10920 * lpfc_sli4_fp_handle_rel_wcqe - Handle fast-path WQ entry consumed event
10921 * @phba: Pointer to HBA context object.
10922 * @cq: Pointer to completion queue.
10923 * @wcqe: Pointer to work-queue completion queue entry.
10924 *
10925 * This routine handles an fast-path WQ entry comsumed event by invoking the
10926 * proper WQ release routine to the slow-path WQ.
10927 **/
10928static void
10929lpfc_sli4_fp_handle_rel_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
10930 struct lpfc_wcqe_release *wcqe)
10931{
10932 struct lpfc_queue *childwq;
10933 bool wqid_matched = false;
10934 uint16_t fcp_wqid;
10935
10936 /* Check for fast-path FCP work queue release */
10937 fcp_wqid = bf_get(lpfc_wcqe_r_wq_id, wcqe);
10938 list_for_each_entry(childwq, &cq->child_list, list) {
10939 if (childwq->queue_id == fcp_wqid) {
10940 lpfc_sli4_wq_release(childwq,
10941 bf_get(lpfc_wcqe_r_wqe_index, wcqe));
10942 wqid_matched = true;
10943 break;
10944 }
10945 }
10946 /* Report warning log message if no match found */
10947 if (wqid_matched != true)
10948 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
10949 "2580 Fast-path wqe consume event carries "
10950 "miss-matched qid: wcqe-qid=x%x\n", fcp_wqid);
10951}
10952
10953/**
10954 * lpfc_sli4_fp_handle_wcqe - Process fast-path work queue completion entry
10955 * @cq: Pointer to the completion queue.
10956 * @eqe: Pointer to fast-path completion queue entry.
10957 *
10958 * This routine process a fast-path work queue completion entry from fast-path
10959 * event queue for FCP command response completion.
10960 **/
10961static int
10962lpfc_sli4_fp_handle_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
10963 struct lpfc_cqe *cqe)
10964{
10965 struct lpfc_wcqe_release wcqe;
10966 bool workposted = false;
10967
10968 /* Copy the work queue CQE and convert endian order if needed */
10969 lpfc_sli_pcimem_bcopy(cqe, &wcqe, sizeof(struct lpfc_cqe));
10970
10971 /* Check and process for different type of WCQE and dispatch */
10972 switch (bf_get(lpfc_wcqe_c_code, &wcqe)) {
10973 case CQE_CODE_COMPL_WQE:
10974 /* Process the WQ complete event */
James Smart98fc5dd2010-06-07 15:24:29 -040010975 phba->last_completion_time = jiffies;
James Smart4f774512009-05-22 14:52:35 -040010976 lpfc_sli4_fp_handle_fcp_wcqe(phba,
10977 (struct lpfc_wcqe_complete *)&wcqe);
10978 break;
10979 case CQE_CODE_RELEASE_WQE:
10980 /* Process the WQ release event */
10981 lpfc_sli4_fp_handle_rel_wcqe(phba, cq,
10982 (struct lpfc_wcqe_release *)&wcqe);
10983 break;
10984 case CQE_CODE_XRI_ABORTED:
10985 /* Process the WQ XRI abort event */
James Smartbc739052010-08-04 16:11:18 -040010986 phba->last_completion_time = jiffies;
James Smart4f774512009-05-22 14:52:35 -040010987 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
10988 (struct sli4_wcqe_xri_aborted *)&wcqe);
10989 break;
10990 default:
10991 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10992 "0144 Not a valid WCQE code: x%x\n",
10993 bf_get(lpfc_wcqe_c_code, &wcqe));
10994 break;
10995 }
10996 return workposted;
10997}
10998
10999/**
11000 * lpfc_sli4_fp_handle_eqe - Process a fast-path event queue entry
11001 * @phba: Pointer to HBA context object.
11002 * @eqe: Pointer to fast-path event queue entry.
11003 *
11004 * This routine process a event queue entry from the fast-path event queue.
11005 * It will check the MajorCode and MinorCode to determine this is for a
11006 * completion event on a completion queue, if not, an error shall be logged
11007 * and just return. Otherwise, it will get to the corresponding completion
11008 * queue and process all the entries on the completion queue, rearm the
11009 * completion queue, and then return.
11010 **/
11011static void
11012lpfc_sli4_fp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
11013 uint32_t fcp_cqidx)
11014{
11015 struct lpfc_queue *cq;
11016 struct lpfc_cqe *cqe;
11017 bool workposted = false;
11018 uint16_t cqid;
11019 int ecount = 0;
11020
James Smartcb5172e2010-03-15 11:25:07 -040011021 if (unlikely(bf_get_le32(lpfc_eqe_major_code, eqe) != 0)) {
James Smart4f774512009-05-22 14:52:35 -040011022 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11023 "0366 Not a valid fast-path completion "
11024 "event: majorcode=x%x, minorcode=x%x\n",
James Smartcb5172e2010-03-15 11:25:07 -040011025 bf_get_le32(lpfc_eqe_major_code, eqe),
11026 bf_get_le32(lpfc_eqe_minor_code, eqe));
James Smart4f774512009-05-22 14:52:35 -040011027 return;
11028 }
11029
11030 cq = phba->sli4_hba.fcp_cq[fcp_cqidx];
11031 if (unlikely(!cq)) {
James Smart75baf692010-06-08 18:31:21 -040011032 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
11033 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11034 "0367 Fast-path completion queue "
11035 "does not exist\n");
James Smart4f774512009-05-22 14:52:35 -040011036 return;
11037 }
11038
11039 /* Get the reference to the corresponding CQ */
James Smartcb5172e2010-03-15 11:25:07 -040011040 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
James Smart4f774512009-05-22 14:52:35 -040011041 if (unlikely(cqid != cq->queue_id)) {
11042 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11043 "0368 Miss-matched fast-path completion "
11044 "queue identifier: eqcqid=%d, fcpcqid=%d\n",
11045 cqid, cq->queue_id);
11046 return;
11047 }
11048
11049 /* Process all the entries to the CQ */
11050 while ((cqe = lpfc_sli4_cq_get(cq))) {
11051 workposted |= lpfc_sli4_fp_handle_wcqe(phba, cq, cqe);
James Smart73d91e52011-10-10 21:32:10 -040011052 if (!(++ecount % cq->entry_repost))
James Smart4f774512009-05-22 14:52:35 -040011053 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
11054 }
11055
11056 /* Catch the no cq entry condition */
11057 if (unlikely(ecount == 0))
11058 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11059 "0369 No entry from fast-path completion "
11060 "queue fcpcqid=%d\n", cq->queue_id);
11061
11062 /* In any case, flash and re-arm the CQ */
11063 lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
11064
11065 /* wake up worker thread if there are works to be done */
11066 if (workposted)
11067 lpfc_worker_wake_up(phba);
11068}
11069
11070static void
11071lpfc_sli4_eq_flush(struct lpfc_hba *phba, struct lpfc_queue *eq)
11072{
11073 struct lpfc_eqe *eqe;
11074
11075 /* walk all the EQ entries and drop on the floor */
11076 while ((eqe = lpfc_sli4_eq_get(eq)))
11077 ;
11078
11079 /* Clear and re-arm the EQ */
11080 lpfc_sli4_eq_release(eq, LPFC_QUEUE_REARM);
11081}
11082
11083/**
11084 * lpfc_sli4_sp_intr_handler - Slow-path interrupt handler to SLI-4 device
11085 * @irq: Interrupt number.
11086 * @dev_id: The device context pointer.
11087 *
11088 * This function is directly called from the PCI layer as an interrupt
11089 * service routine when device with SLI-4 interface spec is enabled with
11090 * MSI-X multi-message interrupt mode and there are slow-path events in
11091 * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
11092 * interrupt mode, this function is called as part of the device-level
11093 * interrupt handler. When the PCI slot is in error recovery or the HBA is
11094 * undergoing initialization, the interrupt handler will not process the
11095 * interrupt. The link attention and ELS ring attention events are handled
11096 * by the worker thread. The interrupt handler signals the worker thread
11097 * and returns for these events. This function is called without any lock
11098 * held. It gets the hbalock to access and update SLI data structures.
11099 *
11100 * This function returns IRQ_HANDLED when interrupt is handled else it
11101 * returns IRQ_NONE.
11102 **/
11103irqreturn_t
11104lpfc_sli4_sp_intr_handler(int irq, void *dev_id)
11105{
11106 struct lpfc_hba *phba;
11107 struct lpfc_queue *speq;
11108 struct lpfc_eqe *eqe;
11109 unsigned long iflag;
11110 int ecount = 0;
11111
11112 /*
11113 * Get the driver's phba structure from the dev_id
11114 */
11115 phba = (struct lpfc_hba *)dev_id;
11116
11117 if (unlikely(!phba))
11118 return IRQ_NONE;
11119
11120 /* Get to the EQ struct associated with this vector */
11121 speq = phba->sli4_hba.sp_eq;
11122
11123 /* Check device state for handling interrupt */
11124 if (unlikely(lpfc_intr_state_check(phba))) {
11125 /* Check again for link_state with lock held */
11126 spin_lock_irqsave(&phba->hbalock, iflag);
11127 if (phba->link_state < LPFC_LINK_DOWN)
11128 /* Flush, clear interrupt, and rearm the EQ */
11129 lpfc_sli4_eq_flush(phba, speq);
11130 spin_unlock_irqrestore(&phba->hbalock, iflag);
11131 return IRQ_NONE;
11132 }
11133
11134 /*
11135 * Process all the event on FCP slow-path EQ
11136 */
11137 while ((eqe = lpfc_sli4_eq_get(speq))) {
11138 lpfc_sli4_sp_handle_eqe(phba, eqe);
James Smart73d91e52011-10-10 21:32:10 -040011139 if (!(++ecount % speq->entry_repost))
James Smart4f774512009-05-22 14:52:35 -040011140 lpfc_sli4_eq_release(speq, LPFC_QUEUE_NOARM);
11141 }
11142
11143 /* Always clear and re-arm the slow-path EQ */
11144 lpfc_sli4_eq_release(speq, LPFC_QUEUE_REARM);
11145
11146 /* Catch the no cq entry condition */
11147 if (unlikely(ecount == 0)) {
11148 if (phba->intr_type == MSIX)
11149 /* MSI-X treated interrupt served as no EQ share INT */
11150 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11151 "0357 MSI-X interrupt with no EQE\n");
11152 else
11153 /* Non MSI-X treated on interrupt as EQ share INT */
11154 return IRQ_NONE;
11155 }
11156
11157 return IRQ_HANDLED;
11158} /* lpfc_sli4_sp_intr_handler */
11159
11160/**
11161 * lpfc_sli4_fp_intr_handler - Fast-path interrupt handler to SLI-4 device
11162 * @irq: Interrupt number.
11163 * @dev_id: The device context pointer.
11164 *
11165 * This function is directly called from the PCI layer as an interrupt
11166 * service routine when device with SLI-4 interface spec is enabled with
11167 * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
11168 * ring event in the HBA. However, when the device is enabled with either
11169 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
11170 * device-level interrupt handler. When the PCI slot is in error recovery
11171 * or the HBA is undergoing initialization, the interrupt handler will not
11172 * process the interrupt. The SCSI FCP fast-path ring event are handled in
11173 * the intrrupt context. This function is called without any lock held.
11174 * It gets the hbalock to access and update SLI data structures. Note that,
11175 * the FCP EQ to FCP CQ are one-to-one map such that the FCP EQ index is
11176 * equal to that of FCP CQ index.
11177 *
11178 * This function returns IRQ_HANDLED when interrupt is handled else it
11179 * returns IRQ_NONE.
11180 **/
11181irqreturn_t
11182lpfc_sli4_fp_intr_handler(int irq, void *dev_id)
11183{
11184 struct lpfc_hba *phba;
11185 struct lpfc_fcp_eq_hdl *fcp_eq_hdl;
11186 struct lpfc_queue *fpeq;
11187 struct lpfc_eqe *eqe;
11188 unsigned long iflag;
11189 int ecount = 0;
11190 uint32_t fcp_eqidx;
11191
11192 /* Get the driver's phba structure from the dev_id */
11193 fcp_eq_hdl = (struct lpfc_fcp_eq_hdl *)dev_id;
11194 phba = fcp_eq_hdl->phba;
11195 fcp_eqidx = fcp_eq_hdl->idx;
11196
11197 if (unlikely(!phba))
11198 return IRQ_NONE;
11199
11200 /* Get to the EQ struct associated with this vector */
11201 fpeq = phba->sli4_hba.fp_eq[fcp_eqidx];
11202
11203 /* Check device state for handling interrupt */
11204 if (unlikely(lpfc_intr_state_check(phba))) {
11205 /* Check again for link_state with lock held */
11206 spin_lock_irqsave(&phba->hbalock, iflag);
11207 if (phba->link_state < LPFC_LINK_DOWN)
11208 /* Flush, clear interrupt, and rearm the EQ */
11209 lpfc_sli4_eq_flush(phba, fpeq);
11210 spin_unlock_irqrestore(&phba->hbalock, iflag);
11211 return IRQ_NONE;
11212 }
11213
11214 /*
11215 * Process all the event on FCP fast-path EQ
11216 */
11217 while ((eqe = lpfc_sli4_eq_get(fpeq))) {
11218 lpfc_sli4_fp_handle_eqe(phba, eqe, fcp_eqidx);
James Smart73d91e52011-10-10 21:32:10 -040011219 if (!(++ecount % fpeq->entry_repost))
James Smart4f774512009-05-22 14:52:35 -040011220 lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_NOARM);
11221 }
11222
11223 /* Always clear and re-arm the fast-path EQ */
11224 lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_REARM);
11225
11226 if (unlikely(ecount == 0)) {
11227 if (phba->intr_type == MSIX)
11228 /* MSI-X treated interrupt served as no EQ share INT */
11229 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11230 "0358 MSI-X interrupt with no EQE\n");
11231 else
11232 /* Non MSI-X treated on interrupt as EQ share INT */
11233 return IRQ_NONE;
11234 }
11235
11236 return IRQ_HANDLED;
11237} /* lpfc_sli4_fp_intr_handler */
11238
11239/**
11240 * lpfc_sli4_intr_handler - Device-level interrupt handler for SLI-4 device
11241 * @irq: Interrupt number.
11242 * @dev_id: The device context pointer.
11243 *
11244 * This function is the device-level interrupt handler to device with SLI-4
11245 * interface spec, called from the PCI layer when either MSI or Pin-IRQ
11246 * interrupt mode is enabled and there is an event in the HBA which requires
11247 * driver attention. This function invokes the slow-path interrupt attention
11248 * handling function and fast-path interrupt attention handling function in
11249 * turn to process the relevant HBA attention events. This function is called
11250 * without any lock held. It gets the hbalock to access and update SLI data
11251 * structures.
11252 *
11253 * This function returns IRQ_HANDLED when interrupt is handled, else it
11254 * returns IRQ_NONE.
11255 **/
11256irqreturn_t
11257lpfc_sli4_intr_handler(int irq, void *dev_id)
11258{
11259 struct lpfc_hba *phba;
11260 irqreturn_t sp_irq_rc, fp_irq_rc;
11261 bool fp_handled = false;
11262 uint32_t fcp_eqidx;
11263
11264 /* Get the driver's phba structure from the dev_id */
11265 phba = (struct lpfc_hba *)dev_id;
11266
11267 if (unlikely(!phba))
11268 return IRQ_NONE;
11269
11270 /*
11271 * Invokes slow-path host attention interrupt handling as appropriate.
11272 */
11273 sp_irq_rc = lpfc_sli4_sp_intr_handler(irq, dev_id);
11274
11275 /*
11276 * Invoke fast-path host attention interrupt handling as appropriate.
11277 */
11278 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count; fcp_eqidx++) {
11279 fp_irq_rc = lpfc_sli4_fp_intr_handler(irq,
11280 &phba->sli4_hba.fcp_eq_hdl[fcp_eqidx]);
11281 if (fp_irq_rc == IRQ_HANDLED)
11282 fp_handled |= true;
11283 }
11284
11285 return (fp_handled == true) ? IRQ_HANDLED : sp_irq_rc;
11286} /* lpfc_sli4_intr_handler */
11287
11288/**
11289 * lpfc_sli4_queue_free - free a queue structure and associated memory
11290 * @queue: The queue structure to free.
11291 *
Uwe Kleine-Königb5950762010-11-01 15:38:34 -040011292 * This function frees a queue structure and the DMAable memory used for
James Smart4f774512009-05-22 14:52:35 -040011293 * the host resident queue. This function must be called after destroying the
11294 * queue on the HBA.
11295 **/
11296void
11297lpfc_sli4_queue_free(struct lpfc_queue *queue)
11298{
11299 struct lpfc_dmabuf *dmabuf;
11300
11301 if (!queue)
11302 return;
11303
11304 while (!list_empty(&queue->page_list)) {
11305 list_remove_head(&queue->page_list, dmabuf, struct lpfc_dmabuf,
11306 list);
James Smart49198b32010-04-06 15:04:33 -040011307 dma_free_coherent(&queue->phba->pcidev->dev, SLI4_PAGE_SIZE,
James Smart4f774512009-05-22 14:52:35 -040011308 dmabuf->virt, dmabuf->phys);
11309 kfree(dmabuf);
11310 }
11311 kfree(queue);
11312 return;
11313}
11314
11315/**
11316 * lpfc_sli4_queue_alloc - Allocate and initialize a queue structure
11317 * @phba: The HBA that this queue is being created on.
11318 * @entry_size: The size of each queue entry for this queue.
11319 * @entry count: The number of entries that this queue will handle.
11320 *
11321 * This function allocates a queue structure and the DMAable memory used for
11322 * the host resident queue. This function must be called before creating the
11323 * queue on the HBA.
11324 **/
11325struct lpfc_queue *
11326lpfc_sli4_queue_alloc(struct lpfc_hba *phba, uint32_t entry_size,
11327 uint32_t entry_count)
11328{
11329 struct lpfc_queue *queue;
11330 struct lpfc_dmabuf *dmabuf;
11331 int x, total_qe_count;
11332 void *dma_pointer;
James Smartcb5172e2010-03-15 11:25:07 -040011333 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smart4f774512009-05-22 14:52:35 -040011334
James Smartcb5172e2010-03-15 11:25:07 -040011335 if (!phba->sli4_hba.pc_sli4_params.supported)
11336 hw_page_size = SLI4_PAGE_SIZE;
11337
James Smart4f774512009-05-22 14:52:35 -040011338 queue = kzalloc(sizeof(struct lpfc_queue) +
11339 (sizeof(union sli4_qe) * entry_count), GFP_KERNEL);
11340 if (!queue)
11341 return NULL;
James Smartcb5172e2010-03-15 11:25:07 -040011342 queue->page_count = (ALIGN(entry_size * entry_count,
11343 hw_page_size))/hw_page_size;
James Smart4f774512009-05-22 14:52:35 -040011344 INIT_LIST_HEAD(&queue->list);
11345 INIT_LIST_HEAD(&queue->page_list);
11346 INIT_LIST_HEAD(&queue->child_list);
11347 for (x = 0, total_qe_count = 0; x < queue->page_count; x++) {
11348 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
11349 if (!dmabuf)
11350 goto out_fail;
11351 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
James Smartcb5172e2010-03-15 11:25:07 -040011352 hw_page_size, &dmabuf->phys,
James Smart4f774512009-05-22 14:52:35 -040011353 GFP_KERNEL);
11354 if (!dmabuf->virt) {
11355 kfree(dmabuf);
11356 goto out_fail;
11357 }
James Smartcb5172e2010-03-15 11:25:07 -040011358 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -040011359 dmabuf->buffer_tag = x;
11360 list_add_tail(&dmabuf->list, &queue->page_list);
11361 /* initialize queue's entry array */
11362 dma_pointer = dmabuf->virt;
11363 for (; total_qe_count < entry_count &&
James Smartcb5172e2010-03-15 11:25:07 -040011364 dma_pointer < (hw_page_size + dmabuf->virt);
James Smart4f774512009-05-22 14:52:35 -040011365 total_qe_count++, dma_pointer += entry_size) {
11366 queue->qe[total_qe_count].address = dma_pointer;
11367 }
11368 }
11369 queue->entry_size = entry_size;
11370 queue->entry_count = entry_count;
James Smart73d91e52011-10-10 21:32:10 -040011371
11372 /*
11373 * entry_repost is calculated based on the number of entries in the
11374 * queue. This works out except for RQs. If buffers are NOT initially
11375 * posted for every RQE, entry_repost should be adjusted accordingly.
11376 */
11377 queue->entry_repost = (entry_count >> 3);
11378 if (queue->entry_repost < LPFC_QUEUE_MIN_REPOST)
11379 queue->entry_repost = LPFC_QUEUE_MIN_REPOST;
James Smart4f774512009-05-22 14:52:35 -040011380 queue->phba = phba;
11381
11382 return queue;
11383out_fail:
11384 lpfc_sli4_queue_free(queue);
11385 return NULL;
11386}
11387
11388/**
11389 * lpfc_eq_create - Create an Event Queue on the HBA
11390 * @phba: HBA structure that indicates port to create a queue on.
11391 * @eq: The queue structure to use to create the event queue.
11392 * @imax: The maximum interrupt per second limit.
11393 *
11394 * This function creates an event queue, as detailed in @eq, on a port,
11395 * described by @phba by sending an EQ_CREATE mailbox command to the HBA.
11396 *
11397 * The @phba struct is used to send mailbox command to HBA. The @eq struct
11398 * is used to get the entry count and entry size that are necessary to
11399 * determine the number of pages to allocate and use for this queue. This
11400 * function will send the EQ_CREATE mailbox command to the HBA to setup the
11401 * event queue. This function is asynchronous and will wait for the mailbox
11402 * command to finish before continuing.
11403 *
11404 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040011405 * memory this function will return -ENOMEM. If the queue create mailbox command
11406 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040011407 **/
11408uint32_t
11409lpfc_eq_create(struct lpfc_hba *phba, struct lpfc_queue *eq, uint16_t imax)
11410{
11411 struct lpfc_mbx_eq_create *eq_create;
11412 LPFC_MBOXQ_t *mbox;
11413 int rc, length, status = 0;
11414 struct lpfc_dmabuf *dmabuf;
11415 uint32_t shdr_status, shdr_add_status;
11416 union lpfc_sli4_cfg_shdr *shdr;
11417 uint16_t dmult;
James Smart49198b32010-04-06 15:04:33 -040011418 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
11419
11420 if (!phba->sli4_hba.pc_sli4_params.supported)
11421 hw_page_size = SLI4_PAGE_SIZE;
James Smart4f774512009-05-22 14:52:35 -040011422
11423 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
11424 if (!mbox)
11425 return -ENOMEM;
11426 length = (sizeof(struct lpfc_mbx_eq_create) -
11427 sizeof(struct lpfc_sli4_cfg_mhdr));
11428 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
11429 LPFC_MBOX_OPCODE_EQ_CREATE,
11430 length, LPFC_SLI4_MBX_EMBED);
11431 eq_create = &mbox->u.mqe.un.eq_create;
11432 bf_set(lpfc_mbx_eq_create_num_pages, &eq_create->u.request,
11433 eq->page_count);
11434 bf_set(lpfc_eq_context_size, &eq_create->u.request.context,
11435 LPFC_EQE_SIZE);
11436 bf_set(lpfc_eq_context_valid, &eq_create->u.request.context, 1);
11437 /* Calculate delay multiper from maximum interrupt per second */
11438 dmult = LPFC_DMULT_CONST/imax - 1;
11439 bf_set(lpfc_eq_context_delay_multi, &eq_create->u.request.context,
11440 dmult);
11441 switch (eq->entry_count) {
11442 default:
11443 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11444 "0360 Unsupported EQ count. (%d)\n",
11445 eq->entry_count);
11446 if (eq->entry_count < 256)
11447 return -EINVAL;
11448 /* otherwise default to smallest count (drop through) */
11449 case 256:
11450 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
11451 LPFC_EQ_CNT_256);
11452 break;
11453 case 512:
11454 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
11455 LPFC_EQ_CNT_512);
11456 break;
11457 case 1024:
11458 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
11459 LPFC_EQ_CNT_1024);
11460 break;
11461 case 2048:
11462 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
11463 LPFC_EQ_CNT_2048);
11464 break;
11465 case 4096:
11466 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
11467 LPFC_EQ_CNT_4096);
11468 break;
11469 }
11470 list_for_each_entry(dmabuf, &eq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040011471 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -040011472 eq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
11473 putPaddrLow(dmabuf->phys);
11474 eq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
11475 putPaddrHigh(dmabuf->phys);
11476 }
11477 mbox->vport = phba->pport;
11478 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
11479 mbox->context1 = NULL;
11480 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
11481 shdr = (union lpfc_sli4_cfg_shdr *) &eq_create->header.cfg_shdr;
11482 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
11483 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
11484 if (shdr_status || shdr_add_status || rc) {
11485 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11486 "2500 EQ_CREATE mailbox failed with "
11487 "status x%x add_status x%x, mbx status x%x\n",
11488 shdr_status, shdr_add_status, rc);
11489 status = -ENXIO;
11490 }
11491 eq->type = LPFC_EQ;
11492 eq->subtype = LPFC_NONE;
11493 eq->queue_id = bf_get(lpfc_mbx_eq_create_q_id, &eq_create->u.response);
11494 if (eq->queue_id == 0xFFFF)
11495 status = -ENXIO;
11496 eq->host_index = 0;
11497 eq->hba_index = 0;
11498
James Smart8fa38512009-07-19 10:01:03 -040011499 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040011500 return status;
11501}
11502
11503/**
11504 * lpfc_cq_create - Create a Completion Queue on the HBA
11505 * @phba: HBA structure that indicates port to create a queue on.
11506 * @cq: The queue structure to use to create the completion queue.
11507 * @eq: The event queue to bind this completion queue to.
11508 *
11509 * This function creates a completion queue, as detailed in @wq, on a port,
11510 * described by @phba by sending a CQ_CREATE mailbox command to the HBA.
11511 *
11512 * The @phba struct is used to send mailbox command to HBA. The @cq struct
11513 * is used to get the entry count and entry size that are necessary to
11514 * determine the number of pages to allocate and use for this queue. The @eq
11515 * is used to indicate which event queue to bind this completion queue to. This
11516 * function will send the CQ_CREATE mailbox command to the HBA to setup the
11517 * completion queue. This function is asynchronous and will wait for the mailbox
11518 * command to finish before continuing.
11519 *
11520 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040011521 * memory this function will return -ENOMEM. If the queue create mailbox command
11522 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040011523 **/
11524uint32_t
11525lpfc_cq_create(struct lpfc_hba *phba, struct lpfc_queue *cq,
11526 struct lpfc_queue *eq, uint32_t type, uint32_t subtype)
11527{
11528 struct lpfc_mbx_cq_create *cq_create;
11529 struct lpfc_dmabuf *dmabuf;
11530 LPFC_MBOXQ_t *mbox;
11531 int rc, length, status = 0;
11532 uint32_t shdr_status, shdr_add_status;
11533 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040011534 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
11535
11536 if (!phba->sli4_hba.pc_sli4_params.supported)
11537 hw_page_size = SLI4_PAGE_SIZE;
11538
James Smart4f774512009-05-22 14:52:35 -040011539 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
11540 if (!mbox)
11541 return -ENOMEM;
11542 length = (sizeof(struct lpfc_mbx_cq_create) -
11543 sizeof(struct lpfc_sli4_cfg_mhdr));
11544 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
11545 LPFC_MBOX_OPCODE_CQ_CREATE,
11546 length, LPFC_SLI4_MBX_EMBED);
11547 cq_create = &mbox->u.mqe.un.cq_create;
James Smart5a6f1332011-03-11 16:05:35 -050011548 shdr = (union lpfc_sli4_cfg_shdr *) &cq_create->header.cfg_shdr;
James Smart4f774512009-05-22 14:52:35 -040011549 bf_set(lpfc_mbx_cq_create_num_pages, &cq_create->u.request,
11550 cq->page_count);
11551 bf_set(lpfc_cq_context_event, &cq_create->u.request.context, 1);
11552 bf_set(lpfc_cq_context_valid, &cq_create->u.request.context, 1);
James Smart5a6f1332011-03-11 16:05:35 -050011553 bf_set(lpfc_mbox_hdr_version, &shdr->request,
11554 phba->sli4_hba.pc_sli4_params.cqv);
11555 if (phba->sli4_hba.pc_sli4_params.cqv == LPFC_Q_CREATE_VERSION_2) {
James Smartc31098c2011-04-16 11:03:33 -040011556 /* FW only supports 1. Should be PAGE_SIZE/SLI4_PAGE_SIZE */
11557 bf_set(lpfc_mbx_cq_create_page_size, &cq_create->u.request, 1);
James Smart5a6f1332011-03-11 16:05:35 -050011558 bf_set(lpfc_cq_eq_id_2, &cq_create->u.request.context,
11559 eq->queue_id);
11560 } else {
11561 bf_set(lpfc_cq_eq_id, &cq_create->u.request.context,
11562 eq->queue_id);
11563 }
James Smart4f774512009-05-22 14:52:35 -040011564 switch (cq->entry_count) {
11565 default:
11566 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11567 "0361 Unsupported CQ count. (%d)\n",
11568 cq->entry_count);
11569 if (cq->entry_count < 256)
11570 return -EINVAL;
11571 /* otherwise default to smallest count (drop through) */
11572 case 256:
11573 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
11574 LPFC_CQ_CNT_256);
11575 break;
11576 case 512:
11577 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
11578 LPFC_CQ_CNT_512);
11579 break;
11580 case 1024:
11581 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
11582 LPFC_CQ_CNT_1024);
11583 break;
11584 }
11585 list_for_each_entry(dmabuf, &cq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040011586 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -040011587 cq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
11588 putPaddrLow(dmabuf->phys);
11589 cq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
11590 putPaddrHigh(dmabuf->phys);
11591 }
11592 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
11593
11594 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart4f774512009-05-22 14:52:35 -040011595 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
11596 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
11597 if (shdr_status || shdr_add_status || rc) {
11598 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11599 "2501 CQ_CREATE mailbox failed with "
11600 "status x%x add_status x%x, mbx status x%x\n",
11601 shdr_status, shdr_add_status, rc);
11602 status = -ENXIO;
11603 goto out;
11604 }
11605 cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
11606 if (cq->queue_id == 0xFFFF) {
11607 status = -ENXIO;
11608 goto out;
11609 }
11610 /* link the cq onto the parent eq child list */
11611 list_add_tail(&cq->list, &eq->child_list);
11612 /* Set up completion queue's type and subtype */
11613 cq->type = type;
11614 cq->subtype = subtype;
11615 cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
James Smart2a622bf2011-02-16 12:40:06 -050011616 cq->assoc_qid = eq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040011617 cq->host_index = 0;
11618 cq->hba_index = 0;
James Smart4f774512009-05-22 14:52:35 -040011619
James Smart8fa38512009-07-19 10:01:03 -040011620out:
11621 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040011622 return status;
11623}
11624
11625/**
James Smartb19a0612010-04-06 14:48:51 -040011626 * lpfc_mq_create_fb_init - Send MCC_CREATE without async events registration
James Smart04c68492009-05-22 14:52:52 -040011627 * @phba: HBA structure that indicates port to create a queue on.
11628 * @mq: The queue structure to use to create the mailbox queue.
James Smartb19a0612010-04-06 14:48:51 -040011629 * @mbox: An allocated pointer to type LPFC_MBOXQ_t
11630 * @cq: The completion queue to associate with this cq.
James Smart04c68492009-05-22 14:52:52 -040011631 *
James Smartb19a0612010-04-06 14:48:51 -040011632 * This function provides failback (fb) functionality when the
11633 * mq_create_ext fails on older FW generations. It's purpose is identical
11634 * to mq_create_ext otherwise.
James Smart04c68492009-05-22 14:52:52 -040011635 *
James Smartb19a0612010-04-06 14:48:51 -040011636 * This routine cannot fail as all attributes were previously accessed and
11637 * initialized in mq_create_ext.
James Smart04c68492009-05-22 14:52:52 -040011638 **/
James Smartb19a0612010-04-06 14:48:51 -040011639static void
11640lpfc_mq_create_fb_init(struct lpfc_hba *phba, struct lpfc_queue *mq,
11641 LPFC_MBOXQ_t *mbox, struct lpfc_queue *cq)
James Smart04c68492009-05-22 14:52:52 -040011642{
11643 struct lpfc_mbx_mq_create *mq_create;
11644 struct lpfc_dmabuf *dmabuf;
James Smartb19a0612010-04-06 14:48:51 -040011645 int length;
James Smart04c68492009-05-22 14:52:52 -040011646
James Smart04c68492009-05-22 14:52:52 -040011647 length = (sizeof(struct lpfc_mbx_mq_create) -
11648 sizeof(struct lpfc_sli4_cfg_mhdr));
11649 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
11650 LPFC_MBOX_OPCODE_MQ_CREATE,
11651 length, LPFC_SLI4_MBX_EMBED);
11652 mq_create = &mbox->u.mqe.un.mq_create;
11653 bf_set(lpfc_mbx_mq_create_num_pages, &mq_create->u.request,
James Smartb19a0612010-04-06 14:48:51 -040011654 mq->page_count);
James Smart04c68492009-05-22 14:52:52 -040011655 bf_set(lpfc_mq_context_cq_id, &mq_create->u.request.context,
James Smartb19a0612010-04-06 14:48:51 -040011656 cq->queue_id);
James Smart04c68492009-05-22 14:52:52 -040011657 bf_set(lpfc_mq_context_valid, &mq_create->u.request.context, 1);
11658 switch (mq->entry_count) {
James Smart04c68492009-05-22 14:52:52 -040011659 case 16:
James Smart5a6f1332011-03-11 16:05:35 -050011660 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
11661 LPFC_MQ_RING_SIZE_16);
James Smart04c68492009-05-22 14:52:52 -040011662 break;
11663 case 32:
James Smart5a6f1332011-03-11 16:05:35 -050011664 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
11665 LPFC_MQ_RING_SIZE_32);
James Smart04c68492009-05-22 14:52:52 -040011666 break;
11667 case 64:
James Smart5a6f1332011-03-11 16:05:35 -050011668 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
11669 LPFC_MQ_RING_SIZE_64);
James Smart04c68492009-05-22 14:52:52 -040011670 break;
11671 case 128:
James Smart5a6f1332011-03-11 16:05:35 -050011672 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
11673 LPFC_MQ_RING_SIZE_128);
James Smart04c68492009-05-22 14:52:52 -040011674 break;
11675 }
11676 list_for_each_entry(dmabuf, &mq->page_list, list) {
11677 mq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
James Smartb19a0612010-04-06 14:48:51 -040011678 putPaddrLow(dmabuf->phys);
James Smart04c68492009-05-22 14:52:52 -040011679 mq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
James Smartb19a0612010-04-06 14:48:51 -040011680 putPaddrHigh(dmabuf->phys);
11681 }
11682}
11683
11684/**
11685 * lpfc_mq_create - Create a mailbox Queue on the HBA
11686 * @phba: HBA structure that indicates port to create a queue on.
11687 * @mq: The queue structure to use to create the mailbox queue.
11688 * @cq: The completion queue to associate with this cq.
11689 * @subtype: The queue's subtype.
11690 *
11691 * This function creates a mailbox queue, as detailed in @mq, on a port,
11692 * described by @phba by sending a MQ_CREATE mailbox command to the HBA.
11693 *
11694 * The @phba struct is used to send mailbox command to HBA. The @cq struct
11695 * is used to get the entry count and entry size that are necessary to
11696 * determine the number of pages to allocate and use for this queue. This
11697 * function will send the MQ_CREATE mailbox command to the HBA to setup the
11698 * mailbox queue. This function is asynchronous and will wait for the mailbox
11699 * command to finish before continuing.
11700 *
11701 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040011702 * memory this function will return -ENOMEM. If the queue create mailbox command
11703 * fails this function will return -ENXIO.
James Smartb19a0612010-04-06 14:48:51 -040011704 **/
11705int32_t
11706lpfc_mq_create(struct lpfc_hba *phba, struct lpfc_queue *mq,
11707 struct lpfc_queue *cq, uint32_t subtype)
11708{
11709 struct lpfc_mbx_mq_create *mq_create;
11710 struct lpfc_mbx_mq_create_ext *mq_create_ext;
11711 struct lpfc_dmabuf *dmabuf;
11712 LPFC_MBOXQ_t *mbox;
11713 int rc, length, status = 0;
11714 uint32_t shdr_status, shdr_add_status;
11715 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040011716 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smartb19a0612010-04-06 14:48:51 -040011717
James Smart49198b32010-04-06 15:04:33 -040011718 if (!phba->sli4_hba.pc_sli4_params.supported)
11719 hw_page_size = SLI4_PAGE_SIZE;
James Smartb19a0612010-04-06 14:48:51 -040011720
11721 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
11722 if (!mbox)
11723 return -ENOMEM;
11724 length = (sizeof(struct lpfc_mbx_mq_create_ext) -
11725 sizeof(struct lpfc_sli4_cfg_mhdr));
11726 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
11727 LPFC_MBOX_OPCODE_MQ_CREATE_EXT,
11728 length, LPFC_SLI4_MBX_EMBED);
11729
11730 mq_create_ext = &mbox->u.mqe.un.mq_create_ext;
James Smart5a6f1332011-03-11 16:05:35 -050011731 shdr = (union lpfc_sli4_cfg_shdr *) &mq_create_ext->header.cfg_shdr;
James Smart70f3c072010-12-15 17:57:33 -050011732 bf_set(lpfc_mbx_mq_create_ext_num_pages,
11733 &mq_create_ext->u.request, mq->page_count);
11734 bf_set(lpfc_mbx_mq_create_ext_async_evt_link,
11735 &mq_create_ext->u.request, 1);
11736 bf_set(lpfc_mbx_mq_create_ext_async_evt_fip,
James Smartb19a0612010-04-06 14:48:51 -040011737 &mq_create_ext->u.request, 1);
11738 bf_set(lpfc_mbx_mq_create_ext_async_evt_group5,
11739 &mq_create_ext->u.request, 1);
James Smart70f3c072010-12-15 17:57:33 -050011740 bf_set(lpfc_mbx_mq_create_ext_async_evt_fc,
11741 &mq_create_ext->u.request, 1);
11742 bf_set(lpfc_mbx_mq_create_ext_async_evt_sli,
11743 &mq_create_ext->u.request, 1);
James Smartb19a0612010-04-06 14:48:51 -040011744 bf_set(lpfc_mq_context_valid, &mq_create_ext->u.request.context, 1);
James Smart5a6f1332011-03-11 16:05:35 -050011745 bf_set(lpfc_mbox_hdr_version, &shdr->request,
11746 phba->sli4_hba.pc_sli4_params.mqv);
11747 if (phba->sli4_hba.pc_sli4_params.mqv == LPFC_Q_CREATE_VERSION_1)
11748 bf_set(lpfc_mbx_mq_create_ext_cq_id, &mq_create_ext->u.request,
11749 cq->queue_id);
11750 else
11751 bf_set(lpfc_mq_context_cq_id, &mq_create_ext->u.request.context,
11752 cq->queue_id);
James Smartb19a0612010-04-06 14:48:51 -040011753 switch (mq->entry_count) {
11754 default:
11755 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11756 "0362 Unsupported MQ count. (%d)\n",
11757 mq->entry_count);
11758 if (mq->entry_count < 16)
11759 return -EINVAL;
11760 /* otherwise default to smallest count (drop through) */
11761 case 16:
James Smart5a6f1332011-03-11 16:05:35 -050011762 bf_set(lpfc_mq_context_ring_size,
11763 &mq_create_ext->u.request.context,
11764 LPFC_MQ_RING_SIZE_16);
James Smartb19a0612010-04-06 14:48:51 -040011765 break;
11766 case 32:
James Smart5a6f1332011-03-11 16:05:35 -050011767 bf_set(lpfc_mq_context_ring_size,
11768 &mq_create_ext->u.request.context,
11769 LPFC_MQ_RING_SIZE_32);
James Smartb19a0612010-04-06 14:48:51 -040011770 break;
11771 case 64:
James Smart5a6f1332011-03-11 16:05:35 -050011772 bf_set(lpfc_mq_context_ring_size,
11773 &mq_create_ext->u.request.context,
11774 LPFC_MQ_RING_SIZE_64);
James Smartb19a0612010-04-06 14:48:51 -040011775 break;
11776 case 128:
James Smart5a6f1332011-03-11 16:05:35 -050011777 bf_set(lpfc_mq_context_ring_size,
11778 &mq_create_ext->u.request.context,
11779 LPFC_MQ_RING_SIZE_128);
James Smartb19a0612010-04-06 14:48:51 -040011780 break;
11781 }
11782 list_for_each_entry(dmabuf, &mq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040011783 memset(dmabuf->virt, 0, hw_page_size);
James Smartb19a0612010-04-06 14:48:51 -040011784 mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_lo =
11785 putPaddrLow(dmabuf->phys);
11786 mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_hi =
James Smart04c68492009-05-22 14:52:52 -040011787 putPaddrHigh(dmabuf->phys);
11788 }
11789 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
James Smartb19a0612010-04-06 14:48:51 -040011790 mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
11791 &mq_create_ext->u.response);
11792 if (rc != MBX_SUCCESS) {
11793 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
11794 "2795 MQ_CREATE_EXT failed with "
11795 "status x%x. Failback to MQ_CREATE.\n",
11796 rc);
11797 lpfc_mq_create_fb_init(phba, mq, mbox, cq);
11798 mq_create = &mbox->u.mqe.un.mq_create;
11799 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
11800 shdr = (union lpfc_sli4_cfg_shdr *) &mq_create->header.cfg_shdr;
11801 mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
11802 &mq_create->u.response);
11803 }
11804
James Smart04c68492009-05-22 14:52:52 -040011805 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart04c68492009-05-22 14:52:52 -040011806 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
11807 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
11808 if (shdr_status || shdr_add_status || rc) {
11809 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11810 "2502 MQ_CREATE mailbox failed with "
11811 "status x%x add_status x%x, mbx status x%x\n",
11812 shdr_status, shdr_add_status, rc);
11813 status = -ENXIO;
11814 goto out;
11815 }
James Smart04c68492009-05-22 14:52:52 -040011816 if (mq->queue_id == 0xFFFF) {
11817 status = -ENXIO;
11818 goto out;
11819 }
11820 mq->type = LPFC_MQ;
James Smart2a622bf2011-02-16 12:40:06 -050011821 mq->assoc_qid = cq->queue_id;
James Smart04c68492009-05-22 14:52:52 -040011822 mq->subtype = subtype;
11823 mq->host_index = 0;
11824 mq->hba_index = 0;
11825
11826 /* link the mq onto the parent cq child list */
11827 list_add_tail(&mq->list, &cq->child_list);
11828out:
James Smart8fa38512009-07-19 10:01:03 -040011829 mempool_free(mbox, phba->mbox_mem_pool);
James Smart04c68492009-05-22 14:52:52 -040011830 return status;
11831}
11832
11833/**
James Smart4f774512009-05-22 14:52:35 -040011834 * lpfc_wq_create - Create a Work Queue on the HBA
11835 * @phba: HBA structure that indicates port to create a queue on.
11836 * @wq: The queue structure to use to create the work queue.
11837 * @cq: The completion queue to bind this work queue to.
11838 * @subtype: The subtype of the work queue indicating its functionality.
11839 *
11840 * This function creates a work queue, as detailed in @wq, on a port, described
11841 * by @phba by sending a WQ_CREATE mailbox command to the HBA.
11842 *
11843 * The @phba struct is used to send mailbox command to HBA. The @wq struct
11844 * is used to get the entry count and entry size that are necessary to
11845 * determine the number of pages to allocate and use for this queue. The @cq
11846 * is used to indicate which completion queue to bind this work queue to. This
11847 * function will send the WQ_CREATE mailbox command to the HBA to setup the
11848 * work queue. This function is asynchronous and will wait for the mailbox
11849 * command to finish before continuing.
11850 *
11851 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040011852 * memory this function will return -ENOMEM. If the queue create mailbox command
11853 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040011854 **/
11855uint32_t
11856lpfc_wq_create(struct lpfc_hba *phba, struct lpfc_queue *wq,
11857 struct lpfc_queue *cq, uint32_t subtype)
11858{
11859 struct lpfc_mbx_wq_create *wq_create;
11860 struct lpfc_dmabuf *dmabuf;
11861 LPFC_MBOXQ_t *mbox;
11862 int rc, length, status = 0;
11863 uint32_t shdr_status, shdr_add_status;
11864 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040011865 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smart5a6f1332011-03-11 16:05:35 -050011866 struct dma_address *page;
James Smart49198b32010-04-06 15:04:33 -040011867
11868 if (!phba->sli4_hba.pc_sli4_params.supported)
11869 hw_page_size = SLI4_PAGE_SIZE;
James Smart4f774512009-05-22 14:52:35 -040011870
11871 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
11872 if (!mbox)
11873 return -ENOMEM;
11874 length = (sizeof(struct lpfc_mbx_wq_create) -
11875 sizeof(struct lpfc_sli4_cfg_mhdr));
11876 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
11877 LPFC_MBOX_OPCODE_FCOE_WQ_CREATE,
11878 length, LPFC_SLI4_MBX_EMBED);
11879 wq_create = &mbox->u.mqe.un.wq_create;
James Smart5a6f1332011-03-11 16:05:35 -050011880 shdr = (union lpfc_sli4_cfg_shdr *) &wq_create->header.cfg_shdr;
James Smart4f774512009-05-22 14:52:35 -040011881 bf_set(lpfc_mbx_wq_create_num_pages, &wq_create->u.request,
11882 wq->page_count);
11883 bf_set(lpfc_mbx_wq_create_cq_id, &wq_create->u.request,
11884 cq->queue_id);
James Smart5a6f1332011-03-11 16:05:35 -050011885 bf_set(lpfc_mbox_hdr_version, &shdr->request,
11886 phba->sli4_hba.pc_sli4_params.wqv);
11887 if (phba->sli4_hba.pc_sli4_params.wqv == LPFC_Q_CREATE_VERSION_1) {
11888 bf_set(lpfc_mbx_wq_create_wqe_count, &wq_create->u.request_1,
11889 wq->entry_count);
11890 switch (wq->entry_size) {
11891 default:
11892 case 64:
11893 bf_set(lpfc_mbx_wq_create_wqe_size,
11894 &wq_create->u.request_1,
11895 LPFC_WQ_WQE_SIZE_64);
11896 break;
11897 case 128:
11898 bf_set(lpfc_mbx_wq_create_wqe_size,
11899 &wq_create->u.request_1,
11900 LPFC_WQ_WQE_SIZE_128);
11901 break;
11902 }
11903 bf_set(lpfc_mbx_wq_create_page_size, &wq_create->u.request_1,
11904 (PAGE_SIZE/SLI4_PAGE_SIZE));
11905 page = wq_create->u.request_1.page;
11906 } else {
11907 page = wq_create->u.request.page;
11908 }
James Smart4f774512009-05-22 14:52:35 -040011909 list_for_each_entry(dmabuf, &wq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040011910 memset(dmabuf->virt, 0, hw_page_size);
James Smart5a6f1332011-03-11 16:05:35 -050011911 page[dmabuf->buffer_tag].addr_lo = putPaddrLow(dmabuf->phys);
11912 page[dmabuf->buffer_tag].addr_hi = putPaddrHigh(dmabuf->phys);
James Smart4f774512009-05-22 14:52:35 -040011913 }
11914 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
11915 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart4f774512009-05-22 14:52:35 -040011916 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
11917 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
11918 if (shdr_status || shdr_add_status || rc) {
11919 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11920 "2503 WQ_CREATE mailbox failed with "
11921 "status x%x add_status x%x, mbx status x%x\n",
11922 shdr_status, shdr_add_status, rc);
11923 status = -ENXIO;
11924 goto out;
11925 }
11926 wq->queue_id = bf_get(lpfc_mbx_wq_create_q_id, &wq_create->u.response);
11927 if (wq->queue_id == 0xFFFF) {
11928 status = -ENXIO;
11929 goto out;
11930 }
11931 wq->type = LPFC_WQ;
James Smart2a622bf2011-02-16 12:40:06 -050011932 wq->assoc_qid = cq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040011933 wq->subtype = subtype;
11934 wq->host_index = 0;
11935 wq->hba_index = 0;
11936
11937 /* link the wq onto the parent cq child list */
11938 list_add_tail(&wq->list, &cq->child_list);
11939out:
James Smart8fa38512009-07-19 10:01:03 -040011940 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040011941 return status;
11942}
11943
11944/**
James Smart73d91e52011-10-10 21:32:10 -040011945 * lpfc_rq_adjust_repost - Adjust entry_repost for an RQ
11946 * @phba: HBA structure that indicates port to create a queue on.
11947 * @rq: The queue structure to use for the receive queue.
11948 * @qno: The associated HBQ number
11949 *
11950 *
11951 * For SLI4 we need to adjust the RQ repost value based on
11952 * the number of buffers that are initially posted to the RQ.
11953 */
11954void
11955lpfc_rq_adjust_repost(struct lpfc_hba *phba, struct lpfc_queue *rq, int qno)
11956{
11957 uint32_t cnt;
11958
11959 cnt = lpfc_hbq_defs[qno]->entry_count;
11960
11961 /* Recalc repost for RQs based on buffers initially posted */
11962 cnt = (cnt >> 3);
11963 if (cnt < LPFC_QUEUE_MIN_REPOST)
11964 cnt = LPFC_QUEUE_MIN_REPOST;
11965
11966 rq->entry_repost = cnt;
11967}
11968
11969/**
James Smart4f774512009-05-22 14:52:35 -040011970 * lpfc_rq_create - Create a Receive Queue on the HBA
11971 * @phba: HBA structure that indicates port to create a queue on.
11972 * @hrq: The queue structure to use to create the header receive queue.
11973 * @drq: The queue structure to use to create the data receive queue.
11974 * @cq: The completion queue to bind this work queue to.
11975 *
11976 * This function creates a receive buffer queue pair , as detailed in @hrq and
11977 * @drq, on a port, described by @phba by sending a RQ_CREATE mailbox command
11978 * to the HBA.
11979 *
11980 * The @phba struct is used to send mailbox command to HBA. The @drq and @hrq
11981 * struct is used to get the entry count that is necessary to determine the
11982 * number of pages to use for this queue. The @cq is used to indicate which
11983 * completion queue to bind received buffers that are posted to these queues to.
11984 * This function will send the RQ_CREATE mailbox command to the HBA to setup the
11985 * receive queue pair. This function is asynchronous and will wait for the
11986 * mailbox command to finish before continuing.
11987 *
11988 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040011989 * memory this function will return -ENOMEM. If the queue create mailbox command
11990 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040011991 **/
11992uint32_t
11993lpfc_rq_create(struct lpfc_hba *phba, struct lpfc_queue *hrq,
11994 struct lpfc_queue *drq, struct lpfc_queue *cq, uint32_t subtype)
11995{
11996 struct lpfc_mbx_rq_create *rq_create;
11997 struct lpfc_dmabuf *dmabuf;
11998 LPFC_MBOXQ_t *mbox;
11999 int rc, length, status = 0;
12000 uint32_t shdr_status, shdr_add_status;
12001 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040012002 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
12003
12004 if (!phba->sli4_hba.pc_sli4_params.supported)
12005 hw_page_size = SLI4_PAGE_SIZE;
James Smart4f774512009-05-22 14:52:35 -040012006
12007 if (hrq->entry_count != drq->entry_count)
12008 return -EINVAL;
12009 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12010 if (!mbox)
12011 return -ENOMEM;
12012 length = (sizeof(struct lpfc_mbx_rq_create) -
12013 sizeof(struct lpfc_sli4_cfg_mhdr));
12014 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
12015 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
12016 length, LPFC_SLI4_MBX_EMBED);
12017 rq_create = &mbox->u.mqe.un.rq_create;
James Smart5a6f1332011-03-11 16:05:35 -050012018 shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
12019 bf_set(lpfc_mbox_hdr_version, &shdr->request,
12020 phba->sli4_hba.pc_sli4_params.rqv);
12021 if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
12022 bf_set(lpfc_rq_context_rqe_count_1,
12023 &rq_create->u.request.context,
12024 hrq->entry_count);
12025 rq_create->u.request.context.buffer_size = LPFC_HDR_BUF_SIZE;
James Smartc31098c2011-04-16 11:03:33 -040012026 bf_set(lpfc_rq_context_rqe_size,
12027 &rq_create->u.request.context,
12028 LPFC_RQE_SIZE_8);
12029 bf_set(lpfc_rq_context_page_size,
12030 &rq_create->u.request.context,
12031 (PAGE_SIZE/SLI4_PAGE_SIZE));
James Smart5a6f1332011-03-11 16:05:35 -050012032 } else {
12033 switch (hrq->entry_count) {
12034 default:
12035 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12036 "2535 Unsupported RQ count. (%d)\n",
12037 hrq->entry_count);
12038 if (hrq->entry_count < 512)
12039 return -EINVAL;
12040 /* otherwise default to smallest count (drop through) */
12041 case 512:
12042 bf_set(lpfc_rq_context_rqe_count,
12043 &rq_create->u.request.context,
12044 LPFC_RQ_RING_SIZE_512);
12045 break;
12046 case 1024:
12047 bf_set(lpfc_rq_context_rqe_count,
12048 &rq_create->u.request.context,
12049 LPFC_RQ_RING_SIZE_1024);
12050 break;
12051 case 2048:
12052 bf_set(lpfc_rq_context_rqe_count,
12053 &rq_create->u.request.context,
12054 LPFC_RQ_RING_SIZE_2048);
12055 break;
12056 case 4096:
12057 bf_set(lpfc_rq_context_rqe_count,
12058 &rq_create->u.request.context,
12059 LPFC_RQ_RING_SIZE_4096);
12060 break;
12061 }
12062 bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
12063 LPFC_HDR_BUF_SIZE);
James Smart4f774512009-05-22 14:52:35 -040012064 }
12065 bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
12066 cq->queue_id);
12067 bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
12068 hrq->page_count);
James Smart4f774512009-05-22 14:52:35 -040012069 list_for_each_entry(dmabuf, &hrq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040012070 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -040012071 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
12072 putPaddrLow(dmabuf->phys);
12073 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
12074 putPaddrHigh(dmabuf->phys);
12075 }
12076 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
12077 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart4f774512009-05-22 14:52:35 -040012078 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12079 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12080 if (shdr_status || shdr_add_status || rc) {
12081 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12082 "2504 RQ_CREATE mailbox failed with "
12083 "status x%x add_status x%x, mbx status x%x\n",
12084 shdr_status, shdr_add_status, rc);
12085 status = -ENXIO;
12086 goto out;
12087 }
12088 hrq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
12089 if (hrq->queue_id == 0xFFFF) {
12090 status = -ENXIO;
12091 goto out;
12092 }
12093 hrq->type = LPFC_HRQ;
James Smart2a622bf2011-02-16 12:40:06 -050012094 hrq->assoc_qid = cq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040012095 hrq->subtype = subtype;
12096 hrq->host_index = 0;
12097 hrq->hba_index = 0;
12098
12099 /* now create the data queue */
12100 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
12101 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
12102 length, LPFC_SLI4_MBX_EMBED);
James Smart5a6f1332011-03-11 16:05:35 -050012103 bf_set(lpfc_mbox_hdr_version, &shdr->request,
12104 phba->sli4_hba.pc_sli4_params.rqv);
12105 if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
12106 bf_set(lpfc_rq_context_rqe_count_1,
James Smartc31098c2011-04-16 11:03:33 -040012107 &rq_create->u.request.context, hrq->entry_count);
James Smart5a6f1332011-03-11 16:05:35 -050012108 rq_create->u.request.context.buffer_size = LPFC_DATA_BUF_SIZE;
James Smartc31098c2011-04-16 11:03:33 -040012109 bf_set(lpfc_rq_context_rqe_size, &rq_create->u.request.context,
12110 LPFC_RQE_SIZE_8);
12111 bf_set(lpfc_rq_context_page_size, &rq_create->u.request.context,
12112 (PAGE_SIZE/SLI4_PAGE_SIZE));
James Smart5a6f1332011-03-11 16:05:35 -050012113 } else {
12114 switch (drq->entry_count) {
12115 default:
12116 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12117 "2536 Unsupported RQ count. (%d)\n",
12118 drq->entry_count);
12119 if (drq->entry_count < 512)
12120 return -EINVAL;
12121 /* otherwise default to smallest count (drop through) */
12122 case 512:
12123 bf_set(lpfc_rq_context_rqe_count,
12124 &rq_create->u.request.context,
12125 LPFC_RQ_RING_SIZE_512);
12126 break;
12127 case 1024:
12128 bf_set(lpfc_rq_context_rqe_count,
12129 &rq_create->u.request.context,
12130 LPFC_RQ_RING_SIZE_1024);
12131 break;
12132 case 2048:
12133 bf_set(lpfc_rq_context_rqe_count,
12134 &rq_create->u.request.context,
12135 LPFC_RQ_RING_SIZE_2048);
12136 break;
12137 case 4096:
12138 bf_set(lpfc_rq_context_rqe_count,
12139 &rq_create->u.request.context,
12140 LPFC_RQ_RING_SIZE_4096);
12141 break;
12142 }
12143 bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
12144 LPFC_DATA_BUF_SIZE);
James Smart4f774512009-05-22 14:52:35 -040012145 }
12146 bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
12147 cq->queue_id);
12148 bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
12149 drq->page_count);
James Smart4f774512009-05-22 14:52:35 -040012150 list_for_each_entry(dmabuf, &drq->page_list, list) {
12151 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
12152 putPaddrLow(dmabuf->phys);
12153 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
12154 putPaddrHigh(dmabuf->phys);
12155 }
12156 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
12157 /* The IOCTL status is embedded in the mailbox subheader. */
12158 shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
12159 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12160 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12161 if (shdr_status || shdr_add_status || rc) {
12162 status = -ENXIO;
12163 goto out;
12164 }
12165 drq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
12166 if (drq->queue_id == 0xFFFF) {
12167 status = -ENXIO;
12168 goto out;
12169 }
12170 drq->type = LPFC_DRQ;
James Smart2a622bf2011-02-16 12:40:06 -050012171 drq->assoc_qid = cq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040012172 drq->subtype = subtype;
12173 drq->host_index = 0;
12174 drq->hba_index = 0;
12175
12176 /* link the header and data RQs onto the parent cq child list */
12177 list_add_tail(&hrq->list, &cq->child_list);
12178 list_add_tail(&drq->list, &cq->child_list);
12179
12180out:
James Smart8fa38512009-07-19 10:01:03 -040012181 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040012182 return status;
12183}
12184
12185/**
12186 * lpfc_eq_destroy - Destroy an event Queue on the HBA
12187 * @eq: The queue structure associated with the queue to destroy.
12188 *
12189 * This function destroys a queue, as detailed in @eq by sending an mailbox
12190 * command, specific to the type of queue, to the HBA.
12191 *
12192 * The @eq struct is used to get the queue ID of the queue to destroy.
12193 *
12194 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040012195 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040012196 **/
12197uint32_t
12198lpfc_eq_destroy(struct lpfc_hba *phba, struct lpfc_queue *eq)
12199{
12200 LPFC_MBOXQ_t *mbox;
12201 int rc, length, status = 0;
12202 uint32_t shdr_status, shdr_add_status;
12203 union lpfc_sli4_cfg_shdr *shdr;
12204
12205 if (!eq)
12206 return -ENODEV;
12207 mbox = mempool_alloc(eq->phba->mbox_mem_pool, GFP_KERNEL);
12208 if (!mbox)
12209 return -ENOMEM;
12210 length = (sizeof(struct lpfc_mbx_eq_destroy) -
12211 sizeof(struct lpfc_sli4_cfg_mhdr));
12212 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
12213 LPFC_MBOX_OPCODE_EQ_DESTROY,
12214 length, LPFC_SLI4_MBX_EMBED);
12215 bf_set(lpfc_mbx_eq_destroy_q_id, &mbox->u.mqe.un.eq_destroy.u.request,
12216 eq->queue_id);
12217 mbox->vport = eq->phba->pport;
12218 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
12219
12220 rc = lpfc_sli_issue_mbox(eq->phba, mbox, MBX_POLL);
12221 /* The IOCTL status is embedded in the mailbox subheader. */
12222 shdr = (union lpfc_sli4_cfg_shdr *)
12223 &mbox->u.mqe.un.eq_destroy.header.cfg_shdr;
12224 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12225 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12226 if (shdr_status || shdr_add_status || rc) {
12227 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12228 "2505 EQ_DESTROY mailbox failed with "
12229 "status x%x add_status x%x, mbx status x%x\n",
12230 shdr_status, shdr_add_status, rc);
12231 status = -ENXIO;
12232 }
12233
12234 /* Remove eq from any list */
12235 list_del_init(&eq->list);
James Smart8fa38512009-07-19 10:01:03 -040012236 mempool_free(mbox, eq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040012237 return status;
12238}
12239
12240/**
12241 * lpfc_cq_destroy - Destroy a Completion Queue on the HBA
12242 * @cq: The queue structure associated with the queue to destroy.
12243 *
12244 * This function destroys a queue, as detailed in @cq by sending an mailbox
12245 * command, specific to the type of queue, to the HBA.
12246 *
12247 * The @cq struct is used to get the queue ID of the queue to destroy.
12248 *
12249 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040012250 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040012251 **/
12252uint32_t
12253lpfc_cq_destroy(struct lpfc_hba *phba, struct lpfc_queue *cq)
12254{
12255 LPFC_MBOXQ_t *mbox;
12256 int rc, length, status = 0;
12257 uint32_t shdr_status, shdr_add_status;
12258 union lpfc_sli4_cfg_shdr *shdr;
12259
12260 if (!cq)
12261 return -ENODEV;
12262 mbox = mempool_alloc(cq->phba->mbox_mem_pool, GFP_KERNEL);
12263 if (!mbox)
12264 return -ENOMEM;
12265 length = (sizeof(struct lpfc_mbx_cq_destroy) -
12266 sizeof(struct lpfc_sli4_cfg_mhdr));
12267 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
12268 LPFC_MBOX_OPCODE_CQ_DESTROY,
12269 length, LPFC_SLI4_MBX_EMBED);
12270 bf_set(lpfc_mbx_cq_destroy_q_id, &mbox->u.mqe.un.cq_destroy.u.request,
12271 cq->queue_id);
12272 mbox->vport = cq->phba->pport;
12273 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
12274 rc = lpfc_sli_issue_mbox(cq->phba, mbox, MBX_POLL);
12275 /* The IOCTL status is embedded in the mailbox subheader. */
12276 shdr = (union lpfc_sli4_cfg_shdr *)
12277 &mbox->u.mqe.un.wq_create.header.cfg_shdr;
12278 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12279 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12280 if (shdr_status || shdr_add_status || rc) {
12281 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12282 "2506 CQ_DESTROY mailbox failed with "
12283 "status x%x add_status x%x, mbx status x%x\n",
12284 shdr_status, shdr_add_status, rc);
12285 status = -ENXIO;
12286 }
12287 /* Remove cq from any list */
12288 list_del_init(&cq->list);
James Smart8fa38512009-07-19 10:01:03 -040012289 mempool_free(mbox, cq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040012290 return status;
12291}
12292
12293/**
James Smart04c68492009-05-22 14:52:52 -040012294 * lpfc_mq_destroy - Destroy a Mailbox Queue on the HBA
12295 * @qm: The queue structure associated with the queue to destroy.
12296 *
12297 * This function destroys a queue, as detailed in @mq by sending an mailbox
12298 * command, specific to the type of queue, to the HBA.
12299 *
12300 * The @mq struct is used to get the queue ID of the queue to destroy.
12301 *
12302 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040012303 * command fails this function will return -ENXIO.
James Smart04c68492009-05-22 14:52:52 -040012304 **/
12305uint32_t
12306lpfc_mq_destroy(struct lpfc_hba *phba, struct lpfc_queue *mq)
12307{
12308 LPFC_MBOXQ_t *mbox;
12309 int rc, length, status = 0;
12310 uint32_t shdr_status, shdr_add_status;
12311 union lpfc_sli4_cfg_shdr *shdr;
12312
12313 if (!mq)
12314 return -ENODEV;
12315 mbox = mempool_alloc(mq->phba->mbox_mem_pool, GFP_KERNEL);
12316 if (!mbox)
12317 return -ENOMEM;
12318 length = (sizeof(struct lpfc_mbx_mq_destroy) -
12319 sizeof(struct lpfc_sli4_cfg_mhdr));
12320 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
12321 LPFC_MBOX_OPCODE_MQ_DESTROY,
12322 length, LPFC_SLI4_MBX_EMBED);
12323 bf_set(lpfc_mbx_mq_destroy_q_id, &mbox->u.mqe.un.mq_destroy.u.request,
12324 mq->queue_id);
12325 mbox->vport = mq->phba->pport;
12326 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
12327 rc = lpfc_sli_issue_mbox(mq->phba, mbox, MBX_POLL);
12328 /* The IOCTL status is embedded in the mailbox subheader. */
12329 shdr = (union lpfc_sli4_cfg_shdr *)
12330 &mbox->u.mqe.un.mq_destroy.header.cfg_shdr;
12331 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12332 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12333 if (shdr_status || shdr_add_status || rc) {
12334 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12335 "2507 MQ_DESTROY mailbox failed with "
12336 "status x%x add_status x%x, mbx status x%x\n",
12337 shdr_status, shdr_add_status, rc);
12338 status = -ENXIO;
12339 }
12340 /* Remove mq from any list */
12341 list_del_init(&mq->list);
James Smart8fa38512009-07-19 10:01:03 -040012342 mempool_free(mbox, mq->phba->mbox_mem_pool);
James Smart04c68492009-05-22 14:52:52 -040012343 return status;
12344}
12345
12346/**
James Smart4f774512009-05-22 14:52:35 -040012347 * lpfc_wq_destroy - Destroy a Work Queue on the HBA
12348 * @wq: The queue structure associated with the queue to destroy.
12349 *
12350 * This function destroys a queue, as detailed in @wq by sending an mailbox
12351 * command, specific to the type of queue, to the HBA.
12352 *
12353 * The @wq struct is used to get the queue ID of the queue to destroy.
12354 *
12355 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040012356 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040012357 **/
12358uint32_t
12359lpfc_wq_destroy(struct lpfc_hba *phba, struct lpfc_queue *wq)
12360{
12361 LPFC_MBOXQ_t *mbox;
12362 int rc, length, status = 0;
12363 uint32_t shdr_status, shdr_add_status;
12364 union lpfc_sli4_cfg_shdr *shdr;
12365
12366 if (!wq)
12367 return -ENODEV;
12368 mbox = mempool_alloc(wq->phba->mbox_mem_pool, GFP_KERNEL);
12369 if (!mbox)
12370 return -ENOMEM;
12371 length = (sizeof(struct lpfc_mbx_wq_destroy) -
12372 sizeof(struct lpfc_sli4_cfg_mhdr));
12373 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
12374 LPFC_MBOX_OPCODE_FCOE_WQ_DESTROY,
12375 length, LPFC_SLI4_MBX_EMBED);
12376 bf_set(lpfc_mbx_wq_destroy_q_id, &mbox->u.mqe.un.wq_destroy.u.request,
12377 wq->queue_id);
12378 mbox->vport = wq->phba->pport;
12379 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
12380 rc = lpfc_sli_issue_mbox(wq->phba, mbox, MBX_POLL);
12381 shdr = (union lpfc_sli4_cfg_shdr *)
12382 &mbox->u.mqe.un.wq_destroy.header.cfg_shdr;
12383 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12384 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12385 if (shdr_status || shdr_add_status || rc) {
12386 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12387 "2508 WQ_DESTROY mailbox failed with "
12388 "status x%x add_status x%x, mbx status x%x\n",
12389 shdr_status, shdr_add_status, rc);
12390 status = -ENXIO;
12391 }
12392 /* Remove wq from any list */
12393 list_del_init(&wq->list);
James Smart8fa38512009-07-19 10:01:03 -040012394 mempool_free(mbox, wq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040012395 return status;
12396}
12397
12398/**
12399 * lpfc_rq_destroy - Destroy a Receive Queue on the HBA
12400 * @rq: The queue structure associated with the queue to destroy.
12401 *
12402 * This function destroys a queue, as detailed in @rq by sending an mailbox
12403 * command, specific to the type of queue, to the HBA.
12404 *
12405 * The @rq struct is used to get the queue ID of the queue to destroy.
12406 *
12407 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040012408 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040012409 **/
12410uint32_t
12411lpfc_rq_destroy(struct lpfc_hba *phba, struct lpfc_queue *hrq,
12412 struct lpfc_queue *drq)
12413{
12414 LPFC_MBOXQ_t *mbox;
12415 int rc, length, status = 0;
12416 uint32_t shdr_status, shdr_add_status;
12417 union lpfc_sli4_cfg_shdr *shdr;
12418
12419 if (!hrq || !drq)
12420 return -ENODEV;
12421 mbox = mempool_alloc(hrq->phba->mbox_mem_pool, GFP_KERNEL);
12422 if (!mbox)
12423 return -ENOMEM;
12424 length = (sizeof(struct lpfc_mbx_rq_destroy) -
James Smartfedd3b72011-02-16 12:39:24 -050012425 sizeof(struct lpfc_sli4_cfg_mhdr));
James Smart4f774512009-05-22 14:52:35 -040012426 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
12427 LPFC_MBOX_OPCODE_FCOE_RQ_DESTROY,
12428 length, LPFC_SLI4_MBX_EMBED);
12429 bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
12430 hrq->queue_id);
12431 mbox->vport = hrq->phba->pport;
12432 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
12433 rc = lpfc_sli_issue_mbox(hrq->phba, mbox, MBX_POLL);
12434 /* The IOCTL status is embedded in the mailbox subheader. */
12435 shdr = (union lpfc_sli4_cfg_shdr *)
12436 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
12437 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12438 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12439 if (shdr_status || shdr_add_status || rc) {
12440 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12441 "2509 RQ_DESTROY mailbox failed with "
12442 "status x%x add_status x%x, mbx status x%x\n",
12443 shdr_status, shdr_add_status, rc);
12444 if (rc != MBX_TIMEOUT)
12445 mempool_free(mbox, hrq->phba->mbox_mem_pool);
12446 return -ENXIO;
12447 }
12448 bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
12449 drq->queue_id);
12450 rc = lpfc_sli_issue_mbox(drq->phba, mbox, MBX_POLL);
12451 shdr = (union lpfc_sli4_cfg_shdr *)
12452 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
12453 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12454 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12455 if (shdr_status || shdr_add_status || rc) {
12456 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12457 "2510 RQ_DESTROY mailbox failed with "
12458 "status x%x add_status x%x, mbx status x%x\n",
12459 shdr_status, shdr_add_status, rc);
12460 status = -ENXIO;
12461 }
12462 list_del_init(&hrq->list);
12463 list_del_init(&drq->list);
James Smart8fa38512009-07-19 10:01:03 -040012464 mempool_free(mbox, hrq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040012465 return status;
12466}
12467
12468/**
12469 * lpfc_sli4_post_sgl - Post scatter gather list for an XRI to HBA
12470 * @phba: The virtual port for which this call being executed.
12471 * @pdma_phys_addr0: Physical address of the 1st SGL page.
12472 * @pdma_phys_addr1: Physical address of the 2nd SGL page.
12473 * @xritag: the xritag that ties this io to the SGL pages.
12474 *
12475 * This routine will post the sgl pages for the IO that has the xritag
12476 * that is in the iocbq structure. The xritag is assigned during iocbq
12477 * creation and persists for as long as the driver is loaded.
12478 * if the caller has fewer than 256 scatter gather segments to map then
12479 * pdma_phys_addr1 should be 0.
12480 * If the caller needs to map more than 256 scatter gather segment then
12481 * pdma_phys_addr1 should be a valid physical address.
12482 * physical address for SGLs must be 64 byte aligned.
12483 * If you are going to map 2 SGL's then the first one must have 256 entries
12484 * the second sgl can have between 1 and 256 entries.
12485 *
12486 * Return codes:
12487 * 0 - Success
12488 * -ENXIO, -ENOMEM - Failure
12489 **/
12490int
12491lpfc_sli4_post_sgl(struct lpfc_hba *phba,
12492 dma_addr_t pdma_phys_addr0,
12493 dma_addr_t pdma_phys_addr1,
12494 uint16_t xritag)
12495{
12496 struct lpfc_mbx_post_sgl_pages *post_sgl_pages;
12497 LPFC_MBOXQ_t *mbox;
12498 int rc;
12499 uint32_t shdr_status, shdr_add_status;
James Smart6d368e52011-05-24 11:44:12 -040012500 uint32_t mbox_tmo;
James Smart4f774512009-05-22 14:52:35 -040012501 union lpfc_sli4_cfg_shdr *shdr;
12502
12503 if (xritag == NO_XRI) {
12504 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12505 "0364 Invalid param:\n");
12506 return -EINVAL;
12507 }
12508
12509 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12510 if (!mbox)
12511 return -ENOMEM;
12512
12513 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
12514 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES,
12515 sizeof(struct lpfc_mbx_post_sgl_pages) -
James Smartfedd3b72011-02-16 12:39:24 -050012516 sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
James Smart4f774512009-05-22 14:52:35 -040012517
12518 post_sgl_pages = (struct lpfc_mbx_post_sgl_pages *)
12519 &mbox->u.mqe.un.post_sgl_pages;
12520 bf_set(lpfc_post_sgl_pages_xri, post_sgl_pages, xritag);
12521 bf_set(lpfc_post_sgl_pages_xricnt, post_sgl_pages, 1);
12522
12523 post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_lo =
12524 cpu_to_le32(putPaddrLow(pdma_phys_addr0));
12525 post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_hi =
12526 cpu_to_le32(putPaddrHigh(pdma_phys_addr0));
12527
12528 post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_lo =
12529 cpu_to_le32(putPaddrLow(pdma_phys_addr1));
12530 post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_hi =
12531 cpu_to_le32(putPaddrHigh(pdma_phys_addr1));
12532 if (!phba->sli4_hba.intr_enable)
12533 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
James Smart6d368e52011-05-24 11:44:12 -040012534 else {
12535 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_SLI4_CONFIG);
12536 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
12537 }
James Smart4f774512009-05-22 14:52:35 -040012538 /* The IOCTL status is embedded in the mailbox subheader. */
12539 shdr = (union lpfc_sli4_cfg_shdr *) &post_sgl_pages->header.cfg_shdr;
12540 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12541 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12542 if (rc != MBX_TIMEOUT)
12543 mempool_free(mbox, phba->mbox_mem_pool);
12544 if (shdr_status || shdr_add_status || rc) {
12545 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12546 "2511 POST_SGL mailbox failed with "
12547 "status x%x add_status x%x, mbx status x%x\n",
12548 shdr_status, shdr_add_status, rc);
12549 rc = -ENXIO;
12550 }
12551 return 0;
12552}
James Smart4f774512009-05-22 14:52:35 -040012553
12554/**
James Smart88a2cfb2011-07-22 18:36:33 -040012555 * lpfc_sli4_alloc_xri - Get an available rpi in the device's range
James Smart6d368e52011-05-24 11:44:12 -040012556 * @phba: pointer to lpfc hba data structure.
12557 *
12558 * This routine is invoked to post rpi header templates to the
James Smart88a2cfb2011-07-22 18:36:33 -040012559 * HBA consistent with the SLI-4 interface spec. This routine
12560 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
12561 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
James Smart6d368e52011-05-24 11:44:12 -040012562 *
James Smart88a2cfb2011-07-22 18:36:33 -040012563 * Returns
12564 * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
12565 * LPFC_RPI_ALLOC_ERROR if no rpis are available.
12566 **/
James Smart6d368e52011-05-24 11:44:12 -040012567uint16_t
12568lpfc_sli4_alloc_xri(struct lpfc_hba *phba)
12569{
12570 unsigned long xri;
12571
12572 /*
12573 * Fetch the next logical xri. Because this index is logical,
12574 * the driver starts at 0 each time.
12575 */
12576 spin_lock_irq(&phba->hbalock);
12577 xri = find_next_zero_bit(phba->sli4_hba.xri_bmask,
12578 phba->sli4_hba.max_cfg_param.max_xri, 0);
12579 if (xri >= phba->sli4_hba.max_cfg_param.max_xri) {
12580 spin_unlock_irq(&phba->hbalock);
12581 return NO_XRI;
12582 } else {
12583 set_bit(xri, phba->sli4_hba.xri_bmask);
12584 phba->sli4_hba.max_cfg_param.xri_used++;
12585 phba->sli4_hba.xri_count++;
12586 }
12587
12588 spin_unlock_irq(&phba->hbalock);
12589 return xri;
12590}
12591
12592/**
12593 * lpfc_sli4_free_xri - Release an xri for reuse.
12594 * @phba: pointer to lpfc hba data structure.
12595 *
12596 * This routine is invoked to release an xri to the pool of
12597 * available rpis maintained by the driver.
12598 **/
12599void
12600__lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
12601{
12602 if (test_and_clear_bit(xri, phba->sli4_hba.xri_bmask)) {
12603 phba->sli4_hba.xri_count--;
12604 phba->sli4_hba.max_cfg_param.xri_used--;
12605 }
12606}
12607
12608/**
12609 * lpfc_sli4_free_xri - Release an xri for reuse.
12610 * @phba: pointer to lpfc hba data structure.
12611 *
12612 * This routine is invoked to release an xri to the pool of
12613 * available rpis maintained by the driver.
12614 **/
12615void
12616lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
12617{
12618 spin_lock_irq(&phba->hbalock);
12619 __lpfc_sli4_free_xri(phba, xri);
12620 spin_unlock_irq(&phba->hbalock);
12621}
12622
12623/**
James Smart4f774512009-05-22 14:52:35 -040012624 * lpfc_sli4_next_xritag - Get an xritag for the io
12625 * @phba: Pointer to HBA context object.
12626 *
12627 * This function gets an xritag for the iocb. If there is no unused xritag
12628 * it will return 0xffff.
12629 * The function returns the allocated xritag if successful, else returns zero.
12630 * Zero is not a valid xritag.
12631 * The caller is not required to hold any lock.
12632 **/
12633uint16_t
12634lpfc_sli4_next_xritag(struct lpfc_hba *phba)
12635{
James Smart6d368e52011-05-24 11:44:12 -040012636 uint16_t xri_index;
James Smart4f774512009-05-22 14:52:35 -040012637
James Smart6d368e52011-05-24 11:44:12 -040012638 xri_index = lpfc_sli4_alloc_xri(phba);
12639 if (xri_index != NO_XRI)
12640 return xri_index;
12641
12642 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart4f774512009-05-22 14:52:35 -040012643 "2004 Failed to allocate XRI.last XRITAG is %d"
12644 " Max XRI is %d, Used XRI is %d\n",
James Smart6d368e52011-05-24 11:44:12 -040012645 xri_index,
James Smart4f774512009-05-22 14:52:35 -040012646 phba->sli4_hba.max_cfg_param.max_xri,
12647 phba->sli4_hba.max_cfg_param.xri_used);
James Smart6d368e52011-05-24 11:44:12 -040012648 return NO_XRI;
James Smart4f774512009-05-22 14:52:35 -040012649}
12650
12651/**
James Smart6d368e52011-05-24 11:44:12 -040012652 * lpfc_sli4_post_els_sgl_list - post a block of ELS sgls to the port.
James Smart4f774512009-05-22 14:52:35 -040012653 * @phba: pointer to lpfc hba data structure.
12654 *
12655 * This routine is invoked to post a block of driver's sgl pages to the
12656 * HBA using non-embedded mailbox command. No Lock is held. This routine
12657 * is only called when the driver is loading and after all IO has been
12658 * stopped.
12659 **/
12660int
James Smart6d368e52011-05-24 11:44:12 -040012661lpfc_sli4_post_els_sgl_list(struct lpfc_hba *phba)
James Smart4f774512009-05-22 14:52:35 -040012662{
12663 struct lpfc_sglq *sglq_entry;
12664 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
12665 struct sgl_page_pairs *sgl_pg_pairs;
12666 void *viraddr;
12667 LPFC_MBOXQ_t *mbox;
12668 uint32_t reqlen, alloclen, pg_pairs;
12669 uint32_t mbox_tmo;
James Smart6d368e52011-05-24 11:44:12 -040012670 uint16_t xritag_start = 0, lxri = 0;
James Smart4f774512009-05-22 14:52:35 -040012671 int els_xri_cnt, rc = 0;
12672 uint32_t shdr_status, shdr_add_status;
12673 union lpfc_sli4_cfg_shdr *shdr;
12674
12675 /* The number of sgls to be posted */
12676 els_xri_cnt = lpfc_sli4_get_els_iocb_cnt(phba);
12677
12678 reqlen = els_xri_cnt * sizeof(struct sgl_page_pairs) +
12679 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
James Smart49198b32010-04-06 15:04:33 -040012680 if (reqlen > SLI4_PAGE_SIZE) {
James Smart4f774512009-05-22 14:52:35 -040012681 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
12682 "2559 Block sgl registration required DMA "
12683 "size (%d) great than a page\n", reqlen);
12684 return -ENOMEM;
12685 }
12686 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
James Smart6d368e52011-05-24 11:44:12 -040012687 if (!mbox)
James Smart4f774512009-05-22 14:52:35 -040012688 return -ENOMEM;
James Smart4f774512009-05-22 14:52:35 -040012689
12690 /* Allocate DMA memory and set up the non-embedded mailbox command */
12691 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
12692 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
12693 LPFC_SLI4_MBX_NEMBED);
12694
12695 if (alloclen < reqlen) {
12696 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12697 "0285 Allocated DMA memory size (%d) is "
12698 "less than the requested DMA memory "
12699 "size (%d)\n", alloclen, reqlen);
12700 lpfc_sli4_mbox_cmd_free(phba, mbox);
12701 return -ENOMEM;
12702 }
James Smart4f774512009-05-22 14:52:35 -040012703 /* Set up the SGL pages in the non-embedded DMA pages */
James Smart6d368e52011-05-24 11:44:12 -040012704 viraddr = mbox->sge_array->addr[0];
James Smart4f774512009-05-22 14:52:35 -040012705 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
12706 sgl_pg_pairs = &sgl->sgl_pg_pairs;
12707
12708 for (pg_pairs = 0; pg_pairs < els_xri_cnt; pg_pairs++) {
12709 sglq_entry = phba->sli4_hba.lpfc_els_sgl_array[pg_pairs];
James Smart6d368e52011-05-24 11:44:12 -040012710
12711 /*
12712 * Assign the sglq a physical xri only if the driver has not
12713 * initialized those resources. A port reset only needs
12714 * the sglq's posted.
12715 */
12716 if (bf_get(lpfc_xri_rsrc_rdy, &phba->sli4_hba.sli4_flags) !=
12717 LPFC_XRI_RSRC_RDY) {
12718 lxri = lpfc_sli4_next_xritag(phba);
12719 if (lxri == NO_XRI) {
12720 lpfc_sli4_mbox_cmd_free(phba, mbox);
12721 return -ENOMEM;
12722 }
12723 sglq_entry->sli4_lxritag = lxri;
12724 sglq_entry->sli4_xritag = phba->sli4_hba.xri_ids[lxri];
12725 }
12726
James Smart4f774512009-05-22 14:52:35 -040012727 /* Set up the sge entry */
12728 sgl_pg_pairs->sgl_pg0_addr_lo =
12729 cpu_to_le32(putPaddrLow(sglq_entry->phys));
12730 sgl_pg_pairs->sgl_pg0_addr_hi =
12731 cpu_to_le32(putPaddrHigh(sglq_entry->phys));
12732 sgl_pg_pairs->sgl_pg1_addr_lo =
12733 cpu_to_le32(putPaddrLow(0));
12734 sgl_pg_pairs->sgl_pg1_addr_hi =
12735 cpu_to_le32(putPaddrHigh(0));
James Smart6d368e52011-05-24 11:44:12 -040012736
James Smart4f774512009-05-22 14:52:35 -040012737 /* Keep the first xritag on the list */
12738 if (pg_pairs == 0)
12739 xritag_start = sglq_entry->sli4_xritag;
12740 sgl_pg_pairs++;
12741 }
James Smart6d368e52011-05-24 11:44:12 -040012742
12743 /* Complete initialization and perform endian conversion. */
James Smart4f774512009-05-22 14:52:35 -040012744 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
James Smart6a9c52c2009-10-02 15:16:51 -040012745 bf_set(lpfc_post_sgl_pages_xricnt, sgl, els_xri_cnt);
James Smart4f774512009-05-22 14:52:35 -040012746 sgl->word0 = cpu_to_le32(sgl->word0);
James Smart4f774512009-05-22 14:52:35 -040012747 if (!phba->sli4_hba.intr_enable)
12748 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
12749 else {
12750 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_SLI4_CONFIG);
12751 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
12752 }
12753 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
12754 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12755 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12756 if (rc != MBX_TIMEOUT)
12757 lpfc_sli4_mbox_cmd_free(phba, mbox);
12758 if (shdr_status || shdr_add_status || rc) {
12759 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12760 "2513 POST_SGL_BLOCK mailbox command failed "
12761 "status x%x add_status x%x mbx status x%x\n",
12762 shdr_status, shdr_add_status, rc);
12763 rc = -ENXIO;
12764 }
James Smart6d368e52011-05-24 11:44:12 -040012765
12766 if (rc == 0)
12767 bf_set(lpfc_xri_rsrc_rdy, &phba->sli4_hba.sli4_flags,
12768 LPFC_XRI_RSRC_RDY);
12769 return rc;
12770}
12771
12772/**
12773 * lpfc_sli4_post_els_sgl_list_ext - post a block of ELS sgls to the port.
12774 * @phba: pointer to lpfc hba data structure.
12775 *
12776 * This routine is invoked to post a block of driver's sgl pages to the
12777 * HBA using non-embedded mailbox command. No Lock is held. This routine
12778 * is only called when the driver is loading and after all IO has been
12779 * stopped.
12780 **/
12781int
12782lpfc_sli4_post_els_sgl_list_ext(struct lpfc_hba *phba)
12783{
12784 struct lpfc_sglq *sglq_entry;
12785 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
12786 struct sgl_page_pairs *sgl_pg_pairs;
12787 void *viraddr;
12788 LPFC_MBOXQ_t *mbox;
12789 uint32_t reqlen, alloclen, index;
12790 uint32_t mbox_tmo;
12791 uint16_t rsrc_start, rsrc_size, els_xri_cnt;
12792 uint16_t xritag_start = 0, lxri = 0;
12793 struct lpfc_rsrc_blks *rsrc_blk;
12794 int cnt, ttl_cnt, rc = 0;
12795 int loop_cnt;
12796 uint32_t shdr_status, shdr_add_status;
12797 union lpfc_sli4_cfg_shdr *shdr;
12798
12799 /* The number of sgls to be posted */
12800 els_xri_cnt = lpfc_sli4_get_els_iocb_cnt(phba);
12801
12802 reqlen = els_xri_cnt * sizeof(struct sgl_page_pairs) +
12803 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
12804 if (reqlen > SLI4_PAGE_SIZE) {
12805 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
12806 "2989 Block sgl registration required DMA "
12807 "size (%d) great than a page\n", reqlen);
12808 return -ENOMEM;
12809 }
12810
12811 cnt = 0;
12812 ttl_cnt = 0;
12813 list_for_each_entry(rsrc_blk, &phba->sli4_hba.lpfc_xri_blk_list,
12814 list) {
12815 rsrc_start = rsrc_blk->rsrc_start;
12816 rsrc_size = rsrc_blk->rsrc_size;
12817
12818 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
12819 "3014 Working ELS Extent start %d, cnt %d\n",
12820 rsrc_start, rsrc_size);
12821
12822 loop_cnt = min(els_xri_cnt, rsrc_size);
12823 if (ttl_cnt + loop_cnt >= els_xri_cnt) {
12824 loop_cnt = els_xri_cnt - ttl_cnt;
12825 ttl_cnt = els_xri_cnt;
12826 }
12827
12828 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12829 if (!mbox)
12830 return -ENOMEM;
12831 /*
12832 * Allocate DMA memory and set up the non-embedded mailbox
12833 * command.
12834 */
12835 alloclen = lpfc_sli4_config(phba, mbox,
12836 LPFC_MBOX_SUBSYSTEM_FCOE,
12837 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES,
12838 reqlen, LPFC_SLI4_MBX_NEMBED);
12839 if (alloclen < reqlen) {
12840 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12841 "2987 Allocated DMA memory size (%d) "
12842 "is less than the requested DMA memory "
12843 "size (%d)\n", alloclen, reqlen);
12844 lpfc_sli4_mbox_cmd_free(phba, mbox);
12845 return -ENOMEM;
12846 }
12847
12848 /* Set up the SGL pages in the non-embedded DMA pages */
12849 viraddr = mbox->sge_array->addr[0];
12850 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
12851 sgl_pg_pairs = &sgl->sgl_pg_pairs;
12852
12853 /*
12854 * The starting resource may not begin at zero. Control
12855 * the loop variants via the block resource parameters,
12856 * but handle the sge pointers with a zero-based index
12857 * that doesn't get reset per loop pass.
12858 */
12859 for (index = rsrc_start;
12860 index < rsrc_start + loop_cnt;
12861 index++) {
12862 sglq_entry = phba->sli4_hba.lpfc_els_sgl_array[cnt];
12863
12864 /*
12865 * Assign the sglq a physical xri only if the driver
12866 * has not initialized those resources. A port reset
12867 * only needs the sglq's posted.
12868 */
12869 if (bf_get(lpfc_xri_rsrc_rdy,
12870 &phba->sli4_hba.sli4_flags) !=
12871 LPFC_XRI_RSRC_RDY) {
12872 lxri = lpfc_sli4_next_xritag(phba);
12873 if (lxri == NO_XRI) {
12874 lpfc_sli4_mbox_cmd_free(phba, mbox);
12875 rc = -ENOMEM;
12876 goto err_exit;
12877 }
12878 sglq_entry->sli4_lxritag = lxri;
12879 sglq_entry->sli4_xritag =
12880 phba->sli4_hba.xri_ids[lxri];
12881 }
12882
12883 /* Set up the sge entry */
12884 sgl_pg_pairs->sgl_pg0_addr_lo =
12885 cpu_to_le32(putPaddrLow(sglq_entry->phys));
12886 sgl_pg_pairs->sgl_pg0_addr_hi =
12887 cpu_to_le32(putPaddrHigh(sglq_entry->phys));
12888 sgl_pg_pairs->sgl_pg1_addr_lo =
12889 cpu_to_le32(putPaddrLow(0));
12890 sgl_pg_pairs->sgl_pg1_addr_hi =
12891 cpu_to_le32(putPaddrHigh(0));
12892
12893 /* Track the starting physical XRI for the mailbox. */
12894 if (index == rsrc_start)
12895 xritag_start = sglq_entry->sli4_xritag;
12896 sgl_pg_pairs++;
12897 cnt++;
12898 }
12899
12900 /* Complete initialization and perform endian conversion. */
12901 rsrc_blk->rsrc_used += loop_cnt;
12902 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
12903 bf_set(lpfc_post_sgl_pages_xricnt, sgl, loop_cnt);
12904 sgl->word0 = cpu_to_le32(sgl->word0);
12905
12906 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
12907 "3015 Post ELS Extent SGL, start %d, "
12908 "cnt %d, used %d\n",
12909 xritag_start, loop_cnt, rsrc_blk->rsrc_used);
12910 if (!phba->sli4_hba.intr_enable)
12911 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
12912 else {
12913 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_SLI4_CONFIG);
12914 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
12915 }
12916 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
12917 shdr_status = bf_get(lpfc_mbox_hdr_status,
12918 &shdr->response);
12919 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
12920 &shdr->response);
12921 if (rc != MBX_TIMEOUT)
12922 lpfc_sli4_mbox_cmd_free(phba, mbox);
12923 if (shdr_status || shdr_add_status || rc) {
12924 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12925 "2988 POST_SGL_BLOCK mailbox "
12926 "command failed status x%x "
12927 "add_status x%x mbx status x%x\n",
12928 shdr_status, shdr_add_status, rc);
12929 rc = -ENXIO;
12930 goto err_exit;
12931 }
12932 if (ttl_cnt >= els_xri_cnt)
12933 break;
12934 }
12935
12936 err_exit:
12937 if (rc == 0)
12938 bf_set(lpfc_xri_rsrc_rdy, &phba->sli4_hba.sli4_flags,
12939 LPFC_XRI_RSRC_RDY);
James Smart4f774512009-05-22 14:52:35 -040012940 return rc;
12941}
12942
12943/**
12944 * lpfc_sli4_post_scsi_sgl_block - post a block of scsi sgl list to firmware
12945 * @phba: pointer to lpfc hba data structure.
12946 * @sblist: pointer to scsi buffer list.
12947 * @count: number of scsi buffers on the list.
12948 *
12949 * This routine is invoked to post a block of @count scsi sgl pages from a
12950 * SCSI buffer list @sblist to the HBA using non-embedded mailbox command.
12951 * No Lock is held.
12952 *
12953 **/
12954int
12955lpfc_sli4_post_scsi_sgl_block(struct lpfc_hba *phba, struct list_head *sblist,
12956 int cnt)
12957{
12958 struct lpfc_scsi_buf *psb;
12959 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
12960 struct sgl_page_pairs *sgl_pg_pairs;
12961 void *viraddr;
12962 LPFC_MBOXQ_t *mbox;
12963 uint32_t reqlen, alloclen, pg_pairs;
12964 uint32_t mbox_tmo;
12965 uint16_t xritag_start = 0;
12966 int rc = 0;
12967 uint32_t shdr_status, shdr_add_status;
12968 dma_addr_t pdma_phys_bpl1;
12969 union lpfc_sli4_cfg_shdr *shdr;
12970
12971 /* Calculate the requested length of the dma memory */
12972 reqlen = cnt * sizeof(struct sgl_page_pairs) +
12973 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
James Smart49198b32010-04-06 15:04:33 -040012974 if (reqlen > SLI4_PAGE_SIZE) {
James Smart4f774512009-05-22 14:52:35 -040012975 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
12976 "0217 Block sgl registration required DMA "
12977 "size (%d) great than a page\n", reqlen);
12978 return -ENOMEM;
12979 }
12980 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12981 if (!mbox) {
12982 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12983 "0283 Failed to allocate mbox cmd memory\n");
12984 return -ENOMEM;
12985 }
12986
12987 /* Allocate DMA memory and set up the non-embedded mailbox command */
12988 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
12989 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
12990 LPFC_SLI4_MBX_NEMBED);
12991
12992 if (alloclen < reqlen) {
12993 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12994 "2561 Allocated DMA memory size (%d) is "
12995 "less than the requested DMA memory "
12996 "size (%d)\n", alloclen, reqlen);
12997 lpfc_sli4_mbox_cmd_free(phba, mbox);
12998 return -ENOMEM;
12999 }
James Smart6d368e52011-05-24 11:44:12 -040013000
James Smart4f774512009-05-22 14:52:35 -040013001 /* Get the first SGE entry from the non-embedded DMA memory */
James Smart4f774512009-05-22 14:52:35 -040013002 viraddr = mbox->sge_array->addr[0];
13003
13004 /* Set up the SGL pages in the non-embedded DMA pages */
13005 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
13006 sgl_pg_pairs = &sgl->sgl_pg_pairs;
13007
13008 pg_pairs = 0;
13009 list_for_each_entry(psb, sblist, list) {
13010 /* Set up the sge entry */
13011 sgl_pg_pairs->sgl_pg0_addr_lo =
13012 cpu_to_le32(putPaddrLow(psb->dma_phys_bpl));
13013 sgl_pg_pairs->sgl_pg0_addr_hi =
13014 cpu_to_le32(putPaddrHigh(psb->dma_phys_bpl));
13015 if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
13016 pdma_phys_bpl1 = psb->dma_phys_bpl + SGL_PAGE_SIZE;
13017 else
13018 pdma_phys_bpl1 = 0;
13019 sgl_pg_pairs->sgl_pg1_addr_lo =
13020 cpu_to_le32(putPaddrLow(pdma_phys_bpl1));
13021 sgl_pg_pairs->sgl_pg1_addr_hi =
13022 cpu_to_le32(putPaddrHigh(pdma_phys_bpl1));
13023 /* Keep the first xritag on the list */
13024 if (pg_pairs == 0)
13025 xritag_start = psb->cur_iocbq.sli4_xritag;
13026 sgl_pg_pairs++;
13027 pg_pairs++;
13028 }
13029 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
13030 bf_set(lpfc_post_sgl_pages_xricnt, sgl, pg_pairs);
13031 /* Perform endian conversion if necessary */
13032 sgl->word0 = cpu_to_le32(sgl->word0);
13033
13034 if (!phba->sli4_hba.intr_enable)
13035 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
13036 else {
13037 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_SLI4_CONFIG);
13038 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
13039 }
13040 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
13041 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13042 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13043 if (rc != MBX_TIMEOUT)
13044 lpfc_sli4_mbox_cmd_free(phba, mbox);
13045 if (shdr_status || shdr_add_status || rc) {
13046 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13047 "2564 POST_SGL_BLOCK mailbox command failed "
13048 "status x%x add_status x%x mbx status x%x\n",
13049 shdr_status, shdr_add_status, rc);
13050 rc = -ENXIO;
13051 }
13052 return rc;
13053}
13054
13055/**
James Smart6d368e52011-05-24 11:44:12 -040013056 * lpfc_sli4_post_scsi_sgl_blk_ext - post a block of scsi sgls to the port.
13057 * @phba: pointer to lpfc hba data structure.
13058 * @sblist: pointer to scsi buffer list.
13059 * @count: number of scsi buffers on the list.
13060 *
13061 * This routine is invoked to post a block of @count scsi sgl pages from a
13062 * SCSI buffer list @sblist to the HBA using non-embedded mailbox command.
13063 * No Lock is held.
13064 *
13065 **/
13066int
13067lpfc_sli4_post_scsi_sgl_blk_ext(struct lpfc_hba *phba, struct list_head *sblist,
13068 int cnt)
13069{
13070 struct lpfc_scsi_buf *psb = NULL;
13071 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
13072 struct sgl_page_pairs *sgl_pg_pairs;
13073 void *viraddr;
13074 LPFC_MBOXQ_t *mbox;
13075 uint32_t reqlen, alloclen, pg_pairs;
13076 uint32_t mbox_tmo;
13077 uint16_t xri_start = 0, scsi_xri_start;
13078 uint16_t rsrc_range;
13079 int rc = 0, avail_cnt;
13080 uint32_t shdr_status, shdr_add_status;
13081 dma_addr_t pdma_phys_bpl1;
13082 union lpfc_sli4_cfg_shdr *shdr;
13083 struct lpfc_rsrc_blks *rsrc_blk;
13084 uint32_t xri_cnt = 0;
13085
13086 /* Calculate the total requested length of the dma memory */
13087 reqlen = cnt * sizeof(struct sgl_page_pairs) +
13088 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
13089 if (reqlen > SLI4_PAGE_SIZE) {
13090 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
13091 "2932 Block sgl registration required DMA "
13092 "size (%d) great than a page\n", reqlen);
13093 return -ENOMEM;
13094 }
13095
13096 /*
13097 * The use of extents requires the driver to post the sgl headers
13098 * in multiple postings to meet the contiguous resource assignment.
13099 */
13100 psb = list_prepare_entry(psb, sblist, list);
13101 scsi_xri_start = phba->sli4_hba.scsi_xri_start;
13102 list_for_each_entry(rsrc_blk, &phba->sli4_hba.lpfc_xri_blk_list,
13103 list) {
13104 rsrc_range = rsrc_blk->rsrc_start + rsrc_blk->rsrc_size;
13105 if (rsrc_range < scsi_xri_start)
13106 continue;
13107 else if (rsrc_blk->rsrc_used >= rsrc_blk->rsrc_size)
13108 continue;
13109 else
13110 avail_cnt = rsrc_blk->rsrc_size - rsrc_blk->rsrc_used;
13111
13112 reqlen = (avail_cnt * sizeof(struct sgl_page_pairs)) +
13113 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
13114 /*
13115 * Allocate DMA memory and set up the non-embedded mailbox
13116 * command. The mbox is used to post an SGL page per loop
13117 * but the DMA memory has a use-once semantic so the mailbox
13118 * is used and freed per loop pass.
13119 */
13120 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
13121 if (!mbox) {
13122 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13123 "2933 Failed to allocate mbox cmd "
13124 "memory\n");
13125 return -ENOMEM;
13126 }
13127 alloclen = lpfc_sli4_config(phba, mbox,
13128 LPFC_MBOX_SUBSYSTEM_FCOE,
13129 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES,
13130 reqlen,
13131 LPFC_SLI4_MBX_NEMBED);
13132 if (alloclen < reqlen) {
13133 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13134 "2934 Allocated DMA memory size (%d) "
13135 "is less than the requested DMA memory "
13136 "size (%d)\n", alloclen, reqlen);
13137 lpfc_sli4_mbox_cmd_free(phba, mbox);
13138 return -ENOMEM;
13139 }
13140
13141 /* Get the first SGE entry from the non-embedded DMA memory */
13142 viraddr = mbox->sge_array->addr[0];
13143
13144 /* Set up the SGL pages in the non-embedded DMA pages */
13145 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
13146 sgl_pg_pairs = &sgl->sgl_pg_pairs;
13147
13148 /* pg_pairs tracks posted SGEs per loop iteration. */
13149 pg_pairs = 0;
13150 list_for_each_entry_continue(psb, sblist, list) {
13151 /* Set up the sge entry */
13152 sgl_pg_pairs->sgl_pg0_addr_lo =
13153 cpu_to_le32(putPaddrLow(psb->dma_phys_bpl));
13154 sgl_pg_pairs->sgl_pg0_addr_hi =
13155 cpu_to_le32(putPaddrHigh(psb->dma_phys_bpl));
13156 if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
13157 pdma_phys_bpl1 = psb->dma_phys_bpl +
13158 SGL_PAGE_SIZE;
13159 else
13160 pdma_phys_bpl1 = 0;
13161 sgl_pg_pairs->sgl_pg1_addr_lo =
13162 cpu_to_le32(putPaddrLow(pdma_phys_bpl1));
13163 sgl_pg_pairs->sgl_pg1_addr_hi =
13164 cpu_to_le32(putPaddrHigh(pdma_phys_bpl1));
13165 /* Keep the first xri for this extent. */
13166 if (pg_pairs == 0)
13167 xri_start = psb->cur_iocbq.sli4_xritag;
13168 sgl_pg_pairs++;
13169 pg_pairs++;
13170 xri_cnt++;
13171
13172 /*
13173 * Track two exit conditions - the loop has constructed
13174 * all of the caller's SGE pairs or all available
13175 * resource IDs in this extent are consumed.
13176 */
13177 if ((xri_cnt == cnt) || (pg_pairs >= avail_cnt))
13178 break;
13179 }
13180 rsrc_blk->rsrc_used += pg_pairs;
13181 bf_set(lpfc_post_sgl_pages_xri, sgl, xri_start);
13182 bf_set(lpfc_post_sgl_pages_xricnt, sgl, pg_pairs);
13183
13184 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
13185 "3016 Post SCSI Extent SGL, start %d, cnt %d "
13186 "blk use %d\n",
13187 xri_start, pg_pairs, rsrc_blk->rsrc_used);
13188 /* Perform endian conversion if necessary */
13189 sgl->word0 = cpu_to_le32(sgl->word0);
13190 if (!phba->sli4_hba.intr_enable)
13191 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
13192 else {
13193 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_SLI4_CONFIG);
13194 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
13195 }
13196 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
13197 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13198 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
13199 &shdr->response);
13200 if (rc != MBX_TIMEOUT)
13201 lpfc_sli4_mbox_cmd_free(phba, mbox);
13202 if (shdr_status || shdr_add_status || rc) {
13203 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13204 "2935 POST_SGL_BLOCK mailbox command "
13205 "failed status x%x add_status x%x "
13206 "mbx status x%x\n",
13207 shdr_status, shdr_add_status, rc);
13208 return -ENXIO;
13209 }
13210
13211 /* Post only what is requested. */
13212 if (xri_cnt >= cnt)
13213 break;
13214 }
13215 return rc;
13216}
13217
13218/**
James Smart4f774512009-05-22 14:52:35 -040013219 * lpfc_fc_frame_check - Check that this frame is a valid frame to handle
13220 * @phba: pointer to lpfc_hba struct that the frame was received on
13221 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
13222 *
13223 * This function checks the fields in the @fc_hdr to see if the FC frame is a
13224 * valid type of frame that the LPFC driver will handle. This function will
13225 * return a zero if the frame is a valid frame or a non zero value when the
13226 * frame does not pass the check.
13227 **/
13228static int
13229lpfc_fc_frame_check(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr)
13230{
Tomas Henzl474ffb72010-12-22 16:52:40 +010013231 /* make rctl_names static to save stack space */
13232 static char *rctl_names[] = FC_RCTL_NAMES_INIT;
James Smart4f774512009-05-22 14:52:35 -040013233 char *type_names[] = FC_TYPE_NAMES_INIT;
13234 struct fc_vft_header *fc_vft_hdr;
James Smart546fc852011-03-11 16:06:29 -050013235 uint32_t *header = (uint32_t *) fc_hdr;
James Smart4f774512009-05-22 14:52:35 -040013236
13237 switch (fc_hdr->fh_r_ctl) {
13238 case FC_RCTL_DD_UNCAT: /* uncategorized information */
13239 case FC_RCTL_DD_SOL_DATA: /* solicited data */
13240 case FC_RCTL_DD_UNSOL_CTL: /* unsolicited control */
13241 case FC_RCTL_DD_SOL_CTL: /* solicited control or reply */
13242 case FC_RCTL_DD_UNSOL_DATA: /* unsolicited data */
13243 case FC_RCTL_DD_DATA_DESC: /* data descriptor */
13244 case FC_RCTL_DD_UNSOL_CMD: /* unsolicited command */
13245 case FC_RCTL_DD_CMD_STATUS: /* command status */
13246 case FC_RCTL_ELS_REQ: /* extended link services request */
13247 case FC_RCTL_ELS_REP: /* extended link services reply */
13248 case FC_RCTL_ELS4_REQ: /* FC-4 ELS request */
13249 case FC_RCTL_ELS4_REP: /* FC-4 ELS reply */
13250 case FC_RCTL_BA_NOP: /* basic link service NOP */
13251 case FC_RCTL_BA_ABTS: /* basic link service abort */
13252 case FC_RCTL_BA_RMC: /* remove connection */
13253 case FC_RCTL_BA_ACC: /* basic accept */
13254 case FC_RCTL_BA_RJT: /* basic reject */
13255 case FC_RCTL_BA_PRMT:
13256 case FC_RCTL_ACK_1: /* acknowledge_1 */
13257 case FC_RCTL_ACK_0: /* acknowledge_0 */
13258 case FC_RCTL_P_RJT: /* port reject */
13259 case FC_RCTL_F_RJT: /* fabric reject */
13260 case FC_RCTL_P_BSY: /* port busy */
13261 case FC_RCTL_F_BSY: /* fabric busy to data frame */
13262 case FC_RCTL_F_BSYL: /* fabric busy to link control frame */
13263 case FC_RCTL_LCR: /* link credit reset */
13264 case FC_RCTL_END: /* end */
13265 break;
13266 case FC_RCTL_VFTH: /* Virtual Fabric tagging Header */
13267 fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
13268 fc_hdr = &((struct fc_frame_header *)fc_vft_hdr)[1];
13269 return lpfc_fc_frame_check(phba, fc_hdr);
13270 default:
13271 goto drop;
13272 }
13273 switch (fc_hdr->fh_type) {
13274 case FC_TYPE_BLS:
13275 case FC_TYPE_ELS:
13276 case FC_TYPE_FCP:
13277 case FC_TYPE_CT:
13278 break;
13279 case FC_TYPE_IP:
13280 case FC_TYPE_ILS:
13281 default:
13282 goto drop;
13283 }
James Smart546fc852011-03-11 16:06:29 -050013284
James Smart4f774512009-05-22 14:52:35 -040013285 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
James Smart546fc852011-03-11 16:06:29 -050013286 "2538 Received frame rctl:%s type:%s "
13287 "Frame Data:%08x %08x %08x %08x %08x %08x\n",
James Smart4f774512009-05-22 14:52:35 -040013288 rctl_names[fc_hdr->fh_r_ctl],
James Smart546fc852011-03-11 16:06:29 -050013289 type_names[fc_hdr->fh_type],
13290 be32_to_cpu(header[0]), be32_to_cpu(header[1]),
13291 be32_to_cpu(header[2]), be32_to_cpu(header[3]),
13292 be32_to_cpu(header[4]), be32_to_cpu(header[5]));
James Smart4f774512009-05-22 14:52:35 -040013293 return 0;
13294drop:
13295 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
13296 "2539 Dropped frame rctl:%s type:%s\n",
13297 rctl_names[fc_hdr->fh_r_ctl],
13298 type_names[fc_hdr->fh_type]);
13299 return 1;
13300}
13301
13302/**
13303 * lpfc_fc_hdr_get_vfi - Get the VFI from an FC frame
13304 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
13305 *
13306 * This function processes the FC header to retrieve the VFI from the VF
13307 * header, if one exists. This function will return the VFI if one exists
13308 * or 0 if no VSAN Header exists.
13309 **/
13310static uint32_t
13311lpfc_fc_hdr_get_vfi(struct fc_frame_header *fc_hdr)
13312{
13313 struct fc_vft_header *fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
13314
13315 if (fc_hdr->fh_r_ctl != FC_RCTL_VFTH)
13316 return 0;
13317 return bf_get(fc_vft_hdr_vf_id, fc_vft_hdr);
13318}
13319
13320/**
13321 * lpfc_fc_frame_to_vport - Finds the vport that a frame is destined to
13322 * @phba: Pointer to the HBA structure to search for the vport on
13323 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
13324 * @fcfi: The FC Fabric ID that the frame came from
13325 *
13326 * This function searches the @phba for a vport that matches the content of the
13327 * @fc_hdr passed in and the @fcfi. This function uses the @fc_hdr to fetch the
13328 * VFI, if the Virtual Fabric Tagging Header exists, and the DID. This function
13329 * returns the matching vport pointer or NULL if unable to match frame to a
13330 * vport.
13331 **/
13332static struct lpfc_vport *
13333lpfc_fc_frame_to_vport(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr,
13334 uint16_t fcfi)
13335{
13336 struct lpfc_vport **vports;
13337 struct lpfc_vport *vport = NULL;
13338 int i;
13339 uint32_t did = (fc_hdr->fh_d_id[0] << 16 |
13340 fc_hdr->fh_d_id[1] << 8 |
13341 fc_hdr->fh_d_id[2]);
James Smartbf086112011-08-21 21:48:13 -040013342 if (did == Fabric_DID)
13343 return phba->pport;
James Smart4f774512009-05-22 14:52:35 -040013344 vports = lpfc_create_vport_work_array(phba);
13345 if (vports != NULL)
13346 for (i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
13347 if (phba->fcf.fcfi == fcfi &&
13348 vports[i]->vfi == lpfc_fc_hdr_get_vfi(fc_hdr) &&
13349 vports[i]->fc_myDID == did) {
13350 vport = vports[i];
13351 break;
13352 }
13353 }
13354 lpfc_destroy_vport_work_array(phba, vports);
13355 return vport;
13356}
13357
13358/**
James Smart45ed1192009-10-02 15:17:02 -040013359 * lpfc_update_rcv_time_stamp - Update vport's rcv seq time stamp
13360 * @vport: The vport to work on.
13361 *
13362 * This function updates the receive sequence time stamp for this vport. The
13363 * receive sequence time stamp indicates the time that the last frame of the
13364 * the sequence that has been idle for the longest amount of time was received.
13365 * the driver uses this time stamp to indicate if any received sequences have
13366 * timed out.
13367 **/
13368void
13369lpfc_update_rcv_time_stamp(struct lpfc_vport *vport)
13370{
13371 struct lpfc_dmabuf *h_buf;
13372 struct hbq_dmabuf *dmabuf = NULL;
13373
13374 /* get the oldest sequence on the rcv list */
13375 h_buf = list_get_first(&vport->rcv_buffer_list,
13376 struct lpfc_dmabuf, list);
13377 if (!h_buf)
13378 return;
13379 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
13380 vport->rcv_buffer_time_stamp = dmabuf->time_stamp;
13381}
13382
13383/**
13384 * lpfc_cleanup_rcv_buffers - Cleans up all outstanding receive sequences.
13385 * @vport: The vport that the received sequences were sent to.
13386 *
13387 * This function cleans up all outstanding received sequences. This is called
13388 * by the driver when a link event or user action invalidates all the received
13389 * sequences.
13390 **/
13391void
13392lpfc_cleanup_rcv_buffers(struct lpfc_vport *vport)
13393{
13394 struct lpfc_dmabuf *h_buf, *hnext;
13395 struct lpfc_dmabuf *d_buf, *dnext;
13396 struct hbq_dmabuf *dmabuf = NULL;
13397
13398 /* start with the oldest sequence on the rcv list */
13399 list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
13400 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
13401 list_del_init(&dmabuf->hbuf.list);
13402 list_for_each_entry_safe(d_buf, dnext,
13403 &dmabuf->dbuf.list, list) {
13404 list_del_init(&d_buf->list);
13405 lpfc_in_buf_free(vport->phba, d_buf);
13406 }
13407 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
13408 }
13409}
13410
13411/**
13412 * lpfc_rcv_seq_check_edtov - Cleans up timed out receive sequences.
13413 * @vport: The vport that the received sequences were sent to.
13414 *
13415 * This function determines whether any received sequences have timed out by
13416 * first checking the vport's rcv_buffer_time_stamp. If this time_stamp
13417 * indicates that there is at least one timed out sequence this routine will
13418 * go through the received sequences one at a time from most inactive to most
13419 * active to determine which ones need to be cleaned up. Once it has determined
13420 * that a sequence needs to be cleaned up it will simply free up the resources
13421 * without sending an abort.
13422 **/
13423void
13424lpfc_rcv_seq_check_edtov(struct lpfc_vport *vport)
13425{
13426 struct lpfc_dmabuf *h_buf, *hnext;
13427 struct lpfc_dmabuf *d_buf, *dnext;
13428 struct hbq_dmabuf *dmabuf = NULL;
13429 unsigned long timeout;
13430 int abort_count = 0;
13431
13432 timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
13433 vport->rcv_buffer_time_stamp);
13434 if (list_empty(&vport->rcv_buffer_list) ||
13435 time_before(jiffies, timeout))
13436 return;
13437 /* start with the oldest sequence on the rcv list */
13438 list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
13439 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
13440 timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
13441 dmabuf->time_stamp);
13442 if (time_before(jiffies, timeout))
13443 break;
13444 abort_count++;
13445 list_del_init(&dmabuf->hbuf.list);
13446 list_for_each_entry_safe(d_buf, dnext,
13447 &dmabuf->dbuf.list, list) {
13448 list_del_init(&d_buf->list);
13449 lpfc_in_buf_free(vport->phba, d_buf);
13450 }
13451 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
13452 }
13453 if (abort_count)
13454 lpfc_update_rcv_time_stamp(vport);
13455}
13456
13457/**
James Smart4f774512009-05-22 14:52:35 -040013458 * lpfc_fc_frame_add - Adds a frame to the vport's list of received sequences
13459 * @dmabuf: pointer to a dmabuf that describes the hdr and data of the FC frame
13460 *
13461 * This function searches through the existing incomplete sequences that have
13462 * been sent to this @vport. If the frame matches one of the incomplete
13463 * sequences then the dbuf in the @dmabuf is added to the list of frames that
13464 * make up that sequence. If no sequence is found that matches this frame then
13465 * the function will add the hbuf in the @dmabuf to the @vport's rcv_buffer_list
13466 * This function returns a pointer to the first dmabuf in the sequence list that
13467 * the frame was linked to.
13468 **/
13469static struct hbq_dmabuf *
13470lpfc_fc_frame_add(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
13471{
13472 struct fc_frame_header *new_hdr;
13473 struct fc_frame_header *temp_hdr;
13474 struct lpfc_dmabuf *d_buf;
13475 struct lpfc_dmabuf *h_buf;
13476 struct hbq_dmabuf *seq_dmabuf = NULL;
13477 struct hbq_dmabuf *temp_dmabuf = NULL;
13478
James Smart4d9ab992009-10-02 15:16:39 -040013479 INIT_LIST_HEAD(&dmabuf->dbuf.list);
James Smart45ed1192009-10-02 15:17:02 -040013480 dmabuf->time_stamp = jiffies;
James Smart4f774512009-05-22 14:52:35 -040013481 new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
13482 /* Use the hdr_buf to find the sequence that this frame belongs to */
13483 list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
13484 temp_hdr = (struct fc_frame_header *)h_buf->virt;
13485 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
13486 (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
13487 (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
13488 continue;
13489 /* found a pending sequence that matches this frame */
13490 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
13491 break;
13492 }
13493 if (!seq_dmabuf) {
13494 /*
13495 * This indicates first frame received for this sequence.
13496 * Queue the buffer on the vport's rcv_buffer_list.
13497 */
13498 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
James Smart45ed1192009-10-02 15:17:02 -040013499 lpfc_update_rcv_time_stamp(vport);
James Smart4f774512009-05-22 14:52:35 -040013500 return dmabuf;
13501 }
13502 temp_hdr = seq_dmabuf->hbuf.virt;
James Smarteeead812009-12-21 17:01:23 -050013503 if (be16_to_cpu(new_hdr->fh_seq_cnt) <
13504 be16_to_cpu(temp_hdr->fh_seq_cnt)) {
James Smart4d9ab992009-10-02 15:16:39 -040013505 list_del_init(&seq_dmabuf->hbuf.list);
13506 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
13507 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
James Smart45ed1192009-10-02 15:17:02 -040013508 lpfc_update_rcv_time_stamp(vport);
James Smart4f774512009-05-22 14:52:35 -040013509 return dmabuf;
13510 }
James Smart45ed1192009-10-02 15:17:02 -040013511 /* move this sequence to the tail to indicate a young sequence */
13512 list_move_tail(&seq_dmabuf->hbuf.list, &vport->rcv_buffer_list);
13513 seq_dmabuf->time_stamp = jiffies;
13514 lpfc_update_rcv_time_stamp(vport);
James Smarteeead812009-12-21 17:01:23 -050013515 if (list_empty(&seq_dmabuf->dbuf.list)) {
13516 temp_hdr = dmabuf->hbuf.virt;
13517 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
13518 return seq_dmabuf;
13519 }
James Smart4f774512009-05-22 14:52:35 -040013520 /* find the correct place in the sequence to insert this frame */
13521 list_for_each_entry_reverse(d_buf, &seq_dmabuf->dbuf.list, list) {
13522 temp_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
13523 temp_hdr = (struct fc_frame_header *)temp_dmabuf->hbuf.virt;
13524 /*
13525 * If the frame's sequence count is greater than the frame on
13526 * the list then insert the frame right after this frame
13527 */
James Smarteeead812009-12-21 17:01:23 -050013528 if (be16_to_cpu(new_hdr->fh_seq_cnt) >
13529 be16_to_cpu(temp_hdr->fh_seq_cnt)) {
James Smart4f774512009-05-22 14:52:35 -040013530 list_add(&dmabuf->dbuf.list, &temp_dmabuf->dbuf.list);
13531 return seq_dmabuf;
13532 }
13533 }
13534 return NULL;
13535}
13536
13537/**
James Smart6669f9b2009-10-02 15:16:45 -040013538 * lpfc_sli4_abort_partial_seq - Abort partially assembled unsol sequence
13539 * @vport: pointer to a vitural port
13540 * @dmabuf: pointer to a dmabuf that describes the FC sequence
13541 *
13542 * This function tries to abort from the partially assembed sequence, described
13543 * by the information from basic abbort @dmabuf. It checks to see whether such
13544 * partially assembled sequence held by the driver. If so, it shall free up all
13545 * the frames from the partially assembled sequence.
13546 *
13547 * Return
13548 * true -- if there is matching partially assembled sequence present and all
13549 * the frames freed with the sequence;
13550 * false -- if there is no matching partially assembled sequence present so
13551 * nothing got aborted in the lower layer driver
13552 **/
13553static bool
13554lpfc_sli4_abort_partial_seq(struct lpfc_vport *vport,
13555 struct hbq_dmabuf *dmabuf)
13556{
13557 struct fc_frame_header *new_hdr;
13558 struct fc_frame_header *temp_hdr;
13559 struct lpfc_dmabuf *d_buf, *n_buf, *h_buf;
13560 struct hbq_dmabuf *seq_dmabuf = NULL;
13561
13562 /* Use the hdr_buf to find the sequence that matches this frame */
13563 INIT_LIST_HEAD(&dmabuf->dbuf.list);
13564 INIT_LIST_HEAD(&dmabuf->hbuf.list);
13565 new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
13566 list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
13567 temp_hdr = (struct fc_frame_header *)h_buf->virt;
13568 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
13569 (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
13570 (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
13571 continue;
13572 /* found a pending sequence that matches this frame */
13573 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
13574 break;
13575 }
13576
13577 /* Free up all the frames from the partially assembled sequence */
13578 if (seq_dmabuf) {
13579 list_for_each_entry_safe(d_buf, n_buf,
13580 &seq_dmabuf->dbuf.list, list) {
13581 list_del_init(&d_buf->list);
13582 lpfc_in_buf_free(vport->phba, d_buf);
13583 }
13584 return true;
13585 }
13586 return false;
13587}
13588
13589/**
James Smart546fc852011-03-11 16:06:29 -050013590 * lpfc_sli4_seq_abort_rsp_cmpl - BLS ABORT RSP seq abort iocb complete handler
James Smart6669f9b2009-10-02 15:16:45 -040013591 * @phba: Pointer to HBA context object.
13592 * @cmd_iocbq: pointer to the command iocbq structure.
13593 * @rsp_iocbq: pointer to the response iocbq structure.
13594 *
James Smart546fc852011-03-11 16:06:29 -050013595 * This function handles the sequence abort response iocb command complete
James Smart6669f9b2009-10-02 15:16:45 -040013596 * event. It properly releases the memory allocated to the sequence abort
13597 * accept iocb.
13598 **/
13599static void
James Smart546fc852011-03-11 16:06:29 -050013600lpfc_sli4_seq_abort_rsp_cmpl(struct lpfc_hba *phba,
James Smart6669f9b2009-10-02 15:16:45 -040013601 struct lpfc_iocbq *cmd_iocbq,
13602 struct lpfc_iocbq *rsp_iocbq)
13603{
13604 if (cmd_iocbq)
13605 lpfc_sli_release_iocbq(phba, cmd_iocbq);
13606}
13607
13608/**
James Smart6d368e52011-05-24 11:44:12 -040013609 * lpfc_sli4_xri_inrange - check xri is in range of xris owned by driver.
13610 * @phba: Pointer to HBA context object.
13611 * @xri: xri id in transaction.
13612 *
13613 * This function validates the xri maps to the known range of XRIs allocated an
13614 * used by the driver.
13615 **/
James Smart7851fe22011-07-22 18:36:52 -040013616uint16_t
James Smart6d368e52011-05-24 11:44:12 -040013617lpfc_sli4_xri_inrange(struct lpfc_hba *phba,
13618 uint16_t xri)
13619{
13620 int i;
13621
13622 for (i = 0; i < phba->sli4_hba.max_cfg_param.max_xri; i++) {
13623 if (xri == phba->sli4_hba.xri_ids[i])
13624 return i;
13625 }
13626 return NO_XRI;
13627}
13628
13629
13630/**
James Smart546fc852011-03-11 16:06:29 -050013631 * lpfc_sli4_seq_abort_rsp - bls rsp to sequence abort
James Smart6669f9b2009-10-02 15:16:45 -040013632 * @phba: Pointer to HBA context object.
13633 * @fc_hdr: pointer to a FC frame header.
13634 *
James Smart546fc852011-03-11 16:06:29 -050013635 * This function sends a basic response to a previous unsol sequence abort
James Smart6669f9b2009-10-02 15:16:45 -040013636 * event after aborting the sequence handling.
13637 **/
13638static void
James Smart546fc852011-03-11 16:06:29 -050013639lpfc_sli4_seq_abort_rsp(struct lpfc_hba *phba,
James Smart6669f9b2009-10-02 15:16:45 -040013640 struct fc_frame_header *fc_hdr)
13641{
13642 struct lpfc_iocbq *ctiocb = NULL;
13643 struct lpfc_nodelist *ndlp;
James Smart5ffc2662009-11-18 15:39:44 -050013644 uint16_t oxid, rxid;
13645 uint32_t sid, fctl;
James Smart6669f9b2009-10-02 15:16:45 -040013646 IOCB_t *icmd;
James Smart546fc852011-03-11 16:06:29 -050013647 int rc;
James Smart6669f9b2009-10-02 15:16:45 -040013648
13649 if (!lpfc_is_link_up(phba))
13650 return;
13651
13652 sid = sli4_sid_from_fc_hdr(fc_hdr);
13653 oxid = be16_to_cpu(fc_hdr->fh_ox_id);
James Smart5ffc2662009-11-18 15:39:44 -050013654 rxid = be16_to_cpu(fc_hdr->fh_rx_id);
James Smart6669f9b2009-10-02 15:16:45 -040013655
13656 ndlp = lpfc_findnode_did(phba->pport, sid);
13657 if (!ndlp) {
13658 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
13659 "1268 Find ndlp returned NULL for oxid:x%x "
13660 "SID:x%x\n", oxid, sid);
13661 return;
13662 }
James Smart6d368e52011-05-24 11:44:12 -040013663 if (lpfc_sli4_xri_inrange(phba, rxid))
James Smart19ca7602010-11-20 23:11:55 -050013664 lpfc_set_rrq_active(phba, ndlp, rxid, oxid, 0);
James Smart6669f9b2009-10-02 15:16:45 -040013665
James Smart546fc852011-03-11 16:06:29 -050013666 /* Allocate buffer for rsp iocb */
James Smart6669f9b2009-10-02 15:16:45 -040013667 ctiocb = lpfc_sli_get_iocbq(phba);
13668 if (!ctiocb)
13669 return;
13670
James Smart5ffc2662009-11-18 15:39:44 -050013671 /* Extract the F_CTL field from FC_HDR */
13672 fctl = sli4_fctl_from_fc_hdr(fc_hdr);
13673
James Smart6669f9b2009-10-02 15:16:45 -040013674 icmd = &ctiocb->iocb;
James Smart6669f9b2009-10-02 15:16:45 -040013675 icmd->un.xseq64.bdl.bdeSize = 0;
James Smart5ffc2662009-11-18 15:39:44 -050013676 icmd->un.xseq64.bdl.ulpIoTag32 = 0;
James Smart6669f9b2009-10-02 15:16:45 -040013677 icmd->un.xseq64.w5.hcsw.Dfctl = 0;
13678 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_ACC;
13679 icmd->un.xseq64.w5.hcsw.Type = FC_TYPE_BLS;
13680
13681 /* Fill in the rest of iocb fields */
13682 icmd->ulpCommand = CMD_XMIT_BLS_RSP64_CX;
13683 icmd->ulpBdeCount = 0;
13684 icmd->ulpLe = 1;
13685 icmd->ulpClass = CLASS3;
James Smart6d368e52011-05-24 11:44:12 -040013686 icmd->ulpContext = phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
James Smartbe858b62010-12-15 17:57:20 -050013687 ctiocb->context1 = ndlp;
James Smart6669f9b2009-10-02 15:16:45 -040013688
James Smart6669f9b2009-10-02 15:16:45 -040013689 ctiocb->iocb_cmpl = NULL;
13690 ctiocb->vport = phba->pport;
James Smart546fc852011-03-11 16:06:29 -050013691 ctiocb->iocb_cmpl = lpfc_sli4_seq_abort_rsp_cmpl;
James Smart6d368e52011-05-24 11:44:12 -040013692 ctiocb->sli4_lxritag = NO_XRI;
James Smart546fc852011-03-11 16:06:29 -050013693 ctiocb->sli4_xritag = NO_XRI;
13694
13695 /* If the oxid maps to the FCP XRI range or if it is out of range,
13696 * send a BLS_RJT. The driver no longer has that exchange.
13697 * Override the IOCB for a BA_RJT.
13698 */
13699 if (oxid > (phba->sli4_hba.max_cfg_param.max_xri +
13700 phba->sli4_hba.max_cfg_param.xri_base) ||
13701 oxid > (lpfc_sli4_get_els_iocb_cnt(phba) +
13702 phba->sli4_hba.max_cfg_param.xri_base)) {
13703 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_RJT;
13704 bf_set(lpfc_vndr_code, &icmd->un.bls_rsp, 0);
13705 bf_set(lpfc_rsn_expln, &icmd->un.bls_rsp, FC_BA_RJT_INV_XID);
13706 bf_set(lpfc_rsn_code, &icmd->un.bls_rsp, FC_BA_RJT_UNABLE);
13707 }
James Smart6669f9b2009-10-02 15:16:45 -040013708
James Smart5ffc2662009-11-18 15:39:44 -050013709 if (fctl & FC_FC_EX_CTX) {
13710 /* ABTS sent by responder to CT exchange, construction
13711 * of BA_ACC will use OX_ID from ABTS for the XRI_TAG
13712 * field and RX_ID from ABTS for RX_ID field.
13713 */
James Smart546fc852011-03-11 16:06:29 -050013714 bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_RSP);
13715 bf_set(lpfc_abts_rxid, &icmd->un.bls_rsp, rxid);
James Smart5ffc2662009-11-18 15:39:44 -050013716 } else {
13717 /* ABTS sent by initiator to CT exchange, construction
13718 * of BA_ACC will need to allocate a new XRI as for the
13719 * XRI_TAG and RX_ID fields.
13720 */
James Smart546fc852011-03-11 16:06:29 -050013721 bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_INT);
13722 bf_set(lpfc_abts_rxid, &icmd->un.bls_rsp, NO_XRI);
James Smart5ffc2662009-11-18 15:39:44 -050013723 }
James Smart546fc852011-03-11 16:06:29 -050013724 bf_set(lpfc_abts_oxid, &icmd->un.bls_rsp, oxid);
James Smart5ffc2662009-11-18 15:39:44 -050013725
James Smart546fc852011-03-11 16:06:29 -050013726 /* Xmit CT abts response on exchange <xid> */
James Smart6669f9b2009-10-02 15:16:45 -040013727 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
James Smart546fc852011-03-11 16:06:29 -050013728 "1200 Send BLS cmd x%x on oxid x%x Data: x%x\n",
13729 icmd->un.xseq64.w5.hcsw.Rctl, oxid, phba->link_state);
13730
13731 rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, ctiocb, 0);
13732 if (rc == IOCB_ERROR) {
13733 lpfc_printf_log(phba, KERN_ERR, LOG_ELS,
13734 "2925 Failed to issue CT ABTS RSP x%x on "
13735 "xri x%x, Data x%x\n",
13736 icmd->un.xseq64.w5.hcsw.Rctl, oxid,
13737 phba->link_state);
13738 lpfc_sli_release_iocbq(phba, ctiocb);
13739 }
James Smart6669f9b2009-10-02 15:16:45 -040013740}
13741
13742/**
13743 * lpfc_sli4_handle_unsol_abort - Handle sli-4 unsolicited abort event
13744 * @vport: Pointer to the vport on which this sequence was received
13745 * @dmabuf: pointer to a dmabuf that describes the FC sequence
13746 *
13747 * This function handles an SLI-4 unsolicited abort event. If the unsolicited
13748 * receive sequence is only partially assembed by the driver, it shall abort
13749 * the partially assembled frames for the sequence. Otherwise, if the
13750 * unsolicited receive sequence has been completely assembled and passed to
13751 * the Upper Layer Protocol (UPL), it then mark the per oxid status for the
13752 * unsolicited sequence has been aborted. After that, it will issue a basic
13753 * accept to accept the abort.
13754 **/
13755void
13756lpfc_sli4_handle_unsol_abort(struct lpfc_vport *vport,
13757 struct hbq_dmabuf *dmabuf)
13758{
13759 struct lpfc_hba *phba = vport->phba;
13760 struct fc_frame_header fc_hdr;
James Smart5ffc2662009-11-18 15:39:44 -050013761 uint32_t fctl;
James Smart6669f9b2009-10-02 15:16:45 -040013762 bool abts_par;
13763
James Smart6669f9b2009-10-02 15:16:45 -040013764 /* Make a copy of fc_hdr before the dmabuf being released */
13765 memcpy(&fc_hdr, dmabuf->hbuf.virt, sizeof(struct fc_frame_header));
James Smart5ffc2662009-11-18 15:39:44 -050013766 fctl = sli4_fctl_from_fc_hdr(&fc_hdr);
James Smart6669f9b2009-10-02 15:16:45 -040013767
James Smart5ffc2662009-11-18 15:39:44 -050013768 if (fctl & FC_FC_EX_CTX) {
13769 /*
13770 * ABTS sent by responder to exchange, just free the buffer
13771 */
James Smart6669f9b2009-10-02 15:16:45 -040013772 lpfc_in_buf_free(phba, &dmabuf->dbuf);
James Smart5ffc2662009-11-18 15:39:44 -050013773 } else {
13774 /*
13775 * ABTS sent by initiator to exchange, need to do cleanup
13776 */
13777 /* Try to abort partially assembled seq */
13778 abts_par = lpfc_sli4_abort_partial_seq(vport, dmabuf);
13779
13780 /* Send abort to ULP if partially seq abort failed */
13781 if (abts_par == false)
13782 lpfc_sli4_send_seq_to_ulp(vport, dmabuf);
13783 else
13784 lpfc_in_buf_free(phba, &dmabuf->dbuf);
13785 }
James Smart6669f9b2009-10-02 15:16:45 -040013786 /* Send basic accept (BA_ACC) to the abort requester */
James Smart546fc852011-03-11 16:06:29 -050013787 lpfc_sli4_seq_abort_rsp(phba, &fc_hdr);
James Smart6669f9b2009-10-02 15:16:45 -040013788}
13789
13790/**
James Smart4f774512009-05-22 14:52:35 -040013791 * lpfc_seq_complete - Indicates if a sequence is complete
13792 * @dmabuf: pointer to a dmabuf that describes the FC sequence
13793 *
13794 * This function checks the sequence, starting with the frame described by
13795 * @dmabuf, to see if all the frames associated with this sequence are present.
13796 * the frames associated with this sequence are linked to the @dmabuf using the
13797 * dbuf list. This function looks for two major things. 1) That the first frame
13798 * has a sequence count of zero. 2) There is a frame with last frame of sequence
13799 * set. 3) That there are no holes in the sequence count. The function will
13800 * return 1 when the sequence is complete, otherwise it will return 0.
13801 **/
13802static int
13803lpfc_seq_complete(struct hbq_dmabuf *dmabuf)
13804{
13805 struct fc_frame_header *hdr;
13806 struct lpfc_dmabuf *d_buf;
13807 struct hbq_dmabuf *seq_dmabuf;
13808 uint32_t fctl;
13809 int seq_count = 0;
13810
13811 hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
13812 /* make sure first fame of sequence has a sequence count of zero */
13813 if (hdr->fh_seq_cnt != seq_count)
13814 return 0;
13815 fctl = (hdr->fh_f_ctl[0] << 16 |
13816 hdr->fh_f_ctl[1] << 8 |
13817 hdr->fh_f_ctl[2]);
13818 /* If last frame of sequence we can return success. */
13819 if (fctl & FC_FC_END_SEQ)
13820 return 1;
13821 list_for_each_entry(d_buf, &dmabuf->dbuf.list, list) {
13822 seq_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
13823 hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
13824 /* If there is a hole in the sequence count then fail. */
James Smarteeead812009-12-21 17:01:23 -050013825 if (++seq_count != be16_to_cpu(hdr->fh_seq_cnt))
James Smart4f774512009-05-22 14:52:35 -040013826 return 0;
13827 fctl = (hdr->fh_f_ctl[0] << 16 |
13828 hdr->fh_f_ctl[1] << 8 |
13829 hdr->fh_f_ctl[2]);
13830 /* If last frame of sequence we can return success. */
13831 if (fctl & FC_FC_END_SEQ)
13832 return 1;
13833 }
13834 return 0;
13835}
13836
13837/**
13838 * lpfc_prep_seq - Prep sequence for ULP processing
13839 * @vport: Pointer to the vport on which this sequence was received
13840 * @dmabuf: pointer to a dmabuf that describes the FC sequence
13841 *
13842 * This function takes a sequence, described by a list of frames, and creates
13843 * a list of iocbq structures to describe the sequence. This iocbq list will be
13844 * used to issue to the generic unsolicited sequence handler. This routine
13845 * returns a pointer to the first iocbq in the list. If the function is unable
13846 * to allocate an iocbq then it throw out the received frames that were not
13847 * able to be described and return a pointer to the first iocbq. If unable to
13848 * allocate any iocbqs (including the first) this function will return NULL.
13849 **/
13850static struct lpfc_iocbq *
13851lpfc_prep_seq(struct lpfc_vport *vport, struct hbq_dmabuf *seq_dmabuf)
13852{
James Smart7851fe22011-07-22 18:36:52 -040013853 struct hbq_dmabuf *hbq_buf;
James Smart4f774512009-05-22 14:52:35 -040013854 struct lpfc_dmabuf *d_buf, *n_buf;
13855 struct lpfc_iocbq *first_iocbq, *iocbq;
13856 struct fc_frame_header *fc_hdr;
13857 uint32_t sid;
James Smart7851fe22011-07-22 18:36:52 -040013858 uint32_t len, tot_len;
James Smarteeead812009-12-21 17:01:23 -050013859 struct ulp_bde64 *pbde;
James Smart4f774512009-05-22 14:52:35 -040013860
13861 fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
13862 /* remove from receive buffer list */
13863 list_del_init(&seq_dmabuf->hbuf.list);
James Smart45ed1192009-10-02 15:17:02 -040013864 lpfc_update_rcv_time_stamp(vport);
James Smart4f774512009-05-22 14:52:35 -040013865 /* get the Remote Port's SID */
James Smart6669f9b2009-10-02 15:16:45 -040013866 sid = sli4_sid_from_fc_hdr(fc_hdr);
James Smart7851fe22011-07-22 18:36:52 -040013867 tot_len = 0;
James Smart4f774512009-05-22 14:52:35 -040013868 /* Get an iocbq struct to fill in. */
13869 first_iocbq = lpfc_sli_get_iocbq(vport->phba);
13870 if (first_iocbq) {
13871 /* Initialize the first IOCB. */
James Smart8fa38512009-07-19 10:01:03 -040013872 first_iocbq->iocb.unsli3.rcvsli3.acc_len = 0;
James Smart4f774512009-05-22 14:52:35 -040013873 first_iocbq->iocb.ulpStatus = IOSTAT_SUCCESS;
13874 first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_SEQ64_CX;
James Smart7851fe22011-07-22 18:36:52 -040013875 first_iocbq->iocb.ulpContext = NO_XRI;
13876 first_iocbq->iocb.unsli3.rcvsli3.ox_id =
13877 be16_to_cpu(fc_hdr->fh_ox_id);
13878 /* iocbq is prepped for internal consumption. Physical vpi. */
13879 first_iocbq->iocb.unsli3.rcvsli3.vpi =
13880 vport->phba->vpi_ids[vport->vpi];
James Smart4f774512009-05-22 14:52:35 -040013881 /* put the first buffer into the first IOCBq */
13882 first_iocbq->context2 = &seq_dmabuf->dbuf;
13883 first_iocbq->context3 = NULL;
13884 first_iocbq->iocb.ulpBdeCount = 1;
13885 first_iocbq->iocb.un.cont64[0].tus.f.bdeSize =
13886 LPFC_DATA_BUF_SIZE;
13887 first_iocbq->iocb.un.rcvels.remoteID = sid;
James Smart7851fe22011-07-22 18:36:52 -040013888 tot_len = bf_get(lpfc_rcqe_length,
James Smart4d9ab992009-10-02 15:16:39 -040013889 &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
James Smart7851fe22011-07-22 18:36:52 -040013890 first_iocbq->iocb.unsli3.rcvsli3.acc_len = tot_len;
James Smart4f774512009-05-22 14:52:35 -040013891 }
13892 iocbq = first_iocbq;
13893 /*
13894 * Each IOCBq can have two Buffers assigned, so go through the list
13895 * of buffers for this sequence and save two buffers in each IOCBq
13896 */
13897 list_for_each_entry_safe(d_buf, n_buf, &seq_dmabuf->dbuf.list, list) {
13898 if (!iocbq) {
13899 lpfc_in_buf_free(vport->phba, d_buf);
13900 continue;
13901 }
13902 if (!iocbq->context3) {
13903 iocbq->context3 = d_buf;
13904 iocbq->iocb.ulpBdeCount++;
James Smarteeead812009-12-21 17:01:23 -050013905 pbde = (struct ulp_bde64 *)
13906 &iocbq->iocb.unsli3.sli3Words[4];
13907 pbde->tus.f.bdeSize = LPFC_DATA_BUF_SIZE;
James Smart7851fe22011-07-22 18:36:52 -040013908
13909 /* We need to get the size out of the right CQE */
13910 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
13911 len = bf_get(lpfc_rcqe_length,
13912 &hbq_buf->cq_event.cqe.rcqe_cmpl);
13913 iocbq->iocb.unsli3.rcvsli3.acc_len += len;
13914 tot_len += len;
James Smart4f774512009-05-22 14:52:35 -040013915 } else {
13916 iocbq = lpfc_sli_get_iocbq(vport->phba);
13917 if (!iocbq) {
13918 if (first_iocbq) {
13919 first_iocbq->iocb.ulpStatus =
13920 IOSTAT_FCP_RSP_ERROR;
13921 first_iocbq->iocb.un.ulpWord[4] =
13922 IOERR_NO_RESOURCES;
13923 }
13924 lpfc_in_buf_free(vport->phba, d_buf);
13925 continue;
13926 }
13927 iocbq->context2 = d_buf;
13928 iocbq->context3 = NULL;
13929 iocbq->iocb.ulpBdeCount = 1;
13930 iocbq->iocb.un.cont64[0].tus.f.bdeSize =
13931 LPFC_DATA_BUF_SIZE;
James Smart7851fe22011-07-22 18:36:52 -040013932
13933 /* We need to get the size out of the right CQE */
13934 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
13935 len = bf_get(lpfc_rcqe_length,
13936 &hbq_buf->cq_event.cqe.rcqe_cmpl);
13937 tot_len += len;
13938 iocbq->iocb.unsli3.rcvsli3.acc_len = tot_len;
13939
James Smart4f774512009-05-22 14:52:35 -040013940 iocbq->iocb.un.rcvels.remoteID = sid;
13941 list_add_tail(&iocbq->list, &first_iocbq->list);
13942 }
13943 }
13944 return first_iocbq;
13945}
13946
James Smart6669f9b2009-10-02 15:16:45 -040013947static void
13948lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *vport,
13949 struct hbq_dmabuf *seq_dmabuf)
13950{
13951 struct fc_frame_header *fc_hdr;
13952 struct lpfc_iocbq *iocbq, *curr_iocb, *next_iocb;
13953 struct lpfc_hba *phba = vport->phba;
13954
13955 fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
13956 iocbq = lpfc_prep_seq(vport, seq_dmabuf);
13957 if (!iocbq) {
13958 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13959 "2707 Ring %d handler: Failed to allocate "
13960 "iocb Rctl x%x Type x%x received\n",
13961 LPFC_ELS_RING,
13962 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
13963 return;
13964 }
13965 if (!lpfc_complete_unsol_iocb(phba,
13966 &phba->sli.ring[LPFC_ELS_RING],
13967 iocbq, fc_hdr->fh_r_ctl,
13968 fc_hdr->fh_type))
James Smart6d368e52011-05-24 11:44:12 -040013969 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart6669f9b2009-10-02 15:16:45 -040013970 "2540 Ring %d handler: unexpected Rctl "
13971 "x%x Type x%x received\n",
13972 LPFC_ELS_RING,
13973 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
13974
13975 /* Free iocb created in lpfc_prep_seq */
13976 list_for_each_entry_safe(curr_iocb, next_iocb,
13977 &iocbq->list, list) {
13978 list_del_init(&curr_iocb->list);
13979 lpfc_sli_release_iocbq(phba, curr_iocb);
13980 }
13981 lpfc_sli_release_iocbq(phba, iocbq);
13982}
13983
James Smart4f774512009-05-22 14:52:35 -040013984/**
13985 * lpfc_sli4_handle_received_buffer - Handle received buffers from firmware
13986 * @phba: Pointer to HBA context object.
13987 *
13988 * This function is called with no lock held. This function processes all
13989 * the received buffers and gives it to upper layers when a received buffer
13990 * indicates that it is the final frame in the sequence. The interrupt
13991 * service routine processes received buffers at interrupt contexts and adds
13992 * received dma buffers to the rb_pend_list queue and signals the worker thread.
13993 * Worker thread calls lpfc_sli4_handle_received_buffer, which will call the
13994 * appropriate receive function when the final frame in a sequence is received.
13995 **/
James Smart4d9ab992009-10-02 15:16:39 -040013996void
13997lpfc_sli4_handle_received_buffer(struct lpfc_hba *phba,
13998 struct hbq_dmabuf *dmabuf)
James Smart4f774512009-05-22 14:52:35 -040013999{
James Smart4d9ab992009-10-02 15:16:39 -040014000 struct hbq_dmabuf *seq_dmabuf;
James Smart4f774512009-05-22 14:52:35 -040014001 struct fc_frame_header *fc_hdr;
14002 struct lpfc_vport *vport;
14003 uint32_t fcfi;
James Smart4f774512009-05-22 14:52:35 -040014004
James Smart4f774512009-05-22 14:52:35 -040014005 /* Process each received buffer */
James Smart4d9ab992009-10-02 15:16:39 -040014006 fc_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
14007 /* check to see if this a valid type of frame */
14008 if (lpfc_fc_frame_check(phba, fc_hdr)) {
14009 lpfc_in_buf_free(phba, &dmabuf->dbuf);
14010 return;
14011 }
James Smart7851fe22011-07-22 18:36:52 -040014012 if ((bf_get(lpfc_cqe_code,
14013 &dmabuf->cq_event.cqe.rcqe_cmpl) == CQE_CODE_RECEIVE_V1))
14014 fcfi = bf_get(lpfc_rcqe_fcf_id_v1,
14015 &dmabuf->cq_event.cqe.rcqe_cmpl);
14016 else
14017 fcfi = bf_get(lpfc_rcqe_fcf_id,
14018 &dmabuf->cq_event.cqe.rcqe_cmpl);
James Smart4d9ab992009-10-02 15:16:39 -040014019 vport = lpfc_fc_frame_to_vport(phba, fc_hdr, fcfi);
James Smartc8685952009-11-18 15:39:16 -050014020 if (!vport || !(vport->vpi_state & LPFC_VPI_REGISTERED)) {
James Smart4d9ab992009-10-02 15:16:39 -040014021 /* throw out the frame */
14022 lpfc_in_buf_free(phba, &dmabuf->dbuf);
14023 return;
14024 }
James Smart6669f9b2009-10-02 15:16:45 -040014025 /* Handle the basic abort sequence (BA_ABTS) event */
14026 if (fc_hdr->fh_r_ctl == FC_RCTL_BA_ABTS) {
14027 lpfc_sli4_handle_unsol_abort(vport, dmabuf);
14028 return;
14029 }
14030
James Smart4d9ab992009-10-02 15:16:39 -040014031 /* Link this frame */
14032 seq_dmabuf = lpfc_fc_frame_add(vport, dmabuf);
14033 if (!seq_dmabuf) {
14034 /* unable to add frame to vport - throw it out */
14035 lpfc_in_buf_free(phba, &dmabuf->dbuf);
14036 return;
14037 }
14038 /* If not last frame in sequence continue processing frames. */
James Smartdef9c7a2009-12-21 17:02:28 -050014039 if (!lpfc_seq_complete(seq_dmabuf))
James Smart4d9ab992009-10-02 15:16:39 -040014040 return;
James Smartdef9c7a2009-12-21 17:02:28 -050014041
James Smart6669f9b2009-10-02 15:16:45 -040014042 /* Send the complete sequence to the upper layer protocol */
14043 lpfc_sli4_send_seq_to_ulp(vport, seq_dmabuf);
James Smart4f774512009-05-22 14:52:35 -040014044}
James Smart6fb120a2009-05-22 14:52:59 -040014045
14046/**
14047 * lpfc_sli4_post_all_rpi_hdrs - Post the rpi header memory region to the port
14048 * @phba: pointer to lpfc hba data structure.
14049 *
14050 * This routine is invoked to post rpi header templates to the
14051 * HBA consistent with the SLI-4 interface spec. This routine
James Smart49198b32010-04-06 15:04:33 -040014052 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
14053 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
James Smart6fb120a2009-05-22 14:52:59 -040014054 *
14055 * This routine does not require any locks. It's usage is expected
14056 * to be driver load or reset recovery when the driver is
14057 * sequential.
14058 *
14059 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020014060 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -040014061 * -EIO - The mailbox failed to complete successfully.
James Smart6fb120a2009-05-22 14:52:59 -040014062 * When this error occurs, the driver is not guaranteed
14063 * to have any rpi regions posted to the device and
14064 * must either attempt to repost the regions or take a
14065 * fatal error.
14066 **/
14067int
14068lpfc_sli4_post_all_rpi_hdrs(struct lpfc_hba *phba)
14069{
14070 struct lpfc_rpi_hdr *rpi_page;
14071 uint32_t rc = 0;
James Smart6d368e52011-05-24 11:44:12 -040014072 uint16_t lrpi = 0;
James Smart6fb120a2009-05-22 14:52:59 -040014073
James Smart6d368e52011-05-24 11:44:12 -040014074 /* SLI4 ports that support extents do not require RPI headers. */
14075 if (!phba->sli4_hba.rpi_hdrs_in_use)
14076 goto exit;
14077 if (phba->sli4_hba.extents_in_use)
14078 return -EIO;
14079
James Smart6fb120a2009-05-22 14:52:59 -040014080 list_for_each_entry(rpi_page, &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
James Smart6d368e52011-05-24 11:44:12 -040014081 /*
14082 * Assign the rpi headers a physical rpi only if the driver
14083 * has not initialized those resources. A port reset only
14084 * needs the headers posted.
14085 */
14086 if (bf_get(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags) !=
14087 LPFC_RPI_RSRC_RDY)
14088 rpi_page->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
14089
James Smart6fb120a2009-05-22 14:52:59 -040014090 rc = lpfc_sli4_post_rpi_hdr(phba, rpi_page);
14091 if (rc != MBX_SUCCESS) {
14092 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14093 "2008 Error %d posting all rpi "
14094 "headers\n", rc);
14095 rc = -EIO;
14096 break;
14097 }
14098 }
14099
James Smart6d368e52011-05-24 11:44:12 -040014100 exit:
14101 bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags,
14102 LPFC_RPI_RSRC_RDY);
James Smart6fb120a2009-05-22 14:52:59 -040014103 return rc;
14104}
14105
14106/**
14107 * lpfc_sli4_post_rpi_hdr - Post an rpi header memory region to the port
14108 * @phba: pointer to lpfc hba data structure.
14109 * @rpi_page: pointer to the rpi memory region.
14110 *
14111 * This routine is invoked to post a single rpi header to the
14112 * HBA consistent with the SLI-4 interface spec. This memory region
14113 * maps up to 64 rpi context regions.
14114 *
14115 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020014116 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -040014117 * -ENOMEM - No available memory
14118 * -EIO - The mailbox failed to complete successfully.
James Smart6fb120a2009-05-22 14:52:59 -040014119 **/
14120int
14121lpfc_sli4_post_rpi_hdr(struct lpfc_hba *phba, struct lpfc_rpi_hdr *rpi_page)
14122{
14123 LPFC_MBOXQ_t *mboxq;
14124 struct lpfc_mbx_post_hdr_tmpl *hdr_tmpl;
14125 uint32_t rc = 0;
James Smart6fb120a2009-05-22 14:52:59 -040014126 uint32_t shdr_status, shdr_add_status;
14127 union lpfc_sli4_cfg_shdr *shdr;
14128
James Smart6d368e52011-05-24 11:44:12 -040014129 /* SLI4 ports that support extents do not require RPI headers. */
14130 if (!phba->sli4_hba.rpi_hdrs_in_use)
14131 return rc;
14132 if (phba->sli4_hba.extents_in_use)
14133 return -EIO;
14134
James Smart6fb120a2009-05-22 14:52:59 -040014135 /* The port is notified of the header region via a mailbox command. */
14136 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14137 if (!mboxq) {
14138 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14139 "2001 Unable to allocate memory for issuing "
14140 "SLI_CONFIG_SPECIAL mailbox command\n");
14141 return -ENOMEM;
14142 }
14143
14144 /* Post all rpi memory regions to the port. */
14145 hdr_tmpl = &mboxq->u.mqe.un.hdr_tmpl;
James Smart6fb120a2009-05-22 14:52:59 -040014146 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
14147 LPFC_MBOX_OPCODE_FCOE_POST_HDR_TEMPLATE,
14148 sizeof(struct lpfc_mbx_post_hdr_tmpl) -
James Smartfedd3b72011-02-16 12:39:24 -050014149 sizeof(struct lpfc_sli4_cfg_mhdr),
14150 LPFC_SLI4_MBX_EMBED);
James Smart6d368e52011-05-24 11:44:12 -040014151
14152
14153 /* Post the physical rpi to the port for this rpi header. */
James Smart6fb120a2009-05-22 14:52:59 -040014154 bf_set(lpfc_mbx_post_hdr_tmpl_rpi_offset, hdr_tmpl,
14155 rpi_page->start_rpi);
James Smart6d368e52011-05-24 11:44:12 -040014156 bf_set(lpfc_mbx_post_hdr_tmpl_page_cnt,
14157 hdr_tmpl, rpi_page->page_count);
14158
James Smart6fb120a2009-05-22 14:52:59 -040014159 hdr_tmpl->rpi_paddr_lo = putPaddrLow(rpi_page->dmabuf->phys);
14160 hdr_tmpl->rpi_paddr_hi = putPaddrHigh(rpi_page->dmabuf->phys);
James Smartf1126682009-06-10 17:22:44 -040014161 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
James Smart6fb120a2009-05-22 14:52:59 -040014162 shdr = (union lpfc_sli4_cfg_shdr *) &hdr_tmpl->header.cfg_shdr;
14163 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14164 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14165 if (rc != MBX_TIMEOUT)
14166 mempool_free(mboxq, phba->mbox_mem_pool);
14167 if (shdr_status || shdr_add_status || rc) {
14168 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14169 "2514 POST_RPI_HDR mailbox failed with "
14170 "status x%x add_status x%x, mbx status x%x\n",
14171 shdr_status, shdr_add_status, rc);
14172 rc = -ENXIO;
14173 }
14174 return rc;
14175}
14176
14177/**
14178 * lpfc_sli4_alloc_rpi - Get an available rpi in the device's range
14179 * @phba: pointer to lpfc hba data structure.
14180 *
14181 * This routine is invoked to post rpi header templates to the
14182 * HBA consistent with the SLI-4 interface spec. This routine
James Smart49198b32010-04-06 15:04:33 -040014183 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
14184 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
James Smart6fb120a2009-05-22 14:52:59 -040014185 *
14186 * Returns
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020014187 * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
James Smart6fb120a2009-05-22 14:52:59 -040014188 * LPFC_RPI_ALLOC_ERROR if no rpis are available.
14189 **/
14190int
14191lpfc_sli4_alloc_rpi(struct lpfc_hba *phba)
14192{
James Smart6d368e52011-05-24 11:44:12 -040014193 unsigned long rpi;
14194 uint16_t max_rpi, rpi_limit;
14195 uint16_t rpi_remaining, lrpi = 0;
James Smart6fb120a2009-05-22 14:52:59 -040014196 struct lpfc_rpi_hdr *rpi_hdr;
14197
14198 max_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
James Smart6fb120a2009-05-22 14:52:59 -040014199 rpi_limit = phba->sli4_hba.next_rpi;
14200
14201 /*
James Smart6d368e52011-05-24 11:44:12 -040014202 * Fetch the next logical rpi. Because this index is logical,
14203 * the driver starts at 0 each time.
James Smart6fb120a2009-05-22 14:52:59 -040014204 */
14205 spin_lock_irq(&phba->hbalock);
James Smart6d368e52011-05-24 11:44:12 -040014206 rpi = find_next_zero_bit(phba->sli4_hba.rpi_bmask, rpi_limit, 0);
14207 if (rpi >= rpi_limit)
James Smart6fb120a2009-05-22 14:52:59 -040014208 rpi = LPFC_RPI_ALLOC_ERROR;
14209 else {
14210 set_bit(rpi, phba->sli4_hba.rpi_bmask);
14211 phba->sli4_hba.max_cfg_param.rpi_used++;
14212 phba->sli4_hba.rpi_count++;
14213 }
14214
14215 /*
14216 * Don't try to allocate more rpi header regions if the device limit
James Smart6d368e52011-05-24 11:44:12 -040014217 * has been exhausted.
James Smart6fb120a2009-05-22 14:52:59 -040014218 */
14219 if ((rpi == LPFC_RPI_ALLOC_ERROR) &&
14220 (phba->sli4_hba.rpi_count >= max_rpi)) {
14221 spin_unlock_irq(&phba->hbalock);
14222 return rpi;
14223 }
14224
14225 /*
James Smart6d368e52011-05-24 11:44:12 -040014226 * RPI header postings are not required for SLI4 ports capable of
14227 * extents.
14228 */
14229 if (!phba->sli4_hba.rpi_hdrs_in_use) {
14230 spin_unlock_irq(&phba->hbalock);
14231 return rpi;
14232 }
14233
14234 /*
James Smart6fb120a2009-05-22 14:52:59 -040014235 * If the driver is running low on rpi resources, allocate another
14236 * page now. Note that the next_rpi value is used because
14237 * it represents how many are actually in use whereas max_rpi notes
14238 * how many are supported max by the device.
14239 */
James Smart6d368e52011-05-24 11:44:12 -040014240 rpi_remaining = phba->sli4_hba.next_rpi - phba->sli4_hba.rpi_count;
James Smart6fb120a2009-05-22 14:52:59 -040014241 spin_unlock_irq(&phba->hbalock);
14242 if (rpi_remaining < LPFC_RPI_LOW_WATER_MARK) {
14243 rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
14244 if (!rpi_hdr) {
14245 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14246 "2002 Error Could not grow rpi "
14247 "count\n");
14248 } else {
James Smart6d368e52011-05-24 11:44:12 -040014249 lrpi = rpi_hdr->start_rpi;
14250 rpi_hdr->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
James Smart6fb120a2009-05-22 14:52:59 -040014251 lpfc_sli4_post_rpi_hdr(phba, rpi_hdr);
14252 }
14253 }
14254
14255 return rpi;
14256}
14257
14258/**
14259 * lpfc_sli4_free_rpi - Release an rpi for reuse.
14260 * @phba: pointer to lpfc hba data structure.
14261 *
14262 * This routine is invoked to release an rpi to the pool of
14263 * available rpis maintained by the driver.
14264 **/
14265void
James Smartd7c47992010-06-08 18:31:54 -040014266__lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
14267{
14268 if (test_and_clear_bit(rpi, phba->sli4_hba.rpi_bmask)) {
14269 phba->sli4_hba.rpi_count--;
14270 phba->sli4_hba.max_cfg_param.rpi_used--;
14271 }
14272}
14273
14274/**
14275 * lpfc_sli4_free_rpi - Release an rpi for reuse.
14276 * @phba: pointer to lpfc hba data structure.
14277 *
14278 * This routine is invoked to release an rpi to the pool of
14279 * available rpis maintained by the driver.
14280 **/
14281void
James Smart6fb120a2009-05-22 14:52:59 -040014282lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
14283{
14284 spin_lock_irq(&phba->hbalock);
James Smartd7c47992010-06-08 18:31:54 -040014285 __lpfc_sli4_free_rpi(phba, rpi);
James Smart6fb120a2009-05-22 14:52:59 -040014286 spin_unlock_irq(&phba->hbalock);
14287}
14288
14289/**
14290 * lpfc_sli4_remove_rpis - Remove the rpi bitmask region
14291 * @phba: pointer to lpfc hba data structure.
14292 *
14293 * This routine is invoked to remove the memory region that
14294 * provided rpi via a bitmask.
14295 **/
14296void
14297lpfc_sli4_remove_rpis(struct lpfc_hba *phba)
14298{
14299 kfree(phba->sli4_hba.rpi_bmask);
James Smart6d368e52011-05-24 11:44:12 -040014300 kfree(phba->sli4_hba.rpi_ids);
14301 bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
James Smart6fb120a2009-05-22 14:52:59 -040014302}
14303
14304/**
14305 * lpfc_sli4_resume_rpi - Remove the rpi bitmask region
14306 * @phba: pointer to lpfc hba data structure.
14307 *
14308 * This routine is invoked to remove the memory region that
14309 * provided rpi via a bitmask.
14310 **/
14311int
14312lpfc_sli4_resume_rpi(struct lpfc_nodelist *ndlp)
14313{
14314 LPFC_MBOXQ_t *mboxq;
14315 struct lpfc_hba *phba = ndlp->phba;
14316 int rc;
14317
14318 /* The port is notified of the header region via a mailbox command. */
14319 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14320 if (!mboxq)
14321 return -ENOMEM;
14322
14323 /* Post all rpi memory regions to the port. */
14324 lpfc_resume_rpi(mboxq, ndlp);
14325 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
14326 if (rc == MBX_NOT_FINISHED) {
14327 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14328 "2010 Resume RPI Mailbox failed "
14329 "status %d, mbxStatus x%x\n", rc,
14330 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
14331 mempool_free(mboxq, phba->mbox_mem_pool);
14332 return -EIO;
14333 }
14334 return 0;
14335}
14336
14337/**
14338 * lpfc_sli4_init_vpi - Initialize a vpi with the port
James Smart76a95d72010-11-20 23:11:48 -050014339 * @vport: Pointer to the vport for which the vpi is being initialized
James Smart6fb120a2009-05-22 14:52:59 -040014340 *
James Smart76a95d72010-11-20 23:11:48 -050014341 * This routine is invoked to activate a vpi with the port.
James Smart6fb120a2009-05-22 14:52:59 -040014342 *
14343 * Returns:
14344 * 0 success
14345 * -Evalue otherwise
14346 **/
14347int
James Smart76a95d72010-11-20 23:11:48 -050014348lpfc_sli4_init_vpi(struct lpfc_vport *vport)
James Smart6fb120a2009-05-22 14:52:59 -040014349{
14350 LPFC_MBOXQ_t *mboxq;
14351 int rc = 0;
James Smart6a9c52c2009-10-02 15:16:51 -040014352 int retval = MBX_SUCCESS;
James Smart6fb120a2009-05-22 14:52:59 -040014353 uint32_t mbox_tmo;
James Smart76a95d72010-11-20 23:11:48 -050014354 struct lpfc_hba *phba = vport->phba;
James Smart6fb120a2009-05-22 14:52:59 -040014355 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14356 if (!mboxq)
14357 return -ENOMEM;
James Smart76a95d72010-11-20 23:11:48 -050014358 lpfc_init_vpi(phba, mboxq, vport->vpi);
James Smart6fb120a2009-05-22 14:52:59 -040014359 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_INIT_VPI);
14360 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
James Smart6fb120a2009-05-22 14:52:59 -040014361 if (rc != MBX_SUCCESS) {
James Smart76a95d72010-11-20 23:11:48 -050014362 lpfc_printf_vlog(vport, KERN_ERR, LOG_SLI,
James Smart6fb120a2009-05-22 14:52:59 -040014363 "2022 INIT VPI Mailbox failed "
14364 "status %d, mbxStatus x%x\n", rc,
14365 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
James Smart6a9c52c2009-10-02 15:16:51 -040014366 retval = -EIO;
James Smart6fb120a2009-05-22 14:52:59 -040014367 }
James Smart6a9c52c2009-10-02 15:16:51 -040014368 if (rc != MBX_TIMEOUT)
James Smart76a95d72010-11-20 23:11:48 -050014369 mempool_free(mboxq, vport->phba->mbox_mem_pool);
James Smart6a9c52c2009-10-02 15:16:51 -040014370
14371 return retval;
James Smart6fb120a2009-05-22 14:52:59 -040014372}
14373
14374/**
14375 * lpfc_mbx_cmpl_add_fcf_record - add fcf mbox completion handler.
14376 * @phba: pointer to lpfc hba data structure.
14377 * @mboxq: Pointer to mailbox object.
14378 *
14379 * This routine is invoked to manually add a single FCF record. The caller
14380 * must pass a completely initialized FCF_Record. This routine takes
14381 * care of the nonembedded mailbox operations.
14382 **/
14383static void
14384lpfc_mbx_cmpl_add_fcf_record(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
14385{
14386 void *virt_addr;
14387 union lpfc_sli4_cfg_shdr *shdr;
14388 uint32_t shdr_status, shdr_add_status;
14389
14390 virt_addr = mboxq->sge_array->addr[0];
14391 /* The IOCTL status is embedded in the mailbox subheader. */
14392 shdr = (union lpfc_sli4_cfg_shdr *) virt_addr;
14393 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14394 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14395
14396 if ((shdr_status || shdr_add_status) &&
14397 (shdr_status != STATUS_FCF_IN_USE))
14398 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14399 "2558 ADD_FCF_RECORD mailbox failed with "
14400 "status x%x add_status x%x\n",
14401 shdr_status, shdr_add_status);
14402
14403 lpfc_sli4_mbox_cmd_free(phba, mboxq);
14404}
14405
14406/**
14407 * lpfc_sli4_add_fcf_record - Manually add an FCF Record.
14408 * @phba: pointer to lpfc hba data structure.
14409 * @fcf_record: pointer to the initialized fcf record to add.
14410 *
14411 * This routine is invoked to manually add a single FCF record. The caller
14412 * must pass a completely initialized FCF_Record. This routine takes
14413 * care of the nonembedded mailbox operations.
14414 **/
14415int
14416lpfc_sli4_add_fcf_record(struct lpfc_hba *phba, struct fcf_record *fcf_record)
14417{
14418 int rc = 0;
14419 LPFC_MBOXQ_t *mboxq;
14420 uint8_t *bytep;
14421 void *virt_addr;
14422 dma_addr_t phys_addr;
14423 struct lpfc_mbx_sge sge;
14424 uint32_t alloc_len, req_len;
14425 uint32_t fcfindex;
14426
14427 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14428 if (!mboxq) {
14429 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14430 "2009 Failed to allocate mbox for ADD_FCF cmd\n");
14431 return -ENOMEM;
14432 }
14433
14434 req_len = sizeof(struct fcf_record) + sizeof(union lpfc_sli4_cfg_shdr) +
14435 sizeof(uint32_t);
14436
14437 /* Allocate DMA memory and set up the non-embedded mailbox command */
14438 alloc_len = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
14439 LPFC_MBOX_OPCODE_FCOE_ADD_FCF,
14440 req_len, LPFC_SLI4_MBX_NEMBED);
14441 if (alloc_len < req_len) {
14442 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14443 "2523 Allocated DMA memory size (x%x) is "
14444 "less than the requested DMA memory "
14445 "size (x%x)\n", alloc_len, req_len);
14446 lpfc_sli4_mbox_cmd_free(phba, mboxq);
14447 return -ENOMEM;
14448 }
14449
14450 /*
14451 * Get the first SGE entry from the non-embedded DMA memory. This
14452 * routine only uses a single SGE.
14453 */
14454 lpfc_sli4_mbx_sge_get(mboxq, 0, &sge);
14455 phys_addr = getPaddr(sge.pa_hi, sge.pa_lo);
James Smart6fb120a2009-05-22 14:52:59 -040014456 virt_addr = mboxq->sge_array->addr[0];
14457 /*
14458 * Configure the FCF record for FCFI 0. This is the driver's
14459 * hardcoded default and gets used in nonFIP mode.
14460 */
14461 fcfindex = bf_get(lpfc_fcf_record_fcf_index, fcf_record);
14462 bytep = virt_addr + sizeof(union lpfc_sli4_cfg_shdr);
14463 lpfc_sli_pcimem_bcopy(&fcfindex, bytep, sizeof(uint32_t));
14464
14465 /*
14466 * Copy the fcf_index and the FCF Record Data. The data starts after
14467 * the FCoE header plus word10. The data copy needs to be endian
14468 * correct.
14469 */
14470 bytep += sizeof(uint32_t);
14471 lpfc_sli_pcimem_bcopy(fcf_record, bytep, sizeof(struct fcf_record));
14472 mboxq->vport = phba->pport;
14473 mboxq->mbox_cmpl = lpfc_mbx_cmpl_add_fcf_record;
14474 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
14475 if (rc == MBX_NOT_FINISHED) {
14476 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14477 "2515 ADD_FCF_RECORD mailbox failed with "
14478 "status 0x%x\n", rc);
14479 lpfc_sli4_mbox_cmd_free(phba, mboxq);
14480 rc = -EIO;
14481 } else
14482 rc = 0;
14483
14484 return rc;
14485}
14486
14487/**
14488 * lpfc_sli4_build_dflt_fcf_record - Build the driver's default FCF Record.
14489 * @phba: pointer to lpfc hba data structure.
14490 * @fcf_record: pointer to the fcf record to write the default data.
14491 * @fcf_index: FCF table entry index.
14492 *
14493 * This routine is invoked to build the driver's default FCF record. The
14494 * values used are hardcoded. This routine handles memory initialization.
14495 *
14496 **/
14497void
14498lpfc_sli4_build_dflt_fcf_record(struct lpfc_hba *phba,
14499 struct fcf_record *fcf_record,
14500 uint16_t fcf_index)
14501{
14502 memset(fcf_record, 0, sizeof(struct fcf_record));
14503 fcf_record->max_rcv_size = LPFC_FCOE_MAX_RCV_SIZE;
14504 fcf_record->fka_adv_period = LPFC_FCOE_FKA_ADV_PER;
14505 fcf_record->fip_priority = LPFC_FCOE_FIP_PRIORITY;
14506 bf_set(lpfc_fcf_record_mac_0, fcf_record, phba->fc_map[0]);
14507 bf_set(lpfc_fcf_record_mac_1, fcf_record, phba->fc_map[1]);
14508 bf_set(lpfc_fcf_record_mac_2, fcf_record, phba->fc_map[2]);
14509 bf_set(lpfc_fcf_record_mac_3, fcf_record, LPFC_FCOE_FCF_MAC3);
14510 bf_set(lpfc_fcf_record_mac_4, fcf_record, LPFC_FCOE_FCF_MAC4);
14511 bf_set(lpfc_fcf_record_mac_5, fcf_record, LPFC_FCOE_FCF_MAC5);
14512 bf_set(lpfc_fcf_record_fc_map_0, fcf_record, phba->fc_map[0]);
14513 bf_set(lpfc_fcf_record_fc_map_1, fcf_record, phba->fc_map[1]);
14514 bf_set(lpfc_fcf_record_fc_map_2, fcf_record, phba->fc_map[2]);
14515 bf_set(lpfc_fcf_record_fcf_valid, fcf_record, 1);
James Smart0c287582009-06-10 17:22:56 -040014516 bf_set(lpfc_fcf_record_fcf_avail, fcf_record, 1);
James Smart6fb120a2009-05-22 14:52:59 -040014517 bf_set(lpfc_fcf_record_fcf_index, fcf_record, fcf_index);
14518 bf_set(lpfc_fcf_record_mac_addr_prov, fcf_record,
14519 LPFC_FCF_FPMA | LPFC_FCF_SPMA);
14520 /* Set the VLAN bit map */
14521 if (phba->valid_vlan) {
14522 fcf_record->vlan_bitmap[phba->vlan_id / 8]
14523 = 1 << (phba->vlan_id % 8);
14524 }
14525}
14526
14527/**
James Smart0c9ab6f2010-02-26 14:15:57 -050014528 * lpfc_sli4_fcf_scan_read_fcf_rec - Read hba fcf record for fcf scan.
James Smart6fb120a2009-05-22 14:52:59 -040014529 * @phba: pointer to lpfc hba data structure.
14530 * @fcf_index: FCF table entry offset.
14531 *
James Smart0c9ab6f2010-02-26 14:15:57 -050014532 * This routine is invoked to scan the entire FCF table by reading FCF
14533 * record and processing it one at a time starting from the @fcf_index
14534 * for initial FCF discovery or fast FCF failover rediscovery.
14535 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030014536 * Return 0 if the mailbox command is submitted successfully, none 0
James Smart0c9ab6f2010-02-26 14:15:57 -050014537 * otherwise.
James Smart6fb120a2009-05-22 14:52:59 -040014538 **/
14539int
James Smart0c9ab6f2010-02-26 14:15:57 -050014540lpfc_sli4_fcf_scan_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
James Smart6fb120a2009-05-22 14:52:59 -040014541{
14542 int rc = 0, error;
14543 LPFC_MBOXQ_t *mboxq;
James Smart6fb120a2009-05-22 14:52:59 -040014544
James Smart32b97932009-07-19 10:01:21 -040014545 phba->fcoe_eventtag_at_fcf_scan = phba->fcoe_eventtag;
James Smart6fb120a2009-05-22 14:52:59 -040014546 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14547 if (!mboxq) {
14548 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14549 "2000 Failed to allocate mbox for "
14550 "READ_FCF cmd\n");
James Smart4d9ab992009-10-02 15:16:39 -040014551 error = -ENOMEM;
James Smart0c9ab6f2010-02-26 14:15:57 -050014552 goto fail_fcf_scan;
James Smart6fb120a2009-05-22 14:52:59 -040014553 }
James Smartecfd03c2010-02-12 14:41:27 -050014554 /* Construct the read FCF record mailbox command */
James Smart0c9ab6f2010-02-26 14:15:57 -050014555 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
James Smartecfd03c2010-02-12 14:41:27 -050014556 if (rc) {
14557 error = -EINVAL;
James Smart0c9ab6f2010-02-26 14:15:57 -050014558 goto fail_fcf_scan;
James Smart6fb120a2009-05-22 14:52:59 -040014559 }
James Smartecfd03c2010-02-12 14:41:27 -050014560 /* Issue the mailbox command asynchronously */
James Smart6fb120a2009-05-22 14:52:59 -040014561 mboxq->vport = phba->pport;
James Smart0c9ab6f2010-02-26 14:15:57 -050014562 mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_scan_read_fcf_rec;
James Smarta93ff372010-10-22 11:06:08 -040014563
14564 spin_lock_irq(&phba->hbalock);
14565 phba->hba_flag |= FCF_TS_INPROG;
14566 spin_unlock_irq(&phba->hbalock);
14567
James Smart6fb120a2009-05-22 14:52:59 -040014568 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
James Smartecfd03c2010-02-12 14:41:27 -050014569 if (rc == MBX_NOT_FINISHED)
James Smart6fb120a2009-05-22 14:52:59 -040014570 error = -EIO;
James Smartecfd03c2010-02-12 14:41:27 -050014571 else {
James Smart38b92ef2010-08-04 16:11:39 -040014572 /* Reset eligible FCF count for new scan */
14573 if (fcf_index == LPFC_FCOE_FCF_GET_FIRST)
James Smart999d8132010-03-15 11:24:56 -040014574 phba->fcf.eligible_fcf_cnt = 0;
James Smart6fb120a2009-05-22 14:52:59 -040014575 error = 0;
James Smart32b97932009-07-19 10:01:21 -040014576 }
James Smart0c9ab6f2010-02-26 14:15:57 -050014577fail_fcf_scan:
James Smart4d9ab992009-10-02 15:16:39 -040014578 if (error) {
14579 if (mboxq)
14580 lpfc_sli4_mbox_cmd_free(phba, mboxq);
James Smarta93ff372010-10-22 11:06:08 -040014581 /* FCF scan failed, clear FCF_TS_INPROG flag */
James Smart4d9ab992009-10-02 15:16:39 -040014582 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -040014583 phba->hba_flag &= ~FCF_TS_INPROG;
James Smart4d9ab992009-10-02 15:16:39 -040014584 spin_unlock_irq(&phba->hbalock);
14585 }
James Smart6fb120a2009-05-22 14:52:59 -040014586 return error;
14587}
James Smarta0c87cb2009-07-19 10:01:10 -040014588
14589/**
James Smarta93ff372010-10-22 11:06:08 -040014590 * lpfc_sli4_fcf_rr_read_fcf_rec - Read hba fcf record for roundrobin fcf.
James Smart0c9ab6f2010-02-26 14:15:57 -050014591 * @phba: pointer to lpfc hba data structure.
14592 * @fcf_index: FCF table entry offset.
14593 *
14594 * This routine is invoked to read an FCF record indicated by @fcf_index
James Smarta93ff372010-10-22 11:06:08 -040014595 * and to use it for FLOGI roundrobin FCF failover.
James Smart0c9ab6f2010-02-26 14:15:57 -050014596 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030014597 * Return 0 if the mailbox command is submitted successfully, none 0
James Smart0c9ab6f2010-02-26 14:15:57 -050014598 * otherwise.
14599 **/
14600int
14601lpfc_sli4_fcf_rr_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
14602{
14603 int rc = 0, error;
14604 LPFC_MBOXQ_t *mboxq;
14605
14606 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14607 if (!mboxq) {
14608 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
14609 "2763 Failed to allocate mbox for "
14610 "READ_FCF cmd\n");
14611 error = -ENOMEM;
14612 goto fail_fcf_read;
14613 }
14614 /* Construct the read FCF record mailbox command */
14615 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
14616 if (rc) {
14617 error = -EINVAL;
14618 goto fail_fcf_read;
14619 }
14620 /* Issue the mailbox command asynchronously */
14621 mboxq->vport = phba->pport;
14622 mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_rr_read_fcf_rec;
14623 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
14624 if (rc == MBX_NOT_FINISHED)
14625 error = -EIO;
14626 else
14627 error = 0;
14628
14629fail_fcf_read:
14630 if (error && mboxq)
14631 lpfc_sli4_mbox_cmd_free(phba, mboxq);
14632 return error;
14633}
14634
14635/**
14636 * lpfc_sli4_read_fcf_rec - Read hba fcf record for update eligible fcf bmask.
14637 * @phba: pointer to lpfc hba data structure.
14638 * @fcf_index: FCF table entry offset.
14639 *
14640 * This routine is invoked to read an FCF record indicated by @fcf_index to
James Smarta93ff372010-10-22 11:06:08 -040014641 * determine whether it's eligible for FLOGI roundrobin failover list.
James Smart0c9ab6f2010-02-26 14:15:57 -050014642 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030014643 * Return 0 if the mailbox command is submitted successfully, none 0
James Smart0c9ab6f2010-02-26 14:15:57 -050014644 * otherwise.
14645 **/
14646int
14647lpfc_sli4_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
14648{
14649 int rc = 0, error;
14650 LPFC_MBOXQ_t *mboxq;
14651
14652 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14653 if (!mboxq) {
14654 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
14655 "2758 Failed to allocate mbox for "
14656 "READ_FCF cmd\n");
14657 error = -ENOMEM;
14658 goto fail_fcf_read;
14659 }
14660 /* Construct the read FCF record mailbox command */
14661 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
14662 if (rc) {
14663 error = -EINVAL;
14664 goto fail_fcf_read;
14665 }
14666 /* Issue the mailbox command asynchronously */
14667 mboxq->vport = phba->pport;
14668 mboxq->mbox_cmpl = lpfc_mbx_cmpl_read_fcf_rec;
14669 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
14670 if (rc == MBX_NOT_FINISHED)
14671 error = -EIO;
14672 else
14673 error = 0;
14674
14675fail_fcf_read:
14676 if (error && mboxq)
14677 lpfc_sli4_mbox_cmd_free(phba, mboxq);
14678 return error;
14679}
14680
14681/**
James Smart7d791df2011-07-22 18:37:52 -040014682 * lpfc_check_next_fcf_pri
14683 * phba pointer to the lpfc_hba struct for this port.
14684 * This routine is called from the lpfc_sli4_fcf_rr_next_index_get
14685 * routine when the rr_bmask is empty. The FCF indecies are put into the
14686 * rr_bmask based on their priority level. Starting from the highest priority
14687 * to the lowest. The most likely FCF candidate will be in the highest
14688 * priority group. When this routine is called it searches the fcf_pri list for
14689 * next lowest priority group and repopulates the rr_bmask with only those
14690 * fcf_indexes.
14691 * returns:
14692 * 1=success 0=failure
14693 **/
14694int
14695lpfc_check_next_fcf_pri_level(struct lpfc_hba *phba)
14696{
14697 uint16_t next_fcf_pri;
14698 uint16_t last_index;
14699 struct lpfc_fcf_pri *fcf_pri;
14700 int rc;
14701 int ret = 0;
14702
14703 last_index = find_first_bit(phba->fcf.fcf_rr_bmask,
14704 LPFC_SLI4_FCF_TBL_INDX_MAX);
14705 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
14706 "3060 Last IDX %d\n", last_index);
14707 if (list_empty(&phba->fcf.fcf_pri_list)) {
14708 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
14709 "3061 Last IDX %d\n", last_index);
14710 return 0; /* Empty rr list */
14711 }
14712 next_fcf_pri = 0;
14713 /*
14714 * Clear the rr_bmask and set all of the bits that are at this
14715 * priority.
14716 */
14717 memset(phba->fcf.fcf_rr_bmask, 0,
14718 sizeof(*phba->fcf.fcf_rr_bmask));
14719 spin_lock_irq(&phba->hbalock);
14720 list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
14721 if (fcf_pri->fcf_rec.flag & LPFC_FCF_FLOGI_FAILED)
14722 continue;
14723 /*
14724 * the 1st priority that has not FLOGI failed
14725 * will be the highest.
14726 */
14727 if (!next_fcf_pri)
14728 next_fcf_pri = fcf_pri->fcf_rec.priority;
14729 spin_unlock_irq(&phba->hbalock);
14730 if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
14731 rc = lpfc_sli4_fcf_rr_index_set(phba,
14732 fcf_pri->fcf_rec.fcf_index);
14733 if (rc)
14734 return 0;
14735 }
14736 spin_lock_irq(&phba->hbalock);
14737 }
14738 /*
14739 * if next_fcf_pri was not set above and the list is not empty then
14740 * we have failed flogis on all of them. So reset flogi failed
14741 * and start at the begining.
14742 */
14743 if (!next_fcf_pri && !list_empty(&phba->fcf.fcf_pri_list)) {
14744 list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
14745 fcf_pri->fcf_rec.flag &= ~LPFC_FCF_FLOGI_FAILED;
14746 /*
14747 * the 1st priority that has not FLOGI failed
14748 * will be the highest.
14749 */
14750 if (!next_fcf_pri)
14751 next_fcf_pri = fcf_pri->fcf_rec.priority;
14752 spin_unlock_irq(&phba->hbalock);
14753 if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
14754 rc = lpfc_sli4_fcf_rr_index_set(phba,
14755 fcf_pri->fcf_rec.fcf_index);
14756 if (rc)
14757 return 0;
14758 }
14759 spin_lock_irq(&phba->hbalock);
14760 }
14761 } else
14762 ret = 1;
14763 spin_unlock_irq(&phba->hbalock);
14764
14765 return ret;
14766}
14767/**
James Smart0c9ab6f2010-02-26 14:15:57 -050014768 * lpfc_sli4_fcf_rr_next_index_get - Get next eligible fcf record index
14769 * @phba: pointer to lpfc hba data structure.
14770 *
14771 * This routine is to get the next eligible FCF record index in a round
14772 * robin fashion. If the next eligible FCF record index equals to the
James Smarta93ff372010-10-22 11:06:08 -040014773 * initial roundrobin FCF record index, LPFC_FCOE_FCF_NEXT_NONE (0xFFFF)
James Smart0c9ab6f2010-02-26 14:15:57 -050014774 * shall be returned, otherwise, the next eligible FCF record's index
14775 * shall be returned.
14776 **/
14777uint16_t
14778lpfc_sli4_fcf_rr_next_index_get(struct lpfc_hba *phba)
14779{
14780 uint16_t next_fcf_index;
14781
James Smart3804dc82010-07-14 15:31:37 -040014782 /* Search start from next bit of currently registered FCF index */
James Smart7d791df2011-07-22 18:37:52 -040014783next_priority:
James Smart3804dc82010-07-14 15:31:37 -040014784 next_fcf_index = (phba->fcf.current_rec.fcf_indx + 1) %
14785 LPFC_SLI4_FCF_TBL_INDX_MAX;
James Smart0c9ab6f2010-02-26 14:15:57 -050014786 next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
14787 LPFC_SLI4_FCF_TBL_INDX_MAX,
James Smart3804dc82010-07-14 15:31:37 -040014788 next_fcf_index);
14789
James Smart0c9ab6f2010-02-26 14:15:57 -050014790 /* Wrap around condition on phba->fcf.fcf_rr_bmask */
James Smart7d791df2011-07-22 18:37:52 -040014791 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
14792 /*
14793 * If we have wrapped then we need to clear the bits that
14794 * have been tested so that we can detect when we should
14795 * change the priority level.
14796 */
James Smart0c9ab6f2010-02-26 14:15:57 -050014797 next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
14798 LPFC_SLI4_FCF_TBL_INDX_MAX, 0);
James Smart7d791df2011-07-22 18:37:52 -040014799 }
14800
James Smart0c9ab6f2010-02-26 14:15:57 -050014801
James Smart3804dc82010-07-14 15:31:37 -040014802 /* Check roundrobin failover list empty condition */
James Smart7d791df2011-07-22 18:37:52 -040014803 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX ||
14804 next_fcf_index == phba->fcf.current_rec.fcf_indx) {
14805 /*
14806 * If next fcf index is not found check if there are lower
14807 * Priority level fcf's in the fcf_priority list.
14808 * Set up the rr_bmask with all of the avaiable fcf bits
14809 * at that level and continue the selection process.
14810 */
14811 if (lpfc_check_next_fcf_pri_level(phba))
14812 goto next_priority;
James Smart3804dc82010-07-14 15:31:37 -040014813 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
14814 "2844 No roundrobin failover FCF available\n");
James Smart7d791df2011-07-22 18:37:52 -040014815 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX)
14816 return LPFC_FCOE_FCF_NEXT_NONE;
14817 else {
14818 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
14819 "3063 Only FCF available idx %d, flag %x\n",
14820 next_fcf_index,
14821 phba->fcf.fcf_pri[next_fcf_index].fcf_rec.flag);
14822 return next_fcf_index;
14823 }
James Smart3804dc82010-07-14 15:31:37 -040014824 }
14825
James Smart7d791df2011-07-22 18:37:52 -040014826 if (next_fcf_index < LPFC_SLI4_FCF_TBL_INDX_MAX &&
14827 phba->fcf.fcf_pri[next_fcf_index].fcf_rec.flag &
14828 LPFC_FCF_FLOGI_FAILED)
14829 goto next_priority;
14830
James Smart3804dc82010-07-14 15:31:37 -040014831 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040014832 "2845 Get next roundrobin failover FCF (x%x)\n",
14833 next_fcf_index);
14834
James Smart0c9ab6f2010-02-26 14:15:57 -050014835 return next_fcf_index;
14836}
14837
14838/**
14839 * lpfc_sli4_fcf_rr_index_set - Set bmask with eligible fcf record index
14840 * @phba: pointer to lpfc hba data structure.
14841 *
14842 * This routine sets the FCF record index in to the eligible bmask for
James Smarta93ff372010-10-22 11:06:08 -040014843 * roundrobin failover search. It checks to make sure that the index
James Smart0c9ab6f2010-02-26 14:15:57 -050014844 * does not go beyond the range of the driver allocated bmask dimension
14845 * before setting the bit.
14846 *
14847 * Returns 0 if the index bit successfully set, otherwise, it returns
14848 * -EINVAL.
14849 **/
14850int
14851lpfc_sli4_fcf_rr_index_set(struct lpfc_hba *phba, uint16_t fcf_index)
14852{
14853 if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
14854 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040014855 "2610 FCF (x%x) reached driver's book "
14856 "keeping dimension:x%x\n",
James Smart0c9ab6f2010-02-26 14:15:57 -050014857 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
14858 return -EINVAL;
14859 }
14860 /* Set the eligible FCF record index bmask */
14861 set_bit(fcf_index, phba->fcf.fcf_rr_bmask);
14862
James Smart3804dc82010-07-14 15:31:37 -040014863 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040014864 "2790 Set FCF (x%x) to roundrobin FCF failover "
James Smart3804dc82010-07-14 15:31:37 -040014865 "bmask\n", fcf_index);
14866
James Smart0c9ab6f2010-02-26 14:15:57 -050014867 return 0;
14868}
14869
14870/**
James Smart3804dc82010-07-14 15:31:37 -040014871 * lpfc_sli4_fcf_rr_index_clear - Clear bmask from eligible fcf record index
James Smart0c9ab6f2010-02-26 14:15:57 -050014872 * @phba: pointer to lpfc hba data structure.
14873 *
14874 * This routine clears the FCF record index from the eligible bmask for
James Smarta93ff372010-10-22 11:06:08 -040014875 * roundrobin failover search. It checks to make sure that the index
James Smart0c9ab6f2010-02-26 14:15:57 -050014876 * does not go beyond the range of the driver allocated bmask dimension
14877 * before clearing the bit.
14878 **/
14879void
14880lpfc_sli4_fcf_rr_index_clear(struct lpfc_hba *phba, uint16_t fcf_index)
14881{
James Smart7d791df2011-07-22 18:37:52 -040014882 struct lpfc_fcf_pri *fcf_pri;
James Smart0c9ab6f2010-02-26 14:15:57 -050014883 if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
14884 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040014885 "2762 FCF (x%x) reached driver's book "
14886 "keeping dimension:x%x\n",
James Smart0c9ab6f2010-02-26 14:15:57 -050014887 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
14888 return;
14889 }
14890 /* Clear the eligible FCF record index bmask */
James Smart7d791df2011-07-22 18:37:52 -040014891 spin_lock_irq(&phba->hbalock);
14892 list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
14893 if (fcf_pri->fcf_rec.fcf_index == fcf_index) {
14894 list_del_init(&fcf_pri->list);
14895 break;
14896 }
14897 }
14898 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -050014899 clear_bit(fcf_index, phba->fcf.fcf_rr_bmask);
James Smart3804dc82010-07-14 15:31:37 -040014900
14901 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040014902 "2791 Clear FCF (x%x) from roundrobin failover "
James Smart3804dc82010-07-14 15:31:37 -040014903 "bmask\n", fcf_index);
James Smart0c9ab6f2010-02-26 14:15:57 -050014904}
14905
14906/**
James Smartecfd03c2010-02-12 14:41:27 -050014907 * lpfc_mbx_cmpl_redisc_fcf_table - completion routine for rediscover FCF table
14908 * @phba: pointer to lpfc hba data structure.
14909 *
14910 * This routine is the completion routine for the rediscover FCF table mailbox
14911 * command. If the mailbox command returned failure, it will try to stop the
14912 * FCF rediscover wait timer.
14913 **/
14914void
14915lpfc_mbx_cmpl_redisc_fcf_table(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
14916{
14917 struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
14918 uint32_t shdr_status, shdr_add_status;
14919
14920 redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
14921
14922 shdr_status = bf_get(lpfc_mbox_hdr_status,
14923 &redisc_fcf->header.cfg_shdr.response);
14924 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
14925 &redisc_fcf->header.cfg_shdr.response);
14926 if (shdr_status || shdr_add_status) {
James Smart0c9ab6f2010-02-26 14:15:57 -050014927 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smartecfd03c2010-02-12 14:41:27 -050014928 "2746 Requesting for FCF rediscovery failed "
14929 "status x%x add_status x%x\n",
14930 shdr_status, shdr_add_status);
James Smart0c9ab6f2010-02-26 14:15:57 -050014931 if (phba->fcf.fcf_flag & FCF_ACVL_DISC) {
James Smartfc2b9892010-02-26 14:15:29 -050014932 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -050014933 phba->fcf.fcf_flag &= ~FCF_ACVL_DISC;
James Smartfc2b9892010-02-26 14:15:29 -050014934 spin_unlock_irq(&phba->hbalock);
14935 /*
14936 * CVL event triggered FCF rediscover request failed,
14937 * last resort to re-try current registered FCF entry.
14938 */
14939 lpfc_retry_pport_discovery(phba);
14940 } else {
14941 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -050014942 phba->fcf.fcf_flag &= ~FCF_DEAD_DISC;
James Smartfc2b9892010-02-26 14:15:29 -050014943 spin_unlock_irq(&phba->hbalock);
14944 /*
14945 * DEAD FCF event triggered FCF rediscover request
14946 * failed, last resort to fail over as a link down
14947 * to FCF registration.
14948 */
14949 lpfc_sli4_fcf_dead_failthrough(phba);
14950 }
James Smart0c9ab6f2010-02-26 14:15:57 -050014951 } else {
14952 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040014953 "2775 Start FCF rediscover quiescent timer\n");
James Smartecfd03c2010-02-12 14:41:27 -050014954 /*
14955 * Start FCF rediscovery wait timer for pending FCF
14956 * before rescan FCF record table.
14957 */
14958 lpfc_fcf_redisc_wait_start_timer(phba);
James Smart0c9ab6f2010-02-26 14:15:57 -050014959 }
James Smartecfd03c2010-02-12 14:41:27 -050014960
14961 mempool_free(mbox, phba->mbox_mem_pool);
14962}
14963
14964/**
James Smart3804dc82010-07-14 15:31:37 -040014965 * lpfc_sli4_redisc_fcf_table - Request to rediscover entire FCF table by port.
James Smartecfd03c2010-02-12 14:41:27 -050014966 * @phba: pointer to lpfc hba data structure.
14967 *
14968 * This routine is invoked to request for rediscovery of the entire FCF table
14969 * by the port.
14970 **/
14971int
14972lpfc_sli4_redisc_fcf_table(struct lpfc_hba *phba)
14973{
14974 LPFC_MBOXQ_t *mbox;
14975 struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
14976 int rc, length;
14977
James Smart0c9ab6f2010-02-26 14:15:57 -050014978 /* Cancel retry delay timers to all vports before FCF rediscover */
14979 lpfc_cancel_all_vport_retry_delay_timer(phba);
14980
James Smartecfd03c2010-02-12 14:41:27 -050014981 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14982 if (!mbox) {
14983 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14984 "2745 Failed to allocate mbox for "
14985 "requesting FCF rediscover.\n");
14986 return -ENOMEM;
14987 }
14988
14989 length = (sizeof(struct lpfc_mbx_redisc_fcf_tbl) -
14990 sizeof(struct lpfc_sli4_cfg_mhdr));
14991 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
14992 LPFC_MBOX_OPCODE_FCOE_REDISCOVER_FCF,
14993 length, LPFC_SLI4_MBX_EMBED);
14994
14995 redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
14996 /* Set count to 0 for invalidating the entire FCF database */
14997 bf_set(lpfc_mbx_redisc_fcf_count, redisc_fcf, 0);
14998
14999 /* Issue the mailbox command asynchronously */
15000 mbox->vport = phba->pport;
15001 mbox->mbox_cmpl = lpfc_mbx_cmpl_redisc_fcf_table;
15002 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
15003
15004 if (rc == MBX_NOT_FINISHED) {
15005 mempool_free(mbox, phba->mbox_mem_pool);
15006 return -EIO;
15007 }
15008 return 0;
15009}
15010
15011/**
James Smartfc2b9892010-02-26 14:15:29 -050015012 * lpfc_sli4_fcf_dead_failthrough - Failthrough routine to fcf dead event
15013 * @phba: pointer to lpfc hba data structure.
15014 *
15015 * This function is the failover routine as a last resort to the FCF DEAD
15016 * event when driver failed to perform fast FCF failover.
15017 **/
15018void
15019lpfc_sli4_fcf_dead_failthrough(struct lpfc_hba *phba)
15020{
15021 uint32_t link_state;
15022
15023 /*
15024 * Last resort as FCF DEAD event failover will treat this as
15025 * a link down, but save the link state because we don't want
15026 * it to be changed to Link Down unless it is already down.
15027 */
15028 link_state = phba->link_state;
15029 lpfc_linkdown(phba);
15030 phba->link_state = link_state;
15031
15032 /* Unregister FCF if no devices connected to it */
15033 lpfc_unregister_unused_fcf(phba);
15034}
15035
15036/**
James Smarta0c87cb2009-07-19 10:01:10 -040015037 * lpfc_sli_read_link_ste - Read region 23 to decide if link is disabled.
15038 * @phba: pointer to lpfc hba data structure.
15039 *
15040 * This function read region 23 and parse TLV for port status to
15041 * decide if the user disaled the port. If the TLV indicates the
15042 * port is disabled, the hba_flag is set accordingly.
15043 **/
15044void
15045lpfc_sli_read_link_ste(struct lpfc_hba *phba)
15046{
15047 LPFC_MBOXQ_t *pmb = NULL;
15048 MAILBOX_t *mb;
15049 uint8_t *rgn23_data = NULL;
15050 uint32_t offset = 0, data_size, sub_tlv_len, tlv_offset;
15051 int rc;
15052
15053 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15054 if (!pmb) {
15055 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15056 "2600 lpfc_sli_read_serdes_param failed to"
15057 " allocate mailbox memory\n");
15058 goto out;
15059 }
15060 mb = &pmb->u.mb;
15061
15062 /* Get adapter Region 23 data */
15063 rgn23_data = kzalloc(DMP_RGN23_SIZE, GFP_KERNEL);
15064 if (!rgn23_data)
15065 goto out;
15066
15067 do {
15068 lpfc_dump_mem(phba, pmb, offset, DMP_REGION_23);
15069 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
15070
15071 if (rc != MBX_SUCCESS) {
15072 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
15073 "2601 lpfc_sli_read_link_ste failed to"
15074 " read config region 23 rc 0x%x Status 0x%x\n",
15075 rc, mb->mbxStatus);
15076 mb->un.varDmp.word_cnt = 0;
15077 }
15078 /*
15079 * dump mem may return a zero when finished or we got a
15080 * mailbox error, either way we are done.
15081 */
15082 if (mb->un.varDmp.word_cnt == 0)
15083 break;
15084 if (mb->un.varDmp.word_cnt > DMP_RGN23_SIZE - offset)
15085 mb->un.varDmp.word_cnt = DMP_RGN23_SIZE - offset;
15086
15087 lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
15088 rgn23_data + offset,
15089 mb->un.varDmp.word_cnt);
15090 offset += mb->un.varDmp.word_cnt;
15091 } while (mb->un.varDmp.word_cnt && offset < DMP_RGN23_SIZE);
15092
15093 data_size = offset;
15094 offset = 0;
15095
15096 if (!data_size)
15097 goto out;
15098
15099 /* Check the region signature first */
15100 if (memcmp(&rgn23_data[offset], LPFC_REGION23_SIGNATURE, 4)) {
15101 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15102 "2619 Config region 23 has bad signature\n");
15103 goto out;
15104 }
15105 offset += 4;
15106
15107 /* Check the data structure version */
15108 if (rgn23_data[offset] != LPFC_REGION23_VERSION) {
15109 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15110 "2620 Config region 23 has bad version\n");
15111 goto out;
15112 }
15113 offset += 4;
15114
15115 /* Parse TLV entries in the region */
15116 while (offset < data_size) {
15117 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC)
15118 break;
15119 /*
15120 * If the TLV is not driver specific TLV or driver id is
15121 * not linux driver id, skip the record.
15122 */
15123 if ((rgn23_data[offset] != DRIVER_SPECIFIC_TYPE) ||
15124 (rgn23_data[offset + 2] != LINUX_DRIVER_ID) ||
15125 (rgn23_data[offset + 3] != 0)) {
15126 offset += rgn23_data[offset + 1] * 4 + 4;
15127 continue;
15128 }
15129
15130 /* Driver found a driver specific TLV in the config region */
15131 sub_tlv_len = rgn23_data[offset + 1] * 4;
15132 offset += 4;
15133 tlv_offset = 0;
15134
15135 /*
15136 * Search for configured port state sub-TLV.
15137 */
15138 while ((offset < data_size) &&
15139 (tlv_offset < sub_tlv_len)) {
15140 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC) {
15141 offset += 4;
15142 tlv_offset += 4;
15143 break;
15144 }
15145 if (rgn23_data[offset] != PORT_STE_TYPE) {
15146 offset += rgn23_data[offset + 1] * 4 + 4;
15147 tlv_offset += rgn23_data[offset + 1] * 4 + 4;
15148 continue;
15149 }
15150
15151 /* This HBA contains PORT_STE configured */
15152 if (!rgn23_data[offset + 2])
15153 phba->hba_flag |= LINK_DISABLED;
15154
15155 goto out;
15156 }
15157 }
15158out:
15159 if (pmb)
15160 mempool_free(pmb, phba->mbox_mem_pool);
15161 kfree(rgn23_data);
15162 return;
15163}
James Smart695a8142010-01-26 23:08:03 -050015164
15165/**
James Smart52d52442011-05-24 11:42:45 -040015166 * lpfc_wr_object - write an object to the firmware
15167 * @phba: HBA structure that indicates port to create a queue on.
15168 * @dmabuf_list: list of dmabufs to write to the port.
15169 * @size: the total byte value of the objects to write to the port.
15170 * @offset: the current offset to be used to start the transfer.
15171 *
15172 * This routine will create a wr_object mailbox command to send to the port.
15173 * the mailbox command will be constructed using the dma buffers described in
15174 * @dmabuf_list to create a list of BDEs. This routine will fill in as many
15175 * BDEs that the imbedded mailbox can support. The @offset variable will be
15176 * used to indicate the starting offset of the transfer and will also return
15177 * the offset after the write object mailbox has completed. @size is used to
15178 * determine the end of the object and whether the eof bit should be set.
15179 *
15180 * Return 0 is successful and offset will contain the the new offset to use
15181 * for the next write.
15182 * Return negative value for error cases.
15183 **/
15184int
15185lpfc_wr_object(struct lpfc_hba *phba, struct list_head *dmabuf_list,
15186 uint32_t size, uint32_t *offset)
15187{
15188 struct lpfc_mbx_wr_object *wr_object;
15189 LPFC_MBOXQ_t *mbox;
15190 int rc = 0, i = 0;
15191 uint32_t shdr_status, shdr_add_status;
15192 uint32_t mbox_tmo;
15193 union lpfc_sli4_cfg_shdr *shdr;
15194 struct lpfc_dmabuf *dmabuf;
15195 uint32_t written = 0;
15196
15197 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15198 if (!mbox)
15199 return -ENOMEM;
15200
15201 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
15202 LPFC_MBOX_OPCODE_WRITE_OBJECT,
15203 sizeof(struct lpfc_mbx_wr_object) -
15204 sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
15205
15206 wr_object = (struct lpfc_mbx_wr_object *)&mbox->u.mqe.un.wr_object;
15207 wr_object->u.request.write_offset = *offset;
15208 sprintf((uint8_t *)wr_object->u.request.object_name, "/");
15209 wr_object->u.request.object_name[0] =
15210 cpu_to_le32(wr_object->u.request.object_name[0]);
15211 bf_set(lpfc_wr_object_eof, &wr_object->u.request, 0);
15212 list_for_each_entry(dmabuf, dmabuf_list, list) {
15213 if (i >= LPFC_MBX_WR_CONFIG_MAX_BDE || written >= size)
15214 break;
15215 wr_object->u.request.bde[i].addrLow = putPaddrLow(dmabuf->phys);
15216 wr_object->u.request.bde[i].addrHigh =
15217 putPaddrHigh(dmabuf->phys);
15218 if (written + SLI4_PAGE_SIZE >= size) {
15219 wr_object->u.request.bde[i].tus.f.bdeSize =
15220 (size - written);
15221 written += (size - written);
15222 bf_set(lpfc_wr_object_eof, &wr_object->u.request, 1);
15223 } else {
15224 wr_object->u.request.bde[i].tus.f.bdeSize =
15225 SLI4_PAGE_SIZE;
15226 written += SLI4_PAGE_SIZE;
15227 }
15228 i++;
15229 }
15230 wr_object->u.request.bde_count = i;
15231 bf_set(lpfc_wr_object_write_length, &wr_object->u.request, written);
15232 if (!phba->sli4_hba.intr_enable)
15233 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15234 else {
15235 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_SLI4_CONFIG);
15236 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
15237 }
15238 /* The IOCTL status is embedded in the mailbox subheader. */
15239 shdr = (union lpfc_sli4_cfg_shdr *) &wr_object->header.cfg_shdr;
15240 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15241 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15242 if (rc != MBX_TIMEOUT)
15243 mempool_free(mbox, phba->mbox_mem_pool);
15244 if (shdr_status || shdr_add_status || rc) {
15245 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15246 "3025 Write Object mailbox failed with "
15247 "status x%x add_status x%x, mbx status x%x\n",
15248 shdr_status, shdr_add_status, rc);
15249 rc = -ENXIO;
15250 } else
15251 *offset += wr_object->u.response.actual_write_length;
15252 return rc;
15253}
15254
15255/**
James Smart695a8142010-01-26 23:08:03 -050015256 * lpfc_cleanup_pending_mbox - Free up vport discovery mailbox commands.
15257 * @vport: pointer to vport data structure.
15258 *
15259 * This function iterate through the mailboxq and clean up all REG_LOGIN
15260 * and REG_VPI mailbox commands associated with the vport. This function
15261 * is called when driver want to restart discovery of the vport due to
15262 * a Clear Virtual Link event.
15263 **/
15264void
15265lpfc_cleanup_pending_mbox(struct lpfc_vport *vport)
15266{
15267 struct lpfc_hba *phba = vport->phba;
15268 LPFC_MBOXQ_t *mb, *nextmb;
15269 struct lpfc_dmabuf *mp;
James Smart78730cf2010-04-06 15:06:30 -040015270 struct lpfc_nodelist *ndlp;
James Smartd439d282010-09-29 11:18:45 -040015271 struct lpfc_nodelist *act_mbx_ndlp = NULL;
James Smart589a52d2010-07-14 15:30:54 -040015272 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smartd439d282010-09-29 11:18:45 -040015273 LIST_HEAD(mbox_cmd_list);
James Smart63e801c2010-11-20 23:14:19 -050015274 uint8_t restart_loop;
James Smart695a8142010-01-26 23:08:03 -050015275
James Smartd439d282010-09-29 11:18:45 -040015276 /* Clean up internally queued mailbox commands with the vport */
James Smart695a8142010-01-26 23:08:03 -050015277 spin_lock_irq(&phba->hbalock);
15278 list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
15279 if (mb->vport != vport)
15280 continue;
15281
15282 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
15283 (mb->u.mb.mbxCommand != MBX_REG_VPI))
15284 continue;
15285
James Smartd439d282010-09-29 11:18:45 -040015286 list_del(&mb->list);
15287 list_add_tail(&mb->list, &mbox_cmd_list);
15288 }
15289 /* Clean up active mailbox command with the vport */
15290 mb = phba->sli.mbox_active;
15291 if (mb && (mb->vport == vport)) {
15292 if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) ||
15293 (mb->u.mb.mbxCommand == MBX_REG_VPI))
15294 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
15295 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
15296 act_mbx_ndlp = (struct lpfc_nodelist *)mb->context2;
15297 /* Put reference count for delayed processing */
15298 act_mbx_ndlp = lpfc_nlp_get(act_mbx_ndlp);
15299 /* Unregister the RPI when mailbox complete */
15300 mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
15301 }
15302 }
James Smart63e801c2010-11-20 23:14:19 -050015303 /* Cleanup any mailbox completions which are not yet processed */
15304 do {
15305 restart_loop = 0;
15306 list_for_each_entry(mb, &phba->sli.mboxq_cmpl, list) {
15307 /*
15308 * If this mailox is already processed or it is
15309 * for another vport ignore it.
15310 */
15311 if ((mb->vport != vport) ||
15312 (mb->mbox_flag & LPFC_MBX_IMED_UNREG))
15313 continue;
15314
15315 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
15316 (mb->u.mb.mbxCommand != MBX_REG_VPI))
15317 continue;
15318
15319 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
15320 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
15321 ndlp = (struct lpfc_nodelist *)mb->context2;
15322 /* Unregister the RPI when mailbox complete */
15323 mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
15324 restart_loop = 1;
15325 spin_unlock_irq(&phba->hbalock);
15326 spin_lock(shost->host_lock);
15327 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
15328 spin_unlock(shost->host_lock);
15329 spin_lock_irq(&phba->hbalock);
15330 break;
15331 }
15332 }
15333 } while (restart_loop);
15334
James Smartd439d282010-09-29 11:18:45 -040015335 spin_unlock_irq(&phba->hbalock);
15336
15337 /* Release the cleaned-up mailbox commands */
15338 while (!list_empty(&mbox_cmd_list)) {
15339 list_remove_head(&mbox_cmd_list, mb, LPFC_MBOXQ_t, list);
James Smart695a8142010-01-26 23:08:03 -050015340 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
15341 mp = (struct lpfc_dmabuf *) (mb->context1);
15342 if (mp) {
15343 __lpfc_mbuf_free(phba, mp->virt, mp->phys);
15344 kfree(mp);
15345 }
James Smart78730cf2010-04-06 15:06:30 -040015346 ndlp = (struct lpfc_nodelist *) mb->context2;
James Smartd439d282010-09-29 11:18:45 -040015347 mb->context2 = NULL;
James Smart78730cf2010-04-06 15:06:30 -040015348 if (ndlp) {
Dan Carpenterec21b3b2010-08-08 00:15:17 +020015349 spin_lock(shost->host_lock);
James Smart589a52d2010-07-14 15:30:54 -040015350 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
Dan Carpenterec21b3b2010-08-08 00:15:17 +020015351 spin_unlock(shost->host_lock);
James Smart78730cf2010-04-06 15:06:30 -040015352 lpfc_nlp_put(ndlp);
James Smart78730cf2010-04-06 15:06:30 -040015353 }
James Smart695a8142010-01-26 23:08:03 -050015354 }
James Smart695a8142010-01-26 23:08:03 -050015355 mempool_free(mb, phba->mbox_mem_pool);
15356 }
James Smartd439d282010-09-29 11:18:45 -040015357
15358 /* Release the ndlp with the cleaned-up active mailbox command */
15359 if (act_mbx_ndlp) {
15360 spin_lock(shost->host_lock);
15361 act_mbx_ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
15362 spin_unlock(shost->host_lock);
15363 lpfc_nlp_put(act_mbx_ndlp);
James Smart695a8142010-01-26 23:08:03 -050015364 }
James Smart695a8142010-01-26 23:08:03 -050015365}
15366
James Smart2a9bf3d2010-06-07 15:24:45 -040015367/**
15368 * lpfc_drain_txq - Drain the txq
15369 * @phba: Pointer to HBA context object.
15370 *
15371 * This function attempt to submit IOCBs on the txq
15372 * to the adapter. For SLI4 adapters, the txq contains
15373 * ELS IOCBs that have been deferred because the there
15374 * are no SGLs. This congestion can occur with large
15375 * vport counts during node discovery.
15376 **/
15377
15378uint32_t
15379lpfc_drain_txq(struct lpfc_hba *phba)
15380{
15381 LIST_HEAD(completions);
15382 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
15383 struct lpfc_iocbq *piocbq = 0;
15384 unsigned long iflags = 0;
15385 char *fail_msg = NULL;
15386 struct lpfc_sglq *sglq;
15387 union lpfc_wqe wqe;
15388
15389 spin_lock_irqsave(&phba->hbalock, iflags);
15390 if (pring->txq_cnt > pring->txq_max)
15391 pring->txq_max = pring->txq_cnt;
15392
15393 spin_unlock_irqrestore(&phba->hbalock, iflags);
15394
15395 while (pring->txq_cnt) {
15396 spin_lock_irqsave(&phba->hbalock, iflags);
15397
James Smart19ca7602010-11-20 23:11:55 -050015398 piocbq = lpfc_sli_ringtx_get(phba, pring);
15399 sglq = __lpfc_sli_get_sglq(phba, piocbq);
James Smart2a9bf3d2010-06-07 15:24:45 -040015400 if (!sglq) {
James Smart19ca7602010-11-20 23:11:55 -050015401 __lpfc_sli_ringtx_put(phba, pring, piocbq);
James Smart2a9bf3d2010-06-07 15:24:45 -040015402 spin_unlock_irqrestore(&phba->hbalock, iflags);
15403 break;
15404 } else {
James Smart2a9bf3d2010-06-07 15:24:45 -040015405 if (!piocbq) {
15406 /* The txq_cnt out of sync. This should
15407 * never happen
15408 */
15409 sglq = __lpfc_clear_active_sglq(phba,
James Smart6d368e52011-05-24 11:44:12 -040015410 sglq->sli4_lxritag);
James Smart2a9bf3d2010-06-07 15:24:45 -040015411 spin_unlock_irqrestore(&phba->hbalock, iflags);
15412 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15413 "2823 txq empty and txq_cnt is %d\n ",
15414 pring->txq_cnt);
15415 break;
15416 }
15417 }
15418
15419 /* The xri and iocb resources secured,
15420 * attempt to issue request
15421 */
James Smart6d368e52011-05-24 11:44:12 -040015422 piocbq->sli4_lxritag = sglq->sli4_lxritag;
James Smart2a9bf3d2010-06-07 15:24:45 -040015423 piocbq->sli4_xritag = sglq->sli4_xritag;
15424 if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocbq, sglq))
15425 fail_msg = "to convert bpl to sgl";
15426 else if (lpfc_sli4_iocb2wqe(phba, piocbq, &wqe))
15427 fail_msg = "to convert iocb to wqe";
15428 else if (lpfc_sli4_wq_put(phba->sli4_hba.els_wq, &wqe))
15429 fail_msg = " - Wq is full";
15430 else
15431 lpfc_sli_ringtxcmpl_put(phba, pring, piocbq);
15432
15433 if (fail_msg) {
15434 /* Failed means we can't issue and need to cancel */
15435 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15436 "2822 IOCB failed %s iotag 0x%x "
15437 "xri 0x%x\n",
15438 fail_msg,
15439 piocbq->iotag, piocbq->sli4_xritag);
15440 list_add_tail(&piocbq->list, &completions);
15441 }
15442 spin_unlock_irqrestore(&phba->hbalock, iflags);
15443 }
15444
James Smart2a9bf3d2010-06-07 15:24:45 -040015445 /* Cancel all the IOCBs that cannot be issued */
15446 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
15447 IOERR_SLI_ABORTED);
15448
15449 return pring->txq_cnt;
15450}