blob: ed8f048dfb53024a0ef4aeb682afb771c0112304 [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 Smartd8e93df2009-05-22 14:53:05 -04004 * Copyright (C) 2004-2009 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;
538
539 /*
540 * set the active bit even if there is no mem available.
541 */
542 adj_xri = xritag - phba->sli4_hba.max_cfg_param.xri_base;
543 if (!ndlp)
544 return -EINVAL;
545 if (test_and_set_bit(adj_xri, ndlp->active_rrqs.xri_bitmap))
546 return -EINVAL;
547 rrq = mempool_alloc(phba->rrq_pool, GFP_KERNEL);
548 if (rrq) {
549 rrq->send_rrq = send_rrq;
550 rrq->xritag = xritag;
551 rrq->rrq_stop_time = jiffies + HZ * (phba->fc_ratov + 1);
552 rrq->ndlp = ndlp;
553 rrq->nlp_DID = ndlp->nlp_DID;
554 rrq->vport = ndlp->vport;
555 rrq->rxid = rxid;
556 empty = list_empty(&phba->active_rrq_list);
557 if (phba->cfg_enable_rrq && send_rrq)
558 /*
559 * We need the xri before we can add this to the
560 * phba active rrq list.
561 */
562 rrq->send_rrq = send_rrq;
563 else
564 rrq->send_rrq = 0;
565 list_add_tail(&rrq->list, &phba->active_rrq_list);
566 if (!(phba->hba_flag & HBA_RRQ_ACTIVE)) {
567 phba->hba_flag |= HBA_RRQ_ACTIVE;
568 if (empty)
569 lpfc_worker_wake_up(phba);
570 }
571 return 0;
572 }
573 return -ENOMEM;
574}
575
576/**
577 * __lpfc_clr_rrq_active - Clears RRQ active bit in xri_bitmap.
578 * @phba: Pointer to HBA context object.
579 * @xritag: xri used in this exchange.
580 * @rrq: The RRQ to be cleared.
581 *
582 * This function is called with hbalock held. This function
583 **/
584static void
585__lpfc_clr_rrq_active(struct lpfc_hba *phba,
586 uint16_t xritag,
587 struct lpfc_node_rrq *rrq)
588{
589 uint16_t adj_xri;
590 struct lpfc_nodelist *ndlp;
591
592 ndlp = lpfc_findnode_did(rrq->vport, rrq->nlp_DID);
593
594 /* The target DID could have been swapped (cable swap)
595 * we should use the ndlp from the findnode if it is
596 * available.
597 */
598 if (!ndlp)
599 ndlp = rrq->ndlp;
600
601 adj_xri = xritag - phba->sli4_hba.max_cfg_param.xri_base;
602 if (test_and_clear_bit(adj_xri, ndlp->active_rrqs.xri_bitmap)) {
603 rrq->send_rrq = 0;
604 rrq->xritag = 0;
605 rrq->rrq_stop_time = 0;
606 }
607 mempool_free(rrq, phba->rrq_pool);
608}
609
610/**
611 * lpfc_handle_rrq_active - Checks if RRQ has waithed RATOV.
612 * @phba: Pointer to HBA context object.
613 *
614 * This function is called with hbalock held. This function
615 * Checks if stop_time (ratov from setting rrq active) has
616 * been reached, if it has and the send_rrq flag is set then
617 * it will call lpfc_send_rrq. If the send_rrq flag is not set
618 * then it will just call the routine to clear the rrq and
619 * free the rrq resource.
620 * The timer is set to the next rrq that is going to expire before
621 * leaving the routine.
622 *
623 **/
624void
625lpfc_handle_rrq_active(struct lpfc_hba *phba)
626{
627 struct lpfc_node_rrq *rrq;
628 struct lpfc_node_rrq *nextrrq;
629 unsigned long next_time;
630 unsigned long iflags;
631
632 spin_lock_irqsave(&phba->hbalock, iflags);
633 phba->hba_flag &= ~HBA_RRQ_ACTIVE;
634 next_time = jiffies + HZ * (phba->fc_ratov + 1);
635 list_for_each_entry_safe(rrq, nextrrq,
636 &phba->active_rrq_list, list) {
637 if (time_after(jiffies, rrq->rrq_stop_time)) {
638 list_del(&rrq->list);
639 if (!rrq->send_rrq)
640 /* this call will free the rrq */
641 __lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
642 else {
643 /* if we send the rrq then the completion handler
644 * will clear the bit in the xribitmap.
645 */
646 spin_unlock_irqrestore(&phba->hbalock, iflags);
647 if (lpfc_send_rrq(phba, rrq)) {
648 lpfc_clr_rrq_active(phba, rrq->xritag,
649 rrq);
650 }
651 spin_lock_irqsave(&phba->hbalock, iflags);
652 }
653 } else if (time_before(rrq->rrq_stop_time, next_time))
654 next_time = rrq->rrq_stop_time;
655 }
656 spin_unlock_irqrestore(&phba->hbalock, iflags);
657 if (!list_empty(&phba->active_rrq_list))
658 mod_timer(&phba->rrq_tmr, next_time);
659}
660
661/**
662 * lpfc_get_active_rrq - Get the active RRQ for this exchange.
663 * @vport: Pointer to vport context object.
664 * @xri: The xri used in the exchange.
665 * @did: The targets DID for this exchange.
666 *
667 * returns NULL = rrq not found in the phba->active_rrq_list.
668 * rrq = rrq for this xri and target.
669 **/
670struct lpfc_node_rrq *
671lpfc_get_active_rrq(struct lpfc_vport *vport, uint16_t xri, uint32_t did)
672{
673 struct lpfc_hba *phba = vport->phba;
674 struct lpfc_node_rrq *rrq;
675 struct lpfc_node_rrq *nextrrq;
676 unsigned long iflags;
677
678 if (phba->sli_rev != LPFC_SLI_REV4)
679 return NULL;
680 spin_lock_irqsave(&phba->hbalock, iflags);
681 list_for_each_entry_safe(rrq, nextrrq, &phba->active_rrq_list, list) {
682 if (rrq->vport == vport && rrq->xritag == xri &&
683 rrq->nlp_DID == did){
684 list_del(&rrq->list);
685 spin_unlock_irqrestore(&phba->hbalock, iflags);
686 return rrq;
687 }
688 }
689 spin_unlock_irqrestore(&phba->hbalock, iflags);
690 return NULL;
691}
692
693/**
694 * lpfc_cleanup_vports_rrqs - Remove and clear the active RRQ for this vport.
695 * @vport: Pointer to vport context object.
696 *
697 * Remove all active RRQs for this vport from the phba->active_rrq_list and
698 * clear the rrq.
699 **/
700void
701lpfc_cleanup_vports_rrqs(struct lpfc_vport *vport)
702
703{
704 struct lpfc_hba *phba = vport->phba;
705 struct lpfc_node_rrq *rrq;
706 struct lpfc_node_rrq *nextrrq;
707 unsigned long iflags;
708
709 if (phba->sli_rev != LPFC_SLI_REV4)
710 return;
711 spin_lock_irqsave(&phba->hbalock, iflags);
712 list_for_each_entry_safe(rrq, nextrrq, &phba->active_rrq_list, list) {
713 if (rrq->vport == vport) {
714 list_del(&rrq->list);
715 __lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
716 }
717 }
718 spin_unlock_irqrestore(&phba->hbalock, iflags);
719}
720
721/**
722 * lpfc_cleanup_wt_rrqs - Remove all rrq's from the active list.
723 * @phba: Pointer to HBA context object.
724 *
725 * Remove all rrqs from the phba->active_rrq_list and free them by
726 * calling __lpfc_clr_active_rrq
727 *
728 **/
729void
730lpfc_cleanup_wt_rrqs(struct lpfc_hba *phba)
731{
732 struct lpfc_node_rrq *rrq;
733 struct lpfc_node_rrq *nextrrq;
734 unsigned long next_time;
735 unsigned long iflags;
736
737 if (phba->sli_rev != LPFC_SLI_REV4)
738 return;
739 spin_lock_irqsave(&phba->hbalock, iflags);
740 phba->hba_flag &= ~HBA_RRQ_ACTIVE;
741 next_time = jiffies + HZ * (phba->fc_ratov * 2);
742 list_for_each_entry_safe(rrq, nextrrq, &phba->active_rrq_list, list) {
743 list_del(&rrq->list);
744 __lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
745 }
746 spin_unlock_irqrestore(&phba->hbalock, iflags);
747 if (!list_empty(&phba->active_rrq_list))
748 mod_timer(&phba->rrq_tmr, next_time);
749}
750
751
752/**
753 * __lpfc_test_rrq_active - Test RRQ bit in xri_bitmap.
754 * @phba: Pointer to HBA context object.
755 * @ndlp: Targets nodelist pointer for this exchange.
756 * @xritag the xri in the bitmap to test.
757 *
758 * This function is called with hbalock held. This function
759 * returns 0 = rrq not active for this xri
760 * 1 = rrq is valid for this xri.
761 **/
762static int
763__lpfc_test_rrq_active(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
764 uint16_t xritag)
765{
766 uint16_t adj_xri;
767
768 adj_xri = xritag - phba->sli4_hba.max_cfg_param.xri_base;
769 if (!ndlp)
770 return 0;
771 if (test_bit(adj_xri, ndlp->active_rrqs.xri_bitmap))
772 return 1;
773 else
774 return 0;
775}
776
777/**
778 * lpfc_set_rrq_active - set RRQ active bit in xri_bitmap.
779 * @phba: Pointer to HBA context object.
780 * @ndlp: nodelist pointer for this target.
781 * @xritag: xri used in this exchange.
782 * @rxid: Remote Exchange ID.
783 * @send_rrq: Flag used to determine if we should send rrq els cmd.
784 *
785 * This function takes the hbalock.
786 * The active bit is always set in the active rrq xri_bitmap even
787 * if there is no slot avaiable for the other rrq information.
788 *
789 * returns 0 rrq actived for this xri
790 * < 0 No memory or invalid ndlp.
791 **/
792int
793lpfc_set_rrq_active(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
794 uint16_t xritag, uint16_t rxid, uint16_t send_rrq)
795{
796 int ret;
797 unsigned long iflags;
798
799 spin_lock_irqsave(&phba->hbalock, iflags);
800 ret = __lpfc_set_rrq_active(phba, ndlp, xritag, rxid, send_rrq);
801 spin_unlock_irqrestore(&phba->hbalock, iflags);
802 return ret;
803}
804
805/**
806 * lpfc_clr_rrq_active - Clears RRQ active bit in xri_bitmap.
807 * @phba: Pointer to HBA context object.
808 * @xritag: xri used in this exchange.
809 * @rrq: The RRQ to be cleared.
810 *
811 * This function is takes the hbalock.
812 **/
813void
814lpfc_clr_rrq_active(struct lpfc_hba *phba,
815 uint16_t xritag,
816 struct lpfc_node_rrq *rrq)
817{
818 unsigned long iflags;
819
820 spin_lock_irqsave(&phba->hbalock, iflags);
821 __lpfc_clr_rrq_active(phba, xritag, rrq);
822 spin_unlock_irqrestore(&phba->hbalock, iflags);
823 return;
824}
825
826
827
828/**
829 * lpfc_test_rrq_active - Test RRQ bit in xri_bitmap.
830 * @phba: Pointer to HBA context object.
831 * @ndlp: Targets nodelist pointer for this exchange.
832 * @xritag the xri in the bitmap to test.
833 *
834 * This function takes the hbalock.
835 * returns 0 = rrq not active for this xri
836 * 1 = rrq is valid for this xri.
837 **/
838int
839lpfc_test_rrq_active(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
840 uint16_t xritag)
841{
842 int ret;
843 unsigned long iflags;
844
845 spin_lock_irqsave(&phba->hbalock, iflags);
846 ret = __lpfc_test_rrq_active(phba, ndlp, xritag);
847 spin_unlock_irqrestore(&phba->hbalock, iflags);
848 return ret;
849}
850
851/**
James Smartda0436e2009-05-22 14:51:39 -0400852 * __lpfc_sli_get_sglq - Allocates an iocb object from sgl pool
853 * @phba: Pointer to HBA context object.
James Smart19ca7602010-11-20 23:11:55 -0500854 * @piocb: Pointer to the iocbq.
James Smartda0436e2009-05-22 14:51:39 -0400855 *
856 * This function is called with hbalock held. This function
857 * Gets a new driver sglq object from the sglq list. If the
858 * list is not empty then it is successful, it returns pointer to the newly
859 * allocated sglq object else it returns NULL.
860 **/
861static struct lpfc_sglq *
James Smart19ca7602010-11-20 23:11:55 -0500862__lpfc_sli_get_sglq(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq)
James Smartda0436e2009-05-22 14:51:39 -0400863{
864 struct list_head *lpfc_sgl_list = &phba->sli4_hba.lpfc_sgl_list;
865 struct lpfc_sglq *sglq = NULL;
James Smart19ca7602010-11-20 23:11:55 -0500866 struct lpfc_sglq *start_sglq = NULL;
James Smartda0436e2009-05-22 14:51:39 -0400867 uint16_t adj_xri;
James Smart19ca7602010-11-20 23:11:55 -0500868 struct lpfc_scsi_buf *lpfc_cmd;
869 struct lpfc_nodelist *ndlp;
870 int found = 0;
871
872 if (piocbq->iocb_flag & LPFC_IO_FCP) {
873 lpfc_cmd = (struct lpfc_scsi_buf *) piocbq->context1;
874 ndlp = lpfc_cmd->rdata->pnode;
James Smartbe858b62010-12-15 17:57:20 -0500875 } else if ((piocbq->iocb.ulpCommand == CMD_GEN_REQUEST64_CR) &&
876 !(piocbq->iocb_flag & LPFC_IO_LIBDFC))
James Smart19ca7602010-11-20 23:11:55 -0500877 ndlp = piocbq->context_un.ndlp;
James Smart19ca7602010-11-20 23:11:55 -0500878 else
879 ndlp = piocbq->context1;
880
James Smartda0436e2009-05-22 14:51:39 -0400881 list_remove_head(lpfc_sgl_list, sglq, struct lpfc_sglq, list);
James Smart19ca7602010-11-20 23:11:55 -0500882 start_sglq = sglq;
883 while (!found) {
884 if (!sglq)
885 return NULL;
886 adj_xri = sglq->sli4_xritag -
887 phba->sli4_hba.max_cfg_param.xri_base;
888 if (__lpfc_test_rrq_active(phba, ndlp, sglq->sli4_xritag)) {
889 /* This xri has an rrq outstanding for this DID.
890 * put it back in the list and get another xri.
891 */
892 list_add_tail(&sglq->list, lpfc_sgl_list);
893 sglq = NULL;
894 list_remove_head(lpfc_sgl_list, sglq,
895 struct lpfc_sglq, list);
896 if (sglq == start_sglq) {
897 sglq = NULL;
898 break;
899 } else
900 continue;
901 }
902 sglq->ndlp = ndlp;
903 found = 1;
904 phba->sli4_hba.lpfc_sglq_active_list[adj_xri] = sglq;
905 sglq->state = SGL_ALLOCATED;
906 }
James Smartda0436e2009-05-22 14:51:39 -0400907 return sglq;
908}
909
910/**
James Smart3621a712009-04-06 18:47:14 -0400911 * lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
James Smarte59058c2008-08-24 21:49:00 -0400912 * @phba: Pointer to HBA context object.
913 *
914 * This function is called with no lock held. This function
915 * allocates a new driver iocb object from the iocb pool. If the
916 * allocation is successful, it returns pointer to the newly
917 * allocated iocb object else it returns NULL.
918 **/
James Smart2e0fef82007-06-17 19:56:36 -0500919struct lpfc_iocbq *
920lpfc_sli_get_iocbq(struct lpfc_hba *phba)
James Bottomley604a3e32005-10-29 10:28:33 -0500921{
James Smart2e0fef82007-06-17 19:56:36 -0500922 struct lpfc_iocbq * iocbq = NULL;
923 unsigned long iflags;
924
925 spin_lock_irqsave(&phba->hbalock, iflags);
926 iocbq = __lpfc_sli_get_iocbq(phba);
927 spin_unlock_irqrestore(&phba->hbalock, iflags);
928 return iocbq;
929}
930
James Smarte59058c2008-08-24 21:49:00 -0400931/**
James Smart4f774512009-05-22 14:52:35 -0400932 * __lpfc_sli_release_iocbq_s4 - Release iocb to the iocb pool
933 * @phba: Pointer to HBA context object.
934 * @iocbq: Pointer to driver iocb object.
935 *
936 * This function is called with hbalock held to release driver
937 * iocb object to the iocb pool. The iotag in the iocb object
938 * does not change for each use of the iocb object. This function
939 * clears all other fields of the iocb object when it is freed.
940 * The sqlq structure that holds the xritag and phys and virtual
941 * mappings for the scatter gather list is retrieved from the
942 * active array of sglq. The get of the sglq pointer also clears
943 * the entry in the array. If the status of the IO indiactes that
944 * this IO was aborted then the sglq entry it put on the
945 * lpfc_abts_els_sgl_list until the CQ_ABORTED_XRI is received. If the
946 * IO has good status or fails for any other reason then the sglq
947 * entry is added to the free list (lpfc_sgl_list).
948 **/
949static void
950__lpfc_sli_release_iocbq_s4(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
951{
952 struct lpfc_sglq *sglq;
953 size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
James Smart2a9bf3d2010-06-07 15:24:45 -0400954 unsigned long iflag = 0;
955 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
James Smart4f774512009-05-22 14:52:35 -0400956
957 if (iocbq->sli4_xritag == NO_XRI)
958 sglq = NULL;
959 else
960 sglq = __lpfc_clear_active_sglq(phba, iocbq->sli4_xritag);
961 if (sglq) {
James Smart0f65ff62010-02-26 14:14:23 -0500962 if ((iocbq->iocb_flag & LPFC_EXCHANGE_BUSY) &&
963 (sglq->state != SGL_XRI_ABORTED)) {
James Smart4f774512009-05-22 14:52:35 -0400964 spin_lock_irqsave(&phba->sli4_hba.abts_sgl_list_lock,
965 iflag);
966 list_add(&sglq->list,
967 &phba->sli4_hba.lpfc_abts_els_sgl_list);
968 spin_unlock_irqrestore(
969 &phba->sli4_hba.abts_sgl_list_lock, iflag);
James Smart0f65ff62010-02-26 14:14:23 -0500970 } else {
971 sglq->state = SGL_FREED;
James Smart19ca7602010-11-20 23:11:55 -0500972 sglq->ndlp = NULL;
James Smartfedd3b72011-02-16 12:39:24 -0500973 list_add_tail(&sglq->list,
974 &phba->sli4_hba.lpfc_sgl_list);
James Smart2a9bf3d2010-06-07 15:24:45 -0400975
976 /* Check if TXQ queue needs to be serviced */
James Smart589a52d2010-07-14 15:30:54 -0400977 if (pring->txq_cnt)
James Smart2a9bf3d2010-06-07 15:24:45 -0400978 lpfc_worker_wake_up(phba);
James Smart0f65ff62010-02-26 14:14:23 -0500979 }
James Smart4f774512009-05-22 14:52:35 -0400980 }
981
982
983 /*
984 * Clean all volatile data fields, preserve iotag and node struct.
985 */
986 memset((char *)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
987 iocbq->sli4_xritag = NO_XRI;
988 list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
989}
990
James Smart2a9bf3d2010-06-07 15:24:45 -0400991
James Smart4f774512009-05-22 14:52:35 -0400992/**
James Smart3772a992009-05-22 14:50:54 -0400993 * __lpfc_sli_release_iocbq_s3 - Release iocb to the iocb pool
994 * @phba: Pointer to HBA context object.
995 * @iocbq: Pointer to driver iocb object.
996 *
997 * This function is called with hbalock held to release driver
998 * iocb object to the iocb pool. The iotag in the iocb object
999 * does not change for each use of the iocb object. This function
1000 * clears all other fields of the iocb object when it is freed.
1001 **/
1002static void
1003__lpfc_sli_release_iocbq_s3(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1004{
1005 size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
1006
1007 /*
1008 * Clean all volatile data fields, preserve iotag and node struct.
1009 */
1010 memset((char*)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
1011 iocbq->sli4_xritag = NO_XRI;
1012 list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
1013}
1014
1015/**
James Smart3621a712009-04-06 18:47:14 -04001016 * __lpfc_sli_release_iocbq - Release iocb to the iocb pool
James Smarte59058c2008-08-24 21:49:00 -04001017 * @phba: Pointer to HBA context object.
1018 * @iocbq: Pointer to driver iocb object.
1019 *
1020 * This function is called with hbalock held to release driver
1021 * iocb object to the iocb pool. The iotag in the iocb object
1022 * does not change for each use of the iocb object. This function
1023 * clears all other fields of the iocb object when it is freed.
1024 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01001025static void
James Smart2e0fef82007-06-17 19:56:36 -05001026__lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1027{
James Smart3772a992009-05-22 14:50:54 -04001028 phba->__lpfc_sli_release_iocbq(phba, iocbq);
James Smart2a9bf3d2010-06-07 15:24:45 -04001029 phba->iocb_cnt--;
James Bottomley604a3e32005-10-29 10:28:33 -05001030}
1031
James Smarte59058c2008-08-24 21:49:00 -04001032/**
James Smart3621a712009-04-06 18:47:14 -04001033 * lpfc_sli_release_iocbq - Release iocb to the iocb pool
James Smarte59058c2008-08-24 21:49:00 -04001034 * @phba: Pointer to HBA context object.
1035 * @iocbq: Pointer to driver iocb object.
1036 *
1037 * This function is called with no lock held to release the iocb to
1038 * iocb pool.
1039 **/
James Smart2e0fef82007-06-17 19:56:36 -05001040void
1041lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1042{
1043 unsigned long iflags;
1044
1045 /*
1046 * Clean all volatile data fields, preserve iotag and node struct.
1047 */
1048 spin_lock_irqsave(&phba->hbalock, iflags);
1049 __lpfc_sli_release_iocbq(phba, iocbq);
1050 spin_unlock_irqrestore(&phba->hbalock, iflags);
1051}
1052
James Smarte59058c2008-08-24 21:49:00 -04001053/**
James Smarta257bf92009-04-06 18:48:10 -04001054 * lpfc_sli_cancel_iocbs - Cancel all iocbs from a list.
1055 * @phba: Pointer to HBA context object.
1056 * @iocblist: List of IOCBs.
1057 * @ulpstatus: ULP status in IOCB command field.
1058 * @ulpWord4: ULP word-4 in IOCB command field.
1059 *
1060 * This function is called with a list of IOCBs to cancel. It cancels the IOCB
1061 * on the list by invoking the complete callback function associated with the
1062 * IOCB with the provided @ulpstatus and @ulpword4 set to the IOCB commond
1063 * fields.
1064 **/
1065void
1066lpfc_sli_cancel_iocbs(struct lpfc_hba *phba, struct list_head *iocblist,
1067 uint32_t ulpstatus, uint32_t ulpWord4)
1068{
1069 struct lpfc_iocbq *piocb;
1070
1071 while (!list_empty(iocblist)) {
1072 list_remove_head(iocblist, piocb, struct lpfc_iocbq, list);
1073
1074 if (!piocb->iocb_cmpl)
1075 lpfc_sli_release_iocbq(phba, piocb);
1076 else {
1077 piocb->iocb.ulpStatus = ulpstatus;
1078 piocb->iocb.un.ulpWord[4] = ulpWord4;
1079 (piocb->iocb_cmpl) (phba, piocb, piocb);
1080 }
1081 }
1082 return;
1083}
1084
1085/**
James Smart3621a712009-04-06 18:47:14 -04001086 * lpfc_sli_iocb_cmd_type - Get the iocb type
1087 * @iocb_cmnd: iocb command code.
James Smarte59058c2008-08-24 21:49:00 -04001088 *
1089 * This function is called by ring event handler function to get the iocb type.
1090 * This function translates the iocb command to an iocb command type used to
1091 * decide the final disposition of each completed IOCB.
1092 * The function returns
1093 * LPFC_UNKNOWN_IOCB if it is an unsupported iocb
1094 * LPFC_SOL_IOCB if it is a solicited iocb completion
1095 * LPFC_ABORT_IOCB if it is an abort iocb
1096 * LPFC_UNSOL_IOCB if it is an unsolicited iocb
1097 *
1098 * The caller is not required to hold any lock.
1099 **/
dea31012005-04-17 16:05:31 -05001100static lpfc_iocb_type
1101lpfc_sli_iocb_cmd_type(uint8_t iocb_cmnd)
1102{
1103 lpfc_iocb_type type = LPFC_UNKNOWN_IOCB;
1104
1105 if (iocb_cmnd > CMD_MAX_IOCB_CMD)
1106 return 0;
1107
1108 switch (iocb_cmnd) {
1109 case CMD_XMIT_SEQUENCE_CR:
1110 case CMD_XMIT_SEQUENCE_CX:
1111 case CMD_XMIT_BCAST_CN:
1112 case CMD_XMIT_BCAST_CX:
1113 case CMD_ELS_REQUEST_CR:
1114 case CMD_ELS_REQUEST_CX:
1115 case CMD_CREATE_XRI_CR:
1116 case CMD_CREATE_XRI_CX:
1117 case CMD_GET_RPI_CN:
1118 case CMD_XMIT_ELS_RSP_CX:
1119 case CMD_GET_RPI_CR:
1120 case CMD_FCP_IWRITE_CR:
1121 case CMD_FCP_IWRITE_CX:
1122 case CMD_FCP_IREAD_CR:
1123 case CMD_FCP_IREAD_CX:
1124 case CMD_FCP_ICMND_CR:
1125 case CMD_FCP_ICMND_CX:
James Smartf5603512006-12-02 13:35:43 -05001126 case CMD_FCP_TSEND_CX:
1127 case CMD_FCP_TRSP_CX:
1128 case CMD_FCP_TRECEIVE_CX:
1129 case CMD_FCP_AUTO_TRSP_CX:
dea31012005-04-17 16:05:31 -05001130 case CMD_ADAPTER_MSG:
1131 case CMD_ADAPTER_DUMP:
1132 case CMD_XMIT_SEQUENCE64_CR:
1133 case CMD_XMIT_SEQUENCE64_CX:
1134 case CMD_XMIT_BCAST64_CN:
1135 case CMD_XMIT_BCAST64_CX:
1136 case CMD_ELS_REQUEST64_CR:
1137 case CMD_ELS_REQUEST64_CX:
1138 case CMD_FCP_IWRITE64_CR:
1139 case CMD_FCP_IWRITE64_CX:
1140 case CMD_FCP_IREAD64_CR:
1141 case CMD_FCP_IREAD64_CX:
1142 case CMD_FCP_ICMND64_CR:
1143 case CMD_FCP_ICMND64_CX:
James Smartf5603512006-12-02 13:35:43 -05001144 case CMD_FCP_TSEND64_CX:
1145 case CMD_FCP_TRSP64_CX:
1146 case CMD_FCP_TRECEIVE64_CX:
dea31012005-04-17 16:05:31 -05001147 case CMD_GEN_REQUEST64_CR:
1148 case CMD_GEN_REQUEST64_CX:
1149 case CMD_XMIT_ELS_RSP64_CX:
James Smartda0436e2009-05-22 14:51:39 -04001150 case DSSCMD_IWRITE64_CR:
1151 case DSSCMD_IWRITE64_CX:
1152 case DSSCMD_IREAD64_CR:
1153 case DSSCMD_IREAD64_CX:
dea31012005-04-17 16:05:31 -05001154 type = LPFC_SOL_IOCB;
1155 break;
1156 case CMD_ABORT_XRI_CN:
1157 case CMD_ABORT_XRI_CX:
1158 case CMD_CLOSE_XRI_CN:
1159 case CMD_CLOSE_XRI_CX:
1160 case CMD_XRI_ABORTED_CX:
1161 case CMD_ABORT_MXRI64_CN:
James Smart6669f9b2009-10-02 15:16:45 -04001162 case CMD_XMIT_BLS_RSP64_CX:
dea31012005-04-17 16:05:31 -05001163 type = LPFC_ABORT_IOCB;
1164 break;
1165 case CMD_RCV_SEQUENCE_CX:
1166 case CMD_RCV_ELS_REQ_CX:
1167 case CMD_RCV_SEQUENCE64_CX:
1168 case CMD_RCV_ELS_REQ64_CX:
James Smart57127f12007-10-27 13:37:05 -04001169 case CMD_ASYNC_STATUS:
James Smarted957682007-06-17 19:56:37 -05001170 case CMD_IOCB_RCV_SEQ64_CX:
1171 case CMD_IOCB_RCV_ELS64_CX:
1172 case CMD_IOCB_RCV_CONT64_CX:
James Smart3163f722008-02-08 18:50:25 -05001173 case CMD_IOCB_RET_XRI64_CX:
dea31012005-04-17 16:05:31 -05001174 type = LPFC_UNSOL_IOCB;
1175 break;
James Smart3163f722008-02-08 18:50:25 -05001176 case CMD_IOCB_XMIT_MSEQ64_CR:
1177 case CMD_IOCB_XMIT_MSEQ64_CX:
1178 case CMD_IOCB_RCV_SEQ_LIST64_CX:
1179 case CMD_IOCB_RCV_ELS_LIST64_CX:
1180 case CMD_IOCB_CLOSE_EXTENDED_CN:
1181 case CMD_IOCB_ABORT_EXTENDED_CN:
1182 case CMD_IOCB_RET_HBQE64_CN:
1183 case CMD_IOCB_FCP_IBIDIR64_CR:
1184 case CMD_IOCB_FCP_IBIDIR64_CX:
1185 case CMD_IOCB_FCP_ITASKMGT64_CX:
1186 case CMD_IOCB_LOGENTRY_CN:
1187 case CMD_IOCB_LOGENTRY_ASYNC_CN:
1188 printk("%s - Unhandled SLI-3 Command x%x\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07001189 __func__, iocb_cmnd);
James Smart3163f722008-02-08 18:50:25 -05001190 type = LPFC_UNKNOWN_IOCB;
1191 break;
dea31012005-04-17 16:05:31 -05001192 default:
1193 type = LPFC_UNKNOWN_IOCB;
1194 break;
1195 }
1196
1197 return type;
1198}
1199
James Smarte59058c2008-08-24 21:49:00 -04001200/**
James Smart3621a712009-04-06 18:47:14 -04001201 * lpfc_sli_ring_map - Issue config_ring mbox for all rings
James Smarte59058c2008-08-24 21:49:00 -04001202 * @phba: Pointer to HBA context object.
1203 *
1204 * This function is called from SLI initialization code
1205 * to configure every ring of the HBA's SLI interface. The
1206 * caller is not required to hold any lock. This function issues
1207 * a config_ring mailbox command for each ring.
1208 * This function returns zero if successful else returns a negative
1209 * error code.
1210 **/
dea31012005-04-17 16:05:31 -05001211static int
James Smarted957682007-06-17 19:56:37 -05001212lpfc_sli_ring_map(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05001213{
1214 struct lpfc_sli *psli = &phba->sli;
James Smarted957682007-06-17 19:56:37 -05001215 LPFC_MBOXQ_t *pmb;
1216 MAILBOX_t *pmbox;
1217 int i, rc, ret = 0;
dea31012005-04-17 16:05:31 -05001218
James Smarted957682007-06-17 19:56:37 -05001219 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1220 if (!pmb)
1221 return -ENOMEM;
James Smart04c68492009-05-22 14:52:52 -04001222 pmbox = &pmb->u.mb;
James Smarted957682007-06-17 19:56:37 -05001223 phba->link_state = LPFC_INIT_MBX_CMDS;
dea31012005-04-17 16:05:31 -05001224 for (i = 0; i < psli->num_rings; i++) {
dea31012005-04-17 16:05:31 -05001225 lpfc_config_ring(phba, i, pmb);
1226 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
1227 if (rc != MBX_SUCCESS) {
James Smart92d7f7b2007-06-17 19:56:38 -05001228 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04001229 "0446 Adapter failed to init (%d), "
dea31012005-04-17 16:05:31 -05001230 "mbxCmd x%x CFG_RING, mbxStatus x%x, "
1231 "ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04001232 rc, pmbox->mbxCommand,
1233 pmbox->mbxStatus, i);
James Smart2e0fef82007-06-17 19:56:36 -05001234 phba->link_state = LPFC_HBA_ERROR;
James Smarted957682007-06-17 19:56:37 -05001235 ret = -ENXIO;
1236 break;
dea31012005-04-17 16:05:31 -05001237 }
1238 }
James Smarted957682007-06-17 19:56:37 -05001239 mempool_free(pmb, phba->mbox_mem_pool);
1240 return ret;
dea31012005-04-17 16:05:31 -05001241}
1242
James Smarte59058c2008-08-24 21:49:00 -04001243/**
James Smart3621a712009-04-06 18:47:14 -04001244 * lpfc_sli_ringtxcmpl_put - Adds new iocb to the txcmplq
James Smarte59058c2008-08-24 21:49:00 -04001245 * @phba: Pointer to HBA context object.
1246 * @pring: Pointer to driver SLI ring object.
1247 * @piocb: Pointer to the driver iocb object.
1248 *
1249 * This function is called with hbalock held. The function adds the
1250 * new iocb to txcmplq of the given ring. This function always returns
1251 * 0. If this function is called for ELS ring, this function checks if
1252 * there is a vport associated with the ELS command. This function also
1253 * starts els_tmofunc timer if this is an ELS command.
1254 **/
dea31012005-04-17 16:05:31 -05001255static int
James Smart2e0fef82007-06-17 19:56:36 -05001256lpfc_sli_ringtxcmpl_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1257 struct lpfc_iocbq *piocb)
dea31012005-04-17 16:05:31 -05001258{
dea31012005-04-17 16:05:31 -05001259 list_add_tail(&piocb->list, &pring->txcmplq);
James Smart2a9bf3d2010-06-07 15:24:45 -04001260 piocb->iocb_flag |= LPFC_IO_ON_Q;
dea31012005-04-17 16:05:31 -05001261 pring->txcmplq_cnt++;
James Smart2a9bf3d2010-06-07 15:24:45 -04001262 if (pring->txcmplq_cnt > pring->txcmplq_max)
1263 pring->txcmplq_max = pring->txcmplq_cnt;
1264
James Smart92d7f7b2007-06-17 19:56:38 -05001265 if ((unlikely(pring->ringno == LPFC_ELS_RING)) &&
1266 (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
1267 (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
1268 if (!piocb->vport)
1269 BUG();
1270 else
1271 mod_timer(&piocb->vport->els_tmofunc,
1272 jiffies + HZ * (phba->fc_ratov << 1));
1273 }
1274
dea31012005-04-17 16:05:31 -05001275
James Smart2e0fef82007-06-17 19:56:36 -05001276 return 0;
dea31012005-04-17 16:05:31 -05001277}
1278
James Smarte59058c2008-08-24 21:49:00 -04001279/**
James Smart3621a712009-04-06 18:47:14 -04001280 * lpfc_sli_ringtx_get - Get first element of the txq
James Smarte59058c2008-08-24 21:49:00 -04001281 * @phba: Pointer to HBA context object.
1282 * @pring: Pointer to driver SLI ring object.
1283 *
1284 * This function is called with hbalock held to get next
1285 * iocb in txq of the given ring. If there is any iocb in
1286 * the txq, the function returns first iocb in the list after
1287 * removing the iocb from the list, else it returns NULL.
1288 **/
James Smart2a9bf3d2010-06-07 15:24:45 -04001289struct lpfc_iocbq *
James Smart2e0fef82007-06-17 19:56:36 -05001290lpfc_sli_ringtx_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -05001291{
dea31012005-04-17 16:05:31 -05001292 struct lpfc_iocbq *cmd_iocb;
1293
James Smart858c9f62007-06-17 19:56:39 -05001294 list_remove_head((&pring->txq), cmd_iocb, struct lpfc_iocbq, list);
1295 if (cmd_iocb != NULL)
dea31012005-04-17 16:05:31 -05001296 pring->txq_cnt--;
James Smart2e0fef82007-06-17 19:56:36 -05001297 return cmd_iocb;
dea31012005-04-17 16:05:31 -05001298}
1299
James Smarte59058c2008-08-24 21:49:00 -04001300/**
James Smart3621a712009-04-06 18:47:14 -04001301 * lpfc_sli_next_iocb_slot - Get next iocb slot in the ring
James Smarte59058c2008-08-24 21:49:00 -04001302 * @phba: Pointer to HBA context object.
1303 * @pring: Pointer to driver SLI ring object.
1304 *
1305 * This function is called with hbalock held and the caller must post the
1306 * iocb without releasing the lock. If the caller releases the lock,
1307 * iocb slot returned by the function is not guaranteed to be available.
1308 * The function returns pointer to the next available iocb slot if there
1309 * is available slot in the ring, else it returns NULL.
1310 * If the get index of the ring is ahead of the put index, the function
1311 * will post an error attention event to the worker thread to take the
1312 * HBA to offline state.
1313 **/
dea31012005-04-17 16:05:31 -05001314static IOCB_t *
1315lpfc_sli_next_iocb_slot (struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
1316{
James Smart34b02dc2008-08-24 21:49:55 -04001317 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
dea31012005-04-17 16:05:31 -05001318 uint32_t max_cmd_idx = pring->numCiocb;
dea31012005-04-17 16:05:31 -05001319 if ((pring->next_cmdidx == pring->cmdidx) &&
1320 (++pring->next_cmdidx >= max_cmd_idx))
1321 pring->next_cmdidx = 0;
1322
1323 if (unlikely(pring->local_getidx == pring->next_cmdidx)) {
1324
1325 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
1326
1327 if (unlikely(pring->local_getidx >= max_cmd_idx)) {
1328 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001329 "0315 Ring %d issue: portCmdGet %d "
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02001330 "is bigger than cmd ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04001331 pring->ringno,
dea31012005-04-17 16:05:31 -05001332 pring->local_getidx, max_cmd_idx);
1333
James Smart2e0fef82007-06-17 19:56:36 -05001334 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05001335 /*
1336 * All error attention handlers are posted to
1337 * worker thread
1338 */
1339 phba->work_ha |= HA_ERATT;
1340 phba->work_hs = HS_FFER3;
James Smart92d7f7b2007-06-17 19:56:38 -05001341
James Smart5e9d9b82008-06-14 22:52:53 -04001342 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -05001343
1344 return NULL;
1345 }
1346
1347 if (pring->local_getidx == pring->next_cmdidx)
1348 return NULL;
1349 }
1350
James Smarted957682007-06-17 19:56:37 -05001351 return lpfc_cmd_iocb(phba, pring);
dea31012005-04-17 16:05:31 -05001352}
1353
James Smarte59058c2008-08-24 21:49:00 -04001354/**
James Smart3621a712009-04-06 18:47:14 -04001355 * lpfc_sli_next_iotag - Get an iotag for the iocb
James Smarte59058c2008-08-24 21:49:00 -04001356 * @phba: Pointer to HBA context object.
1357 * @iocbq: Pointer to driver iocb object.
1358 *
1359 * This function gets an iotag for the iocb. If there is no unused iotag and
1360 * the iocbq_lookup_len < 0xffff, this function allocates a bigger iotag_lookup
1361 * array and assigns a new iotag.
1362 * The function returns the allocated iotag if successful, else returns zero.
1363 * Zero is not a valid iotag.
1364 * The caller is not required to hold any lock.
1365 **/
James Bottomley604a3e32005-10-29 10:28:33 -05001366uint16_t
James Smart2e0fef82007-06-17 19:56:36 -05001367lpfc_sli_next_iotag(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
dea31012005-04-17 16:05:31 -05001368{
James Smart2e0fef82007-06-17 19:56:36 -05001369 struct lpfc_iocbq **new_arr;
1370 struct lpfc_iocbq **old_arr;
James Bottomley604a3e32005-10-29 10:28:33 -05001371 size_t new_len;
1372 struct lpfc_sli *psli = &phba->sli;
1373 uint16_t iotag;
dea31012005-04-17 16:05:31 -05001374
James Smart2e0fef82007-06-17 19:56:36 -05001375 spin_lock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001376 iotag = psli->last_iotag;
1377 if(++iotag < psli->iocbq_lookup_len) {
1378 psli->last_iotag = iotag;
1379 psli->iocbq_lookup[iotag] = iocbq;
James Smart2e0fef82007-06-17 19:56:36 -05001380 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001381 iocbq->iotag = iotag;
1382 return iotag;
James Smart2e0fef82007-06-17 19:56:36 -05001383 } else if (psli->iocbq_lookup_len < (0xffff
James Bottomley604a3e32005-10-29 10:28:33 -05001384 - LPFC_IOCBQ_LOOKUP_INCREMENT)) {
1385 new_len = psli->iocbq_lookup_len + LPFC_IOCBQ_LOOKUP_INCREMENT;
James Smart2e0fef82007-06-17 19:56:36 -05001386 spin_unlock_irq(&phba->hbalock);
1387 new_arr = kzalloc(new_len * sizeof (struct lpfc_iocbq *),
James Bottomley604a3e32005-10-29 10:28:33 -05001388 GFP_KERNEL);
1389 if (new_arr) {
James Smart2e0fef82007-06-17 19:56:36 -05001390 spin_lock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001391 old_arr = psli->iocbq_lookup;
1392 if (new_len <= psli->iocbq_lookup_len) {
1393 /* highly unprobable case */
1394 kfree(new_arr);
1395 iotag = psli->last_iotag;
1396 if(++iotag < psli->iocbq_lookup_len) {
1397 psli->last_iotag = iotag;
1398 psli->iocbq_lookup[iotag] = iocbq;
James Smart2e0fef82007-06-17 19:56:36 -05001399 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001400 iocbq->iotag = iotag;
1401 return iotag;
1402 }
James Smart2e0fef82007-06-17 19:56:36 -05001403 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001404 return 0;
1405 }
1406 if (psli->iocbq_lookup)
1407 memcpy(new_arr, old_arr,
1408 ((psli->last_iotag + 1) *
James Smart311464e2007-08-02 11:10:37 -04001409 sizeof (struct lpfc_iocbq *)));
James Bottomley604a3e32005-10-29 10:28:33 -05001410 psli->iocbq_lookup = new_arr;
1411 psli->iocbq_lookup_len = new_len;
1412 psli->last_iotag = iotag;
1413 psli->iocbq_lookup[iotag] = iocbq;
James Smart2e0fef82007-06-17 19:56:36 -05001414 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001415 iocbq->iotag = iotag;
1416 kfree(old_arr);
1417 return iotag;
1418 }
James Smart8f6d98d2006-08-01 07:34:00 -04001419 } else
James Smart2e0fef82007-06-17 19:56:36 -05001420 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001421
James Smartbc739052010-08-04 16:11:18 -04001422 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001423 "0318 Failed to allocate IOTAG.last IOTAG is %d\n",
1424 psli->last_iotag);
dea31012005-04-17 16:05:31 -05001425
James Bottomley604a3e32005-10-29 10:28:33 -05001426 return 0;
dea31012005-04-17 16:05:31 -05001427}
1428
James Smarte59058c2008-08-24 21:49:00 -04001429/**
James Smart3621a712009-04-06 18:47:14 -04001430 * lpfc_sli_submit_iocb - Submit an iocb to the firmware
James Smarte59058c2008-08-24 21:49:00 -04001431 * @phba: Pointer to HBA context object.
1432 * @pring: Pointer to driver SLI ring object.
1433 * @iocb: Pointer to iocb slot in the ring.
1434 * @nextiocb: Pointer to driver iocb object which need to be
1435 * posted to firmware.
1436 *
1437 * This function is called with hbalock held to post a new iocb to
1438 * the firmware. This function copies the new iocb to ring iocb slot and
1439 * updates the ring pointers. It adds the new iocb to txcmplq if there is
1440 * a completion call back for this iocb else the function will free the
1441 * iocb object.
1442 **/
dea31012005-04-17 16:05:31 -05001443static void
1444lpfc_sli_submit_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1445 IOCB_t *iocb, struct lpfc_iocbq *nextiocb)
1446{
1447 /*
James Bottomley604a3e32005-10-29 10:28:33 -05001448 * Set up an iotag
dea31012005-04-17 16:05:31 -05001449 */
James Bottomley604a3e32005-10-29 10:28:33 -05001450 nextiocb->iocb.ulpIoTag = (nextiocb->iocb_cmpl) ? nextiocb->iotag : 0;
dea31012005-04-17 16:05:31 -05001451
James Smarte2a0a9d2008-12-04 22:40:02 -05001452
James Smarta58cbd52007-08-02 11:09:43 -04001453 if (pring->ringno == LPFC_ELS_RING) {
1454 lpfc_debugfs_slow_ring_trc(phba,
1455 "IOCB cmd ring: wd4:x%08x wd6:x%08x wd7:x%08x",
1456 *(((uint32_t *) &nextiocb->iocb) + 4),
1457 *(((uint32_t *) &nextiocb->iocb) + 6),
1458 *(((uint32_t *) &nextiocb->iocb) + 7));
1459 }
1460
dea31012005-04-17 16:05:31 -05001461 /*
1462 * Issue iocb command to adapter
1463 */
James Smart92d7f7b2007-06-17 19:56:38 -05001464 lpfc_sli_pcimem_bcopy(&nextiocb->iocb, iocb, phba->iocb_cmd_size);
dea31012005-04-17 16:05:31 -05001465 wmb();
1466 pring->stats.iocb_cmd++;
1467
1468 /*
1469 * If there is no completion routine to call, we can release the
1470 * IOCB buffer back right now. For IOCBs, like QUE_RING_BUF,
1471 * that have no rsp ring completion, iocb_cmpl MUST be NULL.
1472 */
1473 if (nextiocb->iocb_cmpl)
1474 lpfc_sli_ringtxcmpl_put(phba, pring, nextiocb);
James Bottomley604a3e32005-10-29 10:28:33 -05001475 else
James Smart2e0fef82007-06-17 19:56:36 -05001476 __lpfc_sli_release_iocbq(phba, nextiocb);
dea31012005-04-17 16:05:31 -05001477
1478 /*
1479 * Let the HBA know what IOCB slot will be the next one the
1480 * driver will put a command into.
1481 */
1482 pring->cmdidx = pring->next_cmdidx;
James Smarted957682007-06-17 19:56:37 -05001483 writel(pring->cmdidx, &phba->host_gp[pring->ringno].cmdPutInx);
dea31012005-04-17 16:05:31 -05001484}
1485
James Smarte59058c2008-08-24 21:49:00 -04001486/**
James Smart3621a712009-04-06 18:47:14 -04001487 * lpfc_sli_update_full_ring - Update the chip attention register
James Smarte59058c2008-08-24 21:49:00 -04001488 * @phba: Pointer to HBA context object.
1489 * @pring: Pointer to driver SLI ring object.
1490 *
1491 * The caller is not required to hold any lock for calling this function.
1492 * This function updates the chip attention bits for the ring to inform firmware
1493 * that there are pending work to be done for this ring and requests an
1494 * interrupt when there is space available in the ring. This function is
1495 * called when the driver is unable to post more iocbs to the ring due
1496 * to unavailability of space in the ring.
1497 **/
dea31012005-04-17 16:05:31 -05001498static void
James Smart2e0fef82007-06-17 19:56:36 -05001499lpfc_sli_update_full_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -05001500{
1501 int ringno = pring->ringno;
1502
1503 pring->flag |= LPFC_CALL_RING_AVAILABLE;
1504
1505 wmb();
1506
1507 /*
1508 * Set ring 'ringno' to SET R0CE_REQ in Chip Att register.
1509 * The HBA will tell us when an IOCB entry is available.
1510 */
1511 writel((CA_R0ATT|CA_R0CE_REQ) << (ringno*4), phba->CAregaddr);
1512 readl(phba->CAregaddr); /* flush */
1513
1514 pring->stats.iocb_cmd_full++;
1515}
1516
James Smarte59058c2008-08-24 21:49:00 -04001517/**
James Smart3621a712009-04-06 18:47:14 -04001518 * lpfc_sli_update_ring - Update chip attention register
James Smarte59058c2008-08-24 21:49:00 -04001519 * @phba: Pointer to HBA context object.
1520 * @pring: Pointer to driver SLI ring object.
1521 *
1522 * This function updates the chip attention register bit for the
1523 * given ring to inform HBA that there is more work to be done
1524 * in this ring. The caller is not required to hold any lock.
1525 **/
dea31012005-04-17 16:05:31 -05001526static void
James Smart2e0fef82007-06-17 19:56:36 -05001527lpfc_sli_update_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -05001528{
1529 int ringno = pring->ringno;
1530
1531 /*
1532 * Tell the HBA that there is work to do in this ring.
1533 */
James Smart34b02dc2008-08-24 21:49:55 -04001534 if (!(phba->sli3_options & LPFC_SLI3_CRP_ENABLED)) {
1535 wmb();
1536 writel(CA_R0ATT << (ringno * 4), phba->CAregaddr);
1537 readl(phba->CAregaddr); /* flush */
1538 }
dea31012005-04-17 16:05:31 -05001539}
1540
James Smarte59058c2008-08-24 21:49:00 -04001541/**
James Smart3621a712009-04-06 18:47:14 -04001542 * lpfc_sli_resume_iocb - Process iocbs in the txq
James Smarte59058c2008-08-24 21:49:00 -04001543 * @phba: Pointer to HBA context object.
1544 * @pring: Pointer to driver SLI ring object.
1545 *
1546 * This function is called with hbalock held to post pending iocbs
1547 * in the txq to the firmware. This function is called when driver
1548 * detects space available in the ring.
1549 **/
dea31012005-04-17 16:05:31 -05001550static void
James Smart2e0fef82007-06-17 19:56:36 -05001551lpfc_sli_resume_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -05001552{
1553 IOCB_t *iocb;
1554 struct lpfc_iocbq *nextiocb;
1555
1556 /*
1557 * Check to see if:
1558 * (a) there is anything on the txq to send
1559 * (b) link is up
1560 * (c) link attention events can be processed (fcp ring only)
1561 * (d) IOCB processing is not blocked by the outstanding mbox command.
1562 */
1563 if (pring->txq_cnt &&
James Smart2e0fef82007-06-17 19:56:36 -05001564 lpfc_is_link_up(phba) &&
dea31012005-04-17 16:05:31 -05001565 (pring->ringno != phba->sli.fcp_ring ||
James Smart0b727fe2007-10-27 13:37:25 -04001566 phba->sli.sli_flag & LPFC_PROCESS_LA)) {
dea31012005-04-17 16:05:31 -05001567
1568 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
1569 (nextiocb = lpfc_sli_ringtx_get(phba, pring)))
1570 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
1571
1572 if (iocb)
1573 lpfc_sli_update_ring(phba, pring);
1574 else
1575 lpfc_sli_update_full_ring(phba, pring);
1576 }
1577
1578 return;
1579}
1580
James Smarte59058c2008-08-24 21:49:00 -04001581/**
James Smart3621a712009-04-06 18:47:14 -04001582 * lpfc_sli_next_hbq_slot - Get next hbq entry for the HBQ
James Smarte59058c2008-08-24 21:49:00 -04001583 * @phba: Pointer to HBA context object.
1584 * @hbqno: HBQ number.
1585 *
1586 * This function is called with hbalock held to get the next
1587 * available slot for the given HBQ. If there is free slot
1588 * available for the HBQ it will return pointer to the next available
1589 * HBQ entry else it will return NULL.
1590 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01001591static struct lpfc_hbq_entry *
James Smarted957682007-06-17 19:56:37 -05001592lpfc_sli_next_hbq_slot(struct lpfc_hba *phba, uint32_t hbqno)
1593{
1594 struct hbq_s *hbqp = &phba->hbqs[hbqno];
1595
1596 if (hbqp->next_hbqPutIdx == hbqp->hbqPutIdx &&
1597 ++hbqp->next_hbqPutIdx >= hbqp->entry_count)
1598 hbqp->next_hbqPutIdx = 0;
1599
1600 if (unlikely(hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)) {
James Smart92d7f7b2007-06-17 19:56:38 -05001601 uint32_t raw_index = phba->hbq_get[hbqno];
James Smarted957682007-06-17 19:56:37 -05001602 uint32_t getidx = le32_to_cpu(raw_index);
1603
1604 hbqp->local_hbqGetIdx = getidx;
1605
1606 if (unlikely(hbqp->local_hbqGetIdx >= hbqp->entry_count)) {
1607 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -05001608 LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04001609 "1802 HBQ %d: local_hbqGetIdx "
James Smarted957682007-06-17 19:56:37 -05001610 "%u is > than hbqp->entry_count %u\n",
James Smarte8b62012007-08-02 11:10:09 -04001611 hbqno, hbqp->local_hbqGetIdx,
James Smarted957682007-06-17 19:56:37 -05001612 hbqp->entry_count);
1613
1614 phba->link_state = LPFC_HBA_ERROR;
1615 return NULL;
1616 }
1617
1618 if (hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)
1619 return NULL;
1620 }
1621
James Smart51ef4c22007-08-02 11:10:31 -04001622 return (struct lpfc_hbq_entry *) phba->hbqs[hbqno].hbq_virt +
1623 hbqp->hbqPutIdx;
James Smarted957682007-06-17 19:56:37 -05001624}
1625
James Smarte59058c2008-08-24 21:49:00 -04001626/**
James Smart3621a712009-04-06 18:47:14 -04001627 * lpfc_sli_hbqbuf_free_all - Free all the hbq buffers
James Smarte59058c2008-08-24 21:49:00 -04001628 * @phba: Pointer to HBA context object.
1629 *
1630 * This function is called with no lock held to free all the
1631 * hbq buffers while uninitializing the SLI interface. It also
1632 * frees the HBQ buffers returned by the firmware but not yet
1633 * processed by the upper layers.
1634 **/
James Smarted957682007-06-17 19:56:37 -05001635void
1636lpfc_sli_hbqbuf_free_all(struct lpfc_hba *phba)
1637{
James Smart92d7f7b2007-06-17 19:56:38 -05001638 struct lpfc_dmabuf *dmabuf, *next_dmabuf;
1639 struct hbq_dmabuf *hbq_buf;
James Smart3163f722008-02-08 18:50:25 -05001640 unsigned long flags;
James Smart51ef4c22007-08-02 11:10:31 -04001641 int i, hbq_count;
James Smart3163f722008-02-08 18:50:25 -05001642 uint32_t hbqno;
James Smarted957682007-06-17 19:56:37 -05001643
James Smart51ef4c22007-08-02 11:10:31 -04001644 hbq_count = lpfc_sli_hbq_count();
James Smarted957682007-06-17 19:56:37 -05001645 /* Return all memory used by all HBQs */
James Smart3163f722008-02-08 18:50:25 -05001646 spin_lock_irqsave(&phba->hbalock, flags);
James Smart51ef4c22007-08-02 11:10:31 -04001647 for (i = 0; i < hbq_count; ++i) {
1648 list_for_each_entry_safe(dmabuf, next_dmabuf,
1649 &phba->hbqs[i].hbq_buffer_list, list) {
1650 hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
1651 list_del(&hbq_buf->dbuf.list);
1652 (phba->hbqs[i].hbq_free_buffer)(phba, hbq_buf);
1653 }
James Smarta8adb832007-10-27 13:37:53 -04001654 phba->hbqs[i].buffer_count = 0;
James Smarted957682007-06-17 19:56:37 -05001655 }
James Smart3163f722008-02-08 18:50:25 -05001656 /* Return all HBQ buffer that are in-fly */
James Smart3772a992009-05-22 14:50:54 -04001657 list_for_each_entry_safe(dmabuf, next_dmabuf, &phba->rb_pend_list,
1658 list) {
James Smart3163f722008-02-08 18:50:25 -05001659 hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
1660 list_del(&hbq_buf->dbuf.list);
1661 if (hbq_buf->tag == -1) {
1662 (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
1663 (phba, hbq_buf);
1664 } else {
1665 hbqno = hbq_buf->tag >> 16;
1666 if (hbqno >= LPFC_MAX_HBQS)
1667 (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
1668 (phba, hbq_buf);
1669 else
1670 (phba->hbqs[hbqno].hbq_free_buffer)(phba,
1671 hbq_buf);
1672 }
1673 }
1674
1675 /* Mark the HBQs not in use */
1676 phba->hbq_in_use = 0;
1677 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smarted957682007-06-17 19:56:37 -05001678}
1679
James Smarte59058c2008-08-24 21:49:00 -04001680/**
James Smart3621a712009-04-06 18:47:14 -04001681 * lpfc_sli_hbq_to_firmware - Post the hbq buffer to firmware
James Smarte59058c2008-08-24 21:49:00 -04001682 * @phba: Pointer to HBA context object.
1683 * @hbqno: HBQ number.
1684 * @hbq_buf: Pointer to HBQ buffer.
1685 *
1686 * This function is called with the hbalock held to post a
1687 * hbq buffer to the firmware. If the function finds an empty
1688 * slot in the HBQ, it will post the buffer. The function will return
1689 * pointer to the hbq entry if it successfully post the buffer
1690 * else it will return NULL.
1691 **/
James Smart3772a992009-05-22 14:50:54 -04001692static int
James Smarted957682007-06-17 19:56:37 -05001693lpfc_sli_hbq_to_firmware(struct lpfc_hba *phba, uint32_t hbqno,
James Smart92d7f7b2007-06-17 19:56:38 -05001694 struct hbq_dmabuf *hbq_buf)
James Smarted957682007-06-17 19:56:37 -05001695{
James Smart3772a992009-05-22 14:50:54 -04001696 return phba->lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buf);
1697}
1698
1699/**
1700 * lpfc_sli_hbq_to_firmware_s3 - Post the hbq buffer to SLI3 firmware
1701 * @phba: Pointer to HBA context object.
1702 * @hbqno: HBQ number.
1703 * @hbq_buf: Pointer to HBQ buffer.
1704 *
1705 * This function is called with the hbalock held to post a hbq buffer to the
1706 * firmware. If the function finds an empty slot in the HBQ, it will post the
1707 * buffer and place it on the hbq_buffer_list. The function will return zero if
1708 * it successfully post the buffer else it will return an error.
1709 **/
1710static int
1711lpfc_sli_hbq_to_firmware_s3(struct lpfc_hba *phba, uint32_t hbqno,
1712 struct hbq_dmabuf *hbq_buf)
1713{
James Smarted957682007-06-17 19:56:37 -05001714 struct lpfc_hbq_entry *hbqe;
James Smart92d7f7b2007-06-17 19:56:38 -05001715 dma_addr_t physaddr = hbq_buf->dbuf.phys;
James Smarted957682007-06-17 19:56:37 -05001716
1717 /* Get next HBQ entry slot to use */
1718 hbqe = lpfc_sli_next_hbq_slot(phba, hbqno);
1719 if (hbqe) {
1720 struct hbq_s *hbqp = &phba->hbqs[hbqno];
1721
James Smart92d7f7b2007-06-17 19:56:38 -05001722 hbqe->bde.addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
1723 hbqe->bde.addrLow = le32_to_cpu(putPaddrLow(physaddr));
James Smart51ef4c22007-08-02 11:10:31 -04001724 hbqe->bde.tus.f.bdeSize = hbq_buf->size;
James Smarted957682007-06-17 19:56:37 -05001725 hbqe->bde.tus.f.bdeFlags = 0;
James Smart92d7f7b2007-06-17 19:56:38 -05001726 hbqe->bde.tus.w = le32_to_cpu(hbqe->bde.tus.w);
1727 hbqe->buffer_tag = le32_to_cpu(hbq_buf->tag);
1728 /* Sync SLIM */
James Smarted957682007-06-17 19:56:37 -05001729 hbqp->hbqPutIdx = hbqp->next_hbqPutIdx;
1730 writel(hbqp->hbqPutIdx, phba->hbq_put + hbqno);
James Smart92d7f7b2007-06-17 19:56:38 -05001731 /* flush */
James Smarted957682007-06-17 19:56:37 -05001732 readl(phba->hbq_put + hbqno);
James Smart51ef4c22007-08-02 11:10:31 -04001733 list_add_tail(&hbq_buf->dbuf.list, &hbqp->hbq_buffer_list);
James Smart3772a992009-05-22 14:50:54 -04001734 return 0;
1735 } else
1736 return -ENOMEM;
James Smarted957682007-06-17 19:56:37 -05001737}
1738
James Smart4f774512009-05-22 14:52:35 -04001739/**
1740 * lpfc_sli_hbq_to_firmware_s4 - Post the hbq buffer to SLI4 firmware
1741 * @phba: Pointer to HBA context object.
1742 * @hbqno: HBQ number.
1743 * @hbq_buf: Pointer to HBQ buffer.
1744 *
1745 * This function is called with the hbalock held to post an RQE to the SLI4
1746 * firmware. If able to post the RQE to the RQ it will queue the hbq entry to
1747 * the hbq_buffer_list and return zero, otherwise it will return an error.
1748 **/
1749static int
1750lpfc_sli_hbq_to_firmware_s4(struct lpfc_hba *phba, uint32_t hbqno,
1751 struct hbq_dmabuf *hbq_buf)
1752{
1753 int rc;
1754 struct lpfc_rqe hrqe;
1755 struct lpfc_rqe drqe;
1756
1757 hrqe.address_lo = putPaddrLow(hbq_buf->hbuf.phys);
1758 hrqe.address_hi = putPaddrHigh(hbq_buf->hbuf.phys);
1759 drqe.address_lo = putPaddrLow(hbq_buf->dbuf.phys);
1760 drqe.address_hi = putPaddrHigh(hbq_buf->dbuf.phys);
1761 rc = lpfc_sli4_rq_put(phba->sli4_hba.hdr_rq, phba->sli4_hba.dat_rq,
1762 &hrqe, &drqe);
1763 if (rc < 0)
1764 return rc;
1765 hbq_buf->tag = rc;
1766 list_add_tail(&hbq_buf->dbuf.list, &phba->hbqs[hbqno].hbq_buffer_list);
1767 return 0;
1768}
1769
James Smarte59058c2008-08-24 21:49:00 -04001770/* HBQ for ELS and CT traffic. */
James Smart92d7f7b2007-06-17 19:56:38 -05001771static struct lpfc_hbq_init lpfc_els_hbq = {
1772 .rn = 1,
James Smartdef9c7a2009-12-21 17:02:28 -05001773 .entry_count = 256,
James Smart92d7f7b2007-06-17 19:56:38 -05001774 .mask_count = 0,
1775 .profile = 0,
James Smart51ef4c22007-08-02 11:10:31 -04001776 .ring_mask = (1 << LPFC_ELS_RING),
James Smart92d7f7b2007-06-17 19:56:38 -05001777 .buffer_count = 0,
James Smarta257bf92009-04-06 18:48:10 -04001778 .init_count = 40,
1779 .add_count = 40,
James Smart92d7f7b2007-06-17 19:56:38 -05001780};
James Smarted957682007-06-17 19:56:37 -05001781
James Smarte59058c2008-08-24 21:49:00 -04001782/* HBQ for the extra ring if needed */
James Smart51ef4c22007-08-02 11:10:31 -04001783static struct lpfc_hbq_init lpfc_extra_hbq = {
1784 .rn = 1,
1785 .entry_count = 200,
1786 .mask_count = 0,
1787 .profile = 0,
1788 .ring_mask = (1 << LPFC_EXTRA_RING),
1789 .buffer_count = 0,
1790 .init_count = 0,
1791 .add_count = 5,
1792};
1793
James Smarte59058c2008-08-24 21:49:00 -04001794/* Array of HBQs */
James Smart78b2d852007-08-02 11:10:21 -04001795struct lpfc_hbq_init *lpfc_hbq_defs[] = {
James Smart92d7f7b2007-06-17 19:56:38 -05001796 &lpfc_els_hbq,
James Smart51ef4c22007-08-02 11:10:31 -04001797 &lpfc_extra_hbq,
James Smart92d7f7b2007-06-17 19:56:38 -05001798};
1799
James Smarte59058c2008-08-24 21:49:00 -04001800/**
James Smart3621a712009-04-06 18:47:14 -04001801 * lpfc_sli_hbqbuf_fill_hbqs - Post more hbq buffers to HBQ
James Smarte59058c2008-08-24 21:49:00 -04001802 * @phba: Pointer to HBA context object.
1803 * @hbqno: HBQ number.
1804 * @count: Number of HBQ buffers to be posted.
1805 *
James Smartd7c255b2008-08-24 21:50:00 -04001806 * This function is called with no lock held to post more hbq buffers to the
1807 * given HBQ. The function returns the number of HBQ buffers successfully
1808 * posted.
James Smarte59058c2008-08-24 21:49:00 -04001809 **/
James Smart311464e2007-08-02 11:10:37 -04001810static int
James Smart92d7f7b2007-06-17 19:56:38 -05001811lpfc_sli_hbqbuf_fill_hbqs(struct lpfc_hba *phba, uint32_t hbqno, uint32_t count)
1812{
James Smartd7c255b2008-08-24 21:50:00 -04001813 uint32_t i, posted = 0;
James Smart3163f722008-02-08 18:50:25 -05001814 unsigned long flags;
James Smart92d7f7b2007-06-17 19:56:38 -05001815 struct hbq_dmabuf *hbq_buffer;
James Smartd7c255b2008-08-24 21:50:00 -04001816 LIST_HEAD(hbq_buf_list);
Matthew Wilcoxeafe1df2008-02-21 05:44:33 -07001817 if (!phba->hbqs[hbqno].hbq_alloc_buffer)
James Smart51ef4c22007-08-02 11:10:31 -04001818 return 0;
James Smart51ef4c22007-08-02 11:10:31 -04001819
James Smartd7c255b2008-08-24 21:50:00 -04001820 if ((phba->hbqs[hbqno].buffer_count + count) >
1821 lpfc_hbq_defs[hbqno]->entry_count)
1822 count = lpfc_hbq_defs[hbqno]->entry_count -
1823 phba->hbqs[hbqno].buffer_count;
1824 if (!count)
1825 return 0;
1826 /* Allocate HBQ entries */
1827 for (i = 0; i < count; i++) {
1828 hbq_buffer = (phba->hbqs[hbqno].hbq_alloc_buffer)(phba);
1829 if (!hbq_buffer)
1830 break;
1831 list_add_tail(&hbq_buffer->dbuf.list, &hbq_buf_list);
1832 }
James Smart3163f722008-02-08 18:50:25 -05001833 /* Check whether HBQ is still in use */
1834 spin_lock_irqsave(&phba->hbalock, flags);
Matthew Wilcoxeafe1df2008-02-21 05:44:33 -07001835 if (!phba->hbq_in_use)
James Smartd7c255b2008-08-24 21:50:00 -04001836 goto err;
1837 while (!list_empty(&hbq_buf_list)) {
1838 list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
1839 dbuf.list);
1840 hbq_buffer->tag = (phba->hbqs[hbqno].buffer_count |
1841 (hbqno << 16));
James Smart3772a992009-05-22 14:50:54 -04001842 if (!lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer)) {
James Smarta8adb832007-10-27 13:37:53 -04001843 phba->hbqs[hbqno].buffer_count++;
James Smartd7c255b2008-08-24 21:50:00 -04001844 posted++;
1845 } else
James Smart51ef4c22007-08-02 11:10:31 -04001846 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
James Smart92d7f7b2007-06-17 19:56:38 -05001847 }
James Smart3163f722008-02-08 18:50:25 -05001848 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smartd7c255b2008-08-24 21:50:00 -04001849 return posted;
1850err:
1851 spin_unlock_irqrestore(&phba->hbalock, flags);
1852 while (!list_empty(&hbq_buf_list)) {
1853 list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
1854 dbuf.list);
1855 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
1856 }
James Smart92d7f7b2007-06-17 19:56:38 -05001857 return 0;
James Smarted957682007-06-17 19:56:37 -05001858}
1859
James Smarte59058c2008-08-24 21:49:00 -04001860/**
James Smart3621a712009-04-06 18:47:14 -04001861 * lpfc_sli_hbqbuf_add_hbqs - Post more HBQ buffers to firmware
James Smarte59058c2008-08-24 21:49:00 -04001862 * @phba: Pointer to HBA context object.
1863 * @qno: HBQ number.
1864 *
1865 * This function posts more buffers to the HBQ. This function
James Smartd7c255b2008-08-24 21:50:00 -04001866 * is called with no lock held. The function returns the number of HBQ entries
1867 * successfully allocated.
James Smarte59058c2008-08-24 21:49:00 -04001868 **/
James Smarted957682007-06-17 19:56:37 -05001869int
James Smart92d7f7b2007-06-17 19:56:38 -05001870lpfc_sli_hbqbuf_add_hbqs(struct lpfc_hba *phba, uint32_t qno)
James Smarted957682007-06-17 19:56:37 -05001871{
James Smartdef9c7a2009-12-21 17:02:28 -05001872 if (phba->sli_rev == LPFC_SLI_REV4)
1873 return 0;
1874 else
1875 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
1876 lpfc_hbq_defs[qno]->add_count);
James Smarted957682007-06-17 19:56:37 -05001877}
1878
James Smarte59058c2008-08-24 21:49:00 -04001879/**
James Smart3621a712009-04-06 18:47:14 -04001880 * lpfc_sli_hbqbuf_init_hbqs - Post initial buffers to the HBQ
James Smarte59058c2008-08-24 21:49:00 -04001881 * @phba: Pointer to HBA context object.
1882 * @qno: HBQ queue number.
1883 *
1884 * This function is called from SLI initialization code path with
1885 * no lock held to post initial HBQ buffers to firmware. The
James Smartd7c255b2008-08-24 21:50:00 -04001886 * function returns the number of HBQ entries successfully allocated.
James Smarte59058c2008-08-24 21:49:00 -04001887 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01001888static int
James Smart92d7f7b2007-06-17 19:56:38 -05001889lpfc_sli_hbqbuf_init_hbqs(struct lpfc_hba *phba, uint32_t qno)
James Smarted957682007-06-17 19:56:37 -05001890{
James Smartdef9c7a2009-12-21 17:02:28 -05001891 if (phba->sli_rev == LPFC_SLI_REV4)
1892 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
1893 lpfc_hbq_defs[qno]->entry_count);
1894 else
1895 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
1896 lpfc_hbq_defs[qno]->init_count);
James Smarted957682007-06-17 19:56:37 -05001897}
1898
James Smarte59058c2008-08-24 21:49:00 -04001899/**
James Smart3772a992009-05-22 14:50:54 -04001900 * lpfc_sli_hbqbuf_get - Remove the first hbq off of an hbq list
1901 * @phba: Pointer to HBA context object.
1902 * @hbqno: HBQ number.
1903 *
1904 * This function removes the first hbq buffer on an hbq list and returns a
1905 * pointer to that buffer. If it finds no buffers on the list it returns NULL.
1906 **/
1907static struct hbq_dmabuf *
1908lpfc_sli_hbqbuf_get(struct list_head *rb_list)
1909{
1910 struct lpfc_dmabuf *d_buf;
1911
1912 list_remove_head(rb_list, d_buf, struct lpfc_dmabuf, list);
1913 if (!d_buf)
1914 return NULL;
1915 return container_of(d_buf, struct hbq_dmabuf, dbuf);
1916}
1917
1918/**
James Smart3621a712009-04-06 18:47:14 -04001919 * lpfc_sli_hbqbuf_find - Find the hbq buffer associated with a tag
James Smarte59058c2008-08-24 21:49:00 -04001920 * @phba: Pointer to HBA context object.
1921 * @tag: Tag of the hbq buffer.
1922 *
1923 * This function is called with hbalock held. This function searches
1924 * for the hbq buffer associated with the given tag in the hbq buffer
1925 * list. If it finds the hbq buffer, it returns the hbq_buffer other wise
1926 * it returns NULL.
1927 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01001928static struct hbq_dmabuf *
James Smarted957682007-06-17 19:56:37 -05001929lpfc_sli_hbqbuf_find(struct lpfc_hba *phba, uint32_t tag)
1930{
James Smart92d7f7b2007-06-17 19:56:38 -05001931 struct lpfc_dmabuf *d_buf;
1932 struct hbq_dmabuf *hbq_buf;
James Smart51ef4c22007-08-02 11:10:31 -04001933 uint32_t hbqno;
James Smarted957682007-06-17 19:56:37 -05001934
James Smart51ef4c22007-08-02 11:10:31 -04001935 hbqno = tag >> 16;
Jesper Juhla0a74e452007-08-09 20:47:15 +02001936 if (hbqno >= LPFC_MAX_HBQS)
James Smart51ef4c22007-08-02 11:10:31 -04001937 return NULL;
1938
James Smart3772a992009-05-22 14:50:54 -04001939 spin_lock_irq(&phba->hbalock);
James Smart51ef4c22007-08-02 11:10:31 -04001940 list_for_each_entry(d_buf, &phba->hbqs[hbqno].hbq_buffer_list, list) {
James Smart92d7f7b2007-06-17 19:56:38 -05001941 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
James Smart51ef4c22007-08-02 11:10:31 -04001942 if (hbq_buf->tag == tag) {
James Smart3772a992009-05-22 14:50:54 -04001943 spin_unlock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -05001944 return hbq_buf;
James Smarted957682007-06-17 19:56:37 -05001945 }
1946 }
James Smart3772a992009-05-22 14:50:54 -04001947 spin_unlock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -05001948 lpfc_printf_log(phba, KERN_ERR, LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04001949 "1803 Bad hbq tag. Data: x%x x%x\n",
James Smarta8adb832007-10-27 13:37:53 -04001950 tag, phba->hbqs[tag >> 16].buffer_count);
James Smart92d7f7b2007-06-17 19:56:38 -05001951 return NULL;
James Smarted957682007-06-17 19:56:37 -05001952}
1953
James Smarte59058c2008-08-24 21:49:00 -04001954/**
James Smart3621a712009-04-06 18:47:14 -04001955 * lpfc_sli_free_hbq - Give back the hbq buffer to firmware
James Smarte59058c2008-08-24 21:49:00 -04001956 * @phba: Pointer to HBA context object.
1957 * @hbq_buffer: Pointer to HBQ buffer.
1958 *
1959 * This function is called with hbalock. This function gives back
1960 * the hbq buffer to firmware. If the HBQ does not have space to
1961 * post the buffer, it will free the buffer.
1962 **/
James Smarted957682007-06-17 19:56:37 -05001963void
James Smart51ef4c22007-08-02 11:10:31 -04001964lpfc_sli_free_hbq(struct lpfc_hba *phba, struct hbq_dmabuf *hbq_buffer)
James Smarted957682007-06-17 19:56:37 -05001965{
1966 uint32_t hbqno;
1967
James Smart51ef4c22007-08-02 11:10:31 -04001968 if (hbq_buffer) {
1969 hbqno = hbq_buffer->tag >> 16;
James Smart3772a992009-05-22 14:50:54 -04001970 if (lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer))
James Smart51ef4c22007-08-02 11:10:31 -04001971 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
James Smarted957682007-06-17 19:56:37 -05001972 }
1973}
1974
James Smarte59058c2008-08-24 21:49:00 -04001975/**
James Smart3621a712009-04-06 18:47:14 -04001976 * lpfc_sli_chk_mbx_command - Check if the mailbox is a legitimate mailbox
James Smarte59058c2008-08-24 21:49:00 -04001977 * @mbxCommand: mailbox command code.
1978 *
1979 * This function is called by the mailbox event handler function to verify
1980 * that the completed mailbox command is a legitimate mailbox command. If the
1981 * completed mailbox is not known to the function, it will return MBX_SHUTDOWN
1982 * and the mailbox event handler will take the HBA offline.
1983 **/
dea31012005-04-17 16:05:31 -05001984static int
1985lpfc_sli_chk_mbx_command(uint8_t mbxCommand)
1986{
1987 uint8_t ret;
1988
1989 switch (mbxCommand) {
1990 case MBX_LOAD_SM:
1991 case MBX_READ_NV:
1992 case MBX_WRITE_NV:
James Smarta8adb832007-10-27 13:37:53 -04001993 case MBX_WRITE_VPARMS:
dea31012005-04-17 16:05:31 -05001994 case MBX_RUN_BIU_DIAG:
1995 case MBX_INIT_LINK:
1996 case MBX_DOWN_LINK:
1997 case MBX_CONFIG_LINK:
1998 case MBX_CONFIG_RING:
1999 case MBX_RESET_RING:
2000 case MBX_READ_CONFIG:
2001 case MBX_READ_RCONFIG:
2002 case MBX_READ_SPARM:
2003 case MBX_READ_STATUS:
2004 case MBX_READ_RPI:
2005 case MBX_READ_XRI:
2006 case MBX_READ_REV:
2007 case MBX_READ_LNK_STAT:
2008 case MBX_REG_LOGIN:
2009 case MBX_UNREG_LOGIN:
dea31012005-04-17 16:05:31 -05002010 case MBX_CLEAR_LA:
2011 case MBX_DUMP_MEMORY:
2012 case MBX_DUMP_CONTEXT:
2013 case MBX_RUN_DIAGS:
2014 case MBX_RESTART:
2015 case MBX_UPDATE_CFG:
2016 case MBX_DOWN_LOAD:
2017 case MBX_DEL_LD_ENTRY:
2018 case MBX_RUN_PROGRAM:
2019 case MBX_SET_MASK:
James Smart09372822008-01-11 01:52:54 -05002020 case MBX_SET_VARIABLE:
dea31012005-04-17 16:05:31 -05002021 case MBX_UNREG_D_ID:
Jamie Wellnitz41415862006-02-28 19:25:27 -05002022 case MBX_KILL_BOARD:
dea31012005-04-17 16:05:31 -05002023 case MBX_CONFIG_FARP:
Jamie Wellnitz41415862006-02-28 19:25:27 -05002024 case MBX_BEACON:
dea31012005-04-17 16:05:31 -05002025 case MBX_LOAD_AREA:
2026 case MBX_RUN_BIU_DIAG64:
2027 case MBX_CONFIG_PORT:
2028 case MBX_READ_SPARM64:
2029 case MBX_READ_RPI64:
2030 case MBX_REG_LOGIN64:
James Smart76a95d72010-11-20 23:11:48 -05002031 case MBX_READ_TOPOLOGY:
James Smart09372822008-01-11 01:52:54 -05002032 case MBX_WRITE_WWN:
dea31012005-04-17 16:05:31 -05002033 case MBX_SET_DEBUG:
2034 case MBX_LOAD_EXP_ROM:
James Smart57127f12007-10-27 13:37:05 -04002035 case MBX_ASYNCEVT_ENABLE:
James Smart92d7f7b2007-06-17 19:56:38 -05002036 case MBX_REG_VPI:
2037 case MBX_UNREG_VPI:
James Smart858c9f62007-06-17 19:56:39 -05002038 case MBX_HEARTBEAT:
James Smart84774a42008-08-24 21:50:06 -04002039 case MBX_PORT_CAPABILITIES:
2040 case MBX_PORT_IOV_CONTROL:
James Smart04c68492009-05-22 14:52:52 -04002041 case MBX_SLI4_CONFIG:
2042 case MBX_SLI4_REQ_FTRS:
2043 case MBX_REG_FCFI:
2044 case MBX_UNREG_FCFI:
2045 case MBX_REG_VFI:
2046 case MBX_UNREG_VFI:
2047 case MBX_INIT_VPI:
2048 case MBX_INIT_VFI:
2049 case MBX_RESUME_RPI:
James Smartc7495932010-04-06 15:05:28 -04002050 case MBX_READ_EVENT_LOG_STATUS:
2051 case MBX_READ_EVENT_LOG:
James Smartdcf2a4e2010-09-29 11:18:53 -04002052 case MBX_SECURITY_MGMT:
2053 case MBX_AUTH_PORT:
dea31012005-04-17 16:05:31 -05002054 ret = mbxCommand;
2055 break;
2056 default:
2057 ret = MBX_SHUTDOWN;
2058 break;
2059 }
James Smart2e0fef82007-06-17 19:56:36 -05002060 return ret;
dea31012005-04-17 16:05:31 -05002061}
James Smarte59058c2008-08-24 21:49:00 -04002062
2063/**
James Smart3621a712009-04-06 18:47:14 -04002064 * lpfc_sli_wake_mbox_wait - lpfc_sli_issue_mbox_wait mbox completion handler
James Smarte59058c2008-08-24 21:49:00 -04002065 * @phba: Pointer to HBA context object.
2066 * @pmboxq: Pointer to mailbox command.
2067 *
2068 * This is completion handler function for mailbox commands issued from
2069 * lpfc_sli_issue_mbox_wait function. This function is called by the
2070 * mailbox event handler function with no lock held. This function
2071 * will wake up thread waiting on the wait queue pointed by context1
2072 * of the mailbox.
2073 **/
James Smart04c68492009-05-22 14:52:52 -04002074void
James Smart2e0fef82007-06-17 19:56:36 -05002075lpfc_sli_wake_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
dea31012005-04-17 16:05:31 -05002076{
2077 wait_queue_head_t *pdone_q;
James Smart858c9f62007-06-17 19:56:39 -05002078 unsigned long drvr_flag;
dea31012005-04-17 16:05:31 -05002079
2080 /*
2081 * If pdone_q is empty, the driver thread gave up waiting and
2082 * continued running.
2083 */
James Smart7054a602007-04-25 09:52:34 -04002084 pmboxq->mbox_flag |= LPFC_MBX_WAKE;
James Smart858c9f62007-06-17 19:56:39 -05002085 spin_lock_irqsave(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05002086 pdone_q = (wait_queue_head_t *) pmboxq->context1;
2087 if (pdone_q)
2088 wake_up_interruptible(pdone_q);
James Smart858c9f62007-06-17 19:56:39 -05002089 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05002090 return;
2091}
2092
James Smarte59058c2008-08-24 21:49:00 -04002093
2094/**
James Smart3621a712009-04-06 18:47:14 -04002095 * lpfc_sli_def_mbox_cmpl - Default mailbox completion handler
James Smarte59058c2008-08-24 21:49:00 -04002096 * @phba: Pointer to HBA context object.
2097 * @pmb: Pointer to mailbox object.
2098 *
2099 * This function is the default mailbox completion handler. It
2100 * frees the memory resources associated with the completed mailbox
2101 * command. If the completed command is a REG_LOGIN mailbox command,
2102 * this function will issue a UREG_LOGIN to re-claim the RPI.
2103 **/
dea31012005-04-17 16:05:31 -05002104void
James Smart2e0fef82007-06-17 19:56:36 -05002105lpfc_sli_def_mbox_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05002106{
James Smartd439d282010-09-29 11:18:45 -04002107 struct lpfc_vport *vport = pmb->vport;
dea31012005-04-17 16:05:31 -05002108 struct lpfc_dmabuf *mp;
James Smartd439d282010-09-29 11:18:45 -04002109 struct lpfc_nodelist *ndlp;
James Smart5af5eee2010-10-22 11:06:38 -04002110 struct Scsi_Host *shost;
James Smart04c68492009-05-22 14:52:52 -04002111 uint16_t rpi, vpi;
James Smart7054a602007-04-25 09:52:34 -04002112 int rc;
2113
dea31012005-04-17 16:05:31 -05002114 mp = (struct lpfc_dmabuf *) (pmb->context1);
James Smart7054a602007-04-25 09:52:34 -04002115
dea31012005-04-17 16:05:31 -05002116 if (mp) {
2117 lpfc_mbuf_free(phba, mp->virt, mp->phys);
2118 kfree(mp);
2119 }
James Smart7054a602007-04-25 09:52:34 -04002120
2121 /*
2122 * If a REG_LOGIN succeeded after node is destroyed or node
2123 * is in re-discovery driver need to cleanup the RPI.
2124 */
James Smart2e0fef82007-06-17 19:56:36 -05002125 if (!(phba->pport->load_flag & FC_UNLOADING) &&
James Smart04c68492009-05-22 14:52:52 -04002126 pmb->u.mb.mbxCommand == MBX_REG_LOGIN64 &&
2127 !pmb->u.mb.mbxStatus) {
2128 rpi = pmb->u.mb.un.varWords[0];
2129 vpi = pmb->u.mb.un.varRegLogin.vpi - phba->vpi_base;
2130 lpfc_unreg_login(phba, vpi, rpi, pmb);
James Smart92d7f7b2007-06-17 19:56:38 -05002131 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart7054a602007-04-25 09:52:34 -04002132 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
2133 if (rc != MBX_NOT_FINISHED)
2134 return;
2135 }
2136
James Smart695a8142010-01-26 23:08:03 -05002137 if ((pmb->u.mb.mbxCommand == MBX_REG_VPI) &&
2138 !(phba->pport->load_flag & FC_UNLOADING) &&
2139 !pmb->u.mb.mbxStatus) {
James Smart5af5eee2010-10-22 11:06:38 -04002140 shost = lpfc_shost_from_vport(vport);
2141 spin_lock_irq(shost->host_lock);
2142 vport->vpi_state |= LPFC_VPI_REGISTERED;
2143 vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
2144 spin_unlock_irq(shost->host_lock);
James Smart695a8142010-01-26 23:08:03 -05002145 }
2146
James Smartd439d282010-09-29 11:18:45 -04002147 if (pmb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
2148 ndlp = (struct lpfc_nodelist *)pmb->context2;
2149 lpfc_nlp_put(ndlp);
2150 pmb->context2 = NULL;
2151 }
2152
James Smartdcf2a4e2010-09-29 11:18:53 -04002153 /* Check security permission status on INIT_LINK mailbox command */
2154 if ((pmb->u.mb.mbxCommand == MBX_INIT_LINK) &&
2155 (pmb->u.mb.mbxStatus == MBXERR_SEC_NO_PERMISSION))
2156 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
2157 "2860 SLI authentication is required "
2158 "for INIT_LINK but has not done yet\n");
2159
James Smart04c68492009-05-22 14:52:52 -04002160 if (bf_get(lpfc_mqe_command, &pmb->u.mqe) == MBX_SLI4_CONFIG)
2161 lpfc_sli4_mbox_cmd_free(phba, pmb);
2162 else
2163 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05002164}
2165
James Smarte59058c2008-08-24 21:49:00 -04002166/**
James Smart3621a712009-04-06 18:47:14 -04002167 * lpfc_sli_handle_mb_event - Handle mailbox completions from firmware
James Smarte59058c2008-08-24 21:49:00 -04002168 * @phba: Pointer to HBA context object.
2169 *
2170 * This function is called with no lock held. This function processes all
2171 * the completed mailbox commands and gives it to upper layers. The interrupt
2172 * service routine processes mailbox completion interrupt and adds completed
2173 * mailbox commands to the mboxq_cmpl queue and signals the worker thread.
2174 * Worker thread call lpfc_sli_handle_mb_event, which will return the
2175 * completed mailbox commands in mboxq_cmpl queue to the upper layers. This
2176 * function returns the mailbox commands to the upper layer by calling the
2177 * completion handler function of each mailbox.
2178 **/
dea31012005-04-17 16:05:31 -05002179int
James Smart2e0fef82007-06-17 19:56:36 -05002180lpfc_sli_handle_mb_event(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05002181{
James Smart92d7f7b2007-06-17 19:56:38 -05002182 MAILBOX_t *pmbox;
dea31012005-04-17 16:05:31 -05002183 LPFC_MBOXQ_t *pmb;
James Smart92d7f7b2007-06-17 19:56:38 -05002184 int rc;
2185 LIST_HEAD(cmplq);
dea31012005-04-17 16:05:31 -05002186
2187 phba->sli.slistat.mbox_event++;
2188
James Smart92d7f7b2007-06-17 19:56:38 -05002189 /* Get all completed mailboxe buffers into the cmplq */
2190 spin_lock_irq(&phba->hbalock);
2191 list_splice_init(&phba->sli.mboxq_cmpl, &cmplq);
2192 spin_unlock_irq(&phba->hbalock);
2193
dea31012005-04-17 16:05:31 -05002194 /* Get a Mailbox buffer to setup mailbox commands for callback */
James Smart92d7f7b2007-06-17 19:56:38 -05002195 do {
2196 list_remove_head(&cmplq, pmb, LPFC_MBOXQ_t, list);
2197 if (pmb == NULL)
2198 break;
2199
James Smart04c68492009-05-22 14:52:52 -04002200 pmbox = &pmb->u.mb;
dea31012005-04-17 16:05:31 -05002201
James Smart858c9f62007-06-17 19:56:39 -05002202 if (pmbox->mbxCommand != MBX_HEARTBEAT) {
2203 if (pmb->vport) {
2204 lpfc_debugfs_disc_trc(pmb->vport,
2205 LPFC_DISC_TRC_MBOX_VPORT,
2206 "MBOX cmpl vport: cmd:x%x mb:x%x x%x",
2207 (uint32_t)pmbox->mbxCommand,
2208 pmbox->un.varWords[0],
2209 pmbox->un.varWords[1]);
2210 }
2211 else {
2212 lpfc_debugfs_disc_trc(phba->pport,
2213 LPFC_DISC_TRC_MBOX,
2214 "MBOX cmpl: cmd:x%x mb:x%x x%x",
2215 (uint32_t)pmbox->mbxCommand,
2216 pmbox->un.varWords[0],
2217 pmbox->un.varWords[1]);
2218 }
2219 }
2220
dea31012005-04-17 16:05:31 -05002221 /*
2222 * It is a fatal error if unknown mbox command completion.
2223 */
2224 if (lpfc_sli_chk_mbx_command(pmbox->mbxCommand) ==
2225 MBX_SHUTDOWN) {
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02002226 /* Unknown mailbox command compl */
James Smart92d7f7b2007-06-17 19:56:38 -05002227 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002228 "(%d):0323 Unknown Mailbox command "
James Smart04c68492009-05-22 14:52:52 -04002229 "x%x (x%x) Cmpl\n",
James Smart92d7f7b2007-06-17 19:56:38 -05002230 pmb->vport ? pmb->vport->vpi : 0,
James Smart04c68492009-05-22 14:52:52 -04002231 pmbox->mbxCommand,
2232 lpfc_sli4_mbox_opcode_get(phba, pmb));
James Smart2e0fef82007-06-17 19:56:36 -05002233 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05002234 phba->work_hs = HS_FFER3;
2235 lpfc_handle_eratt(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05002236 continue;
dea31012005-04-17 16:05:31 -05002237 }
2238
dea31012005-04-17 16:05:31 -05002239 if (pmbox->mbxStatus) {
2240 phba->sli.slistat.mbox_stat_err++;
2241 if (pmbox->mbxStatus == MBXERR_NO_RESOURCES) {
2242 /* Mbox cmd cmpl error - RETRYing */
James Smart92d7f7b2007-06-17 19:56:38 -05002243 lpfc_printf_log(phba, KERN_INFO,
2244 LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002245 "(%d):0305 Mbox cmd cmpl "
James Smart92d7f7b2007-06-17 19:56:38 -05002246 "error - RETRYing Data: x%x "
James Smart04c68492009-05-22 14:52:52 -04002247 "(x%x) x%x x%x x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -05002248 pmb->vport ? pmb->vport->vpi :0,
2249 pmbox->mbxCommand,
James Smart04c68492009-05-22 14:52:52 -04002250 lpfc_sli4_mbox_opcode_get(phba,
2251 pmb),
James Smart92d7f7b2007-06-17 19:56:38 -05002252 pmbox->mbxStatus,
2253 pmbox->un.varWords[0],
2254 pmb->vport->port_state);
dea31012005-04-17 16:05:31 -05002255 pmbox->mbxStatus = 0;
2256 pmbox->mbxOwner = OWN_HOST;
dea31012005-04-17 16:05:31 -05002257 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
James Smart04c68492009-05-22 14:52:52 -04002258 if (rc != MBX_NOT_FINISHED)
James Smart92d7f7b2007-06-17 19:56:38 -05002259 continue;
dea31012005-04-17 16:05:31 -05002260 }
2261 }
2262
2263 /* Mailbox cmd <cmd> Cmpl <cmpl> */
James Smart92d7f7b2007-06-17 19:56:38 -05002264 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smart04c68492009-05-22 14:52:52 -04002265 "(%d):0307 Mailbox cmd x%x (x%x) Cmpl x%p "
dea31012005-04-17 16:05:31 -05002266 "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 -05002267 pmb->vport ? pmb->vport->vpi : 0,
dea31012005-04-17 16:05:31 -05002268 pmbox->mbxCommand,
James Smart04c68492009-05-22 14:52:52 -04002269 lpfc_sli4_mbox_opcode_get(phba, pmb),
dea31012005-04-17 16:05:31 -05002270 pmb->mbox_cmpl,
2271 *((uint32_t *) pmbox),
2272 pmbox->un.varWords[0],
2273 pmbox->un.varWords[1],
2274 pmbox->un.varWords[2],
2275 pmbox->un.varWords[3],
2276 pmbox->un.varWords[4],
2277 pmbox->un.varWords[5],
2278 pmbox->un.varWords[6],
2279 pmbox->un.varWords[7]);
2280
James Smart92d7f7b2007-06-17 19:56:38 -05002281 if (pmb->mbox_cmpl)
dea31012005-04-17 16:05:31 -05002282 pmb->mbox_cmpl(phba,pmb);
James Smart92d7f7b2007-06-17 19:56:38 -05002283 } while (1);
James Smart2e0fef82007-06-17 19:56:36 -05002284 return 0;
dea31012005-04-17 16:05:31 -05002285}
James Smart92d7f7b2007-06-17 19:56:38 -05002286
James Smarte59058c2008-08-24 21:49:00 -04002287/**
James Smart3621a712009-04-06 18:47:14 -04002288 * lpfc_sli_get_buff - Get the buffer associated with the buffer tag
James Smarte59058c2008-08-24 21:49:00 -04002289 * @phba: Pointer to HBA context object.
2290 * @pring: Pointer to driver SLI ring object.
2291 * @tag: buffer tag.
2292 *
2293 * This function is called with no lock held. When QUE_BUFTAG_BIT bit
2294 * is set in the tag the buffer is posted for a particular exchange,
2295 * the function will return the buffer without replacing the buffer.
2296 * If the buffer is for unsolicited ELS or CT traffic, this function
2297 * returns the buffer and also posts another buffer to the firmware.
2298 **/
James Smart76bb24e2007-10-27 13:38:00 -04002299static struct lpfc_dmabuf *
2300lpfc_sli_get_buff(struct lpfc_hba *phba,
James Smart9f1e1b52008-12-04 22:39:40 -05002301 struct lpfc_sli_ring *pring,
2302 uint32_t tag)
James Smart76bb24e2007-10-27 13:38:00 -04002303{
James Smart9f1e1b52008-12-04 22:39:40 -05002304 struct hbq_dmabuf *hbq_entry;
2305
James Smart76bb24e2007-10-27 13:38:00 -04002306 if (tag & QUE_BUFTAG_BIT)
2307 return lpfc_sli_ring_taggedbuf_get(phba, pring, tag);
James Smart9f1e1b52008-12-04 22:39:40 -05002308 hbq_entry = lpfc_sli_hbqbuf_find(phba, tag);
2309 if (!hbq_entry)
2310 return NULL;
2311 return &hbq_entry->dbuf;
James Smart76bb24e2007-10-27 13:38:00 -04002312}
James Smart57127f12007-10-27 13:37:05 -04002313
James Smart3772a992009-05-22 14:50:54 -04002314/**
2315 * lpfc_complete_unsol_iocb - Complete an unsolicited sequence
2316 * @phba: Pointer to HBA context object.
2317 * @pring: Pointer to driver SLI ring object.
2318 * @saveq: Pointer to the iocbq struct representing the sequence starting frame.
2319 * @fch_r_ctl: the r_ctl for the first frame of the sequence.
2320 * @fch_type: the type for the first frame of the sequence.
2321 *
2322 * This function is called with no lock held. This function uses the r_ctl and
2323 * type of the received sequence to find the correct callback function to call
2324 * to process the sequence.
2325 **/
2326static int
2327lpfc_complete_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2328 struct lpfc_iocbq *saveq, uint32_t fch_r_ctl,
2329 uint32_t fch_type)
2330{
2331 int i;
2332
2333 /* unSolicited Responses */
2334 if (pring->prt[0].profile) {
2335 if (pring->prt[0].lpfc_sli_rcv_unsol_event)
2336 (pring->prt[0].lpfc_sli_rcv_unsol_event) (phba, pring,
2337 saveq);
2338 return 1;
2339 }
2340 /* We must search, based on rctl / type
2341 for the right routine */
2342 for (i = 0; i < pring->num_mask; i++) {
2343 if ((pring->prt[i].rctl == fch_r_ctl) &&
2344 (pring->prt[i].type == fch_type)) {
2345 if (pring->prt[i].lpfc_sli_rcv_unsol_event)
2346 (pring->prt[i].lpfc_sli_rcv_unsol_event)
2347 (phba, pring, saveq);
2348 return 1;
2349 }
2350 }
2351 return 0;
2352}
James Smarte59058c2008-08-24 21:49:00 -04002353
2354/**
James Smart3621a712009-04-06 18:47:14 -04002355 * lpfc_sli_process_unsol_iocb - Unsolicited iocb handler
James Smarte59058c2008-08-24 21:49:00 -04002356 * @phba: Pointer to HBA context object.
2357 * @pring: Pointer to driver SLI ring object.
2358 * @saveq: Pointer to the unsolicited iocb.
2359 *
2360 * This function is called with no lock held by the ring event handler
2361 * when there is an unsolicited iocb posted to the response ring by the
2362 * firmware. This function gets the buffer associated with the iocbs
2363 * and calls the event handler for the ring. This function handles both
2364 * qring buffers and hbq buffers.
2365 * When the function returns 1 the caller can free the iocb object otherwise
2366 * upper layer functions will free the iocb objects.
2367 **/
dea31012005-04-17 16:05:31 -05002368static int
2369lpfc_sli_process_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2370 struct lpfc_iocbq *saveq)
2371{
2372 IOCB_t * irsp;
2373 WORD5 * w5p;
2374 uint32_t Rctl, Type;
James Smart3772a992009-05-22 14:50:54 -04002375 uint32_t match;
James Smart76bb24e2007-10-27 13:38:00 -04002376 struct lpfc_iocbq *iocbq;
James Smart3163f722008-02-08 18:50:25 -05002377 struct lpfc_dmabuf *dmzbuf;
dea31012005-04-17 16:05:31 -05002378
2379 match = 0;
2380 irsp = &(saveq->iocb);
James Smart57127f12007-10-27 13:37:05 -04002381
2382 if (irsp->ulpCommand == CMD_ASYNC_STATUS) {
2383 if (pring->lpfc_sli_rcv_async_status)
2384 pring->lpfc_sli_rcv_async_status(phba, pring, saveq);
2385 else
2386 lpfc_printf_log(phba,
2387 KERN_WARNING,
2388 LOG_SLI,
2389 "0316 Ring %d handler: unexpected "
2390 "ASYNC_STATUS iocb received evt_code "
2391 "0x%x\n",
2392 pring->ringno,
2393 irsp->un.asyncstat.evt_code);
2394 return 1;
2395 }
2396
James Smart3163f722008-02-08 18:50:25 -05002397 if ((irsp->ulpCommand == CMD_IOCB_RET_XRI64_CX) &&
2398 (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)) {
2399 if (irsp->ulpBdeCount > 0) {
2400 dmzbuf = lpfc_sli_get_buff(phba, pring,
2401 irsp->un.ulpWord[3]);
2402 lpfc_in_buf_free(phba, dmzbuf);
2403 }
2404
2405 if (irsp->ulpBdeCount > 1) {
2406 dmzbuf = lpfc_sli_get_buff(phba, pring,
2407 irsp->unsli3.sli3Words[3]);
2408 lpfc_in_buf_free(phba, dmzbuf);
2409 }
2410
2411 if (irsp->ulpBdeCount > 2) {
2412 dmzbuf = lpfc_sli_get_buff(phba, pring,
2413 irsp->unsli3.sli3Words[7]);
2414 lpfc_in_buf_free(phba, dmzbuf);
2415 }
2416
2417 return 1;
2418 }
2419
James Smart92d7f7b2007-06-17 19:56:38 -05002420 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
James Smart76bb24e2007-10-27 13:38:00 -04002421 if (irsp->ulpBdeCount != 0) {
2422 saveq->context2 = lpfc_sli_get_buff(phba, pring,
James Smart92d7f7b2007-06-17 19:56:38 -05002423 irsp->un.ulpWord[3]);
James Smart76bb24e2007-10-27 13:38:00 -04002424 if (!saveq->context2)
2425 lpfc_printf_log(phba,
2426 KERN_ERR,
2427 LOG_SLI,
2428 "0341 Ring %d Cannot find buffer for "
2429 "an unsolicited iocb. tag 0x%x\n",
2430 pring->ringno,
2431 irsp->un.ulpWord[3]);
James Smart76bb24e2007-10-27 13:38:00 -04002432 }
2433 if (irsp->ulpBdeCount == 2) {
2434 saveq->context3 = lpfc_sli_get_buff(phba, pring,
James Smart51ef4c22007-08-02 11:10:31 -04002435 irsp->unsli3.sli3Words[7]);
James Smart76bb24e2007-10-27 13:38:00 -04002436 if (!saveq->context3)
2437 lpfc_printf_log(phba,
2438 KERN_ERR,
2439 LOG_SLI,
2440 "0342 Ring %d Cannot find buffer for an"
2441 " unsolicited iocb. tag 0x%x\n",
2442 pring->ringno,
2443 irsp->unsli3.sli3Words[7]);
2444 }
2445 list_for_each_entry(iocbq, &saveq->list, list) {
James Smart76bb24e2007-10-27 13:38:00 -04002446 irsp = &(iocbq->iocb);
James Smart76bb24e2007-10-27 13:38:00 -04002447 if (irsp->ulpBdeCount != 0) {
2448 iocbq->context2 = lpfc_sli_get_buff(phba, pring,
2449 irsp->un.ulpWord[3]);
James Smart9c2face2008-01-11 01:53:18 -05002450 if (!iocbq->context2)
James Smart76bb24e2007-10-27 13:38:00 -04002451 lpfc_printf_log(phba,
2452 KERN_ERR,
2453 LOG_SLI,
2454 "0343 Ring %d Cannot find "
2455 "buffer for an unsolicited iocb"
2456 ". tag 0x%x\n", pring->ringno,
2457 irsp->un.ulpWord[3]);
2458 }
2459 if (irsp->ulpBdeCount == 2) {
2460 iocbq->context3 = lpfc_sli_get_buff(phba, pring,
2461 irsp->unsli3.sli3Words[7]);
James Smart9c2face2008-01-11 01:53:18 -05002462 if (!iocbq->context3)
James Smart76bb24e2007-10-27 13:38:00 -04002463 lpfc_printf_log(phba,
2464 KERN_ERR,
2465 LOG_SLI,
2466 "0344 Ring %d Cannot find "
2467 "buffer for an unsolicited "
2468 "iocb. tag 0x%x\n",
2469 pring->ringno,
2470 irsp->unsli3.sli3Words[7]);
2471 }
2472 }
James Smart92d7f7b2007-06-17 19:56:38 -05002473 }
James Smart9c2face2008-01-11 01:53:18 -05002474 if (irsp->ulpBdeCount != 0 &&
2475 (irsp->ulpCommand == CMD_IOCB_RCV_CONT64_CX ||
2476 irsp->ulpStatus == IOSTAT_INTERMED_RSP)) {
2477 int found = 0;
2478
2479 /* search continue save q for same XRI */
2480 list_for_each_entry(iocbq, &pring->iocb_continue_saveq, clist) {
2481 if (iocbq->iocb.ulpContext == saveq->iocb.ulpContext) {
2482 list_add_tail(&saveq->list, &iocbq->list);
2483 found = 1;
2484 break;
2485 }
2486 }
2487 if (!found)
2488 list_add_tail(&saveq->clist,
2489 &pring->iocb_continue_saveq);
2490 if (saveq->iocb.ulpStatus != IOSTAT_INTERMED_RSP) {
2491 list_del_init(&iocbq->clist);
2492 saveq = iocbq;
2493 irsp = &(saveq->iocb);
2494 } else
2495 return 0;
2496 }
2497 if ((irsp->ulpCommand == CMD_RCV_ELS_REQ64_CX) ||
2498 (irsp->ulpCommand == CMD_RCV_ELS_REQ_CX) ||
2499 (irsp->ulpCommand == CMD_IOCB_RCV_ELS64_CX)) {
James Smart6a9c52c2009-10-02 15:16:51 -04002500 Rctl = FC_RCTL_ELS_REQ;
2501 Type = FC_TYPE_ELS;
James Smart9c2face2008-01-11 01:53:18 -05002502 } else {
2503 w5p = (WORD5 *)&(saveq->iocb.un.ulpWord[5]);
2504 Rctl = w5p->hcsw.Rctl;
2505 Type = w5p->hcsw.Type;
2506
2507 /* Firmware Workaround */
2508 if ((Rctl == 0) && (pring->ringno == LPFC_ELS_RING) &&
2509 (irsp->ulpCommand == CMD_RCV_SEQUENCE64_CX ||
2510 irsp->ulpCommand == CMD_IOCB_RCV_SEQ64_CX)) {
James Smart6a9c52c2009-10-02 15:16:51 -04002511 Rctl = FC_RCTL_ELS_REQ;
2512 Type = FC_TYPE_ELS;
James Smart9c2face2008-01-11 01:53:18 -05002513 w5p->hcsw.Rctl = Rctl;
2514 w5p->hcsw.Type = Type;
2515 }
2516 }
James Smart92d7f7b2007-06-17 19:56:38 -05002517
James Smart3772a992009-05-22 14:50:54 -04002518 if (!lpfc_complete_unsol_iocb(phba, pring, saveq, Rctl, Type))
James Smart92d7f7b2007-06-17 19:56:38 -05002519 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002520 "0313 Ring %d handler: unexpected Rctl x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05002521 "Type x%x received\n",
James Smarte8b62012007-08-02 11:10:09 -04002522 pring->ringno, Rctl, Type);
James Smart3772a992009-05-22 14:50:54 -04002523
James Smart92d7f7b2007-06-17 19:56:38 -05002524 return 1;
dea31012005-04-17 16:05:31 -05002525}
2526
James Smarte59058c2008-08-24 21:49:00 -04002527/**
James Smart3621a712009-04-06 18:47:14 -04002528 * lpfc_sli_iocbq_lookup - Find command iocb for the given response iocb
James Smarte59058c2008-08-24 21:49:00 -04002529 * @phba: Pointer to HBA context object.
2530 * @pring: Pointer to driver SLI ring object.
2531 * @prspiocb: Pointer to response iocb object.
2532 *
2533 * This function looks up the iocb_lookup table to get the command iocb
2534 * corresponding to the given response iocb using the iotag of the
2535 * response iocb. This function is called with the hbalock held.
2536 * This function returns the command iocb object if it finds the command
2537 * iocb else returns NULL.
2538 **/
dea31012005-04-17 16:05:31 -05002539static struct lpfc_iocbq *
James Smart2e0fef82007-06-17 19:56:36 -05002540lpfc_sli_iocbq_lookup(struct lpfc_hba *phba,
2541 struct lpfc_sli_ring *pring,
2542 struct lpfc_iocbq *prspiocb)
dea31012005-04-17 16:05:31 -05002543{
dea31012005-04-17 16:05:31 -05002544 struct lpfc_iocbq *cmd_iocb = NULL;
2545 uint16_t iotag;
2546
James Bottomley604a3e32005-10-29 10:28:33 -05002547 iotag = prspiocb->iocb.ulpIoTag;
dea31012005-04-17 16:05:31 -05002548
James Bottomley604a3e32005-10-29 10:28:33 -05002549 if (iotag != 0 && iotag <= phba->sli.last_iotag) {
2550 cmd_iocb = phba->sli.iocbq_lookup[iotag];
James Smart92d7f7b2007-06-17 19:56:38 -05002551 list_del_init(&cmd_iocb->list);
James Smart2a9bf3d2010-06-07 15:24:45 -04002552 if (cmd_iocb->iocb_flag & LPFC_IO_ON_Q) {
2553 pring->txcmplq_cnt--;
2554 cmd_iocb->iocb_flag &= ~LPFC_IO_ON_Q;
2555 }
James Bottomley604a3e32005-10-29 10:28:33 -05002556 return cmd_iocb;
dea31012005-04-17 16:05:31 -05002557 }
2558
dea31012005-04-17 16:05:31 -05002559 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002560 "0317 iotag x%x is out off "
James Bottomley604a3e32005-10-29 10:28:33 -05002561 "range: max iotag x%x wd0 x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04002562 iotag, phba->sli.last_iotag,
James Bottomley604a3e32005-10-29 10:28:33 -05002563 *(((uint32_t *) &prspiocb->iocb) + 7));
dea31012005-04-17 16:05:31 -05002564 return NULL;
2565}
2566
James Smarte59058c2008-08-24 21:49:00 -04002567/**
James Smart3772a992009-05-22 14:50:54 -04002568 * lpfc_sli_iocbq_lookup_by_tag - Find command iocb for the iotag
2569 * @phba: Pointer to HBA context object.
2570 * @pring: Pointer to driver SLI ring object.
2571 * @iotag: IOCB tag.
2572 *
2573 * This function looks up the iocb_lookup table to get the command iocb
2574 * corresponding to the given iotag. This function is called with the
2575 * hbalock held.
2576 * This function returns the command iocb object if it finds the command
2577 * iocb else returns NULL.
2578 **/
2579static struct lpfc_iocbq *
2580lpfc_sli_iocbq_lookup_by_tag(struct lpfc_hba *phba,
2581 struct lpfc_sli_ring *pring, uint16_t iotag)
2582{
2583 struct lpfc_iocbq *cmd_iocb;
2584
2585 if (iotag != 0 && iotag <= phba->sli.last_iotag) {
2586 cmd_iocb = phba->sli.iocbq_lookup[iotag];
2587 list_del_init(&cmd_iocb->list);
James Smart2a9bf3d2010-06-07 15:24:45 -04002588 if (cmd_iocb->iocb_flag & LPFC_IO_ON_Q) {
2589 cmd_iocb->iocb_flag &= ~LPFC_IO_ON_Q;
2590 pring->txcmplq_cnt--;
2591 }
James Smart3772a992009-05-22 14:50:54 -04002592 return cmd_iocb;
2593 }
2594
2595 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2596 "0372 iotag x%x is out off range: max iotag (x%x)\n",
2597 iotag, phba->sli.last_iotag);
2598 return NULL;
2599}
2600
2601/**
James Smart3621a712009-04-06 18:47:14 -04002602 * lpfc_sli_process_sol_iocb - process solicited iocb completion
James Smarte59058c2008-08-24 21:49:00 -04002603 * @phba: Pointer to HBA context object.
2604 * @pring: Pointer to driver SLI ring object.
2605 * @saveq: Pointer to the response iocb to be processed.
2606 *
2607 * This function is called by the ring event handler for non-fcp
2608 * rings when there is a new response iocb in the response ring.
2609 * The caller is not required to hold any locks. This function
2610 * gets the command iocb associated with the response iocb and
2611 * calls the completion handler for the command iocb. If there
2612 * is no completion handler, the function will free the resources
2613 * associated with command iocb. If the response iocb is for
2614 * an already aborted command iocb, the status of the completion
2615 * is changed to IOSTAT_LOCAL_REJECT/IOERR_SLI_ABORTED.
2616 * This function always returns 1.
2617 **/
dea31012005-04-17 16:05:31 -05002618static int
James Smart2e0fef82007-06-17 19:56:36 -05002619lpfc_sli_process_sol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
dea31012005-04-17 16:05:31 -05002620 struct lpfc_iocbq *saveq)
2621{
James Smart2e0fef82007-06-17 19:56:36 -05002622 struct lpfc_iocbq *cmdiocbp;
dea31012005-04-17 16:05:31 -05002623 int rc = 1;
2624 unsigned long iflag;
2625
2626 /* Based on the iotag field, get the cmd IOCB from the txcmplq */
James Smart2e0fef82007-06-17 19:56:36 -05002627 spin_lock_irqsave(&phba->hbalock, iflag);
James Bottomley604a3e32005-10-29 10:28:33 -05002628 cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring, saveq);
James Smart2e0fef82007-06-17 19:56:36 -05002629 spin_unlock_irqrestore(&phba->hbalock, iflag);
2630
dea31012005-04-17 16:05:31 -05002631 if (cmdiocbp) {
2632 if (cmdiocbp->iocb_cmpl) {
2633 /*
James Smartea2151b2008-09-07 11:52:10 -04002634 * If an ELS command failed send an event to mgmt
2635 * application.
2636 */
2637 if (saveq->iocb.ulpStatus &&
2638 (pring->ringno == LPFC_ELS_RING) &&
2639 (cmdiocbp->iocb.ulpCommand ==
2640 CMD_ELS_REQUEST64_CR))
2641 lpfc_send_els_failure_event(phba,
2642 cmdiocbp, saveq);
2643
2644 /*
dea31012005-04-17 16:05:31 -05002645 * Post all ELS completions to the worker thread.
2646 * All other are passed to the completion callback.
2647 */
2648 if (pring->ringno == LPFC_ELS_RING) {
James Smart341af102010-01-26 23:07:37 -05002649 if ((phba->sli_rev < LPFC_SLI_REV4) &&
2650 (cmdiocbp->iocb_flag &
2651 LPFC_DRIVER_ABORTED)) {
2652 spin_lock_irqsave(&phba->hbalock,
2653 iflag);
James Smart07951072007-04-25 09:51:38 -04002654 cmdiocbp->iocb_flag &=
2655 ~LPFC_DRIVER_ABORTED;
James Smart341af102010-01-26 23:07:37 -05002656 spin_unlock_irqrestore(&phba->hbalock,
2657 iflag);
James Smart07951072007-04-25 09:51:38 -04002658 saveq->iocb.ulpStatus =
2659 IOSTAT_LOCAL_REJECT;
2660 saveq->iocb.un.ulpWord[4] =
2661 IOERR_SLI_ABORTED;
James Smart0ff10d42008-01-11 01:52:36 -05002662
2663 /* Firmware could still be in progress
2664 * of DMAing payload, so don't free data
2665 * buffer till after a hbeat.
2666 */
James Smart341af102010-01-26 23:07:37 -05002667 spin_lock_irqsave(&phba->hbalock,
2668 iflag);
James Smart0ff10d42008-01-11 01:52:36 -05002669 saveq->iocb_flag |= LPFC_DELAY_MEM_FREE;
James Smart341af102010-01-26 23:07:37 -05002670 spin_unlock_irqrestore(&phba->hbalock,
2671 iflag);
2672 }
James Smart0f65ff62010-02-26 14:14:23 -05002673 if (phba->sli_rev == LPFC_SLI_REV4) {
2674 if (saveq->iocb_flag &
2675 LPFC_EXCHANGE_BUSY) {
2676 /* Set cmdiocb flag for the
2677 * exchange busy so sgl (xri)
2678 * will not be released until
2679 * the abort xri is received
2680 * from hba.
2681 */
2682 spin_lock_irqsave(
2683 &phba->hbalock, iflag);
2684 cmdiocbp->iocb_flag |=
2685 LPFC_EXCHANGE_BUSY;
2686 spin_unlock_irqrestore(
2687 &phba->hbalock, iflag);
2688 }
2689 if (cmdiocbp->iocb_flag &
2690 LPFC_DRIVER_ABORTED) {
2691 /*
2692 * Clear LPFC_DRIVER_ABORTED
2693 * bit in case it was driver
2694 * initiated abort.
2695 */
2696 spin_lock_irqsave(
2697 &phba->hbalock, iflag);
2698 cmdiocbp->iocb_flag &=
2699 ~LPFC_DRIVER_ABORTED;
2700 spin_unlock_irqrestore(
2701 &phba->hbalock, iflag);
2702 cmdiocbp->iocb.ulpStatus =
2703 IOSTAT_LOCAL_REJECT;
2704 cmdiocbp->iocb.un.ulpWord[4] =
2705 IOERR_ABORT_REQUESTED;
2706 /*
2707 * For SLI4, irsiocb contains
2708 * NO_XRI in sli_xritag, it
2709 * shall not affect releasing
2710 * sgl (xri) process.
2711 */
2712 saveq->iocb.ulpStatus =
2713 IOSTAT_LOCAL_REJECT;
2714 saveq->iocb.un.ulpWord[4] =
2715 IOERR_SLI_ABORTED;
2716 spin_lock_irqsave(
2717 &phba->hbalock, iflag);
2718 saveq->iocb_flag |=
2719 LPFC_DELAY_MEM_FREE;
2720 spin_unlock_irqrestore(
2721 &phba->hbalock, iflag);
2722 }
James Smart07951072007-04-25 09:51:38 -04002723 }
dea31012005-04-17 16:05:31 -05002724 }
James Smart2e0fef82007-06-17 19:56:36 -05002725 (cmdiocbp->iocb_cmpl) (phba, cmdiocbp, saveq);
James Bottomley604a3e32005-10-29 10:28:33 -05002726 } else
2727 lpfc_sli_release_iocbq(phba, cmdiocbp);
dea31012005-04-17 16:05:31 -05002728 } else {
2729 /*
2730 * Unknown initiating command based on the response iotag.
2731 * This could be the case on the ELS ring because of
2732 * lpfc_els_abort().
2733 */
2734 if (pring->ringno != LPFC_ELS_RING) {
2735 /*
2736 * Ring <ringno> handler: unexpected completion IoTag
2737 * <IoTag>
2738 */
James Smarta257bf92009-04-06 18:48:10 -04002739 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002740 "0322 Ring %d handler: "
2741 "unexpected completion IoTag x%x "
2742 "Data: x%x x%x x%x x%x\n",
2743 pring->ringno,
2744 saveq->iocb.ulpIoTag,
2745 saveq->iocb.ulpStatus,
2746 saveq->iocb.un.ulpWord[4],
2747 saveq->iocb.ulpCommand,
2748 saveq->iocb.ulpContext);
dea31012005-04-17 16:05:31 -05002749 }
2750 }
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04002751
dea31012005-04-17 16:05:31 -05002752 return rc;
2753}
2754
James Smarte59058c2008-08-24 21:49:00 -04002755/**
James Smart3621a712009-04-06 18:47:14 -04002756 * lpfc_sli_rsp_pointers_error - Response ring pointer error handler
James Smarte59058c2008-08-24 21:49:00 -04002757 * @phba: Pointer to HBA context object.
2758 * @pring: Pointer to driver SLI ring object.
2759 *
2760 * This function is called from the iocb ring event handlers when
2761 * put pointer is ahead of the get pointer for a ring. This function signal
2762 * an error attention condition to the worker thread and the worker
2763 * thread will transition the HBA to offline state.
2764 **/
James Smart2e0fef82007-06-17 19:56:36 -05002765static void
2766lpfc_sli_rsp_pointers_error(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002767{
James Smart34b02dc2008-08-24 21:49:55 -04002768 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002769 /*
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02002770 * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002771 * rsp ring <portRspMax>
2772 */
2773 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002774 "0312 Ring %d handler: portRspPut %d "
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02002775 "is bigger than rsp ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04002776 pring->ringno, le32_to_cpu(pgp->rspPutInx),
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002777 pring->numRiocb);
2778
James Smart2e0fef82007-06-17 19:56:36 -05002779 phba->link_state = LPFC_HBA_ERROR;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002780
2781 /*
2782 * All error attention handlers are posted to
2783 * worker thread
2784 */
2785 phba->work_ha |= HA_ERATT;
2786 phba->work_hs = HS_FFER3;
James Smart92d7f7b2007-06-17 19:56:38 -05002787
James Smart5e9d9b82008-06-14 22:52:53 -04002788 lpfc_worker_wake_up(phba);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002789
2790 return;
2791}
2792
James Smarte59058c2008-08-24 21:49:00 -04002793/**
James Smart3621a712009-04-06 18:47:14 -04002794 * lpfc_poll_eratt - Error attention polling timer timeout handler
James Smart93996272008-08-24 21:50:30 -04002795 * @ptr: Pointer to address of HBA context object.
2796 *
2797 * This function is invoked by the Error Attention polling timer when the
2798 * timer times out. It will check the SLI Error Attention register for
2799 * possible attention events. If so, it will post an Error Attention event
2800 * and wake up worker thread to process it. Otherwise, it will set up the
2801 * Error Attention polling timer for the next poll.
2802 **/
2803void lpfc_poll_eratt(unsigned long ptr)
2804{
2805 struct lpfc_hba *phba;
2806 uint32_t eratt = 0;
2807
2808 phba = (struct lpfc_hba *)ptr;
2809
2810 /* Check chip HA register for error event */
2811 eratt = lpfc_sli_check_eratt(phba);
2812
2813 if (eratt)
2814 /* Tell the worker thread there is work to do */
2815 lpfc_worker_wake_up(phba);
2816 else
2817 /* Restart the timer for next eratt poll */
2818 mod_timer(&phba->eratt_poll, jiffies +
2819 HZ * LPFC_ERATT_POLL_INTERVAL);
2820 return;
2821}
2822
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002823
James Smarte59058c2008-08-24 21:49:00 -04002824/**
James Smart3621a712009-04-06 18:47:14 -04002825 * lpfc_sli_handle_fast_ring_event - Handle ring events on FCP ring
James Smarte59058c2008-08-24 21:49:00 -04002826 * @phba: Pointer to HBA context object.
2827 * @pring: Pointer to driver SLI ring object.
2828 * @mask: Host attention register mask for this ring.
2829 *
2830 * This function is called from the interrupt context when there is a ring
2831 * event for the fcp ring. The caller does not hold any lock.
2832 * The function processes each response iocb in the response ring until it
2833 * finds an iocb with LE bit set and chains all the iocbs upto the iocb with
2834 * LE bit set. The function will call the completion handler of the command iocb
2835 * if the response iocb indicates a completion for a command iocb or it is
2836 * an abort completion. The function will call lpfc_sli_process_unsol_iocb
2837 * function if this is an unsolicited iocb.
dea31012005-04-17 16:05:31 -05002838 * This routine presumes LPFC_FCP_RING handling and doesn't bother
James Smart45ed1192009-10-02 15:17:02 -04002839 * to check it explicitly.
2840 */
2841int
James Smart2e0fef82007-06-17 19:56:36 -05002842lpfc_sli_handle_fast_ring_event(struct lpfc_hba *phba,
2843 struct lpfc_sli_ring *pring, uint32_t mask)
dea31012005-04-17 16:05:31 -05002844{
James Smart34b02dc2008-08-24 21:49:55 -04002845 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
dea31012005-04-17 16:05:31 -05002846 IOCB_t *irsp = NULL;
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04002847 IOCB_t *entry = NULL;
dea31012005-04-17 16:05:31 -05002848 struct lpfc_iocbq *cmdiocbq = NULL;
2849 struct lpfc_iocbq rspiocbq;
dea31012005-04-17 16:05:31 -05002850 uint32_t status;
2851 uint32_t portRspPut, portRspMax;
2852 int rc = 1;
2853 lpfc_iocb_type type;
2854 unsigned long iflag;
2855 uint32_t rsp_cmpl = 0;
dea31012005-04-17 16:05:31 -05002856
James Smart2e0fef82007-06-17 19:56:36 -05002857 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05002858 pring->stats.iocb_event++;
2859
dea31012005-04-17 16:05:31 -05002860 /*
2861 * The next available response entry should never exceed the maximum
2862 * entries. If it does, treat it as an adapter hardware error.
2863 */
2864 portRspMax = pring->numRiocb;
2865 portRspPut = le32_to_cpu(pgp->rspPutInx);
2866 if (unlikely(portRspPut >= portRspMax)) {
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002867 lpfc_sli_rsp_pointers_error(phba, pring);
James Smart2e0fef82007-06-17 19:56:36 -05002868 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05002869 return 1;
2870 }
James Smart45ed1192009-10-02 15:17:02 -04002871 if (phba->fcp_ring_in_use) {
2872 spin_unlock_irqrestore(&phba->hbalock, iflag);
2873 return 1;
2874 } else
2875 phba->fcp_ring_in_use = 1;
dea31012005-04-17 16:05:31 -05002876
2877 rmb();
2878 while (pring->rspidx != portRspPut) {
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04002879 /*
2880 * Fetch an entry off the ring and copy it into a local data
2881 * structure. The copy involves a byte-swap since the
2882 * network byte order and pci byte orders are different.
2883 */
James Smarted957682007-06-17 19:56:37 -05002884 entry = lpfc_resp_iocb(phba, pring);
James Smart858c9f62007-06-17 19:56:39 -05002885 phba->last_completion_time = jiffies;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002886
2887 if (++pring->rspidx >= portRspMax)
2888 pring->rspidx = 0;
2889
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04002890 lpfc_sli_pcimem_bcopy((uint32_t *) entry,
2891 (uint32_t *) &rspiocbq.iocb,
James Smarted957682007-06-17 19:56:37 -05002892 phba->iocb_rsp_size);
James Smarta4bc3372006-12-02 13:34:16 -05002893 INIT_LIST_HEAD(&(rspiocbq.list));
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04002894 irsp = &rspiocbq.iocb;
2895
dea31012005-04-17 16:05:31 -05002896 type = lpfc_sli_iocb_cmd_type(irsp->ulpCommand & CMD_IOCB_MASK);
2897 pring->stats.iocb_rsp++;
2898 rsp_cmpl++;
2899
2900 if (unlikely(irsp->ulpStatus)) {
James Smart92d7f7b2007-06-17 19:56:38 -05002901 /*
2902 * If resource errors reported from HBA, reduce
2903 * queuedepths of the SCSI device.
2904 */
2905 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
2906 (irsp->un.ulpWord[4] == IOERR_NO_RESOURCES)) {
2907 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart3772a992009-05-22 14:50:54 -04002908 phba->lpfc_rampdown_queue_depth(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05002909 spin_lock_irqsave(&phba->hbalock, iflag);
2910 }
2911
dea31012005-04-17 16:05:31 -05002912 /* Rsp ring <ringno> error: IOCB */
2913 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002914 "0336 Rsp Ring %d error: IOCB Data: "
James Smart92d7f7b2007-06-17 19:56:38 -05002915 "x%x x%x x%x x%x x%x x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04002916 pring->ringno,
James Smart92d7f7b2007-06-17 19:56:38 -05002917 irsp->un.ulpWord[0],
2918 irsp->un.ulpWord[1],
2919 irsp->un.ulpWord[2],
2920 irsp->un.ulpWord[3],
2921 irsp->un.ulpWord[4],
2922 irsp->un.ulpWord[5],
James Smartd7c255b2008-08-24 21:50:00 -04002923 *(uint32_t *)&irsp->un1,
2924 *((uint32_t *)&irsp->un1 + 1));
dea31012005-04-17 16:05:31 -05002925 }
2926
2927 switch (type) {
2928 case LPFC_ABORT_IOCB:
2929 case LPFC_SOL_IOCB:
2930 /*
2931 * Idle exchange closed via ABTS from port. No iocb
2932 * resources need to be recovered.
2933 */
2934 if (unlikely(irsp->ulpCommand == CMD_XRI_ABORTED_CX)) {
James Smartdca94792006-08-01 07:34:08 -04002935 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002936 "0333 IOCB cmd 0x%x"
James Smartdca94792006-08-01 07:34:08 -04002937 " processed. Skipping"
James Smart92d7f7b2007-06-17 19:56:38 -05002938 " completion\n",
James Smartdca94792006-08-01 07:34:08 -04002939 irsp->ulpCommand);
dea31012005-04-17 16:05:31 -05002940 break;
2941 }
2942
James Bottomley604a3e32005-10-29 10:28:33 -05002943 cmdiocbq = lpfc_sli_iocbq_lookup(phba, pring,
2944 &rspiocbq);
James Smart0f65ff62010-02-26 14:14:23 -05002945 if (unlikely(!cmdiocbq))
2946 break;
2947 if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED)
2948 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
2949 if (cmdiocbq->iocb_cmpl) {
2950 spin_unlock_irqrestore(&phba->hbalock, iflag);
2951 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
2952 &rspiocbq);
2953 spin_lock_irqsave(&phba->hbalock, iflag);
2954 }
dea31012005-04-17 16:05:31 -05002955 break;
James Smarta4bc3372006-12-02 13:34:16 -05002956 case LPFC_UNSOL_IOCB:
James Smart2e0fef82007-06-17 19:56:36 -05002957 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta4bc3372006-12-02 13:34:16 -05002958 lpfc_sli_process_unsol_iocb(phba, pring, &rspiocbq);
James Smart2e0fef82007-06-17 19:56:36 -05002959 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta4bc3372006-12-02 13:34:16 -05002960 break;
dea31012005-04-17 16:05:31 -05002961 default:
2962 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
2963 char adaptermsg[LPFC_MAX_ADPTMSG];
2964 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
2965 memcpy(&adaptermsg[0], (uint8_t *) irsp,
2966 MAX_MSG_DATA);
Joe Perches898eb712007-10-18 03:06:30 -07002967 dev_warn(&((phba->pcidev)->dev),
2968 "lpfc%d: %s\n",
dea31012005-04-17 16:05:31 -05002969 phba->brd_no, adaptermsg);
2970 } else {
2971 /* Unknown IOCB command */
2972 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002973 "0334 Unknown IOCB command "
James Smart92d7f7b2007-06-17 19:56:38 -05002974 "Data: x%x, x%x x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04002975 type, irsp->ulpCommand,
James Smart92d7f7b2007-06-17 19:56:38 -05002976 irsp->ulpStatus,
2977 irsp->ulpIoTag,
2978 irsp->ulpContext);
dea31012005-04-17 16:05:31 -05002979 }
2980 break;
2981 }
2982
2983 /*
2984 * The response IOCB has been processed. Update the ring
2985 * pointer in SLIM. If the port response put pointer has not
2986 * been updated, sync the pgp->rspPutInx and fetch the new port
2987 * response put pointer.
2988 */
James Smarted957682007-06-17 19:56:37 -05002989 writel(pring->rspidx, &phba->host_gp[pring->ringno].rspGetInx);
dea31012005-04-17 16:05:31 -05002990
2991 if (pring->rspidx == portRspPut)
2992 portRspPut = le32_to_cpu(pgp->rspPutInx);
2993 }
2994
2995 if ((rsp_cmpl > 0) && (mask & HA_R0RE_REQ)) {
2996 pring->stats.iocb_rsp_full++;
2997 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
2998 writel(status, phba->CAregaddr);
2999 readl(phba->CAregaddr);
3000 }
3001 if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
3002 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
3003 pring->stats.iocb_cmd_empty++;
3004
3005 /* Force update of the local copy of cmdGetInx */
3006 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
3007 lpfc_sli_resume_iocb(phba, pring);
3008
3009 if ((pring->lpfc_sli_cmd_available))
3010 (pring->lpfc_sli_cmd_available) (phba, pring);
3011
3012 }
3013
James Smart45ed1192009-10-02 15:17:02 -04003014 phba->fcp_ring_in_use = 0;
James Smart2e0fef82007-06-17 19:56:36 -05003015 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003016 return rc;
3017}
3018
James Smarte59058c2008-08-24 21:49:00 -04003019/**
James Smart3772a992009-05-22 14:50:54 -04003020 * lpfc_sli_sp_handle_rspiocb - Handle slow-path response iocb
3021 * @phba: Pointer to HBA context object.
3022 * @pring: Pointer to driver SLI ring object.
3023 * @rspiocbp: Pointer to driver response IOCB object.
3024 *
3025 * This function is called from the worker thread when there is a slow-path
3026 * response IOCB to process. This function chains all the response iocbs until
3027 * seeing the iocb with the LE bit set. The function will call
3028 * lpfc_sli_process_sol_iocb function if the response iocb indicates a
3029 * completion of a command iocb. The function will call the
3030 * lpfc_sli_process_unsol_iocb function if this is an unsolicited iocb.
3031 * The function frees the resources or calls the completion handler if this
3032 * iocb is an abort completion. The function returns NULL when the response
3033 * iocb has the LE bit set and all the chained iocbs are processed, otherwise
3034 * this function shall chain the iocb on to the iocb_continueq and return the
3035 * response iocb passed in.
3036 **/
3037static struct lpfc_iocbq *
3038lpfc_sli_sp_handle_rspiocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
3039 struct lpfc_iocbq *rspiocbp)
3040{
3041 struct lpfc_iocbq *saveq;
3042 struct lpfc_iocbq *cmdiocbp;
3043 struct lpfc_iocbq *next_iocb;
3044 IOCB_t *irsp = NULL;
3045 uint32_t free_saveq;
3046 uint8_t iocb_cmd_type;
3047 lpfc_iocb_type type;
3048 unsigned long iflag;
3049 int rc;
3050
3051 spin_lock_irqsave(&phba->hbalock, iflag);
3052 /* First add the response iocb to the countinueq list */
3053 list_add_tail(&rspiocbp->list, &(pring->iocb_continueq));
3054 pring->iocb_continueq_cnt++;
3055
3056 /* Now, determine whetehr the list is completed for processing */
3057 irsp = &rspiocbp->iocb;
3058 if (irsp->ulpLe) {
3059 /*
3060 * By default, the driver expects to free all resources
3061 * associated with this iocb completion.
3062 */
3063 free_saveq = 1;
3064 saveq = list_get_first(&pring->iocb_continueq,
3065 struct lpfc_iocbq, list);
3066 irsp = &(saveq->iocb);
3067 list_del_init(&pring->iocb_continueq);
3068 pring->iocb_continueq_cnt = 0;
3069
3070 pring->stats.iocb_rsp++;
3071
3072 /*
3073 * If resource errors reported from HBA, reduce
3074 * queuedepths of the SCSI device.
3075 */
3076 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
3077 (irsp->un.ulpWord[4] == IOERR_NO_RESOURCES)) {
3078 spin_unlock_irqrestore(&phba->hbalock, iflag);
3079 phba->lpfc_rampdown_queue_depth(phba);
3080 spin_lock_irqsave(&phba->hbalock, iflag);
3081 }
3082
3083 if (irsp->ulpStatus) {
3084 /* Rsp ring <ringno> error: IOCB */
3085 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
3086 "0328 Rsp Ring %d error: "
3087 "IOCB Data: "
3088 "x%x x%x x%x x%x "
3089 "x%x x%x x%x x%x "
3090 "x%x x%x x%x x%x "
3091 "x%x x%x x%x x%x\n",
3092 pring->ringno,
3093 irsp->un.ulpWord[0],
3094 irsp->un.ulpWord[1],
3095 irsp->un.ulpWord[2],
3096 irsp->un.ulpWord[3],
3097 irsp->un.ulpWord[4],
3098 irsp->un.ulpWord[5],
3099 *(((uint32_t *) irsp) + 6),
3100 *(((uint32_t *) irsp) + 7),
3101 *(((uint32_t *) irsp) + 8),
3102 *(((uint32_t *) irsp) + 9),
3103 *(((uint32_t *) irsp) + 10),
3104 *(((uint32_t *) irsp) + 11),
3105 *(((uint32_t *) irsp) + 12),
3106 *(((uint32_t *) irsp) + 13),
3107 *(((uint32_t *) irsp) + 14),
3108 *(((uint32_t *) irsp) + 15));
3109 }
3110
3111 /*
3112 * Fetch the IOCB command type and call the correct completion
3113 * routine. Solicited and Unsolicited IOCBs on the ELS ring
3114 * get freed back to the lpfc_iocb_list by the discovery
3115 * kernel thread.
3116 */
3117 iocb_cmd_type = irsp->ulpCommand & CMD_IOCB_MASK;
3118 type = lpfc_sli_iocb_cmd_type(iocb_cmd_type);
3119 switch (type) {
3120 case LPFC_SOL_IOCB:
3121 spin_unlock_irqrestore(&phba->hbalock, iflag);
3122 rc = lpfc_sli_process_sol_iocb(phba, pring, saveq);
3123 spin_lock_irqsave(&phba->hbalock, iflag);
3124 break;
3125
3126 case LPFC_UNSOL_IOCB:
3127 spin_unlock_irqrestore(&phba->hbalock, iflag);
3128 rc = lpfc_sli_process_unsol_iocb(phba, pring, saveq);
3129 spin_lock_irqsave(&phba->hbalock, iflag);
3130 if (!rc)
3131 free_saveq = 0;
3132 break;
3133
3134 case LPFC_ABORT_IOCB:
3135 cmdiocbp = NULL;
3136 if (irsp->ulpCommand != CMD_XRI_ABORTED_CX)
3137 cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring,
3138 saveq);
3139 if (cmdiocbp) {
3140 /* Call the specified completion routine */
3141 if (cmdiocbp->iocb_cmpl) {
3142 spin_unlock_irqrestore(&phba->hbalock,
3143 iflag);
3144 (cmdiocbp->iocb_cmpl)(phba, cmdiocbp,
3145 saveq);
3146 spin_lock_irqsave(&phba->hbalock,
3147 iflag);
3148 } else
3149 __lpfc_sli_release_iocbq(phba,
3150 cmdiocbp);
3151 }
3152 break;
3153
3154 case LPFC_UNKNOWN_IOCB:
3155 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
3156 char adaptermsg[LPFC_MAX_ADPTMSG];
3157 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
3158 memcpy(&adaptermsg[0], (uint8_t *)irsp,
3159 MAX_MSG_DATA);
3160 dev_warn(&((phba->pcidev)->dev),
3161 "lpfc%d: %s\n",
3162 phba->brd_no, adaptermsg);
3163 } else {
3164 /* Unknown IOCB command */
3165 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3166 "0335 Unknown IOCB "
3167 "command Data: x%x "
3168 "x%x x%x x%x\n",
3169 irsp->ulpCommand,
3170 irsp->ulpStatus,
3171 irsp->ulpIoTag,
3172 irsp->ulpContext);
3173 }
3174 break;
3175 }
3176
3177 if (free_saveq) {
3178 list_for_each_entry_safe(rspiocbp, next_iocb,
3179 &saveq->list, list) {
3180 list_del(&rspiocbp->list);
3181 __lpfc_sli_release_iocbq(phba, rspiocbp);
3182 }
3183 __lpfc_sli_release_iocbq(phba, saveq);
3184 }
3185 rspiocbp = NULL;
3186 }
3187 spin_unlock_irqrestore(&phba->hbalock, iflag);
3188 return rspiocbp;
3189}
3190
3191/**
3192 * lpfc_sli_handle_slow_ring_event - Wrapper func for handling slow-path iocbs
James Smarte59058c2008-08-24 21:49:00 -04003193 * @phba: Pointer to HBA context object.
3194 * @pring: Pointer to driver SLI ring object.
3195 * @mask: Host attention register mask for this ring.
3196 *
James Smart3772a992009-05-22 14:50:54 -04003197 * This routine wraps the actual slow_ring event process routine from the
3198 * API jump table function pointer from the lpfc_hba struct.
James Smarte59058c2008-08-24 21:49:00 -04003199 **/
James Smart3772a992009-05-22 14:50:54 -04003200void
James Smart2e0fef82007-06-17 19:56:36 -05003201lpfc_sli_handle_slow_ring_event(struct lpfc_hba *phba,
3202 struct lpfc_sli_ring *pring, uint32_t mask)
dea31012005-04-17 16:05:31 -05003203{
James Smart3772a992009-05-22 14:50:54 -04003204 phba->lpfc_sli_handle_slow_ring_event(phba, pring, mask);
3205}
3206
3207/**
3208 * lpfc_sli_handle_slow_ring_event_s3 - Handle SLI3 ring event for non-FCP rings
3209 * @phba: Pointer to HBA context object.
3210 * @pring: Pointer to driver SLI ring object.
3211 * @mask: Host attention register mask for this ring.
3212 *
3213 * This function is called from the worker thread when there is a ring event
3214 * for non-fcp rings. The caller does not hold any lock. The function will
3215 * remove each response iocb in the response ring and calls the handle
3216 * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
3217 **/
3218static void
3219lpfc_sli_handle_slow_ring_event_s3(struct lpfc_hba *phba,
3220 struct lpfc_sli_ring *pring, uint32_t mask)
3221{
James Smart34b02dc2008-08-24 21:49:55 -04003222 struct lpfc_pgp *pgp;
dea31012005-04-17 16:05:31 -05003223 IOCB_t *entry;
3224 IOCB_t *irsp = NULL;
3225 struct lpfc_iocbq *rspiocbp = NULL;
dea31012005-04-17 16:05:31 -05003226 uint32_t portRspPut, portRspMax;
dea31012005-04-17 16:05:31 -05003227 unsigned long iflag;
James Smart3772a992009-05-22 14:50:54 -04003228 uint32_t status;
dea31012005-04-17 16:05:31 -05003229
James Smart34b02dc2008-08-24 21:49:55 -04003230 pgp = &phba->port_gp[pring->ringno];
James Smart2e0fef82007-06-17 19:56:36 -05003231 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003232 pring->stats.iocb_event++;
3233
dea31012005-04-17 16:05:31 -05003234 /*
3235 * The next available response entry should never exceed the maximum
3236 * entries. If it does, treat it as an adapter hardware error.
3237 */
3238 portRspMax = pring->numRiocb;
3239 portRspPut = le32_to_cpu(pgp->rspPutInx);
3240 if (portRspPut >= portRspMax) {
3241 /*
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02003242 * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
dea31012005-04-17 16:05:31 -05003243 * rsp ring <portRspMax>
3244 */
James Smarted957682007-06-17 19:56:37 -05003245 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003246 "0303 Ring %d handler: portRspPut %d "
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02003247 "is bigger than rsp ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04003248 pring->ringno, portRspPut, portRspMax);
dea31012005-04-17 16:05:31 -05003249
James Smart2e0fef82007-06-17 19:56:36 -05003250 phba->link_state = LPFC_HBA_ERROR;
3251 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003252
3253 phba->work_hs = HS_FFER3;
3254 lpfc_handle_eratt(phba);
3255
James Smart3772a992009-05-22 14:50:54 -04003256 return;
dea31012005-04-17 16:05:31 -05003257 }
3258
3259 rmb();
dea31012005-04-17 16:05:31 -05003260 while (pring->rspidx != portRspPut) {
3261 /*
3262 * Build a completion list and call the appropriate handler.
3263 * The process is to get the next available response iocb, get
3264 * a free iocb from the list, copy the response data into the
3265 * free iocb, insert to the continuation list, and update the
3266 * next response index to slim. This process makes response
3267 * iocb's in the ring available to DMA as fast as possible but
3268 * pays a penalty for a copy operation. Since the iocb is
3269 * only 32 bytes, this penalty is considered small relative to
3270 * the PCI reads for register values and a slim write. When
3271 * the ulpLe field is set, the entire Command has been
3272 * received.
3273 */
James Smarted957682007-06-17 19:56:37 -05003274 entry = lpfc_resp_iocb(phba, pring);
3275
James Smart858c9f62007-06-17 19:56:39 -05003276 phba->last_completion_time = jiffies;
James Smart2e0fef82007-06-17 19:56:36 -05003277 rspiocbp = __lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05003278 if (rspiocbp == NULL) {
3279 printk(KERN_ERR "%s: out of buffers! Failing "
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07003280 "completion.\n", __func__);
dea31012005-04-17 16:05:31 -05003281 break;
3282 }
3283
James Smarted957682007-06-17 19:56:37 -05003284 lpfc_sli_pcimem_bcopy(entry, &rspiocbp->iocb,
3285 phba->iocb_rsp_size);
dea31012005-04-17 16:05:31 -05003286 irsp = &rspiocbp->iocb;
3287
3288 if (++pring->rspidx >= portRspMax)
3289 pring->rspidx = 0;
3290
James Smarta58cbd52007-08-02 11:09:43 -04003291 if (pring->ringno == LPFC_ELS_RING) {
3292 lpfc_debugfs_slow_ring_trc(phba,
3293 "IOCB rsp ring: wd4:x%08x wd6:x%08x wd7:x%08x",
3294 *(((uint32_t *) irsp) + 4),
3295 *(((uint32_t *) irsp) + 6),
3296 *(((uint32_t *) irsp) + 7));
3297 }
3298
James Smarted957682007-06-17 19:56:37 -05003299 writel(pring->rspidx, &phba->host_gp[pring->ringno].rspGetInx);
dea31012005-04-17 16:05:31 -05003300
James Smart3772a992009-05-22 14:50:54 -04003301 spin_unlock_irqrestore(&phba->hbalock, iflag);
3302 /* Handle the response IOCB */
3303 rspiocbp = lpfc_sli_sp_handle_rspiocb(phba, pring, rspiocbp);
3304 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003305
3306 /*
3307 * If the port response put pointer has not been updated, sync
3308 * the pgp->rspPutInx in the MAILBOX_tand fetch the new port
3309 * response put pointer.
3310 */
3311 if (pring->rspidx == portRspPut) {
3312 portRspPut = le32_to_cpu(pgp->rspPutInx);
3313 }
3314 } /* while (pring->rspidx != portRspPut) */
3315
James Smart92d7f7b2007-06-17 19:56:38 -05003316 if ((rspiocbp != NULL) && (mask & HA_R0RE_REQ)) {
dea31012005-04-17 16:05:31 -05003317 /* At least one response entry has been freed */
3318 pring->stats.iocb_rsp_full++;
3319 /* SET RxRE_RSP in Chip Att register */
3320 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
3321 writel(status, phba->CAregaddr);
3322 readl(phba->CAregaddr); /* flush */
3323 }
3324 if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
3325 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
3326 pring->stats.iocb_cmd_empty++;
3327
3328 /* Force update of the local copy of cmdGetInx */
3329 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
3330 lpfc_sli_resume_iocb(phba, pring);
3331
3332 if ((pring->lpfc_sli_cmd_available))
3333 (pring->lpfc_sli_cmd_available) (phba, pring);
3334
3335 }
3336
James Smart2e0fef82007-06-17 19:56:36 -05003337 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart3772a992009-05-22 14:50:54 -04003338 return;
dea31012005-04-17 16:05:31 -05003339}
3340
James Smarte59058c2008-08-24 21:49:00 -04003341/**
James Smart4f774512009-05-22 14:52:35 -04003342 * lpfc_sli_handle_slow_ring_event_s4 - Handle SLI4 slow-path els events
3343 * @phba: Pointer to HBA context object.
3344 * @pring: Pointer to driver SLI ring object.
3345 * @mask: Host attention register mask for this ring.
3346 *
3347 * This function is called from the worker thread when there is a pending
3348 * ELS response iocb on the driver internal slow-path response iocb worker
3349 * queue. The caller does not hold any lock. The function will remove each
3350 * response iocb from the response worker queue and calls the handle
3351 * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
3352 **/
3353static void
3354lpfc_sli_handle_slow_ring_event_s4(struct lpfc_hba *phba,
3355 struct lpfc_sli_ring *pring, uint32_t mask)
3356{
3357 struct lpfc_iocbq *irspiocbq;
James Smart4d9ab992009-10-02 15:16:39 -04003358 struct hbq_dmabuf *dmabuf;
3359 struct lpfc_cq_event *cq_event;
James Smart4f774512009-05-22 14:52:35 -04003360 unsigned long iflag;
3361
James Smart45ed1192009-10-02 15:17:02 -04003362 spin_lock_irqsave(&phba->hbalock, iflag);
3363 phba->hba_flag &= ~HBA_SP_QUEUE_EVT;
3364 spin_unlock_irqrestore(&phba->hbalock, iflag);
3365 while (!list_empty(&phba->sli4_hba.sp_queue_event)) {
James Smart4f774512009-05-22 14:52:35 -04003366 /* Get the response iocb from the head of work queue */
3367 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart45ed1192009-10-02 15:17:02 -04003368 list_remove_head(&phba->sli4_hba.sp_queue_event,
James Smart4d9ab992009-10-02 15:16:39 -04003369 cq_event, struct lpfc_cq_event, list);
James Smart4f774512009-05-22 14:52:35 -04003370 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart4d9ab992009-10-02 15:16:39 -04003371
3372 switch (bf_get(lpfc_wcqe_c_code, &cq_event->cqe.wcqe_cmpl)) {
3373 case CQE_CODE_COMPL_WQE:
3374 irspiocbq = container_of(cq_event, struct lpfc_iocbq,
3375 cq_event);
James Smart45ed1192009-10-02 15:17:02 -04003376 /* Translate ELS WCQE to response IOCBQ */
3377 irspiocbq = lpfc_sli4_els_wcqe_to_rspiocbq(phba,
3378 irspiocbq);
3379 if (irspiocbq)
3380 lpfc_sli_sp_handle_rspiocb(phba, pring,
3381 irspiocbq);
James Smart4d9ab992009-10-02 15:16:39 -04003382 break;
3383 case CQE_CODE_RECEIVE:
3384 dmabuf = container_of(cq_event, struct hbq_dmabuf,
3385 cq_event);
3386 lpfc_sli4_handle_received_buffer(phba, dmabuf);
3387 break;
3388 default:
3389 break;
3390 }
James Smart4f774512009-05-22 14:52:35 -04003391 }
3392}
3393
3394/**
James Smart3621a712009-04-06 18:47:14 -04003395 * lpfc_sli_abort_iocb_ring - Abort all iocbs in the ring
James Smarte59058c2008-08-24 21:49:00 -04003396 * @phba: Pointer to HBA context object.
3397 * @pring: Pointer to driver SLI ring object.
3398 *
3399 * This function aborts all iocbs in the given ring and frees all the iocb
3400 * objects in txq. This function issues an abort iocb for all the iocb commands
3401 * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
3402 * the return of this function. The caller is not required to hold any locks.
3403 **/
James Smart2e0fef82007-06-17 19:56:36 -05003404void
dea31012005-04-17 16:05:31 -05003405lpfc_sli_abort_iocb_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
3406{
James Smart2534ba72007-04-25 09:52:20 -04003407 LIST_HEAD(completions);
dea31012005-04-17 16:05:31 -05003408 struct lpfc_iocbq *iocb, *next_iocb;
dea31012005-04-17 16:05:31 -05003409
James Smart92d7f7b2007-06-17 19:56:38 -05003410 if (pring->ringno == LPFC_ELS_RING) {
3411 lpfc_fabric_abort_hba(phba);
3412 }
3413
dea31012005-04-17 16:05:31 -05003414 /* Error everything on txq and txcmplq
3415 * First do the txq.
3416 */
James Smart2e0fef82007-06-17 19:56:36 -05003417 spin_lock_irq(&phba->hbalock);
James Smart2534ba72007-04-25 09:52:20 -04003418 list_splice_init(&pring->txq, &completions);
dea31012005-04-17 16:05:31 -05003419 pring->txq_cnt = 0;
dea31012005-04-17 16:05:31 -05003420
3421 /* Next issue ABTS for everything on the txcmplq */
James Smart2534ba72007-04-25 09:52:20 -04003422 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
3423 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
3424
James Smart2e0fef82007-06-17 19:56:36 -05003425 spin_unlock_irq(&phba->hbalock);
James Smart2534ba72007-04-25 09:52:20 -04003426
James Smarta257bf92009-04-06 18:48:10 -04003427 /* Cancel all the IOCBs from the completions list */
3428 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
3429 IOERR_SLI_ABORTED);
dea31012005-04-17 16:05:31 -05003430}
3431
James Smarte59058c2008-08-24 21:49:00 -04003432/**
James Smart3621a712009-04-06 18:47:14 -04003433 * lpfc_sli_flush_fcp_rings - flush all iocbs in the fcp ring
James Smarta8e497d2008-08-24 21:50:11 -04003434 * @phba: Pointer to HBA context object.
3435 *
3436 * This function flushes all iocbs in the fcp ring and frees all the iocb
3437 * objects in txq and txcmplq. This function will not issue abort iocbs
3438 * for all the iocb commands in txcmplq, they will just be returned with
3439 * IOERR_SLI_DOWN. This function is invoked with EEH when device's PCI
3440 * slot has been permanently disabled.
3441 **/
3442void
3443lpfc_sli_flush_fcp_rings(struct lpfc_hba *phba)
3444{
3445 LIST_HEAD(txq);
3446 LIST_HEAD(txcmplq);
James Smarta8e497d2008-08-24 21:50:11 -04003447 struct lpfc_sli *psli = &phba->sli;
3448 struct lpfc_sli_ring *pring;
3449
3450 /* Currently, only one fcp ring */
3451 pring = &psli->ring[psli->fcp_ring];
3452
3453 spin_lock_irq(&phba->hbalock);
3454 /* Retrieve everything on txq */
3455 list_splice_init(&pring->txq, &txq);
3456 pring->txq_cnt = 0;
3457
3458 /* Retrieve everything on the txcmplq */
3459 list_splice_init(&pring->txcmplq, &txcmplq);
3460 pring->txcmplq_cnt = 0;
3461 spin_unlock_irq(&phba->hbalock);
3462
3463 /* Flush the txq */
James Smarta257bf92009-04-06 18:48:10 -04003464 lpfc_sli_cancel_iocbs(phba, &txq, IOSTAT_LOCAL_REJECT,
3465 IOERR_SLI_DOWN);
James Smarta8e497d2008-08-24 21:50:11 -04003466
3467 /* Flush the txcmpq */
James Smarta257bf92009-04-06 18:48:10 -04003468 lpfc_sli_cancel_iocbs(phba, &txcmplq, IOSTAT_LOCAL_REJECT,
3469 IOERR_SLI_DOWN);
James Smarta8e497d2008-08-24 21:50:11 -04003470}
3471
3472/**
James Smart3772a992009-05-22 14:50:54 -04003473 * lpfc_sli_brdready_s3 - Check for sli3 host ready status
James Smarte59058c2008-08-24 21:49:00 -04003474 * @phba: Pointer to HBA context object.
3475 * @mask: Bit mask to be checked.
3476 *
3477 * This function reads the host status register and compares
3478 * with the provided bit mask to check if HBA completed
3479 * the restart. This function will wait in a loop for the
3480 * HBA to complete restart. If the HBA does not restart within
3481 * 15 iterations, the function will reset the HBA again. The
3482 * function returns 1 when HBA fail to restart otherwise returns
3483 * zero.
3484 **/
James Smart3772a992009-05-22 14:50:54 -04003485static int
3486lpfc_sli_brdready_s3(struct lpfc_hba *phba, uint32_t mask)
dea31012005-04-17 16:05:31 -05003487{
Jamie Wellnitz41415862006-02-28 19:25:27 -05003488 uint32_t status;
3489 int i = 0;
3490 int retval = 0;
dea31012005-04-17 16:05:31 -05003491
Jamie Wellnitz41415862006-02-28 19:25:27 -05003492 /* Read the HBA Host Status Register */
3493 status = readl(phba->HSregaddr);
dea31012005-04-17 16:05:31 -05003494
Jamie Wellnitz41415862006-02-28 19:25:27 -05003495 /*
3496 * Check status register every 100ms for 5 retries, then every
3497 * 500ms for 5, then every 2.5 sec for 5, then reset board and
3498 * every 2.5 sec for 4.
3499 * Break our of the loop if errors occurred during init.
3500 */
3501 while (((status & mask) != mask) &&
3502 !(status & HS_FFERM) &&
3503 i++ < 20) {
dea31012005-04-17 16:05:31 -05003504
Jamie Wellnitz41415862006-02-28 19:25:27 -05003505 if (i <= 5)
3506 msleep(10);
3507 else if (i <= 10)
3508 msleep(500);
3509 else
3510 msleep(2500);
dea31012005-04-17 16:05:31 -05003511
Jamie Wellnitz41415862006-02-28 19:25:27 -05003512 if (i == 15) {
James Smart2e0fef82007-06-17 19:56:36 -05003513 /* Do post */
James Smart92d7f7b2007-06-17 19:56:38 -05003514 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003515 lpfc_sli_brdrestart(phba);
3516 }
3517 /* Read the HBA Host Status Register */
3518 status = readl(phba->HSregaddr);
dea31012005-04-17 16:05:31 -05003519 }
dea31012005-04-17 16:05:31 -05003520
Jamie Wellnitz41415862006-02-28 19:25:27 -05003521 /* Check to see if any errors occurred during init */
3522 if ((status & HS_FFERM) || (i >= 20)) {
James Smarte40a02c2010-02-26 14:13:54 -05003523 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3524 "2751 Adapter failed to restart, "
3525 "status reg x%x, FW Data: A8 x%x AC x%x\n",
3526 status,
3527 readl(phba->MBslimaddr + 0xa8),
3528 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05003529 phba->link_state = LPFC_HBA_ERROR;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003530 retval = 1;
3531 }
dea31012005-04-17 16:05:31 -05003532
Jamie Wellnitz41415862006-02-28 19:25:27 -05003533 return retval;
dea31012005-04-17 16:05:31 -05003534}
3535
James Smartda0436e2009-05-22 14:51:39 -04003536/**
3537 * lpfc_sli_brdready_s4 - Check for sli4 host ready status
3538 * @phba: Pointer to HBA context object.
3539 * @mask: Bit mask to be checked.
3540 *
3541 * This function checks the host status register to check if HBA is
3542 * ready. This function will wait in a loop for the HBA to be ready
3543 * If the HBA is not ready , the function will will reset the HBA PCI
3544 * function again. The function returns 1 when HBA fail to be ready
3545 * otherwise returns zero.
3546 **/
3547static int
3548lpfc_sli_brdready_s4(struct lpfc_hba *phba, uint32_t mask)
3549{
3550 uint32_t status;
3551 int retval = 0;
3552
3553 /* Read the HBA Host Status Register */
3554 status = lpfc_sli4_post_status_check(phba);
3555
3556 if (status) {
3557 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
3558 lpfc_sli_brdrestart(phba);
3559 status = lpfc_sli4_post_status_check(phba);
3560 }
3561
3562 /* Check to see if any errors occurred during init */
3563 if (status) {
3564 phba->link_state = LPFC_HBA_ERROR;
3565 retval = 1;
3566 } else
3567 phba->sli4_hba.intr_enable = 0;
3568
3569 return retval;
3570}
3571
3572/**
3573 * lpfc_sli_brdready - Wrapper func for checking the hba readyness
3574 * @phba: Pointer to HBA context object.
3575 * @mask: Bit mask to be checked.
3576 *
3577 * This routine wraps the actual SLI3 or SLI4 hba readyness check routine
3578 * from the API jump table function pointer from the lpfc_hba struct.
3579 **/
3580int
3581lpfc_sli_brdready(struct lpfc_hba *phba, uint32_t mask)
3582{
3583 return phba->lpfc_sli_brdready(phba, mask);
3584}
3585
James Smart92908312006-03-07 15:04:13 -05003586#define BARRIER_TEST_PATTERN (0xdeadbeef)
3587
James Smarte59058c2008-08-24 21:49:00 -04003588/**
James Smart3621a712009-04-06 18:47:14 -04003589 * lpfc_reset_barrier - Make HBA ready for HBA reset
James Smarte59058c2008-08-24 21:49:00 -04003590 * @phba: Pointer to HBA context object.
3591 *
3592 * This function is called before resetting an HBA. This
3593 * function requests HBA to quiesce DMAs before a reset.
3594 **/
James Smart2e0fef82007-06-17 19:56:36 -05003595void lpfc_reset_barrier(struct lpfc_hba *phba)
James Smart92908312006-03-07 15:04:13 -05003596{
James Smart65a29c12006-07-06 15:50:50 -04003597 uint32_t __iomem *resp_buf;
3598 uint32_t __iomem *mbox_buf;
James Smart92908312006-03-07 15:04:13 -05003599 volatile uint32_t mbox;
3600 uint32_t hc_copy;
3601 int i;
3602 uint8_t hdrtype;
3603
3604 pci_read_config_byte(phba->pcidev, PCI_HEADER_TYPE, &hdrtype);
3605 if (hdrtype != 0x80 ||
3606 (FC_JEDEC_ID(phba->vpd.rev.biuRev) != HELIOS_JEDEC_ID &&
3607 FC_JEDEC_ID(phba->vpd.rev.biuRev) != THOR_JEDEC_ID))
3608 return;
3609
3610 /*
3611 * Tell the other part of the chip to suspend temporarily all
3612 * its DMA activity.
3613 */
James Smart65a29c12006-07-06 15:50:50 -04003614 resp_buf = phba->MBslimaddr;
James Smart92908312006-03-07 15:04:13 -05003615
3616 /* Disable the error attention */
3617 hc_copy = readl(phba->HCregaddr);
3618 writel((hc_copy & ~HC_ERINT_ENA), phba->HCregaddr);
3619 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05003620 phba->link_flag |= LS_IGNORE_ERATT;
James Smart92908312006-03-07 15:04:13 -05003621
3622 if (readl(phba->HAregaddr) & HA_ERATT) {
3623 /* Clear Chip error bit */
3624 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05003625 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05003626 }
3627
3628 mbox = 0;
3629 ((MAILBOX_t *)&mbox)->mbxCommand = MBX_KILL_BOARD;
3630 ((MAILBOX_t *)&mbox)->mbxOwner = OWN_CHIP;
3631
3632 writel(BARRIER_TEST_PATTERN, (resp_buf + 1));
James Smart65a29c12006-07-06 15:50:50 -04003633 mbox_buf = phba->MBslimaddr;
James Smart92908312006-03-07 15:04:13 -05003634 writel(mbox, mbox_buf);
3635
3636 for (i = 0;
3637 readl(resp_buf + 1) != ~(BARRIER_TEST_PATTERN) && i < 50; i++)
3638 mdelay(1);
3639
3640 if (readl(resp_buf + 1) != ~(BARRIER_TEST_PATTERN)) {
James Smartf4b4c682009-05-22 14:53:12 -04003641 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE ||
James Smart2e0fef82007-06-17 19:56:36 -05003642 phba->pport->stopped)
James Smart92908312006-03-07 15:04:13 -05003643 goto restore_hc;
3644 else
3645 goto clear_errat;
3646 }
3647
3648 ((MAILBOX_t *)&mbox)->mbxOwner = OWN_HOST;
3649 for (i = 0; readl(resp_buf) != mbox && i < 500; i++)
3650 mdelay(1);
3651
3652clear_errat:
3653
3654 while (!(readl(phba->HAregaddr) & HA_ERATT) && ++i < 500)
3655 mdelay(1);
3656
3657 if (readl(phba->HAregaddr) & HA_ERATT) {
3658 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05003659 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05003660 }
3661
3662restore_hc:
James Smart2e0fef82007-06-17 19:56:36 -05003663 phba->link_flag &= ~LS_IGNORE_ERATT;
James Smart92908312006-03-07 15:04:13 -05003664 writel(hc_copy, phba->HCregaddr);
3665 readl(phba->HCregaddr); /* flush */
3666}
3667
James Smarte59058c2008-08-24 21:49:00 -04003668/**
James Smart3621a712009-04-06 18:47:14 -04003669 * lpfc_sli_brdkill - Issue a kill_board mailbox command
James Smarte59058c2008-08-24 21:49:00 -04003670 * @phba: Pointer to HBA context object.
3671 *
3672 * This function issues a kill_board mailbox command and waits for
3673 * the error attention interrupt. This function is called for stopping
3674 * the firmware processing. The caller is not required to hold any
3675 * locks. This function calls lpfc_hba_down_post function to free
3676 * any pending commands after the kill. The function will return 1 when it
3677 * fails to kill the board else will return 0.
3678 **/
Jamie Wellnitz41415862006-02-28 19:25:27 -05003679int
James Smart2e0fef82007-06-17 19:56:36 -05003680lpfc_sli_brdkill(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05003681{
Jamie Wellnitz41415862006-02-28 19:25:27 -05003682 struct lpfc_sli *psli;
3683 LPFC_MBOXQ_t *pmb;
3684 uint32_t status;
3685 uint32_t ha_copy;
3686 int retval;
3687 int i = 0;
3688
3689 psli = &phba->sli;
3690
3691 /* Kill HBA */
James Smarted957682007-06-17 19:56:37 -05003692 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003693 "0329 Kill HBA Data: x%x x%x\n",
3694 phba->pport->port_state, psli->sli_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003695
James Smart98c9ea52007-10-27 13:37:33 -04003696 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3697 if (!pmb)
Jamie Wellnitz41415862006-02-28 19:25:27 -05003698 return 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003699
3700 /* Disable the error attention */
James Smart2e0fef82007-06-17 19:56:36 -05003701 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003702 status = readl(phba->HCregaddr);
3703 status &= ~HC_ERINT_ENA;
3704 writel(status, phba->HCregaddr);
3705 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05003706 phba->link_flag |= LS_IGNORE_ERATT;
3707 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003708
3709 lpfc_kill_board(phba, pmb);
3710 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
3711 retval = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
3712
3713 if (retval != MBX_SUCCESS) {
3714 if (retval != MBX_BUSY)
3715 mempool_free(pmb, phba->mbox_mem_pool);
James Smarte40a02c2010-02-26 14:13:54 -05003716 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3717 "2752 KILL_BOARD command failed retval %d\n",
3718 retval);
James Smart2e0fef82007-06-17 19:56:36 -05003719 spin_lock_irq(&phba->hbalock);
3720 phba->link_flag &= ~LS_IGNORE_ERATT;
3721 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003722 return 1;
3723 }
3724
James Smartf4b4c682009-05-22 14:53:12 -04003725 spin_lock_irq(&phba->hbalock);
3726 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
3727 spin_unlock_irq(&phba->hbalock);
James Smart92908312006-03-07 15:04:13 -05003728
Jamie Wellnitz41415862006-02-28 19:25:27 -05003729 mempool_free(pmb, phba->mbox_mem_pool);
3730
3731 /* There is no completion for a KILL_BOARD mbox cmd. Check for an error
3732 * attention every 100ms for 3 seconds. If we don't get ERATT after
3733 * 3 seconds we still set HBA_ERROR state because the status of the
3734 * board is now undefined.
3735 */
3736 ha_copy = readl(phba->HAregaddr);
3737
3738 while ((i++ < 30) && !(ha_copy & HA_ERATT)) {
3739 mdelay(100);
3740 ha_copy = readl(phba->HAregaddr);
3741 }
3742
3743 del_timer_sync(&psli->mbox_tmo);
James Smart92908312006-03-07 15:04:13 -05003744 if (ha_copy & HA_ERATT) {
3745 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05003746 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05003747 }
James Smart2e0fef82007-06-17 19:56:36 -05003748 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003749 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart04c68492009-05-22 14:52:52 -04003750 psli->mbox_active = NULL;
James Smart2e0fef82007-06-17 19:56:36 -05003751 phba->link_flag &= ~LS_IGNORE_ERATT;
3752 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003753
Jamie Wellnitz41415862006-02-28 19:25:27 -05003754 lpfc_hba_down_post(phba);
James Smart2e0fef82007-06-17 19:56:36 -05003755 phba->link_state = LPFC_HBA_ERROR;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003756
James Smart2e0fef82007-06-17 19:56:36 -05003757 return ha_copy & HA_ERATT ? 0 : 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003758}
3759
James Smarte59058c2008-08-24 21:49:00 -04003760/**
James Smart3772a992009-05-22 14:50:54 -04003761 * lpfc_sli_brdreset - Reset a sli-2 or sli-3 HBA
James Smarte59058c2008-08-24 21:49:00 -04003762 * @phba: Pointer to HBA context object.
3763 *
3764 * This function resets the HBA by writing HC_INITFF to the control
3765 * register. After the HBA resets, this function resets all the iocb ring
3766 * indices. This function disables PCI layer parity checking during
3767 * the reset.
3768 * This function returns 0 always.
3769 * The caller is not required to hold any locks.
3770 **/
Jamie Wellnitz41415862006-02-28 19:25:27 -05003771int
James Smart2e0fef82007-06-17 19:56:36 -05003772lpfc_sli_brdreset(struct lpfc_hba *phba)
Jamie Wellnitz41415862006-02-28 19:25:27 -05003773{
3774 struct lpfc_sli *psli;
dea31012005-04-17 16:05:31 -05003775 struct lpfc_sli_ring *pring;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003776 uint16_t cfg_value;
dea31012005-04-17 16:05:31 -05003777 int i;
dea31012005-04-17 16:05:31 -05003778
Jamie Wellnitz41415862006-02-28 19:25:27 -05003779 psli = &phba->sli;
dea31012005-04-17 16:05:31 -05003780
Jamie Wellnitz41415862006-02-28 19:25:27 -05003781 /* Reset HBA */
3782 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003783 "0325 Reset HBA Data: x%x x%x\n",
James Smart2e0fef82007-06-17 19:56:36 -05003784 phba->pport->port_state, psli->sli_flag);
dea31012005-04-17 16:05:31 -05003785
3786 /* perform board reset */
3787 phba->fc_eventTag = 0;
James Smart4d9ab992009-10-02 15:16:39 -04003788 phba->link_events = 0;
James Smart2e0fef82007-06-17 19:56:36 -05003789 phba->pport->fc_myDID = 0;
3790 phba->pport->fc_prevDID = 0;
dea31012005-04-17 16:05:31 -05003791
Jamie Wellnitz41415862006-02-28 19:25:27 -05003792 /* Turn off parity checking and serr during the physical reset */
3793 pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
3794 pci_write_config_word(phba->pcidev, PCI_COMMAND,
3795 (cfg_value &
3796 ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
3797
James Smart3772a992009-05-22 14:50:54 -04003798 psli->sli_flag &= ~(LPFC_SLI_ACTIVE | LPFC_PROCESS_LA);
3799
Jamie Wellnitz41415862006-02-28 19:25:27 -05003800 /* Now toggle INITFF bit in the Host Control Register */
3801 writel(HC_INITFF, phba->HCregaddr);
3802 mdelay(1);
3803 readl(phba->HCregaddr); /* flush */
3804 writel(0, phba->HCregaddr);
3805 readl(phba->HCregaddr); /* flush */
3806
3807 /* Restore PCI cmd register */
3808 pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
dea31012005-04-17 16:05:31 -05003809
3810 /* Initialize relevant SLI info */
Jamie Wellnitz41415862006-02-28 19:25:27 -05003811 for (i = 0; i < psli->num_rings; i++) {
3812 pring = &psli->ring[i];
dea31012005-04-17 16:05:31 -05003813 pring->flag = 0;
3814 pring->rspidx = 0;
3815 pring->next_cmdidx = 0;
3816 pring->local_getidx = 0;
3817 pring->cmdidx = 0;
3818 pring->missbufcnt = 0;
3819 }
dea31012005-04-17 16:05:31 -05003820
James Smart2e0fef82007-06-17 19:56:36 -05003821 phba->link_state = LPFC_WARM_START;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003822 return 0;
3823}
3824
James Smarte59058c2008-08-24 21:49:00 -04003825/**
James Smartda0436e2009-05-22 14:51:39 -04003826 * lpfc_sli4_brdreset - Reset a sli-4 HBA
3827 * @phba: Pointer to HBA context object.
3828 *
3829 * This function resets a SLI4 HBA. This function disables PCI layer parity
3830 * checking during resets the device. The caller is not required to hold
3831 * any locks.
3832 *
3833 * This function returns 0 always.
3834 **/
3835int
3836lpfc_sli4_brdreset(struct lpfc_hba *phba)
3837{
3838 struct lpfc_sli *psli = &phba->sli;
3839 uint16_t cfg_value;
3840 uint8_t qindx;
3841
3842 /* Reset HBA */
3843 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3844 "0295 Reset HBA Data: x%x x%x\n",
3845 phba->pport->port_state, psli->sli_flag);
3846
3847 /* perform board reset */
3848 phba->fc_eventTag = 0;
James Smart4d9ab992009-10-02 15:16:39 -04003849 phba->link_events = 0;
James Smartda0436e2009-05-22 14:51:39 -04003850 phba->pport->fc_myDID = 0;
3851 phba->pport->fc_prevDID = 0;
3852
James Smartda0436e2009-05-22 14:51:39 -04003853 spin_lock_irq(&phba->hbalock);
3854 psli->sli_flag &= ~(LPFC_PROCESS_LA);
3855 phba->fcf.fcf_flag = 0;
3856 /* Clean up the child queue list for the CQs */
3857 list_del_init(&phba->sli4_hba.mbx_wq->list);
3858 list_del_init(&phba->sli4_hba.els_wq->list);
3859 list_del_init(&phba->sli4_hba.hdr_rq->list);
3860 list_del_init(&phba->sli4_hba.dat_rq->list);
3861 list_del_init(&phba->sli4_hba.mbx_cq->list);
3862 list_del_init(&phba->sli4_hba.els_cq->list);
James Smartda0436e2009-05-22 14:51:39 -04003863 for (qindx = 0; qindx < phba->cfg_fcp_wq_count; qindx++)
3864 list_del_init(&phba->sli4_hba.fcp_wq[qindx]->list);
3865 for (qindx = 0; qindx < phba->cfg_fcp_eq_count; qindx++)
3866 list_del_init(&phba->sli4_hba.fcp_cq[qindx]->list);
3867 spin_unlock_irq(&phba->hbalock);
3868
3869 /* Now physically reset the device */
3870 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3871 "0389 Performing PCI function reset!\n");
James Smartbe858b62010-12-15 17:57:20 -05003872
3873 /* Turn off parity checking and serr during the physical reset */
3874 pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
3875 pci_write_config_word(phba->pcidev, PCI_COMMAND, (cfg_value &
3876 ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
3877
James Smartda0436e2009-05-22 14:51:39 -04003878 /* Perform FCoE PCI function reset */
3879 lpfc_pci_function_reset(phba);
3880
James Smartbe858b62010-12-15 17:57:20 -05003881 /* Restore PCI cmd register */
3882 pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
3883
James Smartda0436e2009-05-22 14:51:39 -04003884 return 0;
3885}
3886
3887/**
3888 * lpfc_sli_brdrestart_s3 - Restart a sli-3 hba
James Smarte59058c2008-08-24 21:49:00 -04003889 * @phba: Pointer to HBA context object.
3890 *
3891 * This function is called in the SLI initialization code path to
3892 * restart the HBA. The caller is not required to hold any lock.
3893 * This function writes MBX_RESTART mailbox command to the SLIM and
3894 * resets the HBA. At the end of the function, it calls lpfc_hba_down_post
3895 * function to free any pending commands. The function enables
3896 * POST only during the first initialization. The function returns zero.
3897 * The function does not guarantee completion of MBX_RESTART mailbox
3898 * command before the return of this function.
3899 **/
James Smartda0436e2009-05-22 14:51:39 -04003900static int
3901lpfc_sli_brdrestart_s3(struct lpfc_hba *phba)
Jamie Wellnitz41415862006-02-28 19:25:27 -05003902{
3903 MAILBOX_t *mb;
3904 struct lpfc_sli *psli;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003905 volatile uint32_t word0;
3906 void __iomem *to_slim;
James Smart0d878412009-10-02 15:16:56 -04003907 uint32_t hba_aer_enabled;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003908
James Smart2e0fef82007-06-17 19:56:36 -05003909 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003910
James Smart0d878412009-10-02 15:16:56 -04003911 /* Take PCIe device Advanced Error Reporting (AER) state */
3912 hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
3913
Jamie Wellnitz41415862006-02-28 19:25:27 -05003914 psli = &phba->sli;
3915
3916 /* Restart HBA */
3917 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003918 "0337 Restart HBA Data: x%x x%x\n",
James Smart2e0fef82007-06-17 19:56:36 -05003919 phba->pport->port_state, psli->sli_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003920
3921 word0 = 0;
3922 mb = (MAILBOX_t *) &word0;
3923 mb->mbxCommand = MBX_RESTART;
3924 mb->mbxHc = 1;
3925
James Smart92908312006-03-07 15:04:13 -05003926 lpfc_reset_barrier(phba);
3927
Jamie Wellnitz41415862006-02-28 19:25:27 -05003928 to_slim = phba->MBslimaddr;
3929 writel(*(uint32_t *) mb, to_slim);
3930 readl(to_slim); /* flush */
3931
3932 /* Only skip post after fc_ffinit is completed */
James Smarteaf15d52008-12-04 22:39:29 -05003933 if (phba->pport->port_state)
Jamie Wellnitz41415862006-02-28 19:25:27 -05003934 word0 = 1; /* This is really setting up word1 */
James Smarteaf15d52008-12-04 22:39:29 -05003935 else
Jamie Wellnitz41415862006-02-28 19:25:27 -05003936 word0 = 0; /* This is really setting up word1 */
James Smart65a29c12006-07-06 15:50:50 -04003937 to_slim = phba->MBslimaddr + sizeof (uint32_t);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003938 writel(*(uint32_t *) mb, to_slim);
3939 readl(to_slim); /* flush */
3940
3941 lpfc_sli_brdreset(phba);
James Smart2e0fef82007-06-17 19:56:36 -05003942 phba->pport->stopped = 0;
3943 phba->link_state = LPFC_INIT_START;
James Smartda0436e2009-05-22 14:51:39 -04003944 phba->hba_flag = 0;
James Smart2e0fef82007-06-17 19:56:36 -05003945 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003946
James Smart64ba8812006-08-02 15:24:34 -04003947 memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
3948 psli->stats_start = get_seconds();
3949
James Smarteaf15d52008-12-04 22:39:29 -05003950 /* Give the INITFF and Post time to settle. */
3951 mdelay(100);
dea31012005-04-17 16:05:31 -05003952
James Smart0d878412009-10-02 15:16:56 -04003953 /* Reset HBA AER if it was enabled, note hba_flag was reset above */
3954 if (hba_aer_enabled)
3955 pci_disable_pcie_error_reporting(phba->pcidev);
3956
Jamie Wellnitz41415862006-02-28 19:25:27 -05003957 lpfc_hba_down_post(phba);
dea31012005-04-17 16:05:31 -05003958
3959 return 0;
3960}
3961
James Smarte59058c2008-08-24 21:49:00 -04003962/**
James Smartda0436e2009-05-22 14:51:39 -04003963 * lpfc_sli_brdrestart_s4 - Restart the sli-4 hba
3964 * @phba: Pointer to HBA context object.
3965 *
3966 * This function is called in the SLI initialization code path to restart
3967 * a SLI4 HBA. The caller is not required to hold any lock.
3968 * At the end of the function, it calls lpfc_hba_down_post function to
3969 * free any pending commands.
3970 **/
3971static int
3972lpfc_sli_brdrestart_s4(struct lpfc_hba *phba)
3973{
3974 struct lpfc_sli *psli = &phba->sli;
James Smart75baf692010-06-08 18:31:21 -04003975 uint32_t hba_aer_enabled;
James Smartda0436e2009-05-22 14:51:39 -04003976
3977 /* Restart HBA */
3978 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3979 "0296 Restart HBA Data: x%x x%x\n",
3980 phba->pport->port_state, psli->sli_flag);
3981
James Smart75baf692010-06-08 18:31:21 -04003982 /* Take PCIe device Advanced Error Reporting (AER) state */
3983 hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
3984
James Smartda0436e2009-05-22 14:51:39 -04003985 lpfc_sli4_brdreset(phba);
3986
3987 spin_lock_irq(&phba->hbalock);
3988 phba->pport->stopped = 0;
3989 phba->link_state = LPFC_INIT_START;
3990 phba->hba_flag = 0;
3991 spin_unlock_irq(&phba->hbalock);
3992
3993 memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
3994 psli->stats_start = get_seconds();
3995
James Smart75baf692010-06-08 18:31:21 -04003996 /* Reset HBA AER if it was enabled, note hba_flag was reset above */
3997 if (hba_aer_enabled)
3998 pci_disable_pcie_error_reporting(phba->pcidev);
3999
James Smartda0436e2009-05-22 14:51:39 -04004000 lpfc_hba_down_post(phba);
4001
4002 return 0;
4003}
4004
4005/**
4006 * lpfc_sli_brdrestart - Wrapper func for restarting hba
4007 * @phba: Pointer to HBA context object.
4008 *
4009 * This routine wraps the actual SLI3 or SLI4 hba restart routine from the
4010 * API jump table function pointer from the lpfc_hba struct.
4011**/
4012int
4013lpfc_sli_brdrestart(struct lpfc_hba *phba)
4014{
4015 return phba->lpfc_sli_brdrestart(phba);
4016}
4017
4018/**
James Smart3621a712009-04-06 18:47:14 -04004019 * lpfc_sli_chipset_init - Wait for the restart of the HBA after a restart
James Smarte59058c2008-08-24 21:49:00 -04004020 * @phba: Pointer to HBA context object.
4021 *
4022 * This function is called after a HBA restart to wait for successful
4023 * restart of the HBA. Successful restart of the HBA is indicated by
4024 * HS_FFRDY and HS_MBRDY bits. If the HBA fails to restart even after 15
4025 * iteration, the function will restart the HBA again. The function returns
4026 * zero if HBA successfully restarted else returns negative error code.
4027 **/
dea31012005-04-17 16:05:31 -05004028static int
4029lpfc_sli_chipset_init(struct lpfc_hba *phba)
4030{
4031 uint32_t status, i = 0;
4032
4033 /* Read the HBA Host Status Register */
4034 status = readl(phba->HSregaddr);
4035
4036 /* Check status register to see what current state is */
4037 i = 0;
4038 while ((status & (HS_FFRDY | HS_MBRDY)) != (HS_FFRDY | HS_MBRDY)) {
4039
James Smartdcf2a4e2010-09-29 11:18:53 -04004040 /* Check every 10ms for 10 retries, then every 100ms for 90
4041 * retries, then every 1 sec for 50 retires for a total of
4042 * ~60 seconds before reset the board again and check every
4043 * 1 sec for 50 retries. The up to 60 seconds before the
4044 * board ready is required by the Falcon FIPS zeroization
4045 * complete, and any reset the board in between shall cause
4046 * restart of zeroization, further delay the board ready.
dea31012005-04-17 16:05:31 -05004047 */
James Smartdcf2a4e2010-09-29 11:18:53 -04004048 if (i++ >= 200) {
dea31012005-04-17 16:05:31 -05004049 /* Adapter failed to init, timeout, status reg
4050 <status> */
James Smarted957682007-06-17 19:56:37 -05004051 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004052 "0436 Adapter failed to init, "
James Smart09372822008-01-11 01:52:54 -05004053 "timeout, status reg x%x, "
4054 "FW Data: A8 x%x AC x%x\n", status,
4055 readl(phba->MBslimaddr + 0xa8),
4056 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05004057 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05004058 return -ETIMEDOUT;
4059 }
4060
4061 /* Check to see if any errors occurred during init */
4062 if (status & HS_FFERM) {
4063 /* ERROR: During chipset initialization */
4064 /* Adapter failed to init, chipset, status reg
4065 <status> */
James Smarted957682007-06-17 19:56:37 -05004066 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004067 "0437 Adapter failed to init, "
James Smart09372822008-01-11 01:52:54 -05004068 "chipset, status reg x%x, "
4069 "FW Data: A8 x%x AC x%x\n", status,
4070 readl(phba->MBslimaddr + 0xa8),
4071 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05004072 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05004073 return -EIO;
4074 }
4075
James Smartdcf2a4e2010-09-29 11:18:53 -04004076 if (i <= 10)
dea31012005-04-17 16:05:31 -05004077 msleep(10);
James Smartdcf2a4e2010-09-29 11:18:53 -04004078 else if (i <= 100)
4079 msleep(100);
4080 else
4081 msleep(1000);
dea31012005-04-17 16:05:31 -05004082
James Smartdcf2a4e2010-09-29 11:18:53 -04004083 if (i == 150) {
4084 /* Do post */
James Smart92d7f7b2007-06-17 19:56:38 -05004085 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004086 lpfc_sli_brdrestart(phba);
dea31012005-04-17 16:05:31 -05004087 }
4088 /* Read the HBA Host Status Register */
4089 status = readl(phba->HSregaddr);
4090 }
4091
4092 /* Check to see if any errors occurred during init */
4093 if (status & HS_FFERM) {
4094 /* ERROR: During chipset initialization */
4095 /* Adapter failed to init, chipset, status reg <status> */
James Smarted957682007-06-17 19:56:37 -05004096 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004097 "0438 Adapter failed to init, chipset, "
James Smart09372822008-01-11 01:52:54 -05004098 "status reg x%x, "
4099 "FW Data: A8 x%x AC x%x\n", status,
4100 readl(phba->MBslimaddr + 0xa8),
4101 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05004102 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05004103 return -EIO;
4104 }
4105
4106 /* Clear all interrupt enable conditions */
4107 writel(0, phba->HCregaddr);
4108 readl(phba->HCregaddr); /* flush */
4109
4110 /* setup host attn register */
4111 writel(0xffffffff, phba->HAregaddr);
4112 readl(phba->HAregaddr); /* flush */
4113 return 0;
4114}
4115
James Smarte59058c2008-08-24 21:49:00 -04004116/**
James Smart3621a712009-04-06 18:47:14 -04004117 * lpfc_sli_hbq_count - Get the number of HBQs to be configured
James Smarte59058c2008-08-24 21:49:00 -04004118 *
4119 * This function calculates and returns the number of HBQs required to be
4120 * configured.
4121 **/
James Smart78b2d852007-08-02 11:10:21 -04004122int
James Smarted957682007-06-17 19:56:37 -05004123lpfc_sli_hbq_count(void)
4124{
James Smart92d7f7b2007-06-17 19:56:38 -05004125 return ARRAY_SIZE(lpfc_hbq_defs);
James Smarted957682007-06-17 19:56:37 -05004126}
4127
James Smarte59058c2008-08-24 21:49:00 -04004128/**
James Smart3621a712009-04-06 18:47:14 -04004129 * lpfc_sli_hbq_entry_count - Calculate total number of hbq entries
James Smarte59058c2008-08-24 21:49:00 -04004130 *
4131 * This function adds the number of hbq entries in every HBQ to get
4132 * the total number of hbq entries required for the HBA and returns
4133 * the total count.
4134 **/
James Smarted957682007-06-17 19:56:37 -05004135static int
4136lpfc_sli_hbq_entry_count(void)
4137{
4138 int hbq_count = lpfc_sli_hbq_count();
4139 int count = 0;
4140 int i;
4141
4142 for (i = 0; i < hbq_count; ++i)
James Smart92d7f7b2007-06-17 19:56:38 -05004143 count += lpfc_hbq_defs[i]->entry_count;
James Smarted957682007-06-17 19:56:37 -05004144 return count;
4145}
4146
James Smarte59058c2008-08-24 21:49:00 -04004147/**
James Smart3621a712009-04-06 18:47:14 -04004148 * lpfc_sli_hbq_size - Calculate memory required for all hbq entries
James Smarte59058c2008-08-24 21:49:00 -04004149 *
4150 * This function calculates amount of memory required for all hbq entries
4151 * to be configured and returns the total memory required.
4152 **/
dea31012005-04-17 16:05:31 -05004153int
James Smarted957682007-06-17 19:56:37 -05004154lpfc_sli_hbq_size(void)
4155{
4156 return lpfc_sli_hbq_entry_count() * sizeof(struct lpfc_hbq_entry);
4157}
4158
James Smarte59058c2008-08-24 21:49:00 -04004159/**
James Smart3621a712009-04-06 18:47:14 -04004160 * lpfc_sli_hbq_setup - configure and initialize HBQs
James Smarte59058c2008-08-24 21:49:00 -04004161 * @phba: Pointer to HBA context object.
4162 *
4163 * This function is called during the SLI initialization to configure
4164 * all the HBQs and post buffers to the HBQ. The caller is not
4165 * required to hold any locks. This function will return zero if successful
4166 * else it will return negative error code.
4167 **/
James Smarted957682007-06-17 19:56:37 -05004168static int
4169lpfc_sli_hbq_setup(struct lpfc_hba *phba)
4170{
4171 int hbq_count = lpfc_sli_hbq_count();
4172 LPFC_MBOXQ_t *pmb;
4173 MAILBOX_t *pmbox;
4174 uint32_t hbqno;
4175 uint32_t hbq_entry_index;
James Smarted957682007-06-17 19:56:37 -05004176
James Smart92d7f7b2007-06-17 19:56:38 -05004177 /* Get a Mailbox buffer to setup mailbox
4178 * commands for HBA initialization
4179 */
James Smarted957682007-06-17 19:56:37 -05004180 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4181
4182 if (!pmb)
4183 return -ENOMEM;
4184
James Smart04c68492009-05-22 14:52:52 -04004185 pmbox = &pmb->u.mb;
James Smarted957682007-06-17 19:56:37 -05004186
4187 /* Initialize the struct lpfc_sli_hbq structure for each hbq */
4188 phba->link_state = LPFC_INIT_MBX_CMDS;
James Smart3163f722008-02-08 18:50:25 -05004189 phba->hbq_in_use = 1;
James Smarted957682007-06-17 19:56:37 -05004190
4191 hbq_entry_index = 0;
4192 for (hbqno = 0; hbqno < hbq_count; ++hbqno) {
4193 phba->hbqs[hbqno].next_hbqPutIdx = 0;
4194 phba->hbqs[hbqno].hbqPutIdx = 0;
4195 phba->hbqs[hbqno].local_hbqGetIdx = 0;
4196 phba->hbqs[hbqno].entry_count =
James Smart92d7f7b2007-06-17 19:56:38 -05004197 lpfc_hbq_defs[hbqno]->entry_count;
James Smart51ef4c22007-08-02 11:10:31 -04004198 lpfc_config_hbq(phba, hbqno, lpfc_hbq_defs[hbqno],
4199 hbq_entry_index, pmb);
James Smarted957682007-06-17 19:56:37 -05004200 hbq_entry_index += phba->hbqs[hbqno].entry_count;
4201
4202 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
4203 /* Adapter failed to init, mbxCmd <cmd> CFG_RING,
4204 mbxStatus <status>, ring <num> */
4205
4206 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -05004207 LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04004208 "1805 Adapter failed to init. "
James Smarted957682007-06-17 19:56:37 -05004209 "Data: x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04004210 pmbox->mbxCommand,
James Smarted957682007-06-17 19:56:37 -05004211 pmbox->mbxStatus, hbqno);
4212
4213 phba->link_state = LPFC_HBA_ERROR;
4214 mempool_free(pmb, phba->mbox_mem_pool);
James Smart6e7288d2010-06-07 15:23:35 -04004215 return -ENXIO;
James Smarted957682007-06-17 19:56:37 -05004216 }
4217 }
4218 phba->hbq_count = hbq_count;
4219
James Smarted957682007-06-17 19:56:37 -05004220 mempool_free(pmb, phba->mbox_mem_pool);
4221
James Smart92d7f7b2007-06-17 19:56:38 -05004222 /* Initially populate or replenish the HBQs */
James Smartd7c255b2008-08-24 21:50:00 -04004223 for (hbqno = 0; hbqno < hbq_count; ++hbqno)
4224 lpfc_sli_hbqbuf_init_hbqs(phba, hbqno);
James Smarted957682007-06-17 19:56:37 -05004225 return 0;
4226}
4227
James Smarte59058c2008-08-24 21:49:00 -04004228/**
James Smart4f774512009-05-22 14:52:35 -04004229 * lpfc_sli4_rb_setup - Initialize and post RBs to HBA
4230 * @phba: Pointer to HBA context object.
4231 *
4232 * This function is called during the SLI initialization to configure
4233 * all the HBQs and post buffers to the HBQ. The caller is not
4234 * required to hold any locks. This function will return zero if successful
4235 * else it will return negative error code.
4236 **/
4237static int
4238lpfc_sli4_rb_setup(struct lpfc_hba *phba)
4239{
4240 phba->hbq_in_use = 1;
4241 phba->hbqs[0].entry_count = lpfc_hbq_defs[0]->entry_count;
4242 phba->hbq_count = 1;
4243 /* Initially populate or replenish the HBQs */
4244 lpfc_sli_hbqbuf_init_hbqs(phba, 0);
4245 return 0;
4246}
4247
4248/**
James Smart3621a712009-04-06 18:47:14 -04004249 * lpfc_sli_config_port - Issue config port mailbox command
James Smarte59058c2008-08-24 21:49:00 -04004250 * @phba: Pointer to HBA context object.
4251 * @sli_mode: sli mode - 2/3
4252 *
4253 * This function is called by the sli intialization code path
4254 * to issue config_port mailbox command. This function restarts the
4255 * HBA firmware and issues a config_port mailbox command to configure
4256 * the SLI interface in the sli mode specified by sli_mode
4257 * variable. The caller is not required to hold any locks.
4258 * The function returns 0 if successful, else returns negative error
4259 * code.
4260 **/
James Smart93996272008-08-24 21:50:30 -04004261int
4262lpfc_sli_config_port(struct lpfc_hba *phba, int sli_mode)
dea31012005-04-17 16:05:31 -05004263{
4264 LPFC_MBOXQ_t *pmb;
4265 uint32_t resetcount = 0, rc = 0, done = 0;
4266
4267 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4268 if (!pmb) {
James Smart2e0fef82007-06-17 19:56:36 -05004269 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05004270 return -ENOMEM;
4271 }
4272
James Smarted957682007-06-17 19:56:37 -05004273 phba->sli_rev = sli_mode;
dea31012005-04-17 16:05:31 -05004274 while (resetcount < 2 && !done) {
James Smart2e0fef82007-06-17 19:56:36 -05004275 spin_lock_irq(&phba->hbalock);
James Smart1c067a42006-08-01 07:33:52 -04004276 phba->sli.sli_flag |= LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05004277 spin_unlock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -05004278 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004279 lpfc_sli_brdrestart(phba);
dea31012005-04-17 16:05:31 -05004280 rc = lpfc_sli_chipset_init(phba);
4281 if (rc)
4282 break;
4283
James Smart2e0fef82007-06-17 19:56:36 -05004284 spin_lock_irq(&phba->hbalock);
James Smart1c067a42006-08-01 07:33:52 -04004285 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05004286 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004287 resetcount++;
4288
James Smarted957682007-06-17 19:56:37 -05004289 /* Call pre CONFIG_PORT mailbox command initialization. A
4290 * value of 0 means the call was successful. Any other
4291 * nonzero value is a failure, but if ERESTART is returned,
4292 * the driver may reset the HBA and try again.
4293 */
dea31012005-04-17 16:05:31 -05004294 rc = lpfc_config_port_prep(phba);
4295 if (rc == -ERESTART) {
James Smarted957682007-06-17 19:56:37 -05004296 phba->link_state = LPFC_LINK_UNKNOWN;
dea31012005-04-17 16:05:31 -05004297 continue;
James Smart34b02dc2008-08-24 21:49:55 -04004298 } else if (rc)
dea31012005-04-17 16:05:31 -05004299 break;
James Smart2e0fef82007-06-17 19:56:36 -05004300 phba->link_state = LPFC_INIT_MBX_CMDS;
dea31012005-04-17 16:05:31 -05004301 lpfc_config_port(phba, pmb);
4302 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
James Smart34b02dc2008-08-24 21:49:55 -04004303 phba->sli3_options &= ~(LPFC_SLI3_NPIV_ENABLED |
4304 LPFC_SLI3_HBQ_ENABLED |
4305 LPFC_SLI3_CRP_ENABLED |
James Smartbc739052010-08-04 16:11:18 -04004306 LPFC_SLI3_BG_ENABLED |
4307 LPFC_SLI3_DSS_ENABLED);
James Smarted957682007-06-17 19:56:37 -05004308 if (rc != MBX_SUCCESS) {
dea31012005-04-17 16:05:31 -05004309 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004310 "0442 Adapter failed to init, mbxCmd x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05004311 "CONFIG_PORT, mbxStatus x%x Data: x%x\n",
James Smart04c68492009-05-22 14:52:52 -04004312 pmb->u.mb.mbxCommand, pmb->u.mb.mbxStatus, 0);
James Smart2e0fef82007-06-17 19:56:36 -05004313 spin_lock_irq(&phba->hbalock);
James Smart04c68492009-05-22 14:52:52 -04004314 phba->sli.sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05004315 spin_unlock_irq(&phba->hbalock);
4316 rc = -ENXIO;
James Smart04c68492009-05-22 14:52:52 -04004317 } else {
4318 /* Allow asynchronous mailbox command to go through */
4319 spin_lock_irq(&phba->hbalock);
4320 phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
4321 spin_unlock_irq(&phba->hbalock);
James Smarted957682007-06-17 19:56:37 -05004322 done = 1;
James Smart04c68492009-05-22 14:52:52 -04004323 }
dea31012005-04-17 16:05:31 -05004324 }
James Smarted957682007-06-17 19:56:37 -05004325 if (!done) {
4326 rc = -EINVAL;
4327 goto do_prep_failed;
4328 }
James Smart04c68492009-05-22 14:52:52 -04004329 if (pmb->u.mb.un.varCfgPort.sli_mode == 3) {
4330 if (!pmb->u.mb.un.varCfgPort.cMA) {
James Smart34b02dc2008-08-24 21:49:55 -04004331 rc = -ENXIO;
4332 goto do_prep_failed;
4333 }
James Smart04c68492009-05-22 14:52:52 -04004334 if (phba->max_vpi && pmb->u.mb.un.varCfgPort.gmv) {
James Smart34b02dc2008-08-24 21:49:55 -04004335 phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
James Smart04c68492009-05-22 14:52:52 -04004336 phba->max_vpi = pmb->u.mb.un.varCfgPort.max_vpi;
4337 phba->max_vports = (phba->max_vpi > phba->max_vports) ?
4338 phba->max_vpi : phba->max_vports;
4339
James Smart34b02dc2008-08-24 21:49:55 -04004340 } else
4341 phba->max_vpi = 0;
James Smartbc739052010-08-04 16:11:18 -04004342 phba->fips_level = 0;
4343 phba->fips_spec_rev = 0;
4344 if (pmb->u.mb.un.varCfgPort.gdss) {
James Smart04c68492009-05-22 14:52:52 -04004345 phba->sli3_options |= LPFC_SLI3_DSS_ENABLED;
James Smartbc739052010-08-04 16:11:18 -04004346 phba->fips_level = pmb->u.mb.un.varCfgPort.fips_level;
4347 phba->fips_spec_rev = pmb->u.mb.un.varCfgPort.fips_rev;
4348 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4349 "2850 Security Crypto Active. FIPS x%d "
4350 "(Spec Rev: x%d)",
4351 phba->fips_level, phba->fips_spec_rev);
4352 }
4353 if (pmb->u.mb.un.varCfgPort.sec_err) {
4354 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4355 "2856 Config Port Security Crypto "
4356 "Error: x%x ",
4357 pmb->u.mb.un.varCfgPort.sec_err);
4358 }
James Smart04c68492009-05-22 14:52:52 -04004359 if (pmb->u.mb.un.varCfgPort.gerbm)
James Smart34b02dc2008-08-24 21:49:55 -04004360 phba->sli3_options |= LPFC_SLI3_HBQ_ENABLED;
James Smart04c68492009-05-22 14:52:52 -04004361 if (pmb->u.mb.un.varCfgPort.gcrp)
James Smart34b02dc2008-08-24 21:49:55 -04004362 phba->sli3_options |= LPFC_SLI3_CRP_ENABLED;
James Smart6e7288d2010-06-07 15:23:35 -04004363
4364 phba->hbq_get = phba->mbox->us.s3_pgp.hbq_get;
4365 phba->port_gp = phba->mbox->us.s3_pgp.port;
James Smarte2a0a9d2008-12-04 22:40:02 -05004366
4367 if (phba->cfg_enable_bg) {
James Smart04c68492009-05-22 14:52:52 -04004368 if (pmb->u.mb.un.varCfgPort.gbg)
James Smarte2a0a9d2008-12-04 22:40:02 -05004369 phba->sli3_options |= LPFC_SLI3_BG_ENABLED;
4370 else
4371 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4372 "0443 Adapter did not grant "
4373 "BlockGuard\n");
4374 }
James Smart34b02dc2008-08-24 21:49:55 -04004375 } else {
James Smart8f34f4c2008-12-04 22:39:23 -05004376 phba->hbq_get = NULL;
James Smart34b02dc2008-08-24 21:49:55 -04004377 phba->port_gp = phba->mbox->us.s2.port;
James Smartd7c255b2008-08-24 21:50:00 -04004378 phba->max_vpi = 0;
James Smarted957682007-06-17 19:56:37 -05004379 }
James Smart92d7f7b2007-06-17 19:56:38 -05004380do_prep_failed:
James Smarted957682007-06-17 19:56:37 -05004381 mempool_free(pmb, phba->mbox_mem_pool);
4382 return rc;
4383}
4384
James Smarte59058c2008-08-24 21:49:00 -04004385
4386/**
James Smart3621a712009-04-06 18:47:14 -04004387 * lpfc_sli_hba_setup - SLI intialization function
James Smarte59058c2008-08-24 21:49:00 -04004388 * @phba: Pointer to HBA context object.
4389 *
4390 * This function is the main SLI intialization function. This function
4391 * is called by the HBA intialization code, HBA reset code and HBA
4392 * error attention handler code. Caller is not required to hold any
4393 * locks. This function issues config_port mailbox command to configure
4394 * the SLI, setup iocb rings and HBQ rings. In the end the function
4395 * calls the config_port_post function to issue init_link mailbox
4396 * command and to start the discovery. The function will return zero
4397 * if successful, else it will return negative error code.
4398 **/
James Smarted957682007-06-17 19:56:37 -05004399int
4400lpfc_sli_hba_setup(struct lpfc_hba *phba)
4401{
4402 uint32_t rc;
James Smart92d7f7b2007-06-17 19:56:38 -05004403 int mode = 3;
James Smarted957682007-06-17 19:56:37 -05004404
4405 switch (lpfc_sli_mode) {
4406 case 2:
James Smart78b2d852007-08-02 11:10:21 -04004407 if (phba->cfg_enable_npiv) {
James Smart92d7f7b2007-06-17 19:56:38 -05004408 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04004409 "1824 NPIV enabled: Override lpfc_sli_mode "
James Smart92d7f7b2007-06-17 19:56:38 -05004410 "parameter (%d) to auto (0).\n",
James Smarte8b62012007-08-02 11:10:09 -04004411 lpfc_sli_mode);
James Smart92d7f7b2007-06-17 19:56:38 -05004412 break;
4413 }
James Smarted957682007-06-17 19:56:37 -05004414 mode = 2;
4415 break;
4416 case 0:
4417 case 3:
4418 break;
4419 default:
James Smart92d7f7b2007-06-17 19:56:38 -05004420 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04004421 "1819 Unrecognized lpfc_sli_mode "
4422 "parameter: %d.\n", lpfc_sli_mode);
James Smarted957682007-06-17 19:56:37 -05004423
4424 break;
4425 }
4426
James Smart93996272008-08-24 21:50:30 -04004427 rc = lpfc_sli_config_port(phba, mode);
4428
James Smarted957682007-06-17 19:56:37 -05004429 if (rc && lpfc_sli_mode == 3)
James Smart92d7f7b2007-06-17 19:56:38 -05004430 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04004431 "1820 Unable to select SLI-3. "
4432 "Not supported by adapter.\n");
James Smarted957682007-06-17 19:56:37 -05004433 if (rc && mode != 2)
James Smart93996272008-08-24 21:50:30 -04004434 rc = lpfc_sli_config_port(phba, 2);
James Smarted957682007-06-17 19:56:37 -05004435 if (rc)
dea31012005-04-17 16:05:31 -05004436 goto lpfc_sli_hba_setup_error;
4437
James Smart0d878412009-10-02 15:16:56 -04004438 /* Enable PCIe device Advanced Error Reporting (AER) if configured */
4439 if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
4440 rc = pci_enable_pcie_error_reporting(phba->pcidev);
4441 if (!rc) {
4442 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4443 "2709 This device supports "
4444 "Advanced Error Reporting (AER)\n");
4445 spin_lock_irq(&phba->hbalock);
4446 phba->hba_flag |= HBA_AER_ENABLED;
4447 spin_unlock_irq(&phba->hbalock);
4448 } else {
4449 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4450 "2708 This device does not support "
4451 "Advanced Error Reporting (AER)\n");
4452 phba->cfg_aer_support = 0;
4453 }
4454 }
4455
James Smarted957682007-06-17 19:56:37 -05004456 if (phba->sli_rev == 3) {
4457 phba->iocb_cmd_size = SLI3_IOCB_CMD_SIZE;
4458 phba->iocb_rsp_size = SLI3_IOCB_RSP_SIZE;
James Smarted957682007-06-17 19:56:37 -05004459 } else {
4460 phba->iocb_cmd_size = SLI2_IOCB_CMD_SIZE;
4461 phba->iocb_rsp_size = SLI2_IOCB_RSP_SIZE;
James Smart92d7f7b2007-06-17 19:56:38 -05004462 phba->sli3_options = 0;
James Smarted957682007-06-17 19:56:37 -05004463 }
4464
4465 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004466 "0444 Firmware in SLI %x mode. Max_vpi %d\n",
4467 phba->sli_rev, phba->max_vpi);
James Smarted957682007-06-17 19:56:37 -05004468 rc = lpfc_sli_ring_map(phba);
dea31012005-04-17 16:05:31 -05004469
4470 if (rc)
4471 goto lpfc_sli_hba_setup_error;
4472
James Smart93996272008-08-24 21:50:30 -04004473 /* Init HBQs */
James Smarted957682007-06-17 19:56:37 -05004474 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
4475 rc = lpfc_sli_hbq_setup(phba);
4476 if (rc)
4477 goto lpfc_sli_hba_setup_error;
4478 }
James Smart04c68492009-05-22 14:52:52 -04004479 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004480 phba->sli.sli_flag |= LPFC_PROCESS_LA;
James Smart04c68492009-05-22 14:52:52 -04004481 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004482
4483 rc = lpfc_config_port_post(phba);
4484 if (rc)
4485 goto lpfc_sli_hba_setup_error;
4486
James Smarted957682007-06-17 19:56:37 -05004487 return rc;
4488
James Smart92d7f7b2007-06-17 19:56:38 -05004489lpfc_sli_hba_setup_error:
James Smart2e0fef82007-06-17 19:56:36 -05004490 phba->link_state = LPFC_HBA_ERROR;
James Smarte40a02c2010-02-26 14:13:54 -05004491 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004492 "0445 Firmware initialization failed\n");
dea31012005-04-17 16:05:31 -05004493 return rc;
4494}
4495
James Smartda0436e2009-05-22 14:51:39 -04004496/**
4497 * lpfc_sli4_read_fcoe_params - Read fcoe params from conf region
4498 * @phba: Pointer to HBA context object.
4499 * @mboxq: mailbox pointer.
4500 * This function issue a dump mailbox command to read config region
4501 * 23 and parse the records in the region and populate driver
4502 * data structure.
4503 **/
4504static int
4505lpfc_sli4_read_fcoe_params(struct lpfc_hba *phba,
4506 LPFC_MBOXQ_t *mboxq)
4507{
4508 struct lpfc_dmabuf *mp;
4509 struct lpfc_mqe *mqe;
4510 uint32_t data_length;
4511 int rc;
4512
4513 /* Program the default value of vlan_id and fc_map */
4514 phba->valid_vlan = 0;
4515 phba->fc_map[0] = LPFC_FCOE_FCF_MAP0;
4516 phba->fc_map[1] = LPFC_FCOE_FCF_MAP1;
4517 phba->fc_map[2] = LPFC_FCOE_FCF_MAP2;
4518
4519 mqe = &mboxq->u.mqe;
4520 if (lpfc_dump_fcoe_param(phba, mboxq))
4521 return -ENOMEM;
4522
4523 mp = (struct lpfc_dmabuf *) mboxq->context1;
4524 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4525
4526 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
4527 "(%d):2571 Mailbox cmd x%x Status x%x "
4528 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x "
4529 "x%x x%x x%x x%x x%x x%x x%x x%x x%x "
4530 "CQ: x%x x%x x%x x%x\n",
4531 mboxq->vport ? mboxq->vport->vpi : 0,
4532 bf_get(lpfc_mqe_command, mqe),
4533 bf_get(lpfc_mqe_status, mqe),
4534 mqe->un.mb_words[0], mqe->un.mb_words[1],
4535 mqe->un.mb_words[2], mqe->un.mb_words[3],
4536 mqe->un.mb_words[4], mqe->un.mb_words[5],
4537 mqe->un.mb_words[6], mqe->un.mb_words[7],
4538 mqe->un.mb_words[8], mqe->un.mb_words[9],
4539 mqe->un.mb_words[10], mqe->un.mb_words[11],
4540 mqe->un.mb_words[12], mqe->un.mb_words[13],
4541 mqe->un.mb_words[14], mqe->un.mb_words[15],
4542 mqe->un.mb_words[16], mqe->un.mb_words[50],
4543 mboxq->mcqe.word0,
4544 mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
4545 mboxq->mcqe.trailer);
4546
4547 if (rc) {
4548 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4549 kfree(mp);
4550 return -EIO;
4551 }
4552 data_length = mqe->un.mb_words[5];
James Smarta0c87cb2009-07-19 10:01:10 -04004553 if (data_length > DMP_RGN23_SIZE) {
James Smartd11e31d2009-06-10 17:23:06 -04004554 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4555 kfree(mp);
James Smartda0436e2009-05-22 14:51:39 -04004556 return -EIO;
James Smartd11e31d2009-06-10 17:23:06 -04004557 }
James Smartda0436e2009-05-22 14:51:39 -04004558
4559 lpfc_parse_fcoe_conf(phba, mp->virt, data_length);
4560 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4561 kfree(mp);
4562 return 0;
4563}
4564
4565/**
4566 * lpfc_sli4_read_rev - Issue READ_REV and collect vpd data
4567 * @phba: pointer to lpfc hba data structure.
4568 * @mboxq: pointer to the LPFC_MBOXQ_t structure.
4569 * @vpd: pointer to the memory to hold resulting port vpd data.
4570 * @vpd_size: On input, the number of bytes allocated to @vpd.
4571 * On output, the number of data bytes in @vpd.
4572 *
4573 * This routine executes a READ_REV SLI4 mailbox command. In
4574 * addition, this routine gets the port vpd data.
4575 *
4576 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02004577 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -04004578 * -ENOMEM - could not allocated memory.
James Smartda0436e2009-05-22 14:51:39 -04004579 **/
4580static int
4581lpfc_sli4_read_rev(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
4582 uint8_t *vpd, uint32_t *vpd_size)
4583{
4584 int rc = 0;
4585 uint32_t dma_size;
4586 struct lpfc_dmabuf *dmabuf;
4587 struct lpfc_mqe *mqe;
4588
4589 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
4590 if (!dmabuf)
4591 return -ENOMEM;
4592
4593 /*
4594 * Get a DMA buffer for the vpd data resulting from the READ_REV
4595 * mailbox command.
4596 */
4597 dma_size = *vpd_size;
4598 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
4599 dma_size,
4600 &dmabuf->phys,
4601 GFP_KERNEL);
4602 if (!dmabuf->virt) {
4603 kfree(dmabuf);
4604 return -ENOMEM;
4605 }
4606 memset(dmabuf->virt, 0, dma_size);
4607
4608 /*
4609 * The SLI4 implementation of READ_REV conflicts at word1,
4610 * bits 31:16 and SLI4 adds vpd functionality not present
4611 * in SLI3. This code corrects the conflicts.
4612 */
4613 lpfc_read_rev(phba, mboxq);
4614 mqe = &mboxq->u.mqe;
4615 mqe->un.read_rev.vpd_paddr_high = putPaddrHigh(dmabuf->phys);
4616 mqe->un.read_rev.vpd_paddr_low = putPaddrLow(dmabuf->phys);
4617 mqe->un.read_rev.word1 &= 0x0000FFFF;
4618 bf_set(lpfc_mbx_rd_rev_vpd, &mqe->un.read_rev, 1);
4619 bf_set(lpfc_mbx_rd_rev_avail_len, &mqe->un.read_rev, dma_size);
4620
4621 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4622 if (rc) {
4623 dma_free_coherent(&phba->pcidev->dev, dma_size,
4624 dmabuf->virt, dmabuf->phys);
James Smartdef9c7a2009-12-21 17:02:28 -05004625 kfree(dmabuf);
James Smartda0436e2009-05-22 14:51:39 -04004626 return -EIO;
4627 }
4628
James Smartda0436e2009-05-22 14:51:39 -04004629 /*
4630 * The available vpd length cannot be bigger than the
4631 * DMA buffer passed to the port. Catch the less than
4632 * case and update the caller's size.
4633 */
4634 if (mqe->un.read_rev.avail_vpd_len < *vpd_size)
4635 *vpd_size = mqe->un.read_rev.avail_vpd_len;
4636
James Smartd7c47992010-06-08 18:31:54 -04004637 memcpy(vpd, dmabuf->virt, *vpd_size);
4638
James Smartda0436e2009-05-22 14:51:39 -04004639 dma_free_coherent(&phba->pcidev->dev, dma_size,
4640 dmabuf->virt, dmabuf->phys);
4641 kfree(dmabuf);
4642 return 0;
4643}
4644
4645/**
4646 * lpfc_sli4_arm_cqeq_intr - Arm sli-4 device completion and event queues
4647 * @phba: pointer to lpfc hba data structure.
4648 *
4649 * This routine is called to explicitly arm the SLI4 device's completion and
4650 * event queues
4651 **/
4652static void
4653lpfc_sli4_arm_cqeq_intr(struct lpfc_hba *phba)
4654{
4655 uint8_t fcp_eqidx;
4656
4657 lpfc_sli4_cq_release(phba->sli4_hba.mbx_cq, LPFC_QUEUE_REARM);
4658 lpfc_sli4_cq_release(phba->sli4_hba.els_cq, LPFC_QUEUE_REARM);
James Smartda0436e2009-05-22 14:51:39 -04004659 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count; fcp_eqidx++)
4660 lpfc_sli4_cq_release(phba->sli4_hba.fcp_cq[fcp_eqidx],
4661 LPFC_QUEUE_REARM);
4662 lpfc_sli4_eq_release(phba->sli4_hba.sp_eq, LPFC_QUEUE_REARM);
4663 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count; fcp_eqidx++)
4664 lpfc_sli4_eq_release(phba->sli4_hba.fp_eq[fcp_eqidx],
4665 LPFC_QUEUE_REARM);
4666}
4667
4668/**
4669 * lpfc_sli4_hba_setup - SLI4 device intialization PCI function
4670 * @phba: Pointer to HBA context object.
4671 *
4672 * This function is the main SLI4 device intialization PCI function. This
4673 * function is called by the HBA intialization code, HBA reset code and
4674 * HBA error attention handler code. Caller is not required to hold any
4675 * locks.
4676 **/
4677int
4678lpfc_sli4_hba_setup(struct lpfc_hba *phba)
4679{
4680 int rc;
4681 LPFC_MBOXQ_t *mboxq;
4682 struct lpfc_mqe *mqe;
4683 uint8_t *vpd;
4684 uint32_t vpd_size;
4685 uint32_t ftr_rsp = 0;
4686 struct Scsi_Host *shost = lpfc_shost_from_vport(phba->pport);
4687 struct lpfc_vport *vport = phba->pport;
4688 struct lpfc_dmabuf *mp;
4689
James Smart2fcee4b2010-12-15 17:57:46 -05004690 /*
4691 * TODO: Why does this routine execute these task in a different
4692 * order from probe?
4693 */
James Smartda0436e2009-05-22 14:51:39 -04004694 /* Perform a PCI function reset to start from clean */
4695 rc = lpfc_pci_function_reset(phba);
4696 if (unlikely(rc))
4697 return -ENODEV;
4698
4699 /* Check the HBA Host Status Register for readyness */
4700 rc = lpfc_sli4_post_status_check(phba);
4701 if (unlikely(rc))
4702 return -ENODEV;
4703 else {
4704 spin_lock_irq(&phba->hbalock);
4705 phba->sli.sli_flag |= LPFC_SLI_ACTIVE;
4706 spin_unlock_irq(&phba->hbalock);
4707 }
4708
4709 /*
4710 * Allocate a single mailbox container for initializing the
4711 * port.
4712 */
4713 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4714 if (!mboxq)
4715 return -ENOMEM;
4716
4717 /*
4718 * Continue initialization with default values even if driver failed
4719 * to read FCoE param config regions
4720 */
4721 if (lpfc_sli4_read_fcoe_params(phba, mboxq))
4722 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
James Smarte4e74272009-07-19 10:01:38 -04004723 "2570 Failed to read FCoE parameters\n");
James Smartda0436e2009-05-22 14:51:39 -04004724
4725 /* Issue READ_REV to collect vpd and FW information. */
James Smart49198b32010-04-06 15:04:33 -04004726 vpd_size = SLI4_PAGE_SIZE;
James Smartda0436e2009-05-22 14:51:39 -04004727 vpd = kzalloc(vpd_size, GFP_KERNEL);
4728 if (!vpd) {
4729 rc = -ENOMEM;
4730 goto out_free_mbox;
4731 }
4732
4733 rc = lpfc_sli4_read_rev(phba, mboxq, vpd, &vpd_size);
James Smart76a95d72010-11-20 23:11:48 -05004734 if (unlikely(rc)) {
4735 kfree(vpd);
4736 goto out_free_mbox;
4737 }
James Smartda0436e2009-05-22 14:51:39 -04004738 mqe = &mboxq->u.mqe;
James Smartf1126682009-06-10 17:22:44 -04004739 phba->sli_rev = bf_get(lpfc_mbx_rd_rev_sli_lvl, &mqe->un.read_rev);
4740 if (bf_get(lpfc_mbx_rd_rev_fcoe, &mqe->un.read_rev))
James Smart76a95d72010-11-20 23:11:48 -05004741 phba->hba_flag |= HBA_FCOE_MODE;
4742 else
4743 phba->hba_flag &= ~HBA_FCOE_MODE;
James Smart45ed1192009-10-02 15:17:02 -04004744
4745 if (bf_get(lpfc_mbx_rd_rev_cee_ver, &mqe->un.read_rev) ==
4746 LPFC_DCBX_CEE_MODE)
4747 phba->hba_flag |= HBA_FIP_SUPPORT;
4748 else
4749 phba->hba_flag &= ~HBA_FIP_SUPPORT;
4750
James Smartf1126682009-06-10 17:22:44 -04004751 if (phba->sli_rev != LPFC_SLI_REV4 ||
James Smart76a95d72010-11-20 23:11:48 -05004752 !(phba->hba_flag & HBA_FCOE_MODE)) {
James Smartda0436e2009-05-22 14:51:39 -04004753 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4754 "0376 READ_REV Error. SLI Level %d "
4755 "FCoE enabled %d\n",
James Smart76a95d72010-11-20 23:11:48 -05004756 phba->sli_rev, phba->hba_flag & HBA_FCOE_MODE);
James Smartda0436e2009-05-22 14:51:39 -04004757 rc = -EIO;
James Smart76a95d72010-11-20 23:11:48 -05004758 kfree(vpd);
4759 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04004760 }
James Smartda0436e2009-05-22 14:51:39 -04004761 /*
4762 * Evaluate the read rev and vpd data. Populate the driver
4763 * state with the results. If this routine fails, the failure
4764 * is not fatal as the driver will use generic values.
4765 */
4766 rc = lpfc_parse_vpd(phba, vpd, vpd_size);
4767 if (unlikely(!rc)) {
4768 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4769 "0377 Error %d parsing vpd. "
4770 "Using defaults.\n", rc);
4771 rc = 0;
4772 }
James Smart76a95d72010-11-20 23:11:48 -05004773 kfree(vpd);
James Smartda0436e2009-05-22 14:51:39 -04004774
James Smartf1126682009-06-10 17:22:44 -04004775 /* Save information as VPD data */
4776 phba->vpd.rev.biuRev = mqe->un.read_rev.first_hw_rev;
4777 phba->vpd.rev.smRev = mqe->un.read_rev.second_hw_rev;
4778 phba->vpd.rev.endecRev = mqe->un.read_rev.third_hw_rev;
4779 phba->vpd.rev.fcphHigh = bf_get(lpfc_mbx_rd_rev_fcph_high,
4780 &mqe->un.read_rev);
4781 phba->vpd.rev.fcphLow = bf_get(lpfc_mbx_rd_rev_fcph_low,
4782 &mqe->un.read_rev);
4783 phba->vpd.rev.feaLevelHigh = bf_get(lpfc_mbx_rd_rev_ftr_lvl_high,
4784 &mqe->un.read_rev);
4785 phba->vpd.rev.feaLevelLow = bf_get(lpfc_mbx_rd_rev_ftr_lvl_low,
4786 &mqe->un.read_rev);
4787 phba->vpd.rev.sli1FwRev = mqe->un.read_rev.fw_id_rev;
4788 memcpy(phba->vpd.rev.sli1FwName, mqe->un.read_rev.fw_name, 16);
4789 phba->vpd.rev.sli2FwRev = mqe->un.read_rev.ulp_fw_id_rev;
4790 memcpy(phba->vpd.rev.sli2FwName, mqe->un.read_rev.ulp_fw_name, 16);
4791 phba->vpd.rev.opFwRev = mqe->un.read_rev.fw_id_rev;
4792 memcpy(phba->vpd.rev.opFwName, mqe->un.read_rev.fw_name, 16);
4793 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
4794 "(%d):0380 READ_REV Status x%x "
4795 "fw_rev:%s fcphHi:%x fcphLo:%x flHi:%x flLo:%x\n",
4796 mboxq->vport ? mboxq->vport->vpi : 0,
4797 bf_get(lpfc_mqe_status, mqe),
4798 phba->vpd.rev.opFwName,
4799 phba->vpd.rev.fcphHigh, phba->vpd.rev.fcphLow,
4800 phba->vpd.rev.feaLevelHigh, phba->vpd.rev.feaLevelLow);
James Smartda0436e2009-05-22 14:51:39 -04004801
4802 /*
4803 * Discover the port's supported feature set and match it against the
4804 * hosts requests.
4805 */
4806 lpfc_request_features(phba, mboxq);
4807 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4808 if (unlikely(rc)) {
4809 rc = -EIO;
James Smart76a95d72010-11-20 23:11:48 -05004810 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04004811 }
4812
4813 /*
4814 * The port must support FCP initiator mode as this is the
4815 * only mode running in the host.
4816 */
4817 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_fcpi, &mqe->un.req_ftrs))) {
4818 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
4819 "0378 No support for fcpi mode.\n");
4820 ftr_rsp++;
4821 }
James Smartfedd3b72011-02-16 12:39:24 -05004822 if (bf_get(lpfc_mbx_rq_ftr_rsp_perfh, &mqe->un.req_ftrs))
4823 phba->sli3_options |= LPFC_SLI4_PERFH_ENABLED;
4824 else
4825 phba->sli3_options &= ~LPFC_SLI4_PERFH_ENABLED;
James Smartda0436e2009-05-22 14:51:39 -04004826 /*
4827 * If the port cannot support the host's requested features
4828 * then turn off the global config parameters to disable the
4829 * feature in the driver. This is not a fatal error.
4830 */
4831 if ((phba->cfg_enable_bg) &&
4832 !(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs)))
4833 ftr_rsp++;
4834
4835 if (phba->max_vpi && phba->cfg_enable_npiv &&
4836 !(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
4837 ftr_rsp++;
4838
4839 if (ftr_rsp) {
4840 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
4841 "0379 Feature Mismatch Data: x%08x %08x "
4842 "x%x x%x x%x\n", mqe->un.req_ftrs.word2,
4843 mqe->un.req_ftrs.word3, phba->cfg_enable_bg,
4844 phba->cfg_enable_npiv, phba->max_vpi);
4845 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs)))
4846 phba->cfg_enable_bg = 0;
4847 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
4848 phba->cfg_enable_npiv = 0;
4849 }
4850
4851 /* These SLI3 features are assumed in SLI4 */
4852 spin_lock_irq(&phba->hbalock);
4853 phba->sli3_options |= (LPFC_SLI3_NPIV_ENABLED | LPFC_SLI3_HBQ_ENABLED);
4854 spin_unlock_irq(&phba->hbalock);
4855
4856 /* Read the port's service parameters. */
James Smart9f1177a2010-02-26 14:12:57 -05004857 rc = lpfc_read_sparam(phba, mboxq, vport->vpi);
4858 if (rc) {
4859 phba->link_state = LPFC_HBA_ERROR;
4860 rc = -ENOMEM;
James Smart76a95d72010-11-20 23:11:48 -05004861 goto out_free_mbox;
James Smart9f1177a2010-02-26 14:12:57 -05004862 }
4863
James Smartda0436e2009-05-22 14:51:39 -04004864 mboxq->vport = vport;
4865 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4866 mp = (struct lpfc_dmabuf *) mboxq->context1;
4867 if (rc == MBX_SUCCESS) {
4868 memcpy(&vport->fc_sparam, mp->virt, sizeof(struct serv_parm));
4869 rc = 0;
4870 }
4871
4872 /*
4873 * This memory was allocated by the lpfc_read_sparam routine. Release
4874 * it to the mbuf pool.
4875 */
4876 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4877 kfree(mp);
4878 mboxq->context1 = NULL;
4879 if (unlikely(rc)) {
4880 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4881 "0382 READ_SPARAM command failed "
4882 "status %d, mbxStatus x%x\n",
4883 rc, bf_get(lpfc_mqe_status, mqe));
4884 phba->link_state = LPFC_HBA_ERROR;
4885 rc = -EIO;
James Smart76a95d72010-11-20 23:11:48 -05004886 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04004887 }
4888
4889 if (phba->cfg_soft_wwnn)
4890 u64_to_wwn(phba->cfg_soft_wwnn,
4891 vport->fc_sparam.nodeName.u.wwn);
4892 if (phba->cfg_soft_wwpn)
4893 u64_to_wwn(phba->cfg_soft_wwpn,
4894 vport->fc_sparam.portName.u.wwn);
4895 memcpy(&vport->fc_nodename, &vport->fc_sparam.nodeName,
4896 sizeof(struct lpfc_name));
4897 memcpy(&vport->fc_portname, &vport->fc_sparam.portName,
4898 sizeof(struct lpfc_name));
4899
4900 /* Update the fc_host data structures with new wwn. */
4901 fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
4902 fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
4903
4904 /* Register SGL pool to the device using non-embedded mailbox command */
4905 rc = lpfc_sli4_post_sgl_list(phba);
4906 if (unlikely(rc)) {
4907 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smart6a9c52c2009-10-02 15:16:51 -04004908 "0582 Error %d during sgl post operation\n",
4909 rc);
James Smartda0436e2009-05-22 14:51:39 -04004910 rc = -ENODEV;
James Smart76a95d72010-11-20 23:11:48 -05004911 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04004912 }
4913
4914 /* Register SCSI SGL pool to the device */
4915 rc = lpfc_sli4_repost_scsi_sgl_list(phba);
4916 if (unlikely(rc)) {
4917 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
James Smart6a9c52c2009-10-02 15:16:51 -04004918 "0383 Error %d during scsi sgl post "
4919 "operation\n", rc);
James Smartda0436e2009-05-22 14:51:39 -04004920 /* Some Scsi buffers were moved to the abort scsi list */
4921 /* A pci function reset will repost them */
4922 rc = -ENODEV;
James Smart76a95d72010-11-20 23:11:48 -05004923 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04004924 }
4925
4926 /* Post the rpi header region to the device. */
4927 rc = lpfc_sli4_post_all_rpi_hdrs(phba);
4928 if (unlikely(rc)) {
4929 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4930 "0393 Error %d during rpi post operation\n",
4931 rc);
4932 rc = -ENODEV;
James Smart76a95d72010-11-20 23:11:48 -05004933 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04004934 }
James Smartda0436e2009-05-22 14:51:39 -04004935
4936 /* Set up all the queues to the device */
4937 rc = lpfc_sli4_queue_setup(phba);
4938 if (unlikely(rc)) {
4939 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4940 "0381 Error %d during queue setup.\n ", rc);
4941 goto out_stop_timers;
4942 }
4943
4944 /* Arm the CQs and then EQs on device */
4945 lpfc_sli4_arm_cqeq_intr(phba);
4946
4947 /* Indicate device interrupt mode */
4948 phba->sli4_hba.intr_enable = 1;
4949
4950 /* Allow asynchronous mailbox command to go through */
4951 spin_lock_irq(&phba->hbalock);
4952 phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
4953 spin_unlock_irq(&phba->hbalock);
4954
4955 /* Post receive buffers to the device */
4956 lpfc_sli4_rb_setup(phba);
4957
James Smartfc2b9892010-02-26 14:15:29 -05004958 /* Reset HBA FCF states after HBA reset */
4959 phba->fcf.fcf_flag = 0;
4960 phba->fcf.current_rec.flag = 0;
4961
James Smartda0436e2009-05-22 14:51:39 -04004962 /* Start the ELS watchdog timer */
James Smart8fa38512009-07-19 10:01:03 -04004963 mod_timer(&vport->els_tmofunc,
4964 jiffies + HZ * (phba->fc_ratov * 2));
James Smartda0436e2009-05-22 14:51:39 -04004965
4966 /* Start heart beat timer */
4967 mod_timer(&phba->hb_tmofunc,
4968 jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
4969 phba->hb_outstanding = 0;
4970 phba->last_completion_time = jiffies;
4971
4972 /* Start error attention (ERATT) polling timer */
4973 mod_timer(&phba->eratt_poll, jiffies + HZ * LPFC_ERATT_POLL_INTERVAL);
4974
James Smart75baf692010-06-08 18:31:21 -04004975 /* Enable PCIe device Advanced Error Reporting (AER) if configured */
4976 if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
4977 rc = pci_enable_pcie_error_reporting(phba->pcidev);
4978 if (!rc) {
4979 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4980 "2829 This device supports "
4981 "Advanced Error Reporting (AER)\n");
4982 spin_lock_irq(&phba->hbalock);
4983 phba->hba_flag |= HBA_AER_ENABLED;
4984 spin_unlock_irq(&phba->hbalock);
4985 } else {
4986 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4987 "2830 This device does not support "
4988 "Advanced Error Reporting (AER)\n");
4989 phba->cfg_aer_support = 0;
4990 }
4991 }
4992
James Smart76a95d72010-11-20 23:11:48 -05004993 if (!(phba->hba_flag & HBA_FCOE_MODE)) {
4994 /*
4995 * The FC Port needs to register FCFI (index 0)
4996 */
4997 lpfc_reg_fcfi(phba, mboxq);
4998 mboxq->vport = phba->pport;
4999 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5000 if (rc == MBX_SUCCESS)
5001 rc = 0;
5002 else
5003 goto out_unset_queue;
5004 }
James Smartda0436e2009-05-22 14:51:39 -04005005 /*
5006 * The port is ready, set the host's link state to LINK_DOWN
5007 * in preparation for link interrupts.
5008 */
James Smartda0436e2009-05-22 14:51:39 -04005009 spin_lock_irq(&phba->hbalock);
5010 phba->link_state = LPFC_LINK_DOWN;
5011 spin_unlock_irq(&phba->hbalock);
James Smartfedd3b72011-02-16 12:39:24 -05005012 if (phba->cfg_suppress_link_up == LPFC_INITIALIZE_LINK)
5013 rc = phba->lpfc_hba_init_link(phba, MBX_NOWAIT);
James Smart76a95d72010-11-20 23:11:48 -05005014out_unset_queue:
James Smartda0436e2009-05-22 14:51:39 -04005015 /* Unset all the queues set up in this routine when error out */
5016 if (rc)
5017 lpfc_sli4_queue_unset(phba);
James Smartda0436e2009-05-22 14:51:39 -04005018out_stop_timers:
5019 if (rc)
5020 lpfc_stop_hba_timers(phba);
James Smartda0436e2009-05-22 14:51:39 -04005021out_free_mbox:
5022 mempool_free(mboxq, phba->mbox_mem_pool);
5023 return rc;
5024}
James Smarte59058c2008-08-24 21:49:00 -04005025
5026/**
James Smart3621a712009-04-06 18:47:14 -04005027 * lpfc_mbox_timeout - Timeout call back function for mbox timer
James Smarte59058c2008-08-24 21:49:00 -04005028 * @ptr: context object - pointer to hba structure.
dea31012005-04-17 16:05:31 -05005029 *
James Smarte59058c2008-08-24 21:49:00 -04005030 * This is the callback function for mailbox timer. The mailbox
5031 * timer is armed when a new mailbox command is issued and the timer
5032 * is deleted when the mailbox complete. The function is called by
5033 * the kernel timer code when a mailbox does not complete within
5034 * expected time. This function wakes up the worker thread to
5035 * process the mailbox timeout and returns. All the processing is
5036 * done by the worker thread function lpfc_mbox_timeout_handler.
5037 **/
dea31012005-04-17 16:05:31 -05005038void
5039lpfc_mbox_timeout(unsigned long ptr)
5040{
James Smart92d7f7b2007-06-17 19:56:38 -05005041 struct lpfc_hba *phba = (struct lpfc_hba *) ptr;
dea31012005-04-17 16:05:31 -05005042 unsigned long iflag;
James Smart2e0fef82007-06-17 19:56:36 -05005043 uint32_t tmo_posted;
dea31012005-04-17 16:05:31 -05005044
James Smart2e0fef82007-06-17 19:56:36 -05005045 spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
James Smart92d7f7b2007-06-17 19:56:38 -05005046 tmo_posted = phba->pport->work_port_events & WORKER_MBOX_TMO;
James Smart2e0fef82007-06-17 19:56:36 -05005047 if (!tmo_posted)
5048 phba->pport->work_port_events |= WORKER_MBOX_TMO;
5049 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
5050
James Smart5e9d9b82008-06-14 22:52:53 -04005051 if (!tmo_posted)
5052 lpfc_worker_wake_up(phba);
5053 return;
dea31012005-04-17 16:05:31 -05005054}
5055
James Smarte59058c2008-08-24 21:49:00 -04005056
5057/**
James Smart3621a712009-04-06 18:47:14 -04005058 * lpfc_mbox_timeout_handler - Worker thread function to handle mailbox timeout
James Smarte59058c2008-08-24 21:49:00 -04005059 * @phba: Pointer to HBA context object.
5060 *
5061 * This function is called from worker thread when a mailbox command times out.
5062 * The caller is not required to hold any locks. This function will reset the
5063 * HBA and recover all the pending commands.
5064 **/
dea31012005-04-17 16:05:31 -05005065void
5066lpfc_mbox_timeout_handler(struct lpfc_hba *phba)
5067{
James Smart2e0fef82007-06-17 19:56:36 -05005068 LPFC_MBOXQ_t *pmbox = phba->sli.mbox_active;
James Smart04c68492009-05-22 14:52:52 -04005069 MAILBOX_t *mb = &pmbox->u.mb;
James Smart1dcb58e2007-04-25 09:51:30 -04005070 struct lpfc_sli *psli = &phba->sli;
5071 struct lpfc_sli_ring *pring;
dea31012005-04-17 16:05:31 -05005072
James Smarta257bf92009-04-06 18:48:10 -04005073 /* Check the pmbox pointer first. There is a race condition
5074 * between the mbox timeout handler getting executed in the
5075 * worklist and the mailbox actually completing. When this
5076 * race condition occurs, the mbox_active will be NULL.
5077 */
5078 spin_lock_irq(&phba->hbalock);
5079 if (pmbox == NULL) {
5080 lpfc_printf_log(phba, KERN_WARNING,
5081 LOG_MBOX | LOG_SLI,
5082 "0353 Active Mailbox cleared - mailbox timeout "
5083 "exiting\n");
5084 spin_unlock_irq(&phba->hbalock);
5085 return;
5086 }
5087
dea31012005-04-17 16:05:31 -05005088 /* Mbox cmd <mbxCommand> timeout */
James Smarted957682007-06-17 19:56:37 -05005089 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04005090 "0310 Mailbox command x%x timeout Data: x%x x%x x%p\n",
James Smart92d7f7b2007-06-17 19:56:38 -05005091 mb->mbxCommand,
5092 phba->pport->port_state,
5093 phba->sli.sli_flag,
5094 phba->sli.mbox_active);
James Smarta257bf92009-04-06 18:48:10 -04005095 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05005096
James Smart1dcb58e2007-04-25 09:51:30 -04005097 /* Setting state unknown so lpfc_sli_abort_iocb_ring
5098 * would get IOCB_ERROR from lpfc_sli_issue_iocb, allowing
5099 * it to fail all oustanding SCSI IO.
5100 */
James Smart2e0fef82007-06-17 19:56:36 -05005101 spin_lock_irq(&phba->pport->work_port_lock);
5102 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
5103 spin_unlock_irq(&phba->pport->work_port_lock);
5104 spin_lock_irq(&phba->hbalock);
5105 phba->link_state = LPFC_LINK_UNKNOWN;
James Smartf4b4c682009-05-22 14:53:12 -04005106 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05005107 spin_unlock_irq(&phba->hbalock);
James Smart1dcb58e2007-04-25 09:51:30 -04005108
5109 pring = &psli->ring[psli->fcp_ring];
5110 lpfc_sli_abort_iocb_ring(phba, pring);
5111
5112 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smart76bb24e2007-10-27 13:38:00 -04005113 "0345 Resetting board due to mailbox timeout\n");
James Smart3772a992009-05-22 14:50:54 -04005114
5115 /* Reset the HBA device */
5116 lpfc_reset_hba(phba);
dea31012005-04-17 16:05:31 -05005117}
5118
James Smarte59058c2008-08-24 21:49:00 -04005119/**
James Smart3772a992009-05-22 14:50:54 -04005120 * lpfc_sli_issue_mbox_s3 - Issue an SLI3 mailbox command to firmware
James Smarte59058c2008-08-24 21:49:00 -04005121 * @phba: Pointer to HBA context object.
5122 * @pmbox: Pointer to mailbox object.
5123 * @flag: Flag indicating how the mailbox need to be processed.
5124 *
5125 * This function is called by discovery code and HBA management code
James Smart3772a992009-05-22 14:50:54 -04005126 * to submit a mailbox command to firmware with SLI-3 interface spec. This
5127 * function gets the hbalock to protect the data structures.
James Smarte59058c2008-08-24 21:49:00 -04005128 * The mailbox command can be submitted in polling mode, in which case
5129 * this function will wait in a polling loop for the completion of the
5130 * mailbox.
5131 * If the mailbox is submitted in no_wait mode (not polling) the
5132 * function will submit the command and returns immediately without waiting
5133 * for the mailbox completion. The no_wait is supported only when HBA
5134 * is in SLI2/SLI3 mode - interrupts are enabled.
5135 * The SLI interface allows only one mailbox pending at a time. If the
5136 * mailbox is issued in polling mode and there is already a mailbox
5137 * pending, then the function will return an error. If the mailbox is issued
5138 * in NO_WAIT mode and there is a mailbox pending already, the function
5139 * will return MBX_BUSY after queuing the mailbox into mailbox queue.
5140 * The sli layer owns the mailbox object until the completion of mailbox
5141 * command if this function return MBX_BUSY or MBX_SUCCESS. For all other
5142 * return codes the caller owns the mailbox command after the return of
5143 * the function.
5144 **/
James Smart3772a992009-05-22 14:50:54 -04005145static int
5146lpfc_sli_issue_mbox_s3(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox,
5147 uint32_t flag)
dea31012005-04-17 16:05:31 -05005148{
dea31012005-04-17 16:05:31 -05005149 MAILBOX_t *mb;
James Smart2e0fef82007-06-17 19:56:36 -05005150 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05005151 uint32_t status, evtctr;
5152 uint32_t ha_copy;
5153 int i;
James Smart09372822008-01-11 01:52:54 -05005154 unsigned long timeout;
dea31012005-04-17 16:05:31 -05005155 unsigned long drvr_flag = 0;
James Smart34b02dc2008-08-24 21:49:55 -04005156 uint32_t word0, ldata;
dea31012005-04-17 16:05:31 -05005157 void __iomem *to_slim;
James Smart58da1ff2008-04-07 10:15:56 -04005158 int processing_queue = 0;
5159
5160 spin_lock_irqsave(&phba->hbalock, drvr_flag);
5161 if (!pmbox) {
James Smart8568a4d2009-07-19 10:01:16 -04005162 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart58da1ff2008-04-07 10:15:56 -04005163 /* processing mbox queue from intr_handler */
James Smart3772a992009-05-22 14:50:54 -04005164 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
5165 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
5166 return MBX_SUCCESS;
5167 }
James Smart58da1ff2008-04-07 10:15:56 -04005168 processing_queue = 1;
James Smart58da1ff2008-04-07 10:15:56 -04005169 pmbox = lpfc_mbox_get(phba);
5170 if (!pmbox) {
5171 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
5172 return MBX_SUCCESS;
5173 }
5174 }
dea31012005-04-17 16:05:31 -05005175
James Smarted957682007-06-17 19:56:37 -05005176 if (pmbox->mbox_cmpl && pmbox->mbox_cmpl != lpfc_sli_def_mbox_cmpl &&
James Smart92d7f7b2007-06-17 19:56:38 -05005177 pmbox->mbox_cmpl != lpfc_sli_wake_mbox_wait) {
James Smarted957682007-06-17 19:56:37 -05005178 if(!pmbox->vport) {
James Smart58da1ff2008-04-07 10:15:56 -04005179 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
James Smarted957682007-06-17 19:56:37 -05005180 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -05005181 LOG_MBOX | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04005182 "1806 Mbox x%x failed. No vport\n",
James Smart3772a992009-05-22 14:50:54 -04005183 pmbox->u.mb.mbxCommand);
James Smarted957682007-06-17 19:56:37 -05005184 dump_stack();
James Smart58da1ff2008-04-07 10:15:56 -04005185 goto out_not_finished;
James Smarted957682007-06-17 19:56:37 -05005186 }
5187 }
5188
Linas Vepstas8d63f372007-02-14 14:28:36 -06005189 /* If the PCI channel is in offline state, do not post mbox. */
James Smart58da1ff2008-04-07 10:15:56 -04005190 if (unlikely(pci_channel_offline(phba->pcidev))) {
5191 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
5192 goto out_not_finished;
5193 }
Linas Vepstas8d63f372007-02-14 14:28:36 -06005194
James Smarta257bf92009-04-06 18:48:10 -04005195 /* If HBA has a deferred error attention, fail the iocb. */
5196 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
5197 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
5198 goto out_not_finished;
5199 }
5200
dea31012005-04-17 16:05:31 -05005201 psli = &phba->sli;
James Smart92d7f7b2007-06-17 19:56:38 -05005202
James Smart3772a992009-05-22 14:50:54 -04005203 mb = &pmbox->u.mb;
dea31012005-04-17 16:05:31 -05005204 status = MBX_SUCCESS;
5205
James Smart2e0fef82007-06-17 19:56:36 -05005206 if (phba->link_state == LPFC_HBA_ERROR) {
5207 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05005208
5209 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04005210 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5211 "(%d):0311 Mailbox command x%x cannot "
5212 "issue Data: x%x x%x\n",
5213 pmbox->vport ? pmbox->vport->vpi : 0,
5214 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04005215 goto out_not_finished;
Jamie Wellnitz41415862006-02-28 19:25:27 -05005216 }
5217
James Smart92908312006-03-07 15:04:13 -05005218 if (mb->mbxCommand != MBX_KILL_BOARD && flag & MBX_NOWAIT &&
5219 !(readl(phba->HCregaddr) & HC_MBINT_ENA)) {
James Smart2e0fef82007-06-17 19:56:36 -05005220 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
James Smart3772a992009-05-22 14:50:54 -04005221 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5222 "(%d):2528 Mailbox command x%x cannot "
5223 "issue Data: x%x x%x\n",
5224 pmbox->vport ? pmbox->vport->vpi : 0,
5225 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04005226 goto out_not_finished;
James Smart92908312006-03-07 15:04:13 -05005227 }
5228
dea31012005-04-17 16:05:31 -05005229 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
5230 /* Polling for a mbox command when another one is already active
5231 * is not allowed in SLI. Also, the driver must have established
5232 * SLI2 mode to queue and process multiple mbox commands.
5233 */
5234
5235 if (flag & MBX_POLL) {
James Smart2e0fef82007-06-17 19:56:36 -05005236 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05005237
5238 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04005239 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5240 "(%d):2529 Mailbox command x%x "
5241 "cannot issue Data: x%x x%x\n",
5242 pmbox->vport ? pmbox->vport->vpi : 0,
5243 pmbox->u.mb.mbxCommand,
5244 psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04005245 goto out_not_finished;
dea31012005-04-17 16:05:31 -05005246 }
5247
James Smart3772a992009-05-22 14:50:54 -04005248 if (!(psli->sli_flag & LPFC_SLI_ACTIVE)) {
James Smart2e0fef82007-06-17 19:56:36 -05005249 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05005250 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04005251 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5252 "(%d):2530 Mailbox command x%x "
5253 "cannot issue Data: x%x x%x\n",
5254 pmbox->vport ? pmbox->vport->vpi : 0,
5255 pmbox->u.mb.mbxCommand,
5256 psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04005257 goto out_not_finished;
dea31012005-04-17 16:05:31 -05005258 }
5259
dea31012005-04-17 16:05:31 -05005260 /* Another mailbox command is still being processed, queue this
5261 * command to be processed later.
5262 */
5263 lpfc_mbox_put(phba, pmbox);
5264
5265 /* Mbox cmd issue - BUSY */
James Smarted957682007-06-17 19:56:37 -05005266 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04005267 "(%d):0308 Mbox cmd issue - BUSY Data: "
James Smart92d7f7b2007-06-17 19:56:38 -05005268 "x%x x%x x%x x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -05005269 pmbox->vport ? pmbox->vport->vpi : 0xffffff,
5270 mb->mbxCommand, phba->pport->port_state,
5271 psli->sli_flag, flag);
dea31012005-04-17 16:05:31 -05005272
5273 psli->slistat.mbox_busy++;
James Smart2e0fef82007-06-17 19:56:36 -05005274 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05005275
James Smart858c9f62007-06-17 19:56:39 -05005276 if (pmbox->vport) {
5277 lpfc_debugfs_disc_trc(pmbox->vport,
5278 LPFC_DISC_TRC_MBOX_VPORT,
5279 "MBOX Bsy vport: cmd:x%x mb:x%x x%x",
5280 (uint32_t)mb->mbxCommand,
5281 mb->un.varWords[0], mb->un.varWords[1]);
5282 }
5283 else {
5284 lpfc_debugfs_disc_trc(phba->pport,
5285 LPFC_DISC_TRC_MBOX,
5286 "MBOX Bsy: cmd:x%x mb:x%x x%x",
5287 (uint32_t)mb->mbxCommand,
5288 mb->un.varWords[0], mb->un.varWords[1]);
5289 }
5290
James Smart2e0fef82007-06-17 19:56:36 -05005291 return MBX_BUSY;
dea31012005-04-17 16:05:31 -05005292 }
5293
dea31012005-04-17 16:05:31 -05005294 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
5295
5296 /* If we are not polling, we MUST be in SLI2 mode */
5297 if (flag != MBX_POLL) {
James Smart3772a992009-05-22 14:50:54 -04005298 if (!(psli->sli_flag & LPFC_SLI_ACTIVE) &&
Jamie Wellnitz41415862006-02-28 19:25:27 -05005299 (mb->mbxCommand != MBX_KILL_BOARD)) {
dea31012005-04-17 16:05:31 -05005300 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05005301 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05005302 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04005303 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5304 "(%d):2531 Mailbox command x%x "
5305 "cannot issue Data: x%x x%x\n",
5306 pmbox->vport ? pmbox->vport->vpi : 0,
5307 pmbox->u.mb.mbxCommand,
5308 psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04005309 goto out_not_finished;
dea31012005-04-17 16:05:31 -05005310 }
5311 /* timeout active mbox command */
James Smarta309a6b2006-08-01 07:33:43 -04005312 mod_timer(&psli->mbox_tmo, (jiffies +
5313 (HZ * lpfc_mbox_tmo_val(phba, mb->mbxCommand))));
dea31012005-04-17 16:05:31 -05005314 }
5315
5316 /* Mailbox cmd <cmd> issue */
James Smarted957682007-06-17 19:56:37 -05005317 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04005318 "(%d):0309 Mailbox cmd x%x issue Data: x%x x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05005319 "x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04005320 pmbox->vport ? pmbox->vport->vpi : 0,
James Smart92d7f7b2007-06-17 19:56:38 -05005321 mb->mbxCommand, phba->pport->port_state,
5322 psli->sli_flag, flag);
dea31012005-04-17 16:05:31 -05005323
James Smart858c9f62007-06-17 19:56:39 -05005324 if (mb->mbxCommand != MBX_HEARTBEAT) {
5325 if (pmbox->vport) {
5326 lpfc_debugfs_disc_trc(pmbox->vport,
5327 LPFC_DISC_TRC_MBOX_VPORT,
5328 "MBOX Send vport: cmd:x%x mb:x%x x%x",
5329 (uint32_t)mb->mbxCommand,
5330 mb->un.varWords[0], mb->un.varWords[1]);
5331 }
5332 else {
5333 lpfc_debugfs_disc_trc(phba->pport,
5334 LPFC_DISC_TRC_MBOX,
5335 "MBOX Send: cmd:x%x mb:x%x x%x",
5336 (uint32_t)mb->mbxCommand,
5337 mb->un.varWords[0], mb->un.varWords[1]);
5338 }
5339 }
5340
dea31012005-04-17 16:05:31 -05005341 psli->slistat.mbox_cmd++;
5342 evtctr = psli->slistat.mbox_event;
5343
5344 /* next set own bit for the adapter and copy over command word */
5345 mb->mbxOwner = OWN_CHIP;
5346
James Smart3772a992009-05-22 14:50:54 -04005347 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
James Smart7a470272010-03-15 11:25:20 -04005348 /* Populate mbox extension offset word. */
5349 if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len) {
5350 *(((uint32_t *)mb) + pmbox->mbox_offset_word)
5351 = (uint8_t *)phba->mbox_ext
5352 - (uint8_t *)phba->mbox;
5353 }
5354
5355 /* Copy the mailbox extension data */
5356 if (pmbox->in_ext_byte_len && pmbox->context2) {
5357 lpfc_sli_pcimem_bcopy(pmbox->context2,
5358 (uint8_t *)phba->mbox_ext,
5359 pmbox->in_ext_byte_len);
5360 }
5361 /* Copy command data to host SLIM area */
James Smart34b02dc2008-08-24 21:49:55 -04005362 lpfc_sli_pcimem_bcopy(mb, phba->mbox, MAILBOX_CMD_SIZE);
dea31012005-04-17 16:05:31 -05005363 } else {
James Smart7a470272010-03-15 11:25:20 -04005364 /* Populate mbox extension offset word. */
5365 if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len)
5366 *(((uint32_t *)mb) + pmbox->mbox_offset_word)
5367 = MAILBOX_HBA_EXT_OFFSET;
5368
5369 /* Copy the mailbox extension data */
5370 if (pmbox->in_ext_byte_len && pmbox->context2) {
5371 lpfc_memcpy_to_slim(phba->MBslimaddr +
5372 MAILBOX_HBA_EXT_OFFSET,
5373 pmbox->context2, pmbox->in_ext_byte_len);
5374
5375 }
James Smart92908312006-03-07 15:04:13 -05005376 if (mb->mbxCommand == MBX_CONFIG_PORT) {
dea31012005-04-17 16:05:31 -05005377 /* copy command data into host mbox for cmpl */
James Smart34b02dc2008-08-24 21:49:55 -04005378 lpfc_sli_pcimem_bcopy(mb, phba->mbox, MAILBOX_CMD_SIZE);
dea31012005-04-17 16:05:31 -05005379 }
5380
5381 /* First copy mbox command data to HBA SLIM, skip past first
5382 word */
5383 to_slim = phba->MBslimaddr + sizeof (uint32_t);
5384 lpfc_memcpy_to_slim(to_slim, &mb->un.varWords[0],
5385 MAILBOX_CMD_SIZE - sizeof (uint32_t));
5386
5387 /* Next copy over first word, with mbxOwner set */
James Smart34b02dc2008-08-24 21:49:55 -04005388 ldata = *((uint32_t *)mb);
dea31012005-04-17 16:05:31 -05005389 to_slim = phba->MBslimaddr;
5390 writel(ldata, to_slim);
5391 readl(to_slim); /* flush */
5392
5393 if (mb->mbxCommand == MBX_CONFIG_PORT) {
5394 /* switch over to host mailbox */
James Smart3772a992009-05-22 14:50:54 -04005395 psli->sli_flag |= LPFC_SLI_ACTIVE;
dea31012005-04-17 16:05:31 -05005396 }
5397 }
5398
5399 wmb();
dea31012005-04-17 16:05:31 -05005400
5401 switch (flag) {
5402 case MBX_NOWAIT:
James Smart09372822008-01-11 01:52:54 -05005403 /* Set up reference to mailbox command */
dea31012005-04-17 16:05:31 -05005404 psli->mbox_active = pmbox;
James Smart09372822008-01-11 01:52:54 -05005405 /* Interrupt board to do it */
5406 writel(CA_MBATT, phba->CAregaddr);
5407 readl(phba->CAregaddr); /* flush */
5408 /* Don't wait for it to finish, just return */
dea31012005-04-17 16:05:31 -05005409 break;
5410
5411 case MBX_POLL:
James Smart09372822008-01-11 01:52:54 -05005412 /* Set up null reference to mailbox command */
dea31012005-04-17 16:05:31 -05005413 psli->mbox_active = NULL;
James Smart09372822008-01-11 01:52:54 -05005414 /* Interrupt board to do it */
5415 writel(CA_MBATT, phba->CAregaddr);
5416 readl(phba->CAregaddr); /* flush */
5417
James Smart3772a992009-05-22 14:50:54 -04005418 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05005419 /* First read mbox status word */
James Smart34b02dc2008-08-24 21:49:55 -04005420 word0 = *((uint32_t *)phba->mbox);
dea31012005-04-17 16:05:31 -05005421 word0 = le32_to_cpu(word0);
5422 } else {
5423 /* First read mbox status word */
5424 word0 = readl(phba->MBslimaddr);
5425 }
5426
5427 /* Read the HBA Host Attention Register */
5428 ha_copy = readl(phba->HAregaddr);
James Smart09372822008-01-11 01:52:54 -05005429 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
5430 mb->mbxCommand) *
5431 1000) + jiffies;
5432 i = 0;
dea31012005-04-17 16:05:31 -05005433 /* Wait for command to complete */
Jamie Wellnitz41415862006-02-28 19:25:27 -05005434 while (((word0 & OWN_CHIP) == OWN_CHIP) ||
5435 (!(ha_copy & HA_MBATT) &&
James Smart2e0fef82007-06-17 19:56:36 -05005436 (phba->link_state > LPFC_WARM_START))) {
James Smart09372822008-01-11 01:52:54 -05005437 if (time_after(jiffies, timeout)) {
dea31012005-04-17 16:05:31 -05005438 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05005439 spin_unlock_irqrestore(&phba->hbalock,
dea31012005-04-17 16:05:31 -05005440 drvr_flag);
James Smart58da1ff2008-04-07 10:15:56 -04005441 goto out_not_finished;
dea31012005-04-17 16:05:31 -05005442 }
5443
5444 /* Check if we took a mbox interrupt while we were
5445 polling */
5446 if (((word0 & OWN_CHIP) != OWN_CHIP)
5447 && (evtctr != psli->slistat.mbox_event))
5448 break;
5449
James Smart09372822008-01-11 01:52:54 -05005450 if (i++ > 10) {
5451 spin_unlock_irqrestore(&phba->hbalock,
5452 drvr_flag);
5453 msleep(1);
5454 spin_lock_irqsave(&phba->hbalock, drvr_flag);
5455 }
dea31012005-04-17 16:05:31 -05005456
James Smart3772a992009-05-22 14:50:54 -04005457 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05005458 /* First copy command data */
James Smart34b02dc2008-08-24 21:49:55 -04005459 word0 = *((uint32_t *)phba->mbox);
dea31012005-04-17 16:05:31 -05005460 word0 = le32_to_cpu(word0);
5461 if (mb->mbxCommand == MBX_CONFIG_PORT) {
5462 MAILBOX_t *slimmb;
James Smart34b02dc2008-08-24 21:49:55 -04005463 uint32_t slimword0;
dea31012005-04-17 16:05:31 -05005464 /* Check real SLIM for any errors */
5465 slimword0 = readl(phba->MBslimaddr);
5466 slimmb = (MAILBOX_t *) & slimword0;
5467 if (((slimword0 & OWN_CHIP) != OWN_CHIP)
5468 && slimmb->mbxStatus) {
5469 psli->sli_flag &=
James Smart3772a992009-05-22 14:50:54 -04005470 ~LPFC_SLI_ACTIVE;
dea31012005-04-17 16:05:31 -05005471 word0 = slimword0;
5472 }
5473 }
5474 } else {
5475 /* First copy command data */
5476 word0 = readl(phba->MBslimaddr);
5477 }
5478 /* Read the HBA Host Attention Register */
5479 ha_copy = readl(phba->HAregaddr);
5480 }
5481
James Smart3772a992009-05-22 14:50:54 -04005482 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05005483 /* copy results back to user */
James Smart34b02dc2008-08-24 21:49:55 -04005484 lpfc_sli_pcimem_bcopy(phba->mbox, mb, MAILBOX_CMD_SIZE);
James Smart7a470272010-03-15 11:25:20 -04005485 /* Copy the mailbox extension data */
5486 if (pmbox->out_ext_byte_len && pmbox->context2) {
5487 lpfc_sli_pcimem_bcopy(phba->mbox_ext,
5488 pmbox->context2,
5489 pmbox->out_ext_byte_len);
5490 }
dea31012005-04-17 16:05:31 -05005491 } else {
5492 /* First copy command data */
5493 lpfc_memcpy_from_slim(mb, phba->MBslimaddr,
5494 MAILBOX_CMD_SIZE);
James Smart7a470272010-03-15 11:25:20 -04005495 /* Copy the mailbox extension data */
5496 if (pmbox->out_ext_byte_len && pmbox->context2) {
5497 lpfc_memcpy_from_slim(pmbox->context2,
5498 phba->MBslimaddr +
5499 MAILBOX_HBA_EXT_OFFSET,
5500 pmbox->out_ext_byte_len);
dea31012005-04-17 16:05:31 -05005501 }
5502 }
5503
5504 writel(HA_MBATT, phba->HAregaddr);
5505 readl(phba->HAregaddr); /* flush */
5506
5507 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
5508 status = mb->mbxStatus;
5509 }
5510
James Smart2e0fef82007-06-17 19:56:36 -05005511 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
5512 return status;
James Smart58da1ff2008-04-07 10:15:56 -04005513
5514out_not_finished:
5515 if (processing_queue) {
James Smartda0436e2009-05-22 14:51:39 -04005516 pmbox->u.mb.mbxStatus = MBX_NOT_FINISHED;
James Smart58da1ff2008-04-07 10:15:56 -04005517 lpfc_mbox_cmpl_put(phba, pmbox);
5518 }
5519 return MBX_NOT_FINISHED;
dea31012005-04-17 16:05:31 -05005520}
5521
James Smarte59058c2008-08-24 21:49:00 -04005522/**
James Smartf1126682009-06-10 17:22:44 -04005523 * lpfc_sli4_async_mbox_block - Block posting SLI4 asynchronous mailbox command
5524 * @phba: Pointer to HBA context object.
5525 *
5526 * The function blocks the posting of SLI4 asynchronous mailbox commands from
5527 * the driver internal pending mailbox queue. It will then try to wait out the
5528 * possible outstanding mailbox command before return.
5529 *
5530 * Returns:
5531 * 0 - the outstanding mailbox command completed; otherwise, the wait for
5532 * the outstanding mailbox command timed out.
5533 **/
5534static int
5535lpfc_sli4_async_mbox_block(struct lpfc_hba *phba)
5536{
5537 struct lpfc_sli *psli = &phba->sli;
5538 uint8_t actcmd = MBX_HEARTBEAT;
5539 int rc = 0;
5540 unsigned long timeout;
5541
5542 /* Mark the asynchronous mailbox command posting as blocked */
5543 spin_lock_irq(&phba->hbalock);
5544 psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
5545 if (phba->sli.mbox_active)
5546 actcmd = phba->sli.mbox_active->u.mb.mbxCommand;
5547 spin_unlock_irq(&phba->hbalock);
5548 /* Determine how long we might wait for the active mailbox
5549 * command to be gracefully completed by firmware.
5550 */
5551 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, actcmd) * 1000) +
5552 jiffies;
5553 /* Wait for the outstnading mailbox command to complete */
5554 while (phba->sli.mbox_active) {
5555 /* Check active mailbox complete status every 2ms */
5556 msleep(2);
5557 if (time_after(jiffies, timeout)) {
5558 /* Timeout, marked the outstanding cmd not complete */
5559 rc = 1;
5560 break;
5561 }
5562 }
5563
5564 /* Can not cleanly block async mailbox command, fails it */
5565 if (rc) {
5566 spin_lock_irq(&phba->hbalock);
5567 psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
5568 spin_unlock_irq(&phba->hbalock);
5569 }
5570 return rc;
5571}
5572
5573/**
5574 * lpfc_sli4_async_mbox_unblock - Block posting SLI4 async mailbox command
5575 * @phba: Pointer to HBA context object.
5576 *
5577 * The function unblocks and resume posting of SLI4 asynchronous mailbox
5578 * commands from the driver internal pending mailbox queue. It makes sure
5579 * that there is no outstanding mailbox command before resuming posting
5580 * asynchronous mailbox commands. If, for any reason, there is outstanding
5581 * mailbox command, it will try to wait it out before resuming asynchronous
5582 * mailbox command posting.
5583 **/
5584static void
5585lpfc_sli4_async_mbox_unblock(struct lpfc_hba *phba)
5586{
5587 struct lpfc_sli *psli = &phba->sli;
5588
5589 spin_lock_irq(&phba->hbalock);
5590 if (!(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
5591 /* Asynchronous mailbox posting is not blocked, do nothing */
5592 spin_unlock_irq(&phba->hbalock);
5593 return;
5594 }
5595
5596 /* Outstanding synchronous mailbox command is guaranteed to be done,
5597 * successful or timeout, after timing-out the outstanding mailbox
5598 * command shall always be removed, so just unblock posting async
5599 * mailbox command and resume
5600 */
5601 psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
5602 spin_unlock_irq(&phba->hbalock);
5603
5604 /* wake up worker thread to post asynchronlous mailbox command */
5605 lpfc_worker_wake_up(phba);
5606}
5607
5608/**
James Smartda0436e2009-05-22 14:51:39 -04005609 * lpfc_sli4_post_sync_mbox - Post an SLI4 mailbox to the bootstrap mailbox
5610 * @phba: Pointer to HBA context object.
5611 * @mboxq: Pointer to mailbox object.
5612 *
5613 * The function posts a mailbox to the port. The mailbox is expected
5614 * to be comletely filled in and ready for the port to operate on it.
5615 * This routine executes a synchronous completion operation on the
5616 * mailbox by polling for its completion.
5617 *
5618 * The caller must not be holding any locks when calling this routine.
5619 *
5620 * Returns:
5621 * MBX_SUCCESS - mailbox posted successfully
5622 * Any of the MBX error values.
5623 **/
5624static int
5625lpfc_sli4_post_sync_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
5626{
5627 int rc = MBX_SUCCESS;
5628 unsigned long iflag;
5629 uint32_t db_ready;
5630 uint32_t mcqe_status;
5631 uint32_t mbx_cmnd;
5632 unsigned long timeout;
5633 struct lpfc_sli *psli = &phba->sli;
5634 struct lpfc_mqe *mb = &mboxq->u.mqe;
5635 struct lpfc_bmbx_create *mbox_rgn;
5636 struct dma_address *dma_address;
5637 struct lpfc_register bmbx_reg;
5638
5639 /*
5640 * Only one mailbox can be active to the bootstrap mailbox region
5641 * at a time and there is no queueing provided.
5642 */
5643 spin_lock_irqsave(&phba->hbalock, iflag);
5644 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
5645 spin_unlock_irqrestore(&phba->hbalock, iflag);
5646 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5647 "(%d):2532 Mailbox command x%x (x%x) "
5648 "cannot issue Data: x%x x%x\n",
5649 mboxq->vport ? mboxq->vport->vpi : 0,
5650 mboxq->u.mb.mbxCommand,
5651 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5652 psli->sli_flag, MBX_POLL);
5653 return MBXERR_ERROR;
5654 }
5655 /* The server grabs the token and owns it until release */
5656 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
5657 phba->sli.mbox_active = mboxq;
5658 spin_unlock_irqrestore(&phba->hbalock, iflag);
5659
5660 /*
5661 * Initialize the bootstrap memory region to avoid stale data areas
5662 * in the mailbox post. Then copy the caller's mailbox contents to
5663 * the bmbx mailbox region.
5664 */
5665 mbx_cmnd = bf_get(lpfc_mqe_command, mb);
5666 memset(phba->sli4_hba.bmbx.avirt, 0, sizeof(struct lpfc_bmbx_create));
5667 lpfc_sli_pcimem_bcopy(mb, phba->sli4_hba.bmbx.avirt,
5668 sizeof(struct lpfc_mqe));
5669
5670 /* Post the high mailbox dma address to the port and wait for ready. */
5671 dma_address = &phba->sli4_hba.bmbx.dma_address;
5672 writel(dma_address->addr_hi, phba->sli4_hba.BMBXregaddr);
5673
5674 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mbx_cmnd)
5675 * 1000) + jiffies;
5676 do {
5677 bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
5678 db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
5679 if (!db_ready)
5680 msleep(2);
5681
5682 if (time_after(jiffies, timeout)) {
5683 rc = MBXERR_ERROR;
5684 goto exit;
5685 }
5686 } while (!db_ready);
5687
5688 /* Post the low mailbox dma address to the port. */
5689 writel(dma_address->addr_lo, phba->sli4_hba.BMBXregaddr);
5690 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mbx_cmnd)
5691 * 1000) + jiffies;
5692 do {
5693 bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
5694 db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
5695 if (!db_ready)
5696 msleep(2);
5697
5698 if (time_after(jiffies, timeout)) {
5699 rc = MBXERR_ERROR;
5700 goto exit;
5701 }
5702 } while (!db_ready);
5703
5704 /*
5705 * Read the CQ to ensure the mailbox has completed.
5706 * If so, update the mailbox status so that the upper layers
5707 * can complete the request normally.
5708 */
5709 lpfc_sli_pcimem_bcopy(phba->sli4_hba.bmbx.avirt, mb,
5710 sizeof(struct lpfc_mqe));
5711 mbox_rgn = (struct lpfc_bmbx_create *) phba->sli4_hba.bmbx.avirt;
5712 lpfc_sli_pcimem_bcopy(&mbox_rgn->mcqe, &mboxq->mcqe,
5713 sizeof(struct lpfc_mcqe));
5714 mcqe_status = bf_get(lpfc_mcqe_status, &mbox_rgn->mcqe);
5715
5716 /* Prefix the mailbox status with range x4000 to note SLI4 status. */
5717 if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
5718 bf_set(lpfc_mqe_status, mb, LPFC_MBX_ERROR_RANGE | mcqe_status);
5719 rc = MBXERR_ERROR;
James Smartd7c47992010-06-08 18:31:54 -04005720 } else
5721 lpfc_sli4_swap_str(phba, mboxq);
James Smartda0436e2009-05-22 14:51:39 -04005722
5723 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
5724 "(%d):0356 Mailbox cmd x%x (x%x) Status x%x "
5725 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x"
5726 " x%x x%x CQ: x%x x%x x%x x%x\n",
5727 mboxq->vport ? mboxq->vport->vpi : 0,
5728 mbx_cmnd, lpfc_sli4_mbox_opcode_get(phba, mboxq),
5729 bf_get(lpfc_mqe_status, mb),
5730 mb->un.mb_words[0], mb->un.mb_words[1],
5731 mb->un.mb_words[2], mb->un.mb_words[3],
5732 mb->un.mb_words[4], mb->un.mb_words[5],
5733 mb->un.mb_words[6], mb->un.mb_words[7],
5734 mb->un.mb_words[8], mb->un.mb_words[9],
5735 mb->un.mb_words[10], mb->un.mb_words[11],
5736 mb->un.mb_words[12], mboxq->mcqe.word0,
5737 mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
5738 mboxq->mcqe.trailer);
5739exit:
5740 /* We are holding the token, no needed for lock when release */
5741 spin_lock_irqsave(&phba->hbalock, iflag);
5742 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
5743 phba->sli.mbox_active = NULL;
5744 spin_unlock_irqrestore(&phba->hbalock, iflag);
5745 return rc;
5746}
5747
5748/**
5749 * lpfc_sli_issue_mbox_s4 - Issue an SLI4 mailbox command to firmware
5750 * @phba: Pointer to HBA context object.
5751 * @pmbox: Pointer to mailbox object.
5752 * @flag: Flag indicating how the mailbox need to be processed.
5753 *
5754 * This function is called by discovery code and HBA management code to submit
5755 * a mailbox command to firmware with SLI-4 interface spec.
5756 *
5757 * Return codes the caller owns the mailbox command after the return of the
5758 * function.
5759 **/
5760static int
5761lpfc_sli_issue_mbox_s4(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
5762 uint32_t flag)
5763{
5764 struct lpfc_sli *psli = &phba->sli;
5765 unsigned long iflags;
5766 int rc;
5767
James Smart8fa38512009-07-19 10:01:03 -04005768 rc = lpfc_mbox_dev_check(phba);
5769 if (unlikely(rc)) {
5770 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5771 "(%d):2544 Mailbox command x%x (x%x) "
5772 "cannot issue Data: x%x x%x\n",
5773 mboxq->vport ? mboxq->vport->vpi : 0,
5774 mboxq->u.mb.mbxCommand,
5775 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5776 psli->sli_flag, flag);
5777 goto out_not_finished;
5778 }
5779
James Smartda0436e2009-05-22 14:51:39 -04005780 /* Detect polling mode and jump to a handler */
5781 if (!phba->sli4_hba.intr_enable) {
5782 if (flag == MBX_POLL)
5783 rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
5784 else
5785 rc = -EIO;
5786 if (rc != MBX_SUCCESS)
5787 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5788 "(%d):2541 Mailbox command x%x "
5789 "(x%x) cannot issue Data: x%x x%x\n",
5790 mboxq->vport ? mboxq->vport->vpi : 0,
5791 mboxq->u.mb.mbxCommand,
5792 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5793 psli->sli_flag, flag);
5794 return rc;
5795 } else if (flag == MBX_POLL) {
James Smartf1126682009-06-10 17:22:44 -04005796 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
5797 "(%d):2542 Try to issue mailbox command "
5798 "x%x (x%x) synchronously ahead of async"
5799 "mailbox command queue: x%x x%x\n",
James Smartda0436e2009-05-22 14:51:39 -04005800 mboxq->vport ? mboxq->vport->vpi : 0,
5801 mboxq->u.mb.mbxCommand,
5802 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5803 psli->sli_flag, flag);
James Smartf1126682009-06-10 17:22:44 -04005804 /* Try to block the asynchronous mailbox posting */
5805 rc = lpfc_sli4_async_mbox_block(phba);
5806 if (!rc) {
5807 /* Successfully blocked, now issue sync mbox cmd */
5808 rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
5809 if (rc != MBX_SUCCESS)
5810 lpfc_printf_log(phba, KERN_ERR,
5811 LOG_MBOX | LOG_SLI,
5812 "(%d):2597 Mailbox command "
5813 "x%x (x%x) cannot issue "
5814 "Data: x%x x%x\n",
5815 mboxq->vport ?
5816 mboxq->vport->vpi : 0,
5817 mboxq->u.mb.mbxCommand,
5818 lpfc_sli4_mbox_opcode_get(phba,
5819 mboxq),
5820 psli->sli_flag, flag);
5821 /* Unblock the async mailbox posting afterward */
5822 lpfc_sli4_async_mbox_unblock(phba);
5823 }
5824 return rc;
James Smartda0436e2009-05-22 14:51:39 -04005825 }
5826
5827 /* Now, interrupt mode asynchrous mailbox command */
5828 rc = lpfc_mbox_cmd_check(phba, mboxq);
5829 if (rc) {
5830 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5831 "(%d):2543 Mailbox command x%x (x%x) "
5832 "cannot issue Data: x%x x%x\n",
5833 mboxq->vport ? mboxq->vport->vpi : 0,
5834 mboxq->u.mb.mbxCommand,
5835 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5836 psli->sli_flag, flag);
5837 goto out_not_finished;
5838 }
James Smartda0436e2009-05-22 14:51:39 -04005839
5840 /* Put the mailbox command to the driver internal FIFO */
5841 psli->slistat.mbox_busy++;
5842 spin_lock_irqsave(&phba->hbalock, iflags);
5843 lpfc_mbox_put(phba, mboxq);
5844 spin_unlock_irqrestore(&phba->hbalock, iflags);
5845 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
5846 "(%d):0354 Mbox cmd issue - Enqueue Data: "
5847 "x%x (x%x) x%x x%x x%x\n",
5848 mboxq->vport ? mboxq->vport->vpi : 0xffffff,
5849 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
5850 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5851 phba->pport->port_state,
5852 psli->sli_flag, MBX_NOWAIT);
5853 /* Wake up worker thread to transport mailbox command from head */
5854 lpfc_worker_wake_up(phba);
5855
5856 return MBX_BUSY;
5857
5858out_not_finished:
5859 return MBX_NOT_FINISHED;
5860}
5861
5862/**
5863 * lpfc_sli4_post_async_mbox - Post an SLI4 mailbox command to device
5864 * @phba: Pointer to HBA context object.
5865 *
5866 * This function is called by worker thread to send a mailbox command to
5867 * SLI4 HBA firmware.
5868 *
5869 **/
5870int
5871lpfc_sli4_post_async_mbox(struct lpfc_hba *phba)
5872{
5873 struct lpfc_sli *psli = &phba->sli;
5874 LPFC_MBOXQ_t *mboxq;
5875 int rc = MBX_SUCCESS;
5876 unsigned long iflags;
5877 struct lpfc_mqe *mqe;
5878 uint32_t mbx_cmnd;
5879
5880 /* Check interrupt mode before post async mailbox command */
5881 if (unlikely(!phba->sli4_hba.intr_enable))
5882 return MBX_NOT_FINISHED;
5883
5884 /* Check for mailbox command service token */
5885 spin_lock_irqsave(&phba->hbalock, iflags);
5886 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
5887 spin_unlock_irqrestore(&phba->hbalock, iflags);
5888 return MBX_NOT_FINISHED;
5889 }
5890 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
5891 spin_unlock_irqrestore(&phba->hbalock, iflags);
5892 return MBX_NOT_FINISHED;
5893 }
5894 if (unlikely(phba->sli.mbox_active)) {
5895 spin_unlock_irqrestore(&phba->hbalock, iflags);
5896 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5897 "0384 There is pending active mailbox cmd\n");
5898 return MBX_NOT_FINISHED;
5899 }
5900 /* Take the mailbox command service token */
5901 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
5902
5903 /* Get the next mailbox command from head of queue */
5904 mboxq = lpfc_mbox_get(phba);
5905
5906 /* If no more mailbox command waiting for post, we're done */
5907 if (!mboxq) {
5908 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
5909 spin_unlock_irqrestore(&phba->hbalock, iflags);
5910 return MBX_SUCCESS;
5911 }
5912 phba->sli.mbox_active = mboxq;
5913 spin_unlock_irqrestore(&phba->hbalock, iflags);
5914
5915 /* Check device readiness for posting mailbox command */
5916 rc = lpfc_mbox_dev_check(phba);
5917 if (unlikely(rc))
5918 /* Driver clean routine will clean up pending mailbox */
5919 goto out_not_finished;
5920
5921 /* Prepare the mbox command to be posted */
5922 mqe = &mboxq->u.mqe;
5923 mbx_cmnd = bf_get(lpfc_mqe_command, mqe);
5924
5925 /* Start timer for the mbox_tmo and log some mailbox post messages */
5926 mod_timer(&psli->mbox_tmo, (jiffies +
5927 (HZ * lpfc_mbox_tmo_val(phba, mbx_cmnd))));
5928
5929 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
5930 "(%d):0355 Mailbox cmd x%x (x%x) issue Data: "
5931 "x%x x%x\n",
5932 mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
5933 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5934 phba->pport->port_state, psli->sli_flag);
5935
5936 if (mbx_cmnd != MBX_HEARTBEAT) {
5937 if (mboxq->vport) {
5938 lpfc_debugfs_disc_trc(mboxq->vport,
5939 LPFC_DISC_TRC_MBOX_VPORT,
5940 "MBOX Send vport: cmd:x%x mb:x%x x%x",
5941 mbx_cmnd, mqe->un.mb_words[0],
5942 mqe->un.mb_words[1]);
5943 } else {
5944 lpfc_debugfs_disc_trc(phba->pport,
5945 LPFC_DISC_TRC_MBOX,
5946 "MBOX Send: cmd:x%x mb:x%x x%x",
5947 mbx_cmnd, mqe->un.mb_words[0],
5948 mqe->un.mb_words[1]);
5949 }
5950 }
5951 psli->slistat.mbox_cmd++;
5952
5953 /* Post the mailbox command to the port */
5954 rc = lpfc_sli4_mq_put(phba->sli4_hba.mbx_wq, mqe);
5955 if (rc != MBX_SUCCESS) {
5956 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5957 "(%d):2533 Mailbox command x%x (x%x) "
5958 "cannot issue Data: x%x x%x\n",
5959 mboxq->vport ? mboxq->vport->vpi : 0,
5960 mboxq->u.mb.mbxCommand,
5961 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5962 psli->sli_flag, MBX_NOWAIT);
5963 goto out_not_finished;
5964 }
5965
5966 return rc;
5967
5968out_not_finished:
5969 spin_lock_irqsave(&phba->hbalock, iflags);
5970 mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
5971 __lpfc_mbox_cmpl_put(phba, mboxq);
5972 /* Release the token */
5973 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
5974 phba->sli.mbox_active = NULL;
5975 spin_unlock_irqrestore(&phba->hbalock, iflags);
5976
5977 return MBX_NOT_FINISHED;
5978}
5979
5980/**
5981 * lpfc_sli_issue_mbox - Wrapper func for issuing mailbox command
5982 * @phba: Pointer to HBA context object.
5983 * @pmbox: Pointer to mailbox object.
5984 * @flag: Flag indicating how the mailbox need to be processed.
5985 *
5986 * This routine wraps the actual SLI3 or SLI4 mailbox issuing routine from
5987 * the API jump table function pointer from the lpfc_hba struct.
5988 *
5989 * Return codes the caller owns the mailbox command after the return of the
5990 * function.
5991 **/
5992int
5993lpfc_sli_issue_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox, uint32_t flag)
5994{
5995 return phba->lpfc_sli_issue_mbox(phba, pmbox, flag);
5996}
5997
5998/**
5999 * lpfc_mbox_api_table_setup - Set up mbox api fucntion jump table
6000 * @phba: The hba struct for which this call is being executed.
6001 * @dev_grp: The HBA PCI-Device group number.
6002 *
6003 * This routine sets up the mbox interface API function jump table in @phba
6004 * struct.
6005 * Returns: 0 - success, -ENODEV - failure.
6006 **/
6007int
6008lpfc_mbox_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
6009{
6010
6011 switch (dev_grp) {
6012 case LPFC_PCI_DEV_LP:
6013 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s3;
6014 phba->lpfc_sli_handle_slow_ring_event =
6015 lpfc_sli_handle_slow_ring_event_s3;
6016 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s3;
6017 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s3;
6018 phba->lpfc_sli_brdready = lpfc_sli_brdready_s3;
6019 break;
6020 case LPFC_PCI_DEV_OC:
6021 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s4;
6022 phba->lpfc_sli_handle_slow_ring_event =
6023 lpfc_sli_handle_slow_ring_event_s4;
6024 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s4;
6025 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s4;
6026 phba->lpfc_sli_brdready = lpfc_sli_brdready_s4;
6027 break;
6028 default:
6029 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6030 "1420 Invalid HBA PCI-device group: 0x%x\n",
6031 dev_grp);
6032 return -ENODEV;
6033 break;
6034 }
6035 return 0;
6036}
6037
6038/**
James Smart3621a712009-04-06 18:47:14 -04006039 * __lpfc_sli_ringtx_put - Add an iocb to the txq
James Smarte59058c2008-08-24 21:49:00 -04006040 * @phba: Pointer to HBA context object.
6041 * @pring: Pointer to driver SLI ring object.
6042 * @piocb: Pointer to address of newly added command iocb.
6043 *
6044 * This function is called with hbalock held to add a command
6045 * iocb to the txq when SLI layer cannot submit the command iocb
6046 * to the ring.
6047 **/
James Smart2a9bf3d2010-06-07 15:24:45 -04006048void
James Smart92d7f7b2007-06-17 19:56:38 -05006049__lpfc_sli_ringtx_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -05006050 struct lpfc_iocbq *piocb)
dea31012005-04-17 16:05:31 -05006051{
6052 /* Insert the caller's iocb in the txq tail for later processing. */
6053 list_add_tail(&piocb->list, &pring->txq);
6054 pring->txq_cnt++;
dea31012005-04-17 16:05:31 -05006055}
6056
James Smarte59058c2008-08-24 21:49:00 -04006057/**
James Smart3621a712009-04-06 18:47:14 -04006058 * lpfc_sli_next_iocb - Get the next iocb in the txq
James Smarte59058c2008-08-24 21:49:00 -04006059 * @phba: Pointer to HBA context object.
6060 * @pring: Pointer to driver SLI ring object.
6061 * @piocb: Pointer to address of newly added command iocb.
6062 *
6063 * This function is called with hbalock held before a new
6064 * iocb is submitted to the firmware. This function checks
6065 * txq to flush the iocbs in txq to Firmware before
6066 * submitting new iocbs to the Firmware.
6067 * If there are iocbs in the txq which need to be submitted
6068 * to firmware, lpfc_sli_next_iocb returns the first element
6069 * of the txq after dequeuing it from txq.
6070 * If there is no iocb in the txq then the function will return
6071 * *piocb and *piocb is set to NULL. Caller needs to check
6072 * *piocb to find if there are more commands in the txq.
6073 **/
dea31012005-04-17 16:05:31 -05006074static struct lpfc_iocbq *
6075lpfc_sli_next_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -05006076 struct lpfc_iocbq **piocb)
dea31012005-04-17 16:05:31 -05006077{
6078 struct lpfc_iocbq * nextiocb;
6079
6080 nextiocb = lpfc_sli_ringtx_get(phba, pring);
6081 if (!nextiocb) {
6082 nextiocb = *piocb;
6083 *piocb = NULL;
6084 }
6085
6086 return nextiocb;
6087}
6088
James Smarte59058c2008-08-24 21:49:00 -04006089/**
James Smart3772a992009-05-22 14:50:54 -04006090 * __lpfc_sli_issue_iocb_s3 - SLI3 device lockless ver of lpfc_sli_issue_iocb
James Smarte59058c2008-08-24 21:49:00 -04006091 * @phba: Pointer to HBA context object.
James Smart3772a992009-05-22 14:50:54 -04006092 * @ring_number: SLI ring number to issue iocb on.
James Smarte59058c2008-08-24 21:49:00 -04006093 * @piocb: Pointer to command iocb.
6094 * @flag: Flag indicating if this command can be put into txq.
6095 *
James Smart3772a992009-05-22 14:50:54 -04006096 * __lpfc_sli_issue_iocb_s3 is used by other functions in the driver to issue
6097 * an iocb command to an HBA with SLI-3 interface spec. If the PCI slot is
6098 * recovering from error state, if HBA is resetting or if LPFC_STOP_IOCB_EVENT
6099 * flag is turned on, the function returns IOCB_ERROR. When the link is down,
6100 * this function allows only iocbs for posting buffers. This function finds
6101 * next available slot in the command ring and posts the command to the
6102 * available slot and writes the port attention register to request HBA start
6103 * processing new iocb. If there is no slot available in the ring and
6104 * flag & SLI_IOCB_RET_IOCB is set, the new iocb is added to the txq, otherwise
6105 * the function returns IOCB_BUSY.
James Smarte59058c2008-08-24 21:49:00 -04006106 *
James Smart3772a992009-05-22 14:50:54 -04006107 * This function is called with hbalock held. The function will return success
6108 * after it successfully submit the iocb to firmware or after adding to the
6109 * txq.
James Smarte59058c2008-08-24 21:49:00 -04006110 **/
James Smart98c9ea52007-10-27 13:37:33 -04006111static int
James Smart3772a992009-05-22 14:50:54 -04006112__lpfc_sli_issue_iocb_s3(struct lpfc_hba *phba, uint32_t ring_number,
dea31012005-04-17 16:05:31 -05006113 struct lpfc_iocbq *piocb, uint32_t flag)
6114{
6115 struct lpfc_iocbq *nextiocb;
6116 IOCB_t *iocb;
James Smart3772a992009-05-22 14:50:54 -04006117 struct lpfc_sli_ring *pring = &phba->sli.ring[ring_number];
dea31012005-04-17 16:05:31 -05006118
James Smart92d7f7b2007-06-17 19:56:38 -05006119 if (piocb->iocb_cmpl && (!piocb->vport) &&
6120 (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
6121 (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
6122 lpfc_printf_log(phba, KERN_ERR,
6123 LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04006124 "1807 IOCB x%x failed. No vport\n",
James Smart92d7f7b2007-06-17 19:56:38 -05006125 piocb->iocb.ulpCommand);
6126 dump_stack();
6127 return IOCB_ERROR;
6128 }
6129
6130
Linas Vepstas8d63f372007-02-14 14:28:36 -06006131 /* If the PCI channel is in offline state, do not post iocbs. */
6132 if (unlikely(pci_channel_offline(phba->pcidev)))
6133 return IOCB_ERROR;
6134
James Smarta257bf92009-04-06 18:48:10 -04006135 /* If HBA has a deferred error attention, fail the iocb. */
6136 if (unlikely(phba->hba_flag & DEFER_ERATT))
6137 return IOCB_ERROR;
6138
dea31012005-04-17 16:05:31 -05006139 /*
6140 * We should never get an IOCB if we are in a < LINK_DOWN state
6141 */
James Smart2e0fef82007-06-17 19:56:36 -05006142 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
dea31012005-04-17 16:05:31 -05006143 return IOCB_ERROR;
6144
6145 /*
6146 * Check to see if we are blocking IOCB processing because of a
James Smart0b727fe2007-10-27 13:37:25 -04006147 * outstanding event.
dea31012005-04-17 16:05:31 -05006148 */
James Smart0b727fe2007-10-27 13:37:25 -04006149 if (unlikely(pring->flag & LPFC_STOP_IOCB_EVENT))
dea31012005-04-17 16:05:31 -05006150 goto iocb_busy;
6151
James Smart2e0fef82007-06-17 19:56:36 -05006152 if (unlikely(phba->link_state == LPFC_LINK_DOWN)) {
dea31012005-04-17 16:05:31 -05006153 /*
James Smart2680eea2007-04-25 09:52:55 -04006154 * Only CREATE_XRI, CLOSE_XRI, and QUE_RING_BUF
dea31012005-04-17 16:05:31 -05006155 * can be issued if the link is not up.
6156 */
6157 switch (piocb->iocb.ulpCommand) {
James Smart84774a42008-08-24 21:50:06 -04006158 case CMD_GEN_REQUEST64_CR:
6159 case CMD_GEN_REQUEST64_CX:
6160 if (!(phba->sli.sli_flag & LPFC_MENLO_MAINT) ||
6161 (piocb->iocb.un.genreq64.w5.hcsw.Rctl !=
James Smart6a9c52c2009-10-02 15:16:51 -04006162 FC_RCTL_DD_UNSOL_CMD) ||
James Smart84774a42008-08-24 21:50:06 -04006163 (piocb->iocb.un.genreq64.w5.hcsw.Type !=
6164 MENLO_TRANSPORT_TYPE))
6165
6166 goto iocb_busy;
6167 break;
dea31012005-04-17 16:05:31 -05006168 case CMD_QUE_RING_BUF_CN:
6169 case CMD_QUE_RING_BUF64_CN:
dea31012005-04-17 16:05:31 -05006170 /*
6171 * For IOCBs, like QUE_RING_BUF, that have no rsp ring
6172 * completion, iocb_cmpl MUST be 0.
6173 */
6174 if (piocb->iocb_cmpl)
6175 piocb->iocb_cmpl = NULL;
6176 /*FALLTHROUGH*/
6177 case CMD_CREATE_XRI_CR:
James Smart2680eea2007-04-25 09:52:55 -04006178 case CMD_CLOSE_XRI_CN:
6179 case CMD_CLOSE_XRI_CX:
dea31012005-04-17 16:05:31 -05006180 break;
6181 default:
6182 goto iocb_busy;
6183 }
6184
6185 /*
6186 * For FCP commands, we must be in a state where we can process link
6187 * attention events.
6188 */
6189 } else if (unlikely(pring->ringno == phba->sli.fcp_ring &&
James Smart92d7f7b2007-06-17 19:56:38 -05006190 !(phba->sli.sli_flag & LPFC_PROCESS_LA))) {
dea31012005-04-17 16:05:31 -05006191 goto iocb_busy;
James Smart92d7f7b2007-06-17 19:56:38 -05006192 }
dea31012005-04-17 16:05:31 -05006193
dea31012005-04-17 16:05:31 -05006194 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
6195 (nextiocb = lpfc_sli_next_iocb(phba, pring, &piocb)))
6196 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
6197
6198 if (iocb)
6199 lpfc_sli_update_ring(phba, pring);
6200 else
6201 lpfc_sli_update_full_ring(phba, pring);
6202
6203 if (!piocb)
6204 return IOCB_SUCCESS;
6205
6206 goto out_busy;
6207
6208 iocb_busy:
6209 pring->stats.iocb_cmd_delay++;
6210
6211 out_busy:
6212
6213 if (!(flag & SLI_IOCB_RET_IOCB)) {
James Smart92d7f7b2007-06-17 19:56:38 -05006214 __lpfc_sli_ringtx_put(phba, pring, piocb);
dea31012005-04-17 16:05:31 -05006215 return IOCB_SUCCESS;
6216 }
6217
6218 return IOCB_BUSY;
6219}
6220
James Smart3772a992009-05-22 14:50:54 -04006221/**
James Smart4f774512009-05-22 14:52:35 -04006222 * lpfc_sli4_bpl2sgl - Convert the bpl/bde to a sgl.
6223 * @phba: Pointer to HBA context object.
6224 * @piocb: Pointer to command iocb.
6225 * @sglq: Pointer to the scatter gather queue object.
6226 *
6227 * This routine converts the bpl or bde that is in the IOCB
6228 * to a sgl list for the sli4 hardware. The physical address
6229 * of the bpl/bde is converted back to a virtual address.
6230 * If the IOCB contains a BPL then the list of BDE's is
6231 * converted to sli4_sge's. If the IOCB contains a single
6232 * BDE then it is converted to a single sli_sge.
6233 * The IOCB is still in cpu endianess so the contents of
6234 * the bpl can be used without byte swapping.
6235 *
6236 * Returns valid XRI = Success, NO_XRI = Failure.
6237**/
6238static uint16_t
6239lpfc_sli4_bpl2sgl(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq,
6240 struct lpfc_sglq *sglq)
6241{
6242 uint16_t xritag = NO_XRI;
6243 struct ulp_bde64 *bpl = NULL;
6244 struct ulp_bde64 bde;
6245 struct sli4_sge *sgl = NULL;
6246 IOCB_t *icmd;
6247 int numBdes = 0;
6248 int i = 0;
James Smart63e801c2010-11-20 23:14:19 -05006249 uint32_t offset = 0; /* accumulated offset in the sg request list */
6250 int inbound = 0; /* number of sg reply entries inbound from firmware */
James Smart4f774512009-05-22 14:52:35 -04006251
6252 if (!piocbq || !sglq)
6253 return xritag;
6254
6255 sgl = (struct sli4_sge *)sglq->sgl;
6256 icmd = &piocbq->iocb;
6257 if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
6258 numBdes = icmd->un.genreq64.bdl.bdeSize /
6259 sizeof(struct ulp_bde64);
6260 /* The addrHigh and addrLow fields within the IOCB
6261 * have not been byteswapped yet so there is no
6262 * need to swap them back.
6263 */
6264 bpl = (struct ulp_bde64 *)
6265 ((struct lpfc_dmabuf *)piocbq->context3)->virt;
6266
6267 if (!bpl)
6268 return xritag;
6269
6270 for (i = 0; i < numBdes; i++) {
6271 /* Should already be byte swapped. */
James Smart28baac72010-02-12 14:42:03 -05006272 sgl->addr_hi = bpl->addrHigh;
6273 sgl->addr_lo = bpl->addrLow;
6274
James Smart4f774512009-05-22 14:52:35 -04006275 if ((i+1) == numBdes)
6276 bf_set(lpfc_sli4_sge_last, sgl, 1);
6277 else
6278 bf_set(lpfc_sli4_sge_last, sgl, 0);
6279 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart28baac72010-02-12 14:42:03 -05006280 /* swap the size field back to the cpu so we
6281 * can assign it to the sgl.
6282 */
6283 bde.tus.w = le32_to_cpu(bpl->tus.w);
6284 sgl->sge_len = cpu_to_le32(bde.tus.f.bdeSize);
James Smart63e801c2010-11-20 23:14:19 -05006285 /* The offsets in the sgl need to be accumulated
6286 * separately for the request and reply lists.
6287 * The request is always first, the reply follows.
6288 */
6289 if (piocbq->iocb.ulpCommand == CMD_GEN_REQUEST64_CR) {
6290 /* add up the reply sg entries */
6291 if (bpl->tus.f.bdeFlags == BUFF_TYPE_BDE_64I)
6292 inbound++;
6293 /* first inbound? reset the offset */
6294 if (inbound == 1)
6295 offset = 0;
6296 bf_set(lpfc_sli4_sge_offset, sgl, offset);
6297 offset += bde.tus.f.bdeSize;
6298 }
James Smart4f774512009-05-22 14:52:35 -04006299 bpl++;
6300 sgl++;
6301 }
6302 } else if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BDE_64) {
6303 /* The addrHigh and addrLow fields of the BDE have not
6304 * been byteswapped yet so they need to be swapped
6305 * before putting them in the sgl.
6306 */
6307 sgl->addr_hi =
6308 cpu_to_le32(icmd->un.genreq64.bdl.addrHigh);
6309 sgl->addr_lo =
6310 cpu_to_le32(icmd->un.genreq64.bdl.addrLow);
James Smart4f774512009-05-22 14:52:35 -04006311 bf_set(lpfc_sli4_sge_last, sgl, 1);
6312 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart28baac72010-02-12 14:42:03 -05006313 sgl->sge_len =
6314 cpu_to_le32(icmd->un.genreq64.bdl.bdeSize);
James Smart4f774512009-05-22 14:52:35 -04006315 }
6316 return sglq->sli4_xritag;
6317}
6318
6319/**
6320 * lpfc_sli4_scmd_to_wqidx_distr - scsi command to SLI4 WQ index distribution
6321 * @phba: Pointer to HBA context object.
James Smart4f774512009-05-22 14:52:35 -04006322 *
James Smarta93ff372010-10-22 11:06:08 -04006323 * This routine performs a roundrobin SCSI command to SLI4 FCP WQ index
James Smart8fa38512009-07-19 10:01:03 -04006324 * distribution. This is called by __lpfc_sli_issue_iocb_s4() with the hbalock
6325 * held.
James Smart4f774512009-05-22 14:52:35 -04006326 *
6327 * Return: index into SLI4 fast-path FCP queue index.
6328 **/
6329static uint32_t
James Smart8fa38512009-07-19 10:01:03 -04006330lpfc_sli4_scmd_to_wqidx_distr(struct lpfc_hba *phba)
James Smart4f774512009-05-22 14:52:35 -04006331{
James Smart8fa38512009-07-19 10:01:03 -04006332 ++phba->fcp_qidx;
6333 if (phba->fcp_qidx >= phba->cfg_fcp_wq_count)
6334 phba->fcp_qidx = 0;
James Smart4f774512009-05-22 14:52:35 -04006335
James Smart8fa38512009-07-19 10:01:03 -04006336 return phba->fcp_qidx;
James Smart4f774512009-05-22 14:52:35 -04006337}
6338
6339/**
6340 * lpfc_sli_iocb2wqe - Convert the IOCB to a work queue entry.
6341 * @phba: Pointer to HBA context object.
6342 * @piocb: Pointer to command iocb.
6343 * @wqe: Pointer to the work queue entry.
6344 *
6345 * This routine converts the iocb command to its Work Queue Entry
6346 * equivalent. The wqe pointer should not have any fields set when
6347 * this routine is called because it will memcpy over them.
6348 * This routine does not set the CQ_ID or the WQEC bits in the
6349 * wqe.
6350 *
6351 * Returns: 0 = Success, IOCB_ERROR = Failure.
6352 **/
6353static int
6354lpfc_sli4_iocb2wqe(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq,
6355 union lpfc_wqe *wqe)
6356{
James Smart5ffc2662009-11-18 15:39:44 -05006357 uint32_t xmit_len = 0, total_len = 0;
James Smart4f774512009-05-22 14:52:35 -04006358 uint8_t ct = 0;
6359 uint32_t fip;
6360 uint32_t abort_tag;
6361 uint8_t command_type = ELS_COMMAND_NON_FIP;
6362 uint8_t cmnd;
6363 uint16_t xritag;
James Smartdcf2a4e2010-09-29 11:18:53 -04006364 uint16_t abrt_iotag;
6365 struct lpfc_iocbq *abrtiocbq;
James Smart4f774512009-05-22 14:52:35 -04006366 struct ulp_bde64 *bpl = NULL;
James Smartf0d9bcc2010-10-22 11:07:09 -04006367 uint32_t els_id = LPFC_ELS_ID_DEFAULT;
James Smart5ffc2662009-11-18 15:39:44 -05006368 int numBdes, i;
6369 struct ulp_bde64 bde;
James Smart4f774512009-05-22 14:52:35 -04006370
James Smart45ed1192009-10-02 15:17:02 -04006371 fip = phba->hba_flag & HBA_FIP_SUPPORT;
James Smart4f774512009-05-22 14:52:35 -04006372 /* The fcp commands will set command type */
James Smart0c287582009-06-10 17:22:56 -04006373 if (iocbq->iocb_flag & LPFC_IO_FCP)
James Smart4f774512009-05-22 14:52:35 -04006374 command_type = FCP_COMMAND;
James Smartc8685952009-11-18 15:39:16 -05006375 else if (fip && (iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK))
James Smart0c287582009-06-10 17:22:56 -04006376 command_type = ELS_COMMAND_FIP;
6377 else
6378 command_type = ELS_COMMAND_NON_FIP;
6379
James Smart4f774512009-05-22 14:52:35 -04006380 /* Some of the fields are in the right position already */
6381 memcpy(wqe, &iocbq->iocb, sizeof(union lpfc_wqe));
6382 abort_tag = (uint32_t) iocbq->iotag;
6383 xritag = iocbq->sli4_xritag;
James Smartf0d9bcc2010-10-22 11:07:09 -04006384 wqe->generic.wqe_com.word7 = 0; /* The ct field has moved so reset */
James Smart4f774512009-05-22 14:52:35 -04006385 /* words0-2 bpl convert bde */
6386 if (iocbq->iocb.un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
James Smart5ffc2662009-11-18 15:39:44 -05006387 numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
6388 sizeof(struct ulp_bde64);
James Smart4f774512009-05-22 14:52:35 -04006389 bpl = (struct ulp_bde64 *)
6390 ((struct lpfc_dmabuf *)iocbq->context3)->virt;
6391 if (!bpl)
6392 return IOCB_ERROR;
6393
6394 /* Should already be byte swapped. */
6395 wqe->generic.bde.addrHigh = le32_to_cpu(bpl->addrHigh);
6396 wqe->generic.bde.addrLow = le32_to_cpu(bpl->addrLow);
6397 /* swap the size field back to the cpu so we
6398 * can assign it to the sgl.
6399 */
6400 wqe->generic.bde.tus.w = le32_to_cpu(bpl->tus.w);
James Smart5ffc2662009-11-18 15:39:44 -05006401 xmit_len = wqe->generic.bde.tus.f.bdeSize;
6402 total_len = 0;
6403 for (i = 0; i < numBdes; i++) {
6404 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
6405 total_len += bde.tus.f.bdeSize;
6406 }
James Smart4f774512009-05-22 14:52:35 -04006407 } else
James Smart5ffc2662009-11-18 15:39:44 -05006408 xmit_len = iocbq->iocb.un.fcpi64.bdl.bdeSize;
James Smart4f774512009-05-22 14:52:35 -04006409
6410 iocbq->iocb.ulpIoTag = iocbq->iotag;
6411 cmnd = iocbq->iocb.ulpCommand;
6412
6413 switch (iocbq->iocb.ulpCommand) {
6414 case CMD_ELS_REQUEST64_CR:
6415 if (!iocbq->iocb.ulpLe) {
6416 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6417 "2007 Only Limited Edition cmd Format"
6418 " supported 0x%x\n",
6419 iocbq->iocb.ulpCommand);
6420 return IOCB_ERROR;
6421 }
James Smart5ffc2662009-11-18 15:39:44 -05006422 wqe->els_req.payload_len = xmit_len;
James Smart4f774512009-05-22 14:52:35 -04006423 /* Els_reguest64 has a TMO */
6424 bf_set(wqe_tmo, &wqe->els_req.wqe_com,
6425 iocbq->iocb.ulpTimeout);
6426 /* Need a VF for word 4 set the vf bit*/
6427 bf_set(els_req64_vf, &wqe->els_req, 0);
6428 /* And a VFID for word 12 */
6429 bf_set(els_req64_vfid, &wqe->els_req, 0);
James Smart4f774512009-05-22 14:52:35 -04006430 ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
James Smartf0d9bcc2010-10-22 11:07:09 -04006431 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
6432 iocbq->iocb.ulpContext);
6433 bf_set(wqe_ct, &wqe->els_req.wqe_com, ct);
6434 bf_set(wqe_pu, &wqe->els_req.wqe_com, 0);
James Smart4f774512009-05-22 14:52:35 -04006435 /* CCP CCPE PV PRI in word10 were set in the memcpy */
James Smartc8685952009-11-18 15:39:16 -05006436 if (command_type == ELS_COMMAND_FIP) {
6437 els_id = ((iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK)
6438 >> LPFC_FIP_ELS_ID_SHIFT);
6439 }
James Smartf0d9bcc2010-10-22 11:07:09 -04006440 bf_set(wqe_els_id, &wqe->els_req.wqe_com, els_id);
6441 bf_set(wqe_dbde, &wqe->els_req.wqe_com, 1);
6442 bf_set(wqe_iod, &wqe->els_req.wqe_com, LPFC_WQE_IOD_READ);
6443 bf_set(wqe_qosd, &wqe->els_req.wqe_com, 1);
6444 bf_set(wqe_lenloc, &wqe->els_req.wqe_com, LPFC_WQE_LENLOC_NONE);
6445 bf_set(wqe_ebde_cnt, &wqe->els_req.wqe_com, 0);
James Smart4f774512009-05-22 14:52:35 -04006446 break;
James Smart5ffc2662009-11-18 15:39:44 -05006447 case CMD_XMIT_SEQUENCE64_CX:
James Smartf0d9bcc2010-10-22 11:07:09 -04006448 bf_set(wqe_ctxt_tag, &wqe->xmit_sequence.wqe_com,
6449 iocbq->iocb.un.ulpWord[3]);
6450 bf_set(wqe_rcvoxid, &wqe->xmit_sequence.wqe_com,
6451 iocbq->iocb.ulpContext);
James Smart5ffc2662009-11-18 15:39:44 -05006452 /* The entire sequence is transmitted for this IOCB */
6453 xmit_len = total_len;
6454 cmnd = CMD_XMIT_SEQUENCE64_CR;
James Smart4f774512009-05-22 14:52:35 -04006455 case CMD_XMIT_SEQUENCE64_CR:
James Smartf0d9bcc2010-10-22 11:07:09 -04006456 /* word3 iocb=io_tag32 wqe=reserved */
6457 wqe->xmit_sequence.rsvd3 = 0;
James Smart4f774512009-05-22 14:52:35 -04006458 /* word4 relative_offset memcpy */
6459 /* word5 r_ctl/df_ctl memcpy */
James Smartf0d9bcc2010-10-22 11:07:09 -04006460 bf_set(wqe_pu, &wqe->xmit_sequence.wqe_com, 0);
6461 bf_set(wqe_dbde, &wqe->xmit_sequence.wqe_com, 1);
6462 bf_set(wqe_iod, &wqe->xmit_sequence.wqe_com,
6463 LPFC_WQE_IOD_WRITE);
6464 bf_set(wqe_lenloc, &wqe->xmit_sequence.wqe_com,
6465 LPFC_WQE_LENLOC_WORD12);
6466 bf_set(wqe_ebde_cnt, &wqe->xmit_sequence.wqe_com, 0);
James Smart5ffc2662009-11-18 15:39:44 -05006467 wqe->xmit_sequence.xmit_len = xmit_len;
6468 command_type = OTHER_COMMAND;
James Smart4f774512009-05-22 14:52:35 -04006469 break;
6470 case CMD_XMIT_BCAST64_CN:
James Smartf0d9bcc2010-10-22 11:07:09 -04006471 /* word3 iocb=iotag32 wqe=seq_payload_len */
6472 wqe->xmit_bcast64.seq_payload_len = xmit_len;
James Smart4f774512009-05-22 14:52:35 -04006473 /* word4 iocb=rsvd wqe=rsvd */
6474 /* word5 iocb=rctl/type/df_ctl wqe=rctl/type/df_ctl memcpy */
6475 /* word6 iocb=ctxt_tag/io_tag wqe=ctxt_tag/xri */
James Smartf0d9bcc2010-10-22 11:07:09 -04006476 bf_set(wqe_ct, &wqe->xmit_bcast64.wqe_com,
James Smart4f774512009-05-22 14:52:35 -04006477 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
James Smartf0d9bcc2010-10-22 11:07:09 -04006478 bf_set(wqe_dbde, &wqe->xmit_bcast64.wqe_com, 1);
6479 bf_set(wqe_iod, &wqe->xmit_bcast64.wqe_com, LPFC_WQE_IOD_WRITE);
6480 bf_set(wqe_lenloc, &wqe->xmit_bcast64.wqe_com,
6481 LPFC_WQE_LENLOC_WORD3);
6482 bf_set(wqe_ebde_cnt, &wqe->xmit_bcast64.wqe_com, 0);
James Smart4f774512009-05-22 14:52:35 -04006483 break;
6484 case CMD_FCP_IWRITE64_CR:
6485 command_type = FCP_COMMAND_DATA_OUT;
James Smartf0d9bcc2010-10-22 11:07:09 -04006486 /* word3 iocb=iotag wqe=payload_offset_len */
6487 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
6488 wqe->fcp_iwrite.payload_offset_len =
James Smart5ffc2662009-11-18 15:39:44 -05006489 xmit_len + sizeof(struct fcp_rsp);
James Smartf0d9bcc2010-10-22 11:07:09 -04006490 /* word4 iocb=parameter wqe=total_xfer_length memcpy */
6491 /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
6492 bf_set(wqe_erp, &wqe->fcp_iwrite.wqe_com,
6493 iocbq->iocb.ulpFCP2Rcvy);
6494 bf_set(wqe_lnk, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpXS);
6495 /* Always open the exchange */
6496 bf_set(wqe_xc, &wqe->fcp_iwrite.wqe_com, 0);
6497 bf_set(wqe_dbde, &wqe->fcp_iwrite.wqe_com, 1);
6498 bf_set(wqe_iod, &wqe->fcp_iwrite.wqe_com, LPFC_WQE_IOD_WRITE);
6499 bf_set(wqe_lenloc, &wqe->fcp_iwrite.wqe_com,
6500 LPFC_WQE_LENLOC_WORD4);
6501 bf_set(wqe_ebde_cnt, &wqe->fcp_iwrite.wqe_com, 0);
6502 bf_set(wqe_pu, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpPU);
6503 break;
6504 case CMD_FCP_IREAD64_CR:
6505 /* word3 iocb=iotag wqe=payload_offset_len */
6506 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
6507 wqe->fcp_iread.payload_offset_len =
6508 xmit_len + sizeof(struct fcp_rsp);
6509 /* word4 iocb=parameter wqe=total_xfer_length memcpy */
6510 /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
6511 bf_set(wqe_erp, &wqe->fcp_iread.wqe_com,
6512 iocbq->iocb.ulpFCP2Rcvy);
6513 bf_set(wqe_lnk, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpXS);
James Smart4f774512009-05-22 14:52:35 -04006514 /* Always open the exchange */
6515 bf_set(wqe_xc, &wqe->fcp_iread.wqe_com, 0);
James Smartf0d9bcc2010-10-22 11:07:09 -04006516 bf_set(wqe_dbde, &wqe->fcp_iread.wqe_com, 1);
6517 bf_set(wqe_iod, &wqe->fcp_iread.wqe_com, LPFC_WQE_IOD_READ);
6518 bf_set(wqe_lenloc, &wqe->fcp_iread.wqe_com,
6519 LPFC_WQE_LENLOC_WORD4);
6520 bf_set(wqe_ebde_cnt, &wqe->fcp_iread.wqe_com, 0);
6521 bf_set(wqe_pu, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpPU);
6522 break;
James Smartf1126682009-06-10 17:22:44 -04006523 case CMD_FCP_ICMND64_CR:
James Smartf0d9bcc2010-10-22 11:07:09 -04006524 /* word3 iocb=IO_TAG wqe=reserved */
6525 wqe->fcp_icmd.rsrvd3 = 0;
6526 bf_set(wqe_pu, &wqe->fcp_icmd.wqe_com, 0);
James Smartf1126682009-06-10 17:22:44 -04006527 /* Always open the exchange */
James Smartf0d9bcc2010-10-22 11:07:09 -04006528 bf_set(wqe_xc, &wqe->fcp_icmd.wqe_com, 0);
6529 bf_set(wqe_dbde, &wqe->fcp_icmd.wqe_com, 1);
6530 bf_set(wqe_iod, &wqe->fcp_icmd.wqe_com, LPFC_WQE_IOD_WRITE);
6531 bf_set(wqe_qosd, &wqe->fcp_icmd.wqe_com, 1);
6532 bf_set(wqe_lenloc, &wqe->fcp_icmd.wqe_com,
6533 LPFC_WQE_LENLOC_NONE);
6534 bf_set(wqe_ebde_cnt, &wqe->fcp_icmd.wqe_com, 0);
James Smart4f774512009-05-22 14:52:35 -04006535 break;
6536 case CMD_GEN_REQUEST64_CR:
James Smart63e801c2010-11-20 23:14:19 -05006537 /* For this command calculate the xmit length of the
6538 * request bde.
6539 */
6540 xmit_len = 0;
6541 numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
6542 sizeof(struct ulp_bde64);
6543 for (i = 0; i < numBdes; i++) {
6544 if (bpl[i].tus.f.bdeFlags != BUFF_TYPE_BDE_64)
6545 break;
6546 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
6547 xmit_len += bde.tus.f.bdeSize;
6548 }
James Smartf0d9bcc2010-10-22 11:07:09 -04006549 /* word3 iocb=IO_TAG wqe=request_payload_len */
6550 wqe->gen_req.request_payload_len = xmit_len;
6551 /* word4 iocb=parameter wqe=relative_offset memcpy */
6552 /* word5 [rctl, type, df_ctl, la] copied in memcpy */
James Smart4f774512009-05-22 14:52:35 -04006553 /* word6 context tag copied in memcpy */
6554 if (iocbq->iocb.ulpCt_h || iocbq->iocb.ulpCt_l) {
6555 ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
6556 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6557 "2015 Invalid CT %x command 0x%x\n",
6558 ct, iocbq->iocb.ulpCommand);
6559 return IOCB_ERROR;
6560 }
James Smartf0d9bcc2010-10-22 11:07:09 -04006561 bf_set(wqe_ct, &wqe->gen_req.wqe_com, 0);
6562 bf_set(wqe_tmo, &wqe->gen_req.wqe_com, iocbq->iocb.ulpTimeout);
6563 bf_set(wqe_pu, &wqe->gen_req.wqe_com, iocbq->iocb.ulpPU);
6564 bf_set(wqe_dbde, &wqe->gen_req.wqe_com, 1);
6565 bf_set(wqe_iod, &wqe->gen_req.wqe_com, LPFC_WQE_IOD_READ);
6566 bf_set(wqe_qosd, &wqe->gen_req.wqe_com, 1);
6567 bf_set(wqe_lenloc, &wqe->gen_req.wqe_com, LPFC_WQE_LENLOC_NONE);
6568 bf_set(wqe_ebde_cnt, &wqe->gen_req.wqe_com, 0);
James Smart4f774512009-05-22 14:52:35 -04006569 command_type = OTHER_COMMAND;
6570 break;
6571 case CMD_XMIT_ELS_RSP64_CX:
6572 /* words0-2 BDE memcpy */
James Smartf0d9bcc2010-10-22 11:07:09 -04006573 /* word3 iocb=iotag32 wqe=response_payload_len */
6574 wqe->xmit_els_rsp.response_payload_len = xmit_len;
James Smart4f774512009-05-22 14:52:35 -04006575 /* word4 iocb=did wge=rsvd. */
James Smartf0d9bcc2010-10-22 11:07:09 -04006576 wqe->xmit_els_rsp.rsvd4 = 0;
James Smart4f774512009-05-22 14:52:35 -04006577 /* word5 iocb=rsvd wge=did */
6578 bf_set(wqe_els_did, &wqe->xmit_els_rsp.wqe_dest,
6579 iocbq->iocb.un.elsreq64.remoteID);
James Smartf0d9bcc2010-10-22 11:07:09 -04006580 bf_set(wqe_ct, &wqe->xmit_els_rsp.wqe_com,
6581 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
6582 bf_set(wqe_pu, &wqe->xmit_els_rsp.wqe_com, iocbq->iocb.ulpPU);
6583 bf_set(wqe_rcvoxid, &wqe->xmit_els_rsp.wqe_com,
6584 iocbq->iocb.ulpContext);
James Smart4f774512009-05-22 14:52:35 -04006585 if (!iocbq->iocb.ulpCt_h && iocbq->iocb.ulpCt_l)
James Smartf0d9bcc2010-10-22 11:07:09 -04006586 bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
James Smart4f774512009-05-22 14:52:35 -04006587 iocbq->vport->vpi + phba->vpi_base);
James Smartf0d9bcc2010-10-22 11:07:09 -04006588 bf_set(wqe_dbde, &wqe->xmit_els_rsp.wqe_com, 1);
6589 bf_set(wqe_iod, &wqe->xmit_els_rsp.wqe_com, LPFC_WQE_IOD_WRITE);
6590 bf_set(wqe_qosd, &wqe->xmit_els_rsp.wqe_com, 1);
6591 bf_set(wqe_lenloc, &wqe->xmit_els_rsp.wqe_com,
6592 LPFC_WQE_LENLOC_WORD3);
6593 bf_set(wqe_ebde_cnt, &wqe->xmit_els_rsp.wqe_com, 0);
James Smart4f774512009-05-22 14:52:35 -04006594 command_type = OTHER_COMMAND;
6595 break;
6596 case CMD_CLOSE_XRI_CN:
6597 case CMD_ABORT_XRI_CN:
6598 case CMD_ABORT_XRI_CX:
6599 /* words 0-2 memcpy should be 0 rserved */
6600 /* port will send abts */
James Smartdcf2a4e2010-09-29 11:18:53 -04006601 abrt_iotag = iocbq->iocb.un.acxri.abortContextTag;
6602 if (abrt_iotag != 0 && abrt_iotag <= phba->sli.last_iotag) {
6603 abrtiocbq = phba->sli.iocbq_lookup[abrt_iotag];
6604 fip = abrtiocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK;
6605 } else
6606 fip = 0;
6607
6608 if ((iocbq->iocb.ulpCommand == CMD_CLOSE_XRI_CN) || fip)
James Smart4f774512009-05-22 14:52:35 -04006609 /*
James Smartdcf2a4e2010-09-29 11:18:53 -04006610 * The link is down, or the command was ELS_FIP
6611 * so the fw does not need to send abts
James Smart4f774512009-05-22 14:52:35 -04006612 * on the wire.
6613 */
6614 bf_set(abort_cmd_ia, &wqe->abort_cmd, 1);
6615 else
6616 bf_set(abort_cmd_ia, &wqe->abort_cmd, 0);
6617 bf_set(abort_cmd_criteria, &wqe->abort_cmd, T_XRI_TAG);
James Smartf0d9bcc2010-10-22 11:07:09 -04006618 /* word5 iocb=CONTEXT_TAG|IO_TAG wqe=reserved */
6619 wqe->abort_cmd.rsrvd5 = 0;
6620 bf_set(wqe_ct, &wqe->abort_cmd.wqe_com,
James Smart4f774512009-05-22 14:52:35 -04006621 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
6622 abort_tag = iocbq->iocb.un.acxri.abortIoTag;
James Smart4f774512009-05-22 14:52:35 -04006623 /*
6624 * The abort handler will send us CMD_ABORT_XRI_CN or
6625 * CMD_CLOSE_XRI_CN and the fw only accepts CMD_ABORT_XRI_CX
6626 */
James Smartf0d9bcc2010-10-22 11:07:09 -04006627 bf_set(wqe_cmnd, &wqe->abort_cmd.wqe_com, CMD_ABORT_XRI_CX);
6628 bf_set(wqe_qosd, &wqe->abort_cmd.wqe_com, 1);
6629 bf_set(wqe_lenloc, &wqe->abort_cmd.wqe_com,
6630 LPFC_WQE_LENLOC_NONE);
James Smart4f774512009-05-22 14:52:35 -04006631 cmnd = CMD_ABORT_XRI_CX;
6632 command_type = OTHER_COMMAND;
6633 xritag = 0;
6634 break;
James Smart6669f9b2009-10-02 15:16:45 -04006635 case CMD_XMIT_BLS_RSP64_CX:
6636 /* As BLS ABTS-ACC WQE is very different from other WQEs,
6637 * we re-construct this WQE here based on information in
6638 * iocbq from scratch.
6639 */
6640 memset(wqe, 0, sizeof(union lpfc_wqe));
James Smart5ffc2662009-11-18 15:39:44 -05006641 /* OX_ID is invariable to who sent ABTS to CT exchange */
James Smart6669f9b2009-10-02 15:16:45 -04006642 bf_set(xmit_bls_rsp64_oxid, &wqe->xmit_bls_rsp,
James Smart5ffc2662009-11-18 15:39:44 -05006643 bf_get(lpfc_abts_oxid, &iocbq->iocb.un.bls_acc));
6644 if (bf_get(lpfc_abts_orig, &iocbq->iocb.un.bls_acc) ==
6645 LPFC_ABTS_UNSOL_INT) {
6646 /* ABTS sent by initiator to CT exchange, the
6647 * RX_ID field will be filled with the newly
6648 * allocated responder XRI.
6649 */
6650 bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
6651 iocbq->sli4_xritag);
6652 } else {
6653 /* ABTS sent by responder to CT exchange, the
6654 * RX_ID field will be filled with the responder
6655 * RX_ID from ABTS.
6656 */
6657 bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
6658 bf_get(lpfc_abts_rxid, &iocbq->iocb.un.bls_acc));
6659 }
James Smart6669f9b2009-10-02 15:16:45 -04006660 bf_set(xmit_bls_rsp64_seqcnthi, &wqe->xmit_bls_rsp, 0xffff);
6661 bf_set(wqe_xmit_bls_pt, &wqe->xmit_bls_rsp.wqe_dest, 0x1);
6662 bf_set(wqe_ctxt_tag, &wqe->xmit_bls_rsp.wqe_com,
6663 iocbq->iocb.ulpContext);
James Smartf0d9bcc2010-10-22 11:07:09 -04006664 bf_set(wqe_qosd, &wqe->xmit_bls_rsp.wqe_com, 1);
6665 bf_set(wqe_lenloc, &wqe->xmit_bls_rsp.wqe_com,
6666 LPFC_WQE_LENLOC_NONE);
James Smart6669f9b2009-10-02 15:16:45 -04006667 /* Overwrite the pre-set comnd type with OTHER_COMMAND */
6668 command_type = OTHER_COMMAND;
6669 break;
James Smart4f774512009-05-22 14:52:35 -04006670 case CMD_XRI_ABORTED_CX:
6671 case CMD_CREATE_XRI_CR: /* Do we expect to use this? */
James Smart4f774512009-05-22 14:52:35 -04006672 case CMD_IOCB_FCP_IBIDIR64_CR: /* bidirectional xfer */
6673 case CMD_FCP_TSEND64_CX: /* Target mode send xfer-ready */
6674 case CMD_FCP_TRSP64_CX: /* Target mode rcv */
6675 case CMD_FCP_AUTO_TRSP_CX: /* Auto target rsp */
6676 default:
6677 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6678 "2014 Invalid command 0x%x\n",
6679 iocbq->iocb.ulpCommand);
6680 return IOCB_ERROR;
6681 break;
James Smart4f774512009-05-22 14:52:35 -04006682 }
James Smartf0d9bcc2010-10-22 11:07:09 -04006683 bf_set(wqe_xri_tag, &wqe->generic.wqe_com, xritag);
6684 bf_set(wqe_reqtag, &wqe->generic.wqe_com, iocbq->iotag);
6685 wqe->generic.wqe_com.abort_tag = abort_tag;
6686 bf_set(wqe_cmd_type, &wqe->generic.wqe_com, command_type);
6687 bf_set(wqe_cmnd, &wqe->generic.wqe_com, cmnd);
6688 bf_set(wqe_class, &wqe->generic.wqe_com, iocbq->iocb.ulpClass);
6689 bf_set(wqe_cqid, &wqe->generic.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
James Smart4f774512009-05-22 14:52:35 -04006690 return 0;
6691}
6692
6693/**
6694 * __lpfc_sli_issue_iocb_s4 - SLI4 device lockless ver of lpfc_sli_issue_iocb
6695 * @phba: Pointer to HBA context object.
6696 * @ring_number: SLI ring number to issue iocb on.
6697 * @piocb: Pointer to command iocb.
6698 * @flag: Flag indicating if this command can be put into txq.
6699 *
6700 * __lpfc_sli_issue_iocb_s4 is used by other functions in the driver to issue
6701 * an iocb command to an HBA with SLI-4 interface spec.
6702 *
6703 * This function is called with hbalock held. The function will return success
6704 * after it successfully submit the iocb to firmware or after adding to the
6705 * txq.
6706 **/
6707static int
6708__lpfc_sli_issue_iocb_s4(struct lpfc_hba *phba, uint32_t ring_number,
6709 struct lpfc_iocbq *piocb, uint32_t flag)
6710{
6711 struct lpfc_sglq *sglq;
James Smart4f774512009-05-22 14:52:35 -04006712 union lpfc_wqe wqe;
6713 struct lpfc_sli_ring *pring = &phba->sli.ring[ring_number];
James Smart4f774512009-05-22 14:52:35 -04006714
6715 if (piocb->sli4_xritag == NO_XRI) {
6716 if (piocb->iocb.ulpCommand == CMD_ABORT_XRI_CN ||
James Smart6669f9b2009-10-02 15:16:45 -04006717 piocb->iocb.ulpCommand == CMD_CLOSE_XRI_CN)
James Smart4f774512009-05-22 14:52:35 -04006718 sglq = NULL;
6719 else {
James Smart2a9bf3d2010-06-07 15:24:45 -04006720 if (pring->txq_cnt) {
6721 if (!(flag & SLI_IOCB_RET_IOCB)) {
6722 __lpfc_sli_ringtx_put(phba,
6723 pring, piocb);
6724 return IOCB_SUCCESS;
6725 } else {
6726 return IOCB_BUSY;
6727 }
6728 } else {
James Smart19ca7602010-11-20 23:11:55 -05006729 sglq = __lpfc_sli_get_sglq(phba, piocb);
James Smart2a9bf3d2010-06-07 15:24:45 -04006730 if (!sglq) {
6731 if (!(flag & SLI_IOCB_RET_IOCB)) {
6732 __lpfc_sli_ringtx_put(phba,
6733 pring,
6734 piocb);
6735 return IOCB_SUCCESS;
6736 } else
6737 return IOCB_BUSY;
6738 }
6739 }
James Smart4f774512009-05-22 14:52:35 -04006740 }
6741 } else if (piocb->iocb_flag & LPFC_IO_FCP) {
6742 sglq = NULL; /* These IO's already have an XRI and
6743 * a mapped sgl.
6744 */
6745 } else {
6746 /* This is a continuation of a commandi,(CX) so this
6747 * sglq is on the active list
6748 */
6749 sglq = __lpfc_get_active_sglq(phba, piocb->sli4_xritag);
6750 if (!sglq)
6751 return IOCB_ERROR;
6752 }
6753
6754 if (sglq) {
James Smart2a9bf3d2010-06-07 15:24:45 -04006755 piocb->sli4_xritag = sglq->sli4_xritag;
6756
6757 if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocb, sglq))
James Smart4f774512009-05-22 14:52:35 -04006758 return IOCB_ERROR;
6759 }
6760
6761 if (lpfc_sli4_iocb2wqe(phba, piocb, &wqe))
6762 return IOCB_ERROR;
6763
James Smart341af102010-01-26 23:07:37 -05006764 if ((piocb->iocb_flag & LPFC_IO_FCP) ||
6765 (piocb->iocb_flag & LPFC_USE_FCPWQIDX)) {
James Smart5ffc2662009-11-18 15:39:44 -05006766 /*
6767 * For FCP command IOCB, get a new WQ index to distribute
6768 * WQE across the WQsr. On the other hand, for abort IOCB,
6769 * it carries the same WQ index to the original command
6770 * IOCB.
6771 */
James Smart341af102010-01-26 23:07:37 -05006772 if (piocb->iocb_flag & LPFC_IO_FCP)
James Smart5ffc2662009-11-18 15:39:44 -05006773 piocb->fcp_wqidx = lpfc_sli4_scmd_to_wqidx_distr(phba);
6774 if (lpfc_sli4_wq_put(phba->sli4_hba.fcp_wq[piocb->fcp_wqidx],
6775 &wqe))
James Smart4f774512009-05-22 14:52:35 -04006776 return IOCB_ERROR;
6777 } else {
6778 if (lpfc_sli4_wq_put(phba->sli4_hba.els_wq, &wqe))
6779 return IOCB_ERROR;
6780 }
6781 lpfc_sli_ringtxcmpl_put(phba, pring, piocb);
6782
6783 return 0;
6784}
6785
6786/**
James Smart3772a992009-05-22 14:50:54 -04006787 * __lpfc_sli_issue_iocb - Wrapper func of lockless version for issuing iocb
6788 *
6789 * This routine wraps the actual lockless version for issusing IOCB function
6790 * pointer from the lpfc_hba struct.
6791 *
6792 * Return codes:
6793 * IOCB_ERROR - Error
6794 * IOCB_SUCCESS - Success
6795 * IOCB_BUSY - Busy
6796 **/
James Smart2a9bf3d2010-06-07 15:24:45 -04006797int
James Smart3772a992009-05-22 14:50:54 -04006798__lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
6799 struct lpfc_iocbq *piocb, uint32_t flag)
6800{
6801 return phba->__lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
6802}
6803
6804/**
6805 * lpfc_sli_api_table_setup - Set up sli api fucntion jump table
6806 * @phba: The hba struct for which this call is being executed.
6807 * @dev_grp: The HBA PCI-Device group number.
6808 *
6809 * This routine sets up the SLI interface API function jump table in @phba
6810 * struct.
6811 * Returns: 0 - success, -ENODEV - failure.
6812 **/
6813int
6814lpfc_sli_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
6815{
6816
6817 switch (dev_grp) {
6818 case LPFC_PCI_DEV_LP:
6819 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s3;
6820 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s3;
6821 break;
James Smart4f774512009-05-22 14:52:35 -04006822 case LPFC_PCI_DEV_OC:
6823 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s4;
6824 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s4;
6825 break;
James Smart3772a992009-05-22 14:50:54 -04006826 default:
6827 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6828 "1419 Invalid HBA PCI-device group: 0x%x\n",
6829 dev_grp);
6830 return -ENODEV;
6831 break;
6832 }
6833 phba->lpfc_get_iocb_from_iocbq = lpfc_get_iocb_from_iocbq;
6834 return 0;
6835}
James Smart92d7f7b2007-06-17 19:56:38 -05006836
James Smarte59058c2008-08-24 21:49:00 -04006837/**
James Smart3621a712009-04-06 18:47:14 -04006838 * lpfc_sli_issue_iocb - Wrapper function for __lpfc_sli_issue_iocb
James Smarte59058c2008-08-24 21:49:00 -04006839 * @phba: Pointer to HBA context object.
6840 * @pring: Pointer to driver SLI ring object.
6841 * @piocb: Pointer to command iocb.
6842 * @flag: Flag indicating if this command can be put into txq.
6843 *
6844 * lpfc_sli_issue_iocb is a wrapper around __lpfc_sli_issue_iocb
6845 * function. This function gets the hbalock and calls
6846 * __lpfc_sli_issue_iocb function and will return the error returned
6847 * by __lpfc_sli_issue_iocb function. This wrapper is used by
6848 * functions which do not hold hbalock.
6849 **/
James Smart92d7f7b2007-06-17 19:56:38 -05006850int
James Smart3772a992009-05-22 14:50:54 -04006851lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
James Smart92d7f7b2007-06-17 19:56:38 -05006852 struct lpfc_iocbq *piocb, uint32_t flag)
6853{
6854 unsigned long iflags;
6855 int rc;
6856
6857 spin_lock_irqsave(&phba->hbalock, iflags);
James Smart3772a992009-05-22 14:50:54 -04006858 rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
James Smart92d7f7b2007-06-17 19:56:38 -05006859 spin_unlock_irqrestore(&phba->hbalock, iflags);
6860
6861 return rc;
6862}
6863
James Smarte59058c2008-08-24 21:49:00 -04006864/**
James Smart3621a712009-04-06 18:47:14 -04006865 * lpfc_extra_ring_setup - Extra ring setup function
James Smarte59058c2008-08-24 21:49:00 -04006866 * @phba: Pointer to HBA context object.
6867 *
6868 * This function is called while driver attaches with the
6869 * HBA to setup the extra ring. The extra ring is used
6870 * only when driver needs to support target mode functionality
6871 * or IP over FC functionalities.
6872 *
6873 * This function is called with no lock held.
6874 **/
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05006875static int
6876lpfc_extra_ring_setup( struct lpfc_hba *phba)
6877{
6878 struct lpfc_sli *psli;
6879 struct lpfc_sli_ring *pring;
6880
6881 psli = &phba->sli;
6882
6883 /* Adjust cmd/rsp ring iocb entries more evenly */
James Smarta4bc3372006-12-02 13:34:16 -05006884
6885 /* Take some away from the FCP ring */
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05006886 pring = &psli->ring[psli->fcp_ring];
6887 pring->numCiocb -= SLI2_IOCB_CMD_R1XTRA_ENTRIES;
6888 pring->numRiocb -= SLI2_IOCB_RSP_R1XTRA_ENTRIES;
6889 pring->numCiocb -= SLI2_IOCB_CMD_R3XTRA_ENTRIES;
6890 pring->numRiocb -= SLI2_IOCB_RSP_R3XTRA_ENTRIES;
6891
James Smarta4bc3372006-12-02 13:34:16 -05006892 /* and give them to the extra ring */
6893 pring = &psli->ring[psli->extra_ring];
6894
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05006895 pring->numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
6896 pring->numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
6897 pring->numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
6898 pring->numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
6899
6900 /* Setup default profile for this ring */
6901 pring->iotag_max = 4096;
6902 pring->num_mask = 1;
6903 pring->prt[0].profile = 0; /* Mask 0 */
James Smarta4bc3372006-12-02 13:34:16 -05006904 pring->prt[0].rctl = phba->cfg_multi_ring_rctl;
6905 pring->prt[0].type = phba->cfg_multi_ring_type;
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05006906 pring->prt[0].lpfc_sli_rcv_unsol_event = NULL;
6907 return 0;
6908}
6909
James Smarte59058c2008-08-24 21:49:00 -04006910/**
James Smart3621a712009-04-06 18:47:14 -04006911 * lpfc_sli_async_event_handler - ASYNC iocb handler function
James Smarte59058c2008-08-24 21:49:00 -04006912 * @phba: Pointer to HBA context object.
6913 * @pring: Pointer to driver SLI ring object.
6914 * @iocbq: Pointer to iocb object.
6915 *
6916 * This function is called by the slow ring event handler
6917 * function when there is an ASYNC event iocb in the ring.
6918 * This function is called with no lock held.
6919 * Currently this function handles only temperature related
6920 * ASYNC events. The function decodes the temperature sensor
6921 * event message and posts events for the management applications.
6922 **/
James Smart98c9ea52007-10-27 13:37:33 -04006923static void
James Smart57127f12007-10-27 13:37:05 -04006924lpfc_sli_async_event_handler(struct lpfc_hba * phba,
6925 struct lpfc_sli_ring * pring, struct lpfc_iocbq * iocbq)
6926{
6927 IOCB_t *icmd;
6928 uint16_t evt_code;
6929 uint16_t temp;
6930 struct temp_event temp_event_data;
6931 struct Scsi_Host *shost;
James Smarta257bf92009-04-06 18:48:10 -04006932 uint32_t *iocb_w;
James Smart57127f12007-10-27 13:37:05 -04006933
6934 icmd = &iocbq->iocb;
6935 evt_code = icmd->un.asyncstat.evt_code;
6936 temp = icmd->ulpContext;
6937
6938 if ((evt_code != ASYNC_TEMP_WARN) &&
6939 (evt_code != ASYNC_TEMP_SAFE)) {
James Smarta257bf92009-04-06 18:48:10 -04006940 iocb_w = (uint32_t *) icmd;
James Smart57127f12007-10-27 13:37:05 -04006941 lpfc_printf_log(phba,
6942 KERN_ERR,
6943 LOG_SLI,
James Smart76bb24e2007-10-27 13:38:00 -04006944 "0346 Ring %d handler: unexpected ASYNC_STATUS"
James Smarte4e74272009-07-19 10:01:38 -04006945 " evt_code 0x%x\n"
James Smarta257bf92009-04-06 18:48:10 -04006946 "W0 0x%08x W1 0x%08x W2 0x%08x W3 0x%08x\n"
6947 "W4 0x%08x W5 0x%08x W6 0x%08x W7 0x%08x\n"
6948 "W8 0x%08x W9 0x%08x W10 0x%08x W11 0x%08x\n"
6949 "W12 0x%08x W13 0x%08x W14 0x%08x W15 0x%08x\n",
James Smart57127f12007-10-27 13:37:05 -04006950 pring->ringno,
James Smarta257bf92009-04-06 18:48:10 -04006951 icmd->un.asyncstat.evt_code,
6952 iocb_w[0], iocb_w[1], iocb_w[2], iocb_w[3],
6953 iocb_w[4], iocb_w[5], iocb_w[6], iocb_w[7],
6954 iocb_w[8], iocb_w[9], iocb_w[10], iocb_w[11],
6955 iocb_w[12], iocb_w[13], iocb_w[14], iocb_w[15]);
6956
James Smart57127f12007-10-27 13:37:05 -04006957 return;
6958 }
6959 temp_event_data.data = (uint32_t)temp;
6960 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
6961 if (evt_code == ASYNC_TEMP_WARN) {
6962 temp_event_data.event_code = LPFC_THRESHOLD_TEMP;
6963 lpfc_printf_log(phba,
James Smart09372822008-01-11 01:52:54 -05006964 KERN_ERR,
James Smart57127f12007-10-27 13:37:05 -04006965 LOG_TEMP,
James Smart76bb24e2007-10-27 13:38:00 -04006966 "0347 Adapter is very hot, please take "
James Smart57127f12007-10-27 13:37:05 -04006967 "corrective action. temperature : %d Celsius\n",
6968 temp);
6969 }
6970 if (evt_code == ASYNC_TEMP_SAFE) {
6971 temp_event_data.event_code = LPFC_NORMAL_TEMP;
6972 lpfc_printf_log(phba,
James Smart09372822008-01-11 01:52:54 -05006973 KERN_ERR,
James Smart57127f12007-10-27 13:37:05 -04006974 LOG_TEMP,
6975 "0340 Adapter temperature is OK now. "
6976 "temperature : %d Celsius\n",
6977 temp);
6978 }
6979
6980 /* Send temperature change event to applications */
6981 shost = lpfc_shost_from_vport(phba->pport);
6982 fc_host_post_vendor_event(shost, fc_get_event_number(),
6983 sizeof(temp_event_data), (char *) &temp_event_data,
James Smartddcc50f2008-12-04 22:38:46 -05006984 LPFC_NL_VENDOR_ID);
James Smart57127f12007-10-27 13:37:05 -04006985
6986}
6987
6988
James Smarte59058c2008-08-24 21:49:00 -04006989/**
James Smart3621a712009-04-06 18:47:14 -04006990 * lpfc_sli_setup - SLI ring setup function
James Smarte59058c2008-08-24 21:49:00 -04006991 * @phba: Pointer to HBA context object.
6992 *
6993 * lpfc_sli_setup sets up rings of the SLI interface with
6994 * number of iocbs per ring and iotags. This function is
6995 * called while driver attach to the HBA and before the
6996 * interrupts are enabled. So there is no need for locking.
6997 *
6998 * This function always returns 0.
6999 **/
dea31012005-04-17 16:05:31 -05007000int
7001lpfc_sli_setup(struct lpfc_hba *phba)
7002{
James Smarted957682007-06-17 19:56:37 -05007003 int i, totiocbsize = 0;
dea31012005-04-17 16:05:31 -05007004 struct lpfc_sli *psli = &phba->sli;
7005 struct lpfc_sli_ring *pring;
7006
7007 psli->num_rings = MAX_CONFIGURED_RINGS;
7008 psli->sli_flag = 0;
7009 psli->fcp_ring = LPFC_FCP_RING;
7010 psli->next_ring = LPFC_FCP_NEXT_RING;
James Smarta4bc3372006-12-02 13:34:16 -05007011 psli->extra_ring = LPFC_EXTRA_RING;
dea31012005-04-17 16:05:31 -05007012
James Bottomley604a3e32005-10-29 10:28:33 -05007013 psli->iocbq_lookup = NULL;
7014 psli->iocbq_lookup_len = 0;
7015 psli->last_iotag = 0;
7016
dea31012005-04-17 16:05:31 -05007017 for (i = 0; i < psli->num_rings; i++) {
7018 pring = &psli->ring[i];
7019 switch (i) {
7020 case LPFC_FCP_RING: /* ring 0 - FCP */
7021 /* numCiocb and numRiocb are used in config_port */
7022 pring->numCiocb = SLI2_IOCB_CMD_R0_ENTRIES;
7023 pring->numRiocb = SLI2_IOCB_RSP_R0_ENTRIES;
7024 pring->numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
7025 pring->numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
7026 pring->numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
7027 pring->numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
James Smarted957682007-06-17 19:56:37 -05007028 pring->sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05007029 SLI3_IOCB_CMD_SIZE :
7030 SLI2_IOCB_CMD_SIZE;
James Smarted957682007-06-17 19:56:37 -05007031 pring->sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05007032 SLI3_IOCB_RSP_SIZE :
7033 SLI2_IOCB_RSP_SIZE;
dea31012005-04-17 16:05:31 -05007034 pring->iotag_ctr = 0;
7035 pring->iotag_max =
James Smart92d7f7b2007-06-17 19:56:38 -05007036 (phba->cfg_hba_queue_depth * 2);
dea31012005-04-17 16:05:31 -05007037 pring->fast_iotag = pring->iotag_max;
7038 pring->num_mask = 0;
7039 break;
James Smarta4bc3372006-12-02 13:34:16 -05007040 case LPFC_EXTRA_RING: /* ring 1 - EXTRA */
dea31012005-04-17 16:05:31 -05007041 /* numCiocb and numRiocb are used in config_port */
7042 pring->numCiocb = SLI2_IOCB_CMD_R1_ENTRIES;
7043 pring->numRiocb = SLI2_IOCB_RSP_R1_ENTRIES;
James Smarted957682007-06-17 19:56:37 -05007044 pring->sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05007045 SLI3_IOCB_CMD_SIZE :
7046 SLI2_IOCB_CMD_SIZE;
James Smarted957682007-06-17 19:56:37 -05007047 pring->sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05007048 SLI3_IOCB_RSP_SIZE :
7049 SLI2_IOCB_RSP_SIZE;
James Smart2e0fef82007-06-17 19:56:36 -05007050 pring->iotag_max = phba->cfg_hba_queue_depth;
dea31012005-04-17 16:05:31 -05007051 pring->num_mask = 0;
7052 break;
7053 case LPFC_ELS_RING: /* ring 2 - ELS / CT */
7054 /* numCiocb and numRiocb are used in config_port */
7055 pring->numCiocb = SLI2_IOCB_CMD_R2_ENTRIES;
7056 pring->numRiocb = SLI2_IOCB_RSP_R2_ENTRIES;
James Smarted957682007-06-17 19:56:37 -05007057 pring->sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05007058 SLI3_IOCB_CMD_SIZE :
7059 SLI2_IOCB_CMD_SIZE;
James Smarted957682007-06-17 19:56:37 -05007060 pring->sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05007061 SLI3_IOCB_RSP_SIZE :
7062 SLI2_IOCB_RSP_SIZE;
dea31012005-04-17 16:05:31 -05007063 pring->fast_iotag = 0;
7064 pring->iotag_ctr = 0;
7065 pring->iotag_max = 4096;
James Smart57127f12007-10-27 13:37:05 -04007066 pring->lpfc_sli_rcv_async_status =
7067 lpfc_sli_async_event_handler;
James Smart6669f9b2009-10-02 15:16:45 -04007068 pring->num_mask = LPFC_MAX_RING_MASK;
dea31012005-04-17 16:05:31 -05007069 pring->prt[0].profile = 0; /* Mask 0 */
James Smart6a9c52c2009-10-02 15:16:51 -04007070 pring->prt[0].rctl = FC_RCTL_ELS_REQ;
7071 pring->prt[0].type = FC_TYPE_ELS;
dea31012005-04-17 16:05:31 -05007072 pring->prt[0].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05007073 lpfc_els_unsol_event;
dea31012005-04-17 16:05:31 -05007074 pring->prt[1].profile = 0; /* Mask 1 */
James Smart6a9c52c2009-10-02 15:16:51 -04007075 pring->prt[1].rctl = FC_RCTL_ELS_REP;
7076 pring->prt[1].type = FC_TYPE_ELS;
dea31012005-04-17 16:05:31 -05007077 pring->prt[1].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05007078 lpfc_els_unsol_event;
dea31012005-04-17 16:05:31 -05007079 pring->prt[2].profile = 0; /* Mask 2 */
7080 /* NameServer Inquiry */
James Smart6a9c52c2009-10-02 15:16:51 -04007081 pring->prt[2].rctl = FC_RCTL_DD_UNSOL_CTL;
dea31012005-04-17 16:05:31 -05007082 /* NameServer */
James Smart6a9c52c2009-10-02 15:16:51 -04007083 pring->prt[2].type = FC_TYPE_CT;
dea31012005-04-17 16:05:31 -05007084 pring->prt[2].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05007085 lpfc_ct_unsol_event;
dea31012005-04-17 16:05:31 -05007086 pring->prt[3].profile = 0; /* Mask 3 */
7087 /* NameServer response */
James Smart6a9c52c2009-10-02 15:16:51 -04007088 pring->prt[3].rctl = FC_RCTL_DD_SOL_CTL;
dea31012005-04-17 16:05:31 -05007089 /* NameServer */
James Smart6a9c52c2009-10-02 15:16:51 -04007090 pring->prt[3].type = FC_TYPE_CT;
dea31012005-04-17 16:05:31 -05007091 pring->prt[3].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05007092 lpfc_ct_unsol_event;
James Smart6669f9b2009-10-02 15:16:45 -04007093 /* abort unsolicited sequence */
7094 pring->prt[4].profile = 0; /* Mask 4 */
7095 pring->prt[4].rctl = FC_RCTL_BA_ABTS;
7096 pring->prt[4].type = FC_TYPE_BLS;
7097 pring->prt[4].lpfc_sli_rcv_unsol_event =
7098 lpfc_sli4_ct_abort_unsol_event;
dea31012005-04-17 16:05:31 -05007099 break;
7100 }
James Smarted957682007-06-17 19:56:37 -05007101 totiocbsize += (pring->numCiocb * pring->sizeCiocb) +
James Smart92d7f7b2007-06-17 19:56:38 -05007102 (pring->numRiocb * pring->sizeRiocb);
dea31012005-04-17 16:05:31 -05007103 }
James Smarted957682007-06-17 19:56:37 -05007104 if (totiocbsize > MAX_SLIM_IOCB_SIZE) {
dea31012005-04-17 16:05:31 -05007105 /* Too many cmd / rsp ring entries in SLI2 SLIM */
James Smarte8b62012007-08-02 11:10:09 -04007106 printk(KERN_ERR "%d:0462 Too many cmd / rsp ring entries in "
7107 "SLI2 SLIM Data: x%x x%lx\n",
7108 phba->brd_no, totiocbsize,
7109 (unsigned long) MAX_SLIM_IOCB_SIZE);
dea31012005-04-17 16:05:31 -05007110 }
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05007111 if (phba->cfg_multi_ring_support == 2)
7112 lpfc_extra_ring_setup(phba);
dea31012005-04-17 16:05:31 -05007113
7114 return 0;
7115}
7116
James Smarte59058c2008-08-24 21:49:00 -04007117/**
James Smart3621a712009-04-06 18:47:14 -04007118 * lpfc_sli_queue_setup - Queue initialization function
James Smarte59058c2008-08-24 21:49:00 -04007119 * @phba: Pointer to HBA context object.
7120 *
7121 * lpfc_sli_queue_setup sets up mailbox queues and iocb queues for each
7122 * ring. This function also initializes ring indices of each ring.
7123 * This function is called during the initialization of the SLI
7124 * interface of an HBA.
7125 * This function is called with no lock held and always returns
7126 * 1.
7127 **/
dea31012005-04-17 16:05:31 -05007128int
James Smart2e0fef82007-06-17 19:56:36 -05007129lpfc_sli_queue_setup(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05007130{
7131 struct lpfc_sli *psli;
7132 struct lpfc_sli_ring *pring;
James Bottomley604a3e32005-10-29 10:28:33 -05007133 int i;
dea31012005-04-17 16:05:31 -05007134
7135 psli = &phba->sli;
James Smart2e0fef82007-06-17 19:56:36 -05007136 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05007137 INIT_LIST_HEAD(&psli->mboxq);
James Smart92d7f7b2007-06-17 19:56:38 -05007138 INIT_LIST_HEAD(&psli->mboxq_cmpl);
dea31012005-04-17 16:05:31 -05007139 /* Initialize list headers for txq and txcmplq as double linked lists */
7140 for (i = 0; i < psli->num_rings; i++) {
7141 pring = &psli->ring[i];
7142 pring->ringno = i;
7143 pring->next_cmdidx = 0;
7144 pring->local_getidx = 0;
7145 pring->cmdidx = 0;
7146 INIT_LIST_HEAD(&pring->txq);
7147 INIT_LIST_HEAD(&pring->txcmplq);
7148 INIT_LIST_HEAD(&pring->iocb_continueq);
James Smart9c2face2008-01-11 01:53:18 -05007149 INIT_LIST_HEAD(&pring->iocb_continue_saveq);
dea31012005-04-17 16:05:31 -05007150 INIT_LIST_HEAD(&pring->postbufq);
dea31012005-04-17 16:05:31 -05007151 }
James Smart2e0fef82007-06-17 19:56:36 -05007152 spin_unlock_irq(&phba->hbalock);
7153 return 1;
dea31012005-04-17 16:05:31 -05007154}
7155
James Smarte59058c2008-08-24 21:49:00 -04007156/**
James Smart04c68492009-05-22 14:52:52 -04007157 * lpfc_sli_mbox_sys_flush - Flush mailbox command sub-system
7158 * @phba: Pointer to HBA context object.
7159 *
7160 * This routine flushes the mailbox command subsystem. It will unconditionally
7161 * flush all the mailbox commands in the three possible stages in the mailbox
7162 * command sub-system: pending mailbox command queue; the outstanding mailbox
7163 * command; and completed mailbox command queue. It is caller's responsibility
7164 * to make sure that the driver is in the proper state to flush the mailbox
7165 * command sub-system. Namely, the posting of mailbox commands into the
7166 * pending mailbox command queue from the various clients must be stopped;
7167 * either the HBA is in a state that it will never works on the outstanding
7168 * mailbox command (such as in EEH or ERATT conditions) or the outstanding
7169 * mailbox command has been completed.
7170 **/
7171static void
7172lpfc_sli_mbox_sys_flush(struct lpfc_hba *phba)
7173{
7174 LIST_HEAD(completions);
7175 struct lpfc_sli *psli = &phba->sli;
7176 LPFC_MBOXQ_t *pmb;
7177 unsigned long iflag;
7178
7179 /* Flush all the mailbox commands in the mbox system */
7180 spin_lock_irqsave(&phba->hbalock, iflag);
7181 /* The pending mailbox command queue */
7182 list_splice_init(&phba->sli.mboxq, &completions);
7183 /* The outstanding active mailbox command */
7184 if (psli->mbox_active) {
7185 list_add_tail(&psli->mbox_active->list, &completions);
7186 psli->mbox_active = NULL;
7187 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
7188 }
7189 /* The completed mailbox command queue */
7190 list_splice_init(&phba->sli.mboxq_cmpl, &completions);
7191 spin_unlock_irqrestore(&phba->hbalock, iflag);
7192
7193 /* Return all flushed mailbox commands with MBX_NOT_FINISHED status */
7194 while (!list_empty(&completions)) {
7195 list_remove_head(&completions, pmb, LPFC_MBOXQ_t, list);
7196 pmb->u.mb.mbxStatus = MBX_NOT_FINISHED;
7197 if (pmb->mbox_cmpl)
7198 pmb->mbox_cmpl(phba, pmb);
7199 }
7200}
7201
7202/**
James Smart3621a712009-04-06 18:47:14 -04007203 * lpfc_sli_host_down - Vport cleanup function
James Smarte59058c2008-08-24 21:49:00 -04007204 * @vport: Pointer to virtual port object.
7205 *
7206 * lpfc_sli_host_down is called to clean up the resources
7207 * associated with a vport before destroying virtual
7208 * port data structures.
7209 * This function does following operations:
7210 * - Free discovery resources associated with this virtual
7211 * port.
7212 * - Free iocbs associated with this virtual port in
7213 * the txq.
7214 * - Send abort for all iocb commands associated with this
7215 * vport in txcmplq.
7216 *
7217 * This function is called with no lock held and always returns 1.
7218 **/
dea31012005-04-17 16:05:31 -05007219int
James Smart92d7f7b2007-06-17 19:56:38 -05007220lpfc_sli_host_down(struct lpfc_vport *vport)
7221{
James Smart858c9f62007-06-17 19:56:39 -05007222 LIST_HEAD(completions);
James Smart92d7f7b2007-06-17 19:56:38 -05007223 struct lpfc_hba *phba = vport->phba;
7224 struct lpfc_sli *psli = &phba->sli;
7225 struct lpfc_sli_ring *pring;
7226 struct lpfc_iocbq *iocb, *next_iocb;
James Smart92d7f7b2007-06-17 19:56:38 -05007227 int i;
7228 unsigned long flags = 0;
7229 uint16_t prev_pring_flag;
7230
7231 lpfc_cleanup_discovery_resources(vport);
7232
7233 spin_lock_irqsave(&phba->hbalock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -05007234 for (i = 0; i < psli->num_rings; i++) {
7235 pring = &psli->ring[i];
7236 prev_pring_flag = pring->flag;
James Smart5e9d9b82008-06-14 22:52:53 -04007237 /* Only slow rings */
7238 if (pring->ringno == LPFC_ELS_RING) {
James Smart858c9f62007-06-17 19:56:39 -05007239 pring->flag |= LPFC_DEFERRED_RING_EVENT;
James Smart5e9d9b82008-06-14 22:52:53 -04007240 /* Set the lpfc data pending flag */
7241 set_bit(LPFC_DATA_READY, &phba->data_flags);
7242 }
James Smart92d7f7b2007-06-17 19:56:38 -05007243 /*
7244 * Error everything on the txq since these iocbs have not been
7245 * given to the FW yet.
7246 */
James Smart92d7f7b2007-06-17 19:56:38 -05007247 list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
7248 if (iocb->vport != vport)
7249 continue;
James Smart858c9f62007-06-17 19:56:39 -05007250 list_move_tail(&iocb->list, &completions);
James Smart92d7f7b2007-06-17 19:56:38 -05007251 pring->txq_cnt--;
James Smart92d7f7b2007-06-17 19:56:38 -05007252 }
7253
7254 /* Next issue ABTS for everything on the txcmplq */
7255 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq,
7256 list) {
7257 if (iocb->vport != vport)
7258 continue;
7259 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
7260 }
7261
7262 pring->flag = prev_pring_flag;
7263 }
7264
7265 spin_unlock_irqrestore(&phba->hbalock, flags);
7266
James Smarta257bf92009-04-06 18:48:10 -04007267 /* Cancel all the IOCBs from the completions list */
7268 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
7269 IOERR_SLI_DOWN);
James Smart92d7f7b2007-06-17 19:56:38 -05007270 return 1;
7271}
7272
James Smarte59058c2008-08-24 21:49:00 -04007273/**
James Smart3621a712009-04-06 18:47:14 -04007274 * lpfc_sli_hba_down - Resource cleanup function for the HBA
James Smarte59058c2008-08-24 21:49:00 -04007275 * @phba: Pointer to HBA context object.
7276 *
7277 * This function cleans up all iocb, buffers, mailbox commands
7278 * while shutting down the HBA. This function is called with no
7279 * lock held and always returns 1.
7280 * This function does the following to cleanup driver resources:
7281 * - Free discovery resources for each virtual port
7282 * - Cleanup any pending fabric iocbs
7283 * - Iterate through the iocb txq and free each entry
7284 * in the list.
7285 * - Free up any buffer posted to the HBA
7286 * - Free mailbox commands in the mailbox queue.
7287 **/
James Smart92d7f7b2007-06-17 19:56:38 -05007288int
James Smart2e0fef82007-06-17 19:56:36 -05007289lpfc_sli_hba_down(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05007290{
James Smart2534ba72007-04-25 09:52:20 -04007291 LIST_HEAD(completions);
James Smart2e0fef82007-06-17 19:56:36 -05007292 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05007293 struct lpfc_sli_ring *pring;
James Smart0ff10d42008-01-11 01:52:36 -05007294 struct lpfc_dmabuf *buf_ptr;
dea31012005-04-17 16:05:31 -05007295 unsigned long flags = 0;
James Smart04c68492009-05-22 14:52:52 -04007296 int i;
7297
7298 /* Shutdown the mailbox command sub-system */
7299 lpfc_sli_mbox_sys_shutdown(phba);
dea31012005-04-17 16:05:31 -05007300
dea31012005-04-17 16:05:31 -05007301 lpfc_hba_down_prep(phba);
7302
James Smart92d7f7b2007-06-17 19:56:38 -05007303 lpfc_fabric_abort_hba(phba);
7304
James Smart2e0fef82007-06-17 19:56:36 -05007305 spin_lock_irqsave(&phba->hbalock, flags);
dea31012005-04-17 16:05:31 -05007306 for (i = 0; i < psli->num_rings; i++) {
7307 pring = &psli->ring[i];
James Smart5e9d9b82008-06-14 22:52:53 -04007308 /* Only slow rings */
7309 if (pring->ringno == LPFC_ELS_RING) {
James Smart858c9f62007-06-17 19:56:39 -05007310 pring->flag |= LPFC_DEFERRED_RING_EVENT;
James Smart5e9d9b82008-06-14 22:52:53 -04007311 /* Set the lpfc data pending flag */
7312 set_bit(LPFC_DATA_READY, &phba->data_flags);
7313 }
dea31012005-04-17 16:05:31 -05007314
7315 /*
7316 * Error everything on the txq since these iocbs have not been
7317 * given to the FW yet.
7318 */
James Smart2534ba72007-04-25 09:52:20 -04007319 list_splice_init(&pring->txq, &completions);
dea31012005-04-17 16:05:31 -05007320 pring->txq_cnt = 0;
7321
dea31012005-04-17 16:05:31 -05007322 }
James Smart2e0fef82007-06-17 19:56:36 -05007323 spin_unlock_irqrestore(&phba->hbalock, flags);
dea31012005-04-17 16:05:31 -05007324
James Smarta257bf92009-04-06 18:48:10 -04007325 /* Cancel all the IOCBs from the completions list */
7326 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
7327 IOERR_SLI_DOWN);
James Smart2534ba72007-04-25 09:52:20 -04007328
James Smart0ff10d42008-01-11 01:52:36 -05007329 spin_lock_irqsave(&phba->hbalock, flags);
7330 list_splice_init(&phba->elsbuf, &completions);
7331 phba->elsbuf_cnt = 0;
7332 phba->elsbuf_prev_cnt = 0;
7333 spin_unlock_irqrestore(&phba->hbalock, flags);
7334
7335 while (!list_empty(&completions)) {
7336 list_remove_head(&completions, buf_ptr,
7337 struct lpfc_dmabuf, list);
7338 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
7339 kfree(buf_ptr);
7340 }
7341
dea31012005-04-17 16:05:31 -05007342 /* Return any active mbox cmds */
7343 del_timer_sync(&psli->mbox_tmo);
James Smart92d7f7b2007-06-17 19:56:38 -05007344
James Smartda0436e2009-05-22 14:51:39 -04007345 spin_lock_irqsave(&phba->pport->work_port_lock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -05007346 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
James Smartda0436e2009-05-22 14:51:39 -04007347 spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -05007348
James Smartda0436e2009-05-22 14:51:39 -04007349 return 1;
7350}
James Smart92d7f7b2007-06-17 19:56:38 -05007351
James Smartda0436e2009-05-22 14:51:39 -04007352/**
James Smart3621a712009-04-06 18:47:14 -04007353 * lpfc_sli_pcimem_bcopy - SLI memory copy function
James Smarte59058c2008-08-24 21:49:00 -04007354 * @srcp: Source memory pointer.
7355 * @destp: Destination memory pointer.
7356 * @cnt: Number of words required to be copied.
7357 *
7358 * This function is used for copying data between driver memory
7359 * and the SLI memory. This function also changes the endianness
7360 * of each word if native endianness is different from SLI
7361 * endianness. This function can be called with or without
7362 * lock.
7363 **/
dea31012005-04-17 16:05:31 -05007364void
7365lpfc_sli_pcimem_bcopy(void *srcp, void *destp, uint32_t cnt)
7366{
7367 uint32_t *src = srcp;
7368 uint32_t *dest = destp;
7369 uint32_t ldata;
7370 int i;
7371
7372 for (i = 0; i < (int)cnt; i += sizeof (uint32_t)) {
7373 ldata = *src;
7374 ldata = le32_to_cpu(ldata);
7375 *dest = ldata;
7376 src++;
7377 dest++;
7378 }
7379}
7380
James Smarte59058c2008-08-24 21:49:00 -04007381
7382/**
James Smarta0c87cb2009-07-19 10:01:10 -04007383 * lpfc_sli_bemem_bcopy - SLI memory copy function
7384 * @srcp: Source memory pointer.
7385 * @destp: Destination memory pointer.
7386 * @cnt: Number of words required to be copied.
7387 *
7388 * This function is used for copying data between a data structure
7389 * with big endian representation to local endianness.
7390 * This function can be called with or without lock.
7391 **/
7392void
7393lpfc_sli_bemem_bcopy(void *srcp, void *destp, uint32_t cnt)
7394{
7395 uint32_t *src = srcp;
7396 uint32_t *dest = destp;
7397 uint32_t ldata;
7398 int i;
7399
7400 for (i = 0; i < (int)cnt; i += sizeof(uint32_t)) {
7401 ldata = *src;
7402 ldata = be32_to_cpu(ldata);
7403 *dest = ldata;
7404 src++;
7405 dest++;
7406 }
7407}
7408
7409/**
James Smart3621a712009-04-06 18:47:14 -04007410 * lpfc_sli_ringpostbuf_put - Function to add a buffer to postbufq
James Smarte59058c2008-08-24 21:49:00 -04007411 * @phba: Pointer to HBA context object.
7412 * @pring: Pointer to driver SLI ring object.
7413 * @mp: Pointer to driver buffer object.
7414 *
7415 * This function is called with no lock held.
7416 * It always return zero after adding the buffer to the postbufq
7417 * buffer list.
7418 **/
dea31012005-04-17 16:05:31 -05007419int
James Smart2e0fef82007-06-17 19:56:36 -05007420lpfc_sli_ringpostbuf_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
7421 struct lpfc_dmabuf *mp)
dea31012005-04-17 16:05:31 -05007422{
7423 /* Stick struct lpfc_dmabuf at end of postbufq so driver can look it up
7424 later */
James Smart2e0fef82007-06-17 19:56:36 -05007425 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05007426 list_add_tail(&mp->list, &pring->postbufq);
dea31012005-04-17 16:05:31 -05007427 pring->postbufq_cnt++;
James Smart2e0fef82007-06-17 19:56:36 -05007428 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05007429 return 0;
7430}
7431
James Smarte59058c2008-08-24 21:49:00 -04007432/**
James Smart3621a712009-04-06 18:47:14 -04007433 * lpfc_sli_get_buffer_tag - allocates a tag for a CMD_QUE_XRI64_CX buffer
James Smarte59058c2008-08-24 21:49:00 -04007434 * @phba: Pointer to HBA context object.
7435 *
7436 * When HBQ is enabled, buffers are searched based on tags. This function
7437 * allocates a tag for buffer posted using CMD_QUE_XRI64_CX iocb. The
7438 * tag is bit wise or-ed with QUE_BUFTAG_BIT to make sure that the tag
7439 * does not conflict with tags of buffer posted for unsolicited events.
7440 * The function returns the allocated tag. The function is called with
7441 * no locks held.
7442 **/
James Smart76bb24e2007-10-27 13:38:00 -04007443uint32_t
7444lpfc_sli_get_buffer_tag(struct lpfc_hba *phba)
7445{
7446 spin_lock_irq(&phba->hbalock);
7447 phba->buffer_tag_count++;
7448 /*
7449 * Always set the QUE_BUFTAG_BIT to distiguish between
7450 * a tag assigned by HBQ.
7451 */
7452 phba->buffer_tag_count |= QUE_BUFTAG_BIT;
7453 spin_unlock_irq(&phba->hbalock);
7454 return phba->buffer_tag_count;
7455}
7456
James Smarte59058c2008-08-24 21:49:00 -04007457/**
James Smart3621a712009-04-06 18:47:14 -04007458 * lpfc_sli_ring_taggedbuf_get - find HBQ buffer associated with given tag
James Smarte59058c2008-08-24 21:49:00 -04007459 * @phba: Pointer to HBA context object.
7460 * @pring: Pointer to driver SLI ring object.
7461 * @tag: Buffer tag.
7462 *
7463 * Buffers posted using CMD_QUE_XRI64_CX iocb are in pring->postbufq
7464 * list. After HBA DMA data to these buffers, CMD_IOCB_RET_XRI64_CX
7465 * iocb is posted to the response ring with the tag of the buffer.
7466 * This function searches the pring->postbufq list using the tag
7467 * to find buffer associated with CMD_IOCB_RET_XRI64_CX
7468 * iocb. If the buffer is found then lpfc_dmabuf object of the
7469 * buffer is returned to the caller else NULL is returned.
7470 * This function is called with no lock held.
7471 **/
James Smart76bb24e2007-10-27 13:38:00 -04007472struct lpfc_dmabuf *
7473lpfc_sli_ring_taggedbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
7474 uint32_t tag)
7475{
7476 struct lpfc_dmabuf *mp, *next_mp;
7477 struct list_head *slp = &pring->postbufq;
7478
7479 /* Search postbufq, from the begining, looking for a match on tag */
7480 spin_lock_irq(&phba->hbalock);
7481 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
7482 if (mp->buffer_tag == tag) {
7483 list_del_init(&mp->list);
7484 pring->postbufq_cnt--;
7485 spin_unlock_irq(&phba->hbalock);
7486 return mp;
7487 }
7488 }
7489
7490 spin_unlock_irq(&phba->hbalock);
7491 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartd7c255b2008-08-24 21:50:00 -04007492 "0402 Cannot find virtual addr for buffer tag on "
James Smart76bb24e2007-10-27 13:38:00 -04007493 "ring %d Data x%lx x%p x%p x%x\n",
7494 pring->ringno, (unsigned long) tag,
7495 slp->next, slp->prev, pring->postbufq_cnt);
7496
7497 return NULL;
7498}
dea31012005-04-17 16:05:31 -05007499
James Smarte59058c2008-08-24 21:49:00 -04007500/**
James Smart3621a712009-04-06 18:47:14 -04007501 * lpfc_sli_ringpostbuf_get - search buffers for unsolicited CT and ELS events
James Smarte59058c2008-08-24 21:49:00 -04007502 * @phba: Pointer to HBA context object.
7503 * @pring: Pointer to driver SLI ring object.
7504 * @phys: DMA address of the buffer.
7505 *
7506 * This function searches the buffer list using the dma_address
7507 * of unsolicited event to find the driver's lpfc_dmabuf object
7508 * corresponding to the dma_address. The function returns the
7509 * lpfc_dmabuf object if a buffer is found else it returns NULL.
7510 * This function is called by the ct and els unsolicited event
7511 * handlers to get the buffer associated with the unsolicited
7512 * event.
7513 *
7514 * This function is called with no lock held.
7515 **/
dea31012005-04-17 16:05:31 -05007516struct lpfc_dmabuf *
7517lpfc_sli_ringpostbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
7518 dma_addr_t phys)
7519{
7520 struct lpfc_dmabuf *mp, *next_mp;
7521 struct list_head *slp = &pring->postbufq;
7522
7523 /* Search postbufq, from the begining, looking for a match on phys */
James Smart2e0fef82007-06-17 19:56:36 -05007524 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05007525 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
7526 if (mp->phys == phys) {
7527 list_del_init(&mp->list);
7528 pring->postbufq_cnt--;
James Smart2e0fef82007-06-17 19:56:36 -05007529 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05007530 return mp;
7531 }
7532 }
7533
James Smart2e0fef82007-06-17 19:56:36 -05007534 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05007535 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04007536 "0410 Cannot find virtual addr for mapped buf on "
dea31012005-04-17 16:05:31 -05007537 "ring %d Data x%llx x%p x%p x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04007538 pring->ringno, (unsigned long long)phys,
dea31012005-04-17 16:05:31 -05007539 slp->next, slp->prev, pring->postbufq_cnt);
7540 return NULL;
7541}
7542
James Smarte59058c2008-08-24 21:49:00 -04007543/**
James Smart3621a712009-04-06 18:47:14 -04007544 * lpfc_sli_abort_els_cmpl - Completion handler for the els abort iocbs
James Smarte59058c2008-08-24 21:49:00 -04007545 * @phba: Pointer to HBA context object.
7546 * @cmdiocb: Pointer to driver command iocb object.
7547 * @rspiocb: Pointer to driver response iocb object.
7548 *
7549 * This function is the completion handler for the abort iocbs for
7550 * ELS commands. This function is called from the ELS ring event
7551 * handler with no lock held. This function frees memory resources
7552 * associated with the abort iocb.
7553 **/
dea31012005-04-17 16:05:31 -05007554static void
James Smart2e0fef82007-06-17 19:56:36 -05007555lpfc_sli_abort_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
7556 struct lpfc_iocbq *rspiocb)
dea31012005-04-17 16:05:31 -05007557{
James Smart2e0fef82007-06-17 19:56:36 -05007558 IOCB_t *irsp = &rspiocb->iocb;
James Smart2680eea2007-04-25 09:52:55 -04007559 uint16_t abort_iotag, abort_context;
James Smart92d7f7b2007-06-17 19:56:38 -05007560 struct lpfc_iocbq *abort_iocb;
James Smart2680eea2007-04-25 09:52:55 -04007561 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
7562
7563 abort_iocb = NULL;
James Smart2680eea2007-04-25 09:52:55 -04007564
7565 if (irsp->ulpStatus) {
7566 abort_context = cmdiocb->iocb.un.acxri.abortContextTag;
7567 abort_iotag = cmdiocb->iocb.un.acxri.abortIoTag;
7568
James Smart2e0fef82007-06-17 19:56:36 -05007569 spin_lock_irq(&phba->hbalock);
James Smart45ed1192009-10-02 15:17:02 -04007570 if (phba->sli_rev < LPFC_SLI_REV4) {
7571 if (abort_iotag != 0 &&
7572 abort_iotag <= phba->sli.last_iotag)
7573 abort_iocb =
7574 phba->sli.iocbq_lookup[abort_iotag];
7575 } else
7576 /* For sli4 the abort_tag is the XRI,
7577 * so the abort routine puts the iotag of the iocb
7578 * being aborted in the context field of the abort
7579 * IOCB.
7580 */
7581 abort_iocb = phba->sli.iocbq_lookup[abort_context];
James Smart2680eea2007-04-25 09:52:55 -04007582
James Smart2680eea2007-04-25 09:52:55 -04007583 /*
James Smart58da1ff2008-04-07 10:15:56 -04007584 * If the iocb is not found in Firmware queue the iocb
7585 * might have completed already. Do not free it again.
7586 */
James Smart9b379602008-04-07 10:16:00 -04007587 if (irsp->ulpStatus == IOSTAT_LOCAL_REJECT) {
James Smart45ed1192009-10-02 15:17:02 -04007588 if (irsp->un.ulpWord[4] != IOERR_NO_XRI) {
7589 spin_unlock_irq(&phba->hbalock);
7590 lpfc_sli_release_iocbq(phba, cmdiocb);
7591 return;
7592 }
7593 /* For SLI4 the ulpContext field for abort IOCB
7594 * holds the iotag of the IOCB being aborted so
7595 * the local abort_context needs to be reset to
7596 * match the aborted IOCBs ulpContext.
7597 */
7598 if (abort_iocb && phba->sli_rev == LPFC_SLI_REV4)
7599 abort_context = abort_iocb->iocb.ulpContext;
James Smart58da1ff2008-04-07 10:15:56 -04007600 }
James Smart2a9bf3d2010-06-07 15:24:45 -04007601
7602 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS | LOG_SLI,
7603 "0327 Cannot abort els iocb %p "
7604 "with tag %x context %x, abort status %x, "
7605 "abort code %x\n",
7606 abort_iocb, abort_iotag, abort_context,
7607 irsp->ulpStatus, irsp->un.ulpWord[4]);
James Smart58da1ff2008-04-07 10:15:56 -04007608 /*
James Smart2680eea2007-04-25 09:52:55 -04007609 * make sure we have the right iocbq before taking it
7610 * off the txcmplq and try to call completion routine.
7611 */
James Smart2e0fef82007-06-17 19:56:36 -05007612 if (!abort_iocb ||
7613 abort_iocb->iocb.ulpContext != abort_context ||
7614 (abort_iocb->iocb_flag & LPFC_DRIVER_ABORTED) == 0)
7615 spin_unlock_irq(&phba->hbalock);
James Smart341af102010-01-26 23:07:37 -05007616 else if (phba->sli_rev < LPFC_SLI_REV4) {
7617 /*
7618 * leave the SLI4 aborted command on the txcmplq
7619 * list and the command complete WCQE's XB bit
7620 * will tell whether the SGL (XRI) can be released
7621 * immediately or to the aborted SGL list for the
7622 * following abort XRI from the HBA.
7623 */
James Smart92d7f7b2007-06-17 19:56:38 -05007624 list_del_init(&abort_iocb->list);
James Smart2a9bf3d2010-06-07 15:24:45 -04007625 if (abort_iocb->iocb_flag & LPFC_IO_ON_Q) {
7626 abort_iocb->iocb_flag &= ~LPFC_IO_ON_Q;
7627 pring->txcmplq_cnt--;
7628 }
James Smart2680eea2007-04-25 09:52:55 -04007629
James Smart0ff10d42008-01-11 01:52:36 -05007630 /* Firmware could still be in progress of DMAing
7631 * payload, so don't free data buffer till after
7632 * a hbeat.
7633 */
7634 abort_iocb->iocb_flag |= LPFC_DELAY_MEM_FREE;
James Smart92d7f7b2007-06-17 19:56:38 -05007635 abort_iocb->iocb_flag &= ~LPFC_DRIVER_ABORTED;
James Smart341af102010-01-26 23:07:37 -05007636 spin_unlock_irq(&phba->hbalock);
7637
James Smart92d7f7b2007-06-17 19:56:38 -05007638 abort_iocb->iocb.ulpStatus = IOSTAT_LOCAL_REJECT;
James Smart341af102010-01-26 23:07:37 -05007639 abort_iocb->iocb.un.ulpWord[4] = IOERR_ABORT_REQUESTED;
James Smart92d7f7b2007-06-17 19:56:38 -05007640 (abort_iocb->iocb_cmpl)(phba, abort_iocb, abort_iocb);
James Smart49198b32010-04-06 15:04:33 -04007641 } else
7642 spin_unlock_irq(&phba->hbalock);
James Smart2680eea2007-04-25 09:52:55 -04007643 }
7644
James Bottomley604a3e32005-10-29 10:28:33 -05007645 lpfc_sli_release_iocbq(phba, cmdiocb);
dea31012005-04-17 16:05:31 -05007646 return;
7647}
7648
James Smarte59058c2008-08-24 21:49:00 -04007649/**
James Smart3621a712009-04-06 18:47:14 -04007650 * lpfc_ignore_els_cmpl - Completion handler for aborted ELS command
James Smarte59058c2008-08-24 21:49:00 -04007651 * @phba: Pointer to HBA context object.
7652 * @cmdiocb: Pointer to driver command iocb object.
7653 * @rspiocb: Pointer to driver response iocb object.
7654 *
7655 * The function is called from SLI ring event handler with no
7656 * lock held. This function is the completion handler for ELS commands
7657 * which are aborted. The function frees memory resources used for
7658 * the aborted ELS commands.
7659 **/
James Smart92d7f7b2007-06-17 19:56:38 -05007660static void
7661lpfc_ignore_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
7662 struct lpfc_iocbq *rspiocb)
7663{
7664 IOCB_t *irsp = &rspiocb->iocb;
7665
7666 /* ELS cmd tag <ulpIoTag> completes */
7667 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
James Smartd7c255b2008-08-24 21:50:00 -04007668 "0139 Ignoring ELS cmd tag x%x completion Data: "
James Smart92d7f7b2007-06-17 19:56:38 -05007669 "x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04007670 irsp->ulpIoTag, irsp->ulpStatus,
James Smart92d7f7b2007-06-17 19:56:38 -05007671 irsp->un.ulpWord[4], irsp->ulpTimeout);
James Smart858c9f62007-06-17 19:56:39 -05007672 if (cmdiocb->iocb.ulpCommand == CMD_GEN_REQUEST64_CR)
7673 lpfc_ct_free_iocb(phba, cmdiocb);
7674 else
7675 lpfc_els_free_iocb(phba, cmdiocb);
James Smart92d7f7b2007-06-17 19:56:38 -05007676 return;
7677}
7678
James Smarte59058c2008-08-24 21:49:00 -04007679/**
James Smart5af5eee2010-10-22 11:06:38 -04007680 * lpfc_sli_abort_iotag_issue - Issue abort for a command iocb
James Smarte59058c2008-08-24 21:49:00 -04007681 * @phba: Pointer to HBA context object.
7682 * @pring: Pointer to driver SLI ring object.
7683 * @cmdiocb: Pointer to driver command iocb object.
7684 *
James Smart5af5eee2010-10-22 11:06:38 -04007685 * This function issues an abort iocb for the provided command iocb down to
7686 * the port. Other than the case the outstanding command iocb is an abort
7687 * request, this function issues abort out unconditionally. This function is
7688 * called with hbalock held. The function returns 0 when it fails due to
7689 * memory allocation failure or when the command iocb is an abort request.
James Smarte59058c2008-08-24 21:49:00 -04007690 **/
James Smart5af5eee2010-10-22 11:06:38 -04007691static int
7692lpfc_sli_abort_iotag_issue(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -05007693 struct lpfc_iocbq *cmdiocb)
dea31012005-04-17 16:05:31 -05007694{
James Smart2e0fef82007-06-17 19:56:36 -05007695 struct lpfc_vport *vport = cmdiocb->vport;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007696 struct lpfc_iocbq *abtsiocbp;
dea31012005-04-17 16:05:31 -05007697 IOCB_t *icmd = NULL;
7698 IOCB_t *iabt = NULL;
James Smart5af5eee2010-10-22 11:06:38 -04007699 int retval;
James Smart07951072007-04-25 09:51:38 -04007700
James Smart92d7f7b2007-06-17 19:56:38 -05007701 /*
7702 * There are certain command types we don't want to abort. And we
7703 * don't want to abort commands that are already in the process of
7704 * being aborted.
James Smart07951072007-04-25 09:51:38 -04007705 */
7706 icmd = &cmdiocb->iocb;
James Smart2e0fef82007-06-17 19:56:36 -05007707 if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
James Smart92d7f7b2007-06-17 19:56:38 -05007708 icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
7709 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
James Smart07951072007-04-25 09:51:38 -04007710 return 0;
7711
dea31012005-04-17 16:05:31 -05007712 /* issue ABTS for this IOCB based on iotag */
James Smart92d7f7b2007-06-17 19:56:38 -05007713 abtsiocbp = __lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05007714 if (abtsiocbp == NULL)
7715 return 0;
dea31012005-04-17 16:05:31 -05007716
James Smart07951072007-04-25 09:51:38 -04007717 /* This signals the response to set the correct status
James Smart341af102010-01-26 23:07:37 -05007718 * before calling the completion handler
James Smart07951072007-04-25 09:51:38 -04007719 */
7720 cmdiocb->iocb_flag |= LPFC_DRIVER_ABORTED;
7721
dea31012005-04-17 16:05:31 -05007722 iabt = &abtsiocbp->iocb;
James Smart07951072007-04-25 09:51:38 -04007723 iabt->un.acxri.abortType = ABORT_TYPE_ABTS;
7724 iabt->un.acxri.abortContextTag = icmd->ulpContext;
James Smart45ed1192009-10-02 15:17:02 -04007725 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smartda0436e2009-05-22 14:51:39 -04007726 iabt->un.acxri.abortIoTag = cmdiocb->sli4_xritag;
James Smart45ed1192009-10-02 15:17:02 -04007727 iabt->un.acxri.abortContextTag = cmdiocb->iotag;
7728 }
James Smartda0436e2009-05-22 14:51:39 -04007729 else
7730 iabt->un.acxri.abortIoTag = icmd->ulpIoTag;
dea31012005-04-17 16:05:31 -05007731 iabt->ulpLe = 1;
James Smart07951072007-04-25 09:51:38 -04007732 iabt->ulpClass = icmd->ulpClass;
dea31012005-04-17 16:05:31 -05007733
James Smart5ffc2662009-11-18 15:39:44 -05007734 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
7735 abtsiocbp->fcp_wqidx = cmdiocb->fcp_wqidx;
James Smart341af102010-01-26 23:07:37 -05007736 if (cmdiocb->iocb_flag & LPFC_IO_FCP)
7737 abtsiocbp->iocb_flag |= LPFC_USE_FCPWQIDX;
James Smart5ffc2662009-11-18 15:39:44 -05007738
James Smart2e0fef82007-06-17 19:56:36 -05007739 if (phba->link_state >= LPFC_LINK_UP)
James Smart07951072007-04-25 09:51:38 -04007740 iabt->ulpCommand = CMD_ABORT_XRI_CN;
7741 else
7742 iabt->ulpCommand = CMD_CLOSE_XRI_CN;
7743
7744 abtsiocbp->iocb_cmpl = lpfc_sli_abort_els_cmpl;
James Smart5b8bd0c2007-04-25 09:52:49 -04007745
James Smarte8b62012007-08-02 11:10:09 -04007746 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
7747 "0339 Abort xri x%x, original iotag x%x, "
7748 "abort cmd iotag x%x\n",
James Smart2a9bf3d2010-06-07 15:24:45 -04007749 iabt->un.acxri.abortIoTag,
James Smarte8b62012007-08-02 11:10:09 -04007750 iabt->un.acxri.abortContextTag,
James Smart2a9bf3d2010-06-07 15:24:45 -04007751 abtsiocbp->iotag);
James Smartda0436e2009-05-22 14:51:39 -04007752 retval = __lpfc_sli_issue_iocb(phba, pring->ringno, abtsiocbp, 0);
James Smart07951072007-04-25 09:51:38 -04007753
James Smartd7c255b2008-08-24 21:50:00 -04007754 if (retval)
7755 __lpfc_sli_release_iocbq(phba, abtsiocbp);
James Smart5af5eee2010-10-22 11:06:38 -04007756
7757 /*
7758 * Caller to this routine should check for IOCB_ERROR
7759 * and handle it properly. This routine no longer removes
7760 * iocb off txcmplq and call compl in case of IOCB_ERROR.
7761 */
7762 return retval;
7763}
7764
7765/**
7766 * lpfc_sli_issue_abort_iotag - Abort function for a command iocb
7767 * @phba: Pointer to HBA context object.
7768 * @pring: Pointer to driver SLI ring object.
7769 * @cmdiocb: Pointer to driver command iocb object.
7770 *
7771 * This function issues an abort iocb for the provided command iocb. In case
7772 * of unloading, the abort iocb will not be issued to commands on the ELS
7773 * ring. Instead, the callback function shall be changed to those commands
7774 * so that nothing happens when them finishes. This function is called with
7775 * hbalock held. The function returns 0 when the command iocb is an abort
7776 * request.
7777 **/
7778int
7779lpfc_sli_issue_abort_iotag(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
7780 struct lpfc_iocbq *cmdiocb)
7781{
7782 struct lpfc_vport *vport = cmdiocb->vport;
7783 int retval = IOCB_ERROR;
7784 IOCB_t *icmd = NULL;
7785
7786 /*
7787 * There are certain command types we don't want to abort. And we
7788 * don't want to abort commands that are already in the process of
7789 * being aborted.
7790 */
7791 icmd = &cmdiocb->iocb;
7792 if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
7793 icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
7794 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
7795 return 0;
7796
7797 /*
7798 * If we're unloading, don't abort iocb on the ELS ring, but change
7799 * the callback so that nothing happens when it finishes.
7800 */
7801 if ((vport->load_flag & FC_UNLOADING) &&
7802 (pring->ringno == LPFC_ELS_RING)) {
7803 if (cmdiocb->iocb_flag & LPFC_IO_FABRIC)
7804 cmdiocb->fabric_iocb_cmpl = lpfc_ignore_els_cmpl;
7805 else
7806 cmdiocb->iocb_cmpl = lpfc_ignore_els_cmpl;
7807 goto abort_iotag_exit;
7808 }
7809
7810 /* Now, we try to issue the abort to the cmdiocb out */
7811 retval = lpfc_sli_abort_iotag_issue(phba, pring, cmdiocb);
7812
James Smart07951072007-04-25 09:51:38 -04007813abort_iotag_exit:
James Smart2e0fef82007-06-17 19:56:36 -05007814 /*
7815 * Caller to this routine should check for IOCB_ERROR
7816 * and handle it properly. This routine no longer removes
7817 * iocb off txcmplq and call compl in case of IOCB_ERROR.
James Smart07951072007-04-25 09:51:38 -04007818 */
James Smart2e0fef82007-06-17 19:56:36 -05007819 return retval;
dea31012005-04-17 16:05:31 -05007820}
7821
James Smarte59058c2008-08-24 21:49:00 -04007822/**
James Smart5af5eee2010-10-22 11:06:38 -04007823 * lpfc_sli_iocb_ring_abort - Unconditionally abort all iocbs on an iocb ring
7824 * @phba: Pointer to HBA context object.
7825 * @pring: Pointer to driver SLI ring object.
7826 *
7827 * This function aborts all iocbs in the given ring and frees all the iocb
7828 * objects in txq. This function issues abort iocbs unconditionally for all
7829 * the iocb commands in txcmplq. The iocbs in the txcmplq is not guaranteed
7830 * to complete before the return of this function. The caller is not required
7831 * to hold any locks.
7832 **/
7833static void
7834lpfc_sli_iocb_ring_abort(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
7835{
7836 LIST_HEAD(completions);
7837 struct lpfc_iocbq *iocb, *next_iocb;
7838
7839 if (pring->ringno == LPFC_ELS_RING)
7840 lpfc_fabric_abort_hba(phba);
7841
7842 spin_lock_irq(&phba->hbalock);
7843
7844 /* Take off all the iocbs on txq for cancelling */
7845 list_splice_init(&pring->txq, &completions);
7846 pring->txq_cnt = 0;
7847
7848 /* Next issue ABTS for everything on the txcmplq */
7849 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
7850 lpfc_sli_abort_iotag_issue(phba, pring, iocb);
7851
7852 spin_unlock_irq(&phba->hbalock);
7853
7854 /* Cancel all the IOCBs from the completions list */
7855 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
7856 IOERR_SLI_ABORTED);
7857}
7858
7859/**
7860 * lpfc_sli_hba_iocb_abort - Abort all iocbs to an hba.
7861 * @phba: pointer to lpfc HBA data structure.
7862 *
7863 * This routine will abort all pending and outstanding iocbs to an HBA.
7864 **/
7865void
7866lpfc_sli_hba_iocb_abort(struct lpfc_hba *phba)
7867{
7868 struct lpfc_sli *psli = &phba->sli;
7869 struct lpfc_sli_ring *pring;
7870 int i;
7871
7872 for (i = 0; i < psli->num_rings; i++) {
7873 pring = &psli->ring[i];
7874 lpfc_sli_iocb_ring_abort(phba, pring);
7875 }
7876}
7877
7878/**
James Smart3621a712009-04-06 18:47:14 -04007879 * lpfc_sli_validate_fcp_iocb - find commands associated with a vport or LUN
James Smarte59058c2008-08-24 21:49:00 -04007880 * @iocbq: Pointer to driver iocb object.
7881 * @vport: Pointer to driver virtual port object.
7882 * @tgt_id: SCSI ID of the target.
7883 * @lun_id: LUN ID of the scsi device.
7884 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST
7885 *
James Smart3621a712009-04-06 18:47:14 -04007886 * This function acts as an iocb filter for functions which abort or count
James Smarte59058c2008-08-24 21:49:00 -04007887 * all FCP iocbs pending on a lun/SCSI target/SCSI host. It will return
7888 * 0 if the filtering criteria is met for the given iocb and will return
7889 * 1 if the filtering criteria is not met.
7890 * If ctx_cmd == LPFC_CTX_LUN, the function returns 0 only if the
7891 * given iocb is for the SCSI device specified by vport, tgt_id and
7892 * lun_id parameter.
7893 * If ctx_cmd == LPFC_CTX_TGT, the function returns 0 only if the
7894 * given iocb is for the SCSI target specified by vport and tgt_id
7895 * parameters.
7896 * If ctx_cmd == LPFC_CTX_HOST, the function returns 0 only if the
7897 * given iocb is for the SCSI host associated with the given vport.
7898 * This function is called with no locks held.
7899 **/
dea31012005-04-17 16:05:31 -05007900static int
James Smart51ef4c22007-08-02 11:10:31 -04007901lpfc_sli_validate_fcp_iocb(struct lpfc_iocbq *iocbq, struct lpfc_vport *vport,
7902 uint16_t tgt_id, uint64_t lun_id,
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007903 lpfc_ctx_cmd ctx_cmd)
dea31012005-04-17 16:05:31 -05007904{
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007905 struct lpfc_scsi_buf *lpfc_cmd;
dea31012005-04-17 16:05:31 -05007906 int rc = 1;
7907
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007908 if (!(iocbq->iocb_flag & LPFC_IO_FCP))
7909 return rc;
7910
James Smart51ef4c22007-08-02 11:10:31 -04007911 if (iocbq->vport != vport)
7912 return rc;
7913
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007914 lpfc_cmd = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007915
James Smart495a7142008-06-14 22:52:59 -04007916 if (lpfc_cmd->pCmd == NULL)
dea31012005-04-17 16:05:31 -05007917 return rc;
7918
7919 switch (ctx_cmd) {
7920 case LPFC_CTX_LUN:
James Smart495a7142008-06-14 22:52:59 -04007921 if ((lpfc_cmd->rdata->pnode) &&
7922 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id) &&
7923 (scsilun_to_int(&lpfc_cmd->fcp_cmnd->fcp_lun) == lun_id))
dea31012005-04-17 16:05:31 -05007924 rc = 0;
7925 break;
7926 case LPFC_CTX_TGT:
James Smart495a7142008-06-14 22:52:59 -04007927 if ((lpfc_cmd->rdata->pnode) &&
7928 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id))
dea31012005-04-17 16:05:31 -05007929 rc = 0;
7930 break;
dea31012005-04-17 16:05:31 -05007931 case LPFC_CTX_HOST:
7932 rc = 0;
7933 break;
7934 default:
7935 printk(KERN_ERR "%s: Unknown context cmd type, value %d\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07007936 __func__, ctx_cmd);
dea31012005-04-17 16:05:31 -05007937 break;
7938 }
7939
7940 return rc;
7941}
7942
James Smarte59058c2008-08-24 21:49:00 -04007943/**
James Smart3621a712009-04-06 18:47:14 -04007944 * lpfc_sli_sum_iocb - Function to count the number of FCP iocbs pending
James Smarte59058c2008-08-24 21:49:00 -04007945 * @vport: Pointer to virtual port.
7946 * @tgt_id: SCSI ID of the target.
7947 * @lun_id: LUN ID of the scsi device.
7948 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
7949 *
7950 * This function returns number of FCP commands pending for the vport.
7951 * When ctx_cmd == LPFC_CTX_LUN, the function returns number of FCP
7952 * commands pending on the vport associated with SCSI device specified
7953 * by tgt_id and lun_id parameters.
7954 * When ctx_cmd == LPFC_CTX_TGT, the function returns number of FCP
7955 * commands pending on the vport associated with SCSI target specified
7956 * by tgt_id parameter.
7957 * When ctx_cmd == LPFC_CTX_HOST, the function returns number of FCP
7958 * commands pending on the vport.
7959 * This function returns the number of iocbs which satisfy the filter.
7960 * This function is called without any lock held.
7961 **/
dea31012005-04-17 16:05:31 -05007962int
James Smart51ef4c22007-08-02 11:10:31 -04007963lpfc_sli_sum_iocb(struct lpfc_vport *vport, uint16_t tgt_id, uint64_t lun_id,
7964 lpfc_ctx_cmd ctx_cmd)
dea31012005-04-17 16:05:31 -05007965{
James Smart51ef4c22007-08-02 11:10:31 -04007966 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007967 struct lpfc_iocbq *iocbq;
7968 int sum, i;
dea31012005-04-17 16:05:31 -05007969
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007970 for (i = 1, sum = 0; i <= phba->sli.last_iotag; i++) {
7971 iocbq = phba->sli.iocbq_lookup[i];
dea31012005-04-17 16:05:31 -05007972
James Smart51ef4c22007-08-02 11:10:31 -04007973 if (lpfc_sli_validate_fcp_iocb (iocbq, vport, tgt_id, lun_id,
7974 ctx_cmd) == 0)
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007975 sum++;
dea31012005-04-17 16:05:31 -05007976 }
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007977
dea31012005-04-17 16:05:31 -05007978 return sum;
7979}
7980
James Smarte59058c2008-08-24 21:49:00 -04007981/**
James Smart3621a712009-04-06 18:47:14 -04007982 * lpfc_sli_abort_fcp_cmpl - Completion handler function for aborted FCP IOCBs
James Smarte59058c2008-08-24 21:49:00 -04007983 * @phba: Pointer to HBA context object
7984 * @cmdiocb: Pointer to command iocb object.
7985 * @rspiocb: Pointer to response iocb object.
7986 *
7987 * This function is called when an aborted FCP iocb completes. This
7988 * function is called by the ring event handler with no lock held.
7989 * This function frees the iocb.
7990 **/
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -04007991void
James Smart2e0fef82007-06-17 19:56:36 -05007992lpfc_sli_abort_fcp_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
7993 struct lpfc_iocbq *rspiocb)
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -04007994{
James Bottomley604a3e32005-10-29 10:28:33 -05007995 lpfc_sli_release_iocbq(phba, cmdiocb);
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -04007996 return;
7997}
7998
James Smarte59058c2008-08-24 21:49:00 -04007999/**
James Smart3621a712009-04-06 18:47:14 -04008000 * lpfc_sli_abort_iocb - issue abort for all commands on a host/target/LUN
James Smarte59058c2008-08-24 21:49:00 -04008001 * @vport: Pointer to virtual port.
8002 * @pring: Pointer to driver SLI ring object.
8003 * @tgt_id: SCSI ID of the target.
8004 * @lun_id: LUN ID of the scsi device.
8005 * @abort_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
8006 *
8007 * This function sends an abort command for every SCSI command
8008 * associated with the given virtual port pending on the ring
8009 * filtered by lpfc_sli_validate_fcp_iocb function.
8010 * When abort_cmd == LPFC_CTX_LUN, the function sends abort only to the
8011 * FCP iocbs associated with lun specified by tgt_id and lun_id
8012 * parameters
8013 * When abort_cmd == LPFC_CTX_TGT, the function sends abort only to the
8014 * FCP iocbs associated with SCSI target specified by tgt_id parameter.
8015 * When abort_cmd == LPFC_CTX_HOST, the function sends abort to all
8016 * FCP iocbs associated with virtual port.
8017 * This function returns number of iocbs it failed to abort.
8018 * This function is called with no locks held.
8019 **/
dea31012005-04-17 16:05:31 -05008020int
James Smart51ef4c22007-08-02 11:10:31 -04008021lpfc_sli_abort_iocb(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
8022 uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd abort_cmd)
dea31012005-04-17 16:05:31 -05008023{
James Smart51ef4c22007-08-02 11:10:31 -04008024 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04008025 struct lpfc_iocbq *iocbq;
8026 struct lpfc_iocbq *abtsiocb;
dea31012005-04-17 16:05:31 -05008027 IOCB_t *cmd = NULL;
dea31012005-04-17 16:05:31 -05008028 int errcnt = 0, ret_val = 0;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04008029 int i;
dea31012005-04-17 16:05:31 -05008030
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04008031 for (i = 1; i <= phba->sli.last_iotag; i++) {
8032 iocbq = phba->sli.iocbq_lookup[i];
dea31012005-04-17 16:05:31 -05008033
James Smart51ef4c22007-08-02 11:10:31 -04008034 if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
James Smart2e0fef82007-06-17 19:56:36 -05008035 abort_cmd) != 0)
dea31012005-04-17 16:05:31 -05008036 continue;
8037
8038 /* issue ABTS for this IOCB based on iotag */
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04008039 abtsiocb = lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05008040 if (abtsiocb == NULL) {
8041 errcnt++;
8042 continue;
8043 }
dea31012005-04-17 16:05:31 -05008044
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04008045 cmd = &iocbq->iocb;
dea31012005-04-17 16:05:31 -05008046 abtsiocb->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
8047 abtsiocb->iocb.un.acxri.abortContextTag = cmd->ulpContext;
James Smartda0436e2009-05-22 14:51:39 -04008048 if (phba->sli_rev == LPFC_SLI_REV4)
8049 abtsiocb->iocb.un.acxri.abortIoTag = iocbq->sli4_xritag;
8050 else
8051 abtsiocb->iocb.un.acxri.abortIoTag = cmd->ulpIoTag;
dea31012005-04-17 16:05:31 -05008052 abtsiocb->iocb.ulpLe = 1;
8053 abtsiocb->iocb.ulpClass = cmd->ulpClass;
James Smart2e0fef82007-06-17 19:56:36 -05008054 abtsiocb->vport = phba->pport;
dea31012005-04-17 16:05:31 -05008055
James Smart5ffc2662009-11-18 15:39:44 -05008056 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
8057 abtsiocb->fcp_wqidx = iocbq->fcp_wqidx;
James Smart341af102010-01-26 23:07:37 -05008058 if (iocbq->iocb_flag & LPFC_IO_FCP)
8059 abtsiocb->iocb_flag |= LPFC_USE_FCPWQIDX;
James Smart5ffc2662009-11-18 15:39:44 -05008060
James Smart2e0fef82007-06-17 19:56:36 -05008061 if (lpfc_is_link_up(phba))
dea31012005-04-17 16:05:31 -05008062 abtsiocb->iocb.ulpCommand = CMD_ABORT_XRI_CN;
8063 else
8064 abtsiocb->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
8065
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -04008066 /* Setup callback routine and issue the command. */
8067 abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
James Smartda0436e2009-05-22 14:51:39 -04008068 ret_val = lpfc_sli_issue_iocb(phba, pring->ringno,
8069 abtsiocb, 0);
dea31012005-04-17 16:05:31 -05008070 if (ret_val == IOCB_ERROR) {
James Bottomley604a3e32005-10-29 10:28:33 -05008071 lpfc_sli_release_iocbq(phba, abtsiocb);
dea31012005-04-17 16:05:31 -05008072 errcnt++;
8073 continue;
8074 }
8075 }
8076
8077 return errcnt;
8078}
8079
James Smarte59058c2008-08-24 21:49:00 -04008080/**
James Smart3621a712009-04-06 18:47:14 -04008081 * lpfc_sli_wake_iocb_wait - lpfc_sli_issue_iocb_wait's completion handler
James Smarte59058c2008-08-24 21:49:00 -04008082 * @phba: Pointer to HBA context object.
8083 * @cmdiocbq: Pointer to command iocb.
8084 * @rspiocbq: Pointer to response iocb.
8085 *
8086 * This function is the completion handler for iocbs issued using
8087 * lpfc_sli_issue_iocb_wait function. This function is called by the
8088 * ring event handler function without any lock held. This function
8089 * can be called from both worker thread context and interrupt
8090 * context. This function also can be called from other thread which
8091 * cleans up the SLI layer objects.
8092 * This function copy the contents of the response iocb to the
8093 * response iocb memory object provided by the caller of
8094 * lpfc_sli_issue_iocb_wait and then wakes up the thread which
8095 * sleeps for the iocb completion.
8096 **/
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04008097static void
8098lpfc_sli_wake_iocb_wait(struct lpfc_hba *phba,
8099 struct lpfc_iocbq *cmdiocbq,
8100 struct lpfc_iocbq *rspiocbq)
dea31012005-04-17 16:05:31 -05008101{
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04008102 wait_queue_head_t *pdone_q;
8103 unsigned long iflags;
James Smart0f65ff62010-02-26 14:14:23 -05008104 struct lpfc_scsi_buf *lpfc_cmd;
dea31012005-04-17 16:05:31 -05008105
James Smart2e0fef82007-06-17 19:56:36 -05008106 spin_lock_irqsave(&phba->hbalock, iflags);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04008107 cmdiocbq->iocb_flag |= LPFC_IO_WAKE;
8108 if (cmdiocbq->context2 && rspiocbq)
8109 memcpy(&((struct lpfc_iocbq *)cmdiocbq->context2)->iocb,
8110 &rspiocbq->iocb, sizeof(IOCB_t));
8111
James Smart0f65ff62010-02-26 14:14:23 -05008112 /* Set the exchange busy flag for task management commands */
8113 if ((cmdiocbq->iocb_flag & LPFC_IO_FCP) &&
8114 !(cmdiocbq->iocb_flag & LPFC_IO_LIBDFC)) {
8115 lpfc_cmd = container_of(cmdiocbq, struct lpfc_scsi_buf,
8116 cur_iocbq);
8117 lpfc_cmd->exch_busy = rspiocbq->iocb_flag & LPFC_EXCHANGE_BUSY;
8118 }
8119
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04008120 pdone_q = cmdiocbq->context_un.wait_queue;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04008121 if (pdone_q)
8122 wake_up(pdone_q);
James Smart858c9f62007-06-17 19:56:39 -05008123 spin_unlock_irqrestore(&phba->hbalock, iflags);
dea31012005-04-17 16:05:31 -05008124 return;
8125}
8126
James Smarte59058c2008-08-24 21:49:00 -04008127/**
James Smartd11e31d2009-06-10 17:23:06 -04008128 * lpfc_chk_iocb_flg - Test IOCB flag with lock held.
8129 * @phba: Pointer to HBA context object..
8130 * @piocbq: Pointer to command iocb.
8131 * @flag: Flag to test.
8132 *
8133 * This routine grabs the hbalock and then test the iocb_flag to
8134 * see if the passed in flag is set.
8135 * Returns:
8136 * 1 if flag is set.
8137 * 0 if flag is not set.
8138 **/
8139static int
8140lpfc_chk_iocb_flg(struct lpfc_hba *phba,
8141 struct lpfc_iocbq *piocbq, uint32_t flag)
8142{
8143 unsigned long iflags;
8144 int ret;
8145
8146 spin_lock_irqsave(&phba->hbalock, iflags);
8147 ret = piocbq->iocb_flag & flag;
8148 spin_unlock_irqrestore(&phba->hbalock, iflags);
8149 return ret;
8150
8151}
8152
8153/**
James Smart3621a712009-04-06 18:47:14 -04008154 * lpfc_sli_issue_iocb_wait - Synchronous function to issue iocb commands
James Smarte59058c2008-08-24 21:49:00 -04008155 * @phba: Pointer to HBA context object..
8156 * @pring: Pointer to sli ring.
8157 * @piocb: Pointer to command iocb.
8158 * @prspiocbq: Pointer to response iocb.
8159 * @timeout: Timeout in number of seconds.
8160 *
8161 * This function issues the iocb to firmware and waits for the
8162 * iocb to complete. If the iocb command is not
8163 * completed within timeout seconds, it returns IOCB_TIMEDOUT.
8164 * Caller should not free the iocb resources if this function
8165 * returns IOCB_TIMEDOUT.
8166 * The function waits for the iocb completion using an
8167 * non-interruptible wait.
8168 * This function will sleep while waiting for iocb completion.
8169 * So, this function should not be called from any context which
8170 * does not allow sleeping. Due to the same reason, this function
8171 * cannot be called with interrupt disabled.
8172 * This function assumes that the iocb completions occur while
8173 * this function sleep. So, this function cannot be called from
8174 * the thread which process iocb completion for this ring.
8175 * This function clears the iocb_flag of the iocb object before
8176 * issuing the iocb and the iocb completion handler sets this
8177 * flag and wakes this thread when the iocb completes.
8178 * The contents of the response iocb will be copied to prspiocbq
8179 * by the completion handler when the command completes.
8180 * This function returns IOCB_SUCCESS when success.
8181 * This function is called with no lock held.
8182 **/
dea31012005-04-17 16:05:31 -05008183int
James Smart2e0fef82007-06-17 19:56:36 -05008184lpfc_sli_issue_iocb_wait(struct lpfc_hba *phba,
James Smartda0436e2009-05-22 14:51:39 -04008185 uint32_t ring_number,
James Smart2e0fef82007-06-17 19:56:36 -05008186 struct lpfc_iocbq *piocb,
8187 struct lpfc_iocbq *prspiocbq,
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04008188 uint32_t timeout)
dea31012005-04-17 16:05:31 -05008189{
Peter Zijlstra7259f0d2006-10-29 22:46:36 -08008190 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04008191 long timeleft, timeout_req = 0;
8192 int retval = IOCB_SUCCESS;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05008193 uint32_t creg_val;
James Smart2a9bf3d2010-06-07 15:24:45 -04008194 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
dea31012005-04-17 16:05:31 -05008195 /*
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04008196 * If the caller has provided a response iocbq buffer, then context2
8197 * is NULL or its an error.
dea31012005-04-17 16:05:31 -05008198 */
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04008199 if (prspiocbq) {
8200 if (piocb->context2)
8201 return IOCB_ERROR;
8202 piocb->context2 = prspiocbq;
dea31012005-04-17 16:05:31 -05008203 }
8204
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04008205 piocb->iocb_cmpl = lpfc_sli_wake_iocb_wait;
8206 piocb->context_un.wait_queue = &done_q;
8207 piocb->iocb_flag &= ~LPFC_IO_WAKE;
dea31012005-04-17 16:05:31 -05008208
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05008209 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
8210 creg_val = readl(phba->HCregaddr);
8211 creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
8212 writel(creg_val, phba->HCregaddr);
8213 readl(phba->HCregaddr); /* flush */
8214 }
8215
James Smart2a9bf3d2010-06-07 15:24:45 -04008216 retval = lpfc_sli_issue_iocb(phba, ring_number, piocb,
8217 SLI_IOCB_RET_IOCB);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04008218 if (retval == IOCB_SUCCESS) {
8219 timeout_req = timeout * HZ;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04008220 timeleft = wait_event_timeout(done_q,
James Smartd11e31d2009-06-10 17:23:06 -04008221 lpfc_chk_iocb_flg(phba, piocb, LPFC_IO_WAKE),
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04008222 timeout_req);
dea31012005-04-17 16:05:31 -05008223
James Smart7054a602007-04-25 09:52:34 -04008224 if (piocb->iocb_flag & LPFC_IO_WAKE) {
8225 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04008226 "0331 IOCB wake signaled\n");
James Smart7054a602007-04-25 09:52:34 -04008227 } else if (timeleft == 0) {
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04008228 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04008229 "0338 IOCB wait timeout error - no "
8230 "wake response Data x%x\n", timeout);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04008231 retval = IOCB_TIMEDOUT;
James Smart7054a602007-04-25 09:52:34 -04008232 } else {
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04008233 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04008234 "0330 IOCB wake NOT set, "
8235 "Data x%x x%lx\n",
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04008236 timeout, (timeleft / jiffies));
8237 retval = IOCB_TIMEDOUT;
dea31012005-04-17 16:05:31 -05008238 }
James Smart2a9bf3d2010-06-07 15:24:45 -04008239 } else if (retval == IOCB_BUSY) {
8240 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
8241 "2818 Max IOCBs %d txq cnt %d txcmplq cnt %d\n",
8242 phba->iocb_cnt, pring->txq_cnt, pring->txcmplq_cnt);
8243 return retval;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04008244 } else {
8245 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smartd7c255b2008-08-24 21:50:00 -04008246 "0332 IOCB wait issue failed, Data x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04008247 retval);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04008248 retval = IOCB_ERROR;
dea31012005-04-17 16:05:31 -05008249 }
8250
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05008251 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
8252 creg_val = readl(phba->HCregaddr);
8253 creg_val &= ~(HC_R0INT_ENA << LPFC_FCP_RING);
8254 writel(creg_val, phba->HCregaddr);
8255 readl(phba->HCregaddr); /* flush */
8256 }
8257
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04008258 if (prspiocbq)
8259 piocb->context2 = NULL;
8260
8261 piocb->context_un.wait_queue = NULL;
8262 piocb->iocb_cmpl = NULL;
dea31012005-04-17 16:05:31 -05008263 return retval;
8264}
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04008265
James Smarte59058c2008-08-24 21:49:00 -04008266/**
James Smart3621a712009-04-06 18:47:14 -04008267 * lpfc_sli_issue_mbox_wait - Synchronous function to issue mailbox
James Smarte59058c2008-08-24 21:49:00 -04008268 * @phba: Pointer to HBA context object.
8269 * @pmboxq: Pointer to driver mailbox object.
8270 * @timeout: Timeout in number of seconds.
8271 *
8272 * This function issues the mailbox to firmware and waits for the
8273 * mailbox command to complete. If the mailbox command is not
8274 * completed within timeout seconds, it returns MBX_TIMEOUT.
8275 * The function waits for the mailbox completion using an
8276 * interruptible wait. If the thread is woken up due to a
8277 * signal, MBX_TIMEOUT error is returned to the caller. Caller
8278 * should not free the mailbox resources, if this function returns
8279 * MBX_TIMEOUT.
8280 * This function will sleep while waiting for mailbox completion.
8281 * So, this function should not be called from any context which
8282 * does not allow sleeping. Due to the same reason, this function
8283 * cannot be called with interrupt disabled.
8284 * This function assumes that the mailbox completion occurs while
8285 * this function sleep. So, this function cannot be called from
8286 * the worker thread which processes mailbox completion.
8287 * This function is called in the context of HBA management
8288 * applications.
8289 * This function returns MBX_SUCCESS when successful.
8290 * This function is called with no lock held.
8291 **/
dea31012005-04-17 16:05:31 -05008292int
James Smart2e0fef82007-06-17 19:56:36 -05008293lpfc_sli_issue_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq,
dea31012005-04-17 16:05:31 -05008294 uint32_t timeout)
8295{
Peter Zijlstra7259f0d2006-10-29 22:46:36 -08008296 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
dea31012005-04-17 16:05:31 -05008297 int retval;
James Smart858c9f62007-06-17 19:56:39 -05008298 unsigned long flag;
dea31012005-04-17 16:05:31 -05008299
8300 /* The caller must leave context1 empty. */
James Smart98c9ea52007-10-27 13:37:33 -04008301 if (pmboxq->context1)
James Smart2e0fef82007-06-17 19:56:36 -05008302 return MBX_NOT_FINISHED;
dea31012005-04-17 16:05:31 -05008303
James Smart495a7142008-06-14 22:52:59 -04008304 pmboxq->mbox_flag &= ~LPFC_MBX_WAKE;
dea31012005-04-17 16:05:31 -05008305 /* setup wake call as IOCB callback */
8306 pmboxq->mbox_cmpl = lpfc_sli_wake_mbox_wait;
8307 /* setup context field to pass wait_queue pointer to wake function */
8308 pmboxq->context1 = &done_q;
8309
dea31012005-04-17 16:05:31 -05008310 /* now issue the command */
8311 retval = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
8312
8313 if (retval == MBX_BUSY || retval == MBX_SUCCESS) {
James Smart7054a602007-04-25 09:52:34 -04008314 wait_event_interruptible_timeout(done_q,
8315 pmboxq->mbox_flag & LPFC_MBX_WAKE,
8316 timeout * HZ);
8317
James Smart858c9f62007-06-17 19:56:39 -05008318 spin_lock_irqsave(&phba->hbalock, flag);
dea31012005-04-17 16:05:31 -05008319 pmboxq->context1 = NULL;
James Smart7054a602007-04-25 09:52:34 -04008320 /*
8321 * if LPFC_MBX_WAKE flag is set the mailbox is completed
8322 * else do not free the resources.
8323 */
James Smartd7c47992010-06-08 18:31:54 -04008324 if (pmboxq->mbox_flag & LPFC_MBX_WAKE) {
dea31012005-04-17 16:05:31 -05008325 retval = MBX_SUCCESS;
James Smartd7c47992010-06-08 18:31:54 -04008326 lpfc_sli4_swap_str(phba, pmboxq);
8327 } else {
James Smart7054a602007-04-25 09:52:34 -04008328 retval = MBX_TIMEOUT;
James Smart858c9f62007-06-17 19:56:39 -05008329 pmboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
8330 }
8331 spin_unlock_irqrestore(&phba->hbalock, flag);
dea31012005-04-17 16:05:31 -05008332 }
8333
dea31012005-04-17 16:05:31 -05008334 return retval;
8335}
8336
James Smarte59058c2008-08-24 21:49:00 -04008337/**
James Smart3772a992009-05-22 14:50:54 -04008338 * lpfc_sli_mbox_sys_shutdown - shutdown mailbox command sub-system
James Smarte59058c2008-08-24 21:49:00 -04008339 * @phba: Pointer to HBA context.
8340 *
James Smart3772a992009-05-22 14:50:54 -04008341 * This function is called to shutdown the driver's mailbox sub-system.
8342 * It first marks the mailbox sub-system is in a block state to prevent
8343 * the asynchronous mailbox command from issued off the pending mailbox
8344 * command queue. If the mailbox command sub-system shutdown is due to
8345 * HBA error conditions such as EEH or ERATT, this routine shall invoke
8346 * the mailbox sub-system flush routine to forcefully bring down the
8347 * mailbox sub-system. Otherwise, if it is due to normal condition (such
8348 * as with offline or HBA function reset), this routine will wait for the
8349 * outstanding mailbox command to complete before invoking the mailbox
8350 * sub-system flush routine to gracefully bring down mailbox sub-system.
James Smarte59058c2008-08-24 21:49:00 -04008351 **/
James Smart3772a992009-05-22 14:50:54 -04008352void
8353lpfc_sli_mbox_sys_shutdown(struct lpfc_hba *phba)
James Smartb4c02652006-07-06 15:50:43 -04008354{
James Smart3772a992009-05-22 14:50:54 -04008355 struct lpfc_sli *psli = &phba->sli;
8356 uint8_t actcmd = MBX_HEARTBEAT;
8357 unsigned long timeout;
8358
8359 spin_lock_irq(&phba->hbalock);
8360 psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
8361 spin_unlock_irq(&phba->hbalock);
8362
8363 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
8364 spin_lock_irq(&phba->hbalock);
8365 if (phba->sli.mbox_active)
8366 actcmd = phba->sli.mbox_active->u.mb.mbxCommand;
8367 spin_unlock_irq(&phba->hbalock);
8368 /* Determine how long we might wait for the active mailbox
8369 * command to be gracefully completed by firmware.
8370 */
8371 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, actcmd) *
8372 1000) + jiffies;
8373 while (phba->sli.mbox_active) {
8374 /* Check active mailbox complete status every 2ms */
8375 msleep(2);
8376 if (time_after(jiffies, timeout))
8377 /* Timeout, let the mailbox flush routine to
8378 * forcefully release active mailbox command
8379 */
8380 break;
8381 }
8382 }
8383 lpfc_sli_mbox_sys_flush(phba);
8384}
8385
8386/**
8387 * lpfc_sli_eratt_read - read sli-3 error attention events
8388 * @phba: Pointer to HBA context.
8389 *
8390 * This function is called to read the SLI3 device error attention registers
8391 * for possible error attention events. The caller must hold the hostlock
8392 * with spin_lock_irq().
8393 *
8394 * This fucntion returns 1 when there is Error Attention in the Host Attention
8395 * Register and returns 0 otherwise.
8396 **/
8397static int
8398lpfc_sli_eratt_read(struct lpfc_hba *phba)
8399{
James Smarted957682007-06-17 19:56:37 -05008400 uint32_t ha_copy;
James Smartb4c02652006-07-06 15:50:43 -04008401
James Smart3772a992009-05-22 14:50:54 -04008402 /* Read chip Host Attention (HA) register */
8403 ha_copy = readl(phba->HAregaddr);
8404 if (ha_copy & HA_ERATT) {
8405 /* Read host status register to retrieve error event */
8406 lpfc_sli_read_hs(phba);
James Smartb4c02652006-07-06 15:50:43 -04008407
James Smart3772a992009-05-22 14:50:54 -04008408 /* Check if there is a deferred error condition is active */
8409 if ((HS_FFER1 & phba->work_hs) &&
8410 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
James Smartdcf2a4e2010-09-29 11:18:53 -04008411 HS_FFER6 | HS_FFER7 | HS_FFER8) & phba->work_hs)) {
James Smart3772a992009-05-22 14:50:54 -04008412 phba->hba_flag |= DEFER_ERATT;
James Smart3772a992009-05-22 14:50:54 -04008413 /* Clear all interrupt enable conditions */
8414 writel(0, phba->HCregaddr);
8415 readl(phba->HCregaddr);
8416 }
8417
8418 /* Set the driver HA work bitmap */
James Smart3772a992009-05-22 14:50:54 -04008419 phba->work_ha |= HA_ERATT;
8420 /* Indicate polling handles this ERATT */
8421 phba->hba_flag |= HBA_ERATT_HANDLED;
James Smart3772a992009-05-22 14:50:54 -04008422 return 1;
James Smartb4c02652006-07-06 15:50:43 -04008423 }
James Smart3772a992009-05-22 14:50:54 -04008424 return 0;
James Smartb4c02652006-07-06 15:50:43 -04008425}
8426
James Smarte59058c2008-08-24 21:49:00 -04008427/**
James Smartda0436e2009-05-22 14:51:39 -04008428 * lpfc_sli4_eratt_read - read sli-4 error attention events
8429 * @phba: Pointer to HBA context.
8430 *
8431 * This function is called to read the SLI4 device error attention registers
8432 * for possible error attention events. The caller must hold the hostlock
8433 * with spin_lock_irq().
8434 *
8435 * This fucntion returns 1 when there is Error Attention in the Host Attention
8436 * Register and returns 0 otherwise.
8437 **/
8438static int
8439lpfc_sli4_eratt_read(struct lpfc_hba *phba)
8440{
8441 uint32_t uerr_sta_hi, uerr_sta_lo;
James Smart2fcee4b2010-12-15 17:57:46 -05008442 uint32_t if_type, portsmphr;
8443 struct lpfc_register portstat_reg;
James Smartda0436e2009-05-22 14:51:39 -04008444
James Smart2fcee4b2010-12-15 17:57:46 -05008445 /*
8446 * For now, use the SLI4 device internal unrecoverable error
James Smartda0436e2009-05-22 14:51:39 -04008447 * registers for error attention. This can be changed later.
8448 */
James Smart2fcee4b2010-12-15 17:57:46 -05008449 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
8450 switch (if_type) {
8451 case LPFC_SLI_INTF_IF_TYPE_0:
8452 uerr_sta_lo = readl(phba->sli4_hba.u.if_type0.UERRLOregaddr);
8453 uerr_sta_hi = readl(phba->sli4_hba.u.if_type0.UERRHIregaddr);
8454 if ((~phba->sli4_hba.ue_mask_lo & uerr_sta_lo) ||
8455 (~phba->sli4_hba.ue_mask_hi & uerr_sta_hi)) {
8456 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8457 "1423 HBA Unrecoverable error: "
8458 "uerr_lo_reg=0x%x, uerr_hi_reg=0x%x, "
8459 "ue_mask_lo_reg=0x%x, "
8460 "ue_mask_hi_reg=0x%x\n",
8461 uerr_sta_lo, uerr_sta_hi,
8462 phba->sli4_hba.ue_mask_lo,
8463 phba->sli4_hba.ue_mask_hi);
8464 phba->work_status[0] = uerr_sta_lo;
8465 phba->work_status[1] = uerr_sta_hi;
8466 phba->work_ha |= HA_ERATT;
8467 phba->hba_flag |= HBA_ERATT_HANDLED;
8468 return 1;
8469 }
8470 break;
8471 case LPFC_SLI_INTF_IF_TYPE_2:
8472 portstat_reg.word0 =
8473 readl(phba->sli4_hba.u.if_type2.STATUSregaddr);
8474 portsmphr = readl(phba->sli4_hba.PSMPHRregaddr);
8475 if (bf_get(lpfc_sliport_status_err, &portstat_reg)) {
8476 phba->work_status[0] =
8477 readl(phba->sli4_hba.u.if_type2.ERR1regaddr);
8478 phba->work_status[1] =
8479 readl(phba->sli4_hba.u.if_type2.ERR2regaddr);
8480 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8481 "2885 Port Error Detected: "
8482 "port status reg 0x%x, "
8483 "port smphr reg 0x%x, "
8484 "error 1=0x%x, error 2=0x%x\n",
8485 portstat_reg.word0,
8486 portsmphr,
8487 phba->work_status[0],
8488 phba->work_status[1]);
8489 phba->work_ha |= HA_ERATT;
8490 phba->hba_flag |= HBA_ERATT_HANDLED;
8491 return 1;
8492 }
8493 break;
8494 case LPFC_SLI_INTF_IF_TYPE_1:
8495 default:
James Smarta747c9c2009-11-18 15:41:10 -05008496 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart2fcee4b2010-12-15 17:57:46 -05008497 "2886 HBA Error Attention on unsupported "
8498 "if type %d.", if_type);
James Smarta747c9c2009-11-18 15:41:10 -05008499 return 1;
James Smartda0436e2009-05-22 14:51:39 -04008500 }
James Smart2fcee4b2010-12-15 17:57:46 -05008501
James Smartda0436e2009-05-22 14:51:39 -04008502 return 0;
8503}
8504
8505/**
James Smart3621a712009-04-06 18:47:14 -04008506 * lpfc_sli_check_eratt - check error attention events
James Smart93996272008-08-24 21:50:30 -04008507 * @phba: Pointer to HBA context.
8508 *
James Smart3772a992009-05-22 14:50:54 -04008509 * This function is called from timer soft interrupt context to check HBA's
James Smart93996272008-08-24 21:50:30 -04008510 * error attention register bit for error attention events.
8511 *
8512 * This fucntion returns 1 when there is Error Attention in the Host Attention
8513 * Register and returns 0 otherwise.
8514 **/
8515int
8516lpfc_sli_check_eratt(struct lpfc_hba *phba)
8517{
8518 uint32_t ha_copy;
8519
8520 /* If somebody is waiting to handle an eratt, don't process it
8521 * here. The brdkill function will do this.
8522 */
8523 if (phba->link_flag & LS_IGNORE_ERATT)
8524 return 0;
8525
8526 /* Check if interrupt handler handles this ERATT */
8527 spin_lock_irq(&phba->hbalock);
8528 if (phba->hba_flag & HBA_ERATT_HANDLED) {
8529 /* Interrupt handler has handled ERATT */
8530 spin_unlock_irq(&phba->hbalock);
8531 return 0;
8532 }
8533
James Smarta257bf92009-04-06 18:48:10 -04008534 /*
8535 * If there is deferred error attention, do not check for error
8536 * attention
8537 */
8538 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
8539 spin_unlock_irq(&phba->hbalock);
8540 return 0;
8541 }
8542
James Smart3772a992009-05-22 14:50:54 -04008543 /* If PCI channel is offline, don't process it */
8544 if (unlikely(pci_channel_offline(phba->pcidev))) {
James Smart93996272008-08-24 21:50:30 -04008545 spin_unlock_irq(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -04008546 return 0;
8547 }
8548
8549 switch (phba->sli_rev) {
8550 case LPFC_SLI_REV2:
8551 case LPFC_SLI_REV3:
8552 /* Read chip Host Attention (HA) register */
8553 ha_copy = lpfc_sli_eratt_read(phba);
8554 break;
James Smartda0436e2009-05-22 14:51:39 -04008555 case LPFC_SLI_REV4:
James Smart2fcee4b2010-12-15 17:57:46 -05008556 /* Read device Uncoverable Error (UERR) registers */
James Smartda0436e2009-05-22 14:51:39 -04008557 ha_copy = lpfc_sli4_eratt_read(phba);
8558 break;
James Smart3772a992009-05-22 14:50:54 -04008559 default:
8560 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8561 "0299 Invalid SLI revision (%d)\n",
8562 phba->sli_rev);
8563 ha_copy = 0;
8564 break;
James Smart93996272008-08-24 21:50:30 -04008565 }
8566 spin_unlock_irq(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -04008567
8568 return ha_copy;
8569}
8570
8571/**
8572 * lpfc_intr_state_check - Check device state for interrupt handling
8573 * @phba: Pointer to HBA context.
8574 *
8575 * This inline routine checks whether a device or its PCI slot is in a state
8576 * that the interrupt should be handled.
8577 *
8578 * This function returns 0 if the device or the PCI slot is in a state that
8579 * interrupt should be handled, otherwise -EIO.
8580 */
8581static inline int
8582lpfc_intr_state_check(struct lpfc_hba *phba)
8583{
8584 /* If the pci channel is offline, ignore all the interrupts */
8585 if (unlikely(pci_channel_offline(phba->pcidev)))
8586 return -EIO;
8587
8588 /* Update device level interrupt statistics */
8589 phba->sli.slistat.sli_intr++;
8590
8591 /* Ignore all interrupts during initialization. */
8592 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
8593 return -EIO;
8594
James Smart93996272008-08-24 21:50:30 -04008595 return 0;
8596}
8597
8598/**
James Smart3772a992009-05-22 14:50:54 -04008599 * lpfc_sli_sp_intr_handler - Slow-path interrupt handler to SLI-3 device
James Smarte59058c2008-08-24 21:49:00 -04008600 * @irq: Interrupt number.
8601 * @dev_id: The device context pointer.
8602 *
James Smart93996272008-08-24 21:50:30 -04008603 * This function is directly called from the PCI layer as an interrupt
James Smart3772a992009-05-22 14:50:54 -04008604 * service routine when device with SLI-3 interface spec is enabled with
8605 * MSI-X multi-message interrupt mode and there are slow-path events in
8606 * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
8607 * interrupt mode, this function is called as part of the device-level
8608 * interrupt handler. When the PCI slot is in error recovery or the HBA
8609 * is undergoing initialization, the interrupt handler will not process
8610 * the interrupt. The link attention and ELS ring attention events are
8611 * handled by the worker thread. The interrupt handler signals the worker
8612 * thread and returns for these events. This function is called without
8613 * any lock held. It gets the hbalock to access and update SLI data
James Smart93996272008-08-24 21:50:30 -04008614 * structures.
8615 *
8616 * This function returns IRQ_HANDLED when interrupt is handled else it
8617 * returns IRQ_NONE.
James Smarte59058c2008-08-24 21:49:00 -04008618 **/
dea31012005-04-17 16:05:31 -05008619irqreturn_t
James Smart3772a992009-05-22 14:50:54 -04008620lpfc_sli_sp_intr_handler(int irq, void *dev_id)
dea31012005-04-17 16:05:31 -05008621{
James Smart2e0fef82007-06-17 19:56:36 -05008622 struct lpfc_hba *phba;
James Smarta747c9c2009-11-18 15:41:10 -05008623 uint32_t ha_copy, hc_copy;
dea31012005-04-17 16:05:31 -05008624 uint32_t work_ha_copy;
8625 unsigned long status;
James Smart5b75da22008-12-04 22:39:35 -05008626 unsigned long iflag;
dea31012005-04-17 16:05:31 -05008627 uint32_t control;
8628
James Smart92d7f7b2007-06-17 19:56:38 -05008629 MAILBOX_t *mbox, *pmbox;
James Smart858c9f62007-06-17 19:56:39 -05008630 struct lpfc_vport *vport;
8631 struct lpfc_nodelist *ndlp;
8632 struct lpfc_dmabuf *mp;
James Smart92d7f7b2007-06-17 19:56:38 -05008633 LPFC_MBOXQ_t *pmb;
8634 int rc;
8635
dea31012005-04-17 16:05:31 -05008636 /*
8637 * Get the driver's phba structure from the dev_id and
8638 * assume the HBA is not interrupting.
8639 */
James Smart93996272008-08-24 21:50:30 -04008640 phba = (struct lpfc_hba *)dev_id;
dea31012005-04-17 16:05:31 -05008641
8642 if (unlikely(!phba))
8643 return IRQ_NONE;
8644
dea31012005-04-17 16:05:31 -05008645 /*
James Smart93996272008-08-24 21:50:30 -04008646 * Stuff needs to be attented to when this function is invoked as an
8647 * individual interrupt handler in MSI-X multi-message interrupt mode
dea31012005-04-17 16:05:31 -05008648 */
James Smart93996272008-08-24 21:50:30 -04008649 if (phba->intr_type == MSIX) {
James Smart3772a992009-05-22 14:50:54 -04008650 /* Check device state for handling interrupt */
8651 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -04008652 return IRQ_NONE;
8653 /* Need to read HA REG for slow-path events */
James Smart5b75da22008-12-04 22:39:35 -05008654 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart34b02dc2008-08-24 21:49:55 -04008655 ha_copy = readl(phba->HAregaddr);
James Smart93996272008-08-24 21:50:30 -04008656 /* If somebody is waiting to handle an eratt don't process it
8657 * here. The brdkill function will do this.
8658 */
8659 if (phba->link_flag & LS_IGNORE_ERATT)
8660 ha_copy &= ~HA_ERATT;
8661 /* Check the need for handling ERATT in interrupt handler */
8662 if (ha_copy & HA_ERATT) {
8663 if (phba->hba_flag & HBA_ERATT_HANDLED)
8664 /* ERATT polling has handled ERATT */
8665 ha_copy &= ~HA_ERATT;
8666 else
8667 /* Indicate interrupt handler handles ERATT */
8668 phba->hba_flag |= HBA_ERATT_HANDLED;
8669 }
James Smarta257bf92009-04-06 18:48:10 -04008670
8671 /*
8672 * If there is deferred error attention, do not check for any
8673 * interrupt.
8674 */
8675 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
James Smart3772a992009-05-22 14:50:54 -04008676 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -04008677 return IRQ_NONE;
8678 }
8679
James Smart93996272008-08-24 21:50:30 -04008680 /* Clear up only attention source related to slow-path */
James Smarta747c9c2009-11-18 15:41:10 -05008681 hc_copy = readl(phba->HCregaddr);
8682 writel(hc_copy & ~(HC_MBINT_ENA | HC_R2INT_ENA |
8683 HC_LAINT_ENA | HC_ERINT_ENA),
8684 phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -04008685 writel((ha_copy & (HA_MBATT | HA_R2_CLR_MSK)),
8686 phba->HAregaddr);
James Smarta747c9c2009-11-18 15:41:10 -05008687 writel(hc_copy, phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -04008688 readl(phba->HAregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -05008689 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -04008690 } else
8691 ha_copy = phba->ha_copy;
dea31012005-04-17 16:05:31 -05008692
dea31012005-04-17 16:05:31 -05008693 work_ha_copy = ha_copy & phba->work_ha_mask;
8694
James Smart93996272008-08-24 21:50:30 -04008695 if (work_ha_copy) {
dea31012005-04-17 16:05:31 -05008696 if (work_ha_copy & HA_LATT) {
8697 if (phba->sli.sli_flag & LPFC_PROCESS_LA) {
8698 /*
8699 * Turn off Link Attention interrupts
8700 * until CLEAR_LA done
8701 */
James Smart5b75da22008-12-04 22:39:35 -05008702 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05008703 phba->sli.sli_flag &= ~LPFC_PROCESS_LA;
8704 control = readl(phba->HCregaddr);
8705 control &= ~HC_LAINT_ENA;
8706 writel(control, phba->HCregaddr);
8707 readl(phba->HCregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -05008708 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05008709 }
8710 else
8711 work_ha_copy &= ~HA_LATT;
8712 }
8713
James Smart93996272008-08-24 21:50:30 -04008714 if (work_ha_copy & ~(HA_ERATT | HA_MBATT | HA_LATT)) {
James Smart858c9f62007-06-17 19:56:39 -05008715 /*
8716 * Turn off Slow Rings interrupts, LPFC_ELS_RING is
8717 * the only slow ring.
8718 */
8719 status = (work_ha_copy &
8720 (HA_RXMASK << (4*LPFC_ELS_RING)));
8721 status >>= (4*LPFC_ELS_RING);
8722 if (status & HA_RXMASK) {
James Smart5b75da22008-12-04 22:39:35 -05008723 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart858c9f62007-06-17 19:56:39 -05008724 control = readl(phba->HCregaddr);
James Smarta58cbd52007-08-02 11:09:43 -04008725
8726 lpfc_debugfs_slow_ring_trc(phba,
8727 "ISR slow ring: ctl:x%x stat:x%x isrcnt:x%x",
8728 control, status,
8729 (uint32_t)phba->sli.slistat.sli_intr);
8730
James Smart858c9f62007-06-17 19:56:39 -05008731 if (control & (HC_R0INT_ENA << LPFC_ELS_RING)) {
James Smarta58cbd52007-08-02 11:09:43 -04008732 lpfc_debugfs_slow_ring_trc(phba,
8733 "ISR Disable ring:"
8734 "pwork:x%x hawork:x%x wait:x%x",
8735 phba->work_ha, work_ha_copy,
8736 (uint32_t)((unsigned long)
James Smart5e9d9b82008-06-14 22:52:53 -04008737 &phba->work_waitq));
James Smarta58cbd52007-08-02 11:09:43 -04008738
James Smart858c9f62007-06-17 19:56:39 -05008739 control &=
8740 ~(HC_R0INT_ENA << LPFC_ELS_RING);
dea31012005-04-17 16:05:31 -05008741 writel(control, phba->HCregaddr);
8742 readl(phba->HCregaddr); /* flush */
dea31012005-04-17 16:05:31 -05008743 }
James Smarta58cbd52007-08-02 11:09:43 -04008744 else {
8745 lpfc_debugfs_slow_ring_trc(phba,
8746 "ISR slow ring: pwork:"
8747 "x%x hawork:x%x wait:x%x",
8748 phba->work_ha, work_ha_copy,
8749 (uint32_t)((unsigned long)
James Smart5e9d9b82008-06-14 22:52:53 -04008750 &phba->work_waitq));
James Smarta58cbd52007-08-02 11:09:43 -04008751 }
James Smart5b75da22008-12-04 22:39:35 -05008752 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05008753 }
8754 }
James Smart5b75da22008-12-04 22:39:35 -05008755 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -04008756 if (work_ha_copy & HA_ERATT) {
James Smart93996272008-08-24 21:50:30 -04008757 lpfc_sli_read_hs(phba);
James Smarta257bf92009-04-06 18:48:10 -04008758 /*
8759 * Check if there is a deferred error condition
8760 * is active
8761 */
8762 if ((HS_FFER1 & phba->work_hs) &&
8763 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
James Smartdcf2a4e2010-09-29 11:18:53 -04008764 HS_FFER6 | HS_FFER7 | HS_FFER8) &
8765 phba->work_hs)) {
James Smarta257bf92009-04-06 18:48:10 -04008766 phba->hba_flag |= DEFER_ERATT;
8767 /* Clear all interrupt enable conditions */
8768 writel(0, phba->HCregaddr);
8769 readl(phba->HCregaddr);
8770 }
8771 }
8772
James Smart93996272008-08-24 21:50:30 -04008773 if ((work_ha_copy & HA_MBATT) && (phba->sli.mbox_active)) {
James Smart92d7f7b2007-06-17 19:56:38 -05008774 pmb = phba->sli.mbox_active;
James Smart04c68492009-05-22 14:52:52 -04008775 pmbox = &pmb->u.mb;
James Smart34b02dc2008-08-24 21:49:55 -04008776 mbox = phba->mbox;
James Smart858c9f62007-06-17 19:56:39 -05008777 vport = pmb->vport;
James Smart92d7f7b2007-06-17 19:56:38 -05008778
8779 /* First check out the status word */
8780 lpfc_sli_pcimem_bcopy(mbox, pmbox, sizeof(uint32_t));
8781 if (pmbox->mbxOwner != OWN_HOST) {
James Smart5b75da22008-12-04 22:39:35 -05008782 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart92d7f7b2007-06-17 19:56:38 -05008783 /*
8784 * Stray Mailbox Interrupt, mbxCommand <cmd>
8785 * mbxStatus <status>
8786 */
James Smart09372822008-01-11 01:52:54 -05008787 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
James Smart92d7f7b2007-06-17 19:56:38 -05008788 LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04008789 "(%d):0304 Stray Mailbox "
James Smart92d7f7b2007-06-17 19:56:38 -05008790 "Interrupt mbxCommand x%x "
8791 "mbxStatus x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04008792 (vport ? vport->vpi : 0),
James Smart92d7f7b2007-06-17 19:56:38 -05008793 pmbox->mbxCommand,
8794 pmbox->mbxStatus);
James Smart09372822008-01-11 01:52:54 -05008795 /* clear mailbox attention bit */
8796 work_ha_copy &= ~HA_MBATT;
8797 } else {
James Smart97eab632008-04-07 10:16:05 -04008798 phba->sli.mbox_active = NULL;
James Smart5b75da22008-12-04 22:39:35 -05008799 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart09372822008-01-11 01:52:54 -05008800 phba->last_completion_time = jiffies;
8801 del_timer(&phba->sli.mbox_tmo);
James Smart09372822008-01-11 01:52:54 -05008802 if (pmb->mbox_cmpl) {
8803 lpfc_sli_pcimem_bcopy(mbox, pmbox,
8804 MAILBOX_CMD_SIZE);
James Smart7a470272010-03-15 11:25:20 -04008805 if (pmb->out_ext_byte_len &&
8806 pmb->context2)
8807 lpfc_sli_pcimem_bcopy(
8808 phba->mbox_ext,
8809 pmb->context2,
8810 pmb->out_ext_byte_len);
James Smart858c9f62007-06-17 19:56:39 -05008811 }
James Smart09372822008-01-11 01:52:54 -05008812 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
8813 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
8814
8815 lpfc_debugfs_disc_trc(vport,
8816 LPFC_DISC_TRC_MBOX_VPORT,
8817 "MBOX dflt rpi: : "
8818 "status:x%x rpi:x%x",
8819 (uint32_t)pmbox->mbxStatus,
8820 pmbox->un.varWords[0], 0);
8821
8822 if (!pmbox->mbxStatus) {
8823 mp = (struct lpfc_dmabuf *)
8824 (pmb->context1);
8825 ndlp = (struct lpfc_nodelist *)
8826 pmb->context2;
8827
8828 /* Reg_LOGIN of dflt RPI was
8829 * successful. new lets get
8830 * rid of the RPI using the
8831 * same mbox buffer.
8832 */
8833 lpfc_unreg_login(phba,
8834 vport->vpi,
8835 pmbox->un.varWords[0],
8836 pmb);
8837 pmb->mbox_cmpl =
8838 lpfc_mbx_cmpl_dflt_rpi;
8839 pmb->context1 = mp;
8840 pmb->context2 = ndlp;
8841 pmb->vport = vport;
James Smart58da1ff2008-04-07 10:15:56 -04008842 rc = lpfc_sli_issue_mbox(phba,
8843 pmb,
8844 MBX_NOWAIT);
8845 if (rc != MBX_BUSY)
8846 lpfc_printf_log(phba,
8847 KERN_ERR,
8848 LOG_MBOX | LOG_SLI,
James Smartd7c255b2008-08-24 21:50:00 -04008849 "0350 rc should have"
James Smart6a9c52c2009-10-02 15:16:51 -04008850 "been MBX_BUSY\n");
James Smart3772a992009-05-22 14:50:54 -04008851 if (rc != MBX_NOT_FINISHED)
8852 goto send_current_mbox;
James Smart09372822008-01-11 01:52:54 -05008853 }
8854 }
James Smart5b75da22008-12-04 22:39:35 -05008855 spin_lock_irqsave(
8856 &phba->pport->work_port_lock,
8857 iflag);
James Smart09372822008-01-11 01:52:54 -05008858 phba->pport->work_port_events &=
8859 ~WORKER_MBOX_TMO;
James Smart5b75da22008-12-04 22:39:35 -05008860 spin_unlock_irqrestore(
8861 &phba->pport->work_port_lock,
8862 iflag);
James Smart09372822008-01-11 01:52:54 -05008863 lpfc_mbox_cmpl_put(phba, pmb);
James Smart858c9f62007-06-17 19:56:39 -05008864 }
James Smart97eab632008-04-07 10:16:05 -04008865 } else
James Smart5b75da22008-12-04 22:39:35 -05008866 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -04008867
James Smart92d7f7b2007-06-17 19:56:38 -05008868 if ((work_ha_copy & HA_MBATT) &&
8869 (phba->sli.mbox_active == NULL)) {
James Smart858c9f62007-06-17 19:56:39 -05008870send_current_mbox:
James Smart92d7f7b2007-06-17 19:56:38 -05008871 /* Process next mailbox command if there is one */
James Smart58da1ff2008-04-07 10:15:56 -04008872 do {
8873 rc = lpfc_sli_issue_mbox(phba, NULL,
8874 MBX_NOWAIT);
8875 } while (rc == MBX_NOT_FINISHED);
8876 if (rc != MBX_SUCCESS)
8877 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
8878 LOG_SLI, "0349 rc should be "
James Smart6a9c52c2009-10-02 15:16:51 -04008879 "MBX_SUCCESS\n");
James Smart92d7f7b2007-06-17 19:56:38 -05008880 }
8881
James Smart5b75da22008-12-04 22:39:35 -05008882 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05008883 phba->work_ha |= work_ha_copy;
James Smart5b75da22008-12-04 22:39:35 -05008884 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart5e9d9b82008-06-14 22:52:53 -04008885 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -05008886 }
James Smart93996272008-08-24 21:50:30 -04008887 return IRQ_HANDLED;
dea31012005-04-17 16:05:31 -05008888
James Smart3772a992009-05-22 14:50:54 -04008889} /* lpfc_sli_sp_intr_handler */
James Smart93996272008-08-24 21:50:30 -04008890
8891/**
James Smart3772a992009-05-22 14:50:54 -04008892 * lpfc_sli_fp_intr_handler - Fast-path interrupt handler to SLI-3 device.
James Smart93996272008-08-24 21:50:30 -04008893 * @irq: Interrupt number.
8894 * @dev_id: The device context pointer.
8895 *
8896 * This function is directly called from the PCI layer as an interrupt
James Smart3772a992009-05-22 14:50:54 -04008897 * service routine when device with SLI-3 interface spec is enabled with
8898 * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
8899 * ring event in the HBA. However, when the device is enabled with either
8900 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
8901 * device-level interrupt handler. When the PCI slot is in error recovery
8902 * or the HBA is undergoing initialization, the interrupt handler will not
8903 * process the interrupt. The SCSI FCP fast-path ring event are handled in
8904 * the intrrupt context. This function is called without any lock held.
8905 * It gets the hbalock to access and update SLI data structures.
James Smart93996272008-08-24 21:50:30 -04008906 *
8907 * This function returns IRQ_HANDLED when interrupt is handled else it
8908 * returns IRQ_NONE.
8909 **/
8910irqreturn_t
James Smart3772a992009-05-22 14:50:54 -04008911lpfc_sli_fp_intr_handler(int irq, void *dev_id)
James Smart93996272008-08-24 21:50:30 -04008912{
8913 struct lpfc_hba *phba;
8914 uint32_t ha_copy;
8915 unsigned long status;
James Smart5b75da22008-12-04 22:39:35 -05008916 unsigned long iflag;
James Smart93996272008-08-24 21:50:30 -04008917
8918 /* Get the driver's phba structure from the dev_id and
8919 * assume the HBA is not interrupting.
8920 */
8921 phba = (struct lpfc_hba *) dev_id;
8922
8923 if (unlikely(!phba))
8924 return IRQ_NONE;
dea31012005-04-17 16:05:31 -05008925
8926 /*
James Smart93996272008-08-24 21:50:30 -04008927 * Stuff needs to be attented to when this function is invoked as an
8928 * individual interrupt handler in MSI-X multi-message interrupt mode
dea31012005-04-17 16:05:31 -05008929 */
James Smart93996272008-08-24 21:50:30 -04008930 if (phba->intr_type == MSIX) {
James Smart3772a992009-05-22 14:50:54 -04008931 /* Check device state for handling interrupt */
8932 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -04008933 return IRQ_NONE;
8934 /* Need to read HA REG for FCP ring and other ring events */
8935 ha_copy = readl(phba->HAregaddr);
8936 /* Clear up only attention source related to fast-path */
James Smart5b75da22008-12-04 22:39:35 -05008937 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -04008938 /*
8939 * If there is deferred error attention, do not check for
8940 * any interrupt.
8941 */
8942 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
James Smart3772a992009-05-22 14:50:54 -04008943 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -04008944 return IRQ_NONE;
8945 }
James Smart93996272008-08-24 21:50:30 -04008946 writel((ha_copy & (HA_R0_CLR_MSK | HA_R1_CLR_MSK)),
8947 phba->HAregaddr);
8948 readl(phba->HAregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -05008949 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -04008950 } else
8951 ha_copy = phba->ha_copy;
8952
8953 /*
8954 * Process all events on FCP ring. Take the optimized path for FCP IO.
8955 */
8956 ha_copy &= ~(phba->work_ha_mask);
8957
8958 status = (ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
dea31012005-04-17 16:05:31 -05008959 status >>= (4*LPFC_FCP_RING);
James Smart858c9f62007-06-17 19:56:39 -05008960 if (status & HA_RXMASK)
dea31012005-04-17 16:05:31 -05008961 lpfc_sli_handle_fast_ring_event(phba,
8962 &phba->sli.ring[LPFC_FCP_RING],
8963 status);
James Smarta4bc3372006-12-02 13:34:16 -05008964
8965 if (phba->cfg_multi_ring_support == 2) {
8966 /*
James Smart93996272008-08-24 21:50:30 -04008967 * Process all events on extra ring. Take the optimized path
8968 * for extra ring IO.
James Smarta4bc3372006-12-02 13:34:16 -05008969 */
James Smart93996272008-08-24 21:50:30 -04008970 status = (ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
James Smarta4bc3372006-12-02 13:34:16 -05008971 status >>= (4*LPFC_EXTRA_RING);
James Smart858c9f62007-06-17 19:56:39 -05008972 if (status & HA_RXMASK) {
James Smarta4bc3372006-12-02 13:34:16 -05008973 lpfc_sli_handle_fast_ring_event(phba,
8974 &phba->sli.ring[LPFC_EXTRA_RING],
8975 status);
8976 }
8977 }
dea31012005-04-17 16:05:31 -05008978 return IRQ_HANDLED;
James Smart3772a992009-05-22 14:50:54 -04008979} /* lpfc_sli_fp_intr_handler */
dea31012005-04-17 16:05:31 -05008980
James Smart93996272008-08-24 21:50:30 -04008981/**
James Smart3772a992009-05-22 14:50:54 -04008982 * lpfc_sli_intr_handler - Device-level interrupt handler to SLI-3 device
James Smart93996272008-08-24 21:50:30 -04008983 * @irq: Interrupt number.
8984 * @dev_id: The device context pointer.
8985 *
James Smart3772a992009-05-22 14:50:54 -04008986 * This function is the HBA device-level interrupt handler to device with
8987 * SLI-3 interface spec, called from the PCI layer when either MSI or
8988 * Pin-IRQ interrupt mode is enabled and there is an event in the HBA which
8989 * requires driver attention. This function invokes the slow-path interrupt
8990 * attention handling function and fast-path interrupt attention handling
8991 * function in turn to process the relevant HBA attention events. This
8992 * function is called without any lock held. It gets the hbalock to access
8993 * and update SLI data structures.
James Smart93996272008-08-24 21:50:30 -04008994 *
8995 * This function returns IRQ_HANDLED when interrupt is handled, else it
8996 * returns IRQ_NONE.
8997 **/
8998irqreturn_t
James Smart3772a992009-05-22 14:50:54 -04008999lpfc_sli_intr_handler(int irq, void *dev_id)
James Smart93996272008-08-24 21:50:30 -04009000{
9001 struct lpfc_hba *phba;
9002 irqreturn_t sp_irq_rc, fp_irq_rc;
9003 unsigned long status1, status2;
James Smarta747c9c2009-11-18 15:41:10 -05009004 uint32_t hc_copy;
James Smart93996272008-08-24 21:50:30 -04009005
9006 /*
9007 * Get the driver's phba structure from the dev_id and
9008 * assume the HBA is not interrupting.
9009 */
9010 phba = (struct lpfc_hba *) dev_id;
9011
9012 if (unlikely(!phba))
9013 return IRQ_NONE;
9014
James Smart3772a992009-05-22 14:50:54 -04009015 /* Check device state for handling interrupt */
9016 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -04009017 return IRQ_NONE;
9018
9019 spin_lock(&phba->hbalock);
9020 phba->ha_copy = readl(phba->HAregaddr);
9021 if (unlikely(!phba->ha_copy)) {
9022 spin_unlock(&phba->hbalock);
9023 return IRQ_NONE;
9024 } else if (phba->ha_copy & HA_ERATT) {
9025 if (phba->hba_flag & HBA_ERATT_HANDLED)
9026 /* ERATT polling has handled ERATT */
9027 phba->ha_copy &= ~HA_ERATT;
9028 else
9029 /* Indicate interrupt handler handles ERATT */
9030 phba->hba_flag |= HBA_ERATT_HANDLED;
9031 }
9032
James Smarta257bf92009-04-06 18:48:10 -04009033 /*
9034 * If there is deferred error attention, do not check for any interrupt.
9035 */
9036 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
Dan Carpenterec21b3b2010-08-08 00:15:17 +02009037 spin_unlock(&phba->hbalock);
James Smarta257bf92009-04-06 18:48:10 -04009038 return IRQ_NONE;
9039 }
9040
James Smart93996272008-08-24 21:50:30 -04009041 /* Clear attention sources except link and error attentions */
James Smarta747c9c2009-11-18 15:41:10 -05009042 hc_copy = readl(phba->HCregaddr);
9043 writel(hc_copy & ~(HC_MBINT_ENA | HC_R0INT_ENA | HC_R1INT_ENA
9044 | HC_R2INT_ENA | HC_LAINT_ENA | HC_ERINT_ENA),
9045 phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -04009046 writel((phba->ha_copy & ~(HA_LATT | HA_ERATT)), phba->HAregaddr);
James Smarta747c9c2009-11-18 15:41:10 -05009047 writel(hc_copy, phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -04009048 readl(phba->HAregaddr); /* flush */
9049 spin_unlock(&phba->hbalock);
9050
9051 /*
9052 * Invokes slow-path host attention interrupt handling as appropriate.
9053 */
9054
9055 /* status of events with mailbox and link attention */
9056 status1 = phba->ha_copy & (HA_MBATT | HA_LATT | HA_ERATT);
9057
9058 /* status of events with ELS ring */
9059 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_ELS_RING)));
9060 status2 >>= (4*LPFC_ELS_RING);
9061
9062 if (status1 || (status2 & HA_RXMASK))
James Smart3772a992009-05-22 14:50:54 -04009063 sp_irq_rc = lpfc_sli_sp_intr_handler(irq, dev_id);
James Smart93996272008-08-24 21:50:30 -04009064 else
9065 sp_irq_rc = IRQ_NONE;
9066
9067 /*
9068 * Invoke fast-path host attention interrupt handling as appropriate.
9069 */
9070
9071 /* status of events with FCP ring */
9072 status1 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
9073 status1 >>= (4*LPFC_FCP_RING);
9074
9075 /* status of events with extra ring */
9076 if (phba->cfg_multi_ring_support == 2) {
9077 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
9078 status2 >>= (4*LPFC_EXTRA_RING);
9079 } else
9080 status2 = 0;
9081
9082 if ((status1 & HA_RXMASK) || (status2 & HA_RXMASK))
James Smart3772a992009-05-22 14:50:54 -04009083 fp_irq_rc = lpfc_sli_fp_intr_handler(irq, dev_id);
James Smart93996272008-08-24 21:50:30 -04009084 else
9085 fp_irq_rc = IRQ_NONE;
9086
9087 /* Return device-level interrupt handling status */
9088 return (sp_irq_rc == IRQ_HANDLED) ? sp_irq_rc : fp_irq_rc;
James Smart3772a992009-05-22 14:50:54 -04009089} /* lpfc_sli_intr_handler */
James Smart4f774512009-05-22 14:52:35 -04009090
9091/**
9092 * lpfc_sli4_fcp_xri_abort_event_proc - Process fcp xri abort event
9093 * @phba: pointer to lpfc hba data structure.
9094 *
9095 * This routine is invoked by the worker thread to process all the pending
9096 * SLI4 FCP abort XRI events.
9097 **/
9098void lpfc_sli4_fcp_xri_abort_event_proc(struct lpfc_hba *phba)
9099{
9100 struct lpfc_cq_event *cq_event;
9101
9102 /* First, declare the fcp xri abort event has been handled */
9103 spin_lock_irq(&phba->hbalock);
9104 phba->hba_flag &= ~FCP_XRI_ABORT_EVENT;
9105 spin_unlock_irq(&phba->hbalock);
9106 /* Now, handle all the fcp xri abort events */
9107 while (!list_empty(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue)) {
9108 /* Get the first event from the head of the event queue */
9109 spin_lock_irq(&phba->hbalock);
9110 list_remove_head(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue,
9111 cq_event, struct lpfc_cq_event, list);
9112 spin_unlock_irq(&phba->hbalock);
9113 /* Notify aborted XRI for FCP work queue */
9114 lpfc_sli4_fcp_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
9115 /* Free the event processed back to the free pool */
9116 lpfc_sli4_cq_event_release(phba, cq_event);
9117 }
9118}
9119
9120/**
9121 * lpfc_sli4_els_xri_abort_event_proc - Process els xri abort event
9122 * @phba: pointer to lpfc hba data structure.
9123 *
9124 * This routine is invoked by the worker thread to process all the pending
9125 * SLI4 els abort xri events.
9126 **/
9127void lpfc_sli4_els_xri_abort_event_proc(struct lpfc_hba *phba)
9128{
9129 struct lpfc_cq_event *cq_event;
9130
9131 /* First, declare the els xri abort event has been handled */
9132 spin_lock_irq(&phba->hbalock);
9133 phba->hba_flag &= ~ELS_XRI_ABORT_EVENT;
9134 spin_unlock_irq(&phba->hbalock);
9135 /* Now, handle all the els xri abort events */
9136 while (!list_empty(&phba->sli4_hba.sp_els_xri_aborted_work_queue)) {
9137 /* Get the first event from the head of the event queue */
9138 spin_lock_irq(&phba->hbalock);
9139 list_remove_head(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
9140 cq_event, struct lpfc_cq_event, list);
9141 spin_unlock_irq(&phba->hbalock);
9142 /* Notify aborted XRI for ELS work queue */
9143 lpfc_sli4_els_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
9144 /* Free the event processed back to the free pool */
9145 lpfc_sli4_cq_event_release(phba, cq_event);
9146 }
9147}
9148
James Smart341af102010-01-26 23:07:37 -05009149/**
9150 * lpfc_sli4_iocb_param_transfer - Transfer pIocbOut and cmpl status to pIocbIn
9151 * @phba: pointer to lpfc hba data structure
9152 * @pIocbIn: pointer to the rspiocbq
9153 * @pIocbOut: pointer to the cmdiocbq
9154 * @wcqe: pointer to the complete wcqe
9155 *
9156 * This routine transfers the fields of a command iocbq to a response iocbq
9157 * by copying all the IOCB fields from command iocbq and transferring the
9158 * completion status information from the complete wcqe.
9159 **/
James Smart4f774512009-05-22 14:52:35 -04009160static void
James Smart341af102010-01-26 23:07:37 -05009161lpfc_sli4_iocb_param_transfer(struct lpfc_hba *phba,
9162 struct lpfc_iocbq *pIocbIn,
James Smart4f774512009-05-22 14:52:35 -04009163 struct lpfc_iocbq *pIocbOut,
9164 struct lpfc_wcqe_complete *wcqe)
9165{
James Smart341af102010-01-26 23:07:37 -05009166 unsigned long iflags;
James Smart4f774512009-05-22 14:52:35 -04009167 size_t offset = offsetof(struct lpfc_iocbq, iocb);
9168
9169 memcpy((char *)pIocbIn + offset, (char *)pIocbOut + offset,
9170 sizeof(struct lpfc_iocbq) - offset);
James Smart4f774512009-05-22 14:52:35 -04009171 /* Map WCQE parameters into irspiocb parameters */
9172 pIocbIn->iocb.ulpStatus = bf_get(lpfc_wcqe_c_status, wcqe);
9173 if (pIocbOut->iocb_flag & LPFC_IO_FCP)
9174 if (pIocbIn->iocb.ulpStatus == IOSTAT_FCP_RSP_ERROR)
9175 pIocbIn->iocb.un.fcpi.fcpi_parm =
9176 pIocbOut->iocb.un.fcpi.fcpi_parm -
9177 wcqe->total_data_placed;
9178 else
9179 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
James Smart695a8142010-01-26 23:08:03 -05009180 else {
James Smart4f774512009-05-22 14:52:35 -04009181 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
James Smart695a8142010-01-26 23:08:03 -05009182 pIocbIn->iocb.un.genreq64.bdl.bdeSize = wcqe->total_data_placed;
9183 }
James Smart341af102010-01-26 23:07:37 -05009184
9185 /* Pick up HBA exchange busy condition */
9186 if (bf_get(lpfc_wcqe_c_xb, wcqe)) {
9187 spin_lock_irqsave(&phba->hbalock, iflags);
9188 pIocbIn->iocb_flag |= LPFC_EXCHANGE_BUSY;
9189 spin_unlock_irqrestore(&phba->hbalock, iflags);
9190 }
James Smart4f774512009-05-22 14:52:35 -04009191}
9192
9193/**
James Smart45ed1192009-10-02 15:17:02 -04009194 * lpfc_sli4_els_wcqe_to_rspiocbq - Get response iocbq from els wcqe
9195 * @phba: Pointer to HBA context object.
9196 * @wcqe: Pointer to work-queue completion queue entry.
9197 *
9198 * This routine handles an ELS work-queue completion event and construct
9199 * a pseudo response ELS IODBQ from the SLI4 ELS WCQE for the common
9200 * discovery engine to handle.
9201 *
9202 * Return: Pointer to the receive IOCBQ, NULL otherwise.
9203 **/
9204static struct lpfc_iocbq *
9205lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *phba,
9206 struct lpfc_iocbq *irspiocbq)
9207{
9208 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
9209 struct lpfc_iocbq *cmdiocbq;
9210 struct lpfc_wcqe_complete *wcqe;
9211 unsigned long iflags;
9212
9213 wcqe = &irspiocbq->cq_event.cqe.wcqe_cmpl;
9214 spin_lock_irqsave(&phba->hbalock, iflags);
9215 pring->stats.iocb_event++;
9216 /* Look up the ELS command IOCB and create pseudo response IOCB */
9217 cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
9218 bf_get(lpfc_wcqe_c_request_tag, wcqe));
9219 spin_unlock_irqrestore(&phba->hbalock, iflags);
9220
9221 if (unlikely(!cmdiocbq)) {
9222 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9223 "0386 ELS complete with no corresponding "
9224 "cmdiocb: iotag (%d)\n",
9225 bf_get(lpfc_wcqe_c_request_tag, wcqe));
9226 lpfc_sli_release_iocbq(phba, irspiocbq);
9227 return NULL;
9228 }
9229
9230 /* Fake the irspiocbq and copy necessary response information */
James Smart341af102010-01-26 23:07:37 -05009231 lpfc_sli4_iocb_param_transfer(phba, irspiocbq, cmdiocbq, wcqe);
James Smart45ed1192009-10-02 15:17:02 -04009232
9233 return irspiocbq;
9234}
9235
9236/**
James Smart04c68492009-05-22 14:52:52 -04009237 * lpfc_sli4_sp_handle_async_event - Handle an asynchroous event
9238 * @phba: Pointer to HBA context object.
9239 * @cqe: Pointer to mailbox completion queue entry.
9240 *
9241 * This routine process a mailbox completion queue entry with asynchrous
9242 * event.
9243 *
9244 * Return: true if work posted to worker thread, otherwise false.
9245 **/
9246static bool
9247lpfc_sli4_sp_handle_async_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
9248{
9249 struct lpfc_cq_event *cq_event;
9250 unsigned long iflags;
9251
9252 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
9253 "0392 Async Event: word0:x%x, word1:x%x, "
9254 "word2:x%x, word3:x%x\n", mcqe->word0,
9255 mcqe->mcqe_tag0, mcqe->mcqe_tag1, mcqe->trailer);
9256
9257 /* Allocate a new internal CQ_EVENT entry */
9258 cq_event = lpfc_sli4_cq_event_alloc(phba);
9259 if (!cq_event) {
9260 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9261 "0394 Failed to allocate CQ_EVENT entry\n");
9262 return false;
9263 }
9264
9265 /* Move the CQE into an asynchronous event entry */
9266 memcpy(&cq_event->cqe, mcqe, sizeof(struct lpfc_mcqe));
9267 spin_lock_irqsave(&phba->hbalock, iflags);
9268 list_add_tail(&cq_event->list, &phba->sli4_hba.sp_asynce_work_queue);
9269 /* Set the async event flag */
9270 phba->hba_flag |= ASYNC_EVENT;
9271 spin_unlock_irqrestore(&phba->hbalock, iflags);
9272
9273 return true;
9274}
9275
9276/**
9277 * lpfc_sli4_sp_handle_mbox_event - Handle a mailbox completion event
9278 * @phba: Pointer to HBA context object.
9279 * @cqe: Pointer to mailbox completion queue entry.
9280 *
9281 * This routine process a mailbox completion queue entry with mailbox
9282 * completion event.
9283 *
9284 * Return: true if work posted to worker thread, otherwise false.
9285 **/
9286static bool
9287lpfc_sli4_sp_handle_mbox_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
9288{
9289 uint32_t mcqe_status;
9290 MAILBOX_t *mbox, *pmbox;
9291 struct lpfc_mqe *mqe;
9292 struct lpfc_vport *vport;
9293 struct lpfc_nodelist *ndlp;
9294 struct lpfc_dmabuf *mp;
9295 unsigned long iflags;
9296 LPFC_MBOXQ_t *pmb;
9297 bool workposted = false;
9298 int rc;
9299
9300 /* If not a mailbox complete MCQE, out by checking mailbox consume */
9301 if (!bf_get(lpfc_trailer_completed, mcqe))
9302 goto out_no_mqe_complete;
9303
9304 /* Get the reference to the active mbox command */
9305 spin_lock_irqsave(&phba->hbalock, iflags);
9306 pmb = phba->sli.mbox_active;
9307 if (unlikely(!pmb)) {
9308 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
9309 "1832 No pending MBOX command to handle\n");
9310 spin_unlock_irqrestore(&phba->hbalock, iflags);
9311 goto out_no_mqe_complete;
9312 }
9313 spin_unlock_irqrestore(&phba->hbalock, iflags);
9314 mqe = &pmb->u.mqe;
9315 pmbox = (MAILBOX_t *)&pmb->u.mqe;
9316 mbox = phba->mbox;
9317 vport = pmb->vport;
9318
9319 /* Reset heartbeat timer */
9320 phba->last_completion_time = jiffies;
9321 del_timer(&phba->sli.mbox_tmo);
9322
9323 /* Move mbox data to caller's mailbox region, do endian swapping */
9324 if (pmb->mbox_cmpl && mbox)
9325 lpfc_sli_pcimem_bcopy(mbox, mqe, sizeof(struct lpfc_mqe));
9326 /* Set the mailbox status with SLI4 range 0x4000 */
9327 mcqe_status = bf_get(lpfc_mcqe_status, mcqe);
9328 if (mcqe_status != MB_CQE_STATUS_SUCCESS)
9329 bf_set(lpfc_mqe_status, mqe,
9330 (LPFC_MBX_ERROR_RANGE | mcqe_status));
9331
9332 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
9333 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
9334 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_MBOX_VPORT,
9335 "MBOX dflt rpi: status:x%x rpi:x%x",
9336 mcqe_status,
9337 pmbox->un.varWords[0], 0);
9338 if (mcqe_status == MB_CQE_STATUS_SUCCESS) {
9339 mp = (struct lpfc_dmabuf *)(pmb->context1);
9340 ndlp = (struct lpfc_nodelist *)pmb->context2;
9341 /* Reg_LOGIN of dflt RPI was successful. Now lets get
9342 * RID of the PPI using the same mbox buffer.
9343 */
9344 lpfc_unreg_login(phba, vport->vpi,
9345 pmbox->un.varWords[0], pmb);
9346 pmb->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
9347 pmb->context1 = mp;
9348 pmb->context2 = ndlp;
9349 pmb->vport = vport;
9350 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
9351 if (rc != MBX_BUSY)
9352 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
9353 LOG_SLI, "0385 rc should "
9354 "have been MBX_BUSY\n");
9355 if (rc != MBX_NOT_FINISHED)
9356 goto send_current_mbox;
9357 }
9358 }
9359 spin_lock_irqsave(&phba->pport->work_port_lock, iflags);
9360 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
9361 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflags);
9362
9363 /* There is mailbox completion work to do */
9364 spin_lock_irqsave(&phba->hbalock, iflags);
9365 __lpfc_mbox_cmpl_put(phba, pmb);
9366 phba->work_ha |= HA_MBATT;
9367 spin_unlock_irqrestore(&phba->hbalock, iflags);
9368 workposted = true;
9369
9370send_current_mbox:
9371 spin_lock_irqsave(&phba->hbalock, iflags);
9372 /* Release the mailbox command posting token */
9373 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
9374 /* Setting active mailbox pointer need to be in sync to flag clear */
9375 phba->sli.mbox_active = NULL;
9376 spin_unlock_irqrestore(&phba->hbalock, iflags);
9377 /* Wake up worker thread to post the next pending mailbox command */
9378 lpfc_worker_wake_up(phba);
9379out_no_mqe_complete:
9380 if (bf_get(lpfc_trailer_consumed, mcqe))
9381 lpfc_sli4_mq_release(phba->sli4_hba.mbx_wq);
9382 return workposted;
9383}
9384
9385/**
9386 * lpfc_sli4_sp_handle_mcqe - Process a mailbox completion queue entry
9387 * @phba: Pointer to HBA context object.
9388 * @cqe: Pointer to mailbox completion queue entry.
9389 *
9390 * This routine process a mailbox completion queue entry, it invokes the
9391 * proper mailbox complete handling or asynchrous event handling routine
9392 * according to the MCQE's async bit.
9393 *
9394 * Return: true if work posted to worker thread, otherwise false.
9395 **/
9396static bool
9397lpfc_sli4_sp_handle_mcqe(struct lpfc_hba *phba, struct lpfc_cqe *cqe)
9398{
9399 struct lpfc_mcqe mcqe;
9400 bool workposted;
9401
9402 /* Copy the mailbox MCQE and convert endian order as needed */
9403 lpfc_sli_pcimem_bcopy(cqe, &mcqe, sizeof(struct lpfc_mcqe));
9404
9405 /* Invoke the proper event handling routine */
9406 if (!bf_get(lpfc_trailer_async, &mcqe))
9407 workposted = lpfc_sli4_sp_handle_mbox_event(phba, &mcqe);
9408 else
9409 workposted = lpfc_sli4_sp_handle_async_event(phba, &mcqe);
9410 return workposted;
9411}
9412
9413/**
James Smart4f774512009-05-22 14:52:35 -04009414 * lpfc_sli4_sp_handle_els_wcqe - Handle els work-queue completion event
9415 * @phba: Pointer to HBA context object.
9416 * @wcqe: Pointer to work-queue completion queue entry.
9417 *
9418 * This routine handles an ELS work-queue completion event.
9419 *
9420 * Return: true if work posted to worker thread, otherwise false.
9421 **/
9422static bool
9423lpfc_sli4_sp_handle_els_wcqe(struct lpfc_hba *phba,
9424 struct lpfc_wcqe_complete *wcqe)
9425{
James Smart4f774512009-05-22 14:52:35 -04009426 struct lpfc_iocbq *irspiocbq;
9427 unsigned long iflags;
James Smart2a9bf3d2010-06-07 15:24:45 -04009428 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_FCP_RING];
James Smart4f774512009-05-22 14:52:35 -04009429
James Smart45ed1192009-10-02 15:17:02 -04009430 /* Get an irspiocbq for later ELS response processing use */
James Smart4f774512009-05-22 14:52:35 -04009431 irspiocbq = lpfc_sli_get_iocbq(phba);
9432 if (!irspiocbq) {
9433 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart2a9bf3d2010-06-07 15:24:45 -04009434 "0387 NO IOCBQ data: txq_cnt=%d iocb_cnt=%d "
9435 "fcp_txcmplq_cnt=%d, els_txcmplq_cnt=%d\n",
9436 pring->txq_cnt, phba->iocb_cnt,
9437 phba->sli.ring[LPFC_FCP_RING].txcmplq_cnt,
9438 phba->sli.ring[LPFC_ELS_RING].txcmplq_cnt);
James Smart45ed1192009-10-02 15:17:02 -04009439 return false;
James Smart4f774512009-05-22 14:52:35 -04009440 }
James Smart4f774512009-05-22 14:52:35 -04009441
James Smart45ed1192009-10-02 15:17:02 -04009442 /* Save off the slow-path queue event for work thread to process */
9443 memcpy(&irspiocbq->cq_event.cqe.wcqe_cmpl, wcqe, sizeof(*wcqe));
James Smart4f774512009-05-22 14:52:35 -04009444 spin_lock_irqsave(&phba->hbalock, iflags);
James Smart4d9ab992009-10-02 15:16:39 -04009445 list_add_tail(&irspiocbq->cq_event.list,
James Smart45ed1192009-10-02 15:17:02 -04009446 &phba->sli4_hba.sp_queue_event);
9447 phba->hba_flag |= HBA_SP_QUEUE_EVT;
James Smart4f774512009-05-22 14:52:35 -04009448 spin_unlock_irqrestore(&phba->hbalock, iflags);
James Smart4f774512009-05-22 14:52:35 -04009449
James Smart45ed1192009-10-02 15:17:02 -04009450 return true;
James Smart4f774512009-05-22 14:52:35 -04009451}
9452
9453/**
9454 * lpfc_sli4_sp_handle_rel_wcqe - Handle slow-path WQ entry consumed event
9455 * @phba: Pointer to HBA context object.
9456 * @wcqe: Pointer to work-queue completion queue entry.
9457 *
9458 * This routine handles slow-path WQ entry comsumed event by invoking the
9459 * proper WQ release routine to the slow-path WQ.
9460 **/
9461static void
9462lpfc_sli4_sp_handle_rel_wcqe(struct lpfc_hba *phba,
9463 struct lpfc_wcqe_release *wcqe)
9464{
9465 /* Check for the slow-path ELS work queue */
9466 if (bf_get(lpfc_wcqe_r_wq_id, wcqe) == phba->sli4_hba.els_wq->queue_id)
9467 lpfc_sli4_wq_release(phba->sli4_hba.els_wq,
9468 bf_get(lpfc_wcqe_r_wqe_index, wcqe));
9469 else
9470 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9471 "2579 Slow-path wqe consume event carries "
9472 "miss-matched qid: wcqe-qid=x%x, sp-qid=x%x\n",
9473 bf_get(lpfc_wcqe_r_wqe_index, wcqe),
9474 phba->sli4_hba.els_wq->queue_id);
9475}
9476
9477/**
9478 * lpfc_sli4_sp_handle_abort_xri_wcqe - Handle a xri abort event
9479 * @phba: Pointer to HBA context object.
9480 * @cq: Pointer to a WQ completion queue.
9481 * @wcqe: Pointer to work-queue completion queue entry.
9482 *
9483 * This routine handles an XRI abort event.
9484 *
9485 * Return: true if work posted to worker thread, otherwise false.
9486 **/
9487static bool
9488lpfc_sli4_sp_handle_abort_xri_wcqe(struct lpfc_hba *phba,
9489 struct lpfc_queue *cq,
9490 struct sli4_wcqe_xri_aborted *wcqe)
9491{
9492 bool workposted = false;
9493 struct lpfc_cq_event *cq_event;
9494 unsigned long iflags;
9495
9496 /* Allocate a new internal CQ_EVENT entry */
9497 cq_event = lpfc_sli4_cq_event_alloc(phba);
9498 if (!cq_event) {
9499 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9500 "0602 Failed to allocate CQ_EVENT entry\n");
9501 return false;
9502 }
9503
9504 /* Move the CQE into the proper xri abort event list */
9505 memcpy(&cq_event->cqe, wcqe, sizeof(struct sli4_wcqe_xri_aborted));
9506 switch (cq->subtype) {
9507 case LPFC_FCP:
9508 spin_lock_irqsave(&phba->hbalock, iflags);
9509 list_add_tail(&cq_event->list,
9510 &phba->sli4_hba.sp_fcp_xri_aborted_work_queue);
9511 /* Set the fcp xri abort event flag */
9512 phba->hba_flag |= FCP_XRI_ABORT_EVENT;
9513 spin_unlock_irqrestore(&phba->hbalock, iflags);
9514 workposted = true;
9515 break;
9516 case LPFC_ELS:
9517 spin_lock_irqsave(&phba->hbalock, iflags);
9518 list_add_tail(&cq_event->list,
9519 &phba->sli4_hba.sp_els_xri_aborted_work_queue);
9520 /* Set the els xri abort event flag */
9521 phba->hba_flag |= ELS_XRI_ABORT_EVENT;
9522 spin_unlock_irqrestore(&phba->hbalock, iflags);
9523 workposted = true;
9524 break;
9525 default:
9526 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9527 "0603 Invalid work queue CQE subtype (x%x)\n",
9528 cq->subtype);
9529 workposted = false;
9530 break;
9531 }
9532 return workposted;
9533}
9534
9535/**
James Smart4d9ab992009-10-02 15:16:39 -04009536 * lpfc_sli4_sp_handle_rcqe - Process a receive-queue completion queue entry
James Smart4f774512009-05-22 14:52:35 -04009537 * @phba: Pointer to HBA context object.
James Smart4d9ab992009-10-02 15:16:39 -04009538 * @rcqe: Pointer to receive-queue completion queue entry.
James Smart4f774512009-05-22 14:52:35 -04009539 *
James Smart4d9ab992009-10-02 15:16:39 -04009540 * This routine process a receive-queue completion queue entry.
James Smart4f774512009-05-22 14:52:35 -04009541 *
9542 * Return: true if work posted to worker thread, otherwise false.
9543 **/
9544static bool
James Smart4d9ab992009-10-02 15:16:39 -04009545lpfc_sli4_sp_handle_rcqe(struct lpfc_hba *phba, struct lpfc_rcqe *rcqe)
9546{
9547 bool workposted = false;
9548 struct lpfc_queue *hrq = phba->sli4_hba.hdr_rq;
9549 struct lpfc_queue *drq = phba->sli4_hba.dat_rq;
9550 struct hbq_dmabuf *dma_buf;
9551 uint32_t status;
9552 unsigned long iflags;
9553
James Smart4d9ab992009-10-02 15:16:39 -04009554 if (bf_get(lpfc_rcqe_rq_id, rcqe) != hrq->queue_id)
9555 goto out;
9556
9557 status = bf_get(lpfc_rcqe_status, rcqe);
9558 switch (status) {
9559 case FC_STATUS_RQ_BUF_LEN_EXCEEDED:
9560 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9561 "2537 Receive Frame Truncated!!\n");
9562 case FC_STATUS_RQ_SUCCESS:
James Smart5ffc2662009-11-18 15:39:44 -05009563 lpfc_sli4_rq_release(hrq, drq);
James Smart4d9ab992009-10-02 15:16:39 -04009564 spin_lock_irqsave(&phba->hbalock, iflags);
9565 dma_buf = lpfc_sli_hbqbuf_get(&phba->hbqs[0].hbq_buffer_list);
9566 if (!dma_buf) {
9567 spin_unlock_irqrestore(&phba->hbalock, iflags);
9568 goto out;
9569 }
9570 memcpy(&dma_buf->cq_event.cqe.rcqe_cmpl, rcqe, sizeof(*rcqe));
9571 /* save off the frame for the word thread to process */
9572 list_add_tail(&dma_buf->cq_event.list,
James Smart45ed1192009-10-02 15:17:02 -04009573 &phba->sli4_hba.sp_queue_event);
James Smart4d9ab992009-10-02 15:16:39 -04009574 /* Frame received */
James Smart45ed1192009-10-02 15:17:02 -04009575 phba->hba_flag |= HBA_SP_QUEUE_EVT;
James Smart4d9ab992009-10-02 15:16:39 -04009576 spin_unlock_irqrestore(&phba->hbalock, iflags);
9577 workposted = true;
9578 break;
9579 case FC_STATUS_INSUFF_BUF_NEED_BUF:
9580 case FC_STATUS_INSUFF_BUF_FRM_DISC:
9581 /* Post more buffers if possible */
9582 spin_lock_irqsave(&phba->hbalock, iflags);
9583 phba->hba_flag |= HBA_POST_RECEIVE_BUFFER;
9584 spin_unlock_irqrestore(&phba->hbalock, iflags);
9585 workposted = true;
9586 break;
9587 }
9588out:
9589 return workposted;
James Smart4d9ab992009-10-02 15:16:39 -04009590}
9591
9592/**
9593 * lpfc_sli4_sp_handle_cqe - Process a slow path completion queue entry
9594 * @phba: Pointer to HBA context object.
9595 * @cq: Pointer to the completion queue.
9596 * @wcqe: Pointer to a completion queue entry.
9597 *
9598 * This routine process a slow-path work-queue or recieve queue completion queue
9599 * entry.
9600 *
9601 * Return: true if work posted to worker thread, otherwise false.
9602 **/
9603static bool
9604lpfc_sli4_sp_handle_cqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
James Smart4f774512009-05-22 14:52:35 -04009605 struct lpfc_cqe *cqe)
9606{
James Smart45ed1192009-10-02 15:17:02 -04009607 struct lpfc_cqe cqevt;
James Smart4f774512009-05-22 14:52:35 -04009608 bool workposted = false;
9609
9610 /* Copy the work queue CQE and convert endian order if needed */
James Smart45ed1192009-10-02 15:17:02 -04009611 lpfc_sli_pcimem_bcopy(cqe, &cqevt, sizeof(struct lpfc_cqe));
James Smart4f774512009-05-22 14:52:35 -04009612
9613 /* Check and process for different type of WCQE and dispatch */
James Smart45ed1192009-10-02 15:17:02 -04009614 switch (bf_get(lpfc_cqe_code, &cqevt)) {
James Smart4f774512009-05-22 14:52:35 -04009615 case CQE_CODE_COMPL_WQE:
James Smart45ed1192009-10-02 15:17:02 -04009616 /* Process the WQ/RQ complete event */
James Smartbc739052010-08-04 16:11:18 -04009617 phba->last_completion_time = jiffies;
James Smart4f774512009-05-22 14:52:35 -04009618 workposted = lpfc_sli4_sp_handle_els_wcqe(phba,
James Smart45ed1192009-10-02 15:17:02 -04009619 (struct lpfc_wcqe_complete *)&cqevt);
James Smart4f774512009-05-22 14:52:35 -04009620 break;
9621 case CQE_CODE_RELEASE_WQE:
9622 /* Process the WQ release event */
9623 lpfc_sli4_sp_handle_rel_wcqe(phba,
James Smart45ed1192009-10-02 15:17:02 -04009624 (struct lpfc_wcqe_release *)&cqevt);
James Smart4f774512009-05-22 14:52:35 -04009625 break;
9626 case CQE_CODE_XRI_ABORTED:
9627 /* Process the WQ XRI abort event */
James Smartbc739052010-08-04 16:11:18 -04009628 phba->last_completion_time = jiffies;
James Smart4f774512009-05-22 14:52:35 -04009629 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
James Smart45ed1192009-10-02 15:17:02 -04009630 (struct sli4_wcqe_xri_aborted *)&cqevt);
James Smart4f774512009-05-22 14:52:35 -04009631 break;
James Smart4d9ab992009-10-02 15:16:39 -04009632 case CQE_CODE_RECEIVE:
9633 /* Process the RQ event */
James Smartbc739052010-08-04 16:11:18 -04009634 phba->last_completion_time = jiffies;
James Smart4d9ab992009-10-02 15:16:39 -04009635 workposted = lpfc_sli4_sp_handle_rcqe(phba,
James Smart45ed1192009-10-02 15:17:02 -04009636 (struct lpfc_rcqe *)&cqevt);
James Smart4d9ab992009-10-02 15:16:39 -04009637 break;
James Smart4f774512009-05-22 14:52:35 -04009638 default:
9639 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9640 "0388 Not a valid WCQE code: x%x\n",
James Smart45ed1192009-10-02 15:17:02 -04009641 bf_get(lpfc_cqe_code, &cqevt));
James Smart4f774512009-05-22 14:52:35 -04009642 break;
9643 }
9644 return workposted;
9645}
9646
9647/**
James Smart4f774512009-05-22 14:52:35 -04009648 * lpfc_sli4_sp_handle_eqe - Process a slow-path event queue entry
9649 * @phba: Pointer to HBA context object.
9650 * @eqe: Pointer to fast-path event queue entry.
9651 *
9652 * This routine process a event queue entry from the slow-path event queue.
9653 * It will check the MajorCode and MinorCode to determine this is for a
9654 * completion event on a completion queue, if not, an error shall be logged
9655 * and just return. Otherwise, it will get to the corresponding completion
9656 * queue and process all the entries on that completion queue, rearm the
9657 * completion queue, and then return.
9658 *
9659 **/
9660static void
9661lpfc_sli4_sp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe)
9662{
9663 struct lpfc_queue *cq = NULL, *childq, *speq;
9664 struct lpfc_cqe *cqe;
9665 bool workposted = false;
9666 int ecount = 0;
9667 uint16_t cqid;
9668
James Smartcb5172e2010-03-15 11:25:07 -04009669 if (bf_get_le32(lpfc_eqe_major_code, eqe) != 0) {
James Smart4f774512009-05-22 14:52:35 -04009670 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9671 "0359 Not a valid slow-path completion "
9672 "event: majorcode=x%x, minorcode=x%x\n",
James Smartcb5172e2010-03-15 11:25:07 -04009673 bf_get_le32(lpfc_eqe_major_code, eqe),
9674 bf_get_le32(lpfc_eqe_minor_code, eqe));
James Smart4f774512009-05-22 14:52:35 -04009675 return;
9676 }
9677
9678 /* Get the reference to the corresponding CQ */
James Smartcb5172e2010-03-15 11:25:07 -04009679 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
James Smart4f774512009-05-22 14:52:35 -04009680
9681 /* Search for completion queue pointer matching this cqid */
9682 speq = phba->sli4_hba.sp_eq;
9683 list_for_each_entry(childq, &speq->child_list, list) {
9684 if (childq->queue_id == cqid) {
9685 cq = childq;
9686 break;
9687 }
9688 }
9689 if (unlikely(!cq)) {
James Smart75baf692010-06-08 18:31:21 -04009690 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
9691 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9692 "0365 Slow-path CQ identifier "
9693 "(%d) does not exist\n", cqid);
James Smart4f774512009-05-22 14:52:35 -04009694 return;
9695 }
9696
9697 /* Process all the entries to the CQ */
9698 switch (cq->type) {
9699 case LPFC_MCQ:
9700 while ((cqe = lpfc_sli4_cq_get(cq))) {
9701 workposted |= lpfc_sli4_sp_handle_mcqe(phba, cqe);
9702 if (!(++ecount % LPFC_GET_QE_REL_INT))
9703 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
9704 }
9705 break;
9706 case LPFC_WCQ:
9707 while ((cqe = lpfc_sli4_cq_get(cq))) {
James Smart4d9ab992009-10-02 15:16:39 -04009708 workposted |= lpfc_sli4_sp_handle_cqe(phba, cq, cqe);
James Smart4f774512009-05-22 14:52:35 -04009709 if (!(++ecount % LPFC_GET_QE_REL_INT))
9710 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
9711 }
9712 break;
9713 default:
9714 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9715 "0370 Invalid completion queue type (%d)\n",
9716 cq->type);
9717 return;
9718 }
9719
9720 /* Catch the no cq entry condition, log an error */
9721 if (unlikely(ecount == 0))
9722 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9723 "0371 No entry from the CQ: identifier "
9724 "(x%x), type (%d)\n", cq->queue_id, cq->type);
9725
9726 /* In any case, flash and re-arm the RCQ */
9727 lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
9728
9729 /* wake up worker thread if there are works to be done */
9730 if (workposted)
9731 lpfc_worker_wake_up(phba);
9732}
9733
9734/**
9735 * lpfc_sli4_fp_handle_fcp_wcqe - Process fast-path work queue completion entry
9736 * @eqe: Pointer to fast-path completion queue entry.
9737 *
9738 * This routine process a fast-path work queue completion entry from fast-path
9739 * event queue for FCP command response completion.
9740 **/
9741static void
9742lpfc_sli4_fp_handle_fcp_wcqe(struct lpfc_hba *phba,
9743 struct lpfc_wcqe_complete *wcqe)
9744{
9745 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_FCP_RING];
9746 struct lpfc_iocbq *cmdiocbq;
9747 struct lpfc_iocbq irspiocbq;
9748 unsigned long iflags;
9749
9750 spin_lock_irqsave(&phba->hbalock, iflags);
9751 pring->stats.iocb_event++;
9752 spin_unlock_irqrestore(&phba->hbalock, iflags);
9753
9754 /* Check for response status */
9755 if (unlikely(bf_get(lpfc_wcqe_c_status, wcqe))) {
9756 /* If resource errors reported from HBA, reduce queue
9757 * depth of the SCSI device.
9758 */
9759 if ((bf_get(lpfc_wcqe_c_status, wcqe) ==
9760 IOSTAT_LOCAL_REJECT) &&
9761 (wcqe->parameter == IOERR_NO_RESOURCES)) {
9762 phba->lpfc_rampdown_queue_depth(phba);
9763 }
9764 /* Log the error status */
9765 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9766 "0373 FCP complete error: status=x%x, "
9767 "hw_status=x%x, total_data_specified=%d, "
9768 "parameter=x%x, word3=x%x\n",
9769 bf_get(lpfc_wcqe_c_status, wcqe),
9770 bf_get(lpfc_wcqe_c_hw_status, wcqe),
9771 wcqe->total_data_placed, wcqe->parameter,
9772 wcqe->word3);
9773 }
9774
9775 /* Look up the FCP command IOCB and create pseudo response IOCB */
9776 spin_lock_irqsave(&phba->hbalock, iflags);
9777 cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
9778 bf_get(lpfc_wcqe_c_request_tag, wcqe));
9779 spin_unlock_irqrestore(&phba->hbalock, iflags);
9780 if (unlikely(!cmdiocbq)) {
9781 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9782 "0374 FCP complete with no corresponding "
9783 "cmdiocb: iotag (%d)\n",
9784 bf_get(lpfc_wcqe_c_request_tag, wcqe));
9785 return;
9786 }
9787 if (unlikely(!cmdiocbq->iocb_cmpl)) {
9788 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9789 "0375 FCP cmdiocb not callback function "
9790 "iotag: (%d)\n",
9791 bf_get(lpfc_wcqe_c_request_tag, wcqe));
9792 return;
9793 }
9794
9795 /* Fake the irspiocb and copy necessary response information */
James Smart341af102010-01-26 23:07:37 -05009796 lpfc_sli4_iocb_param_transfer(phba, &irspiocbq, cmdiocbq, wcqe);
James Smart4f774512009-05-22 14:52:35 -04009797
James Smart0f65ff62010-02-26 14:14:23 -05009798 if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED) {
9799 spin_lock_irqsave(&phba->hbalock, iflags);
9800 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
9801 spin_unlock_irqrestore(&phba->hbalock, iflags);
9802 }
9803
James Smart4f774512009-05-22 14:52:35 -04009804 /* Pass the cmd_iocb and the rsp state to the upper layer */
9805 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq, &irspiocbq);
9806}
9807
9808/**
9809 * lpfc_sli4_fp_handle_rel_wcqe - Handle fast-path WQ entry consumed event
9810 * @phba: Pointer to HBA context object.
9811 * @cq: Pointer to completion queue.
9812 * @wcqe: Pointer to work-queue completion queue entry.
9813 *
9814 * This routine handles an fast-path WQ entry comsumed event by invoking the
9815 * proper WQ release routine to the slow-path WQ.
9816 **/
9817static void
9818lpfc_sli4_fp_handle_rel_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
9819 struct lpfc_wcqe_release *wcqe)
9820{
9821 struct lpfc_queue *childwq;
9822 bool wqid_matched = false;
9823 uint16_t fcp_wqid;
9824
9825 /* Check for fast-path FCP work queue release */
9826 fcp_wqid = bf_get(lpfc_wcqe_r_wq_id, wcqe);
9827 list_for_each_entry(childwq, &cq->child_list, list) {
9828 if (childwq->queue_id == fcp_wqid) {
9829 lpfc_sli4_wq_release(childwq,
9830 bf_get(lpfc_wcqe_r_wqe_index, wcqe));
9831 wqid_matched = true;
9832 break;
9833 }
9834 }
9835 /* Report warning log message if no match found */
9836 if (wqid_matched != true)
9837 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9838 "2580 Fast-path wqe consume event carries "
9839 "miss-matched qid: wcqe-qid=x%x\n", fcp_wqid);
9840}
9841
9842/**
9843 * lpfc_sli4_fp_handle_wcqe - Process fast-path work queue completion entry
9844 * @cq: Pointer to the completion queue.
9845 * @eqe: Pointer to fast-path completion queue entry.
9846 *
9847 * This routine process a fast-path work queue completion entry from fast-path
9848 * event queue for FCP command response completion.
9849 **/
9850static int
9851lpfc_sli4_fp_handle_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
9852 struct lpfc_cqe *cqe)
9853{
9854 struct lpfc_wcqe_release wcqe;
9855 bool workposted = false;
9856
9857 /* Copy the work queue CQE and convert endian order if needed */
9858 lpfc_sli_pcimem_bcopy(cqe, &wcqe, sizeof(struct lpfc_cqe));
9859
9860 /* Check and process for different type of WCQE and dispatch */
9861 switch (bf_get(lpfc_wcqe_c_code, &wcqe)) {
9862 case CQE_CODE_COMPL_WQE:
9863 /* Process the WQ complete event */
James Smart98fc5dd2010-06-07 15:24:29 -04009864 phba->last_completion_time = jiffies;
James Smart4f774512009-05-22 14:52:35 -04009865 lpfc_sli4_fp_handle_fcp_wcqe(phba,
9866 (struct lpfc_wcqe_complete *)&wcqe);
9867 break;
9868 case CQE_CODE_RELEASE_WQE:
9869 /* Process the WQ release event */
9870 lpfc_sli4_fp_handle_rel_wcqe(phba, cq,
9871 (struct lpfc_wcqe_release *)&wcqe);
9872 break;
9873 case CQE_CODE_XRI_ABORTED:
9874 /* Process the WQ XRI abort event */
James Smartbc739052010-08-04 16:11:18 -04009875 phba->last_completion_time = jiffies;
James Smart4f774512009-05-22 14:52:35 -04009876 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
9877 (struct sli4_wcqe_xri_aborted *)&wcqe);
9878 break;
9879 default:
9880 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9881 "0144 Not a valid WCQE code: x%x\n",
9882 bf_get(lpfc_wcqe_c_code, &wcqe));
9883 break;
9884 }
9885 return workposted;
9886}
9887
9888/**
9889 * lpfc_sli4_fp_handle_eqe - Process a fast-path event queue entry
9890 * @phba: Pointer to HBA context object.
9891 * @eqe: Pointer to fast-path event queue entry.
9892 *
9893 * This routine process a event queue entry from the fast-path event queue.
9894 * It will check the MajorCode and MinorCode to determine this is for a
9895 * completion event on a completion queue, if not, an error shall be logged
9896 * and just return. Otherwise, it will get to the corresponding completion
9897 * queue and process all the entries on the completion queue, rearm the
9898 * completion queue, and then return.
9899 **/
9900static void
9901lpfc_sli4_fp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
9902 uint32_t fcp_cqidx)
9903{
9904 struct lpfc_queue *cq;
9905 struct lpfc_cqe *cqe;
9906 bool workposted = false;
9907 uint16_t cqid;
9908 int ecount = 0;
9909
James Smartcb5172e2010-03-15 11:25:07 -04009910 if (unlikely(bf_get_le32(lpfc_eqe_major_code, eqe) != 0)) {
James Smart4f774512009-05-22 14:52:35 -04009911 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9912 "0366 Not a valid fast-path completion "
9913 "event: majorcode=x%x, minorcode=x%x\n",
James Smartcb5172e2010-03-15 11:25:07 -04009914 bf_get_le32(lpfc_eqe_major_code, eqe),
9915 bf_get_le32(lpfc_eqe_minor_code, eqe));
James Smart4f774512009-05-22 14:52:35 -04009916 return;
9917 }
9918
9919 cq = phba->sli4_hba.fcp_cq[fcp_cqidx];
9920 if (unlikely(!cq)) {
James Smart75baf692010-06-08 18:31:21 -04009921 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
9922 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9923 "0367 Fast-path completion queue "
9924 "does not exist\n");
James Smart4f774512009-05-22 14:52:35 -04009925 return;
9926 }
9927
9928 /* Get the reference to the corresponding CQ */
James Smartcb5172e2010-03-15 11:25:07 -04009929 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
James Smart4f774512009-05-22 14:52:35 -04009930 if (unlikely(cqid != cq->queue_id)) {
9931 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9932 "0368 Miss-matched fast-path completion "
9933 "queue identifier: eqcqid=%d, fcpcqid=%d\n",
9934 cqid, cq->queue_id);
9935 return;
9936 }
9937
9938 /* Process all the entries to the CQ */
9939 while ((cqe = lpfc_sli4_cq_get(cq))) {
9940 workposted |= lpfc_sli4_fp_handle_wcqe(phba, cq, cqe);
9941 if (!(++ecount % LPFC_GET_QE_REL_INT))
9942 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
9943 }
9944
9945 /* Catch the no cq entry condition */
9946 if (unlikely(ecount == 0))
9947 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9948 "0369 No entry from fast-path completion "
9949 "queue fcpcqid=%d\n", cq->queue_id);
9950
9951 /* In any case, flash and re-arm the CQ */
9952 lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
9953
9954 /* wake up worker thread if there are works to be done */
9955 if (workposted)
9956 lpfc_worker_wake_up(phba);
9957}
9958
9959static void
9960lpfc_sli4_eq_flush(struct lpfc_hba *phba, struct lpfc_queue *eq)
9961{
9962 struct lpfc_eqe *eqe;
9963
9964 /* walk all the EQ entries and drop on the floor */
9965 while ((eqe = lpfc_sli4_eq_get(eq)))
9966 ;
9967
9968 /* Clear and re-arm the EQ */
9969 lpfc_sli4_eq_release(eq, LPFC_QUEUE_REARM);
9970}
9971
9972/**
9973 * lpfc_sli4_sp_intr_handler - Slow-path interrupt handler to SLI-4 device
9974 * @irq: Interrupt number.
9975 * @dev_id: The device context pointer.
9976 *
9977 * This function is directly called from the PCI layer as an interrupt
9978 * service routine when device with SLI-4 interface spec is enabled with
9979 * MSI-X multi-message interrupt mode and there are slow-path events in
9980 * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
9981 * interrupt mode, this function is called as part of the device-level
9982 * interrupt handler. When the PCI slot is in error recovery or the HBA is
9983 * undergoing initialization, the interrupt handler will not process the
9984 * interrupt. The link attention and ELS ring attention events are handled
9985 * by the worker thread. The interrupt handler signals the worker thread
9986 * and returns for these events. This function is called without any lock
9987 * held. It gets the hbalock to access and update SLI data structures.
9988 *
9989 * This function returns IRQ_HANDLED when interrupt is handled else it
9990 * returns IRQ_NONE.
9991 **/
9992irqreturn_t
9993lpfc_sli4_sp_intr_handler(int irq, void *dev_id)
9994{
9995 struct lpfc_hba *phba;
9996 struct lpfc_queue *speq;
9997 struct lpfc_eqe *eqe;
9998 unsigned long iflag;
9999 int ecount = 0;
10000
10001 /*
10002 * Get the driver's phba structure from the dev_id
10003 */
10004 phba = (struct lpfc_hba *)dev_id;
10005
10006 if (unlikely(!phba))
10007 return IRQ_NONE;
10008
10009 /* Get to the EQ struct associated with this vector */
10010 speq = phba->sli4_hba.sp_eq;
10011
10012 /* Check device state for handling interrupt */
10013 if (unlikely(lpfc_intr_state_check(phba))) {
10014 /* Check again for link_state with lock held */
10015 spin_lock_irqsave(&phba->hbalock, iflag);
10016 if (phba->link_state < LPFC_LINK_DOWN)
10017 /* Flush, clear interrupt, and rearm the EQ */
10018 lpfc_sli4_eq_flush(phba, speq);
10019 spin_unlock_irqrestore(&phba->hbalock, iflag);
10020 return IRQ_NONE;
10021 }
10022
10023 /*
10024 * Process all the event on FCP slow-path EQ
10025 */
10026 while ((eqe = lpfc_sli4_eq_get(speq))) {
10027 lpfc_sli4_sp_handle_eqe(phba, eqe);
10028 if (!(++ecount % LPFC_GET_QE_REL_INT))
10029 lpfc_sli4_eq_release(speq, LPFC_QUEUE_NOARM);
10030 }
10031
10032 /* Always clear and re-arm the slow-path EQ */
10033 lpfc_sli4_eq_release(speq, LPFC_QUEUE_REARM);
10034
10035 /* Catch the no cq entry condition */
10036 if (unlikely(ecount == 0)) {
10037 if (phba->intr_type == MSIX)
10038 /* MSI-X treated interrupt served as no EQ share INT */
10039 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
10040 "0357 MSI-X interrupt with no EQE\n");
10041 else
10042 /* Non MSI-X treated on interrupt as EQ share INT */
10043 return IRQ_NONE;
10044 }
10045
10046 return IRQ_HANDLED;
10047} /* lpfc_sli4_sp_intr_handler */
10048
10049/**
10050 * lpfc_sli4_fp_intr_handler - Fast-path interrupt handler to SLI-4 device
10051 * @irq: Interrupt number.
10052 * @dev_id: The device context pointer.
10053 *
10054 * This function is directly called from the PCI layer as an interrupt
10055 * service routine when device with SLI-4 interface spec is enabled with
10056 * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
10057 * ring event in the HBA. However, when the device is enabled with either
10058 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
10059 * device-level interrupt handler. When the PCI slot is in error recovery
10060 * or the HBA is undergoing initialization, the interrupt handler will not
10061 * process the interrupt. The SCSI FCP fast-path ring event are handled in
10062 * the intrrupt context. This function is called without any lock held.
10063 * It gets the hbalock to access and update SLI data structures. Note that,
10064 * the FCP EQ to FCP CQ are one-to-one map such that the FCP EQ index is
10065 * equal to that of FCP CQ index.
10066 *
10067 * This function returns IRQ_HANDLED when interrupt is handled else it
10068 * returns IRQ_NONE.
10069 **/
10070irqreturn_t
10071lpfc_sli4_fp_intr_handler(int irq, void *dev_id)
10072{
10073 struct lpfc_hba *phba;
10074 struct lpfc_fcp_eq_hdl *fcp_eq_hdl;
10075 struct lpfc_queue *fpeq;
10076 struct lpfc_eqe *eqe;
10077 unsigned long iflag;
10078 int ecount = 0;
10079 uint32_t fcp_eqidx;
10080
10081 /* Get the driver's phba structure from the dev_id */
10082 fcp_eq_hdl = (struct lpfc_fcp_eq_hdl *)dev_id;
10083 phba = fcp_eq_hdl->phba;
10084 fcp_eqidx = fcp_eq_hdl->idx;
10085
10086 if (unlikely(!phba))
10087 return IRQ_NONE;
10088
10089 /* Get to the EQ struct associated with this vector */
10090 fpeq = phba->sli4_hba.fp_eq[fcp_eqidx];
10091
10092 /* Check device state for handling interrupt */
10093 if (unlikely(lpfc_intr_state_check(phba))) {
10094 /* Check again for link_state with lock held */
10095 spin_lock_irqsave(&phba->hbalock, iflag);
10096 if (phba->link_state < LPFC_LINK_DOWN)
10097 /* Flush, clear interrupt, and rearm the EQ */
10098 lpfc_sli4_eq_flush(phba, fpeq);
10099 spin_unlock_irqrestore(&phba->hbalock, iflag);
10100 return IRQ_NONE;
10101 }
10102
10103 /*
10104 * Process all the event on FCP fast-path EQ
10105 */
10106 while ((eqe = lpfc_sli4_eq_get(fpeq))) {
10107 lpfc_sli4_fp_handle_eqe(phba, eqe, fcp_eqidx);
10108 if (!(++ecount % LPFC_GET_QE_REL_INT))
10109 lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_NOARM);
10110 }
10111
10112 /* Always clear and re-arm the fast-path EQ */
10113 lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_REARM);
10114
10115 if (unlikely(ecount == 0)) {
10116 if (phba->intr_type == MSIX)
10117 /* MSI-X treated interrupt served as no EQ share INT */
10118 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
10119 "0358 MSI-X interrupt with no EQE\n");
10120 else
10121 /* Non MSI-X treated on interrupt as EQ share INT */
10122 return IRQ_NONE;
10123 }
10124
10125 return IRQ_HANDLED;
10126} /* lpfc_sli4_fp_intr_handler */
10127
10128/**
10129 * lpfc_sli4_intr_handler - Device-level interrupt handler for SLI-4 device
10130 * @irq: Interrupt number.
10131 * @dev_id: The device context pointer.
10132 *
10133 * This function is the device-level interrupt handler to device with SLI-4
10134 * interface spec, called from the PCI layer when either MSI or Pin-IRQ
10135 * interrupt mode is enabled and there is an event in the HBA which requires
10136 * driver attention. This function invokes the slow-path interrupt attention
10137 * handling function and fast-path interrupt attention handling function in
10138 * turn to process the relevant HBA attention events. This function is called
10139 * without any lock held. It gets the hbalock to access and update SLI data
10140 * structures.
10141 *
10142 * This function returns IRQ_HANDLED when interrupt is handled, else it
10143 * returns IRQ_NONE.
10144 **/
10145irqreturn_t
10146lpfc_sli4_intr_handler(int irq, void *dev_id)
10147{
10148 struct lpfc_hba *phba;
10149 irqreturn_t sp_irq_rc, fp_irq_rc;
10150 bool fp_handled = false;
10151 uint32_t fcp_eqidx;
10152
10153 /* Get the driver's phba structure from the dev_id */
10154 phba = (struct lpfc_hba *)dev_id;
10155
10156 if (unlikely(!phba))
10157 return IRQ_NONE;
10158
10159 /*
10160 * Invokes slow-path host attention interrupt handling as appropriate.
10161 */
10162 sp_irq_rc = lpfc_sli4_sp_intr_handler(irq, dev_id);
10163
10164 /*
10165 * Invoke fast-path host attention interrupt handling as appropriate.
10166 */
10167 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count; fcp_eqidx++) {
10168 fp_irq_rc = lpfc_sli4_fp_intr_handler(irq,
10169 &phba->sli4_hba.fcp_eq_hdl[fcp_eqidx]);
10170 if (fp_irq_rc == IRQ_HANDLED)
10171 fp_handled |= true;
10172 }
10173
10174 return (fp_handled == true) ? IRQ_HANDLED : sp_irq_rc;
10175} /* lpfc_sli4_intr_handler */
10176
10177/**
10178 * lpfc_sli4_queue_free - free a queue structure and associated memory
10179 * @queue: The queue structure to free.
10180 *
Uwe Kleine-Königb5950762010-11-01 15:38:34 -040010181 * This function frees a queue structure and the DMAable memory used for
James Smart4f774512009-05-22 14:52:35 -040010182 * the host resident queue. This function must be called after destroying the
10183 * queue on the HBA.
10184 **/
10185void
10186lpfc_sli4_queue_free(struct lpfc_queue *queue)
10187{
10188 struct lpfc_dmabuf *dmabuf;
10189
10190 if (!queue)
10191 return;
10192
10193 while (!list_empty(&queue->page_list)) {
10194 list_remove_head(&queue->page_list, dmabuf, struct lpfc_dmabuf,
10195 list);
James Smart49198b32010-04-06 15:04:33 -040010196 dma_free_coherent(&queue->phba->pcidev->dev, SLI4_PAGE_SIZE,
James Smart4f774512009-05-22 14:52:35 -040010197 dmabuf->virt, dmabuf->phys);
10198 kfree(dmabuf);
10199 }
10200 kfree(queue);
10201 return;
10202}
10203
10204/**
10205 * lpfc_sli4_queue_alloc - Allocate and initialize a queue structure
10206 * @phba: The HBA that this queue is being created on.
10207 * @entry_size: The size of each queue entry for this queue.
10208 * @entry count: The number of entries that this queue will handle.
10209 *
10210 * This function allocates a queue structure and the DMAable memory used for
10211 * the host resident queue. This function must be called before creating the
10212 * queue on the HBA.
10213 **/
10214struct lpfc_queue *
10215lpfc_sli4_queue_alloc(struct lpfc_hba *phba, uint32_t entry_size,
10216 uint32_t entry_count)
10217{
10218 struct lpfc_queue *queue;
10219 struct lpfc_dmabuf *dmabuf;
10220 int x, total_qe_count;
10221 void *dma_pointer;
James Smartcb5172e2010-03-15 11:25:07 -040010222 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smart4f774512009-05-22 14:52:35 -040010223
James Smartcb5172e2010-03-15 11:25:07 -040010224 if (!phba->sli4_hba.pc_sli4_params.supported)
10225 hw_page_size = SLI4_PAGE_SIZE;
10226
James Smart4f774512009-05-22 14:52:35 -040010227 queue = kzalloc(sizeof(struct lpfc_queue) +
10228 (sizeof(union sli4_qe) * entry_count), GFP_KERNEL);
10229 if (!queue)
10230 return NULL;
James Smartcb5172e2010-03-15 11:25:07 -040010231 queue->page_count = (ALIGN(entry_size * entry_count,
10232 hw_page_size))/hw_page_size;
James Smart4f774512009-05-22 14:52:35 -040010233 INIT_LIST_HEAD(&queue->list);
10234 INIT_LIST_HEAD(&queue->page_list);
10235 INIT_LIST_HEAD(&queue->child_list);
10236 for (x = 0, total_qe_count = 0; x < queue->page_count; x++) {
10237 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
10238 if (!dmabuf)
10239 goto out_fail;
10240 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
James Smartcb5172e2010-03-15 11:25:07 -040010241 hw_page_size, &dmabuf->phys,
James Smart4f774512009-05-22 14:52:35 -040010242 GFP_KERNEL);
10243 if (!dmabuf->virt) {
10244 kfree(dmabuf);
10245 goto out_fail;
10246 }
James Smartcb5172e2010-03-15 11:25:07 -040010247 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -040010248 dmabuf->buffer_tag = x;
10249 list_add_tail(&dmabuf->list, &queue->page_list);
10250 /* initialize queue's entry array */
10251 dma_pointer = dmabuf->virt;
10252 for (; total_qe_count < entry_count &&
James Smartcb5172e2010-03-15 11:25:07 -040010253 dma_pointer < (hw_page_size + dmabuf->virt);
James Smart4f774512009-05-22 14:52:35 -040010254 total_qe_count++, dma_pointer += entry_size) {
10255 queue->qe[total_qe_count].address = dma_pointer;
10256 }
10257 }
10258 queue->entry_size = entry_size;
10259 queue->entry_count = entry_count;
10260 queue->phba = phba;
10261
10262 return queue;
10263out_fail:
10264 lpfc_sli4_queue_free(queue);
10265 return NULL;
10266}
10267
10268/**
10269 * lpfc_eq_create - Create an Event Queue on the HBA
10270 * @phba: HBA structure that indicates port to create a queue on.
10271 * @eq: The queue structure to use to create the event queue.
10272 * @imax: The maximum interrupt per second limit.
10273 *
10274 * This function creates an event queue, as detailed in @eq, on a port,
10275 * described by @phba by sending an EQ_CREATE mailbox command to the HBA.
10276 *
10277 * The @phba struct is used to send mailbox command to HBA. The @eq struct
10278 * is used to get the entry count and entry size that are necessary to
10279 * determine the number of pages to allocate and use for this queue. This
10280 * function will send the EQ_CREATE mailbox command to the HBA to setup the
10281 * event queue. This function is asynchronous and will wait for the mailbox
10282 * command to finish before continuing.
10283 *
10284 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040010285 * memory this function will return -ENOMEM. If the queue create mailbox command
10286 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040010287 **/
10288uint32_t
10289lpfc_eq_create(struct lpfc_hba *phba, struct lpfc_queue *eq, uint16_t imax)
10290{
10291 struct lpfc_mbx_eq_create *eq_create;
10292 LPFC_MBOXQ_t *mbox;
10293 int rc, length, status = 0;
10294 struct lpfc_dmabuf *dmabuf;
10295 uint32_t shdr_status, shdr_add_status;
10296 union lpfc_sli4_cfg_shdr *shdr;
10297 uint16_t dmult;
James Smart49198b32010-04-06 15:04:33 -040010298 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
10299
10300 if (!phba->sli4_hba.pc_sli4_params.supported)
10301 hw_page_size = SLI4_PAGE_SIZE;
James Smart4f774512009-05-22 14:52:35 -040010302
10303 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
10304 if (!mbox)
10305 return -ENOMEM;
10306 length = (sizeof(struct lpfc_mbx_eq_create) -
10307 sizeof(struct lpfc_sli4_cfg_mhdr));
10308 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
10309 LPFC_MBOX_OPCODE_EQ_CREATE,
10310 length, LPFC_SLI4_MBX_EMBED);
10311 eq_create = &mbox->u.mqe.un.eq_create;
10312 bf_set(lpfc_mbx_eq_create_num_pages, &eq_create->u.request,
10313 eq->page_count);
10314 bf_set(lpfc_eq_context_size, &eq_create->u.request.context,
10315 LPFC_EQE_SIZE);
10316 bf_set(lpfc_eq_context_valid, &eq_create->u.request.context, 1);
10317 /* Calculate delay multiper from maximum interrupt per second */
10318 dmult = LPFC_DMULT_CONST/imax - 1;
10319 bf_set(lpfc_eq_context_delay_multi, &eq_create->u.request.context,
10320 dmult);
10321 switch (eq->entry_count) {
10322 default:
10323 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10324 "0360 Unsupported EQ count. (%d)\n",
10325 eq->entry_count);
10326 if (eq->entry_count < 256)
10327 return -EINVAL;
10328 /* otherwise default to smallest count (drop through) */
10329 case 256:
10330 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
10331 LPFC_EQ_CNT_256);
10332 break;
10333 case 512:
10334 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
10335 LPFC_EQ_CNT_512);
10336 break;
10337 case 1024:
10338 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
10339 LPFC_EQ_CNT_1024);
10340 break;
10341 case 2048:
10342 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
10343 LPFC_EQ_CNT_2048);
10344 break;
10345 case 4096:
10346 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
10347 LPFC_EQ_CNT_4096);
10348 break;
10349 }
10350 list_for_each_entry(dmabuf, &eq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040010351 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -040010352 eq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
10353 putPaddrLow(dmabuf->phys);
10354 eq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
10355 putPaddrHigh(dmabuf->phys);
10356 }
10357 mbox->vport = phba->pport;
10358 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
10359 mbox->context1 = NULL;
10360 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
10361 shdr = (union lpfc_sli4_cfg_shdr *) &eq_create->header.cfg_shdr;
10362 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10363 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10364 if (shdr_status || shdr_add_status || rc) {
10365 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10366 "2500 EQ_CREATE mailbox failed with "
10367 "status x%x add_status x%x, mbx status x%x\n",
10368 shdr_status, shdr_add_status, rc);
10369 status = -ENXIO;
10370 }
10371 eq->type = LPFC_EQ;
10372 eq->subtype = LPFC_NONE;
10373 eq->queue_id = bf_get(lpfc_mbx_eq_create_q_id, &eq_create->u.response);
10374 if (eq->queue_id == 0xFFFF)
10375 status = -ENXIO;
10376 eq->host_index = 0;
10377 eq->hba_index = 0;
10378
James Smart8fa38512009-07-19 10:01:03 -040010379 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040010380 return status;
10381}
10382
10383/**
10384 * lpfc_cq_create - Create a Completion Queue on the HBA
10385 * @phba: HBA structure that indicates port to create a queue on.
10386 * @cq: The queue structure to use to create the completion queue.
10387 * @eq: The event queue to bind this completion queue to.
10388 *
10389 * This function creates a completion queue, as detailed in @wq, on a port,
10390 * described by @phba by sending a CQ_CREATE mailbox command to the HBA.
10391 *
10392 * The @phba struct is used to send mailbox command to HBA. The @cq struct
10393 * is used to get the entry count and entry size that are necessary to
10394 * determine the number of pages to allocate and use for this queue. The @eq
10395 * is used to indicate which event queue to bind this completion queue to. This
10396 * function will send the CQ_CREATE mailbox command to the HBA to setup the
10397 * completion queue. This function is asynchronous and will wait for the mailbox
10398 * command to finish before continuing.
10399 *
10400 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040010401 * memory this function will return -ENOMEM. If the queue create mailbox command
10402 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040010403 **/
10404uint32_t
10405lpfc_cq_create(struct lpfc_hba *phba, struct lpfc_queue *cq,
10406 struct lpfc_queue *eq, uint32_t type, uint32_t subtype)
10407{
10408 struct lpfc_mbx_cq_create *cq_create;
10409 struct lpfc_dmabuf *dmabuf;
10410 LPFC_MBOXQ_t *mbox;
10411 int rc, length, status = 0;
10412 uint32_t shdr_status, shdr_add_status;
10413 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040010414 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
10415
10416 if (!phba->sli4_hba.pc_sli4_params.supported)
10417 hw_page_size = SLI4_PAGE_SIZE;
10418
James Smart4f774512009-05-22 14:52:35 -040010419
10420 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
10421 if (!mbox)
10422 return -ENOMEM;
10423 length = (sizeof(struct lpfc_mbx_cq_create) -
10424 sizeof(struct lpfc_sli4_cfg_mhdr));
10425 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
10426 LPFC_MBOX_OPCODE_CQ_CREATE,
10427 length, LPFC_SLI4_MBX_EMBED);
10428 cq_create = &mbox->u.mqe.un.cq_create;
10429 bf_set(lpfc_mbx_cq_create_num_pages, &cq_create->u.request,
10430 cq->page_count);
10431 bf_set(lpfc_cq_context_event, &cq_create->u.request.context, 1);
10432 bf_set(lpfc_cq_context_valid, &cq_create->u.request.context, 1);
10433 bf_set(lpfc_cq_eq_id, &cq_create->u.request.context, eq->queue_id);
10434 switch (cq->entry_count) {
10435 default:
10436 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10437 "0361 Unsupported CQ count. (%d)\n",
10438 cq->entry_count);
10439 if (cq->entry_count < 256)
10440 return -EINVAL;
10441 /* otherwise default to smallest count (drop through) */
10442 case 256:
10443 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
10444 LPFC_CQ_CNT_256);
10445 break;
10446 case 512:
10447 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
10448 LPFC_CQ_CNT_512);
10449 break;
10450 case 1024:
10451 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
10452 LPFC_CQ_CNT_1024);
10453 break;
10454 }
10455 list_for_each_entry(dmabuf, &cq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040010456 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -040010457 cq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
10458 putPaddrLow(dmabuf->phys);
10459 cq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
10460 putPaddrHigh(dmabuf->phys);
10461 }
10462 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
10463
10464 /* The IOCTL status is embedded in the mailbox subheader. */
10465 shdr = (union lpfc_sli4_cfg_shdr *) &cq_create->header.cfg_shdr;
10466 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10467 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10468 if (shdr_status || shdr_add_status || rc) {
10469 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10470 "2501 CQ_CREATE mailbox failed with "
10471 "status x%x add_status x%x, mbx status x%x\n",
10472 shdr_status, shdr_add_status, rc);
10473 status = -ENXIO;
10474 goto out;
10475 }
10476 cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
10477 if (cq->queue_id == 0xFFFF) {
10478 status = -ENXIO;
10479 goto out;
10480 }
10481 /* link the cq onto the parent eq child list */
10482 list_add_tail(&cq->list, &eq->child_list);
10483 /* Set up completion queue's type and subtype */
10484 cq->type = type;
10485 cq->subtype = subtype;
10486 cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
10487 cq->host_index = 0;
10488 cq->hba_index = 0;
James Smart4f774512009-05-22 14:52:35 -040010489
James Smart8fa38512009-07-19 10:01:03 -040010490out:
10491 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040010492 return status;
10493}
10494
10495/**
James Smartb19a0612010-04-06 14:48:51 -040010496 * lpfc_mq_create_fb_init - Send MCC_CREATE without async events registration
James Smart04c68492009-05-22 14:52:52 -040010497 * @phba: HBA structure that indicates port to create a queue on.
10498 * @mq: The queue structure to use to create the mailbox queue.
James Smartb19a0612010-04-06 14:48:51 -040010499 * @mbox: An allocated pointer to type LPFC_MBOXQ_t
10500 * @cq: The completion queue to associate with this cq.
James Smart04c68492009-05-22 14:52:52 -040010501 *
James Smartb19a0612010-04-06 14:48:51 -040010502 * This function provides failback (fb) functionality when the
10503 * mq_create_ext fails on older FW generations. It's purpose is identical
10504 * to mq_create_ext otherwise.
James Smart04c68492009-05-22 14:52:52 -040010505 *
James Smartb19a0612010-04-06 14:48:51 -040010506 * This routine cannot fail as all attributes were previously accessed and
10507 * initialized in mq_create_ext.
James Smart04c68492009-05-22 14:52:52 -040010508 **/
James Smartb19a0612010-04-06 14:48:51 -040010509static void
10510lpfc_mq_create_fb_init(struct lpfc_hba *phba, struct lpfc_queue *mq,
10511 LPFC_MBOXQ_t *mbox, struct lpfc_queue *cq)
James Smart04c68492009-05-22 14:52:52 -040010512{
10513 struct lpfc_mbx_mq_create *mq_create;
10514 struct lpfc_dmabuf *dmabuf;
James Smartb19a0612010-04-06 14:48:51 -040010515 int length;
James Smart04c68492009-05-22 14:52:52 -040010516
James Smart04c68492009-05-22 14:52:52 -040010517 length = (sizeof(struct lpfc_mbx_mq_create) -
10518 sizeof(struct lpfc_sli4_cfg_mhdr));
10519 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
10520 LPFC_MBOX_OPCODE_MQ_CREATE,
10521 length, LPFC_SLI4_MBX_EMBED);
10522 mq_create = &mbox->u.mqe.un.mq_create;
10523 bf_set(lpfc_mbx_mq_create_num_pages, &mq_create->u.request,
James Smartb19a0612010-04-06 14:48:51 -040010524 mq->page_count);
James Smart04c68492009-05-22 14:52:52 -040010525 bf_set(lpfc_mq_context_cq_id, &mq_create->u.request.context,
James Smartb19a0612010-04-06 14:48:51 -040010526 cq->queue_id);
James Smart04c68492009-05-22 14:52:52 -040010527 bf_set(lpfc_mq_context_valid, &mq_create->u.request.context, 1);
10528 switch (mq->entry_count) {
James Smart04c68492009-05-22 14:52:52 -040010529 case 16:
10530 bf_set(lpfc_mq_context_count, &mq_create->u.request.context,
10531 LPFC_MQ_CNT_16);
10532 break;
10533 case 32:
10534 bf_set(lpfc_mq_context_count, &mq_create->u.request.context,
10535 LPFC_MQ_CNT_32);
10536 break;
10537 case 64:
10538 bf_set(lpfc_mq_context_count, &mq_create->u.request.context,
10539 LPFC_MQ_CNT_64);
10540 break;
10541 case 128:
10542 bf_set(lpfc_mq_context_count, &mq_create->u.request.context,
10543 LPFC_MQ_CNT_128);
10544 break;
10545 }
10546 list_for_each_entry(dmabuf, &mq->page_list, list) {
10547 mq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
James Smartb19a0612010-04-06 14:48:51 -040010548 putPaddrLow(dmabuf->phys);
James Smart04c68492009-05-22 14:52:52 -040010549 mq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
James Smartb19a0612010-04-06 14:48:51 -040010550 putPaddrHigh(dmabuf->phys);
10551 }
10552}
10553
10554/**
10555 * lpfc_mq_create - Create a mailbox Queue on the HBA
10556 * @phba: HBA structure that indicates port to create a queue on.
10557 * @mq: The queue structure to use to create the mailbox queue.
10558 * @cq: The completion queue to associate with this cq.
10559 * @subtype: The queue's subtype.
10560 *
10561 * This function creates a mailbox queue, as detailed in @mq, on a port,
10562 * described by @phba by sending a MQ_CREATE mailbox command to the HBA.
10563 *
10564 * The @phba struct is used to send mailbox command to HBA. The @cq struct
10565 * is used to get the entry count and entry size that are necessary to
10566 * determine the number of pages to allocate and use for this queue. This
10567 * function will send the MQ_CREATE mailbox command to the HBA to setup the
10568 * mailbox queue. This function is asynchronous and will wait for the mailbox
10569 * command to finish before continuing.
10570 *
10571 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040010572 * memory this function will return -ENOMEM. If the queue create mailbox command
10573 * fails this function will return -ENXIO.
James Smartb19a0612010-04-06 14:48:51 -040010574 **/
10575int32_t
10576lpfc_mq_create(struct lpfc_hba *phba, struct lpfc_queue *mq,
10577 struct lpfc_queue *cq, uint32_t subtype)
10578{
10579 struct lpfc_mbx_mq_create *mq_create;
10580 struct lpfc_mbx_mq_create_ext *mq_create_ext;
10581 struct lpfc_dmabuf *dmabuf;
10582 LPFC_MBOXQ_t *mbox;
10583 int rc, length, status = 0;
10584 uint32_t shdr_status, shdr_add_status;
10585 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040010586 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smartb19a0612010-04-06 14:48:51 -040010587
James Smart49198b32010-04-06 15:04:33 -040010588 if (!phba->sli4_hba.pc_sli4_params.supported)
10589 hw_page_size = SLI4_PAGE_SIZE;
James Smartb19a0612010-04-06 14:48:51 -040010590
10591 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
10592 if (!mbox)
10593 return -ENOMEM;
10594 length = (sizeof(struct lpfc_mbx_mq_create_ext) -
10595 sizeof(struct lpfc_sli4_cfg_mhdr));
10596 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
10597 LPFC_MBOX_OPCODE_MQ_CREATE_EXT,
10598 length, LPFC_SLI4_MBX_EMBED);
10599
10600 mq_create_ext = &mbox->u.mqe.un.mq_create_ext;
James Smart70f3c072010-12-15 17:57:33 -050010601 bf_set(lpfc_mbx_mq_create_ext_num_pages,
10602 &mq_create_ext->u.request, mq->page_count);
10603 bf_set(lpfc_mbx_mq_create_ext_async_evt_link,
10604 &mq_create_ext->u.request, 1);
10605 bf_set(lpfc_mbx_mq_create_ext_async_evt_fip,
James Smartb19a0612010-04-06 14:48:51 -040010606 &mq_create_ext->u.request, 1);
10607 bf_set(lpfc_mbx_mq_create_ext_async_evt_group5,
10608 &mq_create_ext->u.request, 1);
James Smart70f3c072010-12-15 17:57:33 -050010609 bf_set(lpfc_mbx_mq_create_ext_async_evt_fc,
10610 &mq_create_ext->u.request, 1);
10611 bf_set(lpfc_mbx_mq_create_ext_async_evt_sli,
10612 &mq_create_ext->u.request, 1);
10613 bf_set(lpfc_mq_context_cq_id,
10614 &mq_create_ext->u.request.context, cq->queue_id);
James Smartb19a0612010-04-06 14:48:51 -040010615 bf_set(lpfc_mq_context_valid, &mq_create_ext->u.request.context, 1);
10616 switch (mq->entry_count) {
10617 default:
10618 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10619 "0362 Unsupported MQ count. (%d)\n",
10620 mq->entry_count);
10621 if (mq->entry_count < 16)
10622 return -EINVAL;
10623 /* otherwise default to smallest count (drop through) */
10624 case 16:
10625 bf_set(lpfc_mq_context_count, &mq_create_ext->u.request.context,
10626 LPFC_MQ_CNT_16);
10627 break;
10628 case 32:
10629 bf_set(lpfc_mq_context_count, &mq_create_ext->u.request.context,
10630 LPFC_MQ_CNT_32);
10631 break;
10632 case 64:
10633 bf_set(lpfc_mq_context_count, &mq_create_ext->u.request.context,
10634 LPFC_MQ_CNT_64);
10635 break;
10636 case 128:
10637 bf_set(lpfc_mq_context_count, &mq_create_ext->u.request.context,
10638 LPFC_MQ_CNT_128);
10639 break;
10640 }
10641 list_for_each_entry(dmabuf, &mq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040010642 memset(dmabuf->virt, 0, hw_page_size);
James Smartb19a0612010-04-06 14:48:51 -040010643 mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_lo =
10644 putPaddrLow(dmabuf->phys);
10645 mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_hi =
James Smart04c68492009-05-22 14:52:52 -040010646 putPaddrHigh(dmabuf->phys);
10647 }
10648 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
James Smartb19a0612010-04-06 14:48:51 -040010649 shdr = (union lpfc_sli4_cfg_shdr *) &mq_create_ext->header.cfg_shdr;
10650 mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
10651 &mq_create_ext->u.response);
10652 if (rc != MBX_SUCCESS) {
10653 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
10654 "2795 MQ_CREATE_EXT failed with "
10655 "status x%x. Failback to MQ_CREATE.\n",
10656 rc);
10657 lpfc_mq_create_fb_init(phba, mq, mbox, cq);
10658 mq_create = &mbox->u.mqe.un.mq_create;
10659 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
10660 shdr = (union lpfc_sli4_cfg_shdr *) &mq_create->header.cfg_shdr;
10661 mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
10662 &mq_create->u.response);
10663 }
10664
James Smart04c68492009-05-22 14:52:52 -040010665 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart04c68492009-05-22 14:52:52 -040010666 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10667 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10668 if (shdr_status || shdr_add_status || rc) {
10669 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10670 "2502 MQ_CREATE mailbox failed with "
10671 "status x%x add_status x%x, mbx status x%x\n",
10672 shdr_status, shdr_add_status, rc);
10673 status = -ENXIO;
10674 goto out;
10675 }
James Smart04c68492009-05-22 14:52:52 -040010676 if (mq->queue_id == 0xFFFF) {
10677 status = -ENXIO;
10678 goto out;
10679 }
10680 mq->type = LPFC_MQ;
10681 mq->subtype = subtype;
10682 mq->host_index = 0;
10683 mq->hba_index = 0;
10684
10685 /* link the mq onto the parent cq child list */
10686 list_add_tail(&mq->list, &cq->child_list);
10687out:
James Smart8fa38512009-07-19 10:01:03 -040010688 mempool_free(mbox, phba->mbox_mem_pool);
James Smart04c68492009-05-22 14:52:52 -040010689 return status;
10690}
10691
10692/**
James Smart4f774512009-05-22 14:52:35 -040010693 * lpfc_wq_create - Create a Work Queue on the HBA
10694 * @phba: HBA structure that indicates port to create a queue on.
10695 * @wq: The queue structure to use to create the work queue.
10696 * @cq: The completion queue to bind this work queue to.
10697 * @subtype: The subtype of the work queue indicating its functionality.
10698 *
10699 * This function creates a work queue, as detailed in @wq, on a port, described
10700 * by @phba by sending a WQ_CREATE mailbox command to the HBA.
10701 *
10702 * The @phba struct is used to send mailbox command to HBA. The @wq struct
10703 * is used to get the entry count and entry size that are necessary to
10704 * determine the number of pages to allocate and use for this queue. The @cq
10705 * is used to indicate which completion queue to bind this work queue to. This
10706 * function will send the WQ_CREATE mailbox command to the HBA to setup the
10707 * work queue. This function is asynchronous and will wait for the mailbox
10708 * command to finish before continuing.
10709 *
10710 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040010711 * memory this function will return -ENOMEM. If the queue create mailbox command
10712 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040010713 **/
10714uint32_t
10715lpfc_wq_create(struct lpfc_hba *phba, struct lpfc_queue *wq,
10716 struct lpfc_queue *cq, uint32_t subtype)
10717{
10718 struct lpfc_mbx_wq_create *wq_create;
10719 struct lpfc_dmabuf *dmabuf;
10720 LPFC_MBOXQ_t *mbox;
10721 int rc, length, status = 0;
10722 uint32_t shdr_status, shdr_add_status;
10723 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040010724 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
10725
10726 if (!phba->sli4_hba.pc_sli4_params.supported)
10727 hw_page_size = SLI4_PAGE_SIZE;
James Smart4f774512009-05-22 14:52:35 -040010728
10729 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
10730 if (!mbox)
10731 return -ENOMEM;
10732 length = (sizeof(struct lpfc_mbx_wq_create) -
10733 sizeof(struct lpfc_sli4_cfg_mhdr));
10734 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10735 LPFC_MBOX_OPCODE_FCOE_WQ_CREATE,
10736 length, LPFC_SLI4_MBX_EMBED);
10737 wq_create = &mbox->u.mqe.un.wq_create;
10738 bf_set(lpfc_mbx_wq_create_num_pages, &wq_create->u.request,
10739 wq->page_count);
10740 bf_set(lpfc_mbx_wq_create_cq_id, &wq_create->u.request,
10741 cq->queue_id);
10742 list_for_each_entry(dmabuf, &wq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040010743 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -040010744 wq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
10745 putPaddrLow(dmabuf->phys);
10746 wq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
10747 putPaddrHigh(dmabuf->phys);
10748 }
10749 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
10750 /* The IOCTL status is embedded in the mailbox subheader. */
10751 shdr = (union lpfc_sli4_cfg_shdr *) &wq_create->header.cfg_shdr;
10752 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10753 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10754 if (shdr_status || shdr_add_status || rc) {
10755 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10756 "2503 WQ_CREATE mailbox failed with "
10757 "status x%x add_status x%x, mbx status x%x\n",
10758 shdr_status, shdr_add_status, rc);
10759 status = -ENXIO;
10760 goto out;
10761 }
10762 wq->queue_id = bf_get(lpfc_mbx_wq_create_q_id, &wq_create->u.response);
10763 if (wq->queue_id == 0xFFFF) {
10764 status = -ENXIO;
10765 goto out;
10766 }
10767 wq->type = LPFC_WQ;
10768 wq->subtype = subtype;
10769 wq->host_index = 0;
10770 wq->hba_index = 0;
10771
10772 /* link the wq onto the parent cq child list */
10773 list_add_tail(&wq->list, &cq->child_list);
10774out:
James Smart8fa38512009-07-19 10:01:03 -040010775 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040010776 return status;
10777}
10778
10779/**
10780 * lpfc_rq_create - Create a Receive Queue on the HBA
10781 * @phba: HBA structure that indicates port to create a queue on.
10782 * @hrq: The queue structure to use to create the header receive queue.
10783 * @drq: The queue structure to use to create the data receive queue.
10784 * @cq: The completion queue to bind this work queue to.
10785 *
10786 * This function creates a receive buffer queue pair , as detailed in @hrq and
10787 * @drq, on a port, described by @phba by sending a RQ_CREATE mailbox command
10788 * to the HBA.
10789 *
10790 * The @phba struct is used to send mailbox command to HBA. The @drq and @hrq
10791 * struct is used to get the entry count that is necessary to determine the
10792 * number of pages to use for this queue. The @cq is used to indicate which
10793 * completion queue to bind received buffers that are posted to these queues to.
10794 * This function will send the RQ_CREATE mailbox command to the HBA to setup the
10795 * receive queue pair. This function is asynchronous and will wait for the
10796 * mailbox command to finish before continuing.
10797 *
10798 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040010799 * memory this function will return -ENOMEM. If the queue create mailbox command
10800 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040010801 **/
10802uint32_t
10803lpfc_rq_create(struct lpfc_hba *phba, struct lpfc_queue *hrq,
10804 struct lpfc_queue *drq, struct lpfc_queue *cq, uint32_t subtype)
10805{
10806 struct lpfc_mbx_rq_create *rq_create;
10807 struct lpfc_dmabuf *dmabuf;
10808 LPFC_MBOXQ_t *mbox;
10809 int rc, length, status = 0;
10810 uint32_t shdr_status, shdr_add_status;
10811 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040010812 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
10813
10814 if (!phba->sli4_hba.pc_sli4_params.supported)
10815 hw_page_size = SLI4_PAGE_SIZE;
James Smart4f774512009-05-22 14:52:35 -040010816
10817 if (hrq->entry_count != drq->entry_count)
10818 return -EINVAL;
10819 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
10820 if (!mbox)
10821 return -ENOMEM;
10822 length = (sizeof(struct lpfc_mbx_rq_create) -
10823 sizeof(struct lpfc_sli4_cfg_mhdr));
10824 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10825 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
10826 length, LPFC_SLI4_MBX_EMBED);
10827 rq_create = &mbox->u.mqe.un.rq_create;
10828 switch (hrq->entry_count) {
10829 default:
10830 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10831 "2535 Unsupported RQ count. (%d)\n",
10832 hrq->entry_count);
10833 if (hrq->entry_count < 512)
10834 return -EINVAL;
10835 /* otherwise default to smallest count (drop through) */
10836 case 512:
10837 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
10838 LPFC_RQ_RING_SIZE_512);
10839 break;
10840 case 1024:
10841 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
10842 LPFC_RQ_RING_SIZE_1024);
10843 break;
10844 case 2048:
10845 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
10846 LPFC_RQ_RING_SIZE_2048);
10847 break;
10848 case 4096:
10849 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
10850 LPFC_RQ_RING_SIZE_4096);
10851 break;
10852 }
10853 bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
10854 cq->queue_id);
10855 bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
10856 hrq->page_count);
10857 bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
10858 LPFC_HDR_BUF_SIZE);
10859 list_for_each_entry(dmabuf, &hrq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040010860 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -040010861 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
10862 putPaddrLow(dmabuf->phys);
10863 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
10864 putPaddrHigh(dmabuf->phys);
10865 }
10866 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
10867 /* The IOCTL status is embedded in the mailbox subheader. */
10868 shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
10869 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10870 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10871 if (shdr_status || shdr_add_status || rc) {
10872 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10873 "2504 RQ_CREATE mailbox failed with "
10874 "status x%x add_status x%x, mbx status x%x\n",
10875 shdr_status, shdr_add_status, rc);
10876 status = -ENXIO;
10877 goto out;
10878 }
10879 hrq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
10880 if (hrq->queue_id == 0xFFFF) {
10881 status = -ENXIO;
10882 goto out;
10883 }
10884 hrq->type = LPFC_HRQ;
10885 hrq->subtype = subtype;
10886 hrq->host_index = 0;
10887 hrq->hba_index = 0;
10888
10889 /* now create the data queue */
10890 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10891 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
10892 length, LPFC_SLI4_MBX_EMBED);
10893 switch (drq->entry_count) {
10894 default:
10895 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10896 "2536 Unsupported RQ count. (%d)\n",
10897 drq->entry_count);
10898 if (drq->entry_count < 512)
10899 return -EINVAL;
10900 /* otherwise default to smallest count (drop through) */
10901 case 512:
10902 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
10903 LPFC_RQ_RING_SIZE_512);
10904 break;
10905 case 1024:
10906 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
10907 LPFC_RQ_RING_SIZE_1024);
10908 break;
10909 case 2048:
10910 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
10911 LPFC_RQ_RING_SIZE_2048);
10912 break;
10913 case 4096:
10914 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
10915 LPFC_RQ_RING_SIZE_4096);
10916 break;
10917 }
10918 bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
10919 cq->queue_id);
10920 bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
10921 drq->page_count);
10922 bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
10923 LPFC_DATA_BUF_SIZE);
10924 list_for_each_entry(dmabuf, &drq->page_list, list) {
10925 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
10926 putPaddrLow(dmabuf->phys);
10927 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
10928 putPaddrHigh(dmabuf->phys);
10929 }
10930 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
10931 /* The IOCTL status is embedded in the mailbox subheader. */
10932 shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
10933 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10934 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10935 if (shdr_status || shdr_add_status || rc) {
10936 status = -ENXIO;
10937 goto out;
10938 }
10939 drq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
10940 if (drq->queue_id == 0xFFFF) {
10941 status = -ENXIO;
10942 goto out;
10943 }
10944 drq->type = LPFC_DRQ;
10945 drq->subtype = subtype;
10946 drq->host_index = 0;
10947 drq->hba_index = 0;
10948
10949 /* link the header and data RQs onto the parent cq child list */
10950 list_add_tail(&hrq->list, &cq->child_list);
10951 list_add_tail(&drq->list, &cq->child_list);
10952
10953out:
James Smart8fa38512009-07-19 10:01:03 -040010954 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040010955 return status;
10956}
10957
10958/**
10959 * lpfc_eq_destroy - Destroy an event Queue on the HBA
10960 * @eq: The queue structure associated with the queue to destroy.
10961 *
10962 * This function destroys a queue, as detailed in @eq by sending an mailbox
10963 * command, specific to the type of queue, to the HBA.
10964 *
10965 * The @eq struct is used to get the queue ID of the queue to destroy.
10966 *
10967 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040010968 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040010969 **/
10970uint32_t
10971lpfc_eq_destroy(struct lpfc_hba *phba, struct lpfc_queue *eq)
10972{
10973 LPFC_MBOXQ_t *mbox;
10974 int rc, length, status = 0;
10975 uint32_t shdr_status, shdr_add_status;
10976 union lpfc_sli4_cfg_shdr *shdr;
10977
10978 if (!eq)
10979 return -ENODEV;
10980 mbox = mempool_alloc(eq->phba->mbox_mem_pool, GFP_KERNEL);
10981 if (!mbox)
10982 return -ENOMEM;
10983 length = (sizeof(struct lpfc_mbx_eq_destroy) -
10984 sizeof(struct lpfc_sli4_cfg_mhdr));
10985 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
10986 LPFC_MBOX_OPCODE_EQ_DESTROY,
10987 length, LPFC_SLI4_MBX_EMBED);
10988 bf_set(lpfc_mbx_eq_destroy_q_id, &mbox->u.mqe.un.eq_destroy.u.request,
10989 eq->queue_id);
10990 mbox->vport = eq->phba->pport;
10991 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
10992
10993 rc = lpfc_sli_issue_mbox(eq->phba, mbox, MBX_POLL);
10994 /* The IOCTL status is embedded in the mailbox subheader. */
10995 shdr = (union lpfc_sli4_cfg_shdr *)
10996 &mbox->u.mqe.un.eq_destroy.header.cfg_shdr;
10997 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10998 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10999 if (shdr_status || shdr_add_status || rc) {
11000 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11001 "2505 EQ_DESTROY mailbox failed with "
11002 "status x%x add_status x%x, mbx status x%x\n",
11003 shdr_status, shdr_add_status, rc);
11004 status = -ENXIO;
11005 }
11006
11007 /* Remove eq from any list */
11008 list_del_init(&eq->list);
James Smart8fa38512009-07-19 10:01:03 -040011009 mempool_free(mbox, eq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040011010 return status;
11011}
11012
11013/**
11014 * lpfc_cq_destroy - Destroy a Completion Queue on the HBA
11015 * @cq: The queue structure associated with the queue to destroy.
11016 *
11017 * This function destroys a queue, as detailed in @cq by sending an mailbox
11018 * command, specific to the type of queue, to the HBA.
11019 *
11020 * The @cq struct is used to get the queue ID of the queue to destroy.
11021 *
11022 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040011023 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040011024 **/
11025uint32_t
11026lpfc_cq_destroy(struct lpfc_hba *phba, struct lpfc_queue *cq)
11027{
11028 LPFC_MBOXQ_t *mbox;
11029 int rc, length, status = 0;
11030 uint32_t shdr_status, shdr_add_status;
11031 union lpfc_sli4_cfg_shdr *shdr;
11032
11033 if (!cq)
11034 return -ENODEV;
11035 mbox = mempool_alloc(cq->phba->mbox_mem_pool, GFP_KERNEL);
11036 if (!mbox)
11037 return -ENOMEM;
11038 length = (sizeof(struct lpfc_mbx_cq_destroy) -
11039 sizeof(struct lpfc_sli4_cfg_mhdr));
11040 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
11041 LPFC_MBOX_OPCODE_CQ_DESTROY,
11042 length, LPFC_SLI4_MBX_EMBED);
11043 bf_set(lpfc_mbx_cq_destroy_q_id, &mbox->u.mqe.un.cq_destroy.u.request,
11044 cq->queue_id);
11045 mbox->vport = cq->phba->pport;
11046 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
11047 rc = lpfc_sli_issue_mbox(cq->phba, mbox, MBX_POLL);
11048 /* The IOCTL status is embedded in the mailbox subheader. */
11049 shdr = (union lpfc_sli4_cfg_shdr *)
11050 &mbox->u.mqe.un.wq_create.header.cfg_shdr;
11051 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
11052 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
11053 if (shdr_status || shdr_add_status || rc) {
11054 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11055 "2506 CQ_DESTROY mailbox failed with "
11056 "status x%x add_status x%x, mbx status x%x\n",
11057 shdr_status, shdr_add_status, rc);
11058 status = -ENXIO;
11059 }
11060 /* Remove cq from any list */
11061 list_del_init(&cq->list);
James Smart8fa38512009-07-19 10:01:03 -040011062 mempool_free(mbox, cq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040011063 return status;
11064}
11065
11066/**
James Smart04c68492009-05-22 14:52:52 -040011067 * lpfc_mq_destroy - Destroy a Mailbox Queue on the HBA
11068 * @qm: The queue structure associated with the queue to destroy.
11069 *
11070 * This function destroys a queue, as detailed in @mq by sending an mailbox
11071 * command, specific to the type of queue, to the HBA.
11072 *
11073 * The @mq struct is used to get the queue ID of the queue to destroy.
11074 *
11075 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040011076 * command fails this function will return -ENXIO.
James Smart04c68492009-05-22 14:52:52 -040011077 **/
11078uint32_t
11079lpfc_mq_destroy(struct lpfc_hba *phba, struct lpfc_queue *mq)
11080{
11081 LPFC_MBOXQ_t *mbox;
11082 int rc, length, status = 0;
11083 uint32_t shdr_status, shdr_add_status;
11084 union lpfc_sli4_cfg_shdr *shdr;
11085
11086 if (!mq)
11087 return -ENODEV;
11088 mbox = mempool_alloc(mq->phba->mbox_mem_pool, GFP_KERNEL);
11089 if (!mbox)
11090 return -ENOMEM;
11091 length = (sizeof(struct lpfc_mbx_mq_destroy) -
11092 sizeof(struct lpfc_sli4_cfg_mhdr));
11093 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
11094 LPFC_MBOX_OPCODE_MQ_DESTROY,
11095 length, LPFC_SLI4_MBX_EMBED);
11096 bf_set(lpfc_mbx_mq_destroy_q_id, &mbox->u.mqe.un.mq_destroy.u.request,
11097 mq->queue_id);
11098 mbox->vport = mq->phba->pport;
11099 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
11100 rc = lpfc_sli_issue_mbox(mq->phba, mbox, MBX_POLL);
11101 /* The IOCTL status is embedded in the mailbox subheader. */
11102 shdr = (union lpfc_sli4_cfg_shdr *)
11103 &mbox->u.mqe.un.mq_destroy.header.cfg_shdr;
11104 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
11105 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
11106 if (shdr_status || shdr_add_status || rc) {
11107 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11108 "2507 MQ_DESTROY mailbox failed with "
11109 "status x%x add_status x%x, mbx status x%x\n",
11110 shdr_status, shdr_add_status, rc);
11111 status = -ENXIO;
11112 }
11113 /* Remove mq from any list */
11114 list_del_init(&mq->list);
James Smart8fa38512009-07-19 10:01:03 -040011115 mempool_free(mbox, mq->phba->mbox_mem_pool);
James Smart04c68492009-05-22 14:52:52 -040011116 return status;
11117}
11118
11119/**
James Smart4f774512009-05-22 14:52:35 -040011120 * lpfc_wq_destroy - Destroy a Work Queue on the HBA
11121 * @wq: The queue structure associated with the queue to destroy.
11122 *
11123 * This function destroys a queue, as detailed in @wq by sending an mailbox
11124 * command, specific to the type of queue, to the HBA.
11125 *
11126 * The @wq struct is used to get the queue ID of the queue to destroy.
11127 *
11128 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040011129 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040011130 **/
11131uint32_t
11132lpfc_wq_destroy(struct lpfc_hba *phba, struct lpfc_queue *wq)
11133{
11134 LPFC_MBOXQ_t *mbox;
11135 int rc, length, status = 0;
11136 uint32_t shdr_status, shdr_add_status;
11137 union lpfc_sli4_cfg_shdr *shdr;
11138
11139 if (!wq)
11140 return -ENODEV;
11141 mbox = mempool_alloc(wq->phba->mbox_mem_pool, GFP_KERNEL);
11142 if (!mbox)
11143 return -ENOMEM;
11144 length = (sizeof(struct lpfc_mbx_wq_destroy) -
11145 sizeof(struct lpfc_sli4_cfg_mhdr));
11146 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
11147 LPFC_MBOX_OPCODE_FCOE_WQ_DESTROY,
11148 length, LPFC_SLI4_MBX_EMBED);
11149 bf_set(lpfc_mbx_wq_destroy_q_id, &mbox->u.mqe.un.wq_destroy.u.request,
11150 wq->queue_id);
11151 mbox->vport = wq->phba->pport;
11152 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
11153 rc = lpfc_sli_issue_mbox(wq->phba, mbox, MBX_POLL);
11154 shdr = (union lpfc_sli4_cfg_shdr *)
11155 &mbox->u.mqe.un.wq_destroy.header.cfg_shdr;
11156 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
11157 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
11158 if (shdr_status || shdr_add_status || rc) {
11159 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11160 "2508 WQ_DESTROY mailbox failed with "
11161 "status x%x add_status x%x, mbx status x%x\n",
11162 shdr_status, shdr_add_status, rc);
11163 status = -ENXIO;
11164 }
11165 /* Remove wq from any list */
11166 list_del_init(&wq->list);
James Smart8fa38512009-07-19 10:01:03 -040011167 mempool_free(mbox, wq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040011168 return status;
11169}
11170
11171/**
11172 * lpfc_rq_destroy - Destroy a Receive Queue on the HBA
11173 * @rq: The queue structure associated with the queue to destroy.
11174 *
11175 * This function destroys a queue, as detailed in @rq by sending an mailbox
11176 * command, specific to the type of queue, to the HBA.
11177 *
11178 * The @rq struct is used to get the queue ID of the queue to destroy.
11179 *
11180 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040011181 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040011182 **/
11183uint32_t
11184lpfc_rq_destroy(struct lpfc_hba *phba, struct lpfc_queue *hrq,
11185 struct lpfc_queue *drq)
11186{
11187 LPFC_MBOXQ_t *mbox;
11188 int rc, length, status = 0;
11189 uint32_t shdr_status, shdr_add_status;
11190 union lpfc_sli4_cfg_shdr *shdr;
11191
11192 if (!hrq || !drq)
11193 return -ENODEV;
11194 mbox = mempool_alloc(hrq->phba->mbox_mem_pool, GFP_KERNEL);
11195 if (!mbox)
11196 return -ENOMEM;
11197 length = (sizeof(struct lpfc_mbx_rq_destroy) -
James Smartfedd3b72011-02-16 12:39:24 -050011198 sizeof(struct lpfc_sli4_cfg_mhdr));
James Smart4f774512009-05-22 14:52:35 -040011199 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
11200 LPFC_MBOX_OPCODE_FCOE_RQ_DESTROY,
11201 length, LPFC_SLI4_MBX_EMBED);
11202 bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
11203 hrq->queue_id);
11204 mbox->vport = hrq->phba->pport;
11205 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
11206 rc = lpfc_sli_issue_mbox(hrq->phba, mbox, MBX_POLL);
11207 /* The IOCTL status is embedded in the mailbox subheader. */
11208 shdr = (union lpfc_sli4_cfg_shdr *)
11209 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
11210 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
11211 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
11212 if (shdr_status || shdr_add_status || rc) {
11213 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11214 "2509 RQ_DESTROY mailbox failed with "
11215 "status x%x add_status x%x, mbx status x%x\n",
11216 shdr_status, shdr_add_status, rc);
11217 if (rc != MBX_TIMEOUT)
11218 mempool_free(mbox, hrq->phba->mbox_mem_pool);
11219 return -ENXIO;
11220 }
11221 bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
11222 drq->queue_id);
11223 rc = lpfc_sli_issue_mbox(drq->phba, mbox, MBX_POLL);
11224 shdr = (union lpfc_sli4_cfg_shdr *)
11225 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
11226 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
11227 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
11228 if (shdr_status || shdr_add_status || rc) {
11229 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11230 "2510 RQ_DESTROY mailbox failed with "
11231 "status x%x add_status x%x, mbx status x%x\n",
11232 shdr_status, shdr_add_status, rc);
11233 status = -ENXIO;
11234 }
11235 list_del_init(&hrq->list);
11236 list_del_init(&drq->list);
James Smart8fa38512009-07-19 10:01:03 -040011237 mempool_free(mbox, hrq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040011238 return status;
11239}
11240
11241/**
11242 * lpfc_sli4_post_sgl - Post scatter gather list for an XRI to HBA
11243 * @phba: The virtual port for which this call being executed.
11244 * @pdma_phys_addr0: Physical address of the 1st SGL page.
11245 * @pdma_phys_addr1: Physical address of the 2nd SGL page.
11246 * @xritag: the xritag that ties this io to the SGL pages.
11247 *
11248 * This routine will post the sgl pages for the IO that has the xritag
11249 * that is in the iocbq structure. The xritag is assigned during iocbq
11250 * creation and persists for as long as the driver is loaded.
11251 * if the caller has fewer than 256 scatter gather segments to map then
11252 * pdma_phys_addr1 should be 0.
11253 * If the caller needs to map more than 256 scatter gather segment then
11254 * pdma_phys_addr1 should be a valid physical address.
11255 * physical address for SGLs must be 64 byte aligned.
11256 * If you are going to map 2 SGL's then the first one must have 256 entries
11257 * the second sgl can have between 1 and 256 entries.
11258 *
11259 * Return codes:
11260 * 0 - Success
11261 * -ENXIO, -ENOMEM - Failure
11262 **/
11263int
11264lpfc_sli4_post_sgl(struct lpfc_hba *phba,
11265 dma_addr_t pdma_phys_addr0,
11266 dma_addr_t pdma_phys_addr1,
11267 uint16_t xritag)
11268{
11269 struct lpfc_mbx_post_sgl_pages *post_sgl_pages;
11270 LPFC_MBOXQ_t *mbox;
11271 int rc;
11272 uint32_t shdr_status, shdr_add_status;
11273 union lpfc_sli4_cfg_shdr *shdr;
11274
11275 if (xritag == NO_XRI) {
11276 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11277 "0364 Invalid param:\n");
11278 return -EINVAL;
11279 }
11280
11281 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
11282 if (!mbox)
11283 return -ENOMEM;
11284
11285 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
11286 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES,
11287 sizeof(struct lpfc_mbx_post_sgl_pages) -
James Smartfedd3b72011-02-16 12:39:24 -050011288 sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
James Smart4f774512009-05-22 14:52:35 -040011289
11290 post_sgl_pages = (struct lpfc_mbx_post_sgl_pages *)
11291 &mbox->u.mqe.un.post_sgl_pages;
11292 bf_set(lpfc_post_sgl_pages_xri, post_sgl_pages, xritag);
11293 bf_set(lpfc_post_sgl_pages_xricnt, post_sgl_pages, 1);
11294
11295 post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_lo =
11296 cpu_to_le32(putPaddrLow(pdma_phys_addr0));
11297 post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_hi =
11298 cpu_to_le32(putPaddrHigh(pdma_phys_addr0));
11299
11300 post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_lo =
11301 cpu_to_le32(putPaddrLow(pdma_phys_addr1));
11302 post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_hi =
11303 cpu_to_le32(putPaddrHigh(pdma_phys_addr1));
11304 if (!phba->sli4_hba.intr_enable)
11305 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
11306 else
11307 rc = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
11308 /* The IOCTL status is embedded in the mailbox subheader. */
11309 shdr = (union lpfc_sli4_cfg_shdr *) &post_sgl_pages->header.cfg_shdr;
11310 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
11311 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
11312 if (rc != MBX_TIMEOUT)
11313 mempool_free(mbox, phba->mbox_mem_pool);
11314 if (shdr_status || shdr_add_status || rc) {
11315 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11316 "2511 POST_SGL mailbox failed with "
11317 "status x%x add_status x%x, mbx status x%x\n",
11318 shdr_status, shdr_add_status, rc);
11319 rc = -ENXIO;
11320 }
11321 return 0;
11322}
James Smart4f774512009-05-22 14:52:35 -040011323
11324/**
11325 * lpfc_sli4_next_xritag - Get an xritag for the io
11326 * @phba: Pointer to HBA context object.
11327 *
11328 * This function gets an xritag for the iocb. If there is no unused xritag
11329 * it will return 0xffff.
11330 * The function returns the allocated xritag if successful, else returns zero.
11331 * Zero is not a valid xritag.
11332 * The caller is not required to hold any lock.
11333 **/
11334uint16_t
11335lpfc_sli4_next_xritag(struct lpfc_hba *phba)
11336{
11337 uint16_t xritag;
11338
11339 spin_lock_irq(&phba->hbalock);
11340 xritag = phba->sli4_hba.next_xri;
11341 if ((xritag != (uint16_t) -1) && xritag <
11342 (phba->sli4_hba.max_cfg_param.max_xri
11343 + phba->sli4_hba.max_cfg_param.xri_base)) {
11344 phba->sli4_hba.next_xri++;
11345 phba->sli4_hba.max_cfg_param.xri_used++;
11346 spin_unlock_irq(&phba->hbalock);
11347 return xritag;
11348 }
11349 spin_unlock_irq(&phba->hbalock);
James Smart6a9c52c2009-10-02 15:16:51 -040011350 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smart4f774512009-05-22 14:52:35 -040011351 "2004 Failed to allocate XRI.last XRITAG is %d"
11352 " Max XRI is %d, Used XRI is %d\n",
11353 phba->sli4_hba.next_xri,
11354 phba->sli4_hba.max_cfg_param.max_xri,
11355 phba->sli4_hba.max_cfg_param.xri_used);
11356 return -1;
11357}
11358
11359/**
11360 * lpfc_sli4_post_sgl_list - post a block of sgl list to the firmware.
11361 * @phba: pointer to lpfc hba data structure.
11362 *
11363 * This routine is invoked to post a block of driver's sgl pages to the
11364 * HBA using non-embedded mailbox command. No Lock is held. This routine
11365 * is only called when the driver is loading and after all IO has been
11366 * stopped.
11367 **/
11368int
11369lpfc_sli4_post_sgl_list(struct lpfc_hba *phba)
11370{
11371 struct lpfc_sglq *sglq_entry;
11372 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
11373 struct sgl_page_pairs *sgl_pg_pairs;
11374 void *viraddr;
11375 LPFC_MBOXQ_t *mbox;
11376 uint32_t reqlen, alloclen, pg_pairs;
11377 uint32_t mbox_tmo;
11378 uint16_t xritag_start = 0;
11379 int els_xri_cnt, rc = 0;
11380 uint32_t shdr_status, shdr_add_status;
11381 union lpfc_sli4_cfg_shdr *shdr;
11382
11383 /* The number of sgls to be posted */
11384 els_xri_cnt = lpfc_sli4_get_els_iocb_cnt(phba);
11385
11386 reqlen = els_xri_cnt * sizeof(struct sgl_page_pairs) +
11387 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
James Smart49198b32010-04-06 15:04:33 -040011388 if (reqlen > SLI4_PAGE_SIZE) {
James Smart4f774512009-05-22 14:52:35 -040011389 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
11390 "2559 Block sgl registration required DMA "
11391 "size (%d) great than a page\n", reqlen);
11392 return -ENOMEM;
11393 }
11394 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
11395 if (!mbox) {
11396 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11397 "2560 Failed to allocate mbox cmd memory\n");
11398 return -ENOMEM;
11399 }
11400
11401 /* Allocate DMA memory and set up the non-embedded mailbox command */
11402 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
11403 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
11404 LPFC_SLI4_MBX_NEMBED);
11405
11406 if (alloclen < reqlen) {
11407 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11408 "0285 Allocated DMA memory size (%d) is "
11409 "less than the requested DMA memory "
11410 "size (%d)\n", alloclen, reqlen);
11411 lpfc_sli4_mbox_cmd_free(phba, mbox);
11412 return -ENOMEM;
11413 }
James Smart4f774512009-05-22 14:52:35 -040011414 /* Get the first SGE entry from the non-embedded DMA memory */
James Smart4f774512009-05-22 14:52:35 -040011415 viraddr = mbox->sge_array->addr[0];
11416
11417 /* Set up the SGL pages in the non-embedded DMA pages */
11418 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
11419 sgl_pg_pairs = &sgl->sgl_pg_pairs;
11420
11421 for (pg_pairs = 0; pg_pairs < els_xri_cnt; pg_pairs++) {
11422 sglq_entry = phba->sli4_hba.lpfc_els_sgl_array[pg_pairs];
11423 /* Set up the sge entry */
11424 sgl_pg_pairs->sgl_pg0_addr_lo =
11425 cpu_to_le32(putPaddrLow(sglq_entry->phys));
11426 sgl_pg_pairs->sgl_pg0_addr_hi =
11427 cpu_to_le32(putPaddrHigh(sglq_entry->phys));
11428 sgl_pg_pairs->sgl_pg1_addr_lo =
11429 cpu_to_le32(putPaddrLow(0));
11430 sgl_pg_pairs->sgl_pg1_addr_hi =
11431 cpu_to_le32(putPaddrHigh(0));
11432 /* Keep the first xritag on the list */
11433 if (pg_pairs == 0)
11434 xritag_start = sglq_entry->sli4_xritag;
11435 sgl_pg_pairs++;
11436 }
11437 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
James Smart6a9c52c2009-10-02 15:16:51 -040011438 bf_set(lpfc_post_sgl_pages_xricnt, sgl, els_xri_cnt);
James Smart4f774512009-05-22 14:52:35 -040011439 /* Perform endian conversion if necessary */
11440 sgl->word0 = cpu_to_le32(sgl->word0);
11441
11442 if (!phba->sli4_hba.intr_enable)
11443 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
11444 else {
11445 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_SLI4_CONFIG);
11446 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
11447 }
11448 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
11449 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
11450 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
11451 if (rc != MBX_TIMEOUT)
11452 lpfc_sli4_mbox_cmd_free(phba, mbox);
11453 if (shdr_status || shdr_add_status || rc) {
11454 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11455 "2513 POST_SGL_BLOCK mailbox command failed "
11456 "status x%x add_status x%x mbx status x%x\n",
11457 shdr_status, shdr_add_status, rc);
11458 rc = -ENXIO;
11459 }
11460 return rc;
11461}
11462
11463/**
11464 * lpfc_sli4_post_scsi_sgl_block - post a block of scsi sgl list to firmware
11465 * @phba: pointer to lpfc hba data structure.
11466 * @sblist: pointer to scsi buffer list.
11467 * @count: number of scsi buffers on the list.
11468 *
11469 * This routine is invoked to post a block of @count scsi sgl pages from a
11470 * SCSI buffer list @sblist to the HBA using non-embedded mailbox command.
11471 * No Lock is held.
11472 *
11473 **/
11474int
11475lpfc_sli4_post_scsi_sgl_block(struct lpfc_hba *phba, struct list_head *sblist,
11476 int cnt)
11477{
11478 struct lpfc_scsi_buf *psb;
11479 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
11480 struct sgl_page_pairs *sgl_pg_pairs;
11481 void *viraddr;
11482 LPFC_MBOXQ_t *mbox;
11483 uint32_t reqlen, alloclen, pg_pairs;
11484 uint32_t mbox_tmo;
11485 uint16_t xritag_start = 0;
11486 int rc = 0;
11487 uint32_t shdr_status, shdr_add_status;
11488 dma_addr_t pdma_phys_bpl1;
11489 union lpfc_sli4_cfg_shdr *shdr;
11490
11491 /* Calculate the requested length of the dma memory */
11492 reqlen = cnt * sizeof(struct sgl_page_pairs) +
11493 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
James Smart49198b32010-04-06 15:04:33 -040011494 if (reqlen > SLI4_PAGE_SIZE) {
James Smart4f774512009-05-22 14:52:35 -040011495 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
11496 "0217 Block sgl registration required DMA "
11497 "size (%d) great than a page\n", reqlen);
11498 return -ENOMEM;
11499 }
11500 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
11501 if (!mbox) {
11502 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11503 "0283 Failed to allocate mbox cmd memory\n");
11504 return -ENOMEM;
11505 }
11506
11507 /* Allocate DMA memory and set up the non-embedded mailbox command */
11508 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
11509 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
11510 LPFC_SLI4_MBX_NEMBED);
11511
11512 if (alloclen < reqlen) {
11513 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11514 "2561 Allocated DMA memory size (%d) is "
11515 "less than the requested DMA memory "
11516 "size (%d)\n", alloclen, reqlen);
11517 lpfc_sli4_mbox_cmd_free(phba, mbox);
11518 return -ENOMEM;
11519 }
James Smart4f774512009-05-22 14:52:35 -040011520 /* Get the first SGE entry from the non-embedded DMA memory */
James Smart4f774512009-05-22 14:52:35 -040011521 viraddr = mbox->sge_array->addr[0];
11522
11523 /* Set up the SGL pages in the non-embedded DMA pages */
11524 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
11525 sgl_pg_pairs = &sgl->sgl_pg_pairs;
11526
11527 pg_pairs = 0;
11528 list_for_each_entry(psb, sblist, list) {
11529 /* Set up the sge entry */
11530 sgl_pg_pairs->sgl_pg0_addr_lo =
11531 cpu_to_le32(putPaddrLow(psb->dma_phys_bpl));
11532 sgl_pg_pairs->sgl_pg0_addr_hi =
11533 cpu_to_le32(putPaddrHigh(psb->dma_phys_bpl));
11534 if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
11535 pdma_phys_bpl1 = psb->dma_phys_bpl + SGL_PAGE_SIZE;
11536 else
11537 pdma_phys_bpl1 = 0;
11538 sgl_pg_pairs->sgl_pg1_addr_lo =
11539 cpu_to_le32(putPaddrLow(pdma_phys_bpl1));
11540 sgl_pg_pairs->sgl_pg1_addr_hi =
11541 cpu_to_le32(putPaddrHigh(pdma_phys_bpl1));
11542 /* Keep the first xritag on the list */
11543 if (pg_pairs == 0)
11544 xritag_start = psb->cur_iocbq.sli4_xritag;
11545 sgl_pg_pairs++;
11546 pg_pairs++;
11547 }
11548 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
11549 bf_set(lpfc_post_sgl_pages_xricnt, sgl, pg_pairs);
11550 /* Perform endian conversion if necessary */
11551 sgl->word0 = cpu_to_le32(sgl->word0);
11552
11553 if (!phba->sli4_hba.intr_enable)
11554 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
11555 else {
11556 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_SLI4_CONFIG);
11557 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
11558 }
11559 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
11560 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
11561 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
11562 if (rc != MBX_TIMEOUT)
11563 lpfc_sli4_mbox_cmd_free(phba, mbox);
11564 if (shdr_status || shdr_add_status || rc) {
11565 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11566 "2564 POST_SGL_BLOCK mailbox command failed "
11567 "status x%x add_status x%x mbx status x%x\n",
11568 shdr_status, shdr_add_status, rc);
11569 rc = -ENXIO;
11570 }
11571 return rc;
11572}
11573
11574/**
11575 * lpfc_fc_frame_check - Check that this frame is a valid frame to handle
11576 * @phba: pointer to lpfc_hba struct that the frame was received on
11577 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
11578 *
11579 * This function checks the fields in the @fc_hdr to see if the FC frame is a
11580 * valid type of frame that the LPFC driver will handle. This function will
11581 * return a zero if the frame is a valid frame or a non zero value when the
11582 * frame does not pass the check.
11583 **/
11584static int
11585lpfc_fc_frame_check(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr)
11586{
Tomas Henzl474ffb72010-12-22 16:52:40 +010011587 /* make rctl_names static to save stack space */
11588 static char *rctl_names[] = FC_RCTL_NAMES_INIT;
James Smart4f774512009-05-22 14:52:35 -040011589 char *type_names[] = FC_TYPE_NAMES_INIT;
11590 struct fc_vft_header *fc_vft_hdr;
11591
11592 switch (fc_hdr->fh_r_ctl) {
11593 case FC_RCTL_DD_UNCAT: /* uncategorized information */
11594 case FC_RCTL_DD_SOL_DATA: /* solicited data */
11595 case FC_RCTL_DD_UNSOL_CTL: /* unsolicited control */
11596 case FC_RCTL_DD_SOL_CTL: /* solicited control or reply */
11597 case FC_RCTL_DD_UNSOL_DATA: /* unsolicited data */
11598 case FC_RCTL_DD_DATA_DESC: /* data descriptor */
11599 case FC_RCTL_DD_UNSOL_CMD: /* unsolicited command */
11600 case FC_RCTL_DD_CMD_STATUS: /* command status */
11601 case FC_RCTL_ELS_REQ: /* extended link services request */
11602 case FC_RCTL_ELS_REP: /* extended link services reply */
11603 case FC_RCTL_ELS4_REQ: /* FC-4 ELS request */
11604 case FC_RCTL_ELS4_REP: /* FC-4 ELS reply */
11605 case FC_RCTL_BA_NOP: /* basic link service NOP */
11606 case FC_RCTL_BA_ABTS: /* basic link service abort */
11607 case FC_RCTL_BA_RMC: /* remove connection */
11608 case FC_RCTL_BA_ACC: /* basic accept */
11609 case FC_RCTL_BA_RJT: /* basic reject */
11610 case FC_RCTL_BA_PRMT:
11611 case FC_RCTL_ACK_1: /* acknowledge_1 */
11612 case FC_RCTL_ACK_0: /* acknowledge_0 */
11613 case FC_RCTL_P_RJT: /* port reject */
11614 case FC_RCTL_F_RJT: /* fabric reject */
11615 case FC_RCTL_P_BSY: /* port busy */
11616 case FC_RCTL_F_BSY: /* fabric busy to data frame */
11617 case FC_RCTL_F_BSYL: /* fabric busy to link control frame */
11618 case FC_RCTL_LCR: /* link credit reset */
11619 case FC_RCTL_END: /* end */
11620 break;
11621 case FC_RCTL_VFTH: /* Virtual Fabric tagging Header */
11622 fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
11623 fc_hdr = &((struct fc_frame_header *)fc_vft_hdr)[1];
11624 return lpfc_fc_frame_check(phba, fc_hdr);
11625 default:
11626 goto drop;
11627 }
11628 switch (fc_hdr->fh_type) {
11629 case FC_TYPE_BLS:
11630 case FC_TYPE_ELS:
11631 case FC_TYPE_FCP:
11632 case FC_TYPE_CT:
11633 break;
11634 case FC_TYPE_IP:
11635 case FC_TYPE_ILS:
11636 default:
11637 goto drop;
11638 }
11639 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
11640 "2538 Received frame rctl:%s type:%s\n",
11641 rctl_names[fc_hdr->fh_r_ctl],
11642 type_names[fc_hdr->fh_type]);
11643 return 0;
11644drop:
11645 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
11646 "2539 Dropped frame rctl:%s type:%s\n",
11647 rctl_names[fc_hdr->fh_r_ctl],
11648 type_names[fc_hdr->fh_type]);
11649 return 1;
11650}
11651
11652/**
11653 * lpfc_fc_hdr_get_vfi - Get the VFI from an FC frame
11654 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
11655 *
11656 * This function processes the FC header to retrieve the VFI from the VF
11657 * header, if one exists. This function will return the VFI if one exists
11658 * or 0 if no VSAN Header exists.
11659 **/
11660static uint32_t
11661lpfc_fc_hdr_get_vfi(struct fc_frame_header *fc_hdr)
11662{
11663 struct fc_vft_header *fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
11664
11665 if (fc_hdr->fh_r_ctl != FC_RCTL_VFTH)
11666 return 0;
11667 return bf_get(fc_vft_hdr_vf_id, fc_vft_hdr);
11668}
11669
11670/**
11671 * lpfc_fc_frame_to_vport - Finds the vport that a frame is destined to
11672 * @phba: Pointer to the HBA structure to search for the vport on
11673 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
11674 * @fcfi: The FC Fabric ID that the frame came from
11675 *
11676 * This function searches the @phba for a vport that matches the content of the
11677 * @fc_hdr passed in and the @fcfi. This function uses the @fc_hdr to fetch the
11678 * VFI, if the Virtual Fabric Tagging Header exists, and the DID. This function
11679 * returns the matching vport pointer or NULL if unable to match frame to a
11680 * vport.
11681 **/
11682static struct lpfc_vport *
11683lpfc_fc_frame_to_vport(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr,
11684 uint16_t fcfi)
11685{
11686 struct lpfc_vport **vports;
11687 struct lpfc_vport *vport = NULL;
11688 int i;
11689 uint32_t did = (fc_hdr->fh_d_id[0] << 16 |
11690 fc_hdr->fh_d_id[1] << 8 |
11691 fc_hdr->fh_d_id[2]);
11692
11693 vports = lpfc_create_vport_work_array(phba);
11694 if (vports != NULL)
11695 for (i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
11696 if (phba->fcf.fcfi == fcfi &&
11697 vports[i]->vfi == lpfc_fc_hdr_get_vfi(fc_hdr) &&
11698 vports[i]->fc_myDID == did) {
11699 vport = vports[i];
11700 break;
11701 }
11702 }
11703 lpfc_destroy_vport_work_array(phba, vports);
11704 return vport;
11705}
11706
11707/**
James Smart45ed1192009-10-02 15:17:02 -040011708 * lpfc_update_rcv_time_stamp - Update vport's rcv seq time stamp
11709 * @vport: The vport to work on.
11710 *
11711 * This function updates the receive sequence time stamp for this vport. The
11712 * receive sequence time stamp indicates the time that the last frame of the
11713 * the sequence that has been idle for the longest amount of time was received.
11714 * the driver uses this time stamp to indicate if any received sequences have
11715 * timed out.
11716 **/
11717void
11718lpfc_update_rcv_time_stamp(struct lpfc_vport *vport)
11719{
11720 struct lpfc_dmabuf *h_buf;
11721 struct hbq_dmabuf *dmabuf = NULL;
11722
11723 /* get the oldest sequence on the rcv list */
11724 h_buf = list_get_first(&vport->rcv_buffer_list,
11725 struct lpfc_dmabuf, list);
11726 if (!h_buf)
11727 return;
11728 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
11729 vport->rcv_buffer_time_stamp = dmabuf->time_stamp;
11730}
11731
11732/**
11733 * lpfc_cleanup_rcv_buffers - Cleans up all outstanding receive sequences.
11734 * @vport: The vport that the received sequences were sent to.
11735 *
11736 * This function cleans up all outstanding received sequences. This is called
11737 * by the driver when a link event or user action invalidates all the received
11738 * sequences.
11739 **/
11740void
11741lpfc_cleanup_rcv_buffers(struct lpfc_vport *vport)
11742{
11743 struct lpfc_dmabuf *h_buf, *hnext;
11744 struct lpfc_dmabuf *d_buf, *dnext;
11745 struct hbq_dmabuf *dmabuf = NULL;
11746
11747 /* start with the oldest sequence on the rcv list */
11748 list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
11749 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
11750 list_del_init(&dmabuf->hbuf.list);
11751 list_for_each_entry_safe(d_buf, dnext,
11752 &dmabuf->dbuf.list, list) {
11753 list_del_init(&d_buf->list);
11754 lpfc_in_buf_free(vport->phba, d_buf);
11755 }
11756 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
11757 }
11758}
11759
11760/**
11761 * lpfc_rcv_seq_check_edtov - Cleans up timed out receive sequences.
11762 * @vport: The vport that the received sequences were sent to.
11763 *
11764 * This function determines whether any received sequences have timed out by
11765 * first checking the vport's rcv_buffer_time_stamp. If this time_stamp
11766 * indicates that there is at least one timed out sequence this routine will
11767 * go through the received sequences one at a time from most inactive to most
11768 * active to determine which ones need to be cleaned up. Once it has determined
11769 * that a sequence needs to be cleaned up it will simply free up the resources
11770 * without sending an abort.
11771 **/
11772void
11773lpfc_rcv_seq_check_edtov(struct lpfc_vport *vport)
11774{
11775 struct lpfc_dmabuf *h_buf, *hnext;
11776 struct lpfc_dmabuf *d_buf, *dnext;
11777 struct hbq_dmabuf *dmabuf = NULL;
11778 unsigned long timeout;
11779 int abort_count = 0;
11780
11781 timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
11782 vport->rcv_buffer_time_stamp);
11783 if (list_empty(&vport->rcv_buffer_list) ||
11784 time_before(jiffies, timeout))
11785 return;
11786 /* start with the oldest sequence on the rcv list */
11787 list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
11788 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
11789 timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
11790 dmabuf->time_stamp);
11791 if (time_before(jiffies, timeout))
11792 break;
11793 abort_count++;
11794 list_del_init(&dmabuf->hbuf.list);
11795 list_for_each_entry_safe(d_buf, dnext,
11796 &dmabuf->dbuf.list, list) {
11797 list_del_init(&d_buf->list);
11798 lpfc_in_buf_free(vport->phba, d_buf);
11799 }
11800 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
11801 }
11802 if (abort_count)
11803 lpfc_update_rcv_time_stamp(vport);
11804}
11805
11806/**
James Smart4f774512009-05-22 14:52:35 -040011807 * lpfc_fc_frame_add - Adds a frame to the vport's list of received sequences
11808 * @dmabuf: pointer to a dmabuf that describes the hdr and data of the FC frame
11809 *
11810 * This function searches through the existing incomplete sequences that have
11811 * been sent to this @vport. If the frame matches one of the incomplete
11812 * sequences then the dbuf in the @dmabuf is added to the list of frames that
11813 * make up that sequence. If no sequence is found that matches this frame then
11814 * the function will add the hbuf in the @dmabuf to the @vport's rcv_buffer_list
11815 * This function returns a pointer to the first dmabuf in the sequence list that
11816 * the frame was linked to.
11817 **/
11818static struct hbq_dmabuf *
11819lpfc_fc_frame_add(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
11820{
11821 struct fc_frame_header *new_hdr;
11822 struct fc_frame_header *temp_hdr;
11823 struct lpfc_dmabuf *d_buf;
11824 struct lpfc_dmabuf *h_buf;
11825 struct hbq_dmabuf *seq_dmabuf = NULL;
11826 struct hbq_dmabuf *temp_dmabuf = NULL;
11827
James Smart4d9ab992009-10-02 15:16:39 -040011828 INIT_LIST_HEAD(&dmabuf->dbuf.list);
James Smart45ed1192009-10-02 15:17:02 -040011829 dmabuf->time_stamp = jiffies;
James Smart4f774512009-05-22 14:52:35 -040011830 new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
11831 /* Use the hdr_buf to find the sequence that this frame belongs to */
11832 list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
11833 temp_hdr = (struct fc_frame_header *)h_buf->virt;
11834 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
11835 (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
11836 (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
11837 continue;
11838 /* found a pending sequence that matches this frame */
11839 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
11840 break;
11841 }
11842 if (!seq_dmabuf) {
11843 /*
11844 * This indicates first frame received for this sequence.
11845 * Queue the buffer on the vport's rcv_buffer_list.
11846 */
11847 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
James Smart45ed1192009-10-02 15:17:02 -040011848 lpfc_update_rcv_time_stamp(vport);
James Smart4f774512009-05-22 14:52:35 -040011849 return dmabuf;
11850 }
11851 temp_hdr = seq_dmabuf->hbuf.virt;
James Smarteeead812009-12-21 17:01:23 -050011852 if (be16_to_cpu(new_hdr->fh_seq_cnt) <
11853 be16_to_cpu(temp_hdr->fh_seq_cnt)) {
James Smart4d9ab992009-10-02 15:16:39 -040011854 list_del_init(&seq_dmabuf->hbuf.list);
11855 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
11856 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
James Smart45ed1192009-10-02 15:17:02 -040011857 lpfc_update_rcv_time_stamp(vport);
James Smart4f774512009-05-22 14:52:35 -040011858 return dmabuf;
11859 }
James Smart45ed1192009-10-02 15:17:02 -040011860 /* move this sequence to the tail to indicate a young sequence */
11861 list_move_tail(&seq_dmabuf->hbuf.list, &vport->rcv_buffer_list);
11862 seq_dmabuf->time_stamp = jiffies;
11863 lpfc_update_rcv_time_stamp(vport);
James Smarteeead812009-12-21 17:01:23 -050011864 if (list_empty(&seq_dmabuf->dbuf.list)) {
11865 temp_hdr = dmabuf->hbuf.virt;
11866 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
11867 return seq_dmabuf;
11868 }
James Smart4f774512009-05-22 14:52:35 -040011869 /* find the correct place in the sequence to insert this frame */
11870 list_for_each_entry_reverse(d_buf, &seq_dmabuf->dbuf.list, list) {
11871 temp_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
11872 temp_hdr = (struct fc_frame_header *)temp_dmabuf->hbuf.virt;
11873 /*
11874 * If the frame's sequence count is greater than the frame on
11875 * the list then insert the frame right after this frame
11876 */
James Smarteeead812009-12-21 17:01:23 -050011877 if (be16_to_cpu(new_hdr->fh_seq_cnt) >
11878 be16_to_cpu(temp_hdr->fh_seq_cnt)) {
James Smart4f774512009-05-22 14:52:35 -040011879 list_add(&dmabuf->dbuf.list, &temp_dmabuf->dbuf.list);
11880 return seq_dmabuf;
11881 }
11882 }
11883 return NULL;
11884}
11885
11886/**
James Smart6669f9b2009-10-02 15:16:45 -040011887 * lpfc_sli4_abort_partial_seq - Abort partially assembled unsol sequence
11888 * @vport: pointer to a vitural port
11889 * @dmabuf: pointer to a dmabuf that describes the FC sequence
11890 *
11891 * This function tries to abort from the partially assembed sequence, described
11892 * by the information from basic abbort @dmabuf. It checks to see whether such
11893 * partially assembled sequence held by the driver. If so, it shall free up all
11894 * the frames from the partially assembled sequence.
11895 *
11896 * Return
11897 * true -- if there is matching partially assembled sequence present and all
11898 * the frames freed with the sequence;
11899 * false -- if there is no matching partially assembled sequence present so
11900 * nothing got aborted in the lower layer driver
11901 **/
11902static bool
11903lpfc_sli4_abort_partial_seq(struct lpfc_vport *vport,
11904 struct hbq_dmabuf *dmabuf)
11905{
11906 struct fc_frame_header *new_hdr;
11907 struct fc_frame_header *temp_hdr;
11908 struct lpfc_dmabuf *d_buf, *n_buf, *h_buf;
11909 struct hbq_dmabuf *seq_dmabuf = NULL;
11910
11911 /* Use the hdr_buf to find the sequence that matches this frame */
11912 INIT_LIST_HEAD(&dmabuf->dbuf.list);
11913 INIT_LIST_HEAD(&dmabuf->hbuf.list);
11914 new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
11915 list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
11916 temp_hdr = (struct fc_frame_header *)h_buf->virt;
11917 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
11918 (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
11919 (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
11920 continue;
11921 /* found a pending sequence that matches this frame */
11922 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
11923 break;
11924 }
11925
11926 /* Free up all the frames from the partially assembled sequence */
11927 if (seq_dmabuf) {
11928 list_for_each_entry_safe(d_buf, n_buf,
11929 &seq_dmabuf->dbuf.list, list) {
11930 list_del_init(&d_buf->list);
11931 lpfc_in_buf_free(vport->phba, d_buf);
11932 }
11933 return true;
11934 }
11935 return false;
11936}
11937
11938/**
11939 * lpfc_sli4_seq_abort_acc_cmpl - Accept seq abort iocb complete handler
11940 * @phba: Pointer to HBA context object.
11941 * @cmd_iocbq: pointer to the command iocbq structure.
11942 * @rsp_iocbq: pointer to the response iocbq structure.
11943 *
11944 * This function handles the sequence abort accept iocb command complete
11945 * event. It properly releases the memory allocated to the sequence abort
11946 * accept iocb.
11947 **/
11948static void
11949lpfc_sli4_seq_abort_acc_cmpl(struct lpfc_hba *phba,
11950 struct lpfc_iocbq *cmd_iocbq,
11951 struct lpfc_iocbq *rsp_iocbq)
11952{
11953 if (cmd_iocbq)
11954 lpfc_sli_release_iocbq(phba, cmd_iocbq);
11955}
11956
11957/**
11958 * lpfc_sli4_seq_abort_acc - Accept sequence abort
11959 * @phba: Pointer to HBA context object.
11960 * @fc_hdr: pointer to a FC frame header.
11961 *
11962 * This function sends a basic accept to a previous unsol sequence abort
11963 * event after aborting the sequence handling.
11964 **/
11965static void
11966lpfc_sli4_seq_abort_acc(struct lpfc_hba *phba,
11967 struct fc_frame_header *fc_hdr)
11968{
11969 struct lpfc_iocbq *ctiocb = NULL;
11970 struct lpfc_nodelist *ndlp;
James Smart5ffc2662009-11-18 15:39:44 -050011971 uint16_t oxid, rxid;
11972 uint32_t sid, fctl;
James Smart6669f9b2009-10-02 15:16:45 -040011973 IOCB_t *icmd;
11974
11975 if (!lpfc_is_link_up(phba))
11976 return;
11977
11978 sid = sli4_sid_from_fc_hdr(fc_hdr);
11979 oxid = be16_to_cpu(fc_hdr->fh_ox_id);
James Smart5ffc2662009-11-18 15:39:44 -050011980 rxid = be16_to_cpu(fc_hdr->fh_rx_id);
James Smart6669f9b2009-10-02 15:16:45 -040011981
11982 ndlp = lpfc_findnode_did(phba->pport, sid);
11983 if (!ndlp) {
11984 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
11985 "1268 Find ndlp returned NULL for oxid:x%x "
11986 "SID:x%x\n", oxid, sid);
11987 return;
11988 }
James Smart19ca7602010-11-20 23:11:55 -050011989 if (rxid >= phba->sli4_hba.max_cfg_param.xri_base
11990 && rxid <= (phba->sli4_hba.max_cfg_param.max_xri
11991 + phba->sli4_hba.max_cfg_param.xri_base))
11992 lpfc_set_rrq_active(phba, ndlp, rxid, oxid, 0);
James Smart6669f9b2009-10-02 15:16:45 -040011993
11994 /* Allocate buffer for acc iocb */
11995 ctiocb = lpfc_sli_get_iocbq(phba);
11996 if (!ctiocb)
11997 return;
11998
James Smart5ffc2662009-11-18 15:39:44 -050011999 /* Extract the F_CTL field from FC_HDR */
12000 fctl = sli4_fctl_from_fc_hdr(fc_hdr);
12001
James Smart6669f9b2009-10-02 15:16:45 -040012002 icmd = &ctiocb->iocb;
James Smart6669f9b2009-10-02 15:16:45 -040012003 icmd->un.xseq64.bdl.bdeSize = 0;
James Smart5ffc2662009-11-18 15:39:44 -050012004 icmd->un.xseq64.bdl.ulpIoTag32 = 0;
James Smart6669f9b2009-10-02 15:16:45 -040012005 icmd->un.xseq64.w5.hcsw.Dfctl = 0;
12006 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_ACC;
12007 icmd->un.xseq64.w5.hcsw.Type = FC_TYPE_BLS;
12008
12009 /* Fill in the rest of iocb fields */
12010 icmd->ulpCommand = CMD_XMIT_BLS_RSP64_CX;
12011 icmd->ulpBdeCount = 0;
12012 icmd->ulpLe = 1;
12013 icmd->ulpClass = CLASS3;
12014 icmd->ulpContext = ndlp->nlp_rpi;
James Smartbe858b62010-12-15 17:57:20 -050012015 ctiocb->context1 = ndlp;
James Smart6669f9b2009-10-02 15:16:45 -040012016
James Smart6669f9b2009-10-02 15:16:45 -040012017 ctiocb->iocb_cmpl = NULL;
12018 ctiocb->vport = phba->pport;
12019 ctiocb->iocb_cmpl = lpfc_sli4_seq_abort_acc_cmpl;
12020
James Smart5ffc2662009-11-18 15:39:44 -050012021 if (fctl & FC_FC_EX_CTX) {
12022 /* ABTS sent by responder to CT exchange, construction
12023 * of BA_ACC will use OX_ID from ABTS for the XRI_TAG
12024 * field and RX_ID from ABTS for RX_ID field.
12025 */
12026 bf_set(lpfc_abts_orig, &icmd->un.bls_acc, LPFC_ABTS_UNSOL_RSP);
12027 bf_set(lpfc_abts_rxid, &icmd->un.bls_acc, rxid);
12028 ctiocb->sli4_xritag = oxid;
12029 } else {
12030 /* ABTS sent by initiator to CT exchange, construction
12031 * of BA_ACC will need to allocate a new XRI as for the
12032 * XRI_TAG and RX_ID fields.
12033 */
12034 bf_set(lpfc_abts_orig, &icmd->un.bls_acc, LPFC_ABTS_UNSOL_INT);
12035 bf_set(lpfc_abts_rxid, &icmd->un.bls_acc, NO_XRI);
12036 ctiocb->sli4_xritag = NO_XRI;
12037 }
12038 bf_set(lpfc_abts_oxid, &icmd->un.bls_acc, oxid);
12039
James Smart6669f9b2009-10-02 15:16:45 -040012040 /* Xmit CT abts accept on exchange <xid> */
12041 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
12042 "1200 Xmit CT ABTS ACC on exchange x%x Data: x%x\n",
12043 CMD_XMIT_BLS_RSP64_CX, phba->link_state);
12044 lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, ctiocb, 0);
12045}
12046
12047/**
12048 * lpfc_sli4_handle_unsol_abort - Handle sli-4 unsolicited abort event
12049 * @vport: Pointer to the vport on which this sequence was received
12050 * @dmabuf: pointer to a dmabuf that describes the FC sequence
12051 *
12052 * This function handles an SLI-4 unsolicited abort event. If the unsolicited
12053 * receive sequence is only partially assembed by the driver, it shall abort
12054 * the partially assembled frames for the sequence. Otherwise, if the
12055 * unsolicited receive sequence has been completely assembled and passed to
12056 * the Upper Layer Protocol (UPL), it then mark the per oxid status for the
12057 * unsolicited sequence has been aborted. After that, it will issue a basic
12058 * accept to accept the abort.
12059 **/
12060void
12061lpfc_sli4_handle_unsol_abort(struct lpfc_vport *vport,
12062 struct hbq_dmabuf *dmabuf)
12063{
12064 struct lpfc_hba *phba = vport->phba;
12065 struct fc_frame_header fc_hdr;
James Smart5ffc2662009-11-18 15:39:44 -050012066 uint32_t fctl;
James Smart6669f9b2009-10-02 15:16:45 -040012067 bool abts_par;
12068
James Smart6669f9b2009-10-02 15:16:45 -040012069 /* Make a copy of fc_hdr before the dmabuf being released */
12070 memcpy(&fc_hdr, dmabuf->hbuf.virt, sizeof(struct fc_frame_header));
James Smart5ffc2662009-11-18 15:39:44 -050012071 fctl = sli4_fctl_from_fc_hdr(&fc_hdr);
James Smart6669f9b2009-10-02 15:16:45 -040012072
James Smart5ffc2662009-11-18 15:39:44 -050012073 if (fctl & FC_FC_EX_CTX) {
12074 /*
12075 * ABTS sent by responder to exchange, just free the buffer
12076 */
James Smart6669f9b2009-10-02 15:16:45 -040012077 lpfc_in_buf_free(phba, &dmabuf->dbuf);
James Smart5ffc2662009-11-18 15:39:44 -050012078 } else {
12079 /*
12080 * ABTS sent by initiator to exchange, need to do cleanup
12081 */
12082 /* Try to abort partially assembled seq */
12083 abts_par = lpfc_sli4_abort_partial_seq(vport, dmabuf);
12084
12085 /* Send abort to ULP if partially seq abort failed */
12086 if (abts_par == false)
12087 lpfc_sli4_send_seq_to_ulp(vport, dmabuf);
12088 else
12089 lpfc_in_buf_free(phba, &dmabuf->dbuf);
12090 }
James Smart6669f9b2009-10-02 15:16:45 -040012091 /* Send basic accept (BA_ACC) to the abort requester */
12092 lpfc_sli4_seq_abort_acc(phba, &fc_hdr);
12093}
12094
12095/**
James Smart4f774512009-05-22 14:52:35 -040012096 * lpfc_seq_complete - Indicates if a sequence is complete
12097 * @dmabuf: pointer to a dmabuf that describes the FC sequence
12098 *
12099 * This function checks the sequence, starting with the frame described by
12100 * @dmabuf, to see if all the frames associated with this sequence are present.
12101 * the frames associated with this sequence are linked to the @dmabuf using the
12102 * dbuf list. This function looks for two major things. 1) That the first frame
12103 * has a sequence count of zero. 2) There is a frame with last frame of sequence
12104 * set. 3) That there are no holes in the sequence count. The function will
12105 * return 1 when the sequence is complete, otherwise it will return 0.
12106 **/
12107static int
12108lpfc_seq_complete(struct hbq_dmabuf *dmabuf)
12109{
12110 struct fc_frame_header *hdr;
12111 struct lpfc_dmabuf *d_buf;
12112 struct hbq_dmabuf *seq_dmabuf;
12113 uint32_t fctl;
12114 int seq_count = 0;
12115
12116 hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
12117 /* make sure first fame of sequence has a sequence count of zero */
12118 if (hdr->fh_seq_cnt != seq_count)
12119 return 0;
12120 fctl = (hdr->fh_f_ctl[0] << 16 |
12121 hdr->fh_f_ctl[1] << 8 |
12122 hdr->fh_f_ctl[2]);
12123 /* If last frame of sequence we can return success. */
12124 if (fctl & FC_FC_END_SEQ)
12125 return 1;
12126 list_for_each_entry(d_buf, &dmabuf->dbuf.list, list) {
12127 seq_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
12128 hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
12129 /* If there is a hole in the sequence count then fail. */
James Smarteeead812009-12-21 17:01:23 -050012130 if (++seq_count != be16_to_cpu(hdr->fh_seq_cnt))
James Smart4f774512009-05-22 14:52:35 -040012131 return 0;
12132 fctl = (hdr->fh_f_ctl[0] << 16 |
12133 hdr->fh_f_ctl[1] << 8 |
12134 hdr->fh_f_ctl[2]);
12135 /* If last frame of sequence we can return success. */
12136 if (fctl & FC_FC_END_SEQ)
12137 return 1;
12138 }
12139 return 0;
12140}
12141
12142/**
12143 * lpfc_prep_seq - Prep sequence for ULP processing
12144 * @vport: Pointer to the vport on which this sequence was received
12145 * @dmabuf: pointer to a dmabuf that describes the FC sequence
12146 *
12147 * This function takes a sequence, described by a list of frames, and creates
12148 * a list of iocbq structures to describe the sequence. This iocbq list will be
12149 * used to issue to the generic unsolicited sequence handler. This routine
12150 * returns a pointer to the first iocbq in the list. If the function is unable
12151 * to allocate an iocbq then it throw out the received frames that were not
12152 * able to be described and return a pointer to the first iocbq. If unable to
12153 * allocate any iocbqs (including the first) this function will return NULL.
12154 **/
12155static struct lpfc_iocbq *
12156lpfc_prep_seq(struct lpfc_vport *vport, struct hbq_dmabuf *seq_dmabuf)
12157{
12158 struct lpfc_dmabuf *d_buf, *n_buf;
12159 struct lpfc_iocbq *first_iocbq, *iocbq;
12160 struct fc_frame_header *fc_hdr;
12161 uint32_t sid;
James Smarteeead812009-12-21 17:01:23 -050012162 struct ulp_bde64 *pbde;
James Smart4f774512009-05-22 14:52:35 -040012163
12164 fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
12165 /* remove from receive buffer list */
12166 list_del_init(&seq_dmabuf->hbuf.list);
James Smart45ed1192009-10-02 15:17:02 -040012167 lpfc_update_rcv_time_stamp(vport);
James Smart4f774512009-05-22 14:52:35 -040012168 /* get the Remote Port's SID */
James Smart6669f9b2009-10-02 15:16:45 -040012169 sid = sli4_sid_from_fc_hdr(fc_hdr);
James Smart4f774512009-05-22 14:52:35 -040012170 /* Get an iocbq struct to fill in. */
12171 first_iocbq = lpfc_sli_get_iocbq(vport->phba);
12172 if (first_iocbq) {
12173 /* Initialize the first IOCB. */
James Smart8fa38512009-07-19 10:01:03 -040012174 first_iocbq->iocb.unsli3.rcvsli3.acc_len = 0;
James Smart4f774512009-05-22 14:52:35 -040012175 first_iocbq->iocb.ulpStatus = IOSTAT_SUCCESS;
12176 first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_SEQ64_CX;
12177 first_iocbq->iocb.ulpContext = be16_to_cpu(fc_hdr->fh_ox_id);
12178 first_iocbq->iocb.unsli3.rcvsli3.vpi =
12179 vport->vpi + vport->phba->vpi_base;
12180 /* put the first buffer into the first IOCBq */
12181 first_iocbq->context2 = &seq_dmabuf->dbuf;
12182 first_iocbq->context3 = NULL;
12183 first_iocbq->iocb.ulpBdeCount = 1;
12184 first_iocbq->iocb.un.cont64[0].tus.f.bdeSize =
12185 LPFC_DATA_BUF_SIZE;
12186 first_iocbq->iocb.un.rcvels.remoteID = sid;
James Smart8fa38512009-07-19 10:01:03 -040012187 first_iocbq->iocb.unsli3.rcvsli3.acc_len +=
James Smart4d9ab992009-10-02 15:16:39 -040012188 bf_get(lpfc_rcqe_length,
12189 &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
James Smart4f774512009-05-22 14:52:35 -040012190 }
12191 iocbq = first_iocbq;
12192 /*
12193 * Each IOCBq can have two Buffers assigned, so go through the list
12194 * of buffers for this sequence and save two buffers in each IOCBq
12195 */
12196 list_for_each_entry_safe(d_buf, n_buf, &seq_dmabuf->dbuf.list, list) {
12197 if (!iocbq) {
12198 lpfc_in_buf_free(vport->phba, d_buf);
12199 continue;
12200 }
12201 if (!iocbq->context3) {
12202 iocbq->context3 = d_buf;
12203 iocbq->iocb.ulpBdeCount++;
James Smarteeead812009-12-21 17:01:23 -050012204 pbde = (struct ulp_bde64 *)
12205 &iocbq->iocb.unsli3.sli3Words[4];
12206 pbde->tus.f.bdeSize = LPFC_DATA_BUF_SIZE;
James Smart8fa38512009-07-19 10:01:03 -040012207 first_iocbq->iocb.unsli3.rcvsli3.acc_len +=
James Smart4d9ab992009-10-02 15:16:39 -040012208 bf_get(lpfc_rcqe_length,
12209 &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
James Smart4f774512009-05-22 14:52:35 -040012210 } else {
12211 iocbq = lpfc_sli_get_iocbq(vport->phba);
12212 if (!iocbq) {
12213 if (first_iocbq) {
12214 first_iocbq->iocb.ulpStatus =
12215 IOSTAT_FCP_RSP_ERROR;
12216 first_iocbq->iocb.un.ulpWord[4] =
12217 IOERR_NO_RESOURCES;
12218 }
12219 lpfc_in_buf_free(vport->phba, d_buf);
12220 continue;
12221 }
12222 iocbq->context2 = d_buf;
12223 iocbq->context3 = NULL;
12224 iocbq->iocb.ulpBdeCount = 1;
12225 iocbq->iocb.un.cont64[0].tus.f.bdeSize =
12226 LPFC_DATA_BUF_SIZE;
James Smart8fa38512009-07-19 10:01:03 -040012227 first_iocbq->iocb.unsli3.rcvsli3.acc_len +=
James Smart4d9ab992009-10-02 15:16:39 -040012228 bf_get(lpfc_rcqe_length,
12229 &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
James Smart4f774512009-05-22 14:52:35 -040012230 iocbq->iocb.un.rcvels.remoteID = sid;
12231 list_add_tail(&iocbq->list, &first_iocbq->list);
12232 }
12233 }
12234 return first_iocbq;
12235}
12236
James Smart6669f9b2009-10-02 15:16:45 -040012237static void
12238lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *vport,
12239 struct hbq_dmabuf *seq_dmabuf)
12240{
12241 struct fc_frame_header *fc_hdr;
12242 struct lpfc_iocbq *iocbq, *curr_iocb, *next_iocb;
12243 struct lpfc_hba *phba = vport->phba;
12244
12245 fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
12246 iocbq = lpfc_prep_seq(vport, seq_dmabuf);
12247 if (!iocbq) {
12248 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12249 "2707 Ring %d handler: Failed to allocate "
12250 "iocb Rctl x%x Type x%x received\n",
12251 LPFC_ELS_RING,
12252 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
12253 return;
12254 }
12255 if (!lpfc_complete_unsol_iocb(phba,
12256 &phba->sli.ring[LPFC_ELS_RING],
12257 iocbq, fc_hdr->fh_r_ctl,
12258 fc_hdr->fh_type))
12259 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
12260 "2540 Ring %d handler: unexpected Rctl "
12261 "x%x Type x%x received\n",
12262 LPFC_ELS_RING,
12263 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
12264
12265 /* Free iocb created in lpfc_prep_seq */
12266 list_for_each_entry_safe(curr_iocb, next_iocb,
12267 &iocbq->list, list) {
12268 list_del_init(&curr_iocb->list);
12269 lpfc_sli_release_iocbq(phba, curr_iocb);
12270 }
12271 lpfc_sli_release_iocbq(phba, iocbq);
12272}
12273
James Smart4f774512009-05-22 14:52:35 -040012274/**
12275 * lpfc_sli4_handle_received_buffer - Handle received buffers from firmware
12276 * @phba: Pointer to HBA context object.
12277 *
12278 * This function is called with no lock held. This function processes all
12279 * the received buffers and gives it to upper layers when a received buffer
12280 * indicates that it is the final frame in the sequence. The interrupt
12281 * service routine processes received buffers at interrupt contexts and adds
12282 * received dma buffers to the rb_pend_list queue and signals the worker thread.
12283 * Worker thread calls lpfc_sli4_handle_received_buffer, which will call the
12284 * appropriate receive function when the final frame in a sequence is received.
12285 **/
James Smart4d9ab992009-10-02 15:16:39 -040012286void
12287lpfc_sli4_handle_received_buffer(struct lpfc_hba *phba,
12288 struct hbq_dmabuf *dmabuf)
James Smart4f774512009-05-22 14:52:35 -040012289{
James Smart4d9ab992009-10-02 15:16:39 -040012290 struct hbq_dmabuf *seq_dmabuf;
James Smart4f774512009-05-22 14:52:35 -040012291 struct fc_frame_header *fc_hdr;
12292 struct lpfc_vport *vport;
12293 uint32_t fcfi;
James Smart4f774512009-05-22 14:52:35 -040012294
James Smart4f774512009-05-22 14:52:35 -040012295 /* Process each received buffer */
James Smart4d9ab992009-10-02 15:16:39 -040012296 fc_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
12297 /* check to see if this a valid type of frame */
12298 if (lpfc_fc_frame_check(phba, fc_hdr)) {
12299 lpfc_in_buf_free(phba, &dmabuf->dbuf);
12300 return;
12301 }
12302 fcfi = bf_get(lpfc_rcqe_fcf_id, &dmabuf->cq_event.cqe.rcqe_cmpl);
12303 vport = lpfc_fc_frame_to_vport(phba, fc_hdr, fcfi);
James Smartc8685952009-11-18 15:39:16 -050012304 if (!vport || !(vport->vpi_state & LPFC_VPI_REGISTERED)) {
James Smart4d9ab992009-10-02 15:16:39 -040012305 /* throw out the frame */
12306 lpfc_in_buf_free(phba, &dmabuf->dbuf);
12307 return;
12308 }
James Smart6669f9b2009-10-02 15:16:45 -040012309 /* Handle the basic abort sequence (BA_ABTS) event */
12310 if (fc_hdr->fh_r_ctl == FC_RCTL_BA_ABTS) {
12311 lpfc_sli4_handle_unsol_abort(vport, dmabuf);
12312 return;
12313 }
12314
James Smart4d9ab992009-10-02 15:16:39 -040012315 /* Link this frame */
12316 seq_dmabuf = lpfc_fc_frame_add(vport, dmabuf);
12317 if (!seq_dmabuf) {
12318 /* unable to add frame to vport - throw it out */
12319 lpfc_in_buf_free(phba, &dmabuf->dbuf);
12320 return;
12321 }
12322 /* If not last frame in sequence continue processing frames. */
James Smartdef9c7a2009-12-21 17:02:28 -050012323 if (!lpfc_seq_complete(seq_dmabuf))
James Smart4d9ab992009-10-02 15:16:39 -040012324 return;
James Smartdef9c7a2009-12-21 17:02:28 -050012325
James Smart6669f9b2009-10-02 15:16:45 -040012326 /* Send the complete sequence to the upper layer protocol */
12327 lpfc_sli4_send_seq_to_ulp(vport, seq_dmabuf);
James Smart4f774512009-05-22 14:52:35 -040012328}
James Smart6fb120a2009-05-22 14:52:59 -040012329
12330/**
12331 * lpfc_sli4_post_all_rpi_hdrs - Post the rpi header memory region to the port
12332 * @phba: pointer to lpfc hba data structure.
12333 *
12334 * This routine is invoked to post rpi header templates to the
12335 * HBA consistent with the SLI-4 interface spec. This routine
James Smart49198b32010-04-06 15:04:33 -040012336 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
12337 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
James Smart6fb120a2009-05-22 14:52:59 -040012338 *
12339 * This routine does not require any locks. It's usage is expected
12340 * to be driver load or reset recovery when the driver is
12341 * sequential.
12342 *
12343 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020012344 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -040012345 * -EIO - The mailbox failed to complete successfully.
James Smart6fb120a2009-05-22 14:52:59 -040012346 * When this error occurs, the driver is not guaranteed
12347 * to have any rpi regions posted to the device and
12348 * must either attempt to repost the regions or take a
12349 * fatal error.
12350 **/
12351int
12352lpfc_sli4_post_all_rpi_hdrs(struct lpfc_hba *phba)
12353{
12354 struct lpfc_rpi_hdr *rpi_page;
12355 uint32_t rc = 0;
12356
12357 /* Post all rpi memory regions to the port. */
12358 list_for_each_entry(rpi_page, &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
12359 rc = lpfc_sli4_post_rpi_hdr(phba, rpi_page);
12360 if (rc != MBX_SUCCESS) {
12361 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12362 "2008 Error %d posting all rpi "
12363 "headers\n", rc);
12364 rc = -EIO;
12365 break;
12366 }
12367 }
12368
12369 return rc;
12370}
12371
12372/**
12373 * lpfc_sli4_post_rpi_hdr - Post an rpi header memory region to the port
12374 * @phba: pointer to lpfc hba data structure.
12375 * @rpi_page: pointer to the rpi memory region.
12376 *
12377 * This routine is invoked to post a single rpi header to the
12378 * HBA consistent with the SLI-4 interface spec. This memory region
12379 * maps up to 64 rpi context regions.
12380 *
12381 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020012382 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -040012383 * -ENOMEM - No available memory
12384 * -EIO - The mailbox failed to complete successfully.
James Smart6fb120a2009-05-22 14:52:59 -040012385 **/
12386int
12387lpfc_sli4_post_rpi_hdr(struct lpfc_hba *phba, struct lpfc_rpi_hdr *rpi_page)
12388{
12389 LPFC_MBOXQ_t *mboxq;
12390 struct lpfc_mbx_post_hdr_tmpl *hdr_tmpl;
12391 uint32_t rc = 0;
12392 uint32_t mbox_tmo;
12393 uint32_t shdr_status, shdr_add_status;
12394 union lpfc_sli4_cfg_shdr *shdr;
12395
12396 /* The port is notified of the header region via a mailbox command. */
12397 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12398 if (!mboxq) {
12399 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12400 "2001 Unable to allocate memory for issuing "
12401 "SLI_CONFIG_SPECIAL mailbox command\n");
12402 return -ENOMEM;
12403 }
12404
12405 /* Post all rpi memory regions to the port. */
12406 hdr_tmpl = &mboxq->u.mqe.un.hdr_tmpl;
12407 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_SLI4_CONFIG);
12408 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
12409 LPFC_MBOX_OPCODE_FCOE_POST_HDR_TEMPLATE,
12410 sizeof(struct lpfc_mbx_post_hdr_tmpl) -
James Smartfedd3b72011-02-16 12:39:24 -050012411 sizeof(struct lpfc_sli4_cfg_mhdr),
12412 LPFC_SLI4_MBX_EMBED);
James Smart6fb120a2009-05-22 14:52:59 -040012413 bf_set(lpfc_mbx_post_hdr_tmpl_page_cnt,
12414 hdr_tmpl, rpi_page->page_count);
12415 bf_set(lpfc_mbx_post_hdr_tmpl_rpi_offset, hdr_tmpl,
12416 rpi_page->start_rpi);
12417 hdr_tmpl->rpi_paddr_lo = putPaddrLow(rpi_page->dmabuf->phys);
12418 hdr_tmpl->rpi_paddr_hi = putPaddrHigh(rpi_page->dmabuf->phys);
James Smartf1126682009-06-10 17:22:44 -040012419 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
James Smart6fb120a2009-05-22 14:52:59 -040012420 shdr = (union lpfc_sli4_cfg_shdr *) &hdr_tmpl->header.cfg_shdr;
12421 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12422 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12423 if (rc != MBX_TIMEOUT)
12424 mempool_free(mboxq, phba->mbox_mem_pool);
12425 if (shdr_status || shdr_add_status || rc) {
12426 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12427 "2514 POST_RPI_HDR mailbox failed with "
12428 "status x%x add_status x%x, mbx status x%x\n",
12429 shdr_status, shdr_add_status, rc);
12430 rc = -ENXIO;
12431 }
12432 return rc;
12433}
12434
12435/**
12436 * lpfc_sli4_alloc_rpi - Get an available rpi in the device's range
12437 * @phba: pointer to lpfc hba data structure.
12438 *
12439 * This routine is invoked to post rpi header templates to the
12440 * HBA consistent with the SLI-4 interface spec. This routine
James Smart49198b32010-04-06 15:04:33 -040012441 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
12442 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
James Smart6fb120a2009-05-22 14:52:59 -040012443 *
12444 * Returns
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020012445 * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
James Smart6fb120a2009-05-22 14:52:59 -040012446 * LPFC_RPI_ALLOC_ERROR if no rpis are available.
12447 **/
12448int
12449lpfc_sli4_alloc_rpi(struct lpfc_hba *phba)
12450{
12451 int rpi;
12452 uint16_t max_rpi, rpi_base, rpi_limit;
12453 uint16_t rpi_remaining;
12454 struct lpfc_rpi_hdr *rpi_hdr;
12455
12456 max_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
12457 rpi_base = phba->sli4_hba.max_cfg_param.rpi_base;
12458 rpi_limit = phba->sli4_hba.next_rpi;
12459
12460 /*
12461 * The valid rpi range is not guaranteed to be zero-based. Start
12462 * the search at the rpi_base as reported by the port.
12463 */
12464 spin_lock_irq(&phba->hbalock);
12465 rpi = find_next_zero_bit(phba->sli4_hba.rpi_bmask, rpi_limit, rpi_base);
12466 if (rpi >= rpi_limit || rpi < rpi_base)
12467 rpi = LPFC_RPI_ALLOC_ERROR;
12468 else {
12469 set_bit(rpi, phba->sli4_hba.rpi_bmask);
12470 phba->sli4_hba.max_cfg_param.rpi_used++;
12471 phba->sli4_hba.rpi_count++;
12472 }
12473
12474 /*
12475 * Don't try to allocate more rpi header regions if the device limit
12476 * on available rpis max has been exhausted.
12477 */
12478 if ((rpi == LPFC_RPI_ALLOC_ERROR) &&
12479 (phba->sli4_hba.rpi_count >= max_rpi)) {
12480 spin_unlock_irq(&phba->hbalock);
12481 return rpi;
12482 }
12483
12484 /*
12485 * If the driver is running low on rpi resources, allocate another
12486 * page now. Note that the next_rpi value is used because
12487 * it represents how many are actually in use whereas max_rpi notes
12488 * how many are supported max by the device.
12489 */
12490 rpi_remaining = phba->sli4_hba.next_rpi - rpi_base -
12491 phba->sli4_hba.rpi_count;
12492 spin_unlock_irq(&phba->hbalock);
12493 if (rpi_remaining < LPFC_RPI_LOW_WATER_MARK) {
12494 rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
12495 if (!rpi_hdr) {
12496 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12497 "2002 Error Could not grow rpi "
12498 "count\n");
12499 } else {
12500 lpfc_sli4_post_rpi_hdr(phba, rpi_hdr);
12501 }
12502 }
12503
12504 return rpi;
12505}
12506
12507/**
12508 * lpfc_sli4_free_rpi - Release an rpi for reuse.
12509 * @phba: pointer to lpfc hba data structure.
12510 *
12511 * This routine is invoked to release an rpi to the pool of
12512 * available rpis maintained by the driver.
12513 **/
12514void
James Smartd7c47992010-06-08 18:31:54 -040012515__lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
12516{
12517 if (test_and_clear_bit(rpi, phba->sli4_hba.rpi_bmask)) {
12518 phba->sli4_hba.rpi_count--;
12519 phba->sli4_hba.max_cfg_param.rpi_used--;
12520 }
12521}
12522
12523/**
12524 * lpfc_sli4_free_rpi - Release an rpi for reuse.
12525 * @phba: pointer to lpfc hba data structure.
12526 *
12527 * This routine is invoked to release an rpi to the pool of
12528 * available rpis maintained by the driver.
12529 **/
12530void
James Smart6fb120a2009-05-22 14:52:59 -040012531lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
12532{
12533 spin_lock_irq(&phba->hbalock);
James Smartd7c47992010-06-08 18:31:54 -040012534 __lpfc_sli4_free_rpi(phba, rpi);
James Smart6fb120a2009-05-22 14:52:59 -040012535 spin_unlock_irq(&phba->hbalock);
12536}
12537
12538/**
12539 * lpfc_sli4_remove_rpis - Remove the rpi bitmask region
12540 * @phba: pointer to lpfc hba data structure.
12541 *
12542 * This routine is invoked to remove the memory region that
12543 * provided rpi via a bitmask.
12544 **/
12545void
12546lpfc_sli4_remove_rpis(struct lpfc_hba *phba)
12547{
12548 kfree(phba->sli4_hba.rpi_bmask);
12549}
12550
12551/**
12552 * lpfc_sli4_resume_rpi - Remove the rpi bitmask region
12553 * @phba: pointer to lpfc hba data structure.
12554 *
12555 * This routine is invoked to remove the memory region that
12556 * provided rpi via a bitmask.
12557 **/
12558int
12559lpfc_sli4_resume_rpi(struct lpfc_nodelist *ndlp)
12560{
12561 LPFC_MBOXQ_t *mboxq;
12562 struct lpfc_hba *phba = ndlp->phba;
12563 int rc;
12564
12565 /* The port is notified of the header region via a mailbox command. */
12566 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12567 if (!mboxq)
12568 return -ENOMEM;
12569
12570 /* Post all rpi memory regions to the port. */
12571 lpfc_resume_rpi(mboxq, ndlp);
12572 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
12573 if (rc == MBX_NOT_FINISHED) {
12574 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12575 "2010 Resume RPI Mailbox failed "
12576 "status %d, mbxStatus x%x\n", rc,
12577 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
12578 mempool_free(mboxq, phba->mbox_mem_pool);
12579 return -EIO;
12580 }
12581 return 0;
12582}
12583
12584/**
12585 * lpfc_sli4_init_vpi - Initialize a vpi with the port
James Smart76a95d72010-11-20 23:11:48 -050012586 * @vport: Pointer to the vport for which the vpi is being initialized
James Smart6fb120a2009-05-22 14:52:59 -040012587 *
James Smart76a95d72010-11-20 23:11:48 -050012588 * This routine is invoked to activate a vpi with the port.
James Smart6fb120a2009-05-22 14:52:59 -040012589 *
12590 * Returns:
12591 * 0 success
12592 * -Evalue otherwise
12593 **/
12594int
James Smart76a95d72010-11-20 23:11:48 -050012595lpfc_sli4_init_vpi(struct lpfc_vport *vport)
James Smart6fb120a2009-05-22 14:52:59 -040012596{
12597 LPFC_MBOXQ_t *mboxq;
12598 int rc = 0;
James Smart6a9c52c2009-10-02 15:16:51 -040012599 int retval = MBX_SUCCESS;
James Smart6fb120a2009-05-22 14:52:59 -040012600 uint32_t mbox_tmo;
James Smart76a95d72010-11-20 23:11:48 -050012601 struct lpfc_hba *phba = vport->phba;
James Smart6fb120a2009-05-22 14:52:59 -040012602 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12603 if (!mboxq)
12604 return -ENOMEM;
James Smart76a95d72010-11-20 23:11:48 -050012605 lpfc_init_vpi(phba, mboxq, vport->vpi);
James Smart6fb120a2009-05-22 14:52:59 -040012606 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_INIT_VPI);
12607 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
James Smart6fb120a2009-05-22 14:52:59 -040012608 if (rc != MBX_SUCCESS) {
James Smart76a95d72010-11-20 23:11:48 -050012609 lpfc_printf_vlog(vport, KERN_ERR, LOG_SLI,
James Smart6fb120a2009-05-22 14:52:59 -040012610 "2022 INIT VPI Mailbox failed "
12611 "status %d, mbxStatus x%x\n", rc,
12612 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
James Smart6a9c52c2009-10-02 15:16:51 -040012613 retval = -EIO;
James Smart6fb120a2009-05-22 14:52:59 -040012614 }
James Smart6a9c52c2009-10-02 15:16:51 -040012615 if (rc != MBX_TIMEOUT)
James Smart76a95d72010-11-20 23:11:48 -050012616 mempool_free(mboxq, vport->phba->mbox_mem_pool);
James Smart6a9c52c2009-10-02 15:16:51 -040012617
12618 return retval;
James Smart6fb120a2009-05-22 14:52:59 -040012619}
12620
12621/**
12622 * lpfc_mbx_cmpl_add_fcf_record - add fcf mbox completion handler.
12623 * @phba: pointer to lpfc hba data structure.
12624 * @mboxq: Pointer to mailbox object.
12625 *
12626 * This routine is invoked to manually add a single FCF record. The caller
12627 * must pass a completely initialized FCF_Record. This routine takes
12628 * care of the nonembedded mailbox operations.
12629 **/
12630static void
12631lpfc_mbx_cmpl_add_fcf_record(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
12632{
12633 void *virt_addr;
12634 union lpfc_sli4_cfg_shdr *shdr;
12635 uint32_t shdr_status, shdr_add_status;
12636
12637 virt_addr = mboxq->sge_array->addr[0];
12638 /* The IOCTL status is embedded in the mailbox subheader. */
12639 shdr = (union lpfc_sli4_cfg_shdr *) virt_addr;
12640 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12641 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12642
12643 if ((shdr_status || shdr_add_status) &&
12644 (shdr_status != STATUS_FCF_IN_USE))
12645 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12646 "2558 ADD_FCF_RECORD mailbox failed with "
12647 "status x%x add_status x%x\n",
12648 shdr_status, shdr_add_status);
12649
12650 lpfc_sli4_mbox_cmd_free(phba, mboxq);
12651}
12652
12653/**
12654 * lpfc_sli4_add_fcf_record - Manually add an FCF Record.
12655 * @phba: pointer to lpfc hba data structure.
12656 * @fcf_record: pointer to the initialized fcf record to add.
12657 *
12658 * This routine is invoked to manually add a single FCF record. The caller
12659 * must pass a completely initialized FCF_Record. This routine takes
12660 * care of the nonembedded mailbox operations.
12661 **/
12662int
12663lpfc_sli4_add_fcf_record(struct lpfc_hba *phba, struct fcf_record *fcf_record)
12664{
12665 int rc = 0;
12666 LPFC_MBOXQ_t *mboxq;
12667 uint8_t *bytep;
12668 void *virt_addr;
12669 dma_addr_t phys_addr;
12670 struct lpfc_mbx_sge sge;
12671 uint32_t alloc_len, req_len;
12672 uint32_t fcfindex;
12673
12674 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12675 if (!mboxq) {
12676 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12677 "2009 Failed to allocate mbox for ADD_FCF cmd\n");
12678 return -ENOMEM;
12679 }
12680
12681 req_len = sizeof(struct fcf_record) + sizeof(union lpfc_sli4_cfg_shdr) +
12682 sizeof(uint32_t);
12683
12684 /* Allocate DMA memory and set up the non-embedded mailbox command */
12685 alloc_len = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
12686 LPFC_MBOX_OPCODE_FCOE_ADD_FCF,
12687 req_len, LPFC_SLI4_MBX_NEMBED);
12688 if (alloc_len < req_len) {
12689 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12690 "2523 Allocated DMA memory size (x%x) is "
12691 "less than the requested DMA memory "
12692 "size (x%x)\n", alloc_len, req_len);
12693 lpfc_sli4_mbox_cmd_free(phba, mboxq);
12694 return -ENOMEM;
12695 }
12696
12697 /*
12698 * Get the first SGE entry from the non-embedded DMA memory. This
12699 * routine only uses a single SGE.
12700 */
12701 lpfc_sli4_mbx_sge_get(mboxq, 0, &sge);
12702 phys_addr = getPaddr(sge.pa_hi, sge.pa_lo);
James Smart6fb120a2009-05-22 14:52:59 -040012703 virt_addr = mboxq->sge_array->addr[0];
12704 /*
12705 * Configure the FCF record for FCFI 0. This is the driver's
12706 * hardcoded default and gets used in nonFIP mode.
12707 */
12708 fcfindex = bf_get(lpfc_fcf_record_fcf_index, fcf_record);
12709 bytep = virt_addr + sizeof(union lpfc_sli4_cfg_shdr);
12710 lpfc_sli_pcimem_bcopy(&fcfindex, bytep, sizeof(uint32_t));
12711
12712 /*
12713 * Copy the fcf_index and the FCF Record Data. The data starts after
12714 * the FCoE header plus word10. The data copy needs to be endian
12715 * correct.
12716 */
12717 bytep += sizeof(uint32_t);
12718 lpfc_sli_pcimem_bcopy(fcf_record, bytep, sizeof(struct fcf_record));
12719 mboxq->vport = phba->pport;
12720 mboxq->mbox_cmpl = lpfc_mbx_cmpl_add_fcf_record;
12721 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
12722 if (rc == MBX_NOT_FINISHED) {
12723 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12724 "2515 ADD_FCF_RECORD mailbox failed with "
12725 "status 0x%x\n", rc);
12726 lpfc_sli4_mbox_cmd_free(phba, mboxq);
12727 rc = -EIO;
12728 } else
12729 rc = 0;
12730
12731 return rc;
12732}
12733
12734/**
12735 * lpfc_sli4_build_dflt_fcf_record - Build the driver's default FCF Record.
12736 * @phba: pointer to lpfc hba data structure.
12737 * @fcf_record: pointer to the fcf record to write the default data.
12738 * @fcf_index: FCF table entry index.
12739 *
12740 * This routine is invoked to build the driver's default FCF record. The
12741 * values used are hardcoded. This routine handles memory initialization.
12742 *
12743 **/
12744void
12745lpfc_sli4_build_dflt_fcf_record(struct lpfc_hba *phba,
12746 struct fcf_record *fcf_record,
12747 uint16_t fcf_index)
12748{
12749 memset(fcf_record, 0, sizeof(struct fcf_record));
12750 fcf_record->max_rcv_size = LPFC_FCOE_MAX_RCV_SIZE;
12751 fcf_record->fka_adv_period = LPFC_FCOE_FKA_ADV_PER;
12752 fcf_record->fip_priority = LPFC_FCOE_FIP_PRIORITY;
12753 bf_set(lpfc_fcf_record_mac_0, fcf_record, phba->fc_map[0]);
12754 bf_set(lpfc_fcf_record_mac_1, fcf_record, phba->fc_map[1]);
12755 bf_set(lpfc_fcf_record_mac_2, fcf_record, phba->fc_map[2]);
12756 bf_set(lpfc_fcf_record_mac_3, fcf_record, LPFC_FCOE_FCF_MAC3);
12757 bf_set(lpfc_fcf_record_mac_4, fcf_record, LPFC_FCOE_FCF_MAC4);
12758 bf_set(lpfc_fcf_record_mac_5, fcf_record, LPFC_FCOE_FCF_MAC5);
12759 bf_set(lpfc_fcf_record_fc_map_0, fcf_record, phba->fc_map[0]);
12760 bf_set(lpfc_fcf_record_fc_map_1, fcf_record, phba->fc_map[1]);
12761 bf_set(lpfc_fcf_record_fc_map_2, fcf_record, phba->fc_map[2]);
12762 bf_set(lpfc_fcf_record_fcf_valid, fcf_record, 1);
James Smart0c287582009-06-10 17:22:56 -040012763 bf_set(lpfc_fcf_record_fcf_avail, fcf_record, 1);
James Smart6fb120a2009-05-22 14:52:59 -040012764 bf_set(lpfc_fcf_record_fcf_index, fcf_record, fcf_index);
12765 bf_set(lpfc_fcf_record_mac_addr_prov, fcf_record,
12766 LPFC_FCF_FPMA | LPFC_FCF_SPMA);
12767 /* Set the VLAN bit map */
12768 if (phba->valid_vlan) {
12769 fcf_record->vlan_bitmap[phba->vlan_id / 8]
12770 = 1 << (phba->vlan_id % 8);
12771 }
12772}
12773
12774/**
James Smart0c9ab6f2010-02-26 14:15:57 -050012775 * lpfc_sli4_fcf_scan_read_fcf_rec - Read hba fcf record for fcf scan.
James Smart6fb120a2009-05-22 14:52:59 -040012776 * @phba: pointer to lpfc hba data structure.
12777 * @fcf_index: FCF table entry offset.
12778 *
James Smart0c9ab6f2010-02-26 14:15:57 -050012779 * This routine is invoked to scan the entire FCF table by reading FCF
12780 * record and processing it one at a time starting from the @fcf_index
12781 * for initial FCF discovery or fast FCF failover rediscovery.
12782 *
12783 * Return 0 if the mailbox command is submitted sucessfully, none 0
12784 * otherwise.
James Smart6fb120a2009-05-22 14:52:59 -040012785 **/
12786int
James Smart0c9ab6f2010-02-26 14:15:57 -050012787lpfc_sli4_fcf_scan_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
James Smart6fb120a2009-05-22 14:52:59 -040012788{
12789 int rc = 0, error;
12790 LPFC_MBOXQ_t *mboxq;
James Smart6fb120a2009-05-22 14:52:59 -040012791
James Smart32b97932009-07-19 10:01:21 -040012792 phba->fcoe_eventtag_at_fcf_scan = phba->fcoe_eventtag;
James Smart6fb120a2009-05-22 14:52:59 -040012793 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12794 if (!mboxq) {
12795 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12796 "2000 Failed to allocate mbox for "
12797 "READ_FCF cmd\n");
James Smart4d9ab992009-10-02 15:16:39 -040012798 error = -ENOMEM;
James Smart0c9ab6f2010-02-26 14:15:57 -050012799 goto fail_fcf_scan;
James Smart6fb120a2009-05-22 14:52:59 -040012800 }
James Smartecfd03c2010-02-12 14:41:27 -050012801 /* Construct the read FCF record mailbox command */
James Smart0c9ab6f2010-02-26 14:15:57 -050012802 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
James Smartecfd03c2010-02-12 14:41:27 -050012803 if (rc) {
12804 error = -EINVAL;
James Smart0c9ab6f2010-02-26 14:15:57 -050012805 goto fail_fcf_scan;
James Smart6fb120a2009-05-22 14:52:59 -040012806 }
James Smartecfd03c2010-02-12 14:41:27 -050012807 /* Issue the mailbox command asynchronously */
James Smart6fb120a2009-05-22 14:52:59 -040012808 mboxq->vport = phba->pport;
James Smart0c9ab6f2010-02-26 14:15:57 -050012809 mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_scan_read_fcf_rec;
James Smarta93ff372010-10-22 11:06:08 -040012810
12811 spin_lock_irq(&phba->hbalock);
12812 phba->hba_flag |= FCF_TS_INPROG;
12813 spin_unlock_irq(&phba->hbalock);
12814
James Smart6fb120a2009-05-22 14:52:59 -040012815 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
James Smartecfd03c2010-02-12 14:41:27 -050012816 if (rc == MBX_NOT_FINISHED)
James Smart6fb120a2009-05-22 14:52:59 -040012817 error = -EIO;
James Smartecfd03c2010-02-12 14:41:27 -050012818 else {
James Smart38b92ef2010-08-04 16:11:39 -040012819 /* Reset eligible FCF count for new scan */
12820 if (fcf_index == LPFC_FCOE_FCF_GET_FIRST)
James Smart999d8132010-03-15 11:24:56 -040012821 phba->fcf.eligible_fcf_cnt = 0;
James Smart6fb120a2009-05-22 14:52:59 -040012822 error = 0;
James Smart32b97932009-07-19 10:01:21 -040012823 }
James Smart0c9ab6f2010-02-26 14:15:57 -050012824fail_fcf_scan:
James Smart4d9ab992009-10-02 15:16:39 -040012825 if (error) {
12826 if (mboxq)
12827 lpfc_sli4_mbox_cmd_free(phba, mboxq);
James Smarta93ff372010-10-22 11:06:08 -040012828 /* FCF scan failed, clear FCF_TS_INPROG flag */
James Smart4d9ab992009-10-02 15:16:39 -040012829 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -040012830 phba->hba_flag &= ~FCF_TS_INPROG;
James Smart4d9ab992009-10-02 15:16:39 -040012831 spin_unlock_irq(&phba->hbalock);
12832 }
James Smart6fb120a2009-05-22 14:52:59 -040012833 return error;
12834}
James Smarta0c87cb2009-07-19 10:01:10 -040012835
12836/**
James Smarta93ff372010-10-22 11:06:08 -040012837 * lpfc_sli4_fcf_rr_read_fcf_rec - Read hba fcf record for roundrobin fcf.
James Smart0c9ab6f2010-02-26 14:15:57 -050012838 * @phba: pointer to lpfc hba data structure.
12839 * @fcf_index: FCF table entry offset.
12840 *
12841 * This routine is invoked to read an FCF record indicated by @fcf_index
James Smarta93ff372010-10-22 11:06:08 -040012842 * and to use it for FLOGI roundrobin FCF failover.
James Smart0c9ab6f2010-02-26 14:15:57 -050012843 *
12844 * Return 0 if the mailbox command is submitted sucessfully, none 0
12845 * otherwise.
12846 **/
12847int
12848lpfc_sli4_fcf_rr_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
12849{
12850 int rc = 0, error;
12851 LPFC_MBOXQ_t *mboxq;
12852
12853 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12854 if (!mboxq) {
12855 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
12856 "2763 Failed to allocate mbox for "
12857 "READ_FCF cmd\n");
12858 error = -ENOMEM;
12859 goto fail_fcf_read;
12860 }
12861 /* Construct the read FCF record mailbox command */
12862 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
12863 if (rc) {
12864 error = -EINVAL;
12865 goto fail_fcf_read;
12866 }
12867 /* Issue the mailbox command asynchronously */
12868 mboxq->vport = phba->pport;
12869 mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_rr_read_fcf_rec;
12870 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
12871 if (rc == MBX_NOT_FINISHED)
12872 error = -EIO;
12873 else
12874 error = 0;
12875
12876fail_fcf_read:
12877 if (error && mboxq)
12878 lpfc_sli4_mbox_cmd_free(phba, mboxq);
12879 return error;
12880}
12881
12882/**
12883 * lpfc_sli4_read_fcf_rec - Read hba fcf record for update eligible fcf bmask.
12884 * @phba: pointer to lpfc hba data structure.
12885 * @fcf_index: FCF table entry offset.
12886 *
12887 * This routine is invoked to read an FCF record indicated by @fcf_index to
James Smarta93ff372010-10-22 11:06:08 -040012888 * determine whether it's eligible for FLOGI roundrobin failover list.
James Smart0c9ab6f2010-02-26 14:15:57 -050012889 *
12890 * Return 0 if the mailbox command is submitted sucessfully, none 0
12891 * otherwise.
12892 **/
12893int
12894lpfc_sli4_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
12895{
12896 int rc = 0, error;
12897 LPFC_MBOXQ_t *mboxq;
12898
12899 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12900 if (!mboxq) {
12901 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
12902 "2758 Failed to allocate mbox for "
12903 "READ_FCF cmd\n");
12904 error = -ENOMEM;
12905 goto fail_fcf_read;
12906 }
12907 /* Construct the read FCF record mailbox command */
12908 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
12909 if (rc) {
12910 error = -EINVAL;
12911 goto fail_fcf_read;
12912 }
12913 /* Issue the mailbox command asynchronously */
12914 mboxq->vport = phba->pport;
12915 mboxq->mbox_cmpl = lpfc_mbx_cmpl_read_fcf_rec;
12916 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
12917 if (rc == MBX_NOT_FINISHED)
12918 error = -EIO;
12919 else
12920 error = 0;
12921
12922fail_fcf_read:
12923 if (error && mboxq)
12924 lpfc_sli4_mbox_cmd_free(phba, mboxq);
12925 return error;
12926}
12927
12928/**
12929 * lpfc_sli4_fcf_rr_next_index_get - Get next eligible fcf record index
12930 * @phba: pointer to lpfc hba data structure.
12931 *
12932 * This routine is to get the next eligible FCF record index in a round
12933 * robin fashion. If the next eligible FCF record index equals to the
James Smarta93ff372010-10-22 11:06:08 -040012934 * initial roundrobin FCF record index, LPFC_FCOE_FCF_NEXT_NONE (0xFFFF)
James Smart0c9ab6f2010-02-26 14:15:57 -050012935 * shall be returned, otherwise, the next eligible FCF record's index
12936 * shall be returned.
12937 **/
12938uint16_t
12939lpfc_sli4_fcf_rr_next_index_get(struct lpfc_hba *phba)
12940{
12941 uint16_t next_fcf_index;
12942
James Smart3804dc82010-07-14 15:31:37 -040012943 /* Search start from next bit of currently registered FCF index */
12944 next_fcf_index = (phba->fcf.current_rec.fcf_indx + 1) %
12945 LPFC_SLI4_FCF_TBL_INDX_MAX;
James Smart0c9ab6f2010-02-26 14:15:57 -050012946 next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
12947 LPFC_SLI4_FCF_TBL_INDX_MAX,
James Smart3804dc82010-07-14 15:31:37 -040012948 next_fcf_index);
12949
James Smart0c9ab6f2010-02-26 14:15:57 -050012950 /* Wrap around condition on phba->fcf.fcf_rr_bmask */
12951 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX)
12952 next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
12953 LPFC_SLI4_FCF_TBL_INDX_MAX, 0);
James Smart0c9ab6f2010-02-26 14:15:57 -050012954
James Smart3804dc82010-07-14 15:31:37 -040012955 /* Check roundrobin failover list empty condition */
12956 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
12957 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
12958 "2844 No roundrobin failover FCF available\n");
12959 return LPFC_FCOE_FCF_NEXT_NONE;
12960 }
12961
James Smart3804dc82010-07-14 15:31:37 -040012962 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040012963 "2845 Get next roundrobin failover FCF (x%x)\n",
12964 next_fcf_index);
12965
James Smart0c9ab6f2010-02-26 14:15:57 -050012966 return next_fcf_index;
12967}
12968
12969/**
12970 * lpfc_sli4_fcf_rr_index_set - Set bmask with eligible fcf record index
12971 * @phba: pointer to lpfc hba data structure.
12972 *
12973 * This routine sets the FCF record index in to the eligible bmask for
James Smarta93ff372010-10-22 11:06:08 -040012974 * roundrobin failover search. It checks to make sure that the index
James Smart0c9ab6f2010-02-26 14:15:57 -050012975 * does not go beyond the range of the driver allocated bmask dimension
12976 * before setting the bit.
12977 *
12978 * Returns 0 if the index bit successfully set, otherwise, it returns
12979 * -EINVAL.
12980 **/
12981int
12982lpfc_sli4_fcf_rr_index_set(struct lpfc_hba *phba, uint16_t fcf_index)
12983{
12984 if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
12985 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040012986 "2610 FCF (x%x) reached driver's book "
12987 "keeping dimension:x%x\n",
James Smart0c9ab6f2010-02-26 14:15:57 -050012988 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
12989 return -EINVAL;
12990 }
12991 /* Set the eligible FCF record index bmask */
12992 set_bit(fcf_index, phba->fcf.fcf_rr_bmask);
12993
James Smart3804dc82010-07-14 15:31:37 -040012994 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040012995 "2790 Set FCF (x%x) to roundrobin FCF failover "
James Smart3804dc82010-07-14 15:31:37 -040012996 "bmask\n", fcf_index);
12997
James Smart0c9ab6f2010-02-26 14:15:57 -050012998 return 0;
12999}
13000
13001/**
James Smart3804dc82010-07-14 15:31:37 -040013002 * lpfc_sli4_fcf_rr_index_clear - Clear bmask from eligible fcf record index
James Smart0c9ab6f2010-02-26 14:15:57 -050013003 * @phba: pointer to lpfc hba data structure.
13004 *
13005 * This routine clears the FCF record index from the eligible bmask for
James Smarta93ff372010-10-22 11:06:08 -040013006 * roundrobin failover search. It checks to make sure that the index
James Smart0c9ab6f2010-02-26 14:15:57 -050013007 * does not go beyond the range of the driver allocated bmask dimension
13008 * before clearing the bit.
13009 **/
13010void
13011lpfc_sli4_fcf_rr_index_clear(struct lpfc_hba *phba, uint16_t fcf_index)
13012{
13013 if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
13014 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040013015 "2762 FCF (x%x) reached driver's book "
13016 "keeping dimension:x%x\n",
James Smart0c9ab6f2010-02-26 14:15:57 -050013017 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
13018 return;
13019 }
13020 /* Clear the eligible FCF record index bmask */
13021 clear_bit(fcf_index, phba->fcf.fcf_rr_bmask);
James Smart3804dc82010-07-14 15:31:37 -040013022
13023 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040013024 "2791 Clear FCF (x%x) from roundrobin failover "
James Smart3804dc82010-07-14 15:31:37 -040013025 "bmask\n", fcf_index);
James Smart0c9ab6f2010-02-26 14:15:57 -050013026}
13027
13028/**
James Smartecfd03c2010-02-12 14:41:27 -050013029 * lpfc_mbx_cmpl_redisc_fcf_table - completion routine for rediscover FCF table
13030 * @phba: pointer to lpfc hba data structure.
13031 *
13032 * This routine is the completion routine for the rediscover FCF table mailbox
13033 * command. If the mailbox command returned failure, it will try to stop the
13034 * FCF rediscover wait timer.
13035 **/
13036void
13037lpfc_mbx_cmpl_redisc_fcf_table(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
13038{
13039 struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
13040 uint32_t shdr_status, shdr_add_status;
13041
13042 redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
13043
13044 shdr_status = bf_get(lpfc_mbox_hdr_status,
13045 &redisc_fcf->header.cfg_shdr.response);
13046 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
13047 &redisc_fcf->header.cfg_shdr.response);
13048 if (shdr_status || shdr_add_status) {
James Smart0c9ab6f2010-02-26 14:15:57 -050013049 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smartecfd03c2010-02-12 14:41:27 -050013050 "2746 Requesting for FCF rediscovery failed "
13051 "status x%x add_status x%x\n",
13052 shdr_status, shdr_add_status);
James Smart0c9ab6f2010-02-26 14:15:57 -050013053 if (phba->fcf.fcf_flag & FCF_ACVL_DISC) {
James Smartfc2b9892010-02-26 14:15:29 -050013054 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -050013055 phba->fcf.fcf_flag &= ~FCF_ACVL_DISC;
James Smartfc2b9892010-02-26 14:15:29 -050013056 spin_unlock_irq(&phba->hbalock);
13057 /*
13058 * CVL event triggered FCF rediscover request failed,
13059 * last resort to re-try current registered FCF entry.
13060 */
13061 lpfc_retry_pport_discovery(phba);
13062 } else {
13063 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -050013064 phba->fcf.fcf_flag &= ~FCF_DEAD_DISC;
James Smartfc2b9892010-02-26 14:15:29 -050013065 spin_unlock_irq(&phba->hbalock);
13066 /*
13067 * DEAD FCF event triggered FCF rediscover request
13068 * failed, last resort to fail over as a link down
13069 * to FCF registration.
13070 */
13071 lpfc_sli4_fcf_dead_failthrough(phba);
13072 }
James Smart0c9ab6f2010-02-26 14:15:57 -050013073 } else {
13074 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040013075 "2775 Start FCF rediscover quiescent timer\n");
James Smartecfd03c2010-02-12 14:41:27 -050013076 /*
13077 * Start FCF rediscovery wait timer for pending FCF
13078 * before rescan FCF record table.
13079 */
13080 lpfc_fcf_redisc_wait_start_timer(phba);
James Smart0c9ab6f2010-02-26 14:15:57 -050013081 }
James Smartecfd03c2010-02-12 14:41:27 -050013082
13083 mempool_free(mbox, phba->mbox_mem_pool);
13084}
13085
13086/**
James Smart3804dc82010-07-14 15:31:37 -040013087 * lpfc_sli4_redisc_fcf_table - Request to rediscover entire FCF table by port.
James Smartecfd03c2010-02-12 14:41:27 -050013088 * @phba: pointer to lpfc hba data structure.
13089 *
13090 * This routine is invoked to request for rediscovery of the entire FCF table
13091 * by the port.
13092 **/
13093int
13094lpfc_sli4_redisc_fcf_table(struct lpfc_hba *phba)
13095{
13096 LPFC_MBOXQ_t *mbox;
13097 struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
13098 int rc, length;
13099
James Smart0c9ab6f2010-02-26 14:15:57 -050013100 /* Cancel retry delay timers to all vports before FCF rediscover */
13101 lpfc_cancel_all_vport_retry_delay_timer(phba);
13102
James Smartecfd03c2010-02-12 14:41:27 -050013103 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
13104 if (!mbox) {
13105 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13106 "2745 Failed to allocate mbox for "
13107 "requesting FCF rediscover.\n");
13108 return -ENOMEM;
13109 }
13110
13111 length = (sizeof(struct lpfc_mbx_redisc_fcf_tbl) -
13112 sizeof(struct lpfc_sli4_cfg_mhdr));
13113 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
13114 LPFC_MBOX_OPCODE_FCOE_REDISCOVER_FCF,
13115 length, LPFC_SLI4_MBX_EMBED);
13116
13117 redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
13118 /* Set count to 0 for invalidating the entire FCF database */
13119 bf_set(lpfc_mbx_redisc_fcf_count, redisc_fcf, 0);
13120
13121 /* Issue the mailbox command asynchronously */
13122 mbox->vport = phba->pport;
13123 mbox->mbox_cmpl = lpfc_mbx_cmpl_redisc_fcf_table;
13124 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
13125
13126 if (rc == MBX_NOT_FINISHED) {
13127 mempool_free(mbox, phba->mbox_mem_pool);
13128 return -EIO;
13129 }
13130 return 0;
13131}
13132
13133/**
James Smartfc2b9892010-02-26 14:15:29 -050013134 * lpfc_sli4_fcf_dead_failthrough - Failthrough routine to fcf dead event
13135 * @phba: pointer to lpfc hba data structure.
13136 *
13137 * This function is the failover routine as a last resort to the FCF DEAD
13138 * event when driver failed to perform fast FCF failover.
13139 **/
13140void
13141lpfc_sli4_fcf_dead_failthrough(struct lpfc_hba *phba)
13142{
13143 uint32_t link_state;
13144
13145 /*
13146 * Last resort as FCF DEAD event failover will treat this as
13147 * a link down, but save the link state because we don't want
13148 * it to be changed to Link Down unless it is already down.
13149 */
13150 link_state = phba->link_state;
13151 lpfc_linkdown(phba);
13152 phba->link_state = link_state;
13153
13154 /* Unregister FCF if no devices connected to it */
13155 lpfc_unregister_unused_fcf(phba);
13156}
13157
13158/**
James Smarta0c87cb2009-07-19 10:01:10 -040013159 * lpfc_sli_read_link_ste - Read region 23 to decide if link is disabled.
13160 * @phba: pointer to lpfc hba data structure.
13161 *
13162 * This function read region 23 and parse TLV for port status to
13163 * decide if the user disaled the port. If the TLV indicates the
13164 * port is disabled, the hba_flag is set accordingly.
13165 **/
13166void
13167lpfc_sli_read_link_ste(struct lpfc_hba *phba)
13168{
13169 LPFC_MBOXQ_t *pmb = NULL;
13170 MAILBOX_t *mb;
13171 uint8_t *rgn23_data = NULL;
13172 uint32_t offset = 0, data_size, sub_tlv_len, tlv_offset;
13173 int rc;
13174
13175 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
13176 if (!pmb) {
13177 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13178 "2600 lpfc_sli_read_serdes_param failed to"
13179 " allocate mailbox memory\n");
13180 goto out;
13181 }
13182 mb = &pmb->u.mb;
13183
13184 /* Get adapter Region 23 data */
13185 rgn23_data = kzalloc(DMP_RGN23_SIZE, GFP_KERNEL);
13186 if (!rgn23_data)
13187 goto out;
13188
13189 do {
13190 lpfc_dump_mem(phba, pmb, offset, DMP_REGION_23);
13191 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
13192
13193 if (rc != MBX_SUCCESS) {
13194 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
13195 "2601 lpfc_sli_read_link_ste failed to"
13196 " read config region 23 rc 0x%x Status 0x%x\n",
13197 rc, mb->mbxStatus);
13198 mb->un.varDmp.word_cnt = 0;
13199 }
13200 /*
13201 * dump mem may return a zero when finished or we got a
13202 * mailbox error, either way we are done.
13203 */
13204 if (mb->un.varDmp.word_cnt == 0)
13205 break;
13206 if (mb->un.varDmp.word_cnt > DMP_RGN23_SIZE - offset)
13207 mb->un.varDmp.word_cnt = DMP_RGN23_SIZE - offset;
13208
13209 lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
13210 rgn23_data + offset,
13211 mb->un.varDmp.word_cnt);
13212 offset += mb->un.varDmp.word_cnt;
13213 } while (mb->un.varDmp.word_cnt && offset < DMP_RGN23_SIZE);
13214
13215 data_size = offset;
13216 offset = 0;
13217
13218 if (!data_size)
13219 goto out;
13220
13221 /* Check the region signature first */
13222 if (memcmp(&rgn23_data[offset], LPFC_REGION23_SIGNATURE, 4)) {
13223 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13224 "2619 Config region 23 has bad signature\n");
13225 goto out;
13226 }
13227 offset += 4;
13228
13229 /* Check the data structure version */
13230 if (rgn23_data[offset] != LPFC_REGION23_VERSION) {
13231 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13232 "2620 Config region 23 has bad version\n");
13233 goto out;
13234 }
13235 offset += 4;
13236
13237 /* Parse TLV entries in the region */
13238 while (offset < data_size) {
13239 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC)
13240 break;
13241 /*
13242 * If the TLV is not driver specific TLV or driver id is
13243 * not linux driver id, skip the record.
13244 */
13245 if ((rgn23_data[offset] != DRIVER_SPECIFIC_TYPE) ||
13246 (rgn23_data[offset + 2] != LINUX_DRIVER_ID) ||
13247 (rgn23_data[offset + 3] != 0)) {
13248 offset += rgn23_data[offset + 1] * 4 + 4;
13249 continue;
13250 }
13251
13252 /* Driver found a driver specific TLV in the config region */
13253 sub_tlv_len = rgn23_data[offset + 1] * 4;
13254 offset += 4;
13255 tlv_offset = 0;
13256
13257 /*
13258 * Search for configured port state sub-TLV.
13259 */
13260 while ((offset < data_size) &&
13261 (tlv_offset < sub_tlv_len)) {
13262 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC) {
13263 offset += 4;
13264 tlv_offset += 4;
13265 break;
13266 }
13267 if (rgn23_data[offset] != PORT_STE_TYPE) {
13268 offset += rgn23_data[offset + 1] * 4 + 4;
13269 tlv_offset += rgn23_data[offset + 1] * 4 + 4;
13270 continue;
13271 }
13272
13273 /* This HBA contains PORT_STE configured */
13274 if (!rgn23_data[offset + 2])
13275 phba->hba_flag |= LINK_DISABLED;
13276
13277 goto out;
13278 }
13279 }
13280out:
13281 if (pmb)
13282 mempool_free(pmb, phba->mbox_mem_pool);
13283 kfree(rgn23_data);
13284 return;
13285}
James Smart695a8142010-01-26 23:08:03 -050013286
13287/**
13288 * lpfc_cleanup_pending_mbox - Free up vport discovery mailbox commands.
13289 * @vport: pointer to vport data structure.
13290 *
13291 * This function iterate through the mailboxq and clean up all REG_LOGIN
13292 * and REG_VPI mailbox commands associated with the vport. This function
13293 * is called when driver want to restart discovery of the vport due to
13294 * a Clear Virtual Link event.
13295 **/
13296void
13297lpfc_cleanup_pending_mbox(struct lpfc_vport *vport)
13298{
13299 struct lpfc_hba *phba = vport->phba;
13300 LPFC_MBOXQ_t *mb, *nextmb;
13301 struct lpfc_dmabuf *mp;
James Smart78730cf2010-04-06 15:06:30 -040013302 struct lpfc_nodelist *ndlp;
James Smartd439d282010-09-29 11:18:45 -040013303 struct lpfc_nodelist *act_mbx_ndlp = NULL;
James Smart589a52d2010-07-14 15:30:54 -040013304 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smartd439d282010-09-29 11:18:45 -040013305 LIST_HEAD(mbox_cmd_list);
James Smart63e801c2010-11-20 23:14:19 -050013306 uint8_t restart_loop;
James Smart695a8142010-01-26 23:08:03 -050013307
James Smartd439d282010-09-29 11:18:45 -040013308 /* Clean up internally queued mailbox commands with the vport */
James Smart695a8142010-01-26 23:08:03 -050013309 spin_lock_irq(&phba->hbalock);
13310 list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
13311 if (mb->vport != vport)
13312 continue;
13313
13314 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
13315 (mb->u.mb.mbxCommand != MBX_REG_VPI))
13316 continue;
13317
James Smartd439d282010-09-29 11:18:45 -040013318 list_del(&mb->list);
13319 list_add_tail(&mb->list, &mbox_cmd_list);
13320 }
13321 /* Clean up active mailbox command with the vport */
13322 mb = phba->sli.mbox_active;
13323 if (mb && (mb->vport == vport)) {
13324 if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) ||
13325 (mb->u.mb.mbxCommand == MBX_REG_VPI))
13326 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
13327 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
13328 act_mbx_ndlp = (struct lpfc_nodelist *)mb->context2;
13329 /* Put reference count for delayed processing */
13330 act_mbx_ndlp = lpfc_nlp_get(act_mbx_ndlp);
13331 /* Unregister the RPI when mailbox complete */
13332 mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
13333 }
13334 }
James Smart63e801c2010-11-20 23:14:19 -050013335 /* Cleanup any mailbox completions which are not yet processed */
13336 do {
13337 restart_loop = 0;
13338 list_for_each_entry(mb, &phba->sli.mboxq_cmpl, list) {
13339 /*
13340 * If this mailox is already processed or it is
13341 * for another vport ignore it.
13342 */
13343 if ((mb->vport != vport) ||
13344 (mb->mbox_flag & LPFC_MBX_IMED_UNREG))
13345 continue;
13346
13347 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
13348 (mb->u.mb.mbxCommand != MBX_REG_VPI))
13349 continue;
13350
13351 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
13352 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
13353 ndlp = (struct lpfc_nodelist *)mb->context2;
13354 /* Unregister the RPI when mailbox complete */
13355 mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
13356 restart_loop = 1;
13357 spin_unlock_irq(&phba->hbalock);
13358 spin_lock(shost->host_lock);
13359 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
13360 spin_unlock(shost->host_lock);
13361 spin_lock_irq(&phba->hbalock);
13362 break;
13363 }
13364 }
13365 } while (restart_loop);
13366
James Smartd439d282010-09-29 11:18:45 -040013367 spin_unlock_irq(&phba->hbalock);
13368
13369 /* Release the cleaned-up mailbox commands */
13370 while (!list_empty(&mbox_cmd_list)) {
13371 list_remove_head(&mbox_cmd_list, mb, LPFC_MBOXQ_t, list);
James Smart695a8142010-01-26 23:08:03 -050013372 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
13373 mp = (struct lpfc_dmabuf *) (mb->context1);
13374 if (mp) {
13375 __lpfc_mbuf_free(phba, mp->virt, mp->phys);
13376 kfree(mp);
13377 }
James Smart78730cf2010-04-06 15:06:30 -040013378 ndlp = (struct lpfc_nodelist *) mb->context2;
James Smartd439d282010-09-29 11:18:45 -040013379 mb->context2 = NULL;
James Smart78730cf2010-04-06 15:06:30 -040013380 if (ndlp) {
Dan Carpenterec21b3b2010-08-08 00:15:17 +020013381 spin_lock(shost->host_lock);
James Smart589a52d2010-07-14 15:30:54 -040013382 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
Dan Carpenterec21b3b2010-08-08 00:15:17 +020013383 spin_unlock(shost->host_lock);
James Smart78730cf2010-04-06 15:06:30 -040013384 lpfc_nlp_put(ndlp);
James Smart78730cf2010-04-06 15:06:30 -040013385 }
James Smart695a8142010-01-26 23:08:03 -050013386 }
James Smart695a8142010-01-26 23:08:03 -050013387 mempool_free(mb, phba->mbox_mem_pool);
13388 }
James Smartd439d282010-09-29 11:18:45 -040013389
13390 /* Release the ndlp with the cleaned-up active mailbox command */
13391 if (act_mbx_ndlp) {
13392 spin_lock(shost->host_lock);
13393 act_mbx_ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
13394 spin_unlock(shost->host_lock);
13395 lpfc_nlp_put(act_mbx_ndlp);
James Smart695a8142010-01-26 23:08:03 -050013396 }
James Smart695a8142010-01-26 23:08:03 -050013397}
13398
James Smart2a9bf3d2010-06-07 15:24:45 -040013399/**
13400 * lpfc_drain_txq - Drain the txq
13401 * @phba: Pointer to HBA context object.
13402 *
13403 * This function attempt to submit IOCBs on the txq
13404 * to the adapter. For SLI4 adapters, the txq contains
13405 * ELS IOCBs that have been deferred because the there
13406 * are no SGLs. This congestion can occur with large
13407 * vport counts during node discovery.
13408 **/
13409
13410uint32_t
13411lpfc_drain_txq(struct lpfc_hba *phba)
13412{
13413 LIST_HEAD(completions);
13414 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
13415 struct lpfc_iocbq *piocbq = 0;
13416 unsigned long iflags = 0;
13417 char *fail_msg = NULL;
13418 struct lpfc_sglq *sglq;
13419 union lpfc_wqe wqe;
13420
13421 spin_lock_irqsave(&phba->hbalock, iflags);
13422 if (pring->txq_cnt > pring->txq_max)
13423 pring->txq_max = pring->txq_cnt;
13424
13425 spin_unlock_irqrestore(&phba->hbalock, iflags);
13426
13427 while (pring->txq_cnt) {
13428 spin_lock_irqsave(&phba->hbalock, iflags);
13429
James Smart19ca7602010-11-20 23:11:55 -050013430 piocbq = lpfc_sli_ringtx_get(phba, pring);
13431 sglq = __lpfc_sli_get_sglq(phba, piocbq);
James Smart2a9bf3d2010-06-07 15:24:45 -040013432 if (!sglq) {
James Smart19ca7602010-11-20 23:11:55 -050013433 __lpfc_sli_ringtx_put(phba, pring, piocbq);
James Smart2a9bf3d2010-06-07 15:24:45 -040013434 spin_unlock_irqrestore(&phba->hbalock, iflags);
13435 break;
13436 } else {
James Smart2a9bf3d2010-06-07 15:24:45 -040013437 if (!piocbq) {
13438 /* The txq_cnt out of sync. This should
13439 * never happen
13440 */
13441 sglq = __lpfc_clear_active_sglq(phba,
13442 sglq->sli4_xritag);
13443 spin_unlock_irqrestore(&phba->hbalock, iflags);
13444 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13445 "2823 txq empty and txq_cnt is %d\n ",
13446 pring->txq_cnt);
13447 break;
13448 }
13449 }
13450
13451 /* The xri and iocb resources secured,
13452 * attempt to issue request
13453 */
13454 piocbq->sli4_xritag = sglq->sli4_xritag;
13455 if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocbq, sglq))
13456 fail_msg = "to convert bpl to sgl";
13457 else if (lpfc_sli4_iocb2wqe(phba, piocbq, &wqe))
13458 fail_msg = "to convert iocb to wqe";
13459 else if (lpfc_sli4_wq_put(phba->sli4_hba.els_wq, &wqe))
13460 fail_msg = " - Wq is full";
13461 else
13462 lpfc_sli_ringtxcmpl_put(phba, pring, piocbq);
13463
13464 if (fail_msg) {
13465 /* Failed means we can't issue and need to cancel */
13466 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13467 "2822 IOCB failed %s iotag 0x%x "
13468 "xri 0x%x\n",
13469 fail_msg,
13470 piocbq->iotag, piocbq->sli4_xritag);
13471 list_add_tail(&piocbq->list, &completions);
13472 }
13473 spin_unlock_irqrestore(&phba->hbalock, iflags);
13474 }
13475
James Smart2a9bf3d2010-06-07 15:24:45 -040013476 /* Cancel all the IOCBs that cannot be issued */
13477 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
13478 IOERR_SLI_ABORTED);
13479
13480 return pring->txq_cnt;
13481}