blob: c48cd0be1a59644b7cb7b03e007f213f83ae180f [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 Smarta183a152011-10-10 21:32:43 -04002203 "x%x (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,
James Smarta183a152011-10-10 21:32:43 -04002206 lpfc_sli_config_mbox_subsys_get(phba,
2207 pmb),
2208 lpfc_sli_config_mbox_opcode_get(phba,
2209 pmb));
James Smart2e0fef82007-06-17 19:56:36 -05002210 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05002211 phba->work_hs = HS_FFER3;
2212 lpfc_handle_eratt(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05002213 continue;
dea31012005-04-17 16:05:31 -05002214 }
2215
dea31012005-04-17 16:05:31 -05002216 if (pmbox->mbxStatus) {
2217 phba->sli.slistat.mbox_stat_err++;
2218 if (pmbox->mbxStatus == MBXERR_NO_RESOURCES) {
2219 /* Mbox cmd cmpl error - RETRYing */
James Smart92d7f7b2007-06-17 19:56:38 -05002220 lpfc_printf_log(phba, KERN_INFO,
James Smarta183a152011-10-10 21:32:43 -04002221 LOG_MBOX | LOG_SLI,
2222 "(%d):0305 Mbox cmd cmpl "
2223 "error - RETRYing Data: x%x "
2224 "(x%x/x%x) x%x x%x x%x\n",
2225 pmb->vport ? pmb->vport->vpi : 0,
2226 pmbox->mbxCommand,
2227 lpfc_sli_config_mbox_subsys_get(phba,
2228 pmb),
2229 lpfc_sli_config_mbox_opcode_get(phba,
2230 pmb),
2231 pmbox->mbxStatus,
2232 pmbox->un.varWords[0],
2233 pmb->vport->port_state);
dea31012005-04-17 16:05:31 -05002234 pmbox->mbxStatus = 0;
2235 pmbox->mbxOwner = OWN_HOST;
dea31012005-04-17 16:05:31 -05002236 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
James Smart04c68492009-05-22 14:52:52 -04002237 if (rc != MBX_NOT_FINISHED)
James Smart92d7f7b2007-06-17 19:56:38 -05002238 continue;
dea31012005-04-17 16:05:31 -05002239 }
2240 }
2241
2242 /* Mailbox cmd <cmd> Cmpl <cmpl> */
James Smart92d7f7b2007-06-17 19:56:38 -05002243 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04002244 "(%d):0307 Mailbox cmd x%x (x%x/x%x) Cmpl x%p "
dea31012005-04-17 16:05:31 -05002245 "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 -05002246 pmb->vport ? pmb->vport->vpi : 0,
dea31012005-04-17 16:05:31 -05002247 pmbox->mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04002248 lpfc_sli_config_mbox_subsys_get(phba, pmb),
2249 lpfc_sli_config_mbox_opcode_get(phba, pmb),
dea31012005-04-17 16:05:31 -05002250 pmb->mbox_cmpl,
2251 *((uint32_t *) pmbox),
2252 pmbox->un.varWords[0],
2253 pmbox->un.varWords[1],
2254 pmbox->un.varWords[2],
2255 pmbox->un.varWords[3],
2256 pmbox->un.varWords[4],
2257 pmbox->un.varWords[5],
2258 pmbox->un.varWords[6],
2259 pmbox->un.varWords[7]);
2260
James Smart92d7f7b2007-06-17 19:56:38 -05002261 if (pmb->mbox_cmpl)
dea31012005-04-17 16:05:31 -05002262 pmb->mbox_cmpl(phba,pmb);
James Smart92d7f7b2007-06-17 19:56:38 -05002263 } while (1);
James Smart2e0fef82007-06-17 19:56:36 -05002264 return 0;
dea31012005-04-17 16:05:31 -05002265}
James Smart92d7f7b2007-06-17 19:56:38 -05002266
James Smarte59058c2008-08-24 21:49:00 -04002267/**
James Smart3621a712009-04-06 18:47:14 -04002268 * lpfc_sli_get_buff - Get the buffer associated with the buffer tag
James Smarte59058c2008-08-24 21:49:00 -04002269 * @phba: Pointer to HBA context object.
2270 * @pring: Pointer to driver SLI ring object.
2271 * @tag: buffer tag.
2272 *
2273 * This function is called with no lock held. When QUE_BUFTAG_BIT bit
2274 * is set in the tag the buffer is posted for a particular exchange,
2275 * the function will return the buffer without replacing the buffer.
2276 * If the buffer is for unsolicited ELS or CT traffic, this function
2277 * returns the buffer and also posts another buffer to the firmware.
2278 **/
James Smart76bb24e2007-10-27 13:38:00 -04002279static struct lpfc_dmabuf *
2280lpfc_sli_get_buff(struct lpfc_hba *phba,
James Smart9f1e1b52008-12-04 22:39:40 -05002281 struct lpfc_sli_ring *pring,
2282 uint32_t tag)
James Smart76bb24e2007-10-27 13:38:00 -04002283{
James Smart9f1e1b52008-12-04 22:39:40 -05002284 struct hbq_dmabuf *hbq_entry;
2285
James Smart76bb24e2007-10-27 13:38:00 -04002286 if (tag & QUE_BUFTAG_BIT)
2287 return lpfc_sli_ring_taggedbuf_get(phba, pring, tag);
James Smart9f1e1b52008-12-04 22:39:40 -05002288 hbq_entry = lpfc_sli_hbqbuf_find(phba, tag);
2289 if (!hbq_entry)
2290 return NULL;
2291 return &hbq_entry->dbuf;
James Smart76bb24e2007-10-27 13:38:00 -04002292}
James Smart57127f12007-10-27 13:37:05 -04002293
James Smart3772a992009-05-22 14:50:54 -04002294/**
2295 * lpfc_complete_unsol_iocb - Complete an unsolicited sequence
2296 * @phba: Pointer to HBA context object.
2297 * @pring: Pointer to driver SLI ring object.
2298 * @saveq: Pointer to the iocbq struct representing the sequence starting frame.
2299 * @fch_r_ctl: the r_ctl for the first frame of the sequence.
2300 * @fch_type: the type for the first frame of the sequence.
2301 *
2302 * This function is called with no lock held. This function uses the r_ctl and
2303 * type of the received sequence to find the correct callback function to call
2304 * to process the sequence.
2305 **/
2306static int
2307lpfc_complete_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2308 struct lpfc_iocbq *saveq, uint32_t fch_r_ctl,
2309 uint32_t fch_type)
2310{
2311 int i;
2312
2313 /* unSolicited Responses */
2314 if (pring->prt[0].profile) {
2315 if (pring->prt[0].lpfc_sli_rcv_unsol_event)
2316 (pring->prt[0].lpfc_sli_rcv_unsol_event) (phba, pring,
2317 saveq);
2318 return 1;
2319 }
2320 /* We must search, based on rctl / type
2321 for the right routine */
2322 for (i = 0; i < pring->num_mask; i++) {
2323 if ((pring->prt[i].rctl == fch_r_ctl) &&
2324 (pring->prt[i].type == fch_type)) {
2325 if (pring->prt[i].lpfc_sli_rcv_unsol_event)
2326 (pring->prt[i].lpfc_sli_rcv_unsol_event)
2327 (phba, pring, saveq);
2328 return 1;
2329 }
2330 }
2331 return 0;
2332}
James Smarte59058c2008-08-24 21:49:00 -04002333
2334/**
James Smart3621a712009-04-06 18:47:14 -04002335 * lpfc_sli_process_unsol_iocb - Unsolicited iocb handler
James Smarte59058c2008-08-24 21:49:00 -04002336 * @phba: Pointer to HBA context object.
2337 * @pring: Pointer to driver SLI ring object.
2338 * @saveq: Pointer to the unsolicited iocb.
2339 *
2340 * This function is called with no lock held by the ring event handler
2341 * when there is an unsolicited iocb posted to the response ring by the
2342 * firmware. This function gets the buffer associated with the iocbs
2343 * and calls the event handler for the ring. This function handles both
2344 * qring buffers and hbq buffers.
2345 * When the function returns 1 the caller can free the iocb object otherwise
2346 * upper layer functions will free the iocb objects.
2347 **/
dea31012005-04-17 16:05:31 -05002348static int
2349lpfc_sli_process_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2350 struct lpfc_iocbq *saveq)
2351{
2352 IOCB_t * irsp;
2353 WORD5 * w5p;
2354 uint32_t Rctl, Type;
James Smart3772a992009-05-22 14:50:54 -04002355 uint32_t match;
James Smart76bb24e2007-10-27 13:38:00 -04002356 struct lpfc_iocbq *iocbq;
James Smart3163f722008-02-08 18:50:25 -05002357 struct lpfc_dmabuf *dmzbuf;
dea31012005-04-17 16:05:31 -05002358
2359 match = 0;
2360 irsp = &(saveq->iocb);
James Smart57127f12007-10-27 13:37:05 -04002361
2362 if (irsp->ulpCommand == CMD_ASYNC_STATUS) {
2363 if (pring->lpfc_sli_rcv_async_status)
2364 pring->lpfc_sli_rcv_async_status(phba, pring, saveq);
2365 else
2366 lpfc_printf_log(phba,
2367 KERN_WARNING,
2368 LOG_SLI,
2369 "0316 Ring %d handler: unexpected "
2370 "ASYNC_STATUS iocb received evt_code "
2371 "0x%x\n",
2372 pring->ringno,
2373 irsp->un.asyncstat.evt_code);
2374 return 1;
2375 }
2376
James Smart3163f722008-02-08 18:50:25 -05002377 if ((irsp->ulpCommand == CMD_IOCB_RET_XRI64_CX) &&
2378 (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)) {
2379 if (irsp->ulpBdeCount > 0) {
2380 dmzbuf = lpfc_sli_get_buff(phba, pring,
2381 irsp->un.ulpWord[3]);
2382 lpfc_in_buf_free(phba, dmzbuf);
2383 }
2384
2385 if (irsp->ulpBdeCount > 1) {
2386 dmzbuf = lpfc_sli_get_buff(phba, pring,
2387 irsp->unsli3.sli3Words[3]);
2388 lpfc_in_buf_free(phba, dmzbuf);
2389 }
2390
2391 if (irsp->ulpBdeCount > 2) {
2392 dmzbuf = lpfc_sli_get_buff(phba, pring,
2393 irsp->unsli3.sli3Words[7]);
2394 lpfc_in_buf_free(phba, dmzbuf);
2395 }
2396
2397 return 1;
2398 }
2399
James Smart92d7f7b2007-06-17 19:56:38 -05002400 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
James Smart76bb24e2007-10-27 13:38:00 -04002401 if (irsp->ulpBdeCount != 0) {
2402 saveq->context2 = lpfc_sli_get_buff(phba, pring,
James Smart92d7f7b2007-06-17 19:56:38 -05002403 irsp->un.ulpWord[3]);
James Smart76bb24e2007-10-27 13:38:00 -04002404 if (!saveq->context2)
2405 lpfc_printf_log(phba,
2406 KERN_ERR,
2407 LOG_SLI,
2408 "0341 Ring %d Cannot find buffer for "
2409 "an unsolicited iocb. tag 0x%x\n",
2410 pring->ringno,
2411 irsp->un.ulpWord[3]);
James Smart76bb24e2007-10-27 13:38:00 -04002412 }
2413 if (irsp->ulpBdeCount == 2) {
2414 saveq->context3 = lpfc_sli_get_buff(phba, pring,
James Smart51ef4c22007-08-02 11:10:31 -04002415 irsp->unsli3.sli3Words[7]);
James Smart76bb24e2007-10-27 13:38:00 -04002416 if (!saveq->context3)
2417 lpfc_printf_log(phba,
2418 KERN_ERR,
2419 LOG_SLI,
2420 "0342 Ring %d Cannot find buffer for an"
2421 " unsolicited iocb. tag 0x%x\n",
2422 pring->ringno,
2423 irsp->unsli3.sli3Words[7]);
2424 }
2425 list_for_each_entry(iocbq, &saveq->list, list) {
James Smart76bb24e2007-10-27 13:38:00 -04002426 irsp = &(iocbq->iocb);
James Smart76bb24e2007-10-27 13:38:00 -04002427 if (irsp->ulpBdeCount != 0) {
2428 iocbq->context2 = lpfc_sli_get_buff(phba, pring,
2429 irsp->un.ulpWord[3]);
James Smart9c2face2008-01-11 01:53:18 -05002430 if (!iocbq->context2)
James Smart76bb24e2007-10-27 13:38:00 -04002431 lpfc_printf_log(phba,
2432 KERN_ERR,
2433 LOG_SLI,
2434 "0343 Ring %d Cannot find "
2435 "buffer for an unsolicited iocb"
2436 ". tag 0x%x\n", pring->ringno,
2437 irsp->un.ulpWord[3]);
2438 }
2439 if (irsp->ulpBdeCount == 2) {
2440 iocbq->context3 = lpfc_sli_get_buff(phba, pring,
2441 irsp->unsli3.sli3Words[7]);
James Smart9c2face2008-01-11 01:53:18 -05002442 if (!iocbq->context3)
James Smart76bb24e2007-10-27 13:38:00 -04002443 lpfc_printf_log(phba,
2444 KERN_ERR,
2445 LOG_SLI,
2446 "0344 Ring %d Cannot find "
2447 "buffer for an unsolicited "
2448 "iocb. tag 0x%x\n",
2449 pring->ringno,
2450 irsp->unsli3.sli3Words[7]);
2451 }
2452 }
James Smart92d7f7b2007-06-17 19:56:38 -05002453 }
James Smart9c2face2008-01-11 01:53:18 -05002454 if (irsp->ulpBdeCount != 0 &&
2455 (irsp->ulpCommand == CMD_IOCB_RCV_CONT64_CX ||
2456 irsp->ulpStatus == IOSTAT_INTERMED_RSP)) {
2457 int found = 0;
2458
2459 /* search continue save q for same XRI */
2460 list_for_each_entry(iocbq, &pring->iocb_continue_saveq, clist) {
James Smart7851fe22011-07-22 18:36:52 -04002461 if (iocbq->iocb.unsli3.rcvsli3.ox_id ==
2462 saveq->iocb.unsli3.rcvsli3.ox_id) {
James Smart9c2face2008-01-11 01:53:18 -05002463 list_add_tail(&saveq->list, &iocbq->list);
2464 found = 1;
2465 break;
2466 }
2467 }
2468 if (!found)
2469 list_add_tail(&saveq->clist,
2470 &pring->iocb_continue_saveq);
2471 if (saveq->iocb.ulpStatus != IOSTAT_INTERMED_RSP) {
2472 list_del_init(&iocbq->clist);
2473 saveq = iocbq;
2474 irsp = &(saveq->iocb);
2475 } else
2476 return 0;
2477 }
2478 if ((irsp->ulpCommand == CMD_RCV_ELS_REQ64_CX) ||
2479 (irsp->ulpCommand == CMD_RCV_ELS_REQ_CX) ||
2480 (irsp->ulpCommand == CMD_IOCB_RCV_ELS64_CX)) {
James Smart6a9c52c2009-10-02 15:16:51 -04002481 Rctl = FC_RCTL_ELS_REQ;
2482 Type = FC_TYPE_ELS;
James Smart9c2face2008-01-11 01:53:18 -05002483 } else {
2484 w5p = (WORD5 *)&(saveq->iocb.un.ulpWord[5]);
2485 Rctl = w5p->hcsw.Rctl;
2486 Type = w5p->hcsw.Type;
2487
2488 /* Firmware Workaround */
2489 if ((Rctl == 0) && (pring->ringno == LPFC_ELS_RING) &&
2490 (irsp->ulpCommand == CMD_RCV_SEQUENCE64_CX ||
2491 irsp->ulpCommand == CMD_IOCB_RCV_SEQ64_CX)) {
James Smart6a9c52c2009-10-02 15:16:51 -04002492 Rctl = FC_RCTL_ELS_REQ;
2493 Type = FC_TYPE_ELS;
James Smart9c2face2008-01-11 01:53:18 -05002494 w5p->hcsw.Rctl = Rctl;
2495 w5p->hcsw.Type = Type;
2496 }
2497 }
James Smart92d7f7b2007-06-17 19:56:38 -05002498
James Smart3772a992009-05-22 14:50:54 -04002499 if (!lpfc_complete_unsol_iocb(phba, pring, saveq, Rctl, Type))
James Smart92d7f7b2007-06-17 19:56:38 -05002500 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002501 "0313 Ring %d handler: unexpected Rctl x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05002502 "Type x%x received\n",
James Smarte8b62012007-08-02 11:10:09 -04002503 pring->ringno, Rctl, Type);
James Smart3772a992009-05-22 14:50:54 -04002504
James Smart92d7f7b2007-06-17 19:56:38 -05002505 return 1;
dea31012005-04-17 16:05:31 -05002506}
2507
James Smarte59058c2008-08-24 21:49:00 -04002508/**
James Smart3621a712009-04-06 18:47:14 -04002509 * lpfc_sli_iocbq_lookup - Find command iocb for the given response iocb
James Smarte59058c2008-08-24 21:49:00 -04002510 * @phba: Pointer to HBA context object.
2511 * @pring: Pointer to driver SLI ring object.
2512 * @prspiocb: Pointer to response iocb object.
2513 *
2514 * This function looks up the iocb_lookup table to get the command iocb
2515 * corresponding to the given response iocb using the iotag of the
2516 * response iocb. This function is called with the hbalock held.
2517 * This function returns the command iocb object if it finds the command
2518 * iocb else returns NULL.
2519 **/
dea31012005-04-17 16:05:31 -05002520static struct lpfc_iocbq *
James Smart2e0fef82007-06-17 19:56:36 -05002521lpfc_sli_iocbq_lookup(struct lpfc_hba *phba,
2522 struct lpfc_sli_ring *pring,
2523 struct lpfc_iocbq *prspiocb)
dea31012005-04-17 16:05:31 -05002524{
dea31012005-04-17 16:05:31 -05002525 struct lpfc_iocbq *cmd_iocb = NULL;
2526 uint16_t iotag;
2527
James Bottomley604a3e32005-10-29 10:28:33 -05002528 iotag = prspiocb->iocb.ulpIoTag;
dea31012005-04-17 16:05:31 -05002529
James Bottomley604a3e32005-10-29 10:28:33 -05002530 if (iotag != 0 && iotag <= phba->sli.last_iotag) {
2531 cmd_iocb = phba->sli.iocbq_lookup[iotag];
James Smart92d7f7b2007-06-17 19:56:38 -05002532 list_del_init(&cmd_iocb->list);
James Smart2a9bf3d2010-06-07 15:24:45 -04002533 if (cmd_iocb->iocb_flag & LPFC_IO_ON_Q) {
2534 pring->txcmplq_cnt--;
2535 cmd_iocb->iocb_flag &= ~LPFC_IO_ON_Q;
2536 }
James Bottomley604a3e32005-10-29 10:28:33 -05002537 return cmd_iocb;
dea31012005-04-17 16:05:31 -05002538 }
2539
dea31012005-04-17 16:05:31 -05002540 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002541 "0317 iotag x%x is out off "
James Bottomley604a3e32005-10-29 10:28:33 -05002542 "range: max iotag x%x wd0 x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04002543 iotag, phba->sli.last_iotag,
James Bottomley604a3e32005-10-29 10:28:33 -05002544 *(((uint32_t *) &prspiocb->iocb) + 7));
dea31012005-04-17 16:05:31 -05002545 return NULL;
2546}
2547
James Smarte59058c2008-08-24 21:49:00 -04002548/**
James Smart3772a992009-05-22 14:50:54 -04002549 * lpfc_sli_iocbq_lookup_by_tag - Find command iocb for the iotag
2550 * @phba: Pointer to HBA context object.
2551 * @pring: Pointer to driver SLI ring object.
2552 * @iotag: IOCB tag.
2553 *
2554 * This function looks up the iocb_lookup table to get the command iocb
2555 * corresponding to the given iotag. This function is called with the
2556 * hbalock held.
2557 * This function returns the command iocb object if it finds the command
2558 * iocb else returns NULL.
2559 **/
2560static struct lpfc_iocbq *
2561lpfc_sli_iocbq_lookup_by_tag(struct lpfc_hba *phba,
2562 struct lpfc_sli_ring *pring, uint16_t iotag)
2563{
2564 struct lpfc_iocbq *cmd_iocb;
2565
2566 if (iotag != 0 && iotag <= phba->sli.last_iotag) {
2567 cmd_iocb = phba->sli.iocbq_lookup[iotag];
2568 list_del_init(&cmd_iocb->list);
James Smart2a9bf3d2010-06-07 15:24:45 -04002569 if (cmd_iocb->iocb_flag & LPFC_IO_ON_Q) {
2570 cmd_iocb->iocb_flag &= ~LPFC_IO_ON_Q;
2571 pring->txcmplq_cnt--;
2572 }
James Smart3772a992009-05-22 14:50:54 -04002573 return cmd_iocb;
2574 }
2575
2576 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2577 "0372 iotag x%x is out off range: max iotag (x%x)\n",
2578 iotag, phba->sli.last_iotag);
2579 return NULL;
2580}
2581
2582/**
James Smart3621a712009-04-06 18:47:14 -04002583 * lpfc_sli_process_sol_iocb - process solicited iocb completion
James Smarte59058c2008-08-24 21:49:00 -04002584 * @phba: Pointer to HBA context object.
2585 * @pring: Pointer to driver SLI ring object.
2586 * @saveq: Pointer to the response iocb to be processed.
2587 *
2588 * This function is called by the ring event handler for non-fcp
2589 * rings when there is a new response iocb in the response ring.
2590 * The caller is not required to hold any locks. This function
2591 * gets the command iocb associated with the response iocb and
2592 * calls the completion handler for the command iocb. If there
2593 * is no completion handler, the function will free the resources
2594 * associated with command iocb. If the response iocb is for
2595 * an already aborted command iocb, the status of the completion
2596 * is changed to IOSTAT_LOCAL_REJECT/IOERR_SLI_ABORTED.
2597 * This function always returns 1.
2598 **/
dea31012005-04-17 16:05:31 -05002599static int
James Smart2e0fef82007-06-17 19:56:36 -05002600lpfc_sli_process_sol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
dea31012005-04-17 16:05:31 -05002601 struct lpfc_iocbq *saveq)
2602{
James Smart2e0fef82007-06-17 19:56:36 -05002603 struct lpfc_iocbq *cmdiocbp;
dea31012005-04-17 16:05:31 -05002604 int rc = 1;
2605 unsigned long iflag;
2606
2607 /* Based on the iotag field, get the cmd IOCB from the txcmplq */
James Smart2e0fef82007-06-17 19:56:36 -05002608 spin_lock_irqsave(&phba->hbalock, iflag);
James Bottomley604a3e32005-10-29 10:28:33 -05002609 cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring, saveq);
James Smart2e0fef82007-06-17 19:56:36 -05002610 spin_unlock_irqrestore(&phba->hbalock, iflag);
2611
dea31012005-04-17 16:05:31 -05002612 if (cmdiocbp) {
2613 if (cmdiocbp->iocb_cmpl) {
2614 /*
James Smartea2151b2008-09-07 11:52:10 -04002615 * If an ELS command failed send an event to mgmt
2616 * application.
2617 */
2618 if (saveq->iocb.ulpStatus &&
2619 (pring->ringno == LPFC_ELS_RING) &&
2620 (cmdiocbp->iocb.ulpCommand ==
2621 CMD_ELS_REQUEST64_CR))
2622 lpfc_send_els_failure_event(phba,
2623 cmdiocbp, saveq);
2624
2625 /*
dea31012005-04-17 16:05:31 -05002626 * Post all ELS completions to the worker thread.
2627 * All other are passed to the completion callback.
2628 */
2629 if (pring->ringno == LPFC_ELS_RING) {
James Smart341af102010-01-26 23:07:37 -05002630 if ((phba->sli_rev < LPFC_SLI_REV4) &&
2631 (cmdiocbp->iocb_flag &
2632 LPFC_DRIVER_ABORTED)) {
2633 spin_lock_irqsave(&phba->hbalock,
2634 iflag);
James Smart07951072007-04-25 09:51:38 -04002635 cmdiocbp->iocb_flag &=
2636 ~LPFC_DRIVER_ABORTED;
James Smart341af102010-01-26 23:07:37 -05002637 spin_unlock_irqrestore(&phba->hbalock,
2638 iflag);
James Smart07951072007-04-25 09:51:38 -04002639 saveq->iocb.ulpStatus =
2640 IOSTAT_LOCAL_REJECT;
2641 saveq->iocb.un.ulpWord[4] =
2642 IOERR_SLI_ABORTED;
James Smart0ff10d42008-01-11 01:52:36 -05002643
2644 /* Firmware could still be in progress
2645 * of DMAing payload, so don't free data
2646 * buffer till after a hbeat.
2647 */
James Smart341af102010-01-26 23:07:37 -05002648 spin_lock_irqsave(&phba->hbalock,
2649 iflag);
James Smart0ff10d42008-01-11 01:52:36 -05002650 saveq->iocb_flag |= LPFC_DELAY_MEM_FREE;
James Smart341af102010-01-26 23:07:37 -05002651 spin_unlock_irqrestore(&phba->hbalock,
2652 iflag);
2653 }
James Smart0f65ff62010-02-26 14:14:23 -05002654 if (phba->sli_rev == LPFC_SLI_REV4) {
2655 if (saveq->iocb_flag &
2656 LPFC_EXCHANGE_BUSY) {
2657 /* Set cmdiocb flag for the
2658 * exchange busy so sgl (xri)
2659 * will not be released until
2660 * the abort xri is received
2661 * from hba.
2662 */
2663 spin_lock_irqsave(
2664 &phba->hbalock, iflag);
2665 cmdiocbp->iocb_flag |=
2666 LPFC_EXCHANGE_BUSY;
2667 spin_unlock_irqrestore(
2668 &phba->hbalock, iflag);
2669 }
2670 if (cmdiocbp->iocb_flag &
2671 LPFC_DRIVER_ABORTED) {
2672 /*
2673 * Clear LPFC_DRIVER_ABORTED
2674 * bit in case it was driver
2675 * initiated abort.
2676 */
2677 spin_lock_irqsave(
2678 &phba->hbalock, iflag);
2679 cmdiocbp->iocb_flag &=
2680 ~LPFC_DRIVER_ABORTED;
2681 spin_unlock_irqrestore(
2682 &phba->hbalock, iflag);
2683 cmdiocbp->iocb.ulpStatus =
2684 IOSTAT_LOCAL_REJECT;
2685 cmdiocbp->iocb.un.ulpWord[4] =
2686 IOERR_ABORT_REQUESTED;
2687 /*
2688 * For SLI4, irsiocb contains
2689 * NO_XRI in sli_xritag, it
2690 * shall not affect releasing
2691 * sgl (xri) process.
2692 */
2693 saveq->iocb.ulpStatus =
2694 IOSTAT_LOCAL_REJECT;
2695 saveq->iocb.un.ulpWord[4] =
2696 IOERR_SLI_ABORTED;
2697 spin_lock_irqsave(
2698 &phba->hbalock, iflag);
2699 saveq->iocb_flag |=
2700 LPFC_DELAY_MEM_FREE;
2701 spin_unlock_irqrestore(
2702 &phba->hbalock, iflag);
2703 }
James Smart07951072007-04-25 09:51:38 -04002704 }
dea31012005-04-17 16:05:31 -05002705 }
James Smart2e0fef82007-06-17 19:56:36 -05002706 (cmdiocbp->iocb_cmpl) (phba, cmdiocbp, saveq);
James Bottomley604a3e32005-10-29 10:28:33 -05002707 } else
2708 lpfc_sli_release_iocbq(phba, cmdiocbp);
dea31012005-04-17 16:05:31 -05002709 } else {
2710 /*
2711 * Unknown initiating command based on the response iotag.
2712 * This could be the case on the ELS ring because of
2713 * lpfc_els_abort().
2714 */
2715 if (pring->ringno != LPFC_ELS_RING) {
2716 /*
2717 * Ring <ringno> handler: unexpected completion IoTag
2718 * <IoTag>
2719 */
James Smarta257bf92009-04-06 18:48:10 -04002720 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002721 "0322 Ring %d handler: "
2722 "unexpected completion IoTag x%x "
2723 "Data: x%x x%x x%x x%x\n",
2724 pring->ringno,
2725 saveq->iocb.ulpIoTag,
2726 saveq->iocb.ulpStatus,
2727 saveq->iocb.un.ulpWord[4],
2728 saveq->iocb.ulpCommand,
2729 saveq->iocb.ulpContext);
dea31012005-04-17 16:05:31 -05002730 }
2731 }
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04002732
dea31012005-04-17 16:05:31 -05002733 return rc;
2734}
2735
James Smarte59058c2008-08-24 21:49:00 -04002736/**
James Smart3621a712009-04-06 18:47:14 -04002737 * lpfc_sli_rsp_pointers_error - Response ring pointer error handler
James Smarte59058c2008-08-24 21:49:00 -04002738 * @phba: Pointer to HBA context object.
2739 * @pring: Pointer to driver SLI ring object.
2740 *
2741 * This function is called from the iocb ring event handlers when
2742 * put pointer is ahead of the get pointer for a ring. This function signal
2743 * an error attention condition to the worker thread and the worker
2744 * thread will transition the HBA to offline state.
2745 **/
James Smart2e0fef82007-06-17 19:56:36 -05002746static void
2747lpfc_sli_rsp_pointers_error(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002748{
James Smart34b02dc2008-08-24 21:49:55 -04002749 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002750 /*
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02002751 * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002752 * rsp ring <portRspMax>
2753 */
2754 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002755 "0312 Ring %d handler: portRspPut %d "
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02002756 "is bigger than rsp ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04002757 pring->ringno, le32_to_cpu(pgp->rspPutInx),
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002758 pring->numRiocb);
2759
James Smart2e0fef82007-06-17 19:56:36 -05002760 phba->link_state = LPFC_HBA_ERROR;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002761
2762 /*
2763 * All error attention handlers are posted to
2764 * worker thread
2765 */
2766 phba->work_ha |= HA_ERATT;
2767 phba->work_hs = HS_FFER3;
James Smart92d7f7b2007-06-17 19:56:38 -05002768
James Smart5e9d9b82008-06-14 22:52:53 -04002769 lpfc_worker_wake_up(phba);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002770
2771 return;
2772}
2773
James Smarte59058c2008-08-24 21:49:00 -04002774/**
James Smart3621a712009-04-06 18:47:14 -04002775 * lpfc_poll_eratt - Error attention polling timer timeout handler
James Smart93996272008-08-24 21:50:30 -04002776 * @ptr: Pointer to address of HBA context object.
2777 *
2778 * This function is invoked by the Error Attention polling timer when the
2779 * timer times out. It will check the SLI Error Attention register for
2780 * possible attention events. If so, it will post an Error Attention event
2781 * and wake up worker thread to process it. Otherwise, it will set up the
2782 * Error Attention polling timer for the next poll.
2783 **/
2784void lpfc_poll_eratt(unsigned long ptr)
2785{
2786 struct lpfc_hba *phba;
2787 uint32_t eratt = 0;
2788
2789 phba = (struct lpfc_hba *)ptr;
2790
2791 /* Check chip HA register for error event */
2792 eratt = lpfc_sli_check_eratt(phba);
2793
2794 if (eratt)
2795 /* Tell the worker thread there is work to do */
2796 lpfc_worker_wake_up(phba);
2797 else
2798 /* Restart the timer for next eratt poll */
2799 mod_timer(&phba->eratt_poll, jiffies +
2800 HZ * LPFC_ERATT_POLL_INTERVAL);
2801 return;
2802}
2803
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002804
James Smarte59058c2008-08-24 21:49:00 -04002805/**
James Smart3621a712009-04-06 18:47:14 -04002806 * lpfc_sli_handle_fast_ring_event - Handle ring events on FCP ring
James Smarte59058c2008-08-24 21:49:00 -04002807 * @phba: Pointer to HBA context object.
2808 * @pring: Pointer to driver SLI ring object.
2809 * @mask: Host attention register mask for this ring.
2810 *
2811 * This function is called from the interrupt context when there is a ring
2812 * event for the fcp ring. The caller does not hold any lock.
2813 * The function processes each response iocb in the response ring until it
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002814 * finds an iocb with LE bit set and chains all the iocbs up to the iocb with
James Smarte59058c2008-08-24 21:49:00 -04002815 * LE bit set. The function will call the completion handler of the command iocb
2816 * if the response iocb indicates a completion for a command iocb or it is
2817 * an abort completion. The function will call lpfc_sli_process_unsol_iocb
2818 * function if this is an unsolicited iocb.
dea31012005-04-17 16:05:31 -05002819 * This routine presumes LPFC_FCP_RING handling and doesn't bother
James Smart45ed1192009-10-02 15:17:02 -04002820 * to check it explicitly.
2821 */
2822int
James Smart2e0fef82007-06-17 19:56:36 -05002823lpfc_sli_handle_fast_ring_event(struct lpfc_hba *phba,
2824 struct lpfc_sli_ring *pring, uint32_t mask)
dea31012005-04-17 16:05:31 -05002825{
James Smart34b02dc2008-08-24 21:49:55 -04002826 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
dea31012005-04-17 16:05:31 -05002827 IOCB_t *irsp = NULL;
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04002828 IOCB_t *entry = NULL;
dea31012005-04-17 16:05:31 -05002829 struct lpfc_iocbq *cmdiocbq = NULL;
2830 struct lpfc_iocbq rspiocbq;
dea31012005-04-17 16:05:31 -05002831 uint32_t status;
2832 uint32_t portRspPut, portRspMax;
2833 int rc = 1;
2834 lpfc_iocb_type type;
2835 unsigned long iflag;
2836 uint32_t rsp_cmpl = 0;
dea31012005-04-17 16:05:31 -05002837
James Smart2e0fef82007-06-17 19:56:36 -05002838 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05002839 pring->stats.iocb_event++;
2840
dea31012005-04-17 16:05:31 -05002841 /*
2842 * The next available response entry should never exceed the maximum
2843 * entries. If it does, treat it as an adapter hardware error.
2844 */
2845 portRspMax = pring->numRiocb;
2846 portRspPut = le32_to_cpu(pgp->rspPutInx);
2847 if (unlikely(portRspPut >= portRspMax)) {
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002848 lpfc_sli_rsp_pointers_error(phba, pring);
James Smart2e0fef82007-06-17 19:56:36 -05002849 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05002850 return 1;
2851 }
James Smart45ed1192009-10-02 15:17:02 -04002852 if (phba->fcp_ring_in_use) {
2853 spin_unlock_irqrestore(&phba->hbalock, iflag);
2854 return 1;
2855 } else
2856 phba->fcp_ring_in_use = 1;
dea31012005-04-17 16:05:31 -05002857
2858 rmb();
2859 while (pring->rspidx != portRspPut) {
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04002860 /*
2861 * Fetch an entry off the ring and copy it into a local data
2862 * structure. The copy involves a byte-swap since the
2863 * network byte order and pci byte orders are different.
2864 */
James Smarted957682007-06-17 19:56:37 -05002865 entry = lpfc_resp_iocb(phba, pring);
James Smart858c9f62007-06-17 19:56:39 -05002866 phba->last_completion_time = jiffies;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002867
2868 if (++pring->rspidx >= portRspMax)
2869 pring->rspidx = 0;
2870
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04002871 lpfc_sli_pcimem_bcopy((uint32_t *) entry,
2872 (uint32_t *) &rspiocbq.iocb,
James Smarted957682007-06-17 19:56:37 -05002873 phba->iocb_rsp_size);
James Smarta4bc3372006-12-02 13:34:16 -05002874 INIT_LIST_HEAD(&(rspiocbq.list));
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04002875 irsp = &rspiocbq.iocb;
2876
dea31012005-04-17 16:05:31 -05002877 type = lpfc_sli_iocb_cmd_type(irsp->ulpCommand & CMD_IOCB_MASK);
2878 pring->stats.iocb_rsp++;
2879 rsp_cmpl++;
2880
2881 if (unlikely(irsp->ulpStatus)) {
James Smart92d7f7b2007-06-17 19:56:38 -05002882 /*
2883 * If resource errors reported from HBA, reduce
2884 * queuedepths of the SCSI device.
2885 */
2886 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
2887 (irsp->un.ulpWord[4] == IOERR_NO_RESOURCES)) {
2888 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart3772a992009-05-22 14:50:54 -04002889 phba->lpfc_rampdown_queue_depth(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05002890 spin_lock_irqsave(&phba->hbalock, iflag);
2891 }
2892
dea31012005-04-17 16:05:31 -05002893 /* Rsp ring <ringno> error: IOCB */
2894 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002895 "0336 Rsp Ring %d error: IOCB Data: "
James Smart92d7f7b2007-06-17 19:56:38 -05002896 "x%x x%x x%x x%x x%x x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04002897 pring->ringno,
James Smart92d7f7b2007-06-17 19:56:38 -05002898 irsp->un.ulpWord[0],
2899 irsp->un.ulpWord[1],
2900 irsp->un.ulpWord[2],
2901 irsp->un.ulpWord[3],
2902 irsp->un.ulpWord[4],
2903 irsp->un.ulpWord[5],
James Smartd7c255b2008-08-24 21:50:00 -04002904 *(uint32_t *)&irsp->un1,
2905 *((uint32_t *)&irsp->un1 + 1));
dea31012005-04-17 16:05:31 -05002906 }
2907
2908 switch (type) {
2909 case LPFC_ABORT_IOCB:
2910 case LPFC_SOL_IOCB:
2911 /*
2912 * Idle exchange closed via ABTS from port. No iocb
2913 * resources need to be recovered.
2914 */
2915 if (unlikely(irsp->ulpCommand == CMD_XRI_ABORTED_CX)) {
James Smartdca94792006-08-01 07:34:08 -04002916 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002917 "0333 IOCB cmd 0x%x"
James Smartdca94792006-08-01 07:34:08 -04002918 " processed. Skipping"
James Smart92d7f7b2007-06-17 19:56:38 -05002919 " completion\n",
James Smartdca94792006-08-01 07:34:08 -04002920 irsp->ulpCommand);
dea31012005-04-17 16:05:31 -05002921 break;
2922 }
2923
James Bottomley604a3e32005-10-29 10:28:33 -05002924 cmdiocbq = lpfc_sli_iocbq_lookup(phba, pring,
2925 &rspiocbq);
James Smart0f65ff62010-02-26 14:14:23 -05002926 if (unlikely(!cmdiocbq))
2927 break;
2928 if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED)
2929 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
2930 if (cmdiocbq->iocb_cmpl) {
2931 spin_unlock_irqrestore(&phba->hbalock, iflag);
2932 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
2933 &rspiocbq);
2934 spin_lock_irqsave(&phba->hbalock, iflag);
2935 }
dea31012005-04-17 16:05:31 -05002936 break;
James Smarta4bc3372006-12-02 13:34:16 -05002937 case LPFC_UNSOL_IOCB:
James Smart2e0fef82007-06-17 19:56:36 -05002938 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta4bc3372006-12-02 13:34:16 -05002939 lpfc_sli_process_unsol_iocb(phba, pring, &rspiocbq);
James Smart2e0fef82007-06-17 19:56:36 -05002940 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta4bc3372006-12-02 13:34:16 -05002941 break;
dea31012005-04-17 16:05:31 -05002942 default:
2943 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
2944 char adaptermsg[LPFC_MAX_ADPTMSG];
2945 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
2946 memcpy(&adaptermsg[0], (uint8_t *) irsp,
2947 MAX_MSG_DATA);
Joe Perches898eb712007-10-18 03:06:30 -07002948 dev_warn(&((phba->pcidev)->dev),
2949 "lpfc%d: %s\n",
dea31012005-04-17 16:05:31 -05002950 phba->brd_no, adaptermsg);
2951 } else {
2952 /* Unknown IOCB command */
2953 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002954 "0334 Unknown IOCB command "
James Smart92d7f7b2007-06-17 19:56:38 -05002955 "Data: x%x, x%x x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04002956 type, irsp->ulpCommand,
James Smart92d7f7b2007-06-17 19:56:38 -05002957 irsp->ulpStatus,
2958 irsp->ulpIoTag,
2959 irsp->ulpContext);
dea31012005-04-17 16:05:31 -05002960 }
2961 break;
2962 }
2963
2964 /*
2965 * The response IOCB has been processed. Update the ring
2966 * pointer in SLIM. If the port response put pointer has not
2967 * been updated, sync the pgp->rspPutInx and fetch the new port
2968 * response put pointer.
2969 */
James Smarted957682007-06-17 19:56:37 -05002970 writel(pring->rspidx, &phba->host_gp[pring->ringno].rspGetInx);
dea31012005-04-17 16:05:31 -05002971
2972 if (pring->rspidx == portRspPut)
2973 portRspPut = le32_to_cpu(pgp->rspPutInx);
2974 }
2975
2976 if ((rsp_cmpl > 0) && (mask & HA_R0RE_REQ)) {
2977 pring->stats.iocb_rsp_full++;
2978 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
2979 writel(status, phba->CAregaddr);
2980 readl(phba->CAregaddr);
2981 }
2982 if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
2983 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
2984 pring->stats.iocb_cmd_empty++;
2985
2986 /* Force update of the local copy of cmdGetInx */
2987 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
2988 lpfc_sli_resume_iocb(phba, pring);
2989
2990 if ((pring->lpfc_sli_cmd_available))
2991 (pring->lpfc_sli_cmd_available) (phba, pring);
2992
2993 }
2994
James Smart45ed1192009-10-02 15:17:02 -04002995 phba->fcp_ring_in_use = 0;
James Smart2e0fef82007-06-17 19:56:36 -05002996 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05002997 return rc;
2998}
2999
James Smarte59058c2008-08-24 21:49:00 -04003000/**
James Smart3772a992009-05-22 14:50:54 -04003001 * lpfc_sli_sp_handle_rspiocb - Handle slow-path response iocb
3002 * @phba: Pointer to HBA context object.
3003 * @pring: Pointer to driver SLI ring object.
3004 * @rspiocbp: Pointer to driver response IOCB object.
3005 *
3006 * This function is called from the worker thread when there is a slow-path
3007 * response IOCB to process. This function chains all the response iocbs until
3008 * seeing the iocb with the LE bit set. The function will call
3009 * lpfc_sli_process_sol_iocb function if the response iocb indicates a
3010 * completion of a command iocb. The function will call the
3011 * lpfc_sli_process_unsol_iocb function if this is an unsolicited iocb.
3012 * The function frees the resources or calls the completion handler if this
3013 * iocb is an abort completion. The function returns NULL when the response
3014 * iocb has the LE bit set and all the chained iocbs are processed, otherwise
3015 * this function shall chain the iocb on to the iocb_continueq and return the
3016 * response iocb passed in.
3017 **/
3018static struct lpfc_iocbq *
3019lpfc_sli_sp_handle_rspiocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
3020 struct lpfc_iocbq *rspiocbp)
3021{
3022 struct lpfc_iocbq *saveq;
3023 struct lpfc_iocbq *cmdiocbp;
3024 struct lpfc_iocbq *next_iocb;
3025 IOCB_t *irsp = NULL;
3026 uint32_t free_saveq;
3027 uint8_t iocb_cmd_type;
3028 lpfc_iocb_type type;
3029 unsigned long iflag;
3030 int rc;
3031
3032 spin_lock_irqsave(&phba->hbalock, iflag);
3033 /* First add the response iocb to the countinueq list */
3034 list_add_tail(&rspiocbp->list, &(pring->iocb_continueq));
3035 pring->iocb_continueq_cnt++;
3036
Justin P. Mattock70f23fd2011-05-10 10:16:21 +02003037 /* Now, determine whether the list is completed for processing */
James Smart3772a992009-05-22 14:50:54 -04003038 irsp = &rspiocbp->iocb;
3039 if (irsp->ulpLe) {
3040 /*
3041 * By default, the driver expects to free all resources
3042 * associated with this iocb completion.
3043 */
3044 free_saveq = 1;
3045 saveq = list_get_first(&pring->iocb_continueq,
3046 struct lpfc_iocbq, list);
3047 irsp = &(saveq->iocb);
3048 list_del_init(&pring->iocb_continueq);
3049 pring->iocb_continueq_cnt = 0;
3050
3051 pring->stats.iocb_rsp++;
3052
3053 /*
3054 * If resource errors reported from HBA, reduce
3055 * queuedepths of the SCSI device.
3056 */
3057 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
3058 (irsp->un.ulpWord[4] == IOERR_NO_RESOURCES)) {
3059 spin_unlock_irqrestore(&phba->hbalock, iflag);
3060 phba->lpfc_rampdown_queue_depth(phba);
3061 spin_lock_irqsave(&phba->hbalock, iflag);
3062 }
3063
3064 if (irsp->ulpStatus) {
3065 /* Rsp ring <ringno> error: IOCB */
3066 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
3067 "0328 Rsp Ring %d error: "
3068 "IOCB Data: "
3069 "x%x x%x x%x x%x "
3070 "x%x x%x x%x x%x "
3071 "x%x x%x x%x x%x "
3072 "x%x x%x x%x x%x\n",
3073 pring->ringno,
3074 irsp->un.ulpWord[0],
3075 irsp->un.ulpWord[1],
3076 irsp->un.ulpWord[2],
3077 irsp->un.ulpWord[3],
3078 irsp->un.ulpWord[4],
3079 irsp->un.ulpWord[5],
3080 *(((uint32_t *) irsp) + 6),
3081 *(((uint32_t *) irsp) + 7),
3082 *(((uint32_t *) irsp) + 8),
3083 *(((uint32_t *) irsp) + 9),
3084 *(((uint32_t *) irsp) + 10),
3085 *(((uint32_t *) irsp) + 11),
3086 *(((uint32_t *) irsp) + 12),
3087 *(((uint32_t *) irsp) + 13),
3088 *(((uint32_t *) irsp) + 14),
3089 *(((uint32_t *) irsp) + 15));
3090 }
3091
3092 /*
3093 * Fetch the IOCB command type and call the correct completion
3094 * routine. Solicited and Unsolicited IOCBs on the ELS ring
3095 * get freed back to the lpfc_iocb_list by the discovery
3096 * kernel thread.
3097 */
3098 iocb_cmd_type = irsp->ulpCommand & CMD_IOCB_MASK;
3099 type = lpfc_sli_iocb_cmd_type(iocb_cmd_type);
3100 switch (type) {
3101 case LPFC_SOL_IOCB:
3102 spin_unlock_irqrestore(&phba->hbalock, iflag);
3103 rc = lpfc_sli_process_sol_iocb(phba, pring, saveq);
3104 spin_lock_irqsave(&phba->hbalock, iflag);
3105 break;
3106
3107 case LPFC_UNSOL_IOCB:
3108 spin_unlock_irqrestore(&phba->hbalock, iflag);
3109 rc = lpfc_sli_process_unsol_iocb(phba, pring, saveq);
3110 spin_lock_irqsave(&phba->hbalock, iflag);
3111 if (!rc)
3112 free_saveq = 0;
3113 break;
3114
3115 case LPFC_ABORT_IOCB:
3116 cmdiocbp = NULL;
3117 if (irsp->ulpCommand != CMD_XRI_ABORTED_CX)
3118 cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring,
3119 saveq);
3120 if (cmdiocbp) {
3121 /* Call the specified completion routine */
3122 if (cmdiocbp->iocb_cmpl) {
3123 spin_unlock_irqrestore(&phba->hbalock,
3124 iflag);
3125 (cmdiocbp->iocb_cmpl)(phba, cmdiocbp,
3126 saveq);
3127 spin_lock_irqsave(&phba->hbalock,
3128 iflag);
3129 } else
3130 __lpfc_sli_release_iocbq(phba,
3131 cmdiocbp);
3132 }
3133 break;
3134
3135 case LPFC_UNKNOWN_IOCB:
3136 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
3137 char adaptermsg[LPFC_MAX_ADPTMSG];
3138 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
3139 memcpy(&adaptermsg[0], (uint8_t *)irsp,
3140 MAX_MSG_DATA);
3141 dev_warn(&((phba->pcidev)->dev),
3142 "lpfc%d: %s\n",
3143 phba->brd_no, adaptermsg);
3144 } else {
3145 /* Unknown IOCB command */
3146 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3147 "0335 Unknown IOCB "
3148 "command Data: x%x "
3149 "x%x x%x x%x\n",
3150 irsp->ulpCommand,
3151 irsp->ulpStatus,
3152 irsp->ulpIoTag,
3153 irsp->ulpContext);
3154 }
3155 break;
3156 }
3157
3158 if (free_saveq) {
3159 list_for_each_entry_safe(rspiocbp, next_iocb,
3160 &saveq->list, list) {
3161 list_del(&rspiocbp->list);
3162 __lpfc_sli_release_iocbq(phba, rspiocbp);
3163 }
3164 __lpfc_sli_release_iocbq(phba, saveq);
3165 }
3166 rspiocbp = NULL;
3167 }
3168 spin_unlock_irqrestore(&phba->hbalock, iflag);
3169 return rspiocbp;
3170}
3171
3172/**
3173 * lpfc_sli_handle_slow_ring_event - Wrapper func for handling slow-path iocbs
James Smarte59058c2008-08-24 21:49:00 -04003174 * @phba: Pointer to HBA context object.
3175 * @pring: Pointer to driver SLI ring object.
3176 * @mask: Host attention register mask for this ring.
3177 *
James Smart3772a992009-05-22 14:50:54 -04003178 * This routine wraps the actual slow_ring event process routine from the
3179 * API jump table function pointer from the lpfc_hba struct.
James Smarte59058c2008-08-24 21:49:00 -04003180 **/
James Smart3772a992009-05-22 14:50:54 -04003181void
James Smart2e0fef82007-06-17 19:56:36 -05003182lpfc_sli_handle_slow_ring_event(struct lpfc_hba *phba,
3183 struct lpfc_sli_ring *pring, uint32_t mask)
dea31012005-04-17 16:05:31 -05003184{
James Smart3772a992009-05-22 14:50:54 -04003185 phba->lpfc_sli_handle_slow_ring_event(phba, pring, mask);
3186}
3187
3188/**
3189 * lpfc_sli_handle_slow_ring_event_s3 - Handle SLI3 ring event for non-FCP rings
3190 * @phba: Pointer to HBA context object.
3191 * @pring: Pointer to driver SLI ring object.
3192 * @mask: Host attention register mask for this ring.
3193 *
3194 * This function is called from the worker thread when there is a ring event
3195 * for non-fcp rings. The caller does not hold any lock. The function will
3196 * remove each response iocb in the response ring and calls the handle
3197 * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
3198 **/
3199static void
3200lpfc_sli_handle_slow_ring_event_s3(struct lpfc_hba *phba,
3201 struct lpfc_sli_ring *pring, uint32_t mask)
3202{
James Smart34b02dc2008-08-24 21:49:55 -04003203 struct lpfc_pgp *pgp;
dea31012005-04-17 16:05:31 -05003204 IOCB_t *entry;
3205 IOCB_t *irsp = NULL;
3206 struct lpfc_iocbq *rspiocbp = NULL;
dea31012005-04-17 16:05:31 -05003207 uint32_t portRspPut, portRspMax;
dea31012005-04-17 16:05:31 -05003208 unsigned long iflag;
James Smart3772a992009-05-22 14:50:54 -04003209 uint32_t status;
dea31012005-04-17 16:05:31 -05003210
James Smart34b02dc2008-08-24 21:49:55 -04003211 pgp = &phba->port_gp[pring->ringno];
James Smart2e0fef82007-06-17 19:56:36 -05003212 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003213 pring->stats.iocb_event++;
3214
dea31012005-04-17 16:05:31 -05003215 /*
3216 * The next available response entry should never exceed the maximum
3217 * entries. If it does, treat it as an adapter hardware error.
3218 */
3219 portRspMax = pring->numRiocb;
3220 portRspPut = le32_to_cpu(pgp->rspPutInx);
3221 if (portRspPut >= portRspMax) {
3222 /*
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02003223 * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
dea31012005-04-17 16:05:31 -05003224 * rsp ring <portRspMax>
3225 */
James Smarted957682007-06-17 19:56:37 -05003226 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003227 "0303 Ring %d handler: portRspPut %d "
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02003228 "is bigger than rsp ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04003229 pring->ringno, portRspPut, portRspMax);
dea31012005-04-17 16:05:31 -05003230
James Smart2e0fef82007-06-17 19:56:36 -05003231 phba->link_state = LPFC_HBA_ERROR;
3232 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003233
3234 phba->work_hs = HS_FFER3;
3235 lpfc_handle_eratt(phba);
3236
James Smart3772a992009-05-22 14:50:54 -04003237 return;
dea31012005-04-17 16:05:31 -05003238 }
3239
3240 rmb();
dea31012005-04-17 16:05:31 -05003241 while (pring->rspidx != portRspPut) {
3242 /*
3243 * Build a completion list and call the appropriate handler.
3244 * The process is to get the next available response iocb, get
3245 * a free iocb from the list, copy the response data into the
3246 * free iocb, insert to the continuation list, and update the
3247 * next response index to slim. This process makes response
3248 * iocb's in the ring available to DMA as fast as possible but
3249 * pays a penalty for a copy operation. Since the iocb is
3250 * only 32 bytes, this penalty is considered small relative to
3251 * the PCI reads for register values and a slim write. When
3252 * the ulpLe field is set, the entire Command has been
3253 * received.
3254 */
James Smarted957682007-06-17 19:56:37 -05003255 entry = lpfc_resp_iocb(phba, pring);
3256
James Smart858c9f62007-06-17 19:56:39 -05003257 phba->last_completion_time = jiffies;
James Smart2e0fef82007-06-17 19:56:36 -05003258 rspiocbp = __lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05003259 if (rspiocbp == NULL) {
3260 printk(KERN_ERR "%s: out of buffers! Failing "
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07003261 "completion.\n", __func__);
dea31012005-04-17 16:05:31 -05003262 break;
3263 }
3264
James Smarted957682007-06-17 19:56:37 -05003265 lpfc_sli_pcimem_bcopy(entry, &rspiocbp->iocb,
3266 phba->iocb_rsp_size);
dea31012005-04-17 16:05:31 -05003267 irsp = &rspiocbp->iocb;
3268
3269 if (++pring->rspidx >= portRspMax)
3270 pring->rspidx = 0;
3271
James Smarta58cbd52007-08-02 11:09:43 -04003272 if (pring->ringno == LPFC_ELS_RING) {
3273 lpfc_debugfs_slow_ring_trc(phba,
3274 "IOCB rsp ring: wd4:x%08x wd6:x%08x wd7:x%08x",
3275 *(((uint32_t *) irsp) + 4),
3276 *(((uint32_t *) irsp) + 6),
3277 *(((uint32_t *) irsp) + 7));
3278 }
3279
James Smarted957682007-06-17 19:56:37 -05003280 writel(pring->rspidx, &phba->host_gp[pring->ringno].rspGetInx);
dea31012005-04-17 16:05:31 -05003281
James Smart3772a992009-05-22 14:50:54 -04003282 spin_unlock_irqrestore(&phba->hbalock, iflag);
3283 /* Handle the response IOCB */
3284 rspiocbp = lpfc_sli_sp_handle_rspiocb(phba, pring, rspiocbp);
3285 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003286
3287 /*
3288 * If the port response put pointer has not been updated, sync
3289 * the pgp->rspPutInx in the MAILBOX_tand fetch the new port
3290 * response put pointer.
3291 */
3292 if (pring->rspidx == portRspPut) {
3293 portRspPut = le32_to_cpu(pgp->rspPutInx);
3294 }
3295 } /* while (pring->rspidx != portRspPut) */
3296
James Smart92d7f7b2007-06-17 19:56:38 -05003297 if ((rspiocbp != NULL) && (mask & HA_R0RE_REQ)) {
dea31012005-04-17 16:05:31 -05003298 /* At least one response entry has been freed */
3299 pring->stats.iocb_rsp_full++;
3300 /* SET RxRE_RSP in Chip Att register */
3301 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
3302 writel(status, phba->CAregaddr);
3303 readl(phba->CAregaddr); /* flush */
3304 }
3305 if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
3306 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
3307 pring->stats.iocb_cmd_empty++;
3308
3309 /* Force update of the local copy of cmdGetInx */
3310 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
3311 lpfc_sli_resume_iocb(phba, pring);
3312
3313 if ((pring->lpfc_sli_cmd_available))
3314 (pring->lpfc_sli_cmd_available) (phba, pring);
3315
3316 }
3317
James Smart2e0fef82007-06-17 19:56:36 -05003318 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart3772a992009-05-22 14:50:54 -04003319 return;
dea31012005-04-17 16:05:31 -05003320}
3321
James Smarte59058c2008-08-24 21:49:00 -04003322/**
James Smart4f774512009-05-22 14:52:35 -04003323 * lpfc_sli_handle_slow_ring_event_s4 - Handle SLI4 slow-path els events
3324 * @phba: Pointer to HBA context object.
3325 * @pring: Pointer to driver SLI ring object.
3326 * @mask: Host attention register mask for this ring.
3327 *
3328 * This function is called from the worker thread when there is a pending
3329 * ELS response iocb on the driver internal slow-path response iocb worker
3330 * queue. The caller does not hold any lock. The function will remove each
3331 * response iocb from the response worker queue and calls the handle
3332 * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
3333 **/
3334static void
3335lpfc_sli_handle_slow_ring_event_s4(struct lpfc_hba *phba,
3336 struct lpfc_sli_ring *pring, uint32_t mask)
3337{
3338 struct lpfc_iocbq *irspiocbq;
James Smart4d9ab992009-10-02 15:16:39 -04003339 struct hbq_dmabuf *dmabuf;
3340 struct lpfc_cq_event *cq_event;
James Smart4f774512009-05-22 14:52:35 -04003341 unsigned long iflag;
3342
James Smart45ed1192009-10-02 15:17:02 -04003343 spin_lock_irqsave(&phba->hbalock, iflag);
3344 phba->hba_flag &= ~HBA_SP_QUEUE_EVT;
3345 spin_unlock_irqrestore(&phba->hbalock, iflag);
3346 while (!list_empty(&phba->sli4_hba.sp_queue_event)) {
James Smart4f774512009-05-22 14:52:35 -04003347 /* Get the response iocb from the head of work queue */
3348 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart45ed1192009-10-02 15:17:02 -04003349 list_remove_head(&phba->sli4_hba.sp_queue_event,
James Smart4d9ab992009-10-02 15:16:39 -04003350 cq_event, struct lpfc_cq_event, list);
James Smart4f774512009-05-22 14:52:35 -04003351 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart4d9ab992009-10-02 15:16:39 -04003352
3353 switch (bf_get(lpfc_wcqe_c_code, &cq_event->cqe.wcqe_cmpl)) {
3354 case CQE_CODE_COMPL_WQE:
3355 irspiocbq = container_of(cq_event, struct lpfc_iocbq,
3356 cq_event);
James Smart45ed1192009-10-02 15:17:02 -04003357 /* Translate ELS WCQE to response IOCBQ */
3358 irspiocbq = lpfc_sli4_els_wcqe_to_rspiocbq(phba,
3359 irspiocbq);
3360 if (irspiocbq)
3361 lpfc_sli_sp_handle_rspiocb(phba, pring,
3362 irspiocbq);
James Smart4d9ab992009-10-02 15:16:39 -04003363 break;
3364 case CQE_CODE_RECEIVE:
James Smart7851fe22011-07-22 18:36:52 -04003365 case CQE_CODE_RECEIVE_V1:
James Smart4d9ab992009-10-02 15:16:39 -04003366 dmabuf = container_of(cq_event, struct hbq_dmabuf,
3367 cq_event);
3368 lpfc_sli4_handle_received_buffer(phba, dmabuf);
3369 break;
3370 default:
3371 break;
3372 }
James Smart4f774512009-05-22 14:52:35 -04003373 }
3374}
3375
3376/**
James Smart3621a712009-04-06 18:47:14 -04003377 * lpfc_sli_abort_iocb_ring - Abort all iocbs in the ring
James Smarte59058c2008-08-24 21:49:00 -04003378 * @phba: Pointer to HBA context object.
3379 * @pring: Pointer to driver SLI ring object.
3380 *
3381 * This function aborts all iocbs in the given ring and frees all the iocb
3382 * objects in txq. This function issues an abort iocb for all the iocb commands
3383 * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
3384 * the return of this function. The caller is not required to hold any locks.
3385 **/
James Smart2e0fef82007-06-17 19:56:36 -05003386void
dea31012005-04-17 16:05:31 -05003387lpfc_sli_abort_iocb_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
3388{
James Smart2534ba72007-04-25 09:52:20 -04003389 LIST_HEAD(completions);
dea31012005-04-17 16:05:31 -05003390 struct lpfc_iocbq *iocb, *next_iocb;
dea31012005-04-17 16:05:31 -05003391
James Smart92d7f7b2007-06-17 19:56:38 -05003392 if (pring->ringno == LPFC_ELS_RING) {
3393 lpfc_fabric_abort_hba(phba);
3394 }
3395
dea31012005-04-17 16:05:31 -05003396 /* Error everything on txq and txcmplq
3397 * First do the txq.
3398 */
James Smart2e0fef82007-06-17 19:56:36 -05003399 spin_lock_irq(&phba->hbalock);
James Smart2534ba72007-04-25 09:52:20 -04003400 list_splice_init(&pring->txq, &completions);
dea31012005-04-17 16:05:31 -05003401 pring->txq_cnt = 0;
dea31012005-04-17 16:05:31 -05003402
3403 /* Next issue ABTS for everything on the txcmplq */
James Smart2534ba72007-04-25 09:52:20 -04003404 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
3405 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
3406
James Smart2e0fef82007-06-17 19:56:36 -05003407 spin_unlock_irq(&phba->hbalock);
James Smart2534ba72007-04-25 09:52:20 -04003408
James Smarta257bf92009-04-06 18:48:10 -04003409 /* Cancel all the IOCBs from the completions list */
3410 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
3411 IOERR_SLI_ABORTED);
dea31012005-04-17 16:05:31 -05003412}
3413
James Smarte59058c2008-08-24 21:49:00 -04003414/**
James Smart3621a712009-04-06 18:47:14 -04003415 * lpfc_sli_flush_fcp_rings - flush all iocbs in the fcp ring
James Smarta8e497d2008-08-24 21:50:11 -04003416 * @phba: Pointer to HBA context object.
3417 *
3418 * This function flushes all iocbs in the fcp ring and frees all the iocb
3419 * objects in txq and txcmplq. This function will not issue abort iocbs
3420 * for all the iocb commands in txcmplq, they will just be returned with
3421 * IOERR_SLI_DOWN. This function is invoked with EEH when device's PCI
3422 * slot has been permanently disabled.
3423 **/
3424void
3425lpfc_sli_flush_fcp_rings(struct lpfc_hba *phba)
3426{
3427 LIST_HEAD(txq);
3428 LIST_HEAD(txcmplq);
James Smarta8e497d2008-08-24 21:50:11 -04003429 struct lpfc_sli *psli = &phba->sli;
3430 struct lpfc_sli_ring *pring;
3431
3432 /* Currently, only one fcp ring */
3433 pring = &psli->ring[psli->fcp_ring];
3434
3435 spin_lock_irq(&phba->hbalock);
3436 /* Retrieve everything on txq */
3437 list_splice_init(&pring->txq, &txq);
3438 pring->txq_cnt = 0;
3439
3440 /* Retrieve everything on the txcmplq */
3441 list_splice_init(&pring->txcmplq, &txcmplq);
3442 pring->txcmplq_cnt = 0;
3443 spin_unlock_irq(&phba->hbalock);
3444
3445 /* Flush the txq */
James Smarta257bf92009-04-06 18:48:10 -04003446 lpfc_sli_cancel_iocbs(phba, &txq, IOSTAT_LOCAL_REJECT,
3447 IOERR_SLI_DOWN);
James Smarta8e497d2008-08-24 21:50:11 -04003448
3449 /* Flush the txcmpq */
James Smarta257bf92009-04-06 18:48:10 -04003450 lpfc_sli_cancel_iocbs(phba, &txcmplq, IOSTAT_LOCAL_REJECT,
3451 IOERR_SLI_DOWN);
James Smarta8e497d2008-08-24 21:50:11 -04003452}
3453
3454/**
James Smart3772a992009-05-22 14:50:54 -04003455 * lpfc_sli_brdready_s3 - Check for sli3 host ready status
James Smarte59058c2008-08-24 21:49:00 -04003456 * @phba: Pointer to HBA context object.
3457 * @mask: Bit mask to be checked.
3458 *
3459 * This function reads the host status register and compares
3460 * with the provided bit mask to check if HBA completed
3461 * the restart. This function will wait in a loop for the
3462 * HBA to complete restart. If the HBA does not restart within
3463 * 15 iterations, the function will reset the HBA again. The
3464 * function returns 1 when HBA fail to restart otherwise returns
3465 * zero.
3466 **/
James Smart3772a992009-05-22 14:50:54 -04003467static int
3468lpfc_sli_brdready_s3(struct lpfc_hba *phba, uint32_t mask)
dea31012005-04-17 16:05:31 -05003469{
Jamie Wellnitz41415862006-02-28 19:25:27 -05003470 uint32_t status;
3471 int i = 0;
3472 int retval = 0;
dea31012005-04-17 16:05:31 -05003473
Jamie Wellnitz41415862006-02-28 19:25:27 -05003474 /* Read the HBA Host Status Register */
James Smart9940b972011-03-11 16:06:12 -05003475 if (lpfc_readl(phba->HSregaddr, &status))
3476 return 1;
dea31012005-04-17 16:05:31 -05003477
Jamie Wellnitz41415862006-02-28 19:25:27 -05003478 /*
3479 * Check status register every 100ms for 5 retries, then every
3480 * 500ms for 5, then every 2.5 sec for 5, then reset board and
3481 * every 2.5 sec for 4.
3482 * Break our of the loop if errors occurred during init.
3483 */
3484 while (((status & mask) != mask) &&
3485 !(status & HS_FFERM) &&
3486 i++ < 20) {
dea31012005-04-17 16:05:31 -05003487
Jamie Wellnitz41415862006-02-28 19:25:27 -05003488 if (i <= 5)
3489 msleep(10);
3490 else if (i <= 10)
3491 msleep(500);
3492 else
3493 msleep(2500);
dea31012005-04-17 16:05:31 -05003494
Jamie Wellnitz41415862006-02-28 19:25:27 -05003495 if (i == 15) {
James Smart2e0fef82007-06-17 19:56:36 -05003496 /* Do post */
James Smart92d7f7b2007-06-17 19:56:38 -05003497 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003498 lpfc_sli_brdrestart(phba);
3499 }
3500 /* Read the HBA Host Status Register */
James Smart9940b972011-03-11 16:06:12 -05003501 if (lpfc_readl(phba->HSregaddr, &status)) {
3502 retval = 1;
3503 break;
3504 }
dea31012005-04-17 16:05:31 -05003505 }
dea31012005-04-17 16:05:31 -05003506
Jamie Wellnitz41415862006-02-28 19:25:27 -05003507 /* Check to see if any errors occurred during init */
3508 if ((status & HS_FFERM) || (i >= 20)) {
James Smarte40a02c2010-02-26 14:13:54 -05003509 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3510 "2751 Adapter failed to restart, "
3511 "status reg x%x, FW Data: A8 x%x AC x%x\n",
3512 status,
3513 readl(phba->MBslimaddr + 0xa8),
3514 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05003515 phba->link_state = LPFC_HBA_ERROR;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003516 retval = 1;
3517 }
dea31012005-04-17 16:05:31 -05003518
Jamie Wellnitz41415862006-02-28 19:25:27 -05003519 return retval;
dea31012005-04-17 16:05:31 -05003520}
3521
James Smartda0436e2009-05-22 14:51:39 -04003522/**
3523 * lpfc_sli_brdready_s4 - Check for sli4 host ready status
3524 * @phba: Pointer to HBA context object.
3525 * @mask: Bit mask to be checked.
3526 *
3527 * This function checks the host status register to check if HBA is
3528 * ready. This function will wait in a loop for the HBA to be ready
3529 * If the HBA is not ready , the function will will reset the HBA PCI
3530 * function again. The function returns 1 when HBA fail to be ready
3531 * otherwise returns zero.
3532 **/
3533static int
3534lpfc_sli_brdready_s4(struct lpfc_hba *phba, uint32_t mask)
3535{
3536 uint32_t status;
3537 int retval = 0;
3538
3539 /* Read the HBA Host Status Register */
3540 status = lpfc_sli4_post_status_check(phba);
3541
3542 if (status) {
3543 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
3544 lpfc_sli_brdrestart(phba);
3545 status = lpfc_sli4_post_status_check(phba);
3546 }
3547
3548 /* Check to see if any errors occurred during init */
3549 if (status) {
3550 phba->link_state = LPFC_HBA_ERROR;
3551 retval = 1;
3552 } else
3553 phba->sli4_hba.intr_enable = 0;
3554
3555 return retval;
3556}
3557
3558/**
3559 * lpfc_sli_brdready - Wrapper func for checking the hba readyness
3560 * @phba: Pointer to HBA context object.
3561 * @mask: Bit mask to be checked.
3562 *
3563 * This routine wraps the actual SLI3 or SLI4 hba readyness check routine
3564 * from the API jump table function pointer from the lpfc_hba struct.
3565 **/
3566int
3567lpfc_sli_brdready(struct lpfc_hba *phba, uint32_t mask)
3568{
3569 return phba->lpfc_sli_brdready(phba, mask);
3570}
3571
James Smart92908312006-03-07 15:04:13 -05003572#define BARRIER_TEST_PATTERN (0xdeadbeef)
3573
James Smarte59058c2008-08-24 21:49:00 -04003574/**
James Smart3621a712009-04-06 18:47:14 -04003575 * lpfc_reset_barrier - Make HBA ready for HBA reset
James Smarte59058c2008-08-24 21:49:00 -04003576 * @phba: Pointer to HBA context object.
3577 *
3578 * This function is called before resetting an HBA. This
3579 * function requests HBA to quiesce DMAs before a reset.
3580 **/
James Smart2e0fef82007-06-17 19:56:36 -05003581void lpfc_reset_barrier(struct lpfc_hba *phba)
James Smart92908312006-03-07 15:04:13 -05003582{
James Smart65a29c12006-07-06 15:50:50 -04003583 uint32_t __iomem *resp_buf;
3584 uint32_t __iomem *mbox_buf;
James Smart92908312006-03-07 15:04:13 -05003585 volatile uint32_t mbox;
James Smart9940b972011-03-11 16:06:12 -05003586 uint32_t hc_copy, ha_copy, resp_data;
James Smart92908312006-03-07 15:04:13 -05003587 int i;
3588 uint8_t hdrtype;
3589
3590 pci_read_config_byte(phba->pcidev, PCI_HEADER_TYPE, &hdrtype);
3591 if (hdrtype != 0x80 ||
3592 (FC_JEDEC_ID(phba->vpd.rev.biuRev) != HELIOS_JEDEC_ID &&
3593 FC_JEDEC_ID(phba->vpd.rev.biuRev) != THOR_JEDEC_ID))
3594 return;
3595
3596 /*
3597 * Tell the other part of the chip to suspend temporarily all
3598 * its DMA activity.
3599 */
James Smart65a29c12006-07-06 15:50:50 -04003600 resp_buf = phba->MBslimaddr;
James Smart92908312006-03-07 15:04:13 -05003601
3602 /* Disable the error attention */
James Smart9940b972011-03-11 16:06:12 -05003603 if (lpfc_readl(phba->HCregaddr, &hc_copy))
3604 return;
James Smart92908312006-03-07 15:04:13 -05003605 writel((hc_copy & ~HC_ERINT_ENA), phba->HCregaddr);
3606 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05003607 phba->link_flag |= LS_IGNORE_ERATT;
James Smart92908312006-03-07 15:04:13 -05003608
James Smart9940b972011-03-11 16:06:12 -05003609 if (lpfc_readl(phba->HAregaddr, &ha_copy))
3610 return;
3611 if (ha_copy & HA_ERATT) {
James Smart92908312006-03-07 15:04:13 -05003612 /* Clear Chip error bit */
3613 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05003614 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05003615 }
3616
3617 mbox = 0;
3618 ((MAILBOX_t *)&mbox)->mbxCommand = MBX_KILL_BOARD;
3619 ((MAILBOX_t *)&mbox)->mbxOwner = OWN_CHIP;
3620
3621 writel(BARRIER_TEST_PATTERN, (resp_buf + 1));
James Smart65a29c12006-07-06 15:50:50 -04003622 mbox_buf = phba->MBslimaddr;
James Smart92908312006-03-07 15:04:13 -05003623 writel(mbox, mbox_buf);
3624
James Smart9940b972011-03-11 16:06:12 -05003625 for (i = 0; i < 50; i++) {
3626 if (lpfc_readl((resp_buf + 1), &resp_data))
3627 return;
3628 if (resp_data != ~(BARRIER_TEST_PATTERN))
3629 mdelay(1);
3630 else
3631 break;
3632 }
3633 resp_data = 0;
3634 if (lpfc_readl((resp_buf + 1), &resp_data))
3635 return;
3636 if (resp_data != ~(BARRIER_TEST_PATTERN)) {
James Smartf4b4c682009-05-22 14:53:12 -04003637 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE ||
James Smart2e0fef82007-06-17 19:56:36 -05003638 phba->pport->stopped)
James Smart92908312006-03-07 15:04:13 -05003639 goto restore_hc;
3640 else
3641 goto clear_errat;
3642 }
3643
3644 ((MAILBOX_t *)&mbox)->mbxOwner = OWN_HOST;
James Smart9940b972011-03-11 16:06:12 -05003645 resp_data = 0;
3646 for (i = 0; i < 500; i++) {
3647 if (lpfc_readl(resp_buf, &resp_data))
3648 return;
3649 if (resp_data != mbox)
3650 mdelay(1);
3651 else
3652 break;
3653 }
James Smart92908312006-03-07 15:04:13 -05003654
3655clear_errat:
3656
James Smart9940b972011-03-11 16:06:12 -05003657 while (++i < 500) {
3658 if (lpfc_readl(phba->HAregaddr, &ha_copy))
3659 return;
3660 if (!(ha_copy & HA_ERATT))
3661 mdelay(1);
3662 else
3663 break;
3664 }
James Smart92908312006-03-07 15:04:13 -05003665
3666 if (readl(phba->HAregaddr) & HA_ERATT) {
3667 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05003668 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05003669 }
3670
3671restore_hc:
James Smart2e0fef82007-06-17 19:56:36 -05003672 phba->link_flag &= ~LS_IGNORE_ERATT;
James Smart92908312006-03-07 15:04:13 -05003673 writel(hc_copy, phba->HCregaddr);
3674 readl(phba->HCregaddr); /* flush */
3675}
3676
James Smarte59058c2008-08-24 21:49:00 -04003677/**
James Smart3621a712009-04-06 18:47:14 -04003678 * lpfc_sli_brdkill - Issue a kill_board mailbox command
James Smarte59058c2008-08-24 21:49:00 -04003679 * @phba: Pointer to HBA context object.
3680 *
3681 * This function issues a kill_board mailbox command and waits for
3682 * the error attention interrupt. This function is called for stopping
3683 * the firmware processing. The caller is not required to hold any
3684 * locks. This function calls lpfc_hba_down_post function to free
3685 * any pending commands after the kill. The function will return 1 when it
3686 * fails to kill the board else will return 0.
3687 **/
Jamie Wellnitz41415862006-02-28 19:25:27 -05003688int
James Smart2e0fef82007-06-17 19:56:36 -05003689lpfc_sli_brdkill(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05003690{
Jamie Wellnitz41415862006-02-28 19:25:27 -05003691 struct lpfc_sli *psli;
3692 LPFC_MBOXQ_t *pmb;
3693 uint32_t status;
3694 uint32_t ha_copy;
3695 int retval;
3696 int i = 0;
3697
3698 psli = &phba->sli;
3699
3700 /* Kill HBA */
James Smarted957682007-06-17 19:56:37 -05003701 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003702 "0329 Kill HBA Data: x%x x%x\n",
3703 phba->pport->port_state, psli->sli_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003704
James Smart98c9ea52007-10-27 13:37:33 -04003705 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3706 if (!pmb)
Jamie Wellnitz41415862006-02-28 19:25:27 -05003707 return 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003708
3709 /* Disable the error attention */
James Smart2e0fef82007-06-17 19:56:36 -05003710 spin_lock_irq(&phba->hbalock);
James Smart9940b972011-03-11 16:06:12 -05003711 if (lpfc_readl(phba->HCregaddr, &status)) {
3712 spin_unlock_irq(&phba->hbalock);
3713 mempool_free(pmb, phba->mbox_mem_pool);
3714 return 1;
3715 }
Jamie Wellnitz41415862006-02-28 19:25:27 -05003716 status &= ~HC_ERINT_ENA;
3717 writel(status, phba->HCregaddr);
3718 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05003719 phba->link_flag |= LS_IGNORE_ERATT;
3720 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003721
3722 lpfc_kill_board(phba, pmb);
3723 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
3724 retval = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
3725
3726 if (retval != MBX_SUCCESS) {
3727 if (retval != MBX_BUSY)
3728 mempool_free(pmb, phba->mbox_mem_pool);
James Smarte40a02c2010-02-26 14:13:54 -05003729 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3730 "2752 KILL_BOARD command failed retval %d\n",
3731 retval);
James Smart2e0fef82007-06-17 19:56:36 -05003732 spin_lock_irq(&phba->hbalock);
3733 phba->link_flag &= ~LS_IGNORE_ERATT;
3734 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003735 return 1;
3736 }
3737
James Smartf4b4c682009-05-22 14:53:12 -04003738 spin_lock_irq(&phba->hbalock);
3739 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
3740 spin_unlock_irq(&phba->hbalock);
James Smart92908312006-03-07 15:04:13 -05003741
Jamie Wellnitz41415862006-02-28 19:25:27 -05003742 mempool_free(pmb, phba->mbox_mem_pool);
3743
3744 /* There is no completion for a KILL_BOARD mbox cmd. Check for an error
3745 * attention every 100ms for 3 seconds. If we don't get ERATT after
3746 * 3 seconds we still set HBA_ERROR state because the status of the
3747 * board is now undefined.
3748 */
James Smart9940b972011-03-11 16:06:12 -05003749 if (lpfc_readl(phba->HAregaddr, &ha_copy))
3750 return 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003751 while ((i++ < 30) && !(ha_copy & HA_ERATT)) {
3752 mdelay(100);
James Smart9940b972011-03-11 16:06:12 -05003753 if (lpfc_readl(phba->HAregaddr, &ha_copy))
3754 return 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003755 }
3756
3757 del_timer_sync(&psli->mbox_tmo);
James Smart92908312006-03-07 15:04:13 -05003758 if (ha_copy & HA_ERATT) {
3759 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05003760 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05003761 }
James Smart2e0fef82007-06-17 19:56:36 -05003762 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003763 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart04c68492009-05-22 14:52:52 -04003764 psli->mbox_active = NULL;
James Smart2e0fef82007-06-17 19:56:36 -05003765 phba->link_flag &= ~LS_IGNORE_ERATT;
3766 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003767
Jamie Wellnitz41415862006-02-28 19:25:27 -05003768 lpfc_hba_down_post(phba);
James Smart2e0fef82007-06-17 19:56:36 -05003769 phba->link_state = LPFC_HBA_ERROR;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003770
James Smart2e0fef82007-06-17 19:56:36 -05003771 return ha_copy & HA_ERATT ? 0 : 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003772}
3773
James Smarte59058c2008-08-24 21:49:00 -04003774/**
James Smart3772a992009-05-22 14:50:54 -04003775 * lpfc_sli_brdreset - Reset a sli-2 or sli-3 HBA
James Smarte59058c2008-08-24 21:49:00 -04003776 * @phba: Pointer to HBA context object.
3777 *
3778 * This function resets the HBA by writing HC_INITFF to the control
3779 * register. After the HBA resets, this function resets all the iocb ring
3780 * indices. This function disables PCI layer parity checking during
3781 * the reset.
3782 * This function returns 0 always.
3783 * The caller is not required to hold any locks.
3784 **/
Jamie Wellnitz41415862006-02-28 19:25:27 -05003785int
James Smart2e0fef82007-06-17 19:56:36 -05003786lpfc_sli_brdreset(struct lpfc_hba *phba)
Jamie Wellnitz41415862006-02-28 19:25:27 -05003787{
3788 struct lpfc_sli *psli;
dea31012005-04-17 16:05:31 -05003789 struct lpfc_sli_ring *pring;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003790 uint16_t cfg_value;
dea31012005-04-17 16:05:31 -05003791 int i;
dea31012005-04-17 16:05:31 -05003792
Jamie Wellnitz41415862006-02-28 19:25:27 -05003793 psli = &phba->sli;
dea31012005-04-17 16:05:31 -05003794
Jamie Wellnitz41415862006-02-28 19:25:27 -05003795 /* Reset HBA */
3796 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003797 "0325 Reset HBA Data: x%x x%x\n",
James Smart2e0fef82007-06-17 19:56:36 -05003798 phba->pport->port_state, psli->sli_flag);
dea31012005-04-17 16:05:31 -05003799
3800 /* perform board reset */
3801 phba->fc_eventTag = 0;
James Smart4d9ab992009-10-02 15:16:39 -04003802 phba->link_events = 0;
James Smart2e0fef82007-06-17 19:56:36 -05003803 phba->pport->fc_myDID = 0;
3804 phba->pport->fc_prevDID = 0;
dea31012005-04-17 16:05:31 -05003805
Jamie Wellnitz41415862006-02-28 19:25:27 -05003806 /* Turn off parity checking and serr during the physical reset */
3807 pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
3808 pci_write_config_word(phba->pcidev, PCI_COMMAND,
3809 (cfg_value &
3810 ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
3811
James Smart3772a992009-05-22 14:50:54 -04003812 psli->sli_flag &= ~(LPFC_SLI_ACTIVE | LPFC_PROCESS_LA);
3813
Jamie Wellnitz41415862006-02-28 19:25:27 -05003814 /* Now toggle INITFF bit in the Host Control Register */
3815 writel(HC_INITFF, phba->HCregaddr);
3816 mdelay(1);
3817 readl(phba->HCregaddr); /* flush */
3818 writel(0, phba->HCregaddr);
3819 readl(phba->HCregaddr); /* flush */
3820
3821 /* Restore PCI cmd register */
3822 pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
dea31012005-04-17 16:05:31 -05003823
3824 /* Initialize relevant SLI info */
Jamie Wellnitz41415862006-02-28 19:25:27 -05003825 for (i = 0; i < psli->num_rings; i++) {
3826 pring = &psli->ring[i];
dea31012005-04-17 16:05:31 -05003827 pring->flag = 0;
3828 pring->rspidx = 0;
3829 pring->next_cmdidx = 0;
3830 pring->local_getidx = 0;
3831 pring->cmdidx = 0;
3832 pring->missbufcnt = 0;
3833 }
dea31012005-04-17 16:05:31 -05003834
James Smart2e0fef82007-06-17 19:56:36 -05003835 phba->link_state = LPFC_WARM_START;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003836 return 0;
3837}
3838
James Smarte59058c2008-08-24 21:49:00 -04003839/**
James Smartda0436e2009-05-22 14:51:39 -04003840 * lpfc_sli4_brdreset - Reset a sli-4 HBA
3841 * @phba: Pointer to HBA context object.
3842 *
3843 * This function resets a SLI4 HBA. This function disables PCI layer parity
3844 * checking during resets the device. The caller is not required to hold
3845 * any locks.
3846 *
3847 * This function returns 0 always.
3848 **/
3849int
3850lpfc_sli4_brdreset(struct lpfc_hba *phba)
3851{
3852 struct lpfc_sli *psli = &phba->sli;
3853 uint16_t cfg_value;
3854 uint8_t qindx;
3855
3856 /* Reset HBA */
3857 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3858 "0295 Reset HBA Data: x%x x%x\n",
3859 phba->pport->port_state, psli->sli_flag);
3860
3861 /* perform board reset */
3862 phba->fc_eventTag = 0;
James Smart4d9ab992009-10-02 15:16:39 -04003863 phba->link_events = 0;
James Smartda0436e2009-05-22 14:51:39 -04003864 phba->pport->fc_myDID = 0;
3865 phba->pport->fc_prevDID = 0;
3866
James Smartda0436e2009-05-22 14:51:39 -04003867 spin_lock_irq(&phba->hbalock);
3868 psli->sli_flag &= ~(LPFC_PROCESS_LA);
3869 phba->fcf.fcf_flag = 0;
3870 /* Clean up the child queue list for the CQs */
3871 list_del_init(&phba->sli4_hba.mbx_wq->list);
3872 list_del_init(&phba->sli4_hba.els_wq->list);
3873 list_del_init(&phba->sli4_hba.hdr_rq->list);
3874 list_del_init(&phba->sli4_hba.dat_rq->list);
3875 list_del_init(&phba->sli4_hba.mbx_cq->list);
3876 list_del_init(&phba->sli4_hba.els_cq->list);
James Smartda0436e2009-05-22 14:51:39 -04003877 for (qindx = 0; qindx < phba->cfg_fcp_wq_count; qindx++)
3878 list_del_init(&phba->sli4_hba.fcp_wq[qindx]->list);
James Smart05580562011-05-24 11:40:48 -04003879 qindx = 0;
3880 do
James Smartda0436e2009-05-22 14:51:39 -04003881 list_del_init(&phba->sli4_hba.fcp_cq[qindx]->list);
James Smart05580562011-05-24 11:40:48 -04003882 while (++qindx < phba->cfg_fcp_eq_count);
James Smartda0436e2009-05-22 14:51:39 -04003883 spin_unlock_irq(&phba->hbalock);
3884
3885 /* Now physically reset the device */
3886 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3887 "0389 Performing PCI function reset!\n");
James Smartbe858b62010-12-15 17:57:20 -05003888
3889 /* Turn off parity checking and serr during the physical reset */
3890 pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
3891 pci_write_config_word(phba->pcidev, PCI_COMMAND, (cfg_value &
3892 ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
3893
James Smartda0436e2009-05-22 14:51:39 -04003894 /* Perform FCoE PCI function reset */
3895 lpfc_pci_function_reset(phba);
3896
James Smartbe858b62010-12-15 17:57:20 -05003897 /* Restore PCI cmd register */
3898 pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
3899
James Smartda0436e2009-05-22 14:51:39 -04003900 return 0;
3901}
3902
3903/**
3904 * lpfc_sli_brdrestart_s3 - Restart a sli-3 hba
James Smarte59058c2008-08-24 21:49:00 -04003905 * @phba: Pointer to HBA context object.
3906 *
3907 * This function is called in the SLI initialization code path to
3908 * restart the HBA. The caller is not required to hold any lock.
3909 * This function writes MBX_RESTART mailbox command to the SLIM and
3910 * resets the HBA. At the end of the function, it calls lpfc_hba_down_post
3911 * function to free any pending commands. The function enables
3912 * POST only during the first initialization. The function returns zero.
3913 * The function does not guarantee completion of MBX_RESTART mailbox
3914 * command before the return of this function.
3915 **/
James Smartda0436e2009-05-22 14:51:39 -04003916static int
3917lpfc_sli_brdrestart_s3(struct lpfc_hba *phba)
Jamie Wellnitz41415862006-02-28 19:25:27 -05003918{
3919 MAILBOX_t *mb;
3920 struct lpfc_sli *psli;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003921 volatile uint32_t word0;
3922 void __iomem *to_slim;
James Smart0d878412009-10-02 15:16:56 -04003923 uint32_t hba_aer_enabled;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003924
James Smart2e0fef82007-06-17 19:56:36 -05003925 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003926
James Smart0d878412009-10-02 15:16:56 -04003927 /* Take PCIe device Advanced Error Reporting (AER) state */
3928 hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
3929
Jamie Wellnitz41415862006-02-28 19:25:27 -05003930 psli = &phba->sli;
3931
3932 /* Restart HBA */
3933 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003934 "0337 Restart HBA Data: x%x x%x\n",
James Smart2e0fef82007-06-17 19:56:36 -05003935 phba->pport->port_state, psli->sli_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003936
3937 word0 = 0;
3938 mb = (MAILBOX_t *) &word0;
3939 mb->mbxCommand = MBX_RESTART;
3940 mb->mbxHc = 1;
3941
James Smart92908312006-03-07 15:04:13 -05003942 lpfc_reset_barrier(phba);
3943
Jamie Wellnitz41415862006-02-28 19:25:27 -05003944 to_slim = phba->MBslimaddr;
3945 writel(*(uint32_t *) mb, to_slim);
3946 readl(to_slim); /* flush */
3947
3948 /* Only skip post after fc_ffinit is completed */
James Smarteaf15d52008-12-04 22:39:29 -05003949 if (phba->pport->port_state)
Jamie Wellnitz41415862006-02-28 19:25:27 -05003950 word0 = 1; /* This is really setting up word1 */
James Smarteaf15d52008-12-04 22:39:29 -05003951 else
Jamie Wellnitz41415862006-02-28 19:25:27 -05003952 word0 = 0; /* This is really setting up word1 */
James Smart65a29c12006-07-06 15:50:50 -04003953 to_slim = phba->MBslimaddr + sizeof (uint32_t);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003954 writel(*(uint32_t *) mb, to_slim);
3955 readl(to_slim); /* flush */
3956
3957 lpfc_sli_brdreset(phba);
James Smart2e0fef82007-06-17 19:56:36 -05003958 phba->pport->stopped = 0;
3959 phba->link_state = LPFC_INIT_START;
James Smartda0436e2009-05-22 14:51:39 -04003960 phba->hba_flag = 0;
James Smart2e0fef82007-06-17 19:56:36 -05003961 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003962
James Smart64ba8812006-08-02 15:24:34 -04003963 memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
3964 psli->stats_start = get_seconds();
3965
James Smarteaf15d52008-12-04 22:39:29 -05003966 /* Give the INITFF and Post time to settle. */
3967 mdelay(100);
dea31012005-04-17 16:05:31 -05003968
James Smart0d878412009-10-02 15:16:56 -04003969 /* Reset HBA AER if it was enabled, note hba_flag was reset above */
3970 if (hba_aer_enabled)
3971 pci_disable_pcie_error_reporting(phba->pcidev);
3972
Jamie Wellnitz41415862006-02-28 19:25:27 -05003973 lpfc_hba_down_post(phba);
dea31012005-04-17 16:05:31 -05003974
3975 return 0;
3976}
3977
James Smarte59058c2008-08-24 21:49:00 -04003978/**
James Smartda0436e2009-05-22 14:51:39 -04003979 * lpfc_sli_brdrestart_s4 - Restart the sli-4 hba
3980 * @phba: Pointer to HBA context object.
3981 *
3982 * This function is called in the SLI initialization code path to restart
3983 * a SLI4 HBA. The caller is not required to hold any lock.
3984 * At the end of the function, it calls lpfc_hba_down_post function to
3985 * free any pending commands.
3986 **/
3987static int
3988lpfc_sli_brdrestart_s4(struct lpfc_hba *phba)
3989{
3990 struct lpfc_sli *psli = &phba->sli;
James Smart75baf692010-06-08 18:31:21 -04003991 uint32_t hba_aer_enabled;
James Smartda0436e2009-05-22 14:51:39 -04003992
3993 /* Restart HBA */
3994 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3995 "0296 Restart HBA Data: x%x x%x\n",
3996 phba->pport->port_state, psli->sli_flag);
3997
James Smart75baf692010-06-08 18:31:21 -04003998 /* Take PCIe device Advanced Error Reporting (AER) state */
3999 hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
4000
James Smartda0436e2009-05-22 14:51:39 -04004001 lpfc_sli4_brdreset(phba);
4002
4003 spin_lock_irq(&phba->hbalock);
4004 phba->pport->stopped = 0;
4005 phba->link_state = LPFC_INIT_START;
4006 phba->hba_flag = 0;
4007 spin_unlock_irq(&phba->hbalock);
4008
4009 memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
4010 psli->stats_start = get_seconds();
4011
James Smart75baf692010-06-08 18:31:21 -04004012 /* Reset HBA AER if it was enabled, note hba_flag was reset above */
4013 if (hba_aer_enabled)
4014 pci_disable_pcie_error_reporting(phba->pcidev);
4015
James Smartda0436e2009-05-22 14:51:39 -04004016 lpfc_hba_down_post(phba);
4017
4018 return 0;
4019}
4020
4021/**
4022 * lpfc_sli_brdrestart - Wrapper func for restarting hba
4023 * @phba: Pointer to HBA context object.
4024 *
4025 * This routine wraps the actual SLI3 or SLI4 hba restart routine from the
4026 * API jump table function pointer from the lpfc_hba struct.
4027**/
4028int
4029lpfc_sli_brdrestart(struct lpfc_hba *phba)
4030{
4031 return phba->lpfc_sli_brdrestart(phba);
4032}
4033
4034/**
James Smart3621a712009-04-06 18:47:14 -04004035 * lpfc_sli_chipset_init - Wait for the restart of the HBA after a restart
James Smarte59058c2008-08-24 21:49:00 -04004036 * @phba: Pointer to HBA context object.
4037 *
4038 * This function is called after a HBA restart to wait for successful
4039 * restart of the HBA. Successful restart of the HBA is indicated by
4040 * HS_FFRDY and HS_MBRDY bits. If the HBA fails to restart even after 15
4041 * iteration, the function will restart the HBA again. The function returns
4042 * zero if HBA successfully restarted else returns negative error code.
4043 **/
dea31012005-04-17 16:05:31 -05004044static int
4045lpfc_sli_chipset_init(struct lpfc_hba *phba)
4046{
4047 uint32_t status, i = 0;
4048
4049 /* Read the HBA Host Status Register */
James Smart9940b972011-03-11 16:06:12 -05004050 if (lpfc_readl(phba->HSregaddr, &status))
4051 return -EIO;
dea31012005-04-17 16:05:31 -05004052
4053 /* Check status register to see what current state is */
4054 i = 0;
4055 while ((status & (HS_FFRDY | HS_MBRDY)) != (HS_FFRDY | HS_MBRDY)) {
4056
James Smartdcf2a4e2010-09-29 11:18:53 -04004057 /* Check every 10ms for 10 retries, then every 100ms for 90
4058 * retries, then every 1 sec for 50 retires for a total of
4059 * ~60 seconds before reset the board again and check every
4060 * 1 sec for 50 retries. The up to 60 seconds before the
4061 * board ready is required by the Falcon FIPS zeroization
4062 * complete, and any reset the board in between shall cause
4063 * restart of zeroization, further delay the board ready.
dea31012005-04-17 16:05:31 -05004064 */
James Smartdcf2a4e2010-09-29 11:18:53 -04004065 if (i++ >= 200) {
dea31012005-04-17 16:05:31 -05004066 /* Adapter failed to init, timeout, status reg
4067 <status> */
James Smarted957682007-06-17 19:56:37 -05004068 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004069 "0436 Adapter failed to init, "
James Smart09372822008-01-11 01:52:54 -05004070 "timeout, status reg x%x, "
4071 "FW Data: A8 x%x AC x%x\n", status,
4072 readl(phba->MBslimaddr + 0xa8),
4073 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05004074 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05004075 return -ETIMEDOUT;
4076 }
4077
4078 /* Check to see if any errors occurred during init */
4079 if (status & HS_FFERM) {
4080 /* ERROR: During chipset initialization */
4081 /* Adapter failed to init, chipset, status reg
4082 <status> */
James Smarted957682007-06-17 19:56:37 -05004083 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004084 "0437 Adapter failed to init, "
James Smart09372822008-01-11 01:52:54 -05004085 "chipset, status reg x%x, "
4086 "FW Data: A8 x%x AC x%x\n", status,
4087 readl(phba->MBslimaddr + 0xa8),
4088 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05004089 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05004090 return -EIO;
4091 }
4092
James Smartdcf2a4e2010-09-29 11:18:53 -04004093 if (i <= 10)
dea31012005-04-17 16:05:31 -05004094 msleep(10);
James Smartdcf2a4e2010-09-29 11:18:53 -04004095 else if (i <= 100)
4096 msleep(100);
4097 else
4098 msleep(1000);
dea31012005-04-17 16:05:31 -05004099
James Smartdcf2a4e2010-09-29 11:18:53 -04004100 if (i == 150) {
4101 /* Do post */
James Smart92d7f7b2007-06-17 19:56:38 -05004102 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004103 lpfc_sli_brdrestart(phba);
dea31012005-04-17 16:05:31 -05004104 }
4105 /* Read the HBA Host Status Register */
James Smart9940b972011-03-11 16:06:12 -05004106 if (lpfc_readl(phba->HSregaddr, &status))
4107 return -EIO;
dea31012005-04-17 16:05:31 -05004108 }
4109
4110 /* Check to see if any errors occurred during init */
4111 if (status & HS_FFERM) {
4112 /* ERROR: During chipset initialization */
4113 /* Adapter failed to init, chipset, status reg <status> */
James Smarted957682007-06-17 19:56:37 -05004114 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004115 "0438 Adapter failed to init, chipset, "
James Smart09372822008-01-11 01:52:54 -05004116 "status reg x%x, "
4117 "FW Data: A8 x%x AC x%x\n", status,
4118 readl(phba->MBslimaddr + 0xa8),
4119 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05004120 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05004121 return -EIO;
4122 }
4123
4124 /* Clear all interrupt enable conditions */
4125 writel(0, phba->HCregaddr);
4126 readl(phba->HCregaddr); /* flush */
4127
4128 /* setup host attn register */
4129 writel(0xffffffff, phba->HAregaddr);
4130 readl(phba->HAregaddr); /* flush */
4131 return 0;
4132}
4133
James Smarte59058c2008-08-24 21:49:00 -04004134/**
James Smart3621a712009-04-06 18:47:14 -04004135 * lpfc_sli_hbq_count - Get the number of HBQs to be configured
James Smarte59058c2008-08-24 21:49:00 -04004136 *
4137 * This function calculates and returns the number of HBQs required to be
4138 * configured.
4139 **/
James Smart78b2d852007-08-02 11:10:21 -04004140int
James Smarted957682007-06-17 19:56:37 -05004141lpfc_sli_hbq_count(void)
4142{
James Smart92d7f7b2007-06-17 19:56:38 -05004143 return ARRAY_SIZE(lpfc_hbq_defs);
James Smarted957682007-06-17 19:56:37 -05004144}
4145
James Smarte59058c2008-08-24 21:49:00 -04004146/**
James Smart3621a712009-04-06 18:47:14 -04004147 * lpfc_sli_hbq_entry_count - Calculate total number of hbq entries
James Smarte59058c2008-08-24 21:49:00 -04004148 *
4149 * This function adds the number of hbq entries in every HBQ to get
4150 * the total number of hbq entries required for the HBA and returns
4151 * the total count.
4152 **/
James Smarted957682007-06-17 19:56:37 -05004153static int
4154lpfc_sli_hbq_entry_count(void)
4155{
4156 int hbq_count = lpfc_sli_hbq_count();
4157 int count = 0;
4158 int i;
4159
4160 for (i = 0; i < hbq_count; ++i)
James Smart92d7f7b2007-06-17 19:56:38 -05004161 count += lpfc_hbq_defs[i]->entry_count;
James Smarted957682007-06-17 19:56:37 -05004162 return count;
4163}
4164
James Smarte59058c2008-08-24 21:49:00 -04004165/**
James Smart3621a712009-04-06 18:47:14 -04004166 * lpfc_sli_hbq_size - Calculate memory required for all hbq entries
James Smarte59058c2008-08-24 21:49:00 -04004167 *
4168 * This function calculates amount of memory required for all hbq entries
4169 * to be configured and returns the total memory required.
4170 **/
dea31012005-04-17 16:05:31 -05004171int
James Smarted957682007-06-17 19:56:37 -05004172lpfc_sli_hbq_size(void)
4173{
4174 return lpfc_sli_hbq_entry_count() * sizeof(struct lpfc_hbq_entry);
4175}
4176
James Smarte59058c2008-08-24 21:49:00 -04004177/**
James Smart3621a712009-04-06 18:47:14 -04004178 * lpfc_sli_hbq_setup - configure and initialize HBQs
James Smarte59058c2008-08-24 21:49:00 -04004179 * @phba: Pointer to HBA context object.
4180 *
4181 * This function is called during the SLI initialization to configure
4182 * all the HBQs and post buffers to the HBQ. The caller is not
4183 * required to hold any locks. This function will return zero if successful
4184 * else it will return negative error code.
4185 **/
James Smarted957682007-06-17 19:56:37 -05004186static int
4187lpfc_sli_hbq_setup(struct lpfc_hba *phba)
4188{
4189 int hbq_count = lpfc_sli_hbq_count();
4190 LPFC_MBOXQ_t *pmb;
4191 MAILBOX_t *pmbox;
4192 uint32_t hbqno;
4193 uint32_t hbq_entry_index;
James Smarted957682007-06-17 19:56:37 -05004194
James Smart92d7f7b2007-06-17 19:56:38 -05004195 /* Get a Mailbox buffer to setup mailbox
4196 * commands for HBA initialization
4197 */
James Smarted957682007-06-17 19:56:37 -05004198 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4199
4200 if (!pmb)
4201 return -ENOMEM;
4202
James Smart04c68492009-05-22 14:52:52 -04004203 pmbox = &pmb->u.mb;
James Smarted957682007-06-17 19:56:37 -05004204
4205 /* Initialize the struct lpfc_sli_hbq structure for each hbq */
4206 phba->link_state = LPFC_INIT_MBX_CMDS;
James Smart3163f722008-02-08 18:50:25 -05004207 phba->hbq_in_use = 1;
James Smarted957682007-06-17 19:56:37 -05004208
4209 hbq_entry_index = 0;
4210 for (hbqno = 0; hbqno < hbq_count; ++hbqno) {
4211 phba->hbqs[hbqno].next_hbqPutIdx = 0;
4212 phba->hbqs[hbqno].hbqPutIdx = 0;
4213 phba->hbqs[hbqno].local_hbqGetIdx = 0;
4214 phba->hbqs[hbqno].entry_count =
James Smart92d7f7b2007-06-17 19:56:38 -05004215 lpfc_hbq_defs[hbqno]->entry_count;
James Smart51ef4c22007-08-02 11:10:31 -04004216 lpfc_config_hbq(phba, hbqno, lpfc_hbq_defs[hbqno],
4217 hbq_entry_index, pmb);
James Smarted957682007-06-17 19:56:37 -05004218 hbq_entry_index += phba->hbqs[hbqno].entry_count;
4219
4220 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
4221 /* Adapter failed to init, mbxCmd <cmd> CFG_RING,
4222 mbxStatus <status>, ring <num> */
4223
4224 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -05004225 LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04004226 "1805 Adapter failed to init. "
James Smarted957682007-06-17 19:56:37 -05004227 "Data: x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04004228 pmbox->mbxCommand,
James Smarted957682007-06-17 19:56:37 -05004229 pmbox->mbxStatus, hbqno);
4230
4231 phba->link_state = LPFC_HBA_ERROR;
4232 mempool_free(pmb, phba->mbox_mem_pool);
James Smart6e7288d2010-06-07 15:23:35 -04004233 return -ENXIO;
James Smarted957682007-06-17 19:56:37 -05004234 }
4235 }
4236 phba->hbq_count = hbq_count;
4237
James Smarted957682007-06-17 19:56:37 -05004238 mempool_free(pmb, phba->mbox_mem_pool);
4239
James Smart92d7f7b2007-06-17 19:56:38 -05004240 /* Initially populate or replenish the HBQs */
James Smartd7c255b2008-08-24 21:50:00 -04004241 for (hbqno = 0; hbqno < hbq_count; ++hbqno)
4242 lpfc_sli_hbqbuf_init_hbqs(phba, hbqno);
James Smarted957682007-06-17 19:56:37 -05004243 return 0;
4244}
4245
James Smarte59058c2008-08-24 21:49:00 -04004246/**
James Smart4f774512009-05-22 14:52:35 -04004247 * lpfc_sli4_rb_setup - Initialize and post RBs to HBA
4248 * @phba: Pointer to HBA context object.
4249 *
4250 * This function is called during the SLI initialization to configure
4251 * all the HBQs and post buffers to the HBQ. The caller is not
4252 * required to hold any locks. This function will return zero if successful
4253 * else it will return negative error code.
4254 **/
4255static int
4256lpfc_sli4_rb_setup(struct lpfc_hba *phba)
4257{
4258 phba->hbq_in_use = 1;
4259 phba->hbqs[0].entry_count = lpfc_hbq_defs[0]->entry_count;
4260 phba->hbq_count = 1;
4261 /* Initially populate or replenish the HBQs */
4262 lpfc_sli_hbqbuf_init_hbqs(phba, 0);
4263 return 0;
4264}
4265
4266/**
James Smart3621a712009-04-06 18:47:14 -04004267 * lpfc_sli_config_port - Issue config port mailbox command
James Smarte59058c2008-08-24 21:49:00 -04004268 * @phba: Pointer to HBA context object.
4269 * @sli_mode: sli mode - 2/3
4270 *
4271 * This function is called by the sli intialization code path
4272 * to issue config_port mailbox command. This function restarts the
4273 * HBA firmware and issues a config_port mailbox command to configure
4274 * the SLI interface in the sli mode specified by sli_mode
4275 * variable. The caller is not required to hold any locks.
4276 * The function returns 0 if successful, else returns negative error
4277 * code.
4278 **/
James Smart93996272008-08-24 21:50:30 -04004279int
4280lpfc_sli_config_port(struct lpfc_hba *phba, int sli_mode)
dea31012005-04-17 16:05:31 -05004281{
4282 LPFC_MBOXQ_t *pmb;
4283 uint32_t resetcount = 0, rc = 0, done = 0;
4284
4285 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4286 if (!pmb) {
James Smart2e0fef82007-06-17 19:56:36 -05004287 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05004288 return -ENOMEM;
4289 }
4290
James Smarted957682007-06-17 19:56:37 -05004291 phba->sli_rev = sli_mode;
dea31012005-04-17 16:05:31 -05004292 while (resetcount < 2 && !done) {
James Smart2e0fef82007-06-17 19:56:36 -05004293 spin_lock_irq(&phba->hbalock);
James Smart1c067a42006-08-01 07:33:52 -04004294 phba->sli.sli_flag |= LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05004295 spin_unlock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -05004296 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004297 lpfc_sli_brdrestart(phba);
dea31012005-04-17 16:05:31 -05004298 rc = lpfc_sli_chipset_init(phba);
4299 if (rc)
4300 break;
4301
James Smart2e0fef82007-06-17 19:56:36 -05004302 spin_lock_irq(&phba->hbalock);
James Smart1c067a42006-08-01 07:33:52 -04004303 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05004304 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004305 resetcount++;
4306
James Smarted957682007-06-17 19:56:37 -05004307 /* Call pre CONFIG_PORT mailbox command initialization. A
4308 * value of 0 means the call was successful. Any other
4309 * nonzero value is a failure, but if ERESTART is returned,
4310 * the driver may reset the HBA and try again.
4311 */
dea31012005-04-17 16:05:31 -05004312 rc = lpfc_config_port_prep(phba);
4313 if (rc == -ERESTART) {
James Smarted957682007-06-17 19:56:37 -05004314 phba->link_state = LPFC_LINK_UNKNOWN;
dea31012005-04-17 16:05:31 -05004315 continue;
James Smart34b02dc2008-08-24 21:49:55 -04004316 } else if (rc)
dea31012005-04-17 16:05:31 -05004317 break;
James Smart6d368e52011-05-24 11:44:12 -04004318
James Smart2e0fef82007-06-17 19:56:36 -05004319 phba->link_state = LPFC_INIT_MBX_CMDS;
dea31012005-04-17 16:05:31 -05004320 lpfc_config_port(phba, pmb);
4321 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
James Smart34b02dc2008-08-24 21:49:55 -04004322 phba->sli3_options &= ~(LPFC_SLI3_NPIV_ENABLED |
4323 LPFC_SLI3_HBQ_ENABLED |
4324 LPFC_SLI3_CRP_ENABLED |
James Smartbc739052010-08-04 16:11:18 -04004325 LPFC_SLI3_BG_ENABLED |
4326 LPFC_SLI3_DSS_ENABLED);
James Smarted957682007-06-17 19:56:37 -05004327 if (rc != MBX_SUCCESS) {
dea31012005-04-17 16:05:31 -05004328 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004329 "0442 Adapter failed to init, mbxCmd x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05004330 "CONFIG_PORT, mbxStatus x%x Data: x%x\n",
James Smart04c68492009-05-22 14:52:52 -04004331 pmb->u.mb.mbxCommand, pmb->u.mb.mbxStatus, 0);
James Smart2e0fef82007-06-17 19:56:36 -05004332 spin_lock_irq(&phba->hbalock);
James Smart04c68492009-05-22 14:52:52 -04004333 phba->sli.sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05004334 spin_unlock_irq(&phba->hbalock);
4335 rc = -ENXIO;
James Smart04c68492009-05-22 14:52:52 -04004336 } else {
4337 /* Allow asynchronous mailbox command to go through */
4338 spin_lock_irq(&phba->hbalock);
4339 phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
4340 spin_unlock_irq(&phba->hbalock);
James Smarted957682007-06-17 19:56:37 -05004341 done = 1;
James Smart04c68492009-05-22 14:52:52 -04004342 }
dea31012005-04-17 16:05:31 -05004343 }
James Smarted957682007-06-17 19:56:37 -05004344 if (!done) {
4345 rc = -EINVAL;
4346 goto do_prep_failed;
4347 }
James Smart04c68492009-05-22 14:52:52 -04004348 if (pmb->u.mb.un.varCfgPort.sli_mode == 3) {
4349 if (!pmb->u.mb.un.varCfgPort.cMA) {
James Smart34b02dc2008-08-24 21:49:55 -04004350 rc = -ENXIO;
4351 goto do_prep_failed;
4352 }
James Smart04c68492009-05-22 14:52:52 -04004353 if (phba->max_vpi && pmb->u.mb.un.varCfgPort.gmv) {
James Smart34b02dc2008-08-24 21:49:55 -04004354 phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
James Smart04c68492009-05-22 14:52:52 -04004355 phba->max_vpi = pmb->u.mb.un.varCfgPort.max_vpi;
4356 phba->max_vports = (phba->max_vpi > phba->max_vports) ?
4357 phba->max_vpi : phba->max_vports;
4358
James Smart34b02dc2008-08-24 21:49:55 -04004359 } else
4360 phba->max_vpi = 0;
James Smartbc739052010-08-04 16:11:18 -04004361 phba->fips_level = 0;
4362 phba->fips_spec_rev = 0;
4363 if (pmb->u.mb.un.varCfgPort.gdss) {
James Smart04c68492009-05-22 14:52:52 -04004364 phba->sli3_options |= LPFC_SLI3_DSS_ENABLED;
James Smartbc739052010-08-04 16:11:18 -04004365 phba->fips_level = pmb->u.mb.un.varCfgPort.fips_level;
4366 phba->fips_spec_rev = pmb->u.mb.un.varCfgPort.fips_rev;
4367 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4368 "2850 Security Crypto Active. FIPS x%d "
4369 "(Spec Rev: x%d)",
4370 phba->fips_level, phba->fips_spec_rev);
4371 }
4372 if (pmb->u.mb.un.varCfgPort.sec_err) {
4373 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4374 "2856 Config Port Security Crypto "
4375 "Error: x%x ",
4376 pmb->u.mb.un.varCfgPort.sec_err);
4377 }
James Smart04c68492009-05-22 14:52:52 -04004378 if (pmb->u.mb.un.varCfgPort.gerbm)
James Smart34b02dc2008-08-24 21:49:55 -04004379 phba->sli3_options |= LPFC_SLI3_HBQ_ENABLED;
James Smart04c68492009-05-22 14:52:52 -04004380 if (pmb->u.mb.un.varCfgPort.gcrp)
James Smart34b02dc2008-08-24 21:49:55 -04004381 phba->sli3_options |= LPFC_SLI3_CRP_ENABLED;
James Smart6e7288d2010-06-07 15:23:35 -04004382
4383 phba->hbq_get = phba->mbox->us.s3_pgp.hbq_get;
4384 phba->port_gp = phba->mbox->us.s3_pgp.port;
James Smarte2a0a9d2008-12-04 22:40:02 -05004385
4386 if (phba->cfg_enable_bg) {
James Smart04c68492009-05-22 14:52:52 -04004387 if (pmb->u.mb.un.varCfgPort.gbg)
James Smarte2a0a9d2008-12-04 22:40:02 -05004388 phba->sli3_options |= LPFC_SLI3_BG_ENABLED;
4389 else
4390 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4391 "0443 Adapter did not grant "
4392 "BlockGuard\n");
4393 }
James Smart34b02dc2008-08-24 21:49:55 -04004394 } else {
James Smart8f34f4c2008-12-04 22:39:23 -05004395 phba->hbq_get = NULL;
James Smart34b02dc2008-08-24 21:49:55 -04004396 phba->port_gp = phba->mbox->us.s2.port;
James Smartd7c255b2008-08-24 21:50:00 -04004397 phba->max_vpi = 0;
James Smarted957682007-06-17 19:56:37 -05004398 }
James Smart92d7f7b2007-06-17 19:56:38 -05004399do_prep_failed:
James Smarted957682007-06-17 19:56:37 -05004400 mempool_free(pmb, phba->mbox_mem_pool);
4401 return rc;
4402}
4403
James Smarte59058c2008-08-24 21:49:00 -04004404
4405/**
James Smart3621a712009-04-06 18:47:14 -04004406 * lpfc_sli_hba_setup - SLI intialization function
James Smarte59058c2008-08-24 21:49:00 -04004407 * @phba: Pointer to HBA context object.
4408 *
4409 * This function is the main SLI intialization function. This function
4410 * is called by the HBA intialization code, HBA reset code and HBA
4411 * error attention handler code. Caller is not required to hold any
4412 * locks. This function issues config_port mailbox command to configure
4413 * the SLI, setup iocb rings and HBQ rings. In the end the function
4414 * calls the config_port_post function to issue init_link mailbox
4415 * command and to start the discovery. The function will return zero
4416 * if successful, else it will return negative error code.
4417 **/
James Smarted957682007-06-17 19:56:37 -05004418int
4419lpfc_sli_hba_setup(struct lpfc_hba *phba)
4420{
4421 uint32_t rc;
James Smart6d368e52011-05-24 11:44:12 -04004422 int mode = 3, i;
4423 int longs;
James Smarted957682007-06-17 19:56:37 -05004424
4425 switch (lpfc_sli_mode) {
4426 case 2:
James Smart78b2d852007-08-02 11:10:21 -04004427 if (phba->cfg_enable_npiv) {
James Smart92d7f7b2007-06-17 19:56:38 -05004428 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04004429 "1824 NPIV enabled: Override lpfc_sli_mode "
James Smart92d7f7b2007-06-17 19:56:38 -05004430 "parameter (%d) to auto (0).\n",
James Smarte8b62012007-08-02 11:10:09 -04004431 lpfc_sli_mode);
James Smart92d7f7b2007-06-17 19:56:38 -05004432 break;
4433 }
James Smarted957682007-06-17 19:56:37 -05004434 mode = 2;
4435 break;
4436 case 0:
4437 case 3:
4438 break;
4439 default:
James Smart92d7f7b2007-06-17 19:56:38 -05004440 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04004441 "1819 Unrecognized lpfc_sli_mode "
4442 "parameter: %d.\n", lpfc_sli_mode);
James Smarted957682007-06-17 19:56:37 -05004443
4444 break;
4445 }
4446
James Smart93996272008-08-24 21:50:30 -04004447 rc = lpfc_sli_config_port(phba, mode);
4448
James Smarted957682007-06-17 19:56:37 -05004449 if (rc && lpfc_sli_mode == 3)
James Smart92d7f7b2007-06-17 19:56:38 -05004450 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04004451 "1820 Unable to select SLI-3. "
4452 "Not supported by adapter.\n");
James Smarted957682007-06-17 19:56:37 -05004453 if (rc && mode != 2)
James Smart93996272008-08-24 21:50:30 -04004454 rc = lpfc_sli_config_port(phba, 2);
James Smarted957682007-06-17 19:56:37 -05004455 if (rc)
dea31012005-04-17 16:05:31 -05004456 goto lpfc_sli_hba_setup_error;
4457
James Smart0d878412009-10-02 15:16:56 -04004458 /* Enable PCIe device Advanced Error Reporting (AER) if configured */
4459 if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
4460 rc = pci_enable_pcie_error_reporting(phba->pcidev);
4461 if (!rc) {
4462 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4463 "2709 This device supports "
4464 "Advanced Error Reporting (AER)\n");
4465 spin_lock_irq(&phba->hbalock);
4466 phba->hba_flag |= HBA_AER_ENABLED;
4467 spin_unlock_irq(&phba->hbalock);
4468 } else {
4469 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4470 "2708 This device does not support "
4471 "Advanced Error Reporting (AER)\n");
4472 phba->cfg_aer_support = 0;
4473 }
4474 }
4475
James Smarted957682007-06-17 19:56:37 -05004476 if (phba->sli_rev == 3) {
4477 phba->iocb_cmd_size = SLI3_IOCB_CMD_SIZE;
4478 phba->iocb_rsp_size = SLI3_IOCB_RSP_SIZE;
James Smarted957682007-06-17 19:56:37 -05004479 } else {
4480 phba->iocb_cmd_size = SLI2_IOCB_CMD_SIZE;
4481 phba->iocb_rsp_size = SLI2_IOCB_RSP_SIZE;
James Smart92d7f7b2007-06-17 19:56:38 -05004482 phba->sli3_options = 0;
James Smarted957682007-06-17 19:56:37 -05004483 }
4484
4485 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004486 "0444 Firmware in SLI %x mode. Max_vpi %d\n",
4487 phba->sli_rev, phba->max_vpi);
James Smarted957682007-06-17 19:56:37 -05004488 rc = lpfc_sli_ring_map(phba);
dea31012005-04-17 16:05:31 -05004489
4490 if (rc)
4491 goto lpfc_sli_hba_setup_error;
4492
James Smart6d368e52011-05-24 11:44:12 -04004493 /* Initialize VPIs. */
4494 if (phba->sli_rev == LPFC_SLI_REV3) {
4495 /*
4496 * The VPI bitmask and physical ID array are allocated
4497 * and initialized once only - at driver load. A port
4498 * reset doesn't need to reinitialize this memory.
4499 */
4500 if ((phba->vpi_bmask == NULL) && (phba->vpi_ids == NULL)) {
4501 longs = (phba->max_vpi + BITS_PER_LONG) / BITS_PER_LONG;
4502 phba->vpi_bmask = kzalloc(longs * sizeof(unsigned long),
4503 GFP_KERNEL);
4504 if (!phba->vpi_bmask) {
4505 rc = -ENOMEM;
4506 goto lpfc_sli_hba_setup_error;
4507 }
4508
4509 phba->vpi_ids = kzalloc(
4510 (phba->max_vpi+1) * sizeof(uint16_t),
4511 GFP_KERNEL);
4512 if (!phba->vpi_ids) {
4513 kfree(phba->vpi_bmask);
4514 rc = -ENOMEM;
4515 goto lpfc_sli_hba_setup_error;
4516 }
4517 for (i = 0; i < phba->max_vpi; i++)
4518 phba->vpi_ids[i] = i;
4519 }
4520 }
4521
James Smart93996272008-08-24 21:50:30 -04004522 /* Init HBQs */
James Smarted957682007-06-17 19:56:37 -05004523 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
4524 rc = lpfc_sli_hbq_setup(phba);
4525 if (rc)
4526 goto lpfc_sli_hba_setup_error;
4527 }
James Smart04c68492009-05-22 14:52:52 -04004528 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004529 phba->sli.sli_flag |= LPFC_PROCESS_LA;
James Smart04c68492009-05-22 14:52:52 -04004530 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004531
4532 rc = lpfc_config_port_post(phba);
4533 if (rc)
4534 goto lpfc_sli_hba_setup_error;
4535
James Smarted957682007-06-17 19:56:37 -05004536 return rc;
4537
James Smart92d7f7b2007-06-17 19:56:38 -05004538lpfc_sli_hba_setup_error:
James Smart2e0fef82007-06-17 19:56:36 -05004539 phba->link_state = LPFC_HBA_ERROR;
James Smarte40a02c2010-02-26 14:13:54 -05004540 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004541 "0445 Firmware initialization failed\n");
dea31012005-04-17 16:05:31 -05004542 return rc;
4543}
4544
James Smartda0436e2009-05-22 14:51:39 -04004545/**
4546 * lpfc_sli4_read_fcoe_params - Read fcoe params from conf region
4547 * @phba: Pointer to HBA context object.
4548 * @mboxq: mailbox pointer.
4549 * This function issue a dump mailbox command to read config region
4550 * 23 and parse the records in the region and populate driver
4551 * data structure.
4552 **/
4553static int
4554lpfc_sli4_read_fcoe_params(struct lpfc_hba *phba,
4555 LPFC_MBOXQ_t *mboxq)
4556{
4557 struct lpfc_dmabuf *mp;
4558 struct lpfc_mqe *mqe;
4559 uint32_t data_length;
4560 int rc;
4561
4562 /* Program the default value of vlan_id and fc_map */
4563 phba->valid_vlan = 0;
4564 phba->fc_map[0] = LPFC_FCOE_FCF_MAP0;
4565 phba->fc_map[1] = LPFC_FCOE_FCF_MAP1;
4566 phba->fc_map[2] = LPFC_FCOE_FCF_MAP2;
4567
4568 mqe = &mboxq->u.mqe;
4569 if (lpfc_dump_fcoe_param(phba, mboxq))
4570 return -ENOMEM;
4571
4572 mp = (struct lpfc_dmabuf *) mboxq->context1;
4573 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4574
4575 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
4576 "(%d):2571 Mailbox cmd x%x Status x%x "
4577 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x "
4578 "x%x x%x x%x x%x x%x x%x x%x x%x x%x "
4579 "CQ: x%x x%x x%x x%x\n",
4580 mboxq->vport ? mboxq->vport->vpi : 0,
4581 bf_get(lpfc_mqe_command, mqe),
4582 bf_get(lpfc_mqe_status, mqe),
4583 mqe->un.mb_words[0], mqe->un.mb_words[1],
4584 mqe->un.mb_words[2], mqe->un.mb_words[3],
4585 mqe->un.mb_words[4], mqe->un.mb_words[5],
4586 mqe->un.mb_words[6], mqe->un.mb_words[7],
4587 mqe->un.mb_words[8], mqe->un.mb_words[9],
4588 mqe->un.mb_words[10], mqe->un.mb_words[11],
4589 mqe->un.mb_words[12], mqe->un.mb_words[13],
4590 mqe->un.mb_words[14], mqe->un.mb_words[15],
4591 mqe->un.mb_words[16], mqe->un.mb_words[50],
4592 mboxq->mcqe.word0,
4593 mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
4594 mboxq->mcqe.trailer);
4595
4596 if (rc) {
4597 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4598 kfree(mp);
4599 return -EIO;
4600 }
4601 data_length = mqe->un.mb_words[5];
James Smarta0c87cb2009-07-19 10:01:10 -04004602 if (data_length > DMP_RGN23_SIZE) {
James Smartd11e31d2009-06-10 17:23:06 -04004603 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4604 kfree(mp);
James Smartda0436e2009-05-22 14:51:39 -04004605 return -EIO;
James Smartd11e31d2009-06-10 17:23:06 -04004606 }
James Smartda0436e2009-05-22 14:51:39 -04004607
4608 lpfc_parse_fcoe_conf(phba, mp->virt, data_length);
4609 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4610 kfree(mp);
4611 return 0;
4612}
4613
4614/**
4615 * lpfc_sli4_read_rev - Issue READ_REV and collect vpd data
4616 * @phba: pointer to lpfc hba data structure.
4617 * @mboxq: pointer to the LPFC_MBOXQ_t structure.
4618 * @vpd: pointer to the memory to hold resulting port vpd data.
4619 * @vpd_size: On input, the number of bytes allocated to @vpd.
4620 * On output, the number of data bytes in @vpd.
4621 *
4622 * This routine executes a READ_REV SLI4 mailbox command. In
4623 * addition, this routine gets the port vpd data.
4624 *
4625 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02004626 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -04004627 * -ENOMEM - could not allocated memory.
James Smartda0436e2009-05-22 14:51:39 -04004628 **/
4629static int
4630lpfc_sli4_read_rev(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
4631 uint8_t *vpd, uint32_t *vpd_size)
4632{
4633 int rc = 0;
4634 uint32_t dma_size;
4635 struct lpfc_dmabuf *dmabuf;
4636 struct lpfc_mqe *mqe;
4637
4638 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
4639 if (!dmabuf)
4640 return -ENOMEM;
4641
4642 /*
4643 * Get a DMA buffer for the vpd data resulting from the READ_REV
4644 * mailbox command.
4645 */
4646 dma_size = *vpd_size;
4647 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
4648 dma_size,
4649 &dmabuf->phys,
4650 GFP_KERNEL);
4651 if (!dmabuf->virt) {
4652 kfree(dmabuf);
4653 return -ENOMEM;
4654 }
4655 memset(dmabuf->virt, 0, dma_size);
4656
4657 /*
4658 * The SLI4 implementation of READ_REV conflicts at word1,
4659 * bits 31:16 and SLI4 adds vpd functionality not present
4660 * in SLI3. This code corrects the conflicts.
4661 */
4662 lpfc_read_rev(phba, mboxq);
4663 mqe = &mboxq->u.mqe;
4664 mqe->un.read_rev.vpd_paddr_high = putPaddrHigh(dmabuf->phys);
4665 mqe->un.read_rev.vpd_paddr_low = putPaddrLow(dmabuf->phys);
4666 mqe->un.read_rev.word1 &= 0x0000FFFF;
4667 bf_set(lpfc_mbx_rd_rev_vpd, &mqe->un.read_rev, 1);
4668 bf_set(lpfc_mbx_rd_rev_avail_len, &mqe->un.read_rev, dma_size);
4669
4670 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4671 if (rc) {
4672 dma_free_coherent(&phba->pcidev->dev, dma_size,
4673 dmabuf->virt, dmabuf->phys);
James Smartdef9c7a2009-12-21 17:02:28 -05004674 kfree(dmabuf);
James Smartda0436e2009-05-22 14:51:39 -04004675 return -EIO;
4676 }
4677
James Smartda0436e2009-05-22 14:51:39 -04004678 /*
4679 * The available vpd length cannot be bigger than the
4680 * DMA buffer passed to the port. Catch the less than
4681 * case and update the caller's size.
4682 */
4683 if (mqe->un.read_rev.avail_vpd_len < *vpd_size)
4684 *vpd_size = mqe->un.read_rev.avail_vpd_len;
4685
James Smartd7c47992010-06-08 18:31:54 -04004686 memcpy(vpd, dmabuf->virt, *vpd_size);
4687
James Smartda0436e2009-05-22 14:51:39 -04004688 dma_free_coherent(&phba->pcidev->dev, dma_size,
4689 dmabuf->virt, dmabuf->phys);
4690 kfree(dmabuf);
4691 return 0;
4692}
4693
4694/**
4695 * lpfc_sli4_arm_cqeq_intr - Arm sli-4 device completion and event queues
4696 * @phba: pointer to lpfc hba data structure.
4697 *
4698 * This routine is called to explicitly arm the SLI4 device's completion and
4699 * event queues
4700 **/
4701static void
4702lpfc_sli4_arm_cqeq_intr(struct lpfc_hba *phba)
4703{
4704 uint8_t fcp_eqidx;
4705
4706 lpfc_sli4_cq_release(phba->sli4_hba.mbx_cq, LPFC_QUEUE_REARM);
4707 lpfc_sli4_cq_release(phba->sli4_hba.els_cq, LPFC_QUEUE_REARM);
James Smart05580562011-05-24 11:40:48 -04004708 fcp_eqidx = 0;
4709 do
James Smartda0436e2009-05-22 14:51:39 -04004710 lpfc_sli4_cq_release(phba->sli4_hba.fcp_cq[fcp_eqidx],
4711 LPFC_QUEUE_REARM);
James Smart05580562011-05-24 11:40:48 -04004712 while (++fcp_eqidx < phba->cfg_fcp_eq_count);
James Smartda0436e2009-05-22 14:51:39 -04004713 lpfc_sli4_eq_release(phba->sli4_hba.sp_eq, LPFC_QUEUE_REARM);
4714 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count; fcp_eqidx++)
4715 lpfc_sli4_eq_release(phba->sli4_hba.fp_eq[fcp_eqidx],
4716 LPFC_QUEUE_REARM);
4717}
4718
4719/**
James Smart6d368e52011-05-24 11:44:12 -04004720 * lpfc_sli4_get_avail_extnt_rsrc - Get available resource extent count.
4721 * @phba: Pointer to HBA context object.
4722 * @type: The resource extent type.
James Smartb76f2dc2011-07-22 18:37:42 -04004723 * @extnt_count: buffer to hold port available extent count.
4724 * @extnt_size: buffer to hold element count per extent.
James Smart6d368e52011-05-24 11:44:12 -04004725 *
James Smartb76f2dc2011-07-22 18:37:42 -04004726 * This function calls the port and retrievs the number of available
4727 * extents and their size for a particular extent type.
4728 *
4729 * Returns: 0 if successful. Nonzero otherwise.
James Smart6d368e52011-05-24 11:44:12 -04004730 **/
James Smartb76f2dc2011-07-22 18:37:42 -04004731int
James Smart6d368e52011-05-24 11:44:12 -04004732lpfc_sli4_get_avail_extnt_rsrc(struct lpfc_hba *phba, uint16_t type,
4733 uint16_t *extnt_count, uint16_t *extnt_size)
4734{
4735 int rc = 0;
4736 uint32_t length;
4737 uint32_t mbox_tmo;
4738 struct lpfc_mbx_get_rsrc_extent_info *rsrc_info;
4739 LPFC_MBOXQ_t *mbox;
4740
4741 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4742 if (!mbox)
4743 return -ENOMEM;
4744
4745 /* Find out how many extents are available for this resource type */
4746 length = (sizeof(struct lpfc_mbx_get_rsrc_extent_info) -
4747 sizeof(struct lpfc_sli4_cfg_mhdr));
4748 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
4749 LPFC_MBOX_OPCODE_GET_RSRC_EXTENT_INFO,
4750 length, LPFC_SLI4_MBX_EMBED);
4751
4752 /* Send an extents count of 0 - the GET doesn't use it. */
4753 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, 0, type,
4754 LPFC_SLI4_MBX_EMBED);
4755 if (unlikely(rc)) {
4756 rc = -EIO;
4757 goto err_exit;
4758 }
4759
4760 if (!phba->sli4_hba.intr_enable)
4761 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
4762 else {
James Smarta183a152011-10-10 21:32:43 -04004763 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart6d368e52011-05-24 11:44:12 -04004764 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
4765 }
4766 if (unlikely(rc)) {
4767 rc = -EIO;
4768 goto err_exit;
4769 }
4770
4771 rsrc_info = &mbox->u.mqe.un.rsrc_extent_info;
4772 if (bf_get(lpfc_mbox_hdr_status,
4773 &rsrc_info->header.cfg_shdr.response)) {
4774 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
4775 "2930 Failed to get resource extents "
4776 "Status 0x%x Add'l Status 0x%x\n",
4777 bf_get(lpfc_mbox_hdr_status,
4778 &rsrc_info->header.cfg_shdr.response),
4779 bf_get(lpfc_mbox_hdr_add_status,
4780 &rsrc_info->header.cfg_shdr.response));
4781 rc = -EIO;
4782 goto err_exit;
4783 }
4784
4785 *extnt_count = bf_get(lpfc_mbx_get_rsrc_extent_info_cnt,
4786 &rsrc_info->u.rsp);
4787 *extnt_size = bf_get(lpfc_mbx_get_rsrc_extent_info_size,
4788 &rsrc_info->u.rsp);
4789 err_exit:
4790 mempool_free(mbox, phba->mbox_mem_pool);
4791 return rc;
4792}
4793
4794/**
4795 * lpfc_sli4_chk_avail_extnt_rsrc - Check for available SLI4 resource extents.
4796 * @phba: Pointer to HBA context object.
4797 * @type: The extent type to check.
4798 *
4799 * This function reads the current available extents from the port and checks
4800 * if the extent count or extent size has changed since the last access.
4801 * Callers use this routine post port reset to understand if there is a
4802 * extent reprovisioning requirement.
4803 *
4804 * Returns:
4805 * -Error: error indicates problem.
4806 * 1: Extent count or size has changed.
4807 * 0: No changes.
4808 **/
4809static int
4810lpfc_sli4_chk_avail_extnt_rsrc(struct lpfc_hba *phba, uint16_t type)
4811{
4812 uint16_t curr_ext_cnt, rsrc_ext_cnt;
4813 uint16_t size_diff, rsrc_ext_size;
4814 int rc = 0;
4815 struct lpfc_rsrc_blks *rsrc_entry;
4816 struct list_head *rsrc_blk_list = NULL;
4817
4818 size_diff = 0;
4819 curr_ext_cnt = 0;
4820 rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,
4821 &rsrc_ext_cnt,
4822 &rsrc_ext_size);
4823 if (unlikely(rc))
4824 return -EIO;
4825
4826 switch (type) {
4827 case LPFC_RSC_TYPE_FCOE_RPI:
4828 rsrc_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;
4829 break;
4830 case LPFC_RSC_TYPE_FCOE_VPI:
4831 rsrc_blk_list = &phba->lpfc_vpi_blk_list;
4832 break;
4833 case LPFC_RSC_TYPE_FCOE_XRI:
4834 rsrc_blk_list = &phba->sli4_hba.lpfc_xri_blk_list;
4835 break;
4836 case LPFC_RSC_TYPE_FCOE_VFI:
4837 rsrc_blk_list = &phba->sli4_hba.lpfc_vfi_blk_list;
4838 break;
4839 default:
4840 break;
4841 }
4842
4843 list_for_each_entry(rsrc_entry, rsrc_blk_list, list) {
4844 curr_ext_cnt++;
4845 if (rsrc_entry->rsrc_size != rsrc_ext_size)
4846 size_diff++;
4847 }
4848
4849 if (curr_ext_cnt != rsrc_ext_cnt || size_diff != 0)
4850 rc = 1;
4851
4852 return rc;
4853}
4854
4855/**
4856 * lpfc_sli4_cfg_post_extnts -
4857 * @phba: Pointer to HBA context object.
4858 * @extnt_cnt - number of available extents.
4859 * @type - the extent type (rpi, xri, vfi, vpi).
4860 * @emb - buffer to hold either MBX_EMBED or MBX_NEMBED operation.
4861 * @mbox - pointer to the caller's allocated mailbox structure.
4862 *
4863 * This function executes the extents allocation request. It also
4864 * takes care of the amount of memory needed to allocate or get the
4865 * allocated extents. It is the caller's responsibility to evaluate
4866 * the response.
4867 *
4868 * Returns:
4869 * -Error: Error value describes the condition found.
4870 * 0: if successful
4871 **/
4872static int
4873lpfc_sli4_cfg_post_extnts(struct lpfc_hba *phba, uint16_t *extnt_cnt,
4874 uint16_t type, bool *emb, LPFC_MBOXQ_t *mbox)
4875{
4876 int rc = 0;
4877 uint32_t req_len;
4878 uint32_t emb_len;
4879 uint32_t alloc_len, mbox_tmo;
4880
4881 /* Calculate the total requested length of the dma memory */
4882 req_len = *extnt_cnt * sizeof(uint16_t);
4883
4884 /*
4885 * Calculate the size of an embedded mailbox. The uint32_t
4886 * accounts for extents-specific word.
4887 */
4888 emb_len = sizeof(MAILBOX_t) - sizeof(struct mbox_header) -
4889 sizeof(uint32_t);
4890
4891 /*
4892 * Presume the allocation and response will fit into an embedded
4893 * mailbox. If not true, reconfigure to a non-embedded mailbox.
4894 */
4895 *emb = LPFC_SLI4_MBX_EMBED;
4896 if (req_len > emb_len) {
4897 req_len = *extnt_cnt * sizeof(uint16_t) +
4898 sizeof(union lpfc_sli4_cfg_shdr) +
4899 sizeof(uint32_t);
4900 *emb = LPFC_SLI4_MBX_NEMBED;
4901 }
4902
4903 alloc_len = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
4904 LPFC_MBOX_OPCODE_ALLOC_RSRC_EXTENT,
4905 req_len, *emb);
4906 if (alloc_len < req_len) {
4907 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartb76f2dc2011-07-22 18:37:42 -04004908 "2982 Allocated DMA memory size (x%x) is "
James Smart6d368e52011-05-24 11:44:12 -04004909 "less than the requested DMA memory "
4910 "size (x%x)\n", alloc_len, req_len);
4911 return -ENOMEM;
4912 }
4913 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, *extnt_cnt, type, *emb);
4914 if (unlikely(rc))
4915 return -EIO;
4916
4917 if (!phba->sli4_hba.intr_enable)
4918 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
4919 else {
James Smarta183a152011-10-10 21:32:43 -04004920 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart6d368e52011-05-24 11:44:12 -04004921 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
4922 }
4923
4924 if (unlikely(rc))
4925 rc = -EIO;
4926 return rc;
4927}
4928
4929/**
4930 * lpfc_sli4_alloc_extent - Allocate an SLI4 resource extent.
4931 * @phba: Pointer to HBA context object.
4932 * @type: The resource extent type to allocate.
4933 *
4934 * This function allocates the number of elements for the specified
4935 * resource type.
4936 **/
4937static int
4938lpfc_sli4_alloc_extent(struct lpfc_hba *phba, uint16_t type)
4939{
4940 bool emb = false;
4941 uint16_t rsrc_id_cnt, rsrc_cnt, rsrc_size;
4942 uint16_t rsrc_id, rsrc_start, j, k;
4943 uint16_t *ids;
4944 int i, rc;
4945 unsigned long longs;
4946 unsigned long *bmask;
4947 struct lpfc_rsrc_blks *rsrc_blks;
4948 LPFC_MBOXQ_t *mbox;
4949 uint32_t length;
4950 struct lpfc_id_range *id_array = NULL;
4951 void *virtaddr = NULL;
4952 struct lpfc_mbx_nembed_rsrc_extent *n_rsrc;
4953 struct lpfc_mbx_alloc_rsrc_extents *rsrc_ext;
4954 struct list_head *ext_blk_list;
4955
4956 rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,
4957 &rsrc_cnt,
4958 &rsrc_size);
4959 if (unlikely(rc))
4960 return -EIO;
4961
4962 if ((rsrc_cnt == 0) || (rsrc_size == 0)) {
4963 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
4964 "3009 No available Resource Extents "
4965 "for resource type 0x%x: Count: 0x%x, "
4966 "Size 0x%x\n", type, rsrc_cnt,
4967 rsrc_size);
4968 return -ENOMEM;
4969 }
4970
4971 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_INIT,
4972 "2903 Available Resource Extents "
4973 "for resource type 0x%x: Count: 0x%x, "
4974 "Size 0x%x\n", type, rsrc_cnt,
4975 rsrc_size);
4976
4977 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4978 if (!mbox)
4979 return -ENOMEM;
4980
4981 rc = lpfc_sli4_cfg_post_extnts(phba, &rsrc_cnt, type, &emb, mbox);
4982 if (unlikely(rc)) {
4983 rc = -EIO;
4984 goto err_exit;
4985 }
4986
4987 /*
4988 * Figure out where the response is located. Then get local pointers
4989 * to the response data. The port does not guarantee to respond to
4990 * all extents counts request so update the local variable with the
4991 * allocated count from the port.
4992 */
4993 if (emb == LPFC_SLI4_MBX_EMBED) {
4994 rsrc_ext = &mbox->u.mqe.un.alloc_rsrc_extents;
4995 id_array = &rsrc_ext->u.rsp.id[0];
4996 rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, &rsrc_ext->u.rsp);
4997 } else {
4998 virtaddr = mbox->sge_array->addr[0];
4999 n_rsrc = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
5000 rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, n_rsrc);
5001 id_array = &n_rsrc->id;
5002 }
5003
5004 longs = ((rsrc_cnt * rsrc_size) + BITS_PER_LONG - 1) / BITS_PER_LONG;
5005 rsrc_id_cnt = rsrc_cnt * rsrc_size;
5006
5007 /*
5008 * Based on the resource size and count, correct the base and max
5009 * resource values.
5010 */
5011 length = sizeof(struct lpfc_rsrc_blks);
5012 switch (type) {
5013 case LPFC_RSC_TYPE_FCOE_RPI:
5014 phba->sli4_hba.rpi_bmask = kzalloc(longs *
5015 sizeof(unsigned long),
5016 GFP_KERNEL);
5017 if (unlikely(!phba->sli4_hba.rpi_bmask)) {
5018 rc = -ENOMEM;
5019 goto err_exit;
5020 }
5021 phba->sli4_hba.rpi_ids = kzalloc(rsrc_id_cnt *
5022 sizeof(uint16_t),
5023 GFP_KERNEL);
5024 if (unlikely(!phba->sli4_hba.rpi_ids)) {
5025 kfree(phba->sli4_hba.rpi_bmask);
5026 rc = -ENOMEM;
5027 goto err_exit;
5028 }
5029
5030 /*
5031 * The next_rpi was initialized with the maximum available
5032 * count but the port may allocate a smaller number. Catch
5033 * that case and update the next_rpi.
5034 */
5035 phba->sli4_hba.next_rpi = rsrc_id_cnt;
5036
5037 /* Initialize local ptrs for common extent processing later. */
5038 bmask = phba->sli4_hba.rpi_bmask;
5039 ids = phba->sli4_hba.rpi_ids;
5040 ext_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;
5041 break;
5042 case LPFC_RSC_TYPE_FCOE_VPI:
5043 phba->vpi_bmask = kzalloc(longs *
5044 sizeof(unsigned long),
5045 GFP_KERNEL);
5046 if (unlikely(!phba->vpi_bmask)) {
5047 rc = -ENOMEM;
5048 goto err_exit;
5049 }
5050 phba->vpi_ids = kzalloc(rsrc_id_cnt *
5051 sizeof(uint16_t),
5052 GFP_KERNEL);
5053 if (unlikely(!phba->vpi_ids)) {
5054 kfree(phba->vpi_bmask);
5055 rc = -ENOMEM;
5056 goto err_exit;
5057 }
5058
5059 /* Initialize local ptrs for common extent processing later. */
5060 bmask = phba->vpi_bmask;
5061 ids = phba->vpi_ids;
5062 ext_blk_list = &phba->lpfc_vpi_blk_list;
5063 break;
5064 case LPFC_RSC_TYPE_FCOE_XRI:
5065 phba->sli4_hba.xri_bmask = kzalloc(longs *
5066 sizeof(unsigned long),
5067 GFP_KERNEL);
5068 if (unlikely(!phba->sli4_hba.xri_bmask)) {
5069 rc = -ENOMEM;
5070 goto err_exit;
5071 }
5072 phba->sli4_hba.xri_ids = kzalloc(rsrc_id_cnt *
5073 sizeof(uint16_t),
5074 GFP_KERNEL);
5075 if (unlikely(!phba->sli4_hba.xri_ids)) {
5076 kfree(phba->sli4_hba.xri_bmask);
5077 rc = -ENOMEM;
5078 goto err_exit;
5079 }
5080
5081 /* Initialize local ptrs for common extent processing later. */
5082 bmask = phba->sli4_hba.xri_bmask;
5083 ids = phba->sli4_hba.xri_ids;
5084 ext_blk_list = &phba->sli4_hba.lpfc_xri_blk_list;
5085 break;
5086 case LPFC_RSC_TYPE_FCOE_VFI:
5087 phba->sli4_hba.vfi_bmask = kzalloc(longs *
5088 sizeof(unsigned long),
5089 GFP_KERNEL);
5090 if (unlikely(!phba->sli4_hba.vfi_bmask)) {
5091 rc = -ENOMEM;
5092 goto err_exit;
5093 }
5094 phba->sli4_hba.vfi_ids = kzalloc(rsrc_id_cnt *
5095 sizeof(uint16_t),
5096 GFP_KERNEL);
5097 if (unlikely(!phba->sli4_hba.vfi_ids)) {
5098 kfree(phba->sli4_hba.vfi_bmask);
5099 rc = -ENOMEM;
5100 goto err_exit;
5101 }
5102
5103 /* Initialize local ptrs for common extent processing later. */
5104 bmask = phba->sli4_hba.vfi_bmask;
5105 ids = phba->sli4_hba.vfi_ids;
5106 ext_blk_list = &phba->sli4_hba.lpfc_vfi_blk_list;
5107 break;
5108 default:
5109 /* Unsupported Opcode. Fail call. */
5110 id_array = NULL;
5111 bmask = NULL;
5112 ids = NULL;
5113 ext_blk_list = NULL;
5114 goto err_exit;
5115 }
5116
5117 /*
5118 * Complete initializing the extent configuration with the
5119 * allocated ids assigned to this function. The bitmask serves
5120 * as an index into the array and manages the available ids. The
5121 * array just stores the ids communicated to the port via the wqes.
5122 */
5123 for (i = 0, j = 0, k = 0; i < rsrc_cnt; i++) {
5124 if ((i % 2) == 0)
5125 rsrc_id = bf_get(lpfc_mbx_rsrc_id_word4_0,
5126 &id_array[k]);
5127 else
5128 rsrc_id = bf_get(lpfc_mbx_rsrc_id_word4_1,
5129 &id_array[k]);
5130
5131 rsrc_blks = kzalloc(length, GFP_KERNEL);
5132 if (unlikely(!rsrc_blks)) {
5133 rc = -ENOMEM;
5134 kfree(bmask);
5135 kfree(ids);
5136 goto err_exit;
5137 }
5138 rsrc_blks->rsrc_start = rsrc_id;
5139 rsrc_blks->rsrc_size = rsrc_size;
5140 list_add_tail(&rsrc_blks->list, ext_blk_list);
5141 rsrc_start = rsrc_id;
5142 if ((type == LPFC_RSC_TYPE_FCOE_XRI) && (j == 0))
5143 phba->sli4_hba.scsi_xri_start = rsrc_start +
5144 lpfc_sli4_get_els_iocb_cnt(phba);
5145
5146 while (rsrc_id < (rsrc_start + rsrc_size)) {
5147 ids[j] = rsrc_id;
5148 rsrc_id++;
5149 j++;
5150 }
5151 /* Entire word processed. Get next word.*/
5152 if ((i % 2) == 1)
5153 k++;
5154 }
5155 err_exit:
5156 lpfc_sli4_mbox_cmd_free(phba, mbox);
5157 return rc;
5158}
5159
5160/**
5161 * lpfc_sli4_dealloc_extent - Deallocate an SLI4 resource extent.
5162 * @phba: Pointer to HBA context object.
5163 * @type: the extent's type.
5164 *
5165 * This function deallocates all extents of a particular resource type.
5166 * SLI4 does not allow for deallocating a particular extent range. It
5167 * is the caller's responsibility to release all kernel memory resources.
5168 **/
5169static int
5170lpfc_sli4_dealloc_extent(struct lpfc_hba *phba, uint16_t type)
5171{
5172 int rc;
5173 uint32_t length, mbox_tmo = 0;
5174 LPFC_MBOXQ_t *mbox;
5175 struct lpfc_mbx_dealloc_rsrc_extents *dealloc_rsrc;
5176 struct lpfc_rsrc_blks *rsrc_blk, *rsrc_blk_next;
5177
5178 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5179 if (!mbox)
5180 return -ENOMEM;
5181
5182 /*
5183 * This function sends an embedded mailbox because it only sends the
5184 * the resource type. All extents of this type are released by the
5185 * port.
5186 */
5187 length = (sizeof(struct lpfc_mbx_dealloc_rsrc_extents) -
5188 sizeof(struct lpfc_sli4_cfg_mhdr));
5189 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5190 LPFC_MBOX_OPCODE_DEALLOC_RSRC_EXTENT,
5191 length, LPFC_SLI4_MBX_EMBED);
5192
5193 /* Send an extents count of 0 - the dealloc doesn't use it. */
5194 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, 0, type,
5195 LPFC_SLI4_MBX_EMBED);
5196 if (unlikely(rc)) {
5197 rc = -EIO;
5198 goto out_free_mbox;
5199 }
5200 if (!phba->sli4_hba.intr_enable)
5201 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5202 else {
James Smarta183a152011-10-10 21:32:43 -04005203 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart6d368e52011-05-24 11:44:12 -04005204 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5205 }
5206 if (unlikely(rc)) {
5207 rc = -EIO;
5208 goto out_free_mbox;
5209 }
5210
5211 dealloc_rsrc = &mbox->u.mqe.un.dealloc_rsrc_extents;
5212 if (bf_get(lpfc_mbox_hdr_status,
5213 &dealloc_rsrc->header.cfg_shdr.response)) {
5214 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
5215 "2919 Failed to release resource extents "
5216 "for type %d - Status 0x%x Add'l Status 0x%x. "
5217 "Resource memory not released.\n",
5218 type,
5219 bf_get(lpfc_mbox_hdr_status,
5220 &dealloc_rsrc->header.cfg_shdr.response),
5221 bf_get(lpfc_mbox_hdr_add_status,
5222 &dealloc_rsrc->header.cfg_shdr.response));
5223 rc = -EIO;
5224 goto out_free_mbox;
5225 }
5226
5227 /* Release kernel memory resources for the specific type. */
5228 switch (type) {
5229 case LPFC_RSC_TYPE_FCOE_VPI:
5230 kfree(phba->vpi_bmask);
5231 kfree(phba->vpi_ids);
5232 bf_set(lpfc_vpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5233 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5234 &phba->lpfc_vpi_blk_list, list) {
5235 list_del_init(&rsrc_blk->list);
5236 kfree(rsrc_blk);
5237 }
5238 break;
5239 case LPFC_RSC_TYPE_FCOE_XRI:
5240 kfree(phba->sli4_hba.xri_bmask);
5241 kfree(phba->sli4_hba.xri_ids);
5242 bf_set(lpfc_xri_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5243 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5244 &phba->sli4_hba.lpfc_xri_blk_list, list) {
5245 list_del_init(&rsrc_blk->list);
5246 kfree(rsrc_blk);
5247 }
5248 break;
5249 case LPFC_RSC_TYPE_FCOE_VFI:
5250 kfree(phba->sli4_hba.vfi_bmask);
5251 kfree(phba->sli4_hba.vfi_ids);
5252 bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5253 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5254 &phba->sli4_hba.lpfc_vfi_blk_list, list) {
5255 list_del_init(&rsrc_blk->list);
5256 kfree(rsrc_blk);
5257 }
5258 break;
5259 case LPFC_RSC_TYPE_FCOE_RPI:
5260 /* RPI bitmask and physical id array are cleaned up earlier. */
5261 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5262 &phba->sli4_hba.lpfc_rpi_blk_list, list) {
5263 list_del_init(&rsrc_blk->list);
5264 kfree(rsrc_blk);
5265 }
5266 break;
5267 default:
5268 break;
5269 }
5270
5271 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5272
5273 out_free_mbox:
5274 mempool_free(mbox, phba->mbox_mem_pool);
5275 return rc;
5276}
5277
5278/**
5279 * lpfc_sli4_alloc_resource_identifiers - Allocate all SLI4 resource extents.
5280 * @phba: Pointer to HBA context object.
5281 *
5282 * This function allocates all SLI4 resource identifiers.
5283 **/
5284int
5285lpfc_sli4_alloc_resource_identifiers(struct lpfc_hba *phba)
5286{
5287 int i, rc, error = 0;
5288 uint16_t count, base;
5289 unsigned long longs;
5290
5291 if (phba->sli4_hba.extents_in_use) {
5292 /*
5293 * The port supports resource extents. The XRI, VPI, VFI, RPI
5294 * resource extent count must be read and allocated before
5295 * provisioning the resource id arrays.
5296 */
5297 if (bf_get(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags) ==
5298 LPFC_IDX_RSRC_RDY) {
5299 /*
5300 * Extent-based resources are set - the driver could
5301 * be in a port reset. Figure out if any corrective
5302 * actions need to be taken.
5303 */
5304 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
5305 LPFC_RSC_TYPE_FCOE_VFI);
5306 if (rc != 0)
5307 error++;
5308 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
5309 LPFC_RSC_TYPE_FCOE_VPI);
5310 if (rc != 0)
5311 error++;
5312 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
5313 LPFC_RSC_TYPE_FCOE_XRI);
5314 if (rc != 0)
5315 error++;
5316 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
5317 LPFC_RSC_TYPE_FCOE_RPI);
5318 if (rc != 0)
5319 error++;
5320
5321 /*
5322 * It's possible that the number of resources
5323 * provided to this port instance changed between
5324 * resets. Detect this condition and reallocate
5325 * resources. Otherwise, there is no action.
5326 */
5327 if (error) {
5328 lpfc_printf_log(phba, KERN_INFO,
5329 LOG_MBOX | LOG_INIT,
5330 "2931 Detected extent resource "
5331 "change. Reallocating all "
5332 "extents.\n");
5333 rc = lpfc_sli4_dealloc_extent(phba,
5334 LPFC_RSC_TYPE_FCOE_VFI);
5335 rc = lpfc_sli4_dealloc_extent(phba,
5336 LPFC_RSC_TYPE_FCOE_VPI);
5337 rc = lpfc_sli4_dealloc_extent(phba,
5338 LPFC_RSC_TYPE_FCOE_XRI);
5339 rc = lpfc_sli4_dealloc_extent(phba,
5340 LPFC_RSC_TYPE_FCOE_RPI);
5341 } else
5342 return 0;
5343 }
5344
5345 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_VFI);
5346 if (unlikely(rc))
5347 goto err_exit;
5348
5349 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_VPI);
5350 if (unlikely(rc))
5351 goto err_exit;
5352
5353 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_RPI);
5354 if (unlikely(rc))
5355 goto err_exit;
5356
5357 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_XRI);
5358 if (unlikely(rc))
5359 goto err_exit;
5360 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags,
5361 LPFC_IDX_RSRC_RDY);
5362 return rc;
5363 } else {
5364 /*
5365 * The port does not support resource extents. The XRI, VPI,
5366 * VFI, RPI resource ids were determined from READ_CONFIG.
5367 * Just allocate the bitmasks and provision the resource id
5368 * arrays. If a port reset is active, the resources don't
5369 * need any action - just exit.
5370 */
5371 if (bf_get(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags) ==
5372 LPFC_IDX_RSRC_RDY)
5373 return 0;
5374
5375 /* RPIs. */
5376 count = phba->sli4_hba.max_cfg_param.max_rpi;
5377 base = phba->sli4_hba.max_cfg_param.rpi_base;
5378 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
5379 phba->sli4_hba.rpi_bmask = kzalloc(longs *
5380 sizeof(unsigned long),
5381 GFP_KERNEL);
5382 if (unlikely(!phba->sli4_hba.rpi_bmask)) {
5383 rc = -ENOMEM;
5384 goto err_exit;
5385 }
5386 phba->sli4_hba.rpi_ids = kzalloc(count *
5387 sizeof(uint16_t),
5388 GFP_KERNEL);
5389 if (unlikely(!phba->sli4_hba.rpi_ids)) {
5390 rc = -ENOMEM;
5391 goto free_rpi_bmask;
5392 }
5393
5394 for (i = 0; i < count; i++)
5395 phba->sli4_hba.rpi_ids[i] = base + i;
5396
5397 /* VPIs. */
5398 count = phba->sli4_hba.max_cfg_param.max_vpi;
5399 base = phba->sli4_hba.max_cfg_param.vpi_base;
5400 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
5401 phba->vpi_bmask = kzalloc(longs *
5402 sizeof(unsigned long),
5403 GFP_KERNEL);
5404 if (unlikely(!phba->vpi_bmask)) {
5405 rc = -ENOMEM;
5406 goto free_rpi_ids;
5407 }
5408 phba->vpi_ids = kzalloc(count *
5409 sizeof(uint16_t),
5410 GFP_KERNEL);
5411 if (unlikely(!phba->vpi_ids)) {
5412 rc = -ENOMEM;
5413 goto free_vpi_bmask;
5414 }
5415
5416 for (i = 0; i < count; i++)
5417 phba->vpi_ids[i] = base + i;
5418
5419 /* XRIs. */
5420 count = phba->sli4_hba.max_cfg_param.max_xri;
5421 base = phba->sli4_hba.max_cfg_param.xri_base;
5422 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
5423 phba->sli4_hba.xri_bmask = kzalloc(longs *
5424 sizeof(unsigned long),
5425 GFP_KERNEL);
5426 if (unlikely(!phba->sli4_hba.xri_bmask)) {
5427 rc = -ENOMEM;
5428 goto free_vpi_ids;
5429 }
5430 phba->sli4_hba.xri_ids = kzalloc(count *
5431 sizeof(uint16_t),
5432 GFP_KERNEL);
5433 if (unlikely(!phba->sli4_hba.xri_ids)) {
5434 rc = -ENOMEM;
5435 goto free_xri_bmask;
5436 }
5437
5438 for (i = 0; i < count; i++)
5439 phba->sli4_hba.xri_ids[i] = base + i;
5440
5441 /* VFIs. */
5442 count = phba->sli4_hba.max_cfg_param.max_vfi;
5443 base = phba->sli4_hba.max_cfg_param.vfi_base;
5444 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
5445 phba->sli4_hba.vfi_bmask = kzalloc(longs *
5446 sizeof(unsigned long),
5447 GFP_KERNEL);
5448 if (unlikely(!phba->sli4_hba.vfi_bmask)) {
5449 rc = -ENOMEM;
5450 goto free_xri_ids;
5451 }
5452 phba->sli4_hba.vfi_ids = kzalloc(count *
5453 sizeof(uint16_t),
5454 GFP_KERNEL);
5455 if (unlikely(!phba->sli4_hba.vfi_ids)) {
5456 rc = -ENOMEM;
5457 goto free_vfi_bmask;
5458 }
5459
5460 for (i = 0; i < count; i++)
5461 phba->sli4_hba.vfi_ids[i] = base + i;
5462
5463 /*
5464 * Mark all resources ready. An HBA reset doesn't need
5465 * to reset the initialization.
5466 */
5467 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags,
5468 LPFC_IDX_RSRC_RDY);
5469 return 0;
5470 }
5471
5472 free_vfi_bmask:
5473 kfree(phba->sli4_hba.vfi_bmask);
5474 free_xri_ids:
5475 kfree(phba->sli4_hba.xri_ids);
5476 free_xri_bmask:
5477 kfree(phba->sli4_hba.xri_bmask);
5478 free_vpi_ids:
5479 kfree(phba->vpi_ids);
5480 free_vpi_bmask:
5481 kfree(phba->vpi_bmask);
5482 free_rpi_ids:
5483 kfree(phba->sli4_hba.rpi_ids);
5484 free_rpi_bmask:
5485 kfree(phba->sli4_hba.rpi_bmask);
5486 err_exit:
5487 return rc;
5488}
5489
5490/**
5491 * lpfc_sli4_dealloc_resource_identifiers - Deallocate all SLI4 resource extents.
5492 * @phba: Pointer to HBA context object.
5493 *
5494 * This function allocates the number of elements for the specified
5495 * resource type.
5496 **/
5497int
5498lpfc_sli4_dealloc_resource_identifiers(struct lpfc_hba *phba)
5499{
5500 if (phba->sli4_hba.extents_in_use) {
5501 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VPI);
5502 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_RPI);
5503 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_XRI);
5504 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VFI);
5505 } else {
5506 kfree(phba->vpi_bmask);
5507 kfree(phba->vpi_ids);
5508 bf_set(lpfc_vpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5509 kfree(phba->sli4_hba.xri_bmask);
5510 kfree(phba->sli4_hba.xri_ids);
5511 bf_set(lpfc_xri_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5512 kfree(phba->sli4_hba.vfi_bmask);
5513 kfree(phba->sli4_hba.vfi_ids);
5514 bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5515 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5516 }
5517
5518 return 0;
5519}
5520
5521/**
James Smartb76f2dc2011-07-22 18:37:42 -04005522 * lpfc_sli4_get_allocated_extnts - Get the port's allocated extents.
5523 * @phba: Pointer to HBA context object.
5524 * @type: The resource extent type.
5525 * @extnt_count: buffer to hold port extent count response
5526 * @extnt_size: buffer to hold port extent size response.
5527 *
5528 * This function calls the port to read the host allocated extents
5529 * for a particular type.
5530 **/
5531int
5532lpfc_sli4_get_allocated_extnts(struct lpfc_hba *phba, uint16_t type,
5533 uint16_t *extnt_cnt, uint16_t *extnt_size)
5534{
5535 bool emb;
5536 int rc = 0;
5537 uint16_t curr_blks = 0;
5538 uint32_t req_len, emb_len;
5539 uint32_t alloc_len, mbox_tmo;
5540 struct list_head *blk_list_head;
5541 struct lpfc_rsrc_blks *rsrc_blk;
5542 LPFC_MBOXQ_t *mbox;
5543 void *virtaddr = NULL;
5544 struct lpfc_mbx_nembed_rsrc_extent *n_rsrc;
5545 struct lpfc_mbx_alloc_rsrc_extents *rsrc_ext;
5546 union lpfc_sli4_cfg_shdr *shdr;
5547
5548 switch (type) {
5549 case LPFC_RSC_TYPE_FCOE_VPI:
5550 blk_list_head = &phba->lpfc_vpi_blk_list;
5551 break;
5552 case LPFC_RSC_TYPE_FCOE_XRI:
5553 blk_list_head = &phba->sli4_hba.lpfc_xri_blk_list;
5554 break;
5555 case LPFC_RSC_TYPE_FCOE_VFI:
5556 blk_list_head = &phba->sli4_hba.lpfc_vfi_blk_list;
5557 break;
5558 case LPFC_RSC_TYPE_FCOE_RPI:
5559 blk_list_head = &phba->sli4_hba.lpfc_rpi_blk_list;
5560 break;
5561 default:
5562 return -EIO;
5563 }
5564
5565 /* Count the number of extents currently allocatd for this type. */
5566 list_for_each_entry(rsrc_blk, blk_list_head, list) {
5567 if (curr_blks == 0) {
5568 /*
5569 * The GET_ALLOCATED mailbox does not return the size,
5570 * just the count. The size should be just the size
5571 * stored in the current allocated block and all sizes
5572 * for an extent type are the same so set the return
5573 * value now.
5574 */
5575 *extnt_size = rsrc_blk->rsrc_size;
5576 }
5577 curr_blks++;
5578 }
5579
5580 /* Calculate the total requested length of the dma memory. */
5581 req_len = curr_blks * sizeof(uint16_t);
5582
5583 /*
5584 * Calculate the size of an embedded mailbox. The uint32_t
5585 * accounts for extents-specific word.
5586 */
5587 emb_len = sizeof(MAILBOX_t) - sizeof(struct mbox_header) -
5588 sizeof(uint32_t);
5589
5590 /*
5591 * Presume the allocation and response will fit into an embedded
5592 * mailbox. If not true, reconfigure to a non-embedded mailbox.
5593 */
5594 emb = LPFC_SLI4_MBX_EMBED;
5595 req_len = emb_len;
5596 if (req_len > emb_len) {
5597 req_len = curr_blks * sizeof(uint16_t) +
5598 sizeof(union lpfc_sli4_cfg_shdr) +
5599 sizeof(uint32_t);
5600 emb = LPFC_SLI4_MBX_NEMBED;
5601 }
5602
5603 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5604 if (!mbox)
5605 return -ENOMEM;
5606 memset(mbox, 0, sizeof(LPFC_MBOXQ_t));
5607
5608 alloc_len = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5609 LPFC_MBOX_OPCODE_GET_ALLOC_RSRC_EXTENT,
5610 req_len, emb);
5611 if (alloc_len < req_len) {
5612 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5613 "2983 Allocated DMA memory size (x%x) is "
5614 "less than the requested DMA memory "
5615 "size (x%x)\n", alloc_len, req_len);
5616 rc = -ENOMEM;
5617 goto err_exit;
5618 }
5619 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, curr_blks, type, emb);
5620 if (unlikely(rc)) {
5621 rc = -EIO;
5622 goto err_exit;
5623 }
5624
5625 if (!phba->sli4_hba.intr_enable)
5626 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5627 else {
James Smarta183a152011-10-10 21:32:43 -04005628 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smartb76f2dc2011-07-22 18:37:42 -04005629 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5630 }
5631
5632 if (unlikely(rc)) {
5633 rc = -EIO;
5634 goto err_exit;
5635 }
5636
5637 /*
5638 * Figure out where the response is located. Then get local pointers
5639 * to the response data. The port does not guarantee to respond to
5640 * all extents counts request so update the local variable with the
5641 * allocated count from the port.
5642 */
5643 if (emb == LPFC_SLI4_MBX_EMBED) {
5644 rsrc_ext = &mbox->u.mqe.un.alloc_rsrc_extents;
5645 shdr = &rsrc_ext->header.cfg_shdr;
5646 *extnt_cnt = bf_get(lpfc_mbx_rsrc_cnt, &rsrc_ext->u.rsp);
5647 } else {
5648 virtaddr = mbox->sge_array->addr[0];
5649 n_rsrc = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
5650 shdr = &n_rsrc->cfg_shdr;
5651 *extnt_cnt = bf_get(lpfc_mbx_rsrc_cnt, n_rsrc);
5652 }
5653
5654 if (bf_get(lpfc_mbox_hdr_status, &shdr->response)) {
5655 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
5656 "2984 Failed to read allocated resources "
5657 "for type %d - Status 0x%x Add'l Status 0x%x.\n",
5658 type,
5659 bf_get(lpfc_mbox_hdr_status, &shdr->response),
5660 bf_get(lpfc_mbox_hdr_add_status, &shdr->response));
5661 rc = -EIO;
5662 goto err_exit;
5663 }
5664 err_exit:
5665 lpfc_sli4_mbox_cmd_free(phba, mbox);
5666 return rc;
5667}
5668
5669/**
James Smartda0436e2009-05-22 14:51:39 -04005670 * lpfc_sli4_hba_setup - SLI4 device intialization PCI function
5671 * @phba: Pointer to HBA context object.
5672 *
5673 * This function is the main SLI4 device intialization PCI function. This
5674 * function is called by the HBA intialization code, HBA reset code and
5675 * HBA error attention handler code. Caller is not required to hold any
5676 * locks.
5677 **/
5678int
5679lpfc_sli4_hba_setup(struct lpfc_hba *phba)
5680{
5681 int rc;
5682 LPFC_MBOXQ_t *mboxq;
5683 struct lpfc_mqe *mqe;
5684 uint8_t *vpd;
5685 uint32_t vpd_size;
5686 uint32_t ftr_rsp = 0;
5687 struct Scsi_Host *shost = lpfc_shost_from_vport(phba->pport);
5688 struct lpfc_vport *vport = phba->pport;
5689 struct lpfc_dmabuf *mp;
5690
5691 /* Perform a PCI function reset to start from clean */
5692 rc = lpfc_pci_function_reset(phba);
5693 if (unlikely(rc))
5694 return -ENODEV;
5695
5696 /* Check the HBA Host Status Register for readyness */
5697 rc = lpfc_sli4_post_status_check(phba);
5698 if (unlikely(rc))
5699 return -ENODEV;
5700 else {
5701 spin_lock_irq(&phba->hbalock);
5702 phba->sli.sli_flag |= LPFC_SLI_ACTIVE;
5703 spin_unlock_irq(&phba->hbalock);
5704 }
5705
5706 /*
5707 * Allocate a single mailbox container for initializing the
5708 * port.
5709 */
5710 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5711 if (!mboxq)
5712 return -ENOMEM;
5713
5714 /*
5715 * Continue initialization with default values even if driver failed
5716 * to read FCoE param config regions
5717 */
5718 if (lpfc_sli4_read_fcoe_params(phba, mboxq))
James Smart05580562011-05-24 11:40:48 -04005719 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_INIT,
James Smarte4e74272009-07-19 10:01:38 -04005720 "2570 Failed to read FCoE parameters\n");
James Smartda0436e2009-05-22 14:51:39 -04005721
5722 /* Issue READ_REV to collect vpd and FW information. */
James Smart49198b32010-04-06 15:04:33 -04005723 vpd_size = SLI4_PAGE_SIZE;
James Smartda0436e2009-05-22 14:51:39 -04005724 vpd = kzalloc(vpd_size, GFP_KERNEL);
5725 if (!vpd) {
5726 rc = -ENOMEM;
5727 goto out_free_mbox;
5728 }
5729
5730 rc = lpfc_sli4_read_rev(phba, mboxq, vpd, &vpd_size);
James Smart76a95d72010-11-20 23:11:48 -05005731 if (unlikely(rc)) {
5732 kfree(vpd);
5733 goto out_free_mbox;
5734 }
James Smartda0436e2009-05-22 14:51:39 -04005735 mqe = &mboxq->u.mqe;
James Smartf1126682009-06-10 17:22:44 -04005736 phba->sli_rev = bf_get(lpfc_mbx_rd_rev_sli_lvl, &mqe->un.read_rev);
5737 if (bf_get(lpfc_mbx_rd_rev_fcoe, &mqe->un.read_rev))
James Smart76a95d72010-11-20 23:11:48 -05005738 phba->hba_flag |= HBA_FCOE_MODE;
5739 else
5740 phba->hba_flag &= ~HBA_FCOE_MODE;
James Smart45ed1192009-10-02 15:17:02 -04005741
5742 if (bf_get(lpfc_mbx_rd_rev_cee_ver, &mqe->un.read_rev) ==
5743 LPFC_DCBX_CEE_MODE)
5744 phba->hba_flag |= HBA_FIP_SUPPORT;
5745 else
5746 phba->hba_flag &= ~HBA_FIP_SUPPORT;
5747
James Smartc31098c2011-04-16 11:03:33 -04005748 if (phba->sli_rev != LPFC_SLI_REV4) {
James Smartda0436e2009-05-22 14:51:39 -04005749 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5750 "0376 READ_REV Error. SLI Level %d "
5751 "FCoE enabled %d\n",
James Smart76a95d72010-11-20 23:11:48 -05005752 phba->sli_rev, phba->hba_flag & HBA_FCOE_MODE);
James Smartda0436e2009-05-22 14:51:39 -04005753 rc = -EIO;
James Smart76a95d72010-11-20 23:11:48 -05005754 kfree(vpd);
5755 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04005756 }
James Smartda0436e2009-05-22 14:51:39 -04005757 /*
5758 * Evaluate the read rev and vpd data. Populate the driver
5759 * state with the results. If this routine fails, the failure
5760 * is not fatal as the driver will use generic values.
5761 */
5762 rc = lpfc_parse_vpd(phba, vpd, vpd_size);
5763 if (unlikely(!rc)) {
5764 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5765 "0377 Error %d parsing vpd. "
5766 "Using defaults.\n", rc);
5767 rc = 0;
5768 }
James Smart76a95d72010-11-20 23:11:48 -05005769 kfree(vpd);
James Smartda0436e2009-05-22 14:51:39 -04005770
James Smartf1126682009-06-10 17:22:44 -04005771 /* Save information as VPD data */
5772 phba->vpd.rev.biuRev = mqe->un.read_rev.first_hw_rev;
5773 phba->vpd.rev.smRev = mqe->un.read_rev.second_hw_rev;
5774 phba->vpd.rev.endecRev = mqe->un.read_rev.third_hw_rev;
5775 phba->vpd.rev.fcphHigh = bf_get(lpfc_mbx_rd_rev_fcph_high,
5776 &mqe->un.read_rev);
5777 phba->vpd.rev.fcphLow = bf_get(lpfc_mbx_rd_rev_fcph_low,
5778 &mqe->un.read_rev);
5779 phba->vpd.rev.feaLevelHigh = bf_get(lpfc_mbx_rd_rev_ftr_lvl_high,
5780 &mqe->un.read_rev);
5781 phba->vpd.rev.feaLevelLow = bf_get(lpfc_mbx_rd_rev_ftr_lvl_low,
5782 &mqe->un.read_rev);
5783 phba->vpd.rev.sli1FwRev = mqe->un.read_rev.fw_id_rev;
5784 memcpy(phba->vpd.rev.sli1FwName, mqe->un.read_rev.fw_name, 16);
5785 phba->vpd.rev.sli2FwRev = mqe->un.read_rev.ulp_fw_id_rev;
5786 memcpy(phba->vpd.rev.sli2FwName, mqe->un.read_rev.ulp_fw_name, 16);
5787 phba->vpd.rev.opFwRev = mqe->un.read_rev.fw_id_rev;
5788 memcpy(phba->vpd.rev.opFwName, mqe->un.read_rev.fw_name, 16);
5789 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
5790 "(%d):0380 READ_REV Status x%x "
5791 "fw_rev:%s fcphHi:%x fcphLo:%x flHi:%x flLo:%x\n",
5792 mboxq->vport ? mboxq->vport->vpi : 0,
5793 bf_get(lpfc_mqe_status, mqe),
5794 phba->vpd.rev.opFwName,
5795 phba->vpd.rev.fcphHigh, phba->vpd.rev.fcphLow,
5796 phba->vpd.rev.feaLevelHigh, phba->vpd.rev.feaLevelLow);
James Smartda0436e2009-05-22 14:51:39 -04005797
5798 /*
5799 * Discover the port's supported feature set and match it against the
5800 * hosts requests.
5801 */
5802 lpfc_request_features(phba, mboxq);
5803 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5804 if (unlikely(rc)) {
5805 rc = -EIO;
James Smart76a95d72010-11-20 23:11:48 -05005806 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04005807 }
5808
5809 /*
5810 * The port must support FCP initiator mode as this is the
5811 * only mode running in the host.
5812 */
5813 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_fcpi, &mqe->un.req_ftrs))) {
5814 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
5815 "0378 No support for fcpi mode.\n");
5816 ftr_rsp++;
5817 }
James Smartfedd3b72011-02-16 12:39:24 -05005818 if (bf_get(lpfc_mbx_rq_ftr_rsp_perfh, &mqe->un.req_ftrs))
5819 phba->sli3_options |= LPFC_SLI4_PERFH_ENABLED;
5820 else
5821 phba->sli3_options &= ~LPFC_SLI4_PERFH_ENABLED;
James Smartda0436e2009-05-22 14:51:39 -04005822 /*
5823 * If the port cannot support the host's requested features
5824 * then turn off the global config parameters to disable the
5825 * feature in the driver. This is not a fatal error.
5826 */
James Smartbf086112011-08-21 21:48:13 -04005827 phba->sli3_options &= ~LPFC_SLI3_BG_ENABLED;
5828 if (phba->cfg_enable_bg) {
5829 if (bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs))
5830 phba->sli3_options |= LPFC_SLI3_BG_ENABLED;
5831 else
5832 ftr_rsp++;
5833 }
James Smartda0436e2009-05-22 14:51:39 -04005834
5835 if (phba->max_vpi && phba->cfg_enable_npiv &&
5836 !(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
5837 ftr_rsp++;
5838
5839 if (ftr_rsp) {
5840 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
5841 "0379 Feature Mismatch Data: x%08x %08x "
5842 "x%x x%x x%x\n", mqe->un.req_ftrs.word2,
5843 mqe->un.req_ftrs.word3, phba->cfg_enable_bg,
5844 phba->cfg_enable_npiv, phba->max_vpi);
5845 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs)))
5846 phba->cfg_enable_bg = 0;
5847 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
5848 phba->cfg_enable_npiv = 0;
5849 }
5850
5851 /* These SLI3 features are assumed in SLI4 */
5852 spin_lock_irq(&phba->hbalock);
5853 phba->sli3_options |= (LPFC_SLI3_NPIV_ENABLED | LPFC_SLI3_HBQ_ENABLED);
5854 spin_unlock_irq(&phba->hbalock);
5855
James Smart6d368e52011-05-24 11:44:12 -04005856 /*
5857 * Allocate all resources (xri,rpi,vpi,vfi) now. Subsequent
5858 * calls depends on these resources to complete port setup.
5859 */
5860 rc = lpfc_sli4_alloc_resource_identifiers(phba);
5861 if (rc) {
5862 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5863 "2920 Failed to alloc Resource IDs "
5864 "rc = x%x\n", rc);
5865 goto out_free_mbox;
5866 }
5867
James Smartda0436e2009-05-22 14:51:39 -04005868 /* Read the port's service parameters. */
James Smart9f1177a2010-02-26 14:12:57 -05005869 rc = lpfc_read_sparam(phba, mboxq, vport->vpi);
5870 if (rc) {
5871 phba->link_state = LPFC_HBA_ERROR;
5872 rc = -ENOMEM;
James Smart76a95d72010-11-20 23:11:48 -05005873 goto out_free_mbox;
James Smart9f1177a2010-02-26 14:12:57 -05005874 }
5875
James Smartda0436e2009-05-22 14:51:39 -04005876 mboxq->vport = vport;
5877 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5878 mp = (struct lpfc_dmabuf *) mboxq->context1;
5879 if (rc == MBX_SUCCESS) {
5880 memcpy(&vport->fc_sparam, mp->virt, sizeof(struct serv_parm));
5881 rc = 0;
5882 }
5883
5884 /*
5885 * This memory was allocated by the lpfc_read_sparam routine. Release
5886 * it to the mbuf pool.
5887 */
5888 lpfc_mbuf_free(phba, mp->virt, mp->phys);
5889 kfree(mp);
5890 mboxq->context1 = NULL;
5891 if (unlikely(rc)) {
5892 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5893 "0382 READ_SPARAM command failed "
5894 "status %d, mbxStatus x%x\n",
5895 rc, bf_get(lpfc_mqe_status, mqe));
5896 phba->link_state = LPFC_HBA_ERROR;
5897 rc = -EIO;
James Smart76a95d72010-11-20 23:11:48 -05005898 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04005899 }
5900
James Smart05580562011-05-24 11:40:48 -04005901 lpfc_update_vport_wwn(vport);
James Smartda0436e2009-05-22 14:51:39 -04005902
5903 /* Update the fc_host data structures with new wwn. */
5904 fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
5905 fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
5906
5907 /* Register SGL pool to the device using non-embedded mailbox command */
James Smart6d368e52011-05-24 11:44:12 -04005908 if (!phba->sli4_hba.extents_in_use) {
5909 rc = lpfc_sli4_post_els_sgl_list(phba);
5910 if (unlikely(rc)) {
5911 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5912 "0582 Error %d during els sgl post "
5913 "operation\n", rc);
5914 rc = -ENODEV;
5915 goto out_free_mbox;
5916 }
5917 } else {
5918 rc = lpfc_sli4_post_els_sgl_list_ext(phba);
5919 if (unlikely(rc)) {
5920 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5921 "2560 Error %d during els sgl post "
5922 "operation\n", rc);
5923 rc = -ENODEV;
5924 goto out_free_mbox;
5925 }
James Smartda0436e2009-05-22 14:51:39 -04005926 }
5927
5928 /* Register SCSI SGL pool to the device */
5929 rc = lpfc_sli4_repost_scsi_sgl_list(phba);
5930 if (unlikely(rc)) {
James Smart6d368e52011-05-24 11:44:12 -04005931 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smart6a9c52c2009-10-02 15:16:51 -04005932 "0383 Error %d during scsi sgl post "
5933 "operation\n", rc);
James Smartda0436e2009-05-22 14:51:39 -04005934 /* Some Scsi buffers were moved to the abort scsi list */
5935 /* A pci function reset will repost them */
5936 rc = -ENODEV;
James Smart76a95d72010-11-20 23:11:48 -05005937 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04005938 }
5939
5940 /* Post the rpi header region to the device. */
5941 rc = lpfc_sli4_post_all_rpi_hdrs(phba);
5942 if (unlikely(rc)) {
5943 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5944 "0393 Error %d during rpi post operation\n",
5945 rc);
5946 rc = -ENODEV;
James Smart76a95d72010-11-20 23:11:48 -05005947 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04005948 }
James Smartda0436e2009-05-22 14:51:39 -04005949
5950 /* Set up all the queues to the device */
5951 rc = lpfc_sli4_queue_setup(phba);
5952 if (unlikely(rc)) {
5953 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5954 "0381 Error %d during queue setup.\n ", rc);
5955 goto out_stop_timers;
5956 }
5957
5958 /* Arm the CQs and then EQs on device */
5959 lpfc_sli4_arm_cqeq_intr(phba);
5960
5961 /* Indicate device interrupt mode */
5962 phba->sli4_hba.intr_enable = 1;
5963
5964 /* Allow asynchronous mailbox command to go through */
5965 spin_lock_irq(&phba->hbalock);
5966 phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
5967 spin_unlock_irq(&phba->hbalock);
5968
5969 /* Post receive buffers to the device */
5970 lpfc_sli4_rb_setup(phba);
5971
James Smartfc2b9892010-02-26 14:15:29 -05005972 /* Reset HBA FCF states after HBA reset */
5973 phba->fcf.fcf_flag = 0;
5974 phba->fcf.current_rec.flag = 0;
5975
James Smartda0436e2009-05-22 14:51:39 -04005976 /* Start the ELS watchdog timer */
James Smart8fa38512009-07-19 10:01:03 -04005977 mod_timer(&vport->els_tmofunc,
5978 jiffies + HZ * (phba->fc_ratov * 2));
James Smartda0436e2009-05-22 14:51:39 -04005979
5980 /* Start heart beat timer */
5981 mod_timer(&phba->hb_tmofunc,
5982 jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
5983 phba->hb_outstanding = 0;
5984 phba->last_completion_time = jiffies;
5985
5986 /* Start error attention (ERATT) polling timer */
5987 mod_timer(&phba->eratt_poll, jiffies + HZ * LPFC_ERATT_POLL_INTERVAL);
5988
James Smart75baf692010-06-08 18:31:21 -04005989 /* Enable PCIe device Advanced Error Reporting (AER) if configured */
5990 if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
5991 rc = pci_enable_pcie_error_reporting(phba->pcidev);
5992 if (!rc) {
5993 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5994 "2829 This device supports "
5995 "Advanced Error Reporting (AER)\n");
5996 spin_lock_irq(&phba->hbalock);
5997 phba->hba_flag |= HBA_AER_ENABLED;
5998 spin_unlock_irq(&phba->hbalock);
5999 } else {
6000 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6001 "2830 This device does not support "
6002 "Advanced Error Reporting (AER)\n");
6003 phba->cfg_aer_support = 0;
6004 }
James Smart0a96e972011-07-22 18:37:28 -04006005 rc = 0;
James Smart75baf692010-06-08 18:31:21 -04006006 }
6007
James Smart76a95d72010-11-20 23:11:48 -05006008 if (!(phba->hba_flag & HBA_FCOE_MODE)) {
6009 /*
6010 * The FC Port needs to register FCFI (index 0)
6011 */
6012 lpfc_reg_fcfi(phba, mboxq);
6013 mboxq->vport = phba->pport;
6014 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
James Smart9589b0622011-04-16 11:03:17 -04006015 if (rc != MBX_SUCCESS)
James Smart76a95d72010-11-20 23:11:48 -05006016 goto out_unset_queue;
James Smart9589b0622011-04-16 11:03:17 -04006017 rc = 0;
6018 phba->fcf.fcfi = bf_get(lpfc_reg_fcfi_fcfi,
6019 &mboxq->u.mqe.un.reg_fcfi);
James Smart76a95d72010-11-20 23:11:48 -05006020 }
James Smartda0436e2009-05-22 14:51:39 -04006021 /*
6022 * The port is ready, set the host's link state to LINK_DOWN
6023 * in preparation for link interrupts.
6024 */
James Smartda0436e2009-05-22 14:51:39 -04006025 spin_lock_irq(&phba->hbalock);
6026 phba->link_state = LPFC_LINK_DOWN;
6027 spin_unlock_irq(&phba->hbalock);
James Smartfedd3b72011-02-16 12:39:24 -05006028 if (phba->cfg_suppress_link_up == LPFC_INITIALIZE_LINK)
6029 rc = phba->lpfc_hba_init_link(phba, MBX_NOWAIT);
James Smart76a95d72010-11-20 23:11:48 -05006030out_unset_queue:
James Smartda0436e2009-05-22 14:51:39 -04006031 /* Unset all the queues set up in this routine when error out */
6032 if (rc)
6033 lpfc_sli4_queue_unset(phba);
James Smartda0436e2009-05-22 14:51:39 -04006034out_stop_timers:
6035 if (rc)
6036 lpfc_stop_hba_timers(phba);
James Smartda0436e2009-05-22 14:51:39 -04006037out_free_mbox:
6038 mempool_free(mboxq, phba->mbox_mem_pool);
6039 return rc;
6040}
James Smarte59058c2008-08-24 21:49:00 -04006041
6042/**
James Smart3621a712009-04-06 18:47:14 -04006043 * lpfc_mbox_timeout - Timeout call back function for mbox timer
James Smarte59058c2008-08-24 21:49:00 -04006044 * @ptr: context object - pointer to hba structure.
dea31012005-04-17 16:05:31 -05006045 *
James Smarte59058c2008-08-24 21:49:00 -04006046 * This is the callback function for mailbox timer. The mailbox
6047 * timer is armed when a new mailbox command is issued and the timer
6048 * is deleted when the mailbox complete. The function is called by
6049 * the kernel timer code when a mailbox does not complete within
6050 * expected time. This function wakes up the worker thread to
6051 * process the mailbox timeout and returns. All the processing is
6052 * done by the worker thread function lpfc_mbox_timeout_handler.
6053 **/
dea31012005-04-17 16:05:31 -05006054void
6055lpfc_mbox_timeout(unsigned long ptr)
6056{
James Smart92d7f7b2007-06-17 19:56:38 -05006057 struct lpfc_hba *phba = (struct lpfc_hba *) ptr;
dea31012005-04-17 16:05:31 -05006058 unsigned long iflag;
James Smart2e0fef82007-06-17 19:56:36 -05006059 uint32_t tmo_posted;
dea31012005-04-17 16:05:31 -05006060
James Smart2e0fef82007-06-17 19:56:36 -05006061 spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
James Smart92d7f7b2007-06-17 19:56:38 -05006062 tmo_posted = phba->pport->work_port_events & WORKER_MBOX_TMO;
James Smart2e0fef82007-06-17 19:56:36 -05006063 if (!tmo_posted)
6064 phba->pport->work_port_events |= WORKER_MBOX_TMO;
6065 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
6066
James Smart5e9d9b82008-06-14 22:52:53 -04006067 if (!tmo_posted)
6068 lpfc_worker_wake_up(phba);
6069 return;
dea31012005-04-17 16:05:31 -05006070}
6071
James Smarte59058c2008-08-24 21:49:00 -04006072
6073/**
James Smart3621a712009-04-06 18:47:14 -04006074 * lpfc_mbox_timeout_handler - Worker thread function to handle mailbox timeout
James Smarte59058c2008-08-24 21:49:00 -04006075 * @phba: Pointer to HBA context object.
6076 *
6077 * This function is called from worker thread when a mailbox command times out.
6078 * The caller is not required to hold any locks. This function will reset the
6079 * HBA and recover all the pending commands.
6080 **/
dea31012005-04-17 16:05:31 -05006081void
6082lpfc_mbox_timeout_handler(struct lpfc_hba *phba)
6083{
James Smart2e0fef82007-06-17 19:56:36 -05006084 LPFC_MBOXQ_t *pmbox = phba->sli.mbox_active;
James Smart04c68492009-05-22 14:52:52 -04006085 MAILBOX_t *mb = &pmbox->u.mb;
James Smart1dcb58e2007-04-25 09:51:30 -04006086 struct lpfc_sli *psli = &phba->sli;
6087 struct lpfc_sli_ring *pring;
dea31012005-04-17 16:05:31 -05006088
James Smarta257bf92009-04-06 18:48:10 -04006089 /* Check the pmbox pointer first. There is a race condition
6090 * between the mbox timeout handler getting executed in the
6091 * worklist and the mailbox actually completing. When this
6092 * race condition occurs, the mbox_active will be NULL.
6093 */
6094 spin_lock_irq(&phba->hbalock);
6095 if (pmbox == NULL) {
6096 lpfc_printf_log(phba, KERN_WARNING,
6097 LOG_MBOX | LOG_SLI,
6098 "0353 Active Mailbox cleared - mailbox timeout "
6099 "exiting\n");
6100 spin_unlock_irq(&phba->hbalock);
6101 return;
6102 }
6103
dea31012005-04-17 16:05:31 -05006104 /* Mbox cmd <mbxCommand> timeout */
James Smarted957682007-06-17 19:56:37 -05006105 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04006106 "0310 Mailbox command x%x timeout Data: x%x x%x x%p\n",
James Smart92d7f7b2007-06-17 19:56:38 -05006107 mb->mbxCommand,
6108 phba->pport->port_state,
6109 phba->sli.sli_flag,
6110 phba->sli.mbox_active);
James Smarta257bf92009-04-06 18:48:10 -04006111 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05006112
James Smart1dcb58e2007-04-25 09:51:30 -04006113 /* Setting state unknown so lpfc_sli_abort_iocb_ring
6114 * would get IOCB_ERROR from lpfc_sli_issue_iocb, allowing
Lucas De Marchi25985ed2011-03-30 22:57:33 -03006115 * it to fail all outstanding SCSI IO.
James Smart1dcb58e2007-04-25 09:51:30 -04006116 */
James Smart2e0fef82007-06-17 19:56:36 -05006117 spin_lock_irq(&phba->pport->work_port_lock);
6118 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
6119 spin_unlock_irq(&phba->pport->work_port_lock);
6120 spin_lock_irq(&phba->hbalock);
6121 phba->link_state = LPFC_LINK_UNKNOWN;
James Smartf4b4c682009-05-22 14:53:12 -04006122 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05006123 spin_unlock_irq(&phba->hbalock);
James Smart1dcb58e2007-04-25 09:51:30 -04006124
6125 pring = &psli->ring[psli->fcp_ring];
6126 lpfc_sli_abort_iocb_ring(phba, pring);
6127
6128 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smart76bb24e2007-10-27 13:38:00 -04006129 "0345 Resetting board due to mailbox timeout\n");
James Smart3772a992009-05-22 14:50:54 -04006130
6131 /* Reset the HBA device */
6132 lpfc_reset_hba(phba);
dea31012005-04-17 16:05:31 -05006133}
6134
James Smarte59058c2008-08-24 21:49:00 -04006135/**
James Smart3772a992009-05-22 14:50:54 -04006136 * lpfc_sli_issue_mbox_s3 - Issue an SLI3 mailbox command to firmware
James Smarte59058c2008-08-24 21:49:00 -04006137 * @phba: Pointer to HBA context object.
6138 * @pmbox: Pointer to mailbox object.
6139 * @flag: Flag indicating how the mailbox need to be processed.
6140 *
6141 * This function is called by discovery code and HBA management code
James Smart3772a992009-05-22 14:50:54 -04006142 * to submit a mailbox command to firmware with SLI-3 interface spec. This
6143 * function gets the hbalock to protect the data structures.
James Smarte59058c2008-08-24 21:49:00 -04006144 * The mailbox command can be submitted in polling mode, in which case
6145 * this function will wait in a polling loop for the completion of the
6146 * mailbox.
6147 * If the mailbox is submitted in no_wait mode (not polling) the
6148 * function will submit the command and returns immediately without waiting
6149 * for the mailbox completion. The no_wait is supported only when HBA
6150 * is in SLI2/SLI3 mode - interrupts are enabled.
6151 * The SLI interface allows only one mailbox pending at a time. If the
6152 * mailbox is issued in polling mode and there is already a mailbox
6153 * pending, then the function will return an error. If the mailbox is issued
6154 * in NO_WAIT mode and there is a mailbox pending already, the function
6155 * will return MBX_BUSY after queuing the mailbox into mailbox queue.
6156 * The sli layer owns the mailbox object until the completion of mailbox
6157 * command if this function return MBX_BUSY or MBX_SUCCESS. For all other
6158 * return codes the caller owns the mailbox command after the return of
6159 * the function.
6160 **/
James Smart3772a992009-05-22 14:50:54 -04006161static int
6162lpfc_sli_issue_mbox_s3(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox,
6163 uint32_t flag)
dea31012005-04-17 16:05:31 -05006164{
dea31012005-04-17 16:05:31 -05006165 MAILBOX_t *mb;
James Smart2e0fef82007-06-17 19:56:36 -05006166 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05006167 uint32_t status, evtctr;
James Smart9940b972011-03-11 16:06:12 -05006168 uint32_t ha_copy, hc_copy;
dea31012005-04-17 16:05:31 -05006169 int i;
James Smart09372822008-01-11 01:52:54 -05006170 unsigned long timeout;
dea31012005-04-17 16:05:31 -05006171 unsigned long drvr_flag = 0;
James Smart34b02dc2008-08-24 21:49:55 -04006172 uint32_t word0, ldata;
dea31012005-04-17 16:05:31 -05006173 void __iomem *to_slim;
James Smart58da1ff2008-04-07 10:15:56 -04006174 int processing_queue = 0;
6175
6176 spin_lock_irqsave(&phba->hbalock, drvr_flag);
6177 if (!pmbox) {
James Smart8568a4d2009-07-19 10:01:16 -04006178 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart58da1ff2008-04-07 10:15:56 -04006179 /* processing mbox queue from intr_handler */
James Smart3772a992009-05-22 14:50:54 -04006180 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
6181 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
6182 return MBX_SUCCESS;
6183 }
James Smart58da1ff2008-04-07 10:15:56 -04006184 processing_queue = 1;
James Smart58da1ff2008-04-07 10:15:56 -04006185 pmbox = lpfc_mbox_get(phba);
6186 if (!pmbox) {
6187 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
6188 return MBX_SUCCESS;
6189 }
6190 }
dea31012005-04-17 16:05:31 -05006191
James Smarted957682007-06-17 19:56:37 -05006192 if (pmbox->mbox_cmpl && pmbox->mbox_cmpl != lpfc_sli_def_mbox_cmpl &&
James Smart92d7f7b2007-06-17 19:56:38 -05006193 pmbox->mbox_cmpl != lpfc_sli_wake_mbox_wait) {
James Smarted957682007-06-17 19:56:37 -05006194 if(!pmbox->vport) {
James Smart58da1ff2008-04-07 10:15:56 -04006195 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
James Smarted957682007-06-17 19:56:37 -05006196 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -05006197 LOG_MBOX | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04006198 "1806 Mbox x%x failed. No vport\n",
James Smart3772a992009-05-22 14:50:54 -04006199 pmbox->u.mb.mbxCommand);
James Smarted957682007-06-17 19:56:37 -05006200 dump_stack();
James Smart58da1ff2008-04-07 10:15:56 -04006201 goto out_not_finished;
James Smarted957682007-06-17 19:56:37 -05006202 }
6203 }
6204
Linas Vepstas8d63f372007-02-14 14:28:36 -06006205 /* If the PCI channel is in offline state, do not post mbox. */
James Smart58da1ff2008-04-07 10:15:56 -04006206 if (unlikely(pci_channel_offline(phba->pcidev))) {
6207 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
6208 goto out_not_finished;
6209 }
Linas Vepstas8d63f372007-02-14 14:28:36 -06006210
James Smarta257bf92009-04-06 18:48:10 -04006211 /* If HBA has a deferred error attention, fail the iocb. */
6212 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
6213 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
6214 goto out_not_finished;
6215 }
6216
dea31012005-04-17 16:05:31 -05006217 psli = &phba->sli;
James Smart92d7f7b2007-06-17 19:56:38 -05006218
James Smart3772a992009-05-22 14:50:54 -04006219 mb = &pmbox->u.mb;
dea31012005-04-17 16:05:31 -05006220 status = MBX_SUCCESS;
6221
James Smart2e0fef82007-06-17 19:56:36 -05006222 if (phba->link_state == LPFC_HBA_ERROR) {
6223 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05006224
6225 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04006226 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6227 "(%d):0311 Mailbox command x%x cannot "
6228 "issue Data: x%x x%x\n",
6229 pmbox->vport ? pmbox->vport->vpi : 0,
6230 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04006231 goto out_not_finished;
Jamie Wellnitz41415862006-02-28 19:25:27 -05006232 }
6233
James Smart9940b972011-03-11 16:06:12 -05006234 if (mb->mbxCommand != MBX_KILL_BOARD && flag & MBX_NOWAIT) {
6235 if (lpfc_readl(phba->HCregaddr, &hc_copy) ||
6236 !(hc_copy & HC_MBINT_ENA)) {
6237 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
6238 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smart3772a992009-05-22 14:50:54 -04006239 "(%d):2528 Mailbox command x%x cannot "
6240 "issue Data: x%x x%x\n",
6241 pmbox->vport ? pmbox->vport->vpi : 0,
6242 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
James Smart9940b972011-03-11 16:06:12 -05006243 goto out_not_finished;
6244 }
James Smart92908312006-03-07 15:04:13 -05006245 }
6246
dea31012005-04-17 16:05:31 -05006247 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
6248 /* Polling for a mbox command when another one is already active
6249 * is not allowed in SLI. Also, the driver must have established
6250 * SLI2 mode to queue and process multiple mbox commands.
6251 */
6252
6253 if (flag & MBX_POLL) {
James Smart2e0fef82007-06-17 19:56:36 -05006254 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05006255
6256 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04006257 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6258 "(%d):2529 Mailbox command x%x "
6259 "cannot issue Data: x%x x%x\n",
6260 pmbox->vport ? pmbox->vport->vpi : 0,
6261 pmbox->u.mb.mbxCommand,
6262 psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04006263 goto out_not_finished;
dea31012005-04-17 16:05:31 -05006264 }
6265
James Smart3772a992009-05-22 14:50:54 -04006266 if (!(psli->sli_flag & LPFC_SLI_ACTIVE)) {
James Smart2e0fef82007-06-17 19:56:36 -05006267 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05006268 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04006269 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6270 "(%d):2530 Mailbox command x%x "
6271 "cannot issue Data: x%x x%x\n",
6272 pmbox->vport ? pmbox->vport->vpi : 0,
6273 pmbox->u.mb.mbxCommand,
6274 psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04006275 goto out_not_finished;
dea31012005-04-17 16:05:31 -05006276 }
6277
dea31012005-04-17 16:05:31 -05006278 /* Another mailbox command is still being processed, queue this
6279 * command to be processed later.
6280 */
6281 lpfc_mbox_put(phba, pmbox);
6282
6283 /* Mbox cmd issue - BUSY */
James Smarted957682007-06-17 19:56:37 -05006284 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04006285 "(%d):0308 Mbox cmd issue - BUSY Data: "
James Smart92d7f7b2007-06-17 19:56:38 -05006286 "x%x x%x x%x x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -05006287 pmbox->vport ? pmbox->vport->vpi : 0xffffff,
6288 mb->mbxCommand, phba->pport->port_state,
6289 psli->sli_flag, flag);
dea31012005-04-17 16:05:31 -05006290
6291 psli->slistat.mbox_busy++;
James Smart2e0fef82007-06-17 19:56:36 -05006292 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05006293
James Smart858c9f62007-06-17 19:56:39 -05006294 if (pmbox->vport) {
6295 lpfc_debugfs_disc_trc(pmbox->vport,
6296 LPFC_DISC_TRC_MBOX_VPORT,
6297 "MBOX Bsy vport: cmd:x%x mb:x%x x%x",
6298 (uint32_t)mb->mbxCommand,
6299 mb->un.varWords[0], mb->un.varWords[1]);
6300 }
6301 else {
6302 lpfc_debugfs_disc_trc(phba->pport,
6303 LPFC_DISC_TRC_MBOX,
6304 "MBOX Bsy: cmd:x%x mb:x%x x%x",
6305 (uint32_t)mb->mbxCommand,
6306 mb->un.varWords[0], mb->un.varWords[1]);
6307 }
6308
James Smart2e0fef82007-06-17 19:56:36 -05006309 return MBX_BUSY;
dea31012005-04-17 16:05:31 -05006310 }
6311
dea31012005-04-17 16:05:31 -05006312 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
6313
6314 /* If we are not polling, we MUST be in SLI2 mode */
6315 if (flag != MBX_POLL) {
James Smart3772a992009-05-22 14:50:54 -04006316 if (!(psli->sli_flag & LPFC_SLI_ACTIVE) &&
Jamie Wellnitz41415862006-02-28 19:25:27 -05006317 (mb->mbxCommand != MBX_KILL_BOARD)) {
dea31012005-04-17 16:05:31 -05006318 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05006319 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05006320 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04006321 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6322 "(%d):2531 Mailbox command x%x "
6323 "cannot issue Data: x%x x%x\n",
6324 pmbox->vport ? pmbox->vport->vpi : 0,
6325 pmbox->u.mb.mbxCommand,
6326 psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04006327 goto out_not_finished;
dea31012005-04-17 16:05:31 -05006328 }
6329 /* timeout active mbox command */
James Smarta309a6b2006-08-01 07:33:43 -04006330 mod_timer(&psli->mbox_tmo, (jiffies +
James Smarta183a152011-10-10 21:32:43 -04006331 (HZ * lpfc_mbox_tmo_val(phba, pmbox))));
dea31012005-04-17 16:05:31 -05006332 }
6333
6334 /* Mailbox cmd <cmd> issue */
James Smarted957682007-06-17 19:56:37 -05006335 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04006336 "(%d):0309 Mailbox cmd x%x issue Data: x%x x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05006337 "x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04006338 pmbox->vport ? pmbox->vport->vpi : 0,
James Smart92d7f7b2007-06-17 19:56:38 -05006339 mb->mbxCommand, phba->pport->port_state,
6340 psli->sli_flag, flag);
dea31012005-04-17 16:05:31 -05006341
James Smart858c9f62007-06-17 19:56:39 -05006342 if (mb->mbxCommand != MBX_HEARTBEAT) {
6343 if (pmbox->vport) {
6344 lpfc_debugfs_disc_trc(pmbox->vport,
6345 LPFC_DISC_TRC_MBOX_VPORT,
6346 "MBOX Send vport: cmd:x%x mb:x%x x%x",
6347 (uint32_t)mb->mbxCommand,
6348 mb->un.varWords[0], mb->un.varWords[1]);
6349 }
6350 else {
6351 lpfc_debugfs_disc_trc(phba->pport,
6352 LPFC_DISC_TRC_MBOX,
6353 "MBOX Send: cmd:x%x mb:x%x x%x",
6354 (uint32_t)mb->mbxCommand,
6355 mb->un.varWords[0], mb->un.varWords[1]);
6356 }
6357 }
6358
dea31012005-04-17 16:05:31 -05006359 psli->slistat.mbox_cmd++;
6360 evtctr = psli->slistat.mbox_event;
6361
6362 /* next set own bit for the adapter and copy over command word */
6363 mb->mbxOwner = OWN_CHIP;
6364
James Smart3772a992009-05-22 14:50:54 -04006365 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
James Smart7a470272010-03-15 11:25:20 -04006366 /* Populate mbox extension offset word. */
6367 if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len) {
6368 *(((uint32_t *)mb) + pmbox->mbox_offset_word)
6369 = (uint8_t *)phba->mbox_ext
6370 - (uint8_t *)phba->mbox;
6371 }
6372
6373 /* Copy the mailbox extension data */
6374 if (pmbox->in_ext_byte_len && pmbox->context2) {
6375 lpfc_sli_pcimem_bcopy(pmbox->context2,
6376 (uint8_t *)phba->mbox_ext,
6377 pmbox->in_ext_byte_len);
6378 }
6379 /* Copy command data to host SLIM area */
James Smart34b02dc2008-08-24 21:49:55 -04006380 lpfc_sli_pcimem_bcopy(mb, phba->mbox, MAILBOX_CMD_SIZE);
dea31012005-04-17 16:05:31 -05006381 } else {
James Smart7a470272010-03-15 11:25:20 -04006382 /* Populate mbox extension offset word. */
6383 if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len)
6384 *(((uint32_t *)mb) + pmbox->mbox_offset_word)
6385 = MAILBOX_HBA_EXT_OFFSET;
6386
6387 /* Copy the mailbox extension data */
6388 if (pmbox->in_ext_byte_len && pmbox->context2) {
6389 lpfc_memcpy_to_slim(phba->MBslimaddr +
6390 MAILBOX_HBA_EXT_OFFSET,
6391 pmbox->context2, pmbox->in_ext_byte_len);
6392
6393 }
James Smart92908312006-03-07 15:04:13 -05006394 if (mb->mbxCommand == MBX_CONFIG_PORT) {
dea31012005-04-17 16:05:31 -05006395 /* copy command data into host mbox for cmpl */
James Smart34b02dc2008-08-24 21:49:55 -04006396 lpfc_sli_pcimem_bcopy(mb, phba->mbox, MAILBOX_CMD_SIZE);
dea31012005-04-17 16:05:31 -05006397 }
6398
6399 /* First copy mbox command data to HBA SLIM, skip past first
6400 word */
6401 to_slim = phba->MBslimaddr + sizeof (uint32_t);
6402 lpfc_memcpy_to_slim(to_slim, &mb->un.varWords[0],
6403 MAILBOX_CMD_SIZE - sizeof (uint32_t));
6404
6405 /* Next copy over first word, with mbxOwner set */
James Smart34b02dc2008-08-24 21:49:55 -04006406 ldata = *((uint32_t *)mb);
dea31012005-04-17 16:05:31 -05006407 to_slim = phba->MBslimaddr;
6408 writel(ldata, to_slim);
6409 readl(to_slim); /* flush */
6410
6411 if (mb->mbxCommand == MBX_CONFIG_PORT) {
6412 /* switch over to host mailbox */
James Smart3772a992009-05-22 14:50:54 -04006413 psli->sli_flag |= LPFC_SLI_ACTIVE;
dea31012005-04-17 16:05:31 -05006414 }
6415 }
6416
6417 wmb();
dea31012005-04-17 16:05:31 -05006418
6419 switch (flag) {
6420 case MBX_NOWAIT:
James Smart09372822008-01-11 01:52:54 -05006421 /* Set up reference to mailbox command */
dea31012005-04-17 16:05:31 -05006422 psli->mbox_active = pmbox;
James Smart09372822008-01-11 01:52:54 -05006423 /* Interrupt board to do it */
6424 writel(CA_MBATT, phba->CAregaddr);
6425 readl(phba->CAregaddr); /* flush */
6426 /* Don't wait for it to finish, just return */
dea31012005-04-17 16:05:31 -05006427 break;
6428
6429 case MBX_POLL:
James Smart09372822008-01-11 01:52:54 -05006430 /* Set up null reference to mailbox command */
dea31012005-04-17 16:05:31 -05006431 psli->mbox_active = NULL;
James Smart09372822008-01-11 01:52:54 -05006432 /* Interrupt board to do it */
6433 writel(CA_MBATT, phba->CAregaddr);
6434 readl(phba->CAregaddr); /* flush */
6435
James Smart3772a992009-05-22 14:50:54 -04006436 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05006437 /* First read mbox status word */
James Smart34b02dc2008-08-24 21:49:55 -04006438 word0 = *((uint32_t *)phba->mbox);
dea31012005-04-17 16:05:31 -05006439 word0 = le32_to_cpu(word0);
6440 } else {
6441 /* First read mbox status word */
James Smart9940b972011-03-11 16:06:12 -05006442 if (lpfc_readl(phba->MBslimaddr, &word0)) {
6443 spin_unlock_irqrestore(&phba->hbalock,
6444 drvr_flag);
6445 goto out_not_finished;
6446 }
dea31012005-04-17 16:05:31 -05006447 }
6448
6449 /* Read the HBA Host Attention Register */
James Smart9940b972011-03-11 16:06:12 -05006450 if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
6451 spin_unlock_irqrestore(&phba->hbalock,
6452 drvr_flag);
6453 goto out_not_finished;
6454 }
James Smarta183a152011-10-10 21:32:43 -04006455 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, pmbox) *
6456 1000) + jiffies;
James Smart09372822008-01-11 01:52:54 -05006457 i = 0;
dea31012005-04-17 16:05:31 -05006458 /* Wait for command to complete */
Jamie Wellnitz41415862006-02-28 19:25:27 -05006459 while (((word0 & OWN_CHIP) == OWN_CHIP) ||
6460 (!(ha_copy & HA_MBATT) &&
James Smart2e0fef82007-06-17 19:56:36 -05006461 (phba->link_state > LPFC_WARM_START))) {
James Smart09372822008-01-11 01:52:54 -05006462 if (time_after(jiffies, timeout)) {
dea31012005-04-17 16:05:31 -05006463 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05006464 spin_unlock_irqrestore(&phba->hbalock,
dea31012005-04-17 16:05:31 -05006465 drvr_flag);
James Smart58da1ff2008-04-07 10:15:56 -04006466 goto out_not_finished;
dea31012005-04-17 16:05:31 -05006467 }
6468
6469 /* Check if we took a mbox interrupt while we were
6470 polling */
6471 if (((word0 & OWN_CHIP) != OWN_CHIP)
6472 && (evtctr != psli->slistat.mbox_event))
6473 break;
6474
James Smart09372822008-01-11 01:52:54 -05006475 if (i++ > 10) {
6476 spin_unlock_irqrestore(&phba->hbalock,
6477 drvr_flag);
6478 msleep(1);
6479 spin_lock_irqsave(&phba->hbalock, drvr_flag);
6480 }
dea31012005-04-17 16:05:31 -05006481
James Smart3772a992009-05-22 14:50:54 -04006482 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05006483 /* First copy command data */
James Smart34b02dc2008-08-24 21:49:55 -04006484 word0 = *((uint32_t *)phba->mbox);
dea31012005-04-17 16:05:31 -05006485 word0 = le32_to_cpu(word0);
6486 if (mb->mbxCommand == MBX_CONFIG_PORT) {
6487 MAILBOX_t *slimmb;
James Smart34b02dc2008-08-24 21:49:55 -04006488 uint32_t slimword0;
dea31012005-04-17 16:05:31 -05006489 /* Check real SLIM for any errors */
6490 slimword0 = readl(phba->MBslimaddr);
6491 slimmb = (MAILBOX_t *) & slimword0;
6492 if (((slimword0 & OWN_CHIP) != OWN_CHIP)
6493 && slimmb->mbxStatus) {
6494 psli->sli_flag &=
James Smart3772a992009-05-22 14:50:54 -04006495 ~LPFC_SLI_ACTIVE;
dea31012005-04-17 16:05:31 -05006496 word0 = slimword0;
6497 }
6498 }
6499 } else {
6500 /* First copy command data */
6501 word0 = readl(phba->MBslimaddr);
6502 }
6503 /* Read the HBA Host Attention Register */
James Smart9940b972011-03-11 16:06:12 -05006504 if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
6505 spin_unlock_irqrestore(&phba->hbalock,
6506 drvr_flag);
6507 goto out_not_finished;
6508 }
dea31012005-04-17 16:05:31 -05006509 }
6510
James Smart3772a992009-05-22 14:50:54 -04006511 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05006512 /* copy results back to user */
James Smart34b02dc2008-08-24 21:49:55 -04006513 lpfc_sli_pcimem_bcopy(phba->mbox, mb, MAILBOX_CMD_SIZE);
James Smart7a470272010-03-15 11:25:20 -04006514 /* Copy the mailbox extension data */
6515 if (pmbox->out_ext_byte_len && pmbox->context2) {
6516 lpfc_sli_pcimem_bcopy(phba->mbox_ext,
6517 pmbox->context2,
6518 pmbox->out_ext_byte_len);
6519 }
dea31012005-04-17 16:05:31 -05006520 } else {
6521 /* First copy command data */
6522 lpfc_memcpy_from_slim(mb, phba->MBslimaddr,
6523 MAILBOX_CMD_SIZE);
James Smart7a470272010-03-15 11:25:20 -04006524 /* Copy the mailbox extension data */
6525 if (pmbox->out_ext_byte_len && pmbox->context2) {
6526 lpfc_memcpy_from_slim(pmbox->context2,
6527 phba->MBslimaddr +
6528 MAILBOX_HBA_EXT_OFFSET,
6529 pmbox->out_ext_byte_len);
dea31012005-04-17 16:05:31 -05006530 }
6531 }
6532
6533 writel(HA_MBATT, phba->HAregaddr);
6534 readl(phba->HAregaddr); /* flush */
6535
6536 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
6537 status = mb->mbxStatus;
6538 }
6539
James Smart2e0fef82007-06-17 19:56:36 -05006540 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
6541 return status;
James Smart58da1ff2008-04-07 10:15:56 -04006542
6543out_not_finished:
6544 if (processing_queue) {
James Smartda0436e2009-05-22 14:51:39 -04006545 pmbox->u.mb.mbxStatus = MBX_NOT_FINISHED;
James Smart58da1ff2008-04-07 10:15:56 -04006546 lpfc_mbox_cmpl_put(phba, pmbox);
6547 }
6548 return MBX_NOT_FINISHED;
dea31012005-04-17 16:05:31 -05006549}
6550
James Smarte59058c2008-08-24 21:49:00 -04006551/**
James Smartf1126682009-06-10 17:22:44 -04006552 * lpfc_sli4_async_mbox_block - Block posting SLI4 asynchronous mailbox command
6553 * @phba: Pointer to HBA context object.
6554 *
6555 * The function blocks the posting of SLI4 asynchronous mailbox commands from
6556 * the driver internal pending mailbox queue. It will then try to wait out the
6557 * possible outstanding mailbox command before return.
6558 *
6559 * Returns:
6560 * 0 - the outstanding mailbox command completed; otherwise, the wait for
6561 * the outstanding mailbox command timed out.
6562 **/
6563static int
6564lpfc_sli4_async_mbox_block(struct lpfc_hba *phba)
6565{
6566 struct lpfc_sli *psli = &phba->sli;
James Smartf1126682009-06-10 17:22:44 -04006567 int rc = 0;
James Smarta183a152011-10-10 21:32:43 -04006568 unsigned long timeout = 0;
James Smartf1126682009-06-10 17:22:44 -04006569
6570 /* Mark the asynchronous mailbox command posting as blocked */
6571 spin_lock_irq(&phba->hbalock);
6572 psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
James Smartf1126682009-06-10 17:22:44 -04006573 /* Determine how long we might wait for the active mailbox
6574 * command to be gracefully completed by firmware.
6575 */
James Smarta183a152011-10-10 21:32:43 -04006576 if (phba->sli.mbox_active)
6577 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
6578 phba->sli.mbox_active) *
6579 1000) + jiffies;
6580 spin_unlock_irq(&phba->hbalock);
6581
James Smartf1126682009-06-10 17:22:44 -04006582 /* Wait for the outstnading mailbox command to complete */
6583 while (phba->sli.mbox_active) {
6584 /* Check active mailbox complete status every 2ms */
6585 msleep(2);
6586 if (time_after(jiffies, timeout)) {
6587 /* Timeout, marked the outstanding cmd not complete */
6588 rc = 1;
6589 break;
6590 }
6591 }
6592
6593 /* Can not cleanly block async mailbox command, fails it */
6594 if (rc) {
6595 spin_lock_irq(&phba->hbalock);
6596 psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
6597 spin_unlock_irq(&phba->hbalock);
6598 }
6599 return rc;
6600}
6601
6602/**
6603 * lpfc_sli4_async_mbox_unblock - Block posting SLI4 async mailbox command
6604 * @phba: Pointer to HBA context object.
6605 *
6606 * The function unblocks and resume posting of SLI4 asynchronous mailbox
6607 * commands from the driver internal pending mailbox queue. It makes sure
6608 * that there is no outstanding mailbox command before resuming posting
6609 * asynchronous mailbox commands. If, for any reason, there is outstanding
6610 * mailbox command, it will try to wait it out before resuming asynchronous
6611 * mailbox command posting.
6612 **/
6613static void
6614lpfc_sli4_async_mbox_unblock(struct lpfc_hba *phba)
6615{
6616 struct lpfc_sli *psli = &phba->sli;
6617
6618 spin_lock_irq(&phba->hbalock);
6619 if (!(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
6620 /* Asynchronous mailbox posting is not blocked, do nothing */
6621 spin_unlock_irq(&phba->hbalock);
6622 return;
6623 }
6624
6625 /* Outstanding synchronous mailbox command is guaranteed to be done,
6626 * successful or timeout, after timing-out the outstanding mailbox
6627 * command shall always be removed, so just unblock posting async
6628 * mailbox command and resume
6629 */
6630 psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
6631 spin_unlock_irq(&phba->hbalock);
6632
6633 /* wake up worker thread to post asynchronlous mailbox command */
6634 lpfc_worker_wake_up(phba);
6635}
6636
6637/**
James Smartda0436e2009-05-22 14:51:39 -04006638 * lpfc_sli4_post_sync_mbox - Post an SLI4 mailbox to the bootstrap mailbox
6639 * @phba: Pointer to HBA context object.
6640 * @mboxq: Pointer to mailbox object.
6641 *
6642 * The function posts a mailbox to the port. The mailbox is expected
6643 * to be comletely filled in and ready for the port to operate on it.
6644 * This routine executes a synchronous completion operation on the
6645 * mailbox by polling for its completion.
6646 *
6647 * The caller must not be holding any locks when calling this routine.
6648 *
6649 * Returns:
6650 * MBX_SUCCESS - mailbox posted successfully
6651 * Any of the MBX error values.
6652 **/
6653static int
6654lpfc_sli4_post_sync_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
6655{
6656 int rc = MBX_SUCCESS;
6657 unsigned long iflag;
6658 uint32_t db_ready;
6659 uint32_t mcqe_status;
6660 uint32_t mbx_cmnd;
6661 unsigned long timeout;
6662 struct lpfc_sli *psli = &phba->sli;
6663 struct lpfc_mqe *mb = &mboxq->u.mqe;
6664 struct lpfc_bmbx_create *mbox_rgn;
6665 struct dma_address *dma_address;
6666 struct lpfc_register bmbx_reg;
6667
6668 /*
6669 * Only one mailbox can be active to the bootstrap mailbox region
6670 * at a time and there is no queueing provided.
6671 */
6672 spin_lock_irqsave(&phba->hbalock, iflag);
6673 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
6674 spin_unlock_irqrestore(&phba->hbalock, iflag);
6675 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04006676 "(%d):2532 Mailbox command x%x (x%x/x%x) "
James Smartda0436e2009-05-22 14:51:39 -04006677 "cannot issue Data: x%x x%x\n",
6678 mboxq->vport ? mboxq->vport->vpi : 0,
6679 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04006680 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
6681 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04006682 psli->sli_flag, MBX_POLL);
6683 return MBXERR_ERROR;
6684 }
6685 /* The server grabs the token and owns it until release */
6686 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
6687 phba->sli.mbox_active = mboxq;
6688 spin_unlock_irqrestore(&phba->hbalock, iflag);
6689
6690 /*
6691 * Initialize the bootstrap memory region to avoid stale data areas
6692 * in the mailbox post. Then copy the caller's mailbox contents to
6693 * the bmbx mailbox region.
6694 */
6695 mbx_cmnd = bf_get(lpfc_mqe_command, mb);
6696 memset(phba->sli4_hba.bmbx.avirt, 0, sizeof(struct lpfc_bmbx_create));
6697 lpfc_sli_pcimem_bcopy(mb, phba->sli4_hba.bmbx.avirt,
6698 sizeof(struct lpfc_mqe));
6699
6700 /* Post the high mailbox dma address to the port and wait for ready. */
6701 dma_address = &phba->sli4_hba.bmbx.dma_address;
6702 writel(dma_address->addr_hi, phba->sli4_hba.BMBXregaddr);
6703
James Smarta183a152011-10-10 21:32:43 -04006704 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mboxq)
James Smartda0436e2009-05-22 14:51:39 -04006705 * 1000) + jiffies;
6706 do {
6707 bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
6708 db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
6709 if (!db_ready)
6710 msleep(2);
6711
6712 if (time_after(jiffies, timeout)) {
6713 rc = MBXERR_ERROR;
6714 goto exit;
6715 }
6716 } while (!db_ready);
6717
6718 /* Post the low mailbox dma address to the port. */
6719 writel(dma_address->addr_lo, phba->sli4_hba.BMBXregaddr);
James Smarta183a152011-10-10 21:32:43 -04006720 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mboxq)
James Smartda0436e2009-05-22 14:51:39 -04006721 * 1000) + jiffies;
6722 do {
6723 bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
6724 db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
6725 if (!db_ready)
6726 msleep(2);
6727
6728 if (time_after(jiffies, timeout)) {
6729 rc = MBXERR_ERROR;
6730 goto exit;
6731 }
6732 } while (!db_ready);
6733
6734 /*
6735 * Read the CQ to ensure the mailbox has completed.
6736 * If so, update the mailbox status so that the upper layers
6737 * can complete the request normally.
6738 */
6739 lpfc_sli_pcimem_bcopy(phba->sli4_hba.bmbx.avirt, mb,
6740 sizeof(struct lpfc_mqe));
6741 mbox_rgn = (struct lpfc_bmbx_create *) phba->sli4_hba.bmbx.avirt;
6742 lpfc_sli_pcimem_bcopy(&mbox_rgn->mcqe, &mboxq->mcqe,
6743 sizeof(struct lpfc_mcqe));
6744 mcqe_status = bf_get(lpfc_mcqe_status, &mbox_rgn->mcqe);
James Smart05580562011-05-24 11:40:48 -04006745 /*
6746 * When the CQE status indicates a failure and the mailbox status
6747 * indicates success then copy the CQE status into the mailbox status
6748 * (and prefix it with x4000).
6749 */
James Smartda0436e2009-05-22 14:51:39 -04006750 if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
James Smart05580562011-05-24 11:40:48 -04006751 if (bf_get(lpfc_mqe_status, mb) == MBX_SUCCESS)
6752 bf_set(lpfc_mqe_status, mb,
6753 (LPFC_MBX_ERROR_RANGE | mcqe_status));
James Smartda0436e2009-05-22 14:51:39 -04006754 rc = MBXERR_ERROR;
James Smartd7c47992010-06-08 18:31:54 -04006755 } else
6756 lpfc_sli4_swap_str(phba, mboxq);
James Smartda0436e2009-05-22 14:51:39 -04006757
6758 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04006759 "(%d):0356 Mailbox cmd x%x (x%x/x%x) Status x%x "
James Smartda0436e2009-05-22 14:51:39 -04006760 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x"
6761 " x%x x%x CQ: x%x x%x x%x x%x\n",
James Smarta183a152011-10-10 21:32:43 -04006762 mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
6763 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
6764 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04006765 bf_get(lpfc_mqe_status, mb),
6766 mb->un.mb_words[0], mb->un.mb_words[1],
6767 mb->un.mb_words[2], mb->un.mb_words[3],
6768 mb->un.mb_words[4], mb->un.mb_words[5],
6769 mb->un.mb_words[6], mb->un.mb_words[7],
6770 mb->un.mb_words[8], mb->un.mb_words[9],
6771 mb->un.mb_words[10], mb->un.mb_words[11],
6772 mb->un.mb_words[12], mboxq->mcqe.word0,
6773 mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
6774 mboxq->mcqe.trailer);
6775exit:
6776 /* We are holding the token, no needed for lock when release */
6777 spin_lock_irqsave(&phba->hbalock, iflag);
6778 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
6779 phba->sli.mbox_active = NULL;
6780 spin_unlock_irqrestore(&phba->hbalock, iflag);
6781 return rc;
6782}
6783
6784/**
6785 * lpfc_sli_issue_mbox_s4 - Issue an SLI4 mailbox command to firmware
6786 * @phba: Pointer to HBA context object.
6787 * @pmbox: Pointer to mailbox object.
6788 * @flag: Flag indicating how the mailbox need to be processed.
6789 *
6790 * This function is called by discovery code and HBA management code to submit
6791 * a mailbox command to firmware with SLI-4 interface spec.
6792 *
6793 * Return codes the caller owns the mailbox command after the return of the
6794 * function.
6795 **/
6796static int
6797lpfc_sli_issue_mbox_s4(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
6798 uint32_t flag)
6799{
6800 struct lpfc_sli *psli = &phba->sli;
6801 unsigned long iflags;
6802 int rc;
6803
James Smartb76f2dc2011-07-22 18:37:42 -04006804 /* dump from issue mailbox command if setup */
6805 lpfc_idiag_mbxacc_dump_issue_mbox(phba, &mboxq->u.mb);
6806
James Smart8fa38512009-07-19 10:01:03 -04006807 rc = lpfc_mbox_dev_check(phba);
6808 if (unlikely(rc)) {
6809 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04006810 "(%d):2544 Mailbox command x%x (x%x/x%x) "
James Smart8fa38512009-07-19 10:01:03 -04006811 "cannot issue Data: x%x x%x\n",
6812 mboxq->vport ? mboxq->vport->vpi : 0,
6813 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04006814 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
6815 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smart8fa38512009-07-19 10:01:03 -04006816 psli->sli_flag, flag);
6817 goto out_not_finished;
6818 }
6819
James Smartda0436e2009-05-22 14:51:39 -04006820 /* Detect polling mode and jump to a handler */
6821 if (!phba->sli4_hba.intr_enable) {
6822 if (flag == MBX_POLL)
6823 rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
6824 else
6825 rc = -EIO;
6826 if (rc != MBX_SUCCESS)
James Smart05580562011-05-24 11:40:48 -04006827 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
James Smartda0436e2009-05-22 14:51:39 -04006828 "(%d):2541 Mailbox command x%x "
James Smarta183a152011-10-10 21:32:43 -04006829 "(x%x/x%x) cannot issue Data: "
6830 "x%x x%x\n",
James Smartda0436e2009-05-22 14:51:39 -04006831 mboxq->vport ? mboxq->vport->vpi : 0,
6832 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04006833 lpfc_sli_config_mbox_subsys_get(phba,
6834 mboxq),
6835 lpfc_sli_config_mbox_opcode_get(phba,
6836 mboxq),
James Smartda0436e2009-05-22 14:51:39 -04006837 psli->sli_flag, flag);
6838 return rc;
6839 } else if (flag == MBX_POLL) {
James Smartf1126682009-06-10 17:22:44 -04006840 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
6841 "(%d):2542 Try to issue mailbox command "
James Smarta183a152011-10-10 21:32:43 -04006842 "x%x (x%x/x%x) synchronously ahead of async"
James Smartf1126682009-06-10 17:22:44 -04006843 "mailbox command queue: x%x x%x\n",
James Smartda0436e2009-05-22 14:51:39 -04006844 mboxq->vport ? mboxq->vport->vpi : 0,
6845 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04006846 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
6847 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04006848 psli->sli_flag, flag);
James Smartf1126682009-06-10 17:22:44 -04006849 /* Try to block the asynchronous mailbox posting */
6850 rc = lpfc_sli4_async_mbox_block(phba);
6851 if (!rc) {
6852 /* Successfully blocked, now issue sync mbox cmd */
6853 rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
6854 if (rc != MBX_SUCCESS)
6855 lpfc_printf_log(phba, KERN_ERR,
James Smarta183a152011-10-10 21:32:43 -04006856 LOG_MBOX | LOG_SLI,
6857 "(%d):2597 Mailbox command "
6858 "x%x (x%x/x%x) cannot issue "
6859 "Data: x%x x%x\n",
6860 mboxq->vport ?
6861 mboxq->vport->vpi : 0,
6862 mboxq->u.mb.mbxCommand,
6863 lpfc_sli_config_mbox_subsys_get(phba,
6864 mboxq),
6865 lpfc_sli_config_mbox_opcode_get(phba,
6866 mboxq),
6867 psli->sli_flag, flag);
James Smartf1126682009-06-10 17:22:44 -04006868 /* Unblock the async mailbox posting afterward */
6869 lpfc_sli4_async_mbox_unblock(phba);
6870 }
6871 return rc;
James Smartda0436e2009-05-22 14:51:39 -04006872 }
6873
6874 /* Now, interrupt mode asynchrous mailbox command */
6875 rc = lpfc_mbox_cmd_check(phba, mboxq);
6876 if (rc) {
6877 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04006878 "(%d):2543 Mailbox command x%x (x%x/x%x) "
James Smartda0436e2009-05-22 14:51:39 -04006879 "cannot issue Data: x%x x%x\n",
6880 mboxq->vport ? mboxq->vport->vpi : 0,
6881 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04006882 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
6883 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04006884 psli->sli_flag, flag);
6885 goto out_not_finished;
6886 }
James Smartda0436e2009-05-22 14:51:39 -04006887
6888 /* Put the mailbox command to the driver internal FIFO */
6889 psli->slistat.mbox_busy++;
6890 spin_lock_irqsave(&phba->hbalock, iflags);
6891 lpfc_mbox_put(phba, mboxq);
6892 spin_unlock_irqrestore(&phba->hbalock, iflags);
6893 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
6894 "(%d):0354 Mbox cmd issue - Enqueue Data: "
James Smarta183a152011-10-10 21:32:43 -04006895 "x%x (x%x/x%x) x%x x%x x%x\n",
James Smartda0436e2009-05-22 14:51:39 -04006896 mboxq->vport ? mboxq->vport->vpi : 0xffffff,
6897 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
James Smarta183a152011-10-10 21:32:43 -04006898 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
6899 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04006900 phba->pport->port_state,
6901 psli->sli_flag, MBX_NOWAIT);
6902 /* Wake up worker thread to transport mailbox command from head */
6903 lpfc_worker_wake_up(phba);
6904
6905 return MBX_BUSY;
6906
6907out_not_finished:
6908 return MBX_NOT_FINISHED;
6909}
6910
6911/**
6912 * lpfc_sli4_post_async_mbox - Post an SLI4 mailbox command to device
6913 * @phba: Pointer to HBA context object.
6914 *
6915 * This function is called by worker thread to send a mailbox command to
6916 * SLI4 HBA firmware.
6917 *
6918 **/
6919int
6920lpfc_sli4_post_async_mbox(struct lpfc_hba *phba)
6921{
6922 struct lpfc_sli *psli = &phba->sli;
6923 LPFC_MBOXQ_t *mboxq;
6924 int rc = MBX_SUCCESS;
6925 unsigned long iflags;
6926 struct lpfc_mqe *mqe;
6927 uint32_t mbx_cmnd;
6928
6929 /* Check interrupt mode before post async mailbox command */
6930 if (unlikely(!phba->sli4_hba.intr_enable))
6931 return MBX_NOT_FINISHED;
6932
6933 /* Check for mailbox command service token */
6934 spin_lock_irqsave(&phba->hbalock, iflags);
6935 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
6936 spin_unlock_irqrestore(&phba->hbalock, iflags);
6937 return MBX_NOT_FINISHED;
6938 }
6939 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
6940 spin_unlock_irqrestore(&phba->hbalock, iflags);
6941 return MBX_NOT_FINISHED;
6942 }
6943 if (unlikely(phba->sli.mbox_active)) {
6944 spin_unlock_irqrestore(&phba->hbalock, iflags);
6945 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6946 "0384 There is pending active mailbox cmd\n");
6947 return MBX_NOT_FINISHED;
6948 }
6949 /* Take the mailbox command service token */
6950 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
6951
6952 /* Get the next mailbox command from head of queue */
6953 mboxq = lpfc_mbox_get(phba);
6954
6955 /* If no more mailbox command waiting for post, we're done */
6956 if (!mboxq) {
6957 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
6958 spin_unlock_irqrestore(&phba->hbalock, iflags);
6959 return MBX_SUCCESS;
6960 }
6961 phba->sli.mbox_active = mboxq;
6962 spin_unlock_irqrestore(&phba->hbalock, iflags);
6963
6964 /* Check device readiness for posting mailbox command */
6965 rc = lpfc_mbox_dev_check(phba);
6966 if (unlikely(rc))
6967 /* Driver clean routine will clean up pending mailbox */
6968 goto out_not_finished;
6969
6970 /* Prepare the mbox command to be posted */
6971 mqe = &mboxq->u.mqe;
6972 mbx_cmnd = bf_get(lpfc_mqe_command, mqe);
6973
6974 /* Start timer for the mbox_tmo and log some mailbox post messages */
6975 mod_timer(&psli->mbox_tmo, (jiffies +
James Smarta183a152011-10-10 21:32:43 -04006976 (HZ * lpfc_mbox_tmo_val(phba, mboxq))));
James Smartda0436e2009-05-22 14:51:39 -04006977
6978 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04006979 "(%d):0355 Mailbox cmd x%x (x%x/x%x) issue Data: "
James Smartda0436e2009-05-22 14:51:39 -04006980 "x%x x%x\n",
6981 mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
James Smarta183a152011-10-10 21:32:43 -04006982 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
6983 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04006984 phba->pport->port_state, psli->sli_flag);
6985
6986 if (mbx_cmnd != MBX_HEARTBEAT) {
6987 if (mboxq->vport) {
6988 lpfc_debugfs_disc_trc(mboxq->vport,
6989 LPFC_DISC_TRC_MBOX_VPORT,
6990 "MBOX Send vport: cmd:x%x mb:x%x x%x",
6991 mbx_cmnd, mqe->un.mb_words[0],
6992 mqe->un.mb_words[1]);
6993 } else {
6994 lpfc_debugfs_disc_trc(phba->pport,
6995 LPFC_DISC_TRC_MBOX,
6996 "MBOX Send: cmd:x%x mb:x%x x%x",
6997 mbx_cmnd, mqe->un.mb_words[0],
6998 mqe->un.mb_words[1]);
6999 }
7000 }
7001 psli->slistat.mbox_cmd++;
7002
7003 /* Post the mailbox command to the port */
7004 rc = lpfc_sli4_mq_put(phba->sli4_hba.mbx_wq, mqe);
7005 if (rc != MBX_SUCCESS) {
7006 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04007007 "(%d):2533 Mailbox command x%x (x%x/x%x) "
James Smartda0436e2009-05-22 14:51:39 -04007008 "cannot issue Data: x%x x%x\n",
7009 mboxq->vport ? mboxq->vport->vpi : 0,
7010 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04007011 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7012 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04007013 psli->sli_flag, MBX_NOWAIT);
7014 goto out_not_finished;
7015 }
7016
7017 return rc;
7018
7019out_not_finished:
7020 spin_lock_irqsave(&phba->hbalock, iflags);
7021 mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
7022 __lpfc_mbox_cmpl_put(phba, mboxq);
7023 /* Release the token */
7024 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
7025 phba->sli.mbox_active = NULL;
7026 spin_unlock_irqrestore(&phba->hbalock, iflags);
7027
7028 return MBX_NOT_FINISHED;
7029}
7030
7031/**
7032 * lpfc_sli_issue_mbox - Wrapper func for issuing mailbox command
7033 * @phba: Pointer to HBA context object.
7034 * @pmbox: Pointer to mailbox object.
7035 * @flag: Flag indicating how the mailbox need to be processed.
7036 *
7037 * This routine wraps the actual SLI3 or SLI4 mailbox issuing routine from
7038 * the API jump table function pointer from the lpfc_hba struct.
7039 *
7040 * Return codes the caller owns the mailbox command after the return of the
7041 * function.
7042 **/
7043int
7044lpfc_sli_issue_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox, uint32_t flag)
7045{
7046 return phba->lpfc_sli_issue_mbox(phba, pmbox, flag);
7047}
7048
7049/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03007050 * lpfc_mbox_api_table_setup - Set up mbox api function jump table
James Smartda0436e2009-05-22 14:51:39 -04007051 * @phba: The hba struct for which this call is being executed.
7052 * @dev_grp: The HBA PCI-Device group number.
7053 *
7054 * This routine sets up the mbox interface API function jump table in @phba
7055 * struct.
7056 * Returns: 0 - success, -ENODEV - failure.
7057 **/
7058int
7059lpfc_mbox_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
7060{
7061
7062 switch (dev_grp) {
7063 case LPFC_PCI_DEV_LP:
7064 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s3;
7065 phba->lpfc_sli_handle_slow_ring_event =
7066 lpfc_sli_handle_slow_ring_event_s3;
7067 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s3;
7068 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s3;
7069 phba->lpfc_sli_brdready = lpfc_sli_brdready_s3;
7070 break;
7071 case LPFC_PCI_DEV_OC:
7072 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s4;
7073 phba->lpfc_sli_handle_slow_ring_event =
7074 lpfc_sli_handle_slow_ring_event_s4;
7075 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s4;
7076 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s4;
7077 phba->lpfc_sli_brdready = lpfc_sli_brdready_s4;
7078 break;
7079 default:
7080 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7081 "1420 Invalid HBA PCI-device group: 0x%x\n",
7082 dev_grp);
7083 return -ENODEV;
7084 break;
7085 }
7086 return 0;
7087}
7088
7089/**
James Smart3621a712009-04-06 18:47:14 -04007090 * __lpfc_sli_ringtx_put - Add an iocb to 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 to add a command
7096 * iocb to the txq when SLI layer cannot submit the command iocb
7097 * to the ring.
7098 **/
James Smart2a9bf3d2010-06-07 15:24:45 -04007099void
James Smart92d7f7b2007-06-17 19:56:38 -05007100__lpfc_sli_ringtx_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -05007101 struct lpfc_iocbq *piocb)
dea31012005-04-17 16:05:31 -05007102{
7103 /* Insert the caller's iocb in the txq tail for later processing. */
7104 list_add_tail(&piocb->list, &pring->txq);
7105 pring->txq_cnt++;
dea31012005-04-17 16:05:31 -05007106}
7107
James Smarte59058c2008-08-24 21:49:00 -04007108/**
James Smart3621a712009-04-06 18:47:14 -04007109 * lpfc_sli_next_iocb - Get the next iocb in the txq
James Smarte59058c2008-08-24 21:49:00 -04007110 * @phba: Pointer to HBA context object.
7111 * @pring: Pointer to driver SLI ring object.
7112 * @piocb: Pointer to address of newly added command iocb.
7113 *
7114 * This function is called with hbalock held before a new
7115 * iocb is submitted to the firmware. This function checks
7116 * txq to flush the iocbs in txq to Firmware before
7117 * submitting new iocbs to the Firmware.
7118 * If there are iocbs in the txq which need to be submitted
7119 * to firmware, lpfc_sli_next_iocb returns the first element
7120 * of the txq after dequeuing it from txq.
7121 * If there is no iocb in the txq then the function will return
7122 * *piocb and *piocb is set to NULL. Caller needs to check
7123 * *piocb to find if there are more commands in the txq.
7124 **/
dea31012005-04-17 16:05:31 -05007125static struct lpfc_iocbq *
7126lpfc_sli_next_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -05007127 struct lpfc_iocbq **piocb)
dea31012005-04-17 16:05:31 -05007128{
7129 struct lpfc_iocbq * nextiocb;
7130
7131 nextiocb = lpfc_sli_ringtx_get(phba, pring);
7132 if (!nextiocb) {
7133 nextiocb = *piocb;
7134 *piocb = NULL;
7135 }
7136
7137 return nextiocb;
7138}
7139
James Smarte59058c2008-08-24 21:49:00 -04007140/**
James Smart3772a992009-05-22 14:50:54 -04007141 * __lpfc_sli_issue_iocb_s3 - SLI3 device lockless ver of lpfc_sli_issue_iocb
James Smarte59058c2008-08-24 21:49:00 -04007142 * @phba: Pointer to HBA context object.
James Smart3772a992009-05-22 14:50:54 -04007143 * @ring_number: SLI ring number to issue iocb on.
James Smarte59058c2008-08-24 21:49:00 -04007144 * @piocb: Pointer to command iocb.
7145 * @flag: Flag indicating if this command can be put into txq.
7146 *
James Smart3772a992009-05-22 14:50:54 -04007147 * __lpfc_sli_issue_iocb_s3 is used by other functions in the driver to issue
7148 * an iocb command to an HBA with SLI-3 interface spec. If the PCI slot is
7149 * recovering from error state, if HBA is resetting or if LPFC_STOP_IOCB_EVENT
7150 * flag is turned on, the function returns IOCB_ERROR. When the link is down,
7151 * this function allows only iocbs for posting buffers. This function finds
7152 * next available slot in the command ring and posts the command to the
7153 * available slot and writes the port attention register to request HBA start
7154 * processing new iocb. If there is no slot available in the ring and
7155 * flag & SLI_IOCB_RET_IOCB is set, the new iocb is added to the txq, otherwise
7156 * the function returns IOCB_BUSY.
James Smarte59058c2008-08-24 21:49:00 -04007157 *
James Smart3772a992009-05-22 14:50:54 -04007158 * This function is called with hbalock held. The function will return success
7159 * after it successfully submit the iocb to firmware or after adding to the
7160 * txq.
James Smarte59058c2008-08-24 21:49:00 -04007161 **/
James Smart98c9ea52007-10-27 13:37:33 -04007162static int
James Smart3772a992009-05-22 14:50:54 -04007163__lpfc_sli_issue_iocb_s3(struct lpfc_hba *phba, uint32_t ring_number,
dea31012005-04-17 16:05:31 -05007164 struct lpfc_iocbq *piocb, uint32_t flag)
7165{
7166 struct lpfc_iocbq *nextiocb;
7167 IOCB_t *iocb;
James Smart3772a992009-05-22 14:50:54 -04007168 struct lpfc_sli_ring *pring = &phba->sli.ring[ring_number];
dea31012005-04-17 16:05:31 -05007169
James Smart92d7f7b2007-06-17 19:56:38 -05007170 if (piocb->iocb_cmpl && (!piocb->vport) &&
7171 (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
7172 (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
7173 lpfc_printf_log(phba, KERN_ERR,
7174 LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04007175 "1807 IOCB x%x failed. No vport\n",
James Smart92d7f7b2007-06-17 19:56:38 -05007176 piocb->iocb.ulpCommand);
7177 dump_stack();
7178 return IOCB_ERROR;
7179 }
7180
7181
Linas Vepstas8d63f372007-02-14 14:28:36 -06007182 /* If the PCI channel is in offline state, do not post iocbs. */
7183 if (unlikely(pci_channel_offline(phba->pcidev)))
7184 return IOCB_ERROR;
7185
James Smarta257bf92009-04-06 18:48:10 -04007186 /* If HBA has a deferred error attention, fail the iocb. */
7187 if (unlikely(phba->hba_flag & DEFER_ERATT))
7188 return IOCB_ERROR;
7189
dea31012005-04-17 16:05:31 -05007190 /*
7191 * We should never get an IOCB if we are in a < LINK_DOWN state
7192 */
James Smart2e0fef82007-06-17 19:56:36 -05007193 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
dea31012005-04-17 16:05:31 -05007194 return IOCB_ERROR;
7195
7196 /*
7197 * Check to see if we are blocking IOCB processing because of a
James Smart0b727fe2007-10-27 13:37:25 -04007198 * outstanding event.
dea31012005-04-17 16:05:31 -05007199 */
James Smart0b727fe2007-10-27 13:37:25 -04007200 if (unlikely(pring->flag & LPFC_STOP_IOCB_EVENT))
dea31012005-04-17 16:05:31 -05007201 goto iocb_busy;
7202
James Smart2e0fef82007-06-17 19:56:36 -05007203 if (unlikely(phba->link_state == LPFC_LINK_DOWN)) {
dea31012005-04-17 16:05:31 -05007204 /*
James Smart2680eea2007-04-25 09:52:55 -04007205 * Only CREATE_XRI, CLOSE_XRI, and QUE_RING_BUF
dea31012005-04-17 16:05:31 -05007206 * can be issued if the link is not up.
7207 */
7208 switch (piocb->iocb.ulpCommand) {
James Smart84774a42008-08-24 21:50:06 -04007209 case CMD_GEN_REQUEST64_CR:
7210 case CMD_GEN_REQUEST64_CX:
7211 if (!(phba->sli.sli_flag & LPFC_MENLO_MAINT) ||
7212 (piocb->iocb.un.genreq64.w5.hcsw.Rctl !=
James Smart6a9c52c2009-10-02 15:16:51 -04007213 FC_RCTL_DD_UNSOL_CMD) ||
James Smart84774a42008-08-24 21:50:06 -04007214 (piocb->iocb.un.genreq64.w5.hcsw.Type !=
7215 MENLO_TRANSPORT_TYPE))
7216
7217 goto iocb_busy;
7218 break;
dea31012005-04-17 16:05:31 -05007219 case CMD_QUE_RING_BUF_CN:
7220 case CMD_QUE_RING_BUF64_CN:
dea31012005-04-17 16:05:31 -05007221 /*
7222 * For IOCBs, like QUE_RING_BUF, that have no rsp ring
7223 * completion, iocb_cmpl MUST be 0.
7224 */
7225 if (piocb->iocb_cmpl)
7226 piocb->iocb_cmpl = NULL;
7227 /*FALLTHROUGH*/
7228 case CMD_CREATE_XRI_CR:
James Smart2680eea2007-04-25 09:52:55 -04007229 case CMD_CLOSE_XRI_CN:
7230 case CMD_CLOSE_XRI_CX:
dea31012005-04-17 16:05:31 -05007231 break;
7232 default:
7233 goto iocb_busy;
7234 }
7235
7236 /*
7237 * For FCP commands, we must be in a state where we can process link
7238 * attention events.
7239 */
7240 } else if (unlikely(pring->ringno == phba->sli.fcp_ring &&
James Smart92d7f7b2007-06-17 19:56:38 -05007241 !(phba->sli.sli_flag & LPFC_PROCESS_LA))) {
dea31012005-04-17 16:05:31 -05007242 goto iocb_busy;
James Smart92d7f7b2007-06-17 19:56:38 -05007243 }
dea31012005-04-17 16:05:31 -05007244
dea31012005-04-17 16:05:31 -05007245 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
7246 (nextiocb = lpfc_sli_next_iocb(phba, pring, &piocb)))
7247 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
7248
7249 if (iocb)
7250 lpfc_sli_update_ring(phba, pring);
7251 else
7252 lpfc_sli_update_full_ring(phba, pring);
7253
7254 if (!piocb)
7255 return IOCB_SUCCESS;
7256
7257 goto out_busy;
7258
7259 iocb_busy:
7260 pring->stats.iocb_cmd_delay++;
7261
7262 out_busy:
7263
7264 if (!(flag & SLI_IOCB_RET_IOCB)) {
James Smart92d7f7b2007-06-17 19:56:38 -05007265 __lpfc_sli_ringtx_put(phba, pring, piocb);
dea31012005-04-17 16:05:31 -05007266 return IOCB_SUCCESS;
7267 }
7268
7269 return IOCB_BUSY;
7270}
7271
James Smart3772a992009-05-22 14:50:54 -04007272/**
James Smart4f774512009-05-22 14:52:35 -04007273 * lpfc_sli4_bpl2sgl - Convert the bpl/bde to a sgl.
7274 * @phba: Pointer to HBA context object.
7275 * @piocb: Pointer to command iocb.
7276 * @sglq: Pointer to the scatter gather queue object.
7277 *
7278 * This routine converts the bpl or bde that is in the IOCB
7279 * to a sgl list for the sli4 hardware. The physical address
7280 * of the bpl/bde is converted back to a virtual address.
7281 * If the IOCB contains a BPL then the list of BDE's is
7282 * converted to sli4_sge's. If the IOCB contains a single
7283 * BDE then it is converted to a single sli_sge.
7284 * The IOCB is still in cpu endianess so the contents of
7285 * the bpl can be used without byte swapping.
7286 *
7287 * Returns valid XRI = Success, NO_XRI = Failure.
7288**/
7289static uint16_t
7290lpfc_sli4_bpl2sgl(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq,
7291 struct lpfc_sglq *sglq)
7292{
7293 uint16_t xritag = NO_XRI;
7294 struct ulp_bde64 *bpl = NULL;
7295 struct ulp_bde64 bde;
7296 struct sli4_sge *sgl = NULL;
7297 IOCB_t *icmd;
7298 int numBdes = 0;
7299 int i = 0;
James Smart63e801c2010-11-20 23:14:19 -05007300 uint32_t offset = 0; /* accumulated offset in the sg request list */
7301 int inbound = 0; /* number of sg reply entries inbound from firmware */
James Smart4f774512009-05-22 14:52:35 -04007302
7303 if (!piocbq || !sglq)
7304 return xritag;
7305
7306 sgl = (struct sli4_sge *)sglq->sgl;
7307 icmd = &piocbq->iocb;
7308 if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
7309 numBdes = icmd->un.genreq64.bdl.bdeSize /
7310 sizeof(struct ulp_bde64);
7311 /* The addrHigh and addrLow fields within the IOCB
7312 * have not been byteswapped yet so there is no
7313 * need to swap them back.
7314 */
7315 bpl = (struct ulp_bde64 *)
7316 ((struct lpfc_dmabuf *)piocbq->context3)->virt;
7317
7318 if (!bpl)
7319 return xritag;
7320
7321 for (i = 0; i < numBdes; i++) {
7322 /* Should already be byte swapped. */
James Smart28baac72010-02-12 14:42:03 -05007323 sgl->addr_hi = bpl->addrHigh;
7324 sgl->addr_lo = bpl->addrLow;
7325
James Smart05580562011-05-24 11:40:48 -04007326 sgl->word2 = le32_to_cpu(sgl->word2);
James Smart4f774512009-05-22 14:52:35 -04007327 if ((i+1) == numBdes)
7328 bf_set(lpfc_sli4_sge_last, sgl, 1);
7329 else
7330 bf_set(lpfc_sli4_sge_last, sgl, 0);
James Smart28baac72010-02-12 14:42:03 -05007331 /* swap the size field back to the cpu so we
7332 * can assign it to the sgl.
7333 */
7334 bde.tus.w = le32_to_cpu(bpl->tus.w);
7335 sgl->sge_len = cpu_to_le32(bde.tus.f.bdeSize);
James Smart63e801c2010-11-20 23:14:19 -05007336 /* The offsets in the sgl need to be accumulated
7337 * separately for the request and reply lists.
7338 * The request is always first, the reply follows.
7339 */
7340 if (piocbq->iocb.ulpCommand == CMD_GEN_REQUEST64_CR) {
7341 /* add up the reply sg entries */
7342 if (bpl->tus.f.bdeFlags == BUFF_TYPE_BDE_64I)
7343 inbound++;
7344 /* first inbound? reset the offset */
7345 if (inbound == 1)
7346 offset = 0;
7347 bf_set(lpfc_sli4_sge_offset, sgl, offset);
7348 offset += bde.tus.f.bdeSize;
7349 }
James Smart546fc852011-03-11 16:06:29 -05007350 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart4f774512009-05-22 14:52:35 -04007351 bpl++;
7352 sgl++;
7353 }
7354 } else if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BDE_64) {
7355 /* The addrHigh and addrLow fields of the BDE have not
7356 * been byteswapped yet so they need to be swapped
7357 * before putting them in the sgl.
7358 */
7359 sgl->addr_hi =
7360 cpu_to_le32(icmd->un.genreq64.bdl.addrHigh);
7361 sgl->addr_lo =
7362 cpu_to_le32(icmd->un.genreq64.bdl.addrLow);
James Smart05580562011-05-24 11:40:48 -04007363 sgl->word2 = le32_to_cpu(sgl->word2);
James Smart4f774512009-05-22 14:52:35 -04007364 bf_set(lpfc_sli4_sge_last, sgl, 1);
7365 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart28baac72010-02-12 14:42:03 -05007366 sgl->sge_len =
7367 cpu_to_le32(icmd->un.genreq64.bdl.bdeSize);
James Smart4f774512009-05-22 14:52:35 -04007368 }
7369 return sglq->sli4_xritag;
7370}
7371
7372/**
7373 * lpfc_sli4_scmd_to_wqidx_distr - scsi command to SLI4 WQ index distribution
7374 * @phba: Pointer to HBA context object.
James Smart4f774512009-05-22 14:52:35 -04007375 *
James Smarta93ff372010-10-22 11:06:08 -04007376 * This routine performs a roundrobin SCSI command to SLI4 FCP WQ index
James Smart8fa38512009-07-19 10:01:03 -04007377 * distribution. This is called by __lpfc_sli_issue_iocb_s4() with the hbalock
7378 * held.
James Smart4f774512009-05-22 14:52:35 -04007379 *
7380 * Return: index into SLI4 fast-path FCP queue index.
7381 **/
7382static uint32_t
James Smart8fa38512009-07-19 10:01:03 -04007383lpfc_sli4_scmd_to_wqidx_distr(struct lpfc_hba *phba)
James Smart4f774512009-05-22 14:52:35 -04007384{
James Smart8fa38512009-07-19 10:01:03 -04007385 ++phba->fcp_qidx;
7386 if (phba->fcp_qidx >= phba->cfg_fcp_wq_count)
7387 phba->fcp_qidx = 0;
James Smart4f774512009-05-22 14:52:35 -04007388
James Smart8fa38512009-07-19 10:01:03 -04007389 return phba->fcp_qidx;
James Smart4f774512009-05-22 14:52:35 -04007390}
7391
7392/**
7393 * lpfc_sli_iocb2wqe - Convert the IOCB to a work queue entry.
7394 * @phba: Pointer to HBA context object.
7395 * @piocb: Pointer to command iocb.
7396 * @wqe: Pointer to the work queue entry.
7397 *
7398 * This routine converts the iocb command to its Work Queue Entry
7399 * equivalent. The wqe pointer should not have any fields set when
7400 * this routine is called because it will memcpy over them.
7401 * This routine does not set the CQ_ID or the WQEC bits in the
7402 * wqe.
7403 *
7404 * Returns: 0 = Success, IOCB_ERROR = Failure.
7405 **/
7406static int
7407lpfc_sli4_iocb2wqe(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq,
7408 union lpfc_wqe *wqe)
7409{
James Smart5ffc2662009-11-18 15:39:44 -05007410 uint32_t xmit_len = 0, total_len = 0;
James Smart4f774512009-05-22 14:52:35 -04007411 uint8_t ct = 0;
7412 uint32_t fip;
7413 uint32_t abort_tag;
7414 uint8_t command_type = ELS_COMMAND_NON_FIP;
7415 uint8_t cmnd;
7416 uint16_t xritag;
James Smartdcf2a4e2010-09-29 11:18:53 -04007417 uint16_t abrt_iotag;
7418 struct lpfc_iocbq *abrtiocbq;
James Smart4f774512009-05-22 14:52:35 -04007419 struct ulp_bde64 *bpl = NULL;
James Smartf0d9bcc2010-10-22 11:07:09 -04007420 uint32_t els_id = LPFC_ELS_ID_DEFAULT;
James Smart5ffc2662009-11-18 15:39:44 -05007421 int numBdes, i;
7422 struct ulp_bde64 bde;
James Smartc31098c2011-04-16 11:03:33 -04007423 struct lpfc_nodelist *ndlp;
James Smart4f774512009-05-22 14:52:35 -04007424
James Smart45ed1192009-10-02 15:17:02 -04007425 fip = phba->hba_flag & HBA_FIP_SUPPORT;
James Smart4f774512009-05-22 14:52:35 -04007426 /* The fcp commands will set command type */
James Smart0c287582009-06-10 17:22:56 -04007427 if (iocbq->iocb_flag & LPFC_IO_FCP)
James Smart4f774512009-05-22 14:52:35 -04007428 command_type = FCP_COMMAND;
James Smartc8685952009-11-18 15:39:16 -05007429 else if (fip && (iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK))
James Smart0c287582009-06-10 17:22:56 -04007430 command_type = ELS_COMMAND_FIP;
7431 else
7432 command_type = ELS_COMMAND_NON_FIP;
7433
James Smart4f774512009-05-22 14:52:35 -04007434 /* Some of the fields are in the right position already */
7435 memcpy(wqe, &iocbq->iocb, sizeof(union lpfc_wqe));
7436 abort_tag = (uint32_t) iocbq->iotag;
7437 xritag = iocbq->sli4_xritag;
James Smartf0d9bcc2010-10-22 11:07:09 -04007438 wqe->generic.wqe_com.word7 = 0; /* The ct field has moved so reset */
James Smart4f774512009-05-22 14:52:35 -04007439 /* words0-2 bpl convert bde */
7440 if (iocbq->iocb.un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
James Smart5ffc2662009-11-18 15:39:44 -05007441 numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
7442 sizeof(struct ulp_bde64);
James Smart4f774512009-05-22 14:52:35 -04007443 bpl = (struct ulp_bde64 *)
7444 ((struct lpfc_dmabuf *)iocbq->context3)->virt;
7445 if (!bpl)
7446 return IOCB_ERROR;
7447
7448 /* Should already be byte swapped. */
7449 wqe->generic.bde.addrHigh = le32_to_cpu(bpl->addrHigh);
7450 wqe->generic.bde.addrLow = le32_to_cpu(bpl->addrLow);
7451 /* swap the size field back to the cpu so we
7452 * can assign it to the sgl.
7453 */
7454 wqe->generic.bde.tus.w = le32_to_cpu(bpl->tus.w);
James Smart5ffc2662009-11-18 15:39:44 -05007455 xmit_len = wqe->generic.bde.tus.f.bdeSize;
7456 total_len = 0;
7457 for (i = 0; i < numBdes; i++) {
7458 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
7459 total_len += bde.tus.f.bdeSize;
7460 }
James Smart4f774512009-05-22 14:52:35 -04007461 } else
James Smart5ffc2662009-11-18 15:39:44 -05007462 xmit_len = iocbq->iocb.un.fcpi64.bdl.bdeSize;
James Smart4f774512009-05-22 14:52:35 -04007463
7464 iocbq->iocb.ulpIoTag = iocbq->iotag;
7465 cmnd = iocbq->iocb.ulpCommand;
7466
7467 switch (iocbq->iocb.ulpCommand) {
7468 case CMD_ELS_REQUEST64_CR:
James Smartc31098c2011-04-16 11:03:33 -04007469 ndlp = (struct lpfc_nodelist *)iocbq->context1;
James Smart4f774512009-05-22 14:52:35 -04007470 if (!iocbq->iocb.ulpLe) {
7471 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
7472 "2007 Only Limited Edition cmd Format"
7473 " supported 0x%x\n",
7474 iocbq->iocb.ulpCommand);
7475 return IOCB_ERROR;
7476 }
James Smart5ffc2662009-11-18 15:39:44 -05007477 wqe->els_req.payload_len = xmit_len;
James Smart4f774512009-05-22 14:52:35 -04007478 /* Els_reguest64 has a TMO */
7479 bf_set(wqe_tmo, &wqe->els_req.wqe_com,
7480 iocbq->iocb.ulpTimeout);
7481 /* Need a VF for word 4 set the vf bit*/
7482 bf_set(els_req64_vf, &wqe->els_req, 0);
7483 /* And a VFID for word 12 */
7484 bf_set(els_req64_vfid, &wqe->els_req, 0);
James Smart4f774512009-05-22 14:52:35 -04007485 ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
James Smartf0d9bcc2010-10-22 11:07:09 -04007486 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
7487 iocbq->iocb.ulpContext);
7488 bf_set(wqe_ct, &wqe->els_req.wqe_com, ct);
7489 bf_set(wqe_pu, &wqe->els_req.wqe_com, 0);
James Smart4f774512009-05-22 14:52:35 -04007490 /* CCP CCPE PV PRI in word10 were set in the memcpy */
James Smartc8685952009-11-18 15:39:16 -05007491 if (command_type == ELS_COMMAND_FIP) {
7492 els_id = ((iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK)
7493 >> LPFC_FIP_ELS_ID_SHIFT);
7494 }
James Smart6d368e52011-05-24 11:44:12 -04007495 bf_set(wqe_temp_rpi, &wqe->els_req.wqe_com,
7496 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
James Smartf0d9bcc2010-10-22 11:07:09 -04007497 bf_set(wqe_els_id, &wqe->els_req.wqe_com, els_id);
7498 bf_set(wqe_dbde, &wqe->els_req.wqe_com, 1);
7499 bf_set(wqe_iod, &wqe->els_req.wqe_com, LPFC_WQE_IOD_READ);
7500 bf_set(wqe_qosd, &wqe->els_req.wqe_com, 1);
7501 bf_set(wqe_lenloc, &wqe->els_req.wqe_com, LPFC_WQE_LENLOC_NONE);
7502 bf_set(wqe_ebde_cnt, &wqe->els_req.wqe_com, 0);
James Smart7851fe22011-07-22 18:36:52 -04007503 break;
James Smart5ffc2662009-11-18 15:39:44 -05007504 case CMD_XMIT_SEQUENCE64_CX:
James Smartf0d9bcc2010-10-22 11:07:09 -04007505 bf_set(wqe_ctxt_tag, &wqe->xmit_sequence.wqe_com,
7506 iocbq->iocb.un.ulpWord[3]);
7507 bf_set(wqe_rcvoxid, &wqe->xmit_sequence.wqe_com,
James Smart7851fe22011-07-22 18:36:52 -04007508 iocbq->iocb.unsli3.rcvsli3.ox_id);
James Smart5ffc2662009-11-18 15:39:44 -05007509 /* The entire sequence is transmitted for this IOCB */
7510 xmit_len = total_len;
7511 cmnd = CMD_XMIT_SEQUENCE64_CR;
James Smart4f774512009-05-22 14:52:35 -04007512 case CMD_XMIT_SEQUENCE64_CR:
James Smartf0d9bcc2010-10-22 11:07:09 -04007513 /* word3 iocb=io_tag32 wqe=reserved */
7514 wqe->xmit_sequence.rsvd3 = 0;
James Smart4f774512009-05-22 14:52:35 -04007515 /* word4 relative_offset memcpy */
7516 /* word5 r_ctl/df_ctl memcpy */
James Smartf0d9bcc2010-10-22 11:07:09 -04007517 bf_set(wqe_pu, &wqe->xmit_sequence.wqe_com, 0);
7518 bf_set(wqe_dbde, &wqe->xmit_sequence.wqe_com, 1);
7519 bf_set(wqe_iod, &wqe->xmit_sequence.wqe_com,
7520 LPFC_WQE_IOD_WRITE);
7521 bf_set(wqe_lenloc, &wqe->xmit_sequence.wqe_com,
7522 LPFC_WQE_LENLOC_WORD12);
7523 bf_set(wqe_ebde_cnt, &wqe->xmit_sequence.wqe_com, 0);
James Smart5ffc2662009-11-18 15:39:44 -05007524 wqe->xmit_sequence.xmit_len = xmit_len;
7525 command_type = OTHER_COMMAND;
James Smart7851fe22011-07-22 18:36:52 -04007526 break;
James Smart4f774512009-05-22 14:52:35 -04007527 case CMD_XMIT_BCAST64_CN:
James Smartf0d9bcc2010-10-22 11:07:09 -04007528 /* word3 iocb=iotag32 wqe=seq_payload_len */
7529 wqe->xmit_bcast64.seq_payload_len = xmit_len;
James Smart4f774512009-05-22 14:52:35 -04007530 /* word4 iocb=rsvd wqe=rsvd */
7531 /* word5 iocb=rctl/type/df_ctl wqe=rctl/type/df_ctl memcpy */
7532 /* word6 iocb=ctxt_tag/io_tag wqe=ctxt_tag/xri */
James Smartf0d9bcc2010-10-22 11:07:09 -04007533 bf_set(wqe_ct, &wqe->xmit_bcast64.wqe_com,
James Smart4f774512009-05-22 14:52:35 -04007534 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
James Smartf0d9bcc2010-10-22 11:07:09 -04007535 bf_set(wqe_dbde, &wqe->xmit_bcast64.wqe_com, 1);
7536 bf_set(wqe_iod, &wqe->xmit_bcast64.wqe_com, LPFC_WQE_IOD_WRITE);
7537 bf_set(wqe_lenloc, &wqe->xmit_bcast64.wqe_com,
7538 LPFC_WQE_LENLOC_WORD3);
7539 bf_set(wqe_ebde_cnt, &wqe->xmit_bcast64.wqe_com, 0);
James Smart7851fe22011-07-22 18:36:52 -04007540 break;
James Smart4f774512009-05-22 14:52:35 -04007541 case CMD_FCP_IWRITE64_CR:
7542 command_type = FCP_COMMAND_DATA_OUT;
James Smartf0d9bcc2010-10-22 11:07:09 -04007543 /* word3 iocb=iotag wqe=payload_offset_len */
7544 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
7545 wqe->fcp_iwrite.payload_offset_len =
James Smart5ffc2662009-11-18 15:39:44 -05007546 xmit_len + sizeof(struct fcp_rsp);
James Smartf0d9bcc2010-10-22 11:07:09 -04007547 /* 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_iwrite.wqe_com,
7550 iocbq->iocb.ulpFCP2Rcvy);
7551 bf_set(wqe_lnk, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpXS);
7552 /* Always open the exchange */
7553 bf_set(wqe_xc, &wqe->fcp_iwrite.wqe_com, 0);
7554 bf_set(wqe_dbde, &wqe->fcp_iwrite.wqe_com, 1);
7555 bf_set(wqe_iod, &wqe->fcp_iwrite.wqe_com, LPFC_WQE_IOD_WRITE);
7556 bf_set(wqe_lenloc, &wqe->fcp_iwrite.wqe_com,
7557 LPFC_WQE_LENLOC_WORD4);
7558 bf_set(wqe_ebde_cnt, &wqe->fcp_iwrite.wqe_com, 0);
7559 bf_set(wqe_pu, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpPU);
James Smart7851fe22011-07-22 18:36:52 -04007560 break;
James Smartf0d9bcc2010-10-22 11:07:09 -04007561 case CMD_FCP_IREAD64_CR:
7562 /* word3 iocb=iotag wqe=payload_offset_len */
7563 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
7564 wqe->fcp_iread.payload_offset_len =
7565 xmit_len + sizeof(struct fcp_rsp);
7566 /* word4 iocb=parameter wqe=total_xfer_length memcpy */
7567 /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
7568 bf_set(wqe_erp, &wqe->fcp_iread.wqe_com,
7569 iocbq->iocb.ulpFCP2Rcvy);
7570 bf_set(wqe_lnk, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpXS);
James Smart4f774512009-05-22 14:52:35 -04007571 /* Always open the exchange */
7572 bf_set(wqe_xc, &wqe->fcp_iread.wqe_com, 0);
James Smartf0d9bcc2010-10-22 11:07:09 -04007573 bf_set(wqe_dbde, &wqe->fcp_iread.wqe_com, 1);
7574 bf_set(wqe_iod, &wqe->fcp_iread.wqe_com, LPFC_WQE_IOD_READ);
7575 bf_set(wqe_lenloc, &wqe->fcp_iread.wqe_com,
7576 LPFC_WQE_LENLOC_WORD4);
7577 bf_set(wqe_ebde_cnt, &wqe->fcp_iread.wqe_com, 0);
7578 bf_set(wqe_pu, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpPU);
James Smart7851fe22011-07-22 18:36:52 -04007579 break;
James Smartf1126682009-06-10 17:22:44 -04007580 case CMD_FCP_ICMND64_CR:
James Smartf0d9bcc2010-10-22 11:07:09 -04007581 /* word3 iocb=IO_TAG wqe=reserved */
7582 wqe->fcp_icmd.rsrvd3 = 0;
7583 bf_set(wqe_pu, &wqe->fcp_icmd.wqe_com, 0);
James Smartf1126682009-06-10 17:22:44 -04007584 /* Always open the exchange */
James Smartf0d9bcc2010-10-22 11:07:09 -04007585 bf_set(wqe_xc, &wqe->fcp_icmd.wqe_com, 0);
7586 bf_set(wqe_dbde, &wqe->fcp_icmd.wqe_com, 1);
7587 bf_set(wqe_iod, &wqe->fcp_icmd.wqe_com, LPFC_WQE_IOD_WRITE);
7588 bf_set(wqe_qosd, &wqe->fcp_icmd.wqe_com, 1);
7589 bf_set(wqe_lenloc, &wqe->fcp_icmd.wqe_com,
7590 LPFC_WQE_LENLOC_NONE);
7591 bf_set(wqe_ebde_cnt, &wqe->fcp_icmd.wqe_com, 0);
James Smart7851fe22011-07-22 18:36:52 -04007592 break;
James Smart4f774512009-05-22 14:52:35 -04007593 case CMD_GEN_REQUEST64_CR:
James Smart63e801c2010-11-20 23:14:19 -05007594 /* For this command calculate the xmit length of the
7595 * request bde.
7596 */
7597 xmit_len = 0;
7598 numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
7599 sizeof(struct ulp_bde64);
7600 for (i = 0; i < numBdes; i++) {
James Smart63e801c2010-11-20 23:14:19 -05007601 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
James Smart546fc852011-03-11 16:06:29 -05007602 if (bde.tus.f.bdeFlags != BUFF_TYPE_BDE_64)
7603 break;
James Smart63e801c2010-11-20 23:14:19 -05007604 xmit_len += bde.tus.f.bdeSize;
7605 }
James Smartf0d9bcc2010-10-22 11:07:09 -04007606 /* word3 iocb=IO_TAG wqe=request_payload_len */
7607 wqe->gen_req.request_payload_len = xmit_len;
7608 /* word4 iocb=parameter wqe=relative_offset memcpy */
7609 /* word5 [rctl, type, df_ctl, la] copied in memcpy */
James Smart4f774512009-05-22 14:52:35 -04007610 /* word6 context tag copied in memcpy */
7611 if (iocbq->iocb.ulpCt_h || iocbq->iocb.ulpCt_l) {
7612 ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
7613 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
7614 "2015 Invalid CT %x command 0x%x\n",
7615 ct, iocbq->iocb.ulpCommand);
7616 return IOCB_ERROR;
7617 }
James Smartf0d9bcc2010-10-22 11:07:09 -04007618 bf_set(wqe_ct, &wqe->gen_req.wqe_com, 0);
7619 bf_set(wqe_tmo, &wqe->gen_req.wqe_com, iocbq->iocb.ulpTimeout);
7620 bf_set(wqe_pu, &wqe->gen_req.wqe_com, iocbq->iocb.ulpPU);
7621 bf_set(wqe_dbde, &wqe->gen_req.wqe_com, 1);
7622 bf_set(wqe_iod, &wqe->gen_req.wqe_com, LPFC_WQE_IOD_READ);
7623 bf_set(wqe_qosd, &wqe->gen_req.wqe_com, 1);
7624 bf_set(wqe_lenloc, &wqe->gen_req.wqe_com, LPFC_WQE_LENLOC_NONE);
7625 bf_set(wqe_ebde_cnt, &wqe->gen_req.wqe_com, 0);
James Smart4f774512009-05-22 14:52:35 -04007626 command_type = OTHER_COMMAND;
James Smart7851fe22011-07-22 18:36:52 -04007627 break;
James Smart4f774512009-05-22 14:52:35 -04007628 case CMD_XMIT_ELS_RSP64_CX:
James Smartc31098c2011-04-16 11:03:33 -04007629 ndlp = (struct lpfc_nodelist *)iocbq->context1;
James Smart4f774512009-05-22 14:52:35 -04007630 /* words0-2 BDE memcpy */
James Smartf0d9bcc2010-10-22 11:07:09 -04007631 /* word3 iocb=iotag32 wqe=response_payload_len */
7632 wqe->xmit_els_rsp.response_payload_len = xmit_len;
James Smart4f774512009-05-22 14:52:35 -04007633 /* word4 iocb=did wge=rsvd. */
James Smartf0d9bcc2010-10-22 11:07:09 -04007634 wqe->xmit_els_rsp.rsvd4 = 0;
James Smart4f774512009-05-22 14:52:35 -04007635 /* word5 iocb=rsvd wge=did */
7636 bf_set(wqe_els_did, &wqe->xmit_els_rsp.wqe_dest,
7637 iocbq->iocb.un.elsreq64.remoteID);
James Smartf0d9bcc2010-10-22 11:07:09 -04007638 bf_set(wqe_ct, &wqe->xmit_els_rsp.wqe_com,
7639 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
7640 bf_set(wqe_pu, &wqe->xmit_els_rsp.wqe_com, iocbq->iocb.ulpPU);
7641 bf_set(wqe_rcvoxid, &wqe->xmit_els_rsp.wqe_com,
James Smart7851fe22011-07-22 18:36:52 -04007642 iocbq->iocb.unsli3.rcvsli3.ox_id);
James Smart4f774512009-05-22 14:52:35 -04007643 if (!iocbq->iocb.ulpCt_h && iocbq->iocb.ulpCt_l)
James Smartf0d9bcc2010-10-22 11:07:09 -04007644 bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
James Smart6d368e52011-05-24 11:44:12 -04007645 phba->vpi_ids[iocbq->vport->vpi]);
James Smartf0d9bcc2010-10-22 11:07:09 -04007646 bf_set(wqe_dbde, &wqe->xmit_els_rsp.wqe_com, 1);
7647 bf_set(wqe_iod, &wqe->xmit_els_rsp.wqe_com, LPFC_WQE_IOD_WRITE);
7648 bf_set(wqe_qosd, &wqe->xmit_els_rsp.wqe_com, 1);
7649 bf_set(wqe_lenloc, &wqe->xmit_els_rsp.wqe_com,
7650 LPFC_WQE_LENLOC_WORD3);
7651 bf_set(wqe_ebde_cnt, &wqe->xmit_els_rsp.wqe_com, 0);
James Smart6d368e52011-05-24 11:44:12 -04007652 bf_set(wqe_rsp_temp_rpi, &wqe->xmit_els_rsp,
7653 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
James Smart4f774512009-05-22 14:52:35 -04007654 command_type = OTHER_COMMAND;
James Smart7851fe22011-07-22 18:36:52 -04007655 break;
James Smart4f774512009-05-22 14:52:35 -04007656 case CMD_CLOSE_XRI_CN:
7657 case CMD_ABORT_XRI_CN:
7658 case CMD_ABORT_XRI_CX:
7659 /* words 0-2 memcpy should be 0 rserved */
7660 /* port will send abts */
James Smartdcf2a4e2010-09-29 11:18:53 -04007661 abrt_iotag = iocbq->iocb.un.acxri.abortContextTag;
7662 if (abrt_iotag != 0 && abrt_iotag <= phba->sli.last_iotag) {
7663 abrtiocbq = phba->sli.iocbq_lookup[abrt_iotag];
7664 fip = abrtiocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK;
7665 } else
7666 fip = 0;
7667
7668 if ((iocbq->iocb.ulpCommand == CMD_CLOSE_XRI_CN) || fip)
James Smart4f774512009-05-22 14:52:35 -04007669 /*
James Smartdcf2a4e2010-09-29 11:18:53 -04007670 * The link is down, or the command was ELS_FIP
7671 * so the fw does not need to send abts
James Smart4f774512009-05-22 14:52:35 -04007672 * on the wire.
7673 */
7674 bf_set(abort_cmd_ia, &wqe->abort_cmd, 1);
7675 else
7676 bf_set(abort_cmd_ia, &wqe->abort_cmd, 0);
7677 bf_set(abort_cmd_criteria, &wqe->abort_cmd, T_XRI_TAG);
James Smartf0d9bcc2010-10-22 11:07:09 -04007678 /* word5 iocb=CONTEXT_TAG|IO_TAG wqe=reserved */
7679 wqe->abort_cmd.rsrvd5 = 0;
7680 bf_set(wqe_ct, &wqe->abort_cmd.wqe_com,
James Smart4f774512009-05-22 14:52:35 -04007681 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
7682 abort_tag = iocbq->iocb.un.acxri.abortIoTag;
James Smart4f774512009-05-22 14:52:35 -04007683 /*
7684 * The abort handler will send us CMD_ABORT_XRI_CN or
7685 * CMD_CLOSE_XRI_CN and the fw only accepts CMD_ABORT_XRI_CX
7686 */
James Smartf0d9bcc2010-10-22 11:07:09 -04007687 bf_set(wqe_cmnd, &wqe->abort_cmd.wqe_com, CMD_ABORT_XRI_CX);
7688 bf_set(wqe_qosd, &wqe->abort_cmd.wqe_com, 1);
7689 bf_set(wqe_lenloc, &wqe->abort_cmd.wqe_com,
7690 LPFC_WQE_LENLOC_NONE);
James Smart4f774512009-05-22 14:52:35 -04007691 cmnd = CMD_ABORT_XRI_CX;
7692 command_type = OTHER_COMMAND;
7693 xritag = 0;
James Smart7851fe22011-07-22 18:36:52 -04007694 break;
James Smart6669f9b2009-10-02 15:16:45 -04007695 case CMD_XMIT_BLS_RSP64_CX:
James Smart546fc852011-03-11 16:06:29 -05007696 /* As BLS ABTS RSP WQE is very different from other WQEs,
James Smart6669f9b2009-10-02 15:16:45 -04007697 * we re-construct this WQE here based on information in
7698 * iocbq from scratch.
7699 */
7700 memset(wqe, 0, sizeof(union lpfc_wqe));
James Smart5ffc2662009-11-18 15:39:44 -05007701 /* OX_ID is invariable to who sent ABTS to CT exchange */
James Smart6669f9b2009-10-02 15:16:45 -04007702 bf_set(xmit_bls_rsp64_oxid, &wqe->xmit_bls_rsp,
James Smart546fc852011-03-11 16:06:29 -05007703 bf_get(lpfc_abts_oxid, &iocbq->iocb.un.bls_rsp));
7704 if (bf_get(lpfc_abts_orig, &iocbq->iocb.un.bls_rsp) ==
James Smart5ffc2662009-11-18 15:39:44 -05007705 LPFC_ABTS_UNSOL_INT) {
7706 /* ABTS sent by initiator to CT exchange, the
7707 * RX_ID field will be filled with the newly
7708 * allocated responder XRI.
7709 */
7710 bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
7711 iocbq->sli4_xritag);
7712 } else {
7713 /* ABTS sent by responder to CT exchange, the
7714 * RX_ID field will be filled with the responder
7715 * RX_ID from ABTS.
7716 */
7717 bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
James Smart546fc852011-03-11 16:06:29 -05007718 bf_get(lpfc_abts_rxid, &iocbq->iocb.un.bls_rsp));
James Smart5ffc2662009-11-18 15:39:44 -05007719 }
James Smart6669f9b2009-10-02 15:16:45 -04007720 bf_set(xmit_bls_rsp64_seqcnthi, &wqe->xmit_bls_rsp, 0xffff);
7721 bf_set(wqe_xmit_bls_pt, &wqe->xmit_bls_rsp.wqe_dest, 0x1);
7722 bf_set(wqe_ctxt_tag, &wqe->xmit_bls_rsp.wqe_com,
7723 iocbq->iocb.ulpContext);
James Smartf0d9bcc2010-10-22 11:07:09 -04007724 bf_set(wqe_qosd, &wqe->xmit_bls_rsp.wqe_com, 1);
7725 bf_set(wqe_lenloc, &wqe->xmit_bls_rsp.wqe_com,
7726 LPFC_WQE_LENLOC_NONE);
James Smart6669f9b2009-10-02 15:16:45 -04007727 /* Overwrite the pre-set comnd type with OTHER_COMMAND */
7728 command_type = OTHER_COMMAND;
James Smart546fc852011-03-11 16:06:29 -05007729 if (iocbq->iocb.un.xseq64.w5.hcsw.Rctl == FC_RCTL_BA_RJT) {
7730 bf_set(xmit_bls_rsp64_rjt_vspec, &wqe->xmit_bls_rsp,
7731 bf_get(lpfc_vndr_code, &iocbq->iocb.un.bls_rsp));
7732 bf_set(xmit_bls_rsp64_rjt_expc, &wqe->xmit_bls_rsp,
7733 bf_get(lpfc_rsn_expln, &iocbq->iocb.un.bls_rsp));
7734 bf_set(xmit_bls_rsp64_rjt_rsnc, &wqe->xmit_bls_rsp,
7735 bf_get(lpfc_rsn_code, &iocbq->iocb.un.bls_rsp));
7736 }
7737
James Smart7851fe22011-07-22 18:36:52 -04007738 break;
James Smart4f774512009-05-22 14:52:35 -04007739 case CMD_XRI_ABORTED_CX:
7740 case CMD_CREATE_XRI_CR: /* Do we expect to use this? */
James Smart4f774512009-05-22 14:52:35 -04007741 case CMD_IOCB_FCP_IBIDIR64_CR: /* bidirectional xfer */
7742 case CMD_FCP_TSEND64_CX: /* Target mode send xfer-ready */
7743 case CMD_FCP_TRSP64_CX: /* Target mode rcv */
7744 case CMD_FCP_AUTO_TRSP_CX: /* Auto target rsp */
7745 default:
7746 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
7747 "2014 Invalid command 0x%x\n",
7748 iocbq->iocb.ulpCommand);
7749 return IOCB_ERROR;
James Smart7851fe22011-07-22 18:36:52 -04007750 break;
James Smart4f774512009-05-22 14:52:35 -04007751 }
James Smart6d368e52011-05-24 11:44:12 -04007752
James Smartf0d9bcc2010-10-22 11:07:09 -04007753 bf_set(wqe_xri_tag, &wqe->generic.wqe_com, xritag);
7754 bf_set(wqe_reqtag, &wqe->generic.wqe_com, iocbq->iotag);
7755 wqe->generic.wqe_com.abort_tag = abort_tag;
7756 bf_set(wqe_cmd_type, &wqe->generic.wqe_com, command_type);
7757 bf_set(wqe_cmnd, &wqe->generic.wqe_com, cmnd);
7758 bf_set(wqe_class, &wqe->generic.wqe_com, iocbq->iocb.ulpClass);
7759 bf_set(wqe_cqid, &wqe->generic.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
James Smart4f774512009-05-22 14:52:35 -04007760 return 0;
7761}
7762
7763/**
7764 * __lpfc_sli_issue_iocb_s4 - SLI4 device lockless ver of lpfc_sli_issue_iocb
7765 * @phba: Pointer to HBA context object.
7766 * @ring_number: SLI ring number to issue iocb on.
7767 * @piocb: Pointer to command iocb.
7768 * @flag: Flag indicating if this command can be put into txq.
7769 *
7770 * __lpfc_sli_issue_iocb_s4 is used by other functions in the driver to issue
7771 * an iocb command to an HBA with SLI-4 interface spec.
7772 *
7773 * This function is called with hbalock held. The function will return success
7774 * after it successfully submit the iocb to firmware or after adding to the
7775 * txq.
7776 **/
7777static int
7778__lpfc_sli_issue_iocb_s4(struct lpfc_hba *phba, uint32_t ring_number,
7779 struct lpfc_iocbq *piocb, uint32_t flag)
7780{
7781 struct lpfc_sglq *sglq;
James Smart4f774512009-05-22 14:52:35 -04007782 union lpfc_wqe wqe;
7783 struct lpfc_sli_ring *pring = &phba->sli.ring[ring_number];
James Smart4f774512009-05-22 14:52:35 -04007784
7785 if (piocb->sli4_xritag == NO_XRI) {
7786 if (piocb->iocb.ulpCommand == CMD_ABORT_XRI_CN ||
James Smart546fc852011-03-11 16:06:29 -05007787 piocb->iocb.ulpCommand == CMD_CLOSE_XRI_CN ||
7788 piocb->iocb.ulpCommand == CMD_XMIT_BLS_RSP64_CX)
James Smart4f774512009-05-22 14:52:35 -04007789 sglq = NULL;
7790 else {
James Smart2a9bf3d2010-06-07 15:24:45 -04007791 if (pring->txq_cnt) {
7792 if (!(flag & SLI_IOCB_RET_IOCB)) {
7793 __lpfc_sli_ringtx_put(phba,
7794 pring, piocb);
7795 return IOCB_SUCCESS;
7796 } else {
7797 return IOCB_BUSY;
7798 }
7799 } else {
James Smart6d368e52011-05-24 11:44:12 -04007800 sglq = __lpfc_sli_get_sglq(phba, piocb);
James Smart2a9bf3d2010-06-07 15:24:45 -04007801 if (!sglq) {
7802 if (!(flag & SLI_IOCB_RET_IOCB)) {
7803 __lpfc_sli_ringtx_put(phba,
7804 pring,
7805 piocb);
7806 return IOCB_SUCCESS;
7807 } else
7808 return IOCB_BUSY;
7809 }
7810 }
James Smart4f774512009-05-22 14:52:35 -04007811 }
7812 } else if (piocb->iocb_flag & LPFC_IO_FCP) {
James Smart6d368e52011-05-24 11:44:12 -04007813 /* These IO's already have an XRI and a mapped sgl. */
7814 sglq = NULL;
James Smart4f774512009-05-22 14:52:35 -04007815 } else {
James Smart6d368e52011-05-24 11:44:12 -04007816 /*
7817 * This is a continuation of a commandi,(CX) so this
James Smart4f774512009-05-22 14:52:35 -04007818 * sglq is on the active list
7819 */
7820 sglq = __lpfc_get_active_sglq(phba, piocb->sli4_xritag);
7821 if (!sglq)
7822 return IOCB_ERROR;
7823 }
7824
7825 if (sglq) {
James Smart6d368e52011-05-24 11:44:12 -04007826 piocb->sli4_lxritag = sglq->sli4_lxritag;
James Smart2a9bf3d2010-06-07 15:24:45 -04007827 piocb->sli4_xritag = sglq->sli4_xritag;
James Smart2a9bf3d2010-06-07 15:24:45 -04007828 if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocb, sglq))
James Smart4f774512009-05-22 14:52:35 -04007829 return IOCB_ERROR;
7830 }
7831
7832 if (lpfc_sli4_iocb2wqe(phba, piocb, &wqe))
7833 return IOCB_ERROR;
7834
James Smart341af102010-01-26 23:07:37 -05007835 if ((piocb->iocb_flag & LPFC_IO_FCP) ||
7836 (piocb->iocb_flag & LPFC_USE_FCPWQIDX)) {
James Smart5ffc2662009-11-18 15:39:44 -05007837 /*
7838 * For FCP command IOCB, get a new WQ index to distribute
7839 * WQE across the WQsr. On the other hand, for abort IOCB,
7840 * it carries the same WQ index to the original command
7841 * IOCB.
7842 */
James Smart341af102010-01-26 23:07:37 -05007843 if (piocb->iocb_flag & LPFC_IO_FCP)
James Smart5ffc2662009-11-18 15:39:44 -05007844 piocb->fcp_wqidx = lpfc_sli4_scmd_to_wqidx_distr(phba);
7845 if (lpfc_sli4_wq_put(phba->sli4_hba.fcp_wq[piocb->fcp_wqidx],
7846 &wqe))
James Smart4f774512009-05-22 14:52:35 -04007847 return IOCB_ERROR;
7848 } else {
7849 if (lpfc_sli4_wq_put(phba->sli4_hba.els_wq, &wqe))
7850 return IOCB_ERROR;
7851 }
7852 lpfc_sli_ringtxcmpl_put(phba, pring, piocb);
7853
7854 return 0;
7855}
7856
7857/**
James Smart3772a992009-05-22 14:50:54 -04007858 * __lpfc_sli_issue_iocb - Wrapper func of lockless version for issuing iocb
7859 *
7860 * This routine wraps the actual lockless version for issusing IOCB function
7861 * pointer from the lpfc_hba struct.
7862 *
7863 * Return codes:
7864 * IOCB_ERROR - Error
7865 * IOCB_SUCCESS - Success
7866 * IOCB_BUSY - Busy
7867 **/
James Smart2a9bf3d2010-06-07 15:24:45 -04007868int
James Smart3772a992009-05-22 14:50:54 -04007869__lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
7870 struct lpfc_iocbq *piocb, uint32_t flag)
7871{
7872 return phba->__lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
7873}
7874
7875/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03007876 * lpfc_sli_api_table_setup - Set up sli api function jump table
James Smart3772a992009-05-22 14:50:54 -04007877 * @phba: The hba struct for which this call is being executed.
7878 * @dev_grp: The HBA PCI-Device group number.
7879 *
7880 * This routine sets up the SLI interface API function jump table in @phba
7881 * struct.
7882 * Returns: 0 - success, -ENODEV - failure.
7883 **/
7884int
7885lpfc_sli_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
7886{
7887
7888 switch (dev_grp) {
7889 case LPFC_PCI_DEV_LP:
7890 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s3;
7891 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s3;
7892 break;
James Smart4f774512009-05-22 14:52:35 -04007893 case LPFC_PCI_DEV_OC:
7894 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s4;
7895 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s4;
7896 break;
James Smart3772a992009-05-22 14:50:54 -04007897 default:
7898 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7899 "1419 Invalid HBA PCI-device group: 0x%x\n",
7900 dev_grp);
7901 return -ENODEV;
7902 break;
7903 }
7904 phba->lpfc_get_iocb_from_iocbq = lpfc_get_iocb_from_iocbq;
7905 return 0;
7906}
James Smart92d7f7b2007-06-17 19:56:38 -05007907
James Smarte59058c2008-08-24 21:49:00 -04007908/**
James Smart3621a712009-04-06 18:47:14 -04007909 * lpfc_sli_issue_iocb - Wrapper function for __lpfc_sli_issue_iocb
James Smarte59058c2008-08-24 21:49:00 -04007910 * @phba: Pointer to HBA context object.
7911 * @pring: Pointer to driver SLI ring object.
7912 * @piocb: Pointer to command iocb.
7913 * @flag: Flag indicating if this command can be put into txq.
7914 *
7915 * lpfc_sli_issue_iocb is a wrapper around __lpfc_sli_issue_iocb
7916 * function. This function gets the hbalock and calls
7917 * __lpfc_sli_issue_iocb function and will return the error returned
7918 * by __lpfc_sli_issue_iocb function. This wrapper is used by
7919 * functions which do not hold hbalock.
7920 **/
James Smart92d7f7b2007-06-17 19:56:38 -05007921int
James Smart3772a992009-05-22 14:50:54 -04007922lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
James Smart92d7f7b2007-06-17 19:56:38 -05007923 struct lpfc_iocbq *piocb, uint32_t flag)
7924{
7925 unsigned long iflags;
7926 int rc;
7927
7928 spin_lock_irqsave(&phba->hbalock, iflags);
James Smart3772a992009-05-22 14:50:54 -04007929 rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
James Smart92d7f7b2007-06-17 19:56:38 -05007930 spin_unlock_irqrestore(&phba->hbalock, iflags);
7931
7932 return rc;
7933}
7934
James Smarte59058c2008-08-24 21:49:00 -04007935/**
James Smart3621a712009-04-06 18:47:14 -04007936 * lpfc_extra_ring_setup - Extra ring setup function
James Smarte59058c2008-08-24 21:49:00 -04007937 * @phba: Pointer to HBA context object.
7938 *
7939 * This function is called while driver attaches with the
7940 * HBA to setup the extra ring. The extra ring is used
7941 * only when driver needs to support target mode functionality
7942 * or IP over FC functionalities.
7943 *
7944 * This function is called with no lock held.
7945 **/
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05007946static int
7947lpfc_extra_ring_setup( struct lpfc_hba *phba)
7948{
7949 struct lpfc_sli *psli;
7950 struct lpfc_sli_ring *pring;
7951
7952 psli = &phba->sli;
7953
7954 /* Adjust cmd/rsp ring iocb entries more evenly */
James Smarta4bc3372006-12-02 13:34:16 -05007955
7956 /* Take some away from the FCP ring */
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05007957 pring = &psli->ring[psli->fcp_ring];
7958 pring->numCiocb -= SLI2_IOCB_CMD_R1XTRA_ENTRIES;
7959 pring->numRiocb -= SLI2_IOCB_RSP_R1XTRA_ENTRIES;
7960 pring->numCiocb -= SLI2_IOCB_CMD_R3XTRA_ENTRIES;
7961 pring->numRiocb -= SLI2_IOCB_RSP_R3XTRA_ENTRIES;
7962
James Smarta4bc3372006-12-02 13:34:16 -05007963 /* and give them to the extra ring */
7964 pring = &psli->ring[psli->extra_ring];
7965
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05007966 pring->numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
7967 pring->numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
7968 pring->numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
7969 pring->numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
7970
7971 /* Setup default profile for this ring */
7972 pring->iotag_max = 4096;
7973 pring->num_mask = 1;
7974 pring->prt[0].profile = 0; /* Mask 0 */
James Smarta4bc3372006-12-02 13:34:16 -05007975 pring->prt[0].rctl = phba->cfg_multi_ring_rctl;
7976 pring->prt[0].type = phba->cfg_multi_ring_type;
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05007977 pring->prt[0].lpfc_sli_rcv_unsol_event = NULL;
7978 return 0;
7979}
7980
James Smarte59058c2008-08-24 21:49:00 -04007981/**
James Smart3621a712009-04-06 18:47:14 -04007982 * lpfc_sli_async_event_handler - ASYNC iocb handler function
James Smarte59058c2008-08-24 21:49:00 -04007983 * @phba: Pointer to HBA context object.
7984 * @pring: Pointer to driver SLI ring object.
7985 * @iocbq: Pointer to iocb object.
7986 *
7987 * This function is called by the slow ring event handler
7988 * function when there is an ASYNC event iocb in the ring.
7989 * This function is called with no lock held.
7990 * Currently this function handles only temperature related
7991 * ASYNC events. The function decodes the temperature sensor
7992 * event message and posts events for the management applications.
7993 **/
James Smart98c9ea52007-10-27 13:37:33 -04007994static void
James Smart57127f12007-10-27 13:37:05 -04007995lpfc_sli_async_event_handler(struct lpfc_hba * phba,
7996 struct lpfc_sli_ring * pring, struct lpfc_iocbq * iocbq)
7997{
7998 IOCB_t *icmd;
7999 uint16_t evt_code;
8000 uint16_t temp;
8001 struct temp_event temp_event_data;
8002 struct Scsi_Host *shost;
James Smarta257bf92009-04-06 18:48:10 -04008003 uint32_t *iocb_w;
James Smart57127f12007-10-27 13:37:05 -04008004
8005 icmd = &iocbq->iocb;
8006 evt_code = icmd->un.asyncstat.evt_code;
8007 temp = icmd->ulpContext;
8008
8009 if ((evt_code != ASYNC_TEMP_WARN) &&
8010 (evt_code != ASYNC_TEMP_SAFE)) {
James Smarta257bf92009-04-06 18:48:10 -04008011 iocb_w = (uint32_t *) icmd;
James Smart57127f12007-10-27 13:37:05 -04008012 lpfc_printf_log(phba,
8013 KERN_ERR,
8014 LOG_SLI,
James Smart76bb24e2007-10-27 13:38:00 -04008015 "0346 Ring %d handler: unexpected ASYNC_STATUS"
James Smarte4e74272009-07-19 10:01:38 -04008016 " evt_code 0x%x\n"
James Smarta257bf92009-04-06 18:48:10 -04008017 "W0 0x%08x W1 0x%08x W2 0x%08x W3 0x%08x\n"
8018 "W4 0x%08x W5 0x%08x W6 0x%08x W7 0x%08x\n"
8019 "W8 0x%08x W9 0x%08x W10 0x%08x W11 0x%08x\n"
8020 "W12 0x%08x W13 0x%08x W14 0x%08x W15 0x%08x\n",
James Smart57127f12007-10-27 13:37:05 -04008021 pring->ringno,
James Smarta257bf92009-04-06 18:48:10 -04008022 icmd->un.asyncstat.evt_code,
8023 iocb_w[0], iocb_w[1], iocb_w[2], iocb_w[3],
8024 iocb_w[4], iocb_w[5], iocb_w[6], iocb_w[7],
8025 iocb_w[8], iocb_w[9], iocb_w[10], iocb_w[11],
8026 iocb_w[12], iocb_w[13], iocb_w[14], iocb_w[15]);
8027
James Smart57127f12007-10-27 13:37:05 -04008028 return;
8029 }
8030 temp_event_data.data = (uint32_t)temp;
8031 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
8032 if (evt_code == ASYNC_TEMP_WARN) {
8033 temp_event_data.event_code = LPFC_THRESHOLD_TEMP;
8034 lpfc_printf_log(phba,
James Smart09372822008-01-11 01:52:54 -05008035 KERN_ERR,
James Smart57127f12007-10-27 13:37:05 -04008036 LOG_TEMP,
James Smart76bb24e2007-10-27 13:38:00 -04008037 "0347 Adapter is very hot, please take "
James Smart57127f12007-10-27 13:37:05 -04008038 "corrective action. temperature : %d Celsius\n",
8039 temp);
8040 }
8041 if (evt_code == ASYNC_TEMP_SAFE) {
8042 temp_event_data.event_code = LPFC_NORMAL_TEMP;
8043 lpfc_printf_log(phba,
James Smart09372822008-01-11 01:52:54 -05008044 KERN_ERR,
James Smart57127f12007-10-27 13:37:05 -04008045 LOG_TEMP,
8046 "0340 Adapter temperature is OK now. "
8047 "temperature : %d Celsius\n",
8048 temp);
8049 }
8050
8051 /* Send temperature change event to applications */
8052 shost = lpfc_shost_from_vport(phba->pport);
8053 fc_host_post_vendor_event(shost, fc_get_event_number(),
8054 sizeof(temp_event_data), (char *) &temp_event_data,
James Smartddcc50f2008-12-04 22:38:46 -05008055 LPFC_NL_VENDOR_ID);
James Smart57127f12007-10-27 13:37:05 -04008056
8057}
8058
8059
James Smarte59058c2008-08-24 21:49:00 -04008060/**
James Smart3621a712009-04-06 18:47:14 -04008061 * lpfc_sli_setup - SLI ring setup function
James Smarte59058c2008-08-24 21:49:00 -04008062 * @phba: Pointer to HBA context object.
8063 *
8064 * lpfc_sli_setup sets up rings of the SLI interface with
8065 * number of iocbs per ring and iotags. This function is
8066 * called while driver attach to the HBA and before the
8067 * interrupts are enabled. So there is no need for locking.
8068 *
8069 * This function always returns 0.
8070 **/
dea31012005-04-17 16:05:31 -05008071int
8072lpfc_sli_setup(struct lpfc_hba *phba)
8073{
James Smarted957682007-06-17 19:56:37 -05008074 int i, totiocbsize = 0;
dea31012005-04-17 16:05:31 -05008075 struct lpfc_sli *psli = &phba->sli;
8076 struct lpfc_sli_ring *pring;
8077
8078 psli->num_rings = MAX_CONFIGURED_RINGS;
8079 psli->sli_flag = 0;
8080 psli->fcp_ring = LPFC_FCP_RING;
8081 psli->next_ring = LPFC_FCP_NEXT_RING;
James Smarta4bc3372006-12-02 13:34:16 -05008082 psli->extra_ring = LPFC_EXTRA_RING;
dea31012005-04-17 16:05:31 -05008083
James Bottomley604a3e32005-10-29 10:28:33 -05008084 psli->iocbq_lookup = NULL;
8085 psli->iocbq_lookup_len = 0;
8086 psli->last_iotag = 0;
8087
dea31012005-04-17 16:05:31 -05008088 for (i = 0; i < psli->num_rings; i++) {
8089 pring = &psli->ring[i];
8090 switch (i) {
8091 case LPFC_FCP_RING: /* ring 0 - FCP */
8092 /* numCiocb and numRiocb are used in config_port */
8093 pring->numCiocb = SLI2_IOCB_CMD_R0_ENTRIES;
8094 pring->numRiocb = SLI2_IOCB_RSP_R0_ENTRIES;
8095 pring->numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
8096 pring->numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
8097 pring->numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
8098 pring->numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
James Smarted957682007-06-17 19:56:37 -05008099 pring->sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05008100 SLI3_IOCB_CMD_SIZE :
8101 SLI2_IOCB_CMD_SIZE;
James Smarted957682007-06-17 19:56:37 -05008102 pring->sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05008103 SLI3_IOCB_RSP_SIZE :
8104 SLI2_IOCB_RSP_SIZE;
dea31012005-04-17 16:05:31 -05008105 pring->iotag_ctr = 0;
8106 pring->iotag_max =
James Smart92d7f7b2007-06-17 19:56:38 -05008107 (phba->cfg_hba_queue_depth * 2);
dea31012005-04-17 16:05:31 -05008108 pring->fast_iotag = pring->iotag_max;
8109 pring->num_mask = 0;
8110 break;
James Smarta4bc3372006-12-02 13:34:16 -05008111 case LPFC_EXTRA_RING: /* ring 1 - EXTRA */
dea31012005-04-17 16:05:31 -05008112 /* numCiocb and numRiocb are used in config_port */
8113 pring->numCiocb = SLI2_IOCB_CMD_R1_ENTRIES;
8114 pring->numRiocb = SLI2_IOCB_RSP_R1_ENTRIES;
James Smarted957682007-06-17 19:56:37 -05008115 pring->sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05008116 SLI3_IOCB_CMD_SIZE :
8117 SLI2_IOCB_CMD_SIZE;
James Smarted957682007-06-17 19:56:37 -05008118 pring->sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05008119 SLI3_IOCB_RSP_SIZE :
8120 SLI2_IOCB_RSP_SIZE;
James Smart2e0fef82007-06-17 19:56:36 -05008121 pring->iotag_max = phba->cfg_hba_queue_depth;
dea31012005-04-17 16:05:31 -05008122 pring->num_mask = 0;
8123 break;
8124 case LPFC_ELS_RING: /* ring 2 - ELS / CT */
8125 /* numCiocb and numRiocb are used in config_port */
8126 pring->numCiocb = SLI2_IOCB_CMD_R2_ENTRIES;
8127 pring->numRiocb = SLI2_IOCB_RSP_R2_ENTRIES;
James Smarted957682007-06-17 19:56:37 -05008128 pring->sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05008129 SLI3_IOCB_CMD_SIZE :
8130 SLI2_IOCB_CMD_SIZE;
James Smarted957682007-06-17 19:56:37 -05008131 pring->sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05008132 SLI3_IOCB_RSP_SIZE :
8133 SLI2_IOCB_RSP_SIZE;
dea31012005-04-17 16:05:31 -05008134 pring->fast_iotag = 0;
8135 pring->iotag_ctr = 0;
8136 pring->iotag_max = 4096;
James Smart57127f12007-10-27 13:37:05 -04008137 pring->lpfc_sli_rcv_async_status =
8138 lpfc_sli_async_event_handler;
James Smart6669f9b2009-10-02 15:16:45 -04008139 pring->num_mask = LPFC_MAX_RING_MASK;
dea31012005-04-17 16:05:31 -05008140 pring->prt[0].profile = 0; /* Mask 0 */
James Smart6a9c52c2009-10-02 15:16:51 -04008141 pring->prt[0].rctl = FC_RCTL_ELS_REQ;
8142 pring->prt[0].type = FC_TYPE_ELS;
dea31012005-04-17 16:05:31 -05008143 pring->prt[0].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05008144 lpfc_els_unsol_event;
dea31012005-04-17 16:05:31 -05008145 pring->prt[1].profile = 0; /* Mask 1 */
James Smart6a9c52c2009-10-02 15:16:51 -04008146 pring->prt[1].rctl = FC_RCTL_ELS_REP;
8147 pring->prt[1].type = FC_TYPE_ELS;
dea31012005-04-17 16:05:31 -05008148 pring->prt[1].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05008149 lpfc_els_unsol_event;
dea31012005-04-17 16:05:31 -05008150 pring->prt[2].profile = 0; /* Mask 2 */
8151 /* NameServer Inquiry */
James Smart6a9c52c2009-10-02 15:16:51 -04008152 pring->prt[2].rctl = FC_RCTL_DD_UNSOL_CTL;
dea31012005-04-17 16:05:31 -05008153 /* NameServer */
James Smart6a9c52c2009-10-02 15:16:51 -04008154 pring->prt[2].type = FC_TYPE_CT;
dea31012005-04-17 16:05:31 -05008155 pring->prt[2].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05008156 lpfc_ct_unsol_event;
dea31012005-04-17 16:05:31 -05008157 pring->prt[3].profile = 0; /* Mask 3 */
8158 /* NameServer response */
James Smart6a9c52c2009-10-02 15:16:51 -04008159 pring->prt[3].rctl = FC_RCTL_DD_SOL_CTL;
dea31012005-04-17 16:05:31 -05008160 /* NameServer */
James Smart6a9c52c2009-10-02 15:16:51 -04008161 pring->prt[3].type = FC_TYPE_CT;
dea31012005-04-17 16:05:31 -05008162 pring->prt[3].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05008163 lpfc_ct_unsol_event;
James Smart6669f9b2009-10-02 15:16:45 -04008164 /* abort unsolicited sequence */
8165 pring->prt[4].profile = 0; /* Mask 4 */
8166 pring->prt[4].rctl = FC_RCTL_BA_ABTS;
8167 pring->prt[4].type = FC_TYPE_BLS;
8168 pring->prt[4].lpfc_sli_rcv_unsol_event =
8169 lpfc_sli4_ct_abort_unsol_event;
dea31012005-04-17 16:05:31 -05008170 break;
8171 }
James Smarted957682007-06-17 19:56:37 -05008172 totiocbsize += (pring->numCiocb * pring->sizeCiocb) +
James Smart92d7f7b2007-06-17 19:56:38 -05008173 (pring->numRiocb * pring->sizeRiocb);
dea31012005-04-17 16:05:31 -05008174 }
James Smarted957682007-06-17 19:56:37 -05008175 if (totiocbsize > MAX_SLIM_IOCB_SIZE) {
dea31012005-04-17 16:05:31 -05008176 /* Too many cmd / rsp ring entries in SLI2 SLIM */
James Smarte8b62012007-08-02 11:10:09 -04008177 printk(KERN_ERR "%d:0462 Too many cmd / rsp ring entries in "
8178 "SLI2 SLIM Data: x%x x%lx\n",
8179 phba->brd_no, totiocbsize,
8180 (unsigned long) MAX_SLIM_IOCB_SIZE);
dea31012005-04-17 16:05:31 -05008181 }
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05008182 if (phba->cfg_multi_ring_support == 2)
8183 lpfc_extra_ring_setup(phba);
dea31012005-04-17 16:05:31 -05008184
8185 return 0;
8186}
8187
James Smarte59058c2008-08-24 21:49:00 -04008188/**
James Smart3621a712009-04-06 18:47:14 -04008189 * lpfc_sli_queue_setup - Queue initialization function
James Smarte59058c2008-08-24 21:49:00 -04008190 * @phba: Pointer to HBA context object.
8191 *
8192 * lpfc_sli_queue_setup sets up mailbox queues and iocb queues for each
8193 * ring. This function also initializes ring indices of each ring.
8194 * This function is called during the initialization of the SLI
8195 * interface of an HBA.
8196 * This function is called with no lock held and always returns
8197 * 1.
8198 **/
dea31012005-04-17 16:05:31 -05008199int
James Smart2e0fef82007-06-17 19:56:36 -05008200lpfc_sli_queue_setup(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05008201{
8202 struct lpfc_sli *psli;
8203 struct lpfc_sli_ring *pring;
James Bottomley604a3e32005-10-29 10:28:33 -05008204 int i;
dea31012005-04-17 16:05:31 -05008205
8206 psli = &phba->sli;
James Smart2e0fef82007-06-17 19:56:36 -05008207 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05008208 INIT_LIST_HEAD(&psli->mboxq);
James Smart92d7f7b2007-06-17 19:56:38 -05008209 INIT_LIST_HEAD(&psli->mboxq_cmpl);
dea31012005-04-17 16:05:31 -05008210 /* Initialize list headers for txq and txcmplq as double linked lists */
8211 for (i = 0; i < psli->num_rings; i++) {
8212 pring = &psli->ring[i];
8213 pring->ringno = i;
8214 pring->next_cmdidx = 0;
8215 pring->local_getidx = 0;
8216 pring->cmdidx = 0;
8217 INIT_LIST_HEAD(&pring->txq);
8218 INIT_LIST_HEAD(&pring->txcmplq);
8219 INIT_LIST_HEAD(&pring->iocb_continueq);
James Smart9c2face2008-01-11 01:53:18 -05008220 INIT_LIST_HEAD(&pring->iocb_continue_saveq);
dea31012005-04-17 16:05:31 -05008221 INIT_LIST_HEAD(&pring->postbufq);
dea31012005-04-17 16:05:31 -05008222 }
James Smart2e0fef82007-06-17 19:56:36 -05008223 spin_unlock_irq(&phba->hbalock);
8224 return 1;
dea31012005-04-17 16:05:31 -05008225}
8226
James Smarte59058c2008-08-24 21:49:00 -04008227/**
James Smart04c68492009-05-22 14:52:52 -04008228 * lpfc_sli_mbox_sys_flush - Flush mailbox command sub-system
8229 * @phba: Pointer to HBA context object.
8230 *
8231 * This routine flushes the mailbox command subsystem. It will unconditionally
8232 * flush all the mailbox commands in the three possible stages in the mailbox
8233 * command sub-system: pending mailbox command queue; the outstanding mailbox
8234 * command; and completed mailbox command queue. It is caller's responsibility
8235 * to make sure that the driver is in the proper state to flush the mailbox
8236 * command sub-system. Namely, the posting of mailbox commands into the
8237 * pending mailbox command queue from the various clients must be stopped;
8238 * either the HBA is in a state that it will never works on the outstanding
8239 * mailbox command (such as in EEH or ERATT conditions) or the outstanding
8240 * mailbox command has been completed.
8241 **/
8242static void
8243lpfc_sli_mbox_sys_flush(struct lpfc_hba *phba)
8244{
8245 LIST_HEAD(completions);
8246 struct lpfc_sli *psli = &phba->sli;
8247 LPFC_MBOXQ_t *pmb;
8248 unsigned long iflag;
8249
8250 /* Flush all the mailbox commands in the mbox system */
8251 spin_lock_irqsave(&phba->hbalock, iflag);
8252 /* The pending mailbox command queue */
8253 list_splice_init(&phba->sli.mboxq, &completions);
8254 /* The outstanding active mailbox command */
8255 if (psli->mbox_active) {
8256 list_add_tail(&psli->mbox_active->list, &completions);
8257 psli->mbox_active = NULL;
8258 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
8259 }
8260 /* The completed mailbox command queue */
8261 list_splice_init(&phba->sli.mboxq_cmpl, &completions);
8262 spin_unlock_irqrestore(&phba->hbalock, iflag);
8263
8264 /* Return all flushed mailbox commands with MBX_NOT_FINISHED status */
8265 while (!list_empty(&completions)) {
8266 list_remove_head(&completions, pmb, LPFC_MBOXQ_t, list);
8267 pmb->u.mb.mbxStatus = MBX_NOT_FINISHED;
8268 if (pmb->mbox_cmpl)
8269 pmb->mbox_cmpl(phba, pmb);
8270 }
8271}
8272
8273/**
James Smart3621a712009-04-06 18:47:14 -04008274 * lpfc_sli_host_down - Vport cleanup function
James Smarte59058c2008-08-24 21:49:00 -04008275 * @vport: Pointer to virtual port object.
8276 *
8277 * lpfc_sli_host_down is called to clean up the resources
8278 * associated with a vport before destroying virtual
8279 * port data structures.
8280 * This function does following operations:
8281 * - Free discovery resources associated with this virtual
8282 * port.
8283 * - Free iocbs associated with this virtual port in
8284 * the txq.
8285 * - Send abort for all iocb commands associated with this
8286 * vport in txcmplq.
8287 *
8288 * This function is called with no lock held and always returns 1.
8289 **/
dea31012005-04-17 16:05:31 -05008290int
James Smart92d7f7b2007-06-17 19:56:38 -05008291lpfc_sli_host_down(struct lpfc_vport *vport)
8292{
James Smart858c9f62007-06-17 19:56:39 -05008293 LIST_HEAD(completions);
James Smart92d7f7b2007-06-17 19:56:38 -05008294 struct lpfc_hba *phba = vport->phba;
8295 struct lpfc_sli *psli = &phba->sli;
8296 struct lpfc_sli_ring *pring;
8297 struct lpfc_iocbq *iocb, *next_iocb;
James Smart92d7f7b2007-06-17 19:56:38 -05008298 int i;
8299 unsigned long flags = 0;
8300 uint16_t prev_pring_flag;
8301
8302 lpfc_cleanup_discovery_resources(vport);
8303
8304 spin_lock_irqsave(&phba->hbalock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -05008305 for (i = 0; i < psli->num_rings; i++) {
8306 pring = &psli->ring[i];
8307 prev_pring_flag = pring->flag;
James Smart5e9d9b82008-06-14 22:52:53 -04008308 /* Only slow rings */
8309 if (pring->ringno == LPFC_ELS_RING) {
James Smart858c9f62007-06-17 19:56:39 -05008310 pring->flag |= LPFC_DEFERRED_RING_EVENT;
James Smart5e9d9b82008-06-14 22:52:53 -04008311 /* Set the lpfc data pending flag */
8312 set_bit(LPFC_DATA_READY, &phba->data_flags);
8313 }
James Smart92d7f7b2007-06-17 19:56:38 -05008314 /*
8315 * Error everything on the txq since these iocbs have not been
8316 * given to the FW yet.
8317 */
James Smart92d7f7b2007-06-17 19:56:38 -05008318 list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
8319 if (iocb->vport != vport)
8320 continue;
James Smart858c9f62007-06-17 19:56:39 -05008321 list_move_tail(&iocb->list, &completions);
James Smart92d7f7b2007-06-17 19:56:38 -05008322 pring->txq_cnt--;
James Smart92d7f7b2007-06-17 19:56:38 -05008323 }
8324
8325 /* Next issue ABTS for everything on the txcmplq */
8326 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq,
8327 list) {
8328 if (iocb->vport != vport)
8329 continue;
8330 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
8331 }
8332
8333 pring->flag = prev_pring_flag;
8334 }
8335
8336 spin_unlock_irqrestore(&phba->hbalock, flags);
8337
James Smarta257bf92009-04-06 18:48:10 -04008338 /* Cancel all the IOCBs from the completions list */
8339 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
8340 IOERR_SLI_DOWN);
James Smart92d7f7b2007-06-17 19:56:38 -05008341 return 1;
8342}
8343
James Smarte59058c2008-08-24 21:49:00 -04008344/**
James Smart3621a712009-04-06 18:47:14 -04008345 * lpfc_sli_hba_down - Resource cleanup function for the HBA
James Smarte59058c2008-08-24 21:49:00 -04008346 * @phba: Pointer to HBA context object.
8347 *
8348 * This function cleans up all iocb, buffers, mailbox commands
8349 * while shutting down the HBA. This function is called with no
8350 * lock held and always returns 1.
8351 * This function does the following to cleanup driver resources:
8352 * - Free discovery resources for each virtual port
8353 * - Cleanup any pending fabric iocbs
8354 * - Iterate through the iocb txq and free each entry
8355 * in the list.
8356 * - Free up any buffer posted to the HBA
8357 * - Free mailbox commands in the mailbox queue.
8358 **/
James Smart92d7f7b2007-06-17 19:56:38 -05008359int
James Smart2e0fef82007-06-17 19:56:36 -05008360lpfc_sli_hba_down(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05008361{
James Smart2534ba72007-04-25 09:52:20 -04008362 LIST_HEAD(completions);
James Smart2e0fef82007-06-17 19:56:36 -05008363 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05008364 struct lpfc_sli_ring *pring;
James Smart0ff10d42008-01-11 01:52:36 -05008365 struct lpfc_dmabuf *buf_ptr;
dea31012005-04-17 16:05:31 -05008366 unsigned long flags = 0;
James Smart04c68492009-05-22 14:52:52 -04008367 int i;
8368
8369 /* Shutdown the mailbox command sub-system */
8370 lpfc_sli_mbox_sys_shutdown(phba);
dea31012005-04-17 16:05:31 -05008371
dea31012005-04-17 16:05:31 -05008372 lpfc_hba_down_prep(phba);
8373
James Smart92d7f7b2007-06-17 19:56:38 -05008374 lpfc_fabric_abort_hba(phba);
8375
James Smart2e0fef82007-06-17 19:56:36 -05008376 spin_lock_irqsave(&phba->hbalock, flags);
dea31012005-04-17 16:05:31 -05008377 for (i = 0; i < psli->num_rings; i++) {
8378 pring = &psli->ring[i];
James Smart5e9d9b82008-06-14 22:52:53 -04008379 /* Only slow rings */
8380 if (pring->ringno == LPFC_ELS_RING) {
James Smart858c9f62007-06-17 19:56:39 -05008381 pring->flag |= LPFC_DEFERRED_RING_EVENT;
James Smart5e9d9b82008-06-14 22:52:53 -04008382 /* Set the lpfc data pending flag */
8383 set_bit(LPFC_DATA_READY, &phba->data_flags);
8384 }
dea31012005-04-17 16:05:31 -05008385
8386 /*
8387 * Error everything on the txq since these iocbs have not been
8388 * given to the FW yet.
8389 */
James Smart2534ba72007-04-25 09:52:20 -04008390 list_splice_init(&pring->txq, &completions);
dea31012005-04-17 16:05:31 -05008391 pring->txq_cnt = 0;
8392
dea31012005-04-17 16:05:31 -05008393 }
James Smart2e0fef82007-06-17 19:56:36 -05008394 spin_unlock_irqrestore(&phba->hbalock, flags);
dea31012005-04-17 16:05:31 -05008395
James Smarta257bf92009-04-06 18:48:10 -04008396 /* Cancel all the IOCBs from the completions list */
8397 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
8398 IOERR_SLI_DOWN);
James Smart2534ba72007-04-25 09:52:20 -04008399
James Smart0ff10d42008-01-11 01:52:36 -05008400 spin_lock_irqsave(&phba->hbalock, flags);
8401 list_splice_init(&phba->elsbuf, &completions);
8402 phba->elsbuf_cnt = 0;
8403 phba->elsbuf_prev_cnt = 0;
8404 spin_unlock_irqrestore(&phba->hbalock, flags);
8405
8406 while (!list_empty(&completions)) {
8407 list_remove_head(&completions, buf_ptr,
8408 struct lpfc_dmabuf, list);
8409 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
8410 kfree(buf_ptr);
8411 }
8412
dea31012005-04-17 16:05:31 -05008413 /* Return any active mbox cmds */
8414 del_timer_sync(&psli->mbox_tmo);
James Smart92d7f7b2007-06-17 19:56:38 -05008415
James Smartda0436e2009-05-22 14:51:39 -04008416 spin_lock_irqsave(&phba->pport->work_port_lock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -05008417 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
James Smartda0436e2009-05-22 14:51:39 -04008418 spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -05008419
James Smartda0436e2009-05-22 14:51:39 -04008420 return 1;
8421}
James Smart92d7f7b2007-06-17 19:56:38 -05008422
James Smartda0436e2009-05-22 14:51:39 -04008423/**
James Smart3621a712009-04-06 18:47:14 -04008424 * lpfc_sli_pcimem_bcopy - SLI memory copy function
James Smarte59058c2008-08-24 21:49:00 -04008425 * @srcp: Source memory pointer.
8426 * @destp: Destination memory pointer.
8427 * @cnt: Number of words required to be copied.
8428 *
8429 * This function is used for copying data between driver memory
8430 * and the SLI memory. This function also changes the endianness
8431 * of each word if native endianness is different from SLI
8432 * endianness. This function can be called with or without
8433 * lock.
8434 **/
dea31012005-04-17 16:05:31 -05008435void
8436lpfc_sli_pcimem_bcopy(void *srcp, void *destp, uint32_t cnt)
8437{
8438 uint32_t *src = srcp;
8439 uint32_t *dest = destp;
8440 uint32_t ldata;
8441 int i;
8442
8443 for (i = 0; i < (int)cnt; i += sizeof (uint32_t)) {
8444 ldata = *src;
8445 ldata = le32_to_cpu(ldata);
8446 *dest = ldata;
8447 src++;
8448 dest++;
8449 }
8450}
8451
James Smarte59058c2008-08-24 21:49:00 -04008452
8453/**
James Smarta0c87cb2009-07-19 10:01:10 -04008454 * lpfc_sli_bemem_bcopy - SLI memory copy function
8455 * @srcp: Source memory pointer.
8456 * @destp: Destination memory pointer.
8457 * @cnt: Number of words required to be copied.
8458 *
8459 * This function is used for copying data between a data structure
8460 * with big endian representation to local endianness.
8461 * This function can be called with or without lock.
8462 **/
8463void
8464lpfc_sli_bemem_bcopy(void *srcp, void *destp, uint32_t cnt)
8465{
8466 uint32_t *src = srcp;
8467 uint32_t *dest = destp;
8468 uint32_t ldata;
8469 int i;
8470
8471 for (i = 0; i < (int)cnt; i += sizeof(uint32_t)) {
8472 ldata = *src;
8473 ldata = be32_to_cpu(ldata);
8474 *dest = ldata;
8475 src++;
8476 dest++;
8477 }
8478}
8479
8480/**
James Smart3621a712009-04-06 18:47:14 -04008481 * lpfc_sli_ringpostbuf_put - Function to add a buffer to postbufq
James Smarte59058c2008-08-24 21:49:00 -04008482 * @phba: Pointer to HBA context object.
8483 * @pring: Pointer to driver SLI ring object.
8484 * @mp: Pointer to driver buffer object.
8485 *
8486 * This function is called with no lock held.
8487 * It always return zero after adding the buffer to the postbufq
8488 * buffer list.
8489 **/
dea31012005-04-17 16:05:31 -05008490int
James Smart2e0fef82007-06-17 19:56:36 -05008491lpfc_sli_ringpostbuf_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
8492 struct lpfc_dmabuf *mp)
dea31012005-04-17 16:05:31 -05008493{
8494 /* Stick struct lpfc_dmabuf at end of postbufq so driver can look it up
8495 later */
James Smart2e0fef82007-06-17 19:56:36 -05008496 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05008497 list_add_tail(&mp->list, &pring->postbufq);
dea31012005-04-17 16:05:31 -05008498 pring->postbufq_cnt++;
James Smart2e0fef82007-06-17 19:56:36 -05008499 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05008500 return 0;
8501}
8502
James Smarte59058c2008-08-24 21:49:00 -04008503/**
James Smart3621a712009-04-06 18:47:14 -04008504 * lpfc_sli_get_buffer_tag - allocates a tag for a CMD_QUE_XRI64_CX buffer
James Smarte59058c2008-08-24 21:49:00 -04008505 * @phba: Pointer to HBA context object.
8506 *
8507 * When HBQ is enabled, buffers are searched based on tags. This function
8508 * allocates a tag for buffer posted using CMD_QUE_XRI64_CX iocb. The
8509 * tag is bit wise or-ed with QUE_BUFTAG_BIT to make sure that the tag
8510 * does not conflict with tags of buffer posted for unsolicited events.
8511 * The function returns the allocated tag. The function is called with
8512 * no locks held.
8513 **/
James Smart76bb24e2007-10-27 13:38:00 -04008514uint32_t
8515lpfc_sli_get_buffer_tag(struct lpfc_hba *phba)
8516{
8517 spin_lock_irq(&phba->hbalock);
8518 phba->buffer_tag_count++;
8519 /*
8520 * Always set the QUE_BUFTAG_BIT to distiguish between
8521 * a tag assigned by HBQ.
8522 */
8523 phba->buffer_tag_count |= QUE_BUFTAG_BIT;
8524 spin_unlock_irq(&phba->hbalock);
8525 return phba->buffer_tag_count;
8526}
8527
James Smarte59058c2008-08-24 21:49:00 -04008528/**
James Smart3621a712009-04-06 18:47:14 -04008529 * lpfc_sli_ring_taggedbuf_get - find HBQ buffer associated with given tag
James Smarte59058c2008-08-24 21:49:00 -04008530 * @phba: Pointer to HBA context object.
8531 * @pring: Pointer to driver SLI ring object.
8532 * @tag: Buffer tag.
8533 *
8534 * Buffers posted using CMD_QUE_XRI64_CX iocb are in pring->postbufq
8535 * list. After HBA DMA data to these buffers, CMD_IOCB_RET_XRI64_CX
8536 * iocb is posted to the response ring with the tag of the buffer.
8537 * This function searches the pring->postbufq list using the tag
8538 * to find buffer associated with CMD_IOCB_RET_XRI64_CX
8539 * iocb. If the buffer is found then lpfc_dmabuf object of the
8540 * buffer is returned to the caller else NULL is returned.
8541 * This function is called with no lock held.
8542 **/
James Smart76bb24e2007-10-27 13:38:00 -04008543struct lpfc_dmabuf *
8544lpfc_sli_ring_taggedbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
8545 uint32_t tag)
8546{
8547 struct lpfc_dmabuf *mp, *next_mp;
8548 struct list_head *slp = &pring->postbufq;
8549
Lucas De Marchi25985ed2011-03-30 22:57:33 -03008550 /* Search postbufq, from the beginning, looking for a match on tag */
James Smart76bb24e2007-10-27 13:38:00 -04008551 spin_lock_irq(&phba->hbalock);
8552 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
8553 if (mp->buffer_tag == tag) {
8554 list_del_init(&mp->list);
8555 pring->postbufq_cnt--;
8556 spin_unlock_irq(&phba->hbalock);
8557 return mp;
8558 }
8559 }
8560
8561 spin_unlock_irq(&phba->hbalock);
8562 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartd7c255b2008-08-24 21:50:00 -04008563 "0402 Cannot find virtual addr for buffer tag on "
James Smart76bb24e2007-10-27 13:38:00 -04008564 "ring %d Data x%lx x%p x%p x%x\n",
8565 pring->ringno, (unsigned long) tag,
8566 slp->next, slp->prev, pring->postbufq_cnt);
8567
8568 return NULL;
8569}
dea31012005-04-17 16:05:31 -05008570
James Smarte59058c2008-08-24 21:49:00 -04008571/**
James Smart3621a712009-04-06 18:47:14 -04008572 * lpfc_sli_ringpostbuf_get - search buffers for unsolicited CT and ELS events
James Smarte59058c2008-08-24 21:49:00 -04008573 * @phba: Pointer to HBA context object.
8574 * @pring: Pointer to driver SLI ring object.
8575 * @phys: DMA address of the buffer.
8576 *
8577 * This function searches the buffer list using the dma_address
8578 * of unsolicited event to find the driver's lpfc_dmabuf object
8579 * corresponding to the dma_address. The function returns the
8580 * lpfc_dmabuf object if a buffer is found else it returns NULL.
8581 * This function is called by the ct and els unsolicited event
8582 * handlers to get the buffer associated with the unsolicited
8583 * event.
8584 *
8585 * This function is called with no lock held.
8586 **/
dea31012005-04-17 16:05:31 -05008587struct lpfc_dmabuf *
8588lpfc_sli_ringpostbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
8589 dma_addr_t phys)
8590{
8591 struct lpfc_dmabuf *mp, *next_mp;
8592 struct list_head *slp = &pring->postbufq;
8593
Lucas De Marchi25985ed2011-03-30 22:57:33 -03008594 /* Search postbufq, from the beginning, looking for a match on phys */
James Smart2e0fef82007-06-17 19:56:36 -05008595 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05008596 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
8597 if (mp->phys == phys) {
8598 list_del_init(&mp->list);
8599 pring->postbufq_cnt--;
James Smart2e0fef82007-06-17 19:56:36 -05008600 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05008601 return mp;
8602 }
8603 }
8604
James Smart2e0fef82007-06-17 19:56:36 -05008605 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05008606 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04008607 "0410 Cannot find virtual addr for mapped buf on "
dea31012005-04-17 16:05:31 -05008608 "ring %d Data x%llx x%p x%p x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04008609 pring->ringno, (unsigned long long)phys,
dea31012005-04-17 16:05:31 -05008610 slp->next, slp->prev, pring->postbufq_cnt);
8611 return NULL;
8612}
8613
James Smarte59058c2008-08-24 21:49:00 -04008614/**
James Smart3621a712009-04-06 18:47:14 -04008615 * lpfc_sli_abort_els_cmpl - Completion handler for the els abort iocbs
James Smarte59058c2008-08-24 21:49:00 -04008616 * @phba: Pointer to HBA context object.
8617 * @cmdiocb: Pointer to driver command iocb object.
8618 * @rspiocb: Pointer to driver response iocb object.
8619 *
8620 * This function is the completion handler for the abort iocbs for
8621 * ELS commands. This function is called from the ELS ring event
8622 * handler with no lock held. This function frees memory resources
8623 * associated with the abort iocb.
8624 **/
dea31012005-04-17 16:05:31 -05008625static void
James Smart2e0fef82007-06-17 19:56:36 -05008626lpfc_sli_abort_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
8627 struct lpfc_iocbq *rspiocb)
dea31012005-04-17 16:05:31 -05008628{
James Smart2e0fef82007-06-17 19:56:36 -05008629 IOCB_t *irsp = &rspiocb->iocb;
James Smart2680eea2007-04-25 09:52:55 -04008630 uint16_t abort_iotag, abort_context;
James Smart92d7f7b2007-06-17 19:56:38 -05008631 struct lpfc_iocbq *abort_iocb;
James Smart2680eea2007-04-25 09:52:55 -04008632 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
8633
8634 abort_iocb = NULL;
James Smart2680eea2007-04-25 09:52:55 -04008635
8636 if (irsp->ulpStatus) {
8637 abort_context = cmdiocb->iocb.un.acxri.abortContextTag;
8638 abort_iotag = cmdiocb->iocb.un.acxri.abortIoTag;
8639
James Smart2e0fef82007-06-17 19:56:36 -05008640 spin_lock_irq(&phba->hbalock);
James Smart45ed1192009-10-02 15:17:02 -04008641 if (phba->sli_rev < LPFC_SLI_REV4) {
8642 if (abort_iotag != 0 &&
8643 abort_iotag <= phba->sli.last_iotag)
8644 abort_iocb =
8645 phba->sli.iocbq_lookup[abort_iotag];
8646 } else
8647 /* For sli4 the abort_tag is the XRI,
8648 * so the abort routine puts the iotag of the iocb
8649 * being aborted in the context field of the abort
8650 * IOCB.
8651 */
8652 abort_iocb = phba->sli.iocbq_lookup[abort_context];
James Smart2680eea2007-04-25 09:52:55 -04008653
James Smart2680eea2007-04-25 09:52:55 -04008654 /*
James Smart58da1ff2008-04-07 10:15:56 -04008655 * If the iocb is not found in Firmware queue the iocb
8656 * might have completed already. Do not free it again.
8657 */
James Smart9b379602008-04-07 10:16:00 -04008658 if (irsp->ulpStatus == IOSTAT_LOCAL_REJECT) {
James Smart45ed1192009-10-02 15:17:02 -04008659 if (irsp->un.ulpWord[4] != IOERR_NO_XRI) {
8660 spin_unlock_irq(&phba->hbalock);
8661 lpfc_sli_release_iocbq(phba, cmdiocb);
8662 return;
8663 }
8664 /* For SLI4 the ulpContext field for abort IOCB
8665 * holds the iotag of the IOCB being aborted so
8666 * the local abort_context needs to be reset to
8667 * match the aborted IOCBs ulpContext.
8668 */
8669 if (abort_iocb && phba->sli_rev == LPFC_SLI_REV4)
8670 abort_context = abort_iocb->iocb.ulpContext;
James Smart58da1ff2008-04-07 10:15:56 -04008671 }
James Smart2a9bf3d2010-06-07 15:24:45 -04008672
8673 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS | LOG_SLI,
8674 "0327 Cannot abort els iocb %p "
8675 "with tag %x context %x, abort status %x, "
8676 "abort code %x\n",
8677 abort_iocb, abort_iotag, abort_context,
8678 irsp->ulpStatus, irsp->un.ulpWord[4]);
James Smart58da1ff2008-04-07 10:15:56 -04008679 /*
James Smart2680eea2007-04-25 09:52:55 -04008680 * make sure we have the right iocbq before taking it
8681 * off the txcmplq and try to call completion routine.
8682 */
James Smart2e0fef82007-06-17 19:56:36 -05008683 if (!abort_iocb ||
8684 abort_iocb->iocb.ulpContext != abort_context ||
8685 (abort_iocb->iocb_flag & LPFC_DRIVER_ABORTED) == 0)
8686 spin_unlock_irq(&phba->hbalock);
James Smart341af102010-01-26 23:07:37 -05008687 else if (phba->sli_rev < LPFC_SLI_REV4) {
8688 /*
8689 * leave the SLI4 aborted command on the txcmplq
8690 * list and the command complete WCQE's XB bit
8691 * will tell whether the SGL (XRI) can be released
8692 * immediately or to the aborted SGL list for the
8693 * following abort XRI from the HBA.
8694 */
James Smart92d7f7b2007-06-17 19:56:38 -05008695 list_del_init(&abort_iocb->list);
James Smart2a9bf3d2010-06-07 15:24:45 -04008696 if (abort_iocb->iocb_flag & LPFC_IO_ON_Q) {
8697 abort_iocb->iocb_flag &= ~LPFC_IO_ON_Q;
8698 pring->txcmplq_cnt--;
8699 }
James Smart2680eea2007-04-25 09:52:55 -04008700
James Smart0ff10d42008-01-11 01:52:36 -05008701 /* Firmware could still be in progress of DMAing
8702 * payload, so don't free data buffer till after
8703 * a hbeat.
8704 */
8705 abort_iocb->iocb_flag |= LPFC_DELAY_MEM_FREE;
James Smart92d7f7b2007-06-17 19:56:38 -05008706 abort_iocb->iocb_flag &= ~LPFC_DRIVER_ABORTED;
James Smart341af102010-01-26 23:07:37 -05008707 spin_unlock_irq(&phba->hbalock);
8708
James Smart92d7f7b2007-06-17 19:56:38 -05008709 abort_iocb->iocb.ulpStatus = IOSTAT_LOCAL_REJECT;
James Smart341af102010-01-26 23:07:37 -05008710 abort_iocb->iocb.un.ulpWord[4] = IOERR_ABORT_REQUESTED;
James Smart92d7f7b2007-06-17 19:56:38 -05008711 (abort_iocb->iocb_cmpl)(phba, abort_iocb, abort_iocb);
James Smart49198b32010-04-06 15:04:33 -04008712 } else
8713 spin_unlock_irq(&phba->hbalock);
James Smart2680eea2007-04-25 09:52:55 -04008714 }
8715
James Bottomley604a3e32005-10-29 10:28:33 -05008716 lpfc_sli_release_iocbq(phba, cmdiocb);
dea31012005-04-17 16:05:31 -05008717 return;
8718}
8719
James Smarte59058c2008-08-24 21:49:00 -04008720/**
James Smart3621a712009-04-06 18:47:14 -04008721 * lpfc_ignore_els_cmpl - Completion handler for aborted ELS command
James Smarte59058c2008-08-24 21:49:00 -04008722 * @phba: Pointer to HBA context object.
8723 * @cmdiocb: Pointer to driver command iocb object.
8724 * @rspiocb: Pointer to driver response iocb object.
8725 *
8726 * The function is called from SLI ring event handler with no
8727 * lock held. This function is the completion handler for ELS commands
8728 * which are aborted. The function frees memory resources used for
8729 * the aborted ELS commands.
8730 **/
James Smart92d7f7b2007-06-17 19:56:38 -05008731static void
8732lpfc_ignore_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
8733 struct lpfc_iocbq *rspiocb)
8734{
8735 IOCB_t *irsp = &rspiocb->iocb;
8736
8737 /* ELS cmd tag <ulpIoTag> completes */
8738 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
James Smartd7c255b2008-08-24 21:50:00 -04008739 "0139 Ignoring ELS cmd tag x%x completion Data: "
James Smart92d7f7b2007-06-17 19:56:38 -05008740 "x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04008741 irsp->ulpIoTag, irsp->ulpStatus,
James Smart92d7f7b2007-06-17 19:56:38 -05008742 irsp->un.ulpWord[4], irsp->ulpTimeout);
James Smart858c9f62007-06-17 19:56:39 -05008743 if (cmdiocb->iocb.ulpCommand == CMD_GEN_REQUEST64_CR)
8744 lpfc_ct_free_iocb(phba, cmdiocb);
8745 else
8746 lpfc_els_free_iocb(phba, cmdiocb);
James Smart92d7f7b2007-06-17 19:56:38 -05008747 return;
8748}
8749
James Smarte59058c2008-08-24 21:49:00 -04008750/**
James Smart5af5eee2010-10-22 11:06:38 -04008751 * lpfc_sli_abort_iotag_issue - Issue abort for a command iocb
James Smarte59058c2008-08-24 21:49:00 -04008752 * @phba: Pointer to HBA context object.
8753 * @pring: Pointer to driver SLI ring object.
8754 * @cmdiocb: Pointer to driver command iocb object.
8755 *
James Smart5af5eee2010-10-22 11:06:38 -04008756 * This function issues an abort iocb for the provided command iocb down to
8757 * the port. Other than the case the outstanding command iocb is an abort
8758 * request, this function issues abort out unconditionally. This function is
8759 * called with hbalock held. The function returns 0 when it fails due to
8760 * memory allocation failure or when the command iocb is an abort request.
James Smarte59058c2008-08-24 21:49:00 -04008761 **/
James Smart5af5eee2010-10-22 11:06:38 -04008762static int
8763lpfc_sli_abort_iotag_issue(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -05008764 struct lpfc_iocbq *cmdiocb)
dea31012005-04-17 16:05:31 -05008765{
James Smart2e0fef82007-06-17 19:56:36 -05008766 struct lpfc_vport *vport = cmdiocb->vport;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04008767 struct lpfc_iocbq *abtsiocbp;
dea31012005-04-17 16:05:31 -05008768 IOCB_t *icmd = NULL;
8769 IOCB_t *iabt = NULL;
James Smart5af5eee2010-10-22 11:06:38 -04008770 int retval;
James Smart07951072007-04-25 09:51:38 -04008771
James Smart92d7f7b2007-06-17 19:56:38 -05008772 /*
8773 * There are certain command types we don't want to abort. And we
8774 * don't want to abort commands that are already in the process of
8775 * being aborted.
James Smart07951072007-04-25 09:51:38 -04008776 */
8777 icmd = &cmdiocb->iocb;
James Smart2e0fef82007-06-17 19:56:36 -05008778 if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
James Smart92d7f7b2007-06-17 19:56:38 -05008779 icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
8780 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
James Smart07951072007-04-25 09:51:38 -04008781 return 0;
8782
dea31012005-04-17 16:05:31 -05008783 /* issue ABTS for this IOCB based on iotag */
James Smart92d7f7b2007-06-17 19:56:38 -05008784 abtsiocbp = __lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05008785 if (abtsiocbp == NULL)
8786 return 0;
dea31012005-04-17 16:05:31 -05008787
James Smart07951072007-04-25 09:51:38 -04008788 /* This signals the response to set the correct status
James Smart341af102010-01-26 23:07:37 -05008789 * before calling the completion handler
James Smart07951072007-04-25 09:51:38 -04008790 */
8791 cmdiocb->iocb_flag |= LPFC_DRIVER_ABORTED;
8792
dea31012005-04-17 16:05:31 -05008793 iabt = &abtsiocbp->iocb;
James Smart07951072007-04-25 09:51:38 -04008794 iabt->un.acxri.abortType = ABORT_TYPE_ABTS;
8795 iabt->un.acxri.abortContextTag = icmd->ulpContext;
James Smart45ed1192009-10-02 15:17:02 -04008796 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smartda0436e2009-05-22 14:51:39 -04008797 iabt->un.acxri.abortIoTag = cmdiocb->sli4_xritag;
James Smart45ed1192009-10-02 15:17:02 -04008798 iabt->un.acxri.abortContextTag = cmdiocb->iotag;
8799 }
James Smartda0436e2009-05-22 14:51:39 -04008800 else
8801 iabt->un.acxri.abortIoTag = icmd->ulpIoTag;
dea31012005-04-17 16:05:31 -05008802 iabt->ulpLe = 1;
James Smart07951072007-04-25 09:51:38 -04008803 iabt->ulpClass = icmd->ulpClass;
dea31012005-04-17 16:05:31 -05008804
James Smart5ffc2662009-11-18 15:39:44 -05008805 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
8806 abtsiocbp->fcp_wqidx = cmdiocb->fcp_wqidx;
James Smart341af102010-01-26 23:07:37 -05008807 if (cmdiocb->iocb_flag & LPFC_IO_FCP)
8808 abtsiocbp->iocb_flag |= LPFC_USE_FCPWQIDX;
James Smart5ffc2662009-11-18 15:39:44 -05008809
James Smart2e0fef82007-06-17 19:56:36 -05008810 if (phba->link_state >= LPFC_LINK_UP)
James Smart07951072007-04-25 09:51:38 -04008811 iabt->ulpCommand = CMD_ABORT_XRI_CN;
8812 else
8813 iabt->ulpCommand = CMD_CLOSE_XRI_CN;
8814
8815 abtsiocbp->iocb_cmpl = lpfc_sli_abort_els_cmpl;
James Smart5b8bd0c2007-04-25 09:52:49 -04008816
James Smarte8b62012007-08-02 11:10:09 -04008817 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
8818 "0339 Abort xri x%x, original iotag x%x, "
8819 "abort cmd iotag x%x\n",
James Smart2a9bf3d2010-06-07 15:24:45 -04008820 iabt->un.acxri.abortIoTag,
James Smarte8b62012007-08-02 11:10:09 -04008821 iabt->un.acxri.abortContextTag,
James Smart2a9bf3d2010-06-07 15:24:45 -04008822 abtsiocbp->iotag);
James Smartda0436e2009-05-22 14:51:39 -04008823 retval = __lpfc_sli_issue_iocb(phba, pring->ringno, abtsiocbp, 0);
James Smart07951072007-04-25 09:51:38 -04008824
James Smartd7c255b2008-08-24 21:50:00 -04008825 if (retval)
8826 __lpfc_sli_release_iocbq(phba, abtsiocbp);
James Smart5af5eee2010-10-22 11:06:38 -04008827
8828 /*
8829 * Caller to this routine should check for IOCB_ERROR
8830 * and handle it properly. This routine no longer removes
8831 * iocb off txcmplq and call compl in case of IOCB_ERROR.
8832 */
8833 return retval;
8834}
8835
8836/**
8837 * lpfc_sli_issue_abort_iotag - Abort function for a command iocb
8838 * @phba: Pointer to HBA context object.
8839 * @pring: Pointer to driver SLI ring object.
8840 * @cmdiocb: Pointer to driver command iocb object.
8841 *
8842 * This function issues an abort iocb for the provided command iocb. In case
8843 * of unloading, the abort iocb will not be issued to commands on the ELS
8844 * ring. Instead, the callback function shall be changed to those commands
8845 * so that nothing happens when them finishes. This function is called with
8846 * hbalock held. The function returns 0 when the command iocb is an abort
8847 * request.
8848 **/
8849int
8850lpfc_sli_issue_abort_iotag(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
8851 struct lpfc_iocbq *cmdiocb)
8852{
8853 struct lpfc_vport *vport = cmdiocb->vport;
8854 int retval = IOCB_ERROR;
8855 IOCB_t *icmd = NULL;
8856
8857 /*
8858 * There are certain command types we don't want to abort. And we
8859 * don't want to abort commands that are already in the process of
8860 * being aborted.
8861 */
8862 icmd = &cmdiocb->iocb;
8863 if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
8864 icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
8865 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
8866 return 0;
8867
8868 /*
8869 * If we're unloading, don't abort iocb on the ELS ring, but change
8870 * the callback so that nothing happens when it finishes.
8871 */
8872 if ((vport->load_flag & FC_UNLOADING) &&
8873 (pring->ringno == LPFC_ELS_RING)) {
8874 if (cmdiocb->iocb_flag & LPFC_IO_FABRIC)
8875 cmdiocb->fabric_iocb_cmpl = lpfc_ignore_els_cmpl;
8876 else
8877 cmdiocb->iocb_cmpl = lpfc_ignore_els_cmpl;
8878 goto abort_iotag_exit;
8879 }
8880
8881 /* Now, we try to issue the abort to the cmdiocb out */
8882 retval = lpfc_sli_abort_iotag_issue(phba, pring, cmdiocb);
8883
James Smart07951072007-04-25 09:51:38 -04008884abort_iotag_exit:
James Smart2e0fef82007-06-17 19:56:36 -05008885 /*
8886 * Caller to this routine should check for IOCB_ERROR
8887 * and handle it properly. This routine no longer removes
8888 * iocb off txcmplq and call compl in case of IOCB_ERROR.
James Smart07951072007-04-25 09:51:38 -04008889 */
James Smart2e0fef82007-06-17 19:56:36 -05008890 return retval;
dea31012005-04-17 16:05:31 -05008891}
8892
James Smarte59058c2008-08-24 21:49:00 -04008893/**
James Smart5af5eee2010-10-22 11:06:38 -04008894 * lpfc_sli_iocb_ring_abort - Unconditionally abort all iocbs on an iocb ring
8895 * @phba: Pointer to HBA context object.
8896 * @pring: Pointer to driver SLI ring object.
8897 *
8898 * This function aborts all iocbs in the given ring and frees all the iocb
8899 * objects in txq. This function issues abort iocbs unconditionally for all
8900 * the iocb commands in txcmplq. The iocbs in the txcmplq is not guaranteed
8901 * to complete before the return of this function. The caller is not required
8902 * to hold any locks.
8903 **/
8904static void
8905lpfc_sli_iocb_ring_abort(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
8906{
8907 LIST_HEAD(completions);
8908 struct lpfc_iocbq *iocb, *next_iocb;
8909
8910 if (pring->ringno == LPFC_ELS_RING)
8911 lpfc_fabric_abort_hba(phba);
8912
8913 spin_lock_irq(&phba->hbalock);
8914
8915 /* Take off all the iocbs on txq for cancelling */
8916 list_splice_init(&pring->txq, &completions);
8917 pring->txq_cnt = 0;
8918
8919 /* Next issue ABTS for everything on the txcmplq */
8920 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
8921 lpfc_sli_abort_iotag_issue(phba, pring, iocb);
8922
8923 spin_unlock_irq(&phba->hbalock);
8924
8925 /* Cancel all the IOCBs from the completions list */
8926 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
8927 IOERR_SLI_ABORTED);
8928}
8929
8930/**
8931 * lpfc_sli_hba_iocb_abort - Abort all iocbs to an hba.
8932 * @phba: pointer to lpfc HBA data structure.
8933 *
8934 * This routine will abort all pending and outstanding iocbs to an HBA.
8935 **/
8936void
8937lpfc_sli_hba_iocb_abort(struct lpfc_hba *phba)
8938{
8939 struct lpfc_sli *psli = &phba->sli;
8940 struct lpfc_sli_ring *pring;
8941 int i;
8942
8943 for (i = 0; i < psli->num_rings; i++) {
8944 pring = &psli->ring[i];
8945 lpfc_sli_iocb_ring_abort(phba, pring);
8946 }
8947}
8948
8949/**
James Smart3621a712009-04-06 18:47:14 -04008950 * lpfc_sli_validate_fcp_iocb - find commands associated with a vport or LUN
James Smarte59058c2008-08-24 21:49:00 -04008951 * @iocbq: Pointer to driver iocb object.
8952 * @vport: Pointer to driver virtual port object.
8953 * @tgt_id: SCSI ID of the target.
8954 * @lun_id: LUN ID of the scsi device.
8955 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST
8956 *
James Smart3621a712009-04-06 18:47:14 -04008957 * This function acts as an iocb filter for functions which abort or count
James Smarte59058c2008-08-24 21:49:00 -04008958 * all FCP iocbs pending on a lun/SCSI target/SCSI host. It will return
8959 * 0 if the filtering criteria is met for the given iocb and will return
8960 * 1 if the filtering criteria is not met.
8961 * If ctx_cmd == LPFC_CTX_LUN, the function returns 0 only if the
8962 * given iocb is for the SCSI device specified by vport, tgt_id and
8963 * lun_id parameter.
8964 * If ctx_cmd == LPFC_CTX_TGT, the function returns 0 only if the
8965 * given iocb is for the SCSI target specified by vport and tgt_id
8966 * parameters.
8967 * If ctx_cmd == LPFC_CTX_HOST, the function returns 0 only if the
8968 * given iocb is for the SCSI host associated with the given vport.
8969 * This function is called with no locks held.
8970 **/
dea31012005-04-17 16:05:31 -05008971static int
James Smart51ef4c22007-08-02 11:10:31 -04008972lpfc_sli_validate_fcp_iocb(struct lpfc_iocbq *iocbq, struct lpfc_vport *vport,
8973 uint16_t tgt_id, uint64_t lun_id,
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04008974 lpfc_ctx_cmd ctx_cmd)
dea31012005-04-17 16:05:31 -05008975{
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04008976 struct lpfc_scsi_buf *lpfc_cmd;
dea31012005-04-17 16:05:31 -05008977 int rc = 1;
8978
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04008979 if (!(iocbq->iocb_flag & LPFC_IO_FCP))
8980 return rc;
8981
James Smart51ef4c22007-08-02 11:10:31 -04008982 if (iocbq->vport != vport)
8983 return rc;
8984
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04008985 lpfc_cmd = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04008986
James Smart495a7142008-06-14 22:52:59 -04008987 if (lpfc_cmd->pCmd == NULL)
dea31012005-04-17 16:05:31 -05008988 return rc;
8989
8990 switch (ctx_cmd) {
8991 case LPFC_CTX_LUN:
James Smart495a7142008-06-14 22:52:59 -04008992 if ((lpfc_cmd->rdata->pnode) &&
8993 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id) &&
8994 (scsilun_to_int(&lpfc_cmd->fcp_cmnd->fcp_lun) == lun_id))
dea31012005-04-17 16:05:31 -05008995 rc = 0;
8996 break;
8997 case LPFC_CTX_TGT:
James Smart495a7142008-06-14 22:52:59 -04008998 if ((lpfc_cmd->rdata->pnode) &&
8999 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id))
dea31012005-04-17 16:05:31 -05009000 rc = 0;
9001 break;
dea31012005-04-17 16:05:31 -05009002 case LPFC_CTX_HOST:
9003 rc = 0;
9004 break;
9005 default:
9006 printk(KERN_ERR "%s: Unknown context cmd type, value %d\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07009007 __func__, ctx_cmd);
dea31012005-04-17 16:05:31 -05009008 break;
9009 }
9010
9011 return rc;
9012}
9013
James Smarte59058c2008-08-24 21:49:00 -04009014/**
James Smart3621a712009-04-06 18:47:14 -04009015 * lpfc_sli_sum_iocb - Function to count the number of FCP iocbs pending
James Smarte59058c2008-08-24 21:49:00 -04009016 * @vport: Pointer to virtual port.
9017 * @tgt_id: SCSI ID of the target.
9018 * @lun_id: LUN ID of the scsi device.
9019 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
9020 *
9021 * This function returns number of FCP commands pending for the vport.
9022 * When ctx_cmd == LPFC_CTX_LUN, the function returns number of FCP
9023 * commands pending on the vport associated with SCSI device specified
9024 * by tgt_id and lun_id parameters.
9025 * When ctx_cmd == LPFC_CTX_TGT, the function returns number of FCP
9026 * commands pending on the vport associated with SCSI target specified
9027 * by tgt_id parameter.
9028 * When ctx_cmd == LPFC_CTX_HOST, the function returns number of FCP
9029 * commands pending on the vport.
9030 * This function returns the number of iocbs which satisfy the filter.
9031 * This function is called without any lock held.
9032 **/
dea31012005-04-17 16:05:31 -05009033int
James Smart51ef4c22007-08-02 11:10:31 -04009034lpfc_sli_sum_iocb(struct lpfc_vport *vport, uint16_t tgt_id, uint64_t lun_id,
9035 lpfc_ctx_cmd ctx_cmd)
dea31012005-04-17 16:05:31 -05009036{
James Smart51ef4c22007-08-02 11:10:31 -04009037 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009038 struct lpfc_iocbq *iocbq;
9039 int sum, i;
dea31012005-04-17 16:05:31 -05009040
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009041 for (i = 1, sum = 0; i <= phba->sli.last_iotag; i++) {
9042 iocbq = phba->sli.iocbq_lookup[i];
dea31012005-04-17 16:05:31 -05009043
James Smart51ef4c22007-08-02 11:10:31 -04009044 if (lpfc_sli_validate_fcp_iocb (iocbq, vport, tgt_id, lun_id,
9045 ctx_cmd) == 0)
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009046 sum++;
dea31012005-04-17 16:05:31 -05009047 }
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009048
dea31012005-04-17 16:05:31 -05009049 return sum;
9050}
9051
James Smarte59058c2008-08-24 21:49:00 -04009052/**
James Smart3621a712009-04-06 18:47:14 -04009053 * lpfc_sli_abort_fcp_cmpl - Completion handler function for aborted FCP IOCBs
James Smarte59058c2008-08-24 21:49:00 -04009054 * @phba: Pointer to HBA context object
9055 * @cmdiocb: Pointer to command iocb object.
9056 * @rspiocb: Pointer to response iocb object.
9057 *
9058 * This function is called when an aborted FCP iocb completes. This
9059 * function is called by the ring event handler with no lock held.
9060 * This function frees the iocb.
9061 **/
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -04009062void
James Smart2e0fef82007-06-17 19:56:36 -05009063lpfc_sli_abort_fcp_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
9064 struct lpfc_iocbq *rspiocb)
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -04009065{
James Bottomley604a3e32005-10-29 10:28:33 -05009066 lpfc_sli_release_iocbq(phba, cmdiocb);
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -04009067 return;
9068}
9069
James Smarte59058c2008-08-24 21:49:00 -04009070/**
James Smart3621a712009-04-06 18:47:14 -04009071 * lpfc_sli_abort_iocb - issue abort for all commands on a host/target/LUN
James Smarte59058c2008-08-24 21:49:00 -04009072 * @vport: Pointer to virtual port.
9073 * @pring: Pointer to driver SLI ring object.
9074 * @tgt_id: SCSI ID of the target.
9075 * @lun_id: LUN ID of the scsi device.
9076 * @abort_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
9077 *
9078 * This function sends an abort command for every SCSI command
9079 * associated with the given virtual port pending on the ring
9080 * filtered by lpfc_sli_validate_fcp_iocb function.
9081 * When abort_cmd == LPFC_CTX_LUN, the function sends abort only to the
9082 * FCP iocbs associated with lun specified by tgt_id and lun_id
9083 * parameters
9084 * When abort_cmd == LPFC_CTX_TGT, the function sends abort only to the
9085 * FCP iocbs associated with SCSI target specified by tgt_id parameter.
9086 * When abort_cmd == LPFC_CTX_HOST, the function sends abort to all
9087 * FCP iocbs associated with virtual port.
9088 * This function returns number of iocbs it failed to abort.
9089 * This function is called with no locks held.
9090 **/
dea31012005-04-17 16:05:31 -05009091int
James Smart51ef4c22007-08-02 11:10:31 -04009092lpfc_sli_abort_iocb(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
9093 uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd abort_cmd)
dea31012005-04-17 16:05:31 -05009094{
James Smart51ef4c22007-08-02 11:10:31 -04009095 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009096 struct lpfc_iocbq *iocbq;
9097 struct lpfc_iocbq *abtsiocb;
dea31012005-04-17 16:05:31 -05009098 IOCB_t *cmd = NULL;
dea31012005-04-17 16:05:31 -05009099 int errcnt = 0, ret_val = 0;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009100 int i;
dea31012005-04-17 16:05:31 -05009101
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009102 for (i = 1; i <= phba->sli.last_iotag; i++) {
9103 iocbq = phba->sli.iocbq_lookup[i];
dea31012005-04-17 16:05:31 -05009104
James Smart51ef4c22007-08-02 11:10:31 -04009105 if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
James Smart2e0fef82007-06-17 19:56:36 -05009106 abort_cmd) != 0)
dea31012005-04-17 16:05:31 -05009107 continue;
9108
9109 /* issue ABTS for this IOCB based on iotag */
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009110 abtsiocb = lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05009111 if (abtsiocb == NULL) {
9112 errcnt++;
9113 continue;
9114 }
dea31012005-04-17 16:05:31 -05009115
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009116 cmd = &iocbq->iocb;
dea31012005-04-17 16:05:31 -05009117 abtsiocb->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
9118 abtsiocb->iocb.un.acxri.abortContextTag = cmd->ulpContext;
James Smartda0436e2009-05-22 14:51:39 -04009119 if (phba->sli_rev == LPFC_SLI_REV4)
9120 abtsiocb->iocb.un.acxri.abortIoTag = iocbq->sli4_xritag;
9121 else
9122 abtsiocb->iocb.un.acxri.abortIoTag = cmd->ulpIoTag;
dea31012005-04-17 16:05:31 -05009123 abtsiocb->iocb.ulpLe = 1;
9124 abtsiocb->iocb.ulpClass = cmd->ulpClass;
James Smart2e0fef82007-06-17 19:56:36 -05009125 abtsiocb->vport = phba->pport;
dea31012005-04-17 16:05:31 -05009126
James Smart5ffc2662009-11-18 15:39:44 -05009127 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
9128 abtsiocb->fcp_wqidx = iocbq->fcp_wqidx;
James Smart341af102010-01-26 23:07:37 -05009129 if (iocbq->iocb_flag & LPFC_IO_FCP)
9130 abtsiocb->iocb_flag |= LPFC_USE_FCPWQIDX;
James Smart5ffc2662009-11-18 15:39:44 -05009131
James Smart2e0fef82007-06-17 19:56:36 -05009132 if (lpfc_is_link_up(phba))
dea31012005-04-17 16:05:31 -05009133 abtsiocb->iocb.ulpCommand = CMD_ABORT_XRI_CN;
9134 else
9135 abtsiocb->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
9136
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -04009137 /* Setup callback routine and issue the command. */
9138 abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
James Smartda0436e2009-05-22 14:51:39 -04009139 ret_val = lpfc_sli_issue_iocb(phba, pring->ringno,
9140 abtsiocb, 0);
dea31012005-04-17 16:05:31 -05009141 if (ret_val == IOCB_ERROR) {
James Bottomley604a3e32005-10-29 10:28:33 -05009142 lpfc_sli_release_iocbq(phba, abtsiocb);
dea31012005-04-17 16:05:31 -05009143 errcnt++;
9144 continue;
9145 }
9146 }
9147
9148 return errcnt;
9149}
9150
James Smarte59058c2008-08-24 21:49:00 -04009151/**
James Smart3621a712009-04-06 18:47:14 -04009152 * lpfc_sli_wake_iocb_wait - lpfc_sli_issue_iocb_wait's completion handler
James Smarte59058c2008-08-24 21:49:00 -04009153 * @phba: Pointer to HBA context object.
9154 * @cmdiocbq: Pointer to command iocb.
9155 * @rspiocbq: Pointer to response iocb.
9156 *
9157 * This function is the completion handler for iocbs issued using
9158 * lpfc_sli_issue_iocb_wait function. This function is called by the
9159 * ring event handler function without any lock held. This function
9160 * can be called from both worker thread context and interrupt
9161 * context. This function also can be called from other thread which
9162 * cleans up the SLI layer objects.
9163 * This function copy the contents of the response iocb to the
9164 * response iocb memory object provided by the caller of
9165 * lpfc_sli_issue_iocb_wait and then wakes up the thread which
9166 * sleeps for the iocb completion.
9167 **/
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009168static void
9169lpfc_sli_wake_iocb_wait(struct lpfc_hba *phba,
9170 struct lpfc_iocbq *cmdiocbq,
9171 struct lpfc_iocbq *rspiocbq)
dea31012005-04-17 16:05:31 -05009172{
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009173 wait_queue_head_t *pdone_q;
9174 unsigned long iflags;
James Smart0f65ff62010-02-26 14:14:23 -05009175 struct lpfc_scsi_buf *lpfc_cmd;
dea31012005-04-17 16:05:31 -05009176
James Smart2e0fef82007-06-17 19:56:36 -05009177 spin_lock_irqsave(&phba->hbalock, iflags);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009178 cmdiocbq->iocb_flag |= LPFC_IO_WAKE;
9179 if (cmdiocbq->context2 && rspiocbq)
9180 memcpy(&((struct lpfc_iocbq *)cmdiocbq->context2)->iocb,
9181 &rspiocbq->iocb, sizeof(IOCB_t));
9182
James Smart0f65ff62010-02-26 14:14:23 -05009183 /* Set the exchange busy flag for task management commands */
9184 if ((cmdiocbq->iocb_flag & LPFC_IO_FCP) &&
9185 !(cmdiocbq->iocb_flag & LPFC_IO_LIBDFC)) {
9186 lpfc_cmd = container_of(cmdiocbq, struct lpfc_scsi_buf,
9187 cur_iocbq);
9188 lpfc_cmd->exch_busy = rspiocbq->iocb_flag & LPFC_EXCHANGE_BUSY;
9189 }
9190
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009191 pdone_q = cmdiocbq->context_un.wait_queue;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009192 if (pdone_q)
9193 wake_up(pdone_q);
James Smart858c9f62007-06-17 19:56:39 -05009194 spin_unlock_irqrestore(&phba->hbalock, iflags);
dea31012005-04-17 16:05:31 -05009195 return;
9196}
9197
James Smarte59058c2008-08-24 21:49:00 -04009198/**
James Smartd11e31d2009-06-10 17:23:06 -04009199 * lpfc_chk_iocb_flg - Test IOCB flag with lock held.
9200 * @phba: Pointer to HBA context object..
9201 * @piocbq: Pointer to command iocb.
9202 * @flag: Flag to test.
9203 *
9204 * This routine grabs the hbalock and then test the iocb_flag to
9205 * see if the passed in flag is set.
9206 * Returns:
9207 * 1 if flag is set.
9208 * 0 if flag is not set.
9209 **/
9210static int
9211lpfc_chk_iocb_flg(struct lpfc_hba *phba,
9212 struct lpfc_iocbq *piocbq, uint32_t flag)
9213{
9214 unsigned long iflags;
9215 int ret;
9216
9217 spin_lock_irqsave(&phba->hbalock, iflags);
9218 ret = piocbq->iocb_flag & flag;
9219 spin_unlock_irqrestore(&phba->hbalock, iflags);
9220 return ret;
9221
9222}
9223
9224/**
James Smart3621a712009-04-06 18:47:14 -04009225 * lpfc_sli_issue_iocb_wait - Synchronous function to issue iocb commands
James Smarte59058c2008-08-24 21:49:00 -04009226 * @phba: Pointer to HBA context object..
9227 * @pring: Pointer to sli ring.
9228 * @piocb: Pointer to command iocb.
9229 * @prspiocbq: Pointer to response iocb.
9230 * @timeout: Timeout in number of seconds.
9231 *
9232 * This function issues the iocb to firmware and waits for the
9233 * iocb to complete. If the iocb command is not
9234 * completed within timeout seconds, it returns IOCB_TIMEDOUT.
9235 * Caller should not free the iocb resources if this function
9236 * returns IOCB_TIMEDOUT.
9237 * The function waits for the iocb completion using an
9238 * non-interruptible wait.
9239 * This function will sleep while waiting for iocb completion.
9240 * So, this function should not be called from any context which
9241 * does not allow sleeping. Due to the same reason, this function
9242 * cannot be called with interrupt disabled.
9243 * This function assumes that the iocb completions occur while
9244 * this function sleep. So, this function cannot be called from
9245 * the thread which process iocb completion for this ring.
9246 * This function clears the iocb_flag of the iocb object before
9247 * issuing the iocb and the iocb completion handler sets this
9248 * flag and wakes this thread when the iocb completes.
9249 * The contents of the response iocb will be copied to prspiocbq
9250 * by the completion handler when the command completes.
9251 * This function returns IOCB_SUCCESS when success.
9252 * This function is called with no lock held.
9253 **/
dea31012005-04-17 16:05:31 -05009254int
James Smart2e0fef82007-06-17 19:56:36 -05009255lpfc_sli_issue_iocb_wait(struct lpfc_hba *phba,
James Smartda0436e2009-05-22 14:51:39 -04009256 uint32_t ring_number,
James Smart2e0fef82007-06-17 19:56:36 -05009257 struct lpfc_iocbq *piocb,
9258 struct lpfc_iocbq *prspiocbq,
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009259 uint32_t timeout)
dea31012005-04-17 16:05:31 -05009260{
Peter Zijlstra7259f0d2006-10-29 22:46:36 -08009261 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009262 long timeleft, timeout_req = 0;
9263 int retval = IOCB_SUCCESS;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05009264 uint32_t creg_val;
James Smart2a9bf3d2010-06-07 15:24:45 -04009265 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
dea31012005-04-17 16:05:31 -05009266 /*
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009267 * If the caller has provided a response iocbq buffer, then context2
9268 * is NULL or its an error.
dea31012005-04-17 16:05:31 -05009269 */
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009270 if (prspiocbq) {
9271 if (piocb->context2)
9272 return IOCB_ERROR;
9273 piocb->context2 = prspiocbq;
dea31012005-04-17 16:05:31 -05009274 }
9275
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009276 piocb->iocb_cmpl = lpfc_sli_wake_iocb_wait;
9277 piocb->context_un.wait_queue = &done_q;
9278 piocb->iocb_flag &= ~LPFC_IO_WAKE;
dea31012005-04-17 16:05:31 -05009279
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05009280 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
James Smart9940b972011-03-11 16:06:12 -05009281 if (lpfc_readl(phba->HCregaddr, &creg_val))
9282 return IOCB_ERROR;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05009283 creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
9284 writel(creg_val, phba->HCregaddr);
9285 readl(phba->HCregaddr); /* flush */
9286 }
9287
James Smart2a9bf3d2010-06-07 15:24:45 -04009288 retval = lpfc_sli_issue_iocb(phba, ring_number, piocb,
9289 SLI_IOCB_RET_IOCB);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009290 if (retval == IOCB_SUCCESS) {
9291 timeout_req = timeout * HZ;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009292 timeleft = wait_event_timeout(done_q,
James Smartd11e31d2009-06-10 17:23:06 -04009293 lpfc_chk_iocb_flg(phba, piocb, LPFC_IO_WAKE),
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009294 timeout_req);
dea31012005-04-17 16:05:31 -05009295
James Smart7054a602007-04-25 09:52:34 -04009296 if (piocb->iocb_flag & LPFC_IO_WAKE) {
9297 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04009298 "0331 IOCB wake signaled\n");
James Smart7054a602007-04-25 09:52:34 -04009299 } else if (timeleft == 0) {
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009300 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04009301 "0338 IOCB wait timeout error - no "
9302 "wake response Data x%x\n", timeout);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009303 retval = IOCB_TIMEDOUT;
James Smart7054a602007-04-25 09:52:34 -04009304 } else {
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009305 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04009306 "0330 IOCB wake NOT set, "
9307 "Data x%x x%lx\n",
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009308 timeout, (timeleft / jiffies));
9309 retval = IOCB_TIMEDOUT;
dea31012005-04-17 16:05:31 -05009310 }
James Smart2a9bf3d2010-06-07 15:24:45 -04009311 } else if (retval == IOCB_BUSY) {
9312 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
9313 "2818 Max IOCBs %d txq cnt %d txcmplq cnt %d\n",
9314 phba->iocb_cnt, pring->txq_cnt, pring->txcmplq_cnt);
9315 return retval;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009316 } else {
9317 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smartd7c255b2008-08-24 21:50:00 -04009318 "0332 IOCB wait issue failed, Data x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04009319 retval);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009320 retval = IOCB_ERROR;
dea31012005-04-17 16:05:31 -05009321 }
9322
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05009323 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
James Smart9940b972011-03-11 16:06:12 -05009324 if (lpfc_readl(phba->HCregaddr, &creg_val))
9325 return IOCB_ERROR;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05009326 creg_val &= ~(HC_R0INT_ENA << LPFC_FCP_RING);
9327 writel(creg_val, phba->HCregaddr);
9328 readl(phba->HCregaddr); /* flush */
9329 }
9330
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009331 if (prspiocbq)
9332 piocb->context2 = NULL;
9333
9334 piocb->context_un.wait_queue = NULL;
9335 piocb->iocb_cmpl = NULL;
dea31012005-04-17 16:05:31 -05009336 return retval;
9337}
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04009338
James Smarte59058c2008-08-24 21:49:00 -04009339/**
James Smart3621a712009-04-06 18:47:14 -04009340 * lpfc_sli_issue_mbox_wait - Synchronous function to issue mailbox
James Smarte59058c2008-08-24 21:49:00 -04009341 * @phba: Pointer to HBA context object.
9342 * @pmboxq: Pointer to driver mailbox object.
9343 * @timeout: Timeout in number of seconds.
9344 *
9345 * This function issues the mailbox to firmware and waits for the
9346 * mailbox command to complete. If the mailbox command is not
9347 * completed within timeout seconds, it returns MBX_TIMEOUT.
9348 * The function waits for the mailbox completion using an
9349 * interruptible wait. If the thread is woken up due to a
9350 * signal, MBX_TIMEOUT error is returned to the caller. Caller
9351 * should not free the mailbox resources, if this function returns
9352 * MBX_TIMEOUT.
9353 * This function will sleep while waiting for mailbox completion.
9354 * So, this function should not be called from any context which
9355 * does not allow sleeping. Due to the same reason, this function
9356 * cannot be called with interrupt disabled.
9357 * This function assumes that the mailbox completion occurs while
9358 * this function sleep. So, this function cannot be called from
9359 * the worker thread which processes mailbox completion.
9360 * This function is called in the context of HBA management
9361 * applications.
9362 * This function returns MBX_SUCCESS when successful.
9363 * This function is called with no lock held.
9364 **/
dea31012005-04-17 16:05:31 -05009365int
James Smart2e0fef82007-06-17 19:56:36 -05009366lpfc_sli_issue_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq,
dea31012005-04-17 16:05:31 -05009367 uint32_t timeout)
9368{
Peter Zijlstra7259f0d2006-10-29 22:46:36 -08009369 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
dea31012005-04-17 16:05:31 -05009370 int retval;
James Smart858c9f62007-06-17 19:56:39 -05009371 unsigned long flag;
dea31012005-04-17 16:05:31 -05009372
9373 /* The caller must leave context1 empty. */
James Smart98c9ea52007-10-27 13:37:33 -04009374 if (pmboxq->context1)
James Smart2e0fef82007-06-17 19:56:36 -05009375 return MBX_NOT_FINISHED;
dea31012005-04-17 16:05:31 -05009376
James Smart495a7142008-06-14 22:52:59 -04009377 pmboxq->mbox_flag &= ~LPFC_MBX_WAKE;
dea31012005-04-17 16:05:31 -05009378 /* setup wake call as IOCB callback */
9379 pmboxq->mbox_cmpl = lpfc_sli_wake_mbox_wait;
9380 /* setup context field to pass wait_queue pointer to wake function */
9381 pmboxq->context1 = &done_q;
9382
dea31012005-04-17 16:05:31 -05009383 /* now issue the command */
9384 retval = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
9385
9386 if (retval == MBX_BUSY || retval == MBX_SUCCESS) {
James Smart7054a602007-04-25 09:52:34 -04009387 wait_event_interruptible_timeout(done_q,
9388 pmboxq->mbox_flag & LPFC_MBX_WAKE,
9389 timeout * HZ);
9390
James Smart858c9f62007-06-17 19:56:39 -05009391 spin_lock_irqsave(&phba->hbalock, flag);
dea31012005-04-17 16:05:31 -05009392 pmboxq->context1 = NULL;
James Smart7054a602007-04-25 09:52:34 -04009393 /*
9394 * if LPFC_MBX_WAKE flag is set the mailbox is completed
9395 * else do not free the resources.
9396 */
James Smartd7c47992010-06-08 18:31:54 -04009397 if (pmboxq->mbox_flag & LPFC_MBX_WAKE) {
dea31012005-04-17 16:05:31 -05009398 retval = MBX_SUCCESS;
James Smartd7c47992010-06-08 18:31:54 -04009399 lpfc_sli4_swap_str(phba, pmboxq);
9400 } else {
James Smart7054a602007-04-25 09:52:34 -04009401 retval = MBX_TIMEOUT;
James Smart858c9f62007-06-17 19:56:39 -05009402 pmboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
9403 }
9404 spin_unlock_irqrestore(&phba->hbalock, flag);
dea31012005-04-17 16:05:31 -05009405 }
9406
dea31012005-04-17 16:05:31 -05009407 return retval;
9408}
9409
James Smarte59058c2008-08-24 21:49:00 -04009410/**
James Smart3772a992009-05-22 14:50:54 -04009411 * lpfc_sli_mbox_sys_shutdown - shutdown mailbox command sub-system
James Smarte59058c2008-08-24 21:49:00 -04009412 * @phba: Pointer to HBA context.
9413 *
James Smart3772a992009-05-22 14:50:54 -04009414 * This function is called to shutdown the driver's mailbox sub-system.
9415 * It first marks the mailbox sub-system is in a block state to prevent
9416 * the asynchronous mailbox command from issued off the pending mailbox
9417 * command queue. If the mailbox command sub-system shutdown is due to
9418 * HBA error conditions such as EEH or ERATT, this routine shall invoke
9419 * the mailbox sub-system flush routine to forcefully bring down the
9420 * mailbox sub-system. Otherwise, if it is due to normal condition (such
9421 * as with offline or HBA function reset), this routine will wait for the
9422 * outstanding mailbox command to complete before invoking the mailbox
9423 * sub-system flush routine to gracefully bring down mailbox sub-system.
James Smarte59058c2008-08-24 21:49:00 -04009424 **/
James Smart3772a992009-05-22 14:50:54 -04009425void
9426lpfc_sli_mbox_sys_shutdown(struct lpfc_hba *phba)
James Smartb4c02652006-07-06 15:50:43 -04009427{
James Smart3772a992009-05-22 14:50:54 -04009428 struct lpfc_sli *psli = &phba->sli;
James Smart3772a992009-05-22 14:50:54 -04009429 unsigned long timeout;
9430
James Smarta183a152011-10-10 21:32:43 -04009431 timeout = msecs_to_jiffies(LPFC_MBOX_TMO * 1000) + jiffies;
James Smart3772a992009-05-22 14:50:54 -04009432 spin_lock_irq(&phba->hbalock);
9433 psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
9434 spin_unlock_irq(&phba->hbalock);
9435
9436 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
9437 spin_lock_irq(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -04009438 /* Determine how long we might wait for the active mailbox
9439 * command to be gracefully completed by firmware.
9440 */
James Smarta183a152011-10-10 21:32:43 -04009441 if (phba->sli.mbox_active)
9442 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
9443 phba->sli.mbox_active) *
9444 1000) + jiffies;
9445 spin_unlock_irq(&phba->hbalock);
9446
James Smart3772a992009-05-22 14:50:54 -04009447 while (phba->sli.mbox_active) {
9448 /* Check active mailbox complete status every 2ms */
9449 msleep(2);
9450 if (time_after(jiffies, timeout))
9451 /* Timeout, let the mailbox flush routine to
9452 * forcefully release active mailbox command
9453 */
9454 break;
9455 }
9456 }
9457 lpfc_sli_mbox_sys_flush(phba);
9458}
9459
9460/**
9461 * lpfc_sli_eratt_read - read sli-3 error attention events
9462 * @phba: Pointer to HBA context.
9463 *
9464 * This function is called to read the SLI3 device error attention registers
9465 * for possible error attention events. The caller must hold the hostlock
9466 * with spin_lock_irq().
9467 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03009468 * This function returns 1 when there is Error Attention in the Host Attention
James Smart3772a992009-05-22 14:50:54 -04009469 * Register and returns 0 otherwise.
9470 **/
9471static int
9472lpfc_sli_eratt_read(struct lpfc_hba *phba)
9473{
James Smarted957682007-06-17 19:56:37 -05009474 uint32_t ha_copy;
James Smartb4c02652006-07-06 15:50:43 -04009475
James Smart3772a992009-05-22 14:50:54 -04009476 /* Read chip Host Attention (HA) register */
James Smart9940b972011-03-11 16:06:12 -05009477 if (lpfc_readl(phba->HAregaddr, &ha_copy))
9478 goto unplug_err;
9479
James Smart3772a992009-05-22 14:50:54 -04009480 if (ha_copy & HA_ERATT) {
9481 /* Read host status register to retrieve error event */
James Smart9940b972011-03-11 16:06:12 -05009482 if (lpfc_sli_read_hs(phba))
9483 goto unplug_err;
James Smartb4c02652006-07-06 15:50:43 -04009484
James Smart3772a992009-05-22 14:50:54 -04009485 /* Check if there is a deferred error condition is active */
9486 if ((HS_FFER1 & phba->work_hs) &&
9487 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
James Smartdcf2a4e2010-09-29 11:18:53 -04009488 HS_FFER6 | HS_FFER7 | HS_FFER8) & phba->work_hs)) {
James Smart3772a992009-05-22 14:50:54 -04009489 phba->hba_flag |= DEFER_ERATT;
James Smart3772a992009-05-22 14:50:54 -04009490 /* Clear all interrupt enable conditions */
9491 writel(0, phba->HCregaddr);
9492 readl(phba->HCregaddr);
9493 }
9494
9495 /* Set the driver HA work bitmap */
James Smart3772a992009-05-22 14:50:54 -04009496 phba->work_ha |= HA_ERATT;
9497 /* Indicate polling handles this ERATT */
9498 phba->hba_flag |= HBA_ERATT_HANDLED;
James Smart3772a992009-05-22 14:50:54 -04009499 return 1;
James Smartb4c02652006-07-06 15:50:43 -04009500 }
James Smart3772a992009-05-22 14:50:54 -04009501 return 0;
James Smart9940b972011-03-11 16:06:12 -05009502
9503unplug_err:
9504 /* Set the driver HS work bitmap */
9505 phba->work_hs |= UNPLUG_ERR;
9506 /* Set the driver HA work bitmap */
9507 phba->work_ha |= HA_ERATT;
9508 /* Indicate polling handles this ERATT */
9509 phba->hba_flag |= HBA_ERATT_HANDLED;
9510 return 1;
James Smartb4c02652006-07-06 15:50:43 -04009511}
9512
James Smarte59058c2008-08-24 21:49:00 -04009513/**
James Smartda0436e2009-05-22 14:51:39 -04009514 * lpfc_sli4_eratt_read - read sli-4 error attention events
9515 * @phba: Pointer to HBA context.
9516 *
9517 * This function is called to read the SLI4 device error attention registers
9518 * for possible error attention events. The caller must hold the hostlock
9519 * with spin_lock_irq().
9520 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03009521 * This function returns 1 when there is Error Attention in the Host Attention
James Smartda0436e2009-05-22 14:51:39 -04009522 * Register and returns 0 otherwise.
9523 **/
9524static int
9525lpfc_sli4_eratt_read(struct lpfc_hba *phba)
9526{
9527 uint32_t uerr_sta_hi, uerr_sta_lo;
James Smart2fcee4b2010-12-15 17:57:46 -05009528 uint32_t if_type, portsmphr;
9529 struct lpfc_register portstat_reg;
James Smartda0436e2009-05-22 14:51:39 -04009530
James Smart2fcee4b2010-12-15 17:57:46 -05009531 /*
9532 * For now, use the SLI4 device internal unrecoverable error
James Smartda0436e2009-05-22 14:51:39 -04009533 * registers for error attention. This can be changed later.
9534 */
James Smart2fcee4b2010-12-15 17:57:46 -05009535 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
9536 switch (if_type) {
9537 case LPFC_SLI_INTF_IF_TYPE_0:
James Smart9940b972011-03-11 16:06:12 -05009538 if (lpfc_readl(phba->sli4_hba.u.if_type0.UERRLOregaddr,
9539 &uerr_sta_lo) ||
9540 lpfc_readl(phba->sli4_hba.u.if_type0.UERRHIregaddr,
9541 &uerr_sta_hi)) {
9542 phba->work_hs |= UNPLUG_ERR;
9543 phba->work_ha |= HA_ERATT;
9544 phba->hba_flag |= HBA_ERATT_HANDLED;
9545 return 1;
9546 }
James Smart2fcee4b2010-12-15 17:57:46 -05009547 if ((~phba->sli4_hba.ue_mask_lo & uerr_sta_lo) ||
9548 (~phba->sli4_hba.ue_mask_hi & uerr_sta_hi)) {
9549 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9550 "1423 HBA Unrecoverable error: "
9551 "uerr_lo_reg=0x%x, uerr_hi_reg=0x%x, "
9552 "ue_mask_lo_reg=0x%x, "
9553 "ue_mask_hi_reg=0x%x\n",
9554 uerr_sta_lo, uerr_sta_hi,
9555 phba->sli4_hba.ue_mask_lo,
9556 phba->sli4_hba.ue_mask_hi);
9557 phba->work_status[0] = uerr_sta_lo;
9558 phba->work_status[1] = uerr_sta_hi;
9559 phba->work_ha |= HA_ERATT;
9560 phba->hba_flag |= HBA_ERATT_HANDLED;
9561 return 1;
9562 }
9563 break;
9564 case LPFC_SLI_INTF_IF_TYPE_2:
James Smart9940b972011-03-11 16:06:12 -05009565 if (lpfc_readl(phba->sli4_hba.u.if_type2.STATUSregaddr,
9566 &portstat_reg.word0) ||
9567 lpfc_readl(phba->sli4_hba.PSMPHRregaddr,
9568 &portsmphr)){
9569 phba->work_hs |= UNPLUG_ERR;
9570 phba->work_ha |= HA_ERATT;
9571 phba->hba_flag |= HBA_ERATT_HANDLED;
9572 return 1;
9573 }
James Smart2fcee4b2010-12-15 17:57:46 -05009574 if (bf_get(lpfc_sliport_status_err, &portstat_reg)) {
9575 phba->work_status[0] =
9576 readl(phba->sli4_hba.u.if_type2.ERR1regaddr);
9577 phba->work_status[1] =
9578 readl(phba->sli4_hba.u.if_type2.ERR2regaddr);
9579 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9580 "2885 Port Error Detected: "
9581 "port status reg 0x%x, "
9582 "port smphr reg 0x%x, "
9583 "error 1=0x%x, error 2=0x%x\n",
9584 portstat_reg.word0,
9585 portsmphr,
9586 phba->work_status[0],
9587 phba->work_status[1]);
9588 phba->work_ha |= HA_ERATT;
9589 phba->hba_flag |= HBA_ERATT_HANDLED;
9590 return 1;
9591 }
9592 break;
9593 case LPFC_SLI_INTF_IF_TYPE_1:
9594 default:
James Smarta747c9c2009-11-18 15:41:10 -05009595 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart2fcee4b2010-12-15 17:57:46 -05009596 "2886 HBA Error Attention on unsupported "
9597 "if type %d.", if_type);
James Smarta747c9c2009-11-18 15:41:10 -05009598 return 1;
James Smartda0436e2009-05-22 14:51:39 -04009599 }
James Smart2fcee4b2010-12-15 17:57:46 -05009600
James Smartda0436e2009-05-22 14:51:39 -04009601 return 0;
9602}
9603
9604/**
James Smart3621a712009-04-06 18:47:14 -04009605 * lpfc_sli_check_eratt - check error attention events
James Smart93996272008-08-24 21:50:30 -04009606 * @phba: Pointer to HBA context.
9607 *
James Smart3772a992009-05-22 14:50:54 -04009608 * This function is called from timer soft interrupt context to check HBA's
James Smart93996272008-08-24 21:50:30 -04009609 * error attention register bit for error attention events.
9610 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03009611 * This function returns 1 when there is Error Attention in the Host Attention
James Smart93996272008-08-24 21:50:30 -04009612 * Register and returns 0 otherwise.
9613 **/
9614int
9615lpfc_sli_check_eratt(struct lpfc_hba *phba)
9616{
9617 uint32_t ha_copy;
9618
9619 /* If somebody is waiting to handle an eratt, don't process it
9620 * here. The brdkill function will do this.
9621 */
9622 if (phba->link_flag & LS_IGNORE_ERATT)
9623 return 0;
9624
9625 /* Check if interrupt handler handles this ERATT */
9626 spin_lock_irq(&phba->hbalock);
9627 if (phba->hba_flag & HBA_ERATT_HANDLED) {
9628 /* Interrupt handler has handled ERATT */
9629 spin_unlock_irq(&phba->hbalock);
9630 return 0;
9631 }
9632
James Smarta257bf92009-04-06 18:48:10 -04009633 /*
9634 * If there is deferred error attention, do not check for error
9635 * attention
9636 */
9637 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
9638 spin_unlock_irq(&phba->hbalock);
9639 return 0;
9640 }
9641
James Smart3772a992009-05-22 14:50:54 -04009642 /* If PCI channel is offline, don't process it */
9643 if (unlikely(pci_channel_offline(phba->pcidev))) {
James Smart93996272008-08-24 21:50:30 -04009644 spin_unlock_irq(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -04009645 return 0;
9646 }
9647
9648 switch (phba->sli_rev) {
9649 case LPFC_SLI_REV2:
9650 case LPFC_SLI_REV3:
9651 /* Read chip Host Attention (HA) register */
9652 ha_copy = lpfc_sli_eratt_read(phba);
9653 break;
James Smartda0436e2009-05-22 14:51:39 -04009654 case LPFC_SLI_REV4:
James Smart2fcee4b2010-12-15 17:57:46 -05009655 /* Read device Uncoverable Error (UERR) registers */
James Smartda0436e2009-05-22 14:51:39 -04009656 ha_copy = lpfc_sli4_eratt_read(phba);
9657 break;
James Smart3772a992009-05-22 14:50:54 -04009658 default:
9659 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9660 "0299 Invalid SLI revision (%d)\n",
9661 phba->sli_rev);
9662 ha_copy = 0;
9663 break;
James Smart93996272008-08-24 21:50:30 -04009664 }
9665 spin_unlock_irq(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -04009666
9667 return ha_copy;
9668}
9669
9670/**
9671 * lpfc_intr_state_check - Check device state for interrupt handling
9672 * @phba: Pointer to HBA context.
9673 *
9674 * This inline routine checks whether a device or its PCI slot is in a state
9675 * that the interrupt should be handled.
9676 *
9677 * This function returns 0 if the device or the PCI slot is in a state that
9678 * interrupt should be handled, otherwise -EIO.
9679 */
9680static inline int
9681lpfc_intr_state_check(struct lpfc_hba *phba)
9682{
9683 /* If the pci channel is offline, ignore all the interrupts */
9684 if (unlikely(pci_channel_offline(phba->pcidev)))
9685 return -EIO;
9686
9687 /* Update device level interrupt statistics */
9688 phba->sli.slistat.sli_intr++;
9689
9690 /* Ignore all interrupts during initialization. */
9691 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
9692 return -EIO;
9693
James Smart93996272008-08-24 21:50:30 -04009694 return 0;
9695}
9696
9697/**
James Smart3772a992009-05-22 14:50:54 -04009698 * lpfc_sli_sp_intr_handler - Slow-path interrupt handler to SLI-3 device
James Smarte59058c2008-08-24 21:49:00 -04009699 * @irq: Interrupt number.
9700 * @dev_id: The device context pointer.
9701 *
James Smart93996272008-08-24 21:50:30 -04009702 * This function is directly called from the PCI layer as an interrupt
James Smart3772a992009-05-22 14:50:54 -04009703 * service routine when device with SLI-3 interface spec is enabled with
9704 * MSI-X multi-message interrupt mode and there are slow-path events in
9705 * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
9706 * interrupt mode, this function is called as part of the device-level
9707 * interrupt handler. When the PCI slot is in error recovery or the HBA
9708 * is undergoing initialization, the interrupt handler will not process
9709 * the interrupt. The link attention and ELS ring attention events are
9710 * handled by the worker thread. The interrupt handler signals the worker
9711 * thread and returns for these events. This function is called without
9712 * any lock held. It gets the hbalock to access and update SLI data
James Smart93996272008-08-24 21:50:30 -04009713 * structures.
9714 *
9715 * This function returns IRQ_HANDLED when interrupt is handled else it
9716 * returns IRQ_NONE.
James Smarte59058c2008-08-24 21:49:00 -04009717 **/
dea31012005-04-17 16:05:31 -05009718irqreturn_t
James Smart3772a992009-05-22 14:50:54 -04009719lpfc_sli_sp_intr_handler(int irq, void *dev_id)
dea31012005-04-17 16:05:31 -05009720{
James Smart2e0fef82007-06-17 19:56:36 -05009721 struct lpfc_hba *phba;
James Smarta747c9c2009-11-18 15:41:10 -05009722 uint32_t ha_copy, hc_copy;
dea31012005-04-17 16:05:31 -05009723 uint32_t work_ha_copy;
9724 unsigned long status;
James Smart5b75da22008-12-04 22:39:35 -05009725 unsigned long iflag;
dea31012005-04-17 16:05:31 -05009726 uint32_t control;
9727
James Smart92d7f7b2007-06-17 19:56:38 -05009728 MAILBOX_t *mbox, *pmbox;
James Smart858c9f62007-06-17 19:56:39 -05009729 struct lpfc_vport *vport;
9730 struct lpfc_nodelist *ndlp;
9731 struct lpfc_dmabuf *mp;
James Smart92d7f7b2007-06-17 19:56:38 -05009732 LPFC_MBOXQ_t *pmb;
9733 int rc;
9734
dea31012005-04-17 16:05:31 -05009735 /*
9736 * Get the driver's phba structure from the dev_id and
9737 * assume the HBA is not interrupting.
9738 */
James Smart93996272008-08-24 21:50:30 -04009739 phba = (struct lpfc_hba *)dev_id;
dea31012005-04-17 16:05:31 -05009740
9741 if (unlikely(!phba))
9742 return IRQ_NONE;
9743
dea31012005-04-17 16:05:31 -05009744 /*
James Smart93996272008-08-24 21:50:30 -04009745 * Stuff needs to be attented to when this function is invoked as an
9746 * individual interrupt handler in MSI-X multi-message interrupt mode
dea31012005-04-17 16:05:31 -05009747 */
James Smart93996272008-08-24 21:50:30 -04009748 if (phba->intr_type == MSIX) {
James Smart3772a992009-05-22 14:50:54 -04009749 /* Check device state for handling interrupt */
9750 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -04009751 return IRQ_NONE;
9752 /* Need to read HA REG for slow-path events */
James Smart5b75da22008-12-04 22:39:35 -05009753 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart9940b972011-03-11 16:06:12 -05009754 if (lpfc_readl(phba->HAregaddr, &ha_copy))
9755 goto unplug_error;
James Smart93996272008-08-24 21:50:30 -04009756 /* If somebody is waiting to handle an eratt don't process it
9757 * here. The brdkill function will do this.
9758 */
9759 if (phba->link_flag & LS_IGNORE_ERATT)
9760 ha_copy &= ~HA_ERATT;
9761 /* Check the need for handling ERATT in interrupt handler */
9762 if (ha_copy & HA_ERATT) {
9763 if (phba->hba_flag & HBA_ERATT_HANDLED)
9764 /* ERATT polling has handled ERATT */
9765 ha_copy &= ~HA_ERATT;
9766 else
9767 /* Indicate interrupt handler handles ERATT */
9768 phba->hba_flag |= HBA_ERATT_HANDLED;
9769 }
James Smarta257bf92009-04-06 18:48:10 -04009770
9771 /*
9772 * If there is deferred error attention, do not check for any
9773 * interrupt.
9774 */
9775 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
James Smart3772a992009-05-22 14:50:54 -04009776 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -04009777 return IRQ_NONE;
9778 }
9779
James Smart93996272008-08-24 21:50:30 -04009780 /* Clear up only attention source related to slow-path */
James Smart9940b972011-03-11 16:06:12 -05009781 if (lpfc_readl(phba->HCregaddr, &hc_copy))
9782 goto unplug_error;
9783
James Smarta747c9c2009-11-18 15:41:10 -05009784 writel(hc_copy & ~(HC_MBINT_ENA | HC_R2INT_ENA |
9785 HC_LAINT_ENA | HC_ERINT_ENA),
9786 phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -04009787 writel((ha_copy & (HA_MBATT | HA_R2_CLR_MSK)),
9788 phba->HAregaddr);
James Smarta747c9c2009-11-18 15:41:10 -05009789 writel(hc_copy, phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -04009790 readl(phba->HAregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -05009791 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -04009792 } else
9793 ha_copy = phba->ha_copy;
dea31012005-04-17 16:05:31 -05009794
dea31012005-04-17 16:05:31 -05009795 work_ha_copy = ha_copy & phba->work_ha_mask;
9796
James Smart93996272008-08-24 21:50:30 -04009797 if (work_ha_copy) {
dea31012005-04-17 16:05:31 -05009798 if (work_ha_copy & HA_LATT) {
9799 if (phba->sli.sli_flag & LPFC_PROCESS_LA) {
9800 /*
9801 * Turn off Link Attention interrupts
9802 * until CLEAR_LA done
9803 */
James Smart5b75da22008-12-04 22:39:35 -05009804 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05009805 phba->sli.sli_flag &= ~LPFC_PROCESS_LA;
James Smart9940b972011-03-11 16:06:12 -05009806 if (lpfc_readl(phba->HCregaddr, &control))
9807 goto unplug_error;
dea31012005-04-17 16:05:31 -05009808 control &= ~HC_LAINT_ENA;
9809 writel(control, phba->HCregaddr);
9810 readl(phba->HCregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -05009811 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05009812 }
9813 else
9814 work_ha_copy &= ~HA_LATT;
9815 }
9816
James Smart93996272008-08-24 21:50:30 -04009817 if (work_ha_copy & ~(HA_ERATT | HA_MBATT | HA_LATT)) {
James Smart858c9f62007-06-17 19:56:39 -05009818 /*
9819 * Turn off Slow Rings interrupts, LPFC_ELS_RING is
9820 * the only slow ring.
9821 */
9822 status = (work_ha_copy &
9823 (HA_RXMASK << (4*LPFC_ELS_RING)));
9824 status >>= (4*LPFC_ELS_RING);
9825 if (status & HA_RXMASK) {
James Smart5b75da22008-12-04 22:39:35 -05009826 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart9940b972011-03-11 16:06:12 -05009827 if (lpfc_readl(phba->HCregaddr, &control))
9828 goto unplug_error;
James Smarta58cbd52007-08-02 11:09:43 -04009829
9830 lpfc_debugfs_slow_ring_trc(phba,
9831 "ISR slow ring: ctl:x%x stat:x%x isrcnt:x%x",
9832 control, status,
9833 (uint32_t)phba->sli.slistat.sli_intr);
9834
James Smart858c9f62007-06-17 19:56:39 -05009835 if (control & (HC_R0INT_ENA << LPFC_ELS_RING)) {
James Smarta58cbd52007-08-02 11:09:43 -04009836 lpfc_debugfs_slow_ring_trc(phba,
9837 "ISR Disable ring:"
9838 "pwork:x%x hawork:x%x wait:x%x",
9839 phba->work_ha, work_ha_copy,
9840 (uint32_t)((unsigned long)
James Smart5e9d9b82008-06-14 22:52:53 -04009841 &phba->work_waitq));
James Smarta58cbd52007-08-02 11:09:43 -04009842
James Smart858c9f62007-06-17 19:56:39 -05009843 control &=
9844 ~(HC_R0INT_ENA << LPFC_ELS_RING);
dea31012005-04-17 16:05:31 -05009845 writel(control, phba->HCregaddr);
9846 readl(phba->HCregaddr); /* flush */
dea31012005-04-17 16:05:31 -05009847 }
James Smarta58cbd52007-08-02 11:09:43 -04009848 else {
9849 lpfc_debugfs_slow_ring_trc(phba,
9850 "ISR slow ring: pwork:"
9851 "x%x hawork:x%x wait:x%x",
9852 phba->work_ha, work_ha_copy,
9853 (uint32_t)((unsigned long)
James Smart5e9d9b82008-06-14 22:52:53 -04009854 &phba->work_waitq));
James Smarta58cbd52007-08-02 11:09:43 -04009855 }
James Smart5b75da22008-12-04 22:39:35 -05009856 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05009857 }
9858 }
James Smart5b75da22008-12-04 22:39:35 -05009859 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -04009860 if (work_ha_copy & HA_ERATT) {
James Smart9940b972011-03-11 16:06:12 -05009861 if (lpfc_sli_read_hs(phba))
9862 goto unplug_error;
James Smarta257bf92009-04-06 18:48:10 -04009863 /*
9864 * Check if there is a deferred error condition
9865 * is active
9866 */
9867 if ((HS_FFER1 & phba->work_hs) &&
9868 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
James Smartdcf2a4e2010-09-29 11:18:53 -04009869 HS_FFER6 | HS_FFER7 | HS_FFER8) &
9870 phba->work_hs)) {
James Smarta257bf92009-04-06 18:48:10 -04009871 phba->hba_flag |= DEFER_ERATT;
9872 /* Clear all interrupt enable conditions */
9873 writel(0, phba->HCregaddr);
9874 readl(phba->HCregaddr);
9875 }
9876 }
9877
James Smart93996272008-08-24 21:50:30 -04009878 if ((work_ha_copy & HA_MBATT) && (phba->sli.mbox_active)) {
James Smart92d7f7b2007-06-17 19:56:38 -05009879 pmb = phba->sli.mbox_active;
James Smart04c68492009-05-22 14:52:52 -04009880 pmbox = &pmb->u.mb;
James Smart34b02dc2008-08-24 21:49:55 -04009881 mbox = phba->mbox;
James Smart858c9f62007-06-17 19:56:39 -05009882 vport = pmb->vport;
James Smart92d7f7b2007-06-17 19:56:38 -05009883
9884 /* First check out the status word */
9885 lpfc_sli_pcimem_bcopy(mbox, pmbox, sizeof(uint32_t));
9886 if (pmbox->mbxOwner != OWN_HOST) {
James Smart5b75da22008-12-04 22:39:35 -05009887 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart92d7f7b2007-06-17 19:56:38 -05009888 /*
9889 * Stray Mailbox Interrupt, mbxCommand <cmd>
9890 * mbxStatus <status>
9891 */
James Smart09372822008-01-11 01:52:54 -05009892 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
James Smart92d7f7b2007-06-17 19:56:38 -05009893 LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04009894 "(%d):0304 Stray Mailbox "
James Smart92d7f7b2007-06-17 19:56:38 -05009895 "Interrupt mbxCommand x%x "
9896 "mbxStatus x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04009897 (vport ? vport->vpi : 0),
James Smart92d7f7b2007-06-17 19:56:38 -05009898 pmbox->mbxCommand,
9899 pmbox->mbxStatus);
James Smart09372822008-01-11 01:52:54 -05009900 /* clear mailbox attention bit */
9901 work_ha_copy &= ~HA_MBATT;
9902 } else {
James Smart97eab632008-04-07 10:16:05 -04009903 phba->sli.mbox_active = NULL;
James Smart5b75da22008-12-04 22:39:35 -05009904 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart09372822008-01-11 01:52:54 -05009905 phba->last_completion_time = jiffies;
9906 del_timer(&phba->sli.mbox_tmo);
James Smart09372822008-01-11 01:52:54 -05009907 if (pmb->mbox_cmpl) {
9908 lpfc_sli_pcimem_bcopy(mbox, pmbox,
9909 MAILBOX_CMD_SIZE);
James Smart7a470272010-03-15 11:25:20 -04009910 if (pmb->out_ext_byte_len &&
9911 pmb->context2)
9912 lpfc_sli_pcimem_bcopy(
9913 phba->mbox_ext,
9914 pmb->context2,
9915 pmb->out_ext_byte_len);
James Smart858c9f62007-06-17 19:56:39 -05009916 }
James Smart09372822008-01-11 01:52:54 -05009917 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
9918 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
9919
9920 lpfc_debugfs_disc_trc(vport,
9921 LPFC_DISC_TRC_MBOX_VPORT,
9922 "MBOX dflt rpi: : "
9923 "status:x%x rpi:x%x",
9924 (uint32_t)pmbox->mbxStatus,
9925 pmbox->un.varWords[0], 0);
9926
9927 if (!pmbox->mbxStatus) {
9928 mp = (struct lpfc_dmabuf *)
9929 (pmb->context1);
9930 ndlp = (struct lpfc_nodelist *)
9931 pmb->context2;
9932
9933 /* Reg_LOGIN of dflt RPI was
9934 * successful. new lets get
9935 * rid of the RPI using the
9936 * same mbox buffer.
9937 */
9938 lpfc_unreg_login(phba,
9939 vport->vpi,
9940 pmbox->un.varWords[0],
9941 pmb);
9942 pmb->mbox_cmpl =
9943 lpfc_mbx_cmpl_dflt_rpi;
9944 pmb->context1 = mp;
9945 pmb->context2 = ndlp;
9946 pmb->vport = vport;
James Smart58da1ff2008-04-07 10:15:56 -04009947 rc = lpfc_sli_issue_mbox(phba,
9948 pmb,
9949 MBX_NOWAIT);
9950 if (rc != MBX_BUSY)
9951 lpfc_printf_log(phba,
9952 KERN_ERR,
9953 LOG_MBOX | LOG_SLI,
James Smartd7c255b2008-08-24 21:50:00 -04009954 "0350 rc should have"
James Smart6a9c52c2009-10-02 15:16:51 -04009955 "been MBX_BUSY\n");
James Smart3772a992009-05-22 14:50:54 -04009956 if (rc != MBX_NOT_FINISHED)
9957 goto send_current_mbox;
James Smart09372822008-01-11 01:52:54 -05009958 }
9959 }
James Smart5b75da22008-12-04 22:39:35 -05009960 spin_lock_irqsave(
9961 &phba->pport->work_port_lock,
9962 iflag);
James Smart09372822008-01-11 01:52:54 -05009963 phba->pport->work_port_events &=
9964 ~WORKER_MBOX_TMO;
James Smart5b75da22008-12-04 22:39:35 -05009965 spin_unlock_irqrestore(
9966 &phba->pport->work_port_lock,
9967 iflag);
James Smart09372822008-01-11 01:52:54 -05009968 lpfc_mbox_cmpl_put(phba, pmb);
James Smart858c9f62007-06-17 19:56:39 -05009969 }
James Smart97eab632008-04-07 10:16:05 -04009970 } else
James Smart5b75da22008-12-04 22:39:35 -05009971 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -04009972
James Smart92d7f7b2007-06-17 19:56:38 -05009973 if ((work_ha_copy & HA_MBATT) &&
9974 (phba->sli.mbox_active == NULL)) {
James Smart858c9f62007-06-17 19:56:39 -05009975send_current_mbox:
James Smart92d7f7b2007-06-17 19:56:38 -05009976 /* Process next mailbox command if there is one */
James Smart58da1ff2008-04-07 10:15:56 -04009977 do {
9978 rc = lpfc_sli_issue_mbox(phba, NULL,
9979 MBX_NOWAIT);
9980 } while (rc == MBX_NOT_FINISHED);
9981 if (rc != MBX_SUCCESS)
9982 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
9983 LOG_SLI, "0349 rc should be "
James Smart6a9c52c2009-10-02 15:16:51 -04009984 "MBX_SUCCESS\n");
James Smart92d7f7b2007-06-17 19:56:38 -05009985 }
9986
James Smart5b75da22008-12-04 22:39:35 -05009987 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05009988 phba->work_ha |= work_ha_copy;
James Smart5b75da22008-12-04 22:39:35 -05009989 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart5e9d9b82008-06-14 22:52:53 -04009990 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -05009991 }
James Smart93996272008-08-24 21:50:30 -04009992 return IRQ_HANDLED;
James Smart9940b972011-03-11 16:06:12 -05009993unplug_error:
9994 spin_unlock_irqrestore(&phba->hbalock, iflag);
9995 return IRQ_HANDLED;
dea31012005-04-17 16:05:31 -05009996
James Smart3772a992009-05-22 14:50:54 -04009997} /* lpfc_sli_sp_intr_handler */
James Smart93996272008-08-24 21:50:30 -04009998
9999/**
James Smart3772a992009-05-22 14:50:54 -040010000 * lpfc_sli_fp_intr_handler - Fast-path interrupt handler to SLI-3 device.
James Smart93996272008-08-24 21:50:30 -040010001 * @irq: Interrupt number.
10002 * @dev_id: The device context pointer.
10003 *
10004 * This function is directly called from the PCI layer as an interrupt
James Smart3772a992009-05-22 14:50:54 -040010005 * service routine when device with SLI-3 interface spec is enabled with
10006 * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
10007 * ring event in the HBA. However, when the device is enabled with either
10008 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
10009 * device-level interrupt handler. When the PCI slot is in error recovery
10010 * or the HBA is undergoing initialization, the interrupt handler will not
10011 * process the interrupt. The SCSI FCP fast-path ring event are handled in
10012 * the intrrupt context. This function is called without any lock held.
10013 * It gets the hbalock to access and update SLI data structures.
James Smart93996272008-08-24 21:50:30 -040010014 *
10015 * This function returns IRQ_HANDLED when interrupt is handled else it
10016 * returns IRQ_NONE.
10017 **/
10018irqreturn_t
James Smart3772a992009-05-22 14:50:54 -040010019lpfc_sli_fp_intr_handler(int irq, void *dev_id)
James Smart93996272008-08-24 21:50:30 -040010020{
10021 struct lpfc_hba *phba;
10022 uint32_t ha_copy;
10023 unsigned long status;
James Smart5b75da22008-12-04 22:39:35 -050010024 unsigned long iflag;
James Smart93996272008-08-24 21:50:30 -040010025
10026 /* Get the driver's phba structure from the dev_id and
10027 * assume the HBA is not interrupting.
10028 */
10029 phba = (struct lpfc_hba *) dev_id;
10030
10031 if (unlikely(!phba))
10032 return IRQ_NONE;
dea31012005-04-17 16:05:31 -050010033
10034 /*
James Smart93996272008-08-24 21:50:30 -040010035 * Stuff needs to be attented to when this function is invoked as an
10036 * individual interrupt handler in MSI-X multi-message interrupt mode
dea31012005-04-17 16:05:31 -050010037 */
James Smart93996272008-08-24 21:50:30 -040010038 if (phba->intr_type == MSIX) {
James Smart3772a992009-05-22 14:50:54 -040010039 /* Check device state for handling interrupt */
10040 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -040010041 return IRQ_NONE;
10042 /* Need to read HA REG for FCP ring and other ring events */
James Smart9940b972011-03-11 16:06:12 -050010043 if (lpfc_readl(phba->HAregaddr, &ha_copy))
10044 return IRQ_HANDLED;
James Smart93996272008-08-24 21:50:30 -040010045 /* Clear up only attention source related to fast-path */
James Smart5b75da22008-12-04 22:39:35 -050010046 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -040010047 /*
10048 * If there is deferred error attention, do not check for
10049 * any interrupt.
10050 */
10051 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
James Smart3772a992009-05-22 14:50:54 -040010052 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -040010053 return IRQ_NONE;
10054 }
James Smart93996272008-08-24 21:50:30 -040010055 writel((ha_copy & (HA_R0_CLR_MSK | HA_R1_CLR_MSK)),
10056 phba->HAregaddr);
10057 readl(phba->HAregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -050010058 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -040010059 } else
10060 ha_copy = phba->ha_copy;
10061
10062 /*
10063 * Process all events on FCP ring. Take the optimized path for FCP IO.
10064 */
10065 ha_copy &= ~(phba->work_ha_mask);
10066
10067 status = (ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
dea31012005-04-17 16:05:31 -050010068 status >>= (4*LPFC_FCP_RING);
James Smart858c9f62007-06-17 19:56:39 -050010069 if (status & HA_RXMASK)
dea31012005-04-17 16:05:31 -050010070 lpfc_sli_handle_fast_ring_event(phba,
10071 &phba->sli.ring[LPFC_FCP_RING],
10072 status);
James Smarta4bc3372006-12-02 13:34:16 -050010073
10074 if (phba->cfg_multi_ring_support == 2) {
10075 /*
James Smart93996272008-08-24 21:50:30 -040010076 * Process all events on extra ring. Take the optimized path
10077 * for extra ring IO.
James Smarta4bc3372006-12-02 13:34:16 -050010078 */
James Smart93996272008-08-24 21:50:30 -040010079 status = (ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
James Smarta4bc3372006-12-02 13:34:16 -050010080 status >>= (4*LPFC_EXTRA_RING);
James Smart858c9f62007-06-17 19:56:39 -050010081 if (status & HA_RXMASK) {
James Smarta4bc3372006-12-02 13:34:16 -050010082 lpfc_sli_handle_fast_ring_event(phba,
10083 &phba->sli.ring[LPFC_EXTRA_RING],
10084 status);
10085 }
10086 }
dea31012005-04-17 16:05:31 -050010087 return IRQ_HANDLED;
James Smart3772a992009-05-22 14:50:54 -040010088} /* lpfc_sli_fp_intr_handler */
dea31012005-04-17 16:05:31 -050010089
James Smart93996272008-08-24 21:50:30 -040010090/**
James Smart3772a992009-05-22 14:50:54 -040010091 * lpfc_sli_intr_handler - Device-level interrupt handler to SLI-3 device
James Smart93996272008-08-24 21:50:30 -040010092 * @irq: Interrupt number.
10093 * @dev_id: The device context pointer.
10094 *
James Smart3772a992009-05-22 14:50:54 -040010095 * This function is the HBA device-level interrupt handler to device with
10096 * SLI-3 interface spec, called from the PCI layer when either MSI or
10097 * Pin-IRQ interrupt mode is enabled and there is an event in the HBA which
10098 * requires driver attention. This function invokes the slow-path interrupt
10099 * attention handling function and fast-path interrupt attention handling
10100 * function in turn to process the relevant HBA attention events. This
10101 * function is called without any lock held. It gets the hbalock to access
10102 * and update SLI data structures.
James Smart93996272008-08-24 21:50:30 -040010103 *
10104 * This function returns IRQ_HANDLED when interrupt is handled, else it
10105 * returns IRQ_NONE.
10106 **/
10107irqreturn_t
James Smart3772a992009-05-22 14:50:54 -040010108lpfc_sli_intr_handler(int irq, void *dev_id)
James Smart93996272008-08-24 21:50:30 -040010109{
10110 struct lpfc_hba *phba;
10111 irqreturn_t sp_irq_rc, fp_irq_rc;
10112 unsigned long status1, status2;
James Smarta747c9c2009-11-18 15:41:10 -050010113 uint32_t hc_copy;
James Smart93996272008-08-24 21:50:30 -040010114
10115 /*
10116 * Get the driver's phba structure from the dev_id and
10117 * assume the HBA is not interrupting.
10118 */
10119 phba = (struct lpfc_hba *) dev_id;
10120
10121 if (unlikely(!phba))
10122 return IRQ_NONE;
10123
James Smart3772a992009-05-22 14:50:54 -040010124 /* Check device state for handling interrupt */
10125 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -040010126 return IRQ_NONE;
10127
10128 spin_lock(&phba->hbalock);
James Smart9940b972011-03-11 16:06:12 -050010129 if (lpfc_readl(phba->HAregaddr, &phba->ha_copy)) {
10130 spin_unlock(&phba->hbalock);
10131 return IRQ_HANDLED;
10132 }
10133
James Smart93996272008-08-24 21:50:30 -040010134 if (unlikely(!phba->ha_copy)) {
10135 spin_unlock(&phba->hbalock);
10136 return IRQ_NONE;
10137 } else if (phba->ha_copy & HA_ERATT) {
10138 if (phba->hba_flag & HBA_ERATT_HANDLED)
10139 /* ERATT polling has handled ERATT */
10140 phba->ha_copy &= ~HA_ERATT;
10141 else
10142 /* Indicate interrupt handler handles ERATT */
10143 phba->hba_flag |= HBA_ERATT_HANDLED;
10144 }
10145
James Smarta257bf92009-04-06 18:48:10 -040010146 /*
10147 * If there is deferred error attention, do not check for any interrupt.
10148 */
10149 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
Dan Carpenterec21b3b2010-08-08 00:15:17 +020010150 spin_unlock(&phba->hbalock);
James Smarta257bf92009-04-06 18:48:10 -040010151 return IRQ_NONE;
10152 }
10153
James Smart93996272008-08-24 21:50:30 -040010154 /* Clear attention sources except link and error attentions */
James Smart9940b972011-03-11 16:06:12 -050010155 if (lpfc_readl(phba->HCregaddr, &hc_copy)) {
10156 spin_unlock(&phba->hbalock);
10157 return IRQ_HANDLED;
10158 }
James Smarta747c9c2009-11-18 15:41:10 -050010159 writel(hc_copy & ~(HC_MBINT_ENA | HC_R0INT_ENA | HC_R1INT_ENA
10160 | HC_R2INT_ENA | HC_LAINT_ENA | HC_ERINT_ENA),
10161 phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -040010162 writel((phba->ha_copy & ~(HA_LATT | HA_ERATT)), phba->HAregaddr);
James Smarta747c9c2009-11-18 15:41:10 -050010163 writel(hc_copy, phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -040010164 readl(phba->HAregaddr); /* flush */
10165 spin_unlock(&phba->hbalock);
10166
10167 /*
10168 * Invokes slow-path host attention interrupt handling as appropriate.
10169 */
10170
10171 /* status of events with mailbox and link attention */
10172 status1 = phba->ha_copy & (HA_MBATT | HA_LATT | HA_ERATT);
10173
10174 /* status of events with ELS ring */
10175 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_ELS_RING)));
10176 status2 >>= (4*LPFC_ELS_RING);
10177
10178 if (status1 || (status2 & HA_RXMASK))
James Smart3772a992009-05-22 14:50:54 -040010179 sp_irq_rc = lpfc_sli_sp_intr_handler(irq, dev_id);
James Smart93996272008-08-24 21:50:30 -040010180 else
10181 sp_irq_rc = IRQ_NONE;
10182
10183 /*
10184 * Invoke fast-path host attention interrupt handling as appropriate.
10185 */
10186
10187 /* status of events with FCP ring */
10188 status1 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
10189 status1 >>= (4*LPFC_FCP_RING);
10190
10191 /* status of events with extra ring */
10192 if (phba->cfg_multi_ring_support == 2) {
10193 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
10194 status2 >>= (4*LPFC_EXTRA_RING);
10195 } else
10196 status2 = 0;
10197
10198 if ((status1 & HA_RXMASK) || (status2 & HA_RXMASK))
James Smart3772a992009-05-22 14:50:54 -040010199 fp_irq_rc = lpfc_sli_fp_intr_handler(irq, dev_id);
James Smart93996272008-08-24 21:50:30 -040010200 else
10201 fp_irq_rc = IRQ_NONE;
10202
10203 /* Return device-level interrupt handling status */
10204 return (sp_irq_rc == IRQ_HANDLED) ? sp_irq_rc : fp_irq_rc;
James Smart3772a992009-05-22 14:50:54 -040010205} /* lpfc_sli_intr_handler */
James Smart4f774512009-05-22 14:52:35 -040010206
10207/**
10208 * lpfc_sli4_fcp_xri_abort_event_proc - Process fcp xri abort event
10209 * @phba: pointer to lpfc hba data structure.
10210 *
10211 * This routine is invoked by the worker thread to process all the pending
10212 * SLI4 FCP abort XRI events.
10213 **/
10214void lpfc_sli4_fcp_xri_abort_event_proc(struct lpfc_hba *phba)
10215{
10216 struct lpfc_cq_event *cq_event;
10217
10218 /* First, declare the fcp xri abort event has been handled */
10219 spin_lock_irq(&phba->hbalock);
10220 phba->hba_flag &= ~FCP_XRI_ABORT_EVENT;
10221 spin_unlock_irq(&phba->hbalock);
10222 /* Now, handle all the fcp xri abort events */
10223 while (!list_empty(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue)) {
10224 /* Get the first event from the head of the event queue */
10225 spin_lock_irq(&phba->hbalock);
10226 list_remove_head(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue,
10227 cq_event, struct lpfc_cq_event, list);
10228 spin_unlock_irq(&phba->hbalock);
10229 /* Notify aborted XRI for FCP work queue */
10230 lpfc_sli4_fcp_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
10231 /* Free the event processed back to the free pool */
10232 lpfc_sli4_cq_event_release(phba, cq_event);
10233 }
10234}
10235
10236/**
10237 * lpfc_sli4_els_xri_abort_event_proc - Process els xri abort event
10238 * @phba: pointer to lpfc hba data structure.
10239 *
10240 * This routine is invoked by the worker thread to process all the pending
10241 * SLI4 els abort xri events.
10242 **/
10243void lpfc_sli4_els_xri_abort_event_proc(struct lpfc_hba *phba)
10244{
10245 struct lpfc_cq_event *cq_event;
10246
10247 /* First, declare the els xri abort event has been handled */
10248 spin_lock_irq(&phba->hbalock);
10249 phba->hba_flag &= ~ELS_XRI_ABORT_EVENT;
10250 spin_unlock_irq(&phba->hbalock);
10251 /* Now, handle all the els xri abort events */
10252 while (!list_empty(&phba->sli4_hba.sp_els_xri_aborted_work_queue)) {
10253 /* Get the first event from the head of the event queue */
10254 spin_lock_irq(&phba->hbalock);
10255 list_remove_head(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
10256 cq_event, struct lpfc_cq_event, list);
10257 spin_unlock_irq(&phba->hbalock);
10258 /* Notify aborted XRI for ELS work queue */
10259 lpfc_sli4_els_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
10260 /* Free the event processed back to the free pool */
10261 lpfc_sli4_cq_event_release(phba, cq_event);
10262 }
10263}
10264
James Smart341af102010-01-26 23:07:37 -050010265/**
10266 * lpfc_sli4_iocb_param_transfer - Transfer pIocbOut and cmpl status to pIocbIn
10267 * @phba: pointer to lpfc hba data structure
10268 * @pIocbIn: pointer to the rspiocbq
10269 * @pIocbOut: pointer to the cmdiocbq
10270 * @wcqe: pointer to the complete wcqe
10271 *
10272 * This routine transfers the fields of a command iocbq to a response iocbq
10273 * by copying all the IOCB fields from command iocbq and transferring the
10274 * completion status information from the complete wcqe.
10275 **/
James Smart4f774512009-05-22 14:52:35 -040010276static void
James Smart341af102010-01-26 23:07:37 -050010277lpfc_sli4_iocb_param_transfer(struct lpfc_hba *phba,
10278 struct lpfc_iocbq *pIocbIn,
James Smart4f774512009-05-22 14:52:35 -040010279 struct lpfc_iocbq *pIocbOut,
10280 struct lpfc_wcqe_complete *wcqe)
10281{
James Smart341af102010-01-26 23:07:37 -050010282 unsigned long iflags;
James Smart4f774512009-05-22 14:52:35 -040010283 size_t offset = offsetof(struct lpfc_iocbq, iocb);
10284
10285 memcpy((char *)pIocbIn + offset, (char *)pIocbOut + offset,
10286 sizeof(struct lpfc_iocbq) - offset);
James Smart4f774512009-05-22 14:52:35 -040010287 /* Map WCQE parameters into irspiocb parameters */
10288 pIocbIn->iocb.ulpStatus = bf_get(lpfc_wcqe_c_status, wcqe);
10289 if (pIocbOut->iocb_flag & LPFC_IO_FCP)
10290 if (pIocbIn->iocb.ulpStatus == IOSTAT_FCP_RSP_ERROR)
10291 pIocbIn->iocb.un.fcpi.fcpi_parm =
10292 pIocbOut->iocb.un.fcpi.fcpi_parm -
10293 wcqe->total_data_placed;
10294 else
10295 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
James Smart695a8142010-01-26 23:08:03 -050010296 else {
James Smart4f774512009-05-22 14:52:35 -040010297 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
James Smart695a8142010-01-26 23:08:03 -050010298 pIocbIn->iocb.un.genreq64.bdl.bdeSize = wcqe->total_data_placed;
10299 }
James Smart341af102010-01-26 23:07:37 -050010300
10301 /* Pick up HBA exchange busy condition */
10302 if (bf_get(lpfc_wcqe_c_xb, wcqe)) {
10303 spin_lock_irqsave(&phba->hbalock, iflags);
10304 pIocbIn->iocb_flag |= LPFC_EXCHANGE_BUSY;
10305 spin_unlock_irqrestore(&phba->hbalock, iflags);
10306 }
James Smart4f774512009-05-22 14:52:35 -040010307}
10308
10309/**
James Smart45ed1192009-10-02 15:17:02 -040010310 * lpfc_sli4_els_wcqe_to_rspiocbq - Get response iocbq from els wcqe
10311 * @phba: Pointer to HBA context object.
10312 * @wcqe: Pointer to work-queue completion queue entry.
10313 *
10314 * This routine handles an ELS work-queue completion event and construct
10315 * a pseudo response ELS IODBQ from the SLI4 ELS WCQE for the common
10316 * discovery engine to handle.
10317 *
10318 * Return: Pointer to the receive IOCBQ, NULL otherwise.
10319 **/
10320static struct lpfc_iocbq *
10321lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *phba,
10322 struct lpfc_iocbq *irspiocbq)
10323{
10324 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
10325 struct lpfc_iocbq *cmdiocbq;
10326 struct lpfc_wcqe_complete *wcqe;
10327 unsigned long iflags;
10328
10329 wcqe = &irspiocbq->cq_event.cqe.wcqe_cmpl;
10330 spin_lock_irqsave(&phba->hbalock, iflags);
10331 pring->stats.iocb_event++;
10332 /* Look up the ELS command IOCB and create pseudo response IOCB */
10333 cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
10334 bf_get(lpfc_wcqe_c_request_tag, wcqe));
10335 spin_unlock_irqrestore(&phba->hbalock, iflags);
10336
10337 if (unlikely(!cmdiocbq)) {
10338 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
10339 "0386 ELS complete with no corresponding "
10340 "cmdiocb: iotag (%d)\n",
10341 bf_get(lpfc_wcqe_c_request_tag, wcqe));
10342 lpfc_sli_release_iocbq(phba, irspiocbq);
10343 return NULL;
10344 }
10345
10346 /* Fake the irspiocbq and copy necessary response information */
James Smart341af102010-01-26 23:07:37 -050010347 lpfc_sli4_iocb_param_transfer(phba, irspiocbq, cmdiocbq, wcqe);
James Smart45ed1192009-10-02 15:17:02 -040010348
10349 return irspiocbq;
10350}
10351
10352/**
James Smart04c68492009-05-22 14:52:52 -040010353 * lpfc_sli4_sp_handle_async_event - Handle an asynchroous event
10354 * @phba: Pointer to HBA context object.
10355 * @cqe: Pointer to mailbox completion queue entry.
10356 *
10357 * This routine process a mailbox completion queue entry with asynchrous
10358 * event.
10359 *
10360 * Return: true if work posted to worker thread, otherwise false.
10361 **/
10362static bool
10363lpfc_sli4_sp_handle_async_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
10364{
10365 struct lpfc_cq_event *cq_event;
10366 unsigned long iflags;
10367
10368 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
10369 "0392 Async Event: word0:x%x, word1:x%x, "
10370 "word2:x%x, word3:x%x\n", mcqe->word0,
10371 mcqe->mcqe_tag0, mcqe->mcqe_tag1, mcqe->trailer);
10372
10373 /* Allocate a new internal CQ_EVENT entry */
10374 cq_event = lpfc_sli4_cq_event_alloc(phba);
10375 if (!cq_event) {
10376 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10377 "0394 Failed to allocate CQ_EVENT entry\n");
10378 return false;
10379 }
10380
10381 /* Move the CQE into an asynchronous event entry */
10382 memcpy(&cq_event->cqe, mcqe, sizeof(struct lpfc_mcqe));
10383 spin_lock_irqsave(&phba->hbalock, iflags);
10384 list_add_tail(&cq_event->list, &phba->sli4_hba.sp_asynce_work_queue);
10385 /* Set the async event flag */
10386 phba->hba_flag |= ASYNC_EVENT;
10387 spin_unlock_irqrestore(&phba->hbalock, iflags);
10388
10389 return true;
10390}
10391
10392/**
10393 * lpfc_sli4_sp_handle_mbox_event - Handle a mailbox completion event
10394 * @phba: Pointer to HBA context object.
10395 * @cqe: Pointer to mailbox completion queue entry.
10396 *
10397 * This routine process a mailbox completion queue entry with mailbox
10398 * completion event.
10399 *
10400 * Return: true if work posted to worker thread, otherwise false.
10401 **/
10402static bool
10403lpfc_sli4_sp_handle_mbox_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
10404{
10405 uint32_t mcqe_status;
10406 MAILBOX_t *mbox, *pmbox;
10407 struct lpfc_mqe *mqe;
10408 struct lpfc_vport *vport;
10409 struct lpfc_nodelist *ndlp;
10410 struct lpfc_dmabuf *mp;
10411 unsigned long iflags;
10412 LPFC_MBOXQ_t *pmb;
10413 bool workposted = false;
10414 int rc;
10415
10416 /* If not a mailbox complete MCQE, out by checking mailbox consume */
10417 if (!bf_get(lpfc_trailer_completed, mcqe))
10418 goto out_no_mqe_complete;
10419
10420 /* Get the reference to the active mbox command */
10421 spin_lock_irqsave(&phba->hbalock, iflags);
10422 pmb = phba->sli.mbox_active;
10423 if (unlikely(!pmb)) {
10424 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
10425 "1832 No pending MBOX command to handle\n");
10426 spin_unlock_irqrestore(&phba->hbalock, iflags);
10427 goto out_no_mqe_complete;
10428 }
10429 spin_unlock_irqrestore(&phba->hbalock, iflags);
10430 mqe = &pmb->u.mqe;
10431 pmbox = (MAILBOX_t *)&pmb->u.mqe;
10432 mbox = phba->mbox;
10433 vport = pmb->vport;
10434
10435 /* Reset heartbeat timer */
10436 phba->last_completion_time = jiffies;
10437 del_timer(&phba->sli.mbox_tmo);
10438
10439 /* Move mbox data to caller's mailbox region, do endian swapping */
10440 if (pmb->mbox_cmpl && mbox)
10441 lpfc_sli_pcimem_bcopy(mbox, mqe, sizeof(struct lpfc_mqe));
James Smart04c68492009-05-22 14:52:52 -040010442
James Smart73d91e52011-10-10 21:32:10 -040010443 /*
10444 * For mcqe errors, conditionally move a modified error code to
10445 * the mbox so that the error will not be missed.
10446 */
10447 mcqe_status = bf_get(lpfc_mcqe_status, mcqe);
10448 if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
10449 if (bf_get(lpfc_mqe_status, mqe) == MBX_SUCCESS)
10450 bf_set(lpfc_mqe_status, mqe,
10451 (LPFC_MBX_ERROR_RANGE | mcqe_status));
10452 }
James Smart04c68492009-05-22 14:52:52 -040010453 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
10454 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
10455 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_MBOX_VPORT,
10456 "MBOX dflt rpi: status:x%x rpi:x%x",
10457 mcqe_status,
10458 pmbox->un.varWords[0], 0);
10459 if (mcqe_status == MB_CQE_STATUS_SUCCESS) {
10460 mp = (struct lpfc_dmabuf *)(pmb->context1);
10461 ndlp = (struct lpfc_nodelist *)pmb->context2;
10462 /* Reg_LOGIN of dflt RPI was successful. Now lets get
10463 * RID of the PPI using the same mbox buffer.
10464 */
10465 lpfc_unreg_login(phba, vport->vpi,
10466 pmbox->un.varWords[0], pmb);
10467 pmb->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
10468 pmb->context1 = mp;
10469 pmb->context2 = ndlp;
10470 pmb->vport = vport;
10471 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
10472 if (rc != MBX_BUSY)
10473 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
10474 LOG_SLI, "0385 rc should "
10475 "have been MBX_BUSY\n");
10476 if (rc != MBX_NOT_FINISHED)
10477 goto send_current_mbox;
10478 }
10479 }
10480 spin_lock_irqsave(&phba->pport->work_port_lock, iflags);
10481 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
10482 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflags);
10483
10484 /* There is mailbox completion work to do */
10485 spin_lock_irqsave(&phba->hbalock, iflags);
10486 __lpfc_mbox_cmpl_put(phba, pmb);
10487 phba->work_ha |= HA_MBATT;
10488 spin_unlock_irqrestore(&phba->hbalock, iflags);
10489 workposted = true;
10490
10491send_current_mbox:
10492 spin_lock_irqsave(&phba->hbalock, iflags);
10493 /* Release the mailbox command posting token */
10494 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
10495 /* Setting active mailbox pointer need to be in sync to flag clear */
10496 phba->sli.mbox_active = NULL;
10497 spin_unlock_irqrestore(&phba->hbalock, iflags);
10498 /* Wake up worker thread to post the next pending mailbox command */
10499 lpfc_worker_wake_up(phba);
10500out_no_mqe_complete:
10501 if (bf_get(lpfc_trailer_consumed, mcqe))
10502 lpfc_sli4_mq_release(phba->sli4_hba.mbx_wq);
10503 return workposted;
10504}
10505
10506/**
10507 * lpfc_sli4_sp_handle_mcqe - Process a mailbox completion queue entry
10508 * @phba: Pointer to HBA context object.
10509 * @cqe: Pointer to mailbox completion queue entry.
10510 *
10511 * This routine process a mailbox completion queue entry, it invokes the
10512 * proper mailbox complete handling or asynchrous event handling routine
10513 * according to the MCQE's async bit.
10514 *
10515 * Return: true if work posted to worker thread, otherwise false.
10516 **/
10517static bool
10518lpfc_sli4_sp_handle_mcqe(struct lpfc_hba *phba, struct lpfc_cqe *cqe)
10519{
10520 struct lpfc_mcqe mcqe;
10521 bool workposted;
10522
10523 /* Copy the mailbox MCQE and convert endian order as needed */
10524 lpfc_sli_pcimem_bcopy(cqe, &mcqe, sizeof(struct lpfc_mcqe));
10525
10526 /* Invoke the proper event handling routine */
10527 if (!bf_get(lpfc_trailer_async, &mcqe))
10528 workposted = lpfc_sli4_sp_handle_mbox_event(phba, &mcqe);
10529 else
10530 workposted = lpfc_sli4_sp_handle_async_event(phba, &mcqe);
10531 return workposted;
10532}
10533
10534/**
James Smart4f774512009-05-22 14:52:35 -040010535 * lpfc_sli4_sp_handle_els_wcqe - Handle els work-queue completion event
10536 * @phba: Pointer to HBA context object.
10537 * @wcqe: Pointer to work-queue completion queue entry.
10538 *
10539 * This routine handles an ELS work-queue completion event.
10540 *
10541 * Return: true if work posted to worker thread, otherwise false.
10542 **/
10543static bool
10544lpfc_sli4_sp_handle_els_wcqe(struct lpfc_hba *phba,
10545 struct lpfc_wcqe_complete *wcqe)
10546{
James Smart4f774512009-05-22 14:52:35 -040010547 struct lpfc_iocbq *irspiocbq;
10548 unsigned long iflags;
James Smart2a9bf3d2010-06-07 15:24:45 -040010549 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_FCP_RING];
James Smart4f774512009-05-22 14:52:35 -040010550
James Smart45ed1192009-10-02 15:17:02 -040010551 /* Get an irspiocbq for later ELS response processing use */
James Smart4f774512009-05-22 14:52:35 -040010552 irspiocbq = lpfc_sli_get_iocbq(phba);
10553 if (!irspiocbq) {
10554 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart2a9bf3d2010-06-07 15:24:45 -040010555 "0387 NO IOCBQ data: txq_cnt=%d iocb_cnt=%d "
10556 "fcp_txcmplq_cnt=%d, els_txcmplq_cnt=%d\n",
10557 pring->txq_cnt, phba->iocb_cnt,
10558 phba->sli.ring[LPFC_FCP_RING].txcmplq_cnt,
10559 phba->sli.ring[LPFC_ELS_RING].txcmplq_cnt);
James Smart45ed1192009-10-02 15:17:02 -040010560 return false;
James Smart4f774512009-05-22 14:52:35 -040010561 }
James Smart4f774512009-05-22 14:52:35 -040010562
James Smart45ed1192009-10-02 15:17:02 -040010563 /* Save off the slow-path queue event for work thread to process */
10564 memcpy(&irspiocbq->cq_event.cqe.wcqe_cmpl, wcqe, sizeof(*wcqe));
James Smart4f774512009-05-22 14:52:35 -040010565 spin_lock_irqsave(&phba->hbalock, iflags);
James Smart4d9ab992009-10-02 15:16:39 -040010566 list_add_tail(&irspiocbq->cq_event.list,
James Smart45ed1192009-10-02 15:17:02 -040010567 &phba->sli4_hba.sp_queue_event);
10568 phba->hba_flag |= HBA_SP_QUEUE_EVT;
James Smart4f774512009-05-22 14:52:35 -040010569 spin_unlock_irqrestore(&phba->hbalock, iflags);
James Smart4f774512009-05-22 14:52:35 -040010570
James Smart45ed1192009-10-02 15:17:02 -040010571 return true;
James Smart4f774512009-05-22 14:52:35 -040010572}
10573
10574/**
10575 * lpfc_sli4_sp_handle_rel_wcqe - Handle slow-path WQ entry consumed event
10576 * @phba: Pointer to HBA context object.
10577 * @wcqe: Pointer to work-queue completion queue entry.
10578 *
10579 * This routine handles slow-path WQ entry comsumed event by invoking the
10580 * proper WQ release routine to the slow-path WQ.
10581 **/
10582static void
10583lpfc_sli4_sp_handle_rel_wcqe(struct lpfc_hba *phba,
10584 struct lpfc_wcqe_release *wcqe)
10585{
10586 /* Check for the slow-path ELS work queue */
10587 if (bf_get(lpfc_wcqe_r_wq_id, wcqe) == phba->sli4_hba.els_wq->queue_id)
10588 lpfc_sli4_wq_release(phba->sli4_hba.els_wq,
10589 bf_get(lpfc_wcqe_r_wqe_index, wcqe));
10590 else
10591 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
10592 "2579 Slow-path wqe consume event carries "
10593 "miss-matched qid: wcqe-qid=x%x, sp-qid=x%x\n",
10594 bf_get(lpfc_wcqe_r_wqe_index, wcqe),
10595 phba->sli4_hba.els_wq->queue_id);
10596}
10597
10598/**
10599 * lpfc_sli4_sp_handle_abort_xri_wcqe - Handle a xri abort event
10600 * @phba: Pointer to HBA context object.
10601 * @cq: Pointer to a WQ completion queue.
10602 * @wcqe: Pointer to work-queue completion queue entry.
10603 *
10604 * This routine handles an XRI abort event.
10605 *
10606 * Return: true if work posted to worker thread, otherwise false.
10607 **/
10608static bool
10609lpfc_sli4_sp_handle_abort_xri_wcqe(struct lpfc_hba *phba,
10610 struct lpfc_queue *cq,
10611 struct sli4_wcqe_xri_aborted *wcqe)
10612{
10613 bool workposted = false;
10614 struct lpfc_cq_event *cq_event;
10615 unsigned long iflags;
10616
10617 /* Allocate a new internal CQ_EVENT entry */
10618 cq_event = lpfc_sli4_cq_event_alloc(phba);
10619 if (!cq_event) {
10620 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10621 "0602 Failed to allocate CQ_EVENT entry\n");
10622 return false;
10623 }
10624
10625 /* Move the CQE into the proper xri abort event list */
10626 memcpy(&cq_event->cqe, wcqe, sizeof(struct sli4_wcqe_xri_aborted));
10627 switch (cq->subtype) {
10628 case LPFC_FCP:
10629 spin_lock_irqsave(&phba->hbalock, iflags);
10630 list_add_tail(&cq_event->list,
10631 &phba->sli4_hba.sp_fcp_xri_aborted_work_queue);
10632 /* Set the fcp xri abort event flag */
10633 phba->hba_flag |= FCP_XRI_ABORT_EVENT;
10634 spin_unlock_irqrestore(&phba->hbalock, iflags);
10635 workposted = true;
10636 break;
10637 case LPFC_ELS:
10638 spin_lock_irqsave(&phba->hbalock, iflags);
10639 list_add_tail(&cq_event->list,
10640 &phba->sli4_hba.sp_els_xri_aborted_work_queue);
10641 /* Set the els xri abort event flag */
10642 phba->hba_flag |= ELS_XRI_ABORT_EVENT;
10643 spin_unlock_irqrestore(&phba->hbalock, iflags);
10644 workposted = true;
10645 break;
10646 default:
10647 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10648 "0603 Invalid work queue CQE subtype (x%x)\n",
10649 cq->subtype);
10650 workposted = false;
10651 break;
10652 }
10653 return workposted;
10654}
10655
10656/**
James Smart4d9ab992009-10-02 15:16:39 -040010657 * lpfc_sli4_sp_handle_rcqe - Process a receive-queue completion queue entry
James Smart4f774512009-05-22 14:52:35 -040010658 * @phba: Pointer to HBA context object.
James Smart4d9ab992009-10-02 15:16:39 -040010659 * @rcqe: Pointer to receive-queue completion queue entry.
James Smart4f774512009-05-22 14:52:35 -040010660 *
James Smart4d9ab992009-10-02 15:16:39 -040010661 * This routine process a receive-queue completion queue entry.
James Smart4f774512009-05-22 14:52:35 -040010662 *
10663 * Return: true if work posted to worker thread, otherwise false.
10664 **/
10665static bool
James Smart4d9ab992009-10-02 15:16:39 -040010666lpfc_sli4_sp_handle_rcqe(struct lpfc_hba *phba, struct lpfc_rcqe *rcqe)
10667{
10668 bool workposted = false;
10669 struct lpfc_queue *hrq = phba->sli4_hba.hdr_rq;
10670 struct lpfc_queue *drq = phba->sli4_hba.dat_rq;
10671 struct hbq_dmabuf *dma_buf;
James Smart7851fe22011-07-22 18:36:52 -040010672 uint32_t status, rq_id;
James Smart4d9ab992009-10-02 15:16:39 -040010673 unsigned long iflags;
10674
James Smart7851fe22011-07-22 18:36:52 -040010675 if (bf_get(lpfc_cqe_code, rcqe) == CQE_CODE_RECEIVE_V1)
10676 rq_id = bf_get(lpfc_rcqe_rq_id_v1, rcqe);
10677 else
10678 rq_id = bf_get(lpfc_rcqe_rq_id, rcqe);
10679 if (rq_id != hrq->queue_id)
James Smart4d9ab992009-10-02 15:16:39 -040010680 goto out;
10681
10682 status = bf_get(lpfc_rcqe_status, rcqe);
10683 switch (status) {
10684 case FC_STATUS_RQ_BUF_LEN_EXCEEDED:
10685 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10686 "2537 Receive Frame Truncated!!\n");
10687 case FC_STATUS_RQ_SUCCESS:
James Smart5ffc2662009-11-18 15:39:44 -050010688 lpfc_sli4_rq_release(hrq, drq);
James Smart4d9ab992009-10-02 15:16:39 -040010689 spin_lock_irqsave(&phba->hbalock, iflags);
10690 dma_buf = lpfc_sli_hbqbuf_get(&phba->hbqs[0].hbq_buffer_list);
10691 if (!dma_buf) {
10692 spin_unlock_irqrestore(&phba->hbalock, iflags);
10693 goto out;
10694 }
10695 memcpy(&dma_buf->cq_event.cqe.rcqe_cmpl, rcqe, sizeof(*rcqe));
10696 /* save off the frame for the word thread to process */
10697 list_add_tail(&dma_buf->cq_event.list,
James Smart45ed1192009-10-02 15:17:02 -040010698 &phba->sli4_hba.sp_queue_event);
James Smart4d9ab992009-10-02 15:16:39 -040010699 /* Frame received */
James Smart45ed1192009-10-02 15:17:02 -040010700 phba->hba_flag |= HBA_SP_QUEUE_EVT;
James Smart4d9ab992009-10-02 15:16:39 -040010701 spin_unlock_irqrestore(&phba->hbalock, iflags);
10702 workposted = true;
10703 break;
10704 case FC_STATUS_INSUFF_BUF_NEED_BUF:
10705 case FC_STATUS_INSUFF_BUF_FRM_DISC:
10706 /* Post more buffers if possible */
10707 spin_lock_irqsave(&phba->hbalock, iflags);
10708 phba->hba_flag |= HBA_POST_RECEIVE_BUFFER;
10709 spin_unlock_irqrestore(&phba->hbalock, iflags);
10710 workposted = true;
10711 break;
10712 }
10713out:
10714 return workposted;
James Smart4d9ab992009-10-02 15:16:39 -040010715}
10716
10717/**
10718 * lpfc_sli4_sp_handle_cqe - Process a slow path completion queue entry
10719 * @phba: Pointer to HBA context object.
10720 * @cq: Pointer to the completion queue.
10721 * @wcqe: Pointer to a completion queue entry.
10722 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030010723 * This routine process a slow-path work-queue or receive queue completion queue
James Smart4d9ab992009-10-02 15:16:39 -040010724 * entry.
10725 *
10726 * Return: true if work posted to worker thread, otherwise false.
10727 **/
10728static bool
10729lpfc_sli4_sp_handle_cqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
James Smart4f774512009-05-22 14:52:35 -040010730 struct lpfc_cqe *cqe)
10731{
James Smart45ed1192009-10-02 15:17:02 -040010732 struct lpfc_cqe cqevt;
James Smart4f774512009-05-22 14:52:35 -040010733 bool workposted = false;
10734
10735 /* Copy the work queue CQE and convert endian order if needed */
James Smart45ed1192009-10-02 15:17:02 -040010736 lpfc_sli_pcimem_bcopy(cqe, &cqevt, sizeof(struct lpfc_cqe));
James Smart4f774512009-05-22 14:52:35 -040010737
10738 /* Check and process for different type of WCQE and dispatch */
James Smart45ed1192009-10-02 15:17:02 -040010739 switch (bf_get(lpfc_cqe_code, &cqevt)) {
James Smart4f774512009-05-22 14:52:35 -040010740 case CQE_CODE_COMPL_WQE:
James Smart45ed1192009-10-02 15:17:02 -040010741 /* Process the WQ/RQ complete event */
James Smartbc739052010-08-04 16:11:18 -040010742 phba->last_completion_time = jiffies;
James Smart4f774512009-05-22 14:52:35 -040010743 workposted = lpfc_sli4_sp_handle_els_wcqe(phba,
James Smart45ed1192009-10-02 15:17:02 -040010744 (struct lpfc_wcqe_complete *)&cqevt);
James Smart4f774512009-05-22 14:52:35 -040010745 break;
10746 case CQE_CODE_RELEASE_WQE:
10747 /* Process the WQ release event */
10748 lpfc_sli4_sp_handle_rel_wcqe(phba,
James Smart45ed1192009-10-02 15:17:02 -040010749 (struct lpfc_wcqe_release *)&cqevt);
James Smart4f774512009-05-22 14:52:35 -040010750 break;
10751 case CQE_CODE_XRI_ABORTED:
10752 /* Process the WQ XRI abort event */
James Smartbc739052010-08-04 16:11:18 -040010753 phba->last_completion_time = jiffies;
James Smart4f774512009-05-22 14:52:35 -040010754 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
James Smart45ed1192009-10-02 15:17:02 -040010755 (struct sli4_wcqe_xri_aborted *)&cqevt);
James Smart4f774512009-05-22 14:52:35 -040010756 break;
James Smart4d9ab992009-10-02 15:16:39 -040010757 case CQE_CODE_RECEIVE:
James Smart7851fe22011-07-22 18:36:52 -040010758 case CQE_CODE_RECEIVE_V1:
James Smart4d9ab992009-10-02 15:16:39 -040010759 /* Process the RQ event */
James Smartbc739052010-08-04 16:11:18 -040010760 phba->last_completion_time = jiffies;
James Smart4d9ab992009-10-02 15:16:39 -040010761 workposted = lpfc_sli4_sp_handle_rcqe(phba,
James Smart45ed1192009-10-02 15:17:02 -040010762 (struct lpfc_rcqe *)&cqevt);
James Smart4d9ab992009-10-02 15:16:39 -040010763 break;
James Smart4f774512009-05-22 14:52:35 -040010764 default:
10765 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10766 "0388 Not a valid WCQE code: x%x\n",
James Smart45ed1192009-10-02 15:17:02 -040010767 bf_get(lpfc_cqe_code, &cqevt));
James Smart4f774512009-05-22 14:52:35 -040010768 break;
10769 }
10770 return workposted;
10771}
10772
10773/**
James Smart4f774512009-05-22 14:52:35 -040010774 * lpfc_sli4_sp_handle_eqe - Process a slow-path event queue entry
10775 * @phba: Pointer to HBA context object.
10776 * @eqe: Pointer to fast-path event queue entry.
10777 *
10778 * This routine process a event queue entry from the slow-path event queue.
10779 * It will check the MajorCode and MinorCode to determine this is for a
10780 * completion event on a completion queue, if not, an error shall be logged
10781 * and just return. Otherwise, it will get to the corresponding completion
10782 * queue and process all the entries on that completion queue, rearm the
10783 * completion queue, and then return.
10784 *
10785 **/
10786static void
10787lpfc_sli4_sp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe)
10788{
10789 struct lpfc_queue *cq = NULL, *childq, *speq;
10790 struct lpfc_cqe *cqe;
10791 bool workposted = false;
10792 int ecount = 0;
10793 uint16_t cqid;
10794
James Smartcb5172e2010-03-15 11:25:07 -040010795 if (bf_get_le32(lpfc_eqe_major_code, eqe) != 0) {
James Smart4f774512009-05-22 14:52:35 -040010796 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10797 "0359 Not a valid slow-path completion "
10798 "event: majorcode=x%x, minorcode=x%x\n",
James Smartcb5172e2010-03-15 11:25:07 -040010799 bf_get_le32(lpfc_eqe_major_code, eqe),
10800 bf_get_le32(lpfc_eqe_minor_code, eqe));
James Smart4f774512009-05-22 14:52:35 -040010801 return;
10802 }
10803
10804 /* Get the reference to the corresponding CQ */
James Smartcb5172e2010-03-15 11:25:07 -040010805 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
James Smart4f774512009-05-22 14:52:35 -040010806
10807 /* Search for completion queue pointer matching this cqid */
10808 speq = phba->sli4_hba.sp_eq;
10809 list_for_each_entry(childq, &speq->child_list, list) {
10810 if (childq->queue_id == cqid) {
10811 cq = childq;
10812 break;
10813 }
10814 }
10815 if (unlikely(!cq)) {
James Smart75baf692010-06-08 18:31:21 -040010816 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
10817 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10818 "0365 Slow-path CQ identifier "
10819 "(%d) does not exist\n", cqid);
James Smart4f774512009-05-22 14:52:35 -040010820 return;
10821 }
10822
10823 /* Process all the entries to the CQ */
10824 switch (cq->type) {
10825 case LPFC_MCQ:
10826 while ((cqe = lpfc_sli4_cq_get(cq))) {
10827 workposted |= lpfc_sli4_sp_handle_mcqe(phba, cqe);
James Smart73d91e52011-10-10 21:32:10 -040010828 if (!(++ecount % cq->entry_repost))
James Smart4f774512009-05-22 14:52:35 -040010829 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
10830 }
10831 break;
10832 case LPFC_WCQ:
10833 while ((cqe = lpfc_sli4_cq_get(cq))) {
James Smart05580562011-05-24 11:40:48 -040010834 if (cq->subtype == LPFC_FCP)
10835 workposted |= lpfc_sli4_fp_handle_wcqe(phba, cq,
10836 cqe);
10837 else
10838 workposted |= lpfc_sli4_sp_handle_cqe(phba, cq,
10839 cqe);
James Smart73d91e52011-10-10 21:32:10 -040010840 if (!(++ecount % cq->entry_repost))
James Smart4f774512009-05-22 14:52:35 -040010841 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
10842 }
10843 break;
10844 default:
10845 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10846 "0370 Invalid completion queue type (%d)\n",
10847 cq->type);
10848 return;
10849 }
10850
10851 /* Catch the no cq entry condition, log an error */
10852 if (unlikely(ecount == 0))
10853 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10854 "0371 No entry from the CQ: identifier "
10855 "(x%x), type (%d)\n", cq->queue_id, cq->type);
10856
10857 /* In any case, flash and re-arm the RCQ */
10858 lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
10859
10860 /* wake up worker thread if there are works to be done */
10861 if (workposted)
10862 lpfc_worker_wake_up(phba);
10863}
10864
10865/**
10866 * lpfc_sli4_fp_handle_fcp_wcqe - Process fast-path work queue completion entry
10867 * @eqe: Pointer to fast-path completion queue entry.
10868 *
10869 * This routine process a fast-path work queue completion entry from fast-path
10870 * event queue for FCP command response completion.
10871 **/
10872static void
10873lpfc_sli4_fp_handle_fcp_wcqe(struct lpfc_hba *phba,
10874 struct lpfc_wcqe_complete *wcqe)
10875{
10876 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_FCP_RING];
10877 struct lpfc_iocbq *cmdiocbq;
10878 struct lpfc_iocbq irspiocbq;
10879 unsigned long iflags;
10880
10881 spin_lock_irqsave(&phba->hbalock, iflags);
10882 pring->stats.iocb_event++;
10883 spin_unlock_irqrestore(&phba->hbalock, iflags);
10884
10885 /* Check for response status */
10886 if (unlikely(bf_get(lpfc_wcqe_c_status, wcqe))) {
10887 /* If resource errors reported from HBA, reduce queue
10888 * depth of the SCSI device.
10889 */
10890 if ((bf_get(lpfc_wcqe_c_status, wcqe) ==
10891 IOSTAT_LOCAL_REJECT) &&
10892 (wcqe->parameter == IOERR_NO_RESOURCES)) {
10893 phba->lpfc_rampdown_queue_depth(phba);
10894 }
10895 /* Log the error status */
10896 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
10897 "0373 FCP complete error: status=x%x, "
10898 "hw_status=x%x, total_data_specified=%d, "
10899 "parameter=x%x, word3=x%x\n",
10900 bf_get(lpfc_wcqe_c_status, wcqe),
10901 bf_get(lpfc_wcqe_c_hw_status, wcqe),
10902 wcqe->total_data_placed, wcqe->parameter,
10903 wcqe->word3);
10904 }
10905
10906 /* Look up the FCP command IOCB and create pseudo response IOCB */
10907 spin_lock_irqsave(&phba->hbalock, iflags);
10908 cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
10909 bf_get(lpfc_wcqe_c_request_tag, wcqe));
10910 spin_unlock_irqrestore(&phba->hbalock, iflags);
10911 if (unlikely(!cmdiocbq)) {
10912 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
10913 "0374 FCP complete with no corresponding "
10914 "cmdiocb: iotag (%d)\n",
10915 bf_get(lpfc_wcqe_c_request_tag, wcqe));
10916 return;
10917 }
10918 if (unlikely(!cmdiocbq->iocb_cmpl)) {
10919 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
10920 "0375 FCP cmdiocb not callback function "
10921 "iotag: (%d)\n",
10922 bf_get(lpfc_wcqe_c_request_tag, wcqe));
10923 return;
10924 }
10925
10926 /* Fake the irspiocb and copy necessary response information */
James Smart341af102010-01-26 23:07:37 -050010927 lpfc_sli4_iocb_param_transfer(phba, &irspiocbq, cmdiocbq, wcqe);
James Smart4f774512009-05-22 14:52:35 -040010928
James Smart0f65ff62010-02-26 14:14:23 -050010929 if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED) {
10930 spin_lock_irqsave(&phba->hbalock, iflags);
10931 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
10932 spin_unlock_irqrestore(&phba->hbalock, iflags);
10933 }
10934
James Smart4f774512009-05-22 14:52:35 -040010935 /* Pass the cmd_iocb and the rsp state to the upper layer */
10936 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq, &irspiocbq);
10937}
10938
10939/**
10940 * lpfc_sli4_fp_handle_rel_wcqe - Handle fast-path WQ entry consumed event
10941 * @phba: Pointer to HBA context object.
10942 * @cq: Pointer to completion queue.
10943 * @wcqe: Pointer to work-queue completion queue entry.
10944 *
10945 * This routine handles an fast-path WQ entry comsumed event by invoking the
10946 * proper WQ release routine to the slow-path WQ.
10947 **/
10948static void
10949lpfc_sli4_fp_handle_rel_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
10950 struct lpfc_wcqe_release *wcqe)
10951{
10952 struct lpfc_queue *childwq;
10953 bool wqid_matched = false;
10954 uint16_t fcp_wqid;
10955
10956 /* Check for fast-path FCP work queue release */
10957 fcp_wqid = bf_get(lpfc_wcqe_r_wq_id, wcqe);
10958 list_for_each_entry(childwq, &cq->child_list, list) {
10959 if (childwq->queue_id == fcp_wqid) {
10960 lpfc_sli4_wq_release(childwq,
10961 bf_get(lpfc_wcqe_r_wqe_index, wcqe));
10962 wqid_matched = true;
10963 break;
10964 }
10965 }
10966 /* Report warning log message if no match found */
10967 if (wqid_matched != true)
10968 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
10969 "2580 Fast-path wqe consume event carries "
10970 "miss-matched qid: wcqe-qid=x%x\n", fcp_wqid);
10971}
10972
10973/**
10974 * lpfc_sli4_fp_handle_wcqe - Process fast-path work queue completion entry
10975 * @cq: Pointer to the completion queue.
10976 * @eqe: Pointer to fast-path completion queue entry.
10977 *
10978 * This routine process a fast-path work queue completion entry from fast-path
10979 * event queue for FCP command response completion.
10980 **/
10981static int
10982lpfc_sli4_fp_handle_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
10983 struct lpfc_cqe *cqe)
10984{
10985 struct lpfc_wcqe_release wcqe;
10986 bool workposted = false;
10987
10988 /* Copy the work queue CQE and convert endian order if needed */
10989 lpfc_sli_pcimem_bcopy(cqe, &wcqe, sizeof(struct lpfc_cqe));
10990
10991 /* Check and process for different type of WCQE and dispatch */
10992 switch (bf_get(lpfc_wcqe_c_code, &wcqe)) {
10993 case CQE_CODE_COMPL_WQE:
10994 /* Process the WQ complete event */
James Smart98fc5dd2010-06-07 15:24:29 -040010995 phba->last_completion_time = jiffies;
James Smart4f774512009-05-22 14:52:35 -040010996 lpfc_sli4_fp_handle_fcp_wcqe(phba,
10997 (struct lpfc_wcqe_complete *)&wcqe);
10998 break;
10999 case CQE_CODE_RELEASE_WQE:
11000 /* Process the WQ release event */
11001 lpfc_sli4_fp_handle_rel_wcqe(phba, cq,
11002 (struct lpfc_wcqe_release *)&wcqe);
11003 break;
11004 case CQE_CODE_XRI_ABORTED:
11005 /* Process the WQ XRI abort event */
James Smartbc739052010-08-04 16:11:18 -040011006 phba->last_completion_time = jiffies;
James Smart4f774512009-05-22 14:52:35 -040011007 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
11008 (struct sli4_wcqe_xri_aborted *)&wcqe);
11009 break;
11010 default:
11011 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11012 "0144 Not a valid WCQE code: x%x\n",
11013 bf_get(lpfc_wcqe_c_code, &wcqe));
11014 break;
11015 }
11016 return workposted;
11017}
11018
11019/**
11020 * lpfc_sli4_fp_handle_eqe - Process a fast-path event queue entry
11021 * @phba: Pointer to HBA context object.
11022 * @eqe: Pointer to fast-path event queue entry.
11023 *
11024 * This routine process a event queue entry from the fast-path event queue.
11025 * It will check the MajorCode and MinorCode to determine this is for a
11026 * completion event on a completion queue, if not, an error shall be logged
11027 * and just return. Otherwise, it will get to the corresponding completion
11028 * queue and process all the entries on the completion queue, rearm the
11029 * completion queue, and then return.
11030 **/
11031static void
11032lpfc_sli4_fp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
11033 uint32_t fcp_cqidx)
11034{
11035 struct lpfc_queue *cq;
11036 struct lpfc_cqe *cqe;
11037 bool workposted = false;
11038 uint16_t cqid;
11039 int ecount = 0;
11040
James Smartcb5172e2010-03-15 11:25:07 -040011041 if (unlikely(bf_get_le32(lpfc_eqe_major_code, eqe) != 0)) {
James Smart4f774512009-05-22 14:52:35 -040011042 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11043 "0366 Not a valid fast-path completion "
11044 "event: majorcode=x%x, minorcode=x%x\n",
James Smartcb5172e2010-03-15 11:25:07 -040011045 bf_get_le32(lpfc_eqe_major_code, eqe),
11046 bf_get_le32(lpfc_eqe_minor_code, eqe));
James Smart4f774512009-05-22 14:52:35 -040011047 return;
11048 }
11049
11050 cq = phba->sli4_hba.fcp_cq[fcp_cqidx];
11051 if (unlikely(!cq)) {
James Smart75baf692010-06-08 18:31:21 -040011052 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
11053 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11054 "0367 Fast-path completion queue "
11055 "does not exist\n");
James Smart4f774512009-05-22 14:52:35 -040011056 return;
11057 }
11058
11059 /* Get the reference to the corresponding CQ */
James Smartcb5172e2010-03-15 11:25:07 -040011060 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
James Smart4f774512009-05-22 14:52:35 -040011061 if (unlikely(cqid != cq->queue_id)) {
11062 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11063 "0368 Miss-matched fast-path completion "
11064 "queue identifier: eqcqid=%d, fcpcqid=%d\n",
11065 cqid, cq->queue_id);
11066 return;
11067 }
11068
11069 /* Process all the entries to the CQ */
11070 while ((cqe = lpfc_sli4_cq_get(cq))) {
11071 workposted |= lpfc_sli4_fp_handle_wcqe(phba, cq, cqe);
James Smart73d91e52011-10-10 21:32:10 -040011072 if (!(++ecount % cq->entry_repost))
James Smart4f774512009-05-22 14:52:35 -040011073 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
11074 }
11075
11076 /* Catch the no cq entry condition */
11077 if (unlikely(ecount == 0))
11078 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11079 "0369 No entry from fast-path completion "
11080 "queue fcpcqid=%d\n", cq->queue_id);
11081
11082 /* In any case, flash and re-arm the CQ */
11083 lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
11084
11085 /* wake up worker thread if there are works to be done */
11086 if (workposted)
11087 lpfc_worker_wake_up(phba);
11088}
11089
11090static void
11091lpfc_sli4_eq_flush(struct lpfc_hba *phba, struct lpfc_queue *eq)
11092{
11093 struct lpfc_eqe *eqe;
11094
11095 /* walk all the EQ entries and drop on the floor */
11096 while ((eqe = lpfc_sli4_eq_get(eq)))
11097 ;
11098
11099 /* Clear and re-arm the EQ */
11100 lpfc_sli4_eq_release(eq, LPFC_QUEUE_REARM);
11101}
11102
11103/**
11104 * lpfc_sli4_sp_intr_handler - Slow-path interrupt handler to SLI-4 device
11105 * @irq: Interrupt number.
11106 * @dev_id: The device context pointer.
11107 *
11108 * This function is directly called from the PCI layer as an interrupt
11109 * service routine when device with SLI-4 interface spec is enabled with
11110 * MSI-X multi-message interrupt mode and there are slow-path events in
11111 * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
11112 * interrupt mode, this function is called as part of the device-level
11113 * interrupt handler. When the PCI slot is in error recovery or the HBA is
11114 * undergoing initialization, the interrupt handler will not process the
11115 * interrupt. The link attention and ELS ring attention events are handled
11116 * by the worker thread. The interrupt handler signals the worker thread
11117 * and returns for these events. This function is called without any lock
11118 * held. It gets the hbalock to access and update SLI data structures.
11119 *
11120 * This function returns IRQ_HANDLED when interrupt is handled else it
11121 * returns IRQ_NONE.
11122 **/
11123irqreturn_t
11124lpfc_sli4_sp_intr_handler(int irq, void *dev_id)
11125{
11126 struct lpfc_hba *phba;
11127 struct lpfc_queue *speq;
11128 struct lpfc_eqe *eqe;
11129 unsigned long iflag;
11130 int ecount = 0;
11131
11132 /*
11133 * Get the driver's phba structure from the dev_id
11134 */
11135 phba = (struct lpfc_hba *)dev_id;
11136
11137 if (unlikely(!phba))
11138 return IRQ_NONE;
11139
11140 /* Get to the EQ struct associated with this vector */
11141 speq = phba->sli4_hba.sp_eq;
11142
11143 /* Check device state for handling interrupt */
11144 if (unlikely(lpfc_intr_state_check(phba))) {
11145 /* Check again for link_state with lock held */
11146 spin_lock_irqsave(&phba->hbalock, iflag);
11147 if (phba->link_state < LPFC_LINK_DOWN)
11148 /* Flush, clear interrupt, and rearm the EQ */
11149 lpfc_sli4_eq_flush(phba, speq);
11150 spin_unlock_irqrestore(&phba->hbalock, iflag);
11151 return IRQ_NONE;
11152 }
11153
11154 /*
11155 * Process all the event on FCP slow-path EQ
11156 */
11157 while ((eqe = lpfc_sli4_eq_get(speq))) {
11158 lpfc_sli4_sp_handle_eqe(phba, eqe);
James Smart73d91e52011-10-10 21:32:10 -040011159 if (!(++ecount % speq->entry_repost))
James Smart4f774512009-05-22 14:52:35 -040011160 lpfc_sli4_eq_release(speq, LPFC_QUEUE_NOARM);
11161 }
11162
11163 /* Always clear and re-arm the slow-path EQ */
11164 lpfc_sli4_eq_release(speq, LPFC_QUEUE_REARM);
11165
11166 /* Catch the no cq entry condition */
11167 if (unlikely(ecount == 0)) {
11168 if (phba->intr_type == MSIX)
11169 /* MSI-X treated interrupt served as no EQ share INT */
11170 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11171 "0357 MSI-X interrupt with no EQE\n");
11172 else
11173 /* Non MSI-X treated on interrupt as EQ share INT */
11174 return IRQ_NONE;
11175 }
11176
11177 return IRQ_HANDLED;
11178} /* lpfc_sli4_sp_intr_handler */
11179
11180/**
11181 * lpfc_sli4_fp_intr_handler - Fast-path interrupt handler to SLI-4 device
11182 * @irq: Interrupt number.
11183 * @dev_id: The device context pointer.
11184 *
11185 * This function is directly called from the PCI layer as an interrupt
11186 * service routine when device with SLI-4 interface spec is enabled with
11187 * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
11188 * ring event in the HBA. However, when the device is enabled with either
11189 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
11190 * device-level interrupt handler. When the PCI slot is in error recovery
11191 * or the HBA is undergoing initialization, the interrupt handler will not
11192 * process the interrupt. The SCSI FCP fast-path ring event are handled in
11193 * the intrrupt context. This function is called without any lock held.
11194 * It gets the hbalock to access and update SLI data structures. Note that,
11195 * the FCP EQ to FCP CQ are one-to-one map such that the FCP EQ index is
11196 * equal to that of FCP CQ index.
11197 *
11198 * This function returns IRQ_HANDLED when interrupt is handled else it
11199 * returns IRQ_NONE.
11200 **/
11201irqreturn_t
11202lpfc_sli4_fp_intr_handler(int irq, void *dev_id)
11203{
11204 struct lpfc_hba *phba;
11205 struct lpfc_fcp_eq_hdl *fcp_eq_hdl;
11206 struct lpfc_queue *fpeq;
11207 struct lpfc_eqe *eqe;
11208 unsigned long iflag;
11209 int ecount = 0;
11210 uint32_t fcp_eqidx;
11211
11212 /* Get the driver's phba structure from the dev_id */
11213 fcp_eq_hdl = (struct lpfc_fcp_eq_hdl *)dev_id;
11214 phba = fcp_eq_hdl->phba;
11215 fcp_eqidx = fcp_eq_hdl->idx;
11216
11217 if (unlikely(!phba))
11218 return IRQ_NONE;
11219
11220 /* Get to the EQ struct associated with this vector */
11221 fpeq = phba->sli4_hba.fp_eq[fcp_eqidx];
11222
11223 /* Check device state for handling interrupt */
11224 if (unlikely(lpfc_intr_state_check(phba))) {
11225 /* Check again for link_state with lock held */
11226 spin_lock_irqsave(&phba->hbalock, iflag);
11227 if (phba->link_state < LPFC_LINK_DOWN)
11228 /* Flush, clear interrupt, and rearm the EQ */
11229 lpfc_sli4_eq_flush(phba, fpeq);
11230 spin_unlock_irqrestore(&phba->hbalock, iflag);
11231 return IRQ_NONE;
11232 }
11233
11234 /*
11235 * Process all the event on FCP fast-path EQ
11236 */
11237 while ((eqe = lpfc_sli4_eq_get(fpeq))) {
11238 lpfc_sli4_fp_handle_eqe(phba, eqe, fcp_eqidx);
James Smart73d91e52011-10-10 21:32:10 -040011239 if (!(++ecount % fpeq->entry_repost))
James Smart4f774512009-05-22 14:52:35 -040011240 lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_NOARM);
11241 }
11242
11243 /* Always clear and re-arm the fast-path EQ */
11244 lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_REARM);
11245
11246 if (unlikely(ecount == 0)) {
11247 if (phba->intr_type == MSIX)
11248 /* MSI-X treated interrupt served as no EQ share INT */
11249 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11250 "0358 MSI-X interrupt with no EQE\n");
11251 else
11252 /* Non MSI-X treated on interrupt as EQ share INT */
11253 return IRQ_NONE;
11254 }
11255
11256 return IRQ_HANDLED;
11257} /* lpfc_sli4_fp_intr_handler */
11258
11259/**
11260 * lpfc_sli4_intr_handler - Device-level interrupt handler for SLI-4 device
11261 * @irq: Interrupt number.
11262 * @dev_id: The device context pointer.
11263 *
11264 * This function is the device-level interrupt handler to device with SLI-4
11265 * interface spec, called from the PCI layer when either MSI or Pin-IRQ
11266 * interrupt mode is enabled and there is an event in the HBA which requires
11267 * driver attention. This function invokes the slow-path interrupt attention
11268 * handling function and fast-path interrupt attention handling function in
11269 * turn to process the relevant HBA attention events. This function is called
11270 * without any lock held. It gets the hbalock to access and update SLI data
11271 * structures.
11272 *
11273 * This function returns IRQ_HANDLED when interrupt is handled, else it
11274 * returns IRQ_NONE.
11275 **/
11276irqreturn_t
11277lpfc_sli4_intr_handler(int irq, void *dev_id)
11278{
11279 struct lpfc_hba *phba;
11280 irqreturn_t sp_irq_rc, fp_irq_rc;
11281 bool fp_handled = false;
11282 uint32_t fcp_eqidx;
11283
11284 /* Get the driver's phba structure from the dev_id */
11285 phba = (struct lpfc_hba *)dev_id;
11286
11287 if (unlikely(!phba))
11288 return IRQ_NONE;
11289
11290 /*
11291 * Invokes slow-path host attention interrupt handling as appropriate.
11292 */
11293 sp_irq_rc = lpfc_sli4_sp_intr_handler(irq, dev_id);
11294
11295 /*
11296 * Invoke fast-path host attention interrupt handling as appropriate.
11297 */
11298 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count; fcp_eqidx++) {
11299 fp_irq_rc = lpfc_sli4_fp_intr_handler(irq,
11300 &phba->sli4_hba.fcp_eq_hdl[fcp_eqidx]);
11301 if (fp_irq_rc == IRQ_HANDLED)
11302 fp_handled |= true;
11303 }
11304
11305 return (fp_handled == true) ? IRQ_HANDLED : sp_irq_rc;
11306} /* lpfc_sli4_intr_handler */
11307
11308/**
11309 * lpfc_sli4_queue_free - free a queue structure and associated memory
11310 * @queue: The queue structure to free.
11311 *
Uwe Kleine-Königb5950762010-11-01 15:38:34 -040011312 * This function frees a queue structure and the DMAable memory used for
James Smart4f774512009-05-22 14:52:35 -040011313 * the host resident queue. This function must be called after destroying the
11314 * queue on the HBA.
11315 **/
11316void
11317lpfc_sli4_queue_free(struct lpfc_queue *queue)
11318{
11319 struct lpfc_dmabuf *dmabuf;
11320
11321 if (!queue)
11322 return;
11323
11324 while (!list_empty(&queue->page_list)) {
11325 list_remove_head(&queue->page_list, dmabuf, struct lpfc_dmabuf,
11326 list);
James Smart49198b32010-04-06 15:04:33 -040011327 dma_free_coherent(&queue->phba->pcidev->dev, SLI4_PAGE_SIZE,
James Smart4f774512009-05-22 14:52:35 -040011328 dmabuf->virt, dmabuf->phys);
11329 kfree(dmabuf);
11330 }
11331 kfree(queue);
11332 return;
11333}
11334
11335/**
11336 * lpfc_sli4_queue_alloc - Allocate and initialize a queue structure
11337 * @phba: The HBA that this queue is being created on.
11338 * @entry_size: The size of each queue entry for this queue.
11339 * @entry count: The number of entries that this queue will handle.
11340 *
11341 * This function allocates a queue structure and the DMAable memory used for
11342 * the host resident queue. This function must be called before creating the
11343 * queue on the HBA.
11344 **/
11345struct lpfc_queue *
11346lpfc_sli4_queue_alloc(struct lpfc_hba *phba, uint32_t entry_size,
11347 uint32_t entry_count)
11348{
11349 struct lpfc_queue *queue;
11350 struct lpfc_dmabuf *dmabuf;
11351 int x, total_qe_count;
11352 void *dma_pointer;
James Smartcb5172e2010-03-15 11:25:07 -040011353 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smart4f774512009-05-22 14:52:35 -040011354
James Smartcb5172e2010-03-15 11:25:07 -040011355 if (!phba->sli4_hba.pc_sli4_params.supported)
11356 hw_page_size = SLI4_PAGE_SIZE;
11357
James Smart4f774512009-05-22 14:52:35 -040011358 queue = kzalloc(sizeof(struct lpfc_queue) +
11359 (sizeof(union sli4_qe) * entry_count), GFP_KERNEL);
11360 if (!queue)
11361 return NULL;
James Smartcb5172e2010-03-15 11:25:07 -040011362 queue->page_count = (ALIGN(entry_size * entry_count,
11363 hw_page_size))/hw_page_size;
James Smart4f774512009-05-22 14:52:35 -040011364 INIT_LIST_HEAD(&queue->list);
11365 INIT_LIST_HEAD(&queue->page_list);
11366 INIT_LIST_HEAD(&queue->child_list);
11367 for (x = 0, total_qe_count = 0; x < queue->page_count; x++) {
11368 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
11369 if (!dmabuf)
11370 goto out_fail;
11371 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
James Smartcb5172e2010-03-15 11:25:07 -040011372 hw_page_size, &dmabuf->phys,
James Smart4f774512009-05-22 14:52:35 -040011373 GFP_KERNEL);
11374 if (!dmabuf->virt) {
11375 kfree(dmabuf);
11376 goto out_fail;
11377 }
James Smartcb5172e2010-03-15 11:25:07 -040011378 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -040011379 dmabuf->buffer_tag = x;
11380 list_add_tail(&dmabuf->list, &queue->page_list);
11381 /* initialize queue's entry array */
11382 dma_pointer = dmabuf->virt;
11383 for (; total_qe_count < entry_count &&
James Smartcb5172e2010-03-15 11:25:07 -040011384 dma_pointer < (hw_page_size + dmabuf->virt);
James Smart4f774512009-05-22 14:52:35 -040011385 total_qe_count++, dma_pointer += entry_size) {
11386 queue->qe[total_qe_count].address = dma_pointer;
11387 }
11388 }
11389 queue->entry_size = entry_size;
11390 queue->entry_count = entry_count;
James Smart73d91e52011-10-10 21:32:10 -040011391
11392 /*
11393 * entry_repost is calculated based on the number of entries in the
11394 * queue. This works out except for RQs. If buffers are NOT initially
11395 * posted for every RQE, entry_repost should be adjusted accordingly.
11396 */
11397 queue->entry_repost = (entry_count >> 3);
11398 if (queue->entry_repost < LPFC_QUEUE_MIN_REPOST)
11399 queue->entry_repost = LPFC_QUEUE_MIN_REPOST;
James Smart4f774512009-05-22 14:52:35 -040011400 queue->phba = phba;
11401
11402 return queue;
11403out_fail:
11404 lpfc_sli4_queue_free(queue);
11405 return NULL;
11406}
11407
11408/**
11409 * lpfc_eq_create - Create an Event Queue on the HBA
11410 * @phba: HBA structure that indicates port to create a queue on.
11411 * @eq: The queue structure to use to create the event queue.
11412 * @imax: The maximum interrupt per second limit.
11413 *
11414 * This function creates an event queue, as detailed in @eq, on a port,
11415 * described by @phba by sending an EQ_CREATE mailbox command to the HBA.
11416 *
11417 * The @phba struct is used to send mailbox command to HBA. The @eq struct
11418 * is used to get the entry count and entry size that are necessary to
11419 * determine the number of pages to allocate and use for this queue. This
11420 * function will send the EQ_CREATE mailbox command to the HBA to setup the
11421 * event queue. This function is asynchronous and will wait for the mailbox
11422 * command to finish before continuing.
11423 *
11424 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040011425 * memory this function will return -ENOMEM. If the queue create mailbox command
11426 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040011427 **/
11428uint32_t
11429lpfc_eq_create(struct lpfc_hba *phba, struct lpfc_queue *eq, uint16_t imax)
11430{
11431 struct lpfc_mbx_eq_create *eq_create;
11432 LPFC_MBOXQ_t *mbox;
11433 int rc, length, status = 0;
11434 struct lpfc_dmabuf *dmabuf;
11435 uint32_t shdr_status, shdr_add_status;
11436 union lpfc_sli4_cfg_shdr *shdr;
11437 uint16_t dmult;
James Smart49198b32010-04-06 15:04:33 -040011438 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
11439
11440 if (!phba->sli4_hba.pc_sli4_params.supported)
11441 hw_page_size = SLI4_PAGE_SIZE;
James Smart4f774512009-05-22 14:52:35 -040011442
11443 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
11444 if (!mbox)
11445 return -ENOMEM;
11446 length = (sizeof(struct lpfc_mbx_eq_create) -
11447 sizeof(struct lpfc_sli4_cfg_mhdr));
11448 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
11449 LPFC_MBOX_OPCODE_EQ_CREATE,
11450 length, LPFC_SLI4_MBX_EMBED);
11451 eq_create = &mbox->u.mqe.un.eq_create;
11452 bf_set(lpfc_mbx_eq_create_num_pages, &eq_create->u.request,
11453 eq->page_count);
11454 bf_set(lpfc_eq_context_size, &eq_create->u.request.context,
11455 LPFC_EQE_SIZE);
11456 bf_set(lpfc_eq_context_valid, &eq_create->u.request.context, 1);
11457 /* Calculate delay multiper from maximum interrupt per second */
11458 dmult = LPFC_DMULT_CONST/imax - 1;
11459 bf_set(lpfc_eq_context_delay_multi, &eq_create->u.request.context,
11460 dmult);
11461 switch (eq->entry_count) {
11462 default:
11463 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11464 "0360 Unsupported EQ count. (%d)\n",
11465 eq->entry_count);
11466 if (eq->entry_count < 256)
11467 return -EINVAL;
11468 /* otherwise default to smallest count (drop through) */
11469 case 256:
11470 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
11471 LPFC_EQ_CNT_256);
11472 break;
11473 case 512:
11474 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
11475 LPFC_EQ_CNT_512);
11476 break;
11477 case 1024:
11478 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
11479 LPFC_EQ_CNT_1024);
11480 break;
11481 case 2048:
11482 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
11483 LPFC_EQ_CNT_2048);
11484 break;
11485 case 4096:
11486 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
11487 LPFC_EQ_CNT_4096);
11488 break;
11489 }
11490 list_for_each_entry(dmabuf, &eq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040011491 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -040011492 eq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
11493 putPaddrLow(dmabuf->phys);
11494 eq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
11495 putPaddrHigh(dmabuf->phys);
11496 }
11497 mbox->vport = phba->pport;
11498 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
11499 mbox->context1 = NULL;
11500 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
11501 shdr = (union lpfc_sli4_cfg_shdr *) &eq_create->header.cfg_shdr;
11502 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
11503 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
11504 if (shdr_status || shdr_add_status || rc) {
11505 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11506 "2500 EQ_CREATE mailbox failed with "
11507 "status x%x add_status x%x, mbx status x%x\n",
11508 shdr_status, shdr_add_status, rc);
11509 status = -ENXIO;
11510 }
11511 eq->type = LPFC_EQ;
11512 eq->subtype = LPFC_NONE;
11513 eq->queue_id = bf_get(lpfc_mbx_eq_create_q_id, &eq_create->u.response);
11514 if (eq->queue_id == 0xFFFF)
11515 status = -ENXIO;
11516 eq->host_index = 0;
11517 eq->hba_index = 0;
11518
James Smart8fa38512009-07-19 10:01:03 -040011519 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040011520 return status;
11521}
11522
11523/**
11524 * lpfc_cq_create - Create a Completion Queue on the HBA
11525 * @phba: HBA structure that indicates port to create a queue on.
11526 * @cq: The queue structure to use to create the completion queue.
11527 * @eq: The event queue to bind this completion queue to.
11528 *
11529 * This function creates a completion queue, as detailed in @wq, on a port,
11530 * described by @phba by sending a CQ_CREATE mailbox command to the HBA.
11531 *
11532 * The @phba struct is used to send mailbox command to HBA. The @cq struct
11533 * is used to get the entry count and entry size that are necessary to
11534 * determine the number of pages to allocate and use for this queue. The @eq
11535 * is used to indicate which event queue to bind this completion queue to. This
11536 * function will send the CQ_CREATE mailbox command to the HBA to setup the
11537 * completion queue. This function is asynchronous and will wait for the mailbox
11538 * command to finish before continuing.
11539 *
11540 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040011541 * memory this function will return -ENOMEM. If the queue create mailbox command
11542 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040011543 **/
11544uint32_t
11545lpfc_cq_create(struct lpfc_hba *phba, struct lpfc_queue *cq,
11546 struct lpfc_queue *eq, uint32_t type, uint32_t subtype)
11547{
11548 struct lpfc_mbx_cq_create *cq_create;
11549 struct lpfc_dmabuf *dmabuf;
11550 LPFC_MBOXQ_t *mbox;
11551 int rc, length, status = 0;
11552 uint32_t shdr_status, shdr_add_status;
11553 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040011554 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
11555
11556 if (!phba->sli4_hba.pc_sli4_params.supported)
11557 hw_page_size = SLI4_PAGE_SIZE;
11558
James Smart4f774512009-05-22 14:52:35 -040011559 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
11560 if (!mbox)
11561 return -ENOMEM;
11562 length = (sizeof(struct lpfc_mbx_cq_create) -
11563 sizeof(struct lpfc_sli4_cfg_mhdr));
11564 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
11565 LPFC_MBOX_OPCODE_CQ_CREATE,
11566 length, LPFC_SLI4_MBX_EMBED);
11567 cq_create = &mbox->u.mqe.un.cq_create;
James Smart5a6f1332011-03-11 16:05:35 -050011568 shdr = (union lpfc_sli4_cfg_shdr *) &cq_create->header.cfg_shdr;
James Smart4f774512009-05-22 14:52:35 -040011569 bf_set(lpfc_mbx_cq_create_num_pages, &cq_create->u.request,
11570 cq->page_count);
11571 bf_set(lpfc_cq_context_event, &cq_create->u.request.context, 1);
11572 bf_set(lpfc_cq_context_valid, &cq_create->u.request.context, 1);
James Smart5a6f1332011-03-11 16:05:35 -050011573 bf_set(lpfc_mbox_hdr_version, &shdr->request,
11574 phba->sli4_hba.pc_sli4_params.cqv);
11575 if (phba->sli4_hba.pc_sli4_params.cqv == LPFC_Q_CREATE_VERSION_2) {
James Smartc31098c2011-04-16 11:03:33 -040011576 /* FW only supports 1. Should be PAGE_SIZE/SLI4_PAGE_SIZE */
11577 bf_set(lpfc_mbx_cq_create_page_size, &cq_create->u.request, 1);
James Smart5a6f1332011-03-11 16:05:35 -050011578 bf_set(lpfc_cq_eq_id_2, &cq_create->u.request.context,
11579 eq->queue_id);
11580 } else {
11581 bf_set(lpfc_cq_eq_id, &cq_create->u.request.context,
11582 eq->queue_id);
11583 }
James Smart4f774512009-05-22 14:52:35 -040011584 switch (cq->entry_count) {
11585 default:
11586 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11587 "0361 Unsupported CQ count. (%d)\n",
11588 cq->entry_count);
11589 if (cq->entry_count < 256)
11590 return -EINVAL;
11591 /* otherwise default to smallest count (drop through) */
11592 case 256:
11593 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
11594 LPFC_CQ_CNT_256);
11595 break;
11596 case 512:
11597 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
11598 LPFC_CQ_CNT_512);
11599 break;
11600 case 1024:
11601 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
11602 LPFC_CQ_CNT_1024);
11603 break;
11604 }
11605 list_for_each_entry(dmabuf, &cq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040011606 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -040011607 cq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
11608 putPaddrLow(dmabuf->phys);
11609 cq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
11610 putPaddrHigh(dmabuf->phys);
11611 }
11612 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
11613
11614 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart4f774512009-05-22 14:52:35 -040011615 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
11616 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
11617 if (shdr_status || shdr_add_status || rc) {
11618 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11619 "2501 CQ_CREATE mailbox failed with "
11620 "status x%x add_status x%x, mbx status x%x\n",
11621 shdr_status, shdr_add_status, rc);
11622 status = -ENXIO;
11623 goto out;
11624 }
11625 cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
11626 if (cq->queue_id == 0xFFFF) {
11627 status = -ENXIO;
11628 goto out;
11629 }
11630 /* link the cq onto the parent eq child list */
11631 list_add_tail(&cq->list, &eq->child_list);
11632 /* Set up completion queue's type and subtype */
11633 cq->type = type;
11634 cq->subtype = subtype;
11635 cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
James Smart2a622bf2011-02-16 12:40:06 -050011636 cq->assoc_qid = eq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040011637 cq->host_index = 0;
11638 cq->hba_index = 0;
James Smart4f774512009-05-22 14:52:35 -040011639
James Smart8fa38512009-07-19 10:01:03 -040011640out:
11641 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040011642 return status;
11643}
11644
11645/**
James Smartb19a0612010-04-06 14:48:51 -040011646 * lpfc_mq_create_fb_init - Send MCC_CREATE without async events registration
James Smart04c68492009-05-22 14:52:52 -040011647 * @phba: HBA structure that indicates port to create a queue on.
11648 * @mq: The queue structure to use to create the mailbox queue.
James Smartb19a0612010-04-06 14:48:51 -040011649 * @mbox: An allocated pointer to type LPFC_MBOXQ_t
11650 * @cq: The completion queue to associate with this cq.
James Smart04c68492009-05-22 14:52:52 -040011651 *
James Smartb19a0612010-04-06 14:48:51 -040011652 * This function provides failback (fb) functionality when the
11653 * mq_create_ext fails on older FW generations. It's purpose is identical
11654 * to mq_create_ext otherwise.
James Smart04c68492009-05-22 14:52:52 -040011655 *
James Smartb19a0612010-04-06 14:48:51 -040011656 * This routine cannot fail as all attributes were previously accessed and
11657 * initialized in mq_create_ext.
James Smart04c68492009-05-22 14:52:52 -040011658 **/
James Smartb19a0612010-04-06 14:48:51 -040011659static void
11660lpfc_mq_create_fb_init(struct lpfc_hba *phba, struct lpfc_queue *mq,
11661 LPFC_MBOXQ_t *mbox, struct lpfc_queue *cq)
James Smart04c68492009-05-22 14:52:52 -040011662{
11663 struct lpfc_mbx_mq_create *mq_create;
11664 struct lpfc_dmabuf *dmabuf;
James Smartb19a0612010-04-06 14:48:51 -040011665 int length;
James Smart04c68492009-05-22 14:52:52 -040011666
James Smart04c68492009-05-22 14:52:52 -040011667 length = (sizeof(struct lpfc_mbx_mq_create) -
11668 sizeof(struct lpfc_sli4_cfg_mhdr));
11669 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
11670 LPFC_MBOX_OPCODE_MQ_CREATE,
11671 length, LPFC_SLI4_MBX_EMBED);
11672 mq_create = &mbox->u.mqe.un.mq_create;
11673 bf_set(lpfc_mbx_mq_create_num_pages, &mq_create->u.request,
James Smartb19a0612010-04-06 14:48:51 -040011674 mq->page_count);
James Smart04c68492009-05-22 14:52:52 -040011675 bf_set(lpfc_mq_context_cq_id, &mq_create->u.request.context,
James Smartb19a0612010-04-06 14:48:51 -040011676 cq->queue_id);
James Smart04c68492009-05-22 14:52:52 -040011677 bf_set(lpfc_mq_context_valid, &mq_create->u.request.context, 1);
11678 switch (mq->entry_count) {
James Smart04c68492009-05-22 14:52:52 -040011679 case 16:
James Smart5a6f1332011-03-11 16:05:35 -050011680 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
11681 LPFC_MQ_RING_SIZE_16);
James Smart04c68492009-05-22 14:52:52 -040011682 break;
11683 case 32:
James Smart5a6f1332011-03-11 16:05:35 -050011684 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
11685 LPFC_MQ_RING_SIZE_32);
James Smart04c68492009-05-22 14:52:52 -040011686 break;
11687 case 64:
James Smart5a6f1332011-03-11 16:05:35 -050011688 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
11689 LPFC_MQ_RING_SIZE_64);
James Smart04c68492009-05-22 14:52:52 -040011690 break;
11691 case 128:
James Smart5a6f1332011-03-11 16:05:35 -050011692 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
11693 LPFC_MQ_RING_SIZE_128);
James Smart04c68492009-05-22 14:52:52 -040011694 break;
11695 }
11696 list_for_each_entry(dmabuf, &mq->page_list, list) {
11697 mq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
James Smartb19a0612010-04-06 14:48:51 -040011698 putPaddrLow(dmabuf->phys);
James Smart04c68492009-05-22 14:52:52 -040011699 mq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
James Smartb19a0612010-04-06 14:48:51 -040011700 putPaddrHigh(dmabuf->phys);
11701 }
11702}
11703
11704/**
11705 * lpfc_mq_create - Create a mailbox Queue on the HBA
11706 * @phba: HBA structure that indicates port to create a queue on.
11707 * @mq: The queue structure to use to create the mailbox queue.
11708 * @cq: The completion queue to associate with this cq.
11709 * @subtype: The queue's subtype.
11710 *
11711 * This function creates a mailbox queue, as detailed in @mq, on a port,
11712 * described by @phba by sending a MQ_CREATE mailbox command to the HBA.
11713 *
11714 * The @phba struct is used to send mailbox command to HBA. The @cq struct
11715 * is used to get the entry count and entry size that are necessary to
11716 * determine the number of pages to allocate and use for this queue. This
11717 * function will send the MQ_CREATE mailbox command to the HBA to setup the
11718 * mailbox queue. This function is asynchronous and will wait for the mailbox
11719 * command to finish before continuing.
11720 *
11721 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040011722 * memory this function will return -ENOMEM. If the queue create mailbox command
11723 * fails this function will return -ENXIO.
James Smartb19a0612010-04-06 14:48:51 -040011724 **/
11725int32_t
11726lpfc_mq_create(struct lpfc_hba *phba, struct lpfc_queue *mq,
11727 struct lpfc_queue *cq, uint32_t subtype)
11728{
11729 struct lpfc_mbx_mq_create *mq_create;
11730 struct lpfc_mbx_mq_create_ext *mq_create_ext;
11731 struct lpfc_dmabuf *dmabuf;
11732 LPFC_MBOXQ_t *mbox;
11733 int rc, length, status = 0;
11734 uint32_t shdr_status, shdr_add_status;
11735 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040011736 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smartb19a0612010-04-06 14:48:51 -040011737
James Smart49198b32010-04-06 15:04:33 -040011738 if (!phba->sli4_hba.pc_sli4_params.supported)
11739 hw_page_size = SLI4_PAGE_SIZE;
James Smartb19a0612010-04-06 14:48:51 -040011740
11741 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
11742 if (!mbox)
11743 return -ENOMEM;
11744 length = (sizeof(struct lpfc_mbx_mq_create_ext) -
11745 sizeof(struct lpfc_sli4_cfg_mhdr));
11746 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
11747 LPFC_MBOX_OPCODE_MQ_CREATE_EXT,
11748 length, LPFC_SLI4_MBX_EMBED);
11749
11750 mq_create_ext = &mbox->u.mqe.un.mq_create_ext;
James Smart5a6f1332011-03-11 16:05:35 -050011751 shdr = (union lpfc_sli4_cfg_shdr *) &mq_create_ext->header.cfg_shdr;
James Smart70f3c072010-12-15 17:57:33 -050011752 bf_set(lpfc_mbx_mq_create_ext_num_pages,
11753 &mq_create_ext->u.request, mq->page_count);
11754 bf_set(lpfc_mbx_mq_create_ext_async_evt_link,
11755 &mq_create_ext->u.request, 1);
11756 bf_set(lpfc_mbx_mq_create_ext_async_evt_fip,
James Smartb19a0612010-04-06 14:48:51 -040011757 &mq_create_ext->u.request, 1);
11758 bf_set(lpfc_mbx_mq_create_ext_async_evt_group5,
11759 &mq_create_ext->u.request, 1);
James Smart70f3c072010-12-15 17:57:33 -050011760 bf_set(lpfc_mbx_mq_create_ext_async_evt_fc,
11761 &mq_create_ext->u.request, 1);
11762 bf_set(lpfc_mbx_mq_create_ext_async_evt_sli,
11763 &mq_create_ext->u.request, 1);
James Smartb19a0612010-04-06 14:48:51 -040011764 bf_set(lpfc_mq_context_valid, &mq_create_ext->u.request.context, 1);
James Smart5a6f1332011-03-11 16:05:35 -050011765 bf_set(lpfc_mbox_hdr_version, &shdr->request,
11766 phba->sli4_hba.pc_sli4_params.mqv);
11767 if (phba->sli4_hba.pc_sli4_params.mqv == LPFC_Q_CREATE_VERSION_1)
11768 bf_set(lpfc_mbx_mq_create_ext_cq_id, &mq_create_ext->u.request,
11769 cq->queue_id);
11770 else
11771 bf_set(lpfc_mq_context_cq_id, &mq_create_ext->u.request.context,
11772 cq->queue_id);
James Smartb19a0612010-04-06 14:48:51 -040011773 switch (mq->entry_count) {
11774 default:
11775 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11776 "0362 Unsupported MQ count. (%d)\n",
11777 mq->entry_count);
11778 if (mq->entry_count < 16)
11779 return -EINVAL;
11780 /* otherwise default to smallest count (drop through) */
11781 case 16:
James Smart5a6f1332011-03-11 16:05:35 -050011782 bf_set(lpfc_mq_context_ring_size,
11783 &mq_create_ext->u.request.context,
11784 LPFC_MQ_RING_SIZE_16);
James Smartb19a0612010-04-06 14:48:51 -040011785 break;
11786 case 32:
James Smart5a6f1332011-03-11 16:05:35 -050011787 bf_set(lpfc_mq_context_ring_size,
11788 &mq_create_ext->u.request.context,
11789 LPFC_MQ_RING_SIZE_32);
James Smartb19a0612010-04-06 14:48:51 -040011790 break;
11791 case 64:
James Smart5a6f1332011-03-11 16:05:35 -050011792 bf_set(lpfc_mq_context_ring_size,
11793 &mq_create_ext->u.request.context,
11794 LPFC_MQ_RING_SIZE_64);
James Smartb19a0612010-04-06 14:48:51 -040011795 break;
11796 case 128:
James Smart5a6f1332011-03-11 16:05:35 -050011797 bf_set(lpfc_mq_context_ring_size,
11798 &mq_create_ext->u.request.context,
11799 LPFC_MQ_RING_SIZE_128);
James Smartb19a0612010-04-06 14:48:51 -040011800 break;
11801 }
11802 list_for_each_entry(dmabuf, &mq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040011803 memset(dmabuf->virt, 0, hw_page_size);
James Smartb19a0612010-04-06 14:48:51 -040011804 mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_lo =
11805 putPaddrLow(dmabuf->phys);
11806 mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_hi =
James Smart04c68492009-05-22 14:52:52 -040011807 putPaddrHigh(dmabuf->phys);
11808 }
11809 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
James Smartb19a0612010-04-06 14:48:51 -040011810 mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
11811 &mq_create_ext->u.response);
11812 if (rc != MBX_SUCCESS) {
11813 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
11814 "2795 MQ_CREATE_EXT failed with "
11815 "status x%x. Failback to MQ_CREATE.\n",
11816 rc);
11817 lpfc_mq_create_fb_init(phba, mq, mbox, cq);
11818 mq_create = &mbox->u.mqe.un.mq_create;
11819 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
11820 shdr = (union lpfc_sli4_cfg_shdr *) &mq_create->header.cfg_shdr;
11821 mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
11822 &mq_create->u.response);
11823 }
11824
James Smart04c68492009-05-22 14:52:52 -040011825 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart04c68492009-05-22 14:52:52 -040011826 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
11827 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
11828 if (shdr_status || shdr_add_status || rc) {
11829 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11830 "2502 MQ_CREATE mailbox failed with "
11831 "status x%x add_status x%x, mbx status x%x\n",
11832 shdr_status, shdr_add_status, rc);
11833 status = -ENXIO;
11834 goto out;
11835 }
James Smart04c68492009-05-22 14:52:52 -040011836 if (mq->queue_id == 0xFFFF) {
11837 status = -ENXIO;
11838 goto out;
11839 }
11840 mq->type = LPFC_MQ;
James Smart2a622bf2011-02-16 12:40:06 -050011841 mq->assoc_qid = cq->queue_id;
James Smart04c68492009-05-22 14:52:52 -040011842 mq->subtype = subtype;
11843 mq->host_index = 0;
11844 mq->hba_index = 0;
11845
11846 /* link the mq onto the parent cq child list */
11847 list_add_tail(&mq->list, &cq->child_list);
11848out:
James Smart8fa38512009-07-19 10:01:03 -040011849 mempool_free(mbox, phba->mbox_mem_pool);
James Smart04c68492009-05-22 14:52:52 -040011850 return status;
11851}
11852
11853/**
James Smart4f774512009-05-22 14:52:35 -040011854 * lpfc_wq_create - Create a Work Queue on the HBA
11855 * @phba: HBA structure that indicates port to create a queue on.
11856 * @wq: The queue structure to use to create the work queue.
11857 * @cq: The completion queue to bind this work queue to.
11858 * @subtype: The subtype of the work queue indicating its functionality.
11859 *
11860 * This function creates a work queue, as detailed in @wq, on a port, described
11861 * by @phba by sending a WQ_CREATE mailbox command to the HBA.
11862 *
11863 * The @phba struct is used to send mailbox command to HBA. The @wq struct
11864 * is used to get the entry count and entry size that are necessary to
11865 * determine the number of pages to allocate and use for this queue. The @cq
11866 * is used to indicate which completion queue to bind this work queue to. This
11867 * function will send the WQ_CREATE mailbox command to the HBA to setup the
11868 * work queue. This function is asynchronous and will wait for the mailbox
11869 * command to finish before continuing.
11870 *
11871 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040011872 * memory this function will return -ENOMEM. If the queue create mailbox command
11873 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040011874 **/
11875uint32_t
11876lpfc_wq_create(struct lpfc_hba *phba, struct lpfc_queue *wq,
11877 struct lpfc_queue *cq, uint32_t subtype)
11878{
11879 struct lpfc_mbx_wq_create *wq_create;
11880 struct lpfc_dmabuf *dmabuf;
11881 LPFC_MBOXQ_t *mbox;
11882 int rc, length, status = 0;
11883 uint32_t shdr_status, shdr_add_status;
11884 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040011885 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smart5a6f1332011-03-11 16:05:35 -050011886 struct dma_address *page;
James Smart49198b32010-04-06 15:04:33 -040011887
11888 if (!phba->sli4_hba.pc_sli4_params.supported)
11889 hw_page_size = SLI4_PAGE_SIZE;
James Smart4f774512009-05-22 14:52:35 -040011890
11891 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
11892 if (!mbox)
11893 return -ENOMEM;
11894 length = (sizeof(struct lpfc_mbx_wq_create) -
11895 sizeof(struct lpfc_sli4_cfg_mhdr));
11896 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
11897 LPFC_MBOX_OPCODE_FCOE_WQ_CREATE,
11898 length, LPFC_SLI4_MBX_EMBED);
11899 wq_create = &mbox->u.mqe.un.wq_create;
James Smart5a6f1332011-03-11 16:05:35 -050011900 shdr = (union lpfc_sli4_cfg_shdr *) &wq_create->header.cfg_shdr;
James Smart4f774512009-05-22 14:52:35 -040011901 bf_set(lpfc_mbx_wq_create_num_pages, &wq_create->u.request,
11902 wq->page_count);
11903 bf_set(lpfc_mbx_wq_create_cq_id, &wq_create->u.request,
11904 cq->queue_id);
James Smart5a6f1332011-03-11 16:05:35 -050011905 bf_set(lpfc_mbox_hdr_version, &shdr->request,
11906 phba->sli4_hba.pc_sli4_params.wqv);
11907 if (phba->sli4_hba.pc_sli4_params.wqv == LPFC_Q_CREATE_VERSION_1) {
11908 bf_set(lpfc_mbx_wq_create_wqe_count, &wq_create->u.request_1,
11909 wq->entry_count);
11910 switch (wq->entry_size) {
11911 default:
11912 case 64:
11913 bf_set(lpfc_mbx_wq_create_wqe_size,
11914 &wq_create->u.request_1,
11915 LPFC_WQ_WQE_SIZE_64);
11916 break;
11917 case 128:
11918 bf_set(lpfc_mbx_wq_create_wqe_size,
11919 &wq_create->u.request_1,
11920 LPFC_WQ_WQE_SIZE_128);
11921 break;
11922 }
11923 bf_set(lpfc_mbx_wq_create_page_size, &wq_create->u.request_1,
11924 (PAGE_SIZE/SLI4_PAGE_SIZE));
11925 page = wq_create->u.request_1.page;
11926 } else {
11927 page = wq_create->u.request.page;
11928 }
James Smart4f774512009-05-22 14:52:35 -040011929 list_for_each_entry(dmabuf, &wq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040011930 memset(dmabuf->virt, 0, hw_page_size);
James Smart5a6f1332011-03-11 16:05:35 -050011931 page[dmabuf->buffer_tag].addr_lo = putPaddrLow(dmabuf->phys);
11932 page[dmabuf->buffer_tag].addr_hi = putPaddrHigh(dmabuf->phys);
James Smart4f774512009-05-22 14:52:35 -040011933 }
11934 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
11935 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart4f774512009-05-22 14:52:35 -040011936 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
11937 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
11938 if (shdr_status || shdr_add_status || rc) {
11939 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11940 "2503 WQ_CREATE mailbox failed with "
11941 "status x%x add_status x%x, mbx status x%x\n",
11942 shdr_status, shdr_add_status, rc);
11943 status = -ENXIO;
11944 goto out;
11945 }
11946 wq->queue_id = bf_get(lpfc_mbx_wq_create_q_id, &wq_create->u.response);
11947 if (wq->queue_id == 0xFFFF) {
11948 status = -ENXIO;
11949 goto out;
11950 }
11951 wq->type = LPFC_WQ;
James Smart2a622bf2011-02-16 12:40:06 -050011952 wq->assoc_qid = cq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040011953 wq->subtype = subtype;
11954 wq->host_index = 0;
11955 wq->hba_index = 0;
11956
11957 /* link the wq onto the parent cq child list */
11958 list_add_tail(&wq->list, &cq->child_list);
11959out:
James Smart8fa38512009-07-19 10:01:03 -040011960 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040011961 return status;
11962}
11963
11964/**
James Smart73d91e52011-10-10 21:32:10 -040011965 * lpfc_rq_adjust_repost - Adjust entry_repost for an RQ
11966 * @phba: HBA structure that indicates port to create a queue on.
11967 * @rq: The queue structure to use for the receive queue.
11968 * @qno: The associated HBQ number
11969 *
11970 *
11971 * For SLI4 we need to adjust the RQ repost value based on
11972 * the number of buffers that are initially posted to the RQ.
11973 */
11974void
11975lpfc_rq_adjust_repost(struct lpfc_hba *phba, struct lpfc_queue *rq, int qno)
11976{
11977 uint32_t cnt;
11978
11979 cnt = lpfc_hbq_defs[qno]->entry_count;
11980
11981 /* Recalc repost for RQs based on buffers initially posted */
11982 cnt = (cnt >> 3);
11983 if (cnt < LPFC_QUEUE_MIN_REPOST)
11984 cnt = LPFC_QUEUE_MIN_REPOST;
11985
11986 rq->entry_repost = cnt;
11987}
11988
11989/**
James Smart4f774512009-05-22 14:52:35 -040011990 * lpfc_rq_create - Create a Receive Queue on the HBA
11991 * @phba: HBA structure that indicates port to create a queue on.
11992 * @hrq: The queue structure to use to create the header receive queue.
11993 * @drq: The queue structure to use to create the data receive queue.
11994 * @cq: The completion queue to bind this work queue to.
11995 *
11996 * This function creates a receive buffer queue pair , as detailed in @hrq and
11997 * @drq, on a port, described by @phba by sending a RQ_CREATE mailbox command
11998 * to the HBA.
11999 *
12000 * The @phba struct is used to send mailbox command to HBA. The @drq and @hrq
12001 * struct is used to get the entry count that is necessary to determine the
12002 * number of pages to use for this queue. The @cq is used to indicate which
12003 * completion queue to bind received buffers that are posted to these queues to.
12004 * This function will send the RQ_CREATE mailbox command to the HBA to setup the
12005 * receive queue pair. This function is asynchronous and will wait for the
12006 * mailbox command to finish before continuing.
12007 *
12008 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040012009 * memory this function will return -ENOMEM. If the queue create mailbox command
12010 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040012011 **/
12012uint32_t
12013lpfc_rq_create(struct lpfc_hba *phba, struct lpfc_queue *hrq,
12014 struct lpfc_queue *drq, struct lpfc_queue *cq, uint32_t subtype)
12015{
12016 struct lpfc_mbx_rq_create *rq_create;
12017 struct lpfc_dmabuf *dmabuf;
12018 LPFC_MBOXQ_t *mbox;
12019 int rc, length, status = 0;
12020 uint32_t shdr_status, shdr_add_status;
12021 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040012022 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
12023
12024 if (!phba->sli4_hba.pc_sli4_params.supported)
12025 hw_page_size = SLI4_PAGE_SIZE;
James Smart4f774512009-05-22 14:52:35 -040012026
12027 if (hrq->entry_count != drq->entry_count)
12028 return -EINVAL;
12029 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12030 if (!mbox)
12031 return -ENOMEM;
12032 length = (sizeof(struct lpfc_mbx_rq_create) -
12033 sizeof(struct lpfc_sli4_cfg_mhdr));
12034 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
12035 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
12036 length, LPFC_SLI4_MBX_EMBED);
12037 rq_create = &mbox->u.mqe.un.rq_create;
James Smart5a6f1332011-03-11 16:05:35 -050012038 shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
12039 bf_set(lpfc_mbox_hdr_version, &shdr->request,
12040 phba->sli4_hba.pc_sli4_params.rqv);
12041 if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
12042 bf_set(lpfc_rq_context_rqe_count_1,
12043 &rq_create->u.request.context,
12044 hrq->entry_count);
12045 rq_create->u.request.context.buffer_size = LPFC_HDR_BUF_SIZE;
James Smartc31098c2011-04-16 11:03:33 -040012046 bf_set(lpfc_rq_context_rqe_size,
12047 &rq_create->u.request.context,
12048 LPFC_RQE_SIZE_8);
12049 bf_set(lpfc_rq_context_page_size,
12050 &rq_create->u.request.context,
12051 (PAGE_SIZE/SLI4_PAGE_SIZE));
James Smart5a6f1332011-03-11 16:05:35 -050012052 } else {
12053 switch (hrq->entry_count) {
12054 default:
12055 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12056 "2535 Unsupported RQ count. (%d)\n",
12057 hrq->entry_count);
12058 if (hrq->entry_count < 512)
12059 return -EINVAL;
12060 /* otherwise default to smallest count (drop through) */
12061 case 512:
12062 bf_set(lpfc_rq_context_rqe_count,
12063 &rq_create->u.request.context,
12064 LPFC_RQ_RING_SIZE_512);
12065 break;
12066 case 1024:
12067 bf_set(lpfc_rq_context_rqe_count,
12068 &rq_create->u.request.context,
12069 LPFC_RQ_RING_SIZE_1024);
12070 break;
12071 case 2048:
12072 bf_set(lpfc_rq_context_rqe_count,
12073 &rq_create->u.request.context,
12074 LPFC_RQ_RING_SIZE_2048);
12075 break;
12076 case 4096:
12077 bf_set(lpfc_rq_context_rqe_count,
12078 &rq_create->u.request.context,
12079 LPFC_RQ_RING_SIZE_4096);
12080 break;
12081 }
12082 bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
12083 LPFC_HDR_BUF_SIZE);
James Smart4f774512009-05-22 14:52:35 -040012084 }
12085 bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
12086 cq->queue_id);
12087 bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
12088 hrq->page_count);
James Smart4f774512009-05-22 14:52:35 -040012089 list_for_each_entry(dmabuf, &hrq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040012090 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -040012091 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
12092 putPaddrLow(dmabuf->phys);
12093 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
12094 putPaddrHigh(dmabuf->phys);
12095 }
12096 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
12097 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart4f774512009-05-22 14:52:35 -040012098 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12099 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12100 if (shdr_status || shdr_add_status || rc) {
12101 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12102 "2504 RQ_CREATE mailbox failed with "
12103 "status x%x add_status x%x, mbx status x%x\n",
12104 shdr_status, shdr_add_status, rc);
12105 status = -ENXIO;
12106 goto out;
12107 }
12108 hrq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
12109 if (hrq->queue_id == 0xFFFF) {
12110 status = -ENXIO;
12111 goto out;
12112 }
12113 hrq->type = LPFC_HRQ;
James Smart2a622bf2011-02-16 12:40:06 -050012114 hrq->assoc_qid = cq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040012115 hrq->subtype = subtype;
12116 hrq->host_index = 0;
12117 hrq->hba_index = 0;
12118
12119 /* now create the data queue */
12120 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
12121 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
12122 length, LPFC_SLI4_MBX_EMBED);
James Smart5a6f1332011-03-11 16:05:35 -050012123 bf_set(lpfc_mbox_hdr_version, &shdr->request,
12124 phba->sli4_hba.pc_sli4_params.rqv);
12125 if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
12126 bf_set(lpfc_rq_context_rqe_count_1,
James Smartc31098c2011-04-16 11:03:33 -040012127 &rq_create->u.request.context, hrq->entry_count);
James Smart5a6f1332011-03-11 16:05:35 -050012128 rq_create->u.request.context.buffer_size = LPFC_DATA_BUF_SIZE;
James Smartc31098c2011-04-16 11:03:33 -040012129 bf_set(lpfc_rq_context_rqe_size, &rq_create->u.request.context,
12130 LPFC_RQE_SIZE_8);
12131 bf_set(lpfc_rq_context_page_size, &rq_create->u.request.context,
12132 (PAGE_SIZE/SLI4_PAGE_SIZE));
James Smart5a6f1332011-03-11 16:05:35 -050012133 } else {
12134 switch (drq->entry_count) {
12135 default:
12136 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12137 "2536 Unsupported RQ count. (%d)\n",
12138 drq->entry_count);
12139 if (drq->entry_count < 512)
12140 return -EINVAL;
12141 /* otherwise default to smallest count (drop through) */
12142 case 512:
12143 bf_set(lpfc_rq_context_rqe_count,
12144 &rq_create->u.request.context,
12145 LPFC_RQ_RING_SIZE_512);
12146 break;
12147 case 1024:
12148 bf_set(lpfc_rq_context_rqe_count,
12149 &rq_create->u.request.context,
12150 LPFC_RQ_RING_SIZE_1024);
12151 break;
12152 case 2048:
12153 bf_set(lpfc_rq_context_rqe_count,
12154 &rq_create->u.request.context,
12155 LPFC_RQ_RING_SIZE_2048);
12156 break;
12157 case 4096:
12158 bf_set(lpfc_rq_context_rqe_count,
12159 &rq_create->u.request.context,
12160 LPFC_RQ_RING_SIZE_4096);
12161 break;
12162 }
12163 bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
12164 LPFC_DATA_BUF_SIZE);
James Smart4f774512009-05-22 14:52:35 -040012165 }
12166 bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
12167 cq->queue_id);
12168 bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
12169 drq->page_count);
James Smart4f774512009-05-22 14:52:35 -040012170 list_for_each_entry(dmabuf, &drq->page_list, list) {
12171 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
12172 putPaddrLow(dmabuf->phys);
12173 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
12174 putPaddrHigh(dmabuf->phys);
12175 }
12176 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
12177 /* The IOCTL status is embedded in the mailbox subheader. */
12178 shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
12179 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12180 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12181 if (shdr_status || shdr_add_status || rc) {
12182 status = -ENXIO;
12183 goto out;
12184 }
12185 drq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
12186 if (drq->queue_id == 0xFFFF) {
12187 status = -ENXIO;
12188 goto out;
12189 }
12190 drq->type = LPFC_DRQ;
James Smart2a622bf2011-02-16 12:40:06 -050012191 drq->assoc_qid = cq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040012192 drq->subtype = subtype;
12193 drq->host_index = 0;
12194 drq->hba_index = 0;
12195
12196 /* link the header and data RQs onto the parent cq child list */
12197 list_add_tail(&hrq->list, &cq->child_list);
12198 list_add_tail(&drq->list, &cq->child_list);
12199
12200out:
James Smart8fa38512009-07-19 10:01:03 -040012201 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040012202 return status;
12203}
12204
12205/**
12206 * lpfc_eq_destroy - Destroy an event Queue on the HBA
12207 * @eq: The queue structure associated with the queue to destroy.
12208 *
12209 * This function destroys a queue, as detailed in @eq by sending an mailbox
12210 * command, specific to the type of queue, to the HBA.
12211 *
12212 * The @eq struct is used to get the queue ID of the queue to destroy.
12213 *
12214 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040012215 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040012216 **/
12217uint32_t
12218lpfc_eq_destroy(struct lpfc_hba *phba, struct lpfc_queue *eq)
12219{
12220 LPFC_MBOXQ_t *mbox;
12221 int rc, length, status = 0;
12222 uint32_t shdr_status, shdr_add_status;
12223 union lpfc_sli4_cfg_shdr *shdr;
12224
12225 if (!eq)
12226 return -ENODEV;
12227 mbox = mempool_alloc(eq->phba->mbox_mem_pool, GFP_KERNEL);
12228 if (!mbox)
12229 return -ENOMEM;
12230 length = (sizeof(struct lpfc_mbx_eq_destroy) -
12231 sizeof(struct lpfc_sli4_cfg_mhdr));
12232 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
12233 LPFC_MBOX_OPCODE_EQ_DESTROY,
12234 length, LPFC_SLI4_MBX_EMBED);
12235 bf_set(lpfc_mbx_eq_destroy_q_id, &mbox->u.mqe.un.eq_destroy.u.request,
12236 eq->queue_id);
12237 mbox->vport = eq->phba->pport;
12238 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
12239
12240 rc = lpfc_sli_issue_mbox(eq->phba, mbox, MBX_POLL);
12241 /* The IOCTL status is embedded in the mailbox subheader. */
12242 shdr = (union lpfc_sli4_cfg_shdr *)
12243 &mbox->u.mqe.un.eq_destroy.header.cfg_shdr;
12244 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12245 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12246 if (shdr_status || shdr_add_status || rc) {
12247 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12248 "2505 EQ_DESTROY mailbox failed with "
12249 "status x%x add_status x%x, mbx status x%x\n",
12250 shdr_status, shdr_add_status, rc);
12251 status = -ENXIO;
12252 }
12253
12254 /* Remove eq from any list */
12255 list_del_init(&eq->list);
James Smart8fa38512009-07-19 10:01:03 -040012256 mempool_free(mbox, eq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040012257 return status;
12258}
12259
12260/**
12261 * lpfc_cq_destroy - Destroy a Completion Queue on the HBA
12262 * @cq: The queue structure associated with the queue to destroy.
12263 *
12264 * This function destroys a queue, as detailed in @cq by sending an mailbox
12265 * command, specific to the type of queue, to the HBA.
12266 *
12267 * The @cq struct is used to get the queue ID of the queue to destroy.
12268 *
12269 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040012270 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040012271 **/
12272uint32_t
12273lpfc_cq_destroy(struct lpfc_hba *phba, struct lpfc_queue *cq)
12274{
12275 LPFC_MBOXQ_t *mbox;
12276 int rc, length, status = 0;
12277 uint32_t shdr_status, shdr_add_status;
12278 union lpfc_sli4_cfg_shdr *shdr;
12279
12280 if (!cq)
12281 return -ENODEV;
12282 mbox = mempool_alloc(cq->phba->mbox_mem_pool, GFP_KERNEL);
12283 if (!mbox)
12284 return -ENOMEM;
12285 length = (sizeof(struct lpfc_mbx_cq_destroy) -
12286 sizeof(struct lpfc_sli4_cfg_mhdr));
12287 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
12288 LPFC_MBOX_OPCODE_CQ_DESTROY,
12289 length, LPFC_SLI4_MBX_EMBED);
12290 bf_set(lpfc_mbx_cq_destroy_q_id, &mbox->u.mqe.un.cq_destroy.u.request,
12291 cq->queue_id);
12292 mbox->vport = cq->phba->pport;
12293 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
12294 rc = lpfc_sli_issue_mbox(cq->phba, mbox, MBX_POLL);
12295 /* The IOCTL status is embedded in the mailbox subheader. */
12296 shdr = (union lpfc_sli4_cfg_shdr *)
12297 &mbox->u.mqe.un.wq_create.header.cfg_shdr;
12298 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12299 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12300 if (shdr_status || shdr_add_status || rc) {
12301 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12302 "2506 CQ_DESTROY mailbox failed with "
12303 "status x%x add_status x%x, mbx status x%x\n",
12304 shdr_status, shdr_add_status, rc);
12305 status = -ENXIO;
12306 }
12307 /* Remove cq from any list */
12308 list_del_init(&cq->list);
James Smart8fa38512009-07-19 10:01:03 -040012309 mempool_free(mbox, cq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040012310 return status;
12311}
12312
12313/**
James Smart04c68492009-05-22 14:52:52 -040012314 * lpfc_mq_destroy - Destroy a Mailbox Queue on the HBA
12315 * @qm: The queue structure associated with the queue to destroy.
12316 *
12317 * This function destroys a queue, as detailed in @mq by sending an mailbox
12318 * command, specific to the type of queue, to the HBA.
12319 *
12320 * The @mq struct is used to get the queue ID of the queue to destroy.
12321 *
12322 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040012323 * command fails this function will return -ENXIO.
James Smart04c68492009-05-22 14:52:52 -040012324 **/
12325uint32_t
12326lpfc_mq_destroy(struct lpfc_hba *phba, struct lpfc_queue *mq)
12327{
12328 LPFC_MBOXQ_t *mbox;
12329 int rc, length, status = 0;
12330 uint32_t shdr_status, shdr_add_status;
12331 union lpfc_sli4_cfg_shdr *shdr;
12332
12333 if (!mq)
12334 return -ENODEV;
12335 mbox = mempool_alloc(mq->phba->mbox_mem_pool, GFP_KERNEL);
12336 if (!mbox)
12337 return -ENOMEM;
12338 length = (sizeof(struct lpfc_mbx_mq_destroy) -
12339 sizeof(struct lpfc_sli4_cfg_mhdr));
12340 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
12341 LPFC_MBOX_OPCODE_MQ_DESTROY,
12342 length, LPFC_SLI4_MBX_EMBED);
12343 bf_set(lpfc_mbx_mq_destroy_q_id, &mbox->u.mqe.un.mq_destroy.u.request,
12344 mq->queue_id);
12345 mbox->vport = mq->phba->pport;
12346 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
12347 rc = lpfc_sli_issue_mbox(mq->phba, mbox, MBX_POLL);
12348 /* The IOCTL status is embedded in the mailbox subheader. */
12349 shdr = (union lpfc_sli4_cfg_shdr *)
12350 &mbox->u.mqe.un.mq_destroy.header.cfg_shdr;
12351 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12352 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12353 if (shdr_status || shdr_add_status || rc) {
12354 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12355 "2507 MQ_DESTROY mailbox failed with "
12356 "status x%x add_status x%x, mbx status x%x\n",
12357 shdr_status, shdr_add_status, rc);
12358 status = -ENXIO;
12359 }
12360 /* Remove mq from any list */
12361 list_del_init(&mq->list);
James Smart8fa38512009-07-19 10:01:03 -040012362 mempool_free(mbox, mq->phba->mbox_mem_pool);
James Smart04c68492009-05-22 14:52:52 -040012363 return status;
12364}
12365
12366/**
James Smart4f774512009-05-22 14:52:35 -040012367 * lpfc_wq_destroy - Destroy a Work Queue on the HBA
12368 * @wq: The queue structure associated with the queue to destroy.
12369 *
12370 * This function destroys a queue, as detailed in @wq by sending an mailbox
12371 * command, specific to the type of queue, to the HBA.
12372 *
12373 * The @wq struct is used to get the queue ID of the queue to destroy.
12374 *
12375 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040012376 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040012377 **/
12378uint32_t
12379lpfc_wq_destroy(struct lpfc_hba *phba, struct lpfc_queue *wq)
12380{
12381 LPFC_MBOXQ_t *mbox;
12382 int rc, length, status = 0;
12383 uint32_t shdr_status, shdr_add_status;
12384 union lpfc_sli4_cfg_shdr *shdr;
12385
12386 if (!wq)
12387 return -ENODEV;
12388 mbox = mempool_alloc(wq->phba->mbox_mem_pool, GFP_KERNEL);
12389 if (!mbox)
12390 return -ENOMEM;
12391 length = (sizeof(struct lpfc_mbx_wq_destroy) -
12392 sizeof(struct lpfc_sli4_cfg_mhdr));
12393 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
12394 LPFC_MBOX_OPCODE_FCOE_WQ_DESTROY,
12395 length, LPFC_SLI4_MBX_EMBED);
12396 bf_set(lpfc_mbx_wq_destroy_q_id, &mbox->u.mqe.un.wq_destroy.u.request,
12397 wq->queue_id);
12398 mbox->vport = wq->phba->pport;
12399 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
12400 rc = lpfc_sli_issue_mbox(wq->phba, mbox, MBX_POLL);
12401 shdr = (union lpfc_sli4_cfg_shdr *)
12402 &mbox->u.mqe.un.wq_destroy.header.cfg_shdr;
12403 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12404 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12405 if (shdr_status || shdr_add_status || rc) {
12406 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12407 "2508 WQ_DESTROY mailbox failed with "
12408 "status x%x add_status x%x, mbx status x%x\n",
12409 shdr_status, shdr_add_status, rc);
12410 status = -ENXIO;
12411 }
12412 /* Remove wq from any list */
12413 list_del_init(&wq->list);
James Smart8fa38512009-07-19 10:01:03 -040012414 mempool_free(mbox, wq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040012415 return status;
12416}
12417
12418/**
12419 * lpfc_rq_destroy - Destroy a Receive Queue on the HBA
12420 * @rq: The queue structure associated with the queue to destroy.
12421 *
12422 * This function destroys a queue, as detailed in @rq by sending an mailbox
12423 * command, specific to the type of queue, to the HBA.
12424 *
12425 * The @rq struct is used to get the queue ID of the queue to destroy.
12426 *
12427 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040012428 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040012429 **/
12430uint32_t
12431lpfc_rq_destroy(struct lpfc_hba *phba, struct lpfc_queue *hrq,
12432 struct lpfc_queue *drq)
12433{
12434 LPFC_MBOXQ_t *mbox;
12435 int rc, length, status = 0;
12436 uint32_t shdr_status, shdr_add_status;
12437 union lpfc_sli4_cfg_shdr *shdr;
12438
12439 if (!hrq || !drq)
12440 return -ENODEV;
12441 mbox = mempool_alloc(hrq->phba->mbox_mem_pool, GFP_KERNEL);
12442 if (!mbox)
12443 return -ENOMEM;
12444 length = (sizeof(struct lpfc_mbx_rq_destroy) -
James Smartfedd3b72011-02-16 12:39:24 -050012445 sizeof(struct lpfc_sli4_cfg_mhdr));
James Smart4f774512009-05-22 14:52:35 -040012446 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
12447 LPFC_MBOX_OPCODE_FCOE_RQ_DESTROY,
12448 length, LPFC_SLI4_MBX_EMBED);
12449 bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
12450 hrq->queue_id);
12451 mbox->vport = hrq->phba->pport;
12452 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
12453 rc = lpfc_sli_issue_mbox(hrq->phba, mbox, MBX_POLL);
12454 /* The IOCTL status is embedded in the mailbox subheader. */
12455 shdr = (union lpfc_sli4_cfg_shdr *)
12456 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
12457 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12458 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12459 if (shdr_status || shdr_add_status || rc) {
12460 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12461 "2509 RQ_DESTROY mailbox failed with "
12462 "status x%x add_status x%x, mbx status x%x\n",
12463 shdr_status, shdr_add_status, rc);
12464 if (rc != MBX_TIMEOUT)
12465 mempool_free(mbox, hrq->phba->mbox_mem_pool);
12466 return -ENXIO;
12467 }
12468 bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
12469 drq->queue_id);
12470 rc = lpfc_sli_issue_mbox(drq->phba, mbox, MBX_POLL);
12471 shdr = (union lpfc_sli4_cfg_shdr *)
12472 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
12473 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12474 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12475 if (shdr_status || shdr_add_status || rc) {
12476 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12477 "2510 RQ_DESTROY mailbox failed with "
12478 "status x%x add_status x%x, mbx status x%x\n",
12479 shdr_status, shdr_add_status, rc);
12480 status = -ENXIO;
12481 }
12482 list_del_init(&hrq->list);
12483 list_del_init(&drq->list);
James Smart8fa38512009-07-19 10:01:03 -040012484 mempool_free(mbox, hrq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040012485 return status;
12486}
12487
12488/**
12489 * lpfc_sli4_post_sgl - Post scatter gather list for an XRI to HBA
12490 * @phba: The virtual port for which this call being executed.
12491 * @pdma_phys_addr0: Physical address of the 1st SGL page.
12492 * @pdma_phys_addr1: Physical address of the 2nd SGL page.
12493 * @xritag: the xritag that ties this io to the SGL pages.
12494 *
12495 * This routine will post the sgl pages for the IO that has the xritag
12496 * that is in the iocbq structure. The xritag is assigned during iocbq
12497 * creation and persists for as long as the driver is loaded.
12498 * if the caller has fewer than 256 scatter gather segments to map then
12499 * pdma_phys_addr1 should be 0.
12500 * If the caller needs to map more than 256 scatter gather segment then
12501 * pdma_phys_addr1 should be a valid physical address.
12502 * physical address for SGLs must be 64 byte aligned.
12503 * If you are going to map 2 SGL's then the first one must have 256 entries
12504 * the second sgl can have between 1 and 256 entries.
12505 *
12506 * Return codes:
12507 * 0 - Success
12508 * -ENXIO, -ENOMEM - Failure
12509 **/
12510int
12511lpfc_sli4_post_sgl(struct lpfc_hba *phba,
12512 dma_addr_t pdma_phys_addr0,
12513 dma_addr_t pdma_phys_addr1,
12514 uint16_t xritag)
12515{
12516 struct lpfc_mbx_post_sgl_pages *post_sgl_pages;
12517 LPFC_MBOXQ_t *mbox;
12518 int rc;
12519 uint32_t shdr_status, shdr_add_status;
James Smart6d368e52011-05-24 11:44:12 -040012520 uint32_t mbox_tmo;
James Smart4f774512009-05-22 14:52:35 -040012521 union lpfc_sli4_cfg_shdr *shdr;
12522
12523 if (xritag == NO_XRI) {
12524 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12525 "0364 Invalid param:\n");
12526 return -EINVAL;
12527 }
12528
12529 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12530 if (!mbox)
12531 return -ENOMEM;
12532
12533 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
12534 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES,
12535 sizeof(struct lpfc_mbx_post_sgl_pages) -
James Smartfedd3b72011-02-16 12:39:24 -050012536 sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
James Smart4f774512009-05-22 14:52:35 -040012537
12538 post_sgl_pages = (struct lpfc_mbx_post_sgl_pages *)
12539 &mbox->u.mqe.un.post_sgl_pages;
12540 bf_set(lpfc_post_sgl_pages_xri, post_sgl_pages, xritag);
12541 bf_set(lpfc_post_sgl_pages_xricnt, post_sgl_pages, 1);
12542
12543 post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_lo =
12544 cpu_to_le32(putPaddrLow(pdma_phys_addr0));
12545 post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_hi =
12546 cpu_to_le32(putPaddrHigh(pdma_phys_addr0));
12547
12548 post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_lo =
12549 cpu_to_le32(putPaddrLow(pdma_phys_addr1));
12550 post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_hi =
12551 cpu_to_le32(putPaddrHigh(pdma_phys_addr1));
12552 if (!phba->sli4_hba.intr_enable)
12553 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
James Smart6d368e52011-05-24 11:44:12 -040012554 else {
James Smarta183a152011-10-10 21:32:43 -040012555 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart6d368e52011-05-24 11:44:12 -040012556 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
12557 }
James Smart4f774512009-05-22 14:52:35 -040012558 /* The IOCTL status is embedded in the mailbox subheader. */
12559 shdr = (union lpfc_sli4_cfg_shdr *) &post_sgl_pages->header.cfg_shdr;
12560 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12561 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12562 if (rc != MBX_TIMEOUT)
12563 mempool_free(mbox, phba->mbox_mem_pool);
12564 if (shdr_status || shdr_add_status || rc) {
12565 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12566 "2511 POST_SGL mailbox failed with "
12567 "status x%x add_status x%x, mbx status x%x\n",
12568 shdr_status, shdr_add_status, rc);
12569 rc = -ENXIO;
12570 }
12571 return 0;
12572}
James Smart4f774512009-05-22 14:52:35 -040012573
12574/**
James Smart88a2cfb2011-07-22 18:36:33 -040012575 * lpfc_sli4_alloc_xri - Get an available rpi in the device's range
James Smart6d368e52011-05-24 11:44:12 -040012576 * @phba: pointer to lpfc hba data structure.
12577 *
12578 * This routine is invoked to post rpi header templates to the
James Smart88a2cfb2011-07-22 18:36:33 -040012579 * HBA consistent with the SLI-4 interface spec. This routine
12580 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
12581 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
James Smart6d368e52011-05-24 11:44:12 -040012582 *
James Smart88a2cfb2011-07-22 18:36:33 -040012583 * Returns
12584 * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
12585 * LPFC_RPI_ALLOC_ERROR if no rpis are available.
12586 **/
James Smart6d368e52011-05-24 11:44:12 -040012587uint16_t
12588lpfc_sli4_alloc_xri(struct lpfc_hba *phba)
12589{
12590 unsigned long xri;
12591
12592 /*
12593 * Fetch the next logical xri. Because this index is logical,
12594 * the driver starts at 0 each time.
12595 */
12596 spin_lock_irq(&phba->hbalock);
12597 xri = find_next_zero_bit(phba->sli4_hba.xri_bmask,
12598 phba->sli4_hba.max_cfg_param.max_xri, 0);
12599 if (xri >= phba->sli4_hba.max_cfg_param.max_xri) {
12600 spin_unlock_irq(&phba->hbalock);
12601 return NO_XRI;
12602 } else {
12603 set_bit(xri, phba->sli4_hba.xri_bmask);
12604 phba->sli4_hba.max_cfg_param.xri_used++;
12605 phba->sli4_hba.xri_count++;
12606 }
12607
12608 spin_unlock_irq(&phba->hbalock);
12609 return xri;
12610}
12611
12612/**
12613 * lpfc_sli4_free_xri - Release an xri for reuse.
12614 * @phba: pointer to lpfc hba data structure.
12615 *
12616 * This routine is invoked to release an xri to the pool of
12617 * available rpis maintained by the driver.
12618 **/
12619void
12620__lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
12621{
12622 if (test_and_clear_bit(xri, phba->sli4_hba.xri_bmask)) {
12623 phba->sli4_hba.xri_count--;
12624 phba->sli4_hba.max_cfg_param.xri_used--;
12625 }
12626}
12627
12628/**
12629 * lpfc_sli4_free_xri - Release an xri for reuse.
12630 * @phba: pointer to lpfc hba data structure.
12631 *
12632 * This routine is invoked to release an xri to the pool of
12633 * available rpis maintained by the driver.
12634 **/
12635void
12636lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
12637{
12638 spin_lock_irq(&phba->hbalock);
12639 __lpfc_sli4_free_xri(phba, xri);
12640 spin_unlock_irq(&phba->hbalock);
12641}
12642
12643/**
James Smart4f774512009-05-22 14:52:35 -040012644 * lpfc_sli4_next_xritag - Get an xritag for the io
12645 * @phba: Pointer to HBA context object.
12646 *
12647 * This function gets an xritag for the iocb. If there is no unused xritag
12648 * it will return 0xffff.
12649 * The function returns the allocated xritag if successful, else returns zero.
12650 * Zero is not a valid xritag.
12651 * The caller is not required to hold any lock.
12652 **/
12653uint16_t
12654lpfc_sli4_next_xritag(struct lpfc_hba *phba)
12655{
James Smart6d368e52011-05-24 11:44:12 -040012656 uint16_t xri_index;
James Smart4f774512009-05-22 14:52:35 -040012657
James Smart6d368e52011-05-24 11:44:12 -040012658 xri_index = lpfc_sli4_alloc_xri(phba);
12659 if (xri_index != NO_XRI)
12660 return xri_index;
12661
12662 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart4f774512009-05-22 14:52:35 -040012663 "2004 Failed to allocate XRI.last XRITAG is %d"
12664 " Max XRI is %d, Used XRI is %d\n",
James Smart6d368e52011-05-24 11:44:12 -040012665 xri_index,
James Smart4f774512009-05-22 14:52:35 -040012666 phba->sli4_hba.max_cfg_param.max_xri,
12667 phba->sli4_hba.max_cfg_param.xri_used);
James Smart6d368e52011-05-24 11:44:12 -040012668 return NO_XRI;
James Smart4f774512009-05-22 14:52:35 -040012669}
12670
12671/**
James Smart6d368e52011-05-24 11:44:12 -040012672 * lpfc_sli4_post_els_sgl_list - post a block of ELS sgls to the port.
James Smart4f774512009-05-22 14:52:35 -040012673 * @phba: pointer to lpfc hba data structure.
12674 *
12675 * This routine is invoked to post a block of driver's sgl pages to the
12676 * HBA using non-embedded mailbox command. No Lock is held. This routine
12677 * is only called when the driver is loading and after all IO has been
12678 * stopped.
12679 **/
12680int
James Smart6d368e52011-05-24 11:44:12 -040012681lpfc_sli4_post_els_sgl_list(struct lpfc_hba *phba)
James Smart4f774512009-05-22 14:52:35 -040012682{
12683 struct lpfc_sglq *sglq_entry;
12684 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
12685 struct sgl_page_pairs *sgl_pg_pairs;
12686 void *viraddr;
12687 LPFC_MBOXQ_t *mbox;
12688 uint32_t reqlen, alloclen, pg_pairs;
12689 uint32_t mbox_tmo;
James Smart6d368e52011-05-24 11:44:12 -040012690 uint16_t xritag_start = 0, lxri = 0;
James Smart4f774512009-05-22 14:52:35 -040012691 int els_xri_cnt, rc = 0;
12692 uint32_t shdr_status, shdr_add_status;
12693 union lpfc_sli4_cfg_shdr *shdr;
12694
12695 /* The number of sgls to be posted */
12696 els_xri_cnt = lpfc_sli4_get_els_iocb_cnt(phba);
12697
12698 reqlen = els_xri_cnt * sizeof(struct sgl_page_pairs) +
12699 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
James Smart49198b32010-04-06 15:04:33 -040012700 if (reqlen > SLI4_PAGE_SIZE) {
James Smart4f774512009-05-22 14:52:35 -040012701 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
12702 "2559 Block sgl registration required DMA "
12703 "size (%d) great than a page\n", reqlen);
12704 return -ENOMEM;
12705 }
12706 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
James Smart6d368e52011-05-24 11:44:12 -040012707 if (!mbox)
James Smart4f774512009-05-22 14:52:35 -040012708 return -ENOMEM;
James Smart4f774512009-05-22 14:52:35 -040012709
12710 /* Allocate DMA memory and set up the non-embedded mailbox command */
12711 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
12712 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
12713 LPFC_SLI4_MBX_NEMBED);
12714
12715 if (alloclen < reqlen) {
12716 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12717 "0285 Allocated DMA memory size (%d) is "
12718 "less than the requested DMA memory "
12719 "size (%d)\n", alloclen, reqlen);
12720 lpfc_sli4_mbox_cmd_free(phba, mbox);
12721 return -ENOMEM;
12722 }
James Smart4f774512009-05-22 14:52:35 -040012723 /* Set up the SGL pages in the non-embedded DMA pages */
James Smart6d368e52011-05-24 11:44:12 -040012724 viraddr = mbox->sge_array->addr[0];
James Smart4f774512009-05-22 14:52:35 -040012725 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
12726 sgl_pg_pairs = &sgl->sgl_pg_pairs;
12727
12728 for (pg_pairs = 0; pg_pairs < els_xri_cnt; pg_pairs++) {
12729 sglq_entry = phba->sli4_hba.lpfc_els_sgl_array[pg_pairs];
James Smart6d368e52011-05-24 11:44:12 -040012730
12731 /*
12732 * Assign the sglq a physical xri only if the driver has not
12733 * initialized those resources. A port reset only needs
12734 * the sglq's posted.
12735 */
12736 if (bf_get(lpfc_xri_rsrc_rdy, &phba->sli4_hba.sli4_flags) !=
12737 LPFC_XRI_RSRC_RDY) {
12738 lxri = lpfc_sli4_next_xritag(phba);
12739 if (lxri == NO_XRI) {
12740 lpfc_sli4_mbox_cmd_free(phba, mbox);
12741 return -ENOMEM;
12742 }
12743 sglq_entry->sli4_lxritag = lxri;
12744 sglq_entry->sli4_xritag = phba->sli4_hba.xri_ids[lxri];
12745 }
12746
James Smart4f774512009-05-22 14:52:35 -040012747 /* Set up the sge entry */
12748 sgl_pg_pairs->sgl_pg0_addr_lo =
12749 cpu_to_le32(putPaddrLow(sglq_entry->phys));
12750 sgl_pg_pairs->sgl_pg0_addr_hi =
12751 cpu_to_le32(putPaddrHigh(sglq_entry->phys));
12752 sgl_pg_pairs->sgl_pg1_addr_lo =
12753 cpu_to_le32(putPaddrLow(0));
12754 sgl_pg_pairs->sgl_pg1_addr_hi =
12755 cpu_to_le32(putPaddrHigh(0));
James Smart6d368e52011-05-24 11:44:12 -040012756
James Smart4f774512009-05-22 14:52:35 -040012757 /* Keep the first xritag on the list */
12758 if (pg_pairs == 0)
12759 xritag_start = sglq_entry->sli4_xritag;
12760 sgl_pg_pairs++;
12761 }
James Smart6d368e52011-05-24 11:44:12 -040012762
12763 /* Complete initialization and perform endian conversion. */
James Smart4f774512009-05-22 14:52:35 -040012764 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
James Smart6a9c52c2009-10-02 15:16:51 -040012765 bf_set(lpfc_post_sgl_pages_xricnt, sgl, els_xri_cnt);
James Smart4f774512009-05-22 14:52:35 -040012766 sgl->word0 = cpu_to_le32(sgl->word0);
James Smart4f774512009-05-22 14:52:35 -040012767 if (!phba->sli4_hba.intr_enable)
12768 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
12769 else {
James Smarta183a152011-10-10 21:32:43 -040012770 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart4f774512009-05-22 14:52:35 -040012771 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
12772 }
12773 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
12774 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12775 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12776 if (rc != MBX_TIMEOUT)
12777 lpfc_sli4_mbox_cmd_free(phba, mbox);
12778 if (shdr_status || shdr_add_status || rc) {
12779 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12780 "2513 POST_SGL_BLOCK mailbox command failed "
12781 "status x%x add_status x%x mbx status x%x\n",
12782 shdr_status, shdr_add_status, rc);
12783 rc = -ENXIO;
12784 }
James Smart6d368e52011-05-24 11:44:12 -040012785
12786 if (rc == 0)
12787 bf_set(lpfc_xri_rsrc_rdy, &phba->sli4_hba.sli4_flags,
12788 LPFC_XRI_RSRC_RDY);
12789 return rc;
12790}
12791
12792/**
12793 * lpfc_sli4_post_els_sgl_list_ext - post a block of ELS sgls to the port.
12794 * @phba: pointer to lpfc hba data structure.
12795 *
12796 * This routine is invoked to post a block of driver's sgl pages to the
12797 * HBA using non-embedded mailbox command. No Lock is held. This routine
12798 * is only called when the driver is loading and after all IO has been
12799 * stopped.
12800 **/
12801int
12802lpfc_sli4_post_els_sgl_list_ext(struct lpfc_hba *phba)
12803{
12804 struct lpfc_sglq *sglq_entry;
12805 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
12806 struct sgl_page_pairs *sgl_pg_pairs;
12807 void *viraddr;
12808 LPFC_MBOXQ_t *mbox;
12809 uint32_t reqlen, alloclen, index;
12810 uint32_t mbox_tmo;
12811 uint16_t rsrc_start, rsrc_size, els_xri_cnt;
12812 uint16_t xritag_start = 0, lxri = 0;
12813 struct lpfc_rsrc_blks *rsrc_blk;
12814 int cnt, ttl_cnt, rc = 0;
12815 int loop_cnt;
12816 uint32_t shdr_status, shdr_add_status;
12817 union lpfc_sli4_cfg_shdr *shdr;
12818
12819 /* The number of sgls to be posted */
12820 els_xri_cnt = lpfc_sli4_get_els_iocb_cnt(phba);
12821
12822 reqlen = els_xri_cnt * sizeof(struct sgl_page_pairs) +
12823 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
12824 if (reqlen > SLI4_PAGE_SIZE) {
12825 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
12826 "2989 Block sgl registration required DMA "
12827 "size (%d) great than a page\n", reqlen);
12828 return -ENOMEM;
12829 }
12830
12831 cnt = 0;
12832 ttl_cnt = 0;
12833 list_for_each_entry(rsrc_blk, &phba->sli4_hba.lpfc_xri_blk_list,
12834 list) {
12835 rsrc_start = rsrc_blk->rsrc_start;
12836 rsrc_size = rsrc_blk->rsrc_size;
12837
12838 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
12839 "3014 Working ELS Extent start %d, cnt %d\n",
12840 rsrc_start, rsrc_size);
12841
12842 loop_cnt = min(els_xri_cnt, rsrc_size);
12843 if (ttl_cnt + loop_cnt >= els_xri_cnt) {
12844 loop_cnt = els_xri_cnt - ttl_cnt;
12845 ttl_cnt = els_xri_cnt;
12846 }
12847
12848 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12849 if (!mbox)
12850 return -ENOMEM;
12851 /*
12852 * Allocate DMA memory and set up the non-embedded mailbox
12853 * command.
12854 */
12855 alloclen = lpfc_sli4_config(phba, mbox,
12856 LPFC_MBOX_SUBSYSTEM_FCOE,
12857 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES,
12858 reqlen, LPFC_SLI4_MBX_NEMBED);
12859 if (alloclen < reqlen) {
12860 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12861 "2987 Allocated DMA memory size (%d) "
12862 "is less than the requested DMA memory "
12863 "size (%d)\n", alloclen, reqlen);
12864 lpfc_sli4_mbox_cmd_free(phba, mbox);
12865 return -ENOMEM;
12866 }
12867
12868 /* Set up the SGL pages in the non-embedded DMA pages */
12869 viraddr = mbox->sge_array->addr[0];
12870 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
12871 sgl_pg_pairs = &sgl->sgl_pg_pairs;
12872
12873 /*
12874 * The starting resource may not begin at zero. Control
12875 * the loop variants via the block resource parameters,
12876 * but handle the sge pointers with a zero-based index
12877 * that doesn't get reset per loop pass.
12878 */
12879 for (index = rsrc_start;
12880 index < rsrc_start + loop_cnt;
12881 index++) {
12882 sglq_entry = phba->sli4_hba.lpfc_els_sgl_array[cnt];
12883
12884 /*
12885 * Assign the sglq a physical xri only if the driver
12886 * has not initialized those resources. A port reset
12887 * only needs the sglq's posted.
12888 */
12889 if (bf_get(lpfc_xri_rsrc_rdy,
12890 &phba->sli4_hba.sli4_flags) !=
12891 LPFC_XRI_RSRC_RDY) {
12892 lxri = lpfc_sli4_next_xritag(phba);
12893 if (lxri == NO_XRI) {
12894 lpfc_sli4_mbox_cmd_free(phba, mbox);
12895 rc = -ENOMEM;
12896 goto err_exit;
12897 }
12898 sglq_entry->sli4_lxritag = lxri;
12899 sglq_entry->sli4_xritag =
12900 phba->sli4_hba.xri_ids[lxri];
12901 }
12902
12903 /* Set up the sge entry */
12904 sgl_pg_pairs->sgl_pg0_addr_lo =
12905 cpu_to_le32(putPaddrLow(sglq_entry->phys));
12906 sgl_pg_pairs->sgl_pg0_addr_hi =
12907 cpu_to_le32(putPaddrHigh(sglq_entry->phys));
12908 sgl_pg_pairs->sgl_pg1_addr_lo =
12909 cpu_to_le32(putPaddrLow(0));
12910 sgl_pg_pairs->sgl_pg1_addr_hi =
12911 cpu_to_le32(putPaddrHigh(0));
12912
12913 /* Track the starting physical XRI for the mailbox. */
12914 if (index == rsrc_start)
12915 xritag_start = sglq_entry->sli4_xritag;
12916 sgl_pg_pairs++;
12917 cnt++;
12918 }
12919
12920 /* Complete initialization and perform endian conversion. */
12921 rsrc_blk->rsrc_used += loop_cnt;
12922 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
12923 bf_set(lpfc_post_sgl_pages_xricnt, sgl, loop_cnt);
12924 sgl->word0 = cpu_to_le32(sgl->word0);
12925
12926 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
12927 "3015 Post ELS Extent SGL, start %d, "
12928 "cnt %d, used %d\n",
12929 xritag_start, loop_cnt, rsrc_blk->rsrc_used);
12930 if (!phba->sli4_hba.intr_enable)
12931 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
12932 else {
James Smarta183a152011-10-10 21:32:43 -040012933 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart6d368e52011-05-24 11:44:12 -040012934 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
12935 }
12936 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
12937 shdr_status = bf_get(lpfc_mbox_hdr_status,
12938 &shdr->response);
12939 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
12940 &shdr->response);
12941 if (rc != MBX_TIMEOUT)
12942 lpfc_sli4_mbox_cmd_free(phba, mbox);
12943 if (shdr_status || shdr_add_status || rc) {
12944 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12945 "2988 POST_SGL_BLOCK mailbox "
12946 "command failed status x%x "
12947 "add_status x%x mbx status x%x\n",
12948 shdr_status, shdr_add_status, rc);
12949 rc = -ENXIO;
12950 goto err_exit;
12951 }
12952 if (ttl_cnt >= els_xri_cnt)
12953 break;
12954 }
12955
12956 err_exit:
12957 if (rc == 0)
12958 bf_set(lpfc_xri_rsrc_rdy, &phba->sli4_hba.sli4_flags,
12959 LPFC_XRI_RSRC_RDY);
James Smart4f774512009-05-22 14:52:35 -040012960 return rc;
12961}
12962
12963/**
12964 * lpfc_sli4_post_scsi_sgl_block - post a block of scsi sgl list to firmware
12965 * @phba: pointer to lpfc hba data structure.
12966 * @sblist: pointer to scsi buffer list.
12967 * @count: number of scsi buffers on the list.
12968 *
12969 * This routine is invoked to post a block of @count scsi sgl pages from a
12970 * SCSI buffer list @sblist to the HBA using non-embedded mailbox command.
12971 * No Lock is held.
12972 *
12973 **/
12974int
12975lpfc_sli4_post_scsi_sgl_block(struct lpfc_hba *phba, struct list_head *sblist,
12976 int cnt)
12977{
12978 struct lpfc_scsi_buf *psb;
12979 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
12980 struct sgl_page_pairs *sgl_pg_pairs;
12981 void *viraddr;
12982 LPFC_MBOXQ_t *mbox;
12983 uint32_t reqlen, alloclen, pg_pairs;
12984 uint32_t mbox_tmo;
12985 uint16_t xritag_start = 0;
12986 int rc = 0;
12987 uint32_t shdr_status, shdr_add_status;
12988 dma_addr_t pdma_phys_bpl1;
12989 union lpfc_sli4_cfg_shdr *shdr;
12990
12991 /* Calculate the requested length of the dma memory */
12992 reqlen = cnt * sizeof(struct sgl_page_pairs) +
12993 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
James Smart49198b32010-04-06 15:04:33 -040012994 if (reqlen > SLI4_PAGE_SIZE) {
James Smart4f774512009-05-22 14:52:35 -040012995 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
12996 "0217 Block sgl registration required DMA "
12997 "size (%d) great than a page\n", reqlen);
12998 return -ENOMEM;
12999 }
13000 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
13001 if (!mbox) {
13002 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13003 "0283 Failed to allocate mbox cmd memory\n");
13004 return -ENOMEM;
13005 }
13006
13007 /* Allocate DMA memory and set up the non-embedded mailbox command */
13008 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
13009 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
13010 LPFC_SLI4_MBX_NEMBED);
13011
13012 if (alloclen < reqlen) {
13013 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13014 "2561 Allocated DMA memory size (%d) is "
13015 "less than the requested DMA memory "
13016 "size (%d)\n", alloclen, reqlen);
13017 lpfc_sli4_mbox_cmd_free(phba, mbox);
13018 return -ENOMEM;
13019 }
James Smart6d368e52011-05-24 11:44:12 -040013020
James Smart4f774512009-05-22 14:52:35 -040013021 /* Get the first SGE entry from the non-embedded DMA memory */
James Smart4f774512009-05-22 14:52:35 -040013022 viraddr = mbox->sge_array->addr[0];
13023
13024 /* Set up the SGL pages in the non-embedded DMA pages */
13025 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
13026 sgl_pg_pairs = &sgl->sgl_pg_pairs;
13027
13028 pg_pairs = 0;
13029 list_for_each_entry(psb, sblist, list) {
13030 /* Set up the sge entry */
13031 sgl_pg_pairs->sgl_pg0_addr_lo =
13032 cpu_to_le32(putPaddrLow(psb->dma_phys_bpl));
13033 sgl_pg_pairs->sgl_pg0_addr_hi =
13034 cpu_to_le32(putPaddrHigh(psb->dma_phys_bpl));
13035 if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
13036 pdma_phys_bpl1 = psb->dma_phys_bpl + SGL_PAGE_SIZE;
13037 else
13038 pdma_phys_bpl1 = 0;
13039 sgl_pg_pairs->sgl_pg1_addr_lo =
13040 cpu_to_le32(putPaddrLow(pdma_phys_bpl1));
13041 sgl_pg_pairs->sgl_pg1_addr_hi =
13042 cpu_to_le32(putPaddrHigh(pdma_phys_bpl1));
13043 /* Keep the first xritag on the list */
13044 if (pg_pairs == 0)
13045 xritag_start = psb->cur_iocbq.sli4_xritag;
13046 sgl_pg_pairs++;
13047 pg_pairs++;
13048 }
13049 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
13050 bf_set(lpfc_post_sgl_pages_xricnt, sgl, pg_pairs);
13051 /* Perform endian conversion if necessary */
13052 sgl->word0 = cpu_to_le32(sgl->word0);
13053
13054 if (!phba->sli4_hba.intr_enable)
13055 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
13056 else {
James Smarta183a152011-10-10 21:32:43 -040013057 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart4f774512009-05-22 14:52:35 -040013058 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
13059 }
13060 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
13061 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13062 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13063 if (rc != MBX_TIMEOUT)
13064 lpfc_sli4_mbox_cmd_free(phba, mbox);
13065 if (shdr_status || shdr_add_status || rc) {
13066 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13067 "2564 POST_SGL_BLOCK mailbox command failed "
13068 "status x%x add_status x%x mbx status x%x\n",
13069 shdr_status, shdr_add_status, rc);
13070 rc = -ENXIO;
13071 }
13072 return rc;
13073}
13074
13075/**
James Smart6d368e52011-05-24 11:44:12 -040013076 * lpfc_sli4_post_scsi_sgl_blk_ext - post a block of scsi sgls to the port.
13077 * @phba: pointer to lpfc hba data structure.
13078 * @sblist: pointer to scsi buffer list.
13079 * @count: number of scsi buffers on the list.
13080 *
13081 * This routine is invoked to post a block of @count scsi sgl pages from a
13082 * SCSI buffer list @sblist to the HBA using non-embedded mailbox command.
13083 * No Lock is held.
13084 *
13085 **/
13086int
13087lpfc_sli4_post_scsi_sgl_blk_ext(struct lpfc_hba *phba, struct list_head *sblist,
13088 int cnt)
13089{
13090 struct lpfc_scsi_buf *psb = NULL;
13091 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
13092 struct sgl_page_pairs *sgl_pg_pairs;
13093 void *viraddr;
13094 LPFC_MBOXQ_t *mbox;
13095 uint32_t reqlen, alloclen, pg_pairs;
13096 uint32_t mbox_tmo;
13097 uint16_t xri_start = 0, scsi_xri_start;
13098 uint16_t rsrc_range;
13099 int rc = 0, avail_cnt;
13100 uint32_t shdr_status, shdr_add_status;
13101 dma_addr_t pdma_phys_bpl1;
13102 union lpfc_sli4_cfg_shdr *shdr;
13103 struct lpfc_rsrc_blks *rsrc_blk;
13104 uint32_t xri_cnt = 0;
13105
13106 /* Calculate the total requested length of the dma memory */
13107 reqlen = cnt * sizeof(struct sgl_page_pairs) +
13108 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
13109 if (reqlen > SLI4_PAGE_SIZE) {
13110 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
13111 "2932 Block sgl registration required DMA "
13112 "size (%d) great than a page\n", reqlen);
13113 return -ENOMEM;
13114 }
13115
13116 /*
13117 * The use of extents requires the driver to post the sgl headers
13118 * in multiple postings to meet the contiguous resource assignment.
13119 */
13120 psb = list_prepare_entry(psb, sblist, list);
13121 scsi_xri_start = phba->sli4_hba.scsi_xri_start;
13122 list_for_each_entry(rsrc_blk, &phba->sli4_hba.lpfc_xri_blk_list,
13123 list) {
13124 rsrc_range = rsrc_blk->rsrc_start + rsrc_blk->rsrc_size;
13125 if (rsrc_range < scsi_xri_start)
13126 continue;
13127 else if (rsrc_blk->rsrc_used >= rsrc_blk->rsrc_size)
13128 continue;
13129 else
13130 avail_cnt = rsrc_blk->rsrc_size - rsrc_blk->rsrc_used;
13131
13132 reqlen = (avail_cnt * sizeof(struct sgl_page_pairs)) +
13133 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
13134 /*
13135 * Allocate DMA memory and set up the non-embedded mailbox
13136 * command. The mbox is used to post an SGL page per loop
13137 * but the DMA memory has a use-once semantic so the mailbox
13138 * is used and freed per loop pass.
13139 */
13140 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
13141 if (!mbox) {
13142 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13143 "2933 Failed to allocate mbox cmd "
13144 "memory\n");
13145 return -ENOMEM;
13146 }
13147 alloclen = lpfc_sli4_config(phba, mbox,
13148 LPFC_MBOX_SUBSYSTEM_FCOE,
13149 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES,
13150 reqlen,
13151 LPFC_SLI4_MBX_NEMBED);
13152 if (alloclen < reqlen) {
13153 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13154 "2934 Allocated DMA memory size (%d) "
13155 "is less than the requested DMA memory "
13156 "size (%d)\n", alloclen, reqlen);
13157 lpfc_sli4_mbox_cmd_free(phba, mbox);
13158 return -ENOMEM;
13159 }
13160
13161 /* Get the first SGE entry from the non-embedded DMA memory */
13162 viraddr = mbox->sge_array->addr[0];
13163
13164 /* Set up the SGL pages in the non-embedded DMA pages */
13165 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
13166 sgl_pg_pairs = &sgl->sgl_pg_pairs;
13167
13168 /* pg_pairs tracks posted SGEs per loop iteration. */
13169 pg_pairs = 0;
13170 list_for_each_entry_continue(psb, sblist, list) {
13171 /* Set up the sge entry */
13172 sgl_pg_pairs->sgl_pg0_addr_lo =
13173 cpu_to_le32(putPaddrLow(psb->dma_phys_bpl));
13174 sgl_pg_pairs->sgl_pg0_addr_hi =
13175 cpu_to_le32(putPaddrHigh(psb->dma_phys_bpl));
13176 if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
13177 pdma_phys_bpl1 = psb->dma_phys_bpl +
13178 SGL_PAGE_SIZE;
13179 else
13180 pdma_phys_bpl1 = 0;
13181 sgl_pg_pairs->sgl_pg1_addr_lo =
13182 cpu_to_le32(putPaddrLow(pdma_phys_bpl1));
13183 sgl_pg_pairs->sgl_pg1_addr_hi =
13184 cpu_to_le32(putPaddrHigh(pdma_phys_bpl1));
13185 /* Keep the first xri for this extent. */
13186 if (pg_pairs == 0)
13187 xri_start = psb->cur_iocbq.sli4_xritag;
13188 sgl_pg_pairs++;
13189 pg_pairs++;
13190 xri_cnt++;
13191
13192 /*
13193 * Track two exit conditions - the loop has constructed
13194 * all of the caller's SGE pairs or all available
13195 * resource IDs in this extent are consumed.
13196 */
13197 if ((xri_cnt == cnt) || (pg_pairs >= avail_cnt))
13198 break;
13199 }
13200 rsrc_blk->rsrc_used += pg_pairs;
13201 bf_set(lpfc_post_sgl_pages_xri, sgl, xri_start);
13202 bf_set(lpfc_post_sgl_pages_xricnt, sgl, pg_pairs);
13203
13204 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
13205 "3016 Post SCSI Extent SGL, start %d, cnt %d "
13206 "blk use %d\n",
13207 xri_start, pg_pairs, rsrc_blk->rsrc_used);
13208 /* Perform endian conversion if necessary */
13209 sgl->word0 = cpu_to_le32(sgl->word0);
13210 if (!phba->sli4_hba.intr_enable)
13211 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
13212 else {
James Smarta183a152011-10-10 21:32:43 -040013213 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart6d368e52011-05-24 11:44:12 -040013214 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
13215 }
13216 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
13217 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13218 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
13219 &shdr->response);
13220 if (rc != MBX_TIMEOUT)
13221 lpfc_sli4_mbox_cmd_free(phba, mbox);
13222 if (shdr_status || shdr_add_status || rc) {
13223 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13224 "2935 POST_SGL_BLOCK mailbox command "
13225 "failed status x%x add_status x%x "
13226 "mbx status x%x\n",
13227 shdr_status, shdr_add_status, rc);
13228 return -ENXIO;
13229 }
13230
13231 /* Post only what is requested. */
13232 if (xri_cnt >= cnt)
13233 break;
13234 }
13235 return rc;
13236}
13237
13238/**
James Smart4f774512009-05-22 14:52:35 -040013239 * lpfc_fc_frame_check - Check that this frame is a valid frame to handle
13240 * @phba: pointer to lpfc_hba struct that the frame was received on
13241 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
13242 *
13243 * This function checks the fields in the @fc_hdr to see if the FC frame is a
13244 * valid type of frame that the LPFC driver will handle. This function will
13245 * return a zero if the frame is a valid frame or a non zero value when the
13246 * frame does not pass the check.
13247 **/
13248static int
13249lpfc_fc_frame_check(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr)
13250{
Tomas Henzl474ffb72010-12-22 16:52:40 +010013251 /* make rctl_names static to save stack space */
13252 static char *rctl_names[] = FC_RCTL_NAMES_INIT;
James Smart4f774512009-05-22 14:52:35 -040013253 char *type_names[] = FC_TYPE_NAMES_INIT;
13254 struct fc_vft_header *fc_vft_hdr;
James Smart546fc852011-03-11 16:06:29 -050013255 uint32_t *header = (uint32_t *) fc_hdr;
James Smart4f774512009-05-22 14:52:35 -040013256
13257 switch (fc_hdr->fh_r_ctl) {
13258 case FC_RCTL_DD_UNCAT: /* uncategorized information */
13259 case FC_RCTL_DD_SOL_DATA: /* solicited data */
13260 case FC_RCTL_DD_UNSOL_CTL: /* unsolicited control */
13261 case FC_RCTL_DD_SOL_CTL: /* solicited control or reply */
13262 case FC_RCTL_DD_UNSOL_DATA: /* unsolicited data */
13263 case FC_RCTL_DD_DATA_DESC: /* data descriptor */
13264 case FC_RCTL_DD_UNSOL_CMD: /* unsolicited command */
13265 case FC_RCTL_DD_CMD_STATUS: /* command status */
13266 case FC_RCTL_ELS_REQ: /* extended link services request */
13267 case FC_RCTL_ELS_REP: /* extended link services reply */
13268 case FC_RCTL_ELS4_REQ: /* FC-4 ELS request */
13269 case FC_RCTL_ELS4_REP: /* FC-4 ELS reply */
13270 case FC_RCTL_BA_NOP: /* basic link service NOP */
13271 case FC_RCTL_BA_ABTS: /* basic link service abort */
13272 case FC_RCTL_BA_RMC: /* remove connection */
13273 case FC_RCTL_BA_ACC: /* basic accept */
13274 case FC_RCTL_BA_RJT: /* basic reject */
13275 case FC_RCTL_BA_PRMT:
13276 case FC_RCTL_ACK_1: /* acknowledge_1 */
13277 case FC_RCTL_ACK_0: /* acknowledge_0 */
13278 case FC_RCTL_P_RJT: /* port reject */
13279 case FC_RCTL_F_RJT: /* fabric reject */
13280 case FC_RCTL_P_BSY: /* port busy */
13281 case FC_RCTL_F_BSY: /* fabric busy to data frame */
13282 case FC_RCTL_F_BSYL: /* fabric busy to link control frame */
13283 case FC_RCTL_LCR: /* link credit reset */
13284 case FC_RCTL_END: /* end */
13285 break;
13286 case FC_RCTL_VFTH: /* Virtual Fabric tagging Header */
13287 fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
13288 fc_hdr = &((struct fc_frame_header *)fc_vft_hdr)[1];
13289 return lpfc_fc_frame_check(phba, fc_hdr);
13290 default:
13291 goto drop;
13292 }
13293 switch (fc_hdr->fh_type) {
13294 case FC_TYPE_BLS:
13295 case FC_TYPE_ELS:
13296 case FC_TYPE_FCP:
13297 case FC_TYPE_CT:
13298 break;
13299 case FC_TYPE_IP:
13300 case FC_TYPE_ILS:
13301 default:
13302 goto drop;
13303 }
James Smart546fc852011-03-11 16:06:29 -050013304
James Smart4f774512009-05-22 14:52:35 -040013305 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
James Smart546fc852011-03-11 16:06:29 -050013306 "2538 Received frame rctl:%s type:%s "
13307 "Frame Data:%08x %08x %08x %08x %08x %08x\n",
James Smart4f774512009-05-22 14:52:35 -040013308 rctl_names[fc_hdr->fh_r_ctl],
James Smart546fc852011-03-11 16:06:29 -050013309 type_names[fc_hdr->fh_type],
13310 be32_to_cpu(header[0]), be32_to_cpu(header[1]),
13311 be32_to_cpu(header[2]), be32_to_cpu(header[3]),
13312 be32_to_cpu(header[4]), be32_to_cpu(header[5]));
James Smart4f774512009-05-22 14:52:35 -040013313 return 0;
13314drop:
13315 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
13316 "2539 Dropped frame rctl:%s type:%s\n",
13317 rctl_names[fc_hdr->fh_r_ctl],
13318 type_names[fc_hdr->fh_type]);
13319 return 1;
13320}
13321
13322/**
13323 * lpfc_fc_hdr_get_vfi - Get the VFI from an FC frame
13324 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
13325 *
13326 * This function processes the FC header to retrieve the VFI from the VF
13327 * header, if one exists. This function will return the VFI if one exists
13328 * or 0 if no VSAN Header exists.
13329 **/
13330static uint32_t
13331lpfc_fc_hdr_get_vfi(struct fc_frame_header *fc_hdr)
13332{
13333 struct fc_vft_header *fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
13334
13335 if (fc_hdr->fh_r_ctl != FC_RCTL_VFTH)
13336 return 0;
13337 return bf_get(fc_vft_hdr_vf_id, fc_vft_hdr);
13338}
13339
13340/**
13341 * lpfc_fc_frame_to_vport - Finds the vport that a frame is destined to
13342 * @phba: Pointer to the HBA structure to search for the vport on
13343 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
13344 * @fcfi: The FC Fabric ID that the frame came from
13345 *
13346 * This function searches the @phba for a vport that matches the content of the
13347 * @fc_hdr passed in and the @fcfi. This function uses the @fc_hdr to fetch the
13348 * VFI, if the Virtual Fabric Tagging Header exists, and the DID. This function
13349 * returns the matching vport pointer or NULL if unable to match frame to a
13350 * vport.
13351 **/
13352static struct lpfc_vport *
13353lpfc_fc_frame_to_vport(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr,
13354 uint16_t fcfi)
13355{
13356 struct lpfc_vport **vports;
13357 struct lpfc_vport *vport = NULL;
13358 int i;
13359 uint32_t did = (fc_hdr->fh_d_id[0] << 16 |
13360 fc_hdr->fh_d_id[1] << 8 |
13361 fc_hdr->fh_d_id[2]);
James Smartbf086112011-08-21 21:48:13 -040013362 if (did == Fabric_DID)
13363 return phba->pport;
James Smart4f774512009-05-22 14:52:35 -040013364 vports = lpfc_create_vport_work_array(phba);
13365 if (vports != NULL)
13366 for (i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
13367 if (phba->fcf.fcfi == fcfi &&
13368 vports[i]->vfi == lpfc_fc_hdr_get_vfi(fc_hdr) &&
13369 vports[i]->fc_myDID == did) {
13370 vport = vports[i];
13371 break;
13372 }
13373 }
13374 lpfc_destroy_vport_work_array(phba, vports);
13375 return vport;
13376}
13377
13378/**
James Smart45ed1192009-10-02 15:17:02 -040013379 * lpfc_update_rcv_time_stamp - Update vport's rcv seq time stamp
13380 * @vport: The vport to work on.
13381 *
13382 * This function updates the receive sequence time stamp for this vport. The
13383 * receive sequence time stamp indicates the time that the last frame of the
13384 * the sequence that has been idle for the longest amount of time was received.
13385 * the driver uses this time stamp to indicate if any received sequences have
13386 * timed out.
13387 **/
13388void
13389lpfc_update_rcv_time_stamp(struct lpfc_vport *vport)
13390{
13391 struct lpfc_dmabuf *h_buf;
13392 struct hbq_dmabuf *dmabuf = NULL;
13393
13394 /* get the oldest sequence on the rcv list */
13395 h_buf = list_get_first(&vport->rcv_buffer_list,
13396 struct lpfc_dmabuf, list);
13397 if (!h_buf)
13398 return;
13399 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
13400 vport->rcv_buffer_time_stamp = dmabuf->time_stamp;
13401}
13402
13403/**
13404 * lpfc_cleanup_rcv_buffers - Cleans up all outstanding receive sequences.
13405 * @vport: The vport that the received sequences were sent to.
13406 *
13407 * This function cleans up all outstanding received sequences. This is called
13408 * by the driver when a link event or user action invalidates all the received
13409 * sequences.
13410 **/
13411void
13412lpfc_cleanup_rcv_buffers(struct lpfc_vport *vport)
13413{
13414 struct lpfc_dmabuf *h_buf, *hnext;
13415 struct lpfc_dmabuf *d_buf, *dnext;
13416 struct hbq_dmabuf *dmabuf = NULL;
13417
13418 /* start with the oldest sequence on the rcv list */
13419 list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
13420 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
13421 list_del_init(&dmabuf->hbuf.list);
13422 list_for_each_entry_safe(d_buf, dnext,
13423 &dmabuf->dbuf.list, list) {
13424 list_del_init(&d_buf->list);
13425 lpfc_in_buf_free(vport->phba, d_buf);
13426 }
13427 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
13428 }
13429}
13430
13431/**
13432 * lpfc_rcv_seq_check_edtov - Cleans up timed out receive sequences.
13433 * @vport: The vport that the received sequences were sent to.
13434 *
13435 * This function determines whether any received sequences have timed out by
13436 * first checking the vport's rcv_buffer_time_stamp. If this time_stamp
13437 * indicates that there is at least one timed out sequence this routine will
13438 * go through the received sequences one at a time from most inactive to most
13439 * active to determine which ones need to be cleaned up. Once it has determined
13440 * that a sequence needs to be cleaned up it will simply free up the resources
13441 * without sending an abort.
13442 **/
13443void
13444lpfc_rcv_seq_check_edtov(struct lpfc_vport *vport)
13445{
13446 struct lpfc_dmabuf *h_buf, *hnext;
13447 struct lpfc_dmabuf *d_buf, *dnext;
13448 struct hbq_dmabuf *dmabuf = NULL;
13449 unsigned long timeout;
13450 int abort_count = 0;
13451
13452 timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
13453 vport->rcv_buffer_time_stamp);
13454 if (list_empty(&vport->rcv_buffer_list) ||
13455 time_before(jiffies, timeout))
13456 return;
13457 /* start with the oldest sequence on the rcv list */
13458 list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
13459 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
13460 timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
13461 dmabuf->time_stamp);
13462 if (time_before(jiffies, timeout))
13463 break;
13464 abort_count++;
13465 list_del_init(&dmabuf->hbuf.list);
13466 list_for_each_entry_safe(d_buf, dnext,
13467 &dmabuf->dbuf.list, list) {
13468 list_del_init(&d_buf->list);
13469 lpfc_in_buf_free(vport->phba, d_buf);
13470 }
13471 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
13472 }
13473 if (abort_count)
13474 lpfc_update_rcv_time_stamp(vport);
13475}
13476
13477/**
James Smart4f774512009-05-22 14:52:35 -040013478 * lpfc_fc_frame_add - Adds a frame to the vport's list of received sequences
13479 * @dmabuf: pointer to a dmabuf that describes the hdr and data of the FC frame
13480 *
13481 * This function searches through the existing incomplete sequences that have
13482 * been sent to this @vport. If the frame matches one of the incomplete
13483 * sequences then the dbuf in the @dmabuf is added to the list of frames that
13484 * make up that sequence. If no sequence is found that matches this frame then
13485 * the function will add the hbuf in the @dmabuf to the @vport's rcv_buffer_list
13486 * This function returns a pointer to the first dmabuf in the sequence list that
13487 * the frame was linked to.
13488 **/
13489static struct hbq_dmabuf *
13490lpfc_fc_frame_add(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
13491{
13492 struct fc_frame_header *new_hdr;
13493 struct fc_frame_header *temp_hdr;
13494 struct lpfc_dmabuf *d_buf;
13495 struct lpfc_dmabuf *h_buf;
13496 struct hbq_dmabuf *seq_dmabuf = NULL;
13497 struct hbq_dmabuf *temp_dmabuf = NULL;
13498
James Smart4d9ab992009-10-02 15:16:39 -040013499 INIT_LIST_HEAD(&dmabuf->dbuf.list);
James Smart45ed1192009-10-02 15:17:02 -040013500 dmabuf->time_stamp = jiffies;
James Smart4f774512009-05-22 14:52:35 -040013501 new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
13502 /* Use the hdr_buf to find the sequence that this frame belongs to */
13503 list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
13504 temp_hdr = (struct fc_frame_header *)h_buf->virt;
13505 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
13506 (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
13507 (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
13508 continue;
13509 /* found a pending sequence that matches this frame */
13510 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
13511 break;
13512 }
13513 if (!seq_dmabuf) {
13514 /*
13515 * This indicates first frame received for this sequence.
13516 * Queue the buffer on the vport's rcv_buffer_list.
13517 */
13518 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
James Smart45ed1192009-10-02 15:17:02 -040013519 lpfc_update_rcv_time_stamp(vport);
James Smart4f774512009-05-22 14:52:35 -040013520 return dmabuf;
13521 }
13522 temp_hdr = seq_dmabuf->hbuf.virt;
James Smarteeead812009-12-21 17:01:23 -050013523 if (be16_to_cpu(new_hdr->fh_seq_cnt) <
13524 be16_to_cpu(temp_hdr->fh_seq_cnt)) {
James Smart4d9ab992009-10-02 15:16:39 -040013525 list_del_init(&seq_dmabuf->hbuf.list);
13526 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
13527 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
James Smart45ed1192009-10-02 15:17:02 -040013528 lpfc_update_rcv_time_stamp(vport);
James Smart4f774512009-05-22 14:52:35 -040013529 return dmabuf;
13530 }
James Smart45ed1192009-10-02 15:17:02 -040013531 /* move this sequence to the tail to indicate a young sequence */
13532 list_move_tail(&seq_dmabuf->hbuf.list, &vport->rcv_buffer_list);
13533 seq_dmabuf->time_stamp = jiffies;
13534 lpfc_update_rcv_time_stamp(vport);
James Smarteeead812009-12-21 17:01:23 -050013535 if (list_empty(&seq_dmabuf->dbuf.list)) {
13536 temp_hdr = dmabuf->hbuf.virt;
13537 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
13538 return seq_dmabuf;
13539 }
James Smart4f774512009-05-22 14:52:35 -040013540 /* find the correct place in the sequence to insert this frame */
13541 list_for_each_entry_reverse(d_buf, &seq_dmabuf->dbuf.list, list) {
13542 temp_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
13543 temp_hdr = (struct fc_frame_header *)temp_dmabuf->hbuf.virt;
13544 /*
13545 * If the frame's sequence count is greater than the frame on
13546 * the list then insert the frame right after this frame
13547 */
James Smarteeead812009-12-21 17:01:23 -050013548 if (be16_to_cpu(new_hdr->fh_seq_cnt) >
13549 be16_to_cpu(temp_hdr->fh_seq_cnt)) {
James Smart4f774512009-05-22 14:52:35 -040013550 list_add(&dmabuf->dbuf.list, &temp_dmabuf->dbuf.list);
13551 return seq_dmabuf;
13552 }
13553 }
13554 return NULL;
13555}
13556
13557/**
James Smart6669f9b2009-10-02 15:16:45 -040013558 * lpfc_sli4_abort_partial_seq - Abort partially assembled unsol sequence
13559 * @vport: pointer to a vitural port
13560 * @dmabuf: pointer to a dmabuf that describes the FC sequence
13561 *
13562 * This function tries to abort from the partially assembed sequence, described
13563 * by the information from basic abbort @dmabuf. It checks to see whether such
13564 * partially assembled sequence held by the driver. If so, it shall free up all
13565 * the frames from the partially assembled sequence.
13566 *
13567 * Return
13568 * true -- if there is matching partially assembled sequence present and all
13569 * the frames freed with the sequence;
13570 * false -- if there is no matching partially assembled sequence present so
13571 * nothing got aborted in the lower layer driver
13572 **/
13573static bool
13574lpfc_sli4_abort_partial_seq(struct lpfc_vport *vport,
13575 struct hbq_dmabuf *dmabuf)
13576{
13577 struct fc_frame_header *new_hdr;
13578 struct fc_frame_header *temp_hdr;
13579 struct lpfc_dmabuf *d_buf, *n_buf, *h_buf;
13580 struct hbq_dmabuf *seq_dmabuf = NULL;
13581
13582 /* Use the hdr_buf to find the sequence that matches this frame */
13583 INIT_LIST_HEAD(&dmabuf->dbuf.list);
13584 INIT_LIST_HEAD(&dmabuf->hbuf.list);
13585 new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
13586 list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
13587 temp_hdr = (struct fc_frame_header *)h_buf->virt;
13588 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
13589 (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
13590 (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
13591 continue;
13592 /* found a pending sequence that matches this frame */
13593 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
13594 break;
13595 }
13596
13597 /* Free up all the frames from the partially assembled sequence */
13598 if (seq_dmabuf) {
13599 list_for_each_entry_safe(d_buf, n_buf,
13600 &seq_dmabuf->dbuf.list, list) {
13601 list_del_init(&d_buf->list);
13602 lpfc_in_buf_free(vport->phba, d_buf);
13603 }
13604 return true;
13605 }
13606 return false;
13607}
13608
13609/**
James Smart546fc852011-03-11 16:06:29 -050013610 * lpfc_sli4_seq_abort_rsp_cmpl - BLS ABORT RSP seq abort iocb complete handler
James Smart6669f9b2009-10-02 15:16:45 -040013611 * @phba: Pointer to HBA context object.
13612 * @cmd_iocbq: pointer to the command iocbq structure.
13613 * @rsp_iocbq: pointer to the response iocbq structure.
13614 *
James Smart546fc852011-03-11 16:06:29 -050013615 * This function handles the sequence abort response iocb command complete
James Smart6669f9b2009-10-02 15:16:45 -040013616 * event. It properly releases the memory allocated to the sequence abort
13617 * accept iocb.
13618 **/
13619static void
James Smart546fc852011-03-11 16:06:29 -050013620lpfc_sli4_seq_abort_rsp_cmpl(struct lpfc_hba *phba,
James Smart6669f9b2009-10-02 15:16:45 -040013621 struct lpfc_iocbq *cmd_iocbq,
13622 struct lpfc_iocbq *rsp_iocbq)
13623{
13624 if (cmd_iocbq)
13625 lpfc_sli_release_iocbq(phba, cmd_iocbq);
13626}
13627
13628/**
James Smart6d368e52011-05-24 11:44:12 -040013629 * lpfc_sli4_xri_inrange - check xri is in range of xris owned by driver.
13630 * @phba: Pointer to HBA context object.
13631 * @xri: xri id in transaction.
13632 *
13633 * This function validates the xri maps to the known range of XRIs allocated an
13634 * used by the driver.
13635 **/
James Smart7851fe22011-07-22 18:36:52 -040013636uint16_t
James Smart6d368e52011-05-24 11:44:12 -040013637lpfc_sli4_xri_inrange(struct lpfc_hba *phba,
13638 uint16_t xri)
13639{
13640 int i;
13641
13642 for (i = 0; i < phba->sli4_hba.max_cfg_param.max_xri; i++) {
13643 if (xri == phba->sli4_hba.xri_ids[i])
13644 return i;
13645 }
13646 return NO_XRI;
13647}
13648
13649
13650/**
James Smart546fc852011-03-11 16:06:29 -050013651 * lpfc_sli4_seq_abort_rsp - bls rsp to sequence abort
James Smart6669f9b2009-10-02 15:16:45 -040013652 * @phba: Pointer to HBA context object.
13653 * @fc_hdr: pointer to a FC frame header.
13654 *
James Smart546fc852011-03-11 16:06:29 -050013655 * This function sends a basic response to a previous unsol sequence abort
James Smart6669f9b2009-10-02 15:16:45 -040013656 * event after aborting the sequence handling.
13657 **/
13658static void
James Smart546fc852011-03-11 16:06:29 -050013659lpfc_sli4_seq_abort_rsp(struct lpfc_hba *phba,
James Smart6669f9b2009-10-02 15:16:45 -040013660 struct fc_frame_header *fc_hdr)
13661{
13662 struct lpfc_iocbq *ctiocb = NULL;
13663 struct lpfc_nodelist *ndlp;
James Smart5ffc2662009-11-18 15:39:44 -050013664 uint16_t oxid, rxid;
13665 uint32_t sid, fctl;
James Smart6669f9b2009-10-02 15:16:45 -040013666 IOCB_t *icmd;
James Smart546fc852011-03-11 16:06:29 -050013667 int rc;
James Smart6669f9b2009-10-02 15:16:45 -040013668
13669 if (!lpfc_is_link_up(phba))
13670 return;
13671
13672 sid = sli4_sid_from_fc_hdr(fc_hdr);
13673 oxid = be16_to_cpu(fc_hdr->fh_ox_id);
James Smart5ffc2662009-11-18 15:39:44 -050013674 rxid = be16_to_cpu(fc_hdr->fh_rx_id);
James Smart6669f9b2009-10-02 15:16:45 -040013675
13676 ndlp = lpfc_findnode_did(phba->pport, sid);
13677 if (!ndlp) {
13678 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
13679 "1268 Find ndlp returned NULL for oxid:x%x "
13680 "SID:x%x\n", oxid, sid);
13681 return;
13682 }
James Smart6d368e52011-05-24 11:44:12 -040013683 if (lpfc_sli4_xri_inrange(phba, rxid))
James Smart19ca7602010-11-20 23:11:55 -050013684 lpfc_set_rrq_active(phba, ndlp, rxid, oxid, 0);
James Smart6669f9b2009-10-02 15:16:45 -040013685
James Smart546fc852011-03-11 16:06:29 -050013686 /* Allocate buffer for rsp iocb */
James Smart6669f9b2009-10-02 15:16:45 -040013687 ctiocb = lpfc_sli_get_iocbq(phba);
13688 if (!ctiocb)
13689 return;
13690
James Smart5ffc2662009-11-18 15:39:44 -050013691 /* Extract the F_CTL field from FC_HDR */
13692 fctl = sli4_fctl_from_fc_hdr(fc_hdr);
13693
James Smart6669f9b2009-10-02 15:16:45 -040013694 icmd = &ctiocb->iocb;
James Smart6669f9b2009-10-02 15:16:45 -040013695 icmd->un.xseq64.bdl.bdeSize = 0;
James Smart5ffc2662009-11-18 15:39:44 -050013696 icmd->un.xseq64.bdl.ulpIoTag32 = 0;
James Smart6669f9b2009-10-02 15:16:45 -040013697 icmd->un.xseq64.w5.hcsw.Dfctl = 0;
13698 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_ACC;
13699 icmd->un.xseq64.w5.hcsw.Type = FC_TYPE_BLS;
13700
13701 /* Fill in the rest of iocb fields */
13702 icmd->ulpCommand = CMD_XMIT_BLS_RSP64_CX;
13703 icmd->ulpBdeCount = 0;
13704 icmd->ulpLe = 1;
13705 icmd->ulpClass = CLASS3;
James Smart6d368e52011-05-24 11:44:12 -040013706 icmd->ulpContext = phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
James Smartbe858b62010-12-15 17:57:20 -050013707 ctiocb->context1 = ndlp;
James Smart6669f9b2009-10-02 15:16:45 -040013708
James Smart6669f9b2009-10-02 15:16:45 -040013709 ctiocb->iocb_cmpl = NULL;
13710 ctiocb->vport = phba->pport;
James Smart546fc852011-03-11 16:06:29 -050013711 ctiocb->iocb_cmpl = lpfc_sli4_seq_abort_rsp_cmpl;
James Smart6d368e52011-05-24 11:44:12 -040013712 ctiocb->sli4_lxritag = NO_XRI;
James Smart546fc852011-03-11 16:06:29 -050013713 ctiocb->sli4_xritag = NO_XRI;
13714
13715 /* If the oxid maps to the FCP XRI range or if it is out of range,
13716 * send a BLS_RJT. The driver no longer has that exchange.
13717 * Override the IOCB for a BA_RJT.
13718 */
13719 if (oxid > (phba->sli4_hba.max_cfg_param.max_xri +
13720 phba->sli4_hba.max_cfg_param.xri_base) ||
13721 oxid > (lpfc_sli4_get_els_iocb_cnt(phba) +
13722 phba->sli4_hba.max_cfg_param.xri_base)) {
13723 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_RJT;
13724 bf_set(lpfc_vndr_code, &icmd->un.bls_rsp, 0);
13725 bf_set(lpfc_rsn_expln, &icmd->un.bls_rsp, FC_BA_RJT_INV_XID);
13726 bf_set(lpfc_rsn_code, &icmd->un.bls_rsp, FC_BA_RJT_UNABLE);
13727 }
James Smart6669f9b2009-10-02 15:16:45 -040013728
James Smart5ffc2662009-11-18 15:39:44 -050013729 if (fctl & FC_FC_EX_CTX) {
13730 /* ABTS sent by responder to CT exchange, construction
13731 * of BA_ACC will use OX_ID from ABTS for the XRI_TAG
13732 * field and RX_ID from ABTS for RX_ID field.
13733 */
James Smart546fc852011-03-11 16:06:29 -050013734 bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_RSP);
13735 bf_set(lpfc_abts_rxid, &icmd->un.bls_rsp, rxid);
James Smart5ffc2662009-11-18 15:39:44 -050013736 } else {
13737 /* ABTS sent by initiator to CT exchange, construction
13738 * of BA_ACC will need to allocate a new XRI as for the
13739 * XRI_TAG and RX_ID fields.
13740 */
James Smart546fc852011-03-11 16:06:29 -050013741 bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_INT);
13742 bf_set(lpfc_abts_rxid, &icmd->un.bls_rsp, NO_XRI);
James Smart5ffc2662009-11-18 15:39:44 -050013743 }
James Smart546fc852011-03-11 16:06:29 -050013744 bf_set(lpfc_abts_oxid, &icmd->un.bls_rsp, oxid);
James Smart5ffc2662009-11-18 15:39:44 -050013745
James Smart546fc852011-03-11 16:06:29 -050013746 /* Xmit CT abts response on exchange <xid> */
James Smart6669f9b2009-10-02 15:16:45 -040013747 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
James Smart546fc852011-03-11 16:06:29 -050013748 "1200 Send BLS cmd x%x on oxid x%x Data: x%x\n",
13749 icmd->un.xseq64.w5.hcsw.Rctl, oxid, phba->link_state);
13750
13751 rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, ctiocb, 0);
13752 if (rc == IOCB_ERROR) {
13753 lpfc_printf_log(phba, KERN_ERR, LOG_ELS,
13754 "2925 Failed to issue CT ABTS RSP x%x on "
13755 "xri x%x, Data x%x\n",
13756 icmd->un.xseq64.w5.hcsw.Rctl, oxid,
13757 phba->link_state);
13758 lpfc_sli_release_iocbq(phba, ctiocb);
13759 }
James Smart6669f9b2009-10-02 15:16:45 -040013760}
13761
13762/**
13763 * lpfc_sli4_handle_unsol_abort - Handle sli-4 unsolicited abort event
13764 * @vport: Pointer to the vport on which this sequence was received
13765 * @dmabuf: pointer to a dmabuf that describes the FC sequence
13766 *
13767 * This function handles an SLI-4 unsolicited abort event. If the unsolicited
13768 * receive sequence is only partially assembed by the driver, it shall abort
13769 * the partially assembled frames for the sequence. Otherwise, if the
13770 * unsolicited receive sequence has been completely assembled and passed to
13771 * the Upper Layer Protocol (UPL), it then mark the per oxid status for the
13772 * unsolicited sequence has been aborted. After that, it will issue a basic
13773 * accept to accept the abort.
13774 **/
13775void
13776lpfc_sli4_handle_unsol_abort(struct lpfc_vport *vport,
13777 struct hbq_dmabuf *dmabuf)
13778{
13779 struct lpfc_hba *phba = vport->phba;
13780 struct fc_frame_header fc_hdr;
James Smart5ffc2662009-11-18 15:39:44 -050013781 uint32_t fctl;
James Smart6669f9b2009-10-02 15:16:45 -040013782 bool abts_par;
13783
James Smart6669f9b2009-10-02 15:16:45 -040013784 /* Make a copy of fc_hdr before the dmabuf being released */
13785 memcpy(&fc_hdr, dmabuf->hbuf.virt, sizeof(struct fc_frame_header));
James Smart5ffc2662009-11-18 15:39:44 -050013786 fctl = sli4_fctl_from_fc_hdr(&fc_hdr);
James Smart6669f9b2009-10-02 15:16:45 -040013787
James Smart5ffc2662009-11-18 15:39:44 -050013788 if (fctl & FC_FC_EX_CTX) {
13789 /*
13790 * ABTS sent by responder to exchange, just free the buffer
13791 */
James Smart6669f9b2009-10-02 15:16:45 -040013792 lpfc_in_buf_free(phba, &dmabuf->dbuf);
James Smart5ffc2662009-11-18 15:39:44 -050013793 } else {
13794 /*
13795 * ABTS sent by initiator to exchange, need to do cleanup
13796 */
13797 /* Try to abort partially assembled seq */
13798 abts_par = lpfc_sli4_abort_partial_seq(vport, dmabuf);
13799
13800 /* Send abort to ULP if partially seq abort failed */
13801 if (abts_par == false)
13802 lpfc_sli4_send_seq_to_ulp(vport, dmabuf);
13803 else
13804 lpfc_in_buf_free(phba, &dmabuf->dbuf);
13805 }
James Smart6669f9b2009-10-02 15:16:45 -040013806 /* Send basic accept (BA_ACC) to the abort requester */
James Smart546fc852011-03-11 16:06:29 -050013807 lpfc_sli4_seq_abort_rsp(phba, &fc_hdr);
James Smart6669f9b2009-10-02 15:16:45 -040013808}
13809
13810/**
James Smart4f774512009-05-22 14:52:35 -040013811 * lpfc_seq_complete - Indicates if a sequence is complete
13812 * @dmabuf: pointer to a dmabuf that describes the FC sequence
13813 *
13814 * This function checks the sequence, starting with the frame described by
13815 * @dmabuf, to see if all the frames associated with this sequence are present.
13816 * the frames associated with this sequence are linked to the @dmabuf using the
13817 * dbuf list. This function looks for two major things. 1) That the first frame
13818 * has a sequence count of zero. 2) There is a frame with last frame of sequence
13819 * set. 3) That there are no holes in the sequence count. The function will
13820 * return 1 when the sequence is complete, otherwise it will return 0.
13821 **/
13822static int
13823lpfc_seq_complete(struct hbq_dmabuf *dmabuf)
13824{
13825 struct fc_frame_header *hdr;
13826 struct lpfc_dmabuf *d_buf;
13827 struct hbq_dmabuf *seq_dmabuf;
13828 uint32_t fctl;
13829 int seq_count = 0;
13830
13831 hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
13832 /* make sure first fame of sequence has a sequence count of zero */
13833 if (hdr->fh_seq_cnt != seq_count)
13834 return 0;
13835 fctl = (hdr->fh_f_ctl[0] << 16 |
13836 hdr->fh_f_ctl[1] << 8 |
13837 hdr->fh_f_ctl[2]);
13838 /* If last frame of sequence we can return success. */
13839 if (fctl & FC_FC_END_SEQ)
13840 return 1;
13841 list_for_each_entry(d_buf, &dmabuf->dbuf.list, list) {
13842 seq_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
13843 hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
13844 /* If there is a hole in the sequence count then fail. */
James Smarteeead812009-12-21 17:01:23 -050013845 if (++seq_count != be16_to_cpu(hdr->fh_seq_cnt))
James Smart4f774512009-05-22 14:52:35 -040013846 return 0;
13847 fctl = (hdr->fh_f_ctl[0] << 16 |
13848 hdr->fh_f_ctl[1] << 8 |
13849 hdr->fh_f_ctl[2]);
13850 /* If last frame of sequence we can return success. */
13851 if (fctl & FC_FC_END_SEQ)
13852 return 1;
13853 }
13854 return 0;
13855}
13856
13857/**
13858 * lpfc_prep_seq - Prep sequence for ULP processing
13859 * @vport: Pointer to the vport on which this sequence was received
13860 * @dmabuf: pointer to a dmabuf that describes the FC sequence
13861 *
13862 * This function takes a sequence, described by a list of frames, and creates
13863 * a list of iocbq structures to describe the sequence. This iocbq list will be
13864 * used to issue to the generic unsolicited sequence handler. This routine
13865 * returns a pointer to the first iocbq in the list. If the function is unable
13866 * to allocate an iocbq then it throw out the received frames that were not
13867 * able to be described and return a pointer to the first iocbq. If unable to
13868 * allocate any iocbqs (including the first) this function will return NULL.
13869 **/
13870static struct lpfc_iocbq *
13871lpfc_prep_seq(struct lpfc_vport *vport, struct hbq_dmabuf *seq_dmabuf)
13872{
James Smart7851fe22011-07-22 18:36:52 -040013873 struct hbq_dmabuf *hbq_buf;
James Smart4f774512009-05-22 14:52:35 -040013874 struct lpfc_dmabuf *d_buf, *n_buf;
13875 struct lpfc_iocbq *first_iocbq, *iocbq;
13876 struct fc_frame_header *fc_hdr;
13877 uint32_t sid;
James Smart7851fe22011-07-22 18:36:52 -040013878 uint32_t len, tot_len;
James Smarteeead812009-12-21 17:01:23 -050013879 struct ulp_bde64 *pbde;
James Smart4f774512009-05-22 14:52:35 -040013880
13881 fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
13882 /* remove from receive buffer list */
13883 list_del_init(&seq_dmabuf->hbuf.list);
James Smart45ed1192009-10-02 15:17:02 -040013884 lpfc_update_rcv_time_stamp(vport);
James Smart4f774512009-05-22 14:52:35 -040013885 /* get the Remote Port's SID */
James Smart6669f9b2009-10-02 15:16:45 -040013886 sid = sli4_sid_from_fc_hdr(fc_hdr);
James Smart7851fe22011-07-22 18:36:52 -040013887 tot_len = 0;
James Smart4f774512009-05-22 14:52:35 -040013888 /* Get an iocbq struct to fill in. */
13889 first_iocbq = lpfc_sli_get_iocbq(vport->phba);
13890 if (first_iocbq) {
13891 /* Initialize the first IOCB. */
James Smart8fa38512009-07-19 10:01:03 -040013892 first_iocbq->iocb.unsli3.rcvsli3.acc_len = 0;
James Smart4f774512009-05-22 14:52:35 -040013893 first_iocbq->iocb.ulpStatus = IOSTAT_SUCCESS;
13894 first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_SEQ64_CX;
James Smart7851fe22011-07-22 18:36:52 -040013895 first_iocbq->iocb.ulpContext = NO_XRI;
13896 first_iocbq->iocb.unsli3.rcvsli3.ox_id =
13897 be16_to_cpu(fc_hdr->fh_ox_id);
13898 /* iocbq is prepped for internal consumption. Physical vpi. */
13899 first_iocbq->iocb.unsli3.rcvsli3.vpi =
13900 vport->phba->vpi_ids[vport->vpi];
James Smart4f774512009-05-22 14:52:35 -040013901 /* put the first buffer into the first IOCBq */
13902 first_iocbq->context2 = &seq_dmabuf->dbuf;
13903 first_iocbq->context3 = NULL;
13904 first_iocbq->iocb.ulpBdeCount = 1;
13905 first_iocbq->iocb.un.cont64[0].tus.f.bdeSize =
13906 LPFC_DATA_BUF_SIZE;
13907 first_iocbq->iocb.un.rcvels.remoteID = sid;
James Smart7851fe22011-07-22 18:36:52 -040013908 tot_len = bf_get(lpfc_rcqe_length,
James Smart4d9ab992009-10-02 15:16:39 -040013909 &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
James Smart7851fe22011-07-22 18:36:52 -040013910 first_iocbq->iocb.unsli3.rcvsli3.acc_len = tot_len;
James Smart4f774512009-05-22 14:52:35 -040013911 }
13912 iocbq = first_iocbq;
13913 /*
13914 * Each IOCBq can have two Buffers assigned, so go through the list
13915 * of buffers for this sequence and save two buffers in each IOCBq
13916 */
13917 list_for_each_entry_safe(d_buf, n_buf, &seq_dmabuf->dbuf.list, list) {
13918 if (!iocbq) {
13919 lpfc_in_buf_free(vport->phba, d_buf);
13920 continue;
13921 }
13922 if (!iocbq->context3) {
13923 iocbq->context3 = d_buf;
13924 iocbq->iocb.ulpBdeCount++;
James Smarteeead812009-12-21 17:01:23 -050013925 pbde = (struct ulp_bde64 *)
13926 &iocbq->iocb.unsli3.sli3Words[4];
13927 pbde->tus.f.bdeSize = LPFC_DATA_BUF_SIZE;
James Smart7851fe22011-07-22 18:36:52 -040013928
13929 /* We need to get the size out of the right CQE */
13930 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
13931 len = bf_get(lpfc_rcqe_length,
13932 &hbq_buf->cq_event.cqe.rcqe_cmpl);
13933 iocbq->iocb.unsli3.rcvsli3.acc_len += len;
13934 tot_len += len;
James Smart4f774512009-05-22 14:52:35 -040013935 } else {
13936 iocbq = lpfc_sli_get_iocbq(vport->phba);
13937 if (!iocbq) {
13938 if (first_iocbq) {
13939 first_iocbq->iocb.ulpStatus =
13940 IOSTAT_FCP_RSP_ERROR;
13941 first_iocbq->iocb.un.ulpWord[4] =
13942 IOERR_NO_RESOURCES;
13943 }
13944 lpfc_in_buf_free(vport->phba, d_buf);
13945 continue;
13946 }
13947 iocbq->context2 = d_buf;
13948 iocbq->context3 = NULL;
13949 iocbq->iocb.ulpBdeCount = 1;
13950 iocbq->iocb.un.cont64[0].tus.f.bdeSize =
13951 LPFC_DATA_BUF_SIZE;
James Smart7851fe22011-07-22 18:36:52 -040013952
13953 /* We need to get the size out of the right CQE */
13954 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
13955 len = bf_get(lpfc_rcqe_length,
13956 &hbq_buf->cq_event.cqe.rcqe_cmpl);
13957 tot_len += len;
13958 iocbq->iocb.unsli3.rcvsli3.acc_len = tot_len;
13959
James Smart4f774512009-05-22 14:52:35 -040013960 iocbq->iocb.un.rcvels.remoteID = sid;
13961 list_add_tail(&iocbq->list, &first_iocbq->list);
13962 }
13963 }
13964 return first_iocbq;
13965}
13966
James Smart6669f9b2009-10-02 15:16:45 -040013967static void
13968lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *vport,
13969 struct hbq_dmabuf *seq_dmabuf)
13970{
13971 struct fc_frame_header *fc_hdr;
13972 struct lpfc_iocbq *iocbq, *curr_iocb, *next_iocb;
13973 struct lpfc_hba *phba = vport->phba;
13974
13975 fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
13976 iocbq = lpfc_prep_seq(vport, seq_dmabuf);
13977 if (!iocbq) {
13978 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13979 "2707 Ring %d handler: Failed to allocate "
13980 "iocb Rctl x%x Type x%x received\n",
13981 LPFC_ELS_RING,
13982 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
13983 return;
13984 }
13985 if (!lpfc_complete_unsol_iocb(phba,
13986 &phba->sli.ring[LPFC_ELS_RING],
13987 iocbq, fc_hdr->fh_r_ctl,
13988 fc_hdr->fh_type))
James Smart6d368e52011-05-24 11:44:12 -040013989 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart6669f9b2009-10-02 15:16:45 -040013990 "2540 Ring %d handler: unexpected Rctl "
13991 "x%x Type x%x received\n",
13992 LPFC_ELS_RING,
13993 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
13994
13995 /* Free iocb created in lpfc_prep_seq */
13996 list_for_each_entry_safe(curr_iocb, next_iocb,
13997 &iocbq->list, list) {
13998 list_del_init(&curr_iocb->list);
13999 lpfc_sli_release_iocbq(phba, curr_iocb);
14000 }
14001 lpfc_sli_release_iocbq(phba, iocbq);
14002}
14003
James Smart4f774512009-05-22 14:52:35 -040014004/**
14005 * lpfc_sli4_handle_received_buffer - Handle received buffers from firmware
14006 * @phba: Pointer to HBA context object.
14007 *
14008 * This function is called with no lock held. This function processes all
14009 * the received buffers and gives it to upper layers when a received buffer
14010 * indicates that it is the final frame in the sequence. The interrupt
14011 * service routine processes received buffers at interrupt contexts and adds
14012 * received dma buffers to the rb_pend_list queue and signals the worker thread.
14013 * Worker thread calls lpfc_sli4_handle_received_buffer, which will call the
14014 * appropriate receive function when the final frame in a sequence is received.
14015 **/
James Smart4d9ab992009-10-02 15:16:39 -040014016void
14017lpfc_sli4_handle_received_buffer(struct lpfc_hba *phba,
14018 struct hbq_dmabuf *dmabuf)
James Smart4f774512009-05-22 14:52:35 -040014019{
James Smart4d9ab992009-10-02 15:16:39 -040014020 struct hbq_dmabuf *seq_dmabuf;
James Smart4f774512009-05-22 14:52:35 -040014021 struct fc_frame_header *fc_hdr;
14022 struct lpfc_vport *vport;
14023 uint32_t fcfi;
James Smart4f774512009-05-22 14:52:35 -040014024
James Smart4f774512009-05-22 14:52:35 -040014025 /* Process each received buffer */
James Smart4d9ab992009-10-02 15:16:39 -040014026 fc_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
14027 /* check to see if this a valid type of frame */
14028 if (lpfc_fc_frame_check(phba, fc_hdr)) {
14029 lpfc_in_buf_free(phba, &dmabuf->dbuf);
14030 return;
14031 }
James Smart7851fe22011-07-22 18:36:52 -040014032 if ((bf_get(lpfc_cqe_code,
14033 &dmabuf->cq_event.cqe.rcqe_cmpl) == CQE_CODE_RECEIVE_V1))
14034 fcfi = bf_get(lpfc_rcqe_fcf_id_v1,
14035 &dmabuf->cq_event.cqe.rcqe_cmpl);
14036 else
14037 fcfi = bf_get(lpfc_rcqe_fcf_id,
14038 &dmabuf->cq_event.cqe.rcqe_cmpl);
James Smart4d9ab992009-10-02 15:16:39 -040014039 vport = lpfc_fc_frame_to_vport(phba, fc_hdr, fcfi);
James Smartc8685952009-11-18 15:39:16 -050014040 if (!vport || !(vport->vpi_state & LPFC_VPI_REGISTERED)) {
James Smart4d9ab992009-10-02 15:16:39 -040014041 /* throw out the frame */
14042 lpfc_in_buf_free(phba, &dmabuf->dbuf);
14043 return;
14044 }
James Smart6669f9b2009-10-02 15:16:45 -040014045 /* Handle the basic abort sequence (BA_ABTS) event */
14046 if (fc_hdr->fh_r_ctl == FC_RCTL_BA_ABTS) {
14047 lpfc_sli4_handle_unsol_abort(vport, dmabuf);
14048 return;
14049 }
14050
James Smart4d9ab992009-10-02 15:16:39 -040014051 /* Link this frame */
14052 seq_dmabuf = lpfc_fc_frame_add(vport, dmabuf);
14053 if (!seq_dmabuf) {
14054 /* unable to add frame to vport - throw it out */
14055 lpfc_in_buf_free(phba, &dmabuf->dbuf);
14056 return;
14057 }
14058 /* If not last frame in sequence continue processing frames. */
James Smartdef9c7a2009-12-21 17:02:28 -050014059 if (!lpfc_seq_complete(seq_dmabuf))
James Smart4d9ab992009-10-02 15:16:39 -040014060 return;
James Smartdef9c7a2009-12-21 17:02:28 -050014061
James Smart6669f9b2009-10-02 15:16:45 -040014062 /* Send the complete sequence to the upper layer protocol */
14063 lpfc_sli4_send_seq_to_ulp(vport, seq_dmabuf);
James Smart4f774512009-05-22 14:52:35 -040014064}
James Smart6fb120a2009-05-22 14:52:59 -040014065
14066/**
14067 * lpfc_sli4_post_all_rpi_hdrs - Post the rpi header memory region to the port
14068 * @phba: pointer to lpfc hba data structure.
14069 *
14070 * This routine is invoked to post rpi header templates to the
14071 * HBA consistent with the SLI-4 interface spec. This routine
James Smart49198b32010-04-06 15:04:33 -040014072 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
14073 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
James Smart6fb120a2009-05-22 14:52:59 -040014074 *
14075 * This routine does not require any locks. It's usage is expected
14076 * to be driver load or reset recovery when the driver is
14077 * sequential.
14078 *
14079 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020014080 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -040014081 * -EIO - The mailbox failed to complete successfully.
James Smart6fb120a2009-05-22 14:52:59 -040014082 * When this error occurs, the driver is not guaranteed
14083 * to have any rpi regions posted to the device and
14084 * must either attempt to repost the regions or take a
14085 * fatal error.
14086 **/
14087int
14088lpfc_sli4_post_all_rpi_hdrs(struct lpfc_hba *phba)
14089{
14090 struct lpfc_rpi_hdr *rpi_page;
14091 uint32_t rc = 0;
James Smart6d368e52011-05-24 11:44:12 -040014092 uint16_t lrpi = 0;
James Smart6fb120a2009-05-22 14:52:59 -040014093
James Smart6d368e52011-05-24 11:44:12 -040014094 /* SLI4 ports that support extents do not require RPI headers. */
14095 if (!phba->sli4_hba.rpi_hdrs_in_use)
14096 goto exit;
14097 if (phba->sli4_hba.extents_in_use)
14098 return -EIO;
14099
James Smart6fb120a2009-05-22 14:52:59 -040014100 list_for_each_entry(rpi_page, &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
James Smart6d368e52011-05-24 11:44:12 -040014101 /*
14102 * Assign the rpi headers a physical rpi only if the driver
14103 * has not initialized those resources. A port reset only
14104 * needs the headers posted.
14105 */
14106 if (bf_get(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags) !=
14107 LPFC_RPI_RSRC_RDY)
14108 rpi_page->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
14109
James Smart6fb120a2009-05-22 14:52:59 -040014110 rc = lpfc_sli4_post_rpi_hdr(phba, rpi_page);
14111 if (rc != MBX_SUCCESS) {
14112 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14113 "2008 Error %d posting all rpi "
14114 "headers\n", rc);
14115 rc = -EIO;
14116 break;
14117 }
14118 }
14119
James Smart6d368e52011-05-24 11:44:12 -040014120 exit:
14121 bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags,
14122 LPFC_RPI_RSRC_RDY);
James Smart6fb120a2009-05-22 14:52:59 -040014123 return rc;
14124}
14125
14126/**
14127 * lpfc_sli4_post_rpi_hdr - Post an rpi header memory region to the port
14128 * @phba: pointer to lpfc hba data structure.
14129 * @rpi_page: pointer to the rpi memory region.
14130 *
14131 * This routine is invoked to post a single rpi header to the
14132 * HBA consistent with the SLI-4 interface spec. This memory region
14133 * maps up to 64 rpi context regions.
14134 *
14135 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020014136 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -040014137 * -ENOMEM - No available memory
14138 * -EIO - The mailbox failed to complete successfully.
James Smart6fb120a2009-05-22 14:52:59 -040014139 **/
14140int
14141lpfc_sli4_post_rpi_hdr(struct lpfc_hba *phba, struct lpfc_rpi_hdr *rpi_page)
14142{
14143 LPFC_MBOXQ_t *mboxq;
14144 struct lpfc_mbx_post_hdr_tmpl *hdr_tmpl;
14145 uint32_t rc = 0;
James Smart6fb120a2009-05-22 14:52:59 -040014146 uint32_t shdr_status, shdr_add_status;
14147 union lpfc_sli4_cfg_shdr *shdr;
14148
James Smart6d368e52011-05-24 11:44:12 -040014149 /* SLI4 ports that support extents do not require RPI headers. */
14150 if (!phba->sli4_hba.rpi_hdrs_in_use)
14151 return rc;
14152 if (phba->sli4_hba.extents_in_use)
14153 return -EIO;
14154
James Smart6fb120a2009-05-22 14:52:59 -040014155 /* The port is notified of the header region via a mailbox command. */
14156 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14157 if (!mboxq) {
14158 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14159 "2001 Unable to allocate memory for issuing "
14160 "SLI_CONFIG_SPECIAL mailbox command\n");
14161 return -ENOMEM;
14162 }
14163
14164 /* Post all rpi memory regions to the port. */
14165 hdr_tmpl = &mboxq->u.mqe.un.hdr_tmpl;
James Smart6fb120a2009-05-22 14:52:59 -040014166 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
14167 LPFC_MBOX_OPCODE_FCOE_POST_HDR_TEMPLATE,
14168 sizeof(struct lpfc_mbx_post_hdr_tmpl) -
James Smartfedd3b72011-02-16 12:39:24 -050014169 sizeof(struct lpfc_sli4_cfg_mhdr),
14170 LPFC_SLI4_MBX_EMBED);
James Smart6d368e52011-05-24 11:44:12 -040014171
14172
14173 /* Post the physical rpi to the port for this rpi header. */
James Smart6fb120a2009-05-22 14:52:59 -040014174 bf_set(lpfc_mbx_post_hdr_tmpl_rpi_offset, hdr_tmpl,
14175 rpi_page->start_rpi);
James Smart6d368e52011-05-24 11:44:12 -040014176 bf_set(lpfc_mbx_post_hdr_tmpl_page_cnt,
14177 hdr_tmpl, rpi_page->page_count);
14178
James Smart6fb120a2009-05-22 14:52:59 -040014179 hdr_tmpl->rpi_paddr_lo = putPaddrLow(rpi_page->dmabuf->phys);
14180 hdr_tmpl->rpi_paddr_hi = putPaddrHigh(rpi_page->dmabuf->phys);
James Smartf1126682009-06-10 17:22:44 -040014181 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
James Smart6fb120a2009-05-22 14:52:59 -040014182 shdr = (union lpfc_sli4_cfg_shdr *) &hdr_tmpl->header.cfg_shdr;
14183 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14184 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14185 if (rc != MBX_TIMEOUT)
14186 mempool_free(mboxq, phba->mbox_mem_pool);
14187 if (shdr_status || shdr_add_status || rc) {
14188 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14189 "2514 POST_RPI_HDR mailbox failed with "
14190 "status x%x add_status x%x, mbx status x%x\n",
14191 shdr_status, shdr_add_status, rc);
14192 rc = -ENXIO;
14193 }
14194 return rc;
14195}
14196
14197/**
14198 * lpfc_sli4_alloc_rpi - Get an available rpi in the device's range
14199 * @phba: pointer to lpfc hba data structure.
14200 *
14201 * This routine is invoked to post rpi header templates to the
14202 * HBA consistent with the SLI-4 interface spec. This routine
James Smart49198b32010-04-06 15:04:33 -040014203 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
14204 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
James Smart6fb120a2009-05-22 14:52:59 -040014205 *
14206 * Returns
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020014207 * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
James Smart6fb120a2009-05-22 14:52:59 -040014208 * LPFC_RPI_ALLOC_ERROR if no rpis are available.
14209 **/
14210int
14211lpfc_sli4_alloc_rpi(struct lpfc_hba *phba)
14212{
James Smart6d368e52011-05-24 11:44:12 -040014213 unsigned long rpi;
14214 uint16_t max_rpi, rpi_limit;
14215 uint16_t rpi_remaining, lrpi = 0;
James Smart6fb120a2009-05-22 14:52:59 -040014216 struct lpfc_rpi_hdr *rpi_hdr;
14217
14218 max_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
James Smart6fb120a2009-05-22 14:52:59 -040014219 rpi_limit = phba->sli4_hba.next_rpi;
14220
14221 /*
James Smart6d368e52011-05-24 11:44:12 -040014222 * Fetch the next logical rpi. Because this index is logical,
14223 * the driver starts at 0 each time.
James Smart6fb120a2009-05-22 14:52:59 -040014224 */
14225 spin_lock_irq(&phba->hbalock);
James Smart6d368e52011-05-24 11:44:12 -040014226 rpi = find_next_zero_bit(phba->sli4_hba.rpi_bmask, rpi_limit, 0);
14227 if (rpi >= rpi_limit)
James Smart6fb120a2009-05-22 14:52:59 -040014228 rpi = LPFC_RPI_ALLOC_ERROR;
14229 else {
14230 set_bit(rpi, phba->sli4_hba.rpi_bmask);
14231 phba->sli4_hba.max_cfg_param.rpi_used++;
14232 phba->sli4_hba.rpi_count++;
14233 }
14234
14235 /*
14236 * Don't try to allocate more rpi header regions if the device limit
James Smart6d368e52011-05-24 11:44:12 -040014237 * has been exhausted.
James Smart6fb120a2009-05-22 14:52:59 -040014238 */
14239 if ((rpi == LPFC_RPI_ALLOC_ERROR) &&
14240 (phba->sli4_hba.rpi_count >= max_rpi)) {
14241 spin_unlock_irq(&phba->hbalock);
14242 return rpi;
14243 }
14244
14245 /*
James Smart6d368e52011-05-24 11:44:12 -040014246 * RPI header postings are not required for SLI4 ports capable of
14247 * extents.
14248 */
14249 if (!phba->sli4_hba.rpi_hdrs_in_use) {
14250 spin_unlock_irq(&phba->hbalock);
14251 return rpi;
14252 }
14253
14254 /*
James Smart6fb120a2009-05-22 14:52:59 -040014255 * If the driver is running low on rpi resources, allocate another
14256 * page now. Note that the next_rpi value is used because
14257 * it represents how many are actually in use whereas max_rpi notes
14258 * how many are supported max by the device.
14259 */
James Smart6d368e52011-05-24 11:44:12 -040014260 rpi_remaining = phba->sli4_hba.next_rpi - phba->sli4_hba.rpi_count;
James Smart6fb120a2009-05-22 14:52:59 -040014261 spin_unlock_irq(&phba->hbalock);
14262 if (rpi_remaining < LPFC_RPI_LOW_WATER_MARK) {
14263 rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
14264 if (!rpi_hdr) {
14265 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14266 "2002 Error Could not grow rpi "
14267 "count\n");
14268 } else {
James Smart6d368e52011-05-24 11:44:12 -040014269 lrpi = rpi_hdr->start_rpi;
14270 rpi_hdr->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
James Smart6fb120a2009-05-22 14:52:59 -040014271 lpfc_sli4_post_rpi_hdr(phba, rpi_hdr);
14272 }
14273 }
14274
14275 return rpi;
14276}
14277
14278/**
14279 * lpfc_sli4_free_rpi - Release an rpi for reuse.
14280 * @phba: pointer to lpfc hba data structure.
14281 *
14282 * This routine is invoked to release an rpi to the pool of
14283 * available rpis maintained by the driver.
14284 **/
14285void
James Smartd7c47992010-06-08 18:31:54 -040014286__lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
14287{
14288 if (test_and_clear_bit(rpi, phba->sli4_hba.rpi_bmask)) {
14289 phba->sli4_hba.rpi_count--;
14290 phba->sli4_hba.max_cfg_param.rpi_used--;
14291 }
14292}
14293
14294/**
14295 * lpfc_sli4_free_rpi - Release an rpi for reuse.
14296 * @phba: pointer to lpfc hba data structure.
14297 *
14298 * This routine is invoked to release an rpi to the pool of
14299 * available rpis maintained by the driver.
14300 **/
14301void
James Smart6fb120a2009-05-22 14:52:59 -040014302lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
14303{
14304 spin_lock_irq(&phba->hbalock);
James Smartd7c47992010-06-08 18:31:54 -040014305 __lpfc_sli4_free_rpi(phba, rpi);
James Smart6fb120a2009-05-22 14:52:59 -040014306 spin_unlock_irq(&phba->hbalock);
14307}
14308
14309/**
14310 * lpfc_sli4_remove_rpis - Remove the rpi bitmask region
14311 * @phba: pointer to lpfc hba data structure.
14312 *
14313 * This routine is invoked to remove the memory region that
14314 * provided rpi via a bitmask.
14315 **/
14316void
14317lpfc_sli4_remove_rpis(struct lpfc_hba *phba)
14318{
14319 kfree(phba->sli4_hba.rpi_bmask);
James Smart6d368e52011-05-24 11:44:12 -040014320 kfree(phba->sli4_hba.rpi_ids);
14321 bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
James Smart6fb120a2009-05-22 14:52:59 -040014322}
14323
14324/**
14325 * lpfc_sli4_resume_rpi - Remove the rpi bitmask region
14326 * @phba: pointer to lpfc hba data structure.
14327 *
14328 * This routine is invoked to remove the memory region that
14329 * provided rpi via a bitmask.
14330 **/
14331int
14332lpfc_sli4_resume_rpi(struct lpfc_nodelist *ndlp)
14333{
14334 LPFC_MBOXQ_t *mboxq;
14335 struct lpfc_hba *phba = ndlp->phba;
14336 int rc;
14337
14338 /* The port is notified of the header region via a mailbox command. */
14339 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14340 if (!mboxq)
14341 return -ENOMEM;
14342
14343 /* Post all rpi memory regions to the port. */
14344 lpfc_resume_rpi(mboxq, ndlp);
14345 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
14346 if (rc == MBX_NOT_FINISHED) {
14347 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14348 "2010 Resume RPI Mailbox failed "
14349 "status %d, mbxStatus x%x\n", rc,
14350 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
14351 mempool_free(mboxq, phba->mbox_mem_pool);
14352 return -EIO;
14353 }
14354 return 0;
14355}
14356
14357/**
14358 * lpfc_sli4_init_vpi - Initialize a vpi with the port
James Smart76a95d72010-11-20 23:11:48 -050014359 * @vport: Pointer to the vport for which the vpi is being initialized
James Smart6fb120a2009-05-22 14:52:59 -040014360 *
James Smart76a95d72010-11-20 23:11:48 -050014361 * This routine is invoked to activate a vpi with the port.
James Smart6fb120a2009-05-22 14:52:59 -040014362 *
14363 * Returns:
14364 * 0 success
14365 * -Evalue otherwise
14366 **/
14367int
James Smart76a95d72010-11-20 23:11:48 -050014368lpfc_sli4_init_vpi(struct lpfc_vport *vport)
James Smart6fb120a2009-05-22 14:52:59 -040014369{
14370 LPFC_MBOXQ_t *mboxq;
14371 int rc = 0;
James Smart6a9c52c2009-10-02 15:16:51 -040014372 int retval = MBX_SUCCESS;
James Smart6fb120a2009-05-22 14:52:59 -040014373 uint32_t mbox_tmo;
James Smart76a95d72010-11-20 23:11:48 -050014374 struct lpfc_hba *phba = vport->phba;
James Smart6fb120a2009-05-22 14:52:59 -040014375 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14376 if (!mboxq)
14377 return -ENOMEM;
James Smart76a95d72010-11-20 23:11:48 -050014378 lpfc_init_vpi(phba, mboxq, vport->vpi);
James Smarta183a152011-10-10 21:32:43 -040014379 mbox_tmo = lpfc_mbox_tmo_val(phba, mboxq);
James Smart6fb120a2009-05-22 14:52:59 -040014380 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
James Smart6fb120a2009-05-22 14:52:59 -040014381 if (rc != MBX_SUCCESS) {
James Smart76a95d72010-11-20 23:11:48 -050014382 lpfc_printf_vlog(vport, KERN_ERR, LOG_SLI,
James Smart6fb120a2009-05-22 14:52:59 -040014383 "2022 INIT VPI Mailbox failed "
14384 "status %d, mbxStatus x%x\n", rc,
14385 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
James Smart6a9c52c2009-10-02 15:16:51 -040014386 retval = -EIO;
James Smart6fb120a2009-05-22 14:52:59 -040014387 }
James Smart6a9c52c2009-10-02 15:16:51 -040014388 if (rc != MBX_TIMEOUT)
James Smart76a95d72010-11-20 23:11:48 -050014389 mempool_free(mboxq, vport->phba->mbox_mem_pool);
James Smart6a9c52c2009-10-02 15:16:51 -040014390
14391 return retval;
James Smart6fb120a2009-05-22 14:52:59 -040014392}
14393
14394/**
14395 * lpfc_mbx_cmpl_add_fcf_record - add fcf mbox completion handler.
14396 * @phba: pointer to lpfc hba data structure.
14397 * @mboxq: Pointer to mailbox object.
14398 *
14399 * This routine is invoked to manually add a single FCF record. The caller
14400 * must pass a completely initialized FCF_Record. This routine takes
14401 * care of the nonembedded mailbox operations.
14402 **/
14403static void
14404lpfc_mbx_cmpl_add_fcf_record(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
14405{
14406 void *virt_addr;
14407 union lpfc_sli4_cfg_shdr *shdr;
14408 uint32_t shdr_status, shdr_add_status;
14409
14410 virt_addr = mboxq->sge_array->addr[0];
14411 /* The IOCTL status is embedded in the mailbox subheader. */
14412 shdr = (union lpfc_sli4_cfg_shdr *) virt_addr;
14413 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14414 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14415
14416 if ((shdr_status || shdr_add_status) &&
14417 (shdr_status != STATUS_FCF_IN_USE))
14418 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14419 "2558 ADD_FCF_RECORD mailbox failed with "
14420 "status x%x add_status x%x\n",
14421 shdr_status, shdr_add_status);
14422
14423 lpfc_sli4_mbox_cmd_free(phba, mboxq);
14424}
14425
14426/**
14427 * lpfc_sli4_add_fcf_record - Manually add an FCF Record.
14428 * @phba: pointer to lpfc hba data structure.
14429 * @fcf_record: pointer to the initialized fcf record to add.
14430 *
14431 * This routine is invoked to manually add a single FCF record. The caller
14432 * must pass a completely initialized FCF_Record. This routine takes
14433 * care of the nonembedded mailbox operations.
14434 **/
14435int
14436lpfc_sli4_add_fcf_record(struct lpfc_hba *phba, struct fcf_record *fcf_record)
14437{
14438 int rc = 0;
14439 LPFC_MBOXQ_t *mboxq;
14440 uint8_t *bytep;
14441 void *virt_addr;
14442 dma_addr_t phys_addr;
14443 struct lpfc_mbx_sge sge;
14444 uint32_t alloc_len, req_len;
14445 uint32_t fcfindex;
14446
14447 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14448 if (!mboxq) {
14449 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14450 "2009 Failed to allocate mbox for ADD_FCF cmd\n");
14451 return -ENOMEM;
14452 }
14453
14454 req_len = sizeof(struct fcf_record) + sizeof(union lpfc_sli4_cfg_shdr) +
14455 sizeof(uint32_t);
14456
14457 /* Allocate DMA memory and set up the non-embedded mailbox command */
14458 alloc_len = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
14459 LPFC_MBOX_OPCODE_FCOE_ADD_FCF,
14460 req_len, LPFC_SLI4_MBX_NEMBED);
14461 if (alloc_len < req_len) {
14462 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14463 "2523 Allocated DMA memory size (x%x) is "
14464 "less than the requested DMA memory "
14465 "size (x%x)\n", alloc_len, req_len);
14466 lpfc_sli4_mbox_cmd_free(phba, mboxq);
14467 return -ENOMEM;
14468 }
14469
14470 /*
14471 * Get the first SGE entry from the non-embedded DMA memory. This
14472 * routine only uses a single SGE.
14473 */
14474 lpfc_sli4_mbx_sge_get(mboxq, 0, &sge);
14475 phys_addr = getPaddr(sge.pa_hi, sge.pa_lo);
James Smart6fb120a2009-05-22 14:52:59 -040014476 virt_addr = mboxq->sge_array->addr[0];
14477 /*
14478 * Configure the FCF record for FCFI 0. This is the driver's
14479 * hardcoded default and gets used in nonFIP mode.
14480 */
14481 fcfindex = bf_get(lpfc_fcf_record_fcf_index, fcf_record);
14482 bytep = virt_addr + sizeof(union lpfc_sli4_cfg_shdr);
14483 lpfc_sli_pcimem_bcopy(&fcfindex, bytep, sizeof(uint32_t));
14484
14485 /*
14486 * Copy the fcf_index and the FCF Record Data. The data starts after
14487 * the FCoE header plus word10. The data copy needs to be endian
14488 * correct.
14489 */
14490 bytep += sizeof(uint32_t);
14491 lpfc_sli_pcimem_bcopy(fcf_record, bytep, sizeof(struct fcf_record));
14492 mboxq->vport = phba->pport;
14493 mboxq->mbox_cmpl = lpfc_mbx_cmpl_add_fcf_record;
14494 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
14495 if (rc == MBX_NOT_FINISHED) {
14496 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14497 "2515 ADD_FCF_RECORD mailbox failed with "
14498 "status 0x%x\n", rc);
14499 lpfc_sli4_mbox_cmd_free(phba, mboxq);
14500 rc = -EIO;
14501 } else
14502 rc = 0;
14503
14504 return rc;
14505}
14506
14507/**
14508 * lpfc_sli4_build_dflt_fcf_record - Build the driver's default FCF Record.
14509 * @phba: pointer to lpfc hba data structure.
14510 * @fcf_record: pointer to the fcf record to write the default data.
14511 * @fcf_index: FCF table entry index.
14512 *
14513 * This routine is invoked to build the driver's default FCF record. The
14514 * values used are hardcoded. This routine handles memory initialization.
14515 *
14516 **/
14517void
14518lpfc_sli4_build_dflt_fcf_record(struct lpfc_hba *phba,
14519 struct fcf_record *fcf_record,
14520 uint16_t fcf_index)
14521{
14522 memset(fcf_record, 0, sizeof(struct fcf_record));
14523 fcf_record->max_rcv_size = LPFC_FCOE_MAX_RCV_SIZE;
14524 fcf_record->fka_adv_period = LPFC_FCOE_FKA_ADV_PER;
14525 fcf_record->fip_priority = LPFC_FCOE_FIP_PRIORITY;
14526 bf_set(lpfc_fcf_record_mac_0, fcf_record, phba->fc_map[0]);
14527 bf_set(lpfc_fcf_record_mac_1, fcf_record, phba->fc_map[1]);
14528 bf_set(lpfc_fcf_record_mac_2, fcf_record, phba->fc_map[2]);
14529 bf_set(lpfc_fcf_record_mac_3, fcf_record, LPFC_FCOE_FCF_MAC3);
14530 bf_set(lpfc_fcf_record_mac_4, fcf_record, LPFC_FCOE_FCF_MAC4);
14531 bf_set(lpfc_fcf_record_mac_5, fcf_record, LPFC_FCOE_FCF_MAC5);
14532 bf_set(lpfc_fcf_record_fc_map_0, fcf_record, phba->fc_map[0]);
14533 bf_set(lpfc_fcf_record_fc_map_1, fcf_record, phba->fc_map[1]);
14534 bf_set(lpfc_fcf_record_fc_map_2, fcf_record, phba->fc_map[2]);
14535 bf_set(lpfc_fcf_record_fcf_valid, fcf_record, 1);
James Smart0c287582009-06-10 17:22:56 -040014536 bf_set(lpfc_fcf_record_fcf_avail, fcf_record, 1);
James Smart6fb120a2009-05-22 14:52:59 -040014537 bf_set(lpfc_fcf_record_fcf_index, fcf_record, fcf_index);
14538 bf_set(lpfc_fcf_record_mac_addr_prov, fcf_record,
14539 LPFC_FCF_FPMA | LPFC_FCF_SPMA);
14540 /* Set the VLAN bit map */
14541 if (phba->valid_vlan) {
14542 fcf_record->vlan_bitmap[phba->vlan_id / 8]
14543 = 1 << (phba->vlan_id % 8);
14544 }
14545}
14546
14547/**
James Smart0c9ab6f2010-02-26 14:15:57 -050014548 * lpfc_sli4_fcf_scan_read_fcf_rec - Read hba fcf record for fcf scan.
James Smart6fb120a2009-05-22 14:52:59 -040014549 * @phba: pointer to lpfc hba data structure.
14550 * @fcf_index: FCF table entry offset.
14551 *
James Smart0c9ab6f2010-02-26 14:15:57 -050014552 * This routine is invoked to scan the entire FCF table by reading FCF
14553 * record and processing it one at a time starting from the @fcf_index
14554 * for initial FCF discovery or fast FCF failover rediscovery.
14555 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030014556 * Return 0 if the mailbox command is submitted successfully, none 0
James Smart0c9ab6f2010-02-26 14:15:57 -050014557 * otherwise.
James Smart6fb120a2009-05-22 14:52:59 -040014558 **/
14559int
James Smart0c9ab6f2010-02-26 14:15:57 -050014560lpfc_sli4_fcf_scan_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
James Smart6fb120a2009-05-22 14:52:59 -040014561{
14562 int rc = 0, error;
14563 LPFC_MBOXQ_t *mboxq;
James Smart6fb120a2009-05-22 14:52:59 -040014564
James Smart32b97932009-07-19 10:01:21 -040014565 phba->fcoe_eventtag_at_fcf_scan = phba->fcoe_eventtag;
James Smart6fb120a2009-05-22 14:52:59 -040014566 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14567 if (!mboxq) {
14568 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14569 "2000 Failed to allocate mbox for "
14570 "READ_FCF cmd\n");
James Smart4d9ab992009-10-02 15:16:39 -040014571 error = -ENOMEM;
James Smart0c9ab6f2010-02-26 14:15:57 -050014572 goto fail_fcf_scan;
James Smart6fb120a2009-05-22 14:52:59 -040014573 }
James Smartecfd03c2010-02-12 14:41:27 -050014574 /* Construct the read FCF record mailbox command */
James Smart0c9ab6f2010-02-26 14:15:57 -050014575 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
James Smartecfd03c2010-02-12 14:41:27 -050014576 if (rc) {
14577 error = -EINVAL;
James Smart0c9ab6f2010-02-26 14:15:57 -050014578 goto fail_fcf_scan;
James Smart6fb120a2009-05-22 14:52:59 -040014579 }
James Smartecfd03c2010-02-12 14:41:27 -050014580 /* Issue the mailbox command asynchronously */
James Smart6fb120a2009-05-22 14:52:59 -040014581 mboxq->vport = phba->pport;
James Smart0c9ab6f2010-02-26 14:15:57 -050014582 mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_scan_read_fcf_rec;
James Smarta93ff372010-10-22 11:06:08 -040014583
14584 spin_lock_irq(&phba->hbalock);
14585 phba->hba_flag |= FCF_TS_INPROG;
14586 spin_unlock_irq(&phba->hbalock);
14587
James Smart6fb120a2009-05-22 14:52:59 -040014588 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
James Smartecfd03c2010-02-12 14:41:27 -050014589 if (rc == MBX_NOT_FINISHED)
James Smart6fb120a2009-05-22 14:52:59 -040014590 error = -EIO;
James Smartecfd03c2010-02-12 14:41:27 -050014591 else {
James Smart38b92ef2010-08-04 16:11:39 -040014592 /* Reset eligible FCF count for new scan */
14593 if (fcf_index == LPFC_FCOE_FCF_GET_FIRST)
James Smart999d8132010-03-15 11:24:56 -040014594 phba->fcf.eligible_fcf_cnt = 0;
James Smart6fb120a2009-05-22 14:52:59 -040014595 error = 0;
James Smart32b97932009-07-19 10:01:21 -040014596 }
James Smart0c9ab6f2010-02-26 14:15:57 -050014597fail_fcf_scan:
James Smart4d9ab992009-10-02 15:16:39 -040014598 if (error) {
14599 if (mboxq)
14600 lpfc_sli4_mbox_cmd_free(phba, mboxq);
James Smarta93ff372010-10-22 11:06:08 -040014601 /* FCF scan failed, clear FCF_TS_INPROG flag */
James Smart4d9ab992009-10-02 15:16:39 -040014602 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -040014603 phba->hba_flag &= ~FCF_TS_INPROG;
James Smart4d9ab992009-10-02 15:16:39 -040014604 spin_unlock_irq(&phba->hbalock);
14605 }
James Smart6fb120a2009-05-22 14:52:59 -040014606 return error;
14607}
James Smarta0c87cb2009-07-19 10:01:10 -040014608
14609/**
James Smarta93ff372010-10-22 11:06:08 -040014610 * lpfc_sli4_fcf_rr_read_fcf_rec - Read hba fcf record for roundrobin fcf.
James Smart0c9ab6f2010-02-26 14:15:57 -050014611 * @phba: pointer to lpfc hba data structure.
14612 * @fcf_index: FCF table entry offset.
14613 *
14614 * This routine is invoked to read an FCF record indicated by @fcf_index
James Smarta93ff372010-10-22 11:06:08 -040014615 * and to use it for FLOGI roundrobin FCF failover.
James Smart0c9ab6f2010-02-26 14:15:57 -050014616 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030014617 * Return 0 if the mailbox command is submitted successfully, none 0
James Smart0c9ab6f2010-02-26 14:15:57 -050014618 * otherwise.
14619 **/
14620int
14621lpfc_sli4_fcf_rr_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
14622{
14623 int rc = 0, error;
14624 LPFC_MBOXQ_t *mboxq;
14625
14626 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14627 if (!mboxq) {
14628 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
14629 "2763 Failed to allocate mbox for "
14630 "READ_FCF cmd\n");
14631 error = -ENOMEM;
14632 goto fail_fcf_read;
14633 }
14634 /* Construct the read FCF record mailbox command */
14635 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
14636 if (rc) {
14637 error = -EINVAL;
14638 goto fail_fcf_read;
14639 }
14640 /* Issue the mailbox command asynchronously */
14641 mboxq->vport = phba->pport;
14642 mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_rr_read_fcf_rec;
14643 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
14644 if (rc == MBX_NOT_FINISHED)
14645 error = -EIO;
14646 else
14647 error = 0;
14648
14649fail_fcf_read:
14650 if (error && mboxq)
14651 lpfc_sli4_mbox_cmd_free(phba, mboxq);
14652 return error;
14653}
14654
14655/**
14656 * lpfc_sli4_read_fcf_rec - Read hba fcf record for update eligible fcf bmask.
14657 * @phba: pointer to lpfc hba data structure.
14658 * @fcf_index: FCF table entry offset.
14659 *
14660 * This routine is invoked to read an FCF record indicated by @fcf_index to
James Smarta93ff372010-10-22 11:06:08 -040014661 * determine whether it's eligible for FLOGI roundrobin failover list.
James Smart0c9ab6f2010-02-26 14:15:57 -050014662 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030014663 * Return 0 if the mailbox command is submitted successfully, none 0
James Smart0c9ab6f2010-02-26 14:15:57 -050014664 * otherwise.
14665 **/
14666int
14667lpfc_sli4_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
14668{
14669 int rc = 0, error;
14670 LPFC_MBOXQ_t *mboxq;
14671
14672 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14673 if (!mboxq) {
14674 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
14675 "2758 Failed to allocate mbox for "
14676 "READ_FCF cmd\n");
14677 error = -ENOMEM;
14678 goto fail_fcf_read;
14679 }
14680 /* Construct the read FCF record mailbox command */
14681 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
14682 if (rc) {
14683 error = -EINVAL;
14684 goto fail_fcf_read;
14685 }
14686 /* Issue the mailbox command asynchronously */
14687 mboxq->vport = phba->pport;
14688 mboxq->mbox_cmpl = lpfc_mbx_cmpl_read_fcf_rec;
14689 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
14690 if (rc == MBX_NOT_FINISHED)
14691 error = -EIO;
14692 else
14693 error = 0;
14694
14695fail_fcf_read:
14696 if (error && mboxq)
14697 lpfc_sli4_mbox_cmd_free(phba, mboxq);
14698 return error;
14699}
14700
14701/**
James Smart7d791df2011-07-22 18:37:52 -040014702 * lpfc_check_next_fcf_pri
14703 * phba pointer to the lpfc_hba struct for this port.
14704 * This routine is called from the lpfc_sli4_fcf_rr_next_index_get
14705 * routine when the rr_bmask is empty. The FCF indecies are put into the
14706 * rr_bmask based on their priority level. Starting from the highest priority
14707 * to the lowest. The most likely FCF candidate will be in the highest
14708 * priority group. When this routine is called it searches the fcf_pri list for
14709 * next lowest priority group and repopulates the rr_bmask with only those
14710 * fcf_indexes.
14711 * returns:
14712 * 1=success 0=failure
14713 **/
14714int
14715lpfc_check_next_fcf_pri_level(struct lpfc_hba *phba)
14716{
14717 uint16_t next_fcf_pri;
14718 uint16_t last_index;
14719 struct lpfc_fcf_pri *fcf_pri;
14720 int rc;
14721 int ret = 0;
14722
14723 last_index = find_first_bit(phba->fcf.fcf_rr_bmask,
14724 LPFC_SLI4_FCF_TBL_INDX_MAX);
14725 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
14726 "3060 Last IDX %d\n", last_index);
14727 if (list_empty(&phba->fcf.fcf_pri_list)) {
14728 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
14729 "3061 Last IDX %d\n", last_index);
14730 return 0; /* Empty rr list */
14731 }
14732 next_fcf_pri = 0;
14733 /*
14734 * Clear the rr_bmask and set all of the bits that are at this
14735 * priority.
14736 */
14737 memset(phba->fcf.fcf_rr_bmask, 0,
14738 sizeof(*phba->fcf.fcf_rr_bmask));
14739 spin_lock_irq(&phba->hbalock);
14740 list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
14741 if (fcf_pri->fcf_rec.flag & LPFC_FCF_FLOGI_FAILED)
14742 continue;
14743 /*
14744 * the 1st priority that has not FLOGI failed
14745 * will be the highest.
14746 */
14747 if (!next_fcf_pri)
14748 next_fcf_pri = fcf_pri->fcf_rec.priority;
14749 spin_unlock_irq(&phba->hbalock);
14750 if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
14751 rc = lpfc_sli4_fcf_rr_index_set(phba,
14752 fcf_pri->fcf_rec.fcf_index);
14753 if (rc)
14754 return 0;
14755 }
14756 spin_lock_irq(&phba->hbalock);
14757 }
14758 /*
14759 * if next_fcf_pri was not set above and the list is not empty then
14760 * we have failed flogis on all of them. So reset flogi failed
14761 * and start at the begining.
14762 */
14763 if (!next_fcf_pri && !list_empty(&phba->fcf.fcf_pri_list)) {
14764 list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
14765 fcf_pri->fcf_rec.flag &= ~LPFC_FCF_FLOGI_FAILED;
14766 /*
14767 * the 1st priority that has not FLOGI failed
14768 * will be the highest.
14769 */
14770 if (!next_fcf_pri)
14771 next_fcf_pri = fcf_pri->fcf_rec.priority;
14772 spin_unlock_irq(&phba->hbalock);
14773 if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
14774 rc = lpfc_sli4_fcf_rr_index_set(phba,
14775 fcf_pri->fcf_rec.fcf_index);
14776 if (rc)
14777 return 0;
14778 }
14779 spin_lock_irq(&phba->hbalock);
14780 }
14781 } else
14782 ret = 1;
14783 spin_unlock_irq(&phba->hbalock);
14784
14785 return ret;
14786}
14787/**
James Smart0c9ab6f2010-02-26 14:15:57 -050014788 * lpfc_sli4_fcf_rr_next_index_get - Get next eligible fcf record index
14789 * @phba: pointer to lpfc hba data structure.
14790 *
14791 * This routine is to get the next eligible FCF record index in a round
14792 * robin fashion. If the next eligible FCF record index equals to the
James Smarta93ff372010-10-22 11:06:08 -040014793 * initial roundrobin FCF record index, LPFC_FCOE_FCF_NEXT_NONE (0xFFFF)
James Smart0c9ab6f2010-02-26 14:15:57 -050014794 * shall be returned, otherwise, the next eligible FCF record's index
14795 * shall be returned.
14796 **/
14797uint16_t
14798lpfc_sli4_fcf_rr_next_index_get(struct lpfc_hba *phba)
14799{
14800 uint16_t next_fcf_index;
14801
James Smart3804dc82010-07-14 15:31:37 -040014802 /* Search start from next bit of currently registered FCF index */
James Smart7d791df2011-07-22 18:37:52 -040014803next_priority:
James Smart3804dc82010-07-14 15:31:37 -040014804 next_fcf_index = (phba->fcf.current_rec.fcf_indx + 1) %
14805 LPFC_SLI4_FCF_TBL_INDX_MAX;
James Smart0c9ab6f2010-02-26 14:15:57 -050014806 next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
14807 LPFC_SLI4_FCF_TBL_INDX_MAX,
James Smart3804dc82010-07-14 15:31:37 -040014808 next_fcf_index);
14809
James Smart0c9ab6f2010-02-26 14:15:57 -050014810 /* Wrap around condition on phba->fcf.fcf_rr_bmask */
James Smart7d791df2011-07-22 18:37:52 -040014811 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
14812 /*
14813 * If we have wrapped then we need to clear the bits that
14814 * have been tested so that we can detect when we should
14815 * change the priority level.
14816 */
James Smart0c9ab6f2010-02-26 14:15:57 -050014817 next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
14818 LPFC_SLI4_FCF_TBL_INDX_MAX, 0);
James Smart7d791df2011-07-22 18:37:52 -040014819 }
14820
James Smart0c9ab6f2010-02-26 14:15:57 -050014821
James Smart3804dc82010-07-14 15:31:37 -040014822 /* Check roundrobin failover list empty condition */
James Smart7d791df2011-07-22 18:37:52 -040014823 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX ||
14824 next_fcf_index == phba->fcf.current_rec.fcf_indx) {
14825 /*
14826 * If next fcf index is not found check if there are lower
14827 * Priority level fcf's in the fcf_priority list.
14828 * Set up the rr_bmask with all of the avaiable fcf bits
14829 * at that level and continue the selection process.
14830 */
14831 if (lpfc_check_next_fcf_pri_level(phba))
14832 goto next_priority;
James Smart3804dc82010-07-14 15:31:37 -040014833 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
14834 "2844 No roundrobin failover FCF available\n");
James Smart7d791df2011-07-22 18:37:52 -040014835 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX)
14836 return LPFC_FCOE_FCF_NEXT_NONE;
14837 else {
14838 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
14839 "3063 Only FCF available idx %d, flag %x\n",
14840 next_fcf_index,
14841 phba->fcf.fcf_pri[next_fcf_index].fcf_rec.flag);
14842 return next_fcf_index;
14843 }
James Smart3804dc82010-07-14 15:31:37 -040014844 }
14845
James Smart7d791df2011-07-22 18:37:52 -040014846 if (next_fcf_index < LPFC_SLI4_FCF_TBL_INDX_MAX &&
14847 phba->fcf.fcf_pri[next_fcf_index].fcf_rec.flag &
14848 LPFC_FCF_FLOGI_FAILED)
14849 goto next_priority;
14850
James Smart3804dc82010-07-14 15:31:37 -040014851 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040014852 "2845 Get next roundrobin failover FCF (x%x)\n",
14853 next_fcf_index);
14854
James Smart0c9ab6f2010-02-26 14:15:57 -050014855 return next_fcf_index;
14856}
14857
14858/**
14859 * lpfc_sli4_fcf_rr_index_set - Set bmask with eligible fcf record index
14860 * @phba: pointer to lpfc hba data structure.
14861 *
14862 * This routine sets the FCF record index in to the eligible bmask for
James Smarta93ff372010-10-22 11:06:08 -040014863 * roundrobin failover search. It checks to make sure that the index
James Smart0c9ab6f2010-02-26 14:15:57 -050014864 * does not go beyond the range of the driver allocated bmask dimension
14865 * before setting the bit.
14866 *
14867 * Returns 0 if the index bit successfully set, otherwise, it returns
14868 * -EINVAL.
14869 **/
14870int
14871lpfc_sli4_fcf_rr_index_set(struct lpfc_hba *phba, uint16_t fcf_index)
14872{
14873 if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
14874 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040014875 "2610 FCF (x%x) reached driver's book "
14876 "keeping dimension:x%x\n",
James Smart0c9ab6f2010-02-26 14:15:57 -050014877 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
14878 return -EINVAL;
14879 }
14880 /* Set the eligible FCF record index bmask */
14881 set_bit(fcf_index, phba->fcf.fcf_rr_bmask);
14882
James Smart3804dc82010-07-14 15:31:37 -040014883 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040014884 "2790 Set FCF (x%x) to roundrobin FCF failover "
James Smart3804dc82010-07-14 15:31:37 -040014885 "bmask\n", fcf_index);
14886
James Smart0c9ab6f2010-02-26 14:15:57 -050014887 return 0;
14888}
14889
14890/**
James Smart3804dc82010-07-14 15:31:37 -040014891 * lpfc_sli4_fcf_rr_index_clear - Clear bmask from eligible fcf record index
James Smart0c9ab6f2010-02-26 14:15:57 -050014892 * @phba: pointer to lpfc hba data structure.
14893 *
14894 * This routine clears the FCF record index from the eligible bmask for
James Smarta93ff372010-10-22 11:06:08 -040014895 * roundrobin failover search. It checks to make sure that the index
James Smart0c9ab6f2010-02-26 14:15:57 -050014896 * does not go beyond the range of the driver allocated bmask dimension
14897 * before clearing the bit.
14898 **/
14899void
14900lpfc_sli4_fcf_rr_index_clear(struct lpfc_hba *phba, uint16_t fcf_index)
14901{
James Smart7d791df2011-07-22 18:37:52 -040014902 struct lpfc_fcf_pri *fcf_pri;
James Smart0c9ab6f2010-02-26 14:15:57 -050014903 if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
14904 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040014905 "2762 FCF (x%x) reached driver's book "
14906 "keeping dimension:x%x\n",
James Smart0c9ab6f2010-02-26 14:15:57 -050014907 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
14908 return;
14909 }
14910 /* Clear the eligible FCF record index bmask */
James Smart7d791df2011-07-22 18:37:52 -040014911 spin_lock_irq(&phba->hbalock);
14912 list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
14913 if (fcf_pri->fcf_rec.fcf_index == fcf_index) {
14914 list_del_init(&fcf_pri->list);
14915 break;
14916 }
14917 }
14918 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -050014919 clear_bit(fcf_index, phba->fcf.fcf_rr_bmask);
James Smart3804dc82010-07-14 15:31:37 -040014920
14921 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040014922 "2791 Clear FCF (x%x) from roundrobin failover "
James Smart3804dc82010-07-14 15:31:37 -040014923 "bmask\n", fcf_index);
James Smart0c9ab6f2010-02-26 14:15:57 -050014924}
14925
14926/**
James Smartecfd03c2010-02-12 14:41:27 -050014927 * lpfc_mbx_cmpl_redisc_fcf_table - completion routine for rediscover FCF table
14928 * @phba: pointer to lpfc hba data structure.
14929 *
14930 * This routine is the completion routine for the rediscover FCF table mailbox
14931 * command. If the mailbox command returned failure, it will try to stop the
14932 * FCF rediscover wait timer.
14933 **/
14934void
14935lpfc_mbx_cmpl_redisc_fcf_table(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
14936{
14937 struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
14938 uint32_t shdr_status, shdr_add_status;
14939
14940 redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
14941
14942 shdr_status = bf_get(lpfc_mbox_hdr_status,
14943 &redisc_fcf->header.cfg_shdr.response);
14944 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
14945 &redisc_fcf->header.cfg_shdr.response);
14946 if (shdr_status || shdr_add_status) {
James Smart0c9ab6f2010-02-26 14:15:57 -050014947 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smartecfd03c2010-02-12 14:41:27 -050014948 "2746 Requesting for FCF rediscovery failed "
14949 "status x%x add_status x%x\n",
14950 shdr_status, shdr_add_status);
James Smart0c9ab6f2010-02-26 14:15:57 -050014951 if (phba->fcf.fcf_flag & FCF_ACVL_DISC) {
James Smartfc2b9892010-02-26 14:15:29 -050014952 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -050014953 phba->fcf.fcf_flag &= ~FCF_ACVL_DISC;
James Smartfc2b9892010-02-26 14:15:29 -050014954 spin_unlock_irq(&phba->hbalock);
14955 /*
14956 * CVL event triggered FCF rediscover request failed,
14957 * last resort to re-try current registered FCF entry.
14958 */
14959 lpfc_retry_pport_discovery(phba);
14960 } else {
14961 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -050014962 phba->fcf.fcf_flag &= ~FCF_DEAD_DISC;
James Smartfc2b9892010-02-26 14:15:29 -050014963 spin_unlock_irq(&phba->hbalock);
14964 /*
14965 * DEAD FCF event triggered FCF rediscover request
14966 * failed, last resort to fail over as a link down
14967 * to FCF registration.
14968 */
14969 lpfc_sli4_fcf_dead_failthrough(phba);
14970 }
James Smart0c9ab6f2010-02-26 14:15:57 -050014971 } else {
14972 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040014973 "2775 Start FCF rediscover quiescent timer\n");
James Smartecfd03c2010-02-12 14:41:27 -050014974 /*
14975 * Start FCF rediscovery wait timer for pending FCF
14976 * before rescan FCF record table.
14977 */
14978 lpfc_fcf_redisc_wait_start_timer(phba);
James Smart0c9ab6f2010-02-26 14:15:57 -050014979 }
James Smartecfd03c2010-02-12 14:41:27 -050014980
14981 mempool_free(mbox, phba->mbox_mem_pool);
14982}
14983
14984/**
James Smart3804dc82010-07-14 15:31:37 -040014985 * lpfc_sli4_redisc_fcf_table - Request to rediscover entire FCF table by port.
James Smartecfd03c2010-02-12 14:41:27 -050014986 * @phba: pointer to lpfc hba data structure.
14987 *
14988 * This routine is invoked to request for rediscovery of the entire FCF table
14989 * by the port.
14990 **/
14991int
14992lpfc_sli4_redisc_fcf_table(struct lpfc_hba *phba)
14993{
14994 LPFC_MBOXQ_t *mbox;
14995 struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
14996 int rc, length;
14997
James Smart0c9ab6f2010-02-26 14:15:57 -050014998 /* Cancel retry delay timers to all vports before FCF rediscover */
14999 lpfc_cancel_all_vport_retry_delay_timer(phba);
15000
James Smartecfd03c2010-02-12 14:41:27 -050015001 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15002 if (!mbox) {
15003 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15004 "2745 Failed to allocate mbox for "
15005 "requesting FCF rediscover.\n");
15006 return -ENOMEM;
15007 }
15008
15009 length = (sizeof(struct lpfc_mbx_redisc_fcf_tbl) -
15010 sizeof(struct lpfc_sli4_cfg_mhdr));
15011 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15012 LPFC_MBOX_OPCODE_FCOE_REDISCOVER_FCF,
15013 length, LPFC_SLI4_MBX_EMBED);
15014
15015 redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
15016 /* Set count to 0 for invalidating the entire FCF database */
15017 bf_set(lpfc_mbx_redisc_fcf_count, redisc_fcf, 0);
15018
15019 /* Issue the mailbox command asynchronously */
15020 mbox->vport = phba->pport;
15021 mbox->mbox_cmpl = lpfc_mbx_cmpl_redisc_fcf_table;
15022 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
15023
15024 if (rc == MBX_NOT_FINISHED) {
15025 mempool_free(mbox, phba->mbox_mem_pool);
15026 return -EIO;
15027 }
15028 return 0;
15029}
15030
15031/**
James Smartfc2b9892010-02-26 14:15:29 -050015032 * lpfc_sli4_fcf_dead_failthrough - Failthrough routine to fcf dead event
15033 * @phba: pointer to lpfc hba data structure.
15034 *
15035 * This function is the failover routine as a last resort to the FCF DEAD
15036 * event when driver failed to perform fast FCF failover.
15037 **/
15038void
15039lpfc_sli4_fcf_dead_failthrough(struct lpfc_hba *phba)
15040{
15041 uint32_t link_state;
15042
15043 /*
15044 * Last resort as FCF DEAD event failover will treat this as
15045 * a link down, but save the link state because we don't want
15046 * it to be changed to Link Down unless it is already down.
15047 */
15048 link_state = phba->link_state;
15049 lpfc_linkdown(phba);
15050 phba->link_state = link_state;
15051
15052 /* Unregister FCF if no devices connected to it */
15053 lpfc_unregister_unused_fcf(phba);
15054}
15055
15056/**
James Smarta0c87cb2009-07-19 10:01:10 -040015057 * lpfc_sli_read_link_ste - Read region 23 to decide if link is disabled.
15058 * @phba: pointer to lpfc hba data structure.
15059 *
15060 * This function read region 23 and parse TLV for port status to
15061 * decide if the user disaled the port. If the TLV indicates the
15062 * port is disabled, the hba_flag is set accordingly.
15063 **/
15064void
15065lpfc_sli_read_link_ste(struct lpfc_hba *phba)
15066{
15067 LPFC_MBOXQ_t *pmb = NULL;
15068 MAILBOX_t *mb;
15069 uint8_t *rgn23_data = NULL;
15070 uint32_t offset = 0, data_size, sub_tlv_len, tlv_offset;
15071 int rc;
15072
15073 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15074 if (!pmb) {
15075 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15076 "2600 lpfc_sli_read_serdes_param failed to"
15077 " allocate mailbox memory\n");
15078 goto out;
15079 }
15080 mb = &pmb->u.mb;
15081
15082 /* Get adapter Region 23 data */
15083 rgn23_data = kzalloc(DMP_RGN23_SIZE, GFP_KERNEL);
15084 if (!rgn23_data)
15085 goto out;
15086
15087 do {
15088 lpfc_dump_mem(phba, pmb, offset, DMP_REGION_23);
15089 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
15090
15091 if (rc != MBX_SUCCESS) {
15092 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
15093 "2601 lpfc_sli_read_link_ste failed to"
15094 " read config region 23 rc 0x%x Status 0x%x\n",
15095 rc, mb->mbxStatus);
15096 mb->un.varDmp.word_cnt = 0;
15097 }
15098 /*
15099 * dump mem may return a zero when finished or we got a
15100 * mailbox error, either way we are done.
15101 */
15102 if (mb->un.varDmp.word_cnt == 0)
15103 break;
15104 if (mb->un.varDmp.word_cnt > DMP_RGN23_SIZE - offset)
15105 mb->un.varDmp.word_cnt = DMP_RGN23_SIZE - offset;
15106
15107 lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
15108 rgn23_data + offset,
15109 mb->un.varDmp.word_cnt);
15110 offset += mb->un.varDmp.word_cnt;
15111 } while (mb->un.varDmp.word_cnt && offset < DMP_RGN23_SIZE);
15112
15113 data_size = offset;
15114 offset = 0;
15115
15116 if (!data_size)
15117 goto out;
15118
15119 /* Check the region signature first */
15120 if (memcmp(&rgn23_data[offset], LPFC_REGION23_SIGNATURE, 4)) {
15121 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15122 "2619 Config region 23 has bad signature\n");
15123 goto out;
15124 }
15125 offset += 4;
15126
15127 /* Check the data structure version */
15128 if (rgn23_data[offset] != LPFC_REGION23_VERSION) {
15129 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15130 "2620 Config region 23 has bad version\n");
15131 goto out;
15132 }
15133 offset += 4;
15134
15135 /* Parse TLV entries in the region */
15136 while (offset < data_size) {
15137 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC)
15138 break;
15139 /*
15140 * If the TLV is not driver specific TLV or driver id is
15141 * not linux driver id, skip the record.
15142 */
15143 if ((rgn23_data[offset] != DRIVER_SPECIFIC_TYPE) ||
15144 (rgn23_data[offset + 2] != LINUX_DRIVER_ID) ||
15145 (rgn23_data[offset + 3] != 0)) {
15146 offset += rgn23_data[offset + 1] * 4 + 4;
15147 continue;
15148 }
15149
15150 /* Driver found a driver specific TLV in the config region */
15151 sub_tlv_len = rgn23_data[offset + 1] * 4;
15152 offset += 4;
15153 tlv_offset = 0;
15154
15155 /*
15156 * Search for configured port state sub-TLV.
15157 */
15158 while ((offset < data_size) &&
15159 (tlv_offset < sub_tlv_len)) {
15160 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC) {
15161 offset += 4;
15162 tlv_offset += 4;
15163 break;
15164 }
15165 if (rgn23_data[offset] != PORT_STE_TYPE) {
15166 offset += rgn23_data[offset + 1] * 4 + 4;
15167 tlv_offset += rgn23_data[offset + 1] * 4 + 4;
15168 continue;
15169 }
15170
15171 /* This HBA contains PORT_STE configured */
15172 if (!rgn23_data[offset + 2])
15173 phba->hba_flag |= LINK_DISABLED;
15174
15175 goto out;
15176 }
15177 }
15178out:
15179 if (pmb)
15180 mempool_free(pmb, phba->mbox_mem_pool);
15181 kfree(rgn23_data);
15182 return;
15183}
James Smart695a8142010-01-26 23:08:03 -050015184
15185/**
James Smart52d52442011-05-24 11:42:45 -040015186 * lpfc_wr_object - write an object to the firmware
15187 * @phba: HBA structure that indicates port to create a queue on.
15188 * @dmabuf_list: list of dmabufs to write to the port.
15189 * @size: the total byte value of the objects to write to the port.
15190 * @offset: the current offset to be used to start the transfer.
15191 *
15192 * This routine will create a wr_object mailbox command to send to the port.
15193 * the mailbox command will be constructed using the dma buffers described in
15194 * @dmabuf_list to create a list of BDEs. This routine will fill in as many
15195 * BDEs that the imbedded mailbox can support. The @offset variable will be
15196 * used to indicate the starting offset of the transfer and will also return
15197 * the offset after the write object mailbox has completed. @size is used to
15198 * determine the end of the object and whether the eof bit should be set.
15199 *
15200 * Return 0 is successful and offset will contain the the new offset to use
15201 * for the next write.
15202 * Return negative value for error cases.
15203 **/
15204int
15205lpfc_wr_object(struct lpfc_hba *phba, struct list_head *dmabuf_list,
15206 uint32_t size, uint32_t *offset)
15207{
15208 struct lpfc_mbx_wr_object *wr_object;
15209 LPFC_MBOXQ_t *mbox;
15210 int rc = 0, i = 0;
15211 uint32_t shdr_status, shdr_add_status;
15212 uint32_t mbox_tmo;
15213 union lpfc_sli4_cfg_shdr *shdr;
15214 struct lpfc_dmabuf *dmabuf;
15215 uint32_t written = 0;
15216
15217 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15218 if (!mbox)
15219 return -ENOMEM;
15220
15221 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
15222 LPFC_MBOX_OPCODE_WRITE_OBJECT,
15223 sizeof(struct lpfc_mbx_wr_object) -
15224 sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
15225
15226 wr_object = (struct lpfc_mbx_wr_object *)&mbox->u.mqe.un.wr_object;
15227 wr_object->u.request.write_offset = *offset;
15228 sprintf((uint8_t *)wr_object->u.request.object_name, "/");
15229 wr_object->u.request.object_name[0] =
15230 cpu_to_le32(wr_object->u.request.object_name[0]);
15231 bf_set(lpfc_wr_object_eof, &wr_object->u.request, 0);
15232 list_for_each_entry(dmabuf, dmabuf_list, list) {
15233 if (i >= LPFC_MBX_WR_CONFIG_MAX_BDE || written >= size)
15234 break;
15235 wr_object->u.request.bde[i].addrLow = putPaddrLow(dmabuf->phys);
15236 wr_object->u.request.bde[i].addrHigh =
15237 putPaddrHigh(dmabuf->phys);
15238 if (written + SLI4_PAGE_SIZE >= size) {
15239 wr_object->u.request.bde[i].tus.f.bdeSize =
15240 (size - written);
15241 written += (size - written);
15242 bf_set(lpfc_wr_object_eof, &wr_object->u.request, 1);
15243 } else {
15244 wr_object->u.request.bde[i].tus.f.bdeSize =
15245 SLI4_PAGE_SIZE;
15246 written += SLI4_PAGE_SIZE;
15247 }
15248 i++;
15249 }
15250 wr_object->u.request.bde_count = i;
15251 bf_set(lpfc_wr_object_write_length, &wr_object->u.request, written);
15252 if (!phba->sli4_hba.intr_enable)
15253 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15254 else {
James Smarta183a152011-10-10 21:32:43 -040015255 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart52d52442011-05-24 11:42:45 -040015256 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
15257 }
15258 /* The IOCTL status is embedded in the mailbox subheader. */
15259 shdr = (union lpfc_sli4_cfg_shdr *) &wr_object->header.cfg_shdr;
15260 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15261 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15262 if (rc != MBX_TIMEOUT)
15263 mempool_free(mbox, phba->mbox_mem_pool);
15264 if (shdr_status || shdr_add_status || rc) {
15265 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15266 "3025 Write Object mailbox failed with "
15267 "status x%x add_status x%x, mbx status x%x\n",
15268 shdr_status, shdr_add_status, rc);
15269 rc = -ENXIO;
15270 } else
15271 *offset += wr_object->u.response.actual_write_length;
15272 return rc;
15273}
15274
15275/**
James Smart695a8142010-01-26 23:08:03 -050015276 * lpfc_cleanup_pending_mbox - Free up vport discovery mailbox commands.
15277 * @vport: pointer to vport data structure.
15278 *
15279 * This function iterate through the mailboxq and clean up all REG_LOGIN
15280 * and REG_VPI mailbox commands associated with the vport. This function
15281 * is called when driver want to restart discovery of the vport due to
15282 * a Clear Virtual Link event.
15283 **/
15284void
15285lpfc_cleanup_pending_mbox(struct lpfc_vport *vport)
15286{
15287 struct lpfc_hba *phba = vport->phba;
15288 LPFC_MBOXQ_t *mb, *nextmb;
15289 struct lpfc_dmabuf *mp;
James Smart78730cf2010-04-06 15:06:30 -040015290 struct lpfc_nodelist *ndlp;
James Smartd439d282010-09-29 11:18:45 -040015291 struct lpfc_nodelist *act_mbx_ndlp = NULL;
James Smart589a52d2010-07-14 15:30:54 -040015292 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smartd439d282010-09-29 11:18:45 -040015293 LIST_HEAD(mbox_cmd_list);
James Smart63e801c2010-11-20 23:14:19 -050015294 uint8_t restart_loop;
James Smart695a8142010-01-26 23:08:03 -050015295
James Smartd439d282010-09-29 11:18:45 -040015296 /* Clean up internally queued mailbox commands with the vport */
James Smart695a8142010-01-26 23:08:03 -050015297 spin_lock_irq(&phba->hbalock);
15298 list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
15299 if (mb->vport != vport)
15300 continue;
15301
15302 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
15303 (mb->u.mb.mbxCommand != MBX_REG_VPI))
15304 continue;
15305
James Smartd439d282010-09-29 11:18:45 -040015306 list_del(&mb->list);
15307 list_add_tail(&mb->list, &mbox_cmd_list);
15308 }
15309 /* Clean up active mailbox command with the vport */
15310 mb = phba->sli.mbox_active;
15311 if (mb && (mb->vport == vport)) {
15312 if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) ||
15313 (mb->u.mb.mbxCommand == MBX_REG_VPI))
15314 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
15315 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
15316 act_mbx_ndlp = (struct lpfc_nodelist *)mb->context2;
15317 /* Put reference count for delayed processing */
15318 act_mbx_ndlp = lpfc_nlp_get(act_mbx_ndlp);
15319 /* Unregister the RPI when mailbox complete */
15320 mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
15321 }
15322 }
James Smart63e801c2010-11-20 23:14:19 -050015323 /* Cleanup any mailbox completions which are not yet processed */
15324 do {
15325 restart_loop = 0;
15326 list_for_each_entry(mb, &phba->sli.mboxq_cmpl, list) {
15327 /*
15328 * If this mailox is already processed or it is
15329 * for another vport ignore it.
15330 */
15331 if ((mb->vport != vport) ||
15332 (mb->mbox_flag & LPFC_MBX_IMED_UNREG))
15333 continue;
15334
15335 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
15336 (mb->u.mb.mbxCommand != MBX_REG_VPI))
15337 continue;
15338
15339 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
15340 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
15341 ndlp = (struct lpfc_nodelist *)mb->context2;
15342 /* Unregister the RPI when mailbox complete */
15343 mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
15344 restart_loop = 1;
15345 spin_unlock_irq(&phba->hbalock);
15346 spin_lock(shost->host_lock);
15347 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
15348 spin_unlock(shost->host_lock);
15349 spin_lock_irq(&phba->hbalock);
15350 break;
15351 }
15352 }
15353 } while (restart_loop);
15354
James Smartd439d282010-09-29 11:18:45 -040015355 spin_unlock_irq(&phba->hbalock);
15356
15357 /* Release the cleaned-up mailbox commands */
15358 while (!list_empty(&mbox_cmd_list)) {
15359 list_remove_head(&mbox_cmd_list, mb, LPFC_MBOXQ_t, list);
James Smart695a8142010-01-26 23:08:03 -050015360 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
15361 mp = (struct lpfc_dmabuf *) (mb->context1);
15362 if (mp) {
15363 __lpfc_mbuf_free(phba, mp->virt, mp->phys);
15364 kfree(mp);
15365 }
James Smart78730cf2010-04-06 15:06:30 -040015366 ndlp = (struct lpfc_nodelist *) mb->context2;
James Smartd439d282010-09-29 11:18:45 -040015367 mb->context2 = NULL;
James Smart78730cf2010-04-06 15:06:30 -040015368 if (ndlp) {
Dan Carpenterec21b3b2010-08-08 00:15:17 +020015369 spin_lock(shost->host_lock);
James Smart589a52d2010-07-14 15:30:54 -040015370 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
Dan Carpenterec21b3b2010-08-08 00:15:17 +020015371 spin_unlock(shost->host_lock);
James Smart78730cf2010-04-06 15:06:30 -040015372 lpfc_nlp_put(ndlp);
James Smart78730cf2010-04-06 15:06:30 -040015373 }
James Smart695a8142010-01-26 23:08:03 -050015374 }
James Smart695a8142010-01-26 23:08:03 -050015375 mempool_free(mb, phba->mbox_mem_pool);
15376 }
James Smartd439d282010-09-29 11:18:45 -040015377
15378 /* Release the ndlp with the cleaned-up active mailbox command */
15379 if (act_mbx_ndlp) {
15380 spin_lock(shost->host_lock);
15381 act_mbx_ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
15382 spin_unlock(shost->host_lock);
15383 lpfc_nlp_put(act_mbx_ndlp);
James Smart695a8142010-01-26 23:08:03 -050015384 }
James Smart695a8142010-01-26 23:08:03 -050015385}
15386
James Smart2a9bf3d2010-06-07 15:24:45 -040015387/**
15388 * lpfc_drain_txq - Drain the txq
15389 * @phba: Pointer to HBA context object.
15390 *
15391 * This function attempt to submit IOCBs on the txq
15392 * to the adapter. For SLI4 adapters, the txq contains
15393 * ELS IOCBs that have been deferred because the there
15394 * are no SGLs. This congestion can occur with large
15395 * vport counts during node discovery.
15396 **/
15397
15398uint32_t
15399lpfc_drain_txq(struct lpfc_hba *phba)
15400{
15401 LIST_HEAD(completions);
15402 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
15403 struct lpfc_iocbq *piocbq = 0;
15404 unsigned long iflags = 0;
15405 char *fail_msg = NULL;
15406 struct lpfc_sglq *sglq;
15407 union lpfc_wqe wqe;
15408
15409 spin_lock_irqsave(&phba->hbalock, iflags);
15410 if (pring->txq_cnt > pring->txq_max)
15411 pring->txq_max = pring->txq_cnt;
15412
15413 spin_unlock_irqrestore(&phba->hbalock, iflags);
15414
15415 while (pring->txq_cnt) {
15416 spin_lock_irqsave(&phba->hbalock, iflags);
15417
James Smart19ca7602010-11-20 23:11:55 -050015418 piocbq = lpfc_sli_ringtx_get(phba, pring);
15419 sglq = __lpfc_sli_get_sglq(phba, piocbq);
James Smart2a9bf3d2010-06-07 15:24:45 -040015420 if (!sglq) {
James Smart19ca7602010-11-20 23:11:55 -050015421 __lpfc_sli_ringtx_put(phba, pring, piocbq);
James Smart2a9bf3d2010-06-07 15:24:45 -040015422 spin_unlock_irqrestore(&phba->hbalock, iflags);
15423 break;
15424 } else {
James Smart2a9bf3d2010-06-07 15:24:45 -040015425 if (!piocbq) {
15426 /* The txq_cnt out of sync. This should
15427 * never happen
15428 */
15429 sglq = __lpfc_clear_active_sglq(phba,
James Smart6d368e52011-05-24 11:44:12 -040015430 sglq->sli4_lxritag);
James Smart2a9bf3d2010-06-07 15:24:45 -040015431 spin_unlock_irqrestore(&phba->hbalock, iflags);
15432 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15433 "2823 txq empty and txq_cnt is %d\n ",
15434 pring->txq_cnt);
15435 break;
15436 }
15437 }
15438
15439 /* The xri and iocb resources secured,
15440 * attempt to issue request
15441 */
James Smart6d368e52011-05-24 11:44:12 -040015442 piocbq->sli4_lxritag = sglq->sli4_lxritag;
James Smart2a9bf3d2010-06-07 15:24:45 -040015443 piocbq->sli4_xritag = sglq->sli4_xritag;
15444 if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocbq, sglq))
15445 fail_msg = "to convert bpl to sgl";
15446 else if (lpfc_sli4_iocb2wqe(phba, piocbq, &wqe))
15447 fail_msg = "to convert iocb to wqe";
15448 else if (lpfc_sli4_wq_put(phba->sli4_hba.els_wq, &wqe))
15449 fail_msg = " - Wq is full";
15450 else
15451 lpfc_sli_ringtxcmpl_put(phba, pring, piocbq);
15452
15453 if (fail_msg) {
15454 /* Failed means we can't issue and need to cancel */
15455 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15456 "2822 IOCB failed %s iotag 0x%x "
15457 "xri 0x%x\n",
15458 fail_msg,
15459 piocbq->iotag, piocbq->sli4_xritag);
15460 list_add_tail(&piocbq->list, &completions);
15461 }
15462 spin_unlock_irqrestore(&phba->hbalock, iflags);
15463 }
15464
James Smart2a9bf3d2010-06-07 15:24:45 -040015465 /* Cancel all the IOCBs that cannot be issued */
15466 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
15467 IOERR_SLI_ABORTED);
15468
15469 return pring->txq_cnt;
15470}