blob: 580b00bc35ceb494dabb23385f0e0caa717201c9 [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 Smart4f774512009-05-22 14:52:35 -040068static IOCB_t *
69lpfc_get_iocb_from_iocbq(struct lpfc_iocbq *iocbq)
70{
71 return &iocbq->iocb;
72}
73
74/**
75 * lpfc_sli4_wq_put - Put a Work Queue Entry on an Work Queue
76 * @q: The Work Queue to operate on.
77 * @wqe: The work Queue Entry to put on the Work queue.
78 *
79 * This routine will copy the contents of @wqe to the next available entry on
80 * the @q. This function will then ring the Work Queue Doorbell to signal the
81 * HBA to start processing the Work Queue Entry. This function returns 0 if
82 * successful. If no entries are available on @q then this function will return
83 * -ENOMEM.
84 * The caller is expected to hold the hbalock when calling this routine.
85 **/
86static uint32_t
87lpfc_sli4_wq_put(struct lpfc_queue *q, union lpfc_wqe *wqe)
88{
89 union lpfc_wqe *temp_wqe = q->qe[q->host_index].wqe;
90 struct lpfc_register doorbell;
91 uint32_t host_index;
92
93 /* If the host has not yet processed the next entry then we are done */
94 if (((q->host_index + 1) % q->entry_count) == q->hba_index)
95 return -ENOMEM;
96 /* set consumption flag every once in a while */
97 if (!((q->host_index + 1) % LPFC_RELEASE_NOTIFICATION_INTERVAL))
James Smartf0d9bcc2010-10-22 11:07:09 -040098 bf_set(wqe_wqec, &wqe->generic.wqe_com, 1);
James Smartfedd3b72011-02-16 12:39:24 -050099 if (q->phba->sli3_options & LPFC_SLI4_PHWQ_ENABLED)
100 bf_set(wqe_wqid, &wqe->generic.wqe_com, q->queue_id);
James Smart4f774512009-05-22 14:52:35 -0400101 lpfc_sli_pcimem_bcopy(wqe, temp_wqe, q->entry_size);
102
103 /* Update the host index before invoking device */
104 host_index = q->host_index;
105 q->host_index = ((q->host_index + 1) % q->entry_count);
106
107 /* Ring Doorbell */
108 doorbell.word0 = 0;
109 bf_set(lpfc_wq_doorbell_num_posted, &doorbell, 1);
110 bf_set(lpfc_wq_doorbell_index, &doorbell, host_index);
111 bf_set(lpfc_wq_doorbell_id, &doorbell, q->queue_id);
112 writel(doorbell.word0, q->phba->sli4_hba.WQDBregaddr);
113 readl(q->phba->sli4_hba.WQDBregaddr); /* Flush */
114
115 return 0;
116}
117
118/**
119 * lpfc_sli4_wq_release - Updates internal hba index for WQ
120 * @q: The Work Queue to operate on.
121 * @index: The index to advance the hba index to.
122 *
123 * This routine will update the HBA index of a queue to reflect consumption of
124 * Work Queue Entries by the HBA. When the HBA indicates that it has consumed
125 * an entry the host calls this function to update the queue's internal
126 * pointers. This routine returns the number of entries that were consumed by
127 * the HBA.
128 **/
129static uint32_t
130lpfc_sli4_wq_release(struct lpfc_queue *q, uint32_t index)
131{
132 uint32_t released = 0;
133
134 if (q->hba_index == index)
135 return 0;
136 do {
137 q->hba_index = ((q->hba_index + 1) % q->entry_count);
138 released++;
139 } while (q->hba_index != index);
140 return released;
141}
142
143/**
144 * lpfc_sli4_mq_put - Put a Mailbox Queue Entry on an Mailbox Queue
145 * @q: The Mailbox Queue to operate on.
146 * @wqe: The Mailbox Queue Entry to put on the Work queue.
147 *
148 * This routine will copy the contents of @mqe to the next available entry on
149 * the @q. This function will then ring the Work Queue Doorbell to signal the
150 * HBA to start processing the Work Queue Entry. This function returns 0 if
151 * successful. If no entries are available on @q then this function will return
152 * -ENOMEM.
153 * The caller is expected to hold the hbalock when calling this routine.
154 **/
155static uint32_t
156lpfc_sli4_mq_put(struct lpfc_queue *q, struct lpfc_mqe *mqe)
157{
158 struct lpfc_mqe *temp_mqe = q->qe[q->host_index].mqe;
159 struct lpfc_register doorbell;
160 uint32_t host_index;
161
162 /* If the host has not yet processed the next entry then we are done */
163 if (((q->host_index + 1) % q->entry_count) == q->hba_index)
164 return -ENOMEM;
165 lpfc_sli_pcimem_bcopy(mqe, temp_mqe, q->entry_size);
166 /* Save off the mailbox pointer for completion */
167 q->phba->mbox = (MAILBOX_t *)temp_mqe;
168
169 /* Update the host index before invoking device */
170 host_index = q->host_index;
171 q->host_index = ((q->host_index + 1) % q->entry_count);
172
173 /* Ring Doorbell */
174 doorbell.word0 = 0;
175 bf_set(lpfc_mq_doorbell_num_posted, &doorbell, 1);
176 bf_set(lpfc_mq_doorbell_id, &doorbell, q->queue_id);
177 writel(doorbell.word0, q->phba->sli4_hba.MQDBregaddr);
178 readl(q->phba->sli4_hba.MQDBregaddr); /* Flush */
179 return 0;
180}
181
182/**
183 * lpfc_sli4_mq_release - Updates internal hba index for MQ
184 * @q: The Mailbox Queue to operate on.
185 *
186 * This routine will update the HBA index of a queue to reflect consumption of
187 * a Mailbox Queue Entry by the HBA. When the HBA indicates that it has consumed
188 * an entry the host calls this function to update the queue's internal
189 * pointers. This routine returns the number of entries that were consumed by
190 * the HBA.
191 **/
192static uint32_t
193lpfc_sli4_mq_release(struct lpfc_queue *q)
194{
195 /* Clear the mailbox pointer for completion */
196 q->phba->mbox = NULL;
197 q->hba_index = ((q->hba_index + 1) % q->entry_count);
198 return 1;
199}
200
201/**
202 * lpfc_sli4_eq_get - Gets the next valid EQE from a EQ
203 * @q: The Event Queue to get the first valid EQE from
204 *
205 * This routine will get the first valid Event Queue Entry from @q, update
206 * the queue's internal hba index, and return the EQE. If no valid EQEs are in
207 * the Queue (no more work to do), or the Queue is full of EQEs that have been
208 * processed, but not popped back to the HBA then this routine will return NULL.
209 **/
210static struct lpfc_eqe *
211lpfc_sli4_eq_get(struct lpfc_queue *q)
212{
213 struct lpfc_eqe *eqe = q->qe[q->hba_index].eqe;
214
215 /* If the next EQE is not valid then we are done */
James Smartcb5172e2010-03-15 11:25:07 -0400216 if (!bf_get_le32(lpfc_eqe_valid, eqe))
James Smart4f774512009-05-22 14:52:35 -0400217 return NULL;
218 /* If the host has not yet processed the next entry then we are done */
219 if (((q->hba_index + 1) % q->entry_count) == q->host_index)
220 return NULL;
221
222 q->hba_index = ((q->hba_index + 1) % q->entry_count);
223 return eqe;
224}
225
226/**
227 * lpfc_sli4_eq_release - Indicates the host has finished processing an EQ
228 * @q: The Event Queue that the host has completed processing for.
229 * @arm: Indicates whether the host wants to arms this CQ.
230 *
231 * This routine will mark all Event Queue Entries on @q, from the last
232 * known completed entry to the last entry that was processed, as completed
233 * by clearing the valid bit for each completion queue entry. Then it will
234 * notify the HBA, by ringing the doorbell, that the EQEs have been processed.
235 * The internal host index in the @q will be updated by this routine to indicate
236 * that the host has finished processing the entries. The @arm parameter
237 * indicates that the queue should be rearmed when ringing the doorbell.
238 *
239 * This function will return the number of EQEs that were popped.
240 **/
241uint32_t
242lpfc_sli4_eq_release(struct lpfc_queue *q, bool arm)
243{
244 uint32_t released = 0;
245 struct lpfc_eqe *temp_eqe;
246 struct lpfc_register doorbell;
247
248 /* while there are valid entries */
249 while (q->hba_index != q->host_index) {
250 temp_eqe = q->qe[q->host_index].eqe;
James Smartcb5172e2010-03-15 11:25:07 -0400251 bf_set_le32(lpfc_eqe_valid, temp_eqe, 0);
James Smart4f774512009-05-22 14:52:35 -0400252 released++;
253 q->host_index = ((q->host_index + 1) % q->entry_count);
254 }
255 if (unlikely(released == 0 && !arm))
256 return 0;
257
258 /* ring doorbell for number popped */
259 doorbell.word0 = 0;
260 if (arm) {
261 bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);
262 bf_set(lpfc_eqcq_doorbell_eqci, &doorbell, 1);
263 }
264 bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, released);
265 bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_EVENT);
266 bf_set(lpfc_eqcq_doorbell_eqid, &doorbell, q->queue_id);
267 writel(doorbell.word0, q->phba->sli4_hba.EQCQDBregaddr);
James Smarta747c9c2009-11-18 15:41:10 -0500268 /* PCI read to flush PCI pipeline on re-arming for INTx mode */
269 if ((q->phba->intr_type == INTx) && (arm == LPFC_QUEUE_REARM))
270 readl(q->phba->sli4_hba.EQCQDBregaddr);
James Smart4f774512009-05-22 14:52:35 -0400271 return released;
272}
273
274/**
275 * lpfc_sli4_cq_get - Gets the next valid CQE from a CQ
276 * @q: The Completion Queue to get the first valid CQE from
277 *
278 * This routine will get the first valid Completion Queue Entry from @q, update
279 * the queue's internal hba index, and return the CQE. If no valid CQEs are in
280 * the Queue (no more work to do), or the Queue is full of CQEs that have been
281 * processed, but not popped back to the HBA then this routine will return NULL.
282 **/
283static struct lpfc_cqe *
284lpfc_sli4_cq_get(struct lpfc_queue *q)
285{
286 struct lpfc_cqe *cqe;
287
288 /* If the next CQE is not valid then we are done */
James Smartcb5172e2010-03-15 11:25:07 -0400289 if (!bf_get_le32(lpfc_cqe_valid, q->qe[q->hba_index].cqe))
James Smart4f774512009-05-22 14:52:35 -0400290 return NULL;
291 /* If the host has not yet processed the next entry then we are done */
292 if (((q->hba_index + 1) % q->entry_count) == q->host_index)
293 return NULL;
294
295 cqe = q->qe[q->hba_index].cqe;
296 q->hba_index = ((q->hba_index + 1) % q->entry_count);
297 return cqe;
298}
299
300/**
301 * lpfc_sli4_cq_release - Indicates the host has finished processing a CQ
302 * @q: The Completion Queue that the host has completed processing for.
303 * @arm: Indicates whether the host wants to arms this CQ.
304 *
305 * This routine will mark all Completion queue entries on @q, from the last
306 * known completed entry to the last entry that was processed, as completed
307 * by clearing the valid bit for each completion queue entry. Then it will
308 * notify the HBA, by ringing the doorbell, that the CQEs have been processed.
309 * The internal host index in the @q will be updated by this routine to indicate
310 * that the host has finished processing the entries. The @arm parameter
311 * indicates that the queue should be rearmed when ringing the doorbell.
312 *
313 * This function will return the number of CQEs that were released.
314 **/
315uint32_t
316lpfc_sli4_cq_release(struct lpfc_queue *q, bool arm)
317{
318 uint32_t released = 0;
319 struct lpfc_cqe *temp_qe;
320 struct lpfc_register doorbell;
321
322 /* while there are valid entries */
323 while (q->hba_index != q->host_index) {
324 temp_qe = q->qe[q->host_index].cqe;
James Smartcb5172e2010-03-15 11:25:07 -0400325 bf_set_le32(lpfc_cqe_valid, temp_qe, 0);
James Smart4f774512009-05-22 14:52:35 -0400326 released++;
327 q->host_index = ((q->host_index + 1) % q->entry_count);
328 }
329 if (unlikely(released == 0 && !arm))
330 return 0;
331
332 /* ring doorbell for number popped */
333 doorbell.word0 = 0;
334 if (arm)
335 bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);
336 bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, released);
337 bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_COMPLETION);
338 bf_set(lpfc_eqcq_doorbell_cqid, &doorbell, q->queue_id);
339 writel(doorbell.word0, q->phba->sli4_hba.EQCQDBregaddr);
340 return released;
341}
342
343/**
344 * lpfc_sli4_rq_put - Put a Receive Buffer Queue Entry on a Receive Queue
345 * @q: The Header Receive Queue to operate on.
346 * @wqe: The Receive Queue Entry to put on the Receive queue.
347 *
348 * This routine will copy the contents of @wqe to the next available entry on
349 * the @q. This function will then ring the Receive Queue Doorbell to signal the
350 * HBA to start processing the Receive Queue Entry. This function returns the
351 * index that the rqe was copied to if successful. If no entries are available
352 * on @q then this function will return -ENOMEM.
353 * The caller is expected to hold the hbalock when calling this routine.
354 **/
355static int
356lpfc_sli4_rq_put(struct lpfc_queue *hq, struct lpfc_queue *dq,
357 struct lpfc_rqe *hrqe, struct lpfc_rqe *drqe)
358{
359 struct lpfc_rqe *temp_hrqe = hq->qe[hq->host_index].rqe;
360 struct lpfc_rqe *temp_drqe = dq->qe[dq->host_index].rqe;
361 struct lpfc_register doorbell;
362 int put_index = hq->host_index;
363
364 if (hq->type != LPFC_HRQ || dq->type != LPFC_DRQ)
365 return -EINVAL;
366 if (hq->host_index != dq->host_index)
367 return -EINVAL;
368 /* If the host has not yet processed the next entry then we are done */
369 if (((hq->host_index + 1) % hq->entry_count) == hq->hba_index)
370 return -EBUSY;
371 lpfc_sli_pcimem_bcopy(hrqe, temp_hrqe, hq->entry_size);
372 lpfc_sli_pcimem_bcopy(drqe, temp_drqe, dq->entry_size);
373
374 /* Update the host index to point to the next slot */
375 hq->host_index = ((hq->host_index + 1) % hq->entry_count);
376 dq->host_index = ((dq->host_index + 1) % dq->entry_count);
377
378 /* Ring The Header Receive Queue Doorbell */
379 if (!(hq->host_index % LPFC_RQ_POST_BATCH)) {
380 doorbell.word0 = 0;
381 bf_set(lpfc_rq_doorbell_num_posted, &doorbell,
382 LPFC_RQ_POST_BATCH);
383 bf_set(lpfc_rq_doorbell_id, &doorbell, hq->queue_id);
384 writel(doorbell.word0, hq->phba->sli4_hba.RQDBregaddr);
385 }
386 return put_index;
387}
388
389/**
390 * lpfc_sli4_rq_release - Updates internal hba index for RQ
391 * @q: The Header Receive Queue to operate on.
392 *
393 * This routine will update the HBA index of a queue to reflect consumption of
394 * one Receive Queue Entry by the HBA. When the HBA indicates that it has
395 * consumed an entry the host calls this function to update the queue's
396 * internal pointers. This routine returns the number of entries that were
397 * consumed by the HBA.
398 **/
399static uint32_t
400lpfc_sli4_rq_release(struct lpfc_queue *hq, struct lpfc_queue *dq)
401{
402 if ((hq->type != LPFC_HRQ) || (dq->type != LPFC_DRQ))
403 return 0;
404 hq->hba_index = ((hq->hba_index + 1) % hq->entry_count);
405 dq->hba_index = ((dq->hba_index + 1) % dq->entry_count);
406 return 1;
407}
408
James Smarte59058c2008-08-24 21:49:00 -0400409/**
James Smart3621a712009-04-06 18:47:14 -0400410 * lpfc_cmd_iocb - Get next command iocb entry in the ring
James Smarte59058c2008-08-24 21:49:00 -0400411 * @phba: Pointer to HBA context object.
412 * @pring: Pointer to driver SLI ring object.
413 *
414 * This function returns pointer to next command iocb entry
415 * in the command ring. The caller must hold hbalock to prevent
416 * other threads consume the next command iocb.
417 * SLI-2/SLI-3 provide different sized iocbs.
418 **/
James Smarted957682007-06-17 19:56:37 -0500419static inline IOCB_t *
420lpfc_cmd_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
421{
422 return (IOCB_t *) (((char *) pring->cmdringaddr) +
423 pring->cmdidx * phba->iocb_cmd_size);
424}
425
James Smarte59058c2008-08-24 21:49:00 -0400426/**
James Smart3621a712009-04-06 18:47:14 -0400427 * lpfc_resp_iocb - Get next response iocb entry in the ring
James Smarte59058c2008-08-24 21:49:00 -0400428 * @phba: Pointer to HBA context object.
429 * @pring: Pointer to driver SLI ring object.
430 *
431 * This function returns pointer to next response iocb entry
432 * in the response ring. The caller must hold hbalock to make sure
433 * that no other thread consume the next response iocb.
434 * SLI-2/SLI-3 provide different sized iocbs.
435 **/
James Smarted957682007-06-17 19:56:37 -0500436static inline IOCB_t *
437lpfc_resp_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
438{
439 return (IOCB_t *) (((char *) pring->rspringaddr) +
440 pring->rspidx * phba->iocb_rsp_size);
441}
442
James Smarte59058c2008-08-24 21:49:00 -0400443/**
James Smart3621a712009-04-06 18:47:14 -0400444 * __lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
James Smarte59058c2008-08-24 21:49:00 -0400445 * @phba: Pointer to HBA context object.
446 *
447 * This function is called with hbalock held. This function
448 * allocates a new driver iocb object from the iocb pool. If the
449 * allocation is successful, it returns pointer to the newly
450 * allocated iocb object else it returns NULL.
451 **/
James Smart2e0fef82007-06-17 19:56:36 -0500452static struct lpfc_iocbq *
453__lpfc_sli_get_iocbq(struct lpfc_hba *phba)
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400454{
455 struct list_head *lpfc_iocb_list = &phba->lpfc_iocb_list;
456 struct lpfc_iocbq * iocbq = NULL;
457
458 list_remove_head(lpfc_iocb_list, iocbq, struct lpfc_iocbq, list);
James Smart2a9bf3d2010-06-07 15:24:45 -0400459
460 if (iocbq)
461 phba->iocb_cnt++;
462 if (phba->iocb_cnt > phba->iocb_max)
463 phba->iocb_max = phba->iocb_cnt;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400464 return iocbq;
465}
466
James Smarte59058c2008-08-24 21:49:00 -0400467/**
James Smartda0436e2009-05-22 14:51:39 -0400468 * __lpfc_clear_active_sglq - Remove the active sglq for this XRI.
469 * @phba: Pointer to HBA context object.
470 * @xritag: XRI value.
471 *
472 * This function clears the sglq pointer from the array of acive
473 * sglq's. The xritag that is passed in is used to index into the
474 * array. Before the xritag can be used it needs to be adjusted
475 * by subtracting the xribase.
476 *
477 * Returns sglq ponter = success, NULL = Failure.
478 **/
479static struct lpfc_sglq *
480__lpfc_clear_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
481{
482 uint16_t adj_xri;
483 struct lpfc_sglq *sglq;
484 adj_xri = xritag - phba->sli4_hba.max_cfg_param.xri_base;
485 if (adj_xri > phba->sli4_hba.max_cfg_param.max_xri)
486 return NULL;
487 sglq = phba->sli4_hba.lpfc_sglq_active_list[adj_xri];
488 phba->sli4_hba.lpfc_sglq_active_list[adj_xri] = NULL;
489 return sglq;
490}
491
492/**
493 * __lpfc_get_active_sglq - Get the active sglq for this XRI.
494 * @phba: Pointer to HBA context object.
495 * @xritag: XRI value.
496 *
497 * This function returns the sglq pointer from the array of acive
498 * sglq's. The xritag that is passed in is used to index into the
499 * array. Before the xritag can be used it needs to be adjusted
500 * by subtracting the xribase.
501 *
502 * Returns sglq ponter = success, NULL = Failure.
503 **/
James Smart0f65ff62010-02-26 14:14:23 -0500504struct lpfc_sglq *
James Smartda0436e2009-05-22 14:51:39 -0400505__lpfc_get_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
506{
507 uint16_t adj_xri;
508 struct lpfc_sglq *sglq;
509 adj_xri = xritag - phba->sli4_hba.max_cfg_param.xri_base;
510 if (adj_xri > phba->sli4_hba.max_cfg_param.max_xri)
511 return NULL;
512 sglq = phba->sli4_hba.lpfc_sglq_active_list[adj_xri];
513 return sglq;
514}
515
516/**
James Smart19ca7602010-11-20 23:11:55 -0500517 * __lpfc_set_rrq_active - set RRQ active bit in the ndlp's xri_bitmap.
518 * @phba: Pointer to HBA context object.
519 * @ndlp: nodelist pointer for this target.
520 * @xritag: xri used in this exchange.
521 * @rxid: Remote Exchange ID.
522 * @send_rrq: Flag used to determine if we should send rrq els cmd.
523 *
524 * This function is called with hbalock held.
525 * The active bit is set in the ndlp's active rrq xri_bitmap. Allocates an
526 * rrq struct and adds it to the active_rrq_list.
527 *
528 * returns 0 for rrq slot for this xri
529 * < 0 Were not able to get rrq mem or invalid parameter.
530 **/
531static int
532__lpfc_set_rrq_active(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
533 uint16_t xritag, uint16_t rxid, uint16_t send_rrq)
534{
535 uint16_t adj_xri;
536 struct lpfc_node_rrq *rrq;
537 int empty;
James Smart1151e3e2011-02-16 12:39:35 -0500538 uint32_t did = 0;
539
540
541 if (!ndlp)
542 return -EINVAL;
543
544 if (!phba->cfg_enable_rrq)
545 return -EINVAL;
546
547 if (phba->pport->load_flag & FC_UNLOADING) {
548 phba->hba_flag &= ~HBA_RRQ_ACTIVE;
549 goto out;
550 }
551 did = ndlp->nlp_DID;
James Smart19ca7602010-11-20 23:11:55 -0500552
553 /*
554 * set the active bit even if there is no mem available.
555 */
556 adj_xri = xritag - phba->sli4_hba.max_cfg_param.xri_base;
James Smart1151e3e2011-02-16 12:39:35 -0500557
558 if (NLP_CHK_FREE_REQ(ndlp))
559 goto out;
560
561 if (ndlp->vport && (ndlp->vport->load_flag & FC_UNLOADING))
562 goto out;
563
James Smart19ca7602010-11-20 23:11:55 -0500564 if (test_and_set_bit(adj_xri, ndlp->active_rrqs.xri_bitmap))
James Smart1151e3e2011-02-16 12:39:35 -0500565 goto out;
566
James Smart19ca7602010-11-20 23:11:55 -0500567 rrq = mempool_alloc(phba->rrq_pool, GFP_KERNEL);
568 if (rrq) {
569 rrq->send_rrq = send_rrq;
570 rrq->xritag = xritag;
571 rrq->rrq_stop_time = jiffies + HZ * (phba->fc_ratov + 1);
572 rrq->ndlp = ndlp;
573 rrq->nlp_DID = ndlp->nlp_DID;
574 rrq->vport = ndlp->vport;
575 rrq->rxid = rxid;
576 empty = list_empty(&phba->active_rrq_list);
James Smart1151e3e2011-02-16 12:39:35 -0500577 rrq->send_rrq = send_rrq;
James Smart19ca7602010-11-20 23:11:55 -0500578 list_add_tail(&rrq->list, &phba->active_rrq_list);
579 if (!(phba->hba_flag & HBA_RRQ_ACTIVE)) {
580 phba->hba_flag |= HBA_RRQ_ACTIVE;
581 if (empty)
582 lpfc_worker_wake_up(phba);
583 }
584 return 0;
585 }
James Smart1151e3e2011-02-16 12:39:35 -0500586out:
587 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
588 "2921 Can't set rrq active xri:0x%x rxid:0x%x"
589 " DID:0x%x Send:%d\n",
590 xritag, rxid, did, send_rrq);
591 return -EINVAL;
James Smart19ca7602010-11-20 23:11:55 -0500592}
593
594/**
James Smart1151e3e2011-02-16 12:39:35 -0500595 * lpfc_clr_rrq_active - Clears RRQ active bit in xri_bitmap.
James Smart19ca7602010-11-20 23:11:55 -0500596 * @phba: Pointer to HBA context object.
597 * @xritag: xri used in this exchange.
598 * @rrq: The RRQ to be cleared.
599 *
James Smart19ca7602010-11-20 23:11:55 -0500600 **/
James Smart1151e3e2011-02-16 12:39:35 -0500601void
602lpfc_clr_rrq_active(struct lpfc_hba *phba,
603 uint16_t xritag,
604 struct lpfc_node_rrq *rrq)
James Smart19ca7602010-11-20 23:11:55 -0500605{
606 uint16_t adj_xri;
James Smart1151e3e2011-02-16 12:39:35 -0500607 struct lpfc_nodelist *ndlp = NULL;
James Smart19ca7602010-11-20 23:11:55 -0500608
James Smart1151e3e2011-02-16 12:39:35 -0500609 if ((rrq->vport) && NLP_CHK_NODE_ACT(rrq->ndlp))
610 ndlp = lpfc_findnode_did(rrq->vport, rrq->nlp_DID);
James Smart19ca7602010-11-20 23:11:55 -0500611
612 /* The target DID could have been swapped (cable swap)
613 * we should use the ndlp from the findnode if it is
614 * available.
615 */
James Smart1151e3e2011-02-16 12:39:35 -0500616 if ((!ndlp) && rrq->ndlp)
James Smart19ca7602010-11-20 23:11:55 -0500617 ndlp = rrq->ndlp;
618
James Smart1151e3e2011-02-16 12:39:35 -0500619 if (!ndlp)
620 goto out;
621
James Smart19ca7602010-11-20 23:11:55 -0500622 adj_xri = xritag - phba->sli4_hba.max_cfg_param.xri_base;
623 if (test_and_clear_bit(adj_xri, ndlp->active_rrqs.xri_bitmap)) {
624 rrq->send_rrq = 0;
625 rrq->xritag = 0;
626 rrq->rrq_stop_time = 0;
627 }
James Smart1151e3e2011-02-16 12:39:35 -0500628out:
James Smart19ca7602010-11-20 23:11:55 -0500629 mempool_free(rrq, phba->rrq_pool);
630}
631
632/**
633 * lpfc_handle_rrq_active - Checks if RRQ has waithed RATOV.
634 * @phba: Pointer to HBA context object.
635 *
636 * This function is called with hbalock held. This function
637 * Checks if stop_time (ratov from setting rrq active) has
638 * been reached, if it has and the send_rrq flag is set then
639 * it will call lpfc_send_rrq. If the send_rrq flag is not set
640 * then it will just call the routine to clear the rrq and
641 * free the rrq resource.
642 * The timer is set to the next rrq that is going to expire before
643 * leaving the routine.
644 *
645 **/
646void
647lpfc_handle_rrq_active(struct lpfc_hba *phba)
648{
649 struct lpfc_node_rrq *rrq;
650 struct lpfc_node_rrq *nextrrq;
651 unsigned long next_time;
652 unsigned long iflags;
James Smart1151e3e2011-02-16 12:39:35 -0500653 LIST_HEAD(send_rrq);
James Smart19ca7602010-11-20 23:11:55 -0500654
655 spin_lock_irqsave(&phba->hbalock, iflags);
656 phba->hba_flag &= ~HBA_RRQ_ACTIVE;
657 next_time = jiffies + HZ * (phba->fc_ratov + 1);
658 list_for_each_entry_safe(rrq, nextrrq,
James Smart1151e3e2011-02-16 12:39:35 -0500659 &phba->active_rrq_list, list) {
660 if (time_after(jiffies, rrq->rrq_stop_time))
661 list_move(&rrq->list, &send_rrq);
662 else if (time_before(rrq->rrq_stop_time, next_time))
James Smart19ca7602010-11-20 23:11:55 -0500663 next_time = rrq->rrq_stop_time;
664 }
665 spin_unlock_irqrestore(&phba->hbalock, iflags);
666 if (!list_empty(&phba->active_rrq_list))
667 mod_timer(&phba->rrq_tmr, next_time);
James Smart1151e3e2011-02-16 12:39:35 -0500668 list_for_each_entry_safe(rrq, nextrrq, &send_rrq, list) {
669 list_del(&rrq->list);
670 if (!rrq->send_rrq)
671 /* this call will free the rrq */
672 lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
673 else if (lpfc_send_rrq(phba, rrq)) {
674 /* if we send the rrq then the completion handler
675 * will clear the bit in the xribitmap.
676 */
677 lpfc_clr_rrq_active(phba, rrq->xritag,
678 rrq);
679 }
680 }
James Smart19ca7602010-11-20 23:11:55 -0500681}
682
683/**
684 * lpfc_get_active_rrq - Get the active RRQ for this exchange.
685 * @vport: Pointer to vport context object.
686 * @xri: The xri used in the exchange.
687 * @did: The targets DID for this exchange.
688 *
689 * returns NULL = rrq not found in the phba->active_rrq_list.
690 * rrq = rrq for this xri and target.
691 **/
692struct lpfc_node_rrq *
693lpfc_get_active_rrq(struct lpfc_vport *vport, uint16_t xri, uint32_t did)
694{
695 struct lpfc_hba *phba = vport->phba;
696 struct lpfc_node_rrq *rrq;
697 struct lpfc_node_rrq *nextrrq;
698 unsigned long iflags;
699
700 if (phba->sli_rev != LPFC_SLI_REV4)
701 return NULL;
702 spin_lock_irqsave(&phba->hbalock, iflags);
703 list_for_each_entry_safe(rrq, nextrrq, &phba->active_rrq_list, list) {
704 if (rrq->vport == vport && rrq->xritag == xri &&
705 rrq->nlp_DID == did){
706 list_del(&rrq->list);
707 spin_unlock_irqrestore(&phba->hbalock, iflags);
708 return rrq;
709 }
710 }
711 spin_unlock_irqrestore(&phba->hbalock, iflags);
712 return NULL;
713}
714
715/**
716 * lpfc_cleanup_vports_rrqs - Remove and clear the active RRQ for this vport.
717 * @vport: Pointer to vport context object.
James Smart1151e3e2011-02-16 12:39:35 -0500718 * @ndlp: Pointer to the lpfc_node_list structure.
719 * If ndlp is NULL Remove all active RRQs for this vport from the
720 * phba->active_rrq_list and clear the rrq.
721 * If ndlp is not NULL then only remove rrqs for this vport & this ndlp.
James Smart19ca7602010-11-20 23:11:55 -0500722 **/
723void
James Smart1151e3e2011-02-16 12:39:35 -0500724lpfc_cleanup_vports_rrqs(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
James Smart19ca7602010-11-20 23:11:55 -0500725
726{
727 struct lpfc_hba *phba = vport->phba;
728 struct lpfc_node_rrq *rrq;
729 struct lpfc_node_rrq *nextrrq;
730 unsigned long iflags;
James Smart1151e3e2011-02-16 12:39:35 -0500731 LIST_HEAD(rrq_list);
James Smart19ca7602010-11-20 23:11:55 -0500732
733 if (phba->sli_rev != LPFC_SLI_REV4)
734 return;
James Smart1151e3e2011-02-16 12:39:35 -0500735 if (!ndlp) {
736 lpfc_sli4_vport_delete_els_xri_aborted(vport);
737 lpfc_sli4_vport_delete_fcp_xri_aborted(vport);
James Smart19ca7602010-11-20 23:11:55 -0500738 }
James Smart1151e3e2011-02-16 12:39:35 -0500739 spin_lock_irqsave(&phba->hbalock, iflags);
740 list_for_each_entry_safe(rrq, nextrrq, &phba->active_rrq_list, list)
741 if ((rrq->vport == vport) && (!ndlp || rrq->ndlp == ndlp))
742 list_move(&rrq->list, &rrq_list);
James Smart19ca7602010-11-20 23:11:55 -0500743 spin_unlock_irqrestore(&phba->hbalock, iflags);
James Smart1151e3e2011-02-16 12:39:35 -0500744
745 list_for_each_entry_safe(rrq, nextrrq, &rrq_list, list) {
746 list_del(&rrq->list);
747 lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
748 }
James Smart19ca7602010-11-20 23:11:55 -0500749}
750
751/**
752 * lpfc_cleanup_wt_rrqs - Remove all rrq's from the active list.
753 * @phba: Pointer to HBA context object.
754 *
755 * Remove all rrqs from the phba->active_rrq_list and free them by
756 * calling __lpfc_clr_active_rrq
757 *
758 **/
759void
760lpfc_cleanup_wt_rrqs(struct lpfc_hba *phba)
761{
762 struct lpfc_node_rrq *rrq;
763 struct lpfc_node_rrq *nextrrq;
764 unsigned long next_time;
765 unsigned long iflags;
James Smart1151e3e2011-02-16 12:39:35 -0500766 LIST_HEAD(rrq_list);
James Smart19ca7602010-11-20 23:11:55 -0500767
768 if (phba->sli_rev != LPFC_SLI_REV4)
769 return;
770 spin_lock_irqsave(&phba->hbalock, iflags);
771 phba->hba_flag &= ~HBA_RRQ_ACTIVE;
772 next_time = jiffies + HZ * (phba->fc_ratov * 2);
James Smart1151e3e2011-02-16 12:39:35 -0500773 list_splice_init(&phba->active_rrq_list, &rrq_list);
James Smart19ca7602010-11-20 23:11:55 -0500774 spin_unlock_irqrestore(&phba->hbalock, iflags);
James Smart1151e3e2011-02-16 12:39:35 -0500775
776 list_for_each_entry_safe(rrq, nextrrq, &rrq_list, list) {
777 list_del(&rrq->list);
778 lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
779 }
James Smart19ca7602010-11-20 23:11:55 -0500780 if (!list_empty(&phba->active_rrq_list))
781 mod_timer(&phba->rrq_tmr, next_time);
782}
783
784
785/**
James Smart1151e3e2011-02-16 12:39:35 -0500786 * lpfc_test_rrq_active - Test RRQ bit in xri_bitmap.
James Smart19ca7602010-11-20 23:11:55 -0500787 * @phba: Pointer to HBA context object.
788 * @ndlp: Targets nodelist pointer for this exchange.
789 * @xritag the xri in the bitmap to test.
790 *
791 * This function is called with hbalock held. This function
792 * returns 0 = rrq not active for this xri
793 * 1 = rrq is valid for this xri.
794 **/
James Smart1151e3e2011-02-16 12:39:35 -0500795int
796lpfc_test_rrq_active(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
James Smart19ca7602010-11-20 23:11:55 -0500797 uint16_t xritag)
798{
799 uint16_t adj_xri;
800
801 adj_xri = xritag - phba->sli4_hba.max_cfg_param.xri_base;
802 if (!ndlp)
803 return 0;
804 if (test_bit(adj_xri, ndlp->active_rrqs.xri_bitmap))
805 return 1;
806 else
807 return 0;
808}
809
810/**
811 * lpfc_set_rrq_active - set RRQ active bit in xri_bitmap.
812 * @phba: Pointer to HBA context object.
813 * @ndlp: nodelist pointer for this target.
814 * @xritag: xri used in this exchange.
815 * @rxid: Remote Exchange ID.
816 * @send_rrq: Flag used to determine if we should send rrq els cmd.
817 *
818 * This function takes the hbalock.
819 * The active bit is always set in the active rrq xri_bitmap even
820 * if there is no slot avaiable for the other rrq information.
821 *
822 * returns 0 rrq actived for this xri
823 * < 0 No memory or invalid ndlp.
824 **/
825int
826lpfc_set_rrq_active(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
827 uint16_t xritag, uint16_t rxid, uint16_t send_rrq)
828{
829 int ret;
830 unsigned long iflags;
831
832 spin_lock_irqsave(&phba->hbalock, iflags);
833 ret = __lpfc_set_rrq_active(phba, ndlp, xritag, rxid, send_rrq);
834 spin_unlock_irqrestore(&phba->hbalock, iflags);
835 return ret;
836}
837
838/**
James Smartda0436e2009-05-22 14:51:39 -0400839 * __lpfc_sli_get_sglq - Allocates an iocb object from sgl pool
840 * @phba: Pointer to HBA context object.
James Smart19ca7602010-11-20 23:11:55 -0500841 * @piocb: Pointer to the iocbq.
James Smartda0436e2009-05-22 14:51:39 -0400842 *
843 * This function is called with hbalock held. This function
844 * Gets a new driver sglq object from the sglq list. If the
845 * list is not empty then it is successful, it returns pointer to the newly
846 * allocated sglq object else it returns NULL.
847 **/
848static struct lpfc_sglq *
James Smart19ca7602010-11-20 23:11:55 -0500849__lpfc_sli_get_sglq(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq)
James Smartda0436e2009-05-22 14:51:39 -0400850{
851 struct list_head *lpfc_sgl_list = &phba->sli4_hba.lpfc_sgl_list;
852 struct lpfc_sglq *sglq = NULL;
James Smart19ca7602010-11-20 23:11:55 -0500853 struct lpfc_sglq *start_sglq = NULL;
James Smartda0436e2009-05-22 14:51:39 -0400854 uint16_t adj_xri;
James Smart19ca7602010-11-20 23:11:55 -0500855 struct lpfc_scsi_buf *lpfc_cmd;
856 struct lpfc_nodelist *ndlp;
857 int found = 0;
858
859 if (piocbq->iocb_flag & LPFC_IO_FCP) {
860 lpfc_cmd = (struct lpfc_scsi_buf *) piocbq->context1;
861 ndlp = lpfc_cmd->rdata->pnode;
James Smartbe858b62010-12-15 17:57:20 -0500862 } else if ((piocbq->iocb.ulpCommand == CMD_GEN_REQUEST64_CR) &&
863 !(piocbq->iocb_flag & LPFC_IO_LIBDFC))
James Smart19ca7602010-11-20 23:11:55 -0500864 ndlp = piocbq->context_un.ndlp;
James Smart19ca7602010-11-20 23:11:55 -0500865 else
866 ndlp = piocbq->context1;
867
James Smartda0436e2009-05-22 14:51:39 -0400868 list_remove_head(lpfc_sgl_list, sglq, struct lpfc_sglq, list);
James Smart19ca7602010-11-20 23:11:55 -0500869 start_sglq = sglq;
870 while (!found) {
871 if (!sglq)
872 return NULL;
873 adj_xri = sglq->sli4_xritag -
874 phba->sli4_hba.max_cfg_param.xri_base;
James Smart1151e3e2011-02-16 12:39:35 -0500875 if (lpfc_test_rrq_active(phba, ndlp, sglq->sli4_xritag)) {
James Smart19ca7602010-11-20 23:11:55 -0500876 /* This xri has an rrq outstanding for this DID.
877 * put it back in the list and get another xri.
878 */
879 list_add_tail(&sglq->list, lpfc_sgl_list);
880 sglq = NULL;
881 list_remove_head(lpfc_sgl_list, sglq,
882 struct lpfc_sglq, list);
883 if (sglq == start_sglq) {
884 sglq = NULL;
885 break;
886 } else
887 continue;
888 }
889 sglq->ndlp = ndlp;
890 found = 1;
891 phba->sli4_hba.lpfc_sglq_active_list[adj_xri] = sglq;
892 sglq->state = SGL_ALLOCATED;
893 }
James Smartda0436e2009-05-22 14:51:39 -0400894 return sglq;
895}
896
897/**
James Smart3621a712009-04-06 18:47:14 -0400898 * lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
James Smarte59058c2008-08-24 21:49:00 -0400899 * @phba: Pointer to HBA context object.
900 *
901 * This function is called with no lock held. This function
902 * allocates a new driver iocb object from the iocb pool. If the
903 * allocation is successful, it returns pointer to the newly
904 * allocated iocb object else it returns NULL.
905 **/
James Smart2e0fef82007-06-17 19:56:36 -0500906struct lpfc_iocbq *
907lpfc_sli_get_iocbq(struct lpfc_hba *phba)
James Bottomley604a3e32005-10-29 10:28:33 -0500908{
James Smart2e0fef82007-06-17 19:56:36 -0500909 struct lpfc_iocbq * iocbq = NULL;
910 unsigned long iflags;
911
912 spin_lock_irqsave(&phba->hbalock, iflags);
913 iocbq = __lpfc_sli_get_iocbq(phba);
914 spin_unlock_irqrestore(&phba->hbalock, iflags);
915 return iocbq;
916}
917
James Smarte59058c2008-08-24 21:49:00 -0400918/**
James Smart4f774512009-05-22 14:52:35 -0400919 * __lpfc_sli_release_iocbq_s4 - Release iocb to the iocb pool
920 * @phba: Pointer to HBA context object.
921 * @iocbq: Pointer to driver iocb object.
922 *
923 * This function is called with hbalock held to release driver
924 * iocb object to the iocb pool. The iotag in the iocb object
925 * does not change for each use of the iocb object. This function
926 * clears all other fields of the iocb object when it is freed.
927 * The sqlq structure that holds the xritag and phys and virtual
928 * mappings for the scatter gather list is retrieved from the
929 * active array of sglq. The get of the sglq pointer also clears
930 * the entry in the array. If the status of the IO indiactes that
931 * this IO was aborted then the sglq entry it put on the
932 * lpfc_abts_els_sgl_list until the CQ_ABORTED_XRI is received. If the
933 * IO has good status or fails for any other reason then the sglq
934 * entry is added to the free list (lpfc_sgl_list).
935 **/
936static void
937__lpfc_sli_release_iocbq_s4(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
938{
939 struct lpfc_sglq *sglq;
940 size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
James Smart2a9bf3d2010-06-07 15:24:45 -0400941 unsigned long iflag = 0;
942 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
James Smart4f774512009-05-22 14:52:35 -0400943
944 if (iocbq->sli4_xritag == NO_XRI)
945 sglq = NULL;
946 else
947 sglq = __lpfc_clear_active_sglq(phba, iocbq->sli4_xritag);
948 if (sglq) {
James Smart0f65ff62010-02-26 14:14:23 -0500949 if ((iocbq->iocb_flag & LPFC_EXCHANGE_BUSY) &&
950 (sglq->state != SGL_XRI_ABORTED)) {
James Smart4f774512009-05-22 14:52:35 -0400951 spin_lock_irqsave(&phba->sli4_hba.abts_sgl_list_lock,
952 iflag);
953 list_add(&sglq->list,
954 &phba->sli4_hba.lpfc_abts_els_sgl_list);
955 spin_unlock_irqrestore(
956 &phba->sli4_hba.abts_sgl_list_lock, iflag);
James Smart0f65ff62010-02-26 14:14:23 -0500957 } else {
958 sglq->state = SGL_FREED;
James Smart19ca7602010-11-20 23:11:55 -0500959 sglq->ndlp = NULL;
James Smartfedd3b72011-02-16 12:39:24 -0500960 list_add_tail(&sglq->list,
961 &phba->sli4_hba.lpfc_sgl_list);
James Smart2a9bf3d2010-06-07 15:24:45 -0400962
963 /* Check if TXQ queue needs to be serviced */
James Smart589a52d2010-07-14 15:30:54 -0400964 if (pring->txq_cnt)
James Smart2a9bf3d2010-06-07 15:24:45 -0400965 lpfc_worker_wake_up(phba);
James Smart0f65ff62010-02-26 14:14:23 -0500966 }
James Smart4f774512009-05-22 14:52:35 -0400967 }
968
969
970 /*
971 * Clean all volatile data fields, preserve iotag and node struct.
972 */
973 memset((char *)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
974 iocbq->sli4_xritag = NO_XRI;
975 list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
976}
977
James Smart2a9bf3d2010-06-07 15:24:45 -0400978
James Smart4f774512009-05-22 14:52:35 -0400979/**
James Smart3772a992009-05-22 14:50:54 -0400980 * __lpfc_sli_release_iocbq_s3 - Release iocb to the iocb pool
981 * @phba: Pointer to HBA context object.
982 * @iocbq: Pointer to driver iocb object.
983 *
984 * This function is called with hbalock held to release driver
985 * iocb object to the iocb pool. The iotag in the iocb object
986 * does not change for each use of the iocb object. This function
987 * clears all other fields of the iocb object when it is freed.
988 **/
989static void
990__lpfc_sli_release_iocbq_s3(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
991{
992 size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
993
994 /*
995 * Clean all volatile data fields, preserve iotag and node struct.
996 */
997 memset((char*)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
998 iocbq->sli4_xritag = NO_XRI;
999 list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
1000}
1001
1002/**
James Smart3621a712009-04-06 18:47:14 -04001003 * __lpfc_sli_release_iocbq - Release iocb to the iocb pool
James Smarte59058c2008-08-24 21:49:00 -04001004 * @phba: Pointer to HBA context object.
1005 * @iocbq: Pointer to driver iocb object.
1006 *
1007 * This function is called with hbalock held to release driver
1008 * iocb object to the iocb pool. The iotag in the iocb object
1009 * does not change for each use of the iocb object. This function
1010 * clears all other fields of the iocb object when it is freed.
1011 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01001012static void
James Smart2e0fef82007-06-17 19:56:36 -05001013__lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1014{
James Smart3772a992009-05-22 14:50:54 -04001015 phba->__lpfc_sli_release_iocbq(phba, iocbq);
James Smart2a9bf3d2010-06-07 15:24:45 -04001016 phba->iocb_cnt--;
James Bottomley604a3e32005-10-29 10:28:33 -05001017}
1018
James Smarte59058c2008-08-24 21:49:00 -04001019/**
James Smart3621a712009-04-06 18:47:14 -04001020 * lpfc_sli_release_iocbq - Release iocb to the iocb pool
James Smarte59058c2008-08-24 21:49:00 -04001021 * @phba: Pointer to HBA context object.
1022 * @iocbq: Pointer to driver iocb object.
1023 *
1024 * This function is called with no lock held to release the iocb to
1025 * iocb pool.
1026 **/
James Smart2e0fef82007-06-17 19:56:36 -05001027void
1028lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1029{
1030 unsigned long iflags;
1031
1032 /*
1033 * Clean all volatile data fields, preserve iotag and node struct.
1034 */
1035 spin_lock_irqsave(&phba->hbalock, iflags);
1036 __lpfc_sli_release_iocbq(phba, iocbq);
1037 spin_unlock_irqrestore(&phba->hbalock, iflags);
1038}
1039
James Smarte59058c2008-08-24 21:49:00 -04001040/**
James Smarta257bf92009-04-06 18:48:10 -04001041 * lpfc_sli_cancel_iocbs - Cancel all iocbs from a list.
1042 * @phba: Pointer to HBA context object.
1043 * @iocblist: List of IOCBs.
1044 * @ulpstatus: ULP status in IOCB command field.
1045 * @ulpWord4: ULP word-4 in IOCB command field.
1046 *
1047 * This function is called with a list of IOCBs to cancel. It cancels the IOCB
1048 * on the list by invoking the complete callback function associated with the
1049 * IOCB with the provided @ulpstatus and @ulpword4 set to the IOCB commond
1050 * fields.
1051 **/
1052void
1053lpfc_sli_cancel_iocbs(struct lpfc_hba *phba, struct list_head *iocblist,
1054 uint32_t ulpstatus, uint32_t ulpWord4)
1055{
1056 struct lpfc_iocbq *piocb;
1057
1058 while (!list_empty(iocblist)) {
1059 list_remove_head(iocblist, piocb, struct lpfc_iocbq, list);
1060
1061 if (!piocb->iocb_cmpl)
1062 lpfc_sli_release_iocbq(phba, piocb);
1063 else {
1064 piocb->iocb.ulpStatus = ulpstatus;
1065 piocb->iocb.un.ulpWord[4] = ulpWord4;
1066 (piocb->iocb_cmpl) (phba, piocb, piocb);
1067 }
1068 }
1069 return;
1070}
1071
1072/**
James Smart3621a712009-04-06 18:47:14 -04001073 * lpfc_sli_iocb_cmd_type - Get the iocb type
1074 * @iocb_cmnd: iocb command code.
James Smarte59058c2008-08-24 21:49:00 -04001075 *
1076 * This function is called by ring event handler function to get the iocb type.
1077 * This function translates the iocb command to an iocb command type used to
1078 * decide the final disposition of each completed IOCB.
1079 * The function returns
1080 * LPFC_UNKNOWN_IOCB if it is an unsupported iocb
1081 * LPFC_SOL_IOCB if it is a solicited iocb completion
1082 * LPFC_ABORT_IOCB if it is an abort iocb
1083 * LPFC_UNSOL_IOCB if it is an unsolicited iocb
1084 *
1085 * The caller is not required to hold any lock.
1086 **/
dea31012005-04-17 16:05:31 -05001087static lpfc_iocb_type
1088lpfc_sli_iocb_cmd_type(uint8_t iocb_cmnd)
1089{
1090 lpfc_iocb_type type = LPFC_UNKNOWN_IOCB;
1091
1092 if (iocb_cmnd > CMD_MAX_IOCB_CMD)
1093 return 0;
1094
1095 switch (iocb_cmnd) {
1096 case CMD_XMIT_SEQUENCE_CR:
1097 case CMD_XMIT_SEQUENCE_CX:
1098 case CMD_XMIT_BCAST_CN:
1099 case CMD_XMIT_BCAST_CX:
1100 case CMD_ELS_REQUEST_CR:
1101 case CMD_ELS_REQUEST_CX:
1102 case CMD_CREATE_XRI_CR:
1103 case CMD_CREATE_XRI_CX:
1104 case CMD_GET_RPI_CN:
1105 case CMD_XMIT_ELS_RSP_CX:
1106 case CMD_GET_RPI_CR:
1107 case CMD_FCP_IWRITE_CR:
1108 case CMD_FCP_IWRITE_CX:
1109 case CMD_FCP_IREAD_CR:
1110 case CMD_FCP_IREAD_CX:
1111 case CMD_FCP_ICMND_CR:
1112 case CMD_FCP_ICMND_CX:
James Smartf5603512006-12-02 13:35:43 -05001113 case CMD_FCP_TSEND_CX:
1114 case CMD_FCP_TRSP_CX:
1115 case CMD_FCP_TRECEIVE_CX:
1116 case CMD_FCP_AUTO_TRSP_CX:
dea31012005-04-17 16:05:31 -05001117 case CMD_ADAPTER_MSG:
1118 case CMD_ADAPTER_DUMP:
1119 case CMD_XMIT_SEQUENCE64_CR:
1120 case CMD_XMIT_SEQUENCE64_CX:
1121 case CMD_XMIT_BCAST64_CN:
1122 case CMD_XMIT_BCAST64_CX:
1123 case CMD_ELS_REQUEST64_CR:
1124 case CMD_ELS_REQUEST64_CX:
1125 case CMD_FCP_IWRITE64_CR:
1126 case CMD_FCP_IWRITE64_CX:
1127 case CMD_FCP_IREAD64_CR:
1128 case CMD_FCP_IREAD64_CX:
1129 case CMD_FCP_ICMND64_CR:
1130 case CMD_FCP_ICMND64_CX:
James Smartf5603512006-12-02 13:35:43 -05001131 case CMD_FCP_TSEND64_CX:
1132 case CMD_FCP_TRSP64_CX:
1133 case CMD_FCP_TRECEIVE64_CX:
dea31012005-04-17 16:05:31 -05001134 case CMD_GEN_REQUEST64_CR:
1135 case CMD_GEN_REQUEST64_CX:
1136 case CMD_XMIT_ELS_RSP64_CX:
James Smartda0436e2009-05-22 14:51:39 -04001137 case DSSCMD_IWRITE64_CR:
1138 case DSSCMD_IWRITE64_CX:
1139 case DSSCMD_IREAD64_CR:
1140 case DSSCMD_IREAD64_CX:
dea31012005-04-17 16:05:31 -05001141 type = LPFC_SOL_IOCB;
1142 break;
1143 case CMD_ABORT_XRI_CN:
1144 case CMD_ABORT_XRI_CX:
1145 case CMD_CLOSE_XRI_CN:
1146 case CMD_CLOSE_XRI_CX:
1147 case CMD_XRI_ABORTED_CX:
1148 case CMD_ABORT_MXRI64_CN:
James Smart6669f9b2009-10-02 15:16:45 -04001149 case CMD_XMIT_BLS_RSP64_CX:
dea31012005-04-17 16:05:31 -05001150 type = LPFC_ABORT_IOCB;
1151 break;
1152 case CMD_RCV_SEQUENCE_CX:
1153 case CMD_RCV_ELS_REQ_CX:
1154 case CMD_RCV_SEQUENCE64_CX:
1155 case CMD_RCV_ELS_REQ64_CX:
James Smart57127f12007-10-27 13:37:05 -04001156 case CMD_ASYNC_STATUS:
James Smarted957682007-06-17 19:56:37 -05001157 case CMD_IOCB_RCV_SEQ64_CX:
1158 case CMD_IOCB_RCV_ELS64_CX:
1159 case CMD_IOCB_RCV_CONT64_CX:
James Smart3163f722008-02-08 18:50:25 -05001160 case CMD_IOCB_RET_XRI64_CX:
dea31012005-04-17 16:05:31 -05001161 type = LPFC_UNSOL_IOCB;
1162 break;
James Smart3163f722008-02-08 18:50:25 -05001163 case CMD_IOCB_XMIT_MSEQ64_CR:
1164 case CMD_IOCB_XMIT_MSEQ64_CX:
1165 case CMD_IOCB_RCV_SEQ_LIST64_CX:
1166 case CMD_IOCB_RCV_ELS_LIST64_CX:
1167 case CMD_IOCB_CLOSE_EXTENDED_CN:
1168 case CMD_IOCB_ABORT_EXTENDED_CN:
1169 case CMD_IOCB_RET_HBQE64_CN:
1170 case CMD_IOCB_FCP_IBIDIR64_CR:
1171 case CMD_IOCB_FCP_IBIDIR64_CX:
1172 case CMD_IOCB_FCP_ITASKMGT64_CX:
1173 case CMD_IOCB_LOGENTRY_CN:
1174 case CMD_IOCB_LOGENTRY_ASYNC_CN:
1175 printk("%s - Unhandled SLI-3 Command x%x\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07001176 __func__, iocb_cmnd);
James Smart3163f722008-02-08 18:50:25 -05001177 type = LPFC_UNKNOWN_IOCB;
1178 break;
dea31012005-04-17 16:05:31 -05001179 default:
1180 type = LPFC_UNKNOWN_IOCB;
1181 break;
1182 }
1183
1184 return type;
1185}
1186
James Smarte59058c2008-08-24 21:49:00 -04001187/**
James Smart3621a712009-04-06 18:47:14 -04001188 * lpfc_sli_ring_map - Issue config_ring mbox for all rings
James Smarte59058c2008-08-24 21:49:00 -04001189 * @phba: Pointer to HBA context object.
1190 *
1191 * This function is called from SLI initialization code
1192 * to configure every ring of the HBA's SLI interface. The
1193 * caller is not required to hold any lock. This function issues
1194 * a config_ring mailbox command for each ring.
1195 * This function returns zero if successful else returns a negative
1196 * error code.
1197 **/
dea31012005-04-17 16:05:31 -05001198static int
James Smarted957682007-06-17 19:56:37 -05001199lpfc_sli_ring_map(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05001200{
1201 struct lpfc_sli *psli = &phba->sli;
James Smarted957682007-06-17 19:56:37 -05001202 LPFC_MBOXQ_t *pmb;
1203 MAILBOX_t *pmbox;
1204 int i, rc, ret = 0;
dea31012005-04-17 16:05:31 -05001205
James Smarted957682007-06-17 19:56:37 -05001206 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1207 if (!pmb)
1208 return -ENOMEM;
James Smart04c68492009-05-22 14:52:52 -04001209 pmbox = &pmb->u.mb;
James Smarted957682007-06-17 19:56:37 -05001210 phba->link_state = LPFC_INIT_MBX_CMDS;
dea31012005-04-17 16:05:31 -05001211 for (i = 0; i < psli->num_rings; i++) {
dea31012005-04-17 16:05:31 -05001212 lpfc_config_ring(phba, i, pmb);
1213 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
1214 if (rc != MBX_SUCCESS) {
James Smart92d7f7b2007-06-17 19:56:38 -05001215 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04001216 "0446 Adapter failed to init (%d), "
dea31012005-04-17 16:05:31 -05001217 "mbxCmd x%x CFG_RING, mbxStatus x%x, "
1218 "ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04001219 rc, pmbox->mbxCommand,
1220 pmbox->mbxStatus, i);
James Smart2e0fef82007-06-17 19:56:36 -05001221 phba->link_state = LPFC_HBA_ERROR;
James Smarted957682007-06-17 19:56:37 -05001222 ret = -ENXIO;
1223 break;
dea31012005-04-17 16:05:31 -05001224 }
1225 }
James Smarted957682007-06-17 19:56:37 -05001226 mempool_free(pmb, phba->mbox_mem_pool);
1227 return ret;
dea31012005-04-17 16:05:31 -05001228}
1229
James Smarte59058c2008-08-24 21:49:00 -04001230/**
James Smart3621a712009-04-06 18:47:14 -04001231 * lpfc_sli_ringtxcmpl_put - Adds new iocb to the txcmplq
James Smarte59058c2008-08-24 21:49:00 -04001232 * @phba: Pointer to HBA context object.
1233 * @pring: Pointer to driver SLI ring object.
1234 * @piocb: Pointer to the driver iocb object.
1235 *
1236 * This function is called with hbalock held. The function adds the
1237 * new iocb to txcmplq of the given ring. This function always returns
1238 * 0. If this function is called for ELS ring, this function checks if
1239 * there is a vport associated with the ELS command. This function also
1240 * starts els_tmofunc timer if this is an ELS command.
1241 **/
dea31012005-04-17 16:05:31 -05001242static int
James Smart2e0fef82007-06-17 19:56:36 -05001243lpfc_sli_ringtxcmpl_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1244 struct lpfc_iocbq *piocb)
dea31012005-04-17 16:05:31 -05001245{
dea31012005-04-17 16:05:31 -05001246 list_add_tail(&piocb->list, &pring->txcmplq);
James Smart2a9bf3d2010-06-07 15:24:45 -04001247 piocb->iocb_flag |= LPFC_IO_ON_Q;
dea31012005-04-17 16:05:31 -05001248 pring->txcmplq_cnt++;
James Smart2a9bf3d2010-06-07 15:24:45 -04001249 if (pring->txcmplq_cnt > pring->txcmplq_max)
1250 pring->txcmplq_max = pring->txcmplq_cnt;
1251
James Smart92d7f7b2007-06-17 19:56:38 -05001252 if ((unlikely(pring->ringno == LPFC_ELS_RING)) &&
1253 (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
1254 (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
1255 if (!piocb->vport)
1256 BUG();
1257 else
1258 mod_timer(&piocb->vport->els_tmofunc,
1259 jiffies + HZ * (phba->fc_ratov << 1));
1260 }
1261
dea31012005-04-17 16:05:31 -05001262
James Smart2e0fef82007-06-17 19:56:36 -05001263 return 0;
dea31012005-04-17 16:05:31 -05001264}
1265
James Smarte59058c2008-08-24 21:49:00 -04001266/**
James Smart3621a712009-04-06 18:47:14 -04001267 * lpfc_sli_ringtx_get - Get first element of the txq
James Smarte59058c2008-08-24 21:49:00 -04001268 * @phba: Pointer to HBA context object.
1269 * @pring: Pointer to driver SLI ring object.
1270 *
1271 * This function is called with hbalock held to get next
1272 * iocb in txq of the given ring. If there is any iocb in
1273 * the txq, the function returns first iocb in the list after
1274 * removing the iocb from the list, else it returns NULL.
1275 **/
James Smart2a9bf3d2010-06-07 15:24:45 -04001276struct lpfc_iocbq *
James Smart2e0fef82007-06-17 19:56:36 -05001277lpfc_sli_ringtx_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -05001278{
dea31012005-04-17 16:05:31 -05001279 struct lpfc_iocbq *cmd_iocb;
1280
James Smart858c9f62007-06-17 19:56:39 -05001281 list_remove_head((&pring->txq), cmd_iocb, struct lpfc_iocbq, list);
1282 if (cmd_iocb != NULL)
dea31012005-04-17 16:05:31 -05001283 pring->txq_cnt--;
James Smart2e0fef82007-06-17 19:56:36 -05001284 return cmd_iocb;
dea31012005-04-17 16:05:31 -05001285}
1286
James Smarte59058c2008-08-24 21:49:00 -04001287/**
James Smart3621a712009-04-06 18:47:14 -04001288 * lpfc_sli_next_iocb_slot - Get next iocb slot in the ring
James Smarte59058c2008-08-24 21:49:00 -04001289 * @phba: Pointer to HBA context object.
1290 * @pring: Pointer to driver SLI ring object.
1291 *
1292 * This function is called with hbalock held and the caller must post the
1293 * iocb without releasing the lock. If the caller releases the lock,
1294 * iocb slot returned by the function is not guaranteed to be available.
1295 * The function returns pointer to the next available iocb slot if there
1296 * is available slot in the ring, else it returns NULL.
1297 * If the get index of the ring is ahead of the put index, the function
1298 * will post an error attention event to the worker thread to take the
1299 * HBA to offline state.
1300 **/
dea31012005-04-17 16:05:31 -05001301static IOCB_t *
1302lpfc_sli_next_iocb_slot (struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
1303{
James Smart34b02dc2008-08-24 21:49:55 -04001304 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
dea31012005-04-17 16:05:31 -05001305 uint32_t max_cmd_idx = pring->numCiocb;
dea31012005-04-17 16:05:31 -05001306 if ((pring->next_cmdidx == pring->cmdidx) &&
1307 (++pring->next_cmdidx >= max_cmd_idx))
1308 pring->next_cmdidx = 0;
1309
1310 if (unlikely(pring->local_getidx == pring->next_cmdidx)) {
1311
1312 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
1313
1314 if (unlikely(pring->local_getidx >= max_cmd_idx)) {
1315 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001316 "0315 Ring %d issue: portCmdGet %d "
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02001317 "is bigger than cmd ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04001318 pring->ringno,
dea31012005-04-17 16:05:31 -05001319 pring->local_getidx, max_cmd_idx);
1320
James Smart2e0fef82007-06-17 19:56:36 -05001321 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05001322 /*
1323 * All error attention handlers are posted to
1324 * worker thread
1325 */
1326 phba->work_ha |= HA_ERATT;
1327 phba->work_hs = HS_FFER3;
James Smart92d7f7b2007-06-17 19:56:38 -05001328
James Smart5e9d9b82008-06-14 22:52:53 -04001329 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -05001330
1331 return NULL;
1332 }
1333
1334 if (pring->local_getidx == pring->next_cmdidx)
1335 return NULL;
1336 }
1337
James Smarted957682007-06-17 19:56:37 -05001338 return lpfc_cmd_iocb(phba, pring);
dea31012005-04-17 16:05:31 -05001339}
1340
James Smarte59058c2008-08-24 21:49:00 -04001341/**
James Smart3621a712009-04-06 18:47:14 -04001342 * lpfc_sli_next_iotag - Get an iotag for the iocb
James Smarte59058c2008-08-24 21:49:00 -04001343 * @phba: Pointer to HBA context object.
1344 * @iocbq: Pointer to driver iocb object.
1345 *
1346 * This function gets an iotag for the iocb. If there is no unused iotag and
1347 * the iocbq_lookup_len < 0xffff, this function allocates a bigger iotag_lookup
1348 * array and assigns a new iotag.
1349 * The function returns the allocated iotag if successful, else returns zero.
1350 * Zero is not a valid iotag.
1351 * The caller is not required to hold any lock.
1352 **/
James Bottomley604a3e32005-10-29 10:28:33 -05001353uint16_t
James Smart2e0fef82007-06-17 19:56:36 -05001354lpfc_sli_next_iotag(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
dea31012005-04-17 16:05:31 -05001355{
James Smart2e0fef82007-06-17 19:56:36 -05001356 struct lpfc_iocbq **new_arr;
1357 struct lpfc_iocbq **old_arr;
James Bottomley604a3e32005-10-29 10:28:33 -05001358 size_t new_len;
1359 struct lpfc_sli *psli = &phba->sli;
1360 uint16_t iotag;
dea31012005-04-17 16:05:31 -05001361
James Smart2e0fef82007-06-17 19:56:36 -05001362 spin_lock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001363 iotag = psli->last_iotag;
1364 if(++iotag < psli->iocbq_lookup_len) {
1365 psli->last_iotag = iotag;
1366 psli->iocbq_lookup[iotag] = iocbq;
James Smart2e0fef82007-06-17 19:56:36 -05001367 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001368 iocbq->iotag = iotag;
1369 return iotag;
James Smart2e0fef82007-06-17 19:56:36 -05001370 } else if (psli->iocbq_lookup_len < (0xffff
James Bottomley604a3e32005-10-29 10:28:33 -05001371 - LPFC_IOCBQ_LOOKUP_INCREMENT)) {
1372 new_len = psli->iocbq_lookup_len + LPFC_IOCBQ_LOOKUP_INCREMENT;
James Smart2e0fef82007-06-17 19:56:36 -05001373 spin_unlock_irq(&phba->hbalock);
1374 new_arr = kzalloc(new_len * sizeof (struct lpfc_iocbq *),
James Bottomley604a3e32005-10-29 10:28:33 -05001375 GFP_KERNEL);
1376 if (new_arr) {
James Smart2e0fef82007-06-17 19:56:36 -05001377 spin_lock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001378 old_arr = psli->iocbq_lookup;
1379 if (new_len <= psli->iocbq_lookup_len) {
1380 /* highly unprobable case */
1381 kfree(new_arr);
1382 iotag = psli->last_iotag;
1383 if(++iotag < psli->iocbq_lookup_len) {
1384 psli->last_iotag = iotag;
1385 psli->iocbq_lookup[iotag] = iocbq;
James Smart2e0fef82007-06-17 19:56:36 -05001386 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001387 iocbq->iotag = iotag;
1388 return iotag;
1389 }
James Smart2e0fef82007-06-17 19:56:36 -05001390 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001391 return 0;
1392 }
1393 if (psli->iocbq_lookup)
1394 memcpy(new_arr, old_arr,
1395 ((psli->last_iotag + 1) *
James Smart311464e2007-08-02 11:10:37 -04001396 sizeof (struct lpfc_iocbq *)));
James Bottomley604a3e32005-10-29 10:28:33 -05001397 psli->iocbq_lookup = new_arr;
1398 psli->iocbq_lookup_len = new_len;
1399 psli->last_iotag = iotag;
1400 psli->iocbq_lookup[iotag] = iocbq;
James Smart2e0fef82007-06-17 19:56:36 -05001401 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001402 iocbq->iotag = iotag;
1403 kfree(old_arr);
1404 return iotag;
1405 }
James Smart8f6d98d2006-08-01 07:34:00 -04001406 } else
James Smart2e0fef82007-06-17 19:56:36 -05001407 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001408
James Smartbc739052010-08-04 16:11:18 -04001409 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001410 "0318 Failed to allocate IOTAG.last IOTAG is %d\n",
1411 psli->last_iotag);
dea31012005-04-17 16:05:31 -05001412
James Bottomley604a3e32005-10-29 10:28:33 -05001413 return 0;
dea31012005-04-17 16:05:31 -05001414}
1415
James Smarte59058c2008-08-24 21:49:00 -04001416/**
James Smart3621a712009-04-06 18:47:14 -04001417 * lpfc_sli_submit_iocb - Submit an iocb to the firmware
James Smarte59058c2008-08-24 21:49:00 -04001418 * @phba: Pointer to HBA context object.
1419 * @pring: Pointer to driver SLI ring object.
1420 * @iocb: Pointer to iocb slot in the ring.
1421 * @nextiocb: Pointer to driver iocb object which need to be
1422 * posted to firmware.
1423 *
1424 * This function is called with hbalock held to post a new iocb to
1425 * the firmware. This function copies the new iocb to ring iocb slot and
1426 * updates the ring pointers. It adds the new iocb to txcmplq if there is
1427 * a completion call back for this iocb else the function will free the
1428 * iocb object.
1429 **/
dea31012005-04-17 16:05:31 -05001430static void
1431lpfc_sli_submit_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1432 IOCB_t *iocb, struct lpfc_iocbq *nextiocb)
1433{
1434 /*
James Bottomley604a3e32005-10-29 10:28:33 -05001435 * Set up an iotag
dea31012005-04-17 16:05:31 -05001436 */
James Bottomley604a3e32005-10-29 10:28:33 -05001437 nextiocb->iocb.ulpIoTag = (nextiocb->iocb_cmpl) ? nextiocb->iotag : 0;
dea31012005-04-17 16:05:31 -05001438
James Smarte2a0a9d2008-12-04 22:40:02 -05001439
James Smarta58cbd52007-08-02 11:09:43 -04001440 if (pring->ringno == LPFC_ELS_RING) {
1441 lpfc_debugfs_slow_ring_trc(phba,
1442 "IOCB cmd ring: wd4:x%08x wd6:x%08x wd7:x%08x",
1443 *(((uint32_t *) &nextiocb->iocb) + 4),
1444 *(((uint32_t *) &nextiocb->iocb) + 6),
1445 *(((uint32_t *) &nextiocb->iocb) + 7));
1446 }
1447
dea31012005-04-17 16:05:31 -05001448 /*
1449 * Issue iocb command to adapter
1450 */
James Smart92d7f7b2007-06-17 19:56:38 -05001451 lpfc_sli_pcimem_bcopy(&nextiocb->iocb, iocb, phba->iocb_cmd_size);
dea31012005-04-17 16:05:31 -05001452 wmb();
1453 pring->stats.iocb_cmd++;
1454
1455 /*
1456 * If there is no completion routine to call, we can release the
1457 * IOCB buffer back right now. For IOCBs, like QUE_RING_BUF,
1458 * that have no rsp ring completion, iocb_cmpl MUST be NULL.
1459 */
1460 if (nextiocb->iocb_cmpl)
1461 lpfc_sli_ringtxcmpl_put(phba, pring, nextiocb);
James Bottomley604a3e32005-10-29 10:28:33 -05001462 else
James Smart2e0fef82007-06-17 19:56:36 -05001463 __lpfc_sli_release_iocbq(phba, nextiocb);
dea31012005-04-17 16:05:31 -05001464
1465 /*
1466 * Let the HBA know what IOCB slot will be the next one the
1467 * driver will put a command into.
1468 */
1469 pring->cmdidx = pring->next_cmdidx;
James Smarted957682007-06-17 19:56:37 -05001470 writel(pring->cmdidx, &phba->host_gp[pring->ringno].cmdPutInx);
dea31012005-04-17 16:05:31 -05001471}
1472
James Smarte59058c2008-08-24 21:49:00 -04001473/**
James Smart3621a712009-04-06 18:47:14 -04001474 * lpfc_sli_update_full_ring - Update the chip attention register
James Smarte59058c2008-08-24 21:49:00 -04001475 * @phba: Pointer to HBA context object.
1476 * @pring: Pointer to driver SLI ring object.
1477 *
1478 * The caller is not required to hold any lock for calling this function.
1479 * This function updates the chip attention bits for the ring to inform firmware
1480 * that there are pending work to be done for this ring and requests an
1481 * interrupt when there is space available in the ring. This function is
1482 * called when the driver is unable to post more iocbs to the ring due
1483 * to unavailability of space in the ring.
1484 **/
dea31012005-04-17 16:05:31 -05001485static void
James Smart2e0fef82007-06-17 19:56:36 -05001486lpfc_sli_update_full_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -05001487{
1488 int ringno = pring->ringno;
1489
1490 pring->flag |= LPFC_CALL_RING_AVAILABLE;
1491
1492 wmb();
1493
1494 /*
1495 * Set ring 'ringno' to SET R0CE_REQ in Chip Att register.
1496 * The HBA will tell us when an IOCB entry is available.
1497 */
1498 writel((CA_R0ATT|CA_R0CE_REQ) << (ringno*4), phba->CAregaddr);
1499 readl(phba->CAregaddr); /* flush */
1500
1501 pring->stats.iocb_cmd_full++;
1502}
1503
James Smarte59058c2008-08-24 21:49:00 -04001504/**
James Smart3621a712009-04-06 18:47:14 -04001505 * lpfc_sli_update_ring - Update chip attention register
James Smarte59058c2008-08-24 21:49:00 -04001506 * @phba: Pointer to HBA context object.
1507 * @pring: Pointer to driver SLI ring object.
1508 *
1509 * This function updates the chip attention register bit for the
1510 * given ring to inform HBA that there is more work to be done
1511 * in this ring. The caller is not required to hold any lock.
1512 **/
dea31012005-04-17 16:05:31 -05001513static void
James Smart2e0fef82007-06-17 19:56:36 -05001514lpfc_sli_update_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -05001515{
1516 int ringno = pring->ringno;
1517
1518 /*
1519 * Tell the HBA that there is work to do in this ring.
1520 */
James Smart34b02dc2008-08-24 21:49:55 -04001521 if (!(phba->sli3_options & LPFC_SLI3_CRP_ENABLED)) {
1522 wmb();
1523 writel(CA_R0ATT << (ringno * 4), phba->CAregaddr);
1524 readl(phba->CAregaddr); /* flush */
1525 }
dea31012005-04-17 16:05:31 -05001526}
1527
James Smarte59058c2008-08-24 21:49:00 -04001528/**
James Smart3621a712009-04-06 18:47:14 -04001529 * lpfc_sli_resume_iocb - Process iocbs in the txq
James Smarte59058c2008-08-24 21:49:00 -04001530 * @phba: Pointer to HBA context object.
1531 * @pring: Pointer to driver SLI ring object.
1532 *
1533 * This function is called with hbalock held to post pending iocbs
1534 * in the txq to the firmware. This function is called when driver
1535 * detects space available in the ring.
1536 **/
dea31012005-04-17 16:05:31 -05001537static void
James Smart2e0fef82007-06-17 19:56:36 -05001538lpfc_sli_resume_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -05001539{
1540 IOCB_t *iocb;
1541 struct lpfc_iocbq *nextiocb;
1542
1543 /*
1544 * Check to see if:
1545 * (a) there is anything on the txq to send
1546 * (b) link is up
1547 * (c) link attention events can be processed (fcp ring only)
1548 * (d) IOCB processing is not blocked by the outstanding mbox command.
1549 */
1550 if (pring->txq_cnt &&
James Smart2e0fef82007-06-17 19:56:36 -05001551 lpfc_is_link_up(phba) &&
dea31012005-04-17 16:05:31 -05001552 (pring->ringno != phba->sli.fcp_ring ||
James Smart0b727fe2007-10-27 13:37:25 -04001553 phba->sli.sli_flag & LPFC_PROCESS_LA)) {
dea31012005-04-17 16:05:31 -05001554
1555 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
1556 (nextiocb = lpfc_sli_ringtx_get(phba, pring)))
1557 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
1558
1559 if (iocb)
1560 lpfc_sli_update_ring(phba, pring);
1561 else
1562 lpfc_sli_update_full_ring(phba, pring);
1563 }
1564
1565 return;
1566}
1567
James Smarte59058c2008-08-24 21:49:00 -04001568/**
James Smart3621a712009-04-06 18:47:14 -04001569 * lpfc_sli_next_hbq_slot - Get next hbq entry for the HBQ
James Smarte59058c2008-08-24 21:49:00 -04001570 * @phba: Pointer to HBA context object.
1571 * @hbqno: HBQ number.
1572 *
1573 * This function is called with hbalock held to get the next
1574 * available slot for the given HBQ. If there is free slot
1575 * available for the HBQ it will return pointer to the next available
1576 * HBQ entry else it will return NULL.
1577 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01001578static struct lpfc_hbq_entry *
James Smarted957682007-06-17 19:56:37 -05001579lpfc_sli_next_hbq_slot(struct lpfc_hba *phba, uint32_t hbqno)
1580{
1581 struct hbq_s *hbqp = &phba->hbqs[hbqno];
1582
1583 if (hbqp->next_hbqPutIdx == hbqp->hbqPutIdx &&
1584 ++hbqp->next_hbqPutIdx >= hbqp->entry_count)
1585 hbqp->next_hbqPutIdx = 0;
1586
1587 if (unlikely(hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)) {
James Smart92d7f7b2007-06-17 19:56:38 -05001588 uint32_t raw_index = phba->hbq_get[hbqno];
James Smarted957682007-06-17 19:56:37 -05001589 uint32_t getidx = le32_to_cpu(raw_index);
1590
1591 hbqp->local_hbqGetIdx = getidx;
1592
1593 if (unlikely(hbqp->local_hbqGetIdx >= hbqp->entry_count)) {
1594 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -05001595 LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04001596 "1802 HBQ %d: local_hbqGetIdx "
James Smarted957682007-06-17 19:56:37 -05001597 "%u is > than hbqp->entry_count %u\n",
James Smarte8b62012007-08-02 11:10:09 -04001598 hbqno, hbqp->local_hbqGetIdx,
James Smarted957682007-06-17 19:56:37 -05001599 hbqp->entry_count);
1600
1601 phba->link_state = LPFC_HBA_ERROR;
1602 return NULL;
1603 }
1604
1605 if (hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)
1606 return NULL;
1607 }
1608
James Smart51ef4c22007-08-02 11:10:31 -04001609 return (struct lpfc_hbq_entry *) phba->hbqs[hbqno].hbq_virt +
1610 hbqp->hbqPutIdx;
James Smarted957682007-06-17 19:56:37 -05001611}
1612
James Smarte59058c2008-08-24 21:49:00 -04001613/**
James Smart3621a712009-04-06 18:47:14 -04001614 * lpfc_sli_hbqbuf_free_all - Free all the hbq buffers
James Smarte59058c2008-08-24 21:49:00 -04001615 * @phba: Pointer to HBA context object.
1616 *
1617 * This function is called with no lock held to free all the
1618 * hbq buffers while uninitializing the SLI interface. It also
1619 * frees the HBQ buffers returned by the firmware but not yet
1620 * processed by the upper layers.
1621 **/
James Smarted957682007-06-17 19:56:37 -05001622void
1623lpfc_sli_hbqbuf_free_all(struct lpfc_hba *phba)
1624{
James Smart92d7f7b2007-06-17 19:56:38 -05001625 struct lpfc_dmabuf *dmabuf, *next_dmabuf;
1626 struct hbq_dmabuf *hbq_buf;
James Smart3163f722008-02-08 18:50:25 -05001627 unsigned long flags;
James Smart51ef4c22007-08-02 11:10:31 -04001628 int i, hbq_count;
James Smart3163f722008-02-08 18:50:25 -05001629 uint32_t hbqno;
James Smarted957682007-06-17 19:56:37 -05001630
James Smart51ef4c22007-08-02 11:10:31 -04001631 hbq_count = lpfc_sli_hbq_count();
James Smarted957682007-06-17 19:56:37 -05001632 /* Return all memory used by all HBQs */
James Smart3163f722008-02-08 18:50:25 -05001633 spin_lock_irqsave(&phba->hbalock, flags);
James Smart51ef4c22007-08-02 11:10:31 -04001634 for (i = 0; i < hbq_count; ++i) {
1635 list_for_each_entry_safe(dmabuf, next_dmabuf,
1636 &phba->hbqs[i].hbq_buffer_list, list) {
1637 hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
1638 list_del(&hbq_buf->dbuf.list);
1639 (phba->hbqs[i].hbq_free_buffer)(phba, hbq_buf);
1640 }
James Smarta8adb832007-10-27 13:37:53 -04001641 phba->hbqs[i].buffer_count = 0;
James Smarted957682007-06-17 19:56:37 -05001642 }
James Smart3163f722008-02-08 18:50:25 -05001643 /* Return all HBQ buffer that are in-fly */
James Smart3772a992009-05-22 14:50:54 -04001644 list_for_each_entry_safe(dmabuf, next_dmabuf, &phba->rb_pend_list,
1645 list) {
James Smart3163f722008-02-08 18:50:25 -05001646 hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
1647 list_del(&hbq_buf->dbuf.list);
1648 if (hbq_buf->tag == -1) {
1649 (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
1650 (phba, hbq_buf);
1651 } else {
1652 hbqno = hbq_buf->tag >> 16;
1653 if (hbqno >= LPFC_MAX_HBQS)
1654 (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
1655 (phba, hbq_buf);
1656 else
1657 (phba->hbqs[hbqno].hbq_free_buffer)(phba,
1658 hbq_buf);
1659 }
1660 }
1661
1662 /* Mark the HBQs not in use */
1663 phba->hbq_in_use = 0;
1664 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smarted957682007-06-17 19:56:37 -05001665}
1666
James Smarte59058c2008-08-24 21:49:00 -04001667/**
James Smart3621a712009-04-06 18:47:14 -04001668 * lpfc_sli_hbq_to_firmware - Post the hbq buffer to firmware
James Smarte59058c2008-08-24 21:49:00 -04001669 * @phba: Pointer to HBA context object.
1670 * @hbqno: HBQ number.
1671 * @hbq_buf: Pointer to HBQ buffer.
1672 *
1673 * This function is called with the hbalock held to post a
1674 * hbq buffer to the firmware. If the function finds an empty
1675 * slot in the HBQ, it will post the buffer. The function will return
1676 * pointer to the hbq entry if it successfully post the buffer
1677 * else it will return NULL.
1678 **/
James Smart3772a992009-05-22 14:50:54 -04001679static int
James Smarted957682007-06-17 19:56:37 -05001680lpfc_sli_hbq_to_firmware(struct lpfc_hba *phba, uint32_t hbqno,
James Smart92d7f7b2007-06-17 19:56:38 -05001681 struct hbq_dmabuf *hbq_buf)
James Smarted957682007-06-17 19:56:37 -05001682{
James Smart3772a992009-05-22 14:50:54 -04001683 return phba->lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buf);
1684}
1685
1686/**
1687 * lpfc_sli_hbq_to_firmware_s3 - Post the hbq buffer to SLI3 firmware
1688 * @phba: Pointer to HBA context object.
1689 * @hbqno: HBQ number.
1690 * @hbq_buf: Pointer to HBQ buffer.
1691 *
1692 * This function is called with the hbalock held to post a hbq buffer to the
1693 * firmware. If the function finds an empty slot in the HBQ, it will post the
1694 * buffer and place it on the hbq_buffer_list. The function will return zero if
1695 * it successfully post the buffer else it will return an error.
1696 **/
1697static int
1698lpfc_sli_hbq_to_firmware_s3(struct lpfc_hba *phba, uint32_t hbqno,
1699 struct hbq_dmabuf *hbq_buf)
1700{
James Smarted957682007-06-17 19:56:37 -05001701 struct lpfc_hbq_entry *hbqe;
James Smart92d7f7b2007-06-17 19:56:38 -05001702 dma_addr_t physaddr = hbq_buf->dbuf.phys;
James Smarted957682007-06-17 19:56:37 -05001703
1704 /* Get next HBQ entry slot to use */
1705 hbqe = lpfc_sli_next_hbq_slot(phba, hbqno);
1706 if (hbqe) {
1707 struct hbq_s *hbqp = &phba->hbqs[hbqno];
1708
James Smart92d7f7b2007-06-17 19:56:38 -05001709 hbqe->bde.addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
1710 hbqe->bde.addrLow = le32_to_cpu(putPaddrLow(physaddr));
James Smart51ef4c22007-08-02 11:10:31 -04001711 hbqe->bde.tus.f.bdeSize = hbq_buf->size;
James Smarted957682007-06-17 19:56:37 -05001712 hbqe->bde.tus.f.bdeFlags = 0;
James Smart92d7f7b2007-06-17 19:56:38 -05001713 hbqe->bde.tus.w = le32_to_cpu(hbqe->bde.tus.w);
1714 hbqe->buffer_tag = le32_to_cpu(hbq_buf->tag);
1715 /* Sync SLIM */
James Smarted957682007-06-17 19:56:37 -05001716 hbqp->hbqPutIdx = hbqp->next_hbqPutIdx;
1717 writel(hbqp->hbqPutIdx, phba->hbq_put + hbqno);
James Smart92d7f7b2007-06-17 19:56:38 -05001718 /* flush */
James Smarted957682007-06-17 19:56:37 -05001719 readl(phba->hbq_put + hbqno);
James Smart51ef4c22007-08-02 11:10:31 -04001720 list_add_tail(&hbq_buf->dbuf.list, &hbqp->hbq_buffer_list);
James Smart3772a992009-05-22 14:50:54 -04001721 return 0;
1722 } else
1723 return -ENOMEM;
James Smarted957682007-06-17 19:56:37 -05001724}
1725
James Smart4f774512009-05-22 14:52:35 -04001726/**
1727 * lpfc_sli_hbq_to_firmware_s4 - Post the hbq buffer to SLI4 firmware
1728 * @phba: Pointer to HBA context object.
1729 * @hbqno: HBQ number.
1730 * @hbq_buf: Pointer to HBQ buffer.
1731 *
1732 * This function is called with the hbalock held to post an RQE to the SLI4
1733 * firmware. If able to post the RQE to the RQ it will queue the hbq entry to
1734 * the hbq_buffer_list and return zero, otherwise it will return an error.
1735 **/
1736static int
1737lpfc_sli_hbq_to_firmware_s4(struct lpfc_hba *phba, uint32_t hbqno,
1738 struct hbq_dmabuf *hbq_buf)
1739{
1740 int rc;
1741 struct lpfc_rqe hrqe;
1742 struct lpfc_rqe drqe;
1743
1744 hrqe.address_lo = putPaddrLow(hbq_buf->hbuf.phys);
1745 hrqe.address_hi = putPaddrHigh(hbq_buf->hbuf.phys);
1746 drqe.address_lo = putPaddrLow(hbq_buf->dbuf.phys);
1747 drqe.address_hi = putPaddrHigh(hbq_buf->dbuf.phys);
1748 rc = lpfc_sli4_rq_put(phba->sli4_hba.hdr_rq, phba->sli4_hba.dat_rq,
1749 &hrqe, &drqe);
1750 if (rc < 0)
1751 return rc;
1752 hbq_buf->tag = rc;
1753 list_add_tail(&hbq_buf->dbuf.list, &phba->hbqs[hbqno].hbq_buffer_list);
1754 return 0;
1755}
1756
James Smarte59058c2008-08-24 21:49:00 -04001757/* HBQ for ELS and CT traffic. */
James Smart92d7f7b2007-06-17 19:56:38 -05001758static struct lpfc_hbq_init lpfc_els_hbq = {
1759 .rn = 1,
James Smartdef9c7a2009-12-21 17:02:28 -05001760 .entry_count = 256,
James Smart92d7f7b2007-06-17 19:56:38 -05001761 .mask_count = 0,
1762 .profile = 0,
James Smart51ef4c22007-08-02 11:10:31 -04001763 .ring_mask = (1 << LPFC_ELS_RING),
James Smart92d7f7b2007-06-17 19:56:38 -05001764 .buffer_count = 0,
James Smarta257bf92009-04-06 18:48:10 -04001765 .init_count = 40,
1766 .add_count = 40,
James Smart92d7f7b2007-06-17 19:56:38 -05001767};
James Smarted957682007-06-17 19:56:37 -05001768
James Smarte59058c2008-08-24 21:49:00 -04001769/* HBQ for the extra ring if needed */
James Smart51ef4c22007-08-02 11:10:31 -04001770static struct lpfc_hbq_init lpfc_extra_hbq = {
1771 .rn = 1,
1772 .entry_count = 200,
1773 .mask_count = 0,
1774 .profile = 0,
1775 .ring_mask = (1 << LPFC_EXTRA_RING),
1776 .buffer_count = 0,
1777 .init_count = 0,
1778 .add_count = 5,
1779};
1780
James Smarte59058c2008-08-24 21:49:00 -04001781/* Array of HBQs */
James Smart78b2d852007-08-02 11:10:21 -04001782struct lpfc_hbq_init *lpfc_hbq_defs[] = {
James Smart92d7f7b2007-06-17 19:56:38 -05001783 &lpfc_els_hbq,
James Smart51ef4c22007-08-02 11:10:31 -04001784 &lpfc_extra_hbq,
James Smart92d7f7b2007-06-17 19:56:38 -05001785};
1786
James Smarte59058c2008-08-24 21:49:00 -04001787/**
James Smart3621a712009-04-06 18:47:14 -04001788 * lpfc_sli_hbqbuf_fill_hbqs - Post more hbq buffers to HBQ
James Smarte59058c2008-08-24 21:49:00 -04001789 * @phba: Pointer to HBA context object.
1790 * @hbqno: HBQ number.
1791 * @count: Number of HBQ buffers to be posted.
1792 *
James Smartd7c255b2008-08-24 21:50:00 -04001793 * This function is called with no lock held to post more hbq buffers to the
1794 * given HBQ. The function returns the number of HBQ buffers successfully
1795 * posted.
James Smarte59058c2008-08-24 21:49:00 -04001796 **/
James Smart311464e2007-08-02 11:10:37 -04001797static int
James Smart92d7f7b2007-06-17 19:56:38 -05001798lpfc_sli_hbqbuf_fill_hbqs(struct lpfc_hba *phba, uint32_t hbqno, uint32_t count)
1799{
James Smartd7c255b2008-08-24 21:50:00 -04001800 uint32_t i, posted = 0;
James Smart3163f722008-02-08 18:50:25 -05001801 unsigned long flags;
James Smart92d7f7b2007-06-17 19:56:38 -05001802 struct hbq_dmabuf *hbq_buffer;
James Smartd7c255b2008-08-24 21:50:00 -04001803 LIST_HEAD(hbq_buf_list);
Matthew Wilcoxeafe1df2008-02-21 05:44:33 -07001804 if (!phba->hbqs[hbqno].hbq_alloc_buffer)
James Smart51ef4c22007-08-02 11:10:31 -04001805 return 0;
James Smart51ef4c22007-08-02 11:10:31 -04001806
James Smartd7c255b2008-08-24 21:50:00 -04001807 if ((phba->hbqs[hbqno].buffer_count + count) >
1808 lpfc_hbq_defs[hbqno]->entry_count)
1809 count = lpfc_hbq_defs[hbqno]->entry_count -
1810 phba->hbqs[hbqno].buffer_count;
1811 if (!count)
1812 return 0;
1813 /* Allocate HBQ entries */
1814 for (i = 0; i < count; i++) {
1815 hbq_buffer = (phba->hbqs[hbqno].hbq_alloc_buffer)(phba);
1816 if (!hbq_buffer)
1817 break;
1818 list_add_tail(&hbq_buffer->dbuf.list, &hbq_buf_list);
1819 }
James Smart3163f722008-02-08 18:50:25 -05001820 /* Check whether HBQ is still in use */
1821 spin_lock_irqsave(&phba->hbalock, flags);
Matthew Wilcoxeafe1df2008-02-21 05:44:33 -07001822 if (!phba->hbq_in_use)
James Smartd7c255b2008-08-24 21:50:00 -04001823 goto err;
1824 while (!list_empty(&hbq_buf_list)) {
1825 list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
1826 dbuf.list);
1827 hbq_buffer->tag = (phba->hbqs[hbqno].buffer_count |
1828 (hbqno << 16));
James Smart3772a992009-05-22 14:50:54 -04001829 if (!lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer)) {
James Smarta8adb832007-10-27 13:37:53 -04001830 phba->hbqs[hbqno].buffer_count++;
James Smartd7c255b2008-08-24 21:50:00 -04001831 posted++;
1832 } else
James Smart51ef4c22007-08-02 11:10:31 -04001833 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
James Smart92d7f7b2007-06-17 19:56:38 -05001834 }
James Smart3163f722008-02-08 18:50:25 -05001835 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smartd7c255b2008-08-24 21:50:00 -04001836 return posted;
1837err:
1838 spin_unlock_irqrestore(&phba->hbalock, flags);
1839 while (!list_empty(&hbq_buf_list)) {
1840 list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
1841 dbuf.list);
1842 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
1843 }
James Smart92d7f7b2007-06-17 19:56:38 -05001844 return 0;
James Smarted957682007-06-17 19:56:37 -05001845}
1846
James Smarte59058c2008-08-24 21:49:00 -04001847/**
James Smart3621a712009-04-06 18:47:14 -04001848 * lpfc_sli_hbqbuf_add_hbqs - Post more HBQ buffers to firmware
James Smarte59058c2008-08-24 21:49:00 -04001849 * @phba: Pointer to HBA context object.
1850 * @qno: HBQ number.
1851 *
1852 * This function posts more buffers to the HBQ. This function
James Smartd7c255b2008-08-24 21:50:00 -04001853 * is called with no lock held. The function returns the number of HBQ entries
1854 * successfully allocated.
James Smarte59058c2008-08-24 21:49:00 -04001855 **/
James Smarted957682007-06-17 19:56:37 -05001856int
James Smart92d7f7b2007-06-17 19:56:38 -05001857lpfc_sli_hbqbuf_add_hbqs(struct lpfc_hba *phba, uint32_t qno)
James Smarted957682007-06-17 19:56:37 -05001858{
James Smartdef9c7a2009-12-21 17:02:28 -05001859 if (phba->sli_rev == LPFC_SLI_REV4)
1860 return 0;
1861 else
1862 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
1863 lpfc_hbq_defs[qno]->add_count);
James Smarted957682007-06-17 19:56:37 -05001864}
1865
James Smarte59058c2008-08-24 21:49:00 -04001866/**
James Smart3621a712009-04-06 18:47:14 -04001867 * lpfc_sli_hbqbuf_init_hbqs - Post initial buffers to the HBQ
James Smarte59058c2008-08-24 21:49:00 -04001868 * @phba: Pointer to HBA context object.
1869 * @qno: HBQ queue number.
1870 *
1871 * This function is called from SLI initialization code path with
1872 * no lock held to post initial HBQ buffers to firmware. The
James Smartd7c255b2008-08-24 21:50:00 -04001873 * function returns the number of HBQ entries successfully allocated.
James Smarte59058c2008-08-24 21:49:00 -04001874 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01001875static int
James Smart92d7f7b2007-06-17 19:56:38 -05001876lpfc_sli_hbqbuf_init_hbqs(struct lpfc_hba *phba, uint32_t qno)
James Smarted957682007-06-17 19:56:37 -05001877{
James Smartdef9c7a2009-12-21 17:02:28 -05001878 if (phba->sli_rev == LPFC_SLI_REV4)
1879 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
1880 lpfc_hbq_defs[qno]->entry_count);
1881 else
1882 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
1883 lpfc_hbq_defs[qno]->init_count);
James Smarted957682007-06-17 19:56:37 -05001884}
1885
James Smarte59058c2008-08-24 21:49:00 -04001886/**
James Smart3772a992009-05-22 14:50:54 -04001887 * lpfc_sli_hbqbuf_get - Remove the first hbq off of an hbq list
1888 * @phba: Pointer to HBA context object.
1889 * @hbqno: HBQ number.
1890 *
1891 * This function removes the first hbq buffer on an hbq list and returns a
1892 * pointer to that buffer. If it finds no buffers on the list it returns NULL.
1893 **/
1894static struct hbq_dmabuf *
1895lpfc_sli_hbqbuf_get(struct list_head *rb_list)
1896{
1897 struct lpfc_dmabuf *d_buf;
1898
1899 list_remove_head(rb_list, d_buf, struct lpfc_dmabuf, list);
1900 if (!d_buf)
1901 return NULL;
1902 return container_of(d_buf, struct hbq_dmabuf, dbuf);
1903}
1904
1905/**
James Smart3621a712009-04-06 18:47:14 -04001906 * lpfc_sli_hbqbuf_find - Find the hbq buffer associated with a tag
James Smarte59058c2008-08-24 21:49:00 -04001907 * @phba: Pointer to HBA context object.
1908 * @tag: Tag of the hbq buffer.
1909 *
1910 * This function is called with hbalock held. This function searches
1911 * for the hbq buffer associated with the given tag in the hbq buffer
1912 * list. If it finds the hbq buffer, it returns the hbq_buffer other wise
1913 * it returns NULL.
1914 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01001915static struct hbq_dmabuf *
James Smarted957682007-06-17 19:56:37 -05001916lpfc_sli_hbqbuf_find(struct lpfc_hba *phba, uint32_t tag)
1917{
James Smart92d7f7b2007-06-17 19:56:38 -05001918 struct lpfc_dmabuf *d_buf;
1919 struct hbq_dmabuf *hbq_buf;
James Smart51ef4c22007-08-02 11:10:31 -04001920 uint32_t hbqno;
James Smarted957682007-06-17 19:56:37 -05001921
James Smart51ef4c22007-08-02 11:10:31 -04001922 hbqno = tag >> 16;
Jesper Juhla0a74e452007-08-09 20:47:15 +02001923 if (hbqno >= LPFC_MAX_HBQS)
James Smart51ef4c22007-08-02 11:10:31 -04001924 return NULL;
1925
James Smart3772a992009-05-22 14:50:54 -04001926 spin_lock_irq(&phba->hbalock);
James Smart51ef4c22007-08-02 11:10:31 -04001927 list_for_each_entry(d_buf, &phba->hbqs[hbqno].hbq_buffer_list, list) {
James Smart92d7f7b2007-06-17 19:56:38 -05001928 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
James Smart51ef4c22007-08-02 11:10:31 -04001929 if (hbq_buf->tag == tag) {
James Smart3772a992009-05-22 14:50:54 -04001930 spin_unlock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -05001931 return hbq_buf;
James Smarted957682007-06-17 19:56:37 -05001932 }
1933 }
James Smart3772a992009-05-22 14:50:54 -04001934 spin_unlock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -05001935 lpfc_printf_log(phba, KERN_ERR, LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04001936 "1803 Bad hbq tag. Data: x%x x%x\n",
James Smarta8adb832007-10-27 13:37:53 -04001937 tag, phba->hbqs[tag >> 16].buffer_count);
James Smart92d7f7b2007-06-17 19:56:38 -05001938 return NULL;
James Smarted957682007-06-17 19:56:37 -05001939}
1940
James Smarte59058c2008-08-24 21:49:00 -04001941/**
James Smart3621a712009-04-06 18:47:14 -04001942 * lpfc_sli_free_hbq - Give back the hbq buffer to firmware
James Smarte59058c2008-08-24 21:49:00 -04001943 * @phba: Pointer to HBA context object.
1944 * @hbq_buffer: Pointer to HBQ buffer.
1945 *
1946 * This function is called with hbalock. This function gives back
1947 * the hbq buffer to firmware. If the HBQ does not have space to
1948 * post the buffer, it will free the buffer.
1949 **/
James Smarted957682007-06-17 19:56:37 -05001950void
James Smart51ef4c22007-08-02 11:10:31 -04001951lpfc_sli_free_hbq(struct lpfc_hba *phba, struct hbq_dmabuf *hbq_buffer)
James Smarted957682007-06-17 19:56:37 -05001952{
1953 uint32_t hbqno;
1954
James Smart51ef4c22007-08-02 11:10:31 -04001955 if (hbq_buffer) {
1956 hbqno = hbq_buffer->tag >> 16;
James Smart3772a992009-05-22 14:50:54 -04001957 if (lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer))
James Smart51ef4c22007-08-02 11:10:31 -04001958 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
James Smarted957682007-06-17 19:56:37 -05001959 }
1960}
1961
James Smarte59058c2008-08-24 21:49:00 -04001962/**
James Smart3621a712009-04-06 18:47:14 -04001963 * lpfc_sli_chk_mbx_command - Check if the mailbox is a legitimate mailbox
James Smarte59058c2008-08-24 21:49:00 -04001964 * @mbxCommand: mailbox command code.
1965 *
1966 * This function is called by the mailbox event handler function to verify
1967 * that the completed mailbox command is a legitimate mailbox command. If the
1968 * completed mailbox is not known to the function, it will return MBX_SHUTDOWN
1969 * and the mailbox event handler will take the HBA offline.
1970 **/
dea31012005-04-17 16:05:31 -05001971static int
1972lpfc_sli_chk_mbx_command(uint8_t mbxCommand)
1973{
1974 uint8_t ret;
1975
1976 switch (mbxCommand) {
1977 case MBX_LOAD_SM:
1978 case MBX_READ_NV:
1979 case MBX_WRITE_NV:
James Smarta8adb832007-10-27 13:37:53 -04001980 case MBX_WRITE_VPARMS:
dea31012005-04-17 16:05:31 -05001981 case MBX_RUN_BIU_DIAG:
1982 case MBX_INIT_LINK:
1983 case MBX_DOWN_LINK:
1984 case MBX_CONFIG_LINK:
1985 case MBX_CONFIG_RING:
1986 case MBX_RESET_RING:
1987 case MBX_READ_CONFIG:
1988 case MBX_READ_RCONFIG:
1989 case MBX_READ_SPARM:
1990 case MBX_READ_STATUS:
1991 case MBX_READ_RPI:
1992 case MBX_READ_XRI:
1993 case MBX_READ_REV:
1994 case MBX_READ_LNK_STAT:
1995 case MBX_REG_LOGIN:
1996 case MBX_UNREG_LOGIN:
dea31012005-04-17 16:05:31 -05001997 case MBX_CLEAR_LA:
1998 case MBX_DUMP_MEMORY:
1999 case MBX_DUMP_CONTEXT:
2000 case MBX_RUN_DIAGS:
2001 case MBX_RESTART:
2002 case MBX_UPDATE_CFG:
2003 case MBX_DOWN_LOAD:
2004 case MBX_DEL_LD_ENTRY:
2005 case MBX_RUN_PROGRAM:
2006 case MBX_SET_MASK:
James Smart09372822008-01-11 01:52:54 -05002007 case MBX_SET_VARIABLE:
dea31012005-04-17 16:05:31 -05002008 case MBX_UNREG_D_ID:
Jamie Wellnitz41415862006-02-28 19:25:27 -05002009 case MBX_KILL_BOARD:
dea31012005-04-17 16:05:31 -05002010 case MBX_CONFIG_FARP:
Jamie Wellnitz41415862006-02-28 19:25:27 -05002011 case MBX_BEACON:
dea31012005-04-17 16:05:31 -05002012 case MBX_LOAD_AREA:
2013 case MBX_RUN_BIU_DIAG64:
2014 case MBX_CONFIG_PORT:
2015 case MBX_READ_SPARM64:
2016 case MBX_READ_RPI64:
2017 case MBX_REG_LOGIN64:
James Smart76a95d72010-11-20 23:11:48 -05002018 case MBX_READ_TOPOLOGY:
James Smart09372822008-01-11 01:52:54 -05002019 case MBX_WRITE_WWN:
dea31012005-04-17 16:05:31 -05002020 case MBX_SET_DEBUG:
2021 case MBX_LOAD_EXP_ROM:
James Smart57127f12007-10-27 13:37:05 -04002022 case MBX_ASYNCEVT_ENABLE:
James Smart92d7f7b2007-06-17 19:56:38 -05002023 case MBX_REG_VPI:
2024 case MBX_UNREG_VPI:
James Smart858c9f62007-06-17 19:56:39 -05002025 case MBX_HEARTBEAT:
James Smart84774a42008-08-24 21:50:06 -04002026 case MBX_PORT_CAPABILITIES:
2027 case MBX_PORT_IOV_CONTROL:
James Smart04c68492009-05-22 14:52:52 -04002028 case MBX_SLI4_CONFIG:
2029 case MBX_SLI4_REQ_FTRS:
2030 case MBX_REG_FCFI:
2031 case MBX_UNREG_FCFI:
2032 case MBX_REG_VFI:
2033 case MBX_UNREG_VFI:
2034 case MBX_INIT_VPI:
2035 case MBX_INIT_VFI:
2036 case MBX_RESUME_RPI:
James Smartc7495932010-04-06 15:05:28 -04002037 case MBX_READ_EVENT_LOG_STATUS:
2038 case MBX_READ_EVENT_LOG:
James Smartdcf2a4e2010-09-29 11:18:53 -04002039 case MBX_SECURITY_MGMT:
2040 case MBX_AUTH_PORT:
dea31012005-04-17 16:05:31 -05002041 ret = mbxCommand;
2042 break;
2043 default:
2044 ret = MBX_SHUTDOWN;
2045 break;
2046 }
James Smart2e0fef82007-06-17 19:56:36 -05002047 return ret;
dea31012005-04-17 16:05:31 -05002048}
James Smarte59058c2008-08-24 21:49:00 -04002049
2050/**
James Smart3621a712009-04-06 18:47:14 -04002051 * lpfc_sli_wake_mbox_wait - lpfc_sli_issue_mbox_wait mbox completion handler
James Smarte59058c2008-08-24 21:49:00 -04002052 * @phba: Pointer to HBA context object.
2053 * @pmboxq: Pointer to mailbox command.
2054 *
2055 * This is completion handler function for mailbox commands issued from
2056 * lpfc_sli_issue_mbox_wait function. This function is called by the
2057 * mailbox event handler function with no lock held. This function
2058 * will wake up thread waiting on the wait queue pointed by context1
2059 * of the mailbox.
2060 **/
James Smart04c68492009-05-22 14:52:52 -04002061void
James Smart2e0fef82007-06-17 19:56:36 -05002062lpfc_sli_wake_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
dea31012005-04-17 16:05:31 -05002063{
2064 wait_queue_head_t *pdone_q;
James Smart858c9f62007-06-17 19:56:39 -05002065 unsigned long drvr_flag;
dea31012005-04-17 16:05:31 -05002066
2067 /*
2068 * If pdone_q is empty, the driver thread gave up waiting and
2069 * continued running.
2070 */
James Smart7054a602007-04-25 09:52:34 -04002071 pmboxq->mbox_flag |= LPFC_MBX_WAKE;
James Smart858c9f62007-06-17 19:56:39 -05002072 spin_lock_irqsave(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05002073 pdone_q = (wait_queue_head_t *) pmboxq->context1;
2074 if (pdone_q)
2075 wake_up_interruptible(pdone_q);
James Smart858c9f62007-06-17 19:56:39 -05002076 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05002077 return;
2078}
2079
James Smarte59058c2008-08-24 21:49:00 -04002080
2081/**
James Smart3621a712009-04-06 18:47:14 -04002082 * lpfc_sli_def_mbox_cmpl - Default mailbox completion handler
James Smarte59058c2008-08-24 21:49:00 -04002083 * @phba: Pointer to HBA context object.
2084 * @pmb: Pointer to mailbox object.
2085 *
2086 * This function is the default mailbox completion handler. It
2087 * frees the memory resources associated with the completed mailbox
2088 * command. If the completed command is a REG_LOGIN mailbox command,
2089 * this function will issue a UREG_LOGIN to re-claim the RPI.
2090 **/
dea31012005-04-17 16:05:31 -05002091void
James Smart2e0fef82007-06-17 19:56:36 -05002092lpfc_sli_def_mbox_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05002093{
James Smartd439d282010-09-29 11:18:45 -04002094 struct lpfc_vport *vport = pmb->vport;
dea31012005-04-17 16:05:31 -05002095 struct lpfc_dmabuf *mp;
James Smartd439d282010-09-29 11:18:45 -04002096 struct lpfc_nodelist *ndlp;
James Smart5af5eee2010-10-22 11:06:38 -04002097 struct Scsi_Host *shost;
James Smart04c68492009-05-22 14:52:52 -04002098 uint16_t rpi, vpi;
James Smart7054a602007-04-25 09:52:34 -04002099 int rc;
2100
dea31012005-04-17 16:05:31 -05002101 mp = (struct lpfc_dmabuf *) (pmb->context1);
James Smart7054a602007-04-25 09:52:34 -04002102
dea31012005-04-17 16:05:31 -05002103 if (mp) {
2104 lpfc_mbuf_free(phba, mp->virt, mp->phys);
2105 kfree(mp);
2106 }
James Smart7054a602007-04-25 09:52:34 -04002107
2108 /*
2109 * If a REG_LOGIN succeeded after node is destroyed or node
2110 * is in re-discovery driver need to cleanup the RPI.
2111 */
James Smart2e0fef82007-06-17 19:56:36 -05002112 if (!(phba->pport->load_flag & FC_UNLOADING) &&
James Smart04c68492009-05-22 14:52:52 -04002113 pmb->u.mb.mbxCommand == MBX_REG_LOGIN64 &&
2114 !pmb->u.mb.mbxStatus) {
2115 rpi = pmb->u.mb.un.varWords[0];
2116 vpi = pmb->u.mb.un.varRegLogin.vpi - phba->vpi_base;
2117 lpfc_unreg_login(phba, vpi, rpi, pmb);
James Smart92d7f7b2007-06-17 19:56:38 -05002118 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart7054a602007-04-25 09:52:34 -04002119 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
2120 if (rc != MBX_NOT_FINISHED)
2121 return;
2122 }
2123
James Smart695a8142010-01-26 23:08:03 -05002124 if ((pmb->u.mb.mbxCommand == MBX_REG_VPI) &&
2125 !(phba->pport->load_flag & FC_UNLOADING) &&
2126 !pmb->u.mb.mbxStatus) {
James Smart5af5eee2010-10-22 11:06:38 -04002127 shost = lpfc_shost_from_vport(vport);
2128 spin_lock_irq(shost->host_lock);
2129 vport->vpi_state |= LPFC_VPI_REGISTERED;
2130 vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
2131 spin_unlock_irq(shost->host_lock);
James Smart695a8142010-01-26 23:08:03 -05002132 }
2133
James Smartd439d282010-09-29 11:18:45 -04002134 if (pmb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
2135 ndlp = (struct lpfc_nodelist *)pmb->context2;
2136 lpfc_nlp_put(ndlp);
2137 pmb->context2 = NULL;
2138 }
2139
James Smartdcf2a4e2010-09-29 11:18:53 -04002140 /* Check security permission status on INIT_LINK mailbox command */
2141 if ((pmb->u.mb.mbxCommand == MBX_INIT_LINK) &&
2142 (pmb->u.mb.mbxStatus == MBXERR_SEC_NO_PERMISSION))
2143 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
2144 "2860 SLI authentication is required "
2145 "for INIT_LINK but has not done yet\n");
2146
James Smart04c68492009-05-22 14:52:52 -04002147 if (bf_get(lpfc_mqe_command, &pmb->u.mqe) == MBX_SLI4_CONFIG)
2148 lpfc_sli4_mbox_cmd_free(phba, pmb);
2149 else
2150 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05002151}
2152
James Smarte59058c2008-08-24 21:49:00 -04002153/**
James Smart3621a712009-04-06 18:47:14 -04002154 * lpfc_sli_handle_mb_event - Handle mailbox completions from firmware
James Smarte59058c2008-08-24 21:49:00 -04002155 * @phba: Pointer to HBA context object.
2156 *
2157 * This function is called with no lock held. This function processes all
2158 * the completed mailbox commands and gives it to upper layers. The interrupt
2159 * service routine processes mailbox completion interrupt and adds completed
2160 * mailbox commands to the mboxq_cmpl queue and signals the worker thread.
2161 * Worker thread call lpfc_sli_handle_mb_event, which will return the
2162 * completed mailbox commands in mboxq_cmpl queue to the upper layers. This
2163 * function returns the mailbox commands to the upper layer by calling the
2164 * completion handler function of each mailbox.
2165 **/
dea31012005-04-17 16:05:31 -05002166int
James Smart2e0fef82007-06-17 19:56:36 -05002167lpfc_sli_handle_mb_event(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05002168{
James Smart92d7f7b2007-06-17 19:56:38 -05002169 MAILBOX_t *pmbox;
dea31012005-04-17 16:05:31 -05002170 LPFC_MBOXQ_t *pmb;
James Smart92d7f7b2007-06-17 19:56:38 -05002171 int rc;
2172 LIST_HEAD(cmplq);
dea31012005-04-17 16:05:31 -05002173
2174 phba->sli.slistat.mbox_event++;
2175
James Smart92d7f7b2007-06-17 19:56:38 -05002176 /* Get all completed mailboxe buffers into the cmplq */
2177 spin_lock_irq(&phba->hbalock);
2178 list_splice_init(&phba->sli.mboxq_cmpl, &cmplq);
2179 spin_unlock_irq(&phba->hbalock);
2180
dea31012005-04-17 16:05:31 -05002181 /* Get a Mailbox buffer to setup mailbox commands for callback */
James Smart92d7f7b2007-06-17 19:56:38 -05002182 do {
2183 list_remove_head(&cmplq, pmb, LPFC_MBOXQ_t, list);
2184 if (pmb == NULL)
2185 break;
2186
James Smart04c68492009-05-22 14:52:52 -04002187 pmbox = &pmb->u.mb;
dea31012005-04-17 16:05:31 -05002188
James Smart858c9f62007-06-17 19:56:39 -05002189 if (pmbox->mbxCommand != MBX_HEARTBEAT) {
2190 if (pmb->vport) {
2191 lpfc_debugfs_disc_trc(pmb->vport,
2192 LPFC_DISC_TRC_MBOX_VPORT,
2193 "MBOX cmpl vport: cmd:x%x mb:x%x x%x",
2194 (uint32_t)pmbox->mbxCommand,
2195 pmbox->un.varWords[0],
2196 pmbox->un.varWords[1]);
2197 }
2198 else {
2199 lpfc_debugfs_disc_trc(phba->pport,
2200 LPFC_DISC_TRC_MBOX,
2201 "MBOX cmpl: cmd:x%x mb:x%x x%x",
2202 (uint32_t)pmbox->mbxCommand,
2203 pmbox->un.varWords[0],
2204 pmbox->un.varWords[1]);
2205 }
2206 }
2207
dea31012005-04-17 16:05:31 -05002208 /*
2209 * It is a fatal error if unknown mbox command completion.
2210 */
2211 if (lpfc_sli_chk_mbx_command(pmbox->mbxCommand) ==
2212 MBX_SHUTDOWN) {
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02002213 /* Unknown mailbox command compl */
James Smart92d7f7b2007-06-17 19:56:38 -05002214 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002215 "(%d):0323 Unknown Mailbox command "
James Smart04c68492009-05-22 14:52:52 -04002216 "x%x (x%x) Cmpl\n",
James Smart92d7f7b2007-06-17 19:56:38 -05002217 pmb->vport ? pmb->vport->vpi : 0,
James Smart04c68492009-05-22 14:52:52 -04002218 pmbox->mbxCommand,
2219 lpfc_sli4_mbox_opcode_get(phba, pmb));
James Smart2e0fef82007-06-17 19:56:36 -05002220 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05002221 phba->work_hs = HS_FFER3;
2222 lpfc_handle_eratt(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05002223 continue;
dea31012005-04-17 16:05:31 -05002224 }
2225
dea31012005-04-17 16:05:31 -05002226 if (pmbox->mbxStatus) {
2227 phba->sli.slistat.mbox_stat_err++;
2228 if (pmbox->mbxStatus == MBXERR_NO_RESOURCES) {
2229 /* Mbox cmd cmpl error - RETRYing */
James Smart92d7f7b2007-06-17 19:56:38 -05002230 lpfc_printf_log(phba, KERN_INFO,
2231 LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002232 "(%d):0305 Mbox cmd cmpl "
James Smart92d7f7b2007-06-17 19:56:38 -05002233 "error - RETRYing Data: x%x "
James Smart04c68492009-05-22 14:52:52 -04002234 "(x%x) x%x x%x x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -05002235 pmb->vport ? pmb->vport->vpi :0,
2236 pmbox->mbxCommand,
James Smart04c68492009-05-22 14:52:52 -04002237 lpfc_sli4_mbox_opcode_get(phba,
2238 pmb),
James Smart92d7f7b2007-06-17 19:56:38 -05002239 pmbox->mbxStatus,
2240 pmbox->un.varWords[0],
2241 pmb->vport->port_state);
dea31012005-04-17 16:05:31 -05002242 pmbox->mbxStatus = 0;
2243 pmbox->mbxOwner = OWN_HOST;
dea31012005-04-17 16:05:31 -05002244 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
James Smart04c68492009-05-22 14:52:52 -04002245 if (rc != MBX_NOT_FINISHED)
James Smart92d7f7b2007-06-17 19:56:38 -05002246 continue;
dea31012005-04-17 16:05:31 -05002247 }
2248 }
2249
2250 /* Mailbox cmd <cmd> Cmpl <cmpl> */
James Smart92d7f7b2007-06-17 19:56:38 -05002251 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smart04c68492009-05-22 14:52:52 -04002252 "(%d):0307 Mailbox cmd x%x (x%x) Cmpl x%p "
dea31012005-04-17 16:05:31 -05002253 "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 -05002254 pmb->vport ? pmb->vport->vpi : 0,
dea31012005-04-17 16:05:31 -05002255 pmbox->mbxCommand,
James Smart04c68492009-05-22 14:52:52 -04002256 lpfc_sli4_mbox_opcode_get(phba, pmb),
dea31012005-04-17 16:05:31 -05002257 pmb->mbox_cmpl,
2258 *((uint32_t *) pmbox),
2259 pmbox->un.varWords[0],
2260 pmbox->un.varWords[1],
2261 pmbox->un.varWords[2],
2262 pmbox->un.varWords[3],
2263 pmbox->un.varWords[4],
2264 pmbox->un.varWords[5],
2265 pmbox->un.varWords[6],
2266 pmbox->un.varWords[7]);
2267
James Smart92d7f7b2007-06-17 19:56:38 -05002268 if (pmb->mbox_cmpl)
dea31012005-04-17 16:05:31 -05002269 pmb->mbox_cmpl(phba,pmb);
James Smart92d7f7b2007-06-17 19:56:38 -05002270 } while (1);
James Smart2e0fef82007-06-17 19:56:36 -05002271 return 0;
dea31012005-04-17 16:05:31 -05002272}
James Smart92d7f7b2007-06-17 19:56:38 -05002273
James Smarte59058c2008-08-24 21:49:00 -04002274/**
James Smart3621a712009-04-06 18:47:14 -04002275 * lpfc_sli_get_buff - Get the buffer associated with the buffer tag
James Smarte59058c2008-08-24 21:49:00 -04002276 * @phba: Pointer to HBA context object.
2277 * @pring: Pointer to driver SLI ring object.
2278 * @tag: buffer tag.
2279 *
2280 * This function is called with no lock held. When QUE_BUFTAG_BIT bit
2281 * is set in the tag the buffer is posted for a particular exchange,
2282 * the function will return the buffer without replacing the buffer.
2283 * If the buffer is for unsolicited ELS or CT traffic, this function
2284 * returns the buffer and also posts another buffer to the firmware.
2285 **/
James Smart76bb24e2007-10-27 13:38:00 -04002286static struct lpfc_dmabuf *
2287lpfc_sli_get_buff(struct lpfc_hba *phba,
James Smart9f1e1b52008-12-04 22:39:40 -05002288 struct lpfc_sli_ring *pring,
2289 uint32_t tag)
James Smart76bb24e2007-10-27 13:38:00 -04002290{
James Smart9f1e1b52008-12-04 22:39:40 -05002291 struct hbq_dmabuf *hbq_entry;
2292
James Smart76bb24e2007-10-27 13:38:00 -04002293 if (tag & QUE_BUFTAG_BIT)
2294 return lpfc_sli_ring_taggedbuf_get(phba, pring, tag);
James Smart9f1e1b52008-12-04 22:39:40 -05002295 hbq_entry = lpfc_sli_hbqbuf_find(phba, tag);
2296 if (!hbq_entry)
2297 return NULL;
2298 return &hbq_entry->dbuf;
James Smart76bb24e2007-10-27 13:38:00 -04002299}
James Smart57127f12007-10-27 13:37:05 -04002300
James Smart3772a992009-05-22 14:50:54 -04002301/**
2302 * lpfc_complete_unsol_iocb - Complete an unsolicited sequence
2303 * @phba: Pointer to HBA context object.
2304 * @pring: Pointer to driver SLI ring object.
2305 * @saveq: Pointer to the iocbq struct representing the sequence starting frame.
2306 * @fch_r_ctl: the r_ctl for the first frame of the sequence.
2307 * @fch_type: the type for the first frame of the sequence.
2308 *
2309 * This function is called with no lock held. This function uses the r_ctl and
2310 * type of the received sequence to find the correct callback function to call
2311 * to process the sequence.
2312 **/
2313static int
2314lpfc_complete_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2315 struct lpfc_iocbq *saveq, uint32_t fch_r_ctl,
2316 uint32_t fch_type)
2317{
2318 int i;
2319
2320 /* unSolicited Responses */
2321 if (pring->prt[0].profile) {
2322 if (pring->prt[0].lpfc_sli_rcv_unsol_event)
2323 (pring->prt[0].lpfc_sli_rcv_unsol_event) (phba, pring,
2324 saveq);
2325 return 1;
2326 }
2327 /* We must search, based on rctl / type
2328 for the right routine */
2329 for (i = 0; i < pring->num_mask; i++) {
2330 if ((pring->prt[i].rctl == fch_r_ctl) &&
2331 (pring->prt[i].type == fch_type)) {
2332 if (pring->prt[i].lpfc_sli_rcv_unsol_event)
2333 (pring->prt[i].lpfc_sli_rcv_unsol_event)
2334 (phba, pring, saveq);
2335 return 1;
2336 }
2337 }
2338 return 0;
2339}
James Smarte59058c2008-08-24 21:49:00 -04002340
2341/**
James Smart3621a712009-04-06 18:47:14 -04002342 * lpfc_sli_process_unsol_iocb - Unsolicited iocb handler
James Smarte59058c2008-08-24 21:49:00 -04002343 * @phba: Pointer to HBA context object.
2344 * @pring: Pointer to driver SLI ring object.
2345 * @saveq: Pointer to the unsolicited iocb.
2346 *
2347 * This function is called with no lock held by the ring event handler
2348 * when there is an unsolicited iocb posted to the response ring by the
2349 * firmware. This function gets the buffer associated with the iocbs
2350 * and calls the event handler for the ring. This function handles both
2351 * qring buffers and hbq buffers.
2352 * When the function returns 1 the caller can free the iocb object otherwise
2353 * upper layer functions will free the iocb objects.
2354 **/
dea31012005-04-17 16:05:31 -05002355static int
2356lpfc_sli_process_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2357 struct lpfc_iocbq *saveq)
2358{
2359 IOCB_t * irsp;
2360 WORD5 * w5p;
2361 uint32_t Rctl, Type;
James Smart3772a992009-05-22 14:50:54 -04002362 uint32_t match;
James Smart76bb24e2007-10-27 13:38:00 -04002363 struct lpfc_iocbq *iocbq;
James Smart3163f722008-02-08 18:50:25 -05002364 struct lpfc_dmabuf *dmzbuf;
dea31012005-04-17 16:05:31 -05002365
2366 match = 0;
2367 irsp = &(saveq->iocb);
James Smart57127f12007-10-27 13:37:05 -04002368
2369 if (irsp->ulpCommand == CMD_ASYNC_STATUS) {
2370 if (pring->lpfc_sli_rcv_async_status)
2371 pring->lpfc_sli_rcv_async_status(phba, pring, saveq);
2372 else
2373 lpfc_printf_log(phba,
2374 KERN_WARNING,
2375 LOG_SLI,
2376 "0316 Ring %d handler: unexpected "
2377 "ASYNC_STATUS iocb received evt_code "
2378 "0x%x\n",
2379 pring->ringno,
2380 irsp->un.asyncstat.evt_code);
2381 return 1;
2382 }
2383
James Smart3163f722008-02-08 18:50:25 -05002384 if ((irsp->ulpCommand == CMD_IOCB_RET_XRI64_CX) &&
2385 (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)) {
2386 if (irsp->ulpBdeCount > 0) {
2387 dmzbuf = lpfc_sli_get_buff(phba, pring,
2388 irsp->un.ulpWord[3]);
2389 lpfc_in_buf_free(phba, dmzbuf);
2390 }
2391
2392 if (irsp->ulpBdeCount > 1) {
2393 dmzbuf = lpfc_sli_get_buff(phba, pring,
2394 irsp->unsli3.sli3Words[3]);
2395 lpfc_in_buf_free(phba, dmzbuf);
2396 }
2397
2398 if (irsp->ulpBdeCount > 2) {
2399 dmzbuf = lpfc_sli_get_buff(phba, pring,
2400 irsp->unsli3.sli3Words[7]);
2401 lpfc_in_buf_free(phba, dmzbuf);
2402 }
2403
2404 return 1;
2405 }
2406
James Smart92d7f7b2007-06-17 19:56:38 -05002407 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
James Smart76bb24e2007-10-27 13:38:00 -04002408 if (irsp->ulpBdeCount != 0) {
2409 saveq->context2 = lpfc_sli_get_buff(phba, pring,
James Smart92d7f7b2007-06-17 19:56:38 -05002410 irsp->un.ulpWord[3]);
James Smart76bb24e2007-10-27 13:38:00 -04002411 if (!saveq->context2)
2412 lpfc_printf_log(phba,
2413 KERN_ERR,
2414 LOG_SLI,
2415 "0341 Ring %d Cannot find buffer for "
2416 "an unsolicited iocb. tag 0x%x\n",
2417 pring->ringno,
2418 irsp->un.ulpWord[3]);
James Smart76bb24e2007-10-27 13:38:00 -04002419 }
2420 if (irsp->ulpBdeCount == 2) {
2421 saveq->context3 = lpfc_sli_get_buff(phba, pring,
James Smart51ef4c22007-08-02 11:10:31 -04002422 irsp->unsli3.sli3Words[7]);
James Smart76bb24e2007-10-27 13:38:00 -04002423 if (!saveq->context3)
2424 lpfc_printf_log(phba,
2425 KERN_ERR,
2426 LOG_SLI,
2427 "0342 Ring %d Cannot find buffer for an"
2428 " unsolicited iocb. tag 0x%x\n",
2429 pring->ringno,
2430 irsp->unsli3.sli3Words[7]);
2431 }
2432 list_for_each_entry(iocbq, &saveq->list, list) {
James Smart76bb24e2007-10-27 13:38:00 -04002433 irsp = &(iocbq->iocb);
James Smart76bb24e2007-10-27 13:38:00 -04002434 if (irsp->ulpBdeCount != 0) {
2435 iocbq->context2 = lpfc_sli_get_buff(phba, pring,
2436 irsp->un.ulpWord[3]);
James Smart9c2face2008-01-11 01:53:18 -05002437 if (!iocbq->context2)
James Smart76bb24e2007-10-27 13:38:00 -04002438 lpfc_printf_log(phba,
2439 KERN_ERR,
2440 LOG_SLI,
2441 "0343 Ring %d Cannot find "
2442 "buffer for an unsolicited iocb"
2443 ". tag 0x%x\n", pring->ringno,
2444 irsp->un.ulpWord[3]);
2445 }
2446 if (irsp->ulpBdeCount == 2) {
2447 iocbq->context3 = lpfc_sli_get_buff(phba, pring,
2448 irsp->unsli3.sli3Words[7]);
James Smart9c2face2008-01-11 01:53:18 -05002449 if (!iocbq->context3)
James Smart76bb24e2007-10-27 13:38:00 -04002450 lpfc_printf_log(phba,
2451 KERN_ERR,
2452 LOG_SLI,
2453 "0344 Ring %d Cannot find "
2454 "buffer for an unsolicited "
2455 "iocb. tag 0x%x\n",
2456 pring->ringno,
2457 irsp->unsli3.sli3Words[7]);
2458 }
2459 }
James Smart92d7f7b2007-06-17 19:56:38 -05002460 }
James Smart9c2face2008-01-11 01:53:18 -05002461 if (irsp->ulpBdeCount != 0 &&
2462 (irsp->ulpCommand == CMD_IOCB_RCV_CONT64_CX ||
2463 irsp->ulpStatus == IOSTAT_INTERMED_RSP)) {
2464 int found = 0;
2465
2466 /* search continue save q for same XRI */
2467 list_for_each_entry(iocbq, &pring->iocb_continue_saveq, clist) {
2468 if (iocbq->iocb.ulpContext == saveq->iocb.ulpContext) {
2469 list_add_tail(&saveq->list, &iocbq->list);
2470 found = 1;
2471 break;
2472 }
2473 }
2474 if (!found)
2475 list_add_tail(&saveq->clist,
2476 &pring->iocb_continue_saveq);
2477 if (saveq->iocb.ulpStatus != IOSTAT_INTERMED_RSP) {
2478 list_del_init(&iocbq->clist);
2479 saveq = iocbq;
2480 irsp = &(saveq->iocb);
2481 } else
2482 return 0;
2483 }
2484 if ((irsp->ulpCommand == CMD_RCV_ELS_REQ64_CX) ||
2485 (irsp->ulpCommand == CMD_RCV_ELS_REQ_CX) ||
2486 (irsp->ulpCommand == CMD_IOCB_RCV_ELS64_CX)) {
James Smart6a9c52c2009-10-02 15:16:51 -04002487 Rctl = FC_RCTL_ELS_REQ;
2488 Type = FC_TYPE_ELS;
James Smart9c2face2008-01-11 01:53:18 -05002489 } else {
2490 w5p = (WORD5 *)&(saveq->iocb.un.ulpWord[5]);
2491 Rctl = w5p->hcsw.Rctl;
2492 Type = w5p->hcsw.Type;
2493
2494 /* Firmware Workaround */
2495 if ((Rctl == 0) && (pring->ringno == LPFC_ELS_RING) &&
2496 (irsp->ulpCommand == CMD_RCV_SEQUENCE64_CX ||
2497 irsp->ulpCommand == CMD_IOCB_RCV_SEQ64_CX)) {
James Smart6a9c52c2009-10-02 15:16:51 -04002498 Rctl = FC_RCTL_ELS_REQ;
2499 Type = FC_TYPE_ELS;
James Smart9c2face2008-01-11 01:53:18 -05002500 w5p->hcsw.Rctl = Rctl;
2501 w5p->hcsw.Type = Type;
2502 }
2503 }
James Smart92d7f7b2007-06-17 19:56:38 -05002504
James Smart3772a992009-05-22 14:50:54 -04002505 if (!lpfc_complete_unsol_iocb(phba, pring, saveq, Rctl, Type))
James Smart92d7f7b2007-06-17 19:56:38 -05002506 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002507 "0313 Ring %d handler: unexpected Rctl x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05002508 "Type x%x received\n",
James Smarte8b62012007-08-02 11:10:09 -04002509 pring->ringno, Rctl, Type);
James Smart3772a992009-05-22 14:50:54 -04002510
James Smart92d7f7b2007-06-17 19:56:38 -05002511 return 1;
dea31012005-04-17 16:05:31 -05002512}
2513
James Smarte59058c2008-08-24 21:49:00 -04002514/**
James Smart3621a712009-04-06 18:47:14 -04002515 * lpfc_sli_iocbq_lookup - Find command iocb for the given response iocb
James Smarte59058c2008-08-24 21:49:00 -04002516 * @phba: Pointer to HBA context object.
2517 * @pring: Pointer to driver SLI ring object.
2518 * @prspiocb: Pointer to response iocb object.
2519 *
2520 * This function looks up the iocb_lookup table to get the command iocb
2521 * corresponding to the given response iocb using the iotag of the
2522 * response iocb. This function is called with the hbalock held.
2523 * This function returns the command iocb object if it finds the command
2524 * iocb else returns NULL.
2525 **/
dea31012005-04-17 16:05:31 -05002526static struct lpfc_iocbq *
James Smart2e0fef82007-06-17 19:56:36 -05002527lpfc_sli_iocbq_lookup(struct lpfc_hba *phba,
2528 struct lpfc_sli_ring *pring,
2529 struct lpfc_iocbq *prspiocb)
dea31012005-04-17 16:05:31 -05002530{
dea31012005-04-17 16:05:31 -05002531 struct lpfc_iocbq *cmd_iocb = NULL;
2532 uint16_t iotag;
2533
James Bottomley604a3e32005-10-29 10:28:33 -05002534 iotag = prspiocb->iocb.ulpIoTag;
dea31012005-04-17 16:05:31 -05002535
James Bottomley604a3e32005-10-29 10:28:33 -05002536 if (iotag != 0 && iotag <= phba->sli.last_iotag) {
2537 cmd_iocb = phba->sli.iocbq_lookup[iotag];
James Smart92d7f7b2007-06-17 19:56:38 -05002538 list_del_init(&cmd_iocb->list);
James Smart2a9bf3d2010-06-07 15:24:45 -04002539 if (cmd_iocb->iocb_flag & LPFC_IO_ON_Q) {
2540 pring->txcmplq_cnt--;
2541 cmd_iocb->iocb_flag &= ~LPFC_IO_ON_Q;
2542 }
James Bottomley604a3e32005-10-29 10:28:33 -05002543 return cmd_iocb;
dea31012005-04-17 16:05:31 -05002544 }
2545
dea31012005-04-17 16:05:31 -05002546 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002547 "0317 iotag x%x is out off "
James Bottomley604a3e32005-10-29 10:28:33 -05002548 "range: max iotag x%x wd0 x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04002549 iotag, phba->sli.last_iotag,
James Bottomley604a3e32005-10-29 10:28:33 -05002550 *(((uint32_t *) &prspiocb->iocb) + 7));
dea31012005-04-17 16:05:31 -05002551 return NULL;
2552}
2553
James Smarte59058c2008-08-24 21:49:00 -04002554/**
James Smart3772a992009-05-22 14:50:54 -04002555 * lpfc_sli_iocbq_lookup_by_tag - Find command iocb for the iotag
2556 * @phba: Pointer to HBA context object.
2557 * @pring: Pointer to driver SLI ring object.
2558 * @iotag: IOCB tag.
2559 *
2560 * This function looks up the iocb_lookup table to get the command iocb
2561 * corresponding to the given iotag. This function is called with the
2562 * hbalock held.
2563 * This function returns the command iocb object if it finds the command
2564 * iocb else returns NULL.
2565 **/
2566static struct lpfc_iocbq *
2567lpfc_sli_iocbq_lookup_by_tag(struct lpfc_hba *phba,
2568 struct lpfc_sli_ring *pring, uint16_t iotag)
2569{
2570 struct lpfc_iocbq *cmd_iocb;
2571
2572 if (iotag != 0 && iotag <= phba->sli.last_iotag) {
2573 cmd_iocb = phba->sli.iocbq_lookup[iotag];
2574 list_del_init(&cmd_iocb->list);
James Smart2a9bf3d2010-06-07 15:24:45 -04002575 if (cmd_iocb->iocb_flag & LPFC_IO_ON_Q) {
2576 cmd_iocb->iocb_flag &= ~LPFC_IO_ON_Q;
2577 pring->txcmplq_cnt--;
2578 }
James Smart3772a992009-05-22 14:50:54 -04002579 return cmd_iocb;
2580 }
2581
2582 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2583 "0372 iotag x%x is out off range: max iotag (x%x)\n",
2584 iotag, phba->sli.last_iotag);
2585 return NULL;
2586}
2587
2588/**
James Smart3621a712009-04-06 18:47:14 -04002589 * lpfc_sli_process_sol_iocb - process solicited iocb completion
James Smarte59058c2008-08-24 21:49:00 -04002590 * @phba: Pointer to HBA context object.
2591 * @pring: Pointer to driver SLI ring object.
2592 * @saveq: Pointer to the response iocb to be processed.
2593 *
2594 * This function is called by the ring event handler for non-fcp
2595 * rings when there is a new response iocb in the response ring.
2596 * The caller is not required to hold any locks. This function
2597 * gets the command iocb associated with the response iocb and
2598 * calls the completion handler for the command iocb. If there
2599 * is no completion handler, the function will free the resources
2600 * associated with command iocb. If the response iocb is for
2601 * an already aborted command iocb, the status of the completion
2602 * is changed to IOSTAT_LOCAL_REJECT/IOERR_SLI_ABORTED.
2603 * This function always returns 1.
2604 **/
dea31012005-04-17 16:05:31 -05002605static int
James Smart2e0fef82007-06-17 19:56:36 -05002606lpfc_sli_process_sol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
dea31012005-04-17 16:05:31 -05002607 struct lpfc_iocbq *saveq)
2608{
James Smart2e0fef82007-06-17 19:56:36 -05002609 struct lpfc_iocbq *cmdiocbp;
dea31012005-04-17 16:05:31 -05002610 int rc = 1;
2611 unsigned long iflag;
2612
2613 /* Based on the iotag field, get the cmd IOCB from the txcmplq */
James Smart2e0fef82007-06-17 19:56:36 -05002614 spin_lock_irqsave(&phba->hbalock, iflag);
James Bottomley604a3e32005-10-29 10:28:33 -05002615 cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring, saveq);
James Smart2e0fef82007-06-17 19:56:36 -05002616 spin_unlock_irqrestore(&phba->hbalock, iflag);
2617
dea31012005-04-17 16:05:31 -05002618 if (cmdiocbp) {
2619 if (cmdiocbp->iocb_cmpl) {
2620 /*
James Smartea2151b2008-09-07 11:52:10 -04002621 * If an ELS command failed send an event to mgmt
2622 * application.
2623 */
2624 if (saveq->iocb.ulpStatus &&
2625 (pring->ringno == LPFC_ELS_RING) &&
2626 (cmdiocbp->iocb.ulpCommand ==
2627 CMD_ELS_REQUEST64_CR))
2628 lpfc_send_els_failure_event(phba,
2629 cmdiocbp, saveq);
2630
2631 /*
dea31012005-04-17 16:05:31 -05002632 * Post all ELS completions to the worker thread.
2633 * All other are passed to the completion callback.
2634 */
2635 if (pring->ringno == LPFC_ELS_RING) {
James Smart341af102010-01-26 23:07:37 -05002636 if ((phba->sli_rev < LPFC_SLI_REV4) &&
2637 (cmdiocbp->iocb_flag &
2638 LPFC_DRIVER_ABORTED)) {
2639 spin_lock_irqsave(&phba->hbalock,
2640 iflag);
James Smart07951072007-04-25 09:51:38 -04002641 cmdiocbp->iocb_flag &=
2642 ~LPFC_DRIVER_ABORTED;
James Smart341af102010-01-26 23:07:37 -05002643 spin_unlock_irqrestore(&phba->hbalock,
2644 iflag);
James Smart07951072007-04-25 09:51:38 -04002645 saveq->iocb.ulpStatus =
2646 IOSTAT_LOCAL_REJECT;
2647 saveq->iocb.un.ulpWord[4] =
2648 IOERR_SLI_ABORTED;
James Smart0ff10d42008-01-11 01:52:36 -05002649
2650 /* Firmware could still be in progress
2651 * of DMAing payload, so don't free data
2652 * buffer till after a hbeat.
2653 */
James Smart341af102010-01-26 23:07:37 -05002654 spin_lock_irqsave(&phba->hbalock,
2655 iflag);
James Smart0ff10d42008-01-11 01:52:36 -05002656 saveq->iocb_flag |= LPFC_DELAY_MEM_FREE;
James Smart341af102010-01-26 23:07:37 -05002657 spin_unlock_irqrestore(&phba->hbalock,
2658 iflag);
2659 }
James Smart0f65ff62010-02-26 14:14:23 -05002660 if (phba->sli_rev == LPFC_SLI_REV4) {
2661 if (saveq->iocb_flag &
2662 LPFC_EXCHANGE_BUSY) {
2663 /* Set cmdiocb flag for the
2664 * exchange busy so sgl (xri)
2665 * will not be released until
2666 * the abort xri is received
2667 * from hba.
2668 */
2669 spin_lock_irqsave(
2670 &phba->hbalock, iflag);
2671 cmdiocbp->iocb_flag |=
2672 LPFC_EXCHANGE_BUSY;
2673 spin_unlock_irqrestore(
2674 &phba->hbalock, iflag);
2675 }
2676 if (cmdiocbp->iocb_flag &
2677 LPFC_DRIVER_ABORTED) {
2678 /*
2679 * Clear LPFC_DRIVER_ABORTED
2680 * bit in case it was driver
2681 * initiated abort.
2682 */
2683 spin_lock_irqsave(
2684 &phba->hbalock, iflag);
2685 cmdiocbp->iocb_flag &=
2686 ~LPFC_DRIVER_ABORTED;
2687 spin_unlock_irqrestore(
2688 &phba->hbalock, iflag);
2689 cmdiocbp->iocb.ulpStatus =
2690 IOSTAT_LOCAL_REJECT;
2691 cmdiocbp->iocb.un.ulpWord[4] =
2692 IOERR_ABORT_REQUESTED;
2693 /*
2694 * For SLI4, irsiocb contains
2695 * NO_XRI in sli_xritag, it
2696 * shall not affect releasing
2697 * sgl (xri) process.
2698 */
2699 saveq->iocb.ulpStatus =
2700 IOSTAT_LOCAL_REJECT;
2701 saveq->iocb.un.ulpWord[4] =
2702 IOERR_SLI_ABORTED;
2703 spin_lock_irqsave(
2704 &phba->hbalock, iflag);
2705 saveq->iocb_flag |=
2706 LPFC_DELAY_MEM_FREE;
2707 spin_unlock_irqrestore(
2708 &phba->hbalock, iflag);
2709 }
James Smart07951072007-04-25 09:51:38 -04002710 }
dea31012005-04-17 16:05:31 -05002711 }
James Smart2e0fef82007-06-17 19:56:36 -05002712 (cmdiocbp->iocb_cmpl) (phba, cmdiocbp, saveq);
James Bottomley604a3e32005-10-29 10:28:33 -05002713 } else
2714 lpfc_sli_release_iocbq(phba, cmdiocbp);
dea31012005-04-17 16:05:31 -05002715 } else {
2716 /*
2717 * Unknown initiating command based on the response iotag.
2718 * This could be the case on the ELS ring because of
2719 * lpfc_els_abort().
2720 */
2721 if (pring->ringno != LPFC_ELS_RING) {
2722 /*
2723 * Ring <ringno> handler: unexpected completion IoTag
2724 * <IoTag>
2725 */
James Smarta257bf92009-04-06 18:48:10 -04002726 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002727 "0322 Ring %d handler: "
2728 "unexpected completion IoTag x%x "
2729 "Data: x%x x%x x%x x%x\n",
2730 pring->ringno,
2731 saveq->iocb.ulpIoTag,
2732 saveq->iocb.ulpStatus,
2733 saveq->iocb.un.ulpWord[4],
2734 saveq->iocb.ulpCommand,
2735 saveq->iocb.ulpContext);
dea31012005-04-17 16:05:31 -05002736 }
2737 }
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04002738
dea31012005-04-17 16:05:31 -05002739 return rc;
2740}
2741
James Smarte59058c2008-08-24 21:49:00 -04002742/**
James Smart3621a712009-04-06 18:47:14 -04002743 * lpfc_sli_rsp_pointers_error - Response ring pointer error handler
James Smarte59058c2008-08-24 21:49:00 -04002744 * @phba: Pointer to HBA context object.
2745 * @pring: Pointer to driver SLI ring object.
2746 *
2747 * This function is called from the iocb ring event handlers when
2748 * put pointer is ahead of the get pointer for a ring. This function signal
2749 * an error attention condition to the worker thread and the worker
2750 * thread will transition the HBA to offline state.
2751 **/
James Smart2e0fef82007-06-17 19:56:36 -05002752static void
2753lpfc_sli_rsp_pointers_error(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002754{
James Smart34b02dc2008-08-24 21:49:55 -04002755 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002756 /*
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02002757 * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002758 * rsp ring <portRspMax>
2759 */
2760 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002761 "0312 Ring %d handler: portRspPut %d "
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02002762 "is bigger than rsp ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04002763 pring->ringno, le32_to_cpu(pgp->rspPutInx),
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002764 pring->numRiocb);
2765
James Smart2e0fef82007-06-17 19:56:36 -05002766 phba->link_state = LPFC_HBA_ERROR;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002767
2768 /*
2769 * All error attention handlers are posted to
2770 * worker thread
2771 */
2772 phba->work_ha |= HA_ERATT;
2773 phba->work_hs = HS_FFER3;
James Smart92d7f7b2007-06-17 19:56:38 -05002774
James Smart5e9d9b82008-06-14 22:52:53 -04002775 lpfc_worker_wake_up(phba);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002776
2777 return;
2778}
2779
James Smarte59058c2008-08-24 21:49:00 -04002780/**
James Smart3621a712009-04-06 18:47:14 -04002781 * lpfc_poll_eratt - Error attention polling timer timeout handler
James Smart93996272008-08-24 21:50:30 -04002782 * @ptr: Pointer to address of HBA context object.
2783 *
2784 * This function is invoked by the Error Attention polling timer when the
2785 * timer times out. It will check the SLI Error Attention register for
2786 * possible attention events. If so, it will post an Error Attention event
2787 * and wake up worker thread to process it. Otherwise, it will set up the
2788 * Error Attention polling timer for the next poll.
2789 **/
2790void lpfc_poll_eratt(unsigned long ptr)
2791{
2792 struct lpfc_hba *phba;
2793 uint32_t eratt = 0;
2794
2795 phba = (struct lpfc_hba *)ptr;
2796
2797 /* Check chip HA register for error event */
2798 eratt = lpfc_sli_check_eratt(phba);
2799
2800 if (eratt)
2801 /* Tell the worker thread there is work to do */
2802 lpfc_worker_wake_up(phba);
2803 else
2804 /* Restart the timer for next eratt poll */
2805 mod_timer(&phba->eratt_poll, jiffies +
2806 HZ * LPFC_ERATT_POLL_INTERVAL);
2807 return;
2808}
2809
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002810
James Smarte59058c2008-08-24 21:49:00 -04002811/**
James Smart3621a712009-04-06 18:47:14 -04002812 * lpfc_sli_handle_fast_ring_event - Handle ring events on FCP ring
James Smarte59058c2008-08-24 21:49:00 -04002813 * @phba: Pointer to HBA context object.
2814 * @pring: Pointer to driver SLI ring object.
2815 * @mask: Host attention register mask for this ring.
2816 *
2817 * This function is called from the interrupt context when there is a ring
2818 * event for the fcp ring. The caller does not hold any lock.
2819 * The function processes each response iocb in the response ring until it
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002820 * finds an iocb with LE bit set and chains all the iocbs up to the iocb with
James Smarte59058c2008-08-24 21:49:00 -04002821 * LE bit set. The function will call the completion handler of the command iocb
2822 * if the response iocb indicates a completion for a command iocb or it is
2823 * an abort completion. The function will call lpfc_sli_process_unsol_iocb
2824 * function if this is an unsolicited iocb.
dea31012005-04-17 16:05:31 -05002825 * This routine presumes LPFC_FCP_RING handling and doesn't bother
James Smart45ed1192009-10-02 15:17:02 -04002826 * to check it explicitly.
2827 */
2828int
James Smart2e0fef82007-06-17 19:56:36 -05002829lpfc_sli_handle_fast_ring_event(struct lpfc_hba *phba,
2830 struct lpfc_sli_ring *pring, uint32_t mask)
dea31012005-04-17 16:05:31 -05002831{
James Smart34b02dc2008-08-24 21:49:55 -04002832 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
dea31012005-04-17 16:05:31 -05002833 IOCB_t *irsp = NULL;
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04002834 IOCB_t *entry = NULL;
dea31012005-04-17 16:05:31 -05002835 struct lpfc_iocbq *cmdiocbq = NULL;
2836 struct lpfc_iocbq rspiocbq;
dea31012005-04-17 16:05:31 -05002837 uint32_t status;
2838 uint32_t portRspPut, portRspMax;
2839 int rc = 1;
2840 lpfc_iocb_type type;
2841 unsigned long iflag;
2842 uint32_t rsp_cmpl = 0;
dea31012005-04-17 16:05:31 -05002843
James Smart2e0fef82007-06-17 19:56:36 -05002844 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05002845 pring->stats.iocb_event++;
2846
dea31012005-04-17 16:05:31 -05002847 /*
2848 * The next available response entry should never exceed the maximum
2849 * entries. If it does, treat it as an adapter hardware error.
2850 */
2851 portRspMax = pring->numRiocb;
2852 portRspPut = le32_to_cpu(pgp->rspPutInx);
2853 if (unlikely(portRspPut >= portRspMax)) {
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002854 lpfc_sli_rsp_pointers_error(phba, pring);
James Smart2e0fef82007-06-17 19:56:36 -05002855 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05002856 return 1;
2857 }
James Smart45ed1192009-10-02 15:17:02 -04002858 if (phba->fcp_ring_in_use) {
2859 spin_unlock_irqrestore(&phba->hbalock, iflag);
2860 return 1;
2861 } else
2862 phba->fcp_ring_in_use = 1;
dea31012005-04-17 16:05:31 -05002863
2864 rmb();
2865 while (pring->rspidx != portRspPut) {
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04002866 /*
2867 * Fetch an entry off the ring and copy it into a local data
2868 * structure. The copy involves a byte-swap since the
2869 * network byte order and pci byte orders are different.
2870 */
James Smarted957682007-06-17 19:56:37 -05002871 entry = lpfc_resp_iocb(phba, pring);
James Smart858c9f62007-06-17 19:56:39 -05002872 phba->last_completion_time = jiffies;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002873
2874 if (++pring->rspidx >= portRspMax)
2875 pring->rspidx = 0;
2876
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04002877 lpfc_sli_pcimem_bcopy((uint32_t *) entry,
2878 (uint32_t *) &rspiocbq.iocb,
James Smarted957682007-06-17 19:56:37 -05002879 phba->iocb_rsp_size);
James Smarta4bc3372006-12-02 13:34:16 -05002880 INIT_LIST_HEAD(&(rspiocbq.list));
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04002881 irsp = &rspiocbq.iocb;
2882
dea31012005-04-17 16:05:31 -05002883 type = lpfc_sli_iocb_cmd_type(irsp->ulpCommand & CMD_IOCB_MASK);
2884 pring->stats.iocb_rsp++;
2885 rsp_cmpl++;
2886
2887 if (unlikely(irsp->ulpStatus)) {
James Smart92d7f7b2007-06-17 19:56:38 -05002888 /*
2889 * If resource errors reported from HBA, reduce
2890 * queuedepths of the SCSI device.
2891 */
2892 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
2893 (irsp->un.ulpWord[4] == IOERR_NO_RESOURCES)) {
2894 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart3772a992009-05-22 14:50:54 -04002895 phba->lpfc_rampdown_queue_depth(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05002896 spin_lock_irqsave(&phba->hbalock, iflag);
2897 }
2898
dea31012005-04-17 16:05:31 -05002899 /* Rsp ring <ringno> error: IOCB */
2900 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002901 "0336 Rsp Ring %d error: IOCB Data: "
James Smart92d7f7b2007-06-17 19:56:38 -05002902 "x%x x%x x%x x%x x%x x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04002903 pring->ringno,
James Smart92d7f7b2007-06-17 19:56:38 -05002904 irsp->un.ulpWord[0],
2905 irsp->un.ulpWord[1],
2906 irsp->un.ulpWord[2],
2907 irsp->un.ulpWord[3],
2908 irsp->un.ulpWord[4],
2909 irsp->un.ulpWord[5],
James Smartd7c255b2008-08-24 21:50:00 -04002910 *(uint32_t *)&irsp->un1,
2911 *((uint32_t *)&irsp->un1 + 1));
dea31012005-04-17 16:05:31 -05002912 }
2913
2914 switch (type) {
2915 case LPFC_ABORT_IOCB:
2916 case LPFC_SOL_IOCB:
2917 /*
2918 * Idle exchange closed via ABTS from port. No iocb
2919 * resources need to be recovered.
2920 */
2921 if (unlikely(irsp->ulpCommand == CMD_XRI_ABORTED_CX)) {
James Smartdca94792006-08-01 07:34:08 -04002922 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002923 "0333 IOCB cmd 0x%x"
James Smartdca94792006-08-01 07:34:08 -04002924 " processed. Skipping"
James Smart92d7f7b2007-06-17 19:56:38 -05002925 " completion\n",
James Smartdca94792006-08-01 07:34:08 -04002926 irsp->ulpCommand);
dea31012005-04-17 16:05:31 -05002927 break;
2928 }
2929
James Bottomley604a3e32005-10-29 10:28:33 -05002930 cmdiocbq = lpfc_sli_iocbq_lookup(phba, pring,
2931 &rspiocbq);
James Smart0f65ff62010-02-26 14:14:23 -05002932 if (unlikely(!cmdiocbq))
2933 break;
2934 if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED)
2935 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
2936 if (cmdiocbq->iocb_cmpl) {
2937 spin_unlock_irqrestore(&phba->hbalock, iflag);
2938 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
2939 &rspiocbq);
2940 spin_lock_irqsave(&phba->hbalock, iflag);
2941 }
dea31012005-04-17 16:05:31 -05002942 break;
James Smarta4bc3372006-12-02 13:34:16 -05002943 case LPFC_UNSOL_IOCB:
James Smart2e0fef82007-06-17 19:56:36 -05002944 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta4bc3372006-12-02 13:34:16 -05002945 lpfc_sli_process_unsol_iocb(phba, pring, &rspiocbq);
James Smart2e0fef82007-06-17 19:56:36 -05002946 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta4bc3372006-12-02 13:34:16 -05002947 break;
dea31012005-04-17 16:05:31 -05002948 default:
2949 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
2950 char adaptermsg[LPFC_MAX_ADPTMSG];
2951 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
2952 memcpy(&adaptermsg[0], (uint8_t *) irsp,
2953 MAX_MSG_DATA);
Joe Perches898eb712007-10-18 03:06:30 -07002954 dev_warn(&((phba->pcidev)->dev),
2955 "lpfc%d: %s\n",
dea31012005-04-17 16:05:31 -05002956 phba->brd_no, adaptermsg);
2957 } else {
2958 /* Unknown IOCB command */
2959 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002960 "0334 Unknown IOCB command "
James Smart92d7f7b2007-06-17 19:56:38 -05002961 "Data: x%x, x%x x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04002962 type, irsp->ulpCommand,
James Smart92d7f7b2007-06-17 19:56:38 -05002963 irsp->ulpStatus,
2964 irsp->ulpIoTag,
2965 irsp->ulpContext);
dea31012005-04-17 16:05:31 -05002966 }
2967 break;
2968 }
2969
2970 /*
2971 * The response IOCB has been processed. Update the ring
2972 * pointer in SLIM. If the port response put pointer has not
2973 * been updated, sync the pgp->rspPutInx and fetch the new port
2974 * response put pointer.
2975 */
James Smarted957682007-06-17 19:56:37 -05002976 writel(pring->rspidx, &phba->host_gp[pring->ringno].rspGetInx);
dea31012005-04-17 16:05:31 -05002977
2978 if (pring->rspidx == portRspPut)
2979 portRspPut = le32_to_cpu(pgp->rspPutInx);
2980 }
2981
2982 if ((rsp_cmpl > 0) && (mask & HA_R0RE_REQ)) {
2983 pring->stats.iocb_rsp_full++;
2984 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
2985 writel(status, phba->CAregaddr);
2986 readl(phba->CAregaddr);
2987 }
2988 if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
2989 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
2990 pring->stats.iocb_cmd_empty++;
2991
2992 /* Force update of the local copy of cmdGetInx */
2993 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
2994 lpfc_sli_resume_iocb(phba, pring);
2995
2996 if ((pring->lpfc_sli_cmd_available))
2997 (pring->lpfc_sli_cmd_available) (phba, pring);
2998
2999 }
3000
James Smart45ed1192009-10-02 15:17:02 -04003001 phba->fcp_ring_in_use = 0;
James Smart2e0fef82007-06-17 19:56:36 -05003002 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003003 return rc;
3004}
3005
James Smarte59058c2008-08-24 21:49:00 -04003006/**
James Smart3772a992009-05-22 14:50:54 -04003007 * lpfc_sli_sp_handle_rspiocb - Handle slow-path response iocb
3008 * @phba: Pointer to HBA context object.
3009 * @pring: Pointer to driver SLI ring object.
3010 * @rspiocbp: Pointer to driver response IOCB object.
3011 *
3012 * This function is called from the worker thread when there is a slow-path
3013 * response IOCB to process. This function chains all the response iocbs until
3014 * seeing the iocb with the LE bit set. The function will call
3015 * lpfc_sli_process_sol_iocb function if the response iocb indicates a
3016 * completion of a command iocb. The function will call the
3017 * lpfc_sli_process_unsol_iocb function if this is an unsolicited iocb.
3018 * The function frees the resources or calls the completion handler if this
3019 * iocb is an abort completion. The function returns NULL when the response
3020 * iocb has the LE bit set and all the chained iocbs are processed, otherwise
3021 * this function shall chain the iocb on to the iocb_continueq and return the
3022 * response iocb passed in.
3023 **/
3024static struct lpfc_iocbq *
3025lpfc_sli_sp_handle_rspiocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
3026 struct lpfc_iocbq *rspiocbp)
3027{
3028 struct lpfc_iocbq *saveq;
3029 struct lpfc_iocbq *cmdiocbp;
3030 struct lpfc_iocbq *next_iocb;
3031 IOCB_t *irsp = NULL;
3032 uint32_t free_saveq;
3033 uint8_t iocb_cmd_type;
3034 lpfc_iocb_type type;
3035 unsigned long iflag;
3036 int rc;
3037
3038 spin_lock_irqsave(&phba->hbalock, iflag);
3039 /* First add the response iocb to the countinueq list */
3040 list_add_tail(&rspiocbp->list, &(pring->iocb_continueq));
3041 pring->iocb_continueq_cnt++;
3042
3043 /* Now, determine whetehr the list is completed for processing */
3044 irsp = &rspiocbp->iocb;
3045 if (irsp->ulpLe) {
3046 /*
3047 * By default, the driver expects to free all resources
3048 * associated with this iocb completion.
3049 */
3050 free_saveq = 1;
3051 saveq = list_get_first(&pring->iocb_continueq,
3052 struct lpfc_iocbq, list);
3053 irsp = &(saveq->iocb);
3054 list_del_init(&pring->iocb_continueq);
3055 pring->iocb_continueq_cnt = 0;
3056
3057 pring->stats.iocb_rsp++;
3058
3059 /*
3060 * If resource errors reported from HBA, reduce
3061 * queuedepths of the SCSI device.
3062 */
3063 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
3064 (irsp->un.ulpWord[4] == IOERR_NO_RESOURCES)) {
3065 spin_unlock_irqrestore(&phba->hbalock, iflag);
3066 phba->lpfc_rampdown_queue_depth(phba);
3067 spin_lock_irqsave(&phba->hbalock, iflag);
3068 }
3069
3070 if (irsp->ulpStatus) {
3071 /* Rsp ring <ringno> error: IOCB */
3072 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
3073 "0328 Rsp Ring %d error: "
3074 "IOCB Data: "
3075 "x%x x%x x%x x%x "
3076 "x%x x%x x%x x%x "
3077 "x%x x%x x%x x%x "
3078 "x%x x%x x%x x%x\n",
3079 pring->ringno,
3080 irsp->un.ulpWord[0],
3081 irsp->un.ulpWord[1],
3082 irsp->un.ulpWord[2],
3083 irsp->un.ulpWord[3],
3084 irsp->un.ulpWord[4],
3085 irsp->un.ulpWord[5],
3086 *(((uint32_t *) irsp) + 6),
3087 *(((uint32_t *) irsp) + 7),
3088 *(((uint32_t *) irsp) + 8),
3089 *(((uint32_t *) irsp) + 9),
3090 *(((uint32_t *) irsp) + 10),
3091 *(((uint32_t *) irsp) + 11),
3092 *(((uint32_t *) irsp) + 12),
3093 *(((uint32_t *) irsp) + 13),
3094 *(((uint32_t *) irsp) + 14),
3095 *(((uint32_t *) irsp) + 15));
3096 }
3097
3098 /*
3099 * Fetch the IOCB command type and call the correct completion
3100 * routine. Solicited and Unsolicited IOCBs on the ELS ring
3101 * get freed back to the lpfc_iocb_list by the discovery
3102 * kernel thread.
3103 */
3104 iocb_cmd_type = irsp->ulpCommand & CMD_IOCB_MASK;
3105 type = lpfc_sli_iocb_cmd_type(iocb_cmd_type);
3106 switch (type) {
3107 case LPFC_SOL_IOCB:
3108 spin_unlock_irqrestore(&phba->hbalock, iflag);
3109 rc = lpfc_sli_process_sol_iocb(phba, pring, saveq);
3110 spin_lock_irqsave(&phba->hbalock, iflag);
3111 break;
3112
3113 case LPFC_UNSOL_IOCB:
3114 spin_unlock_irqrestore(&phba->hbalock, iflag);
3115 rc = lpfc_sli_process_unsol_iocb(phba, pring, saveq);
3116 spin_lock_irqsave(&phba->hbalock, iflag);
3117 if (!rc)
3118 free_saveq = 0;
3119 break;
3120
3121 case LPFC_ABORT_IOCB:
3122 cmdiocbp = NULL;
3123 if (irsp->ulpCommand != CMD_XRI_ABORTED_CX)
3124 cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring,
3125 saveq);
3126 if (cmdiocbp) {
3127 /* Call the specified completion routine */
3128 if (cmdiocbp->iocb_cmpl) {
3129 spin_unlock_irqrestore(&phba->hbalock,
3130 iflag);
3131 (cmdiocbp->iocb_cmpl)(phba, cmdiocbp,
3132 saveq);
3133 spin_lock_irqsave(&phba->hbalock,
3134 iflag);
3135 } else
3136 __lpfc_sli_release_iocbq(phba,
3137 cmdiocbp);
3138 }
3139 break;
3140
3141 case LPFC_UNKNOWN_IOCB:
3142 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
3143 char adaptermsg[LPFC_MAX_ADPTMSG];
3144 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
3145 memcpy(&adaptermsg[0], (uint8_t *)irsp,
3146 MAX_MSG_DATA);
3147 dev_warn(&((phba->pcidev)->dev),
3148 "lpfc%d: %s\n",
3149 phba->brd_no, adaptermsg);
3150 } else {
3151 /* Unknown IOCB command */
3152 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3153 "0335 Unknown IOCB "
3154 "command Data: x%x "
3155 "x%x x%x x%x\n",
3156 irsp->ulpCommand,
3157 irsp->ulpStatus,
3158 irsp->ulpIoTag,
3159 irsp->ulpContext);
3160 }
3161 break;
3162 }
3163
3164 if (free_saveq) {
3165 list_for_each_entry_safe(rspiocbp, next_iocb,
3166 &saveq->list, list) {
3167 list_del(&rspiocbp->list);
3168 __lpfc_sli_release_iocbq(phba, rspiocbp);
3169 }
3170 __lpfc_sli_release_iocbq(phba, saveq);
3171 }
3172 rspiocbp = NULL;
3173 }
3174 spin_unlock_irqrestore(&phba->hbalock, iflag);
3175 return rspiocbp;
3176}
3177
3178/**
3179 * lpfc_sli_handle_slow_ring_event - Wrapper func for handling slow-path iocbs
James Smarte59058c2008-08-24 21:49:00 -04003180 * @phba: Pointer to HBA context object.
3181 * @pring: Pointer to driver SLI ring object.
3182 * @mask: Host attention register mask for this ring.
3183 *
James Smart3772a992009-05-22 14:50:54 -04003184 * This routine wraps the actual slow_ring event process routine from the
3185 * API jump table function pointer from the lpfc_hba struct.
James Smarte59058c2008-08-24 21:49:00 -04003186 **/
James Smart3772a992009-05-22 14:50:54 -04003187void
James Smart2e0fef82007-06-17 19:56:36 -05003188lpfc_sli_handle_slow_ring_event(struct lpfc_hba *phba,
3189 struct lpfc_sli_ring *pring, uint32_t mask)
dea31012005-04-17 16:05:31 -05003190{
James Smart3772a992009-05-22 14:50:54 -04003191 phba->lpfc_sli_handle_slow_ring_event(phba, pring, mask);
3192}
3193
3194/**
3195 * lpfc_sli_handle_slow_ring_event_s3 - Handle SLI3 ring event for non-FCP rings
3196 * @phba: Pointer to HBA context object.
3197 * @pring: Pointer to driver SLI ring object.
3198 * @mask: Host attention register mask for this ring.
3199 *
3200 * This function is called from the worker thread when there is a ring event
3201 * for non-fcp rings. The caller does not hold any lock. The function will
3202 * remove each response iocb in the response ring and calls the handle
3203 * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
3204 **/
3205static void
3206lpfc_sli_handle_slow_ring_event_s3(struct lpfc_hba *phba,
3207 struct lpfc_sli_ring *pring, uint32_t mask)
3208{
James Smart34b02dc2008-08-24 21:49:55 -04003209 struct lpfc_pgp *pgp;
dea31012005-04-17 16:05:31 -05003210 IOCB_t *entry;
3211 IOCB_t *irsp = NULL;
3212 struct lpfc_iocbq *rspiocbp = NULL;
dea31012005-04-17 16:05:31 -05003213 uint32_t portRspPut, portRspMax;
dea31012005-04-17 16:05:31 -05003214 unsigned long iflag;
James Smart3772a992009-05-22 14:50:54 -04003215 uint32_t status;
dea31012005-04-17 16:05:31 -05003216
James Smart34b02dc2008-08-24 21:49:55 -04003217 pgp = &phba->port_gp[pring->ringno];
James Smart2e0fef82007-06-17 19:56:36 -05003218 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003219 pring->stats.iocb_event++;
3220
dea31012005-04-17 16:05:31 -05003221 /*
3222 * The next available response entry should never exceed the maximum
3223 * entries. If it does, treat it as an adapter hardware error.
3224 */
3225 portRspMax = pring->numRiocb;
3226 portRspPut = le32_to_cpu(pgp->rspPutInx);
3227 if (portRspPut >= portRspMax) {
3228 /*
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02003229 * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
dea31012005-04-17 16:05:31 -05003230 * rsp ring <portRspMax>
3231 */
James Smarted957682007-06-17 19:56:37 -05003232 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003233 "0303 Ring %d handler: portRspPut %d "
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02003234 "is bigger than rsp ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04003235 pring->ringno, portRspPut, portRspMax);
dea31012005-04-17 16:05:31 -05003236
James Smart2e0fef82007-06-17 19:56:36 -05003237 phba->link_state = LPFC_HBA_ERROR;
3238 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003239
3240 phba->work_hs = HS_FFER3;
3241 lpfc_handle_eratt(phba);
3242
James Smart3772a992009-05-22 14:50:54 -04003243 return;
dea31012005-04-17 16:05:31 -05003244 }
3245
3246 rmb();
dea31012005-04-17 16:05:31 -05003247 while (pring->rspidx != portRspPut) {
3248 /*
3249 * Build a completion list and call the appropriate handler.
3250 * The process is to get the next available response iocb, get
3251 * a free iocb from the list, copy the response data into the
3252 * free iocb, insert to the continuation list, and update the
3253 * next response index to slim. This process makes response
3254 * iocb's in the ring available to DMA as fast as possible but
3255 * pays a penalty for a copy operation. Since the iocb is
3256 * only 32 bytes, this penalty is considered small relative to
3257 * the PCI reads for register values and a slim write. When
3258 * the ulpLe field is set, the entire Command has been
3259 * received.
3260 */
James Smarted957682007-06-17 19:56:37 -05003261 entry = lpfc_resp_iocb(phba, pring);
3262
James Smart858c9f62007-06-17 19:56:39 -05003263 phba->last_completion_time = jiffies;
James Smart2e0fef82007-06-17 19:56:36 -05003264 rspiocbp = __lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05003265 if (rspiocbp == NULL) {
3266 printk(KERN_ERR "%s: out of buffers! Failing "
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07003267 "completion.\n", __func__);
dea31012005-04-17 16:05:31 -05003268 break;
3269 }
3270
James Smarted957682007-06-17 19:56:37 -05003271 lpfc_sli_pcimem_bcopy(entry, &rspiocbp->iocb,
3272 phba->iocb_rsp_size);
dea31012005-04-17 16:05:31 -05003273 irsp = &rspiocbp->iocb;
3274
3275 if (++pring->rspidx >= portRspMax)
3276 pring->rspidx = 0;
3277
James Smarta58cbd52007-08-02 11:09:43 -04003278 if (pring->ringno == LPFC_ELS_RING) {
3279 lpfc_debugfs_slow_ring_trc(phba,
3280 "IOCB rsp ring: wd4:x%08x wd6:x%08x wd7:x%08x",
3281 *(((uint32_t *) irsp) + 4),
3282 *(((uint32_t *) irsp) + 6),
3283 *(((uint32_t *) irsp) + 7));
3284 }
3285
James Smarted957682007-06-17 19:56:37 -05003286 writel(pring->rspidx, &phba->host_gp[pring->ringno].rspGetInx);
dea31012005-04-17 16:05:31 -05003287
James Smart3772a992009-05-22 14:50:54 -04003288 spin_unlock_irqrestore(&phba->hbalock, iflag);
3289 /* Handle the response IOCB */
3290 rspiocbp = lpfc_sli_sp_handle_rspiocb(phba, pring, rspiocbp);
3291 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003292
3293 /*
3294 * If the port response put pointer has not been updated, sync
3295 * the pgp->rspPutInx in the MAILBOX_tand fetch the new port
3296 * response put pointer.
3297 */
3298 if (pring->rspidx == portRspPut) {
3299 portRspPut = le32_to_cpu(pgp->rspPutInx);
3300 }
3301 } /* while (pring->rspidx != portRspPut) */
3302
James Smart92d7f7b2007-06-17 19:56:38 -05003303 if ((rspiocbp != NULL) && (mask & HA_R0RE_REQ)) {
dea31012005-04-17 16:05:31 -05003304 /* At least one response entry has been freed */
3305 pring->stats.iocb_rsp_full++;
3306 /* SET RxRE_RSP in Chip Att register */
3307 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
3308 writel(status, phba->CAregaddr);
3309 readl(phba->CAregaddr); /* flush */
3310 }
3311 if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
3312 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
3313 pring->stats.iocb_cmd_empty++;
3314
3315 /* Force update of the local copy of cmdGetInx */
3316 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
3317 lpfc_sli_resume_iocb(phba, pring);
3318
3319 if ((pring->lpfc_sli_cmd_available))
3320 (pring->lpfc_sli_cmd_available) (phba, pring);
3321
3322 }
3323
James Smart2e0fef82007-06-17 19:56:36 -05003324 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart3772a992009-05-22 14:50:54 -04003325 return;
dea31012005-04-17 16:05:31 -05003326}
3327
James Smarte59058c2008-08-24 21:49:00 -04003328/**
James Smart4f774512009-05-22 14:52:35 -04003329 * lpfc_sli_handle_slow_ring_event_s4 - Handle SLI4 slow-path els events
3330 * @phba: Pointer to HBA context object.
3331 * @pring: Pointer to driver SLI ring object.
3332 * @mask: Host attention register mask for this ring.
3333 *
3334 * This function is called from the worker thread when there is a pending
3335 * ELS response iocb on the driver internal slow-path response iocb worker
3336 * queue. The caller does not hold any lock. The function will remove each
3337 * response iocb from the response worker queue and calls the handle
3338 * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
3339 **/
3340static void
3341lpfc_sli_handle_slow_ring_event_s4(struct lpfc_hba *phba,
3342 struct lpfc_sli_ring *pring, uint32_t mask)
3343{
3344 struct lpfc_iocbq *irspiocbq;
James Smart4d9ab992009-10-02 15:16:39 -04003345 struct hbq_dmabuf *dmabuf;
3346 struct lpfc_cq_event *cq_event;
James Smart4f774512009-05-22 14:52:35 -04003347 unsigned long iflag;
3348
James Smart45ed1192009-10-02 15:17:02 -04003349 spin_lock_irqsave(&phba->hbalock, iflag);
3350 phba->hba_flag &= ~HBA_SP_QUEUE_EVT;
3351 spin_unlock_irqrestore(&phba->hbalock, iflag);
3352 while (!list_empty(&phba->sli4_hba.sp_queue_event)) {
James Smart4f774512009-05-22 14:52:35 -04003353 /* Get the response iocb from the head of work queue */
3354 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart45ed1192009-10-02 15:17:02 -04003355 list_remove_head(&phba->sli4_hba.sp_queue_event,
James Smart4d9ab992009-10-02 15:16:39 -04003356 cq_event, struct lpfc_cq_event, list);
James Smart4f774512009-05-22 14:52:35 -04003357 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart4d9ab992009-10-02 15:16:39 -04003358
3359 switch (bf_get(lpfc_wcqe_c_code, &cq_event->cqe.wcqe_cmpl)) {
3360 case CQE_CODE_COMPL_WQE:
3361 irspiocbq = container_of(cq_event, struct lpfc_iocbq,
3362 cq_event);
James Smart45ed1192009-10-02 15:17:02 -04003363 /* Translate ELS WCQE to response IOCBQ */
3364 irspiocbq = lpfc_sli4_els_wcqe_to_rspiocbq(phba,
3365 irspiocbq);
3366 if (irspiocbq)
3367 lpfc_sli_sp_handle_rspiocb(phba, pring,
3368 irspiocbq);
James Smart4d9ab992009-10-02 15:16:39 -04003369 break;
3370 case CQE_CODE_RECEIVE:
3371 dmabuf = container_of(cq_event, struct hbq_dmabuf,
3372 cq_event);
3373 lpfc_sli4_handle_received_buffer(phba, dmabuf);
3374 break;
3375 default:
3376 break;
3377 }
James Smart4f774512009-05-22 14:52:35 -04003378 }
3379}
3380
3381/**
James Smart3621a712009-04-06 18:47:14 -04003382 * lpfc_sli_abort_iocb_ring - Abort all iocbs in the ring
James Smarte59058c2008-08-24 21:49:00 -04003383 * @phba: Pointer to HBA context object.
3384 * @pring: Pointer to driver SLI ring object.
3385 *
3386 * This function aborts all iocbs in the given ring and frees all the iocb
3387 * objects in txq. This function issues an abort iocb for all the iocb commands
3388 * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
3389 * the return of this function. The caller is not required to hold any locks.
3390 **/
James Smart2e0fef82007-06-17 19:56:36 -05003391void
dea31012005-04-17 16:05:31 -05003392lpfc_sli_abort_iocb_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
3393{
James Smart2534ba72007-04-25 09:52:20 -04003394 LIST_HEAD(completions);
dea31012005-04-17 16:05:31 -05003395 struct lpfc_iocbq *iocb, *next_iocb;
dea31012005-04-17 16:05:31 -05003396
James Smart92d7f7b2007-06-17 19:56:38 -05003397 if (pring->ringno == LPFC_ELS_RING) {
3398 lpfc_fabric_abort_hba(phba);
3399 }
3400
dea31012005-04-17 16:05:31 -05003401 /* Error everything on txq and txcmplq
3402 * First do the txq.
3403 */
James Smart2e0fef82007-06-17 19:56:36 -05003404 spin_lock_irq(&phba->hbalock);
James Smart2534ba72007-04-25 09:52:20 -04003405 list_splice_init(&pring->txq, &completions);
dea31012005-04-17 16:05:31 -05003406 pring->txq_cnt = 0;
dea31012005-04-17 16:05:31 -05003407
3408 /* Next issue ABTS for everything on the txcmplq */
James Smart2534ba72007-04-25 09:52:20 -04003409 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
3410 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
3411
James Smart2e0fef82007-06-17 19:56:36 -05003412 spin_unlock_irq(&phba->hbalock);
James Smart2534ba72007-04-25 09:52:20 -04003413
James Smarta257bf92009-04-06 18:48:10 -04003414 /* Cancel all the IOCBs from the completions list */
3415 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
3416 IOERR_SLI_ABORTED);
dea31012005-04-17 16:05:31 -05003417}
3418
James Smarte59058c2008-08-24 21:49:00 -04003419/**
James Smart3621a712009-04-06 18:47:14 -04003420 * lpfc_sli_flush_fcp_rings - flush all iocbs in the fcp ring
James Smarta8e497d2008-08-24 21:50:11 -04003421 * @phba: Pointer to HBA context object.
3422 *
3423 * This function flushes all iocbs in the fcp ring and frees all the iocb
3424 * objects in txq and txcmplq. This function will not issue abort iocbs
3425 * for all the iocb commands in txcmplq, they will just be returned with
3426 * IOERR_SLI_DOWN. This function is invoked with EEH when device's PCI
3427 * slot has been permanently disabled.
3428 **/
3429void
3430lpfc_sli_flush_fcp_rings(struct lpfc_hba *phba)
3431{
3432 LIST_HEAD(txq);
3433 LIST_HEAD(txcmplq);
James Smarta8e497d2008-08-24 21:50:11 -04003434 struct lpfc_sli *psli = &phba->sli;
3435 struct lpfc_sli_ring *pring;
3436
3437 /* Currently, only one fcp ring */
3438 pring = &psli->ring[psli->fcp_ring];
3439
3440 spin_lock_irq(&phba->hbalock);
3441 /* Retrieve everything on txq */
3442 list_splice_init(&pring->txq, &txq);
3443 pring->txq_cnt = 0;
3444
3445 /* Retrieve everything on the txcmplq */
3446 list_splice_init(&pring->txcmplq, &txcmplq);
3447 pring->txcmplq_cnt = 0;
3448 spin_unlock_irq(&phba->hbalock);
3449
3450 /* Flush the txq */
James Smarta257bf92009-04-06 18:48:10 -04003451 lpfc_sli_cancel_iocbs(phba, &txq, IOSTAT_LOCAL_REJECT,
3452 IOERR_SLI_DOWN);
James Smarta8e497d2008-08-24 21:50:11 -04003453
3454 /* Flush the txcmpq */
James Smarta257bf92009-04-06 18:48:10 -04003455 lpfc_sli_cancel_iocbs(phba, &txcmplq, IOSTAT_LOCAL_REJECT,
3456 IOERR_SLI_DOWN);
James Smarta8e497d2008-08-24 21:50:11 -04003457}
3458
3459/**
James Smart3772a992009-05-22 14:50:54 -04003460 * lpfc_sli_brdready_s3 - Check for sli3 host ready status
James Smarte59058c2008-08-24 21:49:00 -04003461 * @phba: Pointer to HBA context object.
3462 * @mask: Bit mask to be checked.
3463 *
3464 * This function reads the host status register and compares
3465 * with the provided bit mask to check if HBA completed
3466 * the restart. This function will wait in a loop for the
3467 * HBA to complete restart. If the HBA does not restart within
3468 * 15 iterations, the function will reset the HBA again. The
3469 * function returns 1 when HBA fail to restart otherwise returns
3470 * zero.
3471 **/
James Smart3772a992009-05-22 14:50:54 -04003472static int
3473lpfc_sli_brdready_s3(struct lpfc_hba *phba, uint32_t mask)
dea31012005-04-17 16:05:31 -05003474{
Jamie Wellnitz41415862006-02-28 19:25:27 -05003475 uint32_t status;
3476 int i = 0;
3477 int retval = 0;
dea31012005-04-17 16:05:31 -05003478
Jamie Wellnitz41415862006-02-28 19:25:27 -05003479 /* Read the HBA Host Status Register */
James Smart9940b972011-03-11 16:06:12 -05003480 if (lpfc_readl(phba->HSregaddr, &status))
3481 return 1;
dea31012005-04-17 16:05:31 -05003482
Jamie Wellnitz41415862006-02-28 19:25:27 -05003483 /*
3484 * Check status register every 100ms for 5 retries, then every
3485 * 500ms for 5, then every 2.5 sec for 5, then reset board and
3486 * every 2.5 sec for 4.
3487 * Break our of the loop if errors occurred during init.
3488 */
3489 while (((status & mask) != mask) &&
3490 !(status & HS_FFERM) &&
3491 i++ < 20) {
dea31012005-04-17 16:05:31 -05003492
Jamie Wellnitz41415862006-02-28 19:25:27 -05003493 if (i <= 5)
3494 msleep(10);
3495 else if (i <= 10)
3496 msleep(500);
3497 else
3498 msleep(2500);
dea31012005-04-17 16:05:31 -05003499
Jamie Wellnitz41415862006-02-28 19:25:27 -05003500 if (i == 15) {
James Smart2e0fef82007-06-17 19:56:36 -05003501 /* Do post */
James Smart92d7f7b2007-06-17 19:56:38 -05003502 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003503 lpfc_sli_brdrestart(phba);
3504 }
3505 /* Read the HBA Host Status Register */
James Smart9940b972011-03-11 16:06:12 -05003506 if (lpfc_readl(phba->HSregaddr, &status)) {
3507 retval = 1;
3508 break;
3509 }
dea31012005-04-17 16:05:31 -05003510 }
dea31012005-04-17 16:05:31 -05003511
Jamie Wellnitz41415862006-02-28 19:25:27 -05003512 /* Check to see if any errors occurred during init */
3513 if ((status & HS_FFERM) || (i >= 20)) {
James Smarte40a02c2010-02-26 14:13:54 -05003514 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3515 "2751 Adapter failed to restart, "
3516 "status reg x%x, FW Data: A8 x%x AC x%x\n",
3517 status,
3518 readl(phba->MBslimaddr + 0xa8),
3519 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05003520 phba->link_state = LPFC_HBA_ERROR;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003521 retval = 1;
3522 }
dea31012005-04-17 16:05:31 -05003523
Jamie Wellnitz41415862006-02-28 19:25:27 -05003524 return retval;
dea31012005-04-17 16:05:31 -05003525}
3526
James Smartda0436e2009-05-22 14:51:39 -04003527/**
3528 * lpfc_sli_brdready_s4 - Check for sli4 host ready status
3529 * @phba: Pointer to HBA context object.
3530 * @mask: Bit mask to be checked.
3531 *
3532 * This function checks the host status register to check if HBA is
3533 * ready. This function will wait in a loop for the HBA to be ready
3534 * If the HBA is not ready , the function will will reset the HBA PCI
3535 * function again. The function returns 1 when HBA fail to be ready
3536 * otherwise returns zero.
3537 **/
3538static int
3539lpfc_sli_brdready_s4(struct lpfc_hba *phba, uint32_t mask)
3540{
3541 uint32_t status;
3542 int retval = 0;
3543
3544 /* Read the HBA Host Status Register */
3545 status = lpfc_sli4_post_status_check(phba);
3546
3547 if (status) {
3548 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
3549 lpfc_sli_brdrestart(phba);
3550 status = lpfc_sli4_post_status_check(phba);
3551 }
3552
3553 /* Check to see if any errors occurred during init */
3554 if (status) {
3555 phba->link_state = LPFC_HBA_ERROR;
3556 retval = 1;
3557 } else
3558 phba->sli4_hba.intr_enable = 0;
3559
3560 return retval;
3561}
3562
3563/**
3564 * lpfc_sli_brdready - Wrapper func for checking the hba readyness
3565 * @phba: Pointer to HBA context object.
3566 * @mask: Bit mask to be checked.
3567 *
3568 * This routine wraps the actual SLI3 or SLI4 hba readyness check routine
3569 * from the API jump table function pointer from the lpfc_hba struct.
3570 **/
3571int
3572lpfc_sli_brdready(struct lpfc_hba *phba, uint32_t mask)
3573{
3574 return phba->lpfc_sli_brdready(phba, mask);
3575}
3576
James Smart92908312006-03-07 15:04:13 -05003577#define BARRIER_TEST_PATTERN (0xdeadbeef)
3578
James Smarte59058c2008-08-24 21:49:00 -04003579/**
James Smart3621a712009-04-06 18:47:14 -04003580 * lpfc_reset_barrier - Make HBA ready for HBA reset
James Smarte59058c2008-08-24 21:49:00 -04003581 * @phba: Pointer to HBA context object.
3582 *
3583 * This function is called before resetting an HBA. This
3584 * function requests HBA to quiesce DMAs before a reset.
3585 **/
James Smart2e0fef82007-06-17 19:56:36 -05003586void lpfc_reset_barrier(struct lpfc_hba *phba)
James Smart92908312006-03-07 15:04:13 -05003587{
James Smart65a29c12006-07-06 15:50:50 -04003588 uint32_t __iomem *resp_buf;
3589 uint32_t __iomem *mbox_buf;
James Smart92908312006-03-07 15:04:13 -05003590 volatile uint32_t mbox;
James Smart9940b972011-03-11 16:06:12 -05003591 uint32_t hc_copy, ha_copy, resp_data;
James Smart92908312006-03-07 15:04:13 -05003592 int i;
3593 uint8_t hdrtype;
3594
3595 pci_read_config_byte(phba->pcidev, PCI_HEADER_TYPE, &hdrtype);
3596 if (hdrtype != 0x80 ||
3597 (FC_JEDEC_ID(phba->vpd.rev.biuRev) != HELIOS_JEDEC_ID &&
3598 FC_JEDEC_ID(phba->vpd.rev.biuRev) != THOR_JEDEC_ID))
3599 return;
3600
3601 /*
3602 * Tell the other part of the chip to suspend temporarily all
3603 * its DMA activity.
3604 */
James Smart65a29c12006-07-06 15:50:50 -04003605 resp_buf = phba->MBslimaddr;
James Smart92908312006-03-07 15:04:13 -05003606
3607 /* Disable the error attention */
James Smart9940b972011-03-11 16:06:12 -05003608 if (lpfc_readl(phba->HCregaddr, &hc_copy))
3609 return;
James Smart92908312006-03-07 15:04:13 -05003610 writel((hc_copy & ~HC_ERINT_ENA), phba->HCregaddr);
3611 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05003612 phba->link_flag |= LS_IGNORE_ERATT;
James Smart92908312006-03-07 15:04:13 -05003613
James Smart9940b972011-03-11 16:06:12 -05003614 if (lpfc_readl(phba->HAregaddr, &ha_copy))
3615 return;
3616 if (ha_copy & HA_ERATT) {
James Smart92908312006-03-07 15:04:13 -05003617 /* Clear Chip error bit */
3618 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05003619 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05003620 }
3621
3622 mbox = 0;
3623 ((MAILBOX_t *)&mbox)->mbxCommand = MBX_KILL_BOARD;
3624 ((MAILBOX_t *)&mbox)->mbxOwner = OWN_CHIP;
3625
3626 writel(BARRIER_TEST_PATTERN, (resp_buf + 1));
James Smart65a29c12006-07-06 15:50:50 -04003627 mbox_buf = phba->MBslimaddr;
James Smart92908312006-03-07 15:04:13 -05003628 writel(mbox, mbox_buf);
3629
James Smart9940b972011-03-11 16:06:12 -05003630 for (i = 0; i < 50; i++) {
3631 if (lpfc_readl((resp_buf + 1), &resp_data))
3632 return;
3633 if (resp_data != ~(BARRIER_TEST_PATTERN))
3634 mdelay(1);
3635 else
3636 break;
3637 }
3638 resp_data = 0;
3639 if (lpfc_readl((resp_buf + 1), &resp_data))
3640 return;
3641 if (resp_data != ~(BARRIER_TEST_PATTERN)) {
James Smartf4b4c682009-05-22 14:53:12 -04003642 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE ||
James Smart2e0fef82007-06-17 19:56:36 -05003643 phba->pport->stopped)
James Smart92908312006-03-07 15:04:13 -05003644 goto restore_hc;
3645 else
3646 goto clear_errat;
3647 }
3648
3649 ((MAILBOX_t *)&mbox)->mbxOwner = OWN_HOST;
James Smart9940b972011-03-11 16:06:12 -05003650 resp_data = 0;
3651 for (i = 0; i < 500; i++) {
3652 if (lpfc_readl(resp_buf, &resp_data))
3653 return;
3654 if (resp_data != mbox)
3655 mdelay(1);
3656 else
3657 break;
3658 }
James Smart92908312006-03-07 15:04:13 -05003659
3660clear_errat:
3661
James Smart9940b972011-03-11 16:06:12 -05003662 while (++i < 500) {
3663 if (lpfc_readl(phba->HAregaddr, &ha_copy))
3664 return;
3665 if (!(ha_copy & HA_ERATT))
3666 mdelay(1);
3667 else
3668 break;
3669 }
James Smart92908312006-03-07 15:04:13 -05003670
3671 if (readl(phba->HAregaddr) & HA_ERATT) {
3672 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05003673 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05003674 }
3675
3676restore_hc:
James Smart2e0fef82007-06-17 19:56:36 -05003677 phba->link_flag &= ~LS_IGNORE_ERATT;
James Smart92908312006-03-07 15:04:13 -05003678 writel(hc_copy, phba->HCregaddr);
3679 readl(phba->HCregaddr); /* flush */
3680}
3681
James Smarte59058c2008-08-24 21:49:00 -04003682/**
James Smart3621a712009-04-06 18:47:14 -04003683 * lpfc_sli_brdkill - Issue a kill_board mailbox command
James Smarte59058c2008-08-24 21:49:00 -04003684 * @phba: Pointer to HBA context object.
3685 *
3686 * This function issues a kill_board mailbox command and waits for
3687 * the error attention interrupt. This function is called for stopping
3688 * the firmware processing. The caller is not required to hold any
3689 * locks. This function calls lpfc_hba_down_post function to free
3690 * any pending commands after the kill. The function will return 1 when it
3691 * fails to kill the board else will return 0.
3692 **/
Jamie Wellnitz41415862006-02-28 19:25:27 -05003693int
James Smart2e0fef82007-06-17 19:56:36 -05003694lpfc_sli_brdkill(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05003695{
Jamie Wellnitz41415862006-02-28 19:25:27 -05003696 struct lpfc_sli *psli;
3697 LPFC_MBOXQ_t *pmb;
3698 uint32_t status;
3699 uint32_t ha_copy;
3700 int retval;
3701 int i = 0;
3702
3703 psli = &phba->sli;
3704
3705 /* Kill HBA */
James Smarted957682007-06-17 19:56:37 -05003706 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003707 "0329 Kill HBA Data: x%x x%x\n",
3708 phba->pport->port_state, psli->sli_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003709
James Smart98c9ea52007-10-27 13:37:33 -04003710 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3711 if (!pmb)
Jamie Wellnitz41415862006-02-28 19:25:27 -05003712 return 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003713
3714 /* Disable the error attention */
James Smart2e0fef82007-06-17 19:56:36 -05003715 spin_lock_irq(&phba->hbalock);
James Smart9940b972011-03-11 16:06:12 -05003716 if (lpfc_readl(phba->HCregaddr, &status)) {
3717 spin_unlock_irq(&phba->hbalock);
3718 mempool_free(pmb, phba->mbox_mem_pool);
3719 return 1;
3720 }
Jamie Wellnitz41415862006-02-28 19:25:27 -05003721 status &= ~HC_ERINT_ENA;
3722 writel(status, phba->HCregaddr);
3723 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05003724 phba->link_flag |= LS_IGNORE_ERATT;
3725 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003726
3727 lpfc_kill_board(phba, pmb);
3728 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
3729 retval = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
3730
3731 if (retval != MBX_SUCCESS) {
3732 if (retval != MBX_BUSY)
3733 mempool_free(pmb, phba->mbox_mem_pool);
James Smarte40a02c2010-02-26 14:13:54 -05003734 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3735 "2752 KILL_BOARD command failed retval %d\n",
3736 retval);
James Smart2e0fef82007-06-17 19:56:36 -05003737 spin_lock_irq(&phba->hbalock);
3738 phba->link_flag &= ~LS_IGNORE_ERATT;
3739 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003740 return 1;
3741 }
3742
James Smartf4b4c682009-05-22 14:53:12 -04003743 spin_lock_irq(&phba->hbalock);
3744 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
3745 spin_unlock_irq(&phba->hbalock);
James Smart92908312006-03-07 15:04:13 -05003746
Jamie Wellnitz41415862006-02-28 19:25:27 -05003747 mempool_free(pmb, phba->mbox_mem_pool);
3748
3749 /* There is no completion for a KILL_BOARD mbox cmd. Check for an error
3750 * attention every 100ms for 3 seconds. If we don't get ERATT after
3751 * 3 seconds we still set HBA_ERROR state because the status of the
3752 * board is now undefined.
3753 */
James Smart9940b972011-03-11 16:06:12 -05003754 if (lpfc_readl(phba->HAregaddr, &ha_copy))
3755 return 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003756 while ((i++ < 30) && !(ha_copy & HA_ERATT)) {
3757 mdelay(100);
James Smart9940b972011-03-11 16:06:12 -05003758 if (lpfc_readl(phba->HAregaddr, &ha_copy))
3759 return 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003760 }
3761
3762 del_timer_sync(&psli->mbox_tmo);
James Smart92908312006-03-07 15:04:13 -05003763 if (ha_copy & HA_ERATT) {
3764 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05003765 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05003766 }
James Smart2e0fef82007-06-17 19:56:36 -05003767 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003768 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart04c68492009-05-22 14:52:52 -04003769 psli->mbox_active = NULL;
James Smart2e0fef82007-06-17 19:56:36 -05003770 phba->link_flag &= ~LS_IGNORE_ERATT;
3771 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003772
Jamie Wellnitz41415862006-02-28 19:25:27 -05003773 lpfc_hba_down_post(phba);
James Smart2e0fef82007-06-17 19:56:36 -05003774 phba->link_state = LPFC_HBA_ERROR;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003775
James Smart2e0fef82007-06-17 19:56:36 -05003776 return ha_copy & HA_ERATT ? 0 : 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003777}
3778
James Smarte59058c2008-08-24 21:49:00 -04003779/**
James Smart3772a992009-05-22 14:50:54 -04003780 * lpfc_sli_brdreset - Reset a sli-2 or sli-3 HBA
James Smarte59058c2008-08-24 21:49:00 -04003781 * @phba: Pointer to HBA context object.
3782 *
3783 * This function resets the HBA by writing HC_INITFF to the control
3784 * register. After the HBA resets, this function resets all the iocb ring
3785 * indices. This function disables PCI layer parity checking during
3786 * the reset.
3787 * This function returns 0 always.
3788 * The caller is not required to hold any locks.
3789 **/
Jamie Wellnitz41415862006-02-28 19:25:27 -05003790int
James Smart2e0fef82007-06-17 19:56:36 -05003791lpfc_sli_brdreset(struct lpfc_hba *phba)
Jamie Wellnitz41415862006-02-28 19:25:27 -05003792{
3793 struct lpfc_sli *psli;
dea31012005-04-17 16:05:31 -05003794 struct lpfc_sli_ring *pring;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003795 uint16_t cfg_value;
dea31012005-04-17 16:05:31 -05003796 int i;
dea31012005-04-17 16:05:31 -05003797
Jamie Wellnitz41415862006-02-28 19:25:27 -05003798 psli = &phba->sli;
dea31012005-04-17 16:05:31 -05003799
Jamie Wellnitz41415862006-02-28 19:25:27 -05003800 /* Reset HBA */
3801 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003802 "0325 Reset HBA Data: x%x x%x\n",
James Smart2e0fef82007-06-17 19:56:36 -05003803 phba->pport->port_state, psli->sli_flag);
dea31012005-04-17 16:05:31 -05003804
3805 /* perform board reset */
3806 phba->fc_eventTag = 0;
James Smart4d9ab992009-10-02 15:16:39 -04003807 phba->link_events = 0;
James Smart2e0fef82007-06-17 19:56:36 -05003808 phba->pport->fc_myDID = 0;
3809 phba->pport->fc_prevDID = 0;
dea31012005-04-17 16:05:31 -05003810
Jamie Wellnitz41415862006-02-28 19:25:27 -05003811 /* Turn off parity checking and serr during the physical reset */
3812 pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
3813 pci_write_config_word(phba->pcidev, PCI_COMMAND,
3814 (cfg_value &
3815 ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
3816
James Smart3772a992009-05-22 14:50:54 -04003817 psli->sli_flag &= ~(LPFC_SLI_ACTIVE | LPFC_PROCESS_LA);
3818
Jamie Wellnitz41415862006-02-28 19:25:27 -05003819 /* Now toggle INITFF bit in the Host Control Register */
3820 writel(HC_INITFF, phba->HCregaddr);
3821 mdelay(1);
3822 readl(phba->HCregaddr); /* flush */
3823 writel(0, phba->HCregaddr);
3824 readl(phba->HCregaddr); /* flush */
3825
3826 /* Restore PCI cmd register */
3827 pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
dea31012005-04-17 16:05:31 -05003828
3829 /* Initialize relevant SLI info */
Jamie Wellnitz41415862006-02-28 19:25:27 -05003830 for (i = 0; i < psli->num_rings; i++) {
3831 pring = &psli->ring[i];
dea31012005-04-17 16:05:31 -05003832 pring->flag = 0;
3833 pring->rspidx = 0;
3834 pring->next_cmdidx = 0;
3835 pring->local_getidx = 0;
3836 pring->cmdidx = 0;
3837 pring->missbufcnt = 0;
3838 }
dea31012005-04-17 16:05:31 -05003839
James Smart2e0fef82007-06-17 19:56:36 -05003840 phba->link_state = LPFC_WARM_START;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003841 return 0;
3842}
3843
James Smarte59058c2008-08-24 21:49:00 -04003844/**
James Smartda0436e2009-05-22 14:51:39 -04003845 * lpfc_sli4_brdreset - Reset a sli-4 HBA
3846 * @phba: Pointer to HBA context object.
3847 *
3848 * This function resets a SLI4 HBA. This function disables PCI layer parity
3849 * checking during resets the device. The caller is not required to hold
3850 * any locks.
3851 *
3852 * This function returns 0 always.
3853 **/
3854int
3855lpfc_sli4_brdreset(struct lpfc_hba *phba)
3856{
3857 struct lpfc_sli *psli = &phba->sli;
3858 uint16_t cfg_value;
3859 uint8_t qindx;
3860
3861 /* Reset HBA */
3862 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3863 "0295 Reset HBA Data: x%x x%x\n",
3864 phba->pport->port_state, psli->sli_flag);
3865
3866 /* perform board reset */
3867 phba->fc_eventTag = 0;
James Smart4d9ab992009-10-02 15:16:39 -04003868 phba->link_events = 0;
James Smartda0436e2009-05-22 14:51:39 -04003869 phba->pport->fc_myDID = 0;
3870 phba->pport->fc_prevDID = 0;
3871
James Smartda0436e2009-05-22 14:51:39 -04003872 spin_lock_irq(&phba->hbalock);
3873 psli->sli_flag &= ~(LPFC_PROCESS_LA);
3874 phba->fcf.fcf_flag = 0;
3875 /* Clean up the child queue list for the CQs */
3876 list_del_init(&phba->sli4_hba.mbx_wq->list);
3877 list_del_init(&phba->sli4_hba.els_wq->list);
3878 list_del_init(&phba->sli4_hba.hdr_rq->list);
3879 list_del_init(&phba->sli4_hba.dat_rq->list);
3880 list_del_init(&phba->sli4_hba.mbx_cq->list);
3881 list_del_init(&phba->sli4_hba.els_cq->list);
James Smartda0436e2009-05-22 14:51:39 -04003882 for (qindx = 0; qindx < phba->cfg_fcp_wq_count; qindx++)
3883 list_del_init(&phba->sli4_hba.fcp_wq[qindx]->list);
3884 for (qindx = 0; qindx < phba->cfg_fcp_eq_count; qindx++)
3885 list_del_init(&phba->sli4_hba.fcp_cq[qindx]->list);
3886 spin_unlock_irq(&phba->hbalock);
3887
3888 /* Now physically reset the device */
3889 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3890 "0389 Performing PCI function reset!\n");
James Smartbe858b62010-12-15 17:57:20 -05003891
3892 /* Turn off parity checking and serr during the physical reset */
3893 pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
3894 pci_write_config_word(phba->pcidev, PCI_COMMAND, (cfg_value &
3895 ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
3896
James Smartda0436e2009-05-22 14:51:39 -04003897 /* Perform FCoE PCI function reset */
3898 lpfc_pci_function_reset(phba);
3899
James Smartbe858b62010-12-15 17:57:20 -05003900 /* Restore PCI cmd register */
3901 pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
3902
James Smartda0436e2009-05-22 14:51:39 -04003903 return 0;
3904}
3905
3906/**
3907 * lpfc_sli_brdrestart_s3 - Restart a sli-3 hba
James Smarte59058c2008-08-24 21:49:00 -04003908 * @phba: Pointer to HBA context object.
3909 *
3910 * This function is called in the SLI initialization code path to
3911 * restart the HBA. The caller is not required to hold any lock.
3912 * This function writes MBX_RESTART mailbox command to the SLIM and
3913 * resets the HBA. At the end of the function, it calls lpfc_hba_down_post
3914 * function to free any pending commands. The function enables
3915 * POST only during the first initialization. The function returns zero.
3916 * The function does not guarantee completion of MBX_RESTART mailbox
3917 * command before the return of this function.
3918 **/
James Smartda0436e2009-05-22 14:51:39 -04003919static int
3920lpfc_sli_brdrestart_s3(struct lpfc_hba *phba)
Jamie Wellnitz41415862006-02-28 19:25:27 -05003921{
3922 MAILBOX_t *mb;
3923 struct lpfc_sli *psli;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003924 volatile uint32_t word0;
3925 void __iomem *to_slim;
James Smart0d878412009-10-02 15:16:56 -04003926 uint32_t hba_aer_enabled;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003927
James Smart2e0fef82007-06-17 19:56:36 -05003928 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003929
James Smart0d878412009-10-02 15:16:56 -04003930 /* Take PCIe device Advanced Error Reporting (AER) state */
3931 hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
3932
Jamie Wellnitz41415862006-02-28 19:25:27 -05003933 psli = &phba->sli;
3934
3935 /* Restart HBA */
3936 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003937 "0337 Restart HBA Data: x%x x%x\n",
James Smart2e0fef82007-06-17 19:56:36 -05003938 phba->pport->port_state, psli->sli_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003939
3940 word0 = 0;
3941 mb = (MAILBOX_t *) &word0;
3942 mb->mbxCommand = MBX_RESTART;
3943 mb->mbxHc = 1;
3944
James Smart92908312006-03-07 15:04:13 -05003945 lpfc_reset_barrier(phba);
3946
Jamie Wellnitz41415862006-02-28 19:25:27 -05003947 to_slim = phba->MBslimaddr;
3948 writel(*(uint32_t *) mb, to_slim);
3949 readl(to_slim); /* flush */
3950
3951 /* Only skip post after fc_ffinit is completed */
James Smarteaf15d52008-12-04 22:39:29 -05003952 if (phba->pport->port_state)
Jamie Wellnitz41415862006-02-28 19:25:27 -05003953 word0 = 1; /* This is really setting up word1 */
James Smarteaf15d52008-12-04 22:39:29 -05003954 else
Jamie Wellnitz41415862006-02-28 19:25:27 -05003955 word0 = 0; /* This is really setting up word1 */
James Smart65a29c12006-07-06 15:50:50 -04003956 to_slim = phba->MBslimaddr + sizeof (uint32_t);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003957 writel(*(uint32_t *) mb, to_slim);
3958 readl(to_slim); /* flush */
3959
3960 lpfc_sli_brdreset(phba);
James Smart2e0fef82007-06-17 19:56:36 -05003961 phba->pport->stopped = 0;
3962 phba->link_state = LPFC_INIT_START;
James Smartda0436e2009-05-22 14:51:39 -04003963 phba->hba_flag = 0;
James Smart2e0fef82007-06-17 19:56:36 -05003964 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003965
James Smart64ba8812006-08-02 15:24:34 -04003966 memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
3967 psli->stats_start = get_seconds();
3968
James Smarteaf15d52008-12-04 22:39:29 -05003969 /* Give the INITFF and Post time to settle. */
3970 mdelay(100);
dea31012005-04-17 16:05:31 -05003971
James Smart0d878412009-10-02 15:16:56 -04003972 /* Reset HBA AER if it was enabled, note hba_flag was reset above */
3973 if (hba_aer_enabled)
3974 pci_disable_pcie_error_reporting(phba->pcidev);
3975
Jamie Wellnitz41415862006-02-28 19:25:27 -05003976 lpfc_hba_down_post(phba);
dea31012005-04-17 16:05:31 -05003977
3978 return 0;
3979}
3980
James Smarte59058c2008-08-24 21:49:00 -04003981/**
James Smartda0436e2009-05-22 14:51:39 -04003982 * lpfc_sli_brdrestart_s4 - Restart the sli-4 hba
3983 * @phba: Pointer to HBA context object.
3984 *
3985 * This function is called in the SLI initialization code path to restart
3986 * a SLI4 HBA. The caller is not required to hold any lock.
3987 * At the end of the function, it calls lpfc_hba_down_post function to
3988 * free any pending commands.
3989 **/
3990static int
3991lpfc_sli_brdrestart_s4(struct lpfc_hba *phba)
3992{
3993 struct lpfc_sli *psli = &phba->sli;
James Smart75baf692010-06-08 18:31:21 -04003994 uint32_t hba_aer_enabled;
James Smartda0436e2009-05-22 14:51:39 -04003995
3996 /* Restart HBA */
3997 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3998 "0296 Restart HBA Data: x%x x%x\n",
3999 phba->pport->port_state, psli->sli_flag);
4000
James Smart75baf692010-06-08 18:31:21 -04004001 /* Take PCIe device Advanced Error Reporting (AER) state */
4002 hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
4003
James Smartda0436e2009-05-22 14:51:39 -04004004 lpfc_sli4_brdreset(phba);
4005
4006 spin_lock_irq(&phba->hbalock);
4007 phba->pport->stopped = 0;
4008 phba->link_state = LPFC_INIT_START;
4009 phba->hba_flag = 0;
4010 spin_unlock_irq(&phba->hbalock);
4011
4012 memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
4013 psli->stats_start = get_seconds();
4014
James Smart75baf692010-06-08 18:31:21 -04004015 /* Reset HBA AER if it was enabled, note hba_flag was reset above */
4016 if (hba_aer_enabled)
4017 pci_disable_pcie_error_reporting(phba->pcidev);
4018
James Smartda0436e2009-05-22 14:51:39 -04004019 lpfc_hba_down_post(phba);
4020
4021 return 0;
4022}
4023
4024/**
4025 * lpfc_sli_brdrestart - Wrapper func for restarting hba
4026 * @phba: Pointer to HBA context object.
4027 *
4028 * This routine wraps the actual SLI3 or SLI4 hba restart routine from the
4029 * API jump table function pointer from the lpfc_hba struct.
4030**/
4031int
4032lpfc_sli_brdrestart(struct lpfc_hba *phba)
4033{
4034 return phba->lpfc_sli_brdrestart(phba);
4035}
4036
4037/**
James Smart3621a712009-04-06 18:47:14 -04004038 * lpfc_sli_chipset_init - Wait for the restart of the HBA after a restart
James Smarte59058c2008-08-24 21:49:00 -04004039 * @phba: Pointer to HBA context object.
4040 *
4041 * This function is called after a HBA restart to wait for successful
4042 * restart of the HBA. Successful restart of the HBA is indicated by
4043 * HS_FFRDY and HS_MBRDY bits. If the HBA fails to restart even after 15
4044 * iteration, the function will restart the HBA again. The function returns
4045 * zero if HBA successfully restarted else returns negative error code.
4046 **/
dea31012005-04-17 16:05:31 -05004047static int
4048lpfc_sli_chipset_init(struct lpfc_hba *phba)
4049{
4050 uint32_t status, i = 0;
4051
4052 /* Read the HBA Host Status Register */
James Smart9940b972011-03-11 16:06:12 -05004053 if (lpfc_readl(phba->HSregaddr, &status))
4054 return -EIO;
dea31012005-04-17 16:05:31 -05004055
4056 /* Check status register to see what current state is */
4057 i = 0;
4058 while ((status & (HS_FFRDY | HS_MBRDY)) != (HS_FFRDY | HS_MBRDY)) {
4059
James Smartdcf2a4e2010-09-29 11:18:53 -04004060 /* Check every 10ms for 10 retries, then every 100ms for 90
4061 * retries, then every 1 sec for 50 retires for a total of
4062 * ~60 seconds before reset the board again and check every
4063 * 1 sec for 50 retries. The up to 60 seconds before the
4064 * board ready is required by the Falcon FIPS zeroization
4065 * complete, and any reset the board in between shall cause
4066 * restart of zeroization, further delay the board ready.
dea31012005-04-17 16:05:31 -05004067 */
James Smartdcf2a4e2010-09-29 11:18:53 -04004068 if (i++ >= 200) {
dea31012005-04-17 16:05:31 -05004069 /* Adapter failed to init, timeout, status reg
4070 <status> */
James Smarted957682007-06-17 19:56:37 -05004071 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004072 "0436 Adapter failed to init, "
James Smart09372822008-01-11 01:52:54 -05004073 "timeout, status reg x%x, "
4074 "FW Data: A8 x%x AC x%x\n", status,
4075 readl(phba->MBslimaddr + 0xa8),
4076 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05004077 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05004078 return -ETIMEDOUT;
4079 }
4080
4081 /* Check to see if any errors occurred during init */
4082 if (status & HS_FFERM) {
4083 /* ERROR: During chipset initialization */
4084 /* Adapter failed to init, chipset, status reg
4085 <status> */
James Smarted957682007-06-17 19:56:37 -05004086 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004087 "0437 Adapter failed to init, "
James Smart09372822008-01-11 01:52:54 -05004088 "chipset, status reg x%x, "
4089 "FW Data: A8 x%x AC x%x\n", status,
4090 readl(phba->MBslimaddr + 0xa8),
4091 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05004092 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05004093 return -EIO;
4094 }
4095
James Smartdcf2a4e2010-09-29 11:18:53 -04004096 if (i <= 10)
dea31012005-04-17 16:05:31 -05004097 msleep(10);
James Smartdcf2a4e2010-09-29 11:18:53 -04004098 else if (i <= 100)
4099 msleep(100);
4100 else
4101 msleep(1000);
dea31012005-04-17 16:05:31 -05004102
James Smartdcf2a4e2010-09-29 11:18:53 -04004103 if (i == 150) {
4104 /* Do post */
James Smart92d7f7b2007-06-17 19:56:38 -05004105 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004106 lpfc_sli_brdrestart(phba);
dea31012005-04-17 16:05:31 -05004107 }
4108 /* Read the HBA Host Status Register */
James Smart9940b972011-03-11 16:06:12 -05004109 if (lpfc_readl(phba->HSregaddr, &status))
4110 return -EIO;
dea31012005-04-17 16:05:31 -05004111 }
4112
4113 /* Check to see if any errors occurred during init */
4114 if (status & HS_FFERM) {
4115 /* ERROR: During chipset initialization */
4116 /* Adapter failed to init, chipset, status reg <status> */
James Smarted957682007-06-17 19:56:37 -05004117 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004118 "0438 Adapter failed to init, chipset, "
James Smart09372822008-01-11 01:52:54 -05004119 "status reg x%x, "
4120 "FW Data: A8 x%x AC x%x\n", status,
4121 readl(phba->MBslimaddr + 0xa8),
4122 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05004123 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05004124 return -EIO;
4125 }
4126
4127 /* Clear all interrupt enable conditions */
4128 writel(0, phba->HCregaddr);
4129 readl(phba->HCregaddr); /* flush */
4130
4131 /* setup host attn register */
4132 writel(0xffffffff, phba->HAregaddr);
4133 readl(phba->HAregaddr); /* flush */
4134 return 0;
4135}
4136
James Smarte59058c2008-08-24 21:49:00 -04004137/**
James Smart3621a712009-04-06 18:47:14 -04004138 * lpfc_sli_hbq_count - Get the number of HBQs to be configured
James Smarte59058c2008-08-24 21:49:00 -04004139 *
4140 * This function calculates and returns the number of HBQs required to be
4141 * configured.
4142 **/
James Smart78b2d852007-08-02 11:10:21 -04004143int
James Smarted957682007-06-17 19:56:37 -05004144lpfc_sli_hbq_count(void)
4145{
James Smart92d7f7b2007-06-17 19:56:38 -05004146 return ARRAY_SIZE(lpfc_hbq_defs);
James Smarted957682007-06-17 19:56:37 -05004147}
4148
James Smarte59058c2008-08-24 21:49:00 -04004149/**
James Smart3621a712009-04-06 18:47:14 -04004150 * lpfc_sli_hbq_entry_count - Calculate total number of hbq entries
James Smarte59058c2008-08-24 21:49:00 -04004151 *
4152 * This function adds the number of hbq entries in every HBQ to get
4153 * the total number of hbq entries required for the HBA and returns
4154 * the total count.
4155 **/
James Smarted957682007-06-17 19:56:37 -05004156static int
4157lpfc_sli_hbq_entry_count(void)
4158{
4159 int hbq_count = lpfc_sli_hbq_count();
4160 int count = 0;
4161 int i;
4162
4163 for (i = 0; i < hbq_count; ++i)
James Smart92d7f7b2007-06-17 19:56:38 -05004164 count += lpfc_hbq_defs[i]->entry_count;
James Smarted957682007-06-17 19:56:37 -05004165 return count;
4166}
4167
James Smarte59058c2008-08-24 21:49:00 -04004168/**
James Smart3621a712009-04-06 18:47:14 -04004169 * lpfc_sli_hbq_size - Calculate memory required for all hbq entries
James Smarte59058c2008-08-24 21:49:00 -04004170 *
4171 * This function calculates amount of memory required for all hbq entries
4172 * to be configured and returns the total memory required.
4173 **/
dea31012005-04-17 16:05:31 -05004174int
James Smarted957682007-06-17 19:56:37 -05004175lpfc_sli_hbq_size(void)
4176{
4177 return lpfc_sli_hbq_entry_count() * sizeof(struct lpfc_hbq_entry);
4178}
4179
James Smarte59058c2008-08-24 21:49:00 -04004180/**
James Smart3621a712009-04-06 18:47:14 -04004181 * lpfc_sli_hbq_setup - configure and initialize HBQs
James Smarte59058c2008-08-24 21:49:00 -04004182 * @phba: Pointer to HBA context object.
4183 *
4184 * This function is called during the SLI initialization to configure
4185 * all the HBQs and post buffers to the HBQ. The caller is not
4186 * required to hold any locks. This function will return zero if successful
4187 * else it will return negative error code.
4188 **/
James Smarted957682007-06-17 19:56:37 -05004189static int
4190lpfc_sli_hbq_setup(struct lpfc_hba *phba)
4191{
4192 int hbq_count = lpfc_sli_hbq_count();
4193 LPFC_MBOXQ_t *pmb;
4194 MAILBOX_t *pmbox;
4195 uint32_t hbqno;
4196 uint32_t hbq_entry_index;
James Smarted957682007-06-17 19:56:37 -05004197
James Smart92d7f7b2007-06-17 19:56:38 -05004198 /* Get a Mailbox buffer to setup mailbox
4199 * commands for HBA initialization
4200 */
James Smarted957682007-06-17 19:56:37 -05004201 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4202
4203 if (!pmb)
4204 return -ENOMEM;
4205
James Smart04c68492009-05-22 14:52:52 -04004206 pmbox = &pmb->u.mb;
James Smarted957682007-06-17 19:56:37 -05004207
4208 /* Initialize the struct lpfc_sli_hbq structure for each hbq */
4209 phba->link_state = LPFC_INIT_MBX_CMDS;
James Smart3163f722008-02-08 18:50:25 -05004210 phba->hbq_in_use = 1;
James Smarted957682007-06-17 19:56:37 -05004211
4212 hbq_entry_index = 0;
4213 for (hbqno = 0; hbqno < hbq_count; ++hbqno) {
4214 phba->hbqs[hbqno].next_hbqPutIdx = 0;
4215 phba->hbqs[hbqno].hbqPutIdx = 0;
4216 phba->hbqs[hbqno].local_hbqGetIdx = 0;
4217 phba->hbqs[hbqno].entry_count =
James Smart92d7f7b2007-06-17 19:56:38 -05004218 lpfc_hbq_defs[hbqno]->entry_count;
James Smart51ef4c22007-08-02 11:10:31 -04004219 lpfc_config_hbq(phba, hbqno, lpfc_hbq_defs[hbqno],
4220 hbq_entry_index, pmb);
James Smarted957682007-06-17 19:56:37 -05004221 hbq_entry_index += phba->hbqs[hbqno].entry_count;
4222
4223 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
4224 /* Adapter failed to init, mbxCmd <cmd> CFG_RING,
4225 mbxStatus <status>, ring <num> */
4226
4227 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -05004228 LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04004229 "1805 Adapter failed to init. "
James Smarted957682007-06-17 19:56:37 -05004230 "Data: x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04004231 pmbox->mbxCommand,
James Smarted957682007-06-17 19:56:37 -05004232 pmbox->mbxStatus, hbqno);
4233
4234 phba->link_state = LPFC_HBA_ERROR;
4235 mempool_free(pmb, phba->mbox_mem_pool);
James Smart6e7288d2010-06-07 15:23:35 -04004236 return -ENXIO;
James Smarted957682007-06-17 19:56:37 -05004237 }
4238 }
4239 phba->hbq_count = hbq_count;
4240
James Smarted957682007-06-17 19:56:37 -05004241 mempool_free(pmb, phba->mbox_mem_pool);
4242
James Smart92d7f7b2007-06-17 19:56:38 -05004243 /* Initially populate or replenish the HBQs */
James Smartd7c255b2008-08-24 21:50:00 -04004244 for (hbqno = 0; hbqno < hbq_count; ++hbqno)
4245 lpfc_sli_hbqbuf_init_hbqs(phba, hbqno);
James Smarted957682007-06-17 19:56:37 -05004246 return 0;
4247}
4248
James Smarte59058c2008-08-24 21:49:00 -04004249/**
James Smart4f774512009-05-22 14:52:35 -04004250 * lpfc_sli4_rb_setup - Initialize and post RBs to HBA
4251 * @phba: Pointer to HBA context object.
4252 *
4253 * This function is called during the SLI initialization to configure
4254 * all the HBQs and post buffers to the HBQ. The caller is not
4255 * required to hold any locks. This function will return zero if successful
4256 * else it will return negative error code.
4257 **/
4258static int
4259lpfc_sli4_rb_setup(struct lpfc_hba *phba)
4260{
4261 phba->hbq_in_use = 1;
4262 phba->hbqs[0].entry_count = lpfc_hbq_defs[0]->entry_count;
4263 phba->hbq_count = 1;
4264 /* Initially populate or replenish the HBQs */
4265 lpfc_sli_hbqbuf_init_hbqs(phba, 0);
4266 return 0;
4267}
4268
4269/**
James Smart3621a712009-04-06 18:47:14 -04004270 * lpfc_sli_config_port - Issue config port mailbox command
James Smarte59058c2008-08-24 21:49:00 -04004271 * @phba: Pointer to HBA context object.
4272 * @sli_mode: sli mode - 2/3
4273 *
4274 * This function is called by the sli intialization code path
4275 * to issue config_port mailbox command. This function restarts the
4276 * HBA firmware and issues a config_port mailbox command to configure
4277 * the SLI interface in the sli mode specified by sli_mode
4278 * variable. The caller is not required to hold any locks.
4279 * The function returns 0 if successful, else returns negative error
4280 * code.
4281 **/
James Smart93996272008-08-24 21:50:30 -04004282int
4283lpfc_sli_config_port(struct lpfc_hba *phba, int sli_mode)
dea31012005-04-17 16:05:31 -05004284{
4285 LPFC_MBOXQ_t *pmb;
4286 uint32_t resetcount = 0, rc = 0, done = 0;
4287
4288 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4289 if (!pmb) {
James Smart2e0fef82007-06-17 19:56:36 -05004290 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05004291 return -ENOMEM;
4292 }
4293
James Smarted957682007-06-17 19:56:37 -05004294 phba->sli_rev = sli_mode;
dea31012005-04-17 16:05:31 -05004295 while (resetcount < 2 && !done) {
James Smart2e0fef82007-06-17 19:56:36 -05004296 spin_lock_irq(&phba->hbalock);
James Smart1c067a42006-08-01 07:33:52 -04004297 phba->sli.sli_flag |= LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05004298 spin_unlock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -05004299 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004300 lpfc_sli_brdrestart(phba);
dea31012005-04-17 16:05:31 -05004301 rc = lpfc_sli_chipset_init(phba);
4302 if (rc)
4303 break;
4304
James Smart2e0fef82007-06-17 19:56:36 -05004305 spin_lock_irq(&phba->hbalock);
James Smart1c067a42006-08-01 07:33:52 -04004306 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05004307 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004308 resetcount++;
4309
James Smarted957682007-06-17 19:56:37 -05004310 /* Call pre CONFIG_PORT mailbox command initialization. A
4311 * value of 0 means the call was successful. Any other
4312 * nonzero value is a failure, but if ERESTART is returned,
4313 * the driver may reset the HBA and try again.
4314 */
dea31012005-04-17 16:05:31 -05004315 rc = lpfc_config_port_prep(phba);
4316 if (rc == -ERESTART) {
James Smarted957682007-06-17 19:56:37 -05004317 phba->link_state = LPFC_LINK_UNKNOWN;
dea31012005-04-17 16:05:31 -05004318 continue;
James Smart34b02dc2008-08-24 21:49:55 -04004319 } else if (rc)
dea31012005-04-17 16:05:31 -05004320 break;
James Smart2e0fef82007-06-17 19:56:36 -05004321 phba->link_state = LPFC_INIT_MBX_CMDS;
dea31012005-04-17 16:05:31 -05004322 lpfc_config_port(phba, pmb);
4323 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
James Smart34b02dc2008-08-24 21:49:55 -04004324 phba->sli3_options &= ~(LPFC_SLI3_NPIV_ENABLED |
4325 LPFC_SLI3_HBQ_ENABLED |
4326 LPFC_SLI3_CRP_ENABLED |
James Smartbc739052010-08-04 16:11:18 -04004327 LPFC_SLI3_BG_ENABLED |
4328 LPFC_SLI3_DSS_ENABLED);
James Smarted957682007-06-17 19:56:37 -05004329 if (rc != MBX_SUCCESS) {
dea31012005-04-17 16:05:31 -05004330 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004331 "0442 Adapter failed to init, mbxCmd x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05004332 "CONFIG_PORT, mbxStatus x%x Data: x%x\n",
James Smart04c68492009-05-22 14:52:52 -04004333 pmb->u.mb.mbxCommand, pmb->u.mb.mbxStatus, 0);
James Smart2e0fef82007-06-17 19:56:36 -05004334 spin_lock_irq(&phba->hbalock);
James Smart04c68492009-05-22 14:52:52 -04004335 phba->sli.sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05004336 spin_unlock_irq(&phba->hbalock);
4337 rc = -ENXIO;
James Smart04c68492009-05-22 14:52:52 -04004338 } else {
4339 /* Allow asynchronous mailbox command to go through */
4340 spin_lock_irq(&phba->hbalock);
4341 phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
4342 spin_unlock_irq(&phba->hbalock);
James Smarted957682007-06-17 19:56:37 -05004343 done = 1;
James Smart04c68492009-05-22 14:52:52 -04004344 }
dea31012005-04-17 16:05:31 -05004345 }
James Smarted957682007-06-17 19:56:37 -05004346 if (!done) {
4347 rc = -EINVAL;
4348 goto do_prep_failed;
4349 }
James Smart04c68492009-05-22 14:52:52 -04004350 if (pmb->u.mb.un.varCfgPort.sli_mode == 3) {
4351 if (!pmb->u.mb.un.varCfgPort.cMA) {
James Smart34b02dc2008-08-24 21:49:55 -04004352 rc = -ENXIO;
4353 goto do_prep_failed;
4354 }
James Smart04c68492009-05-22 14:52:52 -04004355 if (phba->max_vpi && pmb->u.mb.un.varCfgPort.gmv) {
James Smart34b02dc2008-08-24 21:49:55 -04004356 phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
James Smart04c68492009-05-22 14:52:52 -04004357 phba->max_vpi = pmb->u.mb.un.varCfgPort.max_vpi;
4358 phba->max_vports = (phba->max_vpi > phba->max_vports) ?
4359 phba->max_vpi : phba->max_vports;
4360
James Smart34b02dc2008-08-24 21:49:55 -04004361 } else
4362 phba->max_vpi = 0;
James Smartbc739052010-08-04 16:11:18 -04004363 phba->fips_level = 0;
4364 phba->fips_spec_rev = 0;
4365 if (pmb->u.mb.un.varCfgPort.gdss) {
James Smart04c68492009-05-22 14:52:52 -04004366 phba->sli3_options |= LPFC_SLI3_DSS_ENABLED;
James Smartbc739052010-08-04 16:11:18 -04004367 phba->fips_level = pmb->u.mb.un.varCfgPort.fips_level;
4368 phba->fips_spec_rev = pmb->u.mb.un.varCfgPort.fips_rev;
4369 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4370 "2850 Security Crypto Active. FIPS x%d "
4371 "(Spec Rev: x%d)",
4372 phba->fips_level, phba->fips_spec_rev);
4373 }
4374 if (pmb->u.mb.un.varCfgPort.sec_err) {
4375 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4376 "2856 Config Port Security Crypto "
4377 "Error: x%x ",
4378 pmb->u.mb.un.varCfgPort.sec_err);
4379 }
James Smart04c68492009-05-22 14:52:52 -04004380 if (pmb->u.mb.un.varCfgPort.gerbm)
James Smart34b02dc2008-08-24 21:49:55 -04004381 phba->sli3_options |= LPFC_SLI3_HBQ_ENABLED;
James Smart04c68492009-05-22 14:52:52 -04004382 if (pmb->u.mb.un.varCfgPort.gcrp)
James Smart34b02dc2008-08-24 21:49:55 -04004383 phba->sli3_options |= LPFC_SLI3_CRP_ENABLED;
James Smart6e7288d2010-06-07 15:23:35 -04004384
4385 phba->hbq_get = phba->mbox->us.s3_pgp.hbq_get;
4386 phba->port_gp = phba->mbox->us.s3_pgp.port;
James Smarte2a0a9d2008-12-04 22:40:02 -05004387
4388 if (phba->cfg_enable_bg) {
James Smart04c68492009-05-22 14:52:52 -04004389 if (pmb->u.mb.un.varCfgPort.gbg)
James Smarte2a0a9d2008-12-04 22:40:02 -05004390 phba->sli3_options |= LPFC_SLI3_BG_ENABLED;
4391 else
4392 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4393 "0443 Adapter did not grant "
4394 "BlockGuard\n");
4395 }
James Smart34b02dc2008-08-24 21:49:55 -04004396 } else {
James Smart8f34f4c2008-12-04 22:39:23 -05004397 phba->hbq_get = NULL;
James Smart34b02dc2008-08-24 21:49:55 -04004398 phba->port_gp = phba->mbox->us.s2.port;
James Smartd7c255b2008-08-24 21:50:00 -04004399 phba->max_vpi = 0;
James Smarted957682007-06-17 19:56:37 -05004400 }
James Smart92d7f7b2007-06-17 19:56:38 -05004401do_prep_failed:
James Smarted957682007-06-17 19:56:37 -05004402 mempool_free(pmb, phba->mbox_mem_pool);
4403 return rc;
4404}
4405
James Smarte59058c2008-08-24 21:49:00 -04004406
4407/**
James Smart3621a712009-04-06 18:47:14 -04004408 * lpfc_sli_hba_setup - SLI intialization function
James Smarte59058c2008-08-24 21:49:00 -04004409 * @phba: Pointer to HBA context object.
4410 *
4411 * This function is the main SLI intialization function. This function
4412 * is called by the HBA intialization code, HBA reset code and HBA
4413 * error attention handler code. Caller is not required to hold any
4414 * locks. This function issues config_port mailbox command to configure
4415 * the SLI, setup iocb rings and HBQ rings. In the end the function
4416 * calls the config_port_post function to issue init_link mailbox
4417 * command and to start the discovery. The function will return zero
4418 * if successful, else it will return negative error code.
4419 **/
James Smarted957682007-06-17 19:56:37 -05004420int
4421lpfc_sli_hba_setup(struct lpfc_hba *phba)
4422{
4423 uint32_t rc;
James Smart92d7f7b2007-06-17 19:56:38 -05004424 int mode = 3;
James Smarted957682007-06-17 19:56:37 -05004425
4426 switch (lpfc_sli_mode) {
4427 case 2:
James Smart78b2d852007-08-02 11:10:21 -04004428 if (phba->cfg_enable_npiv) {
James Smart92d7f7b2007-06-17 19:56:38 -05004429 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04004430 "1824 NPIV enabled: Override lpfc_sli_mode "
James Smart92d7f7b2007-06-17 19:56:38 -05004431 "parameter (%d) to auto (0).\n",
James Smarte8b62012007-08-02 11:10:09 -04004432 lpfc_sli_mode);
James Smart92d7f7b2007-06-17 19:56:38 -05004433 break;
4434 }
James Smarted957682007-06-17 19:56:37 -05004435 mode = 2;
4436 break;
4437 case 0:
4438 case 3:
4439 break;
4440 default:
James Smart92d7f7b2007-06-17 19:56:38 -05004441 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04004442 "1819 Unrecognized lpfc_sli_mode "
4443 "parameter: %d.\n", lpfc_sli_mode);
James Smarted957682007-06-17 19:56:37 -05004444
4445 break;
4446 }
4447
James Smart93996272008-08-24 21:50:30 -04004448 rc = lpfc_sli_config_port(phba, mode);
4449
James Smarted957682007-06-17 19:56:37 -05004450 if (rc && lpfc_sli_mode == 3)
James Smart92d7f7b2007-06-17 19:56:38 -05004451 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04004452 "1820 Unable to select SLI-3. "
4453 "Not supported by adapter.\n");
James Smarted957682007-06-17 19:56:37 -05004454 if (rc && mode != 2)
James Smart93996272008-08-24 21:50:30 -04004455 rc = lpfc_sli_config_port(phba, 2);
James Smarted957682007-06-17 19:56:37 -05004456 if (rc)
dea31012005-04-17 16:05:31 -05004457 goto lpfc_sli_hba_setup_error;
4458
James Smart0d878412009-10-02 15:16:56 -04004459 /* Enable PCIe device Advanced Error Reporting (AER) if configured */
4460 if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
4461 rc = pci_enable_pcie_error_reporting(phba->pcidev);
4462 if (!rc) {
4463 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4464 "2709 This device supports "
4465 "Advanced Error Reporting (AER)\n");
4466 spin_lock_irq(&phba->hbalock);
4467 phba->hba_flag |= HBA_AER_ENABLED;
4468 spin_unlock_irq(&phba->hbalock);
4469 } else {
4470 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4471 "2708 This device does not support "
4472 "Advanced Error Reporting (AER)\n");
4473 phba->cfg_aer_support = 0;
4474 }
4475 }
4476
James Smarted957682007-06-17 19:56:37 -05004477 if (phba->sli_rev == 3) {
4478 phba->iocb_cmd_size = SLI3_IOCB_CMD_SIZE;
4479 phba->iocb_rsp_size = SLI3_IOCB_RSP_SIZE;
James Smarted957682007-06-17 19:56:37 -05004480 } else {
4481 phba->iocb_cmd_size = SLI2_IOCB_CMD_SIZE;
4482 phba->iocb_rsp_size = SLI2_IOCB_RSP_SIZE;
James Smart92d7f7b2007-06-17 19:56:38 -05004483 phba->sli3_options = 0;
James Smarted957682007-06-17 19:56:37 -05004484 }
4485
4486 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004487 "0444 Firmware in SLI %x mode. Max_vpi %d\n",
4488 phba->sli_rev, phba->max_vpi);
James Smarted957682007-06-17 19:56:37 -05004489 rc = lpfc_sli_ring_map(phba);
dea31012005-04-17 16:05:31 -05004490
4491 if (rc)
4492 goto lpfc_sli_hba_setup_error;
4493
James Smart93996272008-08-24 21:50:30 -04004494 /* Init HBQs */
James Smarted957682007-06-17 19:56:37 -05004495 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
4496 rc = lpfc_sli_hbq_setup(phba);
4497 if (rc)
4498 goto lpfc_sli_hba_setup_error;
4499 }
James Smart04c68492009-05-22 14:52:52 -04004500 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004501 phba->sli.sli_flag |= LPFC_PROCESS_LA;
James Smart04c68492009-05-22 14:52:52 -04004502 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004503
4504 rc = lpfc_config_port_post(phba);
4505 if (rc)
4506 goto lpfc_sli_hba_setup_error;
4507
James Smarted957682007-06-17 19:56:37 -05004508 return rc;
4509
James Smart92d7f7b2007-06-17 19:56:38 -05004510lpfc_sli_hba_setup_error:
James Smart2e0fef82007-06-17 19:56:36 -05004511 phba->link_state = LPFC_HBA_ERROR;
James Smarte40a02c2010-02-26 14:13:54 -05004512 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004513 "0445 Firmware initialization failed\n");
dea31012005-04-17 16:05:31 -05004514 return rc;
4515}
4516
James Smartda0436e2009-05-22 14:51:39 -04004517/**
4518 * lpfc_sli4_read_fcoe_params - Read fcoe params from conf region
4519 * @phba: Pointer to HBA context object.
4520 * @mboxq: mailbox pointer.
4521 * This function issue a dump mailbox command to read config region
4522 * 23 and parse the records in the region and populate driver
4523 * data structure.
4524 **/
4525static int
4526lpfc_sli4_read_fcoe_params(struct lpfc_hba *phba,
4527 LPFC_MBOXQ_t *mboxq)
4528{
4529 struct lpfc_dmabuf *mp;
4530 struct lpfc_mqe *mqe;
4531 uint32_t data_length;
4532 int rc;
4533
4534 /* Program the default value of vlan_id and fc_map */
4535 phba->valid_vlan = 0;
4536 phba->fc_map[0] = LPFC_FCOE_FCF_MAP0;
4537 phba->fc_map[1] = LPFC_FCOE_FCF_MAP1;
4538 phba->fc_map[2] = LPFC_FCOE_FCF_MAP2;
4539
4540 mqe = &mboxq->u.mqe;
4541 if (lpfc_dump_fcoe_param(phba, mboxq))
4542 return -ENOMEM;
4543
4544 mp = (struct lpfc_dmabuf *) mboxq->context1;
4545 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4546
4547 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
4548 "(%d):2571 Mailbox cmd x%x Status x%x "
4549 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x "
4550 "x%x x%x x%x x%x x%x x%x x%x x%x x%x "
4551 "CQ: x%x x%x x%x x%x\n",
4552 mboxq->vport ? mboxq->vport->vpi : 0,
4553 bf_get(lpfc_mqe_command, mqe),
4554 bf_get(lpfc_mqe_status, mqe),
4555 mqe->un.mb_words[0], mqe->un.mb_words[1],
4556 mqe->un.mb_words[2], mqe->un.mb_words[3],
4557 mqe->un.mb_words[4], mqe->un.mb_words[5],
4558 mqe->un.mb_words[6], mqe->un.mb_words[7],
4559 mqe->un.mb_words[8], mqe->un.mb_words[9],
4560 mqe->un.mb_words[10], mqe->un.mb_words[11],
4561 mqe->un.mb_words[12], mqe->un.mb_words[13],
4562 mqe->un.mb_words[14], mqe->un.mb_words[15],
4563 mqe->un.mb_words[16], mqe->un.mb_words[50],
4564 mboxq->mcqe.word0,
4565 mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
4566 mboxq->mcqe.trailer);
4567
4568 if (rc) {
4569 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4570 kfree(mp);
4571 return -EIO;
4572 }
4573 data_length = mqe->un.mb_words[5];
James Smarta0c87cb2009-07-19 10:01:10 -04004574 if (data_length > DMP_RGN23_SIZE) {
James Smartd11e31d2009-06-10 17:23:06 -04004575 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4576 kfree(mp);
James Smartda0436e2009-05-22 14:51:39 -04004577 return -EIO;
James Smartd11e31d2009-06-10 17:23:06 -04004578 }
James Smartda0436e2009-05-22 14:51:39 -04004579
4580 lpfc_parse_fcoe_conf(phba, mp->virt, data_length);
4581 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4582 kfree(mp);
4583 return 0;
4584}
4585
4586/**
4587 * lpfc_sli4_read_rev - Issue READ_REV and collect vpd data
4588 * @phba: pointer to lpfc hba data structure.
4589 * @mboxq: pointer to the LPFC_MBOXQ_t structure.
4590 * @vpd: pointer to the memory to hold resulting port vpd data.
4591 * @vpd_size: On input, the number of bytes allocated to @vpd.
4592 * On output, the number of data bytes in @vpd.
4593 *
4594 * This routine executes a READ_REV SLI4 mailbox command. In
4595 * addition, this routine gets the port vpd data.
4596 *
4597 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02004598 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -04004599 * -ENOMEM - could not allocated memory.
James Smartda0436e2009-05-22 14:51:39 -04004600 **/
4601static int
4602lpfc_sli4_read_rev(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
4603 uint8_t *vpd, uint32_t *vpd_size)
4604{
4605 int rc = 0;
4606 uint32_t dma_size;
4607 struct lpfc_dmabuf *dmabuf;
4608 struct lpfc_mqe *mqe;
4609
4610 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
4611 if (!dmabuf)
4612 return -ENOMEM;
4613
4614 /*
4615 * Get a DMA buffer for the vpd data resulting from the READ_REV
4616 * mailbox command.
4617 */
4618 dma_size = *vpd_size;
4619 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
4620 dma_size,
4621 &dmabuf->phys,
4622 GFP_KERNEL);
4623 if (!dmabuf->virt) {
4624 kfree(dmabuf);
4625 return -ENOMEM;
4626 }
4627 memset(dmabuf->virt, 0, dma_size);
4628
4629 /*
4630 * The SLI4 implementation of READ_REV conflicts at word1,
4631 * bits 31:16 and SLI4 adds vpd functionality not present
4632 * in SLI3. This code corrects the conflicts.
4633 */
4634 lpfc_read_rev(phba, mboxq);
4635 mqe = &mboxq->u.mqe;
4636 mqe->un.read_rev.vpd_paddr_high = putPaddrHigh(dmabuf->phys);
4637 mqe->un.read_rev.vpd_paddr_low = putPaddrLow(dmabuf->phys);
4638 mqe->un.read_rev.word1 &= 0x0000FFFF;
4639 bf_set(lpfc_mbx_rd_rev_vpd, &mqe->un.read_rev, 1);
4640 bf_set(lpfc_mbx_rd_rev_avail_len, &mqe->un.read_rev, dma_size);
4641
4642 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4643 if (rc) {
4644 dma_free_coherent(&phba->pcidev->dev, dma_size,
4645 dmabuf->virt, dmabuf->phys);
James Smartdef9c7a2009-12-21 17:02:28 -05004646 kfree(dmabuf);
James Smartda0436e2009-05-22 14:51:39 -04004647 return -EIO;
4648 }
4649
James Smartda0436e2009-05-22 14:51:39 -04004650 /*
4651 * The available vpd length cannot be bigger than the
4652 * DMA buffer passed to the port. Catch the less than
4653 * case and update the caller's size.
4654 */
4655 if (mqe->un.read_rev.avail_vpd_len < *vpd_size)
4656 *vpd_size = mqe->un.read_rev.avail_vpd_len;
4657
James Smartd7c47992010-06-08 18:31:54 -04004658 memcpy(vpd, dmabuf->virt, *vpd_size);
4659
James Smartda0436e2009-05-22 14:51:39 -04004660 dma_free_coherent(&phba->pcidev->dev, dma_size,
4661 dmabuf->virt, dmabuf->phys);
4662 kfree(dmabuf);
4663 return 0;
4664}
4665
4666/**
4667 * lpfc_sli4_arm_cqeq_intr - Arm sli-4 device completion and event queues
4668 * @phba: pointer to lpfc hba data structure.
4669 *
4670 * This routine is called to explicitly arm the SLI4 device's completion and
4671 * event queues
4672 **/
4673static void
4674lpfc_sli4_arm_cqeq_intr(struct lpfc_hba *phba)
4675{
4676 uint8_t fcp_eqidx;
4677
4678 lpfc_sli4_cq_release(phba->sli4_hba.mbx_cq, LPFC_QUEUE_REARM);
4679 lpfc_sli4_cq_release(phba->sli4_hba.els_cq, LPFC_QUEUE_REARM);
James Smartda0436e2009-05-22 14:51:39 -04004680 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count; fcp_eqidx++)
4681 lpfc_sli4_cq_release(phba->sli4_hba.fcp_cq[fcp_eqidx],
4682 LPFC_QUEUE_REARM);
4683 lpfc_sli4_eq_release(phba->sli4_hba.sp_eq, LPFC_QUEUE_REARM);
4684 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count; fcp_eqidx++)
4685 lpfc_sli4_eq_release(phba->sli4_hba.fp_eq[fcp_eqidx],
4686 LPFC_QUEUE_REARM);
4687}
4688
4689/**
4690 * lpfc_sli4_hba_setup - SLI4 device intialization PCI function
4691 * @phba: Pointer to HBA context object.
4692 *
4693 * This function is the main SLI4 device intialization PCI function. This
4694 * function is called by the HBA intialization code, HBA reset code and
4695 * HBA error attention handler code. Caller is not required to hold any
4696 * locks.
4697 **/
4698int
4699lpfc_sli4_hba_setup(struct lpfc_hba *phba)
4700{
4701 int rc;
4702 LPFC_MBOXQ_t *mboxq;
4703 struct lpfc_mqe *mqe;
4704 uint8_t *vpd;
4705 uint32_t vpd_size;
4706 uint32_t ftr_rsp = 0;
4707 struct Scsi_Host *shost = lpfc_shost_from_vport(phba->pport);
4708 struct lpfc_vport *vport = phba->pport;
4709 struct lpfc_dmabuf *mp;
4710
James Smart2fcee4b2010-12-15 17:57:46 -05004711 /*
4712 * TODO: Why does this routine execute these task in a different
4713 * order from probe?
4714 */
James Smartda0436e2009-05-22 14:51:39 -04004715 /* Perform a PCI function reset to start from clean */
4716 rc = lpfc_pci_function_reset(phba);
4717 if (unlikely(rc))
4718 return -ENODEV;
4719
4720 /* Check the HBA Host Status Register for readyness */
4721 rc = lpfc_sli4_post_status_check(phba);
4722 if (unlikely(rc))
4723 return -ENODEV;
4724 else {
4725 spin_lock_irq(&phba->hbalock);
4726 phba->sli.sli_flag |= LPFC_SLI_ACTIVE;
4727 spin_unlock_irq(&phba->hbalock);
4728 }
4729
4730 /*
4731 * Allocate a single mailbox container for initializing the
4732 * port.
4733 */
4734 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4735 if (!mboxq)
4736 return -ENOMEM;
4737
4738 /*
4739 * Continue initialization with default values even if driver failed
4740 * to read FCoE param config regions
4741 */
4742 if (lpfc_sli4_read_fcoe_params(phba, mboxq))
4743 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
James Smarte4e74272009-07-19 10:01:38 -04004744 "2570 Failed to read FCoE parameters\n");
James Smartda0436e2009-05-22 14:51:39 -04004745
4746 /* Issue READ_REV to collect vpd and FW information. */
James Smart49198b32010-04-06 15:04:33 -04004747 vpd_size = SLI4_PAGE_SIZE;
James Smartda0436e2009-05-22 14:51:39 -04004748 vpd = kzalloc(vpd_size, GFP_KERNEL);
4749 if (!vpd) {
4750 rc = -ENOMEM;
4751 goto out_free_mbox;
4752 }
4753
4754 rc = lpfc_sli4_read_rev(phba, mboxq, vpd, &vpd_size);
James Smart76a95d72010-11-20 23:11:48 -05004755 if (unlikely(rc)) {
4756 kfree(vpd);
4757 goto out_free_mbox;
4758 }
James Smartda0436e2009-05-22 14:51:39 -04004759 mqe = &mboxq->u.mqe;
James Smartf1126682009-06-10 17:22:44 -04004760 phba->sli_rev = bf_get(lpfc_mbx_rd_rev_sli_lvl, &mqe->un.read_rev);
4761 if (bf_get(lpfc_mbx_rd_rev_fcoe, &mqe->un.read_rev))
James Smart76a95d72010-11-20 23:11:48 -05004762 phba->hba_flag |= HBA_FCOE_MODE;
4763 else
4764 phba->hba_flag &= ~HBA_FCOE_MODE;
James Smart45ed1192009-10-02 15:17:02 -04004765
4766 if (bf_get(lpfc_mbx_rd_rev_cee_ver, &mqe->un.read_rev) ==
4767 LPFC_DCBX_CEE_MODE)
4768 phba->hba_flag |= HBA_FIP_SUPPORT;
4769 else
4770 phba->hba_flag &= ~HBA_FIP_SUPPORT;
4771
James Smartf1126682009-06-10 17:22:44 -04004772 if (phba->sli_rev != LPFC_SLI_REV4 ||
James Smart76a95d72010-11-20 23:11:48 -05004773 !(phba->hba_flag & HBA_FCOE_MODE)) {
James Smartda0436e2009-05-22 14:51:39 -04004774 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4775 "0376 READ_REV Error. SLI Level %d "
4776 "FCoE enabled %d\n",
James Smart76a95d72010-11-20 23:11:48 -05004777 phba->sli_rev, phba->hba_flag & HBA_FCOE_MODE);
James Smartda0436e2009-05-22 14:51:39 -04004778 rc = -EIO;
James Smart76a95d72010-11-20 23:11:48 -05004779 kfree(vpd);
4780 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04004781 }
James Smartda0436e2009-05-22 14:51:39 -04004782 /*
4783 * Evaluate the read rev and vpd data. Populate the driver
4784 * state with the results. If this routine fails, the failure
4785 * is not fatal as the driver will use generic values.
4786 */
4787 rc = lpfc_parse_vpd(phba, vpd, vpd_size);
4788 if (unlikely(!rc)) {
4789 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4790 "0377 Error %d parsing vpd. "
4791 "Using defaults.\n", rc);
4792 rc = 0;
4793 }
James Smart76a95d72010-11-20 23:11:48 -05004794 kfree(vpd);
James Smartda0436e2009-05-22 14:51:39 -04004795
James Smartf1126682009-06-10 17:22:44 -04004796 /* Save information as VPD data */
4797 phba->vpd.rev.biuRev = mqe->un.read_rev.first_hw_rev;
4798 phba->vpd.rev.smRev = mqe->un.read_rev.second_hw_rev;
4799 phba->vpd.rev.endecRev = mqe->un.read_rev.third_hw_rev;
4800 phba->vpd.rev.fcphHigh = bf_get(lpfc_mbx_rd_rev_fcph_high,
4801 &mqe->un.read_rev);
4802 phba->vpd.rev.fcphLow = bf_get(lpfc_mbx_rd_rev_fcph_low,
4803 &mqe->un.read_rev);
4804 phba->vpd.rev.feaLevelHigh = bf_get(lpfc_mbx_rd_rev_ftr_lvl_high,
4805 &mqe->un.read_rev);
4806 phba->vpd.rev.feaLevelLow = bf_get(lpfc_mbx_rd_rev_ftr_lvl_low,
4807 &mqe->un.read_rev);
4808 phba->vpd.rev.sli1FwRev = mqe->un.read_rev.fw_id_rev;
4809 memcpy(phba->vpd.rev.sli1FwName, mqe->un.read_rev.fw_name, 16);
4810 phba->vpd.rev.sli2FwRev = mqe->un.read_rev.ulp_fw_id_rev;
4811 memcpy(phba->vpd.rev.sli2FwName, mqe->un.read_rev.ulp_fw_name, 16);
4812 phba->vpd.rev.opFwRev = mqe->un.read_rev.fw_id_rev;
4813 memcpy(phba->vpd.rev.opFwName, mqe->un.read_rev.fw_name, 16);
4814 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
4815 "(%d):0380 READ_REV Status x%x "
4816 "fw_rev:%s fcphHi:%x fcphLo:%x flHi:%x flLo:%x\n",
4817 mboxq->vport ? mboxq->vport->vpi : 0,
4818 bf_get(lpfc_mqe_status, mqe),
4819 phba->vpd.rev.opFwName,
4820 phba->vpd.rev.fcphHigh, phba->vpd.rev.fcphLow,
4821 phba->vpd.rev.feaLevelHigh, phba->vpd.rev.feaLevelLow);
James Smartda0436e2009-05-22 14:51:39 -04004822
4823 /*
4824 * Discover the port's supported feature set and match it against the
4825 * hosts requests.
4826 */
4827 lpfc_request_features(phba, mboxq);
4828 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4829 if (unlikely(rc)) {
4830 rc = -EIO;
James Smart76a95d72010-11-20 23:11:48 -05004831 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04004832 }
4833
4834 /*
4835 * The port must support FCP initiator mode as this is the
4836 * only mode running in the host.
4837 */
4838 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_fcpi, &mqe->un.req_ftrs))) {
4839 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
4840 "0378 No support for fcpi mode.\n");
4841 ftr_rsp++;
4842 }
James Smartfedd3b72011-02-16 12:39:24 -05004843 if (bf_get(lpfc_mbx_rq_ftr_rsp_perfh, &mqe->un.req_ftrs))
4844 phba->sli3_options |= LPFC_SLI4_PERFH_ENABLED;
4845 else
4846 phba->sli3_options &= ~LPFC_SLI4_PERFH_ENABLED;
James Smartda0436e2009-05-22 14:51:39 -04004847 /*
4848 * If the port cannot support the host's requested features
4849 * then turn off the global config parameters to disable the
4850 * feature in the driver. This is not a fatal error.
4851 */
4852 if ((phba->cfg_enable_bg) &&
4853 !(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs)))
4854 ftr_rsp++;
4855
4856 if (phba->max_vpi && phba->cfg_enable_npiv &&
4857 !(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
4858 ftr_rsp++;
4859
4860 if (ftr_rsp) {
4861 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
4862 "0379 Feature Mismatch Data: x%08x %08x "
4863 "x%x x%x x%x\n", mqe->un.req_ftrs.word2,
4864 mqe->un.req_ftrs.word3, phba->cfg_enable_bg,
4865 phba->cfg_enable_npiv, phba->max_vpi);
4866 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs)))
4867 phba->cfg_enable_bg = 0;
4868 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
4869 phba->cfg_enable_npiv = 0;
4870 }
4871
4872 /* These SLI3 features are assumed in SLI4 */
4873 spin_lock_irq(&phba->hbalock);
4874 phba->sli3_options |= (LPFC_SLI3_NPIV_ENABLED | LPFC_SLI3_HBQ_ENABLED);
4875 spin_unlock_irq(&phba->hbalock);
4876
4877 /* Read the port's service parameters. */
James Smart9f1177a2010-02-26 14:12:57 -05004878 rc = lpfc_read_sparam(phba, mboxq, vport->vpi);
4879 if (rc) {
4880 phba->link_state = LPFC_HBA_ERROR;
4881 rc = -ENOMEM;
James Smart76a95d72010-11-20 23:11:48 -05004882 goto out_free_mbox;
James Smart9f1177a2010-02-26 14:12:57 -05004883 }
4884
James Smartda0436e2009-05-22 14:51:39 -04004885 mboxq->vport = vport;
4886 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4887 mp = (struct lpfc_dmabuf *) mboxq->context1;
4888 if (rc == MBX_SUCCESS) {
4889 memcpy(&vport->fc_sparam, mp->virt, sizeof(struct serv_parm));
4890 rc = 0;
4891 }
4892
4893 /*
4894 * This memory was allocated by the lpfc_read_sparam routine. Release
4895 * it to the mbuf pool.
4896 */
4897 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4898 kfree(mp);
4899 mboxq->context1 = NULL;
4900 if (unlikely(rc)) {
4901 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4902 "0382 READ_SPARAM command failed "
4903 "status %d, mbxStatus x%x\n",
4904 rc, bf_get(lpfc_mqe_status, mqe));
4905 phba->link_state = LPFC_HBA_ERROR;
4906 rc = -EIO;
James Smart76a95d72010-11-20 23:11:48 -05004907 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04004908 }
4909
4910 if (phba->cfg_soft_wwnn)
4911 u64_to_wwn(phba->cfg_soft_wwnn,
4912 vport->fc_sparam.nodeName.u.wwn);
4913 if (phba->cfg_soft_wwpn)
4914 u64_to_wwn(phba->cfg_soft_wwpn,
4915 vport->fc_sparam.portName.u.wwn);
4916 memcpy(&vport->fc_nodename, &vport->fc_sparam.nodeName,
4917 sizeof(struct lpfc_name));
4918 memcpy(&vport->fc_portname, &vport->fc_sparam.portName,
4919 sizeof(struct lpfc_name));
4920
4921 /* Update the fc_host data structures with new wwn. */
4922 fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
4923 fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
4924
4925 /* Register SGL pool to the device using non-embedded mailbox command */
4926 rc = lpfc_sli4_post_sgl_list(phba);
4927 if (unlikely(rc)) {
4928 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smart6a9c52c2009-10-02 15:16:51 -04004929 "0582 Error %d during sgl post operation\n",
4930 rc);
James Smartda0436e2009-05-22 14:51:39 -04004931 rc = -ENODEV;
James Smart76a95d72010-11-20 23:11:48 -05004932 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04004933 }
4934
4935 /* Register SCSI SGL pool to the device */
4936 rc = lpfc_sli4_repost_scsi_sgl_list(phba);
4937 if (unlikely(rc)) {
4938 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
James Smart6a9c52c2009-10-02 15:16:51 -04004939 "0383 Error %d during scsi sgl post "
4940 "operation\n", rc);
James Smartda0436e2009-05-22 14:51:39 -04004941 /* Some Scsi buffers were moved to the abort scsi list */
4942 /* A pci function reset will repost them */
4943 rc = -ENODEV;
James Smart76a95d72010-11-20 23:11:48 -05004944 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04004945 }
4946
4947 /* Post the rpi header region to the device. */
4948 rc = lpfc_sli4_post_all_rpi_hdrs(phba);
4949 if (unlikely(rc)) {
4950 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4951 "0393 Error %d during rpi post operation\n",
4952 rc);
4953 rc = -ENODEV;
James Smart76a95d72010-11-20 23:11:48 -05004954 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04004955 }
James Smartda0436e2009-05-22 14:51:39 -04004956
4957 /* Set up all the queues to the device */
4958 rc = lpfc_sli4_queue_setup(phba);
4959 if (unlikely(rc)) {
4960 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4961 "0381 Error %d during queue setup.\n ", rc);
4962 goto out_stop_timers;
4963 }
4964
4965 /* Arm the CQs and then EQs on device */
4966 lpfc_sli4_arm_cqeq_intr(phba);
4967
4968 /* Indicate device interrupt mode */
4969 phba->sli4_hba.intr_enable = 1;
4970
4971 /* Allow asynchronous mailbox command to go through */
4972 spin_lock_irq(&phba->hbalock);
4973 phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
4974 spin_unlock_irq(&phba->hbalock);
4975
4976 /* Post receive buffers to the device */
4977 lpfc_sli4_rb_setup(phba);
4978
James Smartfc2b9892010-02-26 14:15:29 -05004979 /* Reset HBA FCF states after HBA reset */
4980 phba->fcf.fcf_flag = 0;
4981 phba->fcf.current_rec.flag = 0;
4982
James Smartda0436e2009-05-22 14:51:39 -04004983 /* Start the ELS watchdog timer */
James Smart8fa38512009-07-19 10:01:03 -04004984 mod_timer(&vport->els_tmofunc,
4985 jiffies + HZ * (phba->fc_ratov * 2));
James Smartda0436e2009-05-22 14:51:39 -04004986
4987 /* Start heart beat timer */
4988 mod_timer(&phba->hb_tmofunc,
4989 jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
4990 phba->hb_outstanding = 0;
4991 phba->last_completion_time = jiffies;
4992
4993 /* Start error attention (ERATT) polling timer */
4994 mod_timer(&phba->eratt_poll, jiffies + HZ * LPFC_ERATT_POLL_INTERVAL);
4995
James Smart75baf692010-06-08 18:31:21 -04004996 /* Enable PCIe device Advanced Error Reporting (AER) if configured */
4997 if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
4998 rc = pci_enable_pcie_error_reporting(phba->pcidev);
4999 if (!rc) {
5000 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5001 "2829 This device supports "
5002 "Advanced Error Reporting (AER)\n");
5003 spin_lock_irq(&phba->hbalock);
5004 phba->hba_flag |= HBA_AER_ENABLED;
5005 spin_unlock_irq(&phba->hbalock);
5006 } else {
5007 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5008 "2830 This device does not support "
5009 "Advanced Error Reporting (AER)\n");
5010 phba->cfg_aer_support = 0;
5011 }
5012 }
5013
James Smart76a95d72010-11-20 23:11:48 -05005014 if (!(phba->hba_flag & HBA_FCOE_MODE)) {
5015 /*
5016 * The FC Port needs to register FCFI (index 0)
5017 */
5018 lpfc_reg_fcfi(phba, mboxq);
5019 mboxq->vport = phba->pport;
5020 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
James Smart9589b0622011-04-16 11:03:17 -04005021 if (rc != MBX_SUCCESS)
James Smart76a95d72010-11-20 23:11:48 -05005022 goto out_unset_queue;
James Smart9589b0622011-04-16 11:03:17 -04005023 rc = 0;
5024 phba->fcf.fcfi = bf_get(lpfc_reg_fcfi_fcfi,
5025 &mboxq->u.mqe.un.reg_fcfi);
James Smart76a95d72010-11-20 23:11:48 -05005026 }
James Smartda0436e2009-05-22 14:51:39 -04005027 /*
5028 * The port is ready, set the host's link state to LINK_DOWN
5029 * in preparation for link interrupts.
5030 */
James Smartda0436e2009-05-22 14:51:39 -04005031 spin_lock_irq(&phba->hbalock);
5032 phba->link_state = LPFC_LINK_DOWN;
5033 spin_unlock_irq(&phba->hbalock);
James Smartfedd3b72011-02-16 12:39:24 -05005034 if (phba->cfg_suppress_link_up == LPFC_INITIALIZE_LINK)
5035 rc = phba->lpfc_hba_init_link(phba, MBX_NOWAIT);
James Smart76a95d72010-11-20 23:11:48 -05005036out_unset_queue:
James Smartda0436e2009-05-22 14:51:39 -04005037 /* Unset all the queues set up in this routine when error out */
5038 if (rc)
5039 lpfc_sli4_queue_unset(phba);
James Smartda0436e2009-05-22 14:51:39 -04005040out_stop_timers:
5041 if (rc)
5042 lpfc_stop_hba_timers(phba);
James Smartda0436e2009-05-22 14:51:39 -04005043out_free_mbox:
5044 mempool_free(mboxq, phba->mbox_mem_pool);
5045 return rc;
5046}
James Smarte59058c2008-08-24 21:49:00 -04005047
5048/**
James Smart3621a712009-04-06 18:47:14 -04005049 * lpfc_mbox_timeout - Timeout call back function for mbox timer
James Smarte59058c2008-08-24 21:49:00 -04005050 * @ptr: context object - pointer to hba structure.
dea31012005-04-17 16:05:31 -05005051 *
James Smarte59058c2008-08-24 21:49:00 -04005052 * This is the callback function for mailbox timer. The mailbox
5053 * timer is armed when a new mailbox command is issued and the timer
5054 * is deleted when the mailbox complete. The function is called by
5055 * the kernel timer code when a mailbox does not complete within
5056 * expected time. This function wakes up the worker thread to
5057 * process the mailbox timeout and returns. All the processing is
5058 * done by the worker thread function lpfc_mbox_timeout_handler.
5059 **/
dea31012005-04-17 16:05:31 -05005060void
5061lpfc_mbox_timeout(unsigned long ptr)
5062{
James Smart92d7f7b2007-06-17 19:56:38 -05005063 struct lpfc_hba *phba = (struct lpfc_hba *) ptr;
dea31012005-04-17 16:05:31 -05005064 unsigned long iflag;
James Smart2e0fef82007-06-17 19:56:36 -05005065 uint32_t tmo_posted;
dea31012005-04-17 16:05:31 -05005066
James Smart2e0fef82007-06-17 19:56:36 -05005067 spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
James Smart92d7f7b2007-06-17 19:56:38 -05005068 tmo_posted = phba->pport->work_port_events & WORKER_MBOX_TMO;
James Smart2e0fef82007-06-17 19:56:36 -05005069 if (!tmo_posted)
5070 phba->pport->work_port_events |= WORKER_MBOX_TMO;
5071 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
5072
James Smart5e9d9b82008-06-14 22:52:53 -04005073 if (!tmo_posted)
5074 lpfc_worker_wake_up(phba);
5075 return;
dea31012005-04-17 16:05:31 -05005076}
5077
James Smarte59058c2008-08-24 21:49:00 -04005078
5079/**
James Smart3621a712009-04-06 18:47:14 -04005080 * lpfc_mbox_timeout_handler - Worker thread function to handle mailbox timeout
James Smarte59058c2008-08-24 21:49:00 -04005081 * @phba: Pointer to HBA context object.
5082 *
5083 * This function is called from worker thread when a mailbox command times out.
5084 * The caller is not required to hold any locks. This function will reset the
5085 * HBA and recover all the pending commands.
5086 **/
dea31012005-04-17 16:05:31 -05005087void
5088lpfc_mbox_timeout_handler(struct lpfc_hba *phba)
5089{
James Smart2e0fef82007-06-17 19:56:36 -05005090 LPFC_MBOXQ_t *pmbox = phba->sli.mbox_active;
James Smart04c68492009-05-22 14:52:52 -04005091 MAILBOX_t *mb = &pmbox->u.mb;
James Smart1dcb58e2007-04-25 09:51:30 -04005092 struct lpfc_sli *psli = &phba->sli;
5093 struct lpfc_sli_ring *pring;
dea31012005-04-17 16:05:31 -05005094
James Smarta257bf92009-04-06 18:48:10 -04005095 /* Check the pmbox pointer first. There is a race condition
5096 * between the mbox timeout handler getting executed in the
5097 * worklist and the mailbox actually completing. When this
5098 * race condition occurs, the mbox_active will be NULL.
5099 */
5100 spin_lock_irq(&phba->hbalock);
5101 if (pmbox == NULL) {
5102 lpfc_printf_log(phba, KERN_WARNING,
5103 LOG_MBOX | LOG_SLI,
5104 "0353 Active Mailbox cleared - mailbox timeout "
5105 "exiting\n");
5106 spin_unlock_irq(&phba->hbalock);
5107 return;
5108 }
5109
dea31012005-04-17 16:05:31 -05005110 /* Mbox cmd <mbxCommand> timeout */
James Smarted957682007-06-17 19:56:37 -05005111 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04005112 "0310 Mailbox command x%x timeout Data: x%x x%x x%p\n",
James Smart92d7f7b2007-06-17 19:56:38 -05005113 mb->mbxCommand,
5114 phba->pport->port_state,
5115 phba->sli.sli_flag,
5116 phba->sli.mbox_active);
James Smarta257bf92009-04-06 18:48:10 -04005117 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05005118
James Smart1dcb58e2007-04-25 09:51:30 -04005119 /* Setting state unknown so lpfc_sli_abort_iocb_ring
5120 * would get IOCB_ERROR from lpfc_sli_issue_iocb, allowing
Lucas De Marchi25985ed2011-03-30 22:57:33 -03005121 * it to fail all outstanding SCSI IO.
James Smart1dcb58e2007-04-25 09:51:30 -04005122 */
James Smart2e0fef82007-06-17 19:56:36 -05005123 spin_lock_irq(&phba->pport->work_port_lock);
5124 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
5125 spin_unlock_irq(&phba->pport->work_port_lock);
5126 spin_lock_irq(&phba->hbalock);
5127 phba->link_state = LPFC_LINK_UNKNOWN;
James Smartf4b4c682009-05-22 14:53:12 -04005128 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05005129 spin_unlock_irq(&phba->hbalock);
James Smart1dcb58e2007-04-25 09:51:30 -04005130
5131 pring = &psli->ring[psli->fcp_ring];
5132 lpfc_sli_abort_iocb_ring(phba, pring);
5133
5134 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smart76bb24e2007-10-27 13:38:00 -04005135 "0345 Resetting board due to mailbox timeout\n");
James Smart3772a992009-05-22 14:50:54 -04005136
5137 /* Reset the HBA device */
5138 lpfc_reset_hba(phba);
dea31012005-04-17 16:05:31 -05005139}
5140
James Smarte59058c2008-08-24 21:49:00 -04005141/**
James Smart3772a992009-05-22 14:50:54 -04005142 * lpfc_sli_issue_mbox_s3 - Issue an SLI3 mailbox command to firmware
James Smarte59058c2008-08-24 21:49:00 -04005143 * @phba: Pointer to HBA context object.
5144 * @pmbox: Pointer to mailbox object.
5145 * @flag: Flag indicating how the mailbox need to be processed.
5146 *
5147 * This function is called by discovery code and HBA management code
James Smart3772a992009-05-22 14:50:54 -04005148 * to submit a mailbox command to firmware with SLI-3 interface spec. This
5149 * function gets the hbalock to protect the data structures.
James Smarte59058c2008-08-24 21:49:00 -04005150 * The mailbox command can be submitted in polling mode, in which case
5151 * this function will wait in a polling loop for the completion of the
5152 * mailbox.
5153 * If the mailbox is submitted in no_wait mode (not polling) the
5154 * function will submit the command and returns immediately without waiting
5155 * for the mailbox completion. The no_wait is supported only when HBA
5156 * is in SLI2/SLI3 mode - interrupts are enabled.
5157 * The SLI interface allows only one mailbox pending at a time. If the
5158 * mailbox is issued in polling mode and there is already a mailbox
5159 * pending, then the function will return an error. If the mailbox is issued
5160 * in NO_WAIT mode and there is a mailbox pending already, the function
5161 * will return MBX_BUSY after queuing the mailbox into mailbox queue.
5162 * The sli layer owns the mailbox object until the completion of mailbox
5163 * command if this function return MBX_BUSY or MBX_SUCCESS. For all other
5164 * return codes the caller owns the mailbox command after the return of
5165 * the function.
5166 **/
James Smart3772a992009-05-22 14:50:54 -04005167static int
5168lpfc_sli_issue_mbox_s3(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox,
5169 uint32_t flag)
dea31012005-04-17 16:05:31 -05005170{
dea31012005-04-17 16:05:31 -05005171 MAILBOX_t *mb;
James Smart2e0fef82007-06-17 19:56:36 -05005172 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05005173 uint32_t status, evtctr;
James Smart9940b972011-03-11 16:06:12 -05005174 uint32_t ha_copy, hc_copy;
dea31012005-04-17 16:05:31 -05005175 int i;
James Smart09372822008-01-11 01:52:54 -05005176 unsigned long timeout;
dea31012005-04-17 16:05:31 -05005177 unsigned long drvr_flag = 0;
James Smart34b02dc2008-08-24 21:49:55 -04005178 uint32_t word0, ldata;
dea31012005-04-17 16:05:31 -05005179 void __iomem *to_slim;
James Smart58da1ff2008-04-07 10:15:56 -04005180 int processing_queue = 0;
5181
5182 spin_lock_irqsave(&phba->hbalock, drvr_flag);
5183 if (!pmbox) {
James Smart8568a4d2009-07-19 10:01:16 -04005184 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart58da1ff2008-04-07 10:15:56 -04005185 /* processing mbox queue from intr_handler */
James Smart3772a992009-05-22 14:50:54 -04005186 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
5187 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
5188 return MBX_SUCCESS;
5189 }
James Smart58da1ff2008-04-07 10:15:56 -04005190 processing_queue = 1;
James Smart58da1ff2008-04-07 10:15:56 -04005191 pmbox = lpfc_mbox_get(phba);
5192 if (!pmbox) {
5193 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
5194 return MBX_SUCCESS;
5195 }
5196 }
dea31012005-04-17 16:05:31 -05005197
James Smarted957682007-06-17 19:56:37 -05005198 if (pmbox->mbox_cmpl && pmbox->mbox_cmpl != lpfc_sli_def_mbox_cmpl &&
James Smart92d7f7b2007-06-17 19:56:38 -05005199 pmbox->mbox_cmpl != lpfc_sli_wake_mbox_wait) {
James Smarted957682007-06-17 19:56:37 -05005200 if(!pmbox->vport) {
James Smart58da1ff2008-04-07 10:15:56 -04005201 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
James Smarted957682007-06-17 19:56:37 -05005202 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -05005203 LOG_MBOX | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04005204 "1806 Mbox x%x failed. No vport\n",
James Smart3772a992009-05-22 14:50:54 -04005205 pmbox->u.mb.mbxCommand);
James Smarted957682007-06-17 19:56:37 -05005206 dump_stack();
James Smart58da1ff2008-04-07 10:15:56 -04005207 goto out_not_finished;
James Smarted957682007-06-17 19:56:37 -05005208 }
5209 }
5210
Linas Vepstas8d63f372007-02-14 14:28:36 -06005211 /* If the PCI channel is in offline state, do not post mbox. */
James Smart58da1ff2008-04-07 10:15:56 -04005212 if (unlikely(pci_channel_offline(phba->pcidev))) {
5213 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
5214 goto out_not_finished;
5215 }
Linas Vepstas8d63f372007-02-14 14:28:36 -06005216
James Smarta257bf92009-04-06 18:48:10 -04005217 /* If HBA has a deferred error attention, fail the iocb. */
5218 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
5219 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
5220 goto out_not_finished;
5221 }
5222
dea31012005-04-17 16:05:31 -05005223 psli = &phba->sli;
James Smart92d7f7b2007-06-17 19:56:38 -05005224
James Smart3772a992009-05-22 14:50:54 -04005225 mb = &pmbox->u.mb;
dea31012005-04-17 16:05:31 -05005226 status = MBX_SUCCESS;
5227
James Smart2e0fef82007-06-17 19:56:36 -05005228 if (phba->link_state == LPFC_HBA_ERROR) {
5229 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05005230
5231 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04005232 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5233 "(%d):0311 Mailbox command x%x cannot "
5234 "issue Data: x%x x%x\n",
5235 pmbox->vport ? pmbox->vport->vpi : 0,
5236 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04005237 goto out_not_finished;
Jamie Wellnitz41415862006-02-28 19:25:27 -05005238 }
5239
James Smart9940b972011-03-11 16:06:12 -05005240 if (mb->mbxCommand != MBX_KILL_BOARD && flag & MBX_NOWAIT) {
5241 if (lpfc_readl(phba->HCregaddr, &hc_copy) ||
5242 !(hc_copy & HC_MBINT_ENA)) {
5243 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
5244 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smart3772a992009-05-22 14:50:54 -04005245 "(%d):2528 Mailbox command x%x cannot "
5246 "issue Data: x%x x%x\n",
5247 pmbox->vport ? pmbox->vport->vpi : 0,
5248 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
James Smart9940b972011-03-11 16:06:12 -05005249 goto out_not_finished;
5250 }
James Smart92908312006-03-07 15:04:13 -05005251 }
5252
dea31012005-04-17 16:05:31 -05005253 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
5254 /* Polling for a mbox command when another one is already active
5255 * is not allowed in SLI. Also, the driver must have established
5256 * SLI2 mode to queue and process multiple mbox commands.
5257 */
5258
5259 if (flag & MBX_POLL) {
James Smart2e0fef82007-06-17 19:56:36 -05005260 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05005261
5262 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04005263 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5264 "(%d):2529 Mailbox command x%x "
5265 "cannot issue Data: x%x x%x\n",
5266 pmbox->vport ? pmbox->vport->vpi : 0,
5267 pmbox->u.mb.mbxCommand,
5268 psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04005269 goto out_not_finished;
dea31012005-04-17 16:05:31 -05005270 }
5271
James Smart3772a992009-05-22 14:50:54 -04005272 if (!(psli->sli_flag & LPFC_SLI_ACTIVE)) {
James Smart2e0fef82007-06-17 19:56:36 -05005273 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05005274 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04005275 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5276 "(%d):2530 Mailbox command x%x "
5277 "cannot issue Data: x%x x%x\n",
5278 pmbox->vport ? pmbox->vport->vpi : 0,
5279 pmbox->u.mb.mbxCommand,
5280 psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04005281 goto out_not_finished;
dea31012005-04-17 16:05:31 -05005282 }
5283
dea31012005-04-17 16:05:31 -05005284 /* Another mailbox command is still being processed, queue this
5285 * command to be processed later.
5286 */
5287 lpfc_mbox_put(phba, pmbox);
5288
5289 /* Mbox cmd issue - BUSY */
James Smarted957682007-06-17 19:56:37 -05005290 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04005291 "(%d):0308 Mbox cmd issue - BUSY Data: "
James Smart92d7f7b2007-06-17 19:56:38 -05005292 "x%x x%x x%x x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -05005293 pmbox->vport ? pmbox->vport->vpi : 0xffffff,
5294 mb->mbxCommand, phba->pport->port_state,
5295 psli->sli_flag, flag);
dea31012005-04-17 16:05:31 -05005296
5297 psli->slistat.mbox_busy++;
James Smart2e0fef82007-06-17 19:56:36 -05005298 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05005299
James Smart858c9f62007-06-17 19:56:39 -05005300 if (pmbox->vport) {
5301 lpfc_debugfs_disc_trc(pmbox->vport,
5302 LPFC_DISC_TRC_MBOX_VPORT,
5303 "MBOX Bsy vport: cmd:x%x mb:x%x x%x",
5304 (uint32_t)mb->mbxCommand,
5305 mb->un.varWords[0], mb->un.varWords[1]);
5306 }
5307 else {
5308 lpfc_debugfs_disc_trc(phba->pport,
5309 LPFC_DISC_TRC_MBOX,
5310 "MBOX Bsy: cmd:x%x mb:x%x x%x",
5311 (uint32_t)mb->mbxCommand,
5312 mb->un.varWords[0], mb->un.varWords[1]);
5313 }
5314
James Smart2e0fef82007-06-17 19:56:36 -05005315 return MBX_BUSY;
dea31012005-04-17 16:05:31 -05005316 }
5317
dea31012005-04-17 16:05:31 -05005318 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
5319
5320 /* If we are not polling, we MUST be in SLI2 mode */
5321 if (flag != MBX_POLL) {
James Smart3772a992009-05-22 14:50:54 -04005322 if (!(psli->sli_flag & LPFC_SLI_ACTIVE) &&
Jamie Wellnitz41415862006-02-28 19:25:27 -05005323 (mb->mbxCommand != MBX_KILL_BOARD)) {
dea31012005-04-17 16:05:31 -05005324 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05005325 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05005326 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04005327 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5328 "(%d):2531 Mailbox command x%x "
5329 "cannot issue Data: x%x x%x\n",
5330 pmbox->vport ? pmbox->vport->vpi : 0,
5331 pmbox->u.mb.mbxCommand,
5332 psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04005333 goto out_not_finished;
dea31012005-04-17 16:05:31 -05005334 }
5335 /* timeout active mbox command */
James Smarta309a6b2006-08-01 07:33:43 -04005336 mod_timer(&psli->mbox_tmo, (jiffies +
5337 (HZ * lpfc_mbox_tmo_val(phba, mb->mbxCommand))));
dea31012005-04-17 16:05:31 -05005338 }
5339
5340 /* Mailbox cmd <cmd> issue */
James Smarted957682007-06-17 19:56:37 -05005341 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04005342 "(%d):0309 Mailbox cmd x%x issue Data: x%x x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05005343 "x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04005344 pmbox->vport ? pmbox->vport->vpi : 0,
James Smart92d7f7b2007-06-17 19:56:38 -05005345 mb->mbxCommand, phba->pport->port_state,
5346 psli->sli_flag, flag);
dea31012005-04-17 16:05:31 -05005347
James Smart858c9f62007-06-17 19:56:39 -05005348 if (mb->mbxCommand != MBX_HEARTBEAT) {
5349 if (pmbox->vport) {
5350 lpfc_debugfs_disc_trc(pmbox->vport,
5351 LPFC_DISC_TRC_MBOX_VPORT,
5352 "MBOX Send vport: cmd:x%x mb:x%x x%x",
5353 (uint32_t)mb->mbxCommand,
5354 mb->un.varWords[0], mb->un.varWords[1]);
5355 }
5356 else {
5357 lpfc_debugfs_disc_trc(phba->pport,
5358 LPFC_DISC_TRC_MBOX,
5359 "MBOX Send: cmd:x%x mb:x%x x%x",
5360 (uint32_t)mb->mbxCommand,
5361 mb->un.varWords[0], mb->un.varWords[1]);
5362 }
5363 }
5364
dea31012005-04-17 16:05:31 -05005365 psli->slistat.mbox_cmd++;
5366 evtctr = psli->slistat.mbox_event;
5367
5368 /* next set own bit for the adapter and copy over command word */
5369 mb->mbxOwner = OWN_CHIP;
5370
James Smart3772a992009-05-22 14:50:54 -04005371 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
James Smart7a470272010-03-15 11:25:20 -04005372 /* Populate mbox extension offset word. */
5373 if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len) {
5374 *(((uint32_t *)mb) + pmbox->mbox_offset_word)
5375 = (uint8_t *)phba->mbox_ext
5376 - (uint8_t *)phba->mbox;
5377 }
5378
5379 /* Copy the mailbox extension data */
5380 if (pmbox->in_ext_byte_len && pmbox->context2) {
5381 lpfc_sli_pcimem_bcopy(pmbox->context2,
5382 (uint8_t *)phba->mbox_ext,
5383 pmbox->in_ext_byte_len);
5384 }
5385 /* Copy command data to host SLIM area */
James Smart34b02dc2008-08-24 21:49:55 -04005386 lpfc_sli_pcimem_bcopy(mb, phba->mbox, MAILBOX_CMD_SIZE);
dea31012005-04-17 16:05:31 -05005387 } else {
James Smart7a470272010-03-15 11:25:20 -04005388 /* Populate mbox extension offset word. */
5389 if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len)
5390 *(((uint32_t *)mb) + pmbox->mbox_offset_word)
5391 = MAILBOX_HBA_EXT_OFFSET;
5392
5393 /* Copy the mailbox extension data */
5394 if (pmbox->in_ext_byte_len && pmbox->context2) {
5395 lpfc_memcpy_to_slim(phba->MBslimaddr +
5396 MAILBOX_HBA_EXT_OFFSET,
5397 pmbox->context2, pmbox->in_ext_byte_len);
5398
5399 }
James Smart92908312006-03-07 15:04:13 -05005400 if (mb->mbxCommand == MBX_CONFIG_PORT) {
dea31012005-04-17 16:05:31 -05005401 /* copy command data into host mbox for cmpl */
James Smart34b02dc2008-08-24 21:49:55 -04005402 lpfc_sli_pcimem_bcopy(mb, phba->mbox, MAILBOX_CMD_SIZE);
dea31012005-04-17 16:05:31 -05005403 }
5404
5405 /* First copy mbox command data to HBA SLIM, skip past first
5406 word */
5407 to_slim = phba->MBslimaddr + sizeof (uint32_t);
5408 lpfc_memcpy_to_slim(to_slim, &mb->un.varWords[0],
5409 MAILBOX_CMD_SIZE - sizeof (uint32_t));
5410
5411 /* Next copy over first word, with mbxOwner set */
James Smart34b02dc2008-08-24 21:49:55 -04005412 ldata = *((uint32_t *)mb);
dea31012005-04-17 16:05:31 -05005413 to_slim = phba->MBslimaddr;
5414 writel(ldata, to_slim);
5415 readl(to_slim); /* flush */
5416
5417 if (mb->mbxCommand == MBX_CONFIG_PORT) {
5418 /* switch over to host mailbox */
James Smart3772a992009-05-22 14:50:54 -04005419 psli->sli_flag |= LPFC_SLI_ACTIVE;
dea31012005-04-17 16:05:31 -05005420 }
5421 }
5422
5423 wmb();
dea31012005-04-17 16:05:31 -05005424
5425 switch (flag) {
5426 case MBX_NOWAIT:
James Smart09372822008-01-11 01:52:54 -05005427 /* Set up reference to mailbox command */
dea31012005-04-17 16:05:31 -05005428 psli->mbox_active = pmbox;
James Smart09372822008-01-11 01:52:54 -05005429 /* Interrupt board to do it */
5430 writel(CA_MBATT, phba->CAregaddr);
5431 readl(phba->CAregaddr); /* flush */
5432 /* Don't wait for it to finish, just return */
dea31012005-04-17 16:05:31 -05005433 break;
5434
5435 case MBX_POLL:
James Smart09372822008-01-11 01:52:54 -05005436 /* Set up null reference to mailbox command */
dea31012005-04-17 16:05:31 -05005437 psli->mbox_active = NULL;
James Smart09372822008-01-11 01:52:54 -05005438 /* Interrupt board to do it */
5439 writel(CA_MBATT, phba->CAregaddr);
5440 readl(phba->CAregaddr); /* flush */
5441
James Smart3772a992009-05-22 14:50:54 -04005442 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05005443 /* First read mbox status word */
James Smart34b02dc2008-08-24 21:49:55 -04005444 word0 = *((uint32_t *)phba->mbox);
dea31012005-04-17 16:05:31 -05005445 word0 = le32_to_cpu(word0);
5446 } else {
5447 /* First read mbox status word */
James Smart9940b972011-03-11 16:06:12 -05005448 if (lpfc_readl(phba->MBslimaddr, &word0)) {
5449 spin_unlock_irqrestore(&phba->hbalock,
5450 drvr_flag);
5451 goto out_not_finished;
5452 }
dea31012005-04-17 16:05:31 -05005453 }
5454
5455 /* Read the HBA Host Attention Register */
James Smart9940b972011-03-11 16:06:12 -05005456 if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
5457 spin_unlock_irqrestore(&phba->hbalock,
5458 drvr_flag);
5459 goto out_not_finished;
5460 }
James Smart09372822008-01-11 01:52:54 -05005461 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
5462 mb->mbxCommand) *
5463 1000) + jiffies;
5464 i = 0;
dea31012005-04-17 16:05:31 -05005465 /* Wait for command to complete */
Jamie Wellnitz41415862006-02-28 19:25:27 -05005466 while (((word0 & OWN_CHIP) == OWN_CHIP) ||
5467 (!(ha_copy & HA_MBATT) &&
James Smart2e0fef82007-06-17 19:56:36 -05005468 (phba->link_state > LPFC_WARM_START))) {
James Smart09372822008-01-11 01:52:54 -05005469 if (time_after(jiffies, timeout)) {
dea31012005-04-17 16:05:31 -05005470 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05005471 spin_unlock_irqrestore(&phba->hbalock,
dea31012005-04-17 16:05:31 -05005472 drvr_flag);
James Smart58da1ff2008-04-07 10:15:56 -04005473 goto out_not_finished;
dea31012005-04-17 16:05:31 -05005474 }
5475
5476 /* Check if we took a mbox interrupt while we were
5477 polling */
5478 if (((word0 & OWN_CHIP) != OWN_CHIP)
5479 && (evtctr != psli->slistat.mbox_event))
5480 break;
5481
James Smart09372822008-01-11 01:52:54 -05005482 if (i++ > 10) {
5483 spin_unlock_irqrestore(&phba->hbalock,
5484 drvr_flag);
5485 msleep(1);
5486 spin_lock_irqsave(&phba->hbalock, drvr_flag);
5487 }
dea31012005-04-17 16:05:31 -05005488
James Smart3772a992009-05-22 14:50:54 -04005489 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05005490 /* First copy command data */
James Smart34b02dc2008-08-24 21:49:55 -04005491 word0 = *((uint32_t *)phba->mbox);
dea31012005-04-17 16:05:31 -05005492 word0 = le32_to_cpu(word0);
5493 if (mb->mbxCommand == MBX_CONFIG_PORT) {
5494 MAILBOX_t *slimmb;
James Smart34b02dc2008-08-24 21:49:55 -04005495 uint32_t slimword0;
dea31012005-04-17 16:05:31 -05005496 /* Check real SLIM for any errors */
5497 slimword0 = readl(phba->MBslimaddr);
5498 slimmb = (MAILBOX_t *) & slimword0;
5499 if (((slimword0 & OWN_CHIP) != OWN_CHIP)
5500 && slimmb->mbxStatus) {
5501 psli->sli_flag &=
James Smart3772a992009-05-22 14:50:54 -04005502 ~LPFC_SLI_ACTIVE;
dea31012005-04-17 16:05:31 -05005503 word0 = slimword0;
5504 }
5505 }
5506 } else {
5507 /* First copy command data */
5508 word0 = readl(phba->MBslimaddr);
5509 }
5510 /* Read the HBA Host Attention Register */
James Smart9940b972011-03-11 16:06:12 -05005511 if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
5512 spin_unlock_irqrestore(&phba->hbalock,
5513 drvr_flag);
5514 goto out_not_finished;
5515 }
dea31012005-04-17 16:05:31 -05005516 }
5517
James Smart3772a992009-05-22 14:50:54 -04005518 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05005519 /* copy results back to user */
James Smart34b02dc2008-08-24 21:49:55 -04005520 lpfc_sli_pcimem_bcopy(phba->mbox, mb, MAILBOX_CMD_SIZE);
James Smart7a470272010-03-15 11:25:20 -04005521 /* Copy the mailbox extension data */
5522 if (pmbox->out_ext_byte_len && pmbox->context2) {
5523 lpfc_sli_pcimem_bcopy(phba->mbox_ext,
5524 pmbox->context2,
5525 pmbox->out_ext_byte_len);
5526 }
dea31012005-04-17 16:05:31 -05005527 } else {
5528 /* First copy command data */
5529 lpfc_memcpy_from_slim(mb, phba->MBslimaddr,
5530 MAILBOX_CMD_SIZE);
James Smart7a470272010-03-15 11:25:20 -04005531 /* Copy the mailbox extension data */
5532 if (pmbox->out_ext_byte_len && pmbox->context2) {
5533 lpfc_memcpy_from_slim(pmbox->context2,
5534 phba->MBslimaddr +
5535 MAILBOX_HBA_EXT_OFFSET,
5536 pmbox->out_ext_byte_len);
dea31012005-04-17 16:05:31 -05005537 }
5538 }
5539
5540 writel(HA_MBATT, phba->HAregaddr);
5541 readl(phba->HAregaddr); /* flush */
5542
5543 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
5544 status = mb->mbxStatus;
5545 }
5546
James Smart2e0fef82007-06-17 19:56:36 -05005547 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
5548 return status;
James Smart58da1ff2008-04-07 10:15:56 -04005549
5550out_not_finished:
5551 if (processing_queue) {
James Smartda0436e2009-05-22 14:51:39 -04005552 pmbox->u.mb.mbxStatus = MBX_NOT_FINISHED;
James Smart58da1ff2008-04-07 10:15:56 -04005553 lpfc_mbox_cmpl_put(phba, pmbox);
5554 }
5555 return MBX_NOT_FINISHED;
dea31012005-04-17 16:05:31 -05005556}
5557
James Smarte59058c2008-08-24 21:49:00 -04005558/**
James Smartf1126682009-06-10 17:22:44 -04005559 * lpfc_sli4_async_mbox_block - Block posting SLI4 asynchronous mailbox command
5560 * @phba: Pointer to HBA context object.
5561 *
5562 * The function blocks the posting of SLI4 asynchronous mailbox commands from
5563 * the driver internal pending mailbox queue. It will then try to wait out the
5564 * possible outstanding mailbox command before return.
5565 *
5566 * Returns:
5567 * 0 - the outstanding mailbox command completed; otherwise, the wait for
5568 * the outstanding mailbox command timed out.
5569 **/
5570static int
5571lpfc_sli4_async_mbox_block(struct lpfc_hba *phba)
5572{
5573 struct lpfc_sli *psli = &phba->sli;
5574 uint8_t actcmd = MBX_HEARTBEAT;
5575 int rc = 0;
5576 unsigned long timeout;
5577
5578 /* Mark the asynchronous mailbox command posting as blocked */
5579 spin_lock_irq(&phba->hbalock);
5580 psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
5581 if (phba->sli.mbox_active)
5582 actcmd = phba->sli.mbox_active->u.mb.mbxCommand;
5583 spin_unlock_irq(&phba->hbalock);
5584 /* Determine how long we might wait for the active mailbox
5585 * command to be gracefully completed by firmware.
5586 */
5587 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, actcmd) * 1000) +
5588 jiffies;
5589 /* Wait for the outstnading mailbox command to complete */
5590 while (phba->sli.mbox_active) {
5591 /* Check active mailbox complete status every 2ms */
5592 msleep(2);
5593 if (time_after(jiffies, timeout)) {
5594 /* Timeout, marked the outstanding cmd not complete */
5595 rc = 1;
5596 break;
5597 }
5598 }
5599
5600 /* Can not cleanly block async mailbox command, fails it */
5601 if (rc) {
5602 spin_lock_irq(&phba->hbalock);
5603 psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
5604 spin_unlock_irq(&phba->hbalock);
5605 }
5606 return rc;
5607}
5608
5609/**
5610 * lpfc_sli4_async_mbox_unblock - Block posting SLI4 async mailbox command
5611 * @phba: Pointer to HBA context object.
5612 *
5613 * The function unblocks and resume posting of SLI4 asynchronous mailbox
5614 * commands from the driver internal pending mailbox queue. It makes sure
5615 * that there is no outstanding mailbox command before resuming posting
5616 * asynchronous mailbox commands. If, for any reason, there is outstanding
5617 * mailbox command, it will try to wait it out before resuming asynchronous
5618 * mailbox command posting.
5619 **/
5620static void
5621lpfc_sli4_async_mbox_unblock(struct lpfc_hba *phba)
5622{
5623 struct lpfc_sli *psli = &phba->sli;
5624
5625 spin_lock_irq(&phba->hbalock);
5626 if (!(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
5627 /* Asynchronous mailbox posting is not blocked, do nothing */
5628 spin_unlock_irq(&phba->hbalock);
5629 return;
5630 }
5631
5632 /* Outstanding synchronous mailbox command is guaranteed to be done,
5633 * successful or timeout, after timing-out the outstanding mailbox
5634 * command shall always be removed, so just unblock posting async
5635 * mailbox command and resume
5636 */
5637 psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
5638 spin_unlock_irq(&phba->hbalock);
5639
5640 /* wake up worker thread to post asynchronlous mailbox command */
5641 lpfc_worker_wake_up(phba);
5642}
5643
5644/**
James Smartda0436e2009-05-22 14:51:39 -04005645 * lpfc_sli4_post_sync_mbox - Post an SLI4 mailbox to the bootstrap mailbox
5646 * @phba: Pointer to HBA context object.
5647 * @mboxq: Pointer to mailbox object.
5648 *
5649 * The function posts a mailbox to the port. The mailbox is expected
5650 * to be comletely filled in and ready for the port to operate on it.
5651 * This routine executes a synchronous completion operation on the
5652 * mailbox by polling for its completion.
5653 *
5654 * The caller must not be holding any locks when calling this routine.
5655 *
5656 * Returns:
5657 * MBX_SUCCESS - mailbox posted successfully
5658 * Any of the MBX error values.
5659 **/
5660static int
5661lpfc_sli4_post_sync_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
5662{
5663 int rc = MBX_SUCCESS;
5664 unsigned long iflag;
5665 uint32_t db_ready;
5666 uint32_t mcqe_status;
5667 uint32_t mbx_cmnd;
5668 unsigned long timeout;
5669 struct lpfc_sli *psli = &phba->sli;
5670 struct lpfc_mqe *mb = &mboxq->u.mqe;
5671 struct lpfc_bmbx_create *mbox_rgn;
5672 struct dma_address *dma_address;
5673 struct lpfc_register bmbx_reg;
5674
5675 /*
5676 * Only one mailbox can be active to the bootstrap mailbox region
5677 * at a time and there is no queueing provided.
5678 */
5679 spin_lock_irqsave(&phba->hbalock, iflag);
5680 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
5681 spin_unlock_irqrestore(&phba->hbalock, iflag);
5682 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5683 "(%d):2532 Mailbox command x%x (x%x) "
5684 "cannot issue Data: x%x x%x\n",
5685 mboxq->vport ? mboxq->vport->vpi : 0,
5686 mboxq->u.mb.mbxCommand,
5687 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5688 psli->sli_flag, MBX_POLL);
5689 return MBXERR_ERROR;
5690 }
5691 /* The server grabs the token and owns it until release */
5692 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
5693 phba->sli.mbox_active = mboxq;
5694 spin_unlock_irqrestore(&phba->hbalock, iflag);
5695
5696 /*
5697 * Initialize the bootstrap memory region to avoid stale data areas
5698 * in the mailbox post. Then copy the caller's mailbox contents to
5699 * the bmbx mailbox region.
5700 */
5701 mbx_cmnd = bf_get(lpfc_mqe_command, mb);
5702 memset(phba->sli4_hba.bmbx.avirt, 0, sizeof(struct lpfc_bmbx_create));
5703 lpfc_sli_pcimem_bcopy(mb, phba->sli4_hba.bmbx.avirt,
5704 sizeof(struct lpfc_mqe));
5705
5706 /* Post the high mailbox dma address to the port and wait for ready. */
5707 dma_address = &phba->sli4_hba.bmbx.dma_address;
5708 writel(dma_address->addr_hi, phba->sli4_hba.BMBXregaddr);
5709
5710 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mbx_cmnd)
5711 * 1000) + jiffies;
5712 do {
5713 bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
5714 db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
5715 if (!db_ready)
5716 msleep(2);
5717
5718 if (time_after(jiffies, timeout)) {
5719 rc = MBXERR_ERROR;
5720 goto exit;
5721 }
5722 } while (!db_ready);
5723
5724 /* Post the low mailbox dma address to the port. */
5725 writel(dma_address->addr_lo, phba->sli4_hba.BMBXregaddr);
5726 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mbx_cmnd)
5727 * 1000) + jiffies;
5728 do {
5729 bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
5730 db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
5731 if (!db_ready)
5732 msleep(2);
5733
5734 if (time_after(jiffies, timeout)) {
5735 rc = MBXERR_ERROR;
5736 goto exit;
5737 }
5738 } while (!db_ready);
5739
5740 /*
5741 * Read the CQ to ensure the mailbox has completed.
5742 * If so, update the mailbox status so that the upper layers
5743 * can complete the request normally.
5744 */
5745 lpfc_sli_pcimem_bcopy(phba->sli4_hba.bmbx.avirt, mb,
5746 sizeof(struct lpfc_mqe));
5747 mbox_rgn = (struct lpfc_bmbx_create *) phba->sli4_hba.bmbx.avirt;
5748 lpfc_sli_pcimem_bcopy(&mbox_rgn->mcqe, &mboxq->mcqe,
5749 sizeof(struct lpfc_mcqe));
5750 mcqe_status = bf_get(lpfc_mcqe_status, &mbox_rgn->mcqe);
5751
5752 /* Prefix the mailbox status with range x4000 to note SLI4 status. */
5753 if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
5754 bf_set(lpfc_mqe_status, mb, LPFC_MBX_ERROR_RANGE | mcqe_status);
5755 rc = MBXERR_ERROR;
James Smartd7c47992010-06-08 18:31:54 -04005756 } else
5757 lpfc_sli4_swap_str(phba, mboxq);
James Smartda0436e2009-05-22 14:51:39 -04005758
5759 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
5760 "(%d):0356 Mailbox cmd x%x (x%x) Status x%x "
5761 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x"
5762 " x%x x%x CQ: x%x x%x x%x x%x\n",
5763 mboxq->vport ? mboxq->vport->vpi : 0,
5764 mbx_cmnd, lpfc_sli4_mbox_opcode_get(phba, mboxq),
5765 bf_get(lpfc_mqe_status, mb),
5766 mb->un.mb_words[0], mb->un.mb_words[1],
5767 mb->un.mb_words[2], mb->un.mb_words[3],
5768 mb->un.mb_words[4], mb->un.mb_words[5],
5769 mb->un.mb_words[6], mb->un.mb_words[7],
5770 mb->un.mb_words[8], mb->un.mb_words[9],
5771 mb->un.mb_words[10], mb->un.mb_words[11],
5772 mb->un.mb_words[12], mboxq->mcqe.word0,
5773 mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
5774 mboxq->mcqe.trailer);
5775exit:
5776 /* We are holding the token, no needed for lock when release */
5777 spin_lock_irqsave(&phba->hbalock, iflag);
5778 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
5779 phba->sli.mbox_active = NULL;
5780 spin_unlock_irqrestore(&phba->hbalock, iflag);
5781 return rc;
5782}
5783
5784/**
5785 * lpfc_sli_issue_mbox_s4 - Issue an SLI4 mailbox command to firmware
5786 * @phba: Pointer to HBA context object.
5787 * @pmbox: Pointer to mailbox object.
5788 * @flag: Flag indicating how the mailbox need to be processed.
5789 *
5790 * This function is called by discovery code and HBA management code to submit
5791 * a mailbox command to firmware with SLI-4 interface spec.
5792 *
5793 * Return codes the caller owns the mailbox command after the return of the
5794 * function.
5795 **/
5796static int
5797lpfc_sli_issue_mbox_s4(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
5798 uint32_t flag)
5799{
5800 struct lpfc_sli *psli = &phba->sli;
5801 unsigned long iflags;
5802 int rc;
5803
James Smart8fa38512009-07-19 10:01:03 -04005804 rc = lpfc_mbox_dev_check(phba);
5805 if (unlikely(rc)) {
5806 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5807 "(%d):2544 Mailbox command x%x (x%x) "
5808 "cannot issue Data: x%x x%x\n",
5809 mboxq->vport ? mboxq->vport->vpi : 0,
5810 mboxq->u.mb.mbxCommand,
5811 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5812 psli->sli_flag, flag);
5813 goto out_not_finished;
5814 }
5815
James Smartda0436e2009-05-22 14:51:39 -04005816 /* Detect polling mode and jump to a handler */
5817 if (!phba->sli4_hba.intr_enable) {
5818 if (flag == MBX_POLL)
5819 rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
5820 else
5821 rc = -EIO;
5822 if (rc != MBX_SUCCESS)
5823 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5824 "(%d):2541 Mailbox command x%x "
5825 "(x%x) cannot issue Data: x%x x%x\n",
5826 mboxq->vport ? mboxq->vport->vpi : 0,
5827 mboxq->u.mb.mbxCommand,
5828 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5829 psli->sli_flag, flag);
5830 return rc;
5831 } else if (flag == MBX_POLL) {
James Smartf1126682009-06-10 17:22:44 -04005832 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
5833 "(%d):2542 Try to issue mailbox command "
5834 "x%x (x%x) synchronously ahead of async"
5835 "mailbox command queue: x%x x%x\n",
James Smartda0436e2009-05-22 14:51:39 -04005836 mboxq->vport ? mboxq->vport->vpi : 0,
5837 mboxq->u.mb.mbxCommand,
5838 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5839 psli->sli_flag, flag);
James Smartf1126682009-06-10 17:22:44 -04005840 /* Try to block the asynchronous mailbox posting */
5841 rc = lpfc_sli4_async_mbox_block(phba);
5842 if (!rc) {
5843 /* Successfully blocked, now issue sync mbox cmd */
5844 rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
5845 if (rc != MBX_SUCCESS)
5846 lpfc_printf_log(phba, KERN_ERR,
5847 LOG_MBOX | LOG_SLI,
5848 "(%d):2597 Mailbox command "
5849 "x%x (x%x) cannot issue "
5850 "Data: x%x x%x\n",
5851 mboxq->vport ?
5852 mboxq->vport->vpi : 0,
5853 mboxq->u.mb.mbxCommand,
5854 lpfc_sli4_mbox_opcode_get(phba,
5855 mboxq),
5856 psli->sli_flag, flag);
5857 /* Unblock the async mailbox posting afterward */
5858 lpfc_sli4_async_mbox_unblock(phba);
5859 }
5860 return rc;
James Smartda0436e2009-05-22 14:51:39 -04005861 }
5862
5863 /* Now, interrupt mode asynchrous mailbox command */
5864 rc = lpfc_mbox_cmd_check(phba, mboxq);
5865 if (rc) {
5866 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5867 "(%d):2543 Mailbox command x%x (x%x) "
5868 "cannot issue Data: x%x x%x\n",
5869 mboxq->vport ? mboxq->vport->vpi : 0,
5870 mboxq->u.mb.mbxCommand,
5871 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5872 psli->sli_flag, flag);
5873 goto out_not_finished;
5874 }
James Smartda0436e2009-05-22 14:51:39 -04005875
5876 /* Put the mailbox command to the driver internal FIFO */
5877 psli->slistat.mbox_busy++;
5878 spin_lock_irqsave(&phba->hbalock, iflags);
5879 lpfc_mbox_put(phba, mboxq);
5880 spin_unlock_irqrestore(&phba->hbalock, iflags);
5881 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
5882 "(%d):0354 Mbox cmd issue - Enqueue Data: "
5883 "x%x (x%x) x%x x%x x%x\n",
5884 mboxq->vport ? mboxq->vport->vpi : 0xffffff,
5885 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
5886 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5887 phba->pport->port_state,
5888 psli->sli_flag, MBX_NOWAIT);
5889 /* Wake up worker thread to transport mailbox command from head */
5890 lpfc_worker_wake_up(phba);
5891
5892 return MBX_BUSY;
5893
5894out_not_finished:
5895 return MBX_NOT_FINISHED;
5896}
5897
5898/**
5899 * lpfc_sli4_post_async_mbox - Post an SLI4 mailbox command to device
5900 * @phba: Pointer to HBA context object.
5901 *
5902 * This function is called by worker thread to send a mailbox command to
5903 * SLI4 HBA firmware.
5904 *
5905 **/
5906int
5907lpfc_sli4_post_async_mbox(struct lpfc_hba *phba)
5908{
5909 struct lpfc_sli *psli = &phba->sli;
5910 LPFC_MBOXQ_t *mboxq;
5911 int rc = MBX_SUCCESS;
5912 unsigned long iflags;
5913 struct lpfc_mqe *mqe;
5914 uint32_t mbx_cmnd;
5915
5916 /* Check interrupt mode before post async mailbox command */
5917 if (unlikely(!phba->sli4_hba.intr_enable))
5918 return MBX_NOT_FINISHED;
5919
5920 /* Check for mailbox command service token */
5921 spin_lock_irqsave(&phba->hbalock, iflags);
5922 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
5923 spin_unlock_irqrestore(&phba->hbalock, iflags);
5924 return MBX_NOT_FINISHED;
5925 }
5926 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
5927 spin_unlock_irqrestore(&phba->hbalock, iflags);
5928 return MBX_NOT_FINISHED;
5929 }
5930 if (unlikely(phba->sli.mbox_active)) {
5931 spin_unlock_irqrestore(&phba->hbalock, iflags);
5932 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5933 "0384 There is pending active mailbox cmd\n");
5934 return MBX_NOT_FINISHED;
5935 }
5936 /* Take the mailbox command service token */
5937 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
5938
5939 /* Get the next mailbox command from head of queue */
5940 mboxq = lpfc_mbox_get(phba);
5941
5942 /* If no more mailbox command waiting for post, we're done */
5943 if (!mboxq) {
5944 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
5945 spin_unlock_irqrestore(&phba->hbalock, iflags);
5946 return MBX_SUCCESS;
5947 }
5948 phba->sli.mbox_active = mboxq;
5949 spin_unlock_irqrestore(&phba->hbalock, iflags);
5950
5951 /* Check device readiness for posting mailbox command */
5952 rc = lpfc_mbox_dev_check(phba);
5953 if (unlikely(rc))
5954 /* Driver clean routine will clean up pending mailbox */
5955 goto out_not_finished;
5956
5957 /* Prepare the mbox command to be posted */
5958 mqe = &mboxq->u.mqe;
5959 mbx_cmnd = bf_get(lpfc_mqe_command, mqe);
5960
5961 /* Start timer for the mbox_tmo and log some mailbox post messages */
5962 mod_timer(&psli->mbox_tmo, (jiffies +
5963 (HZ * lpfc_mbox_tmo_val(phba, mbx_cmnd))));
5964
5965 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
5966 "(%d):0355 Mailbox cmd x%x (x%x) issue Data: "
5967 "x%x x%x\n",
5968 mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
5969 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5970 phba->pport->port_state, psli->sli_flag);
5971
5972 if (mbx_cmnd != MBX_HEARTBEAT) {
5973 if (mboxq->vport) {
5974 lpfc_debugfs_disc_trc(mboxq->vport,
5975 LPFC_DISC_TRC_MBOX_VPORT,
5976 "MBOX Send vport: cmd:x%x mb:x%x x%x",
5977 mbx_cmnd, mqe->un.mb_words[0],
5978 mqe->un.mb_words[1]);
5979 } else {
5980 lpfc_debugfs_disc_trc(phba->pport,
5981 LPFC_DISC_TRC_MBOX,
5982 "MBOX Send: cmd:x%x mb:x%x x%x",
5983 mbx_cmnd, mqe->un.mb_words[0],
5984 mqe->un.mb_words[1]);
5985 }
5986 }
5987 psli->slistat.mbox_cmd++;
5988
5989 /* Post the mailbox command to the port */
5990 rc = lpfc_sli4_mq_put(phba->sli4_hba.mbx_wq, mqe);
5991 if (rc != MBX_SUCCESS) {
5992 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5993 "(%d):2533 Mailbox command x%x (x%x) "
5994 "cannot issue Data: x%x x%x\n",
5995 mboxq->vport ? mboxq->vport->vpi : 0,
5996 mboxq->u.mb.mbxCommand,
5997 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5998 psli->sli_flag, MBX_NOWAIT);
5999 goto out_not_finished;
6000 }
6001
6002 return rc;
6003
6004out_not_finished:
6005 spin_lock_irqsave(&phba->hbalock, iflags);
6006 mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
6007 __lpfc_mbox_cmpl_put(phba, mboxq);
6008 /* Release the token */
6009 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
6010 phba->sli.mbox_active = NULL;
6011 spin_unlock_irqrestore(&phba->hbalock, iflags);
6012
6013 return MBX_NOT_FINISHED;
6014}
6015
6016/**
6017 * lpfc_sli_issue_mbox - Wrapper func for issuing mailbox command
6018 * @phba: Pointer to HBA context object.
6019 * @pmbox: Pointer to mailbox object.
6020 * @flag: Flag indicating how the mailbox need to be processed.
6021 *
6022 * This routine wraps the actual SLI3 or SLI4 mailbox issuing routine from
6023 * the API jump table function pointer from the lpfc_hba struct.
6024 *
6025 * Return codes the caller owns the mailbox command after the return of the
6026 * function.
6027 **/
6028int
6029lpfc_sli_issue_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox, uint32_t flag)
6030{
6031 return phba->lpfc_sli_issue_mbox(phba, pmbox, flag);
6032}
6033
6034/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03006035 * lpfc_mbox_api_table_setup - Set up mbox api function jump table
James Smartda0436e2009-05-22 14:51:39 -04006036 * @phba: The hba struct for which this call is being executed.
6037 * @dev_grp: The HBA PCI-Device group number.
6038 *
6039 * This routine sets up the mbox interface API function jump table in @phba
6040 * struct.
6041 * Returns: 0 - success, -ENODEV - failure.
6042 **/
6043int
6044lpfc_mbox_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
6045{
6046
6047 switch (dev_grp) {
6048 case LPFC_PCI_DEV_LP:
6049 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s3;
6050 phba->lpfc_sli_handle_slow_ring_event =
6051 lpfc_sli_handle_slow_ring_event_s3;
6052 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s3;
6053 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s3;
6054 phba->lpfc_sli_brdready = lpfc_sli_brdready_s3;
6055 break;
6056 case LPFC_PCI_DEV_OC:
6057 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s4;
6058 phba->lpfc_sli_handle_slow_ring_event =
6059 lpfc_sli_handle_slow_ring_event_s4;
6060 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s4;
6061 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s4;
6062 phba->lpfc_sli_brdready = lpfc_sli_brdready_s4;
6063 break;
6064 default:
6065 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6066 "1420 Invalid HBA PCI-device group: 0x%x\n",
6067 dev_grp);
6068 return -ENODEV;
6069 break;
6070 }
6071 return 0;
6072}
6073
6074/**
James Smart3621a712009-04-06 18:47:14 -04006075 * __lpfc_sli_ringtx_put - Add an iocb to the txq
James Smarte59058c2008-08-24 21:49:00 -04006076 * @phba: Pointer to HBA context object.
6077 * @pring: Pointer to driver SLI ring object.
6078 * @piocb: Pointer to address of newly added command iocb.
6079 *
6080 * This function is called with hbalock held to add a command
6081 * iocb to the txq when SLI layer cannot submit the command iocb
6082 * to the ring.
6083 **/
James Smart2a9bf3d2010-06-07 15:24:45 -04006084void
James Smart92d7f7b2007-06-17 19:56:38 -05006085__lpfc_sli_ringtx_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -05006086 struct lpfc_iocbq *piocb)
dea31012005-04-17 16:05:31 -05006087{
6088 /* Insert the caller's iocb in the txq tail for later processing. */
6089 list_add_tail(&piocb->list, &pring->txq);
6090 pring->txq_cnt++;
dea31012005-04-17 16:05:31 -05006091}
6092
James Smarte59058c2008-08-24 21:49:00 -04006093/**
James Smart3621a712009-04-06 18:47:14 -04006094 * lpfc_sli_next_iocb - Get the next iocb in the txq
James Smarte59058c2008-08-24 21:49:00 -04006095 * @phba: Pointer to HBA context object.
6096 * @pring: Pointer to driver SLI ring object.
6097 * @piocb: Pointer to address of newly added command iocb.
6098 *
6099 * This function is called with hbalock held before a new
6100 * iocb is submitted to the firmware. This function checks
6101 * txq to flush the iocbs in txq to Firmware before
6102 * submitting new iocbs to the Firmware.
6103 * If there are iocbs in the txq which need to be submitted
6104 * to firmware, lpfc_sli_next_iocb returns the first element
6105 * of the txq after dequeuing it from txq.
6106 * If there is no iocb in the txq then the function will return
6107 * *piocb and *piocb is set to NULL. Caller needs to check
6108 * *piocb to find if there are more commands in the txq.
6109 **/
dea31012005-04-17 16:05:31 -05006110static struct lpfc_iocbq *
6111lpfc_sli_next_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -05006112 struct lpfc_iocbq **piocb)
dea31012005-04-17 16:05:31 -05006113{
6114 struct lpfc_iocbq * nextiocb;
6115
6116 nextiocb = lpfc_sli_ringtx_get(phba, pring);
6117 if (!nextiocb) {
6118 nextiocb = *piocb;
6119 *piocb = NULL;
6120 }
6121
6122 return nextiocb;
6123}
6124
James Smarte59058c2008-08-24 21:49:00 -04006125/**
James Smart3772a992009-05-22 14:50:54 -04006126 * __lpfc_sli_issue_iocb_s3 - SLI3 device lockless ver of lpfc_sli_issue_iocb
James Smarte59058c2008-08-24 21:49:00 -04006127 * @phba: Pointer to HBA context object.
James Smart3772a992009-05-22 14:50:54 -04006128 * @ring_number: SLI ring number to issue iocb on.
James Smarte59058c2008-08-24 21:49:00 -04006129 * @piocb: Pointer to command iocb.
6130 * @flag: Flag indicating if this command can be put into txq.
6131 *
James Smart3772a992009-05-22 14:50:54 -04006132 * __lpfc_sli_issue_iocb_s3 is used by other functions in the driver to issue
6133 * an iocb command to an HBA with SLI-3 interface spec. If the PCI slot is
6134 * recovering from error state, if HBA is resetting or if LPFC_STOP_IOCB_EVENT
6135 * flag is turned on, the function returns IOCB_ERROR. When the link is down,
6136 * this function allows only iocbs for posting buffers. This function finds
6137 * next available slot in the command ring and posts the command to the
6138 * available slot and writes the port attention register to request HBA start
6139 * processing new iocb. If there is no slot available in the ring and
6140 * flag & SLI_IOCB_RET_IOCB is set, the new iocb is added to the txq, otherwise
6141 * the function returns IOCB_BUSY.
James Smarte59058c2008-08-24 21:49:00 -04006142 *
James Smart3772a992009-05-22 14:50:54 -04006143 * This function is called with hbalock held. The function will return success
6144 * after it successfully submit the iocb to firmware or after adding to the
6145 * txq.
James Smarte59058c2008-08-24 21:49:00 -04006146 **/
James Smart98c9ea52007-10-27 13:37:33 -04006147static int
James Smart3772a992009-05-22 14:50:54 -04006148__lpfc_sli_issue_iocb_s3(struct lpfc_hba *phba, uint32_t ring_number,
dea31012005-04-17 16:05:31 -05006149 struct lpfc_iocbq *piocb, uint32_t flag)
6150{
6151 struct lpfc_iocbq *nextiocb;
6152 IOCB_t *iocb;
James Smart3772a992009-05-22 14:50:54 -04006153 struct lpfc_sli_ring *pring = &phba->sli.ring[ring_number];
dea31012005-04-17 16:05:31 -05006154
James Smart92d7f7b2007-06-17 19:56:38 -05006155 if (piocb->iocb_cmpl && (!piocb->vport) &&
6156 (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
6157 (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
6158 lpfc_printf_log(phba, KERN_ERR,
6159 LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04006160 "1807 IOCB x%x failed. No vport\n",
James Smart92d7f7b2007-06-17 19:56:38 -05006161 piocb->iocb.ulpCommand);
6162 dump_stack();
6163 return IOCB_ERROR;
6164 }
6165
6166
Linas Vepstas8d63f372007-02-14 14:28:36 -06006167 /* If the PCI channel is in offline state, do not post iocbs. */
6168 if (unlikely(pci_channel_offline(phba->pcidev)))
6169 return IOCB_ERROR;
6170
James Smarta257bf92009-04-06 18:48:10 -04006171 /* If HBA has a deferred error attention, fail the iocb. */
6172 if (unlikely(phba->hba_flag & DEFER_ERATT))
6173 return IOCB_ERROR;
6174
dea31012005-04-17 16:05:31 -05006175 /*
6176 * We should never get an IOCB if we are in a < LINK_DOWN state
6177 */
James Smart2e0fef82007-06-17 19:56:36 -05006178 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
dea31012005-04-17 16:05:31 -05006179 return IOCB_ERROR;
6180
6181 /*
6182 * Check to see if we are blocking IOCB processing because of a
James Smart0b727fe2007-10-27 13:37:25 -04006183 * outstanding event.
dea31012005-04-17 16:05:31 -05006184 */
James Smart0b727fe2007-10-27 13:37:25 -04006185 if (unlikely(pring->flag & LPFC_STOP_IOCB_EVENT))
dea31012005-04-17 16:05:31 -05006186 goto iocb_busy;
6187
James Smart2e0fef82007-06-17 19:56:36 -05006188 if (unlikely(phba->link_state == LPFC_LINK_DOWN)) {
dea31012005-04-17 16:05:31 -05006189 /*
James Smart2680eea2007-04-25 09:52:55 -04006190 * Only CREATE_XRI, CLOSE_XRI, and QUE_RING_BUF
dea31012005-04-17 16:05:31 -05006191 * can be issued if the link is not up.
6192 */
6193 switch (piocb->iocb.ulpCommand) {
James Smart84774a42008-08-24 21:50:06 -04006194 case CMD_GEN_REQUEST64_CR:
6195 case CMD_GEN_REQUEST64_CX:
6196 if (!(phba->sli.sli_flag & LPFC_MENLO_MAINT) ||
6197 (piocb->iocb.un.genreq64.w5.hcsw.Rctl !=
James Smart6a9c52c2009-10-02 15:16:51 -04006198 FC_RCTL_DD_UNSOL_CMD) ||
James Smart84774a42008-08-24 21:50:06 -04006199 (piocb->iocb.un.genreq64.w5.hcsw.Type !=
6200 MENLO_TRANSPORT_TYPE))
6201
6202 goto iocb_busy;
6203 break;
dea31012005-04-17 16:05:31 -05006204 case CMD_QUE_RING_BUF_CN:
6205 case CMD_QUE_RING_BUF64_CN:
dea31012005-04-17 16:05:31 -05006206 /*
6207 * For IOCBs, like QUE_RING_BUF, that have no rsp ring
6208 * completion, iocb_cmpl MUST be 0.
6209 */
6210 if (piocb->iocb_cmpl)
6211 piocb->iocb_cmpl = NULL;
6212 /*FALLTHROUGH*/
6213 case CMD_CREATE_XRI_CR:
James Smart2680eea2007-04-25 09:52:55 -04006214 case CMD_CLOSE_XRI_CN:
6215 case CMD_CLOSE_XRI_CX:
dea31012005-04-17 16:05:31 -05006216 break;
6217 default:
6218 goto iocb_busy;
6219 }
6220
6221 /*
6222 * For FCP commands, we must be in a state where we can process link
6223 * attention events.
6224 */
6225 } else if (unlikely(pring->ringno == phba->sli.fcp_ring &&
James Smart92d7f7b2007-06-17 19:56:38 -05006226 !(phba->sli.sli_flag & LPFC_PROCESS_LA))) {
dea31012005-04-17 16:05:31 -05006227 goto iocb_busy;
James Smart92d7f7b2007-06-17 19:56:38 -05006228 }
dea31012005-04-17 16:05:31 -05006229
dea31012005-04-17 16:05:31 -05006230 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
6231 (nextiocb = lpfc_sli_next_iocb(phba, pring, &piocb)))
6232 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
6233
6234 if (iocb)
6235 lpfc_sli_update_ring(phba, pring);
6236 else
6237 lpfc_sli_update_full_ring(phba, pring);
6238
6239 if (!piocb)
6240 return IOCB_SUCCESS;
6241
6242 goto out_busy;
6243
6244 iocb_busy:
6245 pring->stats.iocb_cmd_delay++;
6246
6247 out_busy:
6248
6249 if (!(flag & SLI_IOCB_RET_IOCB)) {
James Smart92d7f7b2007-06-17 19:56:38 -05006250 __lpfc_sli_ringtx_put(phba, pring, piocb);
dea31012005-04-17 16:05:31 -05006251 return IOCB_SUCCESS;
6252 }
6253
6254 return IOCB_BUSY;
6255}
6256
James Smart3772a992009-05-22 14:50:54 -04006257/**
James Smart4f774512009-05-22 14:52:35 -04006258 * lpfc_sli4_bpl2sgl - Convert the bpl/bde to a sgl.
6259 * @phba: Pointer to HBA context object.
6260 * @piocb: Pointer to command iocb.
6261 * @sglq: Pointer to the scatter gather queue object.
6262 *
6263 * This routine converts the bpl or bde that is in the IOCB
6264 * to a sgl list for the sli4 hardware. The physical address
6265 * of the bpl/bde is converted back to a virtual address.
6266 * If the IOCB contains a BPL then the list of BDE's is
6267 * converted to sli4_sge's. If the IOCB contains a single
6268 * BDE then it is converted to a single sli_sge.
6269 * The IOCB is still in cpu endianess so the contents of
6270 * the bpl can be used without byte swapping.
6271 *
6272 * Returns valid XRI = Success, NO_XRI = Failure.
6273**/
6274static uint16_t
6275lpfc_sli4_bpl2sgl(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq,
6276 struct lpfc_sglq *sglq)
6277{
6278 uint16_t xritag = NO_XRI;
6279 struct ulp_bde64 *bpl = NULL;
6280 struct ulp_bde64 bde;
6281 struct sli4_sge *sgl = NULL;
6282 IOCB_t *icmd;
6283 int numBdes = 0;
6284 int i = 0;
James Smart63e801c2010-11-20 23:14:19 -05006285 uint32_t offset = 0; /* accumulated offset in the sg request list */
6286 int inbound = 0; /* number of sg reply entries inbound from firmware */
James Smart4f774512009-05-22 14:52:35 -04006287
6288 if (!piocbq || !sglq)
6289 return xritag;
6290
6291 sgl = (struct sli4_sge *)sglq->sgl;
6292 icmd = &piocbq->iocb;
6293 if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
6294 numBdes = icmd->un.genreq64.bdl.bdeSize /
6295 sizeof(struct ulp_bde64);
6296 /* The addrHigh and addrLow fields within the IOCB
6297 * have not been byteswapped yet so there is no
6298 * need to swap them back.
6299 */
6300 bpl = (struct ulp_bde64 *)
6301 ((struct lpfc_dmabuf *)piocbq->context3)->virt;
6302
6303 if (!bpl)
6304 return xritag;
6305
6306 for (i = 0; i < numBdes; i++) {
6307 /* Should already be byte swapped. */
James Smart28baac72010-02-12 14:42:03 -05006308 sgl->addr_hi = bpl->addrHigh;
6309 sgl->addr_lo = bpl->addrLow;
6310
James Smart4f774512009-05-22 14:52:35 -04006311 if ((i+1) == numBdes)
6312 bf_set(lpfc_sli4_sge_last, sgl, 1);
6313 else
6314 bf_set(lpfc_sli4_sge_last, sgl, 0);
James Smart28baac72010-02-12 14:42:03 -05006315 /* swap the size field back to the cpu so we
6316 * can assign it to the sgl.
6317 */
6318 bde.tus.w = le32_to_cpu(bpl->tus.w);
6319 sgl->sge_len = cpu_to_le32(bde.tus.f.bdeSize);
James Smart63e801c2010-11-20 23:14:19 -05006320 /* The offsets in the sgl need to be accumulated
6321 * separately for the request and reply lists.
6322 * The request is always first, the reply follows.
6323 */
6324 if (piocbq->iocb.ulpCommand == CMD_GEN_REQUEST64_CR) {
6325 /* add up the reply sg entries */
6326 if (bpl->tus.f.bdeFlags == BUFF_TYPE_BDE_64I)
6327 inbound++;
6328 /* first inbound? reset the offset */
6329 if (inbound == 1)
6330 offset = 0;
6331 bf_set(lpfc_sli4_sge_offset, sgl, offset);
6332 offset += bde.tus.f.bdeSize;
6333 }
James Smart546fc852011-03-11 16:06:29 -05006334 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart4f774512009-05-22 14:52:35 -04006335 bpl++;
6336 sgl++;
6337 }
6338 } else if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BDE_64) {
6339 /* The addrHigh and addrLow fields of the BDE have not
6340 * been byteswapped yet so they need to be swapped
6341 * before putting them in the sgl.
6342 */
6343 sgl->addr_hi =
6344 cpu_to_le32(icmd->un.genreq64.bdl.addrHigh);
6345 sgl->addr_lo =
6346 cpu_to_le32(icmd->un.genreq64.bdl.addrLow);
James Smart4f774512009-05-22 14:52:35 -04006347 bf_set(lpfc_sli4_sge_last, sgl, 1);
6348 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart28baac72010-02-12 14:42:03 -05006349 sgl->sge_len =
6350 cpu_to_le32(icmd->un.genreq64.bdl.bdeSize);
James Smart4f774512009-05-22 14:52:35 -04006351 }
6352 return sglq->sli4_xritag;
6353}
6354
6355/**
6356 * lpfc_sli4_scmd_to_wqidx_distr - scsi command to SLI4 WQ index distribution
6357 * @phba: Pointer to HBA context object.
James Smart4f774512009-05-22 14:52:35 -04006358 *
James Smarta93ff372010-10-22 11:06:08 -04006359 * This routine performs a roundrobin SCSI command to SLI4 FCP WQ index
James Smart8fa38512009-07-19 10:01:03 -04006360 * distribution. This is called by __lpfc_sli_issue_iocb_s4() with the hbalock
6361 * held.
James Smart4f774512009-05-22 14:52:35 -04006362 *
6363 * Return: index into SLI4 fast-path FCP queue index.
6364 **/
6365static uint32_t
James Smart8fa38512009-07-19 10:01:03 -04006366lpfc_sli4_scmd_to_wqidx_distr(struct lpfc_hba *phba)
James Smart4f774512009-05-22 14:52:35 -04006367{
James Smart8fa38512009-07-19 10:01:03 -04006368 ++phba->fcp_qidx;
6369 if (phba->fcp_qidx >= phba->cfg_fcp_wq_count)
6370 phba->fcp_qidx = 0;
James Smart4f774512009-05-22 14:52:35 -04006371
James Smart8fa38512009-07-19 10:01:03 -04006372 return phba->fcp_qidx;
James Smart4f774512009-05-22 14:52:35 -04006373}
6374
6375/**
6376 * lpfc_sli_iocb2wqe - Convert the IOCB to a work queue entry.
6377 * @phba: Pointer to HBA context object.
6378 * @piocb: Pointer to command iocb.
6379 * @wqe: Pointer to the work queue entry.
6380 *
6381 * This routine converts the iocb command to its Work Queue Entry
6382 * equivalent. The wqe pointer should not have any fields set when
6383 * this routine is called because it will memcpy over them.
6384 * This routine does not set the CQ_ID or the WQEC bits in the
6385 * wqe.
6386 *
6387 * Returns: 0 = Success, IOCB_ERROR = Failure.
6388 **/
6389static int
6390lpfc_sli4_iocb2wqe(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq,
6391 union lpfc_wqe *wqe)
6392{
James Smart5ffc2662009-11-18 15:39:44 -05006393 uint32_t xmit_len = 0, total_len = 0;
James Smart4f774512009-05-22 14:52:35 -04006394 uint8_t ct = 0;
6395 uint32_t fip;
6396 uint32_t abort_tag;
6397 uint8_t command_type = ELS_COMMAND_NON_FIP;
6398 uint8_t cmnd;
6399 uint16_t xritag;
James Smartdcf2a4e2010-09-29 11:18:53 -04006400 uint16_t abrt_iotag;
6401 struct lpfc_iocbq *abrtiocbq;
James Smart4f774512009-05-22 14:52:35 -04006402 struct ulp_bde64 *bpl = NULL;
James Smartf0d9bcc2010-10-22 11:07:09 -04006403 uint32_t els_id = LPFC_ELS_ID_DEFAULT;
James Smart5ffc2662009-11-18 15:39:44 -05006404 int numBdes, i;
6405 struct ulp_bde64 bde;
James Smart4f774512009-05-22 14:52:35 -04006406
James Smart45ed1192009-10-02 15:17:02 -04006407 fip = phba->hba_flag & HBA_FIP_SUPPORT;
James Smart4f774512009-05-22 14:52:35 -04006408 /* The fcp commands will set command type */
James Smart0c287582009-06-10 17:22:56 -04006409 if (iocbq->iocb_flag & LPFC_IO_FCP)
James Smart4f774512009-05-22 14:52:35 -04006410 command_type = FCP_COMMAND;
James Smartc8685952009-11-18 15:39:16 -05006411 else if (fip && (iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK))
James Smart0c287582009-06-10 17:22:56 -04006412 command_type = ELS_COMMAND_FIP;
6413 else
6414 command_type = ELS_COMMAND_NON_FIP;
6415
James Smart4f774512009-05-22 14:52:35 -04006416 /* Some of the fields are in the right position already */
6417 memcpy(wqe, &iocbq->iocb, sizeof(union lpfc_wqe));
6418 abort_tag = (uint32_t) iocbq->iotag;
6419 xritag = iocbq->sli4_xritag;
James Smartf0d9bcc2010-10-22 11:07:09 -04006420 wqe->generic.wqe_com.word7 = 0; /* The ct field has moved so reset */
James Smart4f774512009-05-22 14:52:35 -04006421 /* words0-2 bpl convert bde */
6422 if (iocbq->iocb.un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
James Smart5ffc2662009-11-18 15:39:44 -05006423 numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
6424 sizeof(struct ulp_bde64);
James Smart4f774512009-05-22 14:52:35 -04006425 bpl = (struct ulp_bde64 *)
6426 ((struct lpfc_dmabuf *)iocbq->context3)->virt;
6427 if (!bpl)
6428 return IOCB_ERROR;
6429
6430 /* Should already be byte swapped. */
6431 wqe->generic.bde.addrHigh = le32_to_cpu(bpl->addrHigh);
6432 wqe->generic.bde.addrLow = le32_to_cpu(bpl->addrLow);
6433 /* swap the size field back to the cpu so we
6434 * can assign it to the sgl.
6435 */
6436 wqe->generic.bde.tus.w = le32_to_cpu(bpl->tus.w);
James Smart5ffc2662009-11-18 15:39:44 -05006437 xmit_len = wqe->generic.bde.tus.f.bdeSize;
6438 total_len = 0;
6439 for (i = 0; i < numBdes; i++) {
6440 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
6441 total_len += bde.tus.f.bdeSize;
6442 }
James Smart4f774512009-05-22 14:52:35 -04006443 } else
James Smart5ffc2662009-11-18 15:39:44 -05006444 xmit_len = iocbq->iocb.un.fcpi64.bdl.bdeSize;
James Smart4f774512009-05-22 14:52:35 -04006445
6446 iocbq->iocb.ulpIoTag = iocbq->iotag;
6447 cmnd = iocbq->iocb.ulpCommand;
6448
6449 switch (iocbq->iocb.ulpCommand) {
6450 case CMD_ELS_REQUEST64_CR:
6451 if (!iocbq->iocb.ulpLe) {
6452 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6453 "2007 Only Limited Edition cmd Format"
6454 " supported 0x%x\n",
6455 iocbq->iocb.ulpCommand);
6456 return IOCB_ERROR;
6457 }
James Smart5ffc2662009-11-18 15:39:44 -05006458 wqe->els_req.payload_len = xmit_len;
James Smart4f774512009-05-22 14:52:35 -04006459 /* Els_reguest64 has a TMO */
6460 bf_set(wqe_tmo, &wqe->els_req.wqe_com,
6461 iocbq->iocb.ulpTimeout);
6462 /* Need a VF for word 4 set the vf bit*/
6463 bf_set(els_req64_vf, &wqe->els_req, 0);
6464 /* And a VFID for word 12 */
6465 bf_set(els_req64_vfid, &wqe->els_req, 0);
James Smart4f774512009-05-22 14:52:35 -04006466 ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
James Smartf0d9bcc2010-10-22 11:07:09 -04006467 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
6468 iocbq->iocb.ulpContext);
6469 bf_set(wqe_ct, &wqe->els_req.wqe_com, ct);
6470 bf_set(wqe_pu, &wqe->els_req.wqe_com, 0);
James Smart4f774512009-05-22 14:52:35 -04006471 /* CCP CCPE PV PRI in word10 were set in the memcpy */
James Smartc8685952009-11-18 15:39:16 -05006472 if (command_type == ELS_COMMAND_FIP) {
6473 els_id = ((iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK)
6474 >> LPFC_FIP_ELS_ID_SHIFT);
6475 }
James Smartf0d9bcc2010-10-22 11:07:09 -04006476 bf_set(wqe_els_id, &wqe->els_req.wqe_com, els_id);
6477 bf_set(wqe_dbde, &wqe->els_req.wqe_com, 1);
6478 bf_set(wqe_iod, &wqe->els_req.wqe_com, LPFC_WQE_IOD_READ);
6479 bf_set(wqe_qosd, &wqe->els_req.wqe_com, 1);
6480 bf_set(wqe_lenloc, &wqe->els_req.wqe_com, LPFC_WQE_LENLOC_NONE);
6481 bf_set(wqe_ebde_cnt, &wqe->els_req.wqe_com, 0);
James Smart4f774512009-05-22 14:52:35 -04006482 break;
James Smart5ffc2662009-11-18 15:39:44 -05006483 case CMD_XMIT_SEQUENCE64_CX:
James Smartf0d9bcc2010-10-22 11:07:09 -04006484 bf_set(wqe_ctxt_tag, &wqe->xmit_sequence.wqe_com,
6485 iocbq->iocb.un.ulpWord[3]);
6486 bf_set(wqe_rcvoxid, &wqe->xmit_sequence.wqe_com,
6487 iocbq->iocb.ulpContext);
James Smart5ffc2662009-11-18 15:39:44 -05006488 /* The entire sequence is transmitted for this IOCB */
6489 xmit_len = total_len;
6490 cmnd = CMD_XMIT_SEQUENCE64_CR;
James Smart4f774512009-05-22 14:52:35 -04006491 case CMD_XMIT_SEQUENCE64_CR:
James Smartf0d9bcc2010-10-22 11:07:09 -04006492 /* word3 iocb=io_tag32 wqe=reserved */
6493 wqe->xmit_sequence.rsvd3 = 0;
James Smart4f774512009-05-22 14:52:35 -04006494 /* word4 relative_offset memcpy */
6495 /* word5 r_ctl/df_ctl memcpy */
James Smartf0d9bcc2010-10-22 11:07:09 -04006496 bf_set(wqe_pu, &wqe->xmit_sequence.wqe_com, 0);
6497 bf_set(wqe_dbde, &wqe->xmit_sequence.wqe_com, 1);
6498 bf_set(wqe_iod, &wqe->xmit_sequence.wqe_com,
6499 LPFC_WQE_IOD_WRITE);
6500 bf_set(wqe_lenloc, &wqe->xmit_sequence.wqe_com,
6501 LPFC_WQE_LENLOC_WORD12);
6502 bf_set(wqe_ebde_cnt, &wqe->xmit_sequence.wqe_com, 0);
James Smart5ffc2662009-11-18 15:39:44 -05006503 wqe->xmit_sequence.xmit_len = xmit_len;
6504 command_type = OTHER_COMMAND;
James Smart4f774512009-05-22 14:52:35 -04006505 break;
6506 case CMD_XMIT_BCAST64_CN:
James Smartf0d9bcc2010-10-22 11:07:09 -04006507 /* word3 iocb=iotag32 wqe=seq_payload_len */
6508 wqe->xmit_bcast64.seq_payload_len = xmit_len;
James Smart4f774512009-05-22 14:52:35 -04006509 /* word4 iocb=rsvd wqe=rsvd */
6510 /* word5 iocb=rctl/type/df_ctl wqe=rctl/type/df_ctl memcpy */
6511 /* word6 iocb=ctxt_tag/io_tag wqe=ctxt_tag/xri */
James Smartf0d9bcc2010-10-22 11:07:09 -04006512 bf_set(wqe_ct, &wqe->xmit_bcast64.wqe_com,
James Smart4f774512009-05-22 14:52:35 -04006513 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
James Smartf0d9bcc2010-10-22 11:07:09 -04006514 bf_set(wqe_dbde, &wqe->xmit_bcast64.wqe_com, 1);
6515 bf_set(wqe_iod, &wqe->xmit_bcast64.wqe_com, LPFC_WQE_IOD_WRITE);
6516 bf_set(wqe_lenloc, &wqe->xmit_bcast64.wqe_com,
6517 LPFC_WQE_LENLOC_WORD3);
6518 bf_set(wqe_ebde_cnt, &wqe->xmit_bcast64.wqe_com, 0);
James Smart4f774512009-05-22 14:52:35 -04006519 break;
6520 case CMD_FCP_IWRITE64_CR:
6521 command_type = FCP_COMMAND_DATA_OUT;
James Smartf0d9bcc2010-10-22 11:07:09 -04006522 /* word3 iocb=iotag wqe=payload_offset_len */
6523 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
6524 wqe->fcp_iwrite.payload_offset_len =
James Smart5ffc2662009-11-18 15:39:44 -05006525 xmit_len + sizeof(struct fcp_rsp);
James Smartf0d9bcc2010-10-22 11:07:09 -04006526 /* word4 iocb=parameter wqe=total_xfer_length memcpy */
6527 /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
6528 bf_set(wqe_erp, &wqe->fcp_iwrite.wqe_com,
6529 iocbq->iocb.ulpFCP2Rcvy);
6530 bf_set(wqe_lnk, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpXS);
6531 /* Always open the exchange */
6532 bf_set(wqe_xc, &wqe->fcp_iwrite.wqe_com, 0);
6533 bf_set(wqe_dbde, &wqe->fcp_iwrite.wqe_com, 1);
6534 bf_set(wqe_iod, &wqe->fcp_iwrite.wqe_com, LPFC_WQE_IOD_WRITE);
6535 bf_set(wqe_lenloc, &wqe->fcp_iwrite.wqe_com,
6536 LPFC_WQE_LENLOC_WORD4);
6537 bf_set(wqe_ebde_cnt, &wqe->fcp_iwrite.wqe_com, 0);
6538 bf_set(wqe_pu, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpPU);
6539 break;
6540 case CMD_FCP_IREAD64_CR:
6541 /* word3 iocb=iotag wqe=payload_offset_len */
6542 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
6543 wqe->fcp_iread.payload_offset_len =
6544 xmit_len + sizeof(struct fcp_rsp);
6545 /* word4 iocb=parameter wqe=total_xfer_length memcpy */
6546 /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
6547 bf_set(wqe_erp, &wqe->fcp_iread.wqe_com,
6548 iocbq->iocb.ulpFCP2Rcvy);
6549 bf_set(wqe_lnk, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpXS);
James Smart4f774512009-05-22 14:52:35 -04006550 /* Always open the exchange */
6551 bf_set(wqe_xc, &wqe->fcp_iread.wqe_com, 0);
James Smartf0d9bcc2010-10-22 11:07:09 -04006552 bf_set(wqe_dbde, &wqe->fcp_iread.wqe_com, 1);
6553 bf_set(wqe_iod, &wqe->fcp_iread.wqe_com, LPFC_WQE_IOD_READ);
6554 bf_set(wqe_lenloc, &wqe->fcp_iread.wqe_com,
6555 LPFC_WQE_LENLOC_WORD4);
6556 bf_set(wqe_ebde_cnt, &wqe->fcp_iread.wqe_com, 0);
6557 bf_set(wqe_pu, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpPU);
6558 break;
James Smartf1126682009-06-10 17:22:44 -04006559 case CMD_FCP_ICMND64_CR:
James Smartf0d9bcc2010-10-22 11:07:09 -04006560 /* word3 iocb=IO_TAG wqe=reserved */
6561 wqe->fcp_icmd.rsrvd3 = 0;
6562 bf_set(wqe_pu, &wqe->fcp_icmd.wqe_com, 0);
James Smartf1126682009-06-10 17:22:44 -04006563 /* Always open the exchange */
James Smartf0d9bcc2010-10-22 11:07:09 -04006564 bf_set(wqe_xc, &wqe->fcp_icmd.wqe_com, 0);
6565 bf_set(wqe_dbde, &wqe->fcp_icmd.wqe_com, 1);
6566 bf_set(wqe_iod, &wqe->fcp_icmd.wqe_com, LPFC_WQE_IOD_WRITE);
6567 bf_set(wqe_qosd, &wqe->fcp_icmd.wqe_com, 1);
6568 bf_set(wqe_lenloc, &wqe->fcp_icmd.wqe_com,
6569 LPFC_WQE_LENLOC_NONE);
6570 bf_set(wqe_ebde_cnt, &wqe->fcp_icmd.wqe_com, 0);
James Smart4f774512009-05-22 14:52:35 -04006571 break;
6572 case CMD_GEN_REQUEST64_CR:
James Smart63e801c2010-11-20 23:14:19 -05006573 /* For this command calculate the xmit length of the
6574 * request bde.
6575 */
6576 xmit_len = 0;
6577 numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
6578 sizeof(struct ulp_bde64);
6579 for (i = 0; i < numBdes; i++) {
James Smart63e801c2010-11-20 23:14:19 -05006580 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
James Smart546fc852011-03-11 16:06:29 -05006581 if (bde.tus.f.bdeFlags != BUFF_TYPE_BDE_64)
6582 break;
James Smart63e801c2010-11-20 23:14:19 -05006583 xmit_len += bde.tus.f.bdeSize;
6584 }
James Smartf0d9bcc2010-10-22 11:07:09 -04006585 /* word3 iocb=IO_TAG wqe=request_payload_len */
6586 wqe->gen_req.request_payload_len = xmit_len;
6587 /* word4 iocb=parameter wqe=relative_offset memcpy */
6588 /* word5 [rctl, type, df_ctl, la] copied in memcpy */
James Smart4f774512009-05-22 14:52:35 -04006589 /* word6 context tag copied in memcpy */
6590 if (iocbq->iocb.ulpCt_h || iocbq->iocb.ulpCt_l) {
6591 ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
6592 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6593 "2015 Invalid CT %x command 0x%x\n",
6594 ct, iocbq->iocb.ulpCommand);
6595 return IOCB_ERROR;
6596 }
James Smartf0d9bcc2010-10-22 11:07:09 -04006597 bf_set(wqe_ct, &wqe->gen_req.wqe_com, 0);
6598 bf_set(wqe_tmo, &wqe->gen_req.wqe_com, iocbq->iocb.ulpTimeout);
6599 bf_set(wqe_pu, &wqe->gen_req.wqe_com, iocbq->iocb.ulpPU);
6600 bf_set(wqe_dbde, &wqe->gen_req.wqe_com, 1);
6601 bf_set(wqe_iod, &wqe->gen_req.wqe_com, LPFC_WQE_IOD_READ);
6602 bf_set(wqe_qosd, &wqe->gen_req.wqe_com, 1);
6603 bf_set(wqe_lenloc, &wqe->gen_req.wqe_com, LPFC_WQE_LENLOC_NONE);
6604 bf_set(wqe_ebde_cnt, &wqe->gen_req.wqe_com, 0);
James Smart4f774512009-05-22 14:52:35 -04006605 command_type = OTHER_COMMAND;
6606 break;
6607 case CMD_XMIT_ELS_RSP64_CX:
6608 /* words0-2 BDE memcpy */
James Smartf0d9bcc2010-10-22 11:07:09 -04006609 /* word3 iocb=iotag32 wqe=response_payload_len */
6610 wqe->xmit_els_rsp.response_payload_len = xmit_len;
James Smart4f774512009-05-22 14:52:35 -04006611 /* word4 iocb=did wge=rsvd. */
James Smartf0d9bcc2010-10-22 11:07:09 -04006612 wqe->xmit_els_rsp.rsvd4 = 0;
James Smart4f774512009-05-22 14:52:35 -04006613 /* word5 iocb=rsvd wge=did */
6614 bf_set(wqe_els_did, &wqe->xmit_els_rsp.wqe_dest,
6615 iocbq->iocb.un.elsreq64.remoteID);
James Smartf0d9bcc2010-10-22 11:07:09 -04006616 bf_set(wqe_ct, &wqe->xmit_els_rsp.wqe_com,
6617 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
6618 bf_set(wqe_pu, &wqe->xmit_els_rsp.wqe_com, iocbq->iocb.ulpPU);
6619 bf_set(wqe_rcvoxid, &wqe->xmit_els_rsp.wqe_com,
6620 iocbq->iocb.ulpContext);
James Smart4f774512009-05-22 14:52:35 -04006621 if (!iocbq->iocb.ulpCt_h && iocbq->iocb.ulpCt_l)
James Smartf0d9bcc2010-10-22 11:07:09 -04006622 bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
James Smart4f774512009-05-22 14:52:35 -04006623 iocbq->vport->vpi + phba->vpi_base);
James Smartf0d9bcc2010-10-22 11:07:09 -04006624 bf_set(wqe_dbde, &wqe->xmit_els_rsp.wqe_com, 1);
6625 bf_set(wqe_iod, &wqe->xmit_els_rsp.wqe_com, LPFC_WQE_IOD_WRITE);
6626 bf_set(wqe_qosd, &wqe->xmit_els_rsp.wqe_com, 1);
6627 bf_set(wqe_lenloc, &wqe->xmit_els_rsp.wqe_com,
6628 LPFC_WQE_LENLOC_WORD3);
6629 bf_set(wqe_ebde_cnt, &wqe->xmit_els_rsp.wqe_com, 0);
James Smart4f774512009-05-22 14:52:35 -04006630 command_type = OTHER_COMMAND;
6631 break;
6632 case CMD_CLOSE_XRI_CN:
6633 case CMD_ABORT_XRI_CN:
6634 case CMD_ABORT_XRI_CX:
6635 /* words 0-2 memcpy should be 0 rserved */
6636 /* port will send abts */
James Smartdcf2a4e2010-09-29 11:18:53 -04006637 abrt_iotag = iocbq->iocb.un.acxri.abortContextTag;
6638 if (abrt_iotag != 0 && abrt_iotag <= phba->sli.last_iotag) {
6639 abrtiocbq = phba->sli.iocbq_lookup[abrt_iotag];
6640 fip = abrtiocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK;
6641 } else
6642 fip = 0;
6643
6644 if ((iocbq->iocb.ulpCommand == CMD_CLOSE_XRI_CN) || fip)
James Smart4f774512009-05-22 14:52:35 -04006645 /*
James Smartdcf2a4e2010-09-29 11:18:53 -04006646 * The link is down, or the command was ELS_FIP
6647 * so the fw does not need to send abts
James Smart4f774512009-05-22 14:52:35 -04006648 * on the wire.
6649 */
6650 bf_set(abort_cmd_ia, &wqe->abort_cmd, 1);
6651 else
6652 bf_set(abort_cmd_ia, &wqe->abort_cmd, 0);
6653 bf_set(abort_cmd_criteria, &wqe->abort_cmd, T_XRI_TAG);
James Smartf0d9bcc2010-10-22 11:07:09 -04006654 /* word5 iocb=CONTEXT_TAG|IO_TAG wqe=reserved */
6655 wqe->abort_cmd.rsrvd5 = 0;
6656 bf_set(wqe_ct, &wqe->abort_cmd.wqe_com,
James Smart4f774512009-05-22 14:52:35 -04006657 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
6658 abort_tag = iocbq->iocb.un.acxri.abortIoTag;
James Smart4f774512009-05-22 14:52:35 -04006659 /*
6660 * The abort handler will send us CMD_ABORT_XRI_CN or
6661 * CMD_CLOSE_XRI_CN and the fw only accepts CMD_ABORT_XRI_CX
6662 */
James Smartf0d9bcc2010-10-22 11:07:09 -04006663 bf_set(wqe_cmnd, &wqe->abort_cmd.wqe_com, CMD_ABORT_XRI_CX);
6664 bf_set(wqe_qosd, &wqe->abort_cmd.wqe_com, 1);
6665 bf_set(wqe_lenloc, &wqe->abort_cmd.wqe_com,
6666 LPFC_WQE_LENLOC_NONE);
James Smart4f774512009-05-22 14:52:35 -04006667 cmnd = CMD_ABORT_XRI_CX;
6668 command_type = OTHER_COMMAND;
6669 xritag = 0;
6670 break;
James Smart6669f9b2009-10-02 15:16:45 -04006671 case CMD_XMIT_BLS_RSP64_CX:
James Smart546fc852011-03-11 16:06:29 -05006672 /* As BLS ABTS RSP WQE is very different from other WQEs,
James Smart6669f9b2009-10-02 15:16:45 -04006673 * we re-construct this WQE here based on information in
6674 * iocbq from scratch.
6675 */
6676 memset(wqe, 0, sizeof(union lpfc_wqe));
James Smart5ffc2662009-11-18 15:39:44 -05006677 /* OX_ID is invariable to who sent ABTS to CT exchange */
James Smart6669f9b2009-10-02 15:16:45 -04006678 bf_set(xmit_bls_rsp64_oxid, &wqe->xmit_bls_rsp,
James Smart546fc852011-03-11 16:06:29 -05006679 bf_get(lpfc_abts_oxid, &iocbq->iocb.un.bls_rsp));
6680 if (bf_get(lpfc_abts_orig, &iocbq->iocb.un.bls_rsp) ==
James Smart5ffc2662009-11-18 15:39:44 -05006681 LPFC_ABTS_UNSOL_INT) {
6682 /* ABTS sent by initiator to CT exchange, the
6683 * RX_ID field will be filled with the newly
6684 * allocated responder XRI.
6685 */
6686 bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
6687 iocbq->sli4_xritag);
6688 } else {
6689 /* ABTS sent by responder to CT exchange, the
6690 * RX_ID field will be filled with the responder
6691 * RX_ID from ABTS.
6692 */
6693 bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
James Smart546fc852011-03-11 16:06:29 -05006694 bf_get(lpfc_abts_rxid, &iocbq->iocb.un.bls_rsp));
James Smart5ffc2662009-11-18 15:39:44 -05006695 }
James Smart6669f9b2009-10-02 15:16:45 -04006696 bf_set(xmit_bls_rsp64_seqcnthi, &wqe->xmit_bls_rsp, 0xffff);
6697 bf_set(wqe_xmit_bls_pt, &wqe->xmit_bls_rsp.wqe_dest, 0x1);
6698 bf_set(wqe_ctxt_tag, &wqe->xmit_bls_rsp.wqe_com,
6699 iocbq->iocb.ulpContext);
James Smartf0d9bcc2010-10-22 11:07:09 -04006700 bf_set(wqe_qosd, &wqe->xmit_bls_rsp.wqe_com, 1);
6701 bf_set(wqe_lenloc, &wqe->xmit_bls_rsp.wqe_com,
6702 LPFC_WQE_LENLOC_NONE);
James Smart6669f9b2009-10-02 15:16:45 -04006703 /* Overwrite the pre-set comnd type with OTHER_COMMAND */
6704 command_type = OTHER_COMMAND;
James Smart546fc852011-03-11 16:06:29 -05006705 if (iocbq->iocb.un.xseq64.w5.hcsw.Rctl == FC_RCTL_BA_RJT) {
6706 bf_set(xmit_bls_rsp64_rjt_vspec, &wqe->xmit_bls_rsp,
6707 bf_get(lpfc_vndr_code, &iocbq->iocb.un.bls_rsp));
6708 bf_set(xmit_bls_rsp64_rjt_expc, &wqe->xmit_bls_rsp,
6709 bf_get(lpfc_rsn_expln, &iocbq->iocb.un.bls_rsp));
6710 bf_set(xmit_bls_rsp64_rjt_rsnc, &wqe->xmit_bls_rsp,
6711 bf_get(lpfc_rsn_code, &iocbq->iocb.un.bls_rsp));
6712 }
6713
James Smart6669f9b2009-10-02 15:16:45 -04006714 break;
James Smart4f774512009-05-22 14:52:35 -04006715 case CMD_XRI_ABORTED_CX:
6716 case CMD_CREATE_XRI_CR: /* Do we expect to use this? */
James Smart4f774512009-05-22 14:52:35 -04006717 case CMD_IOCB_FCP_IBIDIR64_CR: /* bidirectional xfer */
6718 case CMD_FCP_TSEND64_CX: /* Target mode send xfer-ready */
6719 case CMD_FCP_TRSP64_CX: /* Target mode rcv */
6720 case CMD_FCP_AUTO_TRSP_CX: /* Auto target rsp */
6721 default:
6722 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6723 "2014 Invalid command 0x%x\n",
6724 iocbq->iocb.ulpCommand);
6725 return IOCB_ERROR;
6726 break;
James Smart4f774512009-05-22 14:52:35 -04006727 }
James Smartf0d9bcc2010-10-22 11:07:09 -04006728 bf_set(wqe_xri_tag, &wqe->generic.wqe_com, xritag);
6729 bf_set(wqe_reqtag, &wqe->generic.wqe_com, iocbq->iotag);
6730 wqe->generic.wqe_com.abort_tag = abort_tag;
6731 bf_set(wqe_cmd_type, &wqe->generic.wqe_com, command_type);
6732 bf_set(wqe_cmnd, &wqe->generic.wqe_com, cmnd);
6733 bf_set(wqe_class, &wqe->generic.wqe_com, iocbq->iocb.ulpClass);
6734 bf_set(wqe_cqid, &wqe->generic.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
James Smart4f774512009-05-22 14:52:35 -04006735 return 0;
6736}
6737
6738/**
6739 * __lpfc_sli_issue_iocb_s4 - SLI4 device lockless ver of lpfc_sli_issue_iocb
6740 * @phba: Pointer to HBA context object.
6741 * @ring_number: SLI ring number to issue iocb on.
6742 * @piocb: Pointer to command iocb.
6743 * @flag: Flag indicating if this command can be put into txq.
6744 *
6745 * __lpfc_sli_issue_iocb_s4 is used by other functions in the driver to issue
6746 * an iocb command to an HBA with SLI-4 interface spec.
6747 *
6748 * This function is called with hbalock held. The function will return success
6749 * after it successfully submit the iocb to firmware or after adding to the
6750 * txq.
6751 **/
6752static int
6753__lpfc_sli_issue_iocb_s4(struct lpfc_hba *phba, uint32_t ring_number,
6754 struct lpfc_iocbq *piocb, uint32_t flag)
6755{
6756 struct lpfc_sglq *sglq;
James Smart4f774512009-05-22 14:52:35 -04006757 union lpfc_wqe wqe;
6758 struct lpfc_sli_ring *pring = &phba->sli.ring[ring_number];
James Smart4f774512009-05-22 14:52:35 -04006759
6760 if (piocb->sli4_xritag == NO_XRI) {
6761 if (piocb->iocb.ulpCommand == CMD_ABORT_XRI_CN ||
James Smart546fc852011-03-11 16:06:29 -05006762 piocb->iocb.ulpCommand == CMD_CLOSE_XRI_CN ||
6763 piocb->iocb.ulpCommand == CMD_XMIT_BLS_RSP64_CX)
James Smart4f774512009-05-22 14:52:35 -04006764 sglq = NULL;
6765 else {
James Smart2a9bf3d2010-06-07 15:24:45 -04006766 if (pring->txq_cnt) {
6767 if (!(flag & SLI_IOCB_RET_IOCB)) {
6768 __lpfc_sli_ringtx_put(phba,
6769 pring, piocb);
6770 return IOCB_SUCCESS;
6771 } else {
6772 return IOCB_BUSY;
6773 }
6774 } else {
James Smart19ca7602010-11-20 23:11:55 -05006775 sglq = __lpfc_sli_get_sglq(phba, piocb);
James Smart2a9bf3d2010-06-07 15:24:45 -04006776 if (!sglq) {
6777 if (!(flag & SLI_IOCB_RET_IOCB)) {
6778 __lpfc_sli_ringtx_put(phba,
6779 pring,
6780 piocb);
6781 return IOCB_SUCCESS;
6782 } else
6783 return IOCB_BUSY;
6784 }
6785 }
James Smart4f774512009-05-22 14:52:35 -04006786 }
6787 } else if (piocb->iocb_flag & LPFC_IO_FCP) {
6788 sglq = NULL; /* These IO's already have an XRI and
6789 * a mapped sgl.
6790 */
6791 } else {
6792 /* This is a continuation of a commandi,(CX) so this
6793 * sglq is on the active list
6794 */
6795 sglq = __lpfc_get_active_sglq(phba, piocb->sli4_xritag);
6796 if (!sglq)
6797 return IOCB_ERROR;
6798 }
6799
6800 if (sglq) {
James Smart2a9bf3d2010-06-07 15:24:45 -04006801 piocb->sli4_xritag = sglq->sli4_xritag;
6802
6803 if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocb, sglq))
James Smart4f774512009-05-22 14:52:35 -04006804 return IOCB_ERROR;
6805 }
6806
6807 if (lpfc_sli4_iocb2wqe(phba, piocb, &wqe))
6808 return IOCB_ERROR;
6809
James Smart341af102010-01-26 23:07:37 -05006810 if ((piocb->iocb_flag & LPFC_IO_FCP) ||
6811 (piocb->iocb_flag & LPFC_USE_FCPWQIDX)) {
James Smart5ffc2662009-11-18 15:39:44 -05006812 /*
6813 * For FCP command IOCB, get a new WQ index to distribute
6814 * WQE across the WQsr. On the other hand, for abort IOCB,
6815 * it carries the same WQ index to the original command
6816 * IOCB.
6817 */
James Smart341af102010-01-26 23:07:37 -05006818 if (piocb->iocb_flag & LPFC_IO_FCP)
James Smart5ffc2662009-11-18 15:39:44 -05006819 piocb->fcp_wqidx = lpfc_sli4_scmd_to_wqidx_distr(phba);
6820 if (lpfc_sli4_wq_put(phba->sli4_hba.fcp_wq[piocb->fcp_wqidx],
6821 &wqe))
James Smart4f774512009-05-22 14:52:35 -04006822 return IOCB_ERROR;
6823 } else {
6824 if (lpfc_sli4_wq_put(phba->sli4_hba.els_wq, &wqe))
6825 return IOCB_ERROR;
6826 }
6827 lpfc_sli_ringtxcmpl_put(phba, pring, piocb);
6828
6829 return 0;
6830}
6831
6832/**
James Smart3772a992009-05-22 14:50:54 -04006833 * __lpfc_sli_issue_iocb - Wrapper func of lockless version for issuing iocb
6834 *
6835 * This routine wraps the actual lockless version for issusing IOCB function
6836 * pointer from the lpfc_hba struct.
6837 *
6838 * Return codes:
6839 * IOCB_ERROR - Error
6840 * IOCB_SUCCESS - Success
6841 * IOCB_BUSY - Busy
6842 **/
James Smart2a9bf3d2010-06-07 15:24:45 -04006843int
James Smart3772a992009-05-22 14:50:54 -04006844__lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
6845 struct lpfc_iocbq *piocb, uint32_t flag)
6846{
6847 return phba->__lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
6848}
6849
6850/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03006851 * lpfc_sli_api_table_setup - Set up sli api function jump table
James Smart3772a992009-05-22 14:50:54 -04006852 * @phba: The hba struct for which this call is being executed.
6853 * @dev_grp: The HBA PCI-Device group number.
6854 *
6855 * This routine sets up the SLI interface API function jump table in @phba
6856 * struct.
6857 * Returns: 0 - success, -ENODEV - failure.
6858 **/
6859int
6860lpfc_sli_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
6861{
6862
6863 switch (dev_grp) {
6864 case LPFC_PCI_DEV_LP:
6865 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s3;
6866 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s3;
6867 break;
James Smart4f774512009-05-22 14:52:35 -04006868 case LPFC_PCI_DEV_OC:
6869 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s4;
6870 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s4;
6871 break;
James Smart3772a992009-05-22 14:50:54 -04006872 default:
6873 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6874 "1419 Invalid HBA PCI-device group: 0x%x\n",
6875 dev_grp);
6876 return -ENODEV;
6877 break;
6878 }
6879 phba->lpfc_get_iocb_from_iocbq = lpfc_get_iocb_from_iocbq;
6880 return 0;
6881}
James Smart92d7f7b2007-06-17 19:56:38 -05006882
James Smarte59058c2008-08-24 21:49:00 -04006883/**
James Smart3621a712009-04-06 18:47:14 -04006884 * lpfc_sli_issue_iocb - Wrapper function for __lpfc_sli_issue_iocb
James Smarte59058c2008-08-24 21:49:00 -04006885 * @phba: Pointer to HBA context object.
6886 * @pring: Pointer to driver SLI ring object.
6887 * @piocb: Pointer to command iocb.
6888 * @flag: Flag indicating if this command can be put into txq.
6889 *
6890 * lpfc_sli_issue_iocb is a wrapper around __lpfc_sli_issue_iocb
6891 * function. This function gets the hbalock and calls
6892 * __lpfc_sli_issue_iocb function and will return the error returned
6893 * by __lpfc_sli_issue_iocb function. This wrapper is used by
6894 * functions which do not hold hbalock.
6895 **/
James Smart92d7f7b2007-06-17 19:56:38 -05006896int
James Smart3772a992009-05-22 14:50:54 -04006897lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
James Smart92d7f7b2007-06-17 19:56:38 -05006898 struct lpfc_iocbq *piocb, uint32_t flag)
6899{
6900 unsigned long iflags;
6901 int rc;
6902
6903 spin_lock_irqsave(&phba->hbalock, iflags);
James Smart3772a992009-05-22 14:50:54 -04006904 rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
James Smart92d7f7b2007-06-17 19:56:38 -05006905 spin_unlock_irqrestore(&phba->hbalock, iflags);
6906
6907 return rc;
6908}
6909
James Smarte59058c2008-08-24 21:49:00 -04006910/**
James Smart3621a712009-04-06 18:47:14 -04006911 * lpfc_extra_ring_setup - Extra ring setup function
James Smarte59058c2008-08-24 21:49:00 -04006912 * @phba: Pointer to HBA context object.
6913 *
6914 * This function is called while driver attaches with the
6915 * HBA to setup the extra ring. The extra ring is used
6916 * only when driver needs to support target mode functionality
6917 * or IP over FC functionalities.
6918 *
6919 * This function is called with no lock held.
6920 **/
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05006921static int
6922lpfc_extra_ring_setup( struct lpfc_hba *phba)
6923{
6924 struct lpfc_sli *psli;
6925 struct lpfc_sli_ring *pring;
6926
6927 psli = &phba->sli;
6928
6929 /* Adjust cmd/rsp ring iocb entries more evenly */
James Smarta4bc3372006-12-02 13:34:16 -05006930
6931 /* Take some away from the FCP ring */
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05006932 pring = &psli->ring[psli->fcp_ring];
6933 pring->numCiocb -= SLI2_IOCB_CMD_R1XTRA_ENTRIES;
6934 pring->numRiocb -= SLI2_IOCB_RSP_R1XTRA_ENTRIES;
6935 pring->numCiocb -= SLI2_IOCB_CMD_R3XTRA_ENTRIES;
6936 pring->numRiocb -= SLI2_IOCB_RSP_R3XTRA_ENTRIES;
6937
James Smarta4bc3372006-12-02 13:34:16 -05006938 /* and give them to the extra ring */
6939 pring = &psli->ring[psli->extra_ring];
6940
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05006941 pring->numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
6942 pring->numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
6943 pring->numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
6944 pring->numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
6945
6946 /* Setup default profile for this ring */
6947 pring->iotag_max = 4096;
6948 pring->num_mask = 1;
6949 pring->prt[0].profile = 0; /* Mask 0 */
James Smarta4bc3372006-12-02 13:34:16 -05006950 pring->prt[0].rctl = phba->cfg_multi_ring_rctl;
6951 pring->prt[0].type = phba->cfg_multi_ring_type;
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05006952 pring->prt[0].lpfc_sli_rcv_unsol_event = NULL;
6953 return 0;
6954}
6955
James Smarte59058c2008-08-24 21:49:00 -04006956/**
James Smart3621a712009-04-06 18:47:14 -04006957 * lpfc_sli_async_event_handler - ASYNC iocb handler function
James Smarte59058c2008-08-24 21:49:00 -04006958 * @phba: Pointer to HBA context object.
6959 * @pring: Pointer to driver SLI ring object.
6960 * @iocbq: Pointer to iocb object.
6961 *
6962 * This function is called by the slow ring event handler
6963 * function when there is an ASYNC event iocb in the ring.
6964 * This function is called with no lock held.
6965 * Currently this function handles only temperature related
6966 * ASYNC events. The function decodes the temperature sensor
6967 * event message and posts events for the management applications.
6968 **/
James Smart98c9ea52007-10-27 13:37:33 -04006969static void
James Smart57127f12007-10-27 13:37:05 -04006970lpfc_sli_async_event_handler(struct lpfc_hba * phba,
6971 struct lpfc_sli_ring * pring, struct lpfc_iocbq * iocbq)
6972{
6973 IOCB_t *icmd;
6974 uint16_t evt_code;
6975 uint16_t temp;
6976 struct temp_event temp_event_data;
6977 struct Scsi_Host *shost;
James Smarta257bf92009-04-06 18:48:10 -04006978 uint32_t *iocb_w;
James Smart57127f12007-10-27 13:37:05 -04006979
6980 icmd = &iocbq->iocb;
6981 evt_code = icmd->un.asyncstat.evt_code;
6982 temp = icmd->ulpContext;
6983
6984 if ((evt_code != ASYNC_TEMP_WARN) &&
6985 (evt_code != ASYNC_TEMP_SAFE)) {
James Smarta257bf92009-04-06 18:48:10 -04006986 iocb_w = (uint32_t *) icmd;
James Smart57127f12007-10-27 13:37:05 -04006987 lpfc_printf_log(phba,
6988 KERN_ERR,
6989 LOG_SLI,
James Smart76bb24e2007-10-27 13:38:00 -04006990 "0346 Ring %d handler: unexpected ASYNC_STATUS"
James Smarte4e74272009-07-19 10:01:38 -04006991 " evt_code 0x%x\n"
James Smarta257bf92009-04-06 18:48:10 -04006992 "W0 0x%08x W1 0x%08x W2 0x%08x W3 0x%08x\n"
6993 "W4 0x%08x W5 0x%08x W6 0x%08x W7 0x%08x\n"
6994 "W8 0x%08x W9 0x%08x W10 0x%08x W11 0x%08x\n"
6995 "W12 0x%08x W13 0x%08x W14 0x%08x W15 0x%08x\n",
James Smart57127f12007-10-27 13:37:05 -04006996 pring->ringno,
James Smarta257bf92009-04-06 18:48:10 -04006997 icmd->un.asyncstat.evt_code,
6998 iocb_w[0], iocb_w[1], iocb_w[2], iocb_w[3],
6999 iocb_w[4], iocb_w[5], iocb_w[6], iocb_w[7],
7000 iocb_w[8], iocb_w[9], iocb_w[10], iocb_w[11],
7001 iocb_w[12], iocb_w[13], iocb_w[14], iocb_w[15]);
7002
James Smart57127f12007-10-27 13:37:05 -04007003 return;
7004 }
7005 temp_event_data.data = (uint32_t)temp;
7006 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
7007 if (evt_code == ASYNC_TEMP_WARN) {
7008 temp_event_data.event_code = LPFC_THRESHOLD_TEMP;
7009 lpfc_printf_log(phba,
James Smart09372822008-01-11 01:52:54 -05007010 KERN_ERR,
James Smart57127f12007-10-27 13:37:05 -04007011 LOG_TEMP,
James Smart76bb24e2007-10-27 13:38:00 -04007012 "0347 Adapter is very hot, please take "
James Smart57127f12007-10-27 13:37:05 -04007013 "corrective action. temperature : %d Celsius\n",
7014 temp);
7015 }
7016 if (evt_code == ASYNC_TEMP_SAFE) {
7017 temp_event_data.event_code = LPFC_NORMAL_TEMP;
7018 lpfc_printf_log(phba,
James Smart09372822008-01-11 01:52:54 -05007019 KERN_ERR,
James Smart57127f12007-10-27 13:37:05 -04007020 LOG_TEMP,
7021 "0340 Adapter temperature is OK now. "
7022 "temperature : %d Celsius\n",
7023 temp);
7024 }
7025
7026 /* Send temperature change event to applications */
7027 shost = lpfc_shost_from_vport(phba->pport);
7028 fc_host_post_vendor_event(shost, fc_get_event_number(),
7029 sizeof(temp_event_data), (char *) &temp_event_data,
James Smartddcc50f2008-12-04 22:38:46 -05007030 LPFC_NL_VENDOR_ID);
James Smart57127f12007-10-27 13:37:05 -04007031
7032}
7033
7034
James Smarte59058c2008-08-24 21:49:00 -04007035/**
James Smart3621a712009-04-06 18:47:14 -04007036 * lpfc_sli_setup - SLI ring setup function
James Smarte59058c2008-08-24 21:49:00 -04007037 * @phba: Pointer to HBA context object.
7038 *
7039 * lpfc_sli_setup sets up rings of the SLI interface with
7040 * number of iocbs per ring and iotags. This function is
7041 * called while driver attach to the HBA and before the
7042 * interrupts are enabled. So there is no need for locking.
7043 *
7044 * This function always returns 0.
7045 **/
dea31012005-04-17 16:05:31 -05007046int
7047lpfc_sli_setup(struct lpfc_hba *phba)
7048{
James Smarted957682007-06-17 19:56:37 -05007049 int i, totiocbsize = 0;
dea31012005-04-17 16:05:31 -05007050 struct lpfc_sli *psli = &phba->sli;
7051 struct lpfc_sli_ring *pring;
7052
7053 psli->num_rings = MAX_CONFIGURED_RINGS;
7054 psli->sli_flag = 0;
7055 psli->fcp_ring = LPFC_FCP_RING;
7056 psli->next_ring = LPFC_FCP_NEXT_RING;
James Smarta4bc3372006-12-02 13:34:16 -05007057 psli->extra_ring = LPFC_EXTRA_RING;
dea31012005-04-17 16:05:31 -05007058
James Bottomley604a3e32005-10-29 10:28:33 -05007059 psli->iocbq_lookup = NULL;
7060 psli->iocbq_lookup_len = 0;
7061 psli->last_iotag = 0;
7062
dea31012005-04-17 16:05:31 -05007063 for (i = 0; i < psli->num_rings; i++) {
7064 pring = &psli->ring[i];
7065 switch (i) {
7066 case LPFC_FCP_RING: /* ring 0 - FCP */
7067 /* numCiocb and numRiocb are used in config_port */
7068 pring->numCiocb = SLI2_IOCB_CMD_R0_ENTRIES;
7069 pring->numRiocb = SLI2_IOCB_RSP_R0_ENTRIES;
7070 pring->numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
7071 pring->numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
7072 pring->numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
7073 pring->numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
James Smarted957682007-06-17 19:56:37 -05007074 pring->sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05007075 SLI3_IOCB_CMD_SIZE :
7076 SLI2_IOCB_CMD_SIZE;
James Smarted957682007-06-17 19:56:37 -05007077 pring->sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05007078 SLI3_IOCB_RSP_SIZE :
7079 SLI2_IOCB_RSP_SIZE;
dea31012005-04-17 16:05:31 -05007080 pring->iotag_ctr = 0;
7081 pring->iotag_max =
James Smart92d7f7b2007-06-17 19:56:38 -05007082 (phba->cfg_hba_queue_depth * 2);
dea31012005-04-17 16:05:31 -05007083 pring->fast_iotag = pring->iotag_max;
7084 pring->num_mask = 0;
7085 break;
James Smarta4bc3372006-12-02 13:34:16 -05007086 case LPFC_EXTRA_RING: /* ring 1 - EXTRA */
dea31012005-04-17 16:05:31 -05007087 /* numCiocb and numRiocb are used in config_port */
7088 pring->numCiocb = SLI2_IOCB_CMD_R1_ENTRIES;
7089 pring->numRiocb = SLI2_IOCB_RSP_R1_ENTRIES;
James Smarted957682007-06-17 19:56:37 -05007090 pring->sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05007091 SLI3_IOCB_CMD_SIZE :
7092 SLI2_IOCB_CMD_SIZE;
James Smarted957682007-06-17 19:56:37 -05007093 pring->sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05007094 SLI3_IOCB_RSP_SIZE :
7095 SLI2_IOCB_RSP_SIZE;
James Smart2e0fef82007-06-17 19:56:36 -05007096 pring->iotag_max = phba->cfg_hba_queue_depth;
dea31012005-04-17 16:05:31 -05007097 pring->num_mask = 0;
7098 break;
7099 case LPFC_ELS_RING: /* ring 2 - ELS / CT */
7100 /* numCiocb and numRiocb are used in config_port */
7101 pring->numCiocb = SLI2_IOCB_CMD_R2_ENTRIES;
7102 pring->numRiocb = SLI2_IOCB_RSP_R2_ENTRIES;
James Smarted957682007-06-17 19:56:37 -05007103 pring->sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05007104 SLI3_IOCB_CMD_SIZE :
7105 SLI2_IOCB_CMD_SIZE;
James Smarted957682007-06-17 19:56:37 -05007106 pring->sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05007107 SLI3_IOCB_RSP_SIZE :
7108 SLI2_IOCB_RSP_SIZE;
dea31012005-04-17 16:05:31 -05007109 pring->fast_iotag = 0;
7110 pring->iotag_ctr = 0;
7111 pring->iotag_max = 4096;
James Smart57127f12007-10-27 13:37:05 -04007112 pring->lpfc_sli_rcv_async_status =
7113 lpfc_sli_async_event_handler;
James Smart6669f9b2009-10-02 15:16:45 -04007114 pring->num_mask = LPFC_MAX_RING_MASK;
dea31012005-04-17 16:05:31 -05007115 pring->prt[0].profile = 0; /* Mask 0 */
James Smart6a9c52c2009-10-02 15:16:51 -04007116 pring->prt[0].rctl = FC_RCTL_ELS_REQ;
7117 pring->prt[0].type = FC_TYPE_ELS;
dea31012005-04-17 16:05:31 -05007118 pring->prt[0].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05007119 lpfc_els_unsol_event;
dea31012005-04-17 16:05:31 -05007120 pring->prt[1].profile = 0; /* Mask 1 */
James Smart6a9c52c2009-10-02 15:16:51 -04007121 pring->prt[1].rctl = FC_RCTL_ELS_REP;
7122 pring->prt[1].type = FC_TYPE_ELS;
dea31012005-04-17 16:05:31 -05007123 pring->prt[1].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05007124 lpfc_els_unsol_event;
dea31012005-04-17 16:05:31 -05007125 pring->prt[2].profile = 0; /* Mask 2 */
7126 /* NameServer Inquiry */
James Smart6a9c52c2009-10-02 15:16:51 -04007127 pring->prt[2].rctl = FC_RCTL_DD_UNSOL_CTL;
dea31012005-04-17 16:05:31 -05007128 /* NameServer */
James Smart6a9c52c2009-10-02 15:16:51 -04007129 pring->prt[2].type = FC_TYPE_CT;
dea31012005-04-17 16:05:31 -05007130 pring->prt[2].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05007131 lpfc_ct_unsol_event;
dea31012005-04-17 16:05:31 -05007132 pring->prt[3].profile = 0; /* Mask 3 */
7133 /* NameServer response */
James Smart6a9c52c2009-10-02 15:16:51 -04007134 pring->prt[3].rctl = FC_RCTL_DD_SOL_CTL;
dea31012005-04-17 16:05:31 -05007135 /* NameServer */
James Smart6a9c52c2009-10-02 15:16:51 -04007136 pring->prt[3].type = FC_TYPE_CT;
dea31012005-04-17 16:05:31 -05007137 pring->prt[3].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05007138 lpfc_ct_unsol_event;
James Smart6669f9b2009-10-02 15:16:45 -04007139 /* abort unsolicited sequence */
7140 pring->prt[4].profile = 0; /* Mask 4 */
7141 pring->prt[4].rctl = FC_RCTL_BA_ABTS;
7142 pring->prt[4].type = FC_TYPE_BLS;
7143 pring->prt[4].lpfc_sli_rcv_unsol_event =
7144 lpfc_sli4_ct_abort_unsol_event;
dea31012005-04-17 16:05:31 -05007145 break;
7146 }
James Smarted957682007-06-17 19:56:37 -05007147 totiocbsize += (pring->numCiocb * pring->sizeCiocb) +
James Smart92d7f7b2007-06-17 19:56:38 -05007148 (pring->numRiocb * pring->sizeRiocb);
dea31012005-04-17 16:05:31 -05007149 }
James Smarted957682007-06-17 19:56:37 -05007150 if (totiocbsize > MAX_SLIM_IOCB_SIZE) {
dea31012005-04-17 16:05:31 -05007151 /* Too many cmd / rsp ring entries in SLI2 SLIM */
James Smarte8b62012007-08-02 11:10:09 -04007152 printk(KERN_ERR "%d:0462 Too many cmd / rsp ring entries in "
7153 "SLI2 SLIM Data: x%x x%lx\n",
7154 phba->brd_no, totiocbsize,
7155 (unsigned long) MAX_SLIM_IOCB_SIZE);
dea31012005-04-17 16:05:31 -05007156 }
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05007157 if (phba->cfg_multi_ring_support == 2)
7158 lpfc_extra_ring_setup(phba);
dea31012005-04-17 16:05:31 -05007159
7160 return 0;
7161}
7162
James Smarte59058c2008-08-24 21:49:00 -04007163/**
James Smart3621a712009-04-06 18:47:14 -04007164 * lpfc_sli_queue_setup - Queue initialization function
James Smarte59058c2008-08-24 21:49:00 -04007165 * @phba: Pointer to HBA context object.
7166 *
7167 * lpfc_sli_queue_setup sets up mailbox queues and iocb queues for each
7168 * ring. This function also initializes ring indices of each ring.
7169 * This function is called during the initialization of the SLI
7170 * interface of an HBA.
7171 * This function is called with no lock held and always returns
7172 * 1.
7173 **/
dea31012005-04-17 16:05:31 -05007174int
James Smart2e0fef82007-06-17 19:56:36 -05007175lpfc_sli_queue_setup(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05007176{
7177 struct lpfc_sli *psli;
7178 struct lpfc_sli_ring *pring;
James Bottomley604a3e32005-10-29 10:28:33 -05007179 int i;
dea31012005-04-17 16:05:31 -05007180
7181 psli = &phba->sli;
James Smart2e0fef82007-06-17 19:56:36 -05007182 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05007183 INIT_LIST_HEAD(&psli->mboxq);
James Smart92d7f7b2007-06-17 19:56:38 -05007184 INIT_LIST_HEAD(&psli->mboxq_cmpl);
dea31012005-04-17 16:05:31 -05007185 /* Initialize list headers for txq and txcmplq as double linked lists */
7186 for (i = 0; i < psli->num_rings; i++) {
7187 pring = &psli->ring[i];
7188 pring->ringno = i;
7189 pring->next_cmdidx = 0;
7190 pring->local_getidx = 0;
7191 pring->cmdidx = 0;
7192 INIT_LIST_HEAD(&pring->txq);
7193 INIT_LIST_HEAD(&pring->txcmplq);
7194 INIT_LIST_HEAD(&pring->iocb_continueq);
James Smart9c2face2008-01-11 01:53:18 -05007195 INIT_LIST_HEAD(&pring->iocb_continue_saveq);
dea31012005-04-17 16:05:31 -05007196 INIT_LIST_HEAD(&pring->postbufq);
dea31012005-04-17 16:05:31 -05007197 }
James Smart2e0fef82007-06-17 19:56:36 -05007198 spin_unlock_irq(&phba->hbalock);
7199 return 1;
dea31012005-04-17 16:05:31 -05007200}
7201
James Smarte59058c2008-08-24 21:49:00 -04007202/**
James Smart04c68492009-05-22 14:52:52 -04007203 * lpfc_sli_mbox_sys_flush - Flush mailbox command sub-system
7204 * @phba: Pointer to HBA context object.
7205 *
7206 * This routine flushes the mailbox command subsystem. It will unconditionally
7207 * flush all the mailbox commands in the three possible stages in the mailbox
7208 * command sub-system: pending mailbox command queue; the outstanding mailbox
7209 * command; and completed mailbox command queue. It is caller's responsibility
7210 * to make sure that the driver is in the proper state to flush the mailbox
7211 * command sub-system. Namely, the posting of mailbox commands into the
7212 * pending mailbox command queue from the various clients must be stopped;
7213 * either the HBA is in a state that it will never works on the outstanding
7214 * mailbox command (such as in EEH or ERATT conditions) or the outstanding
7215 * mailbox command has been completed.
7216 **/
7217static void
7218lpfc_sli_mbox_sys_flush(struct lpfc_hba *phba)
7219{
7220 LIST_HEAD(completions);
7221 struct lpfc_sli *psli = &phba->sli;
7222 LPFC_MBOXQ_t *pmb;
7223 unsigned long iflag;
7224
7225 /* Flush all the mailbox commands in the mbox system */
7226 spin_lock_irqsave(&phba->hbalock, iflag);
7227 /* The pending mailbox command queue */
7228 list_splice_init(&phba->sli.mboxq, &completions);
7229 /* The outstanding active mailbox command */
7230 if (psli->mbox_active) {
7231 list_add_tail(&psli->mbox_active->list, &completions);
7232 psli->mbox_active = NULL;
7233 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
7234 }
7235 /* The completed mailbox command queue */
7236 list_splice_init(&phba->sli.mboxq_cmpl, &completions);
7237 spin_unlock_irqrestore(&phba->hbalock, iflag);
7238
7239 /* Return all flushed mailbox commands with MBX_NOT_FINISHED status */
7240 while (!list_empty(&completions)) {
7241 list_remove_head(&completions, pmb, LPFC_MBOXQ_t, list);
7242 pmb->u.mb.mbxStatus = MBX_NOT_FINISHED;
7243 if (pmb->mbox_cmpl)
7244 pmb->mbox_cmpl(phba, pmb);
7245 }
7246}
7247
7248/**
James Smart3621a712009-04-06 18:47:14 -04007249 * lpfc_sli_host_down - Vport cleanup function
James Smarte59058c2008-08-24 21:49:00 -04007250 * @vport: Pointer to virtual port object.
7251 *
7252 * lpfc_sli_host_down is called to clean up the resources
7253 * associated with a vport before destroying virtual
7254 * port data structures.
7255 * This function does following operations:
7256 * - Free discovery resources associated with this virtual
7257 * port.
7258 * - Free iocbs associated with this virtual port in
7259 * the txq.
7260 * - Send abort for all iocb commands associated with this
7261 * vport in txcmplq.
7262 *
7263 * This function is called with no lock held and always returns 1.
7264 **/
dea31012005-04-17 16:05:31 -05007265int
James Smart92d7f7b2007-06-17 19:56:38 -05007266lpfc_sli_host_down(struct lpfc_vport *vport)
7267{
James Smart858c9f62007-06-17 19:56:39 -05007268 LIST_HEAD(completions);
James Smart92d7f7b2007-06-17 19:56:38 -05007269 struct lpfc_hba *phba = vport->phba;
7270 struct lpfc_sli *psli = &phba->sli;
7271 struct lpfc_sli_ring *pring;
7272 struct lpfc_iocbq *iocb, *next_iocb;
James Smart92d7f7b2007-06-17 19:56:38 -05007273 int i;
7274 unsigned long flags = 0;
7275 uint16_t prev_pring_flag;
7276
7277 lpfc_cleanup_discovery_resources(vport);
7278
7279 spin_lock_irqsave(&phba->hbalock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -05007280 for (i = 0; i < psli->num_rings; i++) {
7281 pring = &psli->ring[i];
7282 prev_pring_flag = pring->flag;
James Smart5e9d9b82008-06-14 22:52:53 -04007283 /* Only slow rings */
7284 if (pring->ringno == LPFC_ELS_RING) {
James Smart858c9f62007-06-17 19:56:39 -05007285 pring->flag |= LPFC_DEFERRED_RING_EVENT;
James Smart5e9d9b82008-06-14 22:52:53 -04007286 /* Set the lpfc data pending flag */
7287 set_bit(LPFC_DATA_READY, &phba->data_flags);
7288 }
James Smart92d7f7b2007-06-17 19:56:38 -05007289 /*
7290 * Error everything on the txq since these iocbs have not been
7291 * given to the FW yet.
7292 */
James Smart92d7f7b2007-06-17 19:56:38 -05007293 list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
7294 if (iocb->vport != vport)
7295 continue;
James Smart858c9f62007-06-17 19:56:39 -05007296 list_move_tail(&iocb->list, &completions);
James Smart92d7f7b2007-06-17 19:56:38 -05007297 pring->txq_cnt--;
James Smart92d7f7b2007-06-17 19:56:38 -05007298 }
7299
7300 /* Next issue ABTS for everything on the txcmplq */
7301 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq,
7302 list) {
7303 if (iocb->vport != vport)
7304 continue;
7305 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
7306 }
7307
7308 pring->flag = prev_pring_flag;
7309 }
7310
7311 spin_unlock_irqrestore(&phba->hbalock, flags);
7312
James Smarta257bf92009-04-06 18:48:10 -04007313 /* Cancel all the IOCBs from the completions list */
7314 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
7315 IOERR_SLI_DOWN);
James Smart92d7f7b2007-06-17 19:56:38 -05007316 return 1;
7317}
7318
James Smarte59058c2008-08-24 21:49:00 -04007319/**
James Smart3621a712009-04-06 18:47:14 -04007320 * lpfc_sli_hba_down - Resource cleanup function for the HBA
James Smarte59058c2008-08-24 21:49:00 -04007321 * @phba: Pointer to HBA context object.
7322 *
7323 * This function cleans up all iocb, buffers, mailbox commands
7324 * while shutting down the HBA. This function is called with no
7325 * lock held and always returns 1.
7326 * This function does the following to cleanup driver resources:
7327 * - Free discovery resources for each virtual port
7328 * - Cleanup any pending fabric iocbs
7329 * - Iterate through the iocb txq and free each entry
7330 * in the list.
7331 * - Free up any buffer posted to the HBA
7332 * - Free mailbox commands in the mailbox queue.
7333 **/
James Smart92d7f7b2007-06-17 19:56:38 -05007334int
James Smart2e0fef82007-06-17 19:56:36 -05007335lpfc_sli_hba_down(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05007336{
James Smart2534ba72007-04-25 09:52:20 -04007337 LIST_HEAD(completions);
James Smart2e0fef82007-06-17 19:56:36 -05007338 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05007339 struct lpfc_sli_ring *pring;
James Smart0ff10d42008-01-11 01:52:36 -05007340 struct lpfc_dmabuf *buf_ptr;
dea31012005-04-17 16:05:31 -05007341 unsigned long flags = 0;
James Smart04c68492009-05-22 14:52:52 -04007342 int i;
7343
7344 /* Shutdown the mailbox command sub-system */
7345 lpfc_sli_mbox_sys_shutdown(phba);
dea31012005-04-17 16:05:31 -05007346
dea31012005-04-17 16:05:31 -05007347 lpfc_hba_down_prep(phba);
7348
James Smart92d7f7b2007-06-17 19:56:38 -05007349 lpfc_fabric_abort_hba(phba);
7350
James Smart2e0fef82007-06-17 19:56:36 -05007351 spin_lock_irqsave(&phba->hbalock, flags);
dea31012005-04-17 16:05:31 -05007352 for (i = 0; i < psli->num_rings; i++) {
7353 pring = &psli->ring[i];
James Smart5e9d9b82008-06-14 22:52:53 -04007354 /* Only slow rings */
7355 if (pring->ringno == LPFC_ELS_RING) {
James Smart858c9f62007-06-17 19:56:39 -05007356 pring->flag |= LPFC_DEFERRED_RING_EVENT;
James Smart5e9d9b82008-06-14 22:52:53 -04007357 /* Set the lpfc data pending flag */
7358 set_bit(LPFC_DATA_READY, &phba->data_flags);
7359 }
dea31012005-04-17 16:05:31 -05007360
7361 /*
7362 * Error everything on the txq since these iocbs have not been
7363 * given to the FW yet.
7364 */
James Smart2534ba72007-04-25 09:52:20 -04007365 list_splice_init(&pring->txq, &completions);
dea31012005-04-17 16:05:31 -05007366 pring->txq_cnt = 0;
7367
dea31012005-04-17 16:05:31 -05007368 }
James Smart2e0fef82007-06-17 19:56:36 -05007369 spin_unlock_irqrestore(&phba->hbalock, flags);
dea31012005-04-17 16:05:31 -05007370
James Smarta257bf92009-04-06 18:48:10 -04007371 /* Cancel all the IOCBs from the completions list */
7372 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
7373 IOERR_SLI_DOWN);
James Smart2534ba72007-04-25 09:52:20 -04007374
James Smart0ff10d42008-01-11 01:52:36 -05007375 spin_lock_irqsave(&phba->hbalock, flags);
7376 list_splice_init(&phba->elsbuf, &completions);
7377 phba->elsbuf_cnt = 0;
7378 phba->elsbuf_prev_cnt = 0;
7379 spin_unlock_irqrestore(&phba->hbalock, flags);
7380
7381 while (!list_empty(&completions)) {
7382 list_remove_head(&completions, buf_ptr,
7383 struct lpfc_dmabuf, list);
7384 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
7385 kfree(buf_ptr);
7386 }
7387
dea31012005-04-17 16:05:31 -05007388 /* Return any active mbox cmds */
7389 del_timer_sync(&psli->mbox_tmo);
James Smart92d7f7b2007-06-17 19:56:38 -05007390
James Smartda0436e2009-05-22 14:51:39 -04007391 spin_lock_irqsave(&phba->pport->work_port_lock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -05007392 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
James Smartda0436e2009-05-22 14:51:39 -04007393 spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -05007394
James Smartda0436e2009-05-22 14:51:39 -04007395 return 1;
7396}
James Smart92d7f7b2007-06-17 19:56:38 -05007397
James Smartda0436e2009-05-22 14:51:39 -04007398/**
James Smart3621a712009-04-06 18:47:14 -04007399 * lpfc_sli_pcimem_bcopy - SLI memory copy function
James Smarte59058c2008-08-24 21:49:00 -04007400 * @srcp: Source memory pointer.
7401 * @destp: Destination memory pointer.
7402 * @cnt: Number of words required to be copied.
7403 *
7404 * This function is used for copying data between driver memory
7405 * and the SLI memory. This function also changes the endianness
7406 * of each word if native endianness is different from SLI
7407 * endianness. This function can be called with or without
7408 * lock.
7409 **/
dea31012005-04-17 16:05:31 -05007410void
7411lpfc_sli_pcimem_bcopy(void *srcp, void *destp, uint32_t cnt)
7412{
7413 uint32_t *src = srcp;
7414 uint32_t *dest = destp;
7415 uint32_t ldata;
7416 int i;
7417
7418 for (i = 0; i < (int)cnt; i += sizeof (uint32_t)) {
7419 ldata = *src;
7420 ldata = le32_to_cpu(ldata);
7421 *dest = ldata;
7422 src++;
7423 dest++;
7424 }
7425}
7426
James Smarte59058c2008-08-24 21:49:00 -04007427
7428/**
James Smarta0c87cb2009-07-19 10:01:10 -04007429 * lpfc_sli_bemem_bcopy - SLI memory copy function
7430 * @srcp: Source memory pointer.
7431 * @destp: Destination memory pointer.
7432 * @cnt: Number of words required to be copied.
7433 *
7434 * This function is used for copying data between a data structure
7435 * with big endian representation to local endianness.
7436 * This function can be called with or without lock.
7437 **/
7438void
7439lpfc_sli_bemem_bcopy(void *srcp, void *destp, uint32_t cnt)
7440{
7441 uint32_t *src = srcp;
7442 uint32_t *dest = destp;
7443 uint32_t ldata;
7444 int i;
7445
7446 for (i = 0; i < (int)cnt; i += sizeof(uint32_t)) {
7447 ldata = *src;
7448 ldata = be32_to_cpu(ldata);
7449 *dest = ldata;
7450 src++;
7451 dest++;
7452 }
7453}
7454
7455/**
James Smart3621a712009-04-06 18:47:14 -04007456 * lpfc_sli_ringpostbuf_put - Function to add a buffer to postbufq
James Smarte59058c2008-08-24 21:49:00 -04007457 * @phba: Pointer to HBA context object.
7458 * @pring: Pointer to driver SLI ring object.
7459 * @mp: Pointer to driver buffer object.
7460 *
7461 * This function is called with no lock held.
7462 * It always return zero after adding the buffer to the postbufq
7463 * buffer list.
7464 **/
dea31012005-04-17 16:05:31 -05007465int
James Smart2e0fef82007-06-17 19:56:36 -05007466lpfc_sli_ringpostbuf_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
7467 struct lpfc_dmabuf *mp)
dea31012005-04-17 16:05:31 -05007468{
7469 /* Stick struct lpfc_dmabuf at end of postbufq so driver can look it up
7470 later */
James Smart2e0fef82007-06-17 19:56:36 -05007471 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05007472 list_add_tail(&mp->list, &pring->postbufq);
dea31012005-04-17 16:05:31 -05007473 pring->postbufq_cnt++;
James Smart2e0fef82007-06-17 19:56:36 -05007474 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05007475 return 0;
7476}
7477
James Smarte59058c2008-08-24 21:49:00 -04007478/**
James Smart3621a712009-04-06 18:47:14 -04007479 * lpfc_sli_get_buffer_tag - allocates a tag for a CMD_QUE_XRI64_CX buffer
James Smarte59058c2008-08-24 21:49:00 -04007480 * @phba: Pointer to HBA context object.
7481 *
7482 * When HBQ is enabled, buffers are searched based on tags. This function
7483 * allocates a tag for buffer posted using CMD_QUE_XRI64_CX iocb. The
7484 * tag is bit wise or-ed with QUE_BUFTAG_BIT to make sure that the tag
7485 * does not conflict with tags of buffer posted for unsolicited events.
7486 * The function returns the allocated tag. The function is called with
7487 * no locks held.
7488 **/
James Smart76bb24e2007-10-27 13:38:00 -04007489uint32_t
7490lpfc_sli_get_buffer_tag(struct lpfc_hba *phba)
7491{
7492 spin_lock_irq(&phba->hbalock);
7493 phba->buffer_tag_count++;
7494 /*
7495 * Always set the QUE_BUFTAG_BIT to distiguish between
7496 * a tag assigned by HBQ.
7497 */
7498 phba->buffer_tag_count |= QUE_BUFTAG_BIT;
7499 spin_unlock_irq(&phba->hbalock);
7500 return phba->buffer_tag_count;
7501}
7502
James Smarte59058c2008-08-24 21:49:00 -04007503/**
James Smart3621a712009-04-06 18:47:14 -04007504 * lpfc_sli_ring_taggedbuf_get - find HBQ buffer associated with given tag
James Smarte59058c2008-08-24 21:49:00 -04007505 * @phba: Pointer to HBA context object.
7506 * @pring: Pointer to driver SLI ring object.
7507 * @tag: Buffer tag.
7508 *
7509 * Buffers posted using CMD_QUE_XRI64_CX iocb are in pring->postbufq
7510 * list. After HBA DMA data to these buffers, CMD_IOCB_RET_XRI64_CX
7511 * iocb is posted to the response ring with the tag of the buffer.
7512 * This function searches the pring->postbufq list using the tag
7513 * to find buffer associated with CMD_IOCB_RET_XRI64_CX
7514 * iocb. If the buffer is found then lpfc_dmabuf object of the
7515 * buffer is returned to the caller else NULL is returned.
7516 * This function is called with no lock held.
7517 **/
James Smart76bb24e2007-10-27 13:38:00 -04007518struct lpfc_dmabuf *
7519lpfc_sli_ring_taggedbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
7520 uint32_t tag)
7521{
7522 struct lpfc_dmabuf *mp, *next_mp;
7523 struct list_head *slp = &pring->postbufq;
7524
Lucas De Marchi25985ed2011-03-30 22:57:33 -03007525 /* Search postbufq, from the beginning, looking for a match on tag */
James Smart76bb24e2007-10-27 13:38:00 -04007526 spin_lock_irq(&phba->hbalock);
7527 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
7528 if (mp->buffer_tag == tag) {
7529 list_del_init(&mp->list);
7530 pring->postbufq_cnt--;
7531 spin_unlock_irq(&phba->hbalock);
7532 return mp;
7533 }
7534 }
7535
7536 spin_unlock_irq(&phba->hbalock);
7537 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartd7c255b2008-08-24 21:50:00 -04007538 "0402 Cannot find virtual addr for buffer tag on "
James Smart76bb24e2007-10-27 13:38:00 -04007539 "ring %d Data x%lx x%p x%p x%x\n",
7540 pring->ringno, (unsigned long) tag,
7541 slp->next, slp->prev, pring->postbufq_cnt);
7542
7543 return NULL;
7544}
dea31012005-04-17 16:05:31 -05007545
James Smarte59058c2008-08-24 21:49:00 -04007546/**
James Smart3621a712009-04-06 18:47:14 -04007547 * lpfc_sli_ringpostbuf_get - search buffers for unsolicited CT and ELS events
James Smarte59058c2008-08-24 21:49:00 -04007548 * @phba: Pointer to HBA context object.
7549 * @pring: Pointer to driver SLI ring object.
7550 * @phys: DMA address of the buffer.
7551 *
7552 * This function searches the buffer list using the dma_address
7553 * of unsolicited event to find the driver's lpfc_dmabuf object
7554 * corresponding to the dma_address. The function returns the
7555 * lpfc_dmabuf object if a buffer is found else it returns NULL.
7556 * This function is called by the ct and els unsolicited event
7557 * handlers to get the buffer associated with the unsolicited
7558 * event.
7559 *
7560 * This function is called with no lock held.
7561 **/
dea31012005-04-17 16:05:31 -05007562struct lpfc_dmabuf *
7563lpfc_sli_ringpostbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
7564 dma_addr_t phys)
7565{
7566 struct lpfc_dmabuf *mp, *next_mp;
7567 struct list_head *slp = &pring->postbufq;
7568
Lucas De Marchi25985ed2011-03-30 22:57:33 -03007569 /* Search postbufq, from the beginning, looking for a match on phys */
James Smart2e0fef82007-06-17 19:56:36 -05007570 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05007571 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
7572 if (mp->phys == phys) {
7573 list_del_init(&mp->list);
7574 pring->postbufq_cnt--;
James Smart2e0fef82007-06-17 19:56:36 -05007575 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05007576 return mp;
7577 }
7578 }
7579
James Smart2e0fef82007-06-17 19:56:36 -05007580 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05007581 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04007582 "0410 Cannot find virtual addr for mapped buf on "
dea31012005-04-17 16:05:31 -05007583 "ring %d Data x%llx x%p x%p x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04007584 pring->ringno, (unsigned long long)phys,
dea31012005-04-17 16:05:31 -05007585 slp->next, slp->prev, pring->postbufq_cnt);
7586 return NULL;
7587}
7588
James Smarte59058c2008-08-24 21:49:00 -04007589/**
James Smart3621a712009-04-06 18:47:14 -04007590 * lpfc_sli_abort_els_cmpl - Completion handler for the els abort iocbs
James Smarte59058c2008-08-24 21:49:00 -04007591 * @phba: Pointer to HBA context object.
7592 * @cmdiocb: Pointer to driver command iocb object.
7593 * @rspiocb: Pointer to driver response iocb object.
7594 *
7595 * This function is the completion handler for the abort iocbs for
7596 * ELS commands. This function is called from the ELS ring event
7597 * handler with no lock held. This function frees memory resources
7598 * associated with the abort iocb.
7599 **/
dea31012005-04-17 16:05:31 -05007600static void
James Smart2e0fef82007-06-17 19:56:36 -05007601lpfc_sli_abort_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
7602 struct lpfc_iocbq *rspiocb)
dea31012005-04-17 16:05:31 -05007603{
James Smart2e0fef82007-06-17 19:56:36 -05007604 IOCB_t *irsp = &rspiocb->iocb;
James Smart2680eea2007-04-25 09:52:55 -04007605 uint16_t abort_iotag, abort_context;
James Smart92d7f7b2007-06-17 19:56:38 -05007606 struct lpfc_iocbq *abort_iocb;
James Smart2680eea2007-04-25 09:52:55 -04007607 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
7608
7609 abort_iocb = NULL;
James Smart2680eea2007-04-25 09:52:55 -04007610
7611 if (irsp->ulpStatus) {
7612 abort_context = cmdiocb->iocb.un.acxri.abortContextTag;
7613 abort_iotag = cmdiocb->iocb.un.acxri.abortIoTag;
7614
James Smart2e0fef82007-06-17 19:56:36 -05007615 spin_lock_irq(&phba->hbalock);
James Smart45ed1192009-10-02 15:17:02 -04007616 if (phba->sli_rev < LPFC_SLI_REV4) {
7617 if (abort_iotag != 0 &&
7618 abort_iotag <= phba->sli.last_iotag)
7619 abort_iocb =
7620 phba->sli.iocbq_lookup[abort_iotag];
7621 } else
7622 /* For sli4 the abort_tag is the XRI,
7623 * so the abort routine puts the iotag of the iocb
7624 * being aborted in the context field of the abort
7625 * IOCB.
7626 */
7627 abort_iocb = phba->sli.iocbq_lookup[abort_context];
James Smart2680eea2007-04-25 09:52:55 -04007628
James Smart2680eea2007-04-25 09:52:55 -04007629 /*
James Smart58da1ff2008-04-07 10:15:56 -04007630 * If the iocb is not found in Firmware queue the iocb
7631 * might have completed already. Do not free it again.
7632 */
James Smart9b379602008-04-07 10:16:00 -04007633 if (irsp->ulpStatus == IOSTAT_LOCAL_REJECT) {
James Smart45ed1192009-10-02 15:17:02 -04007634 if (irsp->un.ulpWord[4] != IOERR_NO_XRI) {
7635 spin_unlock_irq(&phba->hbalock);
7636 lpfc_sli_release_iocbq(phba, cmdiocb);
7637 return;
7638 }
7639 /* For SLI4 the ulpContext field for abort IOCB
7640 * holds the iotag of the IOCB being aborted so
7641 * the local abort_context needs to be reset to
7642 * match the aborted IOCBs ulpContext.
7643 */
7644 if (abort_iocb && phba->sli_rev == LPFC_SLI_REV4)
7645 abort_context = abort_iocb->iocb.ulpContext;
James Smart58da1ff2008-04-07 10:15:56 -04007646 }
James Smart2a9bf3d2010-06-07 15:24:45 -04007647
7648 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS | LOG_SLI,
7649 "0327 Cannot abort els iocb %p "
7650 "with tag %x context %x, abort status %x, "
7651 "abort code %x\n",
7652 abort_iocb, abort_iotag, abort_context,
7653 irsp->ulpStatus, irsp->un.ulpWord[4]);
James Smart58da1ff2008-04-07 10:15:56 -04007654 /*
James Smart2680eea2007-04-25 09:52:55 -04007655 * make sure we have the right iocbq before taking it
7656 * off the txcmplq and try to call completion routine.
7657 */
James Smart2e0fef82007-06-17 19:56:36 -05007658 if (!abort_iocb ||
7659 abort_iocb->iocb.ulpContext != abort_context ||
7660 (abort_iocb->iocb_flag & LPFC_DRIVER_ABORTED) == 0)
7661 spin_unlock_irq(&phba->hbalock);
James Smart341af102010-01-26 23:07:37 -05007662 else if (phba->sli_rev < LPFC_SLI_REV4) {
7663 /*
7664 * leave the SLI4 aborted command on the txcmplq
7665 * list and the command complete WCQE's XB bit
7666 * will tell whether the SGL (XRI) can be released
7667 * immediately or to the aborted SGL list for the
7668 * following abort XRI from the HBA.
7669 */
James Smart92d7f7b2007-06-17 19:56:38 -05007670 list_del_init(&abort_iocb->list);
James Smart2a9bf3d2010-06-07 15:24:45 -04007671 if (abort_iocb->iocb_flag & LPFC_IO_ON_Q) {
7672 abort_iocb->iocb_flag &= ~LPFC_IO_ON_Q;
7673 pring->txcmplq_cnt--;
7674 }
James Smart2680eea2007-04-25 09:52:55 -04007675
James Smart0ff10d42008-01-11 01:52:36 -05007676 /* Firmware could still be in progress of DMAing
7677 * payload, so don't free data buffer till after
7678 * a hbeat.
7679 */
7680 abort_iocb->iocb_flag |= LPFC_DELAY_MEM_FREE;
James Smart92d7f7b2007-06-17 19:56:38 -05007681 abort_iocb->iocb_flag &= ~LPFC_DRIVER_ABORTED;
James Smart341af102010-01-26 23:07:37 -05007682 spin_unlock_irq(&phba->hbalock);
7683
James Smart92d7f7b2007-06-17 19:56:38 -05007684 abort_iocb->iocb.ulpStatus = IOSTAT_LOCAL_REJECT;
James Smart341af102010-01-26 23:07:37 -05007685 abort_iocb->iocb.un.ulpWord[4] = IOERR_ABORT_REQUESTED;
James Smart92d7f7b2007-06-17 19:56:38 -05007686 (abort_iocb->iocb_cmpl)(phba, abort_iocb, abort_iocb);
James Smart49198b32010-04-06 15:04:33 -04007687 } else
7688 spin_unlock_irq(&phba->hbalock);
James Smart2680eea2007-04-25 09:52:55 -04007689 }
7690
James Bottomley604a3e32005-10-29 10:28:33 -05007691 lpfc_sli_release_iocbq(phba, cmdiocb);
dea31012005-04-17 16:05:31 -05007692 return;
7693}
7694
James Smarte59058c2008-08-24 21:49:00 -04007695/**
James Smart3621a712009-04-06 18:47:14 -04007696 * lpfc_ignore_els_cmpl - Completion handler for aborted ELS command
James Smarte59058c2008-08-24 21:49:00 -04007697 * @phba: Pointer to HBA context object.
7698 * @cmdiocb: Pointer to driver command iocb object.
7699 * @rspiocb: Pointer to driver response iocb object.
7700 *
7701 * The function is called from SLI ring event handler with no
7702 * lock held. This function is the completion handler for ELS commands
7703 * which are aborted. The function frees memory resources used for
7704 * the aborted ELS commands.
7705 **/
James Smart92d7f7b2007-06-17 19:56:38 -05007706static void
7707lpfc_ignore_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
7708 struct lpfc_iocbq *rspiocb)
7709{
7710 IOCB_t *irsp = &rspiocb->iocb;
7711
7712 /* ELS cmd tag <ulpIoTag> completes */
7713 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
James Smartd7c255b2008-08-24 21:50:00 -04007714 "0139 Ignoring ELS cmd tag x%x completion Data: "
James Smart92d7f7b2007-06-17 19:56:38 -05007715 "x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04007716 irsp->ulpIoTag, irsp->ulpStatus,
James Smart92d7f7b2007-06-17 19:56:38 -05007717 irsp->un.ulpWord[4], irsp->ulpTimeout);
James Smart858c9f62007-06-17 19:56:39 -05007718 if (cmdiocb->iocb.ulpCommand == CMD_GEN_REQUEST64_CR)
7719 lpfc_ct_free_iocb(phba, cmdiocb);
7720 else
7721 lpfc_els_free_iocb(phba, cmdiocb);
James Smart92d7f7b2007-06-17 19:56:38 -05007722 return;
7723}
7724
James Smarte59058c2008-08-24 21:49:00 -04007725/**
James Smart5af5eee2010-10-22 11:06:38 -04007726 * lpfc_sli_abort_iotag_issue - Issue abort for a command iocb
James Smarte59058c2008-08-24 21:49:00 -04007727 * @phba: Pointer to HBA context object.
7728 * @pring: Pointer to driver SLI ring object.
7729 * @cmdiocb: Pointer to driver command iocb object.
7730 *
James Smart5af5eee2010-10-22 11:06:38 -04007731 * This function issues an abort iocb for the provided command iocb down to
7732 * the port. Other than the case the outstanding command iocb is an abort
7733 * request, this function issues abort out unconditionally. This function is
7734 * called with hbalock held. The function returns 0 when it fails due to
7735 * memory allocation failure or when the command iocb is an abort request.
James Smarte59058c2008-08-24 21:49:00 -04007736 **/
James Smart5af5eee2010-10-22 11:06:38 -04007737static int
7738lpfc_sli_abort_iotag_issue(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -05007739 struct lpfc_iocbq *cmdiocb)
dea31012005-04-17 16:05:31 -05007740{
James Smart2e0fef82007-06-17 19:56:36 -05007741 struct lpfc_vport *vport = cmdiocb->vport;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007742 struct lpfc_iocbq *abtsiocbp;
dea31012005-04-17 16:05:31 -05007743 IOCB_t *icmd = NULL;
7744 IOCB_t *iabt = NULL;
James Smart5af5eee2010-10-22 11:06:38 -04007745 int retval;
James Smart07951072007-04-25 09:51:38 -04007746
James Smart92d7f7b2007-06-17 19:56:38 -05007747 /*
7748 * There are certain command types we don't want to abort. And we
7749 * don't want to abort commands that are already in the process of
7750 * being aborted.
James Smart07951072007-04-25 09:51:38 -04007751 */
7752 icmd = &cmdiocb->iocb;
James Smart2e0fef82007-06-17 19:56:36 -05007753 if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
James Smart92d7f7b2007-06-17 19:56:38 -05007754 icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
7755 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
James Smart07951072007-04-25 09:51:38 -04007756 return 0;
7757
dea31012005-04-17 16:05:31 -05007758 /* issue ABTS for this IOCB based on iotag */
James Smart92d7f7b2007-06-17 19:56:38 -05007759 abtsiocbp = __lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05007760 if (abtsiocbp == NULL)
7761 return 0;
dea31012005-04-17 16:05:31 -05007762
James Smart07951072007-04-25 09:51:38 -04007763 /* This signals the response to set the correct status
James Smart341af102010-01-26 23:07:37 -05007764 * before calling the completion handler
James Smart07951072007-04-25 09:51:38 -04007765 */
7766 cmdiocb->iocb_flag |= LPFC_DRIVER_ABORTED;
7767
dea31012005-04-17 16:05:31 -05007768 iabt = &abtsiocbp->iocb;
James Smart07951072007-04-25 09:51:38 -04007769 iabt->un.acxri.abortType = ABORT_TYPE_ABTS;
7770 iabt->un.acxri.abortContextTag = icmd->ulpContext;
James Smart45ed1192009-10-02 15:17:02 -04007771 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smartda0436e2009-05-22 14:51:39 -04007772 iabt->un.acxri.abortIoTag = cmdiocb->sli4_xritag;
James Smart45ed1192009-10-02 15:17:02 -04007773 iabt->un.acxri.abortContextTag = cmdiocb->iotag;
7774 }
James Smartda0436e2009-05-22 14:51:39 -04007775 else
7776 iabt->un.acxri.abortIoTag = icmd->ulpIoTag;
dea31012005-04-17 16:05:31 -05007777 iabt->ulpLe = 1;
James Smart07951072007-04-25 09:51:38 -04007778 iabt->ulpClass = icmd->ulpClass;
dea31012005-04-17 16:05:31 -05007779
James Smart5ffc2662009-11-18 15:39:44 -05007780 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
7781 abtsiocbp->fcp_wqidx = cmdiocb->fcp_wqidx;
James Smart341af102010-01-26 23:07:37 -05007782 if (cmdiocb->iocb_flag & LPFC_IO_FCP)
7783 abtsiocbp->iocb_flag |= LPFC_USE_FCPWQIDX;
James Smart5ffc2662009-11-18 15:39:44 -05007784
James Smart2e0fef82007-06-17 19:56:36 -05007785 if (phba->link_state >= LPFC_LINK_UP)
James Smart07951072007-04-25 09:51:38 -04007786 iabt->ulpCommand = CMD_ABORT_XRI_CN;
7787 else
7788 iabt->ulpCommand = CMD_CLOSE_XRI_CN;
7789
7790 abtsiocbp->iocb_cmpl = lpfc_sli_abort_els_cmpl;
James Smart5b8bd0c2007-04-25 09:52:49 -04007791
James Smarte8b62012007-08-02 11:10:09 -04007792 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
7793 "0339 Abort xri x%x, original iotag x%x, "
7794 "abort cmd iotag x%x\n",
James Smart2a9bf3d2010-06-07 15:24:45 -04007795 iabt->un.acxri.abortIoTag,
James Smarte8b62012007-08-02 11:10:09 -04007796 iabt->un.acxri.abortContextTag,
James Smart2a9bf3d2010-06-07 15:24:45 -04007797 abtsiocbp->iotag);
James Smartda0436e2009-05-22 14:51:39 -04007798 retval = __lpfc_sli_issue_iocb(phba, pring->ringno, abtsiocbp, 0);
James Smart07951072007-04-25 09:51:38 -04007799
James Smartd7c255b2008-08-24 21:50:00 -04007800 if (retval)
7801 __lpfc_sli_release_iocbq(phba, abtsiocbp);
James Smart5af5eee2010-10-22 11:06:38 -04007802
7803 /*
7804 * Caller to this routine should check for IOCB_ERROR
7805 * and handle it properly. This routine no longer removes
7806 * iocb off txcmplq and call compl in case of IOCB_ERROR.
7807 */
7808 return retval;
7809}
7810
7811/**
7812 * lpfc_sli_issue_abort_iotag - Abort function for a command iocb
7813 * @phba: Pointer to HBA context object.
7814 * @pring: Pointer to driver SLI ring object.
7815 * @cmdiocb: Pointer to driver command iocb object.
7816 *
7817 * This function issues an abort iocb for the provided command iocb. In case
7818 * of unloading, the abort iocb will not be issued to commands on the ELS
7819 * ring. Instead, the callback function shall be changed to those commands
7820 * so that nothing happens when them finishes. This function is called with
7821 * hbalock held. The function returns 0 when the command iocb is an abort
7822 * request.
7823 **/
7824int
7825lpfc_sli_issue_abort_iotag(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
7826 struct lpfc_iocbq *cmdiocb)
7827{
7828 struct lpfc_vport *vport = cmdiocb->vport;
7829 int retval = IOCB_ERROR;
7830 IOCB_t *icmd = NULL;
7831
7832 /*
7833 * There are certain command types we don't want to abort. And we
7834 * don't want to abort commands that are already in the process of
7835 * being aborted.
7836 */
7837 icmd = &cmdiocb->iocb;
7838 if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
7839 icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
7840 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
7841 return 0;
7842
7843 /*
7844 * If we're unloading, don't abort iocb on the ELS ring, but change
7845 * the callback so that nothing happens when it finishes.
7846 */
7847 if ((vport->load_flag & FC_UNLOADING) &&
7848 (pring->ringno == LPFC_ELS_RING)) {
7849 if (cmdiocb->iocb_flag & LPFC_IO_FABRIC)
7850 cmdiocb->fabric_iocb_cmpl = lpfc_ignore_els_cmpl;
7851 else
7852 cmdiocb->iocb_cmpl = lpfc_ignore_els_cmpl;
7853 goto abort_iotag_exit;
7854 }
7855
7856 /* Now, we try to issue the abort to the cmdiocb out */
7857 retval = lpfc_sli_abort_iotag_issue(phba, pring, cmdiocb);
7858
James Smart07951072007-04-25 09:51:38 -04007859abort_iotag_exit:
James Smart2e0fef82007-06-17 19:56:36 -05007860 /*
7861 * Caller to this routine should check for IOCB_ERROR
7862 * and handle it properly. This routine no longer removes
7863 * iocb off txcmplq and call compl in case of IOCB_ERROR.
James Smart07951072007-04-25 09:51:38 -04007864 */
James Smart2e0fef82007-06-17 19:56:36 -05007865 return retval;
dea31012005-04-17 16:05:31 -05007866}
7867
James Smarte59058c2008-08-24 21:49:00 -04007868/**
James Smart5af5eee2010-10-22 11:06:38 -04007869 * lpfc_sli_iocb_ring_abort - Unconditionally abort all iocbs on an iocb ring
7870 * @phba: Pointer to HBA context object.
7871 * @pring: Pointer to driver SLI ring object.
7872 *
7873 * This function aborts all iocbs in the given ring and frees all the iocb
7874 * objects in txq. This function issues abort iocbs unconditionally for all
7875 * the iocb commands in txcmplq. The iocbs in the txcmplq is not guaranteed
7876 * to complete before the return of this function. The caller is not required
7877 * to hold any locks.
7878 **/
7879static void
7880lpfc_sli_iocb_ring_abort(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
7881{
7882 LIST_HEAD(completions);
7883 struct lpfc_iocbq *iocb, *next_iocb;
7884
7885 if (pring->ringno == LPFC_ELS_RING)
7886 lpfc_fabric_abort_hba(phba);
7887
7888 spin_lock_irq(&phba->hbalock);
7889
7890 /* Take off all the iocbs on txq for cancelling */
7891 list_splice_init(&pring->txq, &completions);
7892 pring->txq_cnt = 0;
7893
7894 /* Next issue ABTS for everything on the txcmplq */
7895 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
7896 lpfc_sli_abort_iotag_issue(phba, pring, iocb);
7897
7898 spin_unlock_irq(&phba->hbalock);
7899
7900 /* Cancel all the IOCBs from the completions list */
7901 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
7902 IOERR_SLI_ABORTED);
7903}
7904
7905/**
7906 * lpfc_sli_hba_iocb_abort - Abort all iocbs to an hba.
7907 * @phba: pointer to lpfc HBA data structure.
7908 *
7909 * This routine will abort all pending and outstanding iocbs to an HBA.
7910 **/
7911void
7912lpfc_sli_hba_iocb_abort(struct lpfc_hba *phba)
7913{
7914 struct lpfc_sli *psli = &phba->sli;
7915 struct lpfc_sli_ring *pring;
7916 int i;
7917
7918 for (i = 0; i < psli->num_rings; i++) {
7919 pring = &psli->ring[i];
7920 lpfc_sli_iocb_ring_abort(phba, pring);
7921 }
7922}
7923
7924/**
James Smart3621a712009-04-06 18:47:14 -04007925 * lpfc_sli_validate_fcp_iocb - find commands associated with a vport or LUN
James Smarte59058c2008-08-24 21:49:00 -04007926 * @iocbq: Pointer to driver iocb object.
7927 * @vport: Pointer to driver virtual port object.
7928 * @tgt_id: SCSI ID of the target.
7929 * @lun_id: LUN ID of the scsi device.
7930 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST
7931 *
James Smart3621a712009-04-06 18:47:14 -04007932 * This function acts as an iocb filter for functions which abort or count
James Smarte59058c2008-08-24 21:49:00 -04007933 * all FCP iocbs pending on a lun/SCSI target/SCSI host. It will return
7934 * 0 if the filtering criteria is met for the given iocb and will return
7935 * 1 if the filtering criteria is not met.
7936 * If ctx_cmd == LPFC_CTX_LUN, the function returns 0 only if the
7937 * given iocb is for the SCSI device specified by vport, tgt_id and
7938 * lun_id parameter.
7939 * If ctx_cmd == LPFC_CTX_TGT, the function returns 0 only if the
7940 * given iocb is for the SCSI target specified by vport and tgt_id
7941 * parameters.
7942 * If ctx_cmd == LPFC_CTX_HOST, the function returns 0 only if the
7943 * given iocb is for the SCSI host associated with the given vport.
7944 * This function is called with no locks held.
7945 **/
dea31012005-04-17 16:05:31 -05007946static int
James Smart51ef4c22007-08-02 11:10:31 -04007947lpfc_sli_validate_fcp_iocb(struct lpfc_iocbq *iocbq, struct lpfc_vport *vport,
7948 uint16_t tgt_id, uint64_t lun_id,
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007949 lpfc_ctx_cmd ctx_cmd)
dea31012005-04-17 16:05:31 -05007950{
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007951 struct lpfc_scsi_buf *lpfc_cmd;
dea31012005-04-17 16:05:31 -05007952 int rc = 1;
7953
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007954 if (!(iocbq->iocb_flag & LPFC_IO_FCP))
7955 return rc;
7956
James Smart51ef4c22007-08-02 11:10:31 -04007957 if (iocbq->vport != vport)
7958 return rc;
7959
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007960 lpfc_cmd = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007961
James Smart495a7142008-06-14 22:52:59 -04007962 if (lpfc_cmd->pCmd == NULL)
dea31012005-04-17 16:05:31 -05007963 return rc;
7964
7965 switch (ctx_cmd) {
7966 case LPFC_CTX_LUN:
James Smart495a7142008-06-14 22:52:59 -04007967 if ((lpfc_cmd->rdata->pnode) &&
7968 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id) &&
7969 (scsilun_to_int(&lpfc_cmd->fcp_cmnd->fcp_lun) == lun_id))
dea31012005-04-17 16:05:31 -05007970 rc = 0;
7971 break;
7972 case LPFC_CTX_TGT:
James Smart495a7142008-06-14 22:52:59 -04007973 if ((lpfc_cmd->rdata->pnode) &&
7974 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id))
dea31012005-04-17 16:05:31 -05007975 rc = 0;
7976 break;
dea31012005-04-17 16:05:31 -05007977 case LPFC_CTX_HOST:
7978 rc = 0;
7979 break;
7980 default:
7981 printk(KERN_ERR "%s: Unknown context cmd type, value %d\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07007982 __func__, ctx_cmd);
dea31012005-04-17 16:05:31 -05007983 break;
7984 }
7985
7986 return rc;
7987}
7988
James Smarte59058c2008-08-24 21:49:00 -04007989/**
James Smart3621a712009-04-06 18:47:14 -04007990 * lpfc_sli_sum_iocb - Function to count the number of FCP iocbs pending
James Smarte59058c2008-08-24 21:49:00 -04007991 * @vport: Pointer to virtual port.
7992 * @tgt_id: SCSI ID of the target.
7993 * @lun_id: LUN ID of the scsi device.
7994 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
7995 *
7996 * This function returns number of FCP commands pending for the vport.
7997 * When ctx_cmd == LPFC_CTX_LUN, the function returns number of FCP
7998 * commands pending on the vport associated with SCSI device specified
7999 * by tgt_id and lun_id parameters.
8000 * When ctx_cmd == LPFC_CTX_TGT, the function returns number of FCP
8001 * commands pending on the vport associated with SCSI target specified
8002 * by tgt_id parameter.
8003 * When ctx_cmd == LPFC_CTX_HOST, the function returns number of FCP
8004 * commands pending on the vport.
8005 * This function returns the number of iocbs which satisfy the filter.
8006 * This function is called without any lock held.
8007 **/
dea31012005-04-17 16:05:31 -05008008int
James Smart51ef4c22007-08-02 11:10:31 -04008009lpfc_sli_sum_iocb(struct lpfc_vport *vport, uint16_t tgt_id, uint64_t lun_id,
8010 lpfc_ctx_cmd ctx_cmd)
dea31012005-04-17 16:05:31 -05008011{
James Smart51ef4c22007-08-02 11:10:31 -04008012 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04008013 struct lpfc_iocbq *iocbq;
8014 int sum, i;
dea31012005-04-17 16:05:31 -05008015
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04008016 for (i = 1, sum = 0; i <= phba->sli.last_iotag; i++) {
8017 iocbq = phba->sli.iocbq_lookup[i];
dea31012005-04-17 16:05:31 -05008018
James Smart51ef4c22007-08-02 11:10:31 -04008019 if (lpfc_sli_validate_fcp_iocb (iocbq, vport, tgt_id, lun_id,
8020 ctx_cmd) == 0)
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04008021 sum++;
dea31012005-04-17 16:05:31 -05008022 }
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04008023
dea31012005-04-17 16:05:31 -05008024 return sum;
8025}
8026
James Smarte59058c2008-08-24 21:49:00 -04008027/**
James Smart3621a712009-04-06 18:47:14 -04008028 * lpfc_sli_abort_fcp_cmpl - Completion handler function for aborted FCP IOCBs
James Smarte59058c2008-08-24 21:49:00 -04008029 * @phba: Pointer to HBA context object
8030 * @cmdiocb: Pointer to command iocb object.
8031 * @rspiocb: Pointer to response iocb object.
8032 *
8033 * This function is called when an aborted FCP iocb completes. This
8034 * function is called by the ring event handler with no lock held.
8035 * This function frees the iocb.
8036 **/
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -04008037void
James Smart2e0fef82007-06-17 19:56:36 -05008038lpfc_sli_abort_fcp_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
8039 struct lpfc_iocbq *rspiocb)
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -04008040{
James Bottomley604a3e32005-10-29 10:28:33 -05008041 lpfc_sli_release_iocbq(phba, cmdiocb);
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -04008042 return;
8043}
8044
James Smarte59058c2008-08-24 21:49:00 -04008045/**
James Smart3621a712009-04-06 18:47:14 -04008046 * lpfc_sli_abort_iocb - issue abort for all commands on a host/target/LUN
James Smarte59058c2008-08-24 21:49:00 -04008047 * @vport: Pointer to virtual port.
8048 * @pring: Pointer to driver SLI ring object.
8049 * @tgt_id: SCSI ID of the target.
8050 * @lun_id: LUN ID of the scsi device.
8051 * @abort_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
8052 *
8053 * This function sends an abort command for every SCSI command
8054 * associated with the given virtual port pending on the ring
8055 * filtered by lpfc_sli_validate_fcp_iocb function.
8056 * When abort_cmd == LPFC_CTX_LUN, the function sends abort only to the
8057 * FCP iocbs associated with lun specified by tgt_id and lun_id
8058 * parameters
8059 * When abort_cmd == LPFC_CTX_TGT, the function sends abort only to the
8060 * FCP iocbs associated with SCSI target specified by tgt_id parameter.
8061 * When abort_cmd == LPFC_CTX_HOST, the function sends abort to all
8062 * FCP iocbs associated with virtual port.
8063 * This function returns number of iocbs it failed to abort.
8064 * This function is called with no locks held.
8065 **/
dea31012005-04-17 16:05:31 -05008066int
James Smart51ef4c22007-08-02 11:10:31 -04008067lpfc_sli_abort_iocb(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
8068 uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd abort_cmd)
dea31012005-04-17 16:05:31 -05008069{
James Smart51ef4c22007-08-02 11:10:31 -04008070 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04008071 struct lpfc_iocbq *iocbq;
8072 struct lpfc_iocbq *abtsiocb;
dea31012005-04-17 16:05:31 -05008073 IOCB_t *cmd = NULL;
dea31012005-04-17 16:05:31 -05008074 int errcnt = 0, ret_val = 0;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04008075 int i;
dea31012005-04-17 16:05:31 -05008076
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04008077 for (i = 1; i <= phba->sli.last_iotag; i++) {
8078 iocbq = phba->sli.iocbq_lookup[i];
dea31012005-04-17 16:05:31 -05008079
James Smart51ef4c22007-08-02 11:10:31 -04008080 if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
James Smart2e0fef82007-06-17 19:56:36 -05008081 abort_cmd) != 0)
dea31012005-04-17 16:05:31 -05008082 continue;
8083
8084 /* issue ABTS for this IOCB based on iotag */
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04008085 abtsiocb = lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05008086 if (abtsiocb == NULL) {
8087 errcnt++;
8088 continue;
8089 }
dea31012005-04-17 16:05:31 -05008090
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04008091 cmd = &iocbq->iocb;
dea31012005-04-17 16:05:31 -05008092 abtsiocb->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
8093 abtsiocb->iocb.un.acxri.abortContextTag = cmd->ulpContext;
James Smartda0436e2009-05-22 14:51:39 -04008094 if (phba->sli_rev == LPFC_SLI_REV4)
8095 abtsiocb->iocb.un.acxri.abortIoTag = iocbq->sli4_xritag;
8096 else
8097 abtsiocb->iocb.un.acxri.abortIoTag = cmd->ulpIoTag;
dea31012005-04-17 16:05:31 -05008098 abtsiocb->iocb.ulpLe = 1;
8099 abtsiocb->iocb.ulpClass = cmd->ulpClass;
James Smart2e0fef82007-06-17 19:56:36 -05008100 abtsiocb->vport = phba->pport;
dea31012005-04-17 16:05:31 -05008101
James Smart5ffc2662009-11-18 15:39:44 -05008102 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
8103 abtsiocb->fcp_wqidx = iocbq->fcp_wqidx;
James Smart341af102010-01-26 23:07:37 -05008104 if (iocbq->iocb_flag & LPFC_IO_FCP)
8105 abtsiocb->iocb_flag |= LPFC_USE_FCPWQIDX;
James Smart5ffc2662009-11-18 15:39:44 -05008106
James Smart2e0fef82007-06-17 19:56:36 -05008107 if (lpfc_is_link_up(phba))
dea31012005-04-17 16:05:31 -05008108 abtsiocb->iocb.ulpCommand = CMD_ABORT_XRI_CN;
8109 else
8110 abtsiocb->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
8111
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -04008112 /* Setup callback routine and issue the command. */
8113 abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
James Smartda0436e2009-05-22 14:51:39 -04008114 ret_val = lpfc_sli_issue_iocb(phba, pring->ringno,
8115 abtsiocb, 0);
dea31012005-04-17 16:05:31 -05008116 if (ret_val == IOCB_ERROR) {
James Bottomley604a3e32005-10-29 10:28:33 -05008117 lpfc_sli_release_iocbq(phba, abtsiocb);
dea31012005-04-17 16:05:31 -05008118 errcnt++;
8119 continue;
8120 }
8121 }
8122
8123 return errcnt;
8124}
8125
James Smarte59058c2008-08-24 21:49:00 -04008126/**
James Smart3621a712009-04-06 18:47:14 -04008127 * lpfc_sli_wake_iocb_wait - lpfc_sli_issue_iocb_wait's completion handler
James Smarte59058c2008-08-24 21:49:00 -04008128 * @phba: Pointer to HBA context object.
8129 * @cmdiocbq: Pointer to command iocb.
8130 * @rspiocbq: Pointer to response iocb.
8131 *
8132 * This function is the completion handler for iocbs issued using
8133 * lpfc_sli_issue_iocb_wait function. This function is called by the
8134 * ring event handler function without any lock held. This function
8135 * can be called from both worker thread context and interrupt
8136 * context. This function also can be called from other thread which
8137 * cleans up the SLI layer objects.
8138 * This function copy the contents of the response iocb to the
8139 * response iocb memory object provided by the caller of
8140 * lpfc_sli_issue_iocb_wait and then wakes up the thread which
8141 * sleeps for the iocb completion.
8142 **/
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04008143static void
8144lpfc_sli_wake_iocb_wait(struct lpfc_hba *phba,
8145 struct lpfc_iocbq *cmdiocbq,
8146 struct lpfc_iocbq *rspiocbq)
dea31012005-04-17 16:05:31 -05008147{
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04008148 wait_queue_head_t *pdone_q;
8149 unsigned long iflags;
James Smart0f65ff62010-02-26 14:14:23 -05008150 struct lpfc_scsi_buf *lpfc_cmd;
dea31012005-04-17 16:05:31 -05008151
James Smart2e0fef82007-06-17 19:56:36 -05008152 spin_lock_irqsave(&phba->hbalock, iflags);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04008153 cmdiocbq->iocb_flag |= LPFC_IO_WAKE;
8154 if (cmdiocbq->context2 && rspiocbq)
8155 memcpy(&((struct lpfc_iocbq *)cmdiocbq->context2)->iocb,
8156 &rspiocbq->iocb, sizeof(IOCB_t));
8157
James Smart0f65ff62010-02-26 14:14:23 -05008158 /* Set the exchange busy flag for task management commands */
8159 if ((cmdiocbq->iocb_flag & LPFC_IO_FCP) &&
8160 !(cmdiocbq->iocb_flag & LPFC_IO_LIBDFC)) {
8161 lpfc_cmd = container_of(cmdiocbq, struct lpfc_scsi_buf,
8162 cur_iocbq);
8163 lpfc_cmd->exch_busy = rspiocbq->iocb_flag & LPFC_EXCHANGE_BUSY;
8164 }
8165
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04008166 pdone_q = cmdiocbq->context_un.wait_queue;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04008167 if (pdone_q)
8168 wake_up(pdone_q);
James Smart858c9f62007-06-17 19:56:39 -05008169 spin_unlock_irqrestore(&phba->hbalock, iflags);
dea31012005-04-17 16:05:31 -05008170 return;
8171}
8172
James Smarte59058c2008-08-24 21:49:00 -04008173/**
James Smartd11e31d2009-06-10 17:23:06 -04008174 * lpfc_chk_iocb_flg - Test IOCB flag with lock held.
8175 * @phba: Pointer to HBA context object..
8176 * @piocbq: Pointer to command iocb.
8177 * @flag: Flag to test.
8178 *
8179 * This routine grabs the hbalock and then test the iocb_flag to
8180 * see if the passed in flag is set.
8181 * Returns:
8182 * 1 if flag is set.
8183 * 0 if flag is not set.
8184 **/
8185static int
8186lpfc_chk_iocb_flg(struct lpfc_hba *phba,
8187 struct lpfc_iocbq *piocbq, uint32_t flag)
8188{
8189 unsigned long iflags;
8190 int ret;
8191
8192 spin_lock_irqsave(&phba->hbalock, iflags);
8193 ret = piocbq->iocb_flag & flag;
8194 spin_unlock_irqrestore(&phba->hbalock, iflags);
8195 return ret;
8196
8197}
8198
8199/**
James Smart3621a712009-04-06 18:47:14 -04008200 * lpfc_sli_issue_iocb_wait - Synchronous function to issue iocb commands
James Smarte59058c2008-08-24 21:49:00 -04008201 * @phba: Pointer to HBA context object..
8202 * @pring: Pointer to sli ring.
8203 * @piocb: Pointer to command iocb.
8204 * @prspiocbq: Pointer to response iocb.
8205 * @timeout: Timeout in number of seconds.
8206 *
8207 * This function issues the iocb to firmware and waits for the
8208 * iocb to complete. If the iocb command is not
8209 * completed within timeout seconds, it returns IOCB_TIMEDOUT.
8210 * Caller should not free the iocb resources if this function
8211 * returns IOCB_TIMEDOUT.
8212 * The function waits for the iocb completion using an
8213 * non-interruptible wait.
8214 * This function will sleep while waiting for iocb completion.
8215 * So, this function should not be called from any context which
8216 * does not allow sleeping. Due to the same reason, this function
8217 * cannot be called with interrupt disabled.
8218 * This function assumes that the iocb completions occur while
8219 * this function sleep. So, this function cannot be called from
8220 * the thread which process iocb completion for this ring.
8221 * This function clears the iocb_flag of the iocb object before
8222 * issuing the iocb and the iocb completion handler sets this
8223 * flag and wakes this thread when the iocb completes.
8224 * The contents of the response iocb will be copied to prspiocbq
8225 * by the completion handler when the command completes.
8226 * This function returns IOCB_SUCCESS when success.
8227 * This function is called with no lock held.
8228 **/
dea31012005-04-17 16:05:31 -05008229int
James Smart2e0fef82007-06-17 19:56:36 -05008230lpfc_sli_issue_iocb_wait(struct lpfc_hba *phba,
James Smartda0436e2009-05-22 14:51:39 -04008231 uint32_t ring_number,
James Smart2e0fef82007-06-17 19:56:36 -05008232 struct lpfc_iocbq *piocb,
8233 struct lpfc_iocbq *prspiocbq,
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04008234 uint32_t timeout)
dea31012005-04-17 16:05:31 -05008235{
Peter Zijlstra7259f0d2006-10-29 22:46:36 -08008236 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04008237 long timeleft, timeout_req = 0;
8238 int retval = IOCB_SUCCESS;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05008239 uint32_t creg_val;
James Smart2a9bf3d2010-06-07 15:24:45 -04008240 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
dea31012005-04-17 16:05:31 -05008241 /*
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04008242 * If the caller has provided a response iocbq buffer, then context2
8243 * is NULL or its an error.
dea31012005-04-17 16:05:31 -05008244 */
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04008245 if (prspiocbq) {
8246 if (piocb->context2)
8247 return IOCB_ERROR;
8248 piocb->context2 = prspiocbq;
dea31012005-04-17 16:05:31 -05008249 }
8250
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04008251 piocb->iocb_cmpl = lpfc_sli_wake_iocb_wait;
8252 piocb->context_un.wait_queue = &done_q;
8253 piocb->iocb_flag &= ~LPFC_IO_WAKE;
dea31012005-04-17 16:05:31 -05008254
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05008255 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
James Smart9940b972011-03-11 16:06:12 -05008256 if (lpfc_readl(phba->HCregaddr, &creg_val))
8257 return IOCB_ERROR;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05008258 creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
8259 writel(creg_val, phba->HCregaddr);
8260 readl(phba->HCregaddr); /* flush */
8261 }
8262
James Smart2a9bf3d2010-06-07 15:24:45 -04008263 retval = lpfc_sli_issue_iocb(phba, ring_number, piocb,
8264 SLI_IOCB_RET_IOCB);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04008265 if (retval == IOCB_SUCCESS) {
8266 timeout_req = timeout * HZ;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04008267 timeleft = wait_event_timeout(done_q,
James Smartd11e31d2009-06-10 17:23:06 -04008268 lpfc_chk_iocb_flg(phba, piocb, LPFC_IO_WAKE),
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04008269 timeout_req);
dea31012005-04-17 16:05:31 -05008270
James Smart7054a602007-04-25 09:52:34 -04008271 if (piocb->iocb_flag & LPFC_IO_WAKE) {
8272 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04008273 "0331 IOCB wake signaled\n");
James Smart7054a602007-04-25 09:52:34 -04008274 } else if (timeleft == 0) {
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04008275 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04008276 "0338 IOCB wait timeout error - no "
8277 "wake response Data x%x\n", timeout);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04008278 retval = IOCB_TIMEDOUT;
James Smart7054a602007-04-25 09:52:34 -04008279 } else {
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04008280 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04008281 "0330 IOCB wake NOT set, "
8282 "Data x%x x%lx\n",
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04008283 timeout, (timeleft / jiffies));
8284 retval = IOCB_TIMEDOUT;
dea31012005-04-17 16:05:31 -05008285 }
James Smart2a9bf3d2010-06-07 15:24:45 -04008286 } else if (retval == IOCB_BUSY) {
8287 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
8288 "2818 Max IOCBs %d txq cnt %d txcmplq cnt %d\n",
8289 phba->iocb_cnt, pring->txq_cnt, pring->txcmplq_cnt);
8290 return retval;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04008291 } else {
8292 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smartd7c255b2008-08-24 21:50:00 -04008293 "0332 IOCB wait issue failed, Data x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04008294 retval);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04008295 retval = IOCB_ERROR;
dea31012005-04-17 16:05:31 -05008296 }
8297
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05008298 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
James Smart9940b972011-03-11 16:06:12 -05008299 if (lpfc_readl(phba->HCregaddr, &creg_val))
8300 return IOCB_ERROR;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05008301 creg_val &= ~(HC_R0INT_ENA << LPFC_FCP_RING);
8302 writel(creg_val, phba->HCregaddr);
8303 readl(phba->HCregaddr); /* flush */
8304 }
8305
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04008306 if (prspiocbq)
8307 piocb->context2 = NULL;
8308
8309 piocb->context_un.wait_queue = NULL;
8310 piocb->iocb_cmpl = NULL;
dea31012005-04-17 16:05:31 -05008311 return retval;
8312}
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04008313
James Smarte59058c2008-08-24 21:49:00 -04008314/**
James Smart3621a712009-04-06 18:47:14 -04008315 * lpfc_sli_issue_mbox_wait - Synchronous function to issue mailbox
James Smarte59058c2008-08-24 21:49:00 -04008316 * @phba: Pointer to HBA context object.
8317 * @pmboxq: Pointer to driver mailbox object.
8318 * @timeout: Timeout in number of seconds.
8319 *
8320 * This function issues the mailbox to firmware and waits for the
8321 * mailbox command to complete. If the mailbox command is not
8322 * completed within timeout seconds, it returns MBX_TIMEOUT.
8323 * The function waits for the mailbox completion using an
8324 * interruptible wait. If the thread is woken up due to a
8325 * signal, MBX_TIMEOUT error is returned to the caller. Caller
8326 * should not free the mailbox resources, if this function returns
8327 * MBX_TIMEOUT.
8328 * This function will sleep while waiting for mailbox completion.
8329 * So, this function should not be called from any context which
8330 * does not allow sleeping. Due to the same reason, this function
8331 * cannot be called with interrupt disabled.
8332 * This function assumes that the mailbox completion occurs while
8333 * this function sleep. So, this function cannot be called from
8334 * the worker thread which processes mailbox completion.
8335 * This function is called in the context of HBA management
8336 * applications.
8337 * This function returns MBX_SUCCESS when successful.
8338 * This function is called with no lock held.
8339 **/
dea31012005-04-17 16:05:31 -05008340int
James Smart2e0fef82007-06-17 19:56:36 -05008341lpfc_sli_issue_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq,
dea31012005-04-17 16:05:31 -05008342 uint32_t timeout)
8343{
Peter Zijlstra7259f0d2006-10-29 22:46:36 -08008344 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
dea31012005-04-17 16:05:31 -05008345 int retval;
James Smart858c9f62007-06-17 19:56:39 -05008346 unsigned long flag;
dea31012005-04-17 16:05:31 -05008347
8348 /* The caller must leave context1 empty. */
James Smart98c9ea52007-10-27 13:37:33 -04008349 if (pmboxq->context1)
James Smart2e0fef82007-06-17 19:56:36 -05008350 return MBX_NOT_FINISHED;
dea31012005-04-17 16:05:31 -05008351
James Smart495a7142008-06-14 22:52:59 -04008352 pmboxq->mbox_flag &= ~LPFC_MBX_WAKE;
dea31012005-04-17 16:05:31 -05008353 /* setup wake call as IOCB callback */
8354 pmboxq->mbox_cmpl = lpfc_sli_wake_mbox_wait;
8355 /* setup context field to pass wait_queue pointer to wake function */
8356 pmboxq->context1 = &done_q;
8357
dea31012005-04-17 16:05:31 -05008358 /* now issue the command */
8359 retval = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
8360
8361 if (retval == MBX_BUSY || retval == MBX_SUCCESS) {
James Smart7054a602007-04-25 09:52:34 -04008362 wait_event_interruptible_timeout(done_q,
8363 pmboxq->mbox_flag & LPFC_MBX_WAKE,
8364 timeout * HZ);
8365
James Smart858c9f62007-06-17 19:56:39 -05008366 spin_lock_irqsave(&phba->hbalock, flag);
dea31012005-04-17 16:05:31 -05008367 pmboxq->context1 = NULL;
James Smart7054a602007-04-25 09:52:34 -04008368 /*
8369 * if LPFC_MBX_WAKE flag is set the mailbox is completed
8370 * else do not free the resources.
8371 */
James Smartd7c47992010-06-08 18:31:54 -04008372 if (pmboxq->mbox_flag & LPFC_MBX_WAKE) {
dea31012005-04-17 16:05:31 -05008373 retval = MBX_SUCCESS;
James Smartd7c47992010-06-08 18:31:54 -04008374 lpfc_sli4_swap_str(phba, pmboxq);
8375 } else {
James Smart7054a602007-04-25 09:52:34 -04008376 retval = MBX_TIMEOUT;
James Smart858c9f62007-06-17 19:56:39 -05008377 pmboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
8378 }
8379 spin_unlock_irqrestore(&phba->hbalock, flag);
dea31012005-04-17 16:05:31 -05008380 }
8381
dea31012005-04-17 16:05:31 -05008382 return retval;
8383}
8384
James Smarte59058c2008-08-24 21:49:00 -04008385/**
James Smart3772a992009-05-22 14:50:54 -04008386 * lpfc_sli_mbox_sys_shutdown - shutdown mailbox command sub-system
James Smarte59058c2008-08-24 21:49:00 -04008387 * @phba: Pointer to HBA context.
8388 *
James Smart3772a992009-05-22 14:50:54 -04008389 * This function is called to shutdown the driver's mailbox sub-system.
8390 * It first marks the mailbox sub-system is in a block state to prevent
8391 * the asynchronous mailbox command from issued off the pending mailbox
8392 * command queue. If the mailbox command sub-system shutdown is due to
8393 * HBA error conditions such as EEH or ERATT, this routine shall invoke
8394 * the mailbox sub-system flush routine to forcefully bring down the
8395 * mailbox sub-system. Otherwise, if it is due to normal condition (such
8396 * as with offline or HBA function reset), this routine will wait for the
8397 * outstanding mailbox command to complete before invoking the mailbox
8398 * sub-system flush routine to gracefully bring down mailbox sub-system.
James Smarte59058c2008-08-24 21:49:00 -04008399 **/
James Smart3772a992009-05-22 14:50:54 -04008400void
8401lpfc_sli_mbox_sys_shutdown(struct lpfc_hba *phba)
James Smartb4c02652006-07-06 15:50:43 -04008402{
James Smart3772a992009-05-22 14:50:54 -04008403 struct lpfc_sli *psli = &phba->sli;
8404 uint8_t actcmd = MBX_HEARTBEAT;
8405 unsigned long timeout;
8406
8407 spin_lock_irq(&phba->hbalock);
8408 psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
8409 spin_unlock_irq(&phba->hbalock);
8410
8411 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
8412 spin_lock_irq(&phba->hbalock);
8413 if (phba->sli.mbox_active)
8414 actcmd = phba->sli.mbox_active->u.mb.mbxCommand;
8415 spin_unlock_irq(&phba->hbalock);
8416 /* Determine how long we might wait for the active mailbox
8417 * command to be gracefully completed by firmware.
8418 */
8419 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, actcmd) *
8420 1000) + jiffies;
8421 while (phba->sli.mbox_active) {
8422 /* Check active mailbox complete status every 2ms */
8423 msleep(2);
8424 if (time_after(jiffies, timeout))
8425 /* Timeout, let the mailbox flush routine to
8426 * forcefully release active mailbox command
8427 */
8428 break;
8429 }
8430 }
8431 lpfc_sli_mbox_sys_flush(phba);
8432}
8433
8434/**
8435 * lpfc_sli_eratt_read - read sli-3 error attention events
8436 * @phba: Pointer to HBA context.
8437 *
8438 * This function is called to read the SLI3 device error attention registers
8439 * for possible error attention events. The caller must hold the hostlock
8440 * with spin_lock_irq().
8441 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03008442 * This function returns 1 when there is Error Attention in the Host Attention
James Smart3772a992009-05-22 14:50:54 -04008443 * Register and returns 0 otherwise.
8444 **/
8445static int
8446lpfc_sli_eratt_read(struct lpfc_hba *phba)
8447{
James Smarted957682007-06-17 19:56:37 -05008448 uint32_t ha_copy;
James Smartb4c02652006-07-06 15:50:43 -04008449
James Smart3772a992009-05-22 14:50:54 -04008450 /* Read chip Host Attention (HA) register */
James Smart9940b972011-03-11 16:06:12 -05008451 if (lpfc_readl(phba->HAregaddr, &ha_copy))
8452 goto unplug_err;
8453
James Smart3772a992009-05-22 14:50:54 -04008454 if (ha_copy & HA_ERATT) {
8455 /* Read host status register to retrieve error event */
James Smart9940b972011-03-11 16:06:12 -05008456 if (lpfc_sli_read_hs(phba))
8457 goto unplug_err;
James Smartb4c02652006-07-06 15:50:43 -04008458
James Smart3772a992009-05-22 14:50:54 -04008459 /* Check if there is a deferred error condition is active */
8460 if ((HS_FFER1 & phba->work_hs) &&
8461 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
James Smartdcf2a4e2010-09-29 11:18:53 -04008462 HS_FFER6 | HS_FFER7 | HS_FFER8) & phba->work_hs)) {
James Smart3772a992009-05-22 14:50:54 -04008463 phba->hba_flag |= DEFER_ERATT;
James Smart3772a992009-05-22 14:50:54 -04008464 /* Clear all interrupt enable conditions */
8465 writel(0, phba->HCregaddr);
8466 readl(phba->HCregaddr);
8467 }
8468
8469 /* Set the driver HA work bitmap */
James Smart3772a992009-05-22 14:50:54 -04008470 phba->work_ha |= HA_ERATT;
8471 /* Indicate polling handles this ERATT */
8472 phba->hba_flag |= HBA_ERATT_HANDLED;
James Smart3772a992009-05-22 14:50:54 -04008473 return 1;
James Smartb4c02652006-07-06 15:50:43 -04008474 }
James Smart3772a992009-05-22 14:50:54 -04008475 return 0;
James Smart9940b972011-03-11 16:06:12 -05008476
8477unplug_err:
8478 /* Set the driver HS work bitmap */
8479 phba->work_hs |= UNPLUG_ERR;
8480 /* Set the driver HA work bitmap */
8481 phba->work_ha |= HA_ERATT;
8482 /* Indicate polling handles this ERATT */
8483 phba->hba_flag |= HBA_ERATT_HANDLED;
8484 return 1;
James Smartb4c02652006-07-06 15:50:43 -04008485}
8486
James Smarte59058c2008-08-24 21:49:00 -04008487/**
James Smartda0436e2009-05-22 14:51:39 -04008488 * lpfc_sli4_eratt_read - read sli-4 error attention events
8489 * @phba: Pointer to HBA context.
8490 *
8491 * This function is called to read the SLI4 device error attention registers
8492 * for possible error attention events. The caller must hold the hostlock
8493 * with spin_lock_irq().
8494 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03008495 * This function returns 1 when there is Error Attention in the Host Attention
James Smartda0436e2009-05-22 14:51:39 -04008496 * Register and returns 0 otherwise.
8497 **/
8498static int
8499lpfc_sli4_eratt_read(struct lpfc_hba *phba)
8500{
8501 uint32_t uerr_sta_hi, uerr_sta_lo;
James Smart2fcee4b2010-12-15 17:57:46 -05008502 uint32_t if_type, portsmphr;
8503 struct lpfc_register portstat_reg;
James Smartda0436e2009-05-22 14:51:39 -04008504
James Smart2fcee4b2010-12-15 17:57:46 -05008505 /*
8506 * For now, use the SLI4 device internal unrecoverable error
James Smartda0436e2009-05-22 14:51:39 -04008507 * registers for error attention. This can be changed later.
8508 */
James Smart2fcee4b2010-12-15 17:57:46 -05008509 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
8510 switch (if_type) {
8511 case LPFC_SLI_INTF_IF_TYPE_0:
James Smart9940b972011-03-11 16:06:12 -05008512 if (lpfc_readl(phba->sli4_hba.u.if_type0.UERRLOregaddr,
8513 &uerr_sta_lo) ||
8514 lpfc_readl(phba->sli4_hba.u.if_type0.UERRHIregaddr,
8515 &uerr_sta_hi)) {
8516 phba->work_hs |= UNPLUG_ERR;
8517 phba->work_ha |= HA_ERATT;
8518 phba->hba_flag |= HBA_ERATT_HANDLED;
8519 return 1;
8520 }
James Smart2fcee4b2010-12-15 17:57:46 -05008521 if ((~phba->sli4_hba.ue_mask_lo & uerr_sta_lo) ||
8522 (~phba->sli4_hba.ue_mask_hi & uerr_sta_hi)) {
8523 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8524 "1423 HBA Unrecoverable error: "
8525 "uerr_lo_reg=0x%x, uerr_hi_reg=0x%x, "
8526 "ue_mask_lo_reg=0x%x, "
8527 "ue_mask_hi_reg=0x%x\n",
8528 uerr_sta_lo, uerr_sta_hi,
8529 phba->sli4_hba.ue_mask_lo,
8530 phba->sli4_hba.ue_mask_hi);
8531 phba->work_status[0] = uerr_sta_lo;
8532 phba->work_status[1] = uerr_sta_hi;
8533 phba->work_ha |= HA_ERATT;
8534 phba->hba_flag |= HBA_ERATT_HANDLED;
8535 return 1;
8536 }
8537 break;
8538 case LPFC_SLI_INTF_IF_TYPE_2:
James Smart9940b972011-03-11 16:06:12 -05008539 if (lpfc_readl(phba->sli4_hba.u.if_type2.STATUSregaddr,
8540 &portstat_reg.word0) ||
8541 lpfc_readl(phba->sli4_hba.PSMPHRregaddr,
8542 &portsmphr)){
8543 phba->work_hs |= UNPLUG_ERR;
8544 phba->work_ha |= HA_ERATT;
8545 phba->hba_flag |= HBA_ERATT_HANDLED;
8546 return 1;
8547 }
James Smart2fcee4b2010-12-15 17:57:46 -05008548 if (bf_get(lpfc_sliport_status_err, &portstat_reg)) {
8549 phba->work_status[0] =
8550 readl(phba->sli4_hba.u.if_type2.ERR1regaddr);
8551 phba->work_status[1] =
8552 readl(phba->sli4_hba.u.if_type2.ERR2regaddr);
8553 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8554 "2885 Port Error Detected: "
8555 "port status reg 0x%x, "
8556 "port smphr reg 0x%x, "
8557 "error 1=0x%x, error 2=0x%x\n",
8558 portstat_reg.word0,
8559 portsmphr,
8560 phba->work_status[0],
8561 phba->work_status[1]);
8562 phba->work_ha |= HA_ERATT;
8563 phba->hba_flag |= HBA_ERATT_HANDLED;
8564 return 1;
8565 }
8566 break;
8567 case LPFC_SLI_INTF_IF_TYPE_1:
8568 default:
James Smarta747c9c2009-11-18 15:41:10 -05008569 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart2fcee4b2010-12-15 17:57:46 -05008570 "2886 HBA Error Attention on unsupported "
8571 "if type %d.", if_type);
James Smarta747c9c2009-11-18 15:41:10 -05008572 return 1;
James Smartda0436e2009-05-22 14:51:39 -04008573 }
James Smart2fcee4b2010-12-15 17:57:46 -05008574
James Smartda0436e2009-05-22 14:51:39 -04008575 return 0;
8576}
8577
8578/**
James Smart3621a712009-04-06 18:47:14 -04008579 * lpfc_sli_check_eratt - check error attention events
James Smart93996272008-08-24 21:50:30 -04008580 * @phba: Pointer to HBA context.
8581 *
James Smart3772a992009-05-22 14:50:54 -04008582 * This function is called from timer soft interrupt context to check HBA's
James Smart93996272008-08-24 21:50:30 -04008583 * error attention register bit for error attention events.
8584 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03008585 * This function returns 1 when there is Error Attention in the Host Attention
James Smart93996272008-08-24 21:50:30 -04008586 * Register and returns 0 otherwise.
8587 **/
8588int
8589lpfc_sli_check_eratt(struct lpfc_hba *phba)
8590{
8591 uint32_t ha_copy;
8592
8593 /* If somebody is waiting to handle an eratt, don't process it
8594 * here. The brdkill function will do this.
8595 */
8596 if (phba->link_flag & LS_IGNORE_ERATT)
8597 return 0;
8598
8599 /* Check if interrupt handler handles this ERATT */
8600 spin_lock_irq(&phba->hbalock);
8601 if (phba->hba_flag & HBA_ERATT_HANDLED) {
8602 /* Interrupt handler has handled ERATT */
8603 spin_unlock_irq(&phba->hbalock);
8604 return 0;
8605 }
8606
James Smarta257bf92009-04-06 18:48:10 -04008607 /*
8608 * If there is deferred error attention, do not check for error
8609 * attention
8610 */
8611 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
8612 spin_unlock_irq(&phba->hbalock);
8613 return 0;
8614 }
8615
James Smart3772a992009-05-22 14:50:54 -04008616 /* If PCI channel is offline, don't process it */
8617 if (unlikely(pci_channel_offline(phba->pcidev))) {
James Smart93996272008-08-24 21:50:30 -04008618 spin_unlock_irq(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -04008619 return 0;
8620 }
8621
8622 switch (phba->sli_rev) {
8623 case LPFC_SLI_REV2:
8624 case LPFC_SLI_REV3:
8625 /* Read chip Host Attention (HA) register */
8626 ha_copy = lpfc_sli_eratt_read(phba);
8627 break;
James Smartda0436e2009-05-22 14:51:39 -04008628 case LPFC_SLI_REV4:
James Smart2fcee4b2010-12-15 17:57:46 -05008629 /* Read device Uncoverable Error (UERR) registers */
James Smartda0436e2009-05-22 14:51:39 -04008630 ha_copy = lpfc_sli4_eratt_read(phba);
8631 break;
James Smart3772a992009-05-22 14:50:54 -04008632 default:
8633 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8634 "0299 Invalid SLI revision (%d)\n",
8635 phba->sli_rev);
8636 ha_copy = 0;
8637 break;
James Smart93996272008-08-24 21:50:30 -04008638 }
8639 spin_unlock_irq(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -04008640
8641 return ha_copy;
8642}
8643
8644/**
8645 * lpfc_intr_state_check - Check device state for interrupt handling
8646 * @phba: Pointer to HBA context.
8647 *
8648 * This inline routine checks whether a device or its PCI slot is in a state
8649 * that the interrupt should be handled.
8650 *
8651 * This function returns 0 if the device or the PCI slot is in a state that
8652 * interrupt should be handled, otherwise -EIO.
8653 */
8654static inline int
8655lpfc_intr_state_check(struct lpfc_hba *phba)
8656{
8657 /* If the pci channel is offline, ignore all the interrupts */
8658 if (unlikely(pci_channel_offline(phba->pcidev)))
8659 return -EIO;
8660
8661 /* Update device level interrupt statistics */
8662 phba->sli.slistat.sli_intr++;
8663
8664 /* Ignore all interrupts during initialization. */
8665 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
8666 return -EIO;
8667
James Smart93996272008-08-24 21:50:30 -04008668 return 0;
8669}
8670
8671/**
James Smart3772a992009-05-22 14:50:54 -04008672 * lpfc_sli_sp_intr_handler - Slow-path interrupt handler to SLI-3 device
James Smarte59058c2008-08-24 21:49:00 -04008673 * @irq: Interrupt number.
8674 * @dev_id: The device context pointer.
8675 *
James Smart93996272008-08-24 21:50:30 -04008676 * This function is directly called from the PCI layer as an interrupt
James Smart3772a992009-05-22 14:50:54 -04008677 * service routine when device with SLI-3 interface spec is enabled with
8678 * MSI-X multi-message interrupt mode and there are slow-path events in
8679 * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
8680 * interrupt mode, this function is called as part of the device-level
8681 * interrupt handler. When the PCI slot is in error recovery or the HBA
8682 * is undergoing initialization, the interrupt handler will not process
8683 * the interrupt. The link attention and ELS ring attention events are
8684 * handled by the worker thread. The interrupt handler signals the worker
8685 * thread and returns for these events. This function is called without
8686 * any lock held. It gets the hbalock to access and update SLI data
James Smart93996272008-08-24 21:50:30 -04008687 * structures.
8688 *
8689 * This function returns IRQ_HANDLED when interrupt is handled else it
8690 * returns IRQ_NONE.
James Smarte59058c2008-08-24 21:49:00 -04008691 **/
dea31012005-04-17 16:05:31 -05008692irqreturn_t
James Smart3772a992009-05-22 14:50:54 -04008693lpfc_sli_sp_intr_handler(int irq, void *dev_id)
dea31012005-04-17 16:05:31 -05008694{
James Smart2e0fef82007-06-17 19:56:36 -05008695 struct lpfc_hba *phba;
James Smarta747c9c2009-11-18 15:41:10 -05008696 uint32_t ha_copy, hc_copy;
dea31012005-04-17 16:05:31 -05008697 uint32_t work_ha_copy;
8698 unsigned long status;
James Smart5b75da22008-12-04 22:39:35 -05008699 unsigned long iflag;
dea31012005-04-17 16:05:31 -05008700 uint32_t control;
8701
James Smart92d7f7b2007-06-17 19:56:38 -05008702 MAILBOX_t *mbox, *pmbox;
James Smart858c9f62007-06-17 19:56:39 -05008703 struct lpfc_vport *vport;
8704 struct lpfc_nodelist *ndlp;
8705 struct lpfc_dmabuf *mp;
James Smart92d7f7b2007-06-17 19:56:38 -05008706 LPFC_MBOXQ_t *pmb;
8707 int rc;
8708
dea31012005-04-17 16:05:31 -05008709 /*
8710 * Get the driver's phba structure from the dev_id and
8711 * assume the HBA is not interrupting.
8712 */
James Smart93996272008-08-24 21:50:30 -04008713 phba = (struct lpfc_hba *)dev_id;
dea31012005-04-17 16:05:31 -05008714
8715 if (unlikely(!phba))
8716 return IRQ_NONE;
8717
dea31012005-04-17 16:05:31 -05008718 /*
James Smart93996272008-08-24 21:50:30 -04008719 * Stuff needs to be attented to when this function is invoked as an
8720 * individual interrupt handler in MSI-X multi-message interrupt mode
dea31012005-04-17 16:05:31 -05008721 */
James Smart93996272008-08-24 21:50:30 -04008722 if (phba->intr_type == MSIX) {
James Smart3772a992009-05-22 14:50:54 -04008723 /* Check device state for handling interrupt */
8724 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -04008725 return IRQ_NONE;
8726 /* Need to read HA REG for slow-path events */
James Smart5b75da22008-12-04 22:39:35 -05008727 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart9940b972011-03-11 16:06:12 -05008728 if (lpfc_readl(phba->HAregaddr, &ha_copy))
8729 goto unplug_error;
James Smart93996272008-08-24 21:50:30 -04008730 /* If somebody is waiting to handle an eratt don't process it
8731 * here. The brdkill function will do this.
8732 */
8733 if (phba->link_flag & LS_IGNORE_ERATT)
8734 ha_copy &= ~HA_ERATT;
8735 /* Check the need for handling ERATT in interrupt handler */
8736 if (ha_copy & HA_ERATT) {
8737 if (phba->hba_flag & HBA_ERATT_HANDLED)
8738 /* ERATT polling has handled ERATT */
8739 ha_copy &= ~HA_ERATT;
8740 else
8741 /* Indicate interrupt handler handles ERATT */
8742 phba->hba_flag |= HBA_ERATT_HANDLED;
8743 }
James Smarta257bf92009-04-06 18:48:10 -04008744
8745 /*
8746 * If there is deferred error attention, do not check for any
8747 * interrupt.
8748 */
8749 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
James Smart3772a992009-05-22 14:50:54 -04008750 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -04008751 return IRQ_NONE;
8752 }
8753
James Smart93996272008-08-24 21:50:30 -04008754 /* Clear up only attention source related to slow-path */
James Smart9940b972011-03-11 16:06:12 -05008755 if (lpfc_readl(phba->HCregaddr, &hc_copy))
8756 goto unplug_error;
8757
James Smarta747c9c2009-11-18 15:41:10 -05008758 writel(hc_copy & ~(HC_MBINT_ENA | HC_R2INT_ENA |
8759 HC_LAINT_ENA | HC_ERINT_ENA),
8760 phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -04008761 writel((ha_copy & (HA_MBATT | HA_R2_CLR_MSK)),
8762 phba->HAregaddr);
James Smarta747c9c2009-11-18 15:41:10 -05008763 writel(hc_copy, phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -04008764 readl(phba->HAregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -05008765 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -04008766 } else
8767 ha_copy = phba->ha_copy;
dea31012005-04-17 16:05:31 -05008768
dea31012005-04-17 16:05:31 -05008769 work_ha_copy = ha_copy & phba->work_ha_mask;
8770
James Smart93996272008-08-24 21:50:30 -04008771 if (work_ha_copy) {
dea31012005-04-17 16:05:31 -05008772 if (work_ha_copy & HA_LATT) {
8773 if (phba->sli.sli_flag & LPFC_PROCESS_LA) {
8774 /*
8775 * Turn off Link Attention interrupts
8776 * until CLEAR_LA done
8777 */
James Smart5b75da22008-12-04 22:39:35 -05008778 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05008779 phba->sli.sli_flag &= ~LPFC_PROCESS_LA;
James Smart9940b972011-03-11 16:06:12 -05008780 if (lpfc_readl(phba->HCregaddr, &control))
8781 goto unplug_error;
dea31012005-04-17 16:05:31 -05008782 control &= ~HC_LAINT_ENA;
8783 writel(control, phba->HCregaddr);
8784 readl(phba->HCregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -05008785 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05008786 }
8787 else
8788 work_ha_copy &= ~HA_LATT;
8789 }
8790
James Smart93996272008-08-24 21:50:30 -04008791 if (work_ha_copy & ~(HA_ERATT | HA_MBATT | HA_LATT)) {
James Smart858c9f62007-06-17 19:56:39 -05008792 /*
8793 * Turn off Slow Rings interrupts, LPFC_ELS_RING is
8794 * the only slow ring.
8795 */
8796 status = (work_ha_copy &
8797 (HA_RXMASK << (4*LPFC_ELS_RING)));
8798 status >>= (4*LPFC_ELS_RING);
8799 if (status & HA_RXMASK) {
James Smart5b75da22008-12-04 22:39:35 -05008800 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart9940b972011-03-11 16:06:12 -05008801 if (lpfc_readl(phba->HCregaddr, &control))
8802 goto unplug_error;
James Smarta58cbd52007-08-02 11:09:43 -04008803
8804 lpfc_debugfs_slow_ring_trc(phba,
8805 "ISR slow ring: ctl:x%x stat:x%x isrcnt:x%x",
8806 control, status,
8807 (uint32_t)phba->sli.slistat.sli_intr);
8808
James Smart858c9f62007-06-17 19:56:39 -05008809 if (control & (HC_R0INT_ENA << LPFC_ELS_RING)) {
James Smarta58cbd52007-08-02 11:09:43 -04008810 lpfc_debugfs_slow_ring_trc(phba,
8811 "ISR Disable ring:"
8812 "pwork:x%x hawork:x%x wait:x%x",
8813 phba->work_ha, work_ha_copy,
8814 (uint32_t)((unsigned long)
James Smart5e9d9b82008-06-14 22:52:53 -04008815 &phba->work_waitq));
James Smarta58cbd52007-08-02 11:09:43 -04008816
James Smart858c9f62007-06-17 19:56:39 -05008817 control &=
8818 ~(HC_R0INT_ENA << LPFC_ELS_RING);
dea31012005-04-17 16:05:31 -05008819 writel(control, phba->HCregaddr);
8820 readl(phba->HCregaddr); /* flush */
dea31012005-04-17 16:05:31 -05008821 }
James Smarta58cbd52007-08-02 11:09:43 -04008822 else {
8823 lpfc_debugfs_slow_ring_trc(phba,
8824 "ISR slow ring: pwork:"
8825 "x%x hawork:x%x wait:x%x",
8826 phba->work_ha, work_ha_copy,
8827 (uint32_t)((unsigned long)
James Smart5e9d9b82008-06-14 22:52:53 -04008828 &phba->work_waitq));
James Smarta58cbd52007-08-02 11:09:43 -04008829 }
James Smart5b75da22008-12-04 22:39:35 -05008830 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05008831 }
8832 }
James Smart5b75da22008-12-04 22:39:35 -05008833 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -04008834 if (work_ha_copy & HA_ERATT) {
James Smart9940b972011-03-11 16:06:12 -05008835 if (lpfc_sli_read_hs(phba))
8836 goto unplug_error;
James Smarta257bf92009-04-06 18:48:10 -04008837 /*
8838 * Check if there is a deferred error condition
8839 * is active
8840 */
8841 if ((HS_FFER1 & phba->work_hs) &&
8842 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
James Smartdcf2a4e2010-09-29 11:18:53 -04008843 HS_FFER6 | HS_FFER7 | HS_FFER8) &
8844 phba->work_hs)) {
James Smarta257bf92009-04-06 18:48:10 -04008845 phba->hba_flag |= DEFER_ERATT;
8846 /* Clear all interrupt enable conditions */
8847 writel(0, phba->HCregaddr);
8848 readl(phba->HCregaddr);
8849 }
8850 }
8851
James Smart93996272008-08-24 21:50:30 -04008852 if ((work_ha_copy & HA_MBATT) && (phba->sli.mbox_active)) {
James Smart92d7f7b2007-06-17 19:56:38 -05008853 pmb = phba->sli.mbox_active;
James Smart04c68492009-05-22 14:52:52 -04008854 pmbox = &pmb->u.mb;
James Smart34b02dc2008-08-24 21:49:55 -04008855 mbox = phba->mbox;
James Smart858c9f62007-06-17 19:56:39 -05008856 vport = pmb->vport;
James Smart92d7f7b2007-06-17 19:56:38 -05008857
8858 /* First check out the status word */
8859 lpfc_sli_pcimem_bcopy(mbox, pmbox, sizeof(uint32_t));
8860 if (pmbox->mbxOwner != OWN_HOST) {
James Smart5b75da22008-12-04 22:39:35 -05008861 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart92d7f7b2007-06-17 19:56:38 -05008862 /*
8863 * Stray Mailbox Interrupt, mbxCommand <cmd>
8864 * mbxStatus <status>
8865 */
James Smart09372822008-01-11 01:52:54 -05008866 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
James Smart92d7f7b2007-06-17 19:56:38 -05008867 LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04008868 "(%d):0304 Stray Mailbox "
James Smart92d7f7b2007-06-17 19:56:38 -05008869 "Interrupt mbxCommand x%x "
8870 "mbxStatus x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04008871 (vport ? vport->vpi : 0),
James Smart92d7f7b2007-06-17 19:56:38 -05008872 pmbox->mbxCommand,
8873 pmbox->mbxStatus);
James Smart09372822008-01-11 01:52:54 -05008874 /* clear mailbox attention bit */
8875 work_ha_copy &= ~HA_MBATT;
8876 } else {
James Smart97eab632008-04-07 10:16:05 -04008877 phba->sli.mbox_active = NULL;
James Smart5b75da22008-12-04 22:39:35 -05008878 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart09372822008-01-11 01:52:54 -05008879 phba->last_completion_time = jiffies;
8880 del_timer(&phba->sli.mbox_tmo);
James Smart09372822008-01-11 01:52:54 -05008881 if (pmb->mbox_cmpl) {
8882 lpfc_sli_pcimem_bcopy(mbox, pmbox,
8883 MAILBOX_CMD_SIZE);
James Smart7a470272010-03-15 11:25:20 -04008884 if (pmb->out_ext_byte_len &&
8885 pmb->context2)
8886 lpfc_sli_pcimem_bcopy(
8887 phba->mbox_ext,
8888 pmb->context2,
8889 pmb->out_ext_byte_len);
James Smart858c9f62007-06-17 19:56:39 -05008890 }
James Smart09372822008-01-11 01:52:54 -05008891 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
8892 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
8893
8894 lpfc_debugfs_disc_trc(vport,
8895 LPFC_DISC_TRC_MBOX_VPORT,
8896 "MBOX dflt rpi: : "
8897 "status:x%x rpi:x%x",
8898 (uint32_t)pmbox->mbxStatus,
8899 pmbox->un.varWords[0], 0);
8900
8901 if (!pmbox->mbxStatus) {
8902 mp = (struct lpfc_dmabuf *)
8903 (pmb->context1);
8904 ndlp = (struct lpfc_nodelist *)
8905 pmb->context2;
8906
8907 /* Reg_LOGIN of dflt RPI was
8908 * successful. new lets get
8909 * rid of the RPI using the
8910 * same mbox buffer.
8911 */
8912 lpfc_unreg_login(phba,
8913 vport->vpi,
8914 pmbox->un.varWords[0],
8915 pmb);
8916 pmb->mbox_cmpl =
8917 lpfc_mbx_cmpl_dflt_rpi;
8918 pmb->context1 = mp;
8919 pmb->context2 = ndlp;
8920 pmb->vport = vport;
James Smart58da1ff2008-04-07 10:15:56 -04008921 rc = lpfc_sli_issue_mbox(phba,
8922 pmb,
8923 MBX_NOWAIT);
8924 if (rc != MBX_BUSY)
8925 lpfc_printf_log(phba,
8926 KERN_ERR,
8927 LOG_MBOX | LOG_SLI,
James Smartd7c255b2008-08-24 21:50:00 -04008928 "0350 rc should have"
James Smart6a9c52c2009-10-02 15:16:51 -04008929 "been MBX_BUSY\n");
James Smart3772a992009-05-22 14:50:54 -04008930 if (rc != MBX_NOT_FINISHED)
8931 goto send_current_mbox;
James Smart09372822008-01-11 01:52:54 -05008932 }
8933 }
James Smart5b75da22008-12-04 22:39:35 -05008934 spin_lock_irqsave(
8935 &phba->pport->work_port_lock,
8936 iflag);
James Smart09372822008-01-11 01:52:54 -05008937 phba->pport->work_port_events &=
8938 ~WORKER_MBOX_TMO;
James Smart5b75da22008-12-04 22:39:35 -05008939 spin_unlock_irqrestore(
8940 &phba->pport->work_port_lock,
8941 iflag);
James Smart09372822008-01-11 01:52:54 -05008942 lpfc_mbox_cmpl_put(phba, pmb);
James Smart858c9f62007-06-17 19:56:39 -05008943 }
James Smart97eab632008-04-07 10:16:05 -04008944 } else
James Smart5b75da22008-12-04 22:39:35 -05008945 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -04008946
James Smart92d7f7b2007-06-17 19:56:38 -05008947 if ((work_ha_copy & HA_MBATT) &&
8948 (phba->sli.mbox_active == NULL)) {
James Smart858c9f62007-06-17 19:56:39 -05008949send_current_mbox:
James Smart92d7f7b2007-06-17 19:56:38 -05008950 /* Process next mailbox command if there is one */
James Smart58da1ff2008-04-07 10:15:56 -04008951 do {
8952 rc = lpfc_sli_issue_mbox(phba, NULL,
8953 MBX_NOWAIT);
8954 } while (rc == MBX_NOT_FINISHED);
8955 if (rc != MBX_SUCCESS)
8956 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
8957 LOG_SLI, "0349 rc should be "
James Smart6a9c52c2009-10-02 15:16:51 -04008958 "MBX_SUCCESS\n");
James Smart92d7f7b2007-06-17 19:56:38 -05008959 }
8960
James Smart5b75da22008-12-04 22:39:35 -05008961 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05008962 phba->work_ha |= work_ha_copy;
James Smart5b75da22008-12-04 22:39:35 -05008963 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart5e9d9b82008-06-14 22:52:53 -04008964 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -05008965 }
James Smart93996272008-08-24 21:50:30 -04008966 return IRQ_HANDLED;
James Smart9940b972011-03-11 16:06:12 -05008967unplug_error:
8968 spin_unlock_irqrestore(&phba->hbalock, iflag);
8969 return IRQ_HANDLED;
dea31012005-04-17 16:05:31 -05008970
James Smart3772a992009-05-22 14:50:54 -04008971} /* lpfc_sli_sp_intr_handler */
James Smart93996272008-08-24 21:50:30 -04008972
8973/**
James Smart3772a992009-05-22 14:50:54 -04008974 * lpfc_sli_fp_intr_handler - Fast-path interrupt handler to SLI-3 device.
James Smart93996272008-08-24 21:50:30 -04008975 * @irq: Interrupt number.
8976 * @dev_id: The device context pointer.
8977 *
8978 * This function is directly called from the PCI layer as an interrupt
James Smart3772a992009-05-22 14:50:54 -04008979 * service routine when device with SLI-3 interface spec is enabled with
8980 * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
8981 * ring event in the HBA. However, when the device is enabled with either
8982 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
8983 * device-level interrupt handler. When the PCI slot is in error recovery
8984 * or the HBA is undergoing initialization, the interrupt handler will not
8985 * process the interrupt. The SCSI FCP fast-path ring event are handled in
8986 * the intrrupt context. This function is called without any lock held.
8987 * It gets the hbalock to access and update SLI data structures.
James Smart93996272008-08-24 21:50:30 -04008988 *
8989 * This function returns IRQ_HANDLED when interrupt is handled else it
8990 * returns IRQ_NONE.
8991 **/
8992irqreturn_t
James Smart3772a992009-05-22 14:50:54 -04008993lpfc_sli_fp_intr_handler(int irq, void *dev_id)
James Smart93996272008-08-24 21:50:30 -04008994{
8995 struct lpfc_hba *phba;
8996 uint32_t ha_copy;
8997 unsigned long status;
James Smart5b75da22008-12-04 22:39:35 -05008998 unsigned long iflag;
James Smart93996272008-08-24 21:50:30 -04008999
9000 /* Get the driver's phba structure from the dev_id and
9001 * assume the HBA is not interrupting.
9002 */
9003 phba = (struct lpfc_hba *) dev_id;
9004
9005 if (unlikely(!phba))
9006 return IRQ_NONE;
dea31012005-04-17 16:05:31 -05009007
9008 /*
James Smart93996272008-08-24 21:50:30 -04009009 * Stuff needs to be attented to when this function is invoked as an
9010 * individual interrupt handler in MSI-X multi-message interrupt mode
dea31012005-04-17 16:05:31 -05009011 */
James Smart93996272008-08-24 21:50:30 -04009012 if (phba->intr_type == MSIX) {
James Smart3772a992009-05-22 14:50:54 -04009013 /* Check device state for handling interrupt */
9014 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -04009015 return IRQ_NONE;
9016 /* Need to read HA REG for FCP ring and other ring events */
James Smart9940b972011-03-11 16:06:12 -05009017 if (lpfc_readl(phba->HAregaddr, &ha_copy))
9018 return IRQ_HANDLED;
James Smart93996272008-08-24 21:50:30 -04009019 /* Clear up only attention source related to fast-path */
James Smart5b75da22008-12-04 22:39:35 -05009020 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -04009021 /*
9022 * If there is deferred error attention, do not check for
9023 * any interrupt.
9024 */
9025 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
James Smart3772a992009-05-22 14:50:54 -04009026 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -04009027 return IRQ_NONE;
9028 }
James Smart93996272008-08-24 21:50:30 -04009029 writel((ha_copy & (HA_R0_CLR_MSK | HA_R1_CLR_MSK)),
9030 phba->HAregaddr);
9031 readl(phba->HAregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -05009032 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -04009033 } else
9034 ha_copy = phba->ha_copy;
9035
9036 /*
9037 * Process all events on FCP ring. Take the optimized path for FCP IO.
9038 */
9039 ha_copy &= ~(phba->work_ha_mask);
9040
9041 status = (ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
dea31012005-04-17 16:05:31 -05009042 status >>= (4*LPFC_FCP_RING);
James Smart858c9f62007-06-17 19:56:39 -05009043 if (status & HA_RXMASK)
dea31012005-04-17 16:05:31 -05009044 lpfc_sli_handle_fast_ring_event(phba,
9045 &phba->sli.ring[LPFC_FCP_RING],
9046 status);
James Smarta4bc3372006-12-02 13:34:16 -05009047
9048 if (phba->cfg_multi_ring_support == 2) {
9049 /*
James Smart93996272008-08-24 21:50:30 -04009050 * Process all events on extra ring. Take the optimized path
9051 * for extra ring IO.
James Smarta4bc3372006-12-02 13:34:16 -05009052 */
James Smart93996272008-08-24 21:50:30 -04009053 status = (ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
James Smarta4bc3372006-12-02 13:34:16 -05009054 status >>= (4*LPFC_EXTRA_RING);
James Smart858c9f62007-06-17 19:56:39 -05009055 if (status & HA_RXMASK) {
James Smarta4bc3372006-12-02 13:34:16 -05009056 lpfc_sli_handle_fast_ring_event(phba,
9057 &phba->sli.ring[LPFC_EXTRA_RING],
9058 status);
9059 }
9060 }
dea31012005-04-17 16:05:31 -05009061 return IRQ_HANDLED;
James Smart3772a992009-05-22 14:50:54 -04009062} /* lpfc_sli_fp_intr_handler */
dea31012005-04-17 16:05:31 -05009063
James Smart93996272008-08-24 21:50:30 -04009064/**
James Smart3772a992009-05-22 14:50:54 -04009065 * lpfc_sli_intr_handler - Device-level interrupt handler to SLI-3 device
James Smart93996272008-08-24 21:50:30 -04009066 * @irq: Interrupt number.
9067 * @dev_id: The device context pointer.
9068 *
James Smart3772a992009-05-22 14:50:54 -04009069 * This function is the HBA device-level interrupt handler to device with
9070 * SLI-3 interface spec, called from the PCI layer when either MSI or
9071 * Pin-IRQ interrupt mode is enabled and there is an event in the HBA which
9072 * requires driver attention. This function invokes the slow-path interrupt
9073 * attention handling function and fast-path interrupt attention handling
9074 * function in turn to process the relevant HBA attention events. This
9075 * function is called without any lock held. It gets the hbalock to access
9076 * and update SLI data structures.
James Smart93996272008-08-24 21:50:30 -04009077 *
9078 * This function returns IRQ_HANDLED when interrupt is handled, else it
9079 * returns IRQ_NONE.
9080 **/
9081irqreturn_t
James Smart3772a992009-05-22 14:50:54 -04009082lpfc_sli_intr_handler(int irq, void *dev_id)
James Smart93996272008-08-24 21:50:30 -04009083{
9084 struct lpfc_hba *phba;
9085 irqreturn_t sp_irq_rc, fp_irq_rc;
9086 unsigned long status1, status2;
James Smarta747c9c2009-11-18 15:41:10 -05009087 uint32_t hc_copy;
James Smart93996272008-08-24 21:50:30 -04009088
9089 /*
9090 * Get the driver's phba structure from the dev_id and
9091 * assume the HBA is not interrupting.
9092 */
9093 phba = (struct lpfc_hba *) dev_id;
9094
9095 if (unlikely(!phba))
9096 return IRQ_NONE;
9097
James Smart3772a992009-05-22 14:50:54 -04009098 /* Check device state for handling interrupt */
9099 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -04009100 return IRQ_NONE;
9101
9102 spin_lock(&phba->hbalock);
James Smart9940b972011-03-11 16:06:12 -05009103 if (lpfc_readl(phba->HAregaddr, &phba->ha_copy)) {
9104 spin_unlock(&phba->hbalock);
9105 return IRQ_HANDLED;
9106 }
9107
James Smart93996272008-08-24 21:50:30 -04009108 if (unlikely(!phba->ha_copy)) {
9109 spin_unlock(&phba->hbalock);
9110 return IRQ_NONE;
9111 } else if (phba->ha_copy & HA_ERATT) {
9112 if (phba->hba_flag & HBA_ERATT_HANDLED)
9113 /* ERATT polling has handled ERATT */
9114 phba->ha_copy &= ~HA_ERATT;
9115 else
9116 /* Indicate interrupt handler handles ERATT */
9117 phba->hba_flag |= HBA_ERATT_HANDLED;
9118 }
9119
James Smarta257bf92009-04-06 18:48:10 -04009120 /*
9121 * If there is deferred error attention, do not check for any interrupt.
9122 */
9123 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
Dan Carpenterec21b3b2010-08-08 00:15:17 +02009124 spin_unlock(&phba->hbalock);
James Smarta257bf92009-04-06 18:48:10 -04009125 return IRQ_NONE;
9126 }
9127
James Smart93996272008-08-24 21:50:30 -04009128 /* Clear attention sources except link and error attentions */
James Smart9940b972011-03-11 16:06:12 -05009129 if (lpfc_readl(phba->HCregaddr, &hc_copy)) {
9130 spin_unlock(&phba->hbalock);
9131 return IRQ_HANDLED;
9132 }
James Smarta747c9c2009-11-18 15:41:10 -05009133 writel(hc_copy & ~(HC_MBINT_ENA | HC_R0INT_ENA | HC_R1INT_ENA
9134 | HC_R2INT_ENA | HC_LAINT_ENA | HC_ERINT_ENA),
9135 phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -04009136 writel((phba->ha_copy & ~(HA_LATT | HA_ERATT)), phba->HAregaddr);
James Smarta747c9c2009-11-18 15:41:10 -05009137 writel(hc_copy, phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -04009138 readl(phba->HAregaddr); /* flush */
9139 spin_unlock(&phba->hbalock);
9140
9141 /*
9142 * Invokes slow-path host attention interrupt handling as appropriate.
9143 */
9144
9145 /* status of events with mailbox and link attention */
9146 status1 = phba->ha_copy & (HA_MBATT | HA_LATT | HA_ERATT);
9147
9148 /* status of events with ELS ring */
9149 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_ELS_RING)));
9150 status2 >>= (4*LPFC_ELS_RING);
9151
9152 if (status1 || (status2 & HA_RXMASK))
James Smart3772a992009-05-22 14:50:54 -04009153 sp_irq_rc = lpfc_sli_sp_intr_handler(irq, dev_id);
James Smart93996272008-08-24 21:50:30 -04009154 else
9155 sp_irq_rc = IRQ_NONE;
9156
9157 /*
9158 * Invoke fast-path host attention interrupt handling as appropriate.
9159 */
9160
9161 /* status of events with FCP ring */
9162 status1 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
9163 status1 >>= (4*LPFC_FCP_RING);
9164
9165 /* status of events with extra ring */
9166 if (phba->cfg_multi_ring_support == 2) {
9167 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
9168 status2 >>= (4*LPFC_EXTRA_RING);
9169 } else
9170 status2 = 0;
9171
9172 if ((status1 & HA_RXMASK) || (status2 & HA_RXMASK))
James Smart3772a992009-05-22 14:50:54 -04009173 fp_irq_rc = lpfc_sli_fp_intr_handler(irq, dev_id);
James Smart93996272008-08-24 21:50:30 -04009174 else
9175 fp_irq_rc = IRQ_NONE;
9176
9177 /* Return device-level interrupt handling status */
9178 return (sp_irq_rc == IRQ_HANDLED) ? sp_irq_rc : fp_irq_rc;
James Smart3772a992009-05-22 14:50:54 -04009179} /* lpfc_sli_intr_handler */
James Smart4f774512009-05-22 14:52:35 -04009180
9181/**
9182 * lpfc_sli4_fcp_xri_abort_event_proc - Process fcp xri abort event
9183 * @phba: pointer to lpfc hba data structure.
9184 *
9185 * This routine is invoked by the worker thread to process all the pending
9186 * SLI4 FCP abort XRI events.
9187 **/
9188void lpfc_sli4_fcp_xri_abort_event_proc(struct lpfc_hba *phba)
9189{
9190 struct lpfc_cq_event *cq_event;
9191
9192 /* First, declare the fcp xri abort event has been handled */
9193 spin_lock_irq(&phba->hbalock);
9194 phba->hba_flag &= ~FCP_XRI_ABORT_EVENT;
9195 spin_unlock_irq(&phba->hbalock);
9196 /* Now, handle all the fcp xri abort events */
9197 while (!list_empty(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue)) {
9198 /* Get the first event from the head of the event queue */
9199 spin_lock_irq(&phba->hbalock);
9200 list_remove_head(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue,
9201 cq_event, struct lpfc_cq_event, list);
9202 spin_unlock_irq(&phba->hbalock);
9203 /* Notify aborted XRI for FCP work queue */
9204 lpfc_sli4_fcp_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
9205 /* Free the event processed back to the free pool */
9206 lpfc_sli4_cq_event_release(phba, cq_event);
9207 }
9208}
9209
9210/**
9211 * lpfc_sli4_els_xri_abort_event_proc - Process els xri abort event
9212 * @phba: pointer to lpfc hba data structure.
9213 *
9214 * This routine is invoked by the worker thread to process all the pending
9215 * SLI4 els abort xri events.
9216 **/
9217void lpfc_sli4_els_xri_abort_event_proc(struct lpfc_hba *phba)
9218{
9219 struct lpfc_cq_event *cq_event;
9220
9221 /* First, declare the els xri abort event has been handled */
9222 spin_lock_irq(&phba->hbalock);
9223 phba->hba_flag &= ~ELS_XRI_ABORT_EVENT;
9224 spin_unlock_irq(&phba->hbalock);
9225 /* Now, handle all the els xri abort events */
9226 while (!list_empty(&phba->sli4_hba.sp_els_xri_aborted_work_queue)) {
9227 /* Get the first event from the head of the event queue */
9228 spin_lock_irq(&phba->hbalock);
9229 list_remove_head(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
9230 cq_event, struct lpfc_cq_event, list);
9231 spin_unlock_irq(&phba->hbalock);
9232 /* Notify aborted XRI for ELS work queue */
9233 lpfc_sli4_els_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
9234 /* Free the event processed back to the free pool */
9235 lpfc_sli4_cq_event_release(phba, cq_event);
9236 }
9237}
9238
James Smart341af102010-01-26 23:07:37 -05009239/**
9240 * lpfc_sli4_iocb_param_transfer - Transfer pIocbOut and cmpl status to pIocbIn
9241 * @phba: pointer to lpfc hba data structure
9242 * @pIocbIn: pointer to the rspiocbq
9243 * @pIocbOut: pointer to the cmdiocbq
9244 * @wcqe: pointer to the complete wcqe
9245 *
9246 * This routine transfers the fields of a command iocbq to a response iocbq
9247 * by copying all the IOCB fields from command iocbq and transferring the
9248 * completion status information from the complete wcqe.
9249 **/
James Smart4f774512009-05-22 14:52:35 -04009250static void
James Smart341af102010-01-26 23:07:37 -05009251lpfc_sli4_iocb_param_transfer(struct lpfc_hba *phba,
9252 struct lpfc_iocbq *pIocbIn,
James Smart4f774512009-05-22 14:52:35 -04009253 struct lpfc_iocbq *pIocbOut,
9254 struct lpfc_wcqe_complete *wcqe)
9255{
James Smart341af102010-01-26 23:07:37 -05009256 unsigned long iflags;
James Smart4f774512009-05-22 14:52:35 -04009257 size_t offset = offsetof(struct lpfc_iocbq, iocb);
9258
9259 memcpy((char *)pIocbIn + offset, (char *)pIocbOut + offset,
9260 sizeof(struct lpfc_iocbq) - offset);
James Smart4f774512009-05-22 14:52:35 -04009261 /* Map WCQE parameters into irspiocb parameters */
9262 pIocbIn->iocb.ulpStatus = bf_get(lpfc_wcqe_c_status, wcqe);
9263 if (pIocbOut->iocb_flag & LPFC_IO_FCP)
9264 if (pIocbIn->iocb.ulpStatus == IOSTAT_FCP_RSP_ERROR)
9265 pIocbIn->iocb.un.fcpi.fcpi_parm =
9266 pIocbOut->iocb.un.fcpi.fcpi_parm -
9267 wcqe->total_data_placed;
9268 else
9269 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
James Smart695a8142010-01-26 23:08:03 -05009270 else {
James Smart4f774512009-05-22 14:52:35 -04009271 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
James Smart695a8142010-01-26 23:08:03 -05009272 pIocbIn->iocb.un.genreq64.bdl.bdeSize = wcqe->total_data_placed;
9273 }
James Smart341af102010-01-26 23:07:37 -05009274
9275 /* Pick up HBA exchange busy condition */
9276 if (bf_get(lpfc_wcqe_c_xb, wcqe)) {
9277 spin_lock_irqsave(&phba->hbalock, iflags);
9278 pIocbIn->iocb_flag |= LPFC_EXCHANGE_BUSY;
9279 spin_unlock_irqrestore(&phba->hbalock, iflags);
9280 }
James Smart4f774512009-05-22 14:52:35 -04009281}
9282
9283/**
James Smart45ed1192009-10-02 15:17:02 -04009284 * lpfc_sli4_els_wcqe_to_rspiocbq - Get response iocbq from els wcqe
9285 * @phba: Pointer to HBA context object.
9286 * @wcqe: Pointer to work-queue completion queue entry.
9287 *
9288 * This routine handles an ELS work-queue completion event and construct
9289 * a pseudo response ELS IODBQ from the SLI4 ELS WCQE for the common
9290 * discovery engine to handle.
9291 *
9292 * Return: Pointer to the receive IOCBQ, NULL otherwise.
9293 **/
9294static struct lpfc_iocbq *
9295lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *phba,
9296 struct lpfc_iocbq *irspiocbq)
9297{
9298 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
9299 struct lpfc_iocbq *cmdiocbq;
9300 struct lpfc_wcqe_complete *wcqe;
9301 unsigned long iflags;
9302
9303 wcqe = &irspiocbq->cq_event.cqe.wcqe_cmpl;
9304 spin_lock_irqsave(&phba->hbalock, iflags);
9305 pring->stats.iocb_event++;
9306 /* Look up the ELS command IOCB and create pseudo response IOCB */
9307 cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
9308 bf_get(lpfc_wcqe_c_request_tag, wcqe));
9309 spin_unlock_irqrestore(&phba->hbalock, iflags);
9310
9311 if (unlikely(!cmdiocbq)) {
9312 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9313 "0386 ELS complete with no corresponding "
9314 "cmdiocb: iotag (%d)\n",
9315 bf_get(lpfc_wcqe_c_request_tag, wcqe));
9316 lpfc_sli_release_iocbq(phba, irspiocbq);
9317 return NULL;
9318 }
9319
9320 /* Fake the irspiocbq and copy necessary response information */
James Smart341af102010-01-26 23:07:37 -05009321 lpfc_sli4_iocb_param_transfer(phba, irspiocbq, cmdiocbq, wcqe);
James Smart45ed1192009-10-02 15:17:02 -04009322
9323 return irspiocbq;
9324}
9325
9326/**
James Smart04c68492009-05-22 14:52:52 -04009327 * lpfc_sli4_sp_handle_async_event - Handle an asynchroous event
9328 * @phba: Pointer to HBA context object.
9329 * @cqe: Pointer to mailbox completion queue entry.
9330 *
9331 * This routine process a mailbox completion queue entry with asynchrous
9332 * event.
9333 *
9334 * Return: true if work posted to worker thread, otherwise false.
9335 **/
9336static bool
9337lpfc_sli4_sp_handle_async_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
9338{
9339 struct lpfc_cq_event *cq_event;
9340 unsigned long iflags;
9341
9342 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
9343 "0392 Async Event: word0:x%x, word1:x%x, "
9344 "word2:x%x, word3:x%x\n", mcqe->word0,
9345 mcqe->mcqe_tag0, mcqe->mcqe_tag1, mcqe->trailer);
9346
9347 /* Allocate a new internal CQ_EVENT entry */
9348 cq_event = lpfc_sli4_cq_event_alloc(phba);
9349 if (!cq_event) {
9350 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9351 "0394 Failed to allocate CQ_EVENT entry\n");
9352 return false;
9353 }
9354
9355 /* Move the CQE into an asynchronous event entry */
9356 memcpy(&cq_event->cqe, mcqe, sizeof(struct lpfc_mcqe));
9357 spin_lock_irqsave(&phba->hbalock, iflags);
9358 list_add_tail(&cq_event->list, &phba->sli4_hba.sp_asynce_work_queue);
9359 /* Set the async event flag */
9360 phba->hba_flag |= ASYNC_EVENT;
9361 spin_unlock_irqrestore(&phba->hbalock, iflags);
9362
9363 return true;
9364}
9365
9366/**
9367 * lpfc_sli4_sp_handle_mbox_event - Handle a mailbox completion event
9368 * @phba: Pointer to HBA context object.
9369 * @cqe: Pointer to mailbox completion queue entry.
9370 *
9371 * This routine process a mailbox completion queue entry with mailbox
9372 * completion event.
9373 *
9374 * Return: true if work posted to worker thread, otherwise false.
9375 **/
9376static bool
9377lpfc_sli4_sp_handle_mbox_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
9378{
9379 uint32_t mcqe_status;
9380 MAILBOX_t *mbox, *pmbox;
9381 struct lpfc_mqe *mqe;
9382 struct lpfc_vport *vport;
9383 struct lpfc_nodelist *ndlp;
9384 struct lpfc_dmabuf *mp;
9385 unsigned long iflags;
9386 LPFC_MBOXQ_t *pmb;
9387 bool workposted = false;
9388 int rc;
9389
9390 /* If not a mailbox complete MCQE, out by checking mailbox consume */
9391 if (!bf_get(lpfc_trailer_completed, mcqe))
9392 goto out_no_mqe_complete;
9393
9394 /* Get the reference to the active mbox command */
9395 spin_lock_irqsave(&phba->hbalock, iflags);
9396 pmb = phba->sli.mbox_active;
9397 if (unlikely(!pmb)) {
9398 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
9399 "1832 No pending MBOX command to handle\n");
9400 spin_unlock_irqrestore(&phba->hbalock, iflags);
9401 goto out_no_mqe_complete;
9402 }
9403 spin_unlock_irqrestore(&phba->hbalock, iflags);
9404 mqe = &pmb->u.mqe;
9405 pmbox = (MAILBOX_t *)&pmb->u.mqe;
9406 mbox = phba->mbox;
9407 vport = pmb->vport;
9408
9409 /* Reset heartbeat timer */
9410 phba->last_completion_time = jiffies;
9411 del_timer(&phba->sli.mbox_tmo);
9412
9413 /* Move mbox data to caller's mailbox region, do endian swapping */
9414 if (pmb->mbox_cmpl && mbox)
9415 lpfc_sli_pcimem_bcopy(mbox, mqe, sizeof(struct lpfc_mqe));
9416 /* Set the mailbox status with SLI4 range 0x4000 */
9417 mcqe_status = bf_get(lpfc_mcqe_status, mcqe);
9418 if (mcqe_status != MB_CQE_STATUS_SUCCESS)
9419 bf_set(lpfc_mqe_status, mqe,
9420 (LPFC_MBX_ERROR_RANGE | mcqe_status));
9421
9422 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
9423 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
9424 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_MBOX_VPORT,
9425 "MBOX dflt rpi: status:x%x rpi:x%x",
9426 mcqe_status,
9427 pmbox->un.varWords[0], 0);
9428 if (mcqe_status == MB_CQE_STATUS_SUCCESS) {
9429 mp = (struct lpfc_dmabuf *)(pmb->context1);
9430 ndlp = (struct lpfc_nodelist *)pmb->context2;
9431 /* Reg_LOGIN of dflt RPI was successful. Now lets get
9432 * RID of the PPI using the same mbox buffer.
9433 */
9434 lpfc_unreg_login(phba, vport->vpi,
9435 pmbox->un.varWords[0], pmb);
9436 pmb->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
9437 pmb->context1 = mp;
9438 pmb->context2 = ndlp;
9439 pmb->vport = vport;
9440 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
9441 if (rc != MBX_BUSY)
9442 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
9443 LOG_SLI, "0385 rc should "
9444 "have been MBX_BUSY\n");
9445 if (rc != MBX_NOT_FINISHED)
9446 goto send_current_mbox;
9447 }
9448 }
9449 spin_lock_irqsave(&phba->pport->work_port_lock, iflags);
9450 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
9451 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflags);
9452
9453 /* There is mailbox completion work to do */
9454 spin_lock_irqsave(&phba->hbalock, iflags);
9455 __lpfc_mbox_cmpl_put(phba, pmb);
9456 phba->work_ha |= HA_MBATT;
9457 spin_unlock_irqrestore(&phba->hbalock, iflags);
9458 workposted = true;
9459
9460send_current_mbox:
9461 spin_lock_irqsave(&phba->hbalock, iflags);
9462 /* Release the mailbox command posting token */
9463 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
9464 /* Setting active mailbox pointer need to be in sync to flag clear */
9465 phba->sli.mbox_active = NULL;
9466 spin_unlock_irqrestore(&phba->hbalock, iflags);
9467 /* Wake up worker thread to post the next pending mailbox command */
9468 lpfc_worker_wake_up(phba);
9469out_no_mqe_complete:
9470 if (bf_get(lpfc_trailer_consumed, mcqe))
9471 lpfc_sli4_mq_release(phba->sli4_hba.mbx_wq);
9472 return workposted;
9473}
9474
9475/**
9476 * lpfc_sli4_sp_handle_mcqe - Process a mailbox completion queue entry
9477 * @phba: Pointer to HBA context object.
9478 * @cqe: Pointer to mailbox completion queue entry.
9479 *
9480 * This routine process a mailbox completion queue entry, it invokes the
9481 * proper mailbox complete handling or asynchrous event handling routine
9482 * according to the MCQE's async bit.
9483 *
9484 * Return: true if work posted to worker thread, otherwise false.
9485 **/
9486static bool
9487lpfc_sli4_sp_handle_mcqe(struct lpfc_hba *phba, struct lpfc_cqe *cqe)
9488{
9489 struct lpfc_mcqe mcqe;
9490 bool workposted;
9491
9492 /* Copy the mailbox MCQE and convert endian order as needed */
9493 lpfc_sli_pcimem_bcopy(cqe, &mcqe, sizeof(struct lpfc_mcqe));
9494
9495 /* Invoke the proper event handling routine */
9496 if (!bf_get(lpfc_trailer_async, &mcqe))
9497 workposted = lpfc_sli4_sp_handle_mbox_event(phba, &mcqe);
9498 else
9499 workposted = lpfc_sli4_sp_handle_async_event(phba, &mcqe);
9500 return workposted;
9501}
9502
9503/**
James Smart4f774512009-05-22 14:52:35 -04009504 * lpfc_sli4_sp_handle_els_wcqe - Handle els work-queue completion event
9505 * @phba: Pointer to HBA context object.
9506 * @wcqe: Pointer to work-queue completion queue entry.
9507 *
9508 * This routine handles an ELS work-queue completion event.
9509 *
9510 * Return: true if work posted to worker thread, otherwise false.
9511 **/
9512static bool
9513lpfc_sli4_sp_handle_els_wcqe(struct lpfc_hba *phba,
9514 struct lpfc_wcqe_complete *wcqe)
9515{
James Smart4f774512009-05-22 14:52:35 -04009516 struct lpfc_iocbq *irspiocbq;
9517 unsigned long iflags;
James Smart2a9bf3d2010-06-07 15:24:45 -04009518 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_FCP_RING];
James Smart4f774512009-05-22 14:52:35 -04009519
James Smart45ed1192009-10-02 15:17:02 -04009520 /* Get an irspiocbq for later ELS response processing use */
James Smart4f774512009-05-22 14:52:35 -04009521 irspiocbq = lpfc_sli_get_iocbq(phba);
9522 if (!irspiocbq) {
9523 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart2a9bf3d2010-06-07 15:24:45 -04009524 "0387 NO IOCBQ data: txq_cnt=%d iocb_cnt=%d "
9525 "fcp_txcmplq_cnt=%d, els_txcmplq_cnt=%d\n",
9526 pring->txq_cnt, phba->iocb_cnt,
9527 phba->sli.ring[LPFC_FCP_RING].txcmplq_cnt,
9528 phba->sli.ring[LPFC_ELS_RING].txcmplq_cnt);
James Smart45ed1192009-10-02 15:17:02 -04009529 return false;
James Smart4f774512009-05-22 14:52:35 -04009530 }
James Smart4f774512009-05-22 14:52:35 -04009531
James Smart45ed1192009-10-02 15:17:02 -04009532 /* Save off the slow-path queue event for work thread to process */
9533 memcpy(&irspiocbq->cq_event.cqe.wcqe_cmpl, wcqe, sizeof(*wcqe));
James Smart4f774512009-05-22 14:52:35 -04009534 spin_lock_irqsave(&phba->hbalock, iflags);
James Smart4d9ab992009-10-02 15:16:39 -04009535 list_add_tail(&irspiocbq->cq_event.list,
James Smart45ed1192009-10-02 15:17:02 -04009536 &phba->sli4_hba.sp_queue_event);
9537 phba->hba_flag |= HBA_SP_QUEUE_EVT;
James Smart4f774512009-05-22 14:52:35 -04009538 spin_unlock_irqrestore(&phba->hbalock, iflags);
James Smart4f774512009-05-22 14:52:35 -04009539
James Smart45ed1192009-10-02 15:17:02 -04009540 return true;
James Smart4f774512009-05-22 14:52:35 -04009541}
9542
9543/**
9544 * lpfc_sli4_sp_handle_rel_wcqe - Handle slow-path WQ entry consumed event
9545 * @phba: Pointer to HBA context object.
9546 * @wcqe: Pointer to work-queue completion queue entry.
9547 *
9548 * This routine handles slow-path WQ entry comsumed event by invoking the
9549 * proper WQ release routine to the slow-path WQ.
9550 **/
9551static void
9552lpfc_sli4_sp_handle_rel_wcqe(struct lpfc_hba *phba,
9553 struct lpfc_wcqe_release *wcqe)
9554{
9555 /* Check for the slow-path ELS work queue */
9556 if (bf_get(lpfc_wcqe_r_wq_id, wcqe) == phba->sli4_hba.els_wq->queue_id)
9557 lpfc_sli4_wq_release(phba->sli4_hba.els_wq,
9558 bf_get(lpfc_wcqe_r_wqe_index, wcqe));
9559 else
9560 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9561 "2579 Slow-path wqe consume event carries "
9562 "miss-matched qid: wcqe-qid=x%x, sp-qid=x%x\n",
9563 bf_get(lpfc_wcqe_r_wqe_index, wcqe),
9564 phba->sli4_hba.els_wq->queue_id);
9565}
9566
9567/**
9568 * lpfc_sli4_sp_handle_abort_xri_wcqe - Handle a xri abort event
9569 * @phba: Pointer to HBA context object.
9570 * @cq: Pointer to a WQ completion queue.
9571 * @wcqe: Pointer to work-queue completion queue entry.
9572 *
9573 * This routine handles an XRI abort event.
9574 *
9575 * Return: true if work posted to worker thread, otherwise false.
9576 **/
9577static bool
9578lpfc_sli4_sp_handle_abort_xri_wcqe(struct lpfc_hba *phba,
9579 struct lpfc_queue *cq,
9580 struct sli4_wcqe_xri_aborted *wcqe)
9581{
9582 bool workposted = false;
9583 struct lpfc_cq_event *cq_event;
9584 unsigned long iflags;
9585
9586 /* Allocate a new internal CQ_EVENT entry */
9587 cq_event = lpfc_sli4_cq_event_alloc(phba);
9588 if (!cq_event) {
9589 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9590 "0602 Failed to allocate CQ_EVENT entry\n");
9591 return false;
9592 }
9593
9594 /* Move the CQE into the proper xri abort event list */
9595 memcpy(&cq_event->cqe, wcqe, sizeof(struct sli4_wcqe_xri_aborted));
9596 switch (cq->subtype) {
9597 case LPFC_FCP:
9598 spin_lock_irqsave(&phba->hbalock, iflags);
9599 list_add_tail(&cq_event->list,
9600 &phba->sli4_hba.sp_fcp_xri_aborted_work_queue);
9601 /* Set the fcp xri abort event flag */
9602 phba->hba_flag |= FCP_XRI_ABORT_EVENT;
9603 spin_unlock_irqrestore(&phba->hbalock, iflags);
9604 workposted = true;
9605 break;
9606 case LPFC_ELS:
9607 spin_lock_irqsave(&phba->hbalock, iflags);
9608 list_add_tail(&cq_event->list,
9609 &phba->sli4_hba.sp_els_xri_aborted_work_queue);
9610 /* Set the els xri abort event flag */
9611 phba->hba_flag |= ELS_XRI_ABORT_EVENT;
9612 spin_unlock_irqrestore(&phba->hbalock, iflags);
9613 workposted = true;
9614 break;
9615 default:
9616 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9617 "0603 Invalid work queue CQE subtype (x%x)\n",
9618 cq->subtype);
9619 workposted = false;
9620 break;
9621 }
9622 return workposted;
9623}
9624
9625/**
James Smart4d9ab992009-10-02 15:16:39 -04009626 * lpfc_sli4_sp_handle_rcqe - Process a receive-queue completion queue entry
James Smart4f774512009-05-22 14:52:35 -04009627 * @phba: Pointer to HBA context object.
James Smart4d9ab992009-10-02 15:16:39 -04009628 * @rcqe: Pointer to receive-queue completion queue entry.
James Smart4f774512009-05-22 14:52:35 -04009629 *
James Smart4d9ab992009-10-02 15:16:39 -04009630 * This routine process a receive-queue completion queue entry.
James Smart4f774512009-05-22 14:52:35 -04009631 *
9632 * Return: true if work posted to worker thread, otherwise false.
9633 **/
9634static bool
James Smart4d9ab992009-10-02 15:16:39 -04009635lpfc_sli4_sp_handle_rcqe(struct lpfc_hba *phba, struct lpfc_rcqe *rcqe)
9636{
9637 bool workposted = false;
9638 struct lpfc_queue *hrq = phba->sli4_hba.hdr_rq;
9639 struct lpfc_queue *drq = phba->sli4_hba.dat_rq;
9640 struct hbq_dmabuf *dma_buf;
9641 uint32_t status;
9642 unsigned long iflags;
9643
James Smart4d9ab992009-10-02 15:16:39 -04009644 if (bf_get(lpfc_rcqe_rq_id, rcqe) != hrq->queue_id)
9645 goto out;
9646
9647 status = bf_get(lpfc_rcqe_status, rcqe);
9648 switch (status) {
9649 case FC_STATUS_RQ_BUF_LEN_EXCEEDED:
9650 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9651 "2537 Receive Frame Truncated!!\n");
9652 case FC_STATUS_RQ_SUCCESS:
James Smart5ffc2662009-11-18 15:39:44 -05009653 lpfc_sli4_rq_release(hrq, drq);
James Smart4d9ab992009-10-02 15:16:39 -04009654 spin_lock_irqsave(&phba->hbalock, iflags);
9655 dma_buf = lpfc_sli_hbqbuf_get(&phba->hbqs[0].hbq_buffer_list);
9656 if (!dma_buf) {
9657 spin_unlock_irqrestore(&phba->hbalock, iflags);
9658 goto out;
9659 }
9660 memcpy(&dma_buf->cq_event.cqe.rcqe_cmpl, rcqe, sizeof(*rcqe));
9661 /* save off the frame for the word thread to process */
9662 list_add_tail(&dma_buf->cq_event.list,
James Smart45ed1192009-10-02 15:17:02 -04009663 &phba->sli4_hba.sp_queue_event);
James Smart4d9ab992009-10-02 15:16:39 -04009664 /* Frame received */
James Smart45ed1192009-10-02 15:17:02 -04009665 phba->hba_flag |= HBA_SP_QUEUE_EVT;
James Smart4d9ab992009-10-02 15:16:39 -04009666 spin_unlock_irqrestore(&phba->hbalock, iflags);
9667 workposted = true;
9668 break;
9669 case FC_STATUS_INSUFF_BUF_NEED_BUF:
9670 case FC_STATUS_INSUFF_BUF_FRM_DISC:
9671 /* Post more buffers if possible */
9672 spin_lock_irqsave(&phba->hbalock, iflags);
9673 phba->hba_flag |= HBA_POST_RECEIVE_BUFFER;
9674 spin_unlock_irqrestore(&phba->hbalock, iflags);
9675 workposted = true;
9676 break;
9677 }
9678out:
9679 return workposted;
James Smart4d9ab992009-10-02 15:16:39 -04009680}
9681
9682/**
9683 * lpfc_sli4_sp_handle_cqe - Process a slow path completion queue entry
9684 * @phba: Pointer to HBA context object.
9685 * @cq: Pointer to the completion queue.
9686 * @wcqe: Pointer to a completion queue entry.
9687 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03009688 * This routine process a slow-path work-queue or receive queue completion queue
James Smart4d9ab992009-10-02 15:16:39 -04009689 * entry.
9690 *
9691 * Return: true if work posted to worker thread, otherwise false.
9692 **/
9693static bool
9694lpfc_sli4_sp_handle_cqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
James Smart4f774512009-05-22 14:52:35 -04009695 struct lpfc_cqe *cqe)
9696{
James Smart45ed1192009-10-02 15:17:02 -04009697 struct lpfc_cqe cqevt;
James Smart4f774512009-05-22 14:52:35 -04009698 bool workposted = false;
9699
9700 /* Copy the work queue CQE and convert endian order if needed */
James Smart45ed1192009-10-02 15:17:02 -04009701 lpfc_sli_pcimem_bcopy(cqe, &cqevt, sizeof(struct lpfc_cqe));
James Smart4f774512009-05-22 14:52:35 -04009702
9703 /* Check and process for different type of WCQE and dispatch */
James Smart45ed1192009-10-02 15:17:02 -04009704 switch (bf_get(lpfc_cqe_code, &cqevt)) {
James Smart4f774512009-05-22 14:52:35 -04009705 case CQE_CODE_COMPL_WQE:
James Smart45ed1192009-10-02 15:17:02 -04009706 /* Process the WQ/RQ complete event */
James Smartbc739052010-08-04 16:11:18 -04009707 phba->last_completion_time = jiffies;
James Smart4f774512009-05-22 14:52:35 -04009708 workposted = lpfc_sli4_sp_handle_els_wcqe(phba,
James Smart45ed1192009-10-02 15:17:02 -04009709 (struct lpfc_wcqe_complete *)&cqevt);
James Smart4f774512009-05-22 14:52:35 -04009710 break;
9711 case CQE_CODE_RELEASE_WQE:
9712 /* Process the WQ release event */
9713 lpfc_sli4_sp_handle_rel_wcqe(phba,
James Smart45ed1192009-10-02 15:17:02 -04009714 (struct lpfc_wcqe_release *)&cqevt);
James Smart4f774512009-05-22 14:52:35 -04009715 break;
9716 case CQE_CODE_XRI_ABORTED:
9717 /* Process the WQ XRI abort event */
James Smartbc739052010-08-04 16:11:18 -04009718 phba->last_completion_time = jiffies;
James Smart4f774512009-05-22 14:52:35 -04009719 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
James Smart45ed1192009-10-02 15:17:02 -04009720 (struct sli4_wcqe_xri_aborted *)&cqevt);
James Smart4f774512009-05-22 14:52:35 -04009721 break;
James Smart4d9ab992009-10-02 15:16:39 -04009722 case CQE_CODE_RECEIVE:
9723 /* Process the RQ event */
James Smartbc739052010-08-04 16:11:18 -04009724 phba->last_completion_time = jiffies;
James Smart4d9ab992009-10-02 15:16:39 -04009725 workposted = lpfc_sli4_sp_handle_rcqe(phba,
James Smart45ed1192009-10-02 15:17:02 -04009726 (struct lpfc_rcqe *)&cqevt);
James Smart4d9ab992009-10-02 15:16:39 -04009727 break;
James Smart4f774512009-05-22 14:52:35 -04009728 default:
9729 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9730 "0388 Not a valid WCQE code: x%x\n",
James Smart45ed1192009-10-02 15:17:02 -04009731 bf_get(lpfc_cqe_code, &cqevt));
James Smart4f774512009-05-22 14:52:35 -04009732 break;
9733 }
9734 return workposted;
9735}
9736
9737/**
James Smart4f774512009-05-22 14:52:35 -04009738 * lpfc_sli4_sp_handle_eqe - Process a slow-path event queue entry
9739 * @phba: Pointer to HBA context object.
9740 * @eqe: Pointer to fast-path event queue entry.
9741 *
9742 * This routine process a event queue entry from the slow-path event queue.
9743 * It will check the MajorCode and MinorCode to determine this is for a
9744 * completion event on a completion queue, if not, an error shall be logged
9745 * and just return. Otherwise, it will get to the corresponding completion
9746 * queue and process all the entries on that completion queue, rearm the
9747 * completion queue, and then return.
9748 *
9749 **/
9750static void
9751lpfc_sli4_sp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe)
9752{
9753 struct lpfc_queue *cq = NULL, *childq, *speq;
9754 struct lpfc_cqe *cqe;
9755 bool workposted = false;
9756 int ecount = 0;
9757 uint16_t cqid;
9758
James Smartcb5172e2010-03-15 11:25:07 -04009759 if (bf_get_le32(lpfc_eqe_major_code, eqe) != 0) {
James Smart4f774512009-05-22 14:52:35 -04009760 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9761 "0359 Not a valid slow-path completion "
9762 "event: majorcode=x%x, minorcode=x%x\n",
James Smartcb5172e2010-03-15 11:25:07 -04009763 bf_get_le32(lpfc_eqe_major_code, eqe),
9764 bf_get_le32(lpfc_eqe_minor_code, eqe));
James Smart4f774512009-05-22 14:52:35 -04009765 return;
9766 }
9767
9768 /* Get the reference to the corresponding CQ */
James Smartcb5172e2010-03-15 11:25:07 -04009769 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
James Smart4f774512009-05-22 14:52:35 -04009770
9771 /* Search for completion queue pointer matching this cqid */
9772 speq = phba->sli4_hba.sp_eq;
9773 list_for_each_entry(childq, &speq->child_list, list) {
9774 if (childq->queue_id == cqid) {
9775 cq = childq;
9776 break;
9777 }
9778 }
9779 if (unlikely(!cq)) {
James Smart75baf692010-06-08 18:31:21 -04009780 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
9781 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9782 "0365 Slow-path CQ identifier "
9783 "(%d) does not exist\n", cqid);
James Smart4f774512009-05-22 14:52:35 -04009784 return;
9785 }
9786
9787 /* Process all the entries to the CQ */
9788 switch (cq->type) {
9789 case LPFC_MCQ:
9790 while ((cqe = lpfc_sli4_cq_get(cq))) {
9791 workposted |= lpfc_sli4_sp_handle_mcqe(phba, cqe);
9792 if (!(++ecount % LPFC_GET_QE_REL_INT))
9793 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
9794 }
9795 break;
9796 case LPFC_WCQ:
9797 while ((cqe = lpfc_sli4_cq_get(cq))) {
James Smart4d9ab992009-10-02 15:16:39 -04009798 workposted |= lpfc_sli4_sp_handle_cqe(phba, cq, cqe);
James Smart4f774512009-05-22 14:52:35 -04009799 if (!(++ecount % LPFC_GET_QE_REL_INT))
9800 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
9801 }
9802 break;
9803 default:
9804 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9805 "0370 Invalid completion queue type (%d)\n",
9806 cq->type);
9807 return;
9808 }
9809
9810 /* Catch the no cq entry condition, log an error */
9811 if (unlikely(ecount == 0))
9812 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9813 "0371 No entry from the CQ: identifier "
9814 "(x%x), type (%d)\n", cq->queue_id, cq->type);
9815
9816 /* In any case, flash and re-arm the RCQ */
9817 lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
9818
9819 /* wake up worker thread if there are works to be done */
9820 if (workposted)
9821 lpfc_worker_wake_up(phba);
9822}
9823
9824/**
9825 * lpfc_sli4_fp_handle_fcp_wcqe - Process fast-path work queue completion entry
9826 * @eqe: Pointer to fast-path completion queue entry.
9827 *
9828 * This routine process a fast-path work queue completion entry from fast-path
9829 * event queue for FCP command response completion.
9830 **/
9831static void
9832lpfc_sli4_fp_handle_fcp_wcqe(struct lpfc_hba *phba,
9833 struct lpfc_wcqe_complete *wcqe)
9834{
9835 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_FCP_RING];
9836 struct lpfc_iocbq *cmdiocbq;
9837 struct lpfc_iocbq irspiocbq;
9838 unsigned long iflags;
9839
9840 spin_lock_irqsave(&phba->hbalock, iflags);
9841 pring->stats.iocb_event++;
9842 spin_unlock_irqrestore(&phba->hbalock, iflags);
9843
9844 /* Check for response status */
9845 if (unlikely(bf_get(lpfc_wcqe_c_status, wcqe))) {
9846 /* If resource errors reported from HBA, reduce queue
9847 * depth of the SCSI device.
9848 */
9849 if ((bf_get(lpfc_wcqe_c_status, wcqe) ==
9850 IOSTAT_LOCAL_REJECT) &&
9851 (wcqe->parameter == IOERR_NO_RESOURCES)) {
9852 phba->lpfc_rampdown_queue_depth(phba);
9853 }
9854 /* Log the error status */
9855 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9856 "0373 FCP complete error: status=x%x, "
9857 "hw_status=x%x, total_data_specified=%d, "
9858 "parameter=x%x, word3=x%x\n",
9859 bf_get(lpfc_wcqe_c_status, wcqe),
9860 bf_get(lpfc_wcqe_c_hw_status, wcqe),
9861 wcqe->total_data_placed, wcqe->parameter,
9862 wcqe->word3);
9863 }
9864
9865 /* Look up the FCP command IOCB and create pseudo response IOCB */
9866 spin_lock_irqsave(&phba->hbalock, iflags);
9867 cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
9868 bf_get(lpfc_wcqe_c_request_tag, wcqe));
9869 spin_unlock_irqrestore(&phba->hbalock, iflags);
9870 if (unlikely(!cmdiocbq)) {
9871 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9872 "0374 FCP complete with no corresponding "
9873 "cmdiocb: iotag (%d)\n",
9874 bf_get(lpfc_wcqe_c_request_tag, wcqe));
9875 return;
9876 }
9877 if (unlikely(!cmdiocbq->iocb_cmpl)) {
9878 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9879 "0375 FCP cmdiocb not callback function "
9880 "iotag: (%d)\n",
9881 bf_get(lpfc_wcqe_c_request_tag, wcqe));
9882 return;
9883 }
9884
9885 /* Fake the irspiocb and copy necessary response information */
James Smart341af102010-01-26 23:07:37 -05009886 lpfc_sli4_iocb_param_transfer(phba, &irspiocbq, cmdiocbq, wcqe);
James Smart4f774512009-05-22 14:52:35 -04009887
James Smart0f65ff62010-02-26 14:14:23 -05009888 if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED) {
9889 spin_lock_irqsave(&phba->hbalock, iflags);
9890 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
9891 spin_unlock_irqrestore(&phba->hbalock, iflags);
9892 }
9893
James Smart4f774512009-05-22 14:52:35 -04009894 /* Pass the cmd_iocb and the rsp state to the upper layer */
9895 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq, &irspiocbq);
9896}
9897
9898/**
9899 * lpfc_sli4_fp_handle_rel_wcqe - Handle fast-path WQ entry consumed event
9900 * @phba: Pointer to HBA context object.
9901 * @cq: Pointer to completion queue.
9902 * @wcqe: Pointer to work-queue completion queue entry.
9903 *
9904 * This routine handles an fast-path WQ entry comsumed event by invoking the
9905 * proper WQ release routine to the slow-path WQ.
9906 **/
9907static void
9908lpfc_sli4_fp_handle_rel_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
9909 struct lpfc_wcqe_release *wcqe)
9910{
9911 struct lpfc_queue *childwq;
9912 bool wqid_matched = false;
9913 uint16_t fcp_wqid;
9914
9915 /* Check for fast-path FCP work queue release */
9916 fcp_wqid = bf_get(lpfc_wcqe_r_wq_id, wcqe);
9917 list_for_each_entry(childwq, &cq->child_list, list) {
9918 if (childwq->queue_id == fcp_wqid) {
9919 lpfc_sli4_wq_release(childwq,
9920 bf_get(lpfc_wcqe_r_wqe_index, wcqe));
9921 wqid_matched = true;
9922 break;
9923 }
9924 }
9925 /* Report warning log message if no match found */
9926 if (wqid_matched != true)
9927 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9928 "2580 Fast-path wqe consume event carries "
9929 "miss-matched qid: wcqe-qid=x%x\n", fcp_wqid);
9930}
9931
9932/**
9933 * lpfc_sli4_fp_handle_wcqe - Process fast-path work queue completion entry
9934 * @cq: Pointer to the completion queue.
9935 * @eqe: Pointer to fast-path completion queue entry.
9936 *
9937 * This routine process a fast-path work queue completion entry from fast-path
9938 * event queue for FCP command response completion.
9939 **/
9940static int
9941lpfc_sli4_fp_handle_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
9942 struct lpfc_cqe *cqe)
9943{
9944 struct lpfc_wcqe_release wcqe;
9945 bool workposted = false;
9946
9947 /* Copy the work queue CQE and convert endian order if needed */
9948 lpfc_sli_pcimem_bcopy(cqe, &wcqe, sizeof(struct lpfc_cqe));
9949
9950 /* Check and process for different type of WCQE and dispatch */
9951 switch (bf_get(lpfc_wcqe_c_code, &wcqe)) {
9952 case CQE_CODE_COMPL_WQE:
9953 /* Process the WQ complete event */
James Smart98fc5dd2010-06-07 15:24:29 -04009954 phba->last_completion_time = jiffies;
James Smart4f774512009-05-22 14:52:35 -04009955 lpfc_sli4_fp_handle_fcp_wcqe(phba,
9956 (struct lpfc_wcqe_complete *)&wcqe);
9957 break;
9958 case CQE_CODE_RELEASE_WQE:
9959 /* Process the WQ release event */
9960 lpfc_sli4_fp_handle_rel_wcqe(phba, cq,
9961 (struct lpfc_wcqe_release *)&wcqe);
9962 break;
9963 case CQE_CODE_XRI_ABORTED:
9964 /* Process the WQ XRI abort event */
James Smartbc739052010-08-04 16:11:18 -04009965 phba->last_completion_time = jiffies;
James Smart4f774512009-05-22 14:52:35 -04009966 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
9967 (struct sli4_wcqe_xri_aborted *)&wcqe);
9968 break;
9969 default:
9970 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9971 "0144 Not a valid WCQE code: x%x\n",
9972 bf_get(lpfc_wcqe_c_code, &wcqe));
9973 break;
9974 }
9975 return workposted;
9976}
9977
9978/**
9979 * lpfc_sli4_fp_handle_eqe - Process a fast-path event queue entry
9980 * @phba: Pointer to HBA context object.
9981 * @eqe: Pointer to fast-path event queue entry.
9982 *
9983 * This routine process a event queue entry from the fast-path event queue.
9984 * It will check the MajorCode and MinorCode to determine this is for a
9985 * completion event on a completion queue, if not, an error shall be logged
9986 * and just return. Otherwise, it will get to the corresponding completion
9987 * queue and process all the entries on the completion queue, rearm the
9988 * completion queue, and then return.
9989 **/
9990static void
9991lpfc_sli4_fp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
9992 uint32_t fcp_cqidx)
9993{
9994 struct lpfc_queue *cq;
9995 struct lpfc_cqe *cqe;
9996 bool workposted = false;
9997 uint16_t cqid;
9998 int ecount = 0;
9999
James Smartcb5172e2010-03-15 11:25:07 -040010000 if (unlikely(bf_get_le32(lpfc_eqe_major_code, eqe) != 0)) {
James Smart4f774512009-05-22 14:52:35 -040010001 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10002 "0366 Not a valid fast-path completion "
10003 "event: majorcode=x%x, minorcode=x%x\n",
James Smartcb5172e2010-03-15 11:25:07 -040010004 bf_get_le32(lpfc_eqe_major_code, eqe),
10005 bf_get_le32(lpfc_eqe_minor_code, eqe));
James Smart4f774512009-05-22 14:52:35 -040010006 return;
10007 }
10008
10009 cq = phba->sli4_hba.fcp_cq[fcp_cqidx];
10010 if (unlikely(!cq)) {
James Smart75baf692010-06-08 18:31:21 -040010011 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
10012 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10013 "0367 Fast-path completion queue "
10014 "does not exist\n");
James Smart4f774512009-05-22 14:52:35 -040010015 return;
10016 }
10017
10018 /* Get the reference to the corresponding CQ */
James Smartcb5172e2010-03-15 11:25:07 -040010019 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
James Smart4f774512009-05-22 14:52:35 -040010020 if (unlikely(cqid != cq->queue_id)) {
10021 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10022 "0368 Miss-matched fast-path completion "
10023 "queue identifier: eqcqid=%d, fcpcqid=%d\n",
10024 cqid, cq->queue_id);
10025 return;
10026 }
10027
10028 /* Process all the entries to the CQ */
10029 while ((cqe = lpfc_sli4_cq_get(cq))) {
10030 workposted |= lpfc_sli4_fp_handle_wcqe(phba, cq, cqe);
10031 if (!(++ecount % LPFC_GET_QE_REL_INT))
10032 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
10033 }
10034
10035 /* Catch the no cq entry condition */
10036 if (unlikely(ecount == 0))
10037 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10038 "0369 No entry from fast-path completion "
10039 "queue fcpcqid=%d\n", cq->queue_id);
10040
10041 /* In any case, flash and re-arm the CQ */
10042 lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
10043
10044 /* wake up worker thread if there are works to be done */
10045 if (workposted)
10046 lpfc_worker_wake_up(phba);
10047}
10048
10049static void
10050lpfc_sli4_eq_flush(struct lpfc_hba *phba, struct lpfc_queue *eq)
10051{
10052 struct lpfc_eqe *eqe;
10053
10054 /* walk all the EQ entries and drop on the floor */
10055 while ((eqe = lpfc_sli4_eq_get(eq)))
10056 ;
10057
10058 /* Clear and re-arm the EQ */
10059 lpfc_sli4_eq_release(eq, LPFC_QUEUE_REARM);
10060}
10061
10062/**
10063 * lpfc_sli4_sp_intr_handler - Slow-path interrupt handler to SLI-4 device
10064 * @irq: Interrupt number.
10065 * @dev_id: The device context pointer.
10066 *
10067 * This function is directly called from the PCI layer as an interrupt
10068 * service routine when device with SLI-4 interface spec is enabled with
10069 * MSI-X multi-message interrupt mode and there are slow-path events in
10070 * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
10071 * interrupt mode, this function is called as part of the device-level
10072 * interrupt handler. When the PCI slot is in error recovery or the HBA is
10073 * undergoing initialization, the interrupt handler will not process the
10074 * interrupt. The link attention and ELS ring attention events are handled
10075 * by the worker thread. The interrupt handler signals the worker thread
10076 * and returns for these events. This function is called without any lock
10077 * held. It gets the hbalock to access and update SLI data structures.
10078 *
10079 * This function returns IRQ_HANDLED when interrupt is handled else it
10080 * returns IRQ_NONE.
10081 **/
10082irqreturn_t
10083lpfc_sli4_sp_intr_handler(int irq, void *dev_id)
10084{
10085 struct lpfc_hba *phba;
10086 struct lpfc_queue *speq;
10087 struct lpfc_eqe *eqe;
10088 unsigned long iflag;
10089 int ecount = 0;
10090
10091 /*
10092 * Get the driver's phba structure from the dev_id
10093 */
10094 phba = (struct lpfc_hba *)dev_id;
10095
10096 if (unlikely(!phba))
10097 return IRQ_NONE;
10098
10099 /* Get to the EQ struct associated with this vector */
10100 speq = phba->sli4_hba.sp_eq;
10101
10102 /* Check device state for handling interrupt */
10103 if (unlikely(lpfc_intr_state_check(phba))) {
10104 /* Check again for link_state with lock held */
10105 spin_lock_irqsave(&phba->hbalock, iflag);
10106 if (phba->link_state < LPFC_LINK_DOWN)
10107 /* Flush, clear interrupt, and rearm the EQ */
10108 lpfc_sli4_eq_flush(phba, speq);
10109 spin_unlock_irqrestore(&phba->hbalock, iflag);
10110 return IRQ_NONE;
10111 }
10112
10113 /*
10114 * Process all the event on FCP slow-path EQ
10115 */
10116 while ((eqe = lpfc_sli4_eq_get(speq))) {
10117 lpfc_sli4_sp_handle_eqe(phba, eqe);
10118 if (!(++ecount % LPFC_GET_QE_REL_INT))
10119 lpfc_sli4_eq_release(speq, LPFC_QUEUE_NOARM);
10120 }
10121
10122 /* Always clear and re-arm the slow-path EQ */
10123 lpfc_sli4_eq_release(speq, LPFC_QUEUE_REARM);
10124
10125 /* Catch the no cq entry condition */
10126 if (unlikely(ecount == 0)) {
10127 if (phba->intr_type == MSIX)
10128 /* MSI-X treated interrupt served as no EQ share INT */
10129 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
10130 "0357 MSI-X interrupt with no EQE\n");
10131 else
10132 /* Non MSI-X treated on interrupt as EQ share INT */
10133 return IRQ_NONE;
10134 }
10135
10136 return IRQ_HANDLED;
10137} /* lpfc_sli4_sp_intr_handler */
10138
10139/**
10140 * lpfc_sli4_fp_intr_handler - Fast-path interrupt handler to SLI-4 device
10141 * @irq: Interrupt number.
10142 * @dev_id: The device context pointer.
10143 *
10144 * This function is directly called from the PCI layer as an interrupt
10145 * service routine when device with SLI-4 interface spec is enabled with
10146 * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
10147 * ring event in the HBA. However, when the device is enabled with either
10148 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
10149 * device-level interrupt handler. When the PCI slot is in error recovery
10150 * or the HBA is undergoing initialization, the interrupt handler will not
10151 * process the interrupt. The SCSI FCP fast-path ring event are handled in
10152 * the intrrupt context. This function is called without any lock held.
10153 * It gets the hbalock to access and update SLI data structures. Note that,
10154 * the FCP EQ to FCP CQ are one-to-one map such that the FCP EQ index is
10155 * equal to that of FCP CQ index.
10156 *
10157 * This function returns IRQ_HANDLED when interrupt is handled else it
10158 * returns IRQ_NONE.
10159 **/
10160irqreturn_t
10161lpfc_sli4_fp_intr_handler(int irq, void *dev_id)
10162{
10163 struct lpfc_hba *phba;
10164 struct lpfc_fcp_eq_hdl *fcp_eq_hdl;
10165 struct lpfc_queue *fpeq;
10166 struct lpfc_eqe *eqe;
10167 unsigned long iflag;
10168 int ecount = 0;
10169 uint32_t fcp_eqidx;
10170
10171 /* Get the driver's phba structure from the dev_id */
10172 fcp_eq_hdl = (struct lpfc_fcp_eq_hdl *)dev_id;
10173 phba = fcp_eq_hdl->phba;
10174 fcp_eqidx = fcp_eq_hdl->idx;
10175
10176 if (unlikely(!phba))
10177 return IRQ_NONE;
10178
10179 /* Get to the EQ struct associated with this vector */
10180 fpeq = phba->sli4_hba.fp_eq[fcp_eqidx];
10181
10182 /* Check device state for handling interrupt */
10183 if (unlikely(lpfc_intr_state_check(phba))) {
10184 /* Check again for link_state with lock held */
10185 spin_lock_irqsave(&phba->hbalock, iflag);
10186 if (phba->link_state < LPFC_LINK_DOWN)
10187 /* Flush, clear interrupt, and rearm the EQ */
10188 lpfc_sli4_eq_flush(phba, fpeq);
10189 spin_unlock_irqrestore(&phba->hbalock, iflag);
10190 return IRQ_NONE;
10191 }
10192
10193 /*
10194 * Process all the event on FCP fast-path EQ
10195 */
10196 while ((eqe = lpfc_sli4_eq_get(fpeq))) {
10197 lpfc_sli4_fp_handle_eqe(phba, eqe, fcp_eqidx);
10198 if (!(++ecount % LPFC_GET_QE_REL_INT))
10199 lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_NOARM);
10200 }
10201
10202 /* Always clear and re-arm the fast-path EQ */
10203 lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_REARM);
10204
10205 if (unlikely(ecount == 0)) {
10206 if (phba->intr_type == MSIX)
10207 /* MSI-X treated interrupt served as no EQ share INT */
10208 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
10209 "0358 MSI-X interrupt with no EQE\n");
10210 else
10211 /* Non MSI-X treated on interrupt as EQ share INT */
10212 return IRQ_NONE;
10213 }
10214
10215 return IRQ_HANDLED;
10216} /* lpfc_sli4_fp_intr_handler */
10217
10218/**
10219 * lpfc_sli4_intr_handler - Device-level interrupt handler for SLI-4 device
10220 * @irq: Interrupt number.
10221 * @dev_id: The device context pointer.
10222 *
10223 * This function is the device-level interrupt handler to device with SLI-4
10224 * interface spec, called from the PCI layer when either MSI or Pin-IRQ
10225 * interrupt mode is enabled and there is an event in the HBA which requires
10226 * driver attention. This function invokes the slow-path interrupt attention
10227 * handling function and fast-path interrupt attention handling function in
10228 * turn to process the relevant HBA attention events. This function is called
10229 * without any lock held. It gets the hbalock to access and update SLI data
10230 * structures.
10231 *
10232 * This function returns IRQ_HANDLED when interrupt is handled, else it
10233 * returns IRQ_NONE.
10234 **/
10235irqreturn_t
10236lpfc_sli4_intr_handler(int irq, void *dev_id)
10237{
10238 struct lpfc_hba *phba;
10239 irqreturn_t sp_irq_rc, fp_irq_rc;
10240 bool fp_handled = false;
10241 uint32_t fcp_eqidx;
10242
10243 /* Get the driver's phba structure from the dev_id */
10244 phba = (struct lpfc_hba *)dev_id;
10245
10246 if (unlikely(!phba))
10247 return IRQ_NONE;
10248
10249 /*
10250 * Invokes slow-path host attention interrupt handling as appropriate.
10251 */
10252 sp_irq_rc = lpfc_sli4_sp_intr_handler(irq, dev_id);
10253
10254 /*
10255 * Invoke fast-path host attention interrupt handling as appropriate.
10256 */
10257 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count; fcp_eqidx++) {
10258 fp_irq_rc = lpfc_sli4_fp_intr_handler(irq,
10259 &phba->sli4_hba.fcp_eq_hdl[fcp_eqidx]);
10260 if (fp_irq_rc == IRQ_HANDLED)
10261 fp_handled |= true;
10262 }
10263
10264 return (fp_handled == true) ? IRQ_HANDLED : sp_irq_rc;
10265} /* lpfc_sli4_intr_handler */
10266
10267/**
10268 * lpfc_sli4_queue_free - free a queue structure and associated memory
10269 * @queue: The queue structure to free.
10270 *
Uwe Kleine-Königb5950762010-11-01 15:38:34 -040010271 * This function frees a queue structure and the DMAable memory used for
James Smart4f774512009-05-22 14:52:35 -040010272 * the host resident queue. This function must be called after destroying the
10273 * queue on the HBA.
10274 **/
10275void
10276lpfc_sli4_queue_free(struct lpfc_queue *queue)
10277{
10278 struct lpfc_dmabuf *dmabuf;
10279
10280 if (!queue)
10281 return;
10282
10283 while (!list_empty(&queue->page_list)) {
10284 list_remove_head(&queue->page_list, dmabuf, struct lpfc_dmabuf,
10285 list);
James Smart49198b32010-04-06 15:04:33 -040010286 dma_free_coherent(&queue->phba->pcidev->dev, SLI4_PAGE_SIZE,
James Smart4f774512009-05-22 14:52:35 -040010287 dmabuf->virt, dmabuf->phys);
10288 kfree(dmabuf);
10289 }
10290 kfree(queue);
10291 return;
10292}
10293
10294/**
10295 * lpfc_sli4_queue_alloc - Allocate and initialize a queue structure
10296 * @phba: The HBA that this queue is being created on.
10297 * @entry_size: The size of each queue entry for this queue.
10298 * @entry count: The number of entries that this queue will handle.
10299 *
10300 * This function allocates a queue structure and the DMAable memory used for
10301 * the host resident queue. This function must be called before creating the
10302 * queue on the HBA.
10303 **/
10304struct lpfc_queue *
10305lpfc_sli4_queue_alloc(struct lpfc_hba *phba, uint32_t entry_size,
10306 uint32_t entry_count)
10307{
10308 struct lpfc_queue *queue;
10309 struct lpfc_dmabuf *dmabuf;
10310 int x, total_qe_count;
10311 void *dma_pointer;
James Smartcb5172e2010-03-15 11:25:07 -040010312 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smart4f774512009-05-22 14:52:35 -040010313
James Smartcb5172e2010-03-15 11:25:07 -040010314 if (!phba->sli4_hba.pc_sli4_params.supported)
10315 hw_page_size = SLI4_PAGE_SIZE;
10316
James Smart4f774512009-05-22 14:52:35 -040010317 queue = kzalloc(sizeof(struct lpfc_queue) +
10318 (sizeof(union sli4_qe) * entry_count), GFP_KERNEL);
10319 if (!queue)
10320 return NULL;
James Smartcb5172e2010-03-15 11:25:07 -040010321 queue->page_count = (ALIGN(entry_size * entry_count,
10322 hw_page_size))/hw_page_size;
James Smart4f774512009-05-22 14:52:35 -040010323 INIT_LIST_HEAD(&queue->list);
10324 INIT_LIST_HEAD(&queue->page_list);
10325 INIT_LIST_HEAD(&queue->child_list);
10326 for (x = 0, total_qe_count = 0; x < queue->page_count; x++) {
10327 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
10328 if (!dmabuf)
10329 goto out_fail;
10330 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
James Smartcb5172e2010-03-15 11:25:07 -040010331 hw_page_size, &dmabuf->phys,
James Smart4f774512009-05-22 14:52:35 -040010332 GFP_KERNEL);
10333 if (!dmabuf->virt) {
10334 kfree(dmabuf);
10335 goto out_fail;
10336 }
James Smartcb5172e2010-03-15 11:25:07 -040010337 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -040010338 dmabuf->buffer_tag = x;
10339 list_add_tail(&dmabuf->list, &queue->page_list);
10340 /* initialize queue's entry array */
10341 dma_pointer = dmabuf->virt;
10342 for (; total_qe_count < entry_count &&
James Smartcb5172e2010-03-15 11:25:07 -040010343 dma_pointer < (hw_page_size + dmabuf->virt);
James Smart4f774512009-05-22 14:52:35 -040010344 total_qe_count++, dma_pointer += entry_size) {
10345 queue->qe[total_qe_count].address = dma_pointer;
10346 }
10347 }
10348 queue->entry_size = entry_size;
10349 queue->entry_count = entry_count;
10350 queue->phba = phba;
10351
10352 return queue;
10353out_fail:
10354 lpfc_sli4_queue_free(queue);
10355 return NULL;
10356}
10357
10358/**
10359 * lpfc_eq_create - Create an Event Queue on the HBA
10360 * @phba: HBA structure that indicates port to create a queue on.
10361 * @eq: The queue structure to use to create the event queue.
10362 * @imax: The maximum interrupt per second limit.
10363 *
10364 * This function creates an event queue, as detailed in @eq, on a port,
10365 * described by @phba by sending an EQ_CREATE mailbox command to the HBA.
10366 *
10367 * The @phba struct is used to send mailbox command to HBA. The @eq struct
10368 * is used to get the entry count and entry size that are necessary to
10369 * determine the number of pages to allocate and use for this queue. This
10370 * function will send the EQ_CREATE mailbox command to the HBA to setup the
10371 * event queue. This function is asynchronous and will wait for the mailbox
10372 * command to finish before continuing.
10373 *
10374 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040010375 * memory this function will return -ENOMEM. If the queue create mailbox command
10376 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040010377 **/
10378uint32_t
10379lpfc_eq_create(struct lpfc_hba *phba, struct lpfc_queue *eq, uint16_t imax)
10380{
10381 struct lpfc_mbx_eq_create *eq_create;
10382 LPFC_MBOXQ_t *mbox;
10383 int rc, length, status = 0;
10384 struct lpfc_dmabuf *dmabuf;
10385 uint32_t shdr_status, shdr_add_status;
10386 union lpfc_sli4_cfg_shdr *shdr;
10387 uint16_t dmult;
James Smart49198b32010-04-06 15:04:33 -040010388 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
10389
10390 if (!phba->sli4_hba.pc_sli4_params.supported)
10391 hw_page_size = SLI4_PAGE_SIZE;
James Smart4f774512009-05-22 14:52:35 -040010392
10393 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
10394 if (!mbox)
10395 return -ENOMEM;
10396 length = (sizeof(struct lpfc_mbx_eq_create) -
10397 sizeof(struct lpfc_sli4_cfg_mhdr));
10398 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
10399 LPFC_MBOX_OPCODE_EQ_CREATE,
10400 length, LPFC_SLI4_MBX_EMBED);
10401 eq_create = &mbox->u.mqe.un.eq_create;
10402 bf_set(lpfc_mbx_eq_create_num_pages, &eq_create->u.request,
10403 eq->page_count);
10404 bf_set(lpfc_eq_context_size, &eq_create->u.request.context,
10405 LPFC_EQE_SIZE);
10406 bf_set(lpfc_eq_context_valid, &eq_create->u.request.context, 1);
10407 /* Calculate delay multiper from maximum interrupt per second */
10408 dmult = LPFC_DMULT_CONST/imax - 1;
10409 bf_set(lpfc_eq_context_delay_multi, &eq_create->u.request.context,
10410 dmult);
10411 switch (eq->entry_count) {
10412 default:
10413 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10414 "0360 Unsupported EQ count. (%d)\n",
10415 eq->entry_count);
10416 if (eq->entry_count < 256)
10417 return -EINVAL;
10418 /* otherwise default to smallest count (drop through) */
10419 case 256:
10420 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
10421 LPFC_EQ_CNT_256);
10422 break;
10423 case 512:
10424 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
10425 LPFC_EQ_CNT_512);
10426 break;
10427 case 1024:
10428 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
10429 LPFC_EQ_CNT_1024);
10430 break;
10431 case 2048:
10432 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
10433 LPFC_EQ_CNT_2048);
10434 break;
10435 case 4096:
10436 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
10437 LPFC_EQ_CNT_4096);
10438 break;
10439 }
10440 list_for_each_entry(dmabuf, &eq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040010441 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -040010442 eq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
10443 putPaddrLow(dmabuf->phys);
10444 eq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
10445 putPaddrHigh(dmabuf->phys);
10446 }
10447 mbox->vport = phba->pport;
10448 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
10449 mbox->context1 = NULL;
10450 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
10451 shdr = (union lpfc_sli4_cfg_shdr *) &eq_create->header.cfg_shdr;
10452 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10453 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10454 if (shdr_status || shdr_add_status || rc) {
10455 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10456 "2500 EQ_CREATE mailbox failed with "
10457 "status x%x add_status x%x, mbx status x%x\n",
10458 shdr_status, shdr_add_status, rc);
10459 status = -ENXIO;
10460 }
10461 eq->type = LPFC_EQ;
10462 eq->subtype = LPFC_NONE;
10463 eq->queue_id = bf_get(lpfc_mbx_eq_create_q_id, &eq_create->u.response);
10464 if (eq->queue_id == 0xFFFF)
10465 status = -ENXIO;
10466 eq->host_index = 0;
10467 eq->hba_index = 0;
10468
James Smart8fa38512009-07-19 10:01:03 -040010469 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040010470 return status;
10471}
10472
10473/**
10474 * lpfc_cq_create - Create a Completion Queue on the HBA
10475 * @phba: HBA structure that indicates port to create a queue on.
10476 * @cq: The queue structure to use to create the completion queue.
10477 * @eq: The event queue to bind this completion queue to.
10478 *
10479 * This function creates a completion queue, as detailed in @wq, on a port,
10480 * described by @phba by sending a CQ_CREATE mailbox command to the HBA.
10481 *
10482 * The @phba struct is used to send mailbox command to HBA. The @cq struct
10483 * is used to get the entry count and entry size that are necessary to
10484 * determine the number of pages to allocate and use for this queue. The @eq
10485 * is used to indicate which event queue to bind this completion queue to. This
10486 * function will send the CQ_CREATE mailbox command to the HBA to setup the
10487 * completion queue. This function is asynchronous and will wait for the mailbox
10488 * command to finish before continuing.
10489 *
10490 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040010491 * memory this function will return -ENOMEM. If the queue create mailbox command
10492 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040010493 **/
10494uint32_t
10495lpfc_cq_create(struct lpfc_hba *phba, struct lpfc_queue *cq,
10496 struct lpfc_queue *eq, uint32_t type, uint32_t subtype)
10497{
10498 struct lpfc_mbx_cq_create *cq_create;
10499 struct lpfc_dmabuf *dmabuf;
10500 LPFC_MBOXQ_t *mbox;
10501 int rc, length, status = 0;
10502 uint32_t shdr_status, shdr_add_status;
10503 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040010504 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
10505
10506 if (!phba->sli4_hba.pc_sli4_params.supported)
10507 hw_page_size = SLI4_PAGE_SIZE;
10508
James Smart4f774512009-05-22 14:52:35 -040010509 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
10510 if (!mbox)
10511 return -ENOMEM;
10512 length = (sizeof(struct lpfc_mbx_cq_create) -
10513 sizeof(struct lpfc_sli4_cfg_mhdr));
10514 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
10515 LPFC_MBOX_OPCODE_CQ_CREATE,
10516 length, LPFC_SLI4_MBX_EMBED);
10517 cq_create = &mbox->u.mqe.un.cq_create;
James Smart5a6f1332011-03-11 16:05:35 -050010518 shdr = (union lpfc_sli4_cfg_shdr *) &cq_create->header.cfg_shdr;
James Smart4f774512009-05-22 14:52:35 -040010519 bf_set(lpfc_mbx_cq_create_num_pages, &cq_create->u.request,
10520 cq->page_count);
10521 bf_set(lpfc_cq_context_event, &cq_create->u.request.context, 1);
10522 bf_set(lpfc_cq_context_valid, &cq_create->u.request.context, 1);
James Smart5a6f1332011-03-11 16:05:35 -050010523 bf_set(lpfc_mbox_hdr_version, &shdr->request,
10524 phba->sli4_hba.pc_sli4_params.cqv);
10525 if (phba->sli4_hba.pc_sli4_params.cqv == LPFC_Q_CREATE_VERSION_2) {
10526 bf_set(lpfc_mbx_cq_create_page_size, &cq_create->u.request,
10527 (PAGE_SIZE/SLI4_PAGE_SIZE));
10528 bf_set(lpfc_cq_eq_id_2, &cq_create->u.request.context,
10529 eq->queue_id);
10530 } else {
10531 bf_set(lpfc_cq_eq_id, &cq_create->u.request.context,
10532 eq->queue_id);
10533 }
James Smart4f774512009-05-22 14:52:35 -040010534 switch (cq->entry_count) {
10535 default:
10536 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10537 "0361 Unsupported CQ count. (%d)\n",
10538 cq->entry_count);
10539 if (cq->entry_count < 256)
10540 return -EINVAL;
10541 /* otherwise default to smallest count (drop through) */
10542 case 256:
10543 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
10544 LPFC_CQ_CNT_256);
10545 break;
10546 case 512:
10547 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
10548 LPFC_CQ_CNT_512);
10549 break;
10550 case 1024:
10551 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
10552 LPFC_CQ_CNT_1024);
10553 break;
10554 }
10555 list_for_each_entry(dmabuf, &cq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040010556 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -040010557 cq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
10558 putPaddrLow(dmabuf->phys);
10559 cq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
10560 putPaddrHigh(dmabuf->phys);
10561 }
10562 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
10563
10564 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart4f774512009-05-22 14:52:35 -040010565 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10566 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10567 if (shdr_status || shdr_add_status || rc) {
10568 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10569 "2501 CQ_CREATE mailbox failed with "
10570 "status x%x add_status x%x, mbx status x%x\n",
10571 shdr_status, shdr_add_status, rc);
10572 status = -ENXIO;
10573 goto out;
10574 }
10575 cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
10576 if (cq->queue_id == 0xFFFF) {
10577 status = -ENXIO;
10578 goto out;
10579 }
10580 /* link the cq onto the parent eq child list */
10581 list_add_tail(&cq->list, &eq->child_list);
10582 /* Set up completion queue's type and subtype */
10583 cq->type = type;
10584 cq->subtype = subtype;
10585 cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
James Smart2a622bf2011-02-16 12:40:06 -050010586 cq->assoc_qid = eq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040010587 cq->host_index = 0;
10588 cq->hba_index = 0;
James Smart4f774512009-05-22 14:52:35 -040010589
James Smart8fa38512009-07-19 10:01:03 -040010590out:
10591 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040010592 return status;
10593}
10594
10595/**
James Smartb19a0612010-04-06 14:48:51 -040010596 * lpfc_mq_create_fb_init - Send MCC_CREATE without async events registration
James Smart04c68492009-05-22 14:52:52 -040010597 * @phba: HBA structure that indicates port to create a queue on.
10598 * @mq: The queue structure to use to create the mailbox queue.
James Smartb19a0612010-04-06 14:48:51 -040010599 * @mbox: An allocated pointer to type LPFC_MBOXQ_t
10600 * @cq: The completion queue to associate with this cq.
James Smart04c68492009-05-22 14:52:52 -040010601 *
James Smartb19a0612010-04-06 14:48:51 -040010602 * This function provides failback (fb) functionality when the
10603 * mq_create_ext fails on older FW generations. It's purpose is identical
10604 * to mq_create_ext otherwise.
James Smart04c68492009-05-22 14:52:52 -040010605 *
James Smartb19a0612010-04-06 14:48:51 -040010606 * This routine cannot fail as all attributes were previously accessed and
10607 * initialized in mq_create_ext.
James Smart04c68492009-05-22 14:52:52 -040010608 **/
James Smartb19a0612010-04-06 14:48:51 -040010609static void
10610lpfc_mq_create_fb_init(struct lpfc_hba *phba, struct lpfc_queue *mq,
10611 LPFC_MBOXQ_t *mbox, struct lpfc_queue *cq)
James Smart04c68492009-05-22 14:52:52 -040010612{
10613 struct lpfc_mbx_mq_create *mq_create;
10614 struct lpfc_dmabuf *dmabuf;
James Smartb19a0612010-04-06 14:48:51 -040010615 int length;
James Smart04c68492009-05-22 14:52:52 -040010616
James Smart04c68492009-05-22 14:52:52 -040010617 length = (sizeof(struct lpfc_mbx_mq_create) -
10618 sizeof(struct lpfc_sli4_cfg_mhdr));
10619 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
10620 LPFC_MBOX_OPCODE_MQ_CREATE,
10621 length, LPFC_SLI4_MBX_EMBED);
10622 mq_create = &mbox->u.mqe.un.mq_create;
10623 bf_set(lpfc_mbx_mq_create_num_pages, &mq_create->u.request,
James Smartb19a0612010-04-06 14:48:51 -040010624 mq->page_count);
James Smart04c68492009-05-22 14:52:52 -040010625 bf_set(lpfc_mq_context_cq_id, &mq_create->u.request.context,
James Smartb19a0612010-04-06 14:48:51 -040010626 cq->queue_id);
James Smart04c68492009-05-22 14:52:52 -040010627 bf_set(lpfc_mq_context_valid, &mq_create->u.request.context, 1);
10628 switch (mq->entry_count) {
James Smart04c68492009-05-22 14:52:52 -040010629 case 16:
James Smart5a6f1332011-03-11 16:05:35 -050010630 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
10631 LPFC_MQ_RING_SIZE_16);
James Smart04c68492009-05-22 14:52:52 -040010632 break;
10633 case 32:
James Smart5a6f1332011-03-11 16:05:35 -050010634 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
10635 LPFC_MQ_RING_SIZE_32);
James Smart04c68492009-05-22 14:52:52 -040010636 break;
10637 case 64:
James Smart5a6f1332011-03-11 16:05:35 -050010638 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
10639 LPFC_MQ_RING_SIZE_64);
James Smart04c68492009-05-22 14:52:52 -040010640 break;
10641 case 128:
James Smart5a6f1332011-03-11 16:05:35 -050010642 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
10643 LPFC_MQ_RING_SIZE_128);
James Smart04c68492009-05-22 14:52:52 -040010644 break;
10645 }
10646 list_for_each_entry(dmabuf, &mq->page_list, list) {
10647 mq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
James Smartb19a0612010-04-06 14:48:51 -040010648 putPaddrLow(dmabuf->phys);
James Smart04c68492009-05-22 14:52:52 -040010649 mq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
James Smartb19a0612010-04-06 14:48:51 -040010650 putPaddrHigh(dmabuf->phys);
10651 }
10652}
10653
10654/**
10655 * lpfc_mq_create - Create a mailbox Queue on the HBA
10656 * @phba: HBA structure that indicates port to create a queue on.
10657 * @mq: The queue structure to use to create the mailbox queue.
10658 * @cq: The completion queue to associate with this cq.
10659 * @subtype: The queue's subtype.
10660 *
10661 * This function creates a mailbox queue, as detailed in @mq, on a port,
10662 * described by @phba by sending a MQ_CREATE mailbox command to the HBA.
10663 *
10664 * The @phba struct is used to send mailbox command to HBA. The @cq struct
10665 * is used to get the entry count and entry size that are necessary to
10666 * determine the number of pages to allocate and use for this queue. This
10667 * function will send the MQ_CREATE mailbox command to the HBA to setup the
10668 * mailbox queue. This function is asynchronous and will wait for the mailbox
10669 * command to finish before continuing.
10670 *
10671 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040010672 * memory this function will return -ENOMEM. If the queue create mailbox command
10673 * fails this function will return -ENXIO.
James Smartb19a0612010-04-06 14:48:51 -040010674 **/
10675int32_t
10676lpfc_mq_create(struct lpfc_hba *phba, struct lpfc_queue *mq,
10677 struct lpfc_queue *cq, uint32_t subtype)
10678{
10679 struct lpfc_mbx_mq_create *mq_create;
10680 struct lpfc_mbx_mq_create_ext *mq_create_ext;
10681 struct lpfc_dmabuf *dmabuf;
10682 LPFC_MBOXQ_t *mbox;
10683 int rc, length, status = 0;
10684 uint32_t shdr_status, shdr_add_status;
10685 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040010686 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smartb19a0612010-04-06 14:48:51 -040010687
James Smart49198b32010-04-06 15:04:33 -040010688 if (!phba->sli4_hba.pc_sli4_params.supported)
10689 hw_page_size = SLI4_PAGE_SIZE;
James Smartb19a0612010-04-06 14:48:51 -040010690
10691 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
10692 if (!mbox)
10693 return -ENOMEM;
10694 length = (sizeof(struct lpfc_mbx_mq_create_ext) -
10695 sizeof(struct lpfc_sli4_cfg_mhdr));
10696 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
10697 LPFC_MBOX_OPCODE_MQ_CREATE_EXT,
10698 length, LPFC_SLI4_MBX_EMBED);
10699
10700 mq_create_ext = &mbox->u.mqe.un.mq_create_ext;
James Smart5a6f1332011-03-11 16:05:35 -050010701 shdr = (union lpfc_sli4_cfg_shdr *) &mq_create_ext->header.cfg_shdr;
James Smart70f3c072010-12-15 17:57:33 -050010702 bf_set(lpfc_mbx_mq_create_ext_num_pages,
10703 &mq_create_ext->u.request, mq->page_count);
10704 bf_set(lpfc_mbx_mq_create_ext_async_evt_link,
10705 &mq_create_ext->u.request, 1);
10706 bf_set(lpfc_mbx_mq_create_ext_async_evt_fip,
James Smartb19a0612010-04-06 14:48:51 -040010707 &mq_create_ext->u.request, 1);
10708 bf_set(lpfc_mbx_mq_create_ext_async_evt_group5,
10709 &mq_create_ext->u.request, 1);
James Smart70f3c072010-12-15 17:57:33 -050010710 bf_set(lpfc_mbx_mq_create_ext_async_evt_fc,
10711 &mq_create_ext->u.request, 1);
10712 bf_set(lpfc_mbx_mq_create_ext_async_evt_sli,
10713 &mq_create_ext->u.request, 1);
James Smartb19a0612010-04-06 14:48:51 -040010714 bf_set(lpfc_mq_context_valid, &mq_create_ext->u.request.context, 1);
James Smart5a6f1332011-03-11 16:05:35 -050010715 bf_set(lpfc_mbox_hdr_version, &shdr->request,
10716 phba->sli4_hba.pc_sli4_params.mqv);
10717 if (phba->sli4_hba.pc_sli4_params.mqv == LPFC_Q_CREATE_VERSION_1)
10718 bf_set(lpfc_mbx_mq_create_ext_cq_id, &mq_create_ext->u.request,
10719 cq->queue_id);
10720 else
10721 bf_set(lpfc_mq_context_cq_id, &mq_create_ext->u.request.context,
10722 cq->queue_id);
James Smartb19a0612010-04-06 14:48:51 -040010723 switch (mq->entry_count) {
10724 default:
10725 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10726 "0362 Unsupported MQ count. (%d)\n",
10727 mq->entry_count);
10728 if (mq->entry_count < 16)
10729 return -EINVAL;
10730 /* otherwise default to smallest count (drop through) */
10731 case 16:
James Smart5a6f1332011-03-11 16:05:35 -050010732 bf_set(lpfc_mq_context_ring_size,
10733 &mq_create_ext->u.request.context,
10734 LPFC_MQ_RING_SIZE_16);
James Smartb19a0612010-04-06 14:48:51 -040010735 break;
10736 case 32:
James Smart5a6f1332011-03-11 16:05:35 -050010737 bf_set(lpfc_mq_context_ring_size,
10738 &mq_create_ext->u.request.context,
10739 LPFC_MQ_RING_SIZE_32);
James Smartb19a0612010-04-06 14:48:51 -040010740 break;
10741 case 64:
James Smart5a6f1332011-03-11 16:05:35 -050010742 bf_set(lpfc_mq_context_ring_size,
10743 &mq_create_ext->u.request.context,
10744 LPFC_MQ_RING_SIZE_64);
James Smartb19a0612010-04-06 14:48:51 -040010745 break;
10746 case 128:
James Smart5a6f1332011-03-11 16:05:35 -050010747 bf_set(lpfc_mq_context_ring_size,
10748 &mq_create_ext->u.request.context,
10749 LPFC_MQ_RING_SIZE_128);
James Smartb19a0612010-04-06 14:48:51 -040010750 break;
10751 }
10752 list_for_each_entry(dmabuf, &mq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040010753 memset(dmabuf->virt, 0, hw_page_size);
James Smartb19a0612010-04-06 14:48:51 -040010754 mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_lo =
10755 putPaddrLow(dmabuf->phys);
10756 mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_hi =
James Smart04c68492009-05-22 14:52:52 -040010757 putPaddrHigh(dmabuf->phys);
10758 }
10759 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
James Smartb19a0612010-04-06 14:48:51 -040010760 mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
10761 &mq_create_ext->u.response);
10762 if (rc != MBX_SUCCESS) {
10763 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
10764 "2795 MQ_CREATE_EXT failed with "
10765 "status x%x. Failback to MQ_CREATE.\n",
10766 rc);
10767 lpfc_mq_create_fb_init(phba, mq, mbox, cq);
10768 mq_create = &mbox->u.mqe.un.mq_create;
10769 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
10770 shdr = (union lpfc_sli4_cfg_shdr *) &mq_create->header.cfg_shdr;
10771 mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
10772 &mq_create->u.response);
10773 }
10774
James Smart04c68492009-05-22 14:52:52 -040010775 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart04c68492009-05-22 14:52:52 -040010776 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10777 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10778 if (shdr_status || shdr_add_status || rc) {
10779 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10780 "2502 MQ_CREATE mailbox failed with "
10781 "status x%x add_status x%x, mbx status x%x\n",
10782 shdr_status, shdr_add_status, rc);
10783 status = -ENXIO;
10784 goto out;
10785 }
James Smart04c68492009-05-22 14:52:52 -040010786 if (mq->queue_id == 0xFFFF) {
10787 status = -ENXIO;
10788 goto out;
10789 }
10790 mq->type = LPFC_MQ;
James Smart2a622bf2011-02-16 12:40:06 -050010791 mq->assoc_qid = cq->queue_id;
James Smart04c68492009-05-22 14:52:52 -040010792 mq->subtype = subtype;
10793 mq->host_index = 0;
10794 mq->hba_index = 0;
10795
10796 /* link the mq onto the parent cq child list */
10797 list_add_tail(&mq->list, &cq->child_list);
10798out:
James Smart8fa38512009-07-19 10:01:03 -040010799 mempool_free(mbox, phba->mbox_mem_pool);
James Smart04c68492009-05-22 14:52:52 -040010800 return status;
10801}
10802
10803/**
James Smart4f774512009-05-22 14:52:35 -040010804 * lpfc_wq_create - Create a Work Queue on the HBA
10805 * @phba: HBA structure that indicates port to create a queue on.
10806 * @wq: The queue structure to use to create the work queue.
10807 * @cq: The completion queue to bind this work queue to.
10808 * @subtype: The subtype of the work queue indicating its functionality.
10809 *
10810 * This function creates a work queue, as detailed in @wq, on a port, described
10811 * by @phba by sending a WQ_CREATE mailbox command to the HBA.
10812 *
10813 * The @phba struct is used to send mailbox command to HBA. The @wq struct
10814 * is used to get the entry count and entry size that are necessary to
10815 * determine the number of pages to allocate and use for this queue. The @cq
10816 * is used to indicate which completion queue to bind this work queue to. This
10817 * function will send the WQ_CREATE mailbox command to the HBA to setup the
10818 * work queue. This function is asynchronous and will wait for the mailbox
10819 * command to finish before continuing.
10820 *
10821 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040010822 * memory this function will return -ENOMEM. If the queue create mailbox command
10823 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040010824 **/
10825uint32_t
10826lpfc_wq_create(struct lpfc_hba *phba, struct lpfc_queue *wq,
10827 struct lpfc_queue *cq, uint32_t subtype)
10828{
10829 struct lpfc_mbx_wq_create *wq_create;
10830 struct lpfc_dmabuf *dmabuf;
10831 LPFC_MBOXQ_t *mbox;
10832 int rc, length, status = 0;
10833 uint32_t shdr_status, shdr_add_status;
10834 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040010835 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smart5a6f1332011-03-11 16:05:35 -050010836 struct dma_address *page;
James Smart49198b32010-04-06 15:04:33 -040010837
10838 if (!phba->sli4_hba.pc_sli4_params.supported)
10839 hw_page_size = SLI4_PAGE_SIZE;
James Smart4f774512009-05-22 14:52:35 -040010840
10841 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
10842 if (!mbox)
10843 return -ENOMEM;
10844 length = (sizeof(struct lpfc_mbx_wq_create) -
10845 sizeof(struct lpfc_sli4_cfg_mhdr));
10846 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10847 LPFC_MBOX_OPCODE_FCOE_WQ_CREATE,
10848 length, LPFC_SLI4_MBX_EMBED);
10849 wq_create = &mbox->u.mqe.un.wq_create;
James Smart5a6f1332011-03-11 16:05:35 -050010850 shdr = (union lpfc_sli4_cfg_shdr *) &wq_create->header.cfg_shdr;
James Smart4f774512009-05-22 14:52:35 -040010851 bf_set(lpfc_mbx_wq_create_num_pages, &wq_create->u.request,
10852 wq->page_count);
10853 bf_set(lpfc_mbx_wq_create_cq_id, &wq_create->u.request,
10854 cq->queue_id);
James Smart5a6f1332011-03-11 16:05:35 -050010855 bf_set(lpfc_mbox_hdr_version, &shdr->request,
10856 phba->sli4_hba.pc_sli4_params.wqv);
10857 if (phba->sli4_hba.pc_sli4_params.wqv == LPFC_Q_CREATE_VERSION_1) {
10858 bf_set(lpfc_mbx_wq_create_wqe_count, &wq_create->u.request_1,
10859 wq->entry_count);
10860 switch (wq->entry_size) {
10861 default:
10862 case 64:
10863 bf_set(lpfc_mbx_wq_create_wqe_size,
10864 &wq_create->u.request_1,
10865 LPFC_WQ_WQE_SIZE_64);
10866 break;
10867 case 128:
10868 bf_set(lpfc_mbx_wq_create_wqe_size,
10869 &wq_create->u.request_1,
10870 LPFC_WQ_WQE_SIZE_128);
10871 break;
10872 }
10873 bf_set(lpfc_mbx_wq_create_page_size, &wq_create->u.request_1,
10874 (PAGE_SIZE/SLI4_PAGE_SIZE));
10875 page = wq_create->u.request_1.page;
10876 } else {
10877 page = wq_create->u.request.page;
10878 }
James Smart4f774512009-05-22 14:52:35 -040010879 list_for_each_entry(dmabuf, &wq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040010880 memset(dmabuf->virt, 0, hw_page_size);
James Smart5a6f1332011-03-11 16:05:35 -050010881 page[dmabuf->buffer_tag].addr_lo = putPaddrLow(dmabuf->phys);
10882 page[dmabuf->buffer_tag].addr_hi = putPaddrHigh(dmabuf->phys);
James Smart4f774512009-05-22 14:52:35 -040010883 }
10884 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
10885 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart4f774512009-05-22 14:52:35 -040010886 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10887 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10888 if (shdr_status || shdr_add_status || rc) {
10889 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10890 "2503 WQ_CREATE mailbox failed with "
10891 "status x%x add_status x%x, mbx status x%x\n",
10892 shdr_status, shdr_add_status, rc);
10893 status = -ENXIO;
10894 goto out;
10895 }
10896 wq->queue_id = bf_get(lpfc_mbx_wq_create_q_id, &wq_create->u.response);
10897 if (wq->queue_id == 0xFFFF) {
10898 status = -ENXIO;
10899 goto out;
10900 }
10901 wq->type = LPFC_WQ;
James Smart2a622bf2011-02-16 12:40:06 -050010902 wq->assoc_qid = cq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040010903 wq->subtype = subtype;
10904 wq->host_index = 0;
10905 wq->hba_index = 0;
10906
10907 /* link the wq onto the parent cq child list */
10908 list_add_tail(&wq->list, &cq->child_list);
10909out:
James Smart8fa38512009-07-19 10:01:03 -040010910 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040010911 return status;
10912}
10913
10914/**
10915 * lpfc_rq_create - Create a Receive Queue on the HBA
10916 * @phba: HBA structure that indicates port to create a queue on.
10917 * @hrq: The queue structure to use to create the header receive queue.
10918 * @drq: The queue structure to use to create the data receive queue.
10919 * @cq: The completion queue to bind this work queue to.
10920 *
10921 * This function creates a receive buffer queue pair , as detailed in @hrq and
10922 * @drq, on a port, described by @phba by sending a RQ_CREATE mailbox command
10923 * to the HBA.
10924 *
10925 * The @phba struct is used to send mailbox command to HBA. The @drq and @hrq
10926 * struct is used to get the entry count that is necessary to determine the
10927 * number of pages to use for this queue. The @cq is used to indicate which
10928 * completion queue to bind received buffers that are posted to these queues to.
10929 * This function will send the RQ_CREATE mailbox command to the HBA to setup the
10930 * receive queue pair. This function is asynchronous and will wait for the
10931 * mailbox command to finish before continuing.
10932 *
10933 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040010934 * memory this function will return -ENOMEM. If the queue create mailbox command
10935 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040010936 **/
10937uint32_t
10938lpfc_rq_create(struct lpfc_hba *phba, struct lpfc_queue *hrq,
10939 struct lpfc_queue *drq, struct lpfc_queue *cq, uint32_t subtype)
10940{
10941 struct lpfc_mbx_rq_create *rq_create;
10942 struct lpfc_dmabuf *dmabuf;
10943 LPFC_MBOXQ_t *mbox;
10944 int rc, length, status = 0;
10945 uint32_t shdr_status, shdr_add_status;
10946 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040010947 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
10948
10949 if (!phba->sli4_hba.pc_sli4_params.supported)
10950 hw_page_size = SLI4_PAGE_SIZE;
James Smart4f774512009-05-22 14:52:35 -040010951
10952 if (hrq->entry_count != drq->entry_count)
10953 return -EINVAL;
10954 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
10955 if (!mbox)
10956 return -ENOMEM;
10957 length = (sizeof(struct lpfc_mbx_rq_create) -
10958 sizeof(struct lpfc_sli4_cfg_mhdr));
10959 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10960 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
10961 length, LPFC_SLI4_MBX_EMBED);
10962 rq_create = &mbox->u.mqe.un.rq_create;
James Smart5a6f1332011-03-11 16:05:35 -050010963 shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
10964 bf_set(lpfc_mbox_hdr_version, &shdr->request,
10965 phba->sli4_hba.pc_sli4_params.rqv);
10966 if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
10967 bf_set(lpfc_rq_context_rqe_count_1,
10968 &rq_create->u.request.context,
10969 hrq->entry_count);
10970 rq_create->u.request.context.buffer_size = LPFC_HDR_BUF_SIZE;
10971 } else {
10972 switch (hrq->entry_count) {
10973 default:
10974 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10975 "2535 Unsupported RQ count. (%d)\n",
10976 hrq->entry_count);
10977 if (hrq->entry_count < 512)
10978 return -EINVAL;
10979 /* otherwise default to smallest count (drop through) */
10980 case 512:
10981 bf_set(lpfc_rq_context_rqe_count,
10982 &rq_create->u.request.context,
10983 LPFC_RQ_RING_SIZE_512);
10984 break;
10985 case 1024:
10986 bf_set(lpfc_rq_context_rqe_count,
10987 &rq_create->u.request.context,
10988 LPFC_RQ_RING_SIZE_1024);
10989 break;
10990 case 2048:
10991 bf_set(lpfc_rq_context_rqe_count,
10992 &rq_create->u.request.context,
10993 LPFC_RQ_RING_SIZE_2048);
10994 break;
10995 case 4096:
10996 bf_set(lpfc_rq_context_rqe_count,
10997 &rq_create->u.request.context,
10998 LPFC_RQ_RING_SIZE_4096);
10999 break;
11000 }
11001 bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
11002 LPFC_HDR_BUF_SIZE);
James Smart4f774512009-05-22 14:52:35 -040011003 }
11004 bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
11005 cq->queue_id);
11006 bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
11007 hrq->page_count);
James Smart4f774512009-05-22 14:52:35 -040011008 list_for_each_entry(dmabuf, &hrq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040011009 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -040011010 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
11011 putPaddrLow(dmabuf->phys);
11012 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
11013 putPaddrHigh(dmabuf->phys);
11014 }
11015 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
11016 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart4f774512009-05-22 14:52:35 -040011017 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
11018 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
11019 if (shdr_status || shdr_add_status || rc) {
11020 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11021 "2504 RQ_CREATE mailbox failed with "
11022 "status x%x add_status x%x, mbx status x%x\n",
11023 shdr_status, shdr_add_status, rc);
11024 status = -ENXIO;
11025 goto out;
11026 }
11027 hrq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
11028 if (hrq->queue_id == 0xFFFF) {
11029 status = -ENXIO;
11030 goto out;
11031 }
11032 hrq->type = LPFC_HRQ;
James Smart2a622bf2011-02-16 12:40:06 -050011033 hrq->assoc_qid = cq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040011034 hrq->subtype = subtype;
11035 hrq->host_index = 0;
11036 hrq->hba_index = 0;
11037
11038 /* now create the data queue */
11039 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
11040 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
11041 length, LPFC_SLI4_MBX_EMBED);
James Smart5a6f1332011-03-11 16:05:35 -050011042 bf_set(lpfc_mbox_hdr_version, &shdr->request,
11043 phba->sli4_hba.pc_sli4_params.rqv);
11044 if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
11045 bf_set(lpfc_rq_context_rqe_count_1,
11046 &rq_create->u.request.context,
11047 hrq->entry_count);
11048 rq_create->u.request.context.buffer_size = LPFC_DATA_BUF_SIZE;
11049 } else {
11050 switch (drq->entry_count) {
11051 default:
11052 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11053 "2536 Unsupported RQ count. (%d)\n",
11054 drq->entry_count);
11055 if (drq->entry_count < 512)
11056 return -EINVAL;
11057 /* otherwise default to smallest count (drop through) */
11058 case 512:
11059 bf_set(lpfc_rq_context_rqe_count,
11060 &rq_create->u.request.context,
11061 LPFC_RQ_RING_SIZE_512);
11062 break;
11063 case 1024:
11064 bf_set(lpfc_rq_context_rqe_count,
11065 &rq_create->u.request.context,
11066 LPFC_RQ_RING_SIZE_1024);
11067 break;
11068 case 2048:
11069 bf_set(lpfc_rq_context_rqe_count,
11070 &rq_create->u.request.context,
11071 LPFC_RQ_RING_SIZE_2048);
11072 break;
11073 case 4096:
11074 bf_set(lpfc_rq_context_rqe_count,
11075 &rq_create->u.request.context,
11076 LPFC_RQ_RING_SIZE_4096);
11077 break;
11078 }
11079 bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
11080 LPFC_DATA_BUF_SIZE);
James Smart4f774512009-05-22 14:52:35 -040011081 }
11082 bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
11083 cq->queue_id);
11084 bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
11085 drq->page_count);
James Smart4f774512009-05-22 14:52:35 -040011086 list_for_each_entry(dmabuf, &drq->page_list, list) {
11087 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
11088 putPaddrLow(dmabuf->phys);
11089 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
11090 putPaddrHigh(dmabuf->phys);
11091 }
11092 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
11093 /* The IOCTL status is embedded in the mailbox subheader. */
11094 shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
11095 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
11096 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
11097 if (shdr_status || shdr_add_status || rc) {
11098 status = -ENXIO;
11099 goto out;
11100 }
11101 drq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
11102 if (drq->queue_id == 0xFFFF) {
11103 status = -ENXIO;
11104 goto out;
11105 }
11106 drq->type = LPFC_DRQ;
James Smart2a622bf2011-02-16 12:40:06 -050011107 drq->assoc_qid = cq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040011108 drq->subtype = subtype;
11109 drq->host_index = 0;
11110 drq->hba_index = 0;
11111
11112 /* link the header and data RQs onto the parent cq child list */
11113 list_add_tail(&hrq->list, &cq->child_list);
11114 list_add_tail(&drq->list, &cq->child_list);
11115
11116out:
James Smart8fa38512009-07-19 10:01:03 -040011117 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040011118 return status;
11119}
11120
11121/**
11122 * lpfc_eq_destroy - Destroy an event Queue on the HBA
11123 * @eq: The queue structure associated with the queue to destroy.
11124 *
11125 * This function destroys a queue, as detailed in @eq by sending an mailbox
11126 * command, specific to the type of queue, to the HBA.
11127 *
11128 * The @eq struct is used to get the queue ID of the queue to destroy.
11129 *
11130 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040011131 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040011132 **/
11133uint32_t
11134lpfc_eq_destroy(struct lpfc_hba *phba, struct lpfc_queue *eq)
11135{
11136 LPFC_MBOXQ_t *mbox;
11137 int rc, length, status = 0;
11138 uint32_t shdr_status, shdr_add_status;
11139 union lpfc_sli4_cfg_shdr *shdr;
11140
11141 if (!eq)
11142 return -ENODEV;
11143 mbox = mempool_alloc(eq->phba->mbox_mem_pool, GFP_KERNEL);
11144 if (!mbox)
11145 return -ENOMEM;
11146 length = (sizeof(struct lpfc_mbx_eq_destroy) -
11147 sizeof(struct lpfc_sli4_cfg_mhdr));
11148 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
11149 LPFC_MBOX_OPCODE_EQ_DESTROY,
11150 length, LPFC_SLI4_MBX_EMBED);
11151 bf_set(lpfc_mbx_eq_destroy_q_id, &mbox->u.mqe.un.eq_destroy.u.request,
11152 eq->queue_id);
11153 mbox->vport = eq->phba->pport;
11154 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
11155
11156 rc = lpfc_sli_issue_mbox(eq->phba, mbox, MBX_POLL);
11157 /* The IOCTL status is embedded in the mailbox subheader. */
11158 shdr = (union lpfc_sli4_cfg_shdr *)
11159 &mbox->u.mqe.un.eq_destroy.header.cfg_shdr;
11160 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
11161 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
11162 if (shdr_status || shdr_add_status || rc) {
11163 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11164 "2505 EQ_DESTROY mailbox failed with "
11165 "status x%x add_status x%x, mbx status x%x\n",
11166 shdr_status, shdr_add_status, rc);
11167 status = -ENXIO;
11168 }
11169
11170 /* Remove eq from any list */
11171 list_del_init(&eq->list);
James Smart8fa38512009-07-19 10:01:03 -040011172 mempool_free(mbox, eq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040011173 return status;
11174}
11175
11176/**
11177 * lpfc_cq_destroy - Destroy a Completion Queue on the HBA
11178 * @cq: The queue structure associated with the queue to destroy.
11179 *
11180 * This function destroys a queue, as detailed in @cq by sending an mailbox
11181 * command, specific to the type of queue, to the HBA.
11182 *
11183 * The @cq struct is used to get the queue ID of the queue to destroy.
11184 *
11185 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040011186 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040011187 **/
11188uint32_t
11189lpfc_cq_destroy(struct lpfc_hba *phba, struct lpfc_queue *cq)
11190{
11191 LPFC_MBOXQ_t *mbox;
11192 int rc, length, status = 0;
11193 uint32_t shdr_status, shdr_add_status;
11194 union lpfc_sli4_cfg_shdr *shdr;
11195
11196 if (!cq)
11197 return -ENODEV;
11198 mbox = mempool_alloc(cq->phba->mbox_mem_pool, GFP_KERNEL);
11199 if (!mbox)
11200 return -ENOMEM;
11201 length = (sizeof(struct lpfc_mbx_cq_destroy) -
11202 sizeof(struct lpfc_sli4_cfg_mhdr));
11203 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
11204 LPFC_MBOX_OPCODE_CQ_DESTROY,
11205 length, LPFC_SLI4_MBX_EMBED);
11206 bf_set(lpfc_mbx_cq_destroy_q_id, &mbox->u.mqe.un.cq_destroy.u.request,
11207 cq->queue_id);
11208 mbox->vport = cq->phba->pport;
11209 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
11210 rc = lpfc_sli_issue_mbox(cq->phba, mbox, MBX_POLL);
11211 /* The IOCTL status is embedded in the mailbox subheader. */
11212 shdr = (union lpfc_sli4_cfg_shdr *)
11213 &mbox->u.mqe.un.wq_create.header.cfg_shdr;
11214 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
11215 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
11216 if (shdr_status || shdr_add_status || rc) {
11217 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11218 "2506 CQ_DESTROY mailbox failed with "
11219 "status x%x add_status x%x, mbx status x%x\n",
11220 shdr_status, shdr_add_status, rc);
11221 status = -ENXIO;
11222 }
11223 /* Remove cq from any list */
11224 list_del_init(&cq->list);
James Smart8fa38512009-07-19 10:01:03 -040011225 mempool_free(mbox, cq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040011226 return status;
11227}
11228
11229/**
James Smart04c68492009-05-22 14:52:52 -040011230 * lpfc_mq_destroy - Destroy a Mailbox Queue on the HBA
11231 * @qm: The queue structure associated with the queue to destroy.
11232 *
11233 * This function destroys a queue, as detailed in @mq by sending an mailbox
11234 * command, specific to the type of queue, to the HBA.
11235 *
11236 * The @mq struct is used to get the queue ID of the queue to destroy.
11237 *
11238 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040011239 * command fails this function will return -ENXIO.
James Smart04c68492009-05-22 14:52:52 -040011240 **/
11241uint32_t
11242lpfc_mq_destroy(struct lpfc_hba *phba, struct lpfc_queue *mq)
11243{
11244 LPFC_MBOXQ_t *mbox;
11245 int rc, length, status = 0;
11246 uint32_t shdr_status, shdr_add_status;
11247 union lpfc_sli4_cfg_shdr *shdr;
11248
11249 if (!mq)
11250 return -ENODEV;
11251 mbox = mempool_alloc(mq->phba->mbox_mem_pool, GFP_KERNEL);
11252 if (!mbox)
11253 return -ENOMEM;
11254 length = (sizeof(struct lpfc_mbx_mq_destroy) -
11255 sizeof(struct lpfc_sli4_cfg_mhdr));
11256 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
11257 LPFC_MBOX_OPCODE_MQ_DESTROY,
11258 length, LPFC_SLI4_MBX_EMBED);
11259 bf_set(lpfc_mbx_mq_destroy_q_id, &mbox->u.mqe.un.mq_destroy.u.request,
11260 mq->queue_id);
11261 mbox->vport = mq->phba->pport;
11262 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
11263 rc = lpfc_sli_issue_mbox(mq->phba, mbox, MBX_POLL);
11264 /* The IOCTL status is embedded in the mailbox subheader. */
11265 shdr = (union lpfc_sli4_cfg_shdr *)
11266 &mbox->u.mqe.un.mq_destroy.header.cfg_shdr;
11267 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
11268 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
11269 if (shdr_status || shdr_add_status || rc) {
11270 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11271 "2507 MQ_DESTROY mailbox failed with "
11272 "status x%x add_status x%x, mbx status x%x\n",
11273 shdr_status, shdr_add_status, rc);
11274 status = -ENXIO;
11275 }
11276 /* Remove mq from any list */
11277 list_del_init(&mq->list);
James Smart8fa38512009-07-19 10:01:03 -040011278 mempool_free(mbox, mq->phba->mbox_mem_pool);
James Smart04c68492009-05-22 14:52:52 -040011279 return status;
11280}
11281
11282/**
James Smart4f774512009-05-22 14:52:35 -040011283 * lpfc_wq_destroy - Destroy a Work Queue on the HBA
11284 * @wq: The queue structure associated with the queue to destroy.
11285 *
11286 * This function destroys a queue, as detailed in @wq by sending an mailbox
11287 * command, specific to the type of queue, to the HBA.
11288 *
11289 * The @wq struct is used to get the queue ID of the queue to destroy.
11290 *
11291 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040011292 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040011293 **/
11294uint32_t
11295lpfc_wq_destroy(struct lpfc_hba *phba, struct lpfc_queue *wq)
11296{
11297 LPFC_MBOXQ_t *mbox;
11298 int rc, length, status = 0;
11299 uint32_t shdr_status, shdr_add_status;
11300 union lpfc_sli4_cfg_shdr *shdr;
11301
11302 if (!wq)
11303 return -ENODEV;
11304 mbox = mempool_alloc(wq->phba->mbox_mem_pool, GFP_KERNEL);
11305 if (!mbox)
11306 return -ENOMEM;
11307 length = (sizeof(struct lpfc_mbx_wq_destroy) -
11308 sizeof(struct lpfc_sli4_cfg_mhdr));
11309 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
11310 LPFC_MBOX_OPCODE_FCOE_WQ_DESTROY,
11311 length, LPFC_SLI4_MBX_EMBED);
11312 bf_set(lpfc_mbx_wq_destroy_q_id, &mbox->u.mqe.un.wq_destroy.u.request,
11313 wq->queue_id);
11314 mbox->vport = wq->phba->pport;
11315 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
11316 rc = lpfc_sli_issue_mbox(wq->phba, mbox, MBX_POLL);
11317 shdr = (union lpfc_sli4_cfg_shdr *)
11318 &mbox->u.mqe.un.wq_destroy.header.cfg_shdr;
11319 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
11320 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
11321 if (shdr_status || shdr_add_status || rc) {
11322 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11323 "2508 WQ_DESTROY mailbox failed with "
11324 "status x%x add_status x%x, mbx status x%x\n",
11325 shdr_status, shdr_add_status, rc);
11326 status = -ENXIO;
11327 }
11328 /* Remove wq from any list */
11329 list_del_init(&wq->list);
James Smart8fa38512009-07-19 10:01:03 -040011330 mempool_free(mbox, wq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040011331 return status;
11332}
11333
11334/**
11335 * lpfc_rq_destroy - Destroy a Receive Queue on the HBA
11336 * @rq: The queue structure associated with the queue to destroy.
11337 *
11338 * This function destroys a queue, as detailed in @rq by sending an mailbox
11339 * command, specific to the type of queue, to the HBA.
11340 *
11341 * The @rq struct is used to get the queue ID of the queue to destroy.
11342 *
11343 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040011344 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040011345 **/
11346uint32_t
11347lpfc_rq_destroy(struct lpfc_hba *phba, struct lpfc_queue *hrq,
11348 struct lpfc_queue *drq)
11349{
11350 LPFC_MBOXQ_t *mbox;
11351 int rc, length, status = 0;
11352 uint32_t shdr_status, shdr_add_status;
11353 union lpfc_sli4_cfg_shdr *shdr;
11354
11355 if (!hrq || !drq)
11356 return -ENODEV;
11357 mbox = mempool_alloc(hrq->phba->mbox_mem_pool, GFP_KERNEL);
11358 if (!mbox)
11359 return -ENOMEM;
11360 length = (sizeof(struct lpfc_mbx_rq_destroy) -
James Smartfedd3b72011-02-16 12:39:24 -050011361 sizeof(struct lpfc_sli4_cfg_mhdr));
James Smart4f774512009-05-22 14:52:35 -040011362 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
11363 LPFC_MBOX_OPCODE_FCOE_RQ_DESTROY,
11364 length, LPFC_SLI4_MBX_EMBED);
11365 bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
11366 hrq->queue_id);
11367 mbox->vport = hrq->phba->pport;
11368 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
11369 rc = lpfc_sli_issue_mbox(hrq->phba, mbox, MBX_POLL);
11370 /* The IOCTL status is embedded in the mailbox subheader. */
11371 shdr = (union lpfc_sli4_cfg_shdr *)
11372 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
11373 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
11374 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
11375 if (shdr_status || shdr_add_status || rc) {
11376 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11377 "2509 RQ_DESTROY mailbox failed with "
11378 "status x%x add_status x%x, mbx status x%x\n",
11379 shdr_status, shdr_add_status, rc);
11380 if (rc != MBX_TIMEOUT)
11381 mempool_free(mbox, hrq->phba->mbox_mem_pool);
11382 return -ENXIO;
11383 }
11384 bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
11385 drq->queue_id);
11386 rc = lpfc_sli_issue_mbox(drq->phba, mbox, MBX_POLL);
11387 shdr = (union lpfc_sli4_cfg_shdr *)
11388 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
11389 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
11390 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
11391 if (shdr_status || shdr_add_status || rc) {
11392 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11393 "2510 RQ_DESTROY mailbox failed with "
11394 "status x%x add_status x%x, mbx status x%x\n",
11395 shdr_status, shdr_add_status, rc);
11396 status = -ENXIO;
11397 }
11398 list_del_init(&hrq->list);
11399 list_del_init(&drq->list);
James Smart8fa38512009-07-19 10:01:03 -040011400 mempool_free(mbox, hrq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040011401 return status;
11402}
11403
11404/**
11405 * lpfc_sli4_post_sgl - Post scatter gather list for an XRI to HBA
11406 * @phba: The virtual port for which this call being executed.
11407 * @pdma_phys_addr0: Physical address of the 1st SGL page.
11408 * @pdma_phys_addr1: Physical address of the 2nd SGL page.
11409 * @xritag: the xritag that ties this io to the SGL pages.
11410 *
11411 * This routine will post the sgl pages for the IO that has the xritag
11412 * that is in the iocbq structure. The xritag is assigned during iocbq
11413 * creation and persists for as long as the driver is loaded.
11414 * if the caller has fewer than 256 scatter gather segments to map then
11415 * pdma_phys_addr1 should be 0.
11416 * If the caller needs to map more than 256 scatter gather segment then
11417 * pdma_phys_addr1 should be a valid physical address.
11418 * physical address for SGLs must be 64 byte aligned.
11419 * If you are going to map 2 SGL's then the first one must have 256 entries
11420 * the second sgl can have between 1 and 256 entries.
11421 *
11422 * Return codes:
11423 * 0 - Success
11424 * -ENXIO, -ENOMEM - Failure
11425 **/
11426int
11427lpfc_sli4_post_sgl(struct lpfc_hba *phba,
11428 dma_addr_t pdma_phys_addr0,
11429 dma_addr_t pdma_phys_addr1,
11430 uint16_t xritag)
11431{
11432 struct lpfc_mbx_post_sgl_pages *post_sgl_pages;
11433 LPFC_MBOXQ_t *mbox;
11434 int rc;
11435 uint32_t shdr_status, shdr_add_status;
11436 union lpfc_sli4_cfg_shdr *shdr;
11437
11438 if (xritag == NO_XRI) {
11439 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11440 "0364 Invalid param:\n");
11441 return -EINVAL;
11442 }
11443
11444 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
11445 if (!mbox)
11446 return -ENOMEM;
11447
11448 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
11449 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES,
11450 sizeof(struct lpfc_mbx_post_sgl_pages) -
James Smartfedd3b72011-02-16 12:39:24 -050011451 sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
James Smart4f774512009-05-22 14:52:35 -040011452
11453 post_sgl_pages = (struct lpfc_mbx_post_sgl_pages *)
11454 &mbox->u.mqe.un.post_sgl_pages;
11455 bf_set(lpfc_post_sgl_pages_xri, post_sgl_pages, xritag);
11456 bf_set(lpfc_post_sgl_pages_xricnt, post_sgl_pages, 1);
11457
11458 post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_lo =
11459 cpu_to_le32(putPaddrLow(pdma_phys_addr0));
11460 post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_hi =
11461 cpu_to_le32(putPaddrHigh(pdma_phys_addr0));
11462
11463 post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_lo =
11464 cpu_to_le32(putPaddrLow(pdma_phys_addr1));
11465 post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_hi =
11466 cpu_to_le32(putPaddrHigh(pdma_phys_addr1));
11467 if (!phba->sli4_hba.intr_enable)
11468 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
11469 else
11470 rc = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
11471 /* The IOCTL status is embedded in the mailbox subheader. */
11472 shdr = (union lpfc_sli4_cfg_shdr *) &post_sgl_pages->header.cfg_shdr;
11473 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
11474 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
11475 if (rc != MBX_TIMEOUT)
11476 mempool_free(mbox, phba->mbox_mem_pool);
11477 if (shdr_status || shdr_add_status || rc) {
11478 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11479 "2511 POST_SGL mailbox failed with "
11480 "status x%x add_status x%x, mbx status x%x\n",
11481 shdr_status, shdr_add_status, rc);
11482 rc = -ENXIO;
11483 }
11484 return 0;
11485}
James Smart4f774512009-05-22 14:52:35 -040011486
11487/**
11488 * lpfc_sli4_next_xritag - Get an xritag for the io
11489 * @phba: Pointer to HBA context object.
11490 *
11491 * This function gets an xritag for the iocb. If there is no unused xritag
11492 * it will return 0xffff.
11493 * The function returns the allocated xritag if successful, else returns zero.
11494 * Zero is not a valid xritag.
11495 * The caller is not required to hold any lock.
11496 **/
11497uint16_t
11498lpfc_sli4_next_xritag(struct lpfc_hba *phba)
11499{
11500 uint16_t xritag;
11501
11502 spin_lock_irq(&phba->hbalock);
11503 xritag = phba->sli4_hba.next_xri;
11504 if ((xritag != (uint16_t) -1) && xritag <
11505 (phba->sli4_hba.max_cfg_param.max_xri
11506 + phba->sli4_hba.max_cfg_param.xri_base)) {
11507 phba->sli4_hba.next_xri++;
11508 phba->sli4_hba.max_cfg_param.xri_used++;
11509 spin_unlock_irq(&phba->hbalock);
11510 return xritag;
11511 }
11512 spin_unlock_irq(&phba->hbalock);
James Smart6a9c52c2009-10-02 15:16:51 -040011513 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smart4f774512009-05-22 14:52:35 -040011514 "2004 Failed to allocate XRI.last XRITAG is %d"
11515 " Max XRI is %d, Used XRI is %d\n",
11516 phba->sli4_hba.next_xri,
11517 phba->sli4_hba.max_cfg_param.max_xri,
11518 phba->sli4_hba.max_cfg_param.xri_used);
11519 return -1;
11520}
11521
11522/**
11523 * lpfc_sli4_post_sgl_list - post a block of sgl list to the firmware.
11524 * @phba: pointer to lpfc hba data structure.
11525 *
11526 * This routine is invoked to post a block of driver's sgl pages to the
11527 * HBA using non-embedded mailbox command. No Lock is held. This routine
11528 * is only called when the driver is loading and after all IO has been
11529 * stopped.
11530 **/
11531int
11532lpfc_sli4_post_sgl_list(struct lpfc_hba *phba)
11533{
11534 struct lpfc_sglq *sglq_entry;
11535 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
11536 struct sgl_page_pairs *sgl_pg_pairs;
11537 void *viraddr;
11538 LPFC_MBOXQ_t *mbox;
11539 uint32_t reqlen, alloclen, pg_pairs;
11540 uint32_t mbox_tmo;
11541 uint16_t xritag_start = 0;
11542 int els_xri_cnt, rc = 0;
11543 uint32_t shdr_status, shdr_add_status;
11544 union lpfc_sli4_cfg_shdr *shdr;
11545
11546 /* The number of sgls to be posted */
11547 els_xri_cnt = lpfc_sli4_get_els_iocb_cnt(phba);
11548
11549 reqlen = els_xri_cnt * sizeof(struct sgl_page_pairs) +
11550 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
James Smart49198b32010-04-06 15:04:33 -040011551 if (reqlen > SLI4_PAGE_SIZE) {
James Smart4f774512009-05-22 14:52:35 -040011552 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
11553 "2559 Block sgl registration required DMA "
11554 "size (%d) great than a page\n", reqlen);
11555 return -ENOMEM;
11556 }
11557 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
11558 if (!mbox) {
11559 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11560 "2560 Failed to allocate mbox cmd memory\n");
11561 return -ENOMEM;
11562 }
11563
11564 /* Allocate DMA memory and set up the non-embedded mailbox command */
11565 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
11566 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
11567 LPFC_SLI4_MBX_NEMBED);
11568
11569 if (alloclen < reqlen) {
11570 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11571 "0285 Allocated DMA memory size (%d) is "
11572 "less than the requested DMA memory "
11573 "size (%d)\n", alloclen, reqlen);
11574 lpfc_sli4_mbox_cmd_free(phba, mbox);
11575 return -ENOMEM;
11576 }
James Smart4f774512009-05-22 14:52:35 -040011577 /* Get the first SGE entry from the non-embedded DMA memory */
James Smart4f774512009-05-22 14:52:35 -040011578 viraddr = mbox->sge_array->addr[0];
11579
11580 /* Set up the SGL pages in the non-embedded DMA pages */
11581 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
11582 sgl_pg_pairs = &sgl->sgl_pg_pairs;
11583
11584 for (pg_pairs = 0; pg_pairs < els_xri_cnt; pg_pairs++) {
11585 sglq_entry = phba->sli4_hba.lpfc_els_sgl_array[pg_pairs];
11586 /* Set up the sge entry */
11587 sgl_pg_pairs->sgl_pg0_addr_lo =
11588 cpu_to_le32(putPaddrLow(sglq_entry->phys));
11589 sgl_pg_pairs->sgl_pg0_addr_hi =
11590 cpu_to_le32(putPaddrHigh(sglq_entry->phys));
11591 sgl_pg_pairs->sgl_pg1_addr_lo =
11592 cpu_to_le32(putPaddrLow(0));
11593 sgl_pg_pairs->sgl_pg1_addr_hi =
11594 cpu_to_le32(putPaddrHigh(0));
11595 /* Keep the first xritag on the list */
11596 if (pg_pairs == 0)
11597 xritag_start = sglq_entry->sli4_xritag;
11598 sgl_pg_pairs++;
11599 }
11600 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
James Smart6a9c52c2009-10-02 15:16:51 -040011601 bf_set(lpfc_post_sgl_pages_xricnt, sgl, els_xri_cnt);
James Smart4f774512009-05-22 14:52:35 -040011602 /* Perform endian conversion if necessary */
11603 sgl->word0 = cpu_to_le32(sgl->word0);
11604
11605 if (!phba->sli4_hba.intr_enable)
11606 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
11607 else {
11608 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_SLI4_CONFIG);
11609 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
11610 }
11611 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
11612 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
11613 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
11614 if (rc != MBX_TIMEOUT)
11615 lpfc_sli4_mbox_cmd_free(phba, mbox);
11616 if (shdr_status || shdr_add_status || rc) {
11617 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11618 "2513 POST_SGL_BLOCK mailbox command failed "
11619 "status x%x add_status x%x mbx status x%x\n",
11620 shdr_status, shdr_add_status, rc);
11621 rc = -ENXIO;
11622 }
11623 return rc;
11624}
11625
11626/**
11627 * lpfc_sli4_post_scsi_sgl_block - post a block of scsi sgl list to firmware
11628 * @phba: pointer to lpfc hba data structure.
11629 * @sblist: pointer to scsi buffer list.
11630 * @count: number of scsi buffers on the list.
11631 *
11632 * This routine is invoked to post a block of @count scsi sgl pages from a
11633 * SCSI buffer list @sblist to the HBA using non-embedded mailbox command.
11634 * No Lock is held.
11635 *
11636 **/
11637int
11638lpfc_sli4_post_scsi_sgl_block(struct lpfc_hba *phba, struct list_head *sblist,
11639 int cnt)
11640{
11641 struct lpfc_scsi_buf *psb;
11642 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
11643 struct sgl_page_pairs *sgl_pg_pairs;
11644 void *viraddr;
11645 LPFC_MBOXQ_t *mbox;
11646 uint32_t reqlen, alloclen, pg_pairs;
11647 uint32_t mbox_tmo;
11648 uint16_t xritag_start = 0;
11649 int rc = 0;
11650 uint32_t shdr_status, shdr_add_status;
11651 dma_addr_t pdma_phys_bpl1;
11652 union lpfc_sli4_cfg_shdr *shdr;
11653
11654 /* Calculate the requested length of the dma memory */
11655 reqlen = cnt * sizeof(struct sgl_page_pairs) +
11656 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
James Smart49198b32010-04-06 15:04:33 -040011657 if (reqlen > SLI4_PAGE_SIZE) {
James Smart4f774512009-05-22 14:52:35 -040011658 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
11659 "0217 Block sgl registration required DMA "
11660 "size (%d) great than a page\n", reqlen);
11661 return -ENOMEM;
11662 }
11663 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
11664 if (!mbox) {
11665 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11666 "0283 Failed to allocate mbox cmd memory\n");
11667 return -ENOMEM;
11668 }
11669
11670 /* Allocate DMA memory and set up the non-embedded mailbox command */
11671 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
11672 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
11673 LPFC_SLI4_MBX_NEMBED);
11674
11675 if (alloclen < reqlen) {
11676 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11677 "2561 Allocated DMA memory size (%d) is "
11678 "less than the requested DMA memory "
11679 "size (%d)\n", alloclen, reqlen);
11680 lpfc_sli4_mbox_cmd_free(phba, mbox);
11681 return -ENOMEM;
11682 }
James Smart4f774512009-05-22 14:52:35 -040011683 /* Get the first SGE entry from the non-embedded DMA memory */
James Smart4f774512009-05-22 14:52:35 -040011684 viraddr = mbox->sge_array->addr[0];
11685
11686 /* Set up the SGL pages in the non-embedded DMA pages */
11687 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
11688 sgl_pg_pairs = &sgl->sgl_pg_pairs;
11689
11690 pg_pairs = 0;
11691 list_for_each_entry(psb, sblist, list) {
11692 /* Set up the sge entry */
11693 sgl_pg_pairs->sgl_pg0_addr_lo =
11694 cpu_to_le32(putPaddrLow(psb->dma_phys_bpl));
11695 sgl_pg_pairs->sgl_pg0_addr_hi =
11696 cpu_to_le32(putPaddrHigh(psb->dma_phys_bpl));
11697 if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
11698 pdma_phys_bpl1 = psb->dma_phys_bpl + SGL_PAGE_SIZE;
11699 else
11700 pdma_phys_bpl1 = 0;
11701 sgl_pg_pairs->sgl_pg1_addr_lo =
11702 cpu_to_le32(putPaddrLow(pdma_phys_bpl1));
11703 sgl_pg_pairs->sgl_pg1_addr_hi =
11704 cpu_to_le32(putPaddrHigh(pdma_phys_bpl1));
11705 /* Keep the first xritag on the list */
11706 if (pg_pairs == 0)
11707 xritag_start = psb->cur_iocbq.sli4_xritag;
11708 sgl_pg_pairs++;
11709 pg_pairs++;
11710 }
11711 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
11712 bf_set(lpfc_post_sgl_pages_xricnt, sgl, pg_pairs);
11713 /* Perform endian conversion if necessary */
11714 sgl->word0 = cpu_to_le32(sgl->word0);
11715
11716 if (!phba->sli4_hba.intr_enable)
11717 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
11718 else {
11719 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_SLI4_CONFIG);
11720 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
11721 }
11722 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
11723 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
11724 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
11725 if (rc != MBX_TIMEOUT)
11726 lpfc_sli4_mbox_cmd_free(phba, mbox);
11727 if (shdr_status || shdr_add_status || rc) {
11728 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11729 "2564 POST_SGL_BLOCK mailbox command failed "
11730 "status x%x add_status x%x mbx status x%x\n",
11731 shdr_status, shdr_add_status, rc);
11732 rc = -ENXIO;
11733 }
11734 return rc;
11735}
11736
11737/**
11738 * lpfc_fc_frame_check - Check that this frame is a valid frame to handle
11739 * @phba: pointer to lpfc_hba struct that the frame was received on
11740 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
11741 *
11742 * This function checks the fields in the @fc_hdr to see if the FC frame is a
11743 * valid type of frame that the LPFC driver will handle. This function will
11744 * return a zero if the frame is a valid frame or a non zero value when the
11745 * frame does not pass the check.
11746 **/
11747static int
11748lpfc_fc_frame_check(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr)
11749{
Tomas Henzl474ffb72010-12-22 16:52:40 +010011750 /* make rctl_names static to save stack space */
11751 static char *rctl_names[] = FC_RCTL_NAMES_INIT;
James Smart4f774512009-05-22 14:52:35 -040011752 char *type_names[] = FC_TYPE_NAMES_INIT;
11753 struct fc_vft_header *fc_vft_hdr;
James Smart546fc852011-03-11 16:06:29 -050011754 uint32_t *header = (uint32_t *) fc_hdr;
James Smart4f774512009-05-22 14:52:35 -040011755
11756 switch (fc_hdr->fh_r_ctl) {
11757 case FC_RCTL_DD_UNCAT: /* uncategorized information */
11758 case FC_RCTL_DD_SOL_DATA: /* solicited data */
11759 case FC_RCTL_DD_UNSOL_CTL: /* unsolicited control */
11760 case FC_RCTL_DD_SOL_CTL: /* solicited control or reply */
11761 case FC_RCTL_DD_UNSOL_DATA: /* unsolicited data */
11762 case FC_RCTL_DD_DATA_DESC: /* data descriptor */
11763 case FC_RCTL_DD_UNSOL_CMD: /* unsolicited command */
11764 case FC_RCTL_DD_CMD_STATUS: /* command status */
11765 case FC_RCTL_ELS_REQ: /* extended link services request */
11766 case FC_RCTL_ELS_REP: /* extended link services reply */
11767 case FC_RCTL_ELS4_REQ: /* FC-4 ELS request */
11768 case FC_RCTL_ELS4_REP: /* FC-4 ELS reply */
11769 case FC_RCTL_BA_NOP: /* basic link service NOP */
11770 case FC_RCTL_BA_ABTS: /* basic link service abort */
11771 case FC_RCTL_BA_RMC: /* remove connection */
11772 case FC_RCTL_BA_ACC: /* basic accept */
11773 case FC_RCTL_BA_RJT: /* basic reject */
11774 case FC_RCTL_BA_PRMT:
11775 case FC_RCTL_ACK_1: /* acknowledge_1 */
11776 case FC_RCTL_ACK_0: /* acknowledge_0 */
11777 case FC_RCTL_P_RJT: /* port reject */
11778 case FC_RCTL_F_RJT: /* fabric reject */
11779 case FC_RCTL_P_BSY: /* port busy */
11780 case FC_RCTL_F_BSY: /* fabric busy to data frame */
11781 case FC_RCTL_F_BSYL: /* fabric busy to link control frame */
11782 case FC_RCTL_LCR: /* link credit reset */
11783 case FC_RCTL_END: /* end */
11784 break;
11785 case FC_RCTL_VFTH: /* Virtual Fabric tagging Header */
11786 fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
11787 fc_hdr = &((struct fc_frame_header *)fc_vft_hdr)[1];
11788 return lpfc_fc_frame_check(phba, fc_hdr);
11789 default:
11790 goto drop;
11791 }
11792 switch (fc_hdr->fh_type) {
11793 case FC_TYPE_BLS:
11794 case FC_TYPE_ELS:
11795 case FC_TYPE_FCP:
11796 case FC_TYPE_CT:
11797 break;
11798 case FC_TYPE_IP:
11799 case FC_TYPE_ILS:
11800 default:
11801 goto drop;
11802 }
James Smart546fc852011-03-11 16:06:29 -050011803
James Smart4f774512009-05-22 14:52:35 -040011804 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
James Smart546fc852011-03-11 16:06:29 -050011805 "2538 Received frame rctl:%s type:%s "
11806 "Frame Data:%08x %08x %08x %08x %08x %08x\n",
James Smart4f774512009-05-22 14:52:35 -040011807 rctl_names[fc_hdr->fh_r_ctl],
James Smart546fc852011-03-11 16:06:29 -050011808 type_names[fc_hdr->fh_type],
11809 be32_to_cpu(header[0]), be32_to_cpu(header[1]),
11810 be32_to_cpu(header[2]), be32_to_cpu(header[3]),
11811 be32_to_cpu(header[4]), be32_to_cpu(header[5]));
James Smart4f774512009-05-22 14:52:35 -040011812 return 0;
11813drop:
11814 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
11815 "2539 Dropped frame rctl:%s type:%s\n",
11816 rctl_names[fc_hdr->fh_r_ctl],
11817 type_names[fc_hdr->fh_type]);
11818 return 1;
11819}
11820
11821/**
11822 * lpfc_fc_hdr_get_vfi - Get the VFI from an FC frame
11823 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
11824 *
11825 * This function processes the FC header to retrieve the VFI from the VF
11826 * header, if one exists. This function will return the VFI if one exists
11827 * or 0 if no VSAN Header exists.
11828 **/
11829static uint32_t
11830lpfc_fc_hdr_get_vfi(struct fc_frame_header *fc_hdr)
11831{
11832 struct fc_vft_header *fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
11833
11834 if (fc_hdr->fh_r_ctl != FC_RCTL_VFTH)
11835 return 0;
11836 return bf_get(fc_vft_hdr_vf_id, fc_vft_hdr);
11837}
11838
11839/**
11840 * lpfc_fc_frame_to_vport - Finds the vport that a frame is destined to
11841 * @phba: Pointer to the HBA structure to search for the vport on
11842 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
11843 * @fcfi: The FC Fabric ID that the frame came from
11844 *
11845 * This function searches the @phba for a vport that matches the content of the
11846 * @fc_hdr passed in and the @fcfi. This function uses the @fc_hdr to fetch the
11847 * VFI, if the Virtual Fabric Tagging Header exists, and the DID. This function
11848 * returns the matching vport pointer or NULL if unable to match frame to a
11849 * vport.
11850 **/
11851static struct lpfc_vport *
11852lpfc_fc_frame_to_vport(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr,
11853 uint16_t fcfi)
11854{
11855 struct lpfc_vport **vports;
11856 struct lpfc_vport *vport = NULL;
11857 int i;
11858 uint32_t did = (fc_hdr->fh_d_id[0] << 16 |
11859 fc_hdr->fh_d_id[1] << 8 |
11860 fc_hdr->fh_d_id[2]);
11861
11862 vports = lpfc_create_vport_work_array(phba);
11863 if (vports != NULL)
11864 for (i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
11865 if (phba->fcf.fcfi == fcfi &&
11866 vports[i]->vfi == lpfc_fc_hdr_get_vfi(fc_hdr) &&
11867 vports[i]->fc_myDID == did) {
11868 vport = vports[i];
11869 break;
11870 }
11871 }
11872 lpfc_destroy_vport_work_array(phba, vports);
11873 return vport;
11874}
11875
11876/**
James Smart45ed1192009-10-02 15:17:02 -040011877 * lpfc_update_rcv_time_stamp - Update vport's rcv seq time stamp
11878 * @vport: The vport to work on.
11879 *
11880 * This function updates the receive sequence time stamp for this vport. The
11881 * receive sequence time stamp indicates the time that the last frame of the
11882 * the sequence that has been idle for the longest amount of time was received.
11883 * the driver uses this time stamp to indicate if any received sequences have
11884 * timed out.
11885 **/
11886void
11887lpfc_update_rcv_time_stamp(struct lpfc_vport *vport)
11888{
11889 struct lpfc_dmabuf *h_buf;
11890 struct hbq_dmabuf *dmabuf = NULL;
11891
11892 /* get the oldest sequence on the rcv list */
11893 h_buf = list_get_first(&vport->rcv_buffer_list,
11894 struct lpfc_dmabuf, list);
11895 if (!h_buf)
11896 return;
11897 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
11898 vport->rcv_buffer_time_stamp = dmabuf->time_stamp;
11899}
11900
11901/**
11902 * lpfc_cleanup_rcv_buffers - Cleans up all outstanding receive sequences.
11903 * @vport: The vport that the received sequences were sent to.
11904 *
11905 * This function cleans up all outstanding received sequences. This is called
11906 * by the driver when a link event or user action invalidates all the received
11907 * sequences.
11908 **/
11909void
11910lpfc_cleanup_rcv_buffers(struct lpfc_vport *vport)
11911{
11912 struct lpfc_dmabuf *h_buf, *hnext;
11913 struct lpfc_dmabuf *d_buf, *dnext;
11914 struct hbq_dmabuf *dmabuf = NULL;
11915
11916 /* start with the oldest sequence on the rcv list */
11917 list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
11918 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
11919 list_del_init(&dmabuf->hbuf.list);
11920 list_for_each_entry_safe(d_buf, dnext,
11921 &dmabuf->dbuf.list, list) {
11922 list_del_init(&d_buf->list);
11923 lpfc_in_buf_free(vport->phba, d_buf);
11924 }
11925 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
11926 }
11927}
11928
11929/**
11930 * lpfc_rcv_seq_check_edtov - Cleans up timed out receive sequences.
11931 * @vport: The vport that the received sequences were sent to.
11932 *
11933 * This function determines whether any received sequences have timed out by
11934 * first checking the vport's rcv_buffer_time_stamp. If this time_stamp
11935 * indicates that there is at least one timed out sequence this routine will
11936 * go through the received sequences one at a time from most inactive to most
11937 * active to determine which ones need to be cleaned up. Once it has determined
11938 * that a sequence needs to be cleaned up it will simply free up the resources
11939 * without sending an abort.
11940 **/
11941void
11942lpfc_rcv_seq_check_edtov(struct lpfc_vport *vport)
11943{
11944 struct lpfc_dmabuf *h_buf, *hnext;
11945 struct lpfc_dmabuf *d_buf, *dnext;
11946 struct hbq_dmabuf *dmabuf = NULL;
11947 unsigned long timeout;
11948 int abort_count = 0;
11949
11950 timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
11951 vport->rcv_buffer_time_stamp);
11952 if (list_empty(&vport->rcv_buffer_list) ||
11953 time_before(jiffies, timeout))
11954 return;
11955 /* start with the oldest sequence on the rcv list */
11956 list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
11957 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
11958 timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
11959 dmabuf->time_stamp);
11960 if (time_before(jiffies, timeout))
11961 break;
11962 abort_count++;
11963 list_del_init(&dmabuf->hbuf.list);
11964 list_for_each_entry_safe(d_buf, dnext,
11965 &dmabuf->dbuf.list, list) {
11966 list_del_init(&d_buf->list);
11967 lpfc_in_buf_free(vport->phba, d_buf);
11968 }
11969 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
11970 }
11971 if (abort_count)
11972 lpfc_update_rcv_time_stamp(vport);
11973}
11974
11975/**
James Smart4f774512009-05-22 14:52:35 -040011976 * lpfc_fc_frame_add - Adds a frame to the vport's list of received sequences
11977 * @dmabuf: pointer to a dmabuf that describes the hdr and data of the FC frame
11978 *
11979 * This function searches through the existing incomplete sequences that have
11980 * been sent to this @vport. If the frame matches one of the incomplete
11981 * sequences then the dbuf in the @dmabuf is added to the list of frames that
11982 * make up that sequence. If no sequence is found that matches this frame then
11983 * the function will add the hbuf in the @dmabuf to the @vport's rcv_buffer_list
11984 * This function returns a pointer to the first dmabuf in the sequence list that
11985 * the frame was linked to.
11986 **/
11987static struct hbq_dmabuf *
11988lpfc_fc_frame_add(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
11989{
11990 struct fc_frame_header *new_hdr;
11991 struct fc_frame_header *temp_hdr;
11992 struct lpfc_dmabuf *d_buf;
11993 struct lpfc_dmabuf *h_buf;
11994 struct hbq_dmabuf *seq_dmabuf = NULL;
11995 struct hbq_dmabuf *temp_dmabuf = NULL;
11996
James Smart4d9ab992009-10-02 15:16:39 -040011997 INIT_LIST_HEAD(&dmabuf->dbuf.list);
James Smart45ed1192009-10-02 15:17:02 -040011998 dmabuf->time_stamp = jiffies;
James Smart4f774512009-05-22 14:52:35 -040011999 new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
12000 /* Use the hdr_buf to find the sequence that this frame belongs to */
12001 list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
12002 temp_hdr = (struct fc_frame_header *)h_buf->virt;
12003 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
12004 (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
12005 (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
12006 continue;
12007 /* found a pending sequence that matches this frame */
12008 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
12009 break;
12010 }
12011 if (!seq_dmabuf) {
12012 /*
12013 * This indicates first frame received for this sequence.
12014 * Queue the buffer on the vport's rcv_buffer_list.
12015 */
12016 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
James Smart45ed1192009-10-02 15:17:02 -040012017 lpfc_update_rcv_time_stamp(vport);
James Smart4f774512009-05-22 14:52:35 -040012018 return dmabuf;
12019 }
12020 temp_hdr = seq_dmabuf->hbuf.virt;
James Smarteeead812009-12-21 17:01:23 -050012021 if (be16_to_cpu(new_hdr->fh_seq_cnt) <
12022 be16_to_cpu(temp_hdr->fh_seq_cnt)) {
James Smart4d9ab992009-10-02 15:16:39 -040012023 list_del_init(&seq_dmabuf->hbuf.list);
12024 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
12025 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
James Smart45ed1192009-10-02 15:17:02 -040012026 lpfc_update_rcv_time_stamp(vport);
James Smart4f774512009-05-22 14:52:35 -040012027 return dmabuf;
12028 }
James Smart45ed1192009-10-02 15:17:02 -040012029 /* move this sequence to the tail to indicate a young sequence */
12030 list_move_tail(&seq_dmabuf->hbuf.list, &vport->rcv_buffer_list);
12031 seq_dmabuf->time_stamp = jiffies;
12032 lpfc_update_rcv_time_stamp(vport);
James Smarteeead812009-12-21 17:01:23 -050012033 if (list_empty(&seq_dmabuf->dbuf.list)) {
12034 temp_hdr = dmabuf->hbuf.virt;
12035 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
12036 return seq_dmabuf;
12037 }
James Smart4f774512009-05-22 14:52:35 -040012038 /* find the correct place in the sequence to insert this frame */
12039 list_for_each_entry_reverse(d_buf, &seq_dmabuf->dbuf.list, list) {
12040 temp_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
12041 temp_hdr = (struct fc_frame_header *)temp_dmabuf->hbuf.virt;
12042 /*
12043 * If the frame's sequence count is greater than the frame on
12044 * the list then insert the frame right after this frame
12045 */
James Smarteeead812009-12-21 17:01:23 -050012046 if (be16_to_cpu(new_hdr->fh_seq_cnt) >
12047 be16_to_cpu(temp_hdr->fh_seq_cnt)) {
James Smart4f774512009-05-22 14:52:35 -040012048 list_add(&dmabuf->dbuf.list, &temp_dmabuf->dbuf.list);
12049 return seq_dmabuf;
12050 }
12051 }
12052 return NULL;
12053}
12054
12055/**
James Smart6669f9b2009-10-02 15:16:45 -040012056 * lpfc_sli4_abort_partial_seq - Abort partially assembled unsol sequence
12057 * @vport: pointer to a vitural port
12058 * @dmabuf: pointer to a dmabuf that describes the FC sequence
12059 *
12060 * This function tries to abort from the partially assembed sequence, described
12061 * by the information from basic abbort @dmabuf. It checks to see whether such
12062 * partially assembled sequence held by the driver. If so, it shall free up all
12063 * the frames from the partially assembled sequence.
12064 *
12065 * Return
12066 * true -- if there is matching partially assembled sequence present and all
12067 * the frames freed with the sequence;
12068 * false -- if there is no matching partially assembled sequence present so
12069 * nothing got aborted in the lower layer driver
12070 **/
12071static bool
12072lpfc_sli4_abort_partial_seq(struct lpfc_vport *vport,
12073 struct hbq_dmabuf *dmabuf)
12074{
12075 struct fc_frame_header *new_hdr;
12076 struct fc_frame_header *temp_hdr;
12077 struct lpfc_dmabuf *d_buf, *n_buf, *h_buf;
12078 struct hbq_dmabuf *seq_dmabuf = NULL;
12079
12080 /* Use the hdr_buf to find the sequence that matches this frame */
12081 INIT_LIST_HEAD(&dmabuf->dbuf.list);
12082 INIT_LIST_HEAD(&dmabuf->hbuf.list);
12083 new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
12084 list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
12085 temp_hdr = (struct fc_frame_header *)h_buf->virt;
12086 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
12087 (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
12088 (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
12089 continue;
12090 /* found a pending sequence that matches this frame */
12091 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
12092 break;
12093 }
12094
12095 /* Free up all the frames from the partially assembled sequence */
12096 if (seq_dmabuf) {
12097 list_for_each_entry_safe(d_buf, n_buf,
12098 &seq_dmabuf->dbuf.list, list) {
12099 list_del_init(&d_buf->list);
12100 lpfc_in_buf_free(vport->phba, d_buf);
12101 }
12102 return true;
12103 }
12104 return false;
12105}
12106
12107/**
James Smart546fc852011-03-11 16:06:29 -050012108 * lpfc_sli4_seq_abort_rsp_cmpl - BLS ABORT RSP seq abort iocb complete handler
James Smart6669f9b2009-10-02 15:16:45 -040012109 * @phba: Pointer to HBA context object.
12110 * @cmd_iocbq: pointer to the command iocbq structure.
12111 * @rsp_iocbq: pointer to the response iocbq structure.
12112 *
James Smart546fc852011-03-11 16:06:29 -050012113 * This function handles the sequence abort response iocb command complete
James Smart6669f9b2009-10-02 15:16:45 -040012114 * event. It properly releases the memory allocated to the sequence abort
12115 * accept iocb.
12116 **/
12117static void
James Smart546fc852011-03-11 16:06:29 -050012118lpfc_sli4_seq_abort_rsp_cmpl(struct lpfc_hba *phba,
James Smart6669f9b2009-10-02 15:16:45 -040012119 struct lpfc_iocbq *cmd_iocbq,
12120 struct lpfc_iocbq *rsp_iocbq)
12121{
12122 if (cmd_iocbq)
12123 lpfc_sli_release_iocbq(phba, cmd_iocbq);
12124}
12125
12126/**
James Smart546fc852011-03-11 16:06:29 -050012127 * lpfc_sli4_seq_abort_rsp - bls rsp to sequence abort
James Smart6669f9b2009-10-02 15:16:45 -040012128 * @phba: Pointer to HBA context object.
12129 * @fc_hdr: pointer to a FC frame header.
12130 *
James Smart546fc852011-03-11 16:06:29 -050012131 * This function sends a basic response to a previous unsol sequence abort
James Smart6669f9b2009-10-02 15:16:45 -040012132 * event after aborting the sequence handling.
12133 **/
12134static void
James Smart546fc852011-03-11 16:06:29 -050012135lpfc_sli4_seq_abort_rsp(struct lpfc_hba *phba,
James Smart6669f9b2009-10-02 15:16:45 -040012136 struct fc_frame_header *fc_hdr)
12137{
12138 struct lpfc_iocbq *ctiocb = NULL;
12139 struct lpfc_nodelist *ndlp;
James Smart5ffc2662009-11-18 15:39:44 -050012140 uint16_t oxid, rxid;
12141 uint32_t sid, fctl;
James Smart6669f9b2009-10-02 15:16:45 -040012142 IOCB_t *icmd;
James Smart546fc852011-03-11 16:06:29 -050012143 int rc;
James Smart6669f9b2009-10-02 15:16:45 -040012144
12145 if (!lpfc_is_link_up(phba))
12146 return;
12147
12148 sid = sli4_sid_from_fc_hdr(fc_hdr);
12149 oxid = be16_to_cpu(fc_hdr->fh_ox_id);
James Smart5ffc2662009-11-18 15:39:44 -050012150 rxid = be16_to_cpu(fc_hdr->fh_rx_id);
James Smart6669f9b2009-10-02 15:16:45 -040012151
12152 ndlp = lpfc_findnode_did(phba->pport, sid);
12153 if (!ndlp) {
12154 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
12155 "1268 Find ndlp returned NULL for oxid:x%x "
12156 "SID:x%x\n", oxid, sid);
12157 return;
12158 }
James Smart19ca7602010-11-20 23:11:55 -050012159 if (rxid >= phba->sli4_hba.max_cfg_param.xri_base
12160 && rxid <= (phba->sli4_hba.max_cfg_param.max_xri
12161 + phba->sli4_hba.max_cfg_param.xri_base))
12162 lpfc_set_rrq_active(phba, ndlp, rxid, oxid, 0);
James Smart6669f9b2009-10-02 15:16:45 -040012163
James Smart546fc852011-03-11 16:06:29 -050012164 /* Allocate buffer for rsp iocb */
James Smart6669f9b2009-10-02 15:16:45 -040012165 ctiocb = lpfc_sli_get_iocbq(phba);
12166 if (!ctiocb)
12167 return;
12168
James Smart5ffc2662009-11-18 15:39:44 -050012169 /* Extract the F_CTL field from FC_HDR */
12170 fctl = sli4_fctl_from_fc_hdr(fc_hdr);
12171
James Smart6669f9b2009-10-02 15:16:45 -040012172 icmd = &ctiocb->iocb;
James Smart6669f9b2009-10-02 15:16:45 -040012173 icmd->un.xseq64.bdl.bdeSize = 0;
James Smart5ffc2662009-11-18 15:39:44 -050012174 icmd->un.xseq64.bdl.ulpIoTag32 = 0;
James Smart6669f9b2009-10-02 15:16:45 -040012175 icmd->un.xseq64.w5.hcsw.Dfctl = 0;
12176 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_ACC;
12177 icmd->un.xseq64.w5.hcsw.Type = FC_TYPE_BLS;
12178
12179 /* Fill in the rest of iocb fields */
12180 icmd->ulpCommand = CMD_XMIT_BLS_RSP64_CX;
12181 icmd->ulpBdeCount = 0;
12182 icmd->ulpLe = 1;
12183 icmd->ulpClass = CLASS3;
12184 icmd->ulpContext = ndlp->nlp_rpi;
James Smartbe858b62010-12-15 17:57:20 -050012185 ctiocb->context1 = ndlp;
James Smart6669f9b2009-10-02 15:16:45 -040012186
James Smart6669f9b2009-10-02 15:16:45 -040012187 ctiocb->iocb_cmpl = NULL;
12188 ctiocb->vport = phba->pport;
James Smart546fc852011-03-11 16:06:29 -050012189 ctiocb->iocb_cmpl = lpfc_sli4_seq_abort_rsp_cmpl;
12190 ctiocb->sli4_xritag = NO_XRI;
12191
12192 /* If the oxid maps to the FCP XRI range or if it is out of range,
12193 * send a BLS_RJT. The driver no longer has that exchange.
12194 * Override the IOCB for a BA_RJT.
12195 */
12196 if (oxid > (phba->sli4_hba.max_cfg_param.max_xri +
12197 phba->sli4_hba.max_cfg_param.xri_base) ||
12198 oxid > (lpfc_sli4_get_els_iocb_cnt(phba) +
12199 phba->sli4_hba.max_cfg_param.xri_base)) {
12200 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_RJT;
12201 bf_set(lpfc_vndr_code, &icmd->un.bls_rsp, 0);
12202 bf_set(lpfc_rsn_expln, &icmd->un.bls_rsp, FC_BA_RJT_INV_XID);
12203 bf_set(lpfc_rsn_code, &icmd->un.bls_rsp, FC_BA_RJT_UNABLE);
12204 }
James Smart6669f9b2009-10-02 15:16:45 -040012205
James Smart5ffc2662009-11-18 15:39:44 -050012206 if (fctl & FC_FC_EX_CTX) {
12207 /* ABTS sent by responder to CT exchange, construction
12208 * of BA_ACC will use OX_ID from ABTS for the XRI_TAG
12209 * field and RX_ID from ABTS for RX_ID field.
12210 */
James Smart546fc852011-03-11 16:06:29 -050012211 bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_RSP);
12212 bf_set(lpfc_abts_rxid, &icmd->un.bls_rsp, rxid);
James Smart5ffc2662009-11-18 15:39:44 -050012213 } else {
12214 /* ABTS sent by initiator to CT exchange, construction
12215 * of BA_ACC will need to allocate a new XRI as for the
12216 * XRI_TAG and RX_ID fields.
12217 */
James Smart546fc852011-03-11 16:06:29 -050012218 bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_INT);
12219 bf_set(lpfc_abts_rxid, &icmd->un.bls_rsp, NO_XRI);
James Smart5ffc2662009-11-18 15:39:44 -050012220 }
James Smart546fc852011-03-11 16:06:29 -050012221 bf_set(lpfc_abts_oxid, &icmd->un.bls_rsp, oxid);
James Smart5ffc2662009-11-18 15:39:44 -050012222
James Smart546fc852011-03-11 16:06:29 -050012223 /* Xmit CT abts response on exchange <xid> */
James Smart6669f9b2009-10-02 15:16:45 -040012224 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
James Smart546fc852011-03-11 16:06:29 -050012225 "1200 Send BLS cmd x%x on oxid x%x Data: x%x\n",
12226 icmd->un.xseq64.w5.hcsw.Rctl, oxid, phba->link_state);
12227
12228 rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, ctiocb, 0);
12229 if (rc == IOCB_ERROR) {
12230 lpfc_printf_log(phba, KERN_ERR, LOG_ELS,
12231 "2925 Failed to issue CT ABTS RSP x%x on "
12232 "xri x%x, Data x%x\n",
12233 icmd->un.xseq64.w5.hcsw.Rctl, oxid,
12234 phba->link_state);
12235 lpfc_sli_release_iocbq(phba, ctiocb);
12236 }
James Smart6669f9b2009-10-02 15:16:45 -040012237}
12238
12239/**
12240 * lpfc_sli4_handle_unsol_abort - Handle sli-4 unsolicited abort event
12241 * @vport: Pointer to the vport on which this sequence was received
12242 * @dmabuf: pointer to a dmabuf that describes the FC sequence
12243 *
12244 * This function handles an SLI-4 unsolicited abort event. If the unsolicited
12245 * receive sequence is only partially assembed by the driver, it shall abort
12246 * the partially assembled frames for the sequence. Otherwise, if the
12247 * unsolicited receive sequence has been completely assembled and passed to
12248 * the Upper Layer Protocol (UPL), it then mark the per oxid status for the
12249 * unsolicited sequence has been aborted. After that, it will issue a basic
12250 * accept to accept the abort.
12251 **/
12252void
12253lpfc_sli4_handle_unsol_abort(struct lpfc_vport *vport,
12254 struct hbq_dmabuf *dmabuf)
12255{
12256 struct lpfc_hba *phba = vport->phba;
12257 struct fc_frame_header fc_hdr;
James Smart5ffc2662009-11-18 15:39:44 -050012258 uint32_t fctl;
James Smart6669f9b2009-10-02 15:16:45 -040012259 bool abts_par;
12260
James Smart6669f9b2009-10-02 15:16:45 -040012261 /* Make a copy of fc_hdr before the dmabuf being released */
12262 memcpy(&fc_hdr, dmabuf->hbuf.virt, sizeof(struct fc_frame_header));
James Smart5ffc2662009-11-18 15:39:44 -050012263 fctl = sli4_fctl_from_fc_hdr(&fc_hdr);
James Smart6669f9b2009-10-02 15:16:45 -040012264
James Smart5ffc2662009-11-18 15:39:44 -050012265 if (fctl & FC_FC_EX_CTX) {
12266 /*
12267 * ABTS sent by responder to exchange, just free the buffer
12268 */
James Smart6669f9b2009-10-02 15:16:45 -040012269 lpfc_in_buf_free(phba, &dmabuf->dbuf);
James Smart5ffc2662009-11-18 15:39:44 -050012270 } else {
12271 /*
12272 * ABTS sent by initiator to exchange, need to do cleanup
12273 */
12274 /* Try to abort partially assembled seq */
12275 abts_par = lpfc_sli4_abort_partial_seq(vport, dmabuf);
12276
12277 /* Send abort to ULP if partially seq abort failed */
12278 if (abts_par == false)
12279 lpfc_sli4_send_seq_to_ulp(vport, dmabuf);
12280 else
12281 lpfc_in_buf_free(phba, &dmabuf->dbuf);
12282 }
James Smart6669f9b2009-10-02 15:16:45 -040012283 /* Send basic accept (BA_ACC) to the abort requester */
James Smart546fc852011-03-11 16:06:29 -050012284 lpfc_sli4_seq_abort_rsp(phba, &fc_hdr);
James Smart6669f9b2009-10-02 15:16:45 -040012285}
12286
12287/**
James Smart4f774512009-05-22 14:52:35 -040012288 * lpfc_seq_complete - Indicates if a sequence is complete
12289 * @dmabuf: pointer to a dmabuf that describes the FC sequence
12290 *
12291 * This function checks the sequence, starting with the frame described by
12292 * @dmabuf, to see if all the frames associated with this sequence are present.
12293 * the frames associated with this sequence are linked to the @dmabuf using the
12294 * dbuf list. This function looks for two major things. 1) That the first frame
12295 * has a sequence count of zero. 2) There is a frame with last frame of sequence
12296 * set. 3) That there are no holes in the sequence count. The function will
12297 * return 1 when the sequence is complete, otherwise it will return 0.
12298 **/
12299static int
12300lpfc_seq_complete(struct hbq_dmabuf *dmabuf)
12301{
12302 struct fc_frame_header *hdr;
12303 struct lpfc_dmabuf *d_buf;
12304 struct hbq_dmabuf *seq_dmabuf;
12305 uint32_t fctl;
12306 int seq_count = 0;
12307
12308 hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
12309 /* make sure first fame of sequence has a sequence count of zero */
12310 if (hdr->fh_seq_cnt != seq_count)
12311 return 0;
12312 fctl = (hdr->fh_f_ctl[0] << 16 |
12313 hdr->fh_f_ctl[1] << 8 |
12314 hdr->fh_f_ctl[2]);
12315 /* If last frame of sequence we can return success. */
12316 if (fctl & FC_FC_END_SEQ)
12317 return 1;
12318 list_for_each_entry(d_buf, &dmabuf->dbuf.list, list) {
12319 seq_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
12320 hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
12321 /* If there is a hole in the sequence count then fail. */
James Smarteeead812009-12-21 17:01:23 -050012322 if (++seq_count != be16_to_cpu(hdr->fh_seq_cnt))
James Smart4f774512009-05-22 14:52:35 -040012323 return 0;
12324 fctl = (hdr->fh_f_ctl[0] << 16 |
12325 hdr->fh_f_ctl[1] << 8 |
12326 hdr->fh_f_ctl[2]);
12327 /* If last frame of sequence we can return success. */
12328 if (fctl & FC_FC_END_SEQ)
12329 return 1;
12330 }
12331 return 0;
12332}
12333
12334/**
12335 * lpfc_prep_seq - Prep sequence for ULP processing
12336 * @vport: Pointer to the vport on which this sequence was received
12337 * @dmabuf: pointer to a dmabuf that describes the FC sequence
12338 *
12339 * This function takes a sequence, described by a list of frames, and creates
12340 * a list of iocbq structures to describe the sequence. This iocbq list will be
12341 * used to issue to the generic unsolicited sequence handler. This routine
12342 * returns a pointer to the first iocbq in the list. If the function is unable
12343 * to allocate an iocbq then it throw out the received frames that were not
12344 * able to be described and return a pointer to the first iocbq. If unable to
12345 * allocate any iocbqs (including the first) this function will return NULL.
12346 **/
12347static struct lpfc_iocbq *
12348lpfc_prep_seq(struct lpfc_vport *vport, struct hbq_dmabuf *seq_dmabuf)
12349{
12350 struct lpfc_dmabuf *d_buf, *n_buf;
12351 struct lpfc_iocbq *first_iocbq, *iocbq;
12352 struct fc_frame_header *fc_hdr;
12353 uint32_t sid;
James Smarteeead812009-12-21 17:01:23 -050012354 struct ulp_bde64 *pbde;
James Smart4f774512009-05-22 14:52:35 -040012355
12356 fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
12357 /* remove from receive buffer list */
12358 list_del_init(&seq_dmabuf->hbuf.list);
James Smart45ed1192009-10-02 15:17:02 -040012359 lpfc_update_rcv_time_stamp(vport);
James Smart4f774512009-05-22 14:52:35 -040012360 /* get the Remote Port's SID */
James Smart6669f9b2009-10-02 15:16:45 -040012361 sid = sli4_sid_from_fc_hdr(fc_hdr);
James Smart4f774512009-05-22 14:52:35 -040012362 /* Get an iocbq struct to fill in. */
12363 first_iocbq = lpfc_sli_get_iocbq(vport->phba);
12364 if (first_iocbq) {
12365 /* Initialize the first IOCB. */
James Smart8fa38512009-07-19 10:01:03 -040012366 first_iocbq->iocb.unsli3.rcvsli3.acc_len = 0;
James Smart4f774512009-05-22 14:52:35 -040012367 first_iocbq->iocb.ulpStatus = IOSTAT_SUCCESS;
12368 first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_SEQ64_CX;
12369 first_iocbq->iocb.ulpContext = be16_to_cpu(fc_hdr->fh_ox_id);
12370 first_iocbq->iocb.unsli3.rcvsli3.vpi =
12371 vport->vpi + vport->phba->vpi_base;
12372 /* put the first buffer into the first IOCBq */
12373 first_iocbq->context2 = &seq_dmabuf->dbuf;
12374 first_iocbq->context3 = NULL;
12375 first_iocbq->iocb.ulpBdeCount = 1;
12376 first_iocbq->iocb.un.cont64[0].tus.f.bdeSize =
12377 LPFC_DATA_BUF_SIZE;
12378 first_iocbq->iocb.un.rcvels.remoteID = sid;
James Smart8fa38512009-07-19 10:01:03 -040012379 first_iocbq->iocb.unsli3.rcvsli3.acc_len +=
James Smart4d9ab992009-10-02 15:16:39 -040012380 bf_get(lpfc_rcqe_length,
12381 &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
James Smart4f774512009-05-22 14:52:35 -040012382 }
12383 iocbq = first_iocbq;
12384 /*
12385 * Each IOCBq can have two Buffers assigned, so go through the list
12386 * of buffers for this sequence and save two buffers in each IOCBq
12387 */
12388 list_for_each_entry_safe(d_buf, n_buf, &seq_dmabuf->dbuf.list, list) {
12389 if (!iocbq) {
12390 lpfc_in_buf_free(vport->phba, d_buf);
12391 continue;
12392 }
12393 if (!iocbq->context3) {
12394 iocbq->context3 = d_buf;
12395 iocbq->iocb.ulpBdeCount++;
James Smarteeead812009-12-21 17:01:23 -050012396 pbde = (struct ulp_bde64 *)
12397 &iocbq->iocb.unsli3.sli3Words[4];
12398 pbde->tus.f.bdeSize = LPFC_DATA_BUF_SIZE;
James Smart8fa38512009-07-19 10:01:03 -040012399 first_iocbq->iocb.unsli3.rcvsli3.acc_len +=
James Smart4d9ab992009-10-02 15:16:39 -040012400 bf_get(lpfc_rcqe_length,
12401 &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
James Smart4f774512009-05-22 14:52:35 -040012402 } else {
12403 iocbq = lpfc_sli_get_iocbq(vport->phba);
12404 if (!iocbq) {
12405 if (first_iocbq) {
12406 first_iocbq->iocb.ulpStatus =
12407 IOSTAT_FCP_RSP_ERROR;
12408 first_iocbq->iocb.un.ulpWord[4] =
12409 IOERR_NO_RESOURCES;
12410 }
12411 lpfc_in_buf_free(vport->phba, d_buf);
12412 continue;
12413 }
12414 iocbq->context2 = d_buf;
12415 iocbq->context3 = NULL;
12416 iocbq->iocb.ulpBdeCount = 1;
12417 iocbq->iocb.un.cont64[0].tus.f.bdeSize =
12418 LPFC_DATA_BUF_SIZE;
James Smart8fa38512009-07-19 10:01:03 -040012419 first_iocbq->iocb.unsli3.rcvsli3.acc_len +=
James Smart4d9ab992009-10-02 15:16:39 -040012420 bf_get(lpfc_rcqe_length,
12421 &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
James Smart4f774512009-05-22 14:52:35 -040012422 iocbq->iocb.un.rcvels.remoteID = sid;
12423 list_add_tail(&iocbq->list, &first_iocbq->list);
12424 }
12425 }
12426 return first_iocbq;
12427}
12428
James Smart6669f9b2009-10-02 15:16:45 -040012429static void
12430lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *vport,
12431 struct hbq_dmabuf *seq_dmabuf)
12432{
12433 struct fc_frame_header *fc_hdr;
12434 struct lpfc_iocbq *iocbq, *curr_iocb, *next_iocb;
12435 struct lpfc_hba *phba = vport->phba;
12436
12437 fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
12438 iocbq = lpfc_prep_seq(vport, seq_dmabuf);
12439 if (!iocbq) {
12440 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12441 "2707 Ring %d handler: Failed to allocate "
12442 "iocb Rctl x%x Type x%x received\n",
12443 LPFC_ELS_RING,
12444 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
12445 return;
12446 }
12447 if (!lpfc_complete_unsol_iocb(phba,
12448 &phba->sli.ring[LPFC_ELS_RING],
12449 iocbq, fc_hdr->fh_r_ctl,
12450 fc_hdr->fh_type))
12451 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
12452 "2540 Ring %d handler: unexpected Rctl "
12453 "x%x Type x%x received\n",
12454 LPFC_ELS_RING,
12455 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
12456
12457 /* Free iocb created in lpfc_prep_seq */
12458 list_for_each_entry_safe(curr_iocb, next_iocb,
12459 &iocbq->list, list) {
12460 list_del_init(&curr_iocb->list);
12461 lpfc_sli_release_iocbq(phba, curr_iocb);
12462 }
12463 lpfc_sli_release_iocbq(phba, iocbq);
12464}
12465
James Smart4f774512009-05-22 14:52:35 -040012466/**
12467 * lpfc_sli4_handle_received_buffer - Handle received buffers from firmware
12468 * @phba: Pointer to HBA context object.
12469 *
12470 * This function is called with no lock held. This function processes all
12471 * the received buffers and gives it to upper layers when a received buffer
12472 * indicates that it is the final frame in the sequence. The interrupt
12473 * service routine processes received buffers at interrupt contexts and adds
12474 * received dma buffers to the rb_pend_list queue and signals the worker thread.
12475 * Worker thread calls lpfc_sli4_handle_received_buffer, which will call the
12476 * appropriate receive function when the final frame in a sequence is received.
12477 **/
James Smart4d9ab992009-10-02 15:16:39 -040012478void
12479lpfc_sli4_handle_received_buffer(struct lpfc_hba *phba,
12480 struct hbq_dmabuf *dmabuf)
James Smart4f774512009-05-22 14:52:35 -040012481{
James Smart4d9ab992009-10-02 15:16:39 -040012482 struct hbq_dmabuf *seq_dmabuf;
James Smart4f774512009-05-22 14:52:35 -040012483 struct fc_frame_header *fc_hdr;
12484 struct lpfc_vport *vport;
12485 uint32_t fcfi;
James Smart4f774512009-05-22 14:52:35 -040012486
James Smart4f774512009-05-22 14:52:35 -040012487 /* Process each received buffer */
James Smart4d9ab992009-10-02 15:16:39 -040012488 fc_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
12489 /* check to see if this a valid type of frame */
12490 if (lpfc_fc_frame_check(phba, fc_hdr)) {
12491 lpfc_in_buf_free(phba, &dmabuf->dbuf);
12492 return;
12493 }
12494 fcfi = bf_get(lpfc_rcqe_fcf_id, &dmabuf->cq_event.cqe.rcqe_cmpl);
12495 vport = lpfc_fc_frame_to_vport(phba, fc_hdr, fcfi);
James Smartc8685952009-11-18 15:39:16 -050012496 if (!vport || !(vport->vpi_state & LPFC_VPI_REGISTERED)) {
James Smart4d9ab992009-10-02 15:16:39 -040012497 /* throw out the frame */
12498 lpfc_in_buf_free(phba, &dmabuf->dbuf);
12499 return;
12500 }
James Smart6669f9b2009-10-02 15:16:45 -040012501 /* Handle the basic abort sequence (BA_ABTS) event */
12502 if (fc_hdr->fh_r_ctl == FC_RCTL_BA_ABTS) {
12503 lpfc_sli4_handle_unsol_abort(vport, dmabuf);
12504 return;
12505 }
12506
James Smart4d9ab992009-10-02 15:16:39 -040012507 /* Link this frame */
12508 seq_dmabuf = lpfc_fc_frame_add(vport, dmabuf);
12509 if (!seq_dmabuf) {
12510 /* unable to add frame to vport - throw it out */
12511 lpfc_in_buf_free(phba, &dmabuf->dbuf);
12512 return;
12513 }
12514 /* If not last frame in sequence continue processing frames. */
James Smartdef9c7a2009-12-21 17:02:28 -050012515 if (!lpfc_seq_complete(seq_dmabuf))
James Smart4d9ab992009-10-02 15:16:39 -040012516 return;
James Smartdef9c7a2009-12-21 17:02:28 -050012517
James Smart6669f9b2009-10-02 15:16:45 -040012518 /* Send the complete sequence to the upper layer protocol */
12519 lpfc_sli4_send_seq_to_ulp(vport, seq_dmabuf);
James Smart4f774512009-05-22 14:52:35 -040012520}
James Smart6fb120a2009-05-22 14:52:59 -040012521
12522/**
12523 * lpfc_sli4_post_all_rpi_hdrs - Post the rpi header memory region to the port
12524 * @phba: pointer to lpfc hba data structure.
12525 *
12526 * This routine is invoked to post rpi header templates to the
12527 * HBA consistent with the SLI-4 interface spec. This routine
James Smart49198b32010-04-06 15:04:33 -040012528 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
12529 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
James Smart6fb120a2009-05-22 14:52:59 -040012530 *
12531 * This routine does not require any locks. It's usage is expected
12532 * to be driver load or reset recovery when the driver is
12533 * sequential.
12534 *
12535 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020012536 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -040012537 * -EIO - The mailbox failed to complete successfully.
James Smart6fb120a2009-05-22 14:52:59 -040012538 * When this error occurs, the driver is not guaranteed
12539 * to have any rpi regions posted to the device and
12540 * must either attempt to repost the regions or take a
12541 * fatal error.
12542 **/
12543int
12544lpfc_sli4_post_all_rpi_hdrs(struct lpfc_hba *phba)
12545{
12546 struct lpfc_rpi_hdr *rpi_page;
12547 uint32_t rc = 0;
12548
12549 /* Post all rpi memory regions to the port. */
12550 list_for_each_entry(rpi_page, &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
12551 rc = lpfc_sli4_post_rpi_hdr(phba, rpi_page);
12552 if (rc != MBX_SUCCESS) {
12553 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12554 "2008 Error %d posting all rpi "
12555 "headers\n", rc);
12556 rc = -EIO;
12557 break;
12558 }
12559 }
12560
12561 return rc;
12562}
12563
12564/**
12565 * lpfc_sli4_post_rpi_hdr - Post an rpi header memory region to the port
12566 * @phba: pointer to lpfc hba data structure.
12567 * @rpi_page: pointer to the rpi memory region.
12568 *
12569 * This routine is invoked to post a single rpi header to the
12570 * HBA consistent with the SLI-4 interface spec. This memory region
12571 * maps up to 64 rpi context regions.
12572 *
12573 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020012574 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -040012575 * -ENOMEM - No available memory
12576 * -EIO - The mailbox failed to complete successfully.
James Smart6fb120a2009-05-22 14:52:59 -040012577 **/
12578int
12579lpfc_sli4_post_rpi_hdr(struct lpfc_hba *phba, struct lpfc_rpi_hdr *rpi_page)
12580{
12581 LPFC_MBOXQ_t *mboxq;
12582 struct lpfc_mbx_post_hdr_tmpl *hdr_tmpl;
12583 uint32_t rc = 0;
12584 uint32_t mbox_tmo;
12585 uint32_t shdr_status, shdr_add_status;
12586 union lpfc_sli4_cfg_shdr *shdr;
12587
12588 /* The port is notified of the header region via a mailbox command. */
12589 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12590 if (!mboxq) {
12591 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12592 "2001 Unable to allocate memory for issuing "
12593 "SLI_CONFIG_SPECIAL mailbox command\n");
12594 return -ENOMEM;
12595 }
12596
12597 /* Post all rpi memory regions to the port. */
12598 hdr_tmpl = &mboxq->u.mqe.un.hdr_tmpl;
12599 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_SLI4_CONFIG);
12600 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
12601 LPFC_MBOX_OPCODE_FCOE_POST_HDR_TEMPLATE,
12602 sizeof(struct lpfc_mbx_post_hdr_tmpl) -
James Smartfedd3b72011-02-16 12:39:24 -050012603 sizeof(struct lpfc_sli4_cfg_mhdr),
12604 LPFC_SLI4_MBX_EMBED);
James Smart6fb120a2009-05-22 14:52:59 -040012605 bf_set(lpfc_mbx_post_hdr_tmpl_page_cnt,
12606 hdr_tmpl, rpi_page->page_count);
12607 bf_set(lpfc_mbx_post_hdr_tmpl_rpi_offset, hdr_tmpl,
12608 rpi_page->start_rpi);
12609 hdr_tmpl->rpi_paddr_lo = putPaddrLow(rpi_page->dmabuf->phys);
12610 hdr_tmpl->rpi_paddr_hi = putPaddrHigh(rpi_page->dmabuf->phys);
James Smartf1126682009-06-10 17:22:44 -040012611 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
James Smart6fb120a2009-05-22 14:52:59 -040012612 shdr = (union lpfc_sli4_cfg_shdr *) &hdr_tmpl->header.cfg_shdr;
12613 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12614 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12615 if (rc != MBX_TIMEOUT)
12616 mempool_free(mboxq, phba->mbox_mem_pool);
12617 if (shdr_status || shdr_add_status || rc) {
12618 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12619 "2514 POST_RPI_HDR mailbox failed with "
12620 "status x%x add_status x%x, mbx status x%x\n",
12621 shdr_status, shdr_add_status, rc);
12622 rc = -ENXIO;
12623 }
12624 return rc;
12625}
12626
12627/**
12628 * lpfc_sli4_alloc_rpi - Get an available rpi in the device's range
12629 * @phba: pointer to lpfc hba data structure.
12630 *
12631 * This routine is invoked to post rpi header templates to the
12632 * HBA consistent with the SLI-4 interface spec. This routine
James Smart49198b32010-04-06 15:04:33 -040012633 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
12634 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
James Smart6fb120a2009-05-22 14:52:59 -040012635 *
12636 * Returns
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020012637 * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
James Smart6fb120a2009-05-22 14:52:59 -040012638 * LPFC_RPI_ALLOC_ERROR if no rpis are available.
12639 **/
12640int
12641lpfc_sli4_alloc_rpi(struct lpfc_hba *phba)
12642{
12643 int rpi;
12644 uint16_t max_rpi, rpi_base, rpi_limit;
12645 uint16_t rpi_remaining;
12646 struct lpfc_rpi_hdr *rpi_hdr;
12647
12648 max_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
12649 rpi_base = phba->sli4_hba.max_cfg_param.rpi_base;
12650 rpi_limit = phba->sli4_hba.next_rpi;
12651
12652 /*
12653 * The valid rpi range is not guaranteed to be zero-based. Start
12654 * the search at the rpi_base as reported by the port.
12655 */
12656 spin_lock_irq(&phba->hbalock);
12657 rpi = find_next_zero_bit(phba->sli4_hba.rpi_bmask, rpi_limit, rpi_base);
12658 if (rpi >= rpi_limit || rpi < rpi_base)
12659 rpi = LPFC_RPI_ALLOC_ERROR;
12660 else {
12661 set_bit(rpi, phba->sli4_hba.rpi_bmask);
12662 phba->sli4_hba.max_cfg_param.rpi_used++;
12663 phba->sli4_hba.rpi_count++;
12664 }
12665
12666 /*
12667 * Don't try to allocate more rpi header regions if the device limit
12668 * on available rpis max has been exhausted.
12669 */
12670 if ((rpi == LPFC_RPI_ALLOC_ERROR) &&
12671 (phba->sli4_hba.rpi_count >= max_rpi)) {
12672 spin_unlock_irq(&phba->hbalock);
12673 return rpi;
12674 }
12675
12676 /*
12677 * If the driver is running low on rpi resources, allocate another
12678 * page now. Note that the next_rpi value is used because
12679 * it represents how many are actually in use whereas max_rpi notes
12680 * how many are supported max by the device.
12681 */
12682 rpi_remaining = phba->sli4_hba.next_rpi - rpi_base -
12683 phba->sli4_hba.rpi_count;
12684 spin_unlock_irq(&phba->hbalock);
12685 if (rpi_remaining < LPFC_RPI_LOW_WATER_MARK) {
12686 rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
12687 if (!rpi_hdr) {
12688 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12689 "2002 Error Could not grow rpi "
12690 "count\n");
12691 } else {
12692 lpfc_sli4_post_rpi_hdr(phba, rpi_hdr);
12693 }
12694 }
12695
12696 return rpi;
12697}
12698
12699/**
12700 * lpfc_sli4_free_rpi - Release an rpi for reuse.
12701 * @phba: pointer to lpfc hba data structure.
12702 *
12703 * This routine is invoked to release an rpi to the pool of
12704 * available rpis maintained by the driver.
12705 **/
12706void
James Smartd7c47992010-06-08 18:31:54 -040012707__lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
12708{
12709 if (test_and_clear_bit(rpi, phba->sli4_hba.rpi_bmask)) {
12710 phba->sli4_hba.rpi_count--;
12711 phba->sli4_hba.max_cfg_param.rpi_used--;
12712 }
12713}
12714
12715/**
12716 * lpfc_sli4_free_rpi - Release an rpi for reuse.
12717 * @phba: pointer to lpfc hba data structure.
12718 *
12719 * This routine is invoked to release an rpi to the pool of
12720 * available rpis maintained by the driver.
12721 **/
12722void
James Smart6fb120a2009-05-22 14:52:59 -040012723lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
12724{
12725 spin_lock_irq(&phba->hbalock);
James Smartd7c47992010-06-08 18:31:54 -040012726 __lpfc_sli4_free_rpi(phba, rpi);
James Smart6fb120a2009-05-22 14:52:59 -040012727 spin_unlock_irq(&phba->hbalock);
12728}
12729
12730/**
12731 * lpfc_sli4_remove_rpis - Remove the rpi bitmask region
12732 * @phba: pointer to lpfc hba data structure.
12733 *
12734 * This routine is invoked to remove the memory region that
12735 * provided rpi via a bitmask.
12736 **/
12737void
12738lpfc_sli4_remove_rpis(struct lpfc_hba *phba)
12739{
12740 kfree(phba->sli4_hba.rpi_bmask);
12741}
12742
12743/**
12744 * lpfc_sli4_resume_rpi - Remove the rpi bitmask region
12745 * @phba: pointer to lpfc hba data structure.
12746 *
12747 * This routine is invoked to remove the memory region that
12748 * provided rpi via a bitmask.
12749 **/
12750int
12751lpfc_sli4_resume_rpi(struct lpfc_nodelist *ndlp)
12752{
12753 LPFC_MBOXQ_t *mboxq;
12754 struct lpfc_hba *phba = ndlp->phba;
12755 int rc;
12756
12757 /* The port is notified of the header region via a mailbox command. */
12758 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12759 if (!mboxq)
12760 return -ENOMEM;
12761
12762 /* Post all rpi memory regions to the port. */
12763 lpfc_resume_rpi(mboxq, ndlp);
12764 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
12765 if (rc == MBX_NOT_FINISHED) {
12766 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12767 "2010 Resume RPI Mailbox failed "
12768 "status %d, mbxStatus x%x\n", rc,
12769 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
12770 mempool_free(mboxq, phba->mbox_mem_pool);
12771 return -EIO;
12772 }
12773 return 0;
12774}
12775
12776/**
12777 * lpfc_sli4_init_vpi - Initialize a vpi with the port
James Smart76a95d72010-11-20 23:11:48 -050012778 * @vport: Pointer to the vport for which the vpi is being initialized
James Smart6fb120a2009-05-22 14:52:59 -040012779 *
James Smart76a95d72010-11-20 23:11:48 -050012780 * This routine is invoked to activate a vpi with the port.
James Smart6fb120a2009-05-22 14:52:59 -040012781 *
12782 * Returns:
12783 * 0 success
12784 * -Evalue otherwise
12785 **/
12786int
James Smart76a95d72010-11-20 23:11:48 -050012787lpfc_sli4_init_vpi(struct lpfc_vport *vport)
James Smart6fb120a2009-05-22 14:52:59 -040012788{
12789 LPFC_MBOXQ_t *mboxq;
12790 int rc = 0;
James Smart6a9c52c2009-10-02 15:16:51 -040012791 int retval = MBX_SUCCESS;
James Smart6fb120a2009-05-22 14:52:59 -040012792 uint32_t mbox_tmo;
James Smart76a95d72010-11-20 23:11:48 -050012793 struct lpfc_hba *phba = vport->phba;
James Smart6fb120a2009-05-22 14:52:59 -040012794 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12795 if (!mboxq)
12796 return -ENOMEM;
James Smart76a95d72010-11-20 23:11:48 -050012797 lpfc_init_vpi(phba, mboxq, vport->vpi);
James Smart6fb120a2009-05-22 14:52:59 -040012798 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_INIT_VPI);
12799 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
James Smart6fb120a2009-05-22 14:52:59 -040012800 if (rc != MBX_SUCCESS) {
James Smart76a95d72010-11-20 23:11:48 -050012801 lpfc_printf_vlog(vport, KERN_ERR, LOG_SLI,
James Smart6fb120a2009-05-22 14:52:59 -040012802 "2022 INIT VPI Mailbox failed "
12803 "status %d, mbxStatus x%x\n", rc,
12804 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
James Smart6a9c52c2009-10-02 15:16:51 -040012805 retval = -EIO;
James Smart6fb120a2009-05-22 14:52:59 -040012806 }
James Smart6a9c52c2009-10-02 15:16:51 -040012807 if (rc != MBX_TIMEOUT)
James Smart76a95d72010-11-20 23:11:48 -050012808 mempool_free(mboxq, vport->phba->mbox_mem_pool);
James Smart6a9c52c2009-10-02 15:16:51 -040012809
12810 return retval;
James Smart6fb120a2009-05-22 14:52:59 -040012811}
12812
12813/**
12814 * lpfc_mbx_cmpl_add_fcf_record - add fcf mbox completion handler.
12815 * @phba: pointer to lpfc hba data structure.
12816 * @mboxq: Pointer to mailbox object.
12817 *
12818 * This routine is invoked to manually add a single FCF record. The caller
12819 * must pass a completely initialized FCF_Record. This routine takes
12820 * care of the nonembedded mailbox operations.
12821 **/
12822static void
12823lpfc_mbx_cmpl_add_fcf_record(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
12824{
12825 void *virt_addr;
12826 union lpfc_sli4_cfg_shdr *shdr;
12827 uint32_t shdr_status, shdr_add_status;
12828
12829 virt_addr = mboxq->sge_array->addr[0];
12830 /* The IOCTL status is embedded in the mailbox subheader. */
12831 shdr = (union lpfc_sli4_cfg_shdr *) virt_addr;
12832 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12833 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12834
12835 if ((shdr_status || shdr_add_status) &&
12836 (shdr_status != STATUS_FCF_IN_USE))
12837 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12838 "2558 ADD_FCF_RECORD mailbox failed with "
12839 "status x%x add_status x%x\n",
12840 shdr_status, shdr_add_status);
12841
12842 lpfc_sli4_mbox_cmd_free(phba, mboxq);
12843}
12844
12845/**
12846 * lpfc_sli4_add_fcf_record - Manually add an FCF Record.
12847 * @phba: pointer to lpfc hba data structure.
12848 * @fcf_record: pointer to the initialized fcf record to add.
12849 *
12850 * This routine is invoked to manually add a single FCF record. The caller
12851 * must pass a completely initialized FCF_Record. This routine takes
12852 * care of the nonembedded mailbox operations.
12853 **/
12854int
12855lpfc_sli4_add_fcf_record(struct lpfc_hba *phba, struct fcf_record *fcf_record)
12856{
12857 int rc = 0;
12858 LPFC_MBOXQ_t *mboxq;
12859 uint8_t *bytep;
12860 void *virt_addr;
12861 dma_addr_t phys_addr;
12862 struct lpfc_mbx_sge sge;
12863 uint32_t alloc_len, req_len;
12864 uint32_t fcfindex;
12865
12866 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12867 if (!mboxq) {
12868 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12869 "2009 Failed to allocate mbox for ADD_FCF cmd\n");
12870 return -ENOMEM;
12871 }
12872
12873 req_len = sizeof(struct fcf_record) + sizeof(union lpfc_sli4_cfg_shdr) +
12874 sizeof(uint32_t);
12875
12876 /* Allocate DMA memory and set up the non-embedded mailbox command */
12877 alloc_len = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
12878 LPFC_MBOX_OPCODE_FCOE_ADD_FCF,
12879 req_len, LPFC_SLI4_MBX_NEMBED);
12880 if (alloc_len < req_len) {
12881 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12882 "2523 Allocated DMA memory size (x%x) is "
12883 "less than the requested DMA memory "
12884 "size (x%x)\n", alloc_len, req_len);
12885 lpfc_sli4_mbox_cmd_free(phba, mboxq);
12886 return -ENOMEM;
12887 }
12888
12889 /*
12890 * Get the first SGE entry from the non-embedded DMA memory. This
12891 * routine only uses a single SGE.
12892 */
12893 lpfc_sli4_mbx_sge_get(mboxq, 0, &sge);
12894 phys_addr = getPaddr(sge.pa_hi, sge.pa_lo);
James Smart6fb120a2009-05-22 14:52:59 -040012895 virt_addr = mboxq->sge_array->addr[0];
12896 /*
12897 * Configure the FCF record for FCFI 0. This is the driver's
12898 * hardcoded default and gets used in nonFIP mode.
12899 */
12900 fcfindex = bf_get(lpfc_fcf_record_fcf_index, fcf_record);
12901 bytep = virt_addr + sizeof(union lpfc_sli4_cfg_shdr);
12902 lpfc_sli_pcimem_bcopy(&fcfindex, bytep, sizeof(uint32_t));
12903
12904 /*
12905 * Copy the fcf_index and the FCF Record Data. The data starts after
12906 * the FCoE header plus word10. The data copy needs to be endian
12907 * correct.
12908 */
12909 bytep += sizeof(uint32_t);
12910 lpfc_sli_pcimem_bcopy(fcf_record, bytep, sizeof(struct fcf_record));
12911 mboxq->vport = phba->pport;
12912 mboxq->mbox_cmpl = lpfc_mbx_cmpl_add_fcf_record;
12913 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
12914 if (rc == MBX_NOT_FINISHED) {
12915 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12916 "2515 ADD_FCF_RECORD mailbox failed with "
12917 "status 0x%x\n", rc);
12918 lpfc_sli4_mbox_cmd_free(phba, mboxq);
12919 rc = -EIO;
12920 } else
12921 rc = 0;
12922
12923 return rc;
12924}
12925
12926/**
12927 * lpfc_sli4_build_dflt_fcf_record - Build the driver's default FCF Record.
12928 * @phba: pointer to lpfc hba data structure.
12929 * @fcf_record: pointer to the fcf record to write the default data.
12930 * @fcf_index: FCF table entry index.
12931 *
12932 * This routine is invoked to build the driver's default FCF record. The
12933 * values used are hardcoded. This routine handles memory initialization.
12934 *
12935 **/
12936void
12937lpfc_sli4_build_dflt_fcf_record(struct lpfc_hba *phba,
12938 struct fcf_record *fcf_record,
12939 uint16_t fcf_index)
12940{
12941 memset(fcf_record, 0, sizeof(struct fcf_record));
12942 fcf_record->max_rcv_size = LPFC_FCOE_MAX_RCV_SIZE;
12943 fcf_record->fka_adv_period = LPFC_FCOE_FKA_ADV_PER;
12944 fcf_record->fip_priority = LPFC_FCOE_FIP_PRIORITY;
12945 bf_set(lpfc_fcf_record_mac_0, fcf_record, phba->fc_map[0]);
12946 bf_set(lpfc_fcf_record_mac_1, fcf_record, phba->fc_map[1]);
12947 bf_set(lpfc_fcf_record_mac_2, fcf_record, phba->fc_map[2]);
12948 bf_set(lpfc_fcf_record_mac_3, fcf_record, LPFC_FCOE_FCF_MAC3);
12949 bf_set(lpfc_fcf_record_mac_4, fcf_record, LPFC_FCOE_FCF_MAC4);
12950 bf_set(lpfc_fcf_record_mac_5, fcf_record, LPFC_FCOE_FCF_MAC5);
12951 bf_set(lpfc_fcf_record_fc_map_0, fcf_record, phba->fc_map[0]);
12952 bf_set(lpfc_fcf_record_fc_map_1, fcf_record, phba->fc_map[1]);
12953 bf_set(lpfc_fcf_record_fc_map_2, fcf_record, phba->fc_map[2]);
12954 bf_set(lpfc_fcf_record_fcf_valid, fcf_record, 1);
James Smart0c287582009-06-10 17:22:56 -040012955 bf_set(lpfc_fcf_record_fcf_avail, fcf_record, 1);
James Smart6fb120a2009-05-22 14:52:59 -040012956 bf_set(lpfc_fcf_record_fcf_index, fcf_record, fcf_index);
12957 bf_set(lpfc_fcf_record_mac_addr_prov, fcf_record,
12958 LPFC_FCF_FPMA | LPFC_FCF_SPMA);
12959 /* Set the VLAN bit map */
12960 if (phba->valid_vlan) {
12961 fcf_record->vlan_bitmap[phba->vlan_id / 8]
12962 = 1 << (phba->vlan_id % 8);
12963 }
12964}
12965
12966/**
James Smart0c9ab6f2010-02-26 14:15:57 -050012967 * lpfc_sli4_fcf_scan_read_fcf_rec - Read hba fcf record for fcf scan.
James Smart6fb120a2009-05-22 14:52:59 -040012968 * @phba: pointer to lpfc hba data structure.
12969 * @fcf_index: FCF table entry offset.
12970 *
James Smart0c9ab6f2010-02-26 14:15:57 -050012971 * This routine is invoked to scan the entire FCF table by reading FCF
12972 * record and processing it one at a time starting from the @fcf_index
12973 * for initial FCF discovery or fast FCF failover rediscovery.
12974 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030012975 * Return 0 if the mailbox command is submitted successfully, none 0
James Smart0c9ab6f2010-02-26 14:15:57 -050012976 * otherwise.
James Smart6fb120a2009-05-22 14:52:59 -040012977 **/
12978int
James Smart0c9ab6f2010-02-26 14:15:57 -050012979lpfc_sli4_fcf_scan_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
James Smart6fb120a2009-05-22 14:52:59 -040012980{
12981 int rc = 0, error;
12982 LPFC_MBOXQ_t *mboxq;
James Smart6fb120a2009-05-22 14:52:59 -040012983
James Smart32b97932009-07-19 10:01:21 -040012984 phba->fcoe_eventtag_at_fcf_scan = phba->fcoe_eventtag;
James Smart6fb120a2009-05-22 14:52:59 -040012985 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12986 if (!mboxq) {
12987 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12988 "2000 Failed to allocate mbox for "
12989 "READ_FCF cmd\n");
James Smart4d9ab992009-10-02 15:16:39 -040012990 error = -ENOMEM;
James Smart0c9ab6f2010-02-26 14:15:57 -050012991 goto fail_fcf_scan;
James Smart6fb120a2009-05-22 14:52:59 -040012992 }
James Smartecfd03c2010-02-12 14:41:27 -050012993 /* Construct the read FCF record mailbox command */
James Smart0c9ab6f2010-02-26 14:15:57 -050012994 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
James Smartecfd03c2010-02-12 14:41:27 -050012995 if (rc) {
12996 error = -EINVAL;
James Smart0c9ab6f2010-02-26 14:15:57 -050012997 goto fail_fcf_scan;
James Smart6fb120a2009-05-22 14:52:59 -040012998 }
James Smartecfd03c2010-02-12 14:41:27 -050012999 /* Issue the mailbox command asynchronously */
James Smart6fb120a2009-05-22 14:52:59 -040013000 mboxq->vport = phba->pport;
James Smart0c9ab6f2010-02-26 14:15:57 -050013001 mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_scan_read_fcf_rec;
James Smarta93ff372010-10-22 11:06:08 -040013002
13003 spin_lock_irq(&phba->hbalock);
13004 phba->hba_flag |= FCF_TS_INPROG;
13005 spin_unlock_irq(&phba->hbalock);
13006
James Smart6fb120a2009-05-22 14:52:59 -040013007 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
James Smartecfd03c2010-02-12 14:41:27 -050013008 if (rc == MBX_NOT_FINISHED)
James Smart6fb120a2009-05-22 14:52:59 -040013009 error = -EIO;
James Smartecfd03c2010-02-12 14:41:27 -050013010 else {
James Smart38b92ef2010-08-04 16:11:39 -040013011 /* Reset eligible FCF count for new scan */
13012 if (fcf_index == LPFC_FCOE_FCF_GET_FIRST)
James Smart999d8132010-03-15 11:24:56 -040013013 phba->fcf.eligible_fcf_cnt = 0;
James Smart6fb120a2009-05-22 14:52:59 -040013014 error = 0;
James Smart32b97932009-07-19 10:01:21 -040013015 }
James Smart0c9ab6f2010-02-26 14:15:57 -050013016fail_fcf_scan:
James Smart4d9ab992009-10-02 15:16:39 -040013017 if (error) {
13018 if (mboxq)
13019 lpfc_sli4_mbox_cmd_free(phba, mboxq);
James Smarta93ff372010-10-22 11:06:08 -040013020 /* FCF scan failed, clear FCF_TS_INPROG flag */
James Smart4d9ab992009-10-02 15:16:39 -040013021 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -040013022 phba->hba_flag &= ~FCF_TS_INPROG;
James Smart4d9ab992009-10-02 15:16:39 -040013023 spin_unlock_irq(&phba->hbalock);
13024 }
James Smart6fb120a2009-05-22 14:52:59 -040013025 return error;
13026}
James Smarta0c87cb2009-07-19 10:01:10 -040013027
13028/**
James Smarta93ff372010-10-22 11:06:08 -040013029 * lpfc_sli4_fcf_rr_read_fcf_rec - Read hba fcf record for roundrobin fcf.
James Smart0c9ab6f2010-02-26 14:15:57 -050013030 * @phba: pointer to lpfc hba data structure.
13031 * @fcf_index: FCF table entry offset.
13032 *
13033 * This routine is invoked to read an FCF record indicated by @fcf_index
James Smarta93ff372010-10-22 11:06:08 -040013034 * and to use it for FLOGI roundrobin FCF failover.
James Smart0c9ab6f2010-02-26 14:15:57 -050013035 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030013036 * Return 0 if the mailbox command is submitted successfully, none 0
James Smart0c9ab6f2010-02-26 14:15:57 -050013037 * otherwise.
13038 **/
13039int
13040lpfc_sli4_fcf_rr_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
13041{
13042 int rc = 0, error;
13043 LPFC_MBOXQ_t *mboxq;
13044
13045 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
13046 if (!mboxq) {
13047 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
13048 "2763 Failed to allocate mbox for "
13049 "READ_FCF cmd\n");
13050 error = -ENOMEM;
13051 goto fail_fcf_read;
13052 }
13053 /* Construct the read FCF record mailbox command */
13054 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
13055 if (rc) {
13056 error = -EINVAL;
13057 goto fail_fcf_read;
13058 }
13059 /* Issue the mailbox command asynchronously */
13060 mboxq->vport = phba->pport;
13061 mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_rr_read_fcf_rec;
13062 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
13063 if (rc == MBX_NOT_FINISHED)
13064 error = -EIO;
13065 else
13066 error = 0;
13067
13068fail_fcf_read:
13069 if (error && mboxq)
13070 lpfc_sli4_mbox_cmd_free(phba, mboxq);
13071 return error;
13072}
13073
13074/**
13075 * lpfc_sli4_read_fcf_rec - Read hba fcf record for update eligible fcf bmask.
13076 * @phba: pointer to lpfc hba data structure.
13077 * @fcf_index: FCF table entry offset.
13078 *
13079 * This routine is invoked to read an FCF record indicated by @fcf_index to
James Smarta93ff372010-10-22 11:06:08 -040013080 * determine whether it's eligible for FLOGI roundrobin failover list.
James Smart0c9ab6f2010-02-26 14:15:57 -050013081 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030013082 * Return 0 if the mailbox command is submitted successfully, none 0
James Smart0c9ab6f2010-02-26 14:15:57 -050013083 * otherwise.
13084 **/
13085int
13086lpfc_sli4_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
13087{
13088 int rc = 0, error;
13089 LPFC_MBOXQ_t *mboxq;
13090
13091 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
13092 if (!mboxq) {
13093 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
13094 "2758 Failed to allocate mbox for "
13095 "READ_FCF cmd\n");
13096 error = -ENOMEM;
13097 goto fail_fcf_read;
13098 }
13099 /* Construct the read FCF record mailbox command */
13100 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
13101 if (rc) {
13102 error = -EINVAL;
13103 goto fail_fcf_read;
13104 }
13105 /* Issue the mailbox command asynchronously */
13106 mboxq->vport = phba->pport;
13107 mboxq->mbox_cmpl = lpfc_mbx_cmpl_read_fcf_rec;
13108 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
13109 if (rc == MBX_NOT_FINISHED)
13110 error = -EIO;
13111 else
13112 error = 0;
13113
13114fail_fcf_read:
13115 if (error && mboxq)
13116 lpfc_sli4_mbox_cmd_free(phba, mboxq);
13117 return error;
13118}
13119
13120/**
13121 * lpfc_sli4_fcf_rr_next_index_get - Get next eligible fcf record index
13122 * @phba: pointer to lpfc hba data structure.
13123 *
13124 * This routine is to get the next eligible FCF record index in a round
13125 * robin fashion. If the next eligible FCF record index equals to the
James Smarta93ff372010-10-22 11:06:08 -040013126 * initial roundrobin FCF record index, LPFC_FCOE_FCF_NEXT_NONE (0xFFFF)
James Smart0c9ab6f2010-02-26 14:15:57 -050013127 * shall be returned, otherwise, the next eligible FCF record's index
13128 * shall be returned.
13129 **/
13130uint16_t
13131lpfc_sli4_fcf_rr_next_index_get(struct lpfc_hba *phba)
13132{
13133 uint16_t next_fcf_index;
13134
James Smart3804dc82010-07-14 15:31:37 -040013135 /* Search start from next bit of currently registered FCF index */
13136 next_fcf_index = (phba->fcf.current_rec.fcf_indx + 1) %
13137 LPFC_SLI4_FCF_TBL_INDX_MAX;
James Smart0c9ab6f2010-02-26 14:15:57 -050013138 next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
13139 LPFC_SLI4_FCF_TBL_INDX_MAX,
James Smart3804dc82010-07-14 15:31:37 -040013140 next_fcf_index);
13141
James Smart0c9ab6f2010-02-26 14:15:57 -050013142 /* Wrap around condition on phba->fcf.fcf_rr_bmask */
13143 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX)
13144 next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
13145 LPFC_SLI4_FCF_TBL_INDX_MAX, 0);
James Smart0c9ab6f2010-02-26 14:15:57 -050013146
James Smart3804dc82010-07-14 15:31:37 -040013147 /* Check roundrobin failover list empty condition */
13148 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
13149 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
13150 "2844 No roundrobin failover FCF available\n");
13151 return LPFC_FCOE_FCF_NEXT_NONE;
13152 }
13153
James Smart3804dc82010-07-14 15:31:37 -040013154 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040013155 "2845 Get next roundrobin failover FCF (x%x)\n",
13156 next_fcf_index);
13157
James Smart0c9ab6f2010-02-26 14:15:57 -050013158 return next_fcf_index;
13159}
13160
13161/**
13162 * lpfc_sli4_fcf_rr_index_set - Set bmask with eligible fcf record index
13163 * @phba: pointer to lpfc hba data structure.
13164 *
13165 * This routine sets the FCF record index in to the eligible bmask for
James Smarta93ff372010-10-22 11:06:08 -040013166 * roundrobin failover search. It checks to make sure that the index
James Smart0c9ab6f2010-02-26 14:15:57 -050013167 * does not go beyond the range of the driver allocated bmask dimension
13168 * before setting the bit.
13169 *
13170 * Returns 0 if the index bit successfully set, otherwise, it returns
13171 * -EINVAL.
13172 **/
13173int
13174lpfc_sli4_fcf_rr_index_set(struct lpfc_hba *phba, uint16_t fcf_index)
13175{
13176 if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
13177 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040013178 "2610 FCF (x%x) reached driver's book "
13179 "keeping dimension:x%x\n",
James Smart0c9ab6f2010-02-26 14:15:57 -050013180 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
13181 return -EINVAL;
13182 }
13183 /* Set the eligible FCF record index bmask */
13184 set_bit(fcf_index, phba->fcf.fcf_rr_bmask);
13185
James Smart3804dc82010-07-14 15:31:37 -040013186 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040013187 "2790 Set FCF (x%x) to roundrobin FCF failover "
James Smart3804dc82010-07-14 15:31:37 -040013188 "bmask\n", fcf_index);
13189
James Smart0c9ab6f2010-02-26 14:15:57 -050013190 return 0;
13191}
13192
13193/**
James Smart3804dc82010-07-14 15:31:37 -040013194 * lpfc_sli4_fcf_rr_index_clear - Clear bmask from eligible fcf record index
James Smart0c9ab6f2010-02-26 14:15:57 -050013195 * @phba: pointer to lpfc hba data structure.
13196 *
13197 * This routine clears the FCF record index from the eligible bmask for
James Smarta93ff372010-10-22 11:06:08 -040013198 * roundrobin failover search. It checks to make sure that the index
James Smart0c9ab6f2010-02-26 14:15:57 -050013199 * does not go beyond the range of the driver allocated bmask dimension
13200 * before clearing the bit.
13201 **/
13202void
13203lpfc_sli4_fcf_rr_index_clear(struct lpfc_hba *phba, uint16_t fcf_index)
13204{
13205 if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
13206 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040013207 "2762 FCF (x%x) reached driver's book "
13208 "keeping dimension:x%x\n",
James Smart0c9ab6f2010-02-26 14:15:57 -050013209 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
13210 return;
13211 }
13212 /* Clear the eligible FCF record index bmask */
13213 clear_bit(fcf_index, phba->fcf.fcf_rr_bmask);
James Smart3804dc82010-07-14 15:31:37 -040013214
13215 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040013216 "2791 Clear FCF (x%x) from roundrobin failover "
James Smart3804dc82010-07-14 15:31:37 -040013217 "bmask\n", fcf_index);
James Smart0c9ab6f2010-02-26 14:15:57 -050013218}
13219
13220/**
James Smartecfd03c2010-02-12 14:41:27 -050013221 * lpfc_mbx_cmpl_redisc_fcf_table - completion routine for rediscover FCF table
13222 * @phba: pointer to lpfc hba data structure.
13223 *
13224 * This routine is the completion routine for the rediscover FCF table mailbox
13225 * command. If the mailbox command returned failure, it will try to stop the
13226 * FCF rediscover wait timer.
13227 **/
13228void
13229lpfc_mbx_cmpl_redisc_fcf_table(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
13230{
13231 struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
13232 uint32_t shdr_status, shdr_add_status;
13233
13234 redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
13235
13236 shdr_status = bf_get(lpfc_mbox_hdr_status,
13237 &redisc_fcf->header.cfg_shdr.response);
13238 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
13239 &redisc_fcf->header.cfg_shdr.response);
13240 if (shdr_status || shdr_add_status) {
James Smart0c9ab6f2010-02-26 14:15:57 -050013241 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smartecfd03c2010-02-12 14:41:27 -050013242 "2746 Requesting for FCF rediscovery failed "
13243 "status x%x add_status x%x\n",
13244 shdr_status, shdr_add_status);
James Smart0c9ab6f2010-02-26 14:15:57 -050013245 if (phba->fcf.fcf_flag & FCF_ACVL_DISC) {
James Smartfc2b9892010-02-26 14:15:29 -050013246 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -050013247 phba->fcf.fcf_flag &= ~FCF_ACVL_DISC;
James Smartfc2b9892010-02-26 14:15:29 -050013248 spin_unlock_irq(&phba->hbalock);
13249 /*
13250 * CVL event triggered FCF rediscover request failed,
13251 * last resort to re-try current registered FCF entry.
13252 */
13253 lpfc_retry_pport_discovery(phba);
13254 } else {
13255 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -050013256 phba->fcf.fcf_flag &= ~FCF_DEAD_DISC;
James Smartfc2b9892010-02-26 14:15:29 -050013257 spin_unlock_irq(&phba->hbalock);
13258 /*
13259 * DEAD FCF event triggered FCF rediscover request
13260 * failed, last resort to fail over as a link down
13261 * to FCF registration.
13262 */
13263 lpfc_sli4_fcf_dead_failthrough(phba);
13264 }
James Smart0c9ab6f2010-02-26 14:15:57 -050013265 } else {
13266 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040013267 "2775 Start FCF rediscover quiescent timer\n");
James Smartecfd03c2010-02-12 14:41:27 -050013268 /*
13269 * Start FCF rediscovery wait timer for pending FCF
13270 * before rescan FCF record table.
13271 */
13272 lpfc_fcf_redisc_wait_start_timer(phba);
James Smart0c9ab6f2010-02-26 14:15:57 -050013273 }
James Smartecfd03c2010-02-12 14:41:27 -050013274
13275 mempool_free(mbox, phba->mbox_mem_pool);
13276}
13277
13278/**
James Smart3804dc82010-07-14 15:31:37 -040013279 * lpfc_sli4_redisc_fcf_table - Request to rediscover entire FCF table by port.
James Smartecfd03c2010-02-12 14:41:27 -050013280 * @phba: pointer to lpfc hba data structure.
13281 *
13282 * This routine is invoked to request for rediscovery of the entire FCF table
13283 * by the port.
13284 **/
13285int
13286lpfc_sli4_redisc_fcf_table(struct lpfc_hba *phba)
13287{
13288 LPFC_MBOXQ_t *mbox;
13289 struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
13290 int rc, length;
13291
James Smart0c9ab6f2010-02-26 14:15:57 -050013292 /* Cancel retry delay timers to all vports before FCF rediscover */
13293 lpfc_cancel_all_vport_retry_delay_timer(phba);
13294
James Smartecfd03c2010-02-12 14:41:27 -050013295 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
13296 if (!mbox) {
13297 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13298 "2745 Failed to allocate mbox for "
13299 "requesting FCF rediscover.\n");
13300 return -ENOMEM;
13301 }
13302
13303 length = (sizeof(struct lpfc_mbx_redisc_fcf_tbl) -
13304 sizeof(struct lpfc_sli4_cfg_mhdr));
13305 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
13306 LPFC_MBOX_OPCODE_FCOE_REDISCOVER_FCF,
13307 length, LPFC_SLI4_MBX_EMBED);
13308
13309 redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
13310 /* Set count to 0 for invalidating the entire FCF database */
13311 bf_set(lpfc_mbx_redisc_fcf_count, redisc_fcf, 0);
13312
13313 /* Issue the mailbox command asynchronously */
13314 mbox->vport = phba->pport;
13315 mbox->mbox_cmpl = lpfc_mbx_cmpl_redisc_fcf_table;
13316 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
13317
13318 if (rc == MBX_NOT_FINISHED) {
13319 mempool_free(mbox, phba->mbox_mem_pool);
13320 return -EIO;
13321 }
13322 return 0;
13323}
13324
13325/**
James Smartfc2b9892010-02-26 14:15:29 -050013326 * lpfc_sli4_fcf_dead_failthrough - Failthrough routine to fcf dead event
13327 * @phba: pointer to lpfc hba data structure.
13328 *
13329 * This function is the failover routine as a last resort to the FCF DEAD
13330 * event when driver failed to perform fast FCF failover.
13331 **/
13332void
13333lpfc_sli4_fcf_dead_failthrough(struct lpfc_hba *phba)
13334{
13335 uint32_t link_state;
13336
13337 /*
13338 * Last resort as FCF DEAD event failover will treat this as
13339 * a link down, but save the link state because we don't want
13340 * it to be changed to Link Down unless it is already down.
13341 */
13342 link_state = phba->link_state;
13343 lpfc_linkdown(phba);
13344 phba->link_state = link_state;
13345
13346 /* Unregister FCF if no devices connected to it */
13347 lpfc_unregister_unused_fcf(phba);
13348}
13349
13350/**
James Smarta0c87cb2009-07-19 10:01:10 -040013351 * lpfc_sli_read_link_ste - Read region 23 to decide if link is disabled.
13352 * @phba: pointer to lpfc hba data structure.
13353 *
13354 * This function read region 23 and parse TLV for port status to
13355 * decide if the user disaled the port. If the TLV indicates the
13356 * port is disabled, the hba_flag is set accordingly.
13357 **/
13358void
13359lpfc_sli_read_link_ste(struct lpfc_hba *phba)
13360{
13361 LPFC_MBOXQ_t *pmb = NULL;
13362 MAILBOX_t *mb;
13363 uint8_t *rgn23_data = NULL;
13364 uint32_t offset = 0, data_size, sub_tlv_len, tlv_offset;
13365 int rc;
13366
13367 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
13368 if (!pmb) {
13369 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13370 "2600 lpfc_sli_read_serdes_param failed to"
13371 " allocate mailbox memory\n");
13372 goto out;
13373 }
13374 mb = &pmb->u.mb;
13375
13376 /* Get adapter Region 23 data */
13377 rgn23_data = kzalloc(DMP_RGN23_SIZE, GFP_KERNEL);
13378 if (!rgn23_data)
13379 goto out;
13380
13381 do {
13382 lpfc_dump_mem(phba, pmb, offset, DMP_REGION_23);
13383 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
13384
13385 if (rc != MBX_SUCCESS) {
13386 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
13387 "2601 lpfc_sli_read_link_ste failed to"
13388 " read config region 23 rc 0x%x Status 0x%x\n",
13389 rc, mb->mbxStatus);
13390 mb->un.varDmp.word_cnt = 0;
13391 }
13392 /*
13393 * dump mem may return a zero when finished or we got a
13394 * mailbox error, either way we are done.
13395 */
13396 if (mb->un.varDmp.word_cnt == 0)
13397 break;
13398 if (mb->un.varDmp.word_cnt > DMP_RGN23_SIZE - offset)
13399 mb->un.varDmp.word_cnt = DMP_RGN23_SIZE - offset;
13400
13401 lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
13402 rgn23_data + offset,
13403 mb->un.varDmp.word_cnt);
13404 offset += mb->un.varDmp.word_cnt;
13405 } while (mb->un.varDmp.word_cnt && offset < DMP_RGN23_SIZE);
13406
13407 data_size = offset;
13408 offset = 0;
13409
13410 if (!data_size)
13411 goto out;
13412
13413 /* Check the region signature first */
13414 if (memcmp(&rgn23_data[offset], LPFC_REGION23_SIGNATURE, 4)) {
13415 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13416 "2619 Config region 23 has bad signature\n");
13417 goto out;
13418 }
13419 offset += 4;
13420
13421 /* Check the data structure version */
13422 if (rgn23_data[offset] != LPFC_REGION23_VERSION) {
13423 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13424 "2620 Config region 23 has bad version\n");
13425 goto out;
13426 }
13427 offset += 4;
13428
13429 /* Parse TLV entries in the region */
13430 while (offset < data_size) {
13431 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC)
13432 break;
13433 /*
13434 * If the TLV is not driver specific TLV or driver id is
13435 * not linux driver id, skip the record.
13436 */
13437 if ((rgn23_data[offset] != DRIVER_SPECIFIC_TYPE) ||
13438 (rgn23_data[offset + 2] != LINUX_DRIVER_ID) ||
13439 (rgn23_data[offset + 3] != 0)) {
13440 offset += rgn23_data[offset + 1] * 4 + 4;
13441 continue;
13442 }
13443
13444 /* Driver found a driver specific TLV in the config region */
13445 sub_tlv_len = rgn23_data[offset + 1] * 4;
13446 offset += 4;
13447 tlv_offset = 0;
13448
13449 /*
13450 * Search for configured port state sub-TLV.
13451 */
13452 while ((offset < data_size) &&
13453 (tlv_offset < sub_tlv_len)) {
13454 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC) {
13455 offset += 4;
13456 tlv_offset += 4;
13457 break;
13458 }
13459 if (rgn23_data[offset] != PORT_STE_TYPE) {
13460 offset += rgn23_data[offset + 1] * 4 + 4;
13461 tlv_offset += rgn23_data[offset + 1] * 4 + 4;
13462 continue;
13463 }
13464
13465 /* This HBA contains PORT_STE configured */
13466 if (!rgn23_data[offset + 2])
13467 phba->hba_flag |= LINK_DISABLED;
13468
13469 goto out;
13470 }
13471 }
13472out:
13473 if (pmb)
13474 mempool_free(pmb, phba->mbox_mem_pool);
13475 kfree(rgn23_data);
13476 return;
13477}
James Smart695a8142010-01-26 23:08:03 -050013478
13479/**
13480 * lpfc_cleanup_pending_mbox - Free up vport discovery mailbox commands.
13481 * @vport: pointer to vport data structure.
13482 *
13483 * This function iterate through the mailboxq and clean up all REG_LOGIN
13484 * and REG_VPI mailbox commands associated with the vport. This function
13485 * is called when driver want to restart discovery of the vport due to
13486 * a Clear Virtual Link event.
13487 **/
13488void
13489lpfc_cleanup_pending_mbox(struct lpfc_vport *vport)
13490{
13491 struct lpfc_hba *phba = vport->phba;
13492 LPFC_MBOXQ_t *mb, *nextmb;
13493 struct lpfc_dmabuf *mp;
James Smart78730cf2010-04-06 15:06:30 -040013494 struct lpfc_nodelist *ndlp;
James Smartd439d282010-09-29 11:18:45 -040013495 struct lpfc_nodelist *act_mbx_ndlp = NULL;
James Smart589a52d2010-07-14 15:30:54 -040013496 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smartd439d282010-09-29 11:18:45 -040013497 LIST_HEAD(mbox_cmd_list);
James Smart63e801c2010-11-20 23:14:19 -050013498 uint8_t restart_loop;
James Smart695a8142010-01-26 23:08:03 -050013499
James Smartd439d282010-09-29 11:18:45 -040013500 /* Clean up internally queued mailbox commands with the vport */
James Smart695a8142010-01-26 23:08:03 -050013501 spin_lock_irq(&phba->hbalock);
13502 list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
13503 if (mb->vport != vport)
13504 continue;
13505
13506 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
13507 (mb->u.mb.mbxCommand != MBX_REG_VPI))
13508 continue;
13509
James Smartd439d282010-09-29 11:18:45 -040013510 list_del(&mb->list);
13511 list_add_tail(&mb->list, &mbox_cmd_list);
13512 }
13513 /* Clean up active mailbox command with the vport */
13514 mb = phba->sli.mbox_active;
13515 if (mb && (mb->vport == vport)) {
13516 if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) ||
13517 (mb->u.mb.mbxCommand == MBX_REG_VPI))
13518 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
13519 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
13520 act_mbx_ndlp = (struct lpfc_nodelist *)mb->context2;
13521 /* Put reference count for delayed processing */
13522 act_mbx_ndlp = lpfc_nlp_get(act_mbx_ndlp);
13523 /* Unregister the RPI when mailbox complete */
13524 mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
13525 }
13526 }
James Smart63e801c2010-11-20 23:14:19 -050013527 /* Cleanup any mailbox completions which are not yet processed */
13528 do {
13529 restart_loop = 0;
13530 list_for_each_entry(mb, &phba->sli.mboxq_cmpl, list) {
13531 /*
13532 * If this mailox is already processed or it is
13533 * for another vport ignore it.
13534 */
13535 if ((mb->vport != vport) ||
13536 (mb->mbox_flag & LPFC_MBX_IMED_UNREG))
13537 continue;
13538
13539 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
13540 (mb->u.mb.mbxCommand != MBX_REG_VPI))
13541 continue;
13542
13543 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
13544 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
13545 ndlp = (struct lpfc_nodelist *)mb->context2;
13546 /* Unregister the RPI when mailbox complete */
13547 mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
13548 restart_loop = 1;
13549 spin_unlock_irq(&phba->hbalock);
13550 spin_lock(shost->host_lock);
13551 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
13552 spin_unlock(shost->host_lock);
13553 spin_lock_irq(&phba->hbalock);
13554 break;
13555 }
13556 }
13557 } while (restart_loop);
13558
James Smartd439d282010-09-29 11:18:45 -040013559 spin_unlock_irq(&phba->hbalock);
13560
13561 /* Release the cleaned-up mailbox commands */
13562 while (!list_empty(&mbox_cmd_list)) {
13563 list_remove_head(&mbox_cmd_list, mb, LPFC_MBOXQ_t, list);
James Smart695a8142010-01-26 23:08:03 -050013564 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
13565 mp = (struct lpfc_dmabuf *) (mb->context1);
13566 if (mp) {
13567 __lpfc_mbuf_free(phba, mp->virt, mp->phys);
13568 kfree(mp);
13569 }
James Smart78730cf2010-04-06 15:06:30 -040013570 ndlp = (struct lpfc_nodelist *) mb->context2;
James Smartd439d282010-09-29 11:18:45 -040013571 mb->context2 = NULL;
James Smart78730cf2010-04-06 15:06:30 -040013572 if (ndlp) {
Dan Carpenterec21b3b2010-08-08 00:15:17 +020013573 spin_lock(shost->host_lock);
James Smart589a52d2010-07-14 15:30:54 -040013574 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
Dan Carpenterec21b3b2010-08-08 00:15:17 +020013575 spin_unlock(shost->host_lock);
James Smart78730cf2010-04-06 15:06:30 -040013576 lpfc_nlp_put(ndlp);
James Smart78730cf2010-04-06 15:06:30 -040013577 }
James Smart695a8142010-01-26 23:08:03 -050013578 }
James Smart695a8142010-01-26 23:08:03 -050013579 mempool_free(mb, phba->mbox_mem_pool);
13580 }
James Smartd439d282010-09-29 11:18:45 -040013581
13582 /* Release the ndlp with the cleaned-up active mailbox command */
13583 if (act_mbx_ndlp) {
13584 spin_lock(shost->host_lock);
13585 act_mbx_ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
13586 spin_unlock(shost->host_lock);
13587 lpfc_nlp_put(act_mbx_ndlp);
James Smart695a8142010-01-26 23:08:03 -050013588 }
James Smart695a8142010-01-26 23:08:03 -050013589}
13590
James Smart2a9bf3d2010-06-07 15:24:45 -040013591/**
13592 * lpfc_drain_txq - Drain the txq
13593 * @phba: Pointer to HBA context object.
13594 *
13595 * This function attempt to submit IOCBs on the txq
13596 * to the adapter. For SLI4 adapters, the txq contains
13597 * ELS IOCBs that have been deferred because the there
13598 * are no SGLs. This congestion can occur with large
13599 * vport counts during node discovery.
13600 **/
13601
13602uint32_t
13603lpfc_drain_txq(struct lpfc_hba *phba)
13604{
13605 LIST_HEAD(completions);
13606 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
13607 struct lpfc_iocbq *piocbq = 0;
13608 unsigned long iflags = 0;
13609 char *fail_msg = NULL;
13610 struct lpfc_sglq *sglq;
13611 union lpfc_wqe wqe;
13612
13613 spin_lock_irqsave(&phba->hbalock, iflags);
13614 if (pring->txq_cnt > pring->txq_max)
13615 pring->txq_max = pring->txq_cnt;
13616
13617 spin_unlock_irqrestore(&phba->hbalock, iflags);
13618
13619 while (pring->txq_cnt) {
13620 spin_lock_irqsave(&phba->hbalock, iflags);
13621
James Smart19ca7602010-11-20 23:11:55 -050013622 piocbq = lpfc_sli_ringtx_get(phba, pring);
13623 sglq = __lpfc_sli_get_sglq(phba, piocbq);
James Smart2a9bf3d2010-06-07 15:24:45 -040013624 if (!sglq) {
James Smart19ca7602010-11-20 23:11:55 -050013625 __lpfc_sli_ringtx_put(phba, pring, piocbq);
James Smart2a9bf3d2010-06-07 15:24:45 -040013626 spin_unlock_irqrestore(&phba->hbalock, iflags);
13627 break;
13628 } else {
James Smart2a9bf3d2010-06-07 15:24:45 -040013629 if (!piocbq) {
13630 /* The txq_cnt out of sync. This should
13631 * never happen
13632 */
13633 sglq = __lpfc_clear_active_sglq(phba,
13634 sglq->sli4_xritag);
13635 spin_unlock_irqrestore(&phba->hbalock, iflags);
13636 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13637 "2823 txq empty and txq_cnt is %d\n ",
13638 pring->txq_cnt);
13639 break;
13640 }
13641 }
13642
13643 /* The xri and iocb resources secured,
13644 * attempt to issue request
13645 */
13646 piocbq->sli4_xritag = sglq->sli4_xritag;
13647 if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocbq, sglq))
13648 fail_msg = "to convert bpl to sgl";
13649 else if (lpfc_sli4_iocb2wqe(phba, piocbq, &wqe))
13650 fail_msg = "to convert iocb to wqe";
13651 else if (lpfc_sli4_wq_put(phba->sli4_hba.els_wq, &wqe))
13652 fail_msg = " - Wq is full";
13653 else
13654 lpfc_sli_ringtxcmpl_put(phba, pring, piocbq);
13655
13656 if (fail_msg) {
13657 /* Failed means we can't issue and need to cancel */
13658 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13659 "2822 IOCB failed %s iotag 0x%x "
13660 "xri 0x%x\n",
13661 fail_msg,
13662 piocbq->iotag, piocbq->sli4_xritag);
13663 list_add_tail(&piocbq->list, &completions);
13664 }
13665 spin_unlock_irqrestore(&phba->hbalock, iflags);
13666 }
13667
James Smart2a9bf3d2010-06-07 15:24:45 -040013668 /* Cancel all the IOCBs that cannot be issued */
13669 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
13670 IOERR_SLI_ABORTED);
13671
13672 return pring->txq_cnt;
13673}